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{}
967 for _, t := range cc.notifyOthers(
969 tranNotifyChangeUser, nil,
970 NewField(fieldUserName, cc.UserName),
971 NewField(fieldUserID, *cc.ID),
972 NewField(fieldUserIconID, *cc.Icon),
973 NewField(fieldUserFlags, *cc.Flags),
976 cc.Server.outbox <- t
979 res = append(res, cc.NewReply(t))
984 const defaultNewsDateFormat = "Jan02 15:04" // Jun23 20:49
985 // "Mon, 02 Jan 2006 15:04:05 MST"
987 const defaultNewsTemplate = `From %s (%s):
991 __________________________________________________________`
993 // HandleTranOldPostNews updates the flat news
994 // Fields used in this request:
996 func HandleTranOldPostNews(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
997 if !authorize(cc.Account.Access, accessNewsPostArt) {
998 res = append(res, cc.NewErrReply(t, "You are not allowed to post news."))
1002 cc.Server.flatNewsMux.Lock()
1003 defer cc.Server.flatNewsMux.Unlock()
1005 newsDateTemplate := defaultNewsDateFormat
1006 if cc.Server.Config.NewsDateFormat != "" {
1007 newsDateTemplate = cc.Server.Config.NewsDateFormat
1010 newsTemplate := defaultNewsTemplate
1011 if cc.Server.Config.NewsDelimiter != "" {
1012 newsTemplate = cc.Server.Config.NewsDelimiter
1015 newsPost := fmt.Sprintf(newsTemplate+"\r", cc.UserName, time.Now().Format(newsDateTemplate), t.GetField(fieldData).Data)
1016 newsPost = strings.Replace(newsPost, "\n", "\r", -1)
1018 // update news in memory
1019 cc.Server.FlatNews = append([]byte(newsPost), cc.Server.FlatNews...)
1021 // update news on disk
1022 if err := ioutil.WriteFile(cc.Server.ConfigDir+"MessageBoard.txt", cc.Server.FlatNews, 0644); err != nil {
1026 // Notify all clients of updated news
1029 NewField(fieldData, []byte(newsPost)),
1032 res = append(res, cc.NewReply(t))
1036 func HandleDisconnectUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1037 if !authorize(cc.Account.Access, accessDisconUser) {
1038 res = append(res, cc.NewErrReply(t, "You are not allowed to disconnect users."))
1042 clientConn := cc.Server.Clients[binary.BigEndian.Uint16(t.GetField(fieldUserID).Data)]
1044 if authorize(clientConn.Account.Access, accessCannotBeDiscon) {
1045 res = append(res, cc.NewErrReply(t, clientConn.Account.Login+" is not allowed to be disconnected."))
1049 if err := clientConn.Connection.Close(); err != nil {
1053 res = append(res, cc.NewReply(t))
1057 // HandleGetNewsCatNameList returns a list of news categories for a path
1058 // Fields used in the request:
1059 // 325 News path (Optional)
1060 func HandleGetNewsCatNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1061 if !authorize(cc.Account.Access, accessNewsReadArt) {
1062 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1066 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1067 cats := cc.Server.GetNewsCatByPath(pathStrs)
1069 // To store the keys in slice in sorted order
1070 keys := make([]string, len(cats))
1072 for k := range cats {
1078 var fieldData []Field
1079 for _, k := range keys {
1081 b, _ := cat.MarshalBinary()
1082 fieldData = append(fieldData, NewField(
1083 fieldNewsCatListData15,
1088 res = append(res, cc.NewReply(t, fieldData...))
1092 func HandleNewNewsCat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1093 if !authorize(cc.Account.Access, accessNewsCreateCat) {
1094 res = append(res, cc.NewErrReply(t, "You are not allowed to create news categories."))
1098 name := string(t.GetField(fieldNewsCatName).Data)
1099 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1101 cats := cc.Server.GetNewsCatByPath(pathStrs)
1102 cats[name] = NewsCategoryListData15{
1105 Articles: map[uint32]*NewsArtData{},
1106 SubCats: make(map[string]NewsCategoryListData15),
1109 if err := cc.Server.writeThreadedNews(); err != nil {
1112 res = append(res, cc.NewReply(t))
1116 // Fields used in the request:
1117 // 322 News category name
1119 func HandleNewNewsFldr(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1120 if !authorize(cc.Account.Access, accessNewsCreateFldr) {
1121 res = append(res, cc.NewErrReply(t, "You are not allowed to create news folders."))
1125 name := string(t.GetField(fieldFileName).Data)
1126 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1128 cc.logger.Infof("Creating new news folder %s", name)
1130 cats := cc.Server.GetNewsCatByPath(pathStrs)
1131 cats[name] = NewsCategoryListData15{
1134 Articles: map[uint32]*NewsArtData{},
1135 SubCats: make(map[string]NewsCategoryListData15),
1137 if err := cc.Server.writeThreadedNews(); err != nil {
1140 res = append(res, cc.NewReply(t))
1144 // Fields used in the request:
1145 // 325 News path Optional
1148 // 321 News article list data Optional
1149 func HandleGetNewsArtNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1150 if !authorize(cc.Account.Access, accessNewsReadArt) {
1151 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1154 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1156 var cat NewsCategoryListData15
1157 cats := cc.Server.ThreadedNews.Categories
1159 for _, fp := range pathStrs {
1161 cats = cats[fp].SubCats
1164 nald := cat.GetNewsArtListData()
1166 res = append(res, cc.NewReply(t, NewField(fieldNewsArtListData, nald.Payload())))
1170 func HandleGetNewsArtData(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1171 if !authorize(cc.Account.Access, accessNewsReadArt) {
1172 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1178 // 326 News article ID
1179 // 327 News article data flavor
1181 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1183 var cat NewsCategoryListData15
1184 cats := cc.Server.ThreadedNews.Categories
1186 for _, fp := range pathStrs {
1188 cats = cats[fp].SubCats
1190 newsArtID := t.GetField(fieldNewsArtID).Data
1192 convertedArtID := binary.BigEndian.Uint16(newsArtID)
1194 art := cat.Articles[uint32(convertedArtID)]
1196 res = append(res, cc.NewReply(t))
1201 // 328 News article title
1202 // 329 News article poster
1203 // 330 News article date
1204 // 331 Previous article ID
1205 // 332 Next article ID
1206 // 335 Parent article ID
1207 // 336 First child article ID
1208 // 327 News article data flavor "Should be “text/plain”
1209 // 333 News article data Optional (if data flavor is “text/plain”)
1211 res = append(res, cc.NewReply(t,
1212 NewField(fieldNewsArtTitle, []byte(art.Title)),
1213 NewField(fieldNewsArtPoster, []byte(art.Poster)),
1214 NewField(fieldNewsArtDate, art.Date),
1215 NewField(fieldNewsArtPrevArt, art.PrevArt),
1216 NewField(fieldNewsArtNextArt, art.NextArt),
1217 NewField(fieldNewsArtParentArt, art.ParentArt),
1218 NewField(fieldNewsArt1stChildArt, art.FirstChildArt),
1219 NewField(fieldNewsArtDataFlav, []byte("text/plain")),
1220 NewField(fieldNewsArtData, []byte(art.Data)),
1225 func HandleDelNewsItem(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1226 // Has multiple access flags: News Delete Folder (37) or News Delete Category (35)
1229 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1231 // TODO: determine if path is a Folder (Bundle) or Category and check for permission
1233 cc.logger.Infof("DelNewsItem %v", pathStrs)
1235 cats := cc.Server.ThreadedNews.Categories
1237 delName := pathStrs[len(pathStrs)-1]
1238 if len(pathStrs) > 1 {
1239 for _, fp := range pathStrs[0 : len(pathStrs)-1] {
1240 cats = cats[fp].SubCats
1244 delete(cats, delName)
1246 err = cc.Server.writeThreadedNews()
1251 // Reply params: none
1252 res = append(res, cc.NewReply(t))
1257 func HandleDelNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1258 if !authorize(cc.Account.Access, accessNewsDeleteArt) {
1259 res = append(res, cc.NewErrReply(t, "You are not allowed to delete news articles."))
1265 // 326 News article ID
1266 // 337 News article – recursive delete Delete child articles (1) or not (0)
1267 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1268 ID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data)
1270 // TODO: Delete recursive
1271 cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1])
1273 catName := pathStrs[len(pathStrs)-1]
1274 cat := cats[catName]
1276 delete(cat.Articles, uint32(ID))
1279 if err := cc.Server.writeThreadedNews(); err != nil {
1283 res = append(res, cc.NewReply(t))
1289 // 326 News article ID ID of the parent article?
1290 // 328 News article title
1291 // 334 News article flags
1292 // 327 News article data flavor Currently “text/plain”
1293 // 333 News article data
1294 func HandlePostNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1295 if !authorize(cc.Account.Access, accessNewsPostArt) {
1296 res = append(res, cc.NewErrReply(t, "You are not allowed to post news articles."))
1300 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1301 cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1])
1303 catName := pathStrs[len(pathStrs)-1]
1304 cat := cats[catName]
1306 newArt := NewsArtData{
1307 Title: string(t.GetField(fieldNewsArtTitle).Data),
1308 Poster: string(cc.UserName),
1309 Date: toHotlineTime(time.Now()),
1310 PrevArt: []byte{0, 0, 0, 0},
1311 NextArt: []byte{0, 0, 0, 0},
1312 ParentArt: append([]byte{0, 0}, t.GetField(fieldNewsArtID).Data...),
1313 FirstChildArt: []byte{0, 0, 0, 0},
1314 DataFlav: []byte("text/plain"),
1315 Data: string(t.GetField(fieldNewsArtData).Data),
1319 for k := range cat.Articles {
1320 keys = append(keys, int(k))
1326 prevID := uint32(keys[len(keys)-1])
1329 binary.BigEndian.PutUint32(newArt.PrevArt, prevID)
1331 // Set next article ID
1332 binary.BigEndian.PutUint32(cat.Articles[prevID].NextArt, nextID)
1335 // Update parent article with first child reply
1336 parentID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data)
1338 parentArt := cat.Articles[uint32(parentID)]
1340 if bytes.Equal(parentArt.FirstChildArt, []byte{0, 0, 0, 0}) {
1341 binary.BigEndian.PutUint32(parentArt.FirstChildArt, nextID)
1345 cat.Articles[nextID] = &newArt
1348 if err := cc.Server.writeThreadedNews(); err != nil {
1352 res = append(res, cc.NewReply(t))
1356 // HandleGetMsgs returns the flat news data
1357 func HandleGetMsgs(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1358 if !authorize(cc.Account.Access, accessNewsReadArt) {
1359 res = append(res, cc.NewErrReply(t, "You are not allowed to read news."))
1363 res = append(res, cc.NewReply(t, NewField(fieldData, cc.Server.FlatNews)))
1368 func HandleDownloadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1369 if !authorize(cc.Account.Access, accessDownloadFile) {
1370 res = append(res, cc.NewErrReply(t, "You are not allowed to download files."))
1374 fileName := t.GetField(fieldFileName).Data
1375 filePath := t.GetField(fieldFilePath).Data
1376 resumeData := t.GetField(fieldFileResumeData).Data
1378 var dataOffset int64
1379 var frd FileResumeData
1380 if resumeData != nil {
1381 if err := frd.UnmarshalBinary(t.GetField(fieldFileResumeData).Data); err != nil {
1384 // TODO: handle rsrc fork offset
1385 dataOffset = int64(binary.BigEndian.Uint32(frd.ForkInfoList[0].DataSize[:]))
1388 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
1393 hlFile, err := newFileWrapper(cc.Server.FS, fullFilePath, dataOffset)
1398 transactionRef := cc.Server.NewTransactionRef()
1399 data := binary.BigEndian.Uint32(transactionRef)
1401 ft := &FileTransfer{
1404 ReferenceNumber: transactionRef,
1408 // TODO: refactor to remove this
1409 if resumeData != nil {
1410 var frd FileResumeData
1411 if err := frd.UnmarshalBinary(t.GetField(fieldFileResumeData).Data); err != nil {
1414 ft.fileResumeData = &frd
1417 xferSize := hlFile.ffo.TransferSize(0)
1419 // Optional field for when a HL v1.5+ client requests file preview
1420 // Used only for TEXT, JPEG, GIFF, BMP or PICT files
1421 // The value will always be 2
1422 if t.GetField(fieldFileTransferOptions).Data != nil {
1423 ft.options = t.GetField(fieldFileTransferOptions).Data
1424 xferSize = hlFile.ffo.FlatFileDataForkHeader.DataSize[:]
1427 cc.Server.mux.Lock()
1428 defer cc.Server.mux.Unlock()
1429 cc.Server.FileTransfers[data] = ft
1431 cc.Transfers[FileDownload] = append(cc.Transfers[FileDownload], ft)
1433 res = append(res, cc.NewReply(t,
1434 NewField(fieldRefNum, transactionRef),
1435 NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count
1436 NewField(fieldTransferSize, xferSize),
1437 NewField(fieldFileSize, hlFile.ffo.FlatFileDataForkHeader.DataSize[:]),
1443 // Download all files from the specified folder and sub-folders
1444 func HandleDownloadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1445 if !authorize(cc.Account.Access, accessDownloadFile) {
1446 res = append(res, cc.NewErrReply(t, "You are not allowed to download folders."))
1450 transactionRef := cc.Server.NewTransactionRef()
1451 data := binary.BigEndian.Uint32(transactionRef)
1453 fileTransfer := &FileTransfer{
1454 FileName: t.GetField(fieldFileName).Data,
1455 FilePath: t.GetField(fieldFilePath).Data,
1456 ReferenceNumber: transactionRef,
1457 Type: FolderDownload,
1459 cc.Server.mux.Lock()
1460 cc.Server.FileTransfers[data] = fileTransfer
1461 cc.Server.mux.Unlock()
1462 cc.Transfers[FolderDownload] = append(cc.Transfers[FolderDownload], fileTransfer)
1465 err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
1470 fullFilePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFilePath).Data, t.GetField(fieldFileName).Data)
1475 transferSize, err := CalcTotalSize(fullFilePath)
1479 itemCount, err := CalcItemCount(fullFilePath)
1483 res = append(res, cc.NewReply(t,
1484 NewField(fieldRefNum, transactionRef),
1485 NewField(fieldTransferSize, transferSize),
1486 NewField(fieldFolderItemCount, itemCount),
1487 NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count
1492 // Upload all files from the local folder and its subfolders to the specified path on the server
1493 // Fields used in the request
1496 // 108 transfer size Total size of all items in the folder
1497 // 220 Folder item count
1498 // 204 File transfer options "Optional Currently set to 1" (TODO: ??)
1499 func HandleUploadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1500 transactionRef := cc.Server.NewTransactionRef()
1501 data := binary.BigEndian.Uint32(transactionRef)
1504 if t.GetField(fieldFilePath).Data != nil {
1505 if err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data); err != nil {
1510 // Handle special cases for Upload and Drop Box folders
1511 if !authorize(cc.Account.Access, accessUploadAnywhere) {
1512 if !fp.IsUploadDir() && !fp.IsDropbox() {
1513 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))))
1518 fileTransfer := &FileTransfer{
1519 FileName: t.GetField(fieldFileName).Data,
1520 FilePath: t.GetField(fieldFilePath).Data,
1521 ReferenceNumber: transactionRef,
1523 FolderItemCount: t.GetField(fieldFolderItemCount).Data,
1524 TransferSize: t.GetField(fieldTransferSize).Data,
1526 cc.Server.FileTransfers[data] = fileTransfer
1528 res = append(res, cc.NewReply(t, NewField(fieldRefNum, transactionRef)))
1533 // Fields used in the request:
1536 // 204 File transfer options "Optional
1537 // Used only to resume download, currently has value 2"
1538 // 108 File transfer size "Optional used if download is not resumed"
1539 func HandleUploadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1540 if !authorize(cc.Account.Access, accessUploadFile) {
1541 res = append(res, cc.NewErrReply(t, "You are not allowed to upload files."))
1545 fileName := t.GetField(fieldFileName).Data
1546 filePath := t.GetField(fieldFilePath).Data
1548 transferOptions := t.GetField(fieldFileTransferOptions).Data
1550 // TODO: is this field useful for anything?
1551 // transferSize := t.GetField(fieldTransferSize).Data
1554 if filePath != nil {
1555 if err = fp.UnmarshalBinary(filePath); err != nil {
1560 // Handle special cases for Upload and Drop Box folders
1561 if !authorize(cc.Account.Access, accessUploadAnywhere) {
1562 if !fp.IsUploadDir() && !fp.IsDropbox() {
1563 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))))
1568 transactionRef := cc.Server.NewTransactionRef()
1569 data := binary.BigEndian.Uint32(transactionRef)
1571 cc.Server.mux.Lock()
1572 cc.Server.FileTransfers[data] = &FileTransfer{
1575 ReferenceNumber: transactionRef,
1578 cc.Server.mux.Unlock()
1580 replyT := cc.NewReply(t, NewField(fieldRefNum, transactionRef))
1582 // client has requested to resume a partially transferred file
1583 if transferOptions != nil {
1584 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
1589 fileInfo, err := cc.Server.FS.Stat(fullFilePath + incompleteFileSuffix)
1594 offset := make([]byte, 4)
1595 binary.BigEndian.PutUint32(offset, uint32(fileInfo.Size()))
1597 fileResumeData := NewFileResumeData([]ForkInfoList{
1598 *NewForkInfoList(offset),
1601 b, _ := fileResumeData.BinaryMarshal()
1603 replyT.Fields = append(replyT.Fields, NewField(fieldFileResumeData, b))
1606 res = append(res, replyT)
1610 func HandleSetClientUserInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1612 if len(t.GetField(fieldUserIconID).Data) == 4 {
1613 icon = t.GetField(fieldUserIconID).Data[2:]
1615 icon = t.GetField(fieldUserIconID).Data
1618 cc.UserName = t.GetField(fieldUserName).Data
1620 // the options field is only passed by the client versions > 1.2.3.
1621 options := t.GetField(fieldOptions).Data
1624 optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
1625 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags)))
1627 flagBitmap.SetBit(flagBitmap, userFlagRefusePM, optBitmap.Bit(refusePM))
1628 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
1630 flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, optBitmap.Bit(refuseChat))
1631 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
1633 // Check auto response
1634 if optBitmap.Bit(autoResponse) == 1 {
1635 cc.AutoReply = t.GetField(fieldAutomaticResponse).Data
1637 cc.AutoReply = []byte{}
1641 // Notify all clients of updated user info
1643 tranNotifyChangeUser,
1644 NewField(fieldUserID, *cc.ID),
1645 NewField(fieldUserIconID, *cc.Icon),
1646 NewField(fieldUserFlags, *cc.Flags),
1647 NewField(fieldUserName, cc.UserName),
1653 // HandleKeepAlive responds to keepalive transactions with an empty reply
1654 // * HL 1.9.2 Client sends keepalive msg every 3 minutes
1655 // * HL 1.2.3 Client doesn't send keepalives
1656 func HandleKeepAlive(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1657 res = append(res, cc.NewReply(t))
1662 func HandleGetFileNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1663 fullPath, err := readPath(
1664 cc.Server.Config.FileRoot,
1665 t.GetField(fieldFilePath).Data,
1673 if t.GetField(fieldFilePath).Data != nil {
1674 if err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data); err != nil {
1679 // Handle special case for drop box folders
1680 if fp.IsDropbox() && !authorize(cc.Account.Access, accessViewDropBoxes) {
1681 res = append(res, cc.NewErrReply(t, "You are not allowed to view drop boxes."))
1685 fileNames, err := getFileNameList(fullPath)
1690 res = append(res, cc.NewReply(t, fileNames...))
1695 // =================================
1696 // Hotline private chat flow
1697 // =================================
1698 // 1. ClientA sends tranInviteNewChat to server with user ID to invite
1699 // 2. Server creates new ChatID
1700 // 3. Server sends tranInviteToChat to invitee
1701 // 4. Server replies to ClientA with new Chat ID
1703 // A dialog box pops up in the invitee client with options to accept or decline the invitation.
1704 // If Accepted is clicked:
1705 // 1. ClientB sends tranJoinChat with fieldChatID
1707 // HandleInviteNewChat invites users to new private chat
1708 func HandleInviteNewChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1709 if !authorize(cc.Account.Access, accessOpenChat) {
1710 res = append(res, cc.NewErrReply(t, "You are not allowed to request private chat."))
1715 targetID := t.GetField(fieldUserID).Data
1716 newChatID := cc.Server.NewPrivateChat(cc)
1722 NewField(fieldChatID, newChatID),
1723 NewField(fieldUserName, cc.UserName),
1724 NewField(fieldUserID, *cc.ID),
1730 NewField(fieldChatID, newChatID),
1731 NewField(fieldUserName, cc.UserName),
1732 NewField(fieldUserID, *cc.ID),
1733 NewField(fieldUserIconID, *cc.Icon),
1734 NewField(fieldUserFlags, *cc.Flags),
1741 func HandleInviteToChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1742 if !authorize(cc.Account.Access, accessOpenChat) {
1743 res = append(res, cc.NewErrReply(t, "You are not allowed to request private chat."))
1748 targetID := t.GetField(fieldUserID).Data
1749 chatID := t.GetField(fieldChatID).Data
1755 NewField(fieldChatID, chatID),
1756 NewField(fieldUserName, cc.UserName),
1757 NewField(fieldUserID, *cc.ID),
1763 NewField(fieldChatID, chatID),
1764 NewField(fieldUserName, cc.UserName),
1765 NewField(fieldUserID, *cc.ID),
1766 NewField(fieldUserIconID, *cc.Icon),
1767 NewField(fieldUserFlags, *cc.Flags),
1774 func HandleRejectChatInvite(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1775 chatID := t.GetField(fieldChatID).Data
1776 chatInt := binary.BigEndian.Uint32(chatID)
1778 privChat := cc.Server.PrivateChats[chatInt]
1780 resMsg := append(cc.UserName, []byte(" declined invitation to chat")...)
1782 for _, c := range sortedClients(privChat.ClientConn) {
1787 NewField(fieldChatID, chatID),
1788 NewField(fieldData, resMsg),
1796 // HandleJoinChat is sent from a v1.8+ Hotline client when the joins a private chat
1797 // Fields used in the reply:
1798 // * 115 Chat subject
1799 // * 300 User name with info (Optional)
1800 // * 300 (more user names with info)
1801 func HandleJoinChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1802 chatID := t.GetField(fieldChatID).Data
1803 chatInt := binary.BigEndian.Uint32(chatID)
1805 privChat := cc.Server.PrivateChats[chatInt]
1807 // Send tranNotifyChatChangeUser to current members of the chat to inform of new user
1808 for _, c := range sortedClients(privChat.ClientConn) {
1811 tranNotifyChatChangeUser,
1813 NewField(fieldChatID, chatID),
1814 NewField(fieldUserName, cc.UserName),
1815 NewField(fieldUserID, *cc.ID),
1816 NewField(fieldUserIconID, *cc.Icon),
1817 NewField(fieldUserFlags, *cc.Flags),
1822 privChat.ClientConn[cc.uint16ID()] = cc
1824 replyFields := []Field{NewField(fieldChatSubject, []byte(privChat.Subject))}
1825 for _, c := range sortedClients(privChat.ClientConn) {
1830 Name: string(c.UserName),
1833 replyFields = append(replyFields, NewField(fieldUsernameWithInfo, user.Payload()))
1836 res = append(res, cc.NewReply(t, replyFields...))
1840 // HandleLeaveChat is sent from a v1.8+ Hotline client when the user exits a private chat
1841 // Fields used in the request:
1842 // * 114 fieldChatID
1843 // Reply is not expected.
1844 func HandleLeaveChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1845 chatID := t.GetField(fieldChatID).Data
1846 chatInt := binary.BigEndian.Uint32(chatID)
1848 privChat := cc.Server.PrivateChats[chatInt]
1850 delete(privChat.ClientConn, cc.uint16ID())
1852 // Notify members of the private chat that the user has left
1853 for _, c := range sortedClients(privChat.ClientConn) {
1856 tranNotifyChatDeleteUser,
1858 NewField(fieldChatID, chatID),
1859 NewField(fieldUserID, *cc.ID),
1867 // HandleSetChatSubject is sent from a v1.8+ Hotline client when the user sets a private chat subject
1868 // Fields used in the request:
1870 // * 115 Chat subject Chat subject string
1871 // Reply is not expected.
1872 func HandleSetChatSubject(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1873 chatID := t.GetField(fieldChatID).Data
1874 chatInt := binary.BigEndian.Uint32(chatID)
1876 privChat := cc.Server.PrivateChats[chatInt]
1877 privChat.Subject = string(t.GetField(fieldChatSubject).Data)
1879 for _, c := range sortedClients(privChat.ClientConn) {
1882 tranNotifyChatSubject,
1884 NewField(fieldChatID, chatID),
1885 NewField(fieldChatSubject, t.GetField(fieldChatSubject).Data),
1893 // HandleMakeAlias makes a filer alias using the specified path.
1894 // Fields used in the request:
1897 // 212 File new path Destination path
1899 // Fields used in the reply:
1901 func HandleMakeAlias(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1902 if !authorize(cc.Account.Access, accessMakeAlias) {
1903 res = append(res, cc.NewErrReply(t, "You are not allowed to make aliases."))
1906 fileName := t.GetField(fieldFileName).Data
1907 filePath := t.GetField(fieldFilePath).Data
1908 fileNewPath := t.GetField(fieldFileNewPath).Data
1910 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
1915 fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, fileNewPath, fileName)
1920 cc.logger.Debugw("Make alias", "src", fullFilePath, "dst", fullNewFilePath)
1922 if err := cc.Server.FS.Symlink(fullFilePath, fullNewFilePath); err != nil {
1923 res = append(res, cc.NewErrReply(t, "Error creating alias"))
1927 res = append(res, cc.NewReply(t))