19 type TransactionType struct {
20 Handler func(*ClientConn, *Transaction) ([]Transaction, error) // function for handling the transaction type
21 Name string // Name of transaction as it will appear in logging
22 RequiredFields []requiredField
25 var TransactionHandlers = map[uint16]TransactionType{
31 tranNotifyChangeUser: {
32 Name: "tranNotifyChangeUser",
38 Name: "tranShowAgreement",
41 Name: "tranUserAccess",
43 tranNotifyDeleteUser: {
44 Name: "tranNotifyDeleteUser",
48 Handler: HandleTranAgreed,
52 Handler: HandleChatSend,
53 RequiredFields: []requiredField{
61 Name: "tranDelNewsArt",
62 Handler: HandleDelNewsArt,
65 Name: "tranDelNewsItem",
66 Handler: HandleDelNewsItem,
69 Name: "tranDeleteFile",
70 Handler: HandleDeleteFile,
73 Name: "tranDeleteUser",
74 Handler: HandleDeleteUser,
77 Name: "tranDisconnectUser",
78 Handler: HandleDisconnectUser,
81 Name: "tranDownloadFile",
82 Handler: HandleDownloadFile,
85 Name: "tranDownloadFldr",
86 Handler: HandleDownloadFolder,
88 tranGetClientInfoText: {
89 Name: "tranGetClientInfoText",
90 Handler: HandleGetClientInfoText,
93 Name: "tranGetFileInfo",
94 Handler: HandleGetFileInfo,
96 tranGetFileNameList: {
97 Name: "tranGetFileNameList",
98 Handler: HandleGetFileNameList,
102 Handler: HandleGetMsgs,
104 tranGetNewsArtData: {
105 Name: "tranGetNewsArtData",
106 Handler: HandleGetNewsArtData,
108 tranGetNewsArtNameList: {
109 Name: "tranGetNewsArtNameList",
110 Handler: HandleGetNewsArtNameList,
112 tranGetNewsCatNameList: {
113 Name: "tranGetNewsCatNameList",
114 Handler: HandleGetNewsCatNameList,
118 Handler: HandleGetUser,
120 tranGetUserNameList: {
121 Name: "tranHandleGetUserNameList",
122 Handler: HandleGetUserNameList,
125 Name: "tranInviteNewChat",
126 Handler: HandleInviteNewChat,
129 Name: "tranInviteToChat",
130 Handler: HandleInviteToChat,
133 Name: "tranJoinChat",
134 Handler: HandleJoinChat,
137 Name: "tranKeepAlive",
138 Handler: HandleKeepAlive,
141 Name: "tranJoinChat",
142 Handler: HandleLeaveChat,
145 Name: "tranListUsers",
146 Handler: HandleListUsers,
149 Name: "tranMoveFile",
150 Handler: HandleMoveFile,
153 Name: "tranNewFolder",
154 Handler: HandleNewFolder,
157 Name: "tranNewNewsCat",
158 Handler: HandleNewNewsCat,
161 Name: "tranNewNewsFldr",
162 Handler: HandleNewNewsFldr,
166 Handler: HandleNewUser,
169 Name: "tranUpdateUser",
170 Handler: HandleUpdateUser,
173 Name: "tranOldPostNews",
174 Handler: HandleTranOldPostNews,
177 Name: "tranPostNewsArt",
178 Handler: HandlePostNewsArt,
180 tranRejectChatInvite: {
181 Name: "tranRejectChatInvite",
182 Handler: HandleRejectChatInvite,
184 tranSendInstantMsg: {
185 Name: "tranSendInstantMsg",
186 Handler: HandleSendInstantMsg,
187 RequiredFields: []requiredField{
197 tranSetChatSubject: {
198 Name: "tranSetChatSubject",
199 Handler: HandleSetChatSubject,
202 Name: "tranMakeFileAlias",
203 Handler: HandleMakeAlias,
204 RequiredFields: []requiredField{
205 {ID: fieldFileName, minLen: 1},
206 {ID: fieldFilePath, minLen: 1},
207 {ID: fieldFileNewPath, minLen: 1},
210 tranSetClientUserInfo: {
211 Name: "tranSetClientUserInfo",
212 Handler: HandleSetClientUserInfo,
215 Name: "tranSetFileInfo",
216 Handler: HandleSetFileInfo,
220 Handler: HandleSetUser,
223 Name: "tranUploadFile",
224 Handler: HandleUploadFile,
227 Name: "tranUploadFldr",
228 Handler: HandleUploadFolder,
231 Name: "tranUserBroadcast",
232 Handler: HandleUserBroadcast,
234 tranDownloadBanner: {
235 Name: "tranDownloadBanner",
236 Handler: HandleDownloadBanner,
240 func HandleChatSend(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
241 if !authorize(cc.Account.Access, accessSendChat) {
242 res = append(res, cc.NewErrReply(t, "You are not allowed to participate in chat."))
246 // Truncate long usernames
247 trunc := fmt.Sprintf("%13s", cc.UserName)
248 formattedMsg := fmt.Sprintf("\r%.14s: %s", trunc, t.GetField(fieldData).Data)
250 // By holding the option key, Hotline chat allows users to send /me formatted messages like:
251 // *** Halcyon does stuff
252 // This is indicated by the presence of the optional field fieldChatOptions in the transaction payload
253 if t.GetField(fieldChatOptions).Data != nil {
254 formattedMsg = fmt.Sprintf("\r*** %s %s", cc.UserName, t.GetField(fieldData).Data)
257 chatID := t.GetField(fieldChatID).Data
258 // a non-nil chatID indicates the message belongs to a private chat
260 chatInt := binary.BigEndian.Uint32(chatID)
261 privChat := cc.Server.PrivateChats[chatInt]
263 clients := sortedClients(privChat.ClientConn)
265 // send the message to all connected clients of the private chat
266 for _, c := range clients {
267 res = append(res, *NewTransaction(
270 NewField(fieldChatID, chatID),
271 NewField(fieldData, []byte(formattedMsg)),
277 for _, c := range sortedClients(cc.Server.Clients) {
278 // Filter out clients that do not have the read chat permission
279 if authorize(c.Account.Access, accessReadChat) {
280 res = append(res, *NewTransaction(tranChatMsg, c.ID, NewField(fieldData, []byte(formattedMsg))))
287 // HandleSendInstantMsg sends instant message to the user on the current server.
288 // Fields used in the request:
291 // One of the following values:
292 // - User message (myOpt_UserMessage = 1)
293 // - Refuse message (myOpt_RefuseMessage = 2)
294 // - Refuse chat (myOpt_RefuseChat = 3)
295 // - Automatic response (myOpt_AutomaticResponse = 4)"
297 // 214 Quoting message Optional
299 // Fields used in the reply:
301 func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
302 msg := t.GetField(fieldData)
303 ID := t.GetField(fieldUserID)
305 reply := NewTransaction(
308 NewField(fieldData, msg.Data),
309 NewField(fieldUserName, cc.UserName),
310 NewField(fieldUserID, *cc.ID),
311 NewField(fieldOptions, []byte{0, 1}),
314 // Later versions of Hotline include the original message in the fieldQuotingMsg field so
315 // the receiving client can display both the received message and what it is in reply to
316 if t.GetField(fieldQuotingMsg).Data != nil {
317 reply.Fields = append(reply.Fields, NewField(fieldQuotingMsg, t.GetField(fieldQuotingMsg).Data))
320 res = append(res, *reply)
322 id, _ := byteToInt(ID.Data)
323 otherClient, ok := cc.Server.Clients[uint16(id)]
325 return res, errors.New("invalid client ID")
328 // Respond with auto reply if other client has it enabled
329 if len(otherClient.AutoReply) > 0 {
334 NewField(fieldData, otherClient.AutoReply),
335 NewField(fieldUserName, otherClient.UserName),
336 NewField(fieldUserID, *otherClient.ID),
337 NewField(fieldOptions, []byte{0, 1}),
342 res = append(res, cc.NewReply(t))
347 func HandleGetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
348 fileName := t.GetField(fieldFileName).Data
349 filePath := t.GetField(fieldFilePath).Data
351 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
356 fw, err := newFileWrapper(cc.Server.FS, fullFilePath, 0)
361 res = append(res, cc.NewReply(t,
362 NewField(fieldFileName, []byte(fw.name)),
363 NewField(fieldFileTypeString, fw.ffo.FlatFileInformationFork.friendlyType()),
364 NewField(fieldFileCreatorString, fw.ffo.FlatFileInformationFork.friendlyCreator()),
365 NewField(fieldFileComment, fw.ffo.FlatFileInformationFork.Comment),
366 NewField(fieldFileType, fw.ffo.FlatFileInformationFork.TypeSignature),
367 NewField(fieldFileCreateDate, fw.ffo.FlatFileInformationFork.CreateDate),
368 NewField(fieldFileModifyDate, fw.ffo.FlatFileInformationFork.ModifyDate),
369 NewField(fieldFileSize, fw.totalSize()),
374 // HandleSetFileInfo updates a file or folder name and/or comment from the Get Info window
375 // Fields used in the request:
377 // * 202 File path Optional
378 // * 211 File new name Optional
379 // * 210 File comment Optional
380 // Fields used in the reply: None
381 func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
382 fileName := t.GetField(fieldFileName).Data
383 filePath := t.GetField(fieldFilePath).Data
385 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
390 fi, err := cc.Server.FS.Stat(fullFilePath)
395 hlFile, err := newFileWrapper(cc.Server.FS, fullFilePath, 0)
399 if t.GetField(fieldFileComment).Data != nil {
400 switch mode := fi.Mode(); {
402 if !authorize(cc.Account.Access, accessSetFolderComment) {
403 res = append(res, cc.NewErrReply(t, "You are not allowed to set comments for folders."))
406 case mode.IsRegular():
407 if !authorize(cc.Account.Access, accessSetFileComment) {
408 res = append(res, cc.NewErrReply(t, "You are not allowed to set comments for files."))
413 if err := hlFile.ffo.FlatFileInformationFork.setComment(t.GetField(fieldFileComment).Data); err != nil {
416 w, err := hlFile.infoForkWriter()
420 _, err = w.Write(hlFile.ffo.FlatFileInformationFork.MarshalBinary())
426 fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, t.GetField(fieldFileNewName).Data)
431 fileNewName := t.GetField(fieldFileNewName).Data
433 if fileNewName != nil {
434 switch mode := fi.Mode(); {
436 if !authorize(cc.Account.Access, accessRenameFolder) {
437 res = append(res, cc.NewErrReply(t, "You are not allowed to rename folders."))
440 err = os.Rename(fullFilePath, fullNewFilePath)
441 if os.IsNotExist(err) {
442 res = append(res, cc.NewErrReply(t, "Cannot rename folder "+string(fileName)+" because it does not exist or cannot be found."))
445 case mode.IsRegular():
446 if !authorize(cc.Account.Access, accessRenameFile) {
447 res = append(res, cc.NewErrReply(t, "You are not allowed to rename files."))
450 fileDir, err := readPath(cc.Server.Config.FileRoot, filePath, []byte{})
454 hlFile.name = string(fileNewName)
455 err = hlFile.move(fileDir)
456 if os.IsNotExist(err) {
457 res = append(res, cc.NewErrReply(t, "Cannot rename file "+string(fileName)+" because it does not exist or cannot be found."))
466 res = append(res, cc.NewReply(t))
470 // HandleDeleteFile deletes a file or folder
471 // Fields used in the request:
474 // Fields used in the reply: none
475 func HandleDeleteFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
476 fileName := t.GetField(fieldFileName).Data
477 filePath := t.GetField(fieldFilePath).Data
479 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
484 hlFile, err := newFileWrapper(cc.Server.FS, fullFilePath, 0)
489 fi, err := hlFile.dataFile()
491 res = append(res, cc.NewErrReply(t, "Cannot delete file "+string(fileName)+" because it does not exist or cannot be found."))
495 switch mode := fi.Mode(); {
497 if !authorize(cc.Account.Access, accessDeleteFolder) {
498 res = append(res, cc.NewErrReply(t, "You are not allowed to delete folders."))
501 case mode.IsRegular():
502 if !authorize(cc.Account.Access, accessDeleteFile) {
503 res = append(res, cc.NewErrReply(t, "You are not allowed to delete files."))
508 if err := hlFile.delete(); err != nil {
512 res = append(res, cc.NewReply(t))
516 // HandleMoveFile moves files or folders. Note: seemingly not documented
517 func HandleMoveFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
518 fileName := string(t.GetField(fieldFileName).Data)
520 filePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFilePath).Data, t.GetField(fieldFileName).Data)
525 fileNewPath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFileNewPath).Data, nil)
530 cc.logger.Infow("Move file", "src", filePath+"/"+fileName, "dst", fileNewPath+"/"+fileName)
532 hlFile, err := newFileWrapper(cc.Server.FS, filePath, 0)
537 fi, err := hlFile.dataFile()
539 res = append(res, cc.NewErrReply(t, "Cannot delete file "+fileName+" because it does not exist or cannot be found."))
545 switch mode := fi.Mode(); {
547 if !authorize(cc.Account.Access, accessMoveFolder) {
548 res = append(res, cc.NewErrReply(t, "You are not allowed to move folders."))
551 case mode.IsRegular():
552 if !authorize(cc.Account.Access, accessMoveFile) {
553 res = append(res, cc.NewErrReply(t, "You are not allowed to move files."))
557 if err := hlFile.move(fileNewPath); err != nil {
560 // TODO: handle other possible errors; e.g. fileWrapper delete fails due to fileWrapper permission issue
562 res = append(res, cc.NewReply(t))
566 func HandleNewFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
567 if !authorize(cc.Account.Access, accessCreateFolder) {
568 res = append(res, cc.NewErrReply(t, "You are not allowed to create folders."))
571 folderName := string(t.GetField(fieldFileName).Data)
573 folderName = path.Join("/", folderName)
577 // fieldFilePath is only present for nested paths
578 if t.GetField(fieldFilePath).Data != nil {
580 err := newFp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
585 for _, pathItem := range newFp.Items {
586 subPath = filepath.Join("/", subPath, string(pathItem.Name))
589 newFolderPath := path.Join(cc.Server.Config.FileRoot, subPath, folderName)
591 // TODO: check path and folder name lengths
593 if _, err := cc.Server.FS.Stat(newFolderPath); !os.IsNotExist(err) {
594 msg := fmt.Sprintf("Cannot create folder \"%s\" because there is already a file or folder with that name.", folderName)
595 return []Transaction{cc.NewErrReply(t, msg)}, nil
598 // TODO: check for disallowed characters to maintain compatibility for original client
600 if err := cc.Server.FS.Mkdir(newFolderPath, 0777); err != nil {
601 msg := fmt.Sprintf("Cannot create folder \"%s\" because an error occurred.", folderName)
602 return []Transaction{cc.NewErrReply(t, msg)}, nil
605 res = append(res, cc.NewReply(t))
609 func HandleSetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
610 if !authorize(cc.Account.Access, accessModifyUser) {
611 res = append(res, cc.NewErrReply(t, "You are not allowed to modify accounts."))
615 login := DecodeUserString(t.GetField(fieldUserLogin).Data)
616 userName := string(t.GetField(fieldUserName).Data)
618 newAccessLvl := t.GetField(fieldUserAccess).Data
620 account := cc.Server.Accounts[login]
621 account.Access = &newAccessLvl
622 account.Name = userName
624 // If the password field is cleared in the Hotline edit user UI, the SetUser transaction does
625 // not include fieldUserPassword
626 if t.GetField(fieldUserPassword).Data == nil {
627 account.Password = hashAndSalt([]byte(""))
629 if len(t.GetField(fieldUserPassword).Data) > 1 {
630 account.Password = hashAndSalt(t.GetField(fieldUserPassword).Data)
633 out, err := yaml.Marshal(&account)
637 if err := os.WriteFile(cc.Server.ConfigDir+"Users/"+login+".yaml", out, 0666); err != nil {
641 // Notify connected clients logged in as the user of the new access level
642 for _, c := range cc.Server.Clients {
643 if c.Account.Login == login {
644 // Note: comment out these two lines to test server-side deny messages
645 newT := NewTransaction(tranUserAccess, c.ID, NewField(fieldUserAccess, newAccessLvl))
646 res = append(res, *newT)
648 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*c.Flags)))
649 if authorize(c.Account.Access, accessDisconUser) {
650 flagBitmap.SetBit(flagBitmap, userFlagAdmin, 1)
652 flagBitmap.SetBit(flagBitmap, userFlagAdmin, 0)
654 binary.BigEndian.PutUint16(*c.Flags, uint16(flagBitmap.Int64()))
656 c.Account.Access = account.Access
659 tranNotifyChangeUser,
660 NewField(fieldUserID, *c.ID),
661 NewField(fieldUserFlags, *c.Flags),
662 NewField(fieldUserName, c.UserName),
663 NewField(fieldUserIconID, *c.Icon),
668 res = append(res, cc.NewReply(t))
672 func HandleGetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
673 if !authorize(cc.Account.Access, accessOpenUser) {
674 res = append(res, cc.NewErrReply(t, "You are not allowed to view accounts."))
678 account := cc.Server.Accounts[string(t.GetField(fieldUserLogin).Data)]
680 res = append(res, cc.NewErrReply(t, "Account does not exist."))
684 res = append(res, cc.NewReply(t,
685 NewField(fieldUserName, []byte(account.Name)),
686 NewField(fieldUserLogin, negateString(t.GetField(fieldUserLogin).Data)),
687 NewField(fieldUserPassword, []byte(account.Password)),
688 NewField(fieldUserAccess, *account.Access),
693 func HandleListUsers(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
694 if !authorize(cc.Account.Access, accessOpenUser) {
695 res = append(res, cc.NewErrReply(t, "You are not allowed to view accounts."))
699 var userFields []Field
700 for _, acc := range cc.Server.Accounts {
701 b := make([]byte, 0, 100)
702 n, err := acc.Read(b)
707 userFields = append(userFields, NewField(fieldData, b[:n]))
710 res = append(res, cc.NewReply(t, userFields...))
714 // HandleUpdateUser is used by the v1.5+ multi-user editor to perform account editing for multiple users at a time.
715 // An update can be a mix of these actions:
718 // * Modify user (including renaming the account login)
720 // The Transaction sent by the client includes one data field per user that was modified. This data field in turn
721 // contains another data field encoded in its payload with a varying number of sub fields depending on which action is
722 // performed. This seems to be the only place in the Hotline protocol where a data field contains another data field.
723 func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
724 for _, field := range t.Fields {
725 subFields, err := ReadFields(field.Data[0:2], field.Data[2:])
730 if len(subFields) == 1 {
731 login := DecodeUserString(getField(fieldData, &subFields).Data)
732 cc.logger.Infow("DeleteUser", "login", login)
734 if !authorize(cc.Account.Access, accessDeleteUser) {
735 res = append(res, cc.NewErrReply(t, "You are not allowed to delete accounts."))
739 if err := cc.Server.DeleteUser(login); err != nil {
745 login := DecodeUserString(getField(fieldUserLogin, &subFields).Data)
747 // check if the login dataFile; if so, we know we are updating an existing user
748 if acc, ok := cc.Server.Accounts[login]; ok {
749 cc.logger.Infow("UpdateUser", "login", login)
751 // account dataFile, so this is an update action
752 if !authorize(cc.Account.Access, accessModifyUser) {
753 res = append(res, cc.NewErrReply(t, "You are not allowed to modify accounts."))
757 if getField(fieldUserPassword, &subFields) != nil {
758 newPass := getField(fieldUserPassword, &subFields).Data
759 acc.Password = hashAndSalt(newPass)
761 acc.Password = hashAndSalt([]byte(""))
764 if getField(fieldUserAccess, &subFields) != nil {
765 acc.Access = &getField(fieldUserAccess, &subFields).Data
768 err = cc.Server.UpdateUser(
769 DecodeUserString(getField(fieldData, &subFields).Data),
770 DecodeUserString(getField(fieldUserLogin, &subFields).Data),
771 string(getField(fieldUserName, &subFields).Data),
779 cc.logger.Infow("CreateUser", "login", login)
781 if !authorize(cc.Account.Access, accessCreateUser) {
782 res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts."))
786 err := cc.Server.NewUser(
788 string(getField(fieldUserName, &subFields).Data),
789 string(getField(fieldUserPassword, &subFields).Data),
790 getField(fieldUserAccess, &subFields).Data,
793 return []Transaction{}, err
798 res = append(res, cc.NewReply(t))
802 // HandleNewUser creates a new user account
803 func HandleNewUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
804 if !authorize(cc.Account.Access, accessCreateUser) {
805 res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts."))
809 login := DecodeUserString(t.GetField(fieldUserLogin).Data)
811 // If the account already dataFile, reply with an error
812 if _, ok := cc.Server.Accounts[login]; ok {
813 res = append(res, cc.NewErrReply(t, "Cannot create account "+login+" because there is already an account with that login."))
817 if err := cc.Server.NewUser(
819 string(t.GetField(fieldUserName).Data),
820 string(t.GetField(fieldUserPassword).Data),
821 t.GetField(fieldUserAccess).Data,
823 return []Transaction{}, err
826 res = append(res, cc.NewReply(t))
830 func HandleDeleteUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
831 if !authorize(cc.Account.Access, accessDeleteUser) {
832 res = append(res, cc.NewErrReply(t, "You are not allowed to delete accounts."))
836 // TODO: Handle case where account doesn't exist; e.g. delete race condition
837 login := DecodeUserString(t.GetField(fieldUserLogin).Data)
839 if err := cc.Server.DeleteUser(login); err != nil {
843 res = append(res, cc.NewReply(t))
847 // HandleUserBroadcast sends an Administrator Message to all connected clients of the server
848 func HandleUserBroadcast(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
849 if !authorize(cc.Account.Access, accessBroadcast) {
850 res = append(res, cc.NewErrReply(t, "You are not allowed to send broadcast messages."))
856 NewField(fieldData, t.GetField(tranGetMsgs).Data),
857 NewField(fieldChatOptions, []byte{0}),
860 res = append(res, cc.NewReply(t))
864 func byteToInt(bytes []byte) (int, error) {
867 return int(binary.BigEndian.Uint16(bytes)), nil
869 return int(binary.BigEndian.Uint32(bytes)), nil
872 return 0, errors.New("unknown byte length")
875 // HandleGetClientInfoText returns user information for the specific user.
877 // Fields used in the request:
880 // Fields used in the reply:
882 // 101 Data User info text string
883 func HandleGetClientInfoText(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
884 if !authorize(cc.Account.Access, accessGetClientInfo) {
885 res = append(res, cc.NewErrReply(t, "You are not allowed to get client info."))
889 clientID, _ := byteToInt(t.GetField(fieldUserID).Data)
891 clientConn := cc.Server.Clients[uint16(clientID)]
892 if clientConn == nil {
893 return append(res, cc.NewErrReply(t, "User not found.")), err
896 res = append(res, cc.NewReply(t,
897 NewField(fieldData, []byte(clientConn.String())),
898 NewField(fieldUserName, clientConn.UserName),
903 func HandleGetUserNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
904 res = append(res, cc.NewReply(t, cc.Server.connectedUsers()...))
909 func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
911 cc.UserName = t.GetField(fieldUserName).Data
912 *cc.Icon = t.GetField(fieldUserIconID).Data
914 cc.logger = cc.logger.With("name", string(cc.UserName))
915 cc.logger.Infow("Login successful", "clientVersion", fmt.Sprintf("%x", *cc.Version))
917 options := t.GetField(fieldOptions).Data
918 optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
920 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags)))
922 // Check refuse private PM option
923 if optBitmap.Bit(refusePM) == 1 {
924 flagBitmap.SetBit(flagBitmap, userFlagRefusePM, 1)
925 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
928 // Check refuse private chat option
929 if optBitmap.Bit(refuseChat) == 1 {
930 flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, 1)
931 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
934 // Check auto response
935 if optBitmap.Bit(autoResponse) == 1 {
936 cc.AutoReply = t.GetField(fieldAutomaticResponse).Data
938 cc.AutoReply = []byte{}
941 for _, t := range cc.notifyOthers(
943 tranNotifyChangeUser, nil,
944 NewField(fieldUserName, cc.UserName),
945 NewField(fieldUserID, *cc.ID),
946 NewField(fieldUserIconID, *cc.Icon),
947 NewField(fieldUserFlags, *cc.Flags),
950 cc.Server.outbox <- t
953 if cc.Server.Config.BannerFile != "" {
954 cc.Server.outbox <- *NewTransaction(tranServerBanner, cc.ID, NewField(fieldBannerType, []byte("JPEG")))
957 res = append(res, cc.NewReply(t))
962 const defaultNewsDateFormat = "Jan02 15:04" // Jun23 20:49
963 // "Mon, 02 Jan 2006 15:04:05 MST"
965 const defaultNewsTemplate = `From %s (%s):
969 __________________________________________________________`
971 // HandleTranOldPostNews updates the flat news
972 // Fields used in this request:
974 func HandleTranOldPostNews(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
975 if !authorize(cc.Account.Access, accessNewsPostArt) {
976 res = append(res, cc.NewErrReply(t, "You are not allowed to post news."))
980 cc.Server.flatNewsMux.Lock()
981 defer cc.Server.flatNewsMux.Unlock()
983 newsDateTemplate := defaultNewsDateFormat
984 if cc.Server.Config.NewsDateFormat != "" {
985 newsDateTemplate = cc.Server.Config.NewsDateFormat
988 newsTemplate := defaultNewsTemplate
989 if cc.Server.Config.NewsDelimiter != "" {
990 newsTemplate = cc.Server.Config.NewsDelimiter
993 newsPost := fmt.Sprintf(newsTemplate+"\r", cc.UserName, time.Now().Format(newsDateTemplate), t.GetField(fieldData).Data)
994 newsPost = strings.Replace(newsPost, "\n", "\r", -1)
996 // update news in memory
997 cc.Server.FlatNews = append([]byte(newsPost), cc.Server.FlatNews...)
999 // update news on disk
1000 if err := ioutil.WriteFile(cc.Server.ConfigDir+"MessageBoard.txt", cc.Server.FlatNews, 0644); err != nil {
1004 // Notify all clients of updated news
1007 NewField(fieldData, []byte(newsPost)),
1010 res = append(res, cc.NewReply(t))
1014 func HandleDisconnectUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1015 if !authorize(cc.Account.Access, accessDisconUser) {
1016 res = append(res, cc.NewErrReply(t, "You are not allowed to disconnect users."))
1020 clientConn := cc.Server.Clients[binary.BigEndian.Uint16(t.GetField(fieldUserID).Data)]
1022 if authorize(clientConn.Account.Access, accessCannotBeDiscon) {
1023 res = append(res, cc.NewErrReply(t, clientConn.Account.Login+" is not allowed to be disconnected."))
1027 if err := clientConn.Connection.Close(); err != nil {
1031 res = append(res, cc.NewReply(t))
1035 // HandleGetNewsCatNameList returns a list of news categories for a path
1036 // Fields used in the request:
1037 // 325 News path (Optional)
1038 func HandleGetNewsCatNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1039 if !authorize(cc.Account.Access, accessNewsReadArt) {
1040 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1044 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1045 cats := cc.Server.GetNewsCatByPath(pathStrs)
1047 // To store the keys in slice in sorted order
1048 keys := make([]string, len(cats))
1050 for k := range cats {
1056 var fieldData []Field
1057 for _, k := range keys {
1059 b, _ := cat.MarshalBinary()
1060 fieldData = append(fieldData, NewField(
1061 fieldNewsCatListData15,
1066 res = append(res, cc.NewReply(t, fieldData...))
1070 func HandleNewNewsCat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1071 if !authorize(cc.Account.Access, accessNewsCreateCat) {
1072 res = append(res, cc.NewErrReply(t, "You are not allowed to create news categories."))
1076 name := string(t.GetField(fieldNewsCatName).Data)
1077 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1079 cats := cc.Server.GetNewsCatByPath(pathStrs)
1080 cats[name] = NewsCategoryListData15{
1083 Articles: map[uint32]*NewsArtData{},
1084 SubCats: make(map[string]NewsCategoryListData15),
1087 if err := cc.Server.writeThreadedNews(); err != nil {
1090 res = append(res, cc.NewReply(t))
1094 // Fields used in the request:
1095 // 322 News category name
1097 func HandleNewNewsFldr(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1098 if !authorize(cc.Account.Access, accessNewsCreateFldr) {
1099 res = append(res, cc.NewErrReply(t, "You are not allowed to create news folders."))
1103 name := string(t.GetField(fieldFileName).Data)
1104 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1106 cc.logger.Infof("Creating new news folder %s", name)
1108 cats := cc.Server.GetNewsCatByPath(pathStrs)
1109 cats[name] = NewsCategoryListData15{
1112 Articles: map[uint32]*NewsArtData{},
1113 SubCats: make(map[string]NewsCategoryListData15),
1115 if err := cc.Server.writeThreadedNews(); err != nil {
1118 res = append(res, cc.NewReply(t))
1122 // Fields used in the request:
1123 // 325 News path Optional
1126 // 321 News article list data Optional
1127 func HandleGetNewsArtNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1128 if !authorize(cc.Account.Access, accessNewsReadArt) {
1129 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1132 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1134 var cat NewsCategoryListData15
1135 cats := cc.Server.ThreadedNews.Categories
1137 for _, fp := range pathStrs {
1139 cats = cats[fp].SubCats
1142 nald := cat.GetNewsArtListData()
1144 res = append(res, cc.NewReply(t, NewField(fieldNewsArtListData, nald.Payload())))
1148 func HandleGetNewsArtData(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1149 if !authorize(cc.Account.Access, accessNewsReadArt) {
1150 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1156 // 326 News article ID
1157 // 327 News article data flavor
1159 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1161 var cat NewsCategoryListData15
1162 cats := cc.Server.ThreadedNews.Categories
1164 for _, fp := range pathStrs {
1166 cats = cats[fp].SubCats
1168 newsArtID := t.GetField(fieldNewsArtID).Data
1170 convertedArtID := binary.BigEndian.Uint16(newsArtID)
1172 art := cat.Articles[uint32(convertedArtID)]
1174 res = append(res, cc.NewReply(t))
1179 // 328 News article title
1180 // 329 News article poster
1181 // 330 News article date
1182 // 331 Previous article ID
1183 // 332 Next article ID
1184 // 335 Parent article ID
1185 // 336 First child article ID
1186 // 327 News article data flavor "Should be “text/plain”
1187 // 333 News article data Optional (if data flavor is “text/plain”)
1189 res = append(res, cc.NewReply(t,
1190 NewField(fieldNewsArtTitle, []byte(art.Title)),
1191 NewField(fieldNewsArtPoster, []byte(art.Poster)),
1192 NewField(fieldNewsArtDate, art.Date),
1193 NewField(fieldNewsArtPrevArt, art.PrevArt),
1194 NewField(fieldNewsArtNextArt, art.NextArt),
1195 NewField(fieldNewsArtParentArt, art.ParentArt),
1196 NewField(fieldNewsArt1stChildArt, art.FirstChildArt),
1197 NewField(fieldNewsArtDataFlav, []byte("text/plain")),
1198 NewField(fieldNewsArtData, []byte(art.Data)),
1203 func HandleDelNewsItem(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1204 // Has multiple access flags: News Delete Folder (37) or News Delete Category (35)
1207 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1209 // TODO: determine if path is a Folder (Bundle) or Category and check for permission
1211 cc.logger.Infof("DelNewsItem %v", pathStrs)
1213 cats := cc.Server.ThreadedNews.Categories
1215 delName := pathStrs[len(pathStrs)-1]
1216 if len(pathStrs) > 1 {
1217 for _, fp := range pathStrs[0 : len(pathStrs)-1] {
1218 cats = cats[fp].SubCats
1222 delete(cats, delName)
1224 err = cc.Server.writeThreadedNews()
1229 // Reply params: none
1230 res = append(res, cc.NewReply(t))
1235 func HandleDelNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1236 if !authorize(cc.Account.Access, accessNewsDeleteArt) {
1237 res = append(res, cc.NewErrReply(t, "You are not allowed to delete news articles."))
1243 // 326 News article ID
1244 // 337 News article – recursive delete Delete child articles (1) or not (0)
1245 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1246 ID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data)
1248 // TODO: Delete recursive
1249 cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1])
1251 catName := pathStrs[len(pathStrs)-1]
1252 cat := cats[catName]
1254 delete(cat.Articles, uint32(ID))
1257 if err := cc.Server.writeThreadedNews(); err != nil {
1261 res = append(res, cc.NewReply(t))
1267 // 326 News article ID ID of the parent article?
1268 // 328 News article title
1269 // 334 News article flags
1270 // 327 News article data flavor Currently “text/plain”
1271 // 333 News article data
1272 func HandlePostNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1273 if !authorize(cc.Account.Access, accessNewsPostArt) {
1274 res = append(res, cc.NewErrReply(t, "You are not allowed to post news articles."))
1278 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1279 cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1])
1281 catName := pathStrs[len(pathStrs)-1]
1282 cat := cats[catName]
1284 newArt := NewsArtData{
1285 Title: string(t.GetField(fieldNewsArtTitle).Data),
1286 Poster: string(cc.UserName),
1287 Date: toHotlineTime(time.Now()),
1288 PrevArt: []byte{0, 0, 0, 0},
1289 NextArt: []byte{0, 0, 0, 0},
1290 ParentArt: append([]byte{0, 0}, t.GetField(fieldNewsArtID).Data...),
1291 FirstChildArt: []byte{0, 0, 0, 0},
1292 DataFlav: []byte("text/plain"),
1293 Data: string(t.GetField(fieldNewsArtData).Data),
1297 for k := range cat.Articles {
1298 keys = append(keys, int(k))
1304 prevID := uint32(keys[len(keys)-1])
1307 binary.BigEndian.PutUint32(newArt.PrevArt, prevID)
1309 // Set next article ID
1310 binary.BigEndian.PutUint32(cat.Articles[prevID].NextArt, nextID)
1313 // Update parent article with first child reply
1314 parentID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data)
1316 parentArt := cat.Articles[uint32(parentID)]
1318 if bytes.Equal(parentArt.FirstChildArt, []byte{0, 0, 0, 0}) {
1319 binary.BigEndian.PutUint32(parentArt.FirstChildArt, nextID)
1323 cat.Articles[nextID] = &newArt
1326 if err := cc.Server.writeThreadedNews(); err != nil {
1330 res = append(res, cc.NewReply(t))
1334 // HandleGetMsgs returns the flat news data
1335 func HandleGetMsgs(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1336 if !authorize(cc.Account.Access, accessNewsReadArt) {
1337 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1341 res = append(res, cc.NewReply(t, NewField(fieldData, cc.Server.FlatNews)))
1346 func HandleDownloadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1347 if !authorize(cc.Account.Access, accessDownloadFile) {
1348 res = append(res, cc.NewErrReply(t, "You are not allowed to download files."))
1352 fileName := t.GetField(fieldFileName).Data
1353 filePath := t.GetField(fieldFilePath).Data
1354 resumeData := t.GetField(fieldFileResumeData).Data
1356 var dataOffset int64
1357 var frd FileResumeData
1358 if resumeData != nil {
1359 if err := frd.UnmarshalBinary(t.GetField(fieldFileResumeData).Data); err != nil {
1362 // TODO: handle rsrc fork offset
1363 dataOffset = int64(binary.BigEndian.Uint32(frd.ForkInfoList[0].DataSize[:]))
1366 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
1371 hlFile, err := newFileWrapper(cc.Server.FS, fullFilePath, dataOffset)
1376 xferSize := hlFile.ffo.TransferSize(0)
1378 ft := cc.newFileTransfer(FileDownload, fileName, filePath, xferSize)
1380 // TODO: refactor to remove this
1381 if resumeData != nil {
1382 var frd FileResumeData
1383 if err := frd.UnmarshalBinary(t.GetField(fieldFileResumeData).Data); err != nil {
1386 ft.fileResumeData = &frd
1389 // Optional field for when a HL v1.5+ client requests file preview
1390 // Used only for TEXT, JPEG, GIFF, BMP or PICT files
1391 // The value will always be 2
1392 if t.GetField(fieldFileTransferOptions).Data != nil {
1393 ft.options = t.GetField(fieldFileTransferOptions).Data
1394 xferSize = hlFile.ffo.FlatFileDataForkHeader.DataSize[:]
1397 res = append(res, cc.NewReply(t,
1398 NewField(fieldRefNum, ft.refNum[:]),
1399 NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count
1400 NewField(fieldTransferSize, xferSize),
1401 NewField(fieldFileSize, hlFile.ffo.FlatFileDataForkHeader.DataSize[:]),
1407 // Download all files from the specified folder and sub-folders
1408 func HandleDownloadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1409 if !authorize(cc.Account.Access, accessDownloadFile) {
1410 res = append(res, cc.NewErrReply(t, "You are not allowed to download folders."))
1414 fullFilePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFilePath).Data, t.GetField(fieldFileName).Data)
1419 transferSize, err := CalcTotalSize(fullFilePath)
1423 itemCount, err := CalcItemCount(fullFilePath)
1428 fileTransfer := cc.newFileTransfer(FolderDownload, t.GetField(fieldFileName).Data, t.GetField(fieldFilePath).Data, transferSize)
1431 err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
1436 res = append(res, cc.NewReply(t,
1437 NewField(fieldRefNum, fileTransfer.ReferenceNumber),
1438 NewField(fieldTransferSize, transferSize),
1439 NewField(fieldFolderItemCount, itemCount),
1440 NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count
1445 // Upload all files from the local folder and its subfolders to the specified path on the server
1446 // Fields used in the request
1449 // 108 transfer size Total size of all items in the folder
1450 // 220 Folder item count
1451 // 204 File transfer options "Optional Currently set to 1" (TODO: ??)
1452 func HandleUploadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1454 if t.GetField(fieldFilePath).Data != nil {
1455 if err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data); err != nil {
1460 // Handle special cases for Upload and Drop Box folders
1461 if !authorize(cc.Account.Access, accessUploadAnywhere) {
1462 if !fp.IsUploadDir() && !fp.IsDropbox() {
1463 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))))
1468 fileTransfer := cc.newFileTransfer(FolderUpload,
1469 t.GetField(fieldFileName).Data,
1470 t.GetField(fieldFilePath).Data,
1471 t.GetField(fieldTransferSize).Data,
1474 fileTransfer.FolderItemCount = t.GetField(fieldFolderItemCount).Data
1476 res = append(res, cc.NewReply(t, NewField(fieldRefNum, fileTransfer.ReferenceNumber)))
1481 // Fields used in the request:
1484 // 204 File transfer options "Optional
1485 // Used only to resume download, currently has value 2"
1486 // 108 File transfer size "Optional used if download is not resumed"
1487 func HandleUploadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1488 if !authorize(cc.Account.Access, accessUploadFile) {
1489 res = append(res, cc.NewErrReply(t, "You are not allowed to upload files."))
1493 fileName := t.GetField(fieldFileName).Data
1494 filePath := t.GetField(fieldFilePath).Data
1495 transferOptions := t.GetField(fieldFileTransferOptions).Data
1496 transferSize := t.GetField(fieldTransferSize).Data // not sent for resume
1499 if filePath != nil {
1500 if err = fp.UnmarshalBinary(filePath); err != nil {
1505 // Handle special cases for Upload and Drop Box folders
1506 if !authorize(cc.Account.Access, accessUploadAnywhere) {
1507 if !fp.IsUploadDir() && !fp.IsDropbox() {
1508 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))))
1512 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
1517 if _, err := cc.Server.FS.Stat(fullFilePath); err == nil {
1518 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))))
1522 ft := cc.newFileTransfer(FileUpload, fileName, filePath, transferSize)
1524 replyT := cc.NewReply(t, NewField(fieldRefNum, ft.ReferenceNumber))
1526 // client has requested to resume a partially transferred file
1527 if transferOptions != nil {
1529 fileInfo, err := cc.Server.FS.Stat(fullFilePath + incompleteFileSuffix)
1534 offset := make([]byte, 4)
1535 binary.BigEndian.PutUint32(offset, uint32(fileInfo.Size()))
1537 fileResumeData := NewFileResumeData([]ForkInfoList{
1538 *NewForkInfoList(offset),
1541 b, _ := fileResumeData.BinaryMarshal()
1543 ft.TransferSize = offset
1545 replyT.Fields = append(replyT.Fields, NewField(fieldFileResumeData, b))
1548 res = append(res, replyT)
1552 func HandleSetClientUserInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1554 if len(t.GetField(fieldUserIconID).Data) == 4 {
1555 icon = t.GetField(fieldUserIconID).Data[2:]
1557 icon = t.GetField(fieldUserIconID).Data
1560 cc.UserName = t.GetField(fieldUserName).Data
1562 // the options field is only passed by the client versions > 1.2.3.
1563 options := t.GetField(fieldOptions).Data
1566 optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
1567 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags)))
1569 flagBitmap.SetBit(flagBitmap, userFlagRefusePM, optBitmap.Bit(refusePM))
1570 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
1572 flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, optBitmap.Bit(refuseChat))
1573 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
1575 // Check auto response
1576 if optBitmap.Bit(autoResponse) == 1 {
1577 cc.AutoReply = t.GetField(fieldAutomaticResponse).Data
1579 cc.AutoReply = []byte{}
1583 // Notify all clients of updated user info
1585 tranNotifyChangeUser,
1586 NewField(fieldUserID, *cc.ID),
1587 NewField(fieldUserIconID, *cc.Icon),
1588 NewField(fieldUserFlags, *cc.Flags),
1589 NewField(fieldUserName, cc.UserName),
1595 // HandleKeepAlive responds to keepalive transactions with an empty reply
1596 // * HL 1.9.2 Client sends keepalive msg every 3 minutes
1597 // * HL 1.2.3 Client doesn't send keepalives
1598 func HandleKeepAlive(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1599 res = append(res, cc.NewReply(t))
1604 func HandleGetFileNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1605 fullPath, err := readPath(
1606 cc.Server.Config.FileRoot,
1607 t.GetField(fieldFilePath).Data,
1615 if t.GetField(fieldFilePath).Data != nil {
1616 if err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data); err != nil {
1621 // Handle special case for drop box folders
1622 if fp.IsDropbox() && !authorize(cc.Account.Access, accessViewDropBoxes) {
1623 res = append(res, cc.NewErrReply(t, "You are not allowed to view drop boxes."))
1627 fileNames, err := getFileNameList(fullPath)
1632 res = append(res, cc.NewReply(t, fileNames...))
1637 // =================================
1638 // Hotline private chat flow
1639 // =================================
1640 // 1. ClientA sends tranInviteNewChat to server with user ID to invite
1641 // 2. Server creates new ChatID
1642 // 3. Server sends tranInviteToChat to invitee
1643 // 4. Server replies to ClientA with new Chat ID
1645 // A dialog box pops up in the invitee client with options to accept or decline the invitation.
1646 // If Accepted is clicked:
1647 // 1. ClientB sends tranJoinChat with fieldChatID
1649 // HandleInviteNewChat invites users to new private chat
1650 func HandleInviteNewChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1651 if !authorize(cc.Account.Access, accessOpenChat) {
1652 res = append(res, cc.NewErrReply(t, "You are not allowed to request private chat."))
1657 targetID := t.GetField(fieldUserID).Data
1658 newChatID := cc.Server.NewPrivateChat(cc)
1664 NewField(fieldChatID, newChatID),
1665 NewField(fieldUserName, cc.UserName),
1666 NewField(fieldUserID, *cc.ID),
1672 NewField(fieldChatID, newChatID),
1673 NewField(fieldUserName, cc.UserName),
1674 NewField(fieldUserID, *cc.ID),
1675 NewField(fieldUserIconID, *cc.Icon),
1676 NewField(fieldUserFlags, *cc.Flags),
1683 func HandleInviteToChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1684 if !authorize(cc.Account.Access, accessOpenChat) {
1685 res = append(res, cc.NewErrReply(t, "You are not allowed to request private chat."))
1690 targetID := t.GetField(fieldUserID).Data
1691 chatID := t.GetField(fieldChatID).Data
1697 NewField(fieldChatID, chatID),
1698 NewField(fieldUserName, cc.UserName),
1699 NewField(fieldUserID, *cc.ID),
1705 NewField(fieldChatID, chatID),
1706 NewField(fieldUserName, cc.UserName),
1707 NewField(fieldUserID, *cc.ID),
1708 NewField(fieldUserIconID, *cc.Icon),
1709 NewField(fieldUserFlags, *cc.Flags),
1716 func HandleRejectChatInvite(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1717 chatID := t.GetField(fieldChatID).Data
1718 chatInt := binary.BigEndian.Uint32(chatID)
1720 privChat := cc.Server.PrivateChats[chatInt]
1722 resMsg := append(cc.UserName, []byte(" declined invitation to chat")...)
1724 for _, c := range sortedClients(privChat.ClientConn) {
1729 NewField(fieldChatID, chatID),
1730 NewField(fieldData, resMsg),
1738 // HandleJoinChat is sent from a v1.8+ Hotline client when the joins a private chat
1739 // Fields used in the reply:
1740 // * 115 Chat subject
1741 // * 300 User name with info (Optional)
1742 // * 300 (more user names with info)
1743 func HandleJoinChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1744 chatID := t.GetField(fieldChatID).Data
1745 chatInt := binary.BigEndian.Uint32(chatID)
1747 privChat := cc.Server.PrivateChats[chatInt]
1749 // Send tranNotifyChatChangeUser to current members of the chat to inform of new user
1750 for _, c := range sortedClients(privChat.ClientConn) {
1753 tranNotifyChatChangeUser,
1755 NewField(fieldChatID, chatID),
1756 NewField(fieldUserName, cc.UserName),
1757 NewField(fieldUserID, *cc.ID),
1758 NewField(fieldUserIconID, *cc.Icon),
1759 NewField(fieldUserFlags, *cc.Flags),
1764 privChat.ClientConn[cc.uint16ID()] = cc
1766 replyFields := []Field{NewField(fieldChatSubject, []byte(privChat.Subject))}
1767 for _, c := range sortedClients(privChat.ClientConn) {
1772 Name: string(c.UserName),
1775 replyFields = append(replyFields, NewField(fieldUsernameWithInfo, user.Payload()))
1778 res = append(res, cc.NewReply(t, replyFields...))
1782 // HandleLeaveChat is sent from a v1.8+ Hotline client when the user exits a private chat
1783 // Fields used in the request:
1784 // * 114 fieldChatID
1785 // Reply is not expected.
1786 func HandleLeaveChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1787 chatID := t.GetField(fieldChatID).Data
1788 chatInt := binary.BigEndian.Uint32(chatID)
1790 privChat, ok := cc.Server.PrivateChats[chatInt]
1795 delete(privChat.ClientConn, cc.uint16ID())
1797 // Notify members of the private chat that the user has left
1798 for _, c := range sortedClients(privChat.ClientConn) {
1801 tranNotifyChatDeleteUser,
1803 NewField(fieldChatID, chatID),
1804 NewField(fieldUserID, *cc.ID),
1812 // HandleSetChatSubject is sent from a v1.8+ Hotline client when the user sets a private chat subject
1813 // Fields used in the request:
1815 // * 115 Chat subject Chat subject string
1816 // Reply is not expected.
1817 func HandleSetChatSubject(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1818 chatID := t.GetField(fieldChatID).Data
1819 chatInt := binary.BigEndian.Uint32(chatID)
1821 privChat := cc.Server.PrivateChats[chatInt]
1822 privChat.Subject = string(t.GetField(fieldChatSubject).Data)
1824 for _, c := range sortedClients(privChat.ClientConn) {
1827 tranNotifyChatSubject,
1829 NewField(fieldChatID, chatID),
1830 NewField(fieldChatSubject, t.GetField(fieldChatSubject).Data),
1838 // HandleMakeAlias makes a filer alias using the specified path.
1839 // Fields used in the request:
1842 // 212 File new path Destination path
1844 // Fields used in the reply:
1846 func HandleMakeAlias(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1847 if !authorize(cc.Account.Access, accessMakeAlias) {
1848 res = append(res, cc.NewErrReply(t, "You are not allowed to make aliases."))
1851 fileName := t.GetField(fieldFileName).Data
1852 filePath := t.GetField(fieldFilePath).Data
1853 fileNewPath := t.GetField(fieldFileNewPath).Data
1855 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
1860 fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, fileNewPath, fileName)
1865 cc.logger.Debugw("Make alias", "src", fullFilePath, "dst", fullNewFilePath)
1867 if err := cc.Server.FS.Symlink(fullFilePath, fullNewFilePath); err != nil {
1868 res = append(res, cc.NewErrReply(t, "Error creating alias"))
1872 res = append(res, cc.NewReply(t))
1876 func HandleDownloadBanner(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1877 fi, err := cc.Server.FS.Stat(filepath.Join(cc.Server.ConfigDir, cc.Server.Config.BannerFile))
1882 ft := cc.newFileTransfer(bannerDownload, []byte{}, []byte{}, make([]byte, 4))
1884 binary.BigEndian.PutUint32(ft.TransferSize, uint32(fi.Size()))
1886 res = append(res, cc.NewReply(t,
1887 NewField(fieldRefNum, ft.refNum[:]),
1888 NewField(fieldTransferSize, ft.TransferSize),