]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/transaction_handlers.go
Rename encode/decode methods
[rbdr/mobius] / hotline / transaction_handlers.go
index 813d25c072185ab9f80f32915a702d28a7777da5..ee95cc0cfe8abac6ace7b878ab0c96f6daff6ccd 100644 (file)
@@ -305,7 +305,7 @@ func HandleChatSend(cc *ClientConn, t *Transaction) (res []Transaction, err erro
 func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
        if !cc.Authorize(accessSendPrivMsg) {
                res = append(res, cc.NewErrReply(t, "You are not allowed to send private messages."))
 func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
        if !cc.Authorize(accessSendPrivMsg) {
                res = append(res, cc.NewErrReply(t, "You are not allowed to send private messages."))
-               return res, err
+               return res, errors.New("user is not allowed to send private messages")
        }
 
        msg := t.GetField(FieldData)
        }
 
        msg := t.GetField(FieldData)
@@ -326,7 +326,10 @@ func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, er
                reply.Fields = append(reply.Fields, NewField(FieldQuotingMsg, t.GetField(FieldQuotingMsg).Data))
        }
 
                reply.Fields = append(reply.Fields, NewField(FieldQuotingMsg, t.GetField(FieldQuotingMsg).Data))
        }
 
-       id, _ := byteToInt(ID.Data)
+       id, err := byteToInt(ID.Data)
+       if err != nil {
+               return res, errors.New("invalid client ID")
+       }
        otherClient, ok := cc.Server.Clients[uint16(id)]
        if !ok {
                return res, errors.New("invalid client ID")
        otherClient, ok := cc.Server.Clients[uint16(id)]
        if !ok {
                return res, errors.New("invalid client ID")
@@ -334,7 +337,7 @@ func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, er
 
        // Check if target user has "Refuse private messages" flag
        flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(otherClient.Flags)))
 
        // Check if target user has "Refuse private messages" flag
        flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(otherClient.Flags)))
-       if flagBitmap.Bit(userFLagRefusePChat) == 1 {
+       if flagBitmap.Bit(UserFlagRefusePChat) == 1 {
                res = append(res,
                        *NewTransaction(
                                TranServerMsg,
                res = append(res,
                        *NewTransaction(
                                TranServerMsg,
@@ -636,7 +639,7 @@ func HandleSetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error
                return res, err
        }
 
                return res, err
        }
 
-       login := DecodeUserString(t.GetField(FieldUserLogin).Data)
+       login := decodeString(t.GetField(FieldUserLogin).Data)
        userName := string(t.GetField(FieldUserName).Data)
 
        newAccessLvl := t.GetField(FieldUserAccess).Data
        userName := string(t.GetField(FieldUserName).Data)
 
        newAccessLvl := t.GetField(FieldUserAccess).Data
@@ -671,9 +674,9 @@ func HandleSetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error
 
                        flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(c.Flags)))
                        if c.Authorize(accessDisconUser) {
 
                        flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(c.Flags)))
                        if c.Authorize(accessDisconUser) {
-                               flagBitmap.SetBit(flagBitmap, userFlagAdmin, 1)
+                               flagBitmap.SetBit(flagBitmap, UserFlagAdmin, 1)
                        } else {
                        } else {
-                               flagBitmap.SetBit(flagBitmap, userFlagAdmin, 0)
+                               flagBitmap.SetBit(flagBitmap, UserFlagAdmin, 0)
                        }
                        binary.BigEndian.PutUint16(c.Flags, uint16(flagBitmap.Int64()))
 
                        }
                        binary.BigEndian.PutUint16(c.Flags, uint16(flagBitmap.Int64()))
 
@@ -707,7 +710,7 @@ func HandleGetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error
 
        res = append(res, cc.NewReply(t,
                NewField(FieldUserName, []byte(account.Name)),
 
        res = append(res, cc.NewReply(t,
                NewField(FieldUserName, []byte(account.Name)),
-               NewField(FieldUserLogin, negateString(t.GetField(FieldUserLogin).Data)),
+               NewField(FieldUserLogin, encodeString(t.GetField(FieldUserLogin).Data)),
                NewField(FieldUserPassword, []byte(account.Password)),
                NewField(FieldUserAccess, account.Access[:]),
        ))
                NewField(FieldUserPassword, []byte(account.Password)),
                NewField(FieldUserAccess, account.Access[:]),
        ))
