]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/server.go
Simplify news article timestamping
[rbdr/mobius] / hotline / server.go
index 20ef49b9a082bdbc73f91e1462392e96c1651858..c5a29ba9ea19173b3499be0ab3e1ade4d7e49f95 100644 (file)
@@ -111,7 +111,7 @@ func (s *Server) sendTransaction(t Transaction) error {
        client := s.Clients[uint16(clientID)]
        s.mux.Unlock()
        if client == nil {
-               return errors.New("invalid client")
+               return fmt.Errorf("invalid client id %v", *t.clientID)
        }
        userName := string(client.UserName)
        login := client.Account.Login
@@ -169,7 +169,7 @@ func (s *Server) Serve(ctx context.Context, cancelRoot context.CancelFunc, ln ne
 }
 
 const (
-       agreementFile    = "Agreement.txt"
+       agreementFile = "Agreement.txt"
 )
 
 // NewServer constructs a new Server from a config dir
@@ -278,8 +278,8 @@ func (s *Server) keepaliveHandler() {
                s.mux.Lock()
 
                for _, c := range s.Clients {
-                       *c.IdleTime += idleCheckInterval
-                       if *c.IdleTime > userIdleSeconds && !c.Idle {
+                       c.IdleTime += idleCheckInterval
+                       if c.IdleTime > userIdleSeconds && !c.Idle {
                                c.Idle = true
 
                                flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*c.Flags)))
@@ -327,15 +327,13 @@ func (s *Server) NewClientConn(conn net.Conn) *ClientConn {
                Connection: conn,
                Server:     s,
                Version:    &[]byte{},
-               IdleTime:   new(int),
                AutoReply:  &[]byte{},
                Transfers:  make(map[int][]*FileTransfer),
+               Agreed:     false,
        }
        *s.NextGuestID++
        ID := *s.NextGuestID
 
-       *clientConn.IdleTime = 0
-
        binary.BigEndian.PutUint16(*clientConn.ID, ID)
        s.Clients[ID] = clientConn
 
@@ -377,7 +375,10 @@ func (s *Server) connectedUsers() []Field {
        defer s.mux.Unlock()
 
        var connectedUsers []Field
-       for _, c := range s.Clients {
+       for _, c := range sortedClients(s.Clients) {
+               if c.Agreed == false {
+                       continue
+               }
                user := User{
                        ID:    *c.ID,
                        Icon:  *c.Icon,
@@ -413,7 +414,7 @@ func (s *Server) loadAccounts(userDir string) error {
        }
 
        for _, file := range matches {
-               fh, err := os.Open(file)
+               fh, err := FS.Open(file)
                if err != nil {
                        return err
                }
@@ -431,7 +432,7 @@ func (s *Server) loadAccounts(userDir string) error {
 }
 
 func (s *Server) loadConfig(path string) error {
-       fh, err := os.Open(path)
+       fh, err := FS.Open(path)
        if err != nil {
                return err
        }
@@ -536,9 +537,14 @@ func (s *Server) handleNewConnection(conn net.Conn) error {
        // Show agreement to client
        c.Server.outbox <- *NewTransaction(tranShowAgreement, c.ID, NewField(fieldData, s.Agreement))
 
-       if _, err := c.notifyNewUserHasJoined(); err != nil {
-               return err
+       // assume simplified hotline v1.2.3 login flow that does not require agreement
+       if *c.Version == nil {
+               c.Agreed = true
+               if _, err := c.notifyNewUserHasJoined(); err != nil {
+                       return err
+               }
        }
+
        c.Server.Stats.LoginCount += 1
 
        const readBuffSize = 1024000 // 1KB - TODO: what should this be?
@@ -636,11 +642,15 @@ func (s *Server) TransferFile(conn net.Conn) error {
 
        switch fileTransfer.Type {
        case FileDownload:
-               fullFilePath := fmt.Sprintf("%v/%v", s.Config.FileRoot+string(fileTransfer.FilePath), string(fileTransfer.FileName))
+               fullFilePath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
+               if err != nil {
+                       return err
+               }
 
                ffo, err := NewFlattenedFileObject(
-                       s.Config.FileRoot+string(fileTransfer.FilePath),
-                       string(fileTransfer.FileName),
+                       s.Config.FileRoot,
+                       fileTransfer.FilePath,
+                       fileTransfer.FileName,
                )
                if err != nil {
                        return err
@@ -653,7 +663,7 @@ func (s *Server) TransferFile(conn net.Conn) error {
                        return err
                }
 
-               file, err := os.Open(fullFilePath)
+               file, err := FS.Open(fullFilePath)
                if err != nil {
                        return err
                }
@@ -756,9 +766,10 @@ func (s *Server) TransferFile(conn net.Conn) error {
                //
                // This notifies the server to send the next item header
 
-               var fh FilePath
-               _ = fh.UnmarshalBinary(fileTransfer.FilePath)
-               fullFilePath := fmt.Sprintf("%v/%v", s.Config.FileRoot+fh.String(), string(fileTransfer.FileName))
+               fullFilePath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
+               if err != nil {
+                       return err
+               }
 
                basePathLen := len(fullFilePath)
 
@@ -798,7 +809,11 @@ func (s *Server) TransferFile(conn net.Conn) error {
 
                        splitPath := strings.Split(path, "/")
 
-                       ffo, err := NewFlattenedFileObject(strings.Join(splitPath[:len(splitPath)-1], "/"), info.Name())
+                       ffo, err := NewFlattenedFileObject(
+                               strings.Join(splitPath[:len(splitPath)-1], "/"),
+                               nil,
+                               []byte(info.Name()),
+                       )
                        if err != nil {
                                return err
                        }
@@ -821,7 +836,7 @@ func (s *Server) TransferFile(conn net.Conn) error {
                                return err
                        }
 
-                       file, err := os.Open(path)
+                       file, err := FS.Open(path)
                        if err != nil {
                                return err
                        }
@@ -851,20 +866,23 @@ func (s *Server) TransferFile(conn net.Conn) error {
                })
 
        case FolderUpload:
-               dstPath := s.Config.FileRoot + ReadFilePath(fileTransfer.FilePath) + "/" + string(fileTransfer.FileName)
+               dstPath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
+               if err != nil {
+                       return err
+               }
                s.Logger.Infow(
                        "Folder upload started",
                        "transactionRef", fileTransfer.ReferenceNumber,
                        "RemoteAddr", conn.RemoteAddr().String(),
                        "dstPath", dstPath,
-                       "TransferSize", fileTransfer.TransferSize,
+                       "TransferSize", fmt.Sprintf("%x", fileTransfer.TransferSize),
                        "FolderItemCount", fileTransfer.FolderItemCount,
                )
 
                // Check if the target folder exists.  If not, create it.
-               if _, err := os.Stat(dstPath); os.IsNotExist(err) {
-                       s.Logger.Infow("Target path does not exist; Creating...", "dstPath", dstPath)
-                       if err := os.Mkdir(dstPath, 0777); err != nil {
+               if _, err := FS.Stat(dstPath); os.IsNotExist(err) {
+                       s.Logger.Infow("Creating target path", "dstPath", dstPath)
+                       if err := FS.Mkdir(dstPath, 0777); err != nil {
                                s.Logger.Error(err)
                        }
                }
@@ -981,7 +999,6 @@ func transferFile(conn net.Conn, dst string) error {
        }
 }
 
-
 // sortedClients is a utility function that takes a map of *ClientConn and returns a sorted slice of the values.
 // The purpose of this is to ensure that the ordering of client connections is deterministic so that test assertions work.
 func sortedClients(unsortedClients map[uint16]*ClientConn) (clients []*ClientConn) {