]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/server.go
Refactor and backfill tests for tracker listing
[rbdr/mobius] / hotline / server.go
index e5a7b8eb08a4ed8be7c0fce1555e7839264a8587..392c8f31ac47364f356f77614585753d260d048d 100644 (file)
@@ -43,12 +43,10 @@ const (
 var nostalgiaVersion = []byte{0, 0, 2, 0x2c} // version ID used by the Nostalgia client
 
 type Server struct {
 var nostalgiaVersion = []byte{0, 0, 2, 0x2c} // version ID used by the Nostalgia client
 
 type Server struct {
-       Port         int
-       Accounts     map[string]*Account
-       Agreement    []byte
-       Clients      map[uint16]*ClientConn
-       ThreadedNews *ThreadedNews
-
+       Port          int
+       Accounts      map[string]*Account
+       Agreement     []byte
+       Clients       map[uint16]*ClientConn
        fileTransfers map[[4]byte]*FileTransfer
 
        Config        *Config
        fileTransfers map[[4]byte]*FileTransfer
 
        Config        *Config
@@ -64,6 +62,9 @@ type Server struct {
        outbox chan Transaction
        mux    sync.Mutex
 
        outbox chan Transaction
        mux    sync.Mutex
 
+       threadedNewsMux sync.Mutex
+       ThreadedNews    *ThreadedNews
+
        flatNewsMux sync.Mutex
        FlatNews    []byte
 
        flatNewsMux sync.Mutex
        FlatNews    []byte
 
@@ -308,16 +309,16 @@ func (s *Server) keepaliveHandler() {
                        if c.IdleTime > userIdleSeconds && !c.Idle {
                                c.Idle = true
 
                        if c.IdleTime > userIdleSeconds && !c.Idle {
                                c.Idle = true
 
-                               flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*c.Flags)))
+                               flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(c.Flags)))
                                flagBitmap.SetBit(flagBitmap, userFlagAway, 1)
                                flagBitmap.SetBit(flagBitmap, userFlagAway, 1)
-                               binary.BigEndian.PutUint16(*c.Flags, uint16(flagBitmap.Int64()))
+                               binary.BigEndian.PutUint16(c.Flags, uint16(flagBitmap.Int64()))
 
                                c.sendAll(
                                        tranNotifyChangeUser,
                                        NewField(fieldUserID, *c.ID),
 
                                c.sendAll(
                                        tranNotifyChangeUser,
                                        NewField(fieldUserID, *c.ID),
-                                       NewField(fieldUserFlags, *c.Flags),
+                                       NewField(fieldUserFlags, c.Flags),
                                        NewField(fieldUserName, c.UserName),
                                        NewField(fieldUserName, c.UserName),
-                                       NewField(fieldUserIconID, *c.Icon),
+                                       NewField(fieldUserIconID, c.Icon),
                                )
                        }
                }
                                )
                        }
                }
