]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/transaction_handlers.go
Replace zap logger with slog
[rbdr/mobius] / hotline / transaction_handlers.go
index 793bb7420cd234191b875ddf94c063959b21cec7..3accf2c647dad26c9f16ecb8eae486d524cbb85c 100644 (file)
@@ -460,7 +460,7 @@ func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err e
                if err != nil {
                        return res, err
                }
-               _, err = w.Write(hlFile.ffo.FlatFileInformationFork.MarshalBinary())
+               _, err = io.Copy(w, &hlFile.ffo.FlatFileInformationFork)
                if err != nil {
                        return res, err
                }
@@ -574,7 +574,7 @@ func HandleMoveFile(cc *ClientConn, t *Transaction) (res []Transaction, err erro
                return res, err
        }
 
-       cc.logger.Infow("Move file", "src", filePath+"/"+fileName, "dst", fileNewPath+"/"+fileName)
+       cc.logger.Info("Move file", "src", filePath+"/"+fileName, "dst", fileNewPath+"/"+fileName)
 
        hlFile, err := newFileWrapper(cc.Server.FS, filePath, 0)
        if err != nil {
@@ -658,7 +658,7 @@ func HandleSetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error
                return res, err
        }
 
-       login := decodeString(t.GetField(FieldUserLogin).Data)
+       login := string(encodeString(t.GetField(FieldUserLogin).Data))
        userName := string(t.GetField(FieldUserName).Data)
 
        newAccessLvl := t.GetField(FieldUserAccess).Data
@@ -783,8 +783,8 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                                return res, err
                        }
 
