18 type TransactionType struct {
19 Access int // Specifies access privilege required to perform the transaction
20 DenyMsg string // The error reply message when user does not have access
21 Handler func(*ClientConn, *Transaction) ([]Transaction, error) // function for handling the transaction type
22 Name string // Name of transaction as it will appear in logging
23 RequiredFields []requiredField
26 var TransactionHandlers = map[uint16]TransactionType{
32 tranNotifyChangeUser: {
33 Name: "tranNotifyChangeUser",
39 Name: "tranShowAgreement",
42 Name: "tranUserAccess",
44 tranNotifyDeleteUser: {
45 Name: "tranNotifyDeleteUser",
48 Access: accessAlwaysAllow,
50 Handler: HandleTranAgreed,
53 Access: accessSendChat,
54 DenyMsg: "You are not allowed to participate in chat.",
55 Handler: HandleChatSend,
57 RequiredFields: []requiredField{
65 Access: accessNewsDeleteArt,
66 DenyMsg: "You are not allowed to delete news articles.",
67 Name: "tranDelNewsArt",
68 Handler: HandleDelNewsArt,
71 Access: accessAlwaysAllow, // Granular access enforced inside the handler
72 // Has multiple access flags: News Delete Folder (37) or News Delete Category (35)
73 // TODO: Implement inside the handler
74 Name: "tranDelNewsItem",
75 Handler: HandleDelNewsItem,
78 Access: accessAlwaysAllow, // Granular access enforced inside the handler
79 Name: "tranDeleteFile",
80 Handler: HandleDeleteFile,
83 Access: accessDeleteUser,
84 DenyMsg: "You are not allowed to delete accounts.",
85 Name: "tranDeleteUser",
86 Handler: HandleDeleteUser,
89 Access: accessDisconUser,
90 DenyMsg: "You are not allowed to disconnect users.",
91 Name: "tranDisconnectUser",
92 Handler: HandleDisconnectUser,
95 Access: accessDownloadFile,
96 DenyMsg: "You are not allowed to download files.",
97 Name: "tranDownloadFile",
98 Handler: HandleDownloadFile,
101 Access: accessDownloadFile, // There is no specific access flag for folder vs file download
102 DenyMsg: "You are not allowed to download files.",
103 Name: "tranDownloadFldr",
104 Handler: HandleDownloadFolder,
106 tranGetClientInfoText: {
107 Access: accessGetClientInfo,
108 DenyMsg: "You are not allowed to get client info",
109 Name: "tranGetClientInfoText",
110 Handler: HandleGetClientConnInfoText,
113 Access: accessAlwaysAllow,
114 Name: "tranGetFileInfo",
115 Handler: HandleGetFileInfo,
117 tranGetFileNameList: {
118 Access: accessAlwaysAllow,
119 Name: "tranGetFileNameList",
120 Handler: HandleGetFileNameList,
123 Access: accessNewsReadArt,
124 DenyMsg: "You are not allowed to read news.",
126 Handler: HandleGetMsgs,
128 tranGetNewsArtData: {
129 Access: accessNewsReadArt,
130 DenyMsg: "You are not allowed to read news.",
131 Name: "tranGetNewsArtData",
132 Handler: HandleGetNewsArtData,
134 tranGetNewsArtNameList: {
135 Access: accessNewsReadArt,
136 DenyMsg: "You are not allowed to read news.",
137 Name: "tranGetNewsArtNameList",
138 Handler: HandleGetNewsArtNameList,
140 tranGetNewsCatNameList: {
141 Access: accessNewsReadArt,
142 DenyMsg: "You are not allowed to read news.",
143 Name: "tranGetNewsCatNameList",
144 Handler: HandleGetNewsCatNameList,
147 Access: accessOpenUser,
148 DenyMsg: "You are not allowed to view accounts.",
150 Handler: HandleGetUser,
152 tranGetUserNameList: {
153 Access: accessAlwaysAllow,
154 Name: "tranHandleGetUserNameList",
155 Handler: HandleGetUserNameList,
158 Access: accessOpenChat,
159 DenyMsg: "You are not allowed to request private chat.",
160 Name: "tranInviteNewChat",
161 Handler: HandleInviteNewChat,
164 Access: accessOpenChat,
165 DenyMsg: "You are not allowed to request private chat.",
166 Name: "tranInviteToChat",
167 Handler: HandleInviteToChat,
170 Access: accessAlwaysAllow,
171 Name: "tranJoinChat",
172 Handler: HandleJoinChat,
175 Access: accessAlwaysAllow,
176 Name: "tranKeepAlive",
177 Handler: HandleKeepAlive,
180 Access: accessAlwaysAllow,
181 Name: "tranJoinChat",
182 Handler: HandleLeaveChat,
186 Access: accessOpenUser,
187 DenyMsg: "You are not allowed to view accounts.",
188 Name: "tranListUsers",
189 Handler: HandleListUsers,
192 Access: accessMoveFile,
193 DenyMsg: "You are not allowed to move files.",
194 Name: "tranMoveFile",
195 Handler: HandleMoveFile,
198 Access: accessCreateFolder,
199 DenyMsg: "You are not allow to create folders.",
200 Name: "tranNewFolder",
201 Handler: HandleNewFolder,
204 Access: accessNewsCreateCat,
205 DenyMsg: "You are not allowed to create news categories.",
206 Name: "tranNewNewsCat",
207 Handler: HandleNewNewsCat,
210 Access: accessNewsCreateFldr,
211 DenyMsg: "You are not allowed to create news folders.",
212 Name: "tranNewNewsFldr",
213 Handler: HandleNewNewsFldr,
216 Access: accessCreateUser,
217 DenyMsg: "You are not allowed to create new accounts.",
219 Handler: HandleNewUser,
222 Access: accessNewsPostArt,
223 DenyMsg: "You are not allowed to post news.",
224 Name: "tranOldPostNews",
225 Handler: HandleTranOldPostNews,
228 Access: accessNewsPostArt,
229 DenyMsg: "You are not allowed to post news articles.",
230 Name: "tranPostNewsArt",
231 Handler: HandlePostNewsArt,
233 tranRejectChatInvite: {
234 Access: accessAlwaysAllow,
235 Name: "tranRejectChatInvite",
236 Handler: HandleRejectChatInvite,
238 tranSendInstantMsg: {
239 Access: accessAlwaysAllow,
240 // Access: accessSendPrivMsg,
241 // DenyMsg: "You are not allowed to send private messages",
242 Name: "tranSendInstantMsg",
243 Handler: HandleSendInstantMsg,
244 RequiredFields: []requiredField{
254 tranSetChatSubject: {
255 Access: accessAlwaysAllow,
256 Name: "tranSetChatSubject",
257 Handler: HandleSetChatSubject,
260 Access: accessAlwaysAllow,
261 Name: "tranMakeFileAlias",
262 Handler: HandleMakeAlias,
263 RequiredFields: []requiredField{
264 {ID: fieldFileName, minLen: 1},
265 {ID: fieldFilePath, minLen: 1},
266 {ID: fieldFileNewPath, minLen: 1},
269 tranSetClientUserInfo: {
270 Access: accessAlwaysAllow,
271 Name: "tranSetClientUserInfo",
272 Handler: HandleSetClientUserInfo,
275 Access: accessAlwaysAllow, // granular access is in the handler
276 Name: "tranSetFileInfo",
277 Handler: HandleSetFileInfo,
280 Access: accessModifyUser,
281 DenyMsg: "You are not allowed to modify accounts.",
283 Handler: HandleSetUser,
286 Access: accessAlwaysAllow,
287 DenyMsg: "You are not allowed to upload files.",
288 Name: "tranUploadFile",
289 Handler: HandleUploadFile,
292 Access: accessAlwaysAllow, // TODO: what should this be?
293 Name: "tranUploadFldr",
294 Handler: HandleUploadFolder,
297 Access: accessBroadcast,
298 DenyMsg: "You are not allowed to send broadcast messages.",
299 Name: "tranUserBroadcast",
300 Handler: HandleUserBroadcast,
304 func HandleChatSend(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
305 // Truncate long usernames
306 trunc := fmt.Sprintf("%13s", cc.UserName)
307 formattedMsg := fmt.Sprintf("\r%.14s: %s", trunc, t.GetField(fieldData).Data)
309 // By holding the option key, Hotline chat allows users to send /me formatted messages like:
310 // *** Halcyon does stuff
311 // This is indicated by the presence of the optional field fieldChatOptions in the transaction payload
312 if t.GetField(fieldChatOptions).Data != nil {
313 formattedMsg = fmt.Sprintf("\r*** %s %s", cc.UserName, t.GetField(fieldData).Data)
316 if bytes.Equal(t.GetField(fieldData).Data, []byte("/stats")) {
317 formattedMsg = strings.Replace(cc.Server.Stats.String(), "\n", "\r", -1)
320 chatID := t.GetField(fieldChatID).Data
321 // a non-nil chatID indicates the message belongs to a private chat
323 chatInt := binary.BigEndian.Uint32(chatID)
324 privChat := cc.Server.PrivateChats[chatInt]
326 // send the message to all connected clients of the private chat
327 for _, c := range privChat.ClientConn {
328 res = append(res, *NewTransaction(
331 NewField(fieldChatID, chatID),
332 NewField(fieldData, []byte(formattedMsg)),
338 for _, c := range sortedClients(cc.Server.Clients) {
339 // Filter out clients that do not have the read chat permission
340 if authorize(c.Account.Access, accessReadChat) {
341 res = append(res, *NewTransaction(tranChatMsg, c.ID, NewField(fieldData, []byte(formattedMsg))))
348 // HandleSendInstantMsg sends instant message to the user on the current server.
349 // Fields used in the request:
352 // One of the following values:
353 // - User message (myOpt_UserMessage = 1)
354 // - Refuse message (myOpt_RefuseMessage = 2)
355 // - Refuse chat (myOpt_RefuseChat = 3)
356 // - Automatic response (myOpt_AutomaticResponse = 4)"
358 // 214 Quoting message Optional
360 // Fields used in the reply:
362 func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
363 msg := t.GetField(fieldData)
364 ID := t.GetField(fieldUserID)
365 // TODO: Implement reply quoting
366 // options := transaction.GetField(hotline.fieldOptions)
372 NewField(fieldData, msg.Data),
373 NewField(fieldUserName, cc.UserName),
374 NewField(fieldUserID, *cc.ID),
375 NewField(fieldOptions, []byte{0, 1}),
378 id, _ := byteToInt(ID.Data)
380 otherClient := cc.Server.Clients[uint16(id)]
381 if otherClient == nil {
382 return res, errors.New("ohno")
385 // Respond with auto reply if other client has it enabled
386 if len(otherClient.AutoReply) > 0 {
391 NewField(fieldData, otherClient.AutoReply),
392 NewField(fieldUserName, otherClient.UserName),
393 NewField(fieldUserID, *otherClient.ID),
394 NewField(fieldOptions, []byte{0, 1}),
399 res = append(res, cc.NewReply(t))
404 func HandleGetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
405 fileName := t.GetField(fieldFileName).Data
406 filePath := t.GetField(fieldFilePath).Data
408 ffo, err := NewFlattenedFileObject(cc.Server.Config.FileRoot, filePath, fileName)
413 res = append(res, cc.NewReply(t,
414 NewField(fieldFileName, fileName),
415 NewField(fieldFileTypeString, ffo.FlatFileInformationFork.TypeSignature),
416 NewField(fieldFileCreatorString, ffo.FlatFileInformationFork.CreatorSignature),
417 NewField(fieldFileComment, ffo.FlatFileInformationFork.Comment),
418 NewField(fieldFileType, ffo.FlatFileInformationFork.TypeSignature),
419 NewField(fieldFileCreateDate, ffo.FlatFileInformationFork.CreateDate),
420 NewField(fieldFileModifyDate, ffo.FlatFileInformationFork.ModifyDate),
421 NewField(fieldFileSize, ffo.FlatFileDataForkHeader.DataSize),
426 // HandleSetFileInfo updates a file or folder name and/or comment from the Get Info window
427 // TODO: Implement support for comments
428 // Fields used in the request:
430 // * 202 File path Optional
431 // * 211 File new name Optional
432 // * 210 File comment Optional
433 // Fields used in the reply: None
434 func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
435 fileName := t.GetField(fieldFileName).Data
436 filePath := t.GetField(fieldFilePath).Data
438 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
443 fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, t.GetField(fieldFileNewName).Data)
448 // fileComment := t.GetField(fieldFileComment).Data
449 fileNewName := t.GetField(fieldFileNewName).Data
451 if fileNewName != nil {
452 fi, err := FS.Stat(fullFilePath)
456 switch mode := fi.Mode(); {
458 if !authorize(cc.Account.Access, accessRenameFolder) {
459 res = append(res, cc.NewErrReply(t, "You are not allowed to rename folders."))
462 case mode.IsRegular():
463 if !authorize(cc.Account.Access, accessRenameFile) {
464 res = append(res, cc.NewErrReply(t, "You are not allowed to rename files."))
469 err = os.Rename(fullFilePath, fullNewFilePath)
470 if os.IsNotExist(err) {
471 res = append(res, cc.NewErrReply(t, "Cannot rename file "+string(fileName)+" because it does not exist or cannot be found."))
476 res = append(res, cc.NewReply(t))
480 // HandleDeleteFile deletes a file or folder
481 // Fields used in the request:
484 // Fields used in the reply: none
485 func HandleDeleteFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
486 fileName := t.GetField(fieldFileName).Data
487 filePath := t.GetField(fieldFilePath).Data
489 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
494 cc.Server.Logger.Debugw("Delete file", "src", fullFilePath)
496 fi, err := os.Stat(fullFilePath)
498 res = append(res, cc.NewErrReply(t, "Cannot delete file "+string(fileName)+" because it does not exist or cannot be found."))
501 switch mode := fi.Mode(); {
503 if !authorize(cc.Account.Access, accessDeleteFolder) {
504 res = append(res, cc.NewErrReply(t, "You are not allowed to delete folders."))
507 case mode.IsRegular():
508 if !authorize(cc.Account.Access, accessDeleteFile) {
509 res = append(res, cc.NewErrReply(t, "You are not allowed to delete files."))
514 if err := os.RemoveAll(fullFilePath); err != nil {
518 res = append(res, cc.NewReply(t))
522 // HandleMoveFile moves files or folders. Note: seemingly not documented
523 func HandleMoveFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
524 fileName := string(t.GetField(fieldFileName).Data)
525 filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data)
526 fileNewPath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFileNewPath).Data)
528 cc.Server.Logger.Debugw("Move file", "src", filePath+"/"+fileName, "dst", fileNewPath+"/"+fileName)
530 path := filePath + "/" + fileName
531 fi, err := os.Stat(path)
535 switch mode := fi.Mode(); {
537 if !authorize(cc.Account.Access, accessMoveFolder) {
538 res = append(res, cc.NewErrReply(t, "You are not allowed to move folders."))
541 case mode.IsRegular():
542 if !authorize(cc.Account.Access, accessMoveFile) {
543 res = append(res, cc.NewErrReply(t, "You are not allowed to move files."))
548 err = os.Rename(filePath+"/"+fileName, fileNewPath+"/"+fileName)
549 if os.IsNotExist(err) {
550 res = append(res, cc.NewErrReply(t, "Cannot delete file "+fileName+" because it does not exist or cannot be found."))
554 return []Transaction{}, err
556 // TODO: handle other possible errors; e.g. file delete fails due to file permission issue
558 res = append(res, cc.NewReply(t))
562 func HandleNewFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
563 newFolderPath := cc.Server.Config.FileRoot
564 folderName := string(t.GetField(fieldFileName).Data)
566 folderName = path.Join("/", folderName)
568 // fieldFilePath is only present for nested paths
569 if t.GetField(fieldFilePath).Data != nil {
571 err := newFp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
575 newFolderPath += newFp.String()
577 newFolderPath = path.Join(newFolderPath, folderName)
579 // TODO: check path and folder name lengths
581 if _, err := FS.Stat(newFolderPath); !os.IsNotExist(err) {
582 msg := fmt.Sprintf("Cannot create folder \"%s\" because there is already a file or folder with that name.", folderName)
583 return []Transaction{cc.NewErrReply(t, msg)}, nil
586 // TODO: check for disallowed characters to maintain compatibility for original client
588 if err := FS.Mkdir(newFolderPath, 0777); err != nil {
589 msg := fmt.Sprintf("Cannot create folder \"%s\" because an error occurred.", folderName)
590 return []Transaction{cc.NewErrReply(t, msg)}, nil
593 res = append(res, cc.NewReply(t))
597 func HandleSetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
598 login := DecodeUserString(t.GetField(fieldUserLogin).Data)
599 userName := string(t.GetField(fieldUserName).Data)
601 newAccessLvl := t.GetField(fieldUserAccess).Data
603 account := cc.Server.Accounts[login]
604 account.Access = &newAccessLvl
605 account.Name = userName
607 // If the password field is cleared in the Hotline edit user UI, the SetUser transaction does
608 // not include fieldUserPassword
609 if t.GetField(fieldUserPassword).Data == nil {
610 account.Password = hashAndSalt([]byte(""))
612 if len(t.GetField(fieldUserPassword).Data) > 1 {
613 account.Password = hashAndSalt(t.GetField(fieldUserPassword).Data)
616 file := cc.Server.ConfigDir + "Users/" + login + ".yaml"
617 out, err := yaml.Marshal(&account)
621 if err := ioutil.WriteFile(file, out, 0666); err != nil {
625 // Notify connected clients logged in as the user of the new access level
626 for _, c := range cc.Server.Clients {
627 if c.Account.Login == login {
628 // Note: comment out these two lines to test server-side deny messages
629 newT := NewTransaction(tranUserAccess, c.ID, NewField(fieldUserAccess, newAccessLvl))
630 res = append(res, *newT)
632 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*c.Flags)))
633 if authorize(c.Account.Access, accessDisconUser) {
634 flagBitmap.SetBit(flagBitmap, userFlagAdmin, 1)
636 flagBitmap.SetBit(flagBitmap, userFlagAdmin, 0)
638 binary.BigEndian.PutUint16(*c.Flags, uint16(flagBitmap.Int64()))
640 c.Account.Access = account.Access
643 tranNotifyChangeUser,
644 NewField(fieldUserID, *c.ID),
645 NewField(fieldUserFlags, *c.Flags),
646 NewField(fieldUserName, c.UserName),
647 NewField(fieldUserIconID, *c.Icon),
652 // TODO: If we have just promoted a connected user to admin, notify
653 // connected clients to turn the user red
655 res = append(res, cc.NewReply(t))
659 func HandleGetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
660 // userLogin := string(t.GetField(fieldUserLogin).Data)
661 account := cc.Server.Accounts[string(t.GetField(fieldUserLogin).Data)]
663 errorT := cc.NewErrReply(t, "Account does not exist.")
664 res = append(res, errorT)
668 res = append(res, cc.NewReply(t,
669 NewField(fieldUserName, []byte(account.Name)),
670 NewField(fieldUserLogin, negateString(t.GetField(fieldUserLogin).Data)),
671 NewField(fieldUserPassword, []byte(account.Password)),
672 NewField(fieldUserAccess, *account.Access),
677 func HandleListUsers(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
678 var userFields []Field
679 // TODO: make order deterministic
680 for _, acc := range cc.Server.Accounts {
681 userField := acc.MarshalBinary()
682 userFields = append(userFields, NewField(fieldData, userField))
685 res = append(res, cc.NewReply(t, userFields...))
689 // HandleNewUser creates a new user account
690 func HandleNewUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
691 login := DecodeUserString(t.GetField(fieldUserLogin).Data)
693 // If the account already exists, reply with an error
694 // TODO: make order deterministic
695 if _, ok := cc.Server.Accounts[login]; ok {
696 res = append(res, cc.NewErrReply(t, "Cannot create account "+login+" because there is already an account with that login."))
700 if err := cc.Server.NewUser(
702 string(t.GetField(fieldUserName).Data),
703 string(t.GetField(fieldUserPassword).Data),
704 t.GetField(fieldUserAccess).Data,
706 return []Transaction{}, err
709 res = append(res, cc.NewReply(t))
713 func HandleDeleteUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
714 // TODO: Handle case where account doesn't exist; e.g. delete race condition
715 login := DecodeUserString(t.GetField(fieldUserLogin).Data)
717 if err := cc.Server.DeleteUser(login); err != nil {
721 res = append(res, cc.NewReply(t))
725 // HandleUserBroadcast sends an Administrator Message to all connected clients of the server
726 func HandleUserBroadcast(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
729 NewField(fieldData, t.GetField(tranGetMsgs).Data),
730 NewField(fieldChatOptions, []byte{0}),
733 res = append(res, cc.NewReply(t))
737 func byteToInt(bytes []byte) (int, error) {
740 return int(binary.BigEndian.Uint16(bytes)), nil
742 return int(binary.BigEndian.Uint32(bytes)), nil
745 return 0, errors.New("unknown byte length")
748 func HandleGetClientConnInfoText(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
749 clientID, _ := byteToInt(t.GetField(fieldUserID).Data)
751 clientConn := cc.Server.Clients[uint16(clientID)]
752 if clientConn == nil {
753 return res, errors.New("invalid client")
756 // TODO: Implement non-hardcoded values
757 template := `Nickname: %s
762 -------- File Downloads ---------
766 ------- Folder Downloads --------
770 --------- File Uploads ----------
774 -------- Folder Uploads ---------
778 ------- Waiting Downloads -------
784 activeDownloads := clientConn.Transfers[FileDownload]
785 activeDownloadList := "None."
786 for _, dl := range activeDownloads {
787 activeDownloadList += dl.String() + "\n"
790 template = fmt.Sprintf(
793 clientConn.Account.Name,
794 clientConn.Account.Login,
795 clientConn.Connection.RemoteAddr().String(),
798 template = strings.Replace(template, "\n", "\r", -1)
800 res = append(res, cc.NewReply(t,
801 NewField(fieldData, []byte(template)),
802 NewField(fieldUserName, clientConn.UserName),
807 func HandleGetUserNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
808 res = append(res, cc.NewReply(t, cc.Server.connectedUsers()...))
813 func (cc *ClientConn) notifyNewUserHasJoined() (res []Transaction, err error) {
814 // Notify other ccs that a new user has connected
817 tranNotifyChangeUser, nil,
818 NewField(fieldUserName, cc.UserName),
819 NewField(fieldUserID, *cc.ID),
820 NewField(fieldUserIconID, *cc.Icon),
821 NewField(fieldUserFlags, *cc.Flags),
828 func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
830 cc.UserName = t.GetField(fieldUserName).Data
831 *cc.Icon = t.GetField(fieldUserIconID).Data
833 options := t.GetField(fieldOptions).Data
834 optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
836 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags)))
838 // Check refuse private PM option
839 if optBitmap.Bit(refusePM) == 1 {
840 flagBitmap.SetBit(flagBitmap, userFlagRefusePM, 1)
841 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
844 // Check refuse private chat option
845 if optBitmap.Bit(refuseChat) == 1 {
846 flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, 1)
847 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
850 // Check auto response
851 if optBitmap.Bit(autoResponse) == 1 {
852 cc.AutoReply = t.GetField(fieldAutomaticResponse).Data
854 cc.AutoReply = []byte{}
857 _, _ = cc.notifyNewUserHasJoined()
859 res = append(res, cc.NewReply(t))
864 const defaultNewsDateFormat = "Jan02 15:04" // Jun23 20:49
865 // "Mon, 02 Jan 2006 15:04:05 MST"
867 const defaultNewsTemplate = `From %s (%s):
871 __________________________________________________________`
873 // HandleTranOldPostNews updates the flat news
874 // Fields used in this request:
876 func HandleTranOldPostNews(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
877 cc.Server.flatNewsMux.Lock()
878 defer cc.Server.flatNewsMux.Unlock()
880 newsDateTemplate := defaultNewsDateFormat
881 if cc.Server.Config.NewsDateFormat != "" {
882 newsDateTemplate = cc.Server.Config.NewsDateFormat
885 newsTemplate := defaultNewsTemplate
886 if cc.Server.Config.NewsDelimiter != "" {
887 newsTemplate = cc.Server.Config.NewsDelimiter
890 newsPost := fmt.Sprintf(newsTemplate+"\r", cc.UserName, time.Now().Format(newsDateTemplate), t.GetField(fieldData).Data)
891 newsPost = strings.Replace(newsPost, "\n", "\r", -1)
893 // update news in memory
894 cc.Server.FlatNews = append([]byte(newsPost), cc.Server.FlatNews...)
896 // update news on disk
897 if err := ioutil.WriteFile(cc.Server.ConfigDir+"MessageBoard.txt", cc.Server.FlatNews, 0644); err != nil {
901 // Notify all clients of updated news
904 NewField(fieldData, []byte(newsPost)),
907 res = append(res, cc.NewReply(t))
911 func HandleDisconnectUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
912 clientConn := cc.Server.Clients[binary.BigEndian.Uint16(t.GetField(fieldUserID).Data)]
914 if authorize(clientConn.Account.Access, accessCannotBeDiscon) {
915 res = append(res, cc.NewErrReply(t, clientConn.Account.Login+" is not allowed to be disconnected."))
919 if err := clientConn.Connection.Close(); err != nil {
923 res = append(res, cc.NewReply(t))
927 func HandleGetNewsCatNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
928 // Fields used in the request:
929 // 325 News path (Optional)
931 newsPath := t.GetField(fieldNewsPath).Data
932 cc.Server.Logger.Infow("NewsPath: ", "np", string(newsPath))
934 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
935 cats := cc.Server.GetNewsCatByPath(pathStrs)
937 // To store the keys in slice in sorted order
938 keys := make([]string, len(cats))
940 for k := range cats {
946 var fieldData []Field
947 for _, k := range keys {
949 b, _ := cat.MarshalBinary()
950 fieldData = append(fieldData, NewField(
951 fieldNewsCatListData15,
956 res = append(res, cc.NewReply(t, fieldData...))
960 func HandleNewNewsCat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
961 name := string(t.GetField(fieldNewsCatName).Data)
962 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
964 cats := cc.Server.GetNewsCatByPath(pathStrs)
965 cats[name] = NewsCategoryListData15{
968 Articles: map[uint32]*NewsArtData{},
969 SubCats: make(map[string]NewsCategoryListData15),
972 if err := cc.Server.writeThreadedNews(); err != nil {
975 res = append(res, cc.NewReply(t))
979 func HandleNewNewsFldr(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
980 // Fields used in the request:
981 // 322 News category name
983 name := string(t.GetField(fieldFileName).Data)
984 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
986 cc.Server.Logger.Infof("Creating new news folder %s", name)
988 cats := cc.Server.GetNewsCatByPath(pathStrs)
989 cats[name] = NewsCategoryListData15{
992 Articles: map[uint32]*NewsArtData{},
993 SubCats: make(map[string]NewsCategoryListData15),
995 if err := cc.Server.writeThreadedNews(); err != nil {
998 res = append(res, cc.NewReply(t))
1002 // Fields used in the request:
1003 // 325 News path Optional
1006 // 321 News article list data Optional
1007 func HandleGetNewsArtNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1008 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1010 var cat NewsCategoryListData15
1011 cats := cc.Server.ThreadedNews.Categories
1013 for _, path := range pathStrs {
1015 cats = cats[path].SubCats
1018 nald := cat.GetNewsArtListData()
1020 res = append(res, cc.NewReply(t, NewField(fieldNewsArtListData, nald.Payload())))
1024 func HandleGetNewsArtData(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1027 // 326 News article ID
1028 // 327 News article data flavor
1030 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1032 var cat NewsCategoryListData15
1033 cats := cc.Server.ThreadedNews.Categories
1035 for _, path := range pathStrs {
1037 cats = cats[path].SubCats
1039 newsArtID := t.GetField(fieldNewsArtID).Data
1041 convertedArtID := binary.BigEndian.Uint16(newsArtID)
1043 art := cat.Articles[uint32(convertedArtID)]
1045 res = append(res, cc.NewReply(t))
1050 // 328 News article title
1051 // 329 News article poster
1052 // 330 News article date
1053 // 331 Previous article ID
1054 // 332 Next article ID
1055 // 335 Parent article ID
1056 // 336 First child article ID
1057 // 327 News article data flavor "Should be “text/plain”
1058 // 333 News article data Optional (if data flavor is “text/plain”)
1060 res = append(res, cc.NewReply(t,
1061 NewField(fieldNewsArtTitle, []byte(art.Title)),
1062 NewField(fieldNewsArtPoster, []byte(art.Poster)),
1063 NewField(fieldNewsArtDate, art.Date),
1064 NewField(fieldNewsArtPrevArt, art.PrevArt),
1065 NewField(fieldNewsArtNextArt, art.NextArt),
1066 NewField(fieldNewsArtParentArt, art.ParentArt),
1067 NewField(fieldNewsArt1stChildArt, art.FirstChildArt),
1068 NewField(fieldNewsArtDataFlav, []byte("text/plain")),
1069 NewField(fieldNewsArtData, []byte(art.Data)),
1074 func HandleDelNewsItem(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1075 // Access: News Delete Folder (37) or News Delete Category (35)
1077 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1079 // TODO: determine if path is a Folder (Bundle) or Category and check for permission
1081 cc.Server.Logger.Infof("DelNewsItem %v", pathStrs)
1083 cats := cc.Server.ThreadedNews.Categories
1085 delName := pathStrs[len(pathStrs)-1]
1086 if len(pathStrs) > 1 {
1087 for _, path := range pathStrs[0 : len(pathStrs)-1] {
1088 cats = cats[path].SubCats
1092 delete(cats, delName)
1094 err = cc.Server.writeThreadedNews()
1099 // Reply params: none
1100 res = append(res, cc.NewReply(t))
1105 func HandleDelNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1108 // 326 News article ID
1109 // 337 News article – recursive delete Delete child articles (1) or not (0)
1110 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1111 ID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data)
1113 // TODO: Delete recursive
1114 cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1])
1116 catName := pathStrs[len(pathStrs)-1]
1117 cat := cats[catName]
1119 delete(cat.Articles, uint32(ID))
1122 if err := cc.Server.writeThreadedNews(); err != nil {
1126 res = append(res, cc.NewReply(t))
1130 func HandlePostNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1133 // 326 News article ID ID of the parent article?
1134 // 328 News article title
1135 // 334 News article flags
1136 // 327 News article data flavor Currently “text/plain”
1137 // 333 News article data
1139 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1140 cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1])
1142 catName := pathStrs[len(pathStrs)-1]
1143 cat := cats[catName]
1145 newArt := NewsArtData{
1146 Title: string(t.GetField(fieldNewsArtTitle).Data),
1147 Poster: string(cc.UserName),
1148 Date: toHotlineTime(time.Now()),
1149 PrevArt: []byte{0, 0, 0, 0},
1150 NextArt: []byte{0, 0, 0, 0},
1151 ParentArt: append([]byte{0, 0}, t.GetField(fieldNewsArtID).Data...),
1152 FirstChildArt: []byte{0, 0, 0, 0},
1153 DataFlav: []byte("text/plain"),
1154 Data: string(t.GetField(fieldNewsArtData).Data),
1158 for k := range cat.Articles {
1159 keys = append(keys, int(k))
1165 prevID := uint32(keys[len(keys)-1])
1168 binary.BigEndian.PutUint32(newArt.PrevArt, prevID)
1170 // Set next article ID
1171 binary.BigEndian.PutUint32(cat.Articles[prevID].NextArt, nextID)
1174 // Update parent article with first child reply
1175 parentID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data)
1177 parentArt := cat.Articles[uint32(parentID)]
1179 if bytes.Equal(parentArt.FirstChildArt, []byte{0, 0, 0, 0}) {
1180 binary.BigEndian.PutUint32(parentArt.FirstChildArt, nextID)
1184 cat.Articles[nextID] = &newArt
1187 if err := cc.Server.writeThreadedNews(); err != nil {
1191 res = append(res, cc.NewReply(t))
1195 // HandleGetMsgs returns the flat news data
1196 func HandleGetMsgs(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1197 res = append(res, cc.NewReply(t, NewField(fieldData, cc.Server.FlatNews)))
1202 func HandleDownloadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1203 fileName := t.GetField(fieldFileName).Data
1204 filePath := t.GetField(fieldFilePath).Data
1207 err = fp.UnmarshalBinary(filePath)
1212 ffo, err := NewFlattenedFileObject(cc.Server.Config.FileRoot, filePath, fileName)
1217 transactionRef := cc.Server.NewTransactionRef()
1218 data := binary.BigEndian.Uint32(transactionRef)
1220 ft := &FileTransfer{
1223 ReferenceNumber: transactionRef,
1227 cc.Server.FileTransfers[data] = ft
1228 cc.Transfers[FileDownload] = append(cc.Transfers[FileDownload], ft)
1230 res = append(res, cc.NewReply(t,
1231 NewField(fieldRefNum, transactionRef),
1232 NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count
1233 NewField(fieldTransferSize, ffo.TransferSize()),
1234 NewField(fieldFileSize, ffo.FlatFileDataForkHeader.DataSize),
1240 // Download all files from the specified folder and sub-folders
1253 // 00 6c // transfer size
1257 // 00 dc // field Folder item count
1261 // 00 6b // ref number
1264 func HandleDownloadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1265 transactionRef := cc.Server.NewTransactionRef()
1266 data := binary.BigEndian.Uint32(transactionRef)
1268 fileTransfer := &FileTransfer{
1269 FileName: t.GetField(fieldFileName).Data,
1270 FilePath: t.GetField(fieldFilePath).Data,
1271 ReferenceNumber: transactionRef,
1272 Type: FolderDownload,
1274 cc.Server.FileTransfers[data] = fileTransfer
1275 cc.Transfers[FolderDownload] = append(cc.Transfers[FolderDownload], fileTransfer)
1278 err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
1283 fullFilePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFilePath).Data, t.GetField(fieldFileName).Data)
1288 transferSize, err := CalcTotalSize(fullFilePath)
1292 itemCount, err := CalcItemCount(fullFilePath)
1296 res = append(res, cc.NewReply(t,
1297 NewField(fieldRefNum, transactionRef),
1298 NewField(fieldTransferSize, transferSize),
1299 NewField(fieldFolderItemCount, itemCount),
1300 NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count
1305 // Upload all files from the local folder and its subfolders to the specified path on the server
1306 // Fields used in the request
1309 // 108 transfer size Total size of all items in the folder
1310 // 220 Folder item count
1311 // 204 File transfer options "Optional Currently set to 1" (TODO: ??)
1312 func HandleUploadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1313 transactionRef := cc.Server.NewTransactionRef()
1314 data := binary.BigEndian.Uint32(transactionRef)
1316 fileTransfer := &FileTransfer{
1317 FileName: t.GetField(fieldFileName).Data,
1318 FilePath: t.GetField(fieldFilePath).Data,
1319 ReferenceNumber: transactionRef,
1321 FolderItemCount: t.GetField(fieldFolderItemCount).Data,
1322 TransferSize: t.GetField(fieldTransferSize).Data,
1324 cc.Server.FileTransfers[data] = fileTransfer
1326 res = append(res, cc.NewReply(t, NewField(fieldRefNum, transactionRef)))
1330 func HandleUploadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1331 // TODO: add permission handing for upload folders and drop boxes
1332 if !authorize(cc.Account.Access, accessUploadFile) {
1333 res = append(res, cc.NewErrReply(t, "You are not allowed to upload files."))
1337 fileName := t.GetField(fieldFileName).Data
1338 filePath := t.GetField(fieldFilePath).Data
1340 transactionRef := cc.Server.NewTransactionRef()
1341 data := binary.BigEndian.Uint32(transactionRef)
1343 cc.Server.FileTransfers[data] = &FileTransfer{
1346 ReferenceNumber: transactionRef,
1350 res = append(res, cc.NewReply(t, NewField(fieldRefNum, transactionRef)))
1361 func HandleSetClientUserInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1363 if len(t.GetField(fieldUserIconID).Data) == 4 {
1364 icon = t.GetField(fieldUserIconID).Data[2:]
1366 icon = t.GetField(fieldUserIconID).Data
1369 cc.UserName = t.GetField(fieldUserName).Data
1371 // the options field is only passed by the client versions > 1.2.3.
1372 options := t.GetField(fieldOptions).Data
1375 optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
1376 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags)))
1378 flagBitmap.SetBit(flagBitmap, userFlagRefusePM, optBitmap.Bit(refusePM))
1379 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
1381 flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, optBitmap.Bit(refuseChat))
1382 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
1384 // Check auto response
1385 if optBitmap.Bit(autoResponse) == 1 {
1386 cc.AutoReply = t.GetField(fieldAutomaticResponse).Data
1388 cc.AutoReply = []byte{}
1392 // Notify all clients of updated user info
1394 tranNotifyChangeUser,
1395 NewField(fieldUserID, *cc.ID),
1396 NewField(fieldUserIconID, *cc.Icon),
1397 NewField(fieldUserFlags, *cc.Flags),
1398 NewField(fieldUserName, cc.UserName),
1404 // HandleKeepAlive responds to keepalive transactions with an empty reply
1405 // * HL 1.9.2 Client sends keepalive msg every 3 minutes
1406 // * HL 1.2.3 Client doesn't send keepalives
1407 func HandleKeepAlive(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1408 res = append(res, cc.NewReply(t))
1413 func HandleGetFileNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1414 fullPath, err := readPath(
1415 cc.Server.Config.FileRoot,
1416 t.GetField(fieldFilePath).Data,
1423 fileNames, err := getFileNameList(fullPath)
1428 res = append(res, cc.NewReply(t, fileNames...))
1433 // =================================
1434 // Hotline private chat flow
1435 // =================================
1436 // 1. ClientA sends tranInviteNewChat to server with user ID to invite
1437 // 2. Server creates new ChatID
1438 // 3. Server sends tranInviteToChat to invitee
1439 // 4. Server replies to ClientA with new Chat ID
1441 // A dialog box pops up in the invitee client with options to accept or decline the invitation.
1442 // If Accepted is clicked:
1443 // 1. ClientB sends tranJoinChat with fieldChatID
1445 // HandleInviteNewChat invites users to new private chat
1446 func HandleInviteNewChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1448 targetID := t.GetField(fieldUserID).Data
1449 newChatID := cc.Server.NewPrivateChat(cc)
1455 NewField(fieldChatID, newChatID),
1456 NewField(fieldUserName, cc.UserName),
1457 NewField(fieldUserID, *cc.ID),
1463 NewField(fieldChatID, newChatID),
1464 NewField(fieldUserName, cc.UserName),
1465 NewField(fieldUserID, *cc.ID),
1466 NewField(fieldUserIconID, *cc.Icon),
1467 NewField(fieldUserFlags, *cc.Flags),
1474 func HandleInviteToChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1476 targetID := t.GetField(fieldUserID).Data
1477 chatID := t.GetField(fieldChatID).Data
1483 NewField(fieldChatID, chatID),
1484 NewField(fieldUserName, cc.UserName),
1485 NewField(fieldUserID, *cc.ID),
1491 NewField(fieldChatID, chatID),
1492 NewField(fieldUserName, cc.UserName),
1493 NewField(fieldUserID, *cc.ID),
1494 NewField(fieldUserIconID, *cc.Icon),
1495 NewField(fieldUserFlags, *cc.Flags),
1502 func HandleRejectChatInvite(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1503 chatID := t.GetField(fieldChatID).Data
1504 chatInt := binary.BigEndian.Uint32(chatID)
1506 privChat := cc.Server.PrivateChats[chatInt]
1508 resMsg := append(cc.UserName, []byte(" declined invitation to chat")...)
1510 for _, c := range sortedClients(privChat.ClientConn) {
1515 NewField(fieldChatID, chatID),
1516 NewField(fieldData, resMsg),
1524 // HandleJoinChat is sent from a v1.8+ Hotline client when the joins a private chat
1525 // Fields used in the reply:
1526 // * 115 Chat subject
1527 // * 300 User name with info (Optional)
1528 // * 300 (more user names with info)
1529 func HandleJoinChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1530 chatID := t.GetField(fieldChatID).Data
1531 chatInt := binary.BigEndian.Uint32(chatID)
1533 privChat := cc.Server.PrivateChats[chatInt]
1535 // Send tranNotifyChatChangeUser to current members of the chat to inform of new user
1536 for _, c := range sortedClients(privChat.ClientConn) {
1539 tranNotifyChatChangeUser,
1541 NewField(fieldChatID, chatID),
1542 NewField(fieldUserName, cc.UserName),
1543 NewField(fieldUserID, *cc.ID),
1544 NewField(fieldUserIconID, *cc.Icon),
1545 NewField(fieldUserFlags, *cc.Flags),
1550 privChat.ClientConn[cc.uint16ID()] = cc
1552 replyFields := []Field{NewField(fieldChatSubject, []byte(privChat.Subject))}
1553 for _, c := range sortedClients(privChat.ClientConn) {
1558 Name: string(c.UserName),
1561 replyFields = append(replyFields, NewField(fieldUsernameWithInfo, user.Payload()))
1564 res = append(res, cc.NewReply(t, replyFields...))
1568 // HandleLeaveChat is sent from a v1.8+ Hotline client when the user exits a private chat
1569 // Fields used in the request:
1570 // * 114 fieldChatID
1571 // Reply is not expected.
1572 func HandleLeaveChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1573 chatID := t.GetField(fieldChatID).Data
1574 chatInt := binary.BigEndian.Uint32(chatID)
1576 privChat := cc.Server.PrivateChats[chatInt]
1578 delete(privChat.ClientConn, cc.uint16ID())
1580 // Notify members of the private chat that the user has left
1581 for _, c := range sortedClients(privChat.ClientConn) {
1584 tranNotifyChatDeleteUser,
1586 NewField(fieldChatID, chatID),
1587 NewField(fieldUserID, *cc.ID),
1595 // HandleSetChatSubject is sent from a v1.8+ Hotline client when the user sets a private chat subject
1596 // Fields used in the request:
1598 // * 115 Chat subject Chat subject string
1599 // Reply is not expected.
1600 func HandleSetChatSubject(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1601 chatID := t.GetField(fieldChatID).Data
1602 chatInt := binary.BigEndian.Uint32(chatID)
1604 privChat := cc.Server.PrivateChats[chatInt]
1605 privChat.Subject = string(t.GetField(fieldChatSubject).Data)
1607 for _, c := range sortedClients(privChat.ClientConn) {
1610 tranNotifyChatSubject,
1612 NewField(fieldChatID, chatID),
1613 NewField(fieldChatSubject, t.GetField(fieldChatSubject).Data),
1621 // HandleMakeAlias makes a file alias using the specified path.
1622 // Fields used in the request:
1625 // 212 File new path Destination path
1627 // Fields used in the reply:
1629 func HandleMakeAlias(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1630 if !authorize(cc.Account.Access, accessMakeAlias) {
1631 res = append(res, cc.NewErrReply(t, "You are not allowed to make aliases."))
1634 fileName := t.GetField(fieldFileName).Data
1635 filePath := t.GetField(fieldFilePath).Data
1636 fileNewPath := t.GetField(fieldFileNewPath).Data
1638 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
1643 fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, fileNewPath, fileName)
1648 cc.Server.Logger.Debugw("Make alias", "src", fullFilePath, "dst", fullNewFilePath)
1650 if err := FS.Symlink(fullFilePath, fullNewFilePath); err != nil {
1651 res = append(res, cc.NewErrReply(t, "Error creating alias"))
1655 res = append(res, cc.NewReply(t))