@@ -342,14 +343,14 @@ func (s *Server) writeBanList() error {
 }
 
 func (s *Server) writeThreadedNews() error {
 }
 
 func (s *Server) writeThreadedNews() error {
-       s.mux.Lock()
-       defer s.mux.Unlock()
+       s.threadedNewsMux.Lock()
+       defer s.threadedNewsMux.Unlock()
 
        out, err := yaml.Marshal(s.ThreadedNews)
        if err != nil {
                return err
        }
 
        out, err := yaml.Marshal(s.ThreadedNews)
        if err != nil {
                return err
        }
-       err = ioutil.WriteFile(
+       err = s.FS.WriteFile(
                filepath.Join(s.ConfigDir, "ThreadedNews.yaml"),
                out,
                0666,
                filepath.Join(s.ConfigDir, "ThreadedNews.yaml"),
                out,
                0666,
@@ -363,12 +364,12 @@ func (s *Server) NewClientConn(conn io.ReadWriteCloser, remoteAddr string) *Clie
 
        clientConn := &ClientConn{
                ID:         &[]byte{0, 0},
 
        clientConn := &ClientConn{
                ID:         &[]byte{0, 0},
-               Icon:       &[]byte{0, 0},
-               Flags:      &[]byte{0, 0},
+               Icon:       []byte{0, 0},
+               Flags:      []byte{0, 0},
                UserName:   []byte{},
                Connection: conn,
                Server:     s,
                UserName:   []byte{},
                Connection: conn,
                Server:     s,
-               Version:    &[]byte{},
+               Version:    []byte{},
                AutoReply:  []byte{},
                transfers:  map[int]map[[4]byte]*FileTransfer{},
                Agreed:     false,
                AutoReply:  []byte{},
                transfers:  map[int]map[[4]byte]*FileTransfer{},
                Agreed:     false,
@@ -392,7 +393,7 @@ func (s *Server) NewClientConn(conn io.ReadWriteCloser, remoteAddr string) *Clie
 }
 
 // NewUser creates a new user account entry in the server map and config file
 }
 
 // NewUser creates a new user account entry in the server map and config file
-func (s *Server) NewUser(login, name, password string, access []byte) error {
+func (s *Server) NewUser(login, name, password string, access accessBitmap) error {
        s.mux.Lock()
        defer s.mux.Unlock()
 
        s.mux.Lock()
        defer s.mux.Unlock()
 
@@ -400,7 +401,7 @@ func (s *Server) NewUser(login, name, password string, access []byte) error {
                Login:    login,
                Name:     name,
                Password: hashAndSalt([]byte(password)),
                Login:    login,
                Name:     name,
                Password: hashAndSalt([]byte(password)),
-               Access:   &access,
+               Access:   access,
        }
        out, err := yaml.Marshal(&account)
        if err != nil {
        }
        out, err := yaml.Marshal(&account)
        if err != nil {
@@ -411,7 +412,7 @@ func (s *Server) NewUser(login, name, password string, access []byte) error {
        return s.FS.WriteFile(filepath.Join(s.ConfigDir, "Users", login+".yaml"), out, 0666)
 }
 
        return s.FS.WriteFile(filepath.Join(s.ConfigDir, "Users", login+".yaml"), out, 0666)
 }
 
-func (s *Server) UpdateUser(login, newLogin, name, password string, access []byte) error {
+func (s *Server) UpdateUser(login, newLogin, name, password string, access accessBitmap) error {
        s.mux.Lock()
        defer s.mux.Unlock()
 
        s.mux.Lock()
        defer s.mux.Unlock()
 
@@ -426,7 +427,7 @@ func (s *Server) UpdateUser(login, newLogin, name, password string, access []byt
        }
 
        account := s.Accounts[newLogin]
        }
 
        account := s.Accounts[newLogin]
-       account.Access = &access
+       account.Access = access
        account.Name = name
        account.Password = password
 
        account.Name = name
        account.Password = password
 
@@ -463,8 +464,8 @@ func (s *Server) connectedUsers() []Field {
                }
                user := User{
                        ID:    *c.ID,
                }
                user := User{
                        ID:    *c.ID,
-                       Icon:  *c.Icon,
-                       Flags: *c.Flags,
+                       Icon:  c.Icon,
+                       Flags: c.Flags,
                        Name:  string(c.UserName),
                }
                connectedUsers = append(connectedUsers, NewField(fieldUsernameWithInfo, user.Payload()))
                        Name:  string(c.UserName),
                }
                connectedUsers = append(connectedUsers, NewField(fieldUsernameWithInfo, user.Payload()))
@@ -598,7 +599,7 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser
 
        encodedLogin := clientLogin.GetField(fieldUserLogin).Data
        encodedPassword := clientLogin.GetField(fieldUserPassword).Data
 
        encodedLogin := clientLogin.GetField(fieldUserLogin).Data
        encodedPassword := clientLogin.GetField(fieldUserPassword).Data
-       *c.Version = clientLogin.GetField(fieldVersion).Data
+       c.Version = clientLogin.GetField(fieldVersion).Data
 
        var login string
        for _, char := range encodedLogin {
 
        var login string
        for _, char := range encodedLogin {
@@ -621,11 +622,17 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser
                        return err
                }
 
                        return err
                }
 
-               c.logger.Infow("Login failed", "clientVersion", fmt.Sprintf("%x", *c.Version))
+               c.logger.Infow("Login failed", "clientVersion", fmt.Sprintf("%x", c.Version))
 
                return nil
        }
 
 
                return nil
        }
 
+       if clientLogin.GetField(fieldUserIconID).Data != nil {
+               c.Icon = clientLogin.GetField(fieldUserIconID).Data
+       }
+
+       c.Account = c.Server.Accounts[login]
+
        if clientLogin.GetField(fieldUserName).Data != nil {
                if c.Authorize(accessAnyName) {
                        c.UserName = clientLogin.GetField(fieldUserName).Data
        if clientLogin.GetField(fieldUserName).Data != nil {
                if c.Authorize(accessAnyName) {
                        c.UserName = clientLogin.GetField(fieldUserName).Data
@@ -634,14 +641,8 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser
                }
        }
 
                }
        }
 
-       if clientLogin.GetField(fieldUserIconID).Data != nil {
-               *c.Icon = clientLogin.GetField(fieldUserIconID).Data
-       }
-
-       c.Account = c.Server.Accounts[login]
-
        if c.Authorize(accessDisconUser) {
        if c.Authorize(accessDisconUser) {
-               *c.Flags = []byte{0, 2}
+               c.Flags = []byte{0, 2}
        }
 
        s.outbox <- c.NewReply(clientLogin,
        }
 
        s.outbox <- c.NewReply(clientLogin,
@@ -651,24 +652,33 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser
        )
 
        // Send user access privs so client UI knows how to behave
        )
 
        // Send user access privs so client UI knows how to behave
-       c.Server.outbox <- *NewTransaction(tranUserAccess, c.ID, NewField(fieldUserAccess, *c.Account.Access))
-
-       // Show agreement to client
-       c.Server.outbox <- *NewTransaction(tranShowAgreement, c.ID, NewField(fieldData, s.Agreement))
+       c.Server.outbox <- *NewTransaction(tranUserAccess, c.ID, NewField(fieldUserAccess, c.Account.Access[:]))
+
+       // Accounts with accessNoAgreement do not receive the server agreement on login.  The behavior is different between
+       // client versions.  For 1.2.3 client, we do not send tranShowAgreement.  For other client versions, we send
+       // tranShowAgreement but with the NoServerAgreement field set to 1.
+       if c.Authorize(accessNoAgreement) {
+               // If client version is nil, then the client uses the 1.2.3 login behavior
+               if c.Version != nil {
+                       c.Server.outbox <- *NewTransaction(tranShowAgreement, c.ID, NewField(fieldNoServerAgreement, []byte{1}))
+               }
+       } else {
+               c.Server.outbox <- *NewTransaction(tranShowAgreement, c.ID, NewField(fieldData, s.Agreement))
+       }
 
        // Used simplified hotline v1.2.3 login flow for clients that do not send login info in tranAgreed
 
        // Used simplified hotline v1.2.3 login flow for clients that do not send login info in tranAgreed
-       if *c.Version == nil || bytes.Equal(*c.Version, nostalgiaVersion) {
+       if c.Version == nil || bytes.Equal(c.Version, nostalgiaVersion) {
                c.Agreed = true
                c.logger = c.logger.With("name", string(c.UserName))
                c.Agreed = true
                c.logger = c.logger.With("name", string(c.UserName))
-               c.logger.Infow("Login successful", "clientVersion", fmt.Sprintf("%x", *c.Version))
+               c.logger.Infow("Login successful", "clientVersion", fmt.Sprintf("%v", func() int { i, _ := byteToInt(c.Version); return i }()))
 
                for _, t := range c.notifyOthers(
                        *NewTransaction(
                                tranNotifyChangeUser, nil,
                                NewField(fieldUserName, c.UserName),
                                NewField(fieldUserID, *c.ID),
 
                for _, t := range c.notifyOthers(
                        *NewTransaction(
                                tranNotifyChangeUser, nil,
                                NewField(fieldUserName, c.UserName),
                                NewField(fieldUserID, *c.ID),
-                               NewField(fieldUserIconID, *c.Icon),
-                               NewField(fieldUserFlags, *c.Flags),
+                               NewField(fieldUserIconID, c.Icon),
+                               NewField(fieldUserFlags, c.Flags),
                        ),
                ) {
                        c.Server.outbox <- t
                        ),
                ) {
                        c.Server.outbox <- t