18 type HandlerFunc func(*ClientConn, *Transaction) ([]Transaction, error)
20 type TransactionType struct {
21 Handler HandlerFunc // function for handling the transaction type
22 Name string // Name of transaction as it will appear in logging
23 RequiredFields []requiredField
26 var TransactionHandlers = map[uint16]TransactionType{
32 TranNotifyChangeUser: {
33 Name: "TranNotifyChangeUser",
39 Name: "TranShowAgreement",
42 Name: "TranUserAccess",
44 TranNotifyDeleteUser: {
45 Name: "TranNotifyDeleteUser",
49 Handler: HandleTranAgreed,
53 Handler: HandleChatSend,
54 RequiredFields: []requiredField{
62 Name: "TranDelNewsArt",
63 Handler: HandleDelNewsArt,
66 Name: "TranDelNewsItem",
67 Handler: HandleDelNewsItem,
70 Name: "TranDeleteFile",
71 Handler: HandleDeleteFile,
74 Name: "TranDeleteUser",
75 Handler: HandleDeleteUser,
78 Name: "TranDisconnectUser",
79 Handler: HandleDisconnectUser,
82 Name: "TranDownloadFile",
83 Handler: HandleDownloadFile,
86 Name: "TranDownloadFldr",
87 Handler: HandleDownloadFolder,
89 TranGetClientInfoText: {
90 Name: "TranGetClientInfoText",
91 Handler: HandleGetClientInfoText,
94 Name: "TranGetFileInfo",
95 Handler: HandleGetFileInfo,
97 TranGetFileNameList: {
98 Name: "TranGetFileNameList",
99 Handler: HandleGetFileNameList,
103 Handler: HandleGetMsgs,
105 TranGetNewsArtData: {
106 Name: "TranGetNewsArtData",
107 Handler: HandleGetNewsArtData,
109 TranGetNewsArtNameList: {
110 Name: "TranGetNewsArtNameList",
111 Handler: HandleGetNewsArtNameList,
113 TranGetNewsCatNameList: {
114 Name: "TranGetNewsCatNameList",
115 Handler: HandleGetNewsCatNameList,
119 Handler: HandleGetUser,
121 TranGetUserNameList: {
122 Name: "tranHandleGetUserNameList",
123 Handler: HandleGetUserNameList,
126 Name: "TranInviteNewChat",
127 Handler: HandleInviteNewChat,
130 Name: "TranInviteToChat",
131 Handler: HandleInviteToChat,
134 Name: "TranJoinChat",
135 Handler: HandleJoinChat,
138 Name: "TranKeepAlive",
139 Handler: HandleKeepAlive,
142 Name: "TranJoinChat",
143 Handler: HandleLeaveChat,
146 Name: "TranListUsers",
147 Handler: HandleListUsers,
150 Name: "TranMoveFile",
151 Handler: HandleMoveFile,
154 Name: "TranNewFolder",
155 Handler: HandleNewFolder,
158 Name: "TranNewNewsCat",
159 Handler: HandleNewNewsCat,
162 Name: "TranNewNewsFldr",
163 Handler: HandleNewNewsFldr,
167 Handler: HandleNewUser,
170 Name: "TranUpdateUser",
171 Handler: HandleUpdateUser,
174 Name: "TranOldPostNews",
175 Handler: HandleTranOldPostNews,
178 Name: "TranPostNewsArt",
179 Handler: HandlePostNewsArt,
181 TranRejectChatInvite: {
182 Name: "TranRejectChatInvite",
183 Handler: HandleRejectChatInvite,
185 TranSendInstantMsg: {
186 Name: "TranSendInstantMsg",
187 Handler: HandleSendInstantMsg,
188 RequiredFields: []requiredField{
198 TranSetChatSubject: {
199 Name: "TranSetChatSubject",
200 Handler: HandleSetChatSubject,
203 Name: "TranMakeFileAlias",
204 Handler: HandleMakeAlias,
205 RequiredFields: []requiredField{
206 {ID: FieldFileName, minLen: 1},
207 {ID: FieldFilePath, minLen: 1},
208 {ID: FieldFileNewPath, minLen: 1},
211 TranSetClientUserInfo: {
212 Name: "TranSetClientUserInfo",
213 Handler: HandleSetClientUserInfo,
216 Name: "TranSetFileInfo",
217 Handler: HandleSetFileInfo,
221 Handler: HandleSetUser,
224 Name: "TranUploadFile",
225 Handler: HandleUploadFile,
228 Name: "TranUploadFldr",
229 Handler: HandleUploadFolder,
232 Name: "TranUserBroadcast",
233 Handler: HandleUserBroadcast,
235 TranDownloadBanner: {
236 Name: "TranDownloadBanner",
237 Handler: HandleDownloadBanner,
241 func HandleChatSend(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
242 if !cc.Authorize(accessSendChat) {
243 res = append(res, cc.NewErrReply(t, "You are not allowed to participate in chat."))
247 // Truncate long usernames
248 trunc := fmt.Sprintf("%13s", cc.UserName)
249 formattedMsg := fmt.Sprintf("\r%.14s: %s", trunc, t.GetField(FieldData).Data)
251 // By holding the option key, Hotline chat allows users to send /me formatted messages like:
252 // *** Halcyon does stuff
253 // This is indicated by the presence of the optional field FieldChatOptions set to a value of 1.
254 // Most clients do not send this option for normal chat messages.
255 if t.GetField(FieldChatOptions).Data != nil && bytes.Equal(t.GetField(FieldChatOptions).Data, []byte{0, 1}) {
256 formattedMsg = fmt.Sprintf("\r*** %s %s", cc.UserName, t.GetField(FieldData).Data)
259 // The ChatID field is used to identify messages as belonging to a private chat.
260 // All clients *except* Frogblast omit this field for public chat, but Frogblast sends a value of 00 00 00 00.
261 chatID := t.GetField(FieldChatID).Data
262 if chatID != nil && !bytes.Equal([]byte{0, 0, 0, 0}, chatID) {
263 chatInt := binary.BigEndian.Uint32(chatID)
264 privChat := cc.Server.PrivateChats[chatInt]
266 clients := sortedClients(privChat.ClientConn)
268 // send the message to all connected clients of the private chat
269 for _, c := range clients {
270 res = append(res, *NewTransaction(
273 NewField(FieldChatID, chatID),
274 NewField(FieldData, []byte(formattedMsg)),
280 for _, c := range sortedClients(cc.Server.Clients) {
281 // Filter out clients that do not have the read chat permission
282 if c.Authorize(accessReadChat) {
283 res = append(res, *NewTransaction(TranChatMsg, c.ID, NewField(FieldData, []byte(formattedMsg))))
290 // HandleSendInstantMsg sends instant message to the user on the current server.
291 // Fields used in the request:
295 // One of the following values:
296 // - User message (myOpt_UserMessage = 1)
297 // - Refuse message (myOpt_RefuseMessage = 2)
298 // - Refuse chat (myOpt_RefuseChat = 3)
299 // - Automatic response (myOpt_AutomaticResponse = 4)"
301 // 214 Quoting message Optional
303 // Fields used in the reply:
305 func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
306 if !cc.Authorize(accessSendPrivMsg) {
307 res = append(res, cc.NewErrReply(t, "You are not allowed to send private messages."))
311 msg := t.GetField(FieldData)
312 ID := t.GetField(FieldUserID)
314 reply := NewTransaction(
317 NewField(FieldData, msg.Data),
318 NewField(FieldUserName, cc.UserName),
319 NewField(FieldUserID, *cc.ID),
320 NewField(FieldOptions, []byte{0, 1}),
323 // Later versions of Hotline include the original message in the FieldQuotingMsg field so
324 // the receiving client can display both the received message and what it is in reply to
325 if t.GetField(FieldQuotingMsg).Data != nil {
326 reply.Fields = append(reply.Fields, NewField(FieldQuotingMsg, t.GetField(FieldQuotingMsg).Data))
329 id, _ := byteToInt(ID.Data)
330 otherClient, ok := cc.Server.Clients[uint16(id)]
332 return res, errors.New("invalid client ID")
335 // Check if target user has "Refuse private messages" flag
336 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(otherClient.Flags)))
337 if flagBitmap.Bit(userFLagRefusePChat) == 1 {
342 NewField(FieldData, []byte(string(otherClient.UserName)+" does not accept private messages.")),
343 NewField(FieldUserName, otherClient.UserName),
344 NewField(FieldUserID, *otherClient.ID),
345 NewField(FieldOptions, []byte{0, 2}),
349 res = append(res, *reply)
352 // Respond with auto reply if other client has it enabled
353 if len(otherClient.AutoReply) > 0 {
358 NewField(FieldData, otherClient.AutoReply),
359 NewField(FieldUserName, otherClient.UserName),
360 NewField(FieldUserID, *otherClient.ID),
361 NewField(FieldOptions, []byte{0, 1}),
366 res = append(res, cc.NewReply(t))
371 func HandleGetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
372 fileName := t.GetField(FieldFileName).Data
373 filePath := t.GetField(FieldFilePath).Data
375 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
380 fw, err := newFileWrapper(cc.Server.FS, fullFilePath, 0)
385 res = append(res, cc.NewReply(t,
386 NewField(FieldFileName, []byte(fw.name)),
387 NewField(FieldFileTypeString, fw.ffo.FlatFileInformationFork.friendlyType()),
388 NewField(FieldFileCreatorString, fw.ffo.FlatFileInformationFork.friendlyCreator()),
389 NewField(FieldFileComment, fw.ffo.FlatFileInformationFork.Comment),
390 NewField(FieldFileType, fw.ffo.FlatFileInformationFork.TypeSignature),
391 NewField(FieldFileCreateDate, fw.ffo.FlatFileInformationFork.CreateDate),
392 NewField(FieldFileModifyDate, fw.ffo.FlatFileInformationFork.ModifyDate),
393 NewField(FieldFileSize, fw.totalSize()),
398 // HandleSetFileInfo updates a file or folder name and/or comment from the Get Info window
399 // Fields used in the request:
401 // * 202 File path Optional
402 // * 211 File new name Optional
403 // * 210 File comment Optional
404 // Fields used in the reply: None
405 func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
406 fileName := t.GetField(FieldFileName).Data
407 filePath := t.GetField(FieldFilePath).Data
409 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
414 fi, err := cc.Server.FS.Stat(fullFilePath)
419 hlFile, err := newFileWrapper(cc.Server.FS, fullFilePath, 0)
423 if t.GetField(FieldFileComment).Data != nil {
424 switch mode := fi.Mode(); {
426 if !cc.Authorize(accessSetFolderComment) {
427 res = append(res, cc.NewErrReply(t, "You are not allowed to set comments for folders."))
430 case mode.IsRegular():
431 if !cc.Authorize(accessSetFileComment) {
432 res = append(res, cc.NewErrReply(t, "You are not allowed to set comments for files."))
437 if err := hlFile.ffo.FlatFileInformationFork.setComment(t.GetField(FieldFileComment).Data); err != nil {
440 w, err := hlFile.infoForkWriter()
444 _, err = w.Write(hlFile.ffo.FlatFileInformationFork.MarshalBinary())
450 fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, t.GetField(FieldFileNewName).Data)
455 fileNewName := t.GetField(FieldFileNewName).Data
457 if fileNewName != nil {
458 switch mode := fi.Mode(); {
460 if !cc.Authorize(accessRenameFolder) {
461 res = append(res, cc.NewErrReply(t, "You are not allowed to rename folders."))
464 err = os.Rename(fullFilePath, fullNewFilePath)
465 if os.IsNotExist(err) {
466 res = append(res, cc.NewErrReply(t, "Cannot rename folder "+string(fileName)+" because it does not exist or cannot be found."))
469 case mode.IsRegular():
470 if !cc.Authorize(accessRenameFile) {
471 res = append(res, cc.NewErrReply(t, "You are not allowed to rename files."))
474 fileDir, err := readPath(cc.Server.Config.FileRoot, filePath, []byte{})
478 hlFile.name = string(fileNewName)
479 err = hlFile.move(fileDir)
480 if os.IsNotExist(err) {
481 res = append(res, cc.NewErrReply(t, "Cannot rename file "+string(fileName)+" because it does not exist or cannot be found."))
490 res = append(res, cc.NewReply(t))
494 // HandleDeleteFile deletes a file or folder
495 // Fields used in the request:
498 // Fields used in the reply: none
499 func HandleDeleteFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
500 fileName := t.GetField(FieldFileName).Data
501 filePath := t.GetField(FieldFilePath).Data
503 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
508 hlFile, err := newFileWrapper(cc.Server.FS, fullFilePath, 0)
513 fi, err := hlFile.dataFile()
515 res = append(res, cc.NewErrReply(t, "Cannot delete file "+string(fileName)+" because it does not exist or cannot be found."))
519 switch mode := fi.Mode(); {
521 if !cc.Authorize(accessDeleteFolder) {
522 res = append(res, cc.NewErrReply(t, "You are not allowed to delete folders."))
525 case mode.IsRegular():
526 if !cc.Authorize(accessDeleteFile) {
527 res = append(res, cc.NewErrReply(t, "You are not allowed to delete files."))
532 if err := hlFile.delete(); err != nil {
536 res = append(res, cc.NewReply(t))
540 // HandleMoveFile moves files or folders. Note: seemingly not documented
541 func HandleMoveFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
542 fileName := string(t.GetField(FieldFileName).Data)
544 filePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(FieldFilePath).Data, t.GetField(FieldFileName).Data)
549 fileNewPath, err := readPath(cc.Server.Config.FileRoot, t.GetField(FieldFileNewPath).Data, nil)
554 cc.logger.Infow("Move file", "src", filePath+"/"+fileName, "dst", fileNewPath+"/"+fileName)
556 hlFile, err := newFileWrapper(cc.Server.FS, filePath, 0)
561 fi, err := hlFile.dataFile()
563 res = append(res, cc.NewErrReply(t, "Cannot delete file "+fileName+" because it does not exist or cannot be found."))
569 switch mode := fi.Mode(); {
571 if !cc.Authorize(accessMoveFolder) {
572 res = append(res, cc.NewErrReply(t, "You are not allowed to move folders."))
575 case mode.IsRegular():
576 if !cc.Authorize(accessMoveFile) {
577 res = append(res, cc.NewErrReply(t, "You are not allowed to move files."))
581 if err := hlFile.move(fileNewPath); err != nil {
584 // TODO: handle other possible errors; e.g. fileWrapper delete fails due to fileWrapper permission issue
586 res = append(res, cc.NewReply(t))
590 func HandleNewFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
591 if !cc.Authorize(accessCreateFolder) {
592 res = append(res, cc.NewErrReply(t, "You are not allowed to create folders."))
595 folderName := string(t.GetField(FieldFileName).Data)
597 folderName = path.Join("/", folderName)
601 // FieldFilePath is only present for nested paths
602 if t.GetField(FieldFilePath).Data != nil {
604 _, err := newFp.Write(t.GetField(FieldFilePath).Data)
609 for _, pathItem := range newFp.Items {
610 subPath = filepath.Join("/", subPath, string(pathItem.Name))
613 newFolderPath := path.Join(cc.Server.Config.FileRoot, subPath, folderName)
615 // TODO: check path and folder name lengths
617 if _, err := cc.Server.FS.Stat(newFolderPath); !os.IsNotExist(err) {
618 msg := fmt.Sprintf("Cannot create folder \"%s\" because there is already a file or folder with that name.", folderName)
619 return []Transaction{cc.NewErrReply(t, msg)}, nil
622 // TODO: check for disallowed characters to maintain compatibility for original client
624 if err := cc.Server.FS.Mkdir(newFolderPath, 0777); err != nil {
625 msg := fmt.Sprintf("Cannot create folder \"%s\" because an error occurred.", folderName)
626 return []Transaction{cc.NewErrReply(t, msg)}, nil
629 res = append(res, cc.NewReply(t))
633 func HandleSetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
634 if !cc.Authorize(accessModifyUser) {
635 res = append(res, cc.NewErrReply(t, "You are not allowed to modify accounts."))
639 login := DecodeUserString(t.GetField(FieldUserLogin).Data)
640 userName := string(t.GetField(FieldUserName).Data)
642 newAccessLvl := t.GetField(FieldUserAccess).Data
644 account := cc.Server.Accounts[login]
645 account.Name = userName
646 copy(account.Access[:], newAccessLvl)
648 // If the password field is cleared in the Hotline edit user UI, the SetUser transaction does
649 // not include FieldUserPassword
650 if t.GetField(FieldUserPassword).Data == nil {
651 account.Password = hashAndSalt([]byte(""))
653 if len(t.GetField(FieldUserPassword).Data) > 1 {
654 account.Password = hashAndSalt(t.GetField(FieldUserPassword).Data)
657 out, err := yaml.Marshal(&account)
661 if err := os.WriteFile(filepath.Join(cc.Server.ConfigDir, "Users", login+".yaml"), out, 0666); err != nil {
665 // Notify connected clients logged in as the user of the new access level
666 for _, c := range cc.Server.Clients {
667 if c.Account.Login == login {
668 // Note: comment out these two lines to test server-side deny messages
669 newT := NewTransaction(TranUserAccess, c.ID, NewField(FieldUserAccess, newAccessLvl))
670 res = append(res, *newT)
672 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(c.Flags)))
673 if c.Authorize(accessDisconUser) {
674 flagBitmap.SetBit(flagBitmap, userFlagAdmin, 1)
676 flagBitmap.SetBit(flagBitmap, userFlagAdmin, 0)
678 binary.BigEndian.PutUint16(c.Flags, uint16(flagBitmap.Int64()))
680 c.Account.Access = account.Access
683 TranNotifyChangeUser,
684 NewField(FieldUserID, *c.ID),
685 NewField(FieldUserFlags, c.Flags),
686 NewField(FieldUserName, c.UserName),
687 NewField(FieldUserIconID, c.Icon),
692 res = append(res, cc.NewReply(t))
696 func HandleGetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
697 if !cc.Authorize(accessOpenUser) {
698 res = append(res, cc.NewErrReply(t, "You are not allowed to view accounts."))
702 account := cc.Server.Accounts[string(t.GetField(FieldUserLogin).Data)]
704 res = append(res, cc.NewErrReply(t, "Account does not exist."))
708 res = append(res, cc.NewReply(t,
709 NewField(FieldUserName, []byte(account.Name)),
710 NewField(FieldUserLogin, negateString(t.GetField(FieldUserLogin).Data)),
711 NewField(FieldUserPassword, []byte(account.Password)),
712 NewField(FieldUserAccess, account.Access[:]),
717 func HandleListUsers(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
718 if !cc.Authorize(accessOpenUser) {
719 res = append(res, cc.NewErrReply(t, "You are not allowed to view accounts."))
723 var userFields []Field
724 for _, acc := range cc.Server.Accounts {
725 b := make([]byte, 0, 100)
726 n, err := acc.Read(b)
731 userFields = append(userFields, NewField(FieldData, b[:n]))
734 res = append(res, cc.NewReply(t, userFields...))
738 // HandleUpdateUser is used by the v1.5+ multi-user editor to perform account editing for multiple users at a time.
739 // An update can be a mix of these actions:
742 // * Modify user (including renaming the account login)
744 // The Transaction sent by the client includes one data field per user that was modified. This data field in turn
745 // contains another data field encoded in its payload with a varying number of sub fields depending on which action is
746 // performed. This seems to be the only place in the Hotline protocol where a data field contains another data field.
747 func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
748 for _, field := range t.Fields {
749 subFields, err := ReadFields(field.Data[0:2], field.Data[2:])
754 if len(subFields) == 1 {
755 login := DecodeUserString(getField(FieldData, &subFields).Data)
756 cc.logger.Infow("DeleteUser", "login", login)
758 if !cc.Authorize(accessDeleteUser) {
759 res = append(res, cc.NewErrReply(t, "You are not allowed to delete accounts."))
763 if err := cc.Server.DeleteUser(login); err != nil {
769 login := DecodeUserString(getField(FieldUserLogin, &subFields).Data)
771 // check if the login dataFile; if so, we know we are updating an existing user
772 if acc, ok := cc.Server.Accounts[login]; ok {
773 cc.logger.Infow("UpdateUser", "login", login)
775 // account dataFile, so this is an update action
776 if !cc.Authorize(accessModifyUser) {
777 res = append(res, cc.NewErrReply(t, "You are not allowed to modify accounts."))
781 if getField(FieldUserPassword, &subFields) != nil {
782 newPass := getField(FieldUserPassword, &subFields).Data
783 acc.Password = hashAndSalt(newPass)
785 acc.Password = hashAndSalt([]byte(""))
788 if getField(FieldUserAccess, &subFields) != nil {
789 copy(acc.Access[:], getField(FieldUserAccess, &subFields).Data)
792 err = cc.Server.UpdateUser(
793 DecodeUserString(getField(FieldData, &subFields).Data),
794 DecodeUserString(getField(FieldUserLogin, &subFields).Data),
795 string(getField(FieldUserName, &subFields).Data),
803 cc.logger.Infow("CreateUser", "login", login)
805 if !cc.Authorize(accessCreateUser) {
806 res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts."))
810 newAccess := accessBitmap{}
811 copy(newAccess[:], getField(FieldUserAccess, &subFields).Data)
813 // Prevent account from creating new account with greater permission
814 for i := 0; i < 64; i++ {
815 if newAccess.IsSet(i) {
816 if !cc.Authorize(i) {
817 return append(res, cc.NewErrReply(t, "Cannot create account with more access than yourself.")), err
822 err := cc.Server.NewUser(login, string(getField(FieldUserName, &subFields).Data), string(getField(FieldUserPassword, &subFields).Data), newAccess)
824 return []Transaction{}, err
829 res = append(res, cc.NewReply(t))
833 // HandleNewUser creates a new user account
834 func HandleNewUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
835 if !cc.Authorize(accessCreateUser) {
836 res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts."))
840 login := DecodeUserString(t.GetField(FieldUserLogin).Data)
842 // If the account already dataFile, reply with an error
843 if _, ok := cc.Server.Accounts[login]; ok {
844 res = append(res, cc.NewErrReply(t, "Cannot create account "+login+" because there is already an account with that login."))
848 newAccess := accessBitmap{}
849 copy(newAccess[:], t.GetField(FieldUserAccess).Data)
851 // Prevent account from creating new account with greater permission
852 for i := 0; i < 64; i++ {
853 if newAccess.IsSet(i) {
854 if !cc.Authorize(i) {
855 res = append(res, cc.NewErrReply(t, "Cannot create account with more access than yourself."))
861 if err := cc.Server.NewUser(login, string(t.GetField(FieldUserName).Data), string(t.GetField(FieldUserPassword).Data), newAccess); err != nil {
862 return []Transaction{}, err
865 res = append(res, cc.NewReply(t))
869 func HandleDeleteUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
870 if !cc.Authorize(accessDeleteUser) {
871 res = append(res, cc.NewErrReply(t, "You are not allowed to delete accounts."))
875 // TODO: Handle case where account doesn't exist; e.g. delete race condition
876 login := DecodeUserString(t.GetField(FieldUserLogin).Data)
878 if err := cc.Server.DeleteUser(login); err != nil {
882 res = append(res, cc.NewReply(t))
886 // HandleUserBroadcast sends an Administrator Message to all connected clients of the server
887 func HandleUserBroadcast(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
888 if !cc.Authorize(accessBroadcast) {
889 res = append(res, cc.NewErrReply(t, "You are not allowed to send broadcast messages."))
895 NewField(FieldData, t.GetField(TranGetMsgs).Data),
896 NewField(FieldChatOptions, []byte{0}),
899 res = append(res, cc.NewReply(t))
903 // HandleGetClientInfoText returns user information for the specific user.
905 // Fields used in the request:
908 // Fields used in the reply:
910 // 101 Data User info text string
911 func HandleGetClientInfoText(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
912 if !cc.Authorize(accessGetClientInfo) {
913 res = append(res, cc.NewErrReply(t, "You are not allowed to get client info."))
917 clientID, _ := byteToInt(t.GetField(FieldUserID).Data)
919 clientConn := cc.Server.Clients[uint16(clientID)]
920 if clientConn == nil {
921 return append(res, cc.NewErrReply(t, "User not found.")), err
924 res = append(res, cc.NewReply(t,
925 NewField(FieldData, []byte(clientConn.String())),
926 NewField(FieldUserName, clientConn.UserName),
931 func HandleGetUserNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
932 res = append(res, cc.NewReply(t, cc.Server.connectedUsers()...))
937 func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
938 if t.GetField(FieldUserName).Data != nil {
939 if cc.Authorize(accessAnyName) {
940 cc.UserName = t.GetField(FieldUserName).Data
942 cc.UserName = []byte(cc.Account.Name)
946 cc.Icon = t.GetField(FieldUserIconID).Data
948 cc.logger = cc.logger.With("name", string(cc.UserName))
949 cc.logger.Infow("Login successful", "clientVersion", fmt.Sprintf("%v", func() int { i, _ := byteToInt(cc.Version); return i }()))
951 options := t.GetField(FieldOptions).Data
952 optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
954 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(cc.Flags)))
956 // Check refuse private PM option
957 if optBitmap.Bit(refusePM) == 1 {
958 flagBitmap.SetBit(flagBitmap, userFlagRefusePM, 1)
959 binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64()))
962 // Check refuse private chat option
963 if optBitmap.Bit(refuseChat) == 1 {
964 flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, 1)
965 binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64()))
968 // Check auto response
969 if optBitmap.Bit(autoResponse) == 1 {
970 cc.AutoReply = t.GetField(FieldAutomaticResponse).Data
972 cc.AutoReply = []byte{}
975 trans := cc.notifyOthers(
977 TranNotifyChangeUser, nil,
978 NewField(FieldUserName, cc.UserName),
979 NewField(FieldUserID, *cc.ID),
980 NewField(FieldUserIconID, cc.Icon),
981 NewField(FieldUserFlags, cc.Flags),
984 res = append(res, trans...)
986 if cc.Server.Config.BannerFile != "" {
987 res = append(res, *NewTransaction(TranServerBanner, cc.ID, NewField(FieldBannerType, []byte("JPEG"))))
990 res = append(res, cc.NewReply(t))
995 // HandleTranOldPostNews updates the flat news
996 // Fields used in this request:
998 func HandleTranOldPostNews(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
999 if !cc.Authorize(accessNewsPostArt) {
1000 res = append(res, cc.NewErrReply(t, "You are not allowed to post news."))
1004 cc.Server.flatNewsMux.Lock()
1005 defer cc.Server.flatNewsMux.Unlock()
1007 newsDateTemplate := defaultNewsDateFormat
1008 if cc.Server.Config.NewsDateFormat != "" {
1009 newsDateTemplate = cc.Server.Config.NewsDateFormat
1012 newsTemplate := defaultNewsTemplate
1013 if cc.Server.Config.NewsDelimiter != "" {
1014 newsTemplate = cc.Server.Config.NewsDelimiter
1017 newsPost := fmt.Sprintf(newsTemplate+"\r", cc.UserName, time.Now().Format(newsDateTemplate), t.GetField(FieldData).Data)
1018 newsPost = strings.ReplaceAll(newsPost, "\n", "\r")
1020 // update news in memory
1021 cc.Server.FlatNews = append([]byte(newsPost), cc.Server.FlatNews...)
1023 // update news on disk
1024 if err := cc.Server.FS.WriteFile(filepath.Join(cc.Server.ConfigDir, "MessageBoard.txt"), cc.Server.FlatNews, 0644); err != nil {
1028 // Notify all clients of updated news
1031 NewField(FieldData, []byte(newsPost)),
1034 res = append(res, cc.NewReply(t))
1038 func HandleDisconnectUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1039 if !cc.Authorize(accessDisconUser) {
1040 res = append(res, cc.NewErrReply(t, "You are not allowed to disconnect users."))
1044 clientConn := cc.Server.Clients[binary.BigEndian.Uint16(t.GetField(FieldUserID).Data)]
1046 if clientConn.Authorize(accessCannotBeDiscon) {
1047 res = append(res, cc.NewErrReply(t, clientConn.Account.Login+" is not allowed to be disconnected."))
1051 // If FieldOptions is set, then the client IP is banned in addition to disconnected.
1052 // 00 01 = temporary ban
1053 // 00 02 = permanent ban
1054 if t.GetField(FieldOptions).Data != nil {
1055 switch t.GetField(FieldOptions).Data[1] {
1057 // send message: "You are temporarily banned on this server"
1058 cc.logger.Infow("Disconnect & temporarily ban " + string(clientConn.UserName))
1060 res = append(res, *NewTransaction(
1063 NewField(FieldData, []byte("You are temporarily banned on this server")),
1064 NewField(FieldChatOptions, []byte{0, 0}),
1067 banUntil := time.Now().Add(tempBanDuration)
1068 cc.Server.banList[strings.Split(clientConn.RemoteAddr, ":")[0]] = &banUntil
1069 cc.Server.writeBanList()
1071 // send message: "You are permanently banned on this server"
1072 cc.logger.Infow("Disconnect & ban " + string(clientConn.UserName))
1074 res = append(res, *NewTransaction(
1077 NewField(FieldData, []byte("You are permanently banned on this server")),
1078 NewField(FieldChatOptions, []byte{0, 0}),
1081 cc.Server.banList[strings.Split(clientConn.RemoteAddr, ":")[0]] = nil
1082 cc.Server.writeBanList()
1086 // TODO: remove this awful hack
1088 time.Sleep(1 * time.Second)
1089 clientConn.Disconnect()
1092 return append(res, cc.NewReply(t)), err
1095 // HandleGetNewsCatNameList returns a list of news categories for a path
1096 // Fields used in the request:
1097 // 325 News path (Optional)
1098 func HandleGetNewsCatNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1099 if !cc.Authorize(accessNewsReadArt) {
1100 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1104 pathStrs := ReadNewsPath(t.GetField(FieldNewsPath).Data)
1105 cats := cc.Server.GetNewsCatByPath(pathStrs)
1107 // To store the keys in slice in sorted order
1108 keys := make([]string, len(cats))
1110 for k := range cats {
1116 var fieldData []Field
1117 for _, k := range keys {
1119 b, _ := cat.MarshalBinary()
1120 fieldData = append(fieldData, NewField(
1121 FieldNewsCatListData15,
1126 res = append(res, cc.NewReply(t, fieldData...))
1130 func HandleNewNewsCat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1131 if !cc.Authorize(accessNewsCreateCat) {
1132 res = append(res, cc.NewErrReply(t, "You are not allowed to create news categories."))
1136 name := string(t.GetField(FieldNewsCatName).Data)
1137 pathStrs := ReadNewsPath(t.GetField(FieldNewsPath).Data)
1139 cats := cc.Server.GetNewsCatByPath(pathStrs)
1140 cats[name] = NewsCategoryListData15{
1143 Articles: map[uint32]*NewsArtData{},
1144 SubCats: make(map[string]NewsCategoryListData15),
1147 if err := cc.Server.writeThreadedNews(); err != nil {
1150 res = append(res, cc.NewReply(t))
1154 // Fields used in the request:
1155 // 322 News category name
1157 func HandleNewNewsFldr(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1158 if !cc.Authorize(accessNewsCreateFldr) {
1159 res = append(res, cc.NewErrReply(t, "You are not allowed to create news folders."))
1163 name := string(t.GetField(FieldFileName).Data)
1164 pathStrs := ReadNewsPath(t.GetField(FieldNewsPath).Data)
1166 cc.logger.Infof("Creating new news folder %s", name)
1168 cats := cc.Server.GetNewsCatByPath(pathStrs)
1169 cats[name] = NewsCategoryListData15{
1172 Articles: map[uint32]*NewsArtData{},
1173 SubCats: make(map[string]NewsCategoryListData15),
1175 if err := cc.Server.writeThreadedNews(); err != nil {
1178 res = append(res, cc.NewReply(t))
1182 // HandleGetNewsArtData gets the list of article names at the specified news path.
1184 // Fields used in the request:
1185 // 325 News path Optional
1187 // Fields used in the reply:
1188 // 321 News article list data Optional
1189 func HandleGetNewsArtNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1190 if !cc.Authorize(accessNewsReadArt) {
1191 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1194 pathStrs := ReadNewsPath(t.GetField(FieldNewsPath).Data)
1196 var cat NewsCategoryListData15
1197 cats := cc.Server.ThreadedNews.Categories
1199 for _, fp := range pathStrs {
1201 cats = cats[fp].SubCats
1204 nald := cat.GetNewsArtListData()
1206 res = append(res, cc.NewReply(t, NewField(FieldNewsArtListData, nald.Payload())))
1210 // HandleGetNewsArtData requests information about the specific news article.
1211 // Fields used in the request:
1215 // 326 News article ID
1216 // 327 News article data flavor
1218 // Fields used in the reply:
1219 // 328 News article title
1220 // 329 News article poster
1221 // 330 News article date
1222 // 331 Previous article ID
1223 // 332 Next article ID
1224 // 335 Parent article ID
1225 // 336 First child article ID
1226 // 327 News article data flavor "Should be “text/plain”
1227 // 333 News article data Optional (if data flavor is “text/plain”)
1228 func HandleGetNewsArtData(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1229 if !cc.Authorize(accessNewsReadArt) {
1230 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1234 var cat NewsCategoryListData15
1235 cats := cc.Server.ThreadedNews.Categories
1237 for _, fp := range ReadNewsPath(t.GetField(FieldNewsPath).Data) {
1239 cats = cats[fp].SubCats
1242 // The official Hotline clients will send the article ID as 2 bytes if possible, but
1243 // some third party clients such as Frogblast and Heildrun will always send 4 bytes
1244 convertedID, err := byteToInt(t.GetField(FieldNewsArtID).Data)
1249 art := cat.Articles[uint32(convertedID)]
1251 res = append(res, cc.NewReply(t))
1255 res = append(res, cc.NewReply(t,
1256 NewField(FieldNewsArtTitle, []byte(art.Title)),
1257 NewField(FieldNewsArtPoster, []byte(art.Poster)),
1258 NewField(FieldNewsArtDate, art.Date),
1259 NewField(FieldNewsArtPrevArt, art.PrevArt),
1260 NewField(FieldNewsArtNextArt, art.NextArt),
1261 NewField(FieldNewsArtParentArt, art.ParentArt),
1262 NewField(FieldNewsArt1stChildArt, art.FirstChildArt),
1263 NewField(FieldNewsArtDataFlav, []byte("text/plain")),
1264 NewField(FieldNewsArtData, []byte(art.Data)),
1269 // HandleDelNewsItem deletes an existing threaded news folder or category from the server.
1270 // Fields used in the request:
1272 // Fields used in the reply:
1274 func HandleDelNewsItem(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1275 pathStrs := ReadNewsPath(t.GetField(FieldNewsPath).Data)
1277 cats := cc.Server.ThreadedNews.Categories
1278 delName := pathStrs[len(pathStrs)-1]
1279 if len(pathStrs) > 1 {
1280 for _, fp := range pathStrs[0 : len(pathStrs)-1] {
1281 cats = cats[fp].SubCats
1285 if bytes.Equal(cats[delName].Type, []byte{0, 3}) {
1286 if !cc.Authorize(accessNewsDeleteCat) {
1287 return append(res, cc.NewErrReply(t, "You are not allowed to delete news categories.")), nil
1290 if !cc.Authorize(accessNewsDeleteFldr) {
1291 return append(res, cc.NewErrReply(t, "You are not allowed to delete news folders.")), nil
1295 delete(cats, delName)
1297 if err := cc.Server.writeThreadedNews(); err != nil {
1301 return append(res, cc.NewReply(t)), nil
1304 func HandleDelNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1305 if !cc.Authorize(accessNewsDeleteArt) {
1306 res = append(res, cc.NewErrReply(t, "You are not allowed to delete news articles."))
1312 // 326 News article ID
1313 // 337 News article – recursive delete Delete child articles (1) or not (0)
1314 pathStrs := ReadNewsPath(t.GetField(FieldNewsPath).Data)
1315 ID, err := byteToInt(t.GetField(FieldNewsArtID).Data)
1320 // TODO: Delete recursive
1321 cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1])
1323 catName := pathStrs[len(pathStrs)-1]
1324 cat := cats[catName]
1326 delete(cat.Articles, uint32(ID))
1329 if err := cc.Server.writeThreadedNews(); err != nil {
1333 res = append(res, cc.NewReply(t))
1339 // 326 News article ID ID of the parent article?
1340 // 328 News article title
1341 // 334 News article flags
1342 // 327 News article data flavor Currently “text/plain”
1343 // 333 News article data
1344 func HandlePostNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1345 if !cc.Authorize(accessNewsPostArt) {
1346 res = append(res, cc.NewErrReply(t, "You are not allowed to post news articles."))
1350 pathStrs := ReadNewsPath(t.GetField(FieldNewsPath).Data)
1351 cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1])
1353 catName := pathStrs[len(pathStrs)-1]
1354 cat := cats[catName]
1356 artID, err := byteToInt(t.GetField(FieldNewsArtID).Data)
1360 convertedArtID := uint32(artID)
1361 bs := make([]byte, 4)
1362 binary.BigEndian.PutUint32(bs, convertedArtID)
1364 newArt := NewsArtData{
1365 Title: string(t.GetField(FieldNewsArtTitle).Data),
1366 Poster: string(cc.UserName),
1367 Date: toHotlineTime(time.Now()),
1368 PrevArt: []byte{0, 0, 0, 0},
1369 NextArt: []byte{0, 0, 0, 0},
1371 FirstChildArt: []byte{0, 0, 0, 0},
1372 DataFlav: []byte("text/plain"),
1373 Data: string(t.GetField(FieldNewsArtData).Data),
1377 for k := range cat.Articles {
1378 keys = append(keys, int(k))
1384 prevID := uint32(keys[len(keys)-1])
1387 binary.BigEndian.PutUint32(newArt.PrevArt, prevID)
1389 // Set next article ID
1390 binary.BigEndian.PutUint32(cat.Articles[prevID].NextArt, nextID)
1393 // Update parent article with first child reply
1394 parentID := convertedArtID
1396 parentArt := cat.Articles[parentID]
1398 if bytes.Equal(parentArt.FirstChildArt, []byte{0, 0, 0, 0}) {
1399 binary.BigEndian.PutUint32(parentArt.FirstChildArt, nextID)
1403 cat.Articles[nextID] = &newArt
1406 if err := cc.Server.writeThreadedNews(); err != nil {
1410 res = append(res, cc.NewReply(t))
1414 // HandleGetMsgs returns the flat news data
1415 func HandleGetMsgs(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1416 if !cc.Authorize(accessNewsReadArt) {
1417 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1421 res = append(res, cc.NewReply(t, NewField(FieldData, cc.Server.FlatNews)))
1426 func HandleDownloadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1427 if !cc.Authorize(accessDownloadFile) {
1428 res = append(res, cc.NewErrReply(t, "You are not allowed to download files."))
1432 fileName := t.GetField(FieldFileName).Data
1433 filePath := t.GetField(FieldFilePath).Data
1434 resumeData := t.GetField(FieldFileResumeData).Data
1436 var dataOffset int64
1437 var frd FileResumeData
1438 if resumeData != nil {
1439 if err := frd.UnmarshalBinary(t.GetField(FieldFileResumeData).Data); err != nil {
1442 // TODO: handle rsrc fork offset
1443 dataOffset = int64(binary.BigEndian.Uint32(frd.ForkInfoList[0].DataSize[:]))
1446 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
1451 hlFile, err := newFileWrapper(cc.Server.FS, fullFilePath, dataOffset)
1456 xferSize := hlFile.ffo.TransferSize(0)
1458 ft := cc.newFileTransfer(FileDownload, fileName, filePath, xferSize)
1460 // TODO: refactor to remove this
1461 if resumeData != nil {
1462 var frd FileResumeData
1463 if err := frd.UnmarshalBinary(t.GetField(FieldFileResumeData).Data); err != nil {
1466 ft.fileResumeData = &frd
1469 // Optional field for when a HL v1.5+ client requests file preview
1470 // Used only for TEXT, JPEG, GIFF, BMP or PICT files
1471 // The value will always be 2
1472 if t.GetField(FieldFileTransferOptions).Data != nil {
1473 ft.options = t.GetField(FieldFileTransferOptions).Data
1474 xferSize = hlFile.ffo.FlatFileDataForkHeader.DataSize[:]
1477 res = append(res, cc.NewReply(t,
1478 NewField(FieldRefNum, ft.refNum[:]),
1479 NewField(FieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count
1480 NewField(FieldTransferSize, xferSize),
1481 NewField(FieldFileSize, hlFile.ffo.FlatFileDataForkHeader.DataSize[:]),
1487 // Download all files from the specified folder and sub-folders
1488 func HandleDownloadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1489 if !cc.Authorize(accessDownloadFile) {
1490 res = append(res, cc.NewErrReply(t, "You are not allowed to download folders."))
1494 fullFilePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(FieldFilePath).Data, t.GetField(FieldFileName).Data)
1499 transferSize, err := CalcTotalSize(fullFilePath)
1503 itemCount, err := CalcItemCount(fullFilePath)
1508 fileTransfer := cc.newFileTransfer(FolderDownload, t.GetField(FieldFileName).Data, t.GetField(FieldFilePath).Data, transferSize)
1511 _, err = fp.Write(t.GetField(FieldFilePath).Data)
1516 res = append(res, cc.NewReply(t,
1517 NewField(FieldRefNum, fileTransfer.ReferenceNumber),
1518 NewField(FieldTransferSize, transferSize),
1519 NewField(FieldFolderItemCount, itemCount),
1520 NewField(FieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count
1525 // Upload all files from the local folder and its subfolders to the specified path on the server
1526 // Fields used in the request
1529 // 108 transfer size Total size of all items in the folder
1530 // 220 Folder item count
1531 // 204 File transfer options "Optional Currently set to 1" (TODO: ??)
1532 func HandleUploadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1534 if t.GetField(FieldFilePath).Data != nil {
1535 if _, err = fp.Write(t.GetField(FieldFilePath).Data); err != nil {
1540 // Handle special cases for Upload and Drop Box folders
1541 if !cc.Authorize(accessUploadAnywhere) {
1542 if !fp.IsUploadDir() && !fp.IsDropbox() {
1543 res = append(res, cc.NewErrReply(t, fmt.Sprintf("Cannot accept upload of the folder \"%v\" because you are only allowed to upload to the \"Uploads\" folder.", string(t.GetField(FieldFileName).Data))))
1548 fileTransfer := cc.newFileTransfer(FolderUpload,
1549 t.GetField(FieldFileName).Data,
1550 t.GetField(FieldFilePath).Data,
1551 t.GetField(FieldTransferSize).Data,
1554 fileTransfer.FolderItemCount = t.GetField(FieldFolderItemCount).Data
1556 res = append(res, cc.NewReply(t, NewField(FieldRefNum, fileTransfer.ReferenceNumber)))
1561 // Fields used in the request:
1564 // 204 File transfer options "Optional
1565 // Used only to resume download, currently has value 2"
1566 // 108 File transfer size "Optional used if download is not resumed"
1567 func HandleUploadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1568 if !cc.Authorize(accessUploadFile) {
1569 res = append(res, cc.NewErrReply(t, "You are not allowed to upload files."))
1573 fileName := t.GetField(FieldFileName).Data
1574 filePath := t.GetField(FieldFilePath).Data
1575 transferOptions := t.GetField(FieldFileTransferOptions).Data
1576 transferSize := t.GetField(FieldTransferSize).Data // not sent for resume
1579 if filePath != nil {
1580 if _, err = fp.Write(filePath); err != nil {
1585 // Handle special cases for Upload and Drop Box folders
1586 if !cc.Authorize(accessUploadAnywhere) {
1587 if !fp.IsUploadDir() && !fp.IsDropbox() {
1588 res = append(res, cc.NewErrReply(t, fmt.Sprintf("Cannot accept upload of the file \"%v\" because you are only allowed to upload to the \"Uploads\" folder.", string(fileName))))
1592 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
1597 if _, err := cc.Server.FS.Stat(fullFilePath); err == nil {
1598 res = append(res, cc.NewErrReply(t, fmt.Sprintf("Cannot accept upload because there is already a file named \"%v\". Try choosing a different name.", string(fileName))))
1602 ft := cc.newFileTransfer(FileUpload, fileName, filePath, transferSize)
1604 replyT := cc.NewReply(t, NewField(FieldRefNum, ft.ReferenceNumber))
1606 // client has requested to resume a partially transferred file
1607 if transferOptions != nil {
1608 fileInfo, err := cc.Server.FS.Stat(fullFilePath + incompleteFileSuffix)
1613 offset := make([]byte, 4)
1614 binary.BigEndian.PutUint32(offset, uint32(fileInfo.Size()))
1616 fileResumeData := NewFileResumeData([]ForkInfoList{
1617 *NewForkInfoList(offset),
1620 b, _ := fileResumeData.BinaryMarshal()
1622 ft.TransferSize = offset
1624 replyT.Fields = append(replyT.Fields, NewField(FieldFileResumeData, b))
1627 res = append(res, replyT)
1631 func HandleSetClientUserInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1632 if len(t.GetField(FieldUserIconID).Data) == 4 {
1633 cc.Icon = t.GetField(FieldUserIconID).Data[2:]
1635 cc.Icon = t.GetField(FieldUserIconID).Data
1637 if cc.Authorize(accessAnyName) {
1638 cc.UserName = t.GetField(FieldUserName).Data
1641 // the options field is only passed by the client versions > 1.2.3.
1642 options := t.GetField(FieldOptions).Data
1644 optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
1645 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(cc.Flags)))
1647 flagBitmap.SetBit(flagBitmap, userFlagRefusePM, optBitmap.Bit(refusePM))
1648 binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64()))
1650 flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, optBitmap.Bit(refuseChat))
1651 binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64()))
1653 // Check auto response
1654 if optBitmap.Bit(autoResponse) == 1 {
1655 cc.AutoReply = t.GetField(FieldAutomaticResponse).Data
1657 cc.AutoReply = []byte{}
1661 for _, c := range sortedClients(cc.Server.Clients) {
1662 res = append(res, *NewTransaction(
1663 TranNotifyChangeUser,
1665 NewField(FieldUserID, *cc.ID),
1666 NewField(FieldUserIconID, cc.Icon),
1667 NewField(FieldUserFlags, cc.Flags),
1668 NewField(FieldUserName, cc.UserName),
1675 // HandleKeepAlive responds to keepalive transactions with an empty reply
1676 // * HL 1.9.2 Client sends keepalive msg every 3 minutes
1677 // * HL 1.2.3 Client doesn't send keepalives
1678 func HandleKeepAlive(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1679 res = append(res, cc.NewReply(t))
1684 func HandleGetFileNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1685 fullPath, err := readPath(
1686 cc.Server.Config.FileRoot,
1687 t.GetField(FieldFilePath).Data,
1695 if t.GetField(FieldFilePath).Data != nil {
1696 if _, err = fp.Write(t.GetField(FieldFilePath).Data); err != nil {
1701 // Handle special case for drop box folders
1702 if fp.IsDropbox() && !cc.Authorize(accessViewDropBoxes) {
1703 res = append(res, cc.NewErrReply(t, "You are not allowed to view drop boxes."))
1707 fileNames, err := getFileNameList(fullPath, cc.Server.Config.IgnoreFiles)
1712 res = append(res, cc.NewReply(t, fileNames...))
1717 // =================================
1718 // Hotline private chat flow
1719 // =================================
1720 // 1. ClientA sends TranInviteNewChat to server with user ID to invite
1721 // 2. Server creates new ChatID
1722 // 3. Server sends TranInviteToChat to invitee
1723 // 4. Server replies to ClientA with new Chat ID
1725 // A dialog box pops up in the invitee client with options to accept or decline the invitation.
1726 // If Accepted is clicked:
1727 // 1. ClientB sends TranJoinChat with FieldChatID
1729 // HandleInviteNewChat invites users to new private chat
1730 func HandleInviteNewChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1731 if !cc.Authorize(accessOpenChat) {
1732 res = append(res, cc.NewErrReply(t, "You are not allowed to request private chat."))
1737 targetID := t.GetField(FieldUserID).Data
1738 newChatID := cc.Server.NewPrivateChat(cc)
1740 // Check if target user has "Refuse private chat" flag
1741 binary.BigEndian.Uint16(targetID)
1742 targetClient := cc.Server.Clients[binary.BigEndian.Uint16(targetID)]
1744 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(targetClient.Flags)))
1745 if flagBitmap.Bit(userFLagRefusePChat) == 1 {
1750 NewField(FieldData, []byte(string(targetClient.UserName)+" does not accept private chats.")),
1751 NewField(FieldUserName, targetClient.UserName),
1752 NewField(FieldUserID, *targetClient.ID),
1753 NewField(FieldOptions, []byte{0, 2}),
1761 NewField(FieldChatID, newChatID),
1762 NewField(FieldUserName, cc.UserName),
1763 NewField(FieldUserID, *cc.ID),
1770 NewField(FieldChatID, newChatID),
1771 NewField(FieldUserName, cc.UserName),
1772 NewField(FieldUserID, *cc.ID),
1773 NewField(FieldUserIconID, cc.Icon),
1774 NewField(FieldUserFlags, cc.Flags),
1781 func HandleInviteToChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1782 if !cc.Authorize(accessOpenChat) {
1783 res = append(res, cc.NewErrReply(t, "You are not allowed to request private chat."))
1788 targetID := t.GetField(FieldUserID).Data
1789 chatID := t.GetField(FieldChatID).Data
1795 NewField(FieldChatID, chatID),
1796 NewField(FieldUserName, cc.UserName),
1797 NewField(FieldUserID, *cc.ID),
1803 NewField(FieldChatID, chatID),
1804 NewField(FieldUserName, cc.UserName),
1805 NewField(FieldUserID, *cc.ID),
1806 NewField(FieldUserIconID, cc.Icon),
1807 NewField(FieldUserFlags, cc.Flags),
1814 func HandleRejectChatInvite(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1815 chatID := t.GetField(FieldChatID).Data
1816 chatInt := binary.BigEndian.Uint32(chatID)
1818 privChat := cc.Server.PrivateChats[chatInt]
1820 resMsg := append(cc.UserName, []byte(" declined invitation to chat")...)
1822 for _, c := range sortedClients(privChat.ClientConn) {
1827 NewField(FieldChatID, chatID),
1828 NewField(FieldData, resMsg),
1836 // HandleJoinChat is sent from a v1.8+ Hotline client when the joins a private chat
1837 // Fields used in the reply:
1838 // * 115 Chat subject
1839 // * 300 User name with info (Optional)
1840 // * 300 (more user names with info)
1841 func HandleJoinChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1842 chatID := t.GetField(FieldChatID).Data
1843 chatInt := binary.BigEndian.Uint32(chatID)
1845 privChat := cc.Server.PrivateChats[chatInt]
1847 // Send TranNotifyChatChangeUser to current members of the chat to inform of new user
1848 for _, c := range sortedClients(privChat.ClientConn) {
1851 TranNotifyChatChangeUser,
1853 NewField(FieldChatID, chatID),
1854 NewField(FieldUserName, cc.UserName),
1855 NewField(FieldUserID, *cc.ID),
1856 NewField(FieldUserIconID, cc.Icon),
1857 NewField(FieldUserFlags, cc.Flags),
1862 privChat.ClientConn[cc.uint16ID()] = cc
1864 replyFields := []Field{NewField(FieldChatSubject, []byte(privChat.Subject))}
1865 for _, c := range sortedClients(privChat.ClientConn) {
1870 Name: string(c.UserName),
1873 replyFields = append(replyFields, NewField(FieldUsernameWithInfo, user.Payload()))
1876 res = append(res, cc.NewReply(t, replyFields...))
1880 // HandleLeaveChat is sent from a v1.8+ Hotline client when the user exits a private chat
1881 // Fields used in the request:
1882 // - 114 FieldChatID
1884 // Reply is not expected.
1885 func HandleLeaveChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1886 chatID := t.GetField(FieldChatID).Data
1887 chatInt := binary.BigEndian.Uint32(chatID)
1889 privChat, ok := cc.Server.PrivateChats[chatInt]
1894 delete(privChat.ClientConn, cc.uint16ID())
1896 // Notify members of the private chat that the user has left
1897 for _, c := range sortedClients(privChat.ClientConn) {
1900 TranNotifyChatDeleteUser,
1902 NewField(FieldChatID, chatID),
1903 NewField(FieldUserID, *cc.ID),
1911 // HandleSetChatSubject is sent from a v1.8+ Hotline client when the user sets a private chat subject
1912 // Fields used in the request:
1914 // * 115 Chat subject
1915 // Reply is not expected.
1916 func HandleSetChatSubject(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1917 chatID := t.GetField(FieldChatID).Data
1918 chatInt := binary.BigEndian.Uint32(chatID)
1920 privChat := cc.Server.PrivateChats[chatInt]
1921 privChat.Subject = string(t.GetField(FieldChatSubject).Data)
1923 for _, c := range sortedClients(privChat.ClientConn) {
1926 TranNotifyChatSubject,
1928 NewField(FieldChatID, chatID),
1929 NewField(FieldChatSubject, t.GetField(FieldChatSubject).Data),
1937 // HandleMakeAlias makes a file alias using the specified path.
1938 // Fields used in the request:
1941 // 212 File new path Destination path
1943 // Fields used in the reply:
1945 func HandleMakeAlias(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1946 if !cc.Authorize(accessMakeAlias) {
1947 res = append(res, cc.NewErrReply(t, "You are not allowed to make aliases."))
1950 fileName := t.GetField(FieldFileName).Data
1951 filePath := t.GetField(FieldFilePath).Data
1952 fileNewPath := t.GetField(FieldFileNewPath).Data
1954 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
1959 fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, fileNewPath, fileName)
1964 cc.logger.Debugw("Make alias", "src", fullFilePath, "dst", fullNewFilePath)
1966 if err := cc.Server.FS.Symlink(fullFilePath, fullNewFilePath); err != nil {
1967 res = append(res, cc.NewErrReply(t, "Error creating alias"))
1971 res = append(res, cc.NewReply(t))
1975 // HandleDownloadBanner handles requests for a new banner from the server
1976 // Fields used in the request:
1978 // Fields used in the reply:
1979 // 107 FieldRefNum Used later for transfer
1980 // 108 FieldTransferSize Size of data to be downloaded
1981 func HandleDownloadBanner(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1982 fi, err := cc.Server.FS.Stat(filepath.Join(cc.Server.ConfigDir, cc.Server.Config.BannerFile))
1987 ft := cc.newFileTransfer(bannerDownload, []byte{}, []byte{}, make([]byte, 4))
1989 binary.BigEndian.PutUint32(ft.TransferSize, uint32(fi.Size()))
1991 res = append(res, cc.NewReply(t,
1992 NewField(FieldRefNum, ft.refNum[:]),
1993 NewField(FieldTransferSize, ft.TransferSize),