]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/transaction_handlers.go
Add .goreleaser.yaml
[rbdr/mobius] / hotline / transaction_handlers.go
index d0651221a0921a7a440cfcbbea447c132a47d79c..ff23684b21eda1c678e37148a81faa5869b33f0a 100644 (file)
@@ -6,6 +6,7 @@ import (
        "errors"
        "fmt"
        "gopkg.in/yaml.v3"
        "errors"
        "fmt"
        "gopkg.in/yaml.v3"
+       "io"
        "math/big"
        "os"
        "path"
        "math/big"
        "os"
        "path"
@@ -459,7 +460,7 @@ func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err e
                if err != nil {
                        return res, err
                }
                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
                }
                if err != nil {
                        return res, err
                }
@@ -657,7 +658,7 @@ func HandleSetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error
                return res, err
        }
 
                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
        userName := string(t.GetField(FieldUserName).Data)
 
        newAccessLvl := t.GetField(FieldUserAccess).Data
@@ -747,13 +748,12 @@ func HandleListUsers(cc *ClientConn, t *Transaction) (res []Transaction, err err
 
        var userFields []Field
        for _, acc := range cc.Server.Accounts {
 
        var userFields []Field
        for _, acc := range cc.Server.Accounts {
-               b := make([]byte, 0, 100)
-               n, err := acc.Read(b)
+               b, err := io.ReadAll(acc)
                if err != nil {
                        return res, err
                }
 
                if err != nil {
                        return res, err
                }
 
-               userFields = append(userFields, NewField(FieldData, b[:n]))
+               userFields = append(userFields, NewField(FieldData, b))
        }
 
        res = append(res, cc.NewReply(t, userFields...))
        }
 
        res = append(res, cc.NewReply(t, userFields...))
@@ -776,26 +776,44 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                        return res, err
                }
 
                        return res, err
                }
 
+               // If there's only one subfield, that indicates this is a delete operation for the login in FieldData
                if len(subFields) == 1 {
                if len(subFields) == 1 {
-                       login := decodeString(getField(FieldData, &subFields).Data)
-                       cc.logger.Infow("DeleteUser", "login", login)
-
                        if !cc.Authorize(accessDeleteUser) {
                                res = append(res, cc.NewErrReply(t, "You are not allowed to delete accounts."))
                                return res, err
                        }
 
                        if !cc.Authorize(accessDeleteUser) {
                                res = append(res, cc.NewErrReply(t, "You are not allowed to delete accounts."))
                                return res, err
                        }
 
+                       login := string(encodeString(getField(FieldData, &subFields).Data))
+                       cc.logger.Infow("DeleteUser", "login", login)
+
                        if err := cc.Server.DeleteUser(login); err != nil {
                                return res, err
                        }
                        continue
                }
 
                        if err := cc.Server.DeleteUser(login); err != nil {
                                return res, err
                        }
                        continue
                }
 
-               login := decodeString(getField(FieldUserLogin, &subFields).Data)
+               // login of the account to update
+               var accountToUpdate, loginToRename string
+
+               // 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 = string(encodeString(getField(FieldData, &subFields).Data))
+               }
+               userLogin := string(encodeString(getField(FieldUserLogin, &subFields).Data))
+               if loginToRename != "" {
+                       accountToUpdate = loginToRename
+               } else {
+                       accountToUpdate = userLogin
+               }
 
 
-               // check if the login dataFile; if so, we know we are updating an existing user
-               if acc, ok := cc.Server.Accounts[login]; ok {
-                       cc.logger.Infow("UpdateUser", "login", login)
+               // 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)
+                       } else {
+                               cc.logger.Infow("UpdateUser", "login", accountToUpdate)
+                       }
 
                        // account exists, so this is an update action
                        if !cc.Authorize(accessModifyUser) {
 
                        // account exists, so this is an update action
                        if !cc.Authorize(accessModifyUser) {
@@ -824,8 +842,8 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                        }
 
                        err = cc.Server.UpdateUser(
                        }
 
                        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,
                                string(getField(FieldUserName, &subFields).Data),
                                acc.Password,
                                acc.Access,
@@ -834,13 +852,13 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                                return res, err
                        }
                } else {
                                return res, err
                        }
                } else {
-                       cc.logger.Infow("CreateUser", "login", login)
-
                        if !cc.Authorize(accessCreateUser) {
                                res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts."))
                                return res, nil
                        }
 
                        if !cc.Authorize(accessCreateUser) {
                                res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts."))
                                return res, nil
                        }
 
+                       cc.logger.Infow("CreateUser", "login", userLogin)
+
                        newAccess := accessBitmap{}
                        copy(newAccess[:], getField(FieldUserAccess, &subFields).Data)
 
                        newAccess := accessBitmap{}
                        copy(newAccess[:], getField(FieldUserAccess, &subFields).Data)
 
@@ -853,7 +871,7 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                                }
                        }
 
                                }
                        }
 
-                       err = cc.Server.NewUser(login, string(getField(FieldUserName, &subFields).Data), string(getField(FieldUserPassword, &subFields).Data), newAccess)
+                       err = cc.Server.NewUser(userLogin, string(getField(FieldUserName, &subFields).Data), string(getField(FieldUserPassword, &subFields).Data), newAccess)
                        if err != nil {
                                return append(res, cc.NewErrReply(t, "Cannot create account because there is already an account with that login.")), nil
                        }
                        if err != nil {
                                return append(res, cc.NewErrReply(t, "Cannot create account because there is already an account with that login.")), nil
                        }
@@ -871,7 +889,7 @@ func HandleNewUser(cc *ClientConn, t *Transaction) (res []Transaction, err error
                return res, err
        }
 
                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 {
 
        // If the account already dataFile, reply with an error
        if _, ok := cc.Server.Accounts[login]; ok {
@@ -907,7 +925,7 @@ func HandleDeleteUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                return res, nil
        }
 
                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
 
        if err := cc.Server.DeleteUser(login); err != nil {
                return res, err
@@ -1176,7 +1194,7 @@ func HandleNewNewsCat(cc *ClientConn, t *Transaction) (res []Transaction, err er
        cats := cc.Server.GetNewsCatByPath(pathStrs)
        cats[name] = NewsCategoryListData15{
                Name:     name,
        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),
        }
                Articles: map[uint32]*NewsArtData{},
                SubCats:  make(map[string]NewsCategoryListData15),
        }
@@ -1205,7 +1223,7 @@ func HandleNewNewsFldr(cc *ClientConn, t *Transaction) (res []Transaction, err e
        cats := cc.Server.GetNewsCatByPath(pathStrs)
        cats[name] = NewsCategoryListData15{
                Name:     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),
        }
                Articles: map[uint32]*NewsArtData{},
                SubCats:  make(map[string]NewsCategoryListData15),
        }
@@ -1240,7 +1258,12 @@ func HandleGetNewsArtNameList(cc *ClientConn, t *Transaction) (res []Transaction
 
        nald := cat.GetNewsArtListData()
 
 
        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
 }
 
        return res, err
 }
 
@@ -1319,7 +1342,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
                }
                if !cc.Authorize(accessNewsDeleteCat) {
                        return append(res, cc.NewErrReply(t, "You are not allowed to delete news categories.")), nil
                }
@@ -1900,14 +1923,17 @@ func HandleJoinChat(cc *ClientConn, t *Transaction) (res []Transaction, err erro
 
        replyFields := []Field{NewField(FieldChatSubject, []byte(privChat.Subject))}
        for _, c := range sortedClients(privChat.ClientConn) {
 
        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),
                        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...))
        }
 
        res = append(res, cc.NewReply(t, replyFields...))