]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/server.go
Add tests and clean up
[rbdr/mobius] / hotline / server.go
index 0203dc9e67d4b02230ba58fb9739010d83fffaba..8e0fb5086056bcb0b3ba60e000bbaec867f409ce 100644 (file)
@@ -33,7 +33,6 @@ const (
 )
 
 type Server struct {
 )
 
 type Server struct {
-       Interface     string
        Port          int
        Accounts      map[string]*Account
        Agreement     []byte
        Port          int
        Accounts      map[string]*Account
        Agreement     []byte
@@ -115,13 +114,17 @@ func (s *Server) sendTransaction(t Transaction) error {
        if client == nil {
                return errors.New("invalid client")
        }
        if client == nil {
                return errors.New("invalid client")
        }
-       userName := string(*client.UserName)
+       userName := string(client.UserName)
        login := client.Account.Login
 
        handler := TransactionHandlers[requestNum]
 
        login := client.Account.Login
 
        handler := TransactionHandlers[requestNum]
 
+       b, err := t.MarshalBinary()
+       if err != nil {
+               return err
+       }
        var n int
        var n int
-       if n, err = client.Connection.Write(t.Payload()); err != nil {
+       if n, err = client.Connection.Write(b); err != nil {
                return err
        }
        s.Logger.Debugw("Sent Transaction",
                return err
        }
        s.Logger.Debugw("Sent Transaction",
@@ -168,8 +171,6 @@ func (s *Server) Serve(ctx context.Context, cancelRoot context.CancelFunc, ln ne
 
 const (
        agreementFile    = "Agreement.txt"
 
 const (
        agreementFile    = "Agreement.txt"
-       messageBoardFile = "MessageBoard.txt"
-       threadedNewsFile = "ThreadedNews.yaml"
 )
 
 // NewServer constructs a new Server from a config dir
 )
 
 // NewServer constructs a new Server from a config dir
@@ -290,7 +291,7 @@ func (s *Server) keepaliveHandler() {
                                        tranNotifyChangeUser,
                                        NewField(fieldUserID, *c.ID),
                                        NewField(fieldUserFlags, *c.Flags),
                                        tranNotifyChangeUser,
                                        NewField(fieldUserID, *c.ID),
                                        NewField(fieldUserFlags, *c.Flags),
-                                       NewField(fieldUserName, *c.UserName),
+                                       NewField(fieldUserName, c.UserName),
                                        NewField(fieldUserIconID, *c.Icon),
                                )
                        }
                                        NewField(fieldUserIconID, *c.Icon),
                                )
                        }
@@ -323,7 +324,7 @@ func (s *Server) NewClientConn(conn net.Conn) *ClientConn {
                ID:         &[]byte{0, 0},
                Icon:       &[]byte{0, 0},
                Flags:      &[]byte{0, 0},
                ID:         &[]byte{0, 0},
                Icon:       &[]byte{0, 0},
                Flags:      &[]byte{0, 0},
-               UserName:   &[]byte{},
+               UserName:   []byte{},
                Connection: conn,
                Server:     s,
                Version:    &[]byte{},
                Connection: conn,
                Server:     s,
                Version:    &[]byte{},
@@ -382,7 +383,7 @@ func (s *Server) connectedUsers() []Field {
                        ID:    *c.ID,
                        Icon:  *c.Icon,
                        Flags: *c.Flags,
                        ID:    *c.ID,
                        Icon:  *c.Icon,
                        Flags: *c.Flags,
-                       Name:  string(*c.UserName),
+                       Name:  string(c.UserName),
                }
                connectedUsers = append(connectedUsers, NewField(fieldUsernameWithInfo, user.Payload()))
        }
                }
                connectedUsers = append(connectedUsers, NewField(fieldUsernameWithInfo, user.Payload()))
        }
@@ -497,15 +498,19 @@ func (s *Server) handleNewConnection(conn net.Conn) error {
 
        // If authentication fails, send error reply and close connection
        if !c.Authenticate(login, encodedPassword) {
 
        // If authentication fails, send error reply and close connection
        if !c.Authenticate(login, encodedPassword) {
-               rep := c.NewErrReply(clientLogin, "Incorrect login.")
-               if _, err := conn.Write(rep.Payload()); err != nil {
+               t := c.NewErrReply(clientLogin, "Incorrect login.")
+               b, err := t.MarshalBinary()
+               if err != nil {
+                       return err
+               }
+               if _, err := conn.Write(b); err != nil {
                        return err
                }
                return fmt.Errorf("incorrect login")
        }
 
        if clientLogin.GetField(fieldUserName).Data != nil {
                        return err
                }
                return fmt.Errorf("incorrect login")
        }
 
        if clientLogin.GetField(fieldUserName).Data != nil {
-               *c.UserName = clientLogin.GetField(fieldUserName).Data
+               c.UserName = clientLogin.GetField(fieldUserName).Data
        }
 
        if clientLogin.GetField(fieldUserIconID).Data != nil {
        }
 
        if clientLogin.GetField(fieldUserIconID).Data != nil {
@@ -753,7 +758,8 @@ func (s *Server) TransferFile(conn net.Conn) error {
                //
                // This notifies the server to send the next item header
 
                //
                // This notifies the server to send the next item header
 
-               fh := NewFilePath(fileTransfer.FilePath)
+               var fh FilePath
+               _ = fh.UnmarshalBinary(fileTransfer.FilePath)
                fullFilePath := fmt.Sprintf("%v/%v", s.Config.FileRoot+fh.String(), string(fileTransfer.FileName))
 
                basePathLen := len(fullFilePath)
                fullFilePath := fmt.Sprintf("%v/%v", s.Config.FileRoot+fh.String(), string(fileTransfer.FileName))
 
                basePathLen := len(fullFilePath)
@@ -765,7 +771,7 @@ func (s *Server) TransferFile(conn net.Conn) error {
                i := 0
                _ = filepath.Walk(fullFilePath+"/", func(path string, info os.FileInfo, _ error) error {
                        i += 1
                i := 0
                _ = filepath.Walk(fullFilePath+"/", func(path string, info os.FileInfo, _ error) error {
                        i += 1
-                       subPath := path[basePathLen-2:]
+                       subPath := path[basePathLen:]
                        s.Logger.Infow("Sending fileheader", "i", i, "path", path, "fullFilePath", fullFilePath, "subPath", subPath, "IsDir", info.IsDir())
 
                        fileHeader := NewFileHeader(subPath, info.IsDir())
                        s.Logger.Infow("Sending fileheader", "i", i, "path", path, "fullFilePath", fullFilePath, "subPath", subPath, "IsDir", info.IsDir())
 
                        fileHeader := NewFileHeader(subPath, info.IsDir())