]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/transaction_handlers.go
Misc cleanup
[rbdr/mobius] / hotline / transaction_handlers.go
index ea6d087540ec9a03e9fc3071810772f6888dfcf8..91a4c0dbe015e50632e008bd553aa51eba6bf812 100644 (file)
@@ -237,8 +237,8 @@ var TransactionHandlers = map[uint16]TransactionType{
        },
        tranSendInstantMsg: {
                Access: accessAlwaysAllow,
-               //Access: accessSendPrivMsg,
-               //DenyMsg: "You are not allowed to send private messages",
+               // Access: accessSendPrivMsg,
+               // DenyMsg: "You are not allowed to send private messages",
                Name:    "tranSendInstantMsg",
                Handler: HandleSendInstantMsg,
                RequiredFields: []requiredField{
@@ -256,6 +256,16 @@ var TransactionHandlers = map[uint16]TransactionType{
                Name:    "tranSetChatSubject",
                Handler: HandleSetChatSubject,
        },
+       tranMakeFileAlias: {
+               Access:  accessAlwaysAllow,
+               Name:    "tranMakeFileAlias",
+               Handler: HandleMakeAlias,
+               RequiredFields: []requiredField{
+                       {ID: fieldFileName, minLen: 1},
+                       {ID: fieldFilePath, minLen: 1},
+                       {ID: fieldFileNewPath, minLen: 1},
+               },
+       },
        tranSetClientUserInfo: {
                Access:  accessAlwaysAllow,
                Name:    "tranSetClientUserInfo",
@@ -273,7 +283,7 @@ var TransactionHandlers = map[uint16]TransactionType{
                Handler: HandleSetUser,
        },
        tranUploadFile: {
-               Access:  accessUploadFile,
+               Access:  accessAlwaysAllow,
                DenyMsg: "You are not allowed to upload files.",
                Name:    "tranUploadFile",
                Handler: HandleUploadFile,
@@ -347,13 +357,13 @@ func HandleChatSend(cc *ClientConn, t *Transaction) (res []Transaction, err erro
 //     101     Data    Optional
 //     214     Quoting message Optional
 //
-//Fields used in the reply:
+// Fields used in the reply:
 // None
 func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
        msg := t.GetField(fieldData)
        ID := t.GetField(fieldUserID)
        // TODO: Implement reply quoting
-       //options := transaction.GetField(hotline.fieldOptions)
+       // options := transaction.GetField(hotline.fieldOptions)
 
        res = append(res,
                *NewTransaction(
@@ -367,23 +377,18 @@ func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, er
        )
        id, _ := byteToInt(ID.Data)
 
-       //keys := make([]uint16, 0, len(cc.Server.Clients))
-       //for k := range cc.Server.Clients {
-       //      keys = append(keys, k)
-       //}
-
        otherClient := cc.Server.Clients[uint16(id)]
        if otherClient == nil {
                return res, errors.New("ohno")
        }
 
        // Respond with auto reply if other client has it enabled
-       if len(*otherClient.AutoReply) > 0 {
+       if len(otherClient.AutoReply) > 0 {
                res = append(res,
                        *NewTransaction(
                                tranServerMsg,
                                cc.ID,
-                               NewField(fieldData, *otherClient.AutoReply),
+                               NewField(fieldData, otherClient.AutoReply),
                                NewField(fieldUserName, otherClient.UserName),
                                NewField(fieldUserID, *otherClient.ID),
                                NewField(fieldOptions, []byte{0, 1}),
@@ -440,7 +445,7 @@ func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err e
                return nil, err
        }
 
-       //fileComment := t.GetField(fieldFileComment).Data
+       // fileComment := t.GetField(fieldFileComment).Data
        fileNewName := t.GetField(fieldFileNewName).Data
 
        if fileNewName != nil {
@@ -652,8 +657,8 @@ func HandleSetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error
 }
 
 func HandleGetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
-       userLogin := string(t.GetField(fieldUserLogin).Data)
-       account := cc.Server.Accounts[userLogin]
+       // userLogin := string(t.GetField(fieldUserLogin).Data)
+       account := cc.Server.Accounts[string(t.GetField(fieldUserLogin).Data)]
        if account == nil {
                errorT := cc.NewErrReply(t, "Account does not exist.")
                res = append(res, errorT)
@@ -821,11 +826,8 @@ func (cc *ClientConn) notifyNewUserHasJoined() (res []Transaction, err error) {
 }
 
 func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
-       bs := make([]byte, 2)
-       binary.BigEndian.PutUint16(bs, *cc.Server.NextGuestID)
-
+       cc.Agreed = true
        cc.UserName = t.GetField(fieldUserName).Data
-       *cc.ID = bs
        *cc.Icon = t.GetField(fieldUserIconID).Data
 
        options := t.GetField(fieldOptions).Data
@@ -847,9 +849,9 @@ func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err er
 
        // Check auto response
        if optBitmap.Bit(autoResponse) == 1 {
-               *cc.AutoReply = t.GetField(fieldAutomaticResponse).Data
+               cc.AutoReply = t.GetField(fieldAutomaticResponse).Data
        } else {
-               *cc.AutoReply = []byte{}
+               cc.AutoReply = []byte{}
        }
 
        _, _ = cc.notifyNewUserHasJoined()
@@ -1143,7 +1145,7 @@ func HandlePostNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err e
        newArt := NewsArtData{
                Title:         string(t.GetField(fieldNewsArtTitle).Data),
                Poster:        string(cc.UserName),
-               Date:          NewsDate(),
+               Date:          toHotlineTime(time.Now()),
                PrevArt:       []byte{0, 0, 0, 0},
                NextArt:       []byte{0, 0, 0, 0},
                ParentArt:     append([]byte{0, 0}, t.GetField(fieldNewsArtID).Data...),
@@ -1279,6 +1281,9 @@ func HandleDownloadFolder(cc *ClientConn, t *Transaction) (res []Transaction, er
        }
 
        fullFilePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFilePath).Data, t.GetField(fieldFileName).Data)
+       if err != nil {
+               return res, err
+       }
 
        transferSize, err := CalcTotalSize(fullFilePath)
        if err != nil {
@@ -1323,21 +1328,25 @@ func HandleUploadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err
 }
 
 func HandleUploadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
+       // TODO: add permission handing for upload folders and drop boxes
+       if !authorize(cc.Account.Access, accessUploadFile) {
+               res = append(res, cc.NewErrReply(t, "You are not allowed to upload files."))
+               return res, err
+       }
+
        fileName := t.GetField(fieldFileName).Data
        filePath := t.GetField(fieldFilePath).Data
 
        transactionRef := cc.Server.NewTransactionRef()
        data := binary.BigEndian.Uint32(transactionRef)
 
-       fileTransfer := &FileTransfer{
+       cc.Server.FileTransfers[data] = &FileTransfer{
                FileName:        fileName,
                FilePath:        filePath,
                ReferenceNumber: transactionRef,
                Type:            FileUpload,
        }
 
-       cc.Server.FileTransfers[data] = fileTransfer
-
        res = append(res, cc.NewReply(t, NewField(fieldRefNum, transactionRef)))
        return res, err
 }
@@ -1366,23 +1375,17 @@ 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)))
 
-               // Check refuse private PM option
-               if optBitmap.Bit(refusePM) == 1 {
-                       flagBitmap.SetBit(flagBitmap, userFlagRefusePM, 1)
-                       binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
-               }
+               flagBitmap.SetBit(flagBitmap, userFlagRefusePM, optBitmap.Bit(refusePM))
+               binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
 
-               // Check refuse private chat option
-               if optBitmap.Bit(refuseChat) == 1 {
-                       flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, 1)
-                       binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
-               }
+               flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, optBitmap.Bit(refuseChat))
+               binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
 
                // Check auto response
                if optBitmap.Bit(autoResponse) == 1 {
-                       *cc.AutoReply = t.GetField(fieldAutomaticResponse).Data
+                       cc.AutoReply = t.GetField(fieldAutomaticResponse).Data
                } else {
-                       *cc.AutoReply = []byte{}
+                       cc.AutoReply = []byte{}
                }
        }
 
@@ -1398,9 +1401,9 @@ func HandleSetClientUserInfo(cc *ClientConn, t *Transaction) (res []Transaction,
        return res, err
 }
 
-// HandleKeepAlive response to keepalive transactions with an empty reply
-// HL 1.9.2 Client sends keepalive msg every 3 minutes
-// HL 1.2.3 Client doesn't send keepalives
+// HandleKeepAlive responds to keepalive transactions with an empty reply
+// HL 1.9.2 Client sends keepalive msg every 3 minutes
+// HL 1.2.3 Client doesn't send keepalives
 func HandleKeepAlive(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
        res = append(res, cc.NewReply(t))
 
@@ -1614,3 +1617,41 @@ func HandleSetChatSubject(cc *ClientConn, t *Transaction) (res []Transaction, er
 
        return res, err
 }
+
+// HandleMakeAlias makes a file alias using the specified path.
+// Fields used in the request:
+// 201 File name
+// 202 File path
+// 212 File new path   Destination path
+//
+// Fields used in the reply:
+// None
+func HandleMakeAlias(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
+       if !authorize(cc.Account.Access, accessMakeAlias) {
+               res = append(res, cc.NewErrReply(t, "You are not allowed to make aliases."))
+               return res, err
+       }
+       fileName := t.GetField(fieldFileName).Data
+       filePath := t.GetField(fieldFilePath).Data
+       fileNewPath := t.GetField(fieldFileNewPath).Data
+
+       fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
+       if err != nil {
+               return res, err
+       }
+
+       fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, fileNewPath, fileName)
+       if err != nil {
+               return res, err
+       }
+
+       cc.Server.Logger.Debugw("Make alias", "src", fullFilePath, "dst", fullNewFilePath)
+
+       if err := FS.Symlink(fullFilePath, fullNewFilePath); err != nil {
+               res = append(res, cc.NewErrReply(t, "Error creating alias"))
+               return res, nil
+       }
+
+       res = append(res, cc.NewReply(t))
+       return res, err
+}