-                       login := decodeString(getField(FieldData, &subFields).Data)
-                       cc.logger.Infow("DeleteUser", "login", login)
+                       login := string(encodeString(getField(FieldData, &subFields).Data))
+                       cc.logger.Info("DeleteUser", "login", login)
 
                        if err := cc.Server.DeleteUser(login); err != nil {
                                return res, err
@@ -798,9 +798,9 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                // If FieldData is included, this is a rename operation where FieldData contains the login of the existing
                // account and FieldUserLogin contains the new login.
                if getField(FieldData, &subFields) != nil {
-                       loginToRename = decodeString(getField(FieldData, &subFields).Data)
+                       loginToRename = string(encodeString(getField(FieldData, &subFields).Data))
                }
-               userLogin := decodeString(getField(FieldUserLogin, &subFields).Data)
+               userLogin := string(encodeString(getField(FieldUserLogin, &subFields).Data))
                if loginToRename != "" {
                        accountToUpdate = loginToRename
                } else {
@@ -810,9 +810,9 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                // Check if accountToUpdate has an existing account.  If so, we know we are updating an existing user.
                if acc, ok := cc.Server.Accounts[accountToUpdate]; ok {
                        if loginToRename != "" {
-                               cc.logger.Infow("RenameUser", "prevLogin", accountToUpdate, "newLogin", userLogin)
+                               cc.logger.Info("RenameUser", "prevLogin", accountToUpdate, "newLogin", userLogin)
                        } else {
-                               cc.logger.Infow("UpdateUser", "login", accountToUpdate)
+                               cc.logger.Info("UpdateUser", "login", accountToUpdate)
                        }
 
                        // account exists, so this is an update action
@@ -842,8 +842,8 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                        }
 
                        err = cc.Server.UpdateUser(
-                               decodeString(getField(FieldData, &subFields).Data),
-                               decodeString(getField(FieldUserLogin, &subFields).Data),
+                               string(encodeString(getField(FieldData, &subFields).Data)),
+                               string(encodeString(getField(FieldUserLogin, &subFields).Data)),
                                string(getField(FieldUserName, &subFields).Data),
                                acc.Password,
                                acc.Access,
@@ -857,7 +857,7 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                                return res, nil
                        }
 
-                       cc.logger.Infow("CreateUser", "login", userLogin)
+                       cc.logger.Info("CreateUser", "login", userLogin)
 
                        newAccess := accessBitmap{}
                        copy(newAccess[:], getField(FieldUserAccess, &subFields).Data)
@@ -889,7 +889,7 @@ func HandleNewUser(cc *ClientConn, t *Transaction) (res []Transaction, err error
                return res, err
        }
 
-       login := decodeString(t.GetField(FieldUserLogin).Data)
+       login := string(encodeString(t.GetField(FieldUserLogin).Data))
 
        // If the account already dataFile, reply with an error
        if _, ok := cc.Server.Accounts[login]; ok {
@@ -925,7 +925,7 @@ func HandleDeleteUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                return res, nil
        }
 
-       login := decodeString(t.GetField(FieldUserLogin).Data)
+       login := string(encodeString(t.GetField(FieldUserLogin).Data))
 
        if err := cc.Server.DeleteUser(login); err != nil {
                return res, err
@@ -998,7 +998,7 @@ func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err er
        cc.Icon = t.GetField(FieldUserIconID).Data
 
        cc.logger = cc.logger.With("name", string(cc.UserName))
-       cc.logger.Infow("Login successful", "clientVersion", fmt.Sprintf("%v", func() int { i, _ := byteToInt(cc.Version); return i }()))
+       cc.logger.Info("Login successful", "clientVersion", fmt.Sprintf("%v", func() int { i, _ := byteToInt(cc.Version); return i }()))
 
        options := t.GetField(FieldOptions).Data
        optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
@@ -1107,7 +1107,7 @@ func HandleDisconnectUser(cc *ClientConn, t *Transaction) (res []Transaction, er
                switch t.GetField(FieldOptions).Data[1] {
                case 1:
                        // send message: "You are temporarily banned on this server"
-                       cc.logger.Infow("Disconnect & temporarily ban " + string(clientConn.UserName))
+                       cc.logger.Info("Disconnect & temporarily ban " + string(clientConn.UserName))
 
                        res = append(res, *NewTransaction(
                                TranServerMsg,
@@ -1120,7 +1120,7 @@ func HandleDisconnectUser(cc *ClientConn, t *Transaction) (res []Transaction, er
                        cc.Server.banList[strings.Split(clientConn.RemoteAddr, ":")[0]] = &banUntil
                case 2:
                        // send message: "You are permanently banned on this server"
-                       cc.logger.Infow("Disconnect & ban " + string(clientConn.UserName))
+                       cc.logger.Info("Disconnect & ban " + string(clientConn.UserName))
 
                        res = append(res, *NewTransaction(
                                TranServerMsg,
@@ -1194,7 +1194,7 @@ func HandleNewNewsCat(cc *ClientConn, t *Transaction) (res []Transaction, err er
        cats := cc.Server.GetNewsCatByPath(pathStrs)
        cats[name] = NewsCategoryListData15{
                Name:     name,
-               Type:     []byte{0, 3},
+               Type:     [2]byte{0, 3},
                Articles: map[uint32]*NewsArtData{},
                SubCats:  make(map[string]NewsCategoryListData15),
        }
@@ -1218,12 +1218,10 @@ func HandleNewNewsFldr(cc *ClientConn, t *Transaction) (res []Transaction, err e
        name := string(t.GetField(FieldFileName).Data)
        pathStrs := ReadNewsPath(t.GetField(FieldNewsPath).Data)
 
-       cc.logger.Infof("Creating new news folder %s", name)
-
        cats := cc.Server.GetNewsCatByPath(pathStrs)
        cats[name] = NewsCategoryListData15{
                Name:     name,
-               Type:     []byte{0, 2},
+               Type:     [2]byte{0, 2},
                Articles: map[uint32]*NewsArtData{},
                SubCats:  make(map[string]NewsCategoryListData15),
        }
@@ -1258,7 +1256,12 @@ func HandleGetNewsArtNameList(cc *ClientConn, t *Transaction) (res []Transaction
 
        nald := cat.GetNewsArtListData()
 
-       res = append(res, cc.NewReply(t, NewField(FieldNewsArtListData, nald.Payload())))
+       b, err := io.ReadAll(&nald)
+       if err != nil {
+
+       }
+
+       res = append(res, cc.NewReply(t, NewField(FieldNewsArtListData, b)))
        return res, err
 }
 
@@ -1337,7 +1340,7 @@ func HandleDelNewsItem(cc *ClientConn, t *Transaction) (res []Transaction, err e
                }
        }
 
-       if bytes.Equal(cats[delName].Type, []byte{0, 3}) {
+       if cats[delName].Type == [2]byte{0, 3} {
                if !cc.Authorize(accessNewsDeleteCat) {
                        return append(res, cc.NewErrReply(t, "You are not allowed to delete news categories.")), nil
                }
@@ -1918,14 +1921,17 @@ func HandleJoinChat(cc *ClientConn, t *Transaction) (res []Transaction, err erro
 
        replyFields := []Field{NewField(FieldChatSubject, []byte(privChat.Subject))}
        for _, c := range sortedClients(privChat.ClientConn) {
-               user := User{
+
+               b, err := io.ReadAll(&User{
                        ID:    *c.ID,
                        Icon:  c.Icon,
                        Flags: c.Flags,
                        Name:  string(c.UserName),
+               })
+               if err != nil {
+                       return res, nil
                }
-
-               replyFields = append(replyFields, NewField(FieldUsernameWithInfo, user.Payload()))
+               replyFields = append(replyFields, NewField(FieldUsernameWithInfo, b))
        }
 
        res = append(res, cc.NewReply(t, replyFields...))
@@ -2016,7 +2022,7 @@ func HandleMakeAlias(cc *ClientConn, t *Transaction) (res []Transaction, err err
                return res, err
        }
 
-       cc.logger.Debugw("Make alias", "src", fullFilePath, "dst", fullNewFilePath)
+       cc.logger.Debug("Make alias", "src", fullFilePath, "dst", fullNewFilePath)
 
        if err := cc.Server.FS.Symlink(fullFilePath, fullNewFilePath); err != nil {
                res = append(res, cc.NewErrReply(t, "Error creating alias"))