]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/client_conn.go
Implement Make Alias transaction
[rbdr/mobius] / hotline / client_conn.go
index 5c25f928860348d66814f60f63f2f46fc8e5fef1..b43a6ff65a371dcca85a169404ae98b5346f5b72 100644 (file)
@@ -29,14 +29,15 @@ type ClientConn struct {
        ID         *[]byte
        Icon       *[]byte
        Flags      *[]byte
-       UserName   *[]byte
+       UserName   []byte
        Account    *Account
-       IdleTime   *int
+       IdleTime   int
        Server     *Server
        Version    *[]byte
        Idle       bool
        AutoReply  *[]byte
        Transfers  map[int][]*FileTransfer
+       Agreed     bool
 }
 
 func (cc *ClientConn) sendAll(t int, fields ...Field) {
@@ -55,7 +56,7 @@ func (cc *ClientConn) handleTransaction(transaction *Transaction) error {
                        if field.ID == nil {
                                cc.Server.Logger.Infow(
                                        "Missing required field",
-                                       "Account", cc.Account.Login, "UserName", string(*cc.UserName), "RequestType", handler.Name, "FieldID", reqField.ID,
+                                       "Account", cc.Account.Login, "UserName", string(cc.UserName), "RequestType", handler.Name, "FieldID", reqField.ID,
                                )
                                return nil
                        }
@@ -63,7 +64,7 @@ func (cc *ClientConn) handleTransaction(transaction *Transaction) error {
                        if len(field.Data) < reqField.minLen {
                                cc.Server.Logger.Infow(
                                        "Field does not meet minLen",
-                                       "Account", cc.Account.Login, "UserName", string(*cc.UserName), "RequestType", handler.Name, "FieldID", reqField.ID,
+                                       "Account", cc.Account.Login, "UserName", string(cc.UserName), "RequestType", handler.Name, "FieldID", reqField.ID,
                                )
                                return nil
                        }
@@ -71,7 +72,7 @@ func (cc *ClientConn) handleTransaction(transaction *Transaction) error {
                if !authorize(cc.Account.Access, handler.Access) {
                        cc.Server.Logger.Infow(
                                "Unauthorized Action",
-                               "Account", cc.Account.Login, "UserName", string(*cc.UserName), "RequestType", handler.Name,
+                               "Account", cc.Account.Login, "UserName", string(cc.UserName), "RequestType", handler.Name,
                        )
                        cc.Server.outbox <- cc.NewErrReply(transaction, handler.DenyMsg)
 
@@ -81,7 +82,7 @@ func (cc *ClientConn) handleTransaction(transaction *Transaction) error {
                cc.Server.Logger.Infow(
                        "Received Transaction",
                        "login", cc.Account.Login,
-                       "name", string(*cc.UserName),
+                       "name", string(cc.UserName),
                        "RequestType", handler.Name,
                )
 
@@ -95,35 +96,35 @@ func (cc *ClientConn) handleTransaction(transaction *Transaction) error {
        } else {
                cc.Server.Logger.Errorw(
                        "Unimplemented transaction type received",
-                       "UserName", string(*cc.UserName), "RequestID", requestNum,
+                       "UserName", string(cc.UserName), "RequestID", requestNum,
                )
        }
 
        cc.Server.mux.Lock()
        defer cc.Server.mux.Unlock()
 
-       // if user was idle and this is a non-keepalive transaction
-       if *cc.IdleTime > userIdleSeconds && requestNum != tranKeepAlive {
-               flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags)))
-               flagBitmap.SetBit(flagBitmap, userFlagAway, 0)
-               binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
-               cc.Idle = false
-               //*cc.IdleTime = 0
-
-               cc.sendAll(
-                       tranNotifyChangeUser,
-                       NewField(fieldUserID, *cc.ID),
-                       NewField(fieldUserFlags, *cc.Flags),
-                       NewField(fieldUserName, *cc.UserName),
-                       NewField(fieldUserIconID, *cc.Icon),
-               )
-
-               //return nil
+       if requestNum != tranKeepAlive {
+               // reset the user idle timer
+               cc.IdleTime = 0
+
+               // if user was previously idle, mark as not idle and notify other connected clients that
+               // the user is no longer away
+               if cc.Idle {
+                       flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags)))
+                       flagBitmap.SetBit(flagBitmap, userFlagAway, 0)
+                       binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
+                       cc.Idle = false
+
+                       cc.sendAll(
+                               tranNotifyChangeUser,
+                               NewField(fieldUserID, *cc.ID),
+                               NewField(fieldUserFlags, *cc.Flags),
+                               NewField(fieldUserName, cc.UserName),
+                               NewField(fieldUserIconID, *cc.Icon),
+                       )
+               }
        }
 
-       // TODO: Don't we need to skip this if requestNum == tranKeepalive ??
-       *cc.IdleTime = 0
-
        return nil
 }
 
@@ -168,7 +169,7 @@ func (cc ClientConn) Disconnect() {
 // NotifyOthers sends transaction t to other clients connected to the server
 func (cc ClientConn) NotifyOthers(t Transaction) {
        for _, c := range sortedClients(cc.Server.Clients) {
-               if c.ID != cc.ID {
+               if c.ID != cc.ID && c.Agreed {
                        t.clientID = c.ID
                        cc.Server.outbox <- t
                }
@@ -201,7 +202,7 @@ type handshake struct {
 // Description         Size    Data    Note
 // Protocol ID         4               TRTP
 //Error code           4                               Error code returned by the server (0 = no error)
-func  Handshake(conn net.Conn, buf []byte) error {
+func Handshake(conn net.Conn, buf []byte) error {
        var h handshake
        r := bytes.NewReader(buf)
        if err := binary.Read(r, binary.BigEndian, &h); err != nil {