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: HandleGetClientConnInfoText,
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,
236 func HandleChatSend(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
237 if !authorize(cc.Account.Access, accessSendChat) {
238 res = append(res, cc.NewErrReply(t, "You are not allowed to participate in chat."))
242 // Truncate long usernames
243 trunc := fmt.Sprintf("%13s", cc.UserName)
244 formattedMsg := fmt.Sprintf("\r%.14s: %s", trunc, t.GetField(fieldData).Data)
246 // By holding the option key, Hotline chat allows users to send /me formatted messages like:
247 // *** Halcyon does stuff
248 // This is indicated by the presence of the optional field fieldChatOptions in the transaction payload
249 if t.GetField(fieldChatOptions).Data != nil {
250 formattedMsg = fmt.Sprintf("\r*** %s %s", cc.UserName, t.GetField(fieldData).Data)
253 chatID := t.GetField(fieldChatID).Data
254 // a non-nil chatID indicates the message belongs to a private chat
256 chatInt := binary.BigEndian.Uint32(chatID)
257 privChat := cc.Server.PrivateChats[chatInt]
259 clients := sortedClients(privChat.ClientConn)
261 // send the message to all connected clients of the private chat
262 for _, c := range clients {
263 res = append(res, *NewTransaction(
266 NewField(fieldChatID, chatID),
267 NewField(fieldData, []byte(formattedMsg)),
273 for _, c := range sortedClients(cc.Server.Clients) {
274 // Filter out clients that do not have the read chat permission
275 if authorize(c.Account.Access, accessReadChat) {
276 res = append(res, *NewTransaction(tranChatMsg, c.ID, NewField(fieldData, []byte(formattedMsg))))
283 // HandleSendInstantMsg sends instant message to the user on the current server.
284 // Fields used in the request:
287 // One of the following values:
288 // - User message (myOpt_UserMessage = 1)
289 // - Refuse message (myOpt_RefuseMessage = 2)
290 // - Refuse chat (myOpt_RefuseChat = 3)
291 // - Automatic response (myOpt_AutomaticResponse = 4)"
293 // 214 Quoting message Optional
295 // Fields used in the reply:
297 func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
298 msg := t.GetField(fieldData)
299 ID := t.GetField(fieldUserID)
301 reply := NewTransaction(
304 NewField(fieldData, msg.Data),
305 NewField(fieldUserName, cc.UserName),
306 NewField(fieldUserID, *cc.ID),
307 NewField(fieldOptions, []byte{0, 1}),
310 // Later versions of Hotline include the original message in the fieldQuotingMsg field so
311 // the receiving client can display both the received message and what it is in reply to
312 if t.GetField(fieldQuotingMsg).Data != nil {
313 reply.Fields = append(reply.Fields, NewField(fieldQuotingMsg, t.GetField(fieldQuotingMsg).Data))
316 res = append(res, *reply)
318 id, _ := byteToInt(ID.Data)
319 otherClient, ok := cc.Server.Clients[uint16(id)]
321 return res, errors.New("invalid client ID")
324 // Respond with auto reply if other client has it enabled
325 if len(otherClient.AutoReply) > 0 {
330 NewField(fieldData, otherClient.AutoReply),
331 NewField(fieldUserName, otherClient.UserName),
332 NewField(fieldUserID, *otherClient.ID),
333 NewField(fieldOptions, []byte{0, 1}),
338 res = append(res, cc.NewReply(t))
343 func HandleGetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
344 fileName := t.GetField(fieldFileName).Data
345 filePath := t.GetField(fieldFilePath).Data
347 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
352 fw, err := newFileWrapper(cc.Server.FS, fullFilePath, 0)
357 res = append(res, cc.NewReply(t,
358 NewField(fieldFileName, []byte(fw.name)),
359 NewField(fieldFileTypeString, fw.ffo.FlatFileInformationFork.friendlyType()),
360 NewField(fieldFileCreatorString, fw.ffo.FlatFileInformationFork.friendlyCreator()),
361 NewField(fieldFileComment, fw.ffo.FlatFileInformationFork.Comment),
362 NewField(fieldFileType, fw.ffo.FlatFileInformationFork.TypeSignature),
363 NewField(fieldFileCreateDate, fw.ffo.FlatFileInformationFork.CreateDate),
364 NewField(fieldFileModifyDate, fw.ffo.FlatFileInformationFork.ModifyDate),
365 NewField(fieldFileSize, fw.totalSize()),
370 // HandleSetFileInfo updates a file or folder name and/or comment from the Get Info window
371 // Fields used in the request:
373 // * 202 File path Optional
374 // * 211 File new name Optional
375 // * 210 File comment Optional
376 // Fields used in the reply: None
377 func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
378 fileName := t.GetField(fieldFileName).Data
379 filePath := t.GetField(fieldFilePath).Data
381 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
386 fi, err := cc.Server.FS.Stat(fullFilePath)
391 hlFile, err := newFileWrapper(cc.Server.FS, fullFilePath, 0)
395 if t.GetField(fieldFileComment).Data != nil {
396 switch mode := fi.Mode(); {
398 if !authorize(cc.Account.Access, accessSetFolderComment) {
399 res = append(res, cc.NewErrReply(t, "You are not allowed to set comments for folders."))
402 case mode.IsRegular():
403 if !authorize(cc.Account.Access, accessSetFileComment) {
404 res = append(res, cc.NewErrReply(t, "You are not allowed to set comments for files."))
409 if err := hlFile.ffo.FlatFileInformationFork.setComment(t.GetField(fieldFileComment).Data); err != nil {
412 w, err := hlFile.infoForkWriter()
416 _, err = w.Write(hlFile.ffo.FlatFileInformationFork.MarshalBinary())
422 fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, t.GetField(fieldFileNewName).Data)
427 fileNewName := t.GetField(fieldFileNewName).Data
429 if fileNewName != nil {
430 switch mode := fi.Mode(); {
432 if !authorize(cc.Account.Access, accessRenameFolder) {
433 res = append(res, cc.NewErrReply(t, "You are not allowed to rename folders."))
436 err = os.Rename(fullFilePath, fullNewFilePath)
437 if os.IsNotExist(err) {
438 res = append(res, cc.NewErrReply(t, "Cannot rename folder "+string(fileName)+" because it does not exist or cannot be found."))
441 case mode.IsRegular():
442 if !authorize(cc.Account.Access, accessRenameFile) {
443 res = append(res, cc.NewErrReply(t, "You are not allowed to rename files."))
446 fileDir, err := readPath(cc.Server.Config.FileRoot, filePath, []byte{})
450 hlFile.name = string(fileNewName)
451 err = hlFile.move(fileDir)
452 if os.IsNotExist(err) {
453 res = append(res, cc.NewErrReply(t, "Cannot rename file "+string(fileName)+" because it does not exist or cannot be found."))
462 res = append(res, cc.NewReply(t))
466 // HandleDeleteFile deletes a file or folder
467 // Fields used in the request:
470 // Fields used in the reply: none
471 func HandleDeleteFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
472 fileName := t.GetField(fieldFileName).Data
473 filePath := t.GetField(fieldFilePath).Data
475 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
480 hlFile, err := newFileWrapper(cc.Server.FS, fullFilePath, 0)
485 fi, err := hlFile.dataFile()
487 res = append(res, cc.NewErrReply(t, "Cannot delete file "+string(fileName)+" because it does not exist or cannot be found."))
491 switch mode := fi.Mode(); {
493 if !authorize(cc.Account.Access, accessDeleteFolder) {
494 res = append(res, cc.NewErrReply(t, "You are not allowed to delete folders."))
497 case mode.IsRegular():
498 if !authorize(cc.Account.Access, accessDeleteFile) {
499 res = append(res, cc.NewErrReply(t, "You are not allowed to delete files."))
504 if err := hlFile.delete(); err != nil {
508 res = append(res, cc.NewReply(t))
512 // HandleMoveFile moves files or folders. Note: seemingly not documented
513 func HandleMoveFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
514 fileName := string(t.GetField(fieldFileName).Data)
516 filePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFilePath).Data, t.GetField(fieldFileName).Data)
521 fileNewPath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFileNewPath).Data, nil)
526 cc.logger.Infow("Move file", "src", filePath+"/"+fileName, "dst", fileNewPath+"/"+fileName)
528 hlFile, err := newFileWrapper(cc.Server.FS, filePath, 0)
533 fi, err := hlFile.dataFile()
535 res = append(res, cc.NewErrReply(t, "Cannot delete file "+fileName+" because it does not exist or cannot be found."))
541 switch mode := fi.Mode(); {
543 if !authorize(cc.Account.Access, accessMoveFolder) {
544 res = append(res, cc.NewErrReply(t, "You are not allowed to move folders."))
547 case mode.IsRegular():
548 if !authorize(cc.Account.Access, accessMoveFile) {
549 res = append(res, cc.NewErrReply(t, "You are not allowed to move files."))
553 if err := hlFile.move(fileNewPath); err != nil {
556 // TODO: handle other possible errors; e.g. fileWrapper delete fails due to fileWrapper permission issue
558 res = append(res, cc.NewReply(t))
562 func HandleNewFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
563 if !authorize(cc.Account.Access, accessCreateFolder) {
564 res = append(res, cc.NewErrReply(t, "You are not allowed to create folders."))
567 folderName := string(t.GetField(fieldFileName).Data)
569 folderName = path.Join("/", folderName)
573 // fieldFilePath is only present for nested paths
574 if t.GetField(fieldFilePath).Data != nil {
576 err := newFp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
581 for _, pathItem := range newFp.Items {
582 subPath = filepath.Join("/", subPath, string(pathItem.Name))
585 newFolderPath := path.Join(cc.Server.Config.FileRoot, subPath, folderName)
587 // TODO: check path and folder name lengths
589 if _, err := cc.Server.FS.Stat(newFolderPath); !os.IsNotExist(err) {
590 msg := fmt.Sprintf("Cannot create folder \"%s\" because there is already a file or folder with that name.", folderName)
591 return []Transaction{cc.NewErrReply(t, msg)}, nil
594 // TODO: check for disallowed characters to maintain compatibility for original client
596 if err := cc.Server.FS.Mkdir(newFolderPath, 0777); err != nil {
597 msg := fmt.Sprintf("Cannot create folder \"%s\" because an error occurred.", folderName)
598 return []Transaction{cc.NewErrReply(t, msg)}, nil
601 res = append(res, cc.NewReply(t))
605 func HandleSetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
606 if !authorize(cc.Account.Access, accessModifyUser) {
607 res = append(res, cc.NewErrReply(t, "You are not allowed to modify accounts."))
611 login := DecodeUserString(t.GetField(fieldUserLogin).Data)
612 userName := string(t.GetField(fieldUserName).Data)
614 newAccessLvl := t.GetField(fieldUserAccess).Data
616 account := cc.Server.Accounts[login]
617 account.Access = &newAccessLvl
618 account.Name = userName
620 // If the password field is cleared in the Hotline edit user UI, the SetUser transaction does
621 // not include fieldUserPassword
622 if t.GetField(fieldUserPassword).Data == nil {
623 account.Password = hashAndSalt([]byte(""))
625 if len(t.GetField(fieldUserPassword).Data) > 1 {
626 account.Password = hashAndSalt(t.GetField(fieldUserPassword).Data)
629 out, err := yaml.Marshal(&account)
633 if err := os.WriteFile(cc.Server.ConfigDir+"Users/"+login+".yaml", out, 0666); err != nil {
637 // Notify connected clients logged in as the user of the new access level
638 for _, c := range cc.Server.Clients {
639 if c.Account.Login == login {
640 // Note: comment out these two lines to test server-side deny messages
641 newT := NewTransaction(tranUserAccess, c.ID, NewField(fieldUserAccess, newAccessLvl))
642 res = append(res, *newT)
644 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*c.Flags)))
645 if authorize(c.Account.Access, accessDisconUser) {
646 flagBitmap.SetBit(flagBitmap, userFlagAdmin, 1)
648 flagBitmap.SetBit(flagBitmap, userFlagAdmin, 0)
650 binary.BigEndian.PutUint16(*c.Flags, uint16(flagBitmap.Int64()))
652 c.Account.Access = account.Access
655 tranNotifyChangeUser,
656 NewField(fieldUserID, *c.ID),
657 NewField(fieldUserFlags, *c.Flags),
658 NewField(fieldUserName, c.UserName),
659 NewField(fieldUserIconID, *c.Icon),
664 res = append(res, cc.NewReply(t))
668 func HandleGetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
669 if !authorize(cc.Account.Access, accessOpenUser) {
670 res = append(res, cc.NewErrReply(t, "You are not allowed to view accounts."))
674 account := cc.Server.Accounts[string(t.GetField(fieldUserLogin).Data)]
676 res = append(res, cc.NewErrReply(t, "Account does not exist."))
680 res = append(res, cc.NewReply(t,
681 NewField(fieldUserName, []byte(account.Name)),
682 NewField(fieldUserLogin, negateString(t.GetField(fieldUserLogin).Data)),
683 NewField(fieldUserPassword, []byte(account.Password)),
684 NewField(fieldUserAccess, *account.Access),
689 func HandleListUsers(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
690 if !authorize(cc.Account.Access, accessOpenUser) {
691 res = append(res, cc.NewErrReply(t, "You are not allowed to view accounts."))
695 var userFields []Field
696 for _, acc := range cc.Server.Accounts {
697 userField := acc.MarshalBinary()
698 userFields = append(userFields, NewField(fieldData, userField))
701 res = append(res, cc.NewReply(t, userFields...))
705 // HandleUpdateUser is used by the v1.5+ multi-user editor to perform account editing for multiple users at a time.
706 // An update can be a mix of these actions:
709 // * Modify user (including renaming the account login)
711 // The Transaction sent by the client includes one data field per user that was modified. This data field in turn
712 // contains another data field encoded in its payload with a varying number of sub fields depending on which action is
713 // performed. This seems to be the only place in the Hotline protocol where a data field contains another data field.
714 func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
715 for _, field := range t.Fields {
716 subFields, err := ReadFields(field.Data[0:2], field.Data[2:])
721 if len(subFields) == 1 {
722 login := DecodeUserString(getField(fieldData, &subFields).Data)
723 cc.logger.Infow("DeleteUser", "login", login)
725 if !authorize(cc.Account.Access, accessDeleteUser) {
726 res = append(res, cc.NewErrReply(t, "You are not allowed to delete accounts."))
730 if err := cc.Server.DeleteUser(login); err != nil {
736 login := DecodeUserString(getField(fieldUserLogin, &subFields).Data)
738 // check if the login dataFile; if so, we know we are updating an existing user
739 if acc, ok := cc.Server.Accounts[login]; ok {
740 cc.logger.Infow("UpdateUser", "login", login)
742 // account dataFile, so this is an update action
743 if !authorize(cc.Account.Access, accessModifyUser) {
744 res = append(res, cc.NewErrReply(t, "You are not allowed to modify accounts."))
748 if getField(fieldUserPassword, &subFields) != nil {
749 newPass := getField(fieldUserPassword, &subFields).Data
750 acc.Password = hashAndSalt(newPass)
752 acc.Password = hashAndSalt([]byte(""))
755 if getField(fieldUserAccess, &subFields) != nil {
756 acc.Access = &getField(fieldUserAccess, &subFields).Data
759 err = cc.Server.UpdateUser(
760 DecodeUserString(getField(fieldData, &subFields).Data),
761 DecodeUserString(getField(fieldUserLogin, &subFields).Data),
762 string(getField(fieldUserName, &subFields).Data),
770 cc.logger.Infow("CreateUser", "login", login)
772 if !authorize(cc.Account.Access, accessCreateUser) {
773 res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts."))
777 err := cc.Server.NewUser(
779 string(getField(fieldUserName, &subFields).Data),
780 string(getField(fieldUserPassword, &subFields).Data),
781 getField(fieldUserAccess, &subFields).Data,
784 return []Transaction{}, err
789 res = append(res, cc.NewReply(t))
793 // HandleNewUser creates a new user account
794 func HandleNewUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
795 if !authorize(cc.Account.Access, accessCreateUser) {
796 res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts."))
800 login := DecodeUserString(t.GetField(fieldUserLogin).Data)
802 // If the account already dataFile, reply with an error
803 if _, ok := cc.Server.Accounts[login]; ok {
804 res = append(res, cc.NewErrReply(t, "Cannot create account "+login+" because there is already an account with that login."))
808 if err := cc.Server.NewUser(
810 string(t.GetField(fieldUserName).Data),
811 string(t.GetField(fieldUserPassword).Data),
812 t.GetField(fieldUserAccess).Data,
814 return []Transaction{}, err
817 res = append(res, cc.NewReply(t))
821 func HandleDeleteUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
822 if !authorize(cc.Account.Access, accessDeleteUser) {
823 res = append(res, cc.NewErrReply(t, "You are not allowed to delete accounts."))
827 // TODO: Handle case where account doesn't exist; e.g. delete race condition
828 login := DecodeUserString(t.GetField(fieldUserLogin).Data)
830 if err := cc.Server.DeleteUser(login); err != nil {
834 res = append(res, cc.NewReply(t))
838 // HandleUserBroadcast sends an Administrator Message to all connected clients of the server
839 func HandleUserBroadcast(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
840 if !authorize(cc.Account.Access, accessBroadcast) {
841 res = append(res, cc.NewErrReply(t, "You are not allowed to send broadcast messages."))
847 NewField(fieldData, t.GetField(tranGetMsgs).Data),
848 NewField(fieldChatOptions, []byte{0}),
851 res = append(res, cc.NewReply(t))
855 func byteToInt(bytes []byte) (int, error) {
858 return int(binary.BigEndian.Uint16(bytes)), nil
860 return int(binary.BigEndian.Uint32(bytes)), nil
863 return 0, errors.New("unknown byte length")
866 func HandleGetClientConnInfoText(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
867 if !authorize(cc.Account.Access, accessGetClientInfo) {
868 res = append(res, cc.NewErrReply(t, "You are not allowed to get client info"))
872 clientID, _ := byteToInt(t.GetField(fieldUserID).Data)
874 clientConn := cc.Server.Clients[uint16(clientID)]
875 if clientConn == nil {
876 return res, errors.New("invalid client")
879 // TODO: Implement non-hardcoded values
880 template := `Nickname: %s
885 -------- File Downloads ---------
889 ------- Folder Downloads --------
893 --------- File Uploads ----------
897 -------- Folder Uploads ---------
901 ------- Waiting Downloads -------
907 activeDownloads := clientConn.Transfers[FileDownload]
908 activeDownloadList := "None."
909 for _, dl := range activeDownloads {
910 activeDownloadList += dl.String() + "\n"
913 template = fmt.Sprintf(
916 clientConn.Account.Name,
917 clientConn.Account.Login,
918 clientConn.RemoteAddr,
921 template = strings.Replace(template, "\n", "\r", -1)
923 res = append(res, cc.NewReply(t,
924 NewField(fieldData, []byte(template)),
925 NewField(fieldUserName, clientConn.UserName),
930 func HandleGetUserNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
931 res = append(res, cc.NewReply(t, cc.Server.connectedUsers()...))
936 func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
938 cc.UserName = t.GetField(fieldUserName).Data
939 *cc.Icon = t.GetField(fieldUserIconID).Data
941 cc.logger = cc.logger.With("name", string(cc.UserName))
943 options := t.GetField(fieldOptions).Data
944 optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
946 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags)))
948 // Check refuse private PM option
949 if optBitmap.Bit(refusePM) == 1 {
950 flagBitmap.SetBit(flagBitmap, userFlagRefusePM, 1)
951 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
954 // Check refuse private chat option
955 if optBitmap.Bit(refuseChat) == 1 {
956 flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, 1)
957 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
960 // Check auto response
961 if optBitmap.Bit(autoResponse) == 1 {
962 cc.AutoReply = t.GetField(fieldAutomaticResponse).Data
964 cc.AutoReply = []byte{}
969 tranNotifyChangeUser, nil,
970 NewField(fieldUserName, cc.UserName),
971 NewField(fieldUserID, *cc.ID),
972 NewField(fieldUserIconID, *cc.Icon),
973 NewField(fieldUserFlags, *cc.Flags),
977 res = append(res, cc.NewReply(t))
982 const defaultNewsDateFormat = "Jan02 15:04" // Jun23 20:49
983 // "Mon, 02 Jan 2006 15:04:05 MST"
985 const defaultNewsTemplate = `From %s (%s):
989 __________________________________________________________`
991 // HandleTranOldPostNews updates the flat news
992 // Fields used in this request:
994 func HandleTranOldPostNews(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
995 if !authorize(cc.Account.Access, accessNewsPostArt) {
996 res = append(res, cc.NewErrReply(t, "You are not allowed to post news."))
1000 cc.Server.flatNewsMux.Lock()
1001 defer cc.Server.flatNewsMux.Unlock()
1003 newsDateTemplate := defaultNewsDateFormat
1004 if cc.Server.Config.NewsDateFormat != "" {
1005 newsDateTemplate = cc.Server.Config.NewsDateFormat
1008 newsTemplate := defaultNewsTemplate
1009 if cc.Server.Config.NewsDelimiter != "" {
1010 newsTemplate = cc.Server.Config.NewsDelimiter
1013 newsPost := fmt.Sprintf(newsTemplate+"\r", cc.UserName, time.Now().Format(newsDateTemplate), t.GetField(fieldData).Data)
1014 newsPost = strings.Replace(newsPost, "\n", "\r", -1)
1016 // update news in memory
1017 cc.Server.FlatNews = append([]byte(newsPost), cc.Server.FlatNews...)
1019 // update news on disk
1020 if err := ioutil.WriteFile(cc.Server.ConfigDir+"MessageBoard.txt", cc.Server.FlatNews, 0644); err != nil {
1024 // Notify all clients of updated news
1027 NewField(fieldData, []byte(newsPost)),
1030 res = append(res, cc.NewReply(t))
1034 func HandleDisconnectUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1035 if !authorize(cc.Account.Access, accessDisconUser) {
1036 res = append(res, cc.NewErrReply(t, "You are not allowed to disconnect users."))
1040 clientConn := cc.Server.Clients[binary.BigEndian.Uint16(t.GetField(fieldUserID).Data)]
1042 if authorize(clientConn.Account.Access, accessCannotBeDiscon) {
1043 res = append(res, cc.NewErrReply(t, clientConn.Account.Login+" is not allowed to be disconnected."))
1047 if err := clientConn.Connection.Close(); err != nil {
1051 res = append(res, cc.NewReply(t))
1055 // HandleGetNewsCatNameList returns a list of news categories for a path
1056 // Fields used in the request:
1057 // 325 News path (Optional)
1058 func HandleGetNewsCatNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1059 if !authorize(cc.Account.Access, accessNewsReadArt) {
1060 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1064 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1065 cats := cc.Server.GetNewsCatByPath(pathStrs)
1067 // To store the keys in slice in sorted order
1068 keys := make([]string, len(cats))
1070 for k := range cats {
1076 var fieldData []Field
1077 for _, k := range keys {
1079 b, _ := cat.MarshalBinary()
1080 fieldData = append(fieldData, NewField(
1081 fieldNewsCatListData15,
1086 res = append(res, cc.NewReply(t, fieldData...))
1090 func HandleNewNewsCat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1091 if !authorize(cc.Account.Access, accessNewsCreateCat) {
1092 res = append(res, cc.NewErrReply(t, "You are not allowed to create news categories."))
1096 name := string(t.GetField(fieldNewsCatName).Data)
1097 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1099 cats := cc.Server.GetNewsCatByPath(pathStrs)
1100 cats[name] = NewsCategoryListData15{
1103 Articles: map[uint32]*NewsArtData{},
1104 SubCats: make(map[string]NewsCategoryListData15),
1107 if err := cc.Server.writeThreadedNews(); err != nil {
1110 res = append(res, cc.NewReply(t))
1114 // Fields used in the request:
1115 // 322 News category name
1117 func HandleNewNewsFldr(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1118 if !authorize(cc.Account.Access, accessNewsCreateFldr) {
1119 res = append(res, cc.NewErrReply(t, "You are not allowed to create news folders."))
1123 name := string(t.GetField(fieldFileName).Data)
1124 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1126 cc.logger.Infof("Creating new news folder %s", name)
1128 cats := cc.Server.GetNewsCatByPath(pathStrs)
1129 cats[name] = NewsCategoryListData15{
1132 Articles: map[uint32]*NewsArtData{},
1133 SubCats: make(map[string]NewsCategoryListData15),
1135 if err := cc.Server.writeThreadedNews(); err != nil {
1138 res = append(res, cc.NewReply(t))
1142 // Fields used in the request:
1143 // 325 News path Optional
1146 // 321 News article list data Optional
1147 func HandleGetNewsArtNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1148 if !authorize(cc.Account.Access, accessNewsReadArt) {
1149 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1152 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1154 var cat NewsCategoryListData15
1155 cats := cc.Server.ThreadedNews.Categories
1157 for _, fp := range pathStrs {
1159 cats = cats[fp].SubCats
1162 nald := cat.GetNewsArtListData()
1164 res = append(res, cc.NewReply(t, NewField(fieldNewsArtListData, nald.Payload())))
1168 func HandleGetNewsArtData(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1169 if !authorize(cc.Account.Access, accessNewsReadArt) {
1170 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1176 // 326 News article ID
1177 // 327 News article data flavor
1179 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1181 var cat NewsCategoryListData15
1182 cats := cc.Server.ThreadedNews.Categories
1184 for _, fp := range pathStrs {
1186 cats = cats[fp].SubCats
1188 newsArtID := t.GetField(fieldNewsArtID).Data
1190 convertedArtID := binary.BigEndian.Uint16(newsArtID)
1192 art := cat.Articles[uint32(convertedArtID)]
1194 res = append(res, cc.NewReply(t))
1199 // 328 News article title
1200 // 329 News article poster
1201 // 330 News article date
1202 // 331 Previous article ID
1203 // 332 Next article ID
1204 // 335 Parent article ID
1205 // 336 First child article ID
1206 // 327 News article data flavor "Should be “text/plain”
1207 // 333 News article data Optional (if data flavor is “text/plain”)
1209 res = append(res, cc.NewReply(t,
1210 NewField(fieldNewsArtTitle, []byte(art.Title)),
1211 NewField(fieldNewsArtPoster, []byte(art.Poster)),
1212 NewField(fieldNewsArtDate, art.Date),
1213 NewField(fieldNewsArtPrevArt, art.PrevArt),
1214 NewField(fieldNewsArtNextArt, art.NextArt),
1215 NewField(fieldNewsArtParentArt, art.ParentArt),
1216 NewField(fieldNewsArt1stChildArt, art.FirstChildArt),
1217 NewField(fieldNewsArtDataFlav, []byte("text/plain")),
1218 NewField(fieldNewsArtData, []byte(art.Data)),
1223 func HandleDelNewsItem(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1224 // Has multiple access flags: News Delete Folder (37) or News Delete Category (35)
1227 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1229 // TODO: determine if path is a Folder (Bundle) or Category and check for permission
1231 cc.logger.Infof("DelNewsItem %v", pathStrs)
1233 cats := cc.Server.ThreadedNews.Categories
1235 delName := pathStrs[len(pathStrs)-1]
1236 if len(pathStrs) > 1 {
1237 for _, fp := range pathStrs[0 : len(pathStrs)-1] {
1238 cats = cats[fp].SubCats
1242 delete(cats, delName)
1244 err = cc.Server.writeThreadedNews()
1249 // Reply params: none
1250 res = append(res, cc.NewReply(t))
1255 func HandleDelNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1256 if !authorize(cc.Account.Access, accessNewsDeleteArt) {
1257 res = append(res, cc.NewErrReply(t, "You are not allowed to delete news articles."))
1263 // 326 News article ID
1264 // 337 News article – recursive delete Delete child articles (1) or not (0)
1265 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1266 ID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data)
1268 // TODO: Delete recursive
1269 cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1])
1271 catName := pathStrs[len(pathStrs)-1]
1272 cat := cats[catName]
1274 delete(cat.Articles, uint32(ID))
1277 if err := cc.Server.writeThreadedNews(); err != nil {
1281 res = append(res, cc.NewReply(t))
1287 // 326 News article ID ID of the parent article?
1288 // 328 News article title
1289 // 334 News article flags
1290 // 327 News article data flavor Currently “text/plain”
1291 // 333 News article data
1292 func HandlePostNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1293 if !authorize(cc.Account.Access, accessNewsPostArt) {
1294 res = append(res, cc.NewErrReply(t, "You are not allowed to post news articles."))
1298 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1299 cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1])
1301 catName := pathStrs[len(pathStrs)-1]
1302 cat := cats[catName]
1304 newArt := NewsArtData{
1305 Title: string(t.GetField(fieldNewsArtTitle).Data),
1306 Poster: string(cc.UserName),
1307 Date: toHotlineTime(time.Now()),
1308 PrevArt: []byte{0, 0, 0, 0},
1309 NextArt: []byte{0, 0, 0, 0},
1310 ParentArt: append([]byte{0, 0}, t.GetField(fieldNewsArtID).Data...),
1311 FirstChildArt: []byte{0, 0, 0, 0},
1312 DataFlav: []byte("text/plain"),
1313 Data: string(t.GetField(fieldNewsArtData).Data),
1317 for k := range cat.Articles {
1318 keys = append(keys, int(k))
1324 prevID := uint32(keys[len(keys)-1])
1327 binary.BigEndian.PutUint32(newArt.PrevArt, prevID)
1329 // Set next article ID
1330 binary.BigEndian.PutUint32(cat.Articles[prevID].NextArt, nextID)
1333 // Update parent article with first child reply
1334 parentID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data)
1336 parentArt := cat.Articles[uint32(parentID)]
1338 if bytes.Equal(parentArt.FirstChildArt, []byte{0, 0, 0, 0}) {
1339 binary.BigEndian.PutUint32(parentArt.FirstChildArt, nextID)
1343 cat.Articles[nextID] = &newArt
1346 if err := cc.Server.writeThreadedNews(); err != nil {
1350 res = append(res, cc.NewReply(t))
1354 // HandleGetMsgs returns the flat news data
1355 func HandleGetMsgs(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1356 if !authorize(cc.Account.Access, accessNewsReadArt) {
1357 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1361 res = append(res, cc.NewReply(t, NewField(fieldData, cc.Server.FlatNews)))
1366 func HandleDownloadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1367 if !authorize(cc.Account.Access, accessDownloadFile) {
1368 res = append(res, cc.NewErrReply(t, "You are not allowed to download files."))
1372 fileName := t.GetField(fieldFileName).Data
1373 filePath := t.GetField(fieldFilePath).Data
1374 resumeData := t.GetField(fieldFileResumeData).Data
1376 var dataOffset int64
1377 var frd FileResumeData
1378 if resumeData != nil {
1379 if err := frd.UnmarshalBinary(t.GetField(fieldFileResumeData).Data); err != nil {
1382 // TODO: handle rsrc fork offset
1383 dataOffset = int64(binary.BigEndian.Uint32(frd.ForkInfoList[0].DataSize[:]))
1386 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
1391 hlFile, err := newFileWrapper(cc.Server.FS, fullFilePath, dataOffset)
1396 transactionRef := cc.Server.NewTransactionRef()
1397 data := binary.BigEndian.Uint32(transactionRef)
1399 ft := &FileTransfer{
1402 ReferenceNumber: transactionRef,
1406 // TODO: refactor to remove this
1407 if resumeData != nil {
1408 var frd FileResumeData
1409 if err := frd.UnmarshalBinary(t.GetField(fieldFileResumeData).Data); err != nil {
1412 ft.fileResumeData = &frd
1415 xferSize := hlFile.ffo.TransferSize(0)
1417 // Optional field for when a HL v1.5+ client requests file preview
1418 // Used only for TEXT, JPEG, GIFF, BMP or PICT files
1419 // The value will always be 2
1420 if t.GetField(fieldFileTransferOptions).Data != nil {
1421 ft.options = t.GetField(fieldFileTransferOptions).Data
1422 xferSize = hlFile.ffo.FlatFileDataForkHeader.DataSize[:]
1425 cc.Server.mux.Lock()
1426 defer cc.Server.mux.Unlock()
1427 cc.Server.FileTransfers[data] = ft
1429 cc.Transfers[FileDownload] = append(cc.Transfers[FileDownload], ft)
1431 res = append(res, cc.NewReply(t,
1432 NewField(fieldRefNum, transactionRef),
1433 NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count
1434 NewField(fieldTransferSize, xferSize),
1435 NewField(fieldFileSize, hlFile.ffo.FlatFileDataForkHeader.DataSize[:]),
1441 // Download all files from the specified folder and sub-folders
1442 func HandleDownloadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1443 if !authorize(cc.Account.Access, accessDownloadFile) {
1444 res = append(res, cc.NewErrReply(t, "You are not allowed to download folders."))
1448 transactionRef := cc.Server.NewTransactionRef()
1449 data := binary.BigEndian.Uint32(transactionRef)
1451 fileTransfer := &FileTransfer{
1452 FileName: t.GetField(fieldFileName).Data,
1453 FilePath: t.GetField(fieldFilePath).Data,
1454 ReferenceNumber: transactionRef,
1455 Type: FolderDownload,
1457 cc.Server.mux.Lock()
1458 cc.Server.FileTransfers[data] = fileTransfer
1459 cc.Server.mux.Unlock()
1460 cc.Transfers[FolderDownload] = append(cc.Transfers[FolderDownload], fileTransfer)
1463 err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
1468 fullFilePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFilePath).Data, t.GetField(fieldFileName).Data)
1473 transferSize, err := CalcTotalSize(fullFilePath)
1477 itemCount, err := CalcItemCount(fullFilePath)
1481 res = append(res, cc.NewReply(t,
1482 NewField(fieldRefNum, transactionRef),
1483 NewField(fieldTransferSize, transferSize),
1484 NewField(fieldFolderItemCount, itemCount),
1485 NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count
1490 // Upload all files from the local folder and its subfolders to the specified path on the server
1491 // Fields used in the request
1494 // 108 transfer size Total size of all items in the folder
1495 // 220 Folder item count
1496 // 204 File transfer options "Optional Currently set to 1" (TODO: ??)
1497 func HandleUploadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1498 transactionRef := cc.Server.NewTransactionRef()
1499 data := binary.BigEndian.Uint32(transactionRef)
1502 if t.GetField(fieldFilePath).Data != nil {
1503 if err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data); err != nil {
1508 // Handle special cases for Upload and Drop Box folders
1509 if !authorize(cc.Account.Access, accessUploadAnywhere) {
1510 if !fp.IsUploadDir() && !fp.IsDropbox() {
1511 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))))
1516 fileTransfer := &FileTransfer{
1517 FileName: t.GetField(fieldFileName).Data,
1518 FilePath: t.GetField(fieldFilePath).Data,
1519 ReferenceNumber: transactionRef,
1521 FolderItemCount: t.GetField(fieldFolderItemCount).Data,
1522 TransferSize: t.GetField(fieldTransferSize).Data,
1524 cc.Server.FileTransfers[data] = fileTransfer
1526 res = append(res, cc.NewReply(t, NewField(fieldRefNum, transactionRef)))
1531 // Fields used in the request:
1534 // 204 File transfer options "Optional
1535 // Used only to resume download, currently has value 2"
1536 // 108 File transfer size "Optional used if download is not resumed"
1537 func HandleUploadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1538 if !authorize(cc.Account.Access, accessUploadFile) {
1539 res = append(res, cc.NewErrReply(t, "You are not allowed to upload files."))
1543 fileName := t.GetField(fieldFileName).Data
1544 filePath := t.GetField(fieldFilePath).Data
1546 transferOptions := t.GetField(fieldFileTransferOptions).Data
1548 // TODO: is this field useful for anything?
1549 // transferSize := t.GetField(fieldTransferSize).Data
1552 if filePath != nil {
1553 if err = fp.UnmarshalBinary(filePath); err != nil {
1558 // Handle special cases for Upload and Drop Box folders
1559 if !authorize(cc.Account.Access, accessUploadAnywhere) {
1560 if !fp.IsUploadDir() && !fp.IsDropbox() {
1561 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))))
1566 transactionRef := cc.Server.NewTransactionRef()
1567 data := binary.BigEndian.Uint32(transactionRef)
1569 cc.Server.mux.Lock()
1570 cc.Server.FileTransfers[data] = &FileTransfer{
1573 ReferenceNumber: transactionRef,
1576 cc.Server.mux.Unlock()
1578 replyT := cc.NewReply(t, NewField(fieldRefNum, transactionRef))
1580 // client has requested to resume a partially transferred file
1581 if transferOptions != nil {
1582 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
1587 fileInfo, err := cc.Server.FS.Stat(fullFilePath + incompleteFileSuffix)
1592 offset := make([]byte, 4)
1593 binary.BigEndian.PutUint32(offset, uint32(fileInfo.Size()))
1595 fileResumeData := NewFileResumeData([]ForkInfoList{
1596 *NewForkInfoList(offset),
1599 b, _ := fileResumeData.BinaryMarshal()
1601 replyT.Fields = append(replyT.Fields, NewField(fieldFileResumeData, b))
1604 res = append(res, replyT)
1608 func HandleSetClientUserInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1610 if len(t.GetField(fieldUserIconID).Data) == 4 {
1611 icon = t.GetField(fieldUserIconID).Data[2:]
1613 icon = t.GetField(fieldUserIconID).Data
1616 cc.UserName = t.GetField(fieldUserName).Data
1618 // the options field is only passed by the client versions > 1.2.3.
1619 options := t.GetField(fieldOptions).Data
1622 optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
1623 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags)))
1625 flagBitmap.SetBit(flagBitmap, userFlagRefusePM, optBitmap.Bit(refusePM))
1626 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
1628 flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, optBitmap.Bit(refuseChat))
1629 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
1631 // Check auto response
1632 if optBitmap.Bit(autoResponse) == 1 {
1633 cc.AutoReply = t.GetField(fieldAutomaticResponse).Data
1635 cc.AutoReply = []byte{}
1639 // Notify all clients of updated user info
1641 tranNotifyChangeUser,
1642 NewField(fieldUserID, *cc.ID),
1643 NewField(fieldUserIconID, *cc.Icon),
1644 NewField(fieldUserFlags, *cc.Flags),
1645 NewField(fieldUserName, cc.UserName),
1651 // HandleKeepAlive responds to keepalive transactions with an empty reply
1652 // * HL 1.9.2 Client sends keepalive msg every 3 minutes
1653 // * HL 1.2.3 Client doesn't send keepalives
1654 func HandleKeepAlive(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1655 res = append(res, cc.NewReply(t))
1660 func HandleGetFileNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1661 fullPath, err := readPath(
1662 cc.Server.Config.FileRoot,
1663 t.GetField(fieldFilePath).Data,
1671 if t.GetField(fieldFilePath).Data != nil {
1672 if err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data); err != nil {
1677 // Handle special case for drop box folders
1678 if fp.IsDropbox() && !authorize(cc.Account.Access, accessViewDropBoxes) {
1679 res = append(res, cc.NewErrReply(t, "You are not allowed to view drop boxes."))
1683 fileNames, err := getFileNameList(fullPath)
1688 res = append(res, cc.NewReply(t, fileNames...))
1693 // =================================
1694 // Hotline private chat flow
1695 // =================================
1696 // 1. ClientA sends tranInviteNewChat to server with user ID to invite
1697 // 2. Server creates new ChatID
1698 // 3. Server sends tranInviteToChat to invitee
1699 // 4. Server replies to ClientA with new Chat ID
1701 // A dialog box pops up in the invitee client with options to accept or decline the invitation.
1702 // If Accepted is clicked:
1703 // 1. ClientB sends tranJoinChat with fieldChatID
1705 // HandleInviteNewChat invites users to new private chat
1706 func HandleInviteNewChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1707 if !authorize(cc.Account.Access, accessOpenChat) {
1708 res = append(res, cc.NewErrReply(t, "You are not allowed to request private chat."))
1713 targetID := t.GetField(fieldUserID).Data
1714 newChatID := cc.Server.NewPrivateChat(cc)
1720 NewField(fieldChatID, newChatID),
1721 NewField(fieldUserName, cc.UserName),
1722 NewField(fieldUserID, *cc.ID),
1728 NewField(fieldChatID, newChatID),
1729 NewField(fieldUserName, cc.UserName),
1730 NewField(fieldUserID, *cc.ID),
1731 NewField(fieldUserIconID, *cc.Icon),
1732 NewField(fieldUserFlags, *cc.Flags),
1739 func HandleInviteToChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1740 if !authorize(cc.Account.Access, accessOpenChat) {
1741 res = append(res, cc.NewErrReply(t, "You are not allowed to request private chat."))
1746 targetID := t.GetField(fieldUserID).Data
1747 chatID := t.GetField(fieldChatID).Data
1753 NewField(fieldChatID, chatID),
1754 NewField(fieldUserName, cc.UserName),
1755 NewField(fieldUserID, *cc.ID),
1761 NewField(fieldChatID, chatID),
1762 NewField(fieldUserName, cc.UserName),
1763 NewField(fieldUserID, *cc.ID),
1764 NewField(fieldUserIconID, *cc.Icon),
1765 NewField(fieldUserFlags, *cc.Flags),
1772 func HandleRejectChatInvite(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1773 chatID := t.GetField(fieldChatID).Data
1774 chatInt := binary.BigEndian.Uint32(chatID)
1776 privChat := cc.Server.PrivateChats[chatInt]
1778 resMsg := append(cc.UserName, []byte(" declined invitation to chat")...)
1780 for _, c := range sortedClients(privChat.ClientConn) {
1785 NewField(fieldChatID, chatID),
1786 NewField(fieldData, resMsg),
1794 // HandleJoinChat is sent from a v1.8+ Hotline client when the joins a private chat
1795 // Fields used in the reply:
1796 // * 115 Chat subject
1797 // * 300 User name with info (Optional)
1798 // * 300 (more user names with info)
1799 func HandleJoinChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1800 chatID := t.GetField(fieldChatID).Data
1801 chatInt := binary.BigEndian.Uint32(chatID)
1803 privChat := cc.Server.PrivateChats[chatInt]
1805 // Send tranNotifyChatChangeUser to current members of the chat to inform of new user
1806 for _, c := range sortedClients(privChat.ClientConn) {
1809 tranNotifyChatChangeUser,
1811 NewField(fieldChatID, chatID),
1812 NewField(fieldUserName, cc.UserName),
1813 NewField(fieldUserID, *cc.ID),
1814 NewField(fieldUserIconID, *cc.Icon),
1815 NewField(fieldUserFlags, *cc.Flags),
1820 privChat.ClientConn[cc.uint16ID()] = cc
1822 replyFields := []Field{NewField(fieldChatSubject, []byte(privChat.Subject))}
1823 for _, c := range sortedClients(privChat.ClientConn) {
1828 Name: string(c.UserName),
1831 replyFields = append(replyFields, NewField(fieldUsernameWithInfo, user.Payload()))
1834 res = append(res, cc.NewReply(t, replyFields...))
1838 // HandleLeaveChat is sent from a v1.8+ Hotline client when the user exits a private chat
1839 // Fields used in the request:
1840 // * 114 fieldChatID
1841 // Reply is not expected.
1842 func HandleLeaveChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1843 chatID := t.GetField(fieldChatID).Data
1844 chatInt := binary.BigEndian.Uint32(chatID)
1846 privChat := cc.Server.PrivateChats[chatInt]
1848 delete(privChat.ClientConn, cc.uint16ID())
1850 // Notify members of the private chat that the user has left
1851 for _, c := range sortedClients(privChat.ClientConn) {
1854 tranNotifyChatDeleteUser,
1856 NewField(fieldChatID, chatID),
1857 NewField(fieldUserID, *cc.ID),
1865 // HandleSetChatSubject is sent from a v1.8+ Hotline client when the user sets a private chat subject
1866 // Fields used in the request:
1868 // * 115 Chat subject Chat subject string
1869 // Reply is not expected.
1870 func HandleSetChatSubject(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1871 chatID := t.GetField(fieldChatID).Data
1872 chatInt := binary.BigEndian.Uint32(chatID)
1874 privChat := cc.Server.PrivateChats[chatInt]
1875 privChat.Subject = string(t.GetField(fieldChatSubject).Data)
1877 for _, c := range sortedClients(privChat.ClientConn) {
1880 tranNotifyChatSubject,
1882 NewField(fieldChatID, chatID),
1883 NewField(fieldChatSubject, t.GetField(fieldChatSubject).Data),
1891 // HandleMakeAlias makes a filer alias using the specified path.
1892 // Fields used in the request:
1895 // 212 File new path Destination path
1897 // Fields used in the reply:
1899 func HandleMakeAlias(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1900 if !authorize(cc.Account.Access, accessMakeAlias) {
1901 res = append(res, cc.NewErrReply(t, "You are not allowed to make aliases."))
1904 fileName := t.GetField(fieldFileName).Data
1905 filePath := t.GetField(fieldFilePath).Data
1906 fileNewPath := t.GetField(fieldFileNewPath).Data
1908 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
1913 fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, fileNewPath, fileName)
1918 cc.logger.Debugw("Make alias", "src", fullFilePath, "dst", fullNewFilePath)
1920 if err := cc.Server.FS.Symlink(fullFilePath, fullNewFilePath); err != nil {
1921 res = append(res, cc.NewErrReply(t, "Error creating alias"))
1925 res = append(res, cc.NewReply(t))