@@ -752,7 +755,7 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                }
 
                if len(subFields) == 1 {
                }
 
                if len(subFields) == 1 {
-                       login := DecodeUserString(getField(FieldData, &subFields).Data)
+                       login := decodeString(getField(FieldData, &subFields).Data)
                        cc.logger.Infow("DeleteUser", "login", login)
 
                        if !cc.Authorize(accessDeleteUser) {
                        cc.logger.Infow("DeleteUser", "login", login)
 
                        if !cc.Authorize(accessDeleteUser) {
@@ -766,7 +769,7 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                        continue
                }
 
                        continue
                }
 
-               login := DecodeUserString(getField(FieldUserLogin, &subFields).Data)
+               login := decodeString(getField(FieldUserLogin, &subFields).Data)
 
                // check if the login dataFile; if so, we know we are updating an existing user
                if acc, ok := cc.Server.Accounts[login]; ok {
 
                // check if the login dataFile; if so, we know we are updating an existing user
                if acc, ok := cc.Server.Accounts[login]; ok {
@@ -790,8 +793,8 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                        }
 
                        err = cc.Server.UpdateUser(
                        }
 
                        err = cc.Server.UpdateUser(
-                               DecodeUserString(getField(FieldData, &subFields).Data),
-                               DecodeUserString(getField(FieldUserLogin, &subFields).Data),
+                               decodeString(getField(FieldData, &subFields).Data),
+                               decodeString(getField(FieldUserLogin, &subFields).Data),
                                string(getField(FieldUserName, &subFields).Data),
                                acc.Password,
                                acc.Access,
                                string(getField(FieldUserName, &subFields).Data),
                                acc.Password,
                                acc.Access,
@@ -808,7 +811,7 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                        }
 
                        newAccess := accessBitmap{}
                        }
 
                        newAccess := accessBitmap{}
-                       copy(newAccess[:], getField(FieldUserAccess, &subFields).Data[:])
+                       copy(newAccess[:], getField(FieldUserAccess, &subFields).Data)
 
                        // Prevent account from creating new account with greater permission
                        for i := 0; i < 64; i++ {
 
                        // Prevent account from creating new account with greater permission
                        for i := 0; i < 64; i++ {
@@ -837,7 +840,7 @@ func HandleNewUser(cc *ClientConn, t *Transaction) (res []Transaction, err error
                return res, err
        }
 
                return res, err
        }
 
-       login := DecodeUserString(t.GetField(FieldUserLogin).Data)
+       login := decodeString(t.GetField(FieldUserLogin).Data)
 
        // If the account already dataFile, reply with an error
        if _, ok := cc.Server.Accounts[login]; ok {
 
        // If the account already dataFile, reply with an error
        if _, ok := cc.Server.Accounts[login]; ok {
@@ -846,7 +849,7 @@ func HandleNewUser(cc *ClientConn, t *Transaction) (res []Transaction, err error
        }
 
        newAccess := accessBitmap{}
        }
 
        newAccess := accessBitmap{}
-       copy(newAccess[:], t.GetField(FieldUserAccess).Data[:])
+       copy(newAccess[:], t.GetField(FieldUserAccess).Data)
 
        // Prevent account from creating new account with greater permission
        for i := 0; i < 64; i++ {
 
        // Prevent account from creating new account with greater permission
        for i := 0; i < 64; i++ {
@@ -873,7 +876,7 @@ func HandleDeleteUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
        }
 
        // TODO: Handle case where account doesn't exist; e.g. delete race condition
        }
 
        // TODO: Handle case where account doesn't exist; e.g. delete race condition
-       login := DecodeUserString(t.GetField(FieldUserLogin).Data)
+       login := decodeString(t.GetField(FieldUserLogin).Data)
 
        if err := cc.Server.DeleteUser(login); err != nil {
                return res, err
 
        if err := cc.Server.DeleteUser(login); err != nil {
                return res, err
@@ -955,13 +958,13 @@ func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err er
 
        // Check refuse private PM option
        if optBitmap.Bit(refusePM) == 1 {
 
        // Check refuse private PM option
        if optBitmap.Bit(refusePM) == 1 {
-               flagBitmap.SetBit(flagBitmap, userFlagRefusePM, 1)
+               flagBitmap.SetBit(flagBitmap, UserFlagRefusePM, 1)
                binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64()))
        }
 
        // Check refuse private chat option
        if optBitmap.Bit(refuseChat) == 1 {
                binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64()))
        }
 
        // Check refuse private chat option
        if optBitmap.Bit(refuseChat) == 1 {
-               flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, 1)
+               flagBitmap.SetBit(flagBitmap, UserFlagRefusePChat, 1)
                binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64()))
        }
 
                binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64()))
        }
 
@@ -1015,7 +1018,7 @@ func HandleTranOldPostNews(cc *ClientConn, t *Transaction) (res []Transaction, e
        }
 
        newsPost := fmt.Sprintf(newsTemplate+"\r", cc.UserName, time.Now().Format(newsDateTemplate), t.GetField(FieldData).Data)
        }
 
        newsPost := fmt.Sprintf(newsTemplate+"\r", cc.UserName, time.Now().Format(newsDateTemplate), t.GetField(FieldData).Data)
-       newsPost = strings.Replace(newsPost, "\n", "\r", -1)
+       newsPost = strings.ReplaceAll(newsPost, "\n", "\r")
 
        // update news in memory
        cc.Server.FlatNews = append([]byte(newsPost), cc.Server.FlatNews...)
 
        // update news in memory
        cc.Server.FlatNews = append([]byte(newsPost), cc.Server.FlatNews...)
@@ -1066,7 +1069,6 @@ func HandleDisconnectUser(cc *ClientConn, t *Transaction) (res []Transaction, er
 
                        banUntil := time.Now().Add(tempBanDuration)
                        cc.Server.banList[strings.Split(clientConn.RemoteAddr, ":")[0]] = &banUntil
 
                        banUntil := time.Now().Add(tempBanDuration)
                        cc.Server.banList[strings.Split(clientConn.RemoteAddr, ":")[0]] = &banUntil
-                       cc.Server.writeBanList()
                case 2:
                        // send message: "You are permanently banned on this server"
                        cc.logger.Infow("Disconnect & ban " + string(clientConn.UserName))
                case 2:
                        // send message: "You are permanently banned on this server"
                        cc.logger.Infow("Disconnect & ban " + string(clientConn.UserName))
@@ -1079,7 +1081,11 @@ func HandleDisconnectUser(cc *ClientConn, t *Transaction) (res []Transaction, er
                        ))
 
                        cc.Server.banList[strings.Split(clientConn.RemoteAddr, ":")[0]] = nil
                        ))
 
                        cc.Server.banList[strings.Split(clientConn.RemoteAddr, ":")[0]] = nil
-                       cc.Server.writeBanList()
+               }
+
+               err := cc.Server.writeBanList()
+               if err != nil {
+                       return res, err
                }
        }
 
                }
        }
 
@@ -1605,7 +1611,6 @@ func HandleUploadFile(cc *ClientConn, t *Transaction) (res []Transaction, err er
 
        // client has requested to resume a partially transferred file
        if transferOptions != nil {
 
        // client has requested to resume a partially transferred file
        if transferOptions != nil {
-
                fileInfo, err := cc.Server.FS.Stat(fullFilePath + incompleteFileSuffix)
                if err != nil {
                        return res, err
                fileInfo, err := cc.Server.FS.Stat(fullFilePath + incompleteFileSuffix)
                if err != nil {
                        return res, err
@@ -1645,10 +1650,10 @@ func HandleSetClientUserInfo(cc *ClientConn, t *Transaction) (res []Transaction,
                optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
                flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(cc.Flags)))
 
                optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
                flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(cc.Flags)))
 
-               flagBitmap.SetBit(flagBitmap, userFlagRefusePM, optBitmap.Bit(refusePM))
+               flagBitmap.SetBit(flagBitmap, UserFlagRefusePM, optBitmap.Bit(refusePM))
                binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64()))
 
                binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64()))
 
-               flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, optBitmap.Bit(refuseChat))
+               flagBitmap.SetBit(flagBitmap, UserFlagRefusePChat, optBitmap.Bit(refuseChat))
                binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64()))
 
                // Check auto response
                binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64()))
 
                // Check auto response
@@ -1743,7 +1748,7 @@ func HandleInviteNewChat(cc *ClientConn, t *Transaction) (res []Transaction, err
        targetClient := cc.Server.Clients[binary.BigEndian.Uint16(targetID)]
 
        flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(targetClient.Flags)))
        targetClient := cc.Server.Clients[binary.BigEndian.Uint16(targetID)]
 
        flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(targetClient.Flags)))
-       if flagBitmap.Bit(userFLagRefusePChat) == 1 {
+       if flagBitmap.Bit(UserFlagRefusePChat) == 1 {
                res = append(res,
                        *NewTransaction(
                                TranServerMsg,
                res = append(res,
                        *NewTransaction(
                                TranServerMsg,