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,
259 tranSetClientUserInfo: {
260 Access: accessAlwaysAllow,
261 Name: "tranSetClientUserInfo",
262 Handler: HandleSetClientUserInfo,
265 Access: accessAlwaysAllow, // granular access is in the handler
266 Name: "tranSetFileInfo",
267 Handler: HandleSetFileInfo,
270 Access: accessModifyUser,
271 DenyMsg: "You are not allowed to modify accounts.",
273 Handler: HandleSetUser,
276 Access: accessAlwaysAllow,
277 DenyMsg: "You are not allowed to upload files.",
278 Name: "tranUploadFile",
279 Handler: HandleUploadFile,
282 Access: accessAlwaysAllow, // TODO: what should this be?
283 Name: "tranUploadFldr",
284 Handler: HandleUploadFolder,
287 Access: accessBroadcast,
288 DenyMsg: "You are not allowed to send broadcast messages.",
289 Name: "tranUserBroadcast",
290 Handler: HandleUserBroadcast,
294 func HandleChatSend(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
295 // Truncate long usernames
296 trunc := fmt.Sprintf("%13s", cc.UserName)
297 formattedMsg := fmt.Sprintf("\r%.14s: %s", trunc, t.GetField(fieldData).Data)
299 // By holding the option key, Hotline chat allows users to send /me formatted messages like:
300 // *** Halcyon does stuff
301 // This is indicated by the presence of the optional field fieldChatOptions in the transaction payload
302 if t.GetField(fieldChatOptions).Data != nil {
303 formattedMsg = fmt.Sprintf("\r*** %s %s", cc.UserName, t.GetField(fieldData).Data)
306 if bytes.Equal(t.GetField(fieldData).Data, []byte("/stats")) {
307 formattedMsg = strings.Replace(cc.Server.Stats.String(), "\n", "\r", -1)
310 chatID := t.GetField(fieldChatID).Data
311 // a non-nil chatID indicates the message belongs to a private chat
313 chatInt := binary.BigEndian.Uint32(chatID)
314 privChat := cc.Server.PrivateChats[chatInt]
316 // send the message to all connected clients of the private chat
317 for _, c := range privChat.ClientConn {
318 res = append(res, *NewTransaction(
321 NewField(fieldChatID, chatID),
322 NewField(fieldData, []byte(formattedMsg)),
328 for _, c := range sortedClients(cc.Server.Clients) {
329 // Filter out clients that do not have the read chat permission
330 if authorize(c.Account.Access, accessReadChat) {
331 res = append(res, *NewTransaction(tranChatMsg, c.ID, NewField(fieldData, []byte(formattedMsg))))
338 // HandleSendInstantMsg sends instant message to the user on the current server.
339 // Fields used in the request:
342 // One of the following values:
343 // - User message (myOpt_UserMessage = 1)
344 // - Refuse message (myOpt_RefuseMessage = 2)
345 // - Refuse chat (myOpt_RefuseChat = 3)
346 // - Automatic response (myOpt_AutomaticResponse = 4)"
348 // 214 Quoting message Optional
350 //Fields used in the reply:
352 func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
353 msg := t.GetField(fieldData)
354 ID := t.GetField(fieldUserID)
355 // TODO: Implement reply quoting
356 //options := transaction.GetField(hotline.fieldOptions)
362 NewField(fieldData, msg.Data),
363 NewField(fieldUserName, cc.UserName),
364 NewField(fieldUserID, *cc.ID),
365 NewField(fieldOptions, []byte{0, 1}),
368 id, _ := byteToInt(ID.Data)
370 //keys := make([]uint16, 0, len(cc.Server.Clients))
371 //for k := range cc.Server.Clients {
372 // keys = append(keys, k)
375 otherClient := cc.Server.Clients[uint16(id)]
376 if otherClient == nil {
377 return res, errors.New("ohno")
380 // Respond with auto reply if other client has it enabled
381 if len(*otherClient.AutoReply) > 0 {
386 NewField(fieldData, *otherClient.AutoReply),
387 NewField(fieldUserName, otherClient.UserName),
388 NewField(fieldUserID, *otherClient.ID),
389 NewField(fieldOptions, []byte{0, 1}),
394 res = append(res, cc.NewReply(t))
399 func HandleGetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
400 fileName := t.GetField(fieldFileName).Data
401 filePath := t.GetField(fieldFilePath).Data
403 ffo, err := NewFlattenedFileObject(cc.Server.Config.FileRoot, filePath, fileName)
408 res = append(res, cc.NewReply(t,
409 NewField(fieldFileName, fileName),
410 NewField(fieldFileTypeString, ffo.FlatFileInformationFork.TypeSignature),
411 NewField(fieldFileCreatorString, ffo.FlatFileInformationFork.CreatorSignature),
412 NewField(fieldFileComment, ffo.FlatFileInformationFork.Comment),
413 NewField(fieldFileType, ffo.FlatFileInformationFork.TypeSignature),
414 NewField(fieldFileCreateDate, ffo.FlatFileInformationFork.CreateDate),
415 NewField(fieldFileModifyDate, ffo.FlatFileInformationFork.ModifyDate),
416 NewField(fieldFileSize, ffo.FlatFileDataForkHeader.DataSize),
421 // HandleSetFileInfo updates a file or folder name and/or comment from the Get Info window
422 // TODO: Implement support for comments
423 // Fields used in the request:
425 // * 202 File path Optional
426 // * 211 File new name Optional
427 // * 210 File comment Optional
428 // Fields used in the reply: None
429 func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
430 fileName := t.GetField(fieldFileName).Data
431 filePath := t.GetField(fieldFilePath).Data
433 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
438 fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, t.GetField(fieldFileNewName).Data)
443 //fileComment := t.GetField(fieldFileComment).Data
444 fileNewName := t.GetField(fieldFileNewName).Data
446 if fileNewName != nil {
447 fi, err := FS.Stat(fullFilePath)
451 switch mode := fi.Mode(); {
453 if !authorize(cc.Account.Access, accessRenameFolder) {
454 res = append(res, cc.NewErrReply(t, "You are not allowed to rename folders."))
457 case mode.IsRegular():
458 if !authorize(cc.Account.Access, accessRenameFile) {
459 res = append(res, cc.NewErrReply(t, "You are not allowed to rename files."))
464 err = os.Rename(fullFilePath, fullNewFilePath)
465 if os.IsNotExist(err) {
466 res = append(res, cc.NewErrReply(t, "Cannot rename file "+string(fileName)+" because it does not exist or cannot be found."))
471 res = append(res, cc.NewReply(t))
475 // HandleDeleteFile deletes a file or folder
476 // Fields used in the request:
479 // Fields used in the reply: none
480 func HandleDeleteFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
481 fileName := t.GetField(fieldFileName).Data
482 filePath := t.GetField(fieldFilePath).Data
484 fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
489 cc.Server.Logger.Debugw("Delete file", "src", fullFilePath)
491 fi, err := os.Stat(fullFilePath)
493 res = append(res, cc.NewErrReply(t, "Cannot delete file "+string(fileName)+" because it does not exist or cannot be found."))
496 switch mode := fi.Mode(); {
498 if !authorize(cc.Account.Access, accessDeleteFolder) {
499 res = append(res, cc.NewErrReply(t, "You are not allowed to delete folders."))
502 case mode.IsRegular():
503 if !authorize(cc.Account.Access, accessDeleteFile) {
504 res = append(res, cc.NewErrReply(t, "You are not allowed to delete files."))
509 if err := os.RemoveAll(fullFilePath); err != nil {
513 res = append(res, cc.NewReply(t))
517 // HandleMoveFile moves files or folders. Note: seemingly not documented
518 func HandleMoveFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
519 fileName := string(t.GetField(fieldFileName).Data)
520 filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data)
521 fileNewPath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFileNewPath).Data)
523 cc.Server.Logger.Debugw("Move file", "src", filePath+"/"+fileName, "dst", fileNewPath+"/"+fileName)
525 path := filePath + "/" + fileName
526 fi, err := os.Stat(path)
530 switch mode := fi.Mode(); {
532 if !authorize(cc.Account.Access, accessMoveFolder) {
533 res = append(res, cc.NewErrReply(t, "You are not allowed to move folders."))
536 case mode.IsRegular():
537 if !authorize(cc.Account.Access, accessMoveFile) {
538 res = append(res, cc.NewErrReply(t, "You are not allowed to move files."))
543 err = os.Rename(filePath+"/"+fileName, fileNewPath+"/"+fileName)
544 if os.IsNotExist(err) {
545 res = append(res, cc.NewErrReply(t, "Cannot delete file "+fileName+" because it does not exist or cannot be found."))
549 return []Transaction{}, err
551 // TODO: handle other possible errors; e.g. file delete fails due to file permission issue
553 res = append(res, cc.NewReply(t))
557 func HandleNewFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
558 newFolderPath := cc.Server.Config.FileRoot
559 folderName := string(t.GetField(fieldFileName).Data)
561 folderName = path.Join("/", folderName)
563 // fieldFilePath is only present for nested paths
564 if t.GetField(fieldFilePath).Data != nil {
566 err := newFp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
570 newFolderPath += newFp.String()
572 newFolderPath = path.Join(newFolderPath, folderName)
574 // TODO: check path and folder name lengths
576 if _, err := FS.Stat(newFolderPath); !os.IsNotExist(err) {
577 msg := fmt.Sprintf("Cannot create folder \"%s\" because there is already a file or folder with that name.", folderName)
578 return []Transaction{cc.NewErrReply(t, msg)}, nil
581 // TODO: check for disallowed characters to maintain compatibility for original client
583 if err := FS.Mkdir(newFolderPath, 0777); err != nil {
584 msg := fmt.Sprintf("Cannot create folder \"%s\" because an error occurred.", folderName)
585 return []Transaction{cc.NewErrReply(t, msg)}, nil
588 res = append(res, cc.NewReply(t))
592 func HandleSetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
593 login := DecodeUserString(t.GetField(fieldUserLogin).Data)
594 userName := string(t.GetField(fieldUserName).Data)
596 newAccessLvl := t.GetField(fieldUserAccess).Data
598 account := cc.Server.Accounts[login]
599 account.Access = &newAccessLvl
600 account.Name = userName
602 // If the password field is cleared in the Hotline edit user UI, the SetUser transaction does
603 // not include fieldUserPassword
604 if t.GetField(fieldUserPassword).Data == nil {
605 account.Password = hashAndSalt([]byte(""))
607 if len(t.GetField(fieldUserPassword).Data) > 1 {
608 account.Password = hashAndSalt(t.GetField(fieldUserPassword).Data)
611 file := cc.Server.ConfigDir + "Users/" + login + ".yaml"
612 out, err := yaml.Marshal(&account)
616 if err := ioutil.WriteFile(file, out, 0666); err != nil {
620 // Notify connected clients logged in as the user of the new access level
621 for _, c := range cc.Server.Clients {
622 if c.Account.Login == login {
623 // Note: comment out these two lines to test server-side deny messages
624 newT := NewTransaction(tranUserAccess, c.ID, NewField(fieldUserAccess, newAccessLvl))
625 res = append(res, *newT)
627 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*c.Flags)))
628 if authorize(c.Account.Access, accessDisconUser) {
629 flagBitmap.SetBit(flagBitmap, userFlagAdmin, 1)
631 flagBitmap.SetBit(flagBitmap, userFlagAdmin, 0)
633 binary.BigEndian.PutUint16(*c.Flags, uint16(flagBitmap.Int64()))
635 c.Account.Access = account.Access
638 tranNotifyChangeUser,
639 NewField(fieldUserID, *c.ID),
640 NewField(fieldUserFlags, *c.Flags),
641 NewField(fieldUserName, c.UserName),
642 NewField(fieldUserIconID, *c.Icon),
647 // TODO: If we have just promoted a connected user to admin, notify
648 // connected clients to turn the user red
650 res = append(res, cc.NewReply(t))
654 func HandleGetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
655 userLogin := string(t.GetField(fieldUserLogin).Data)
656 account := cc.Server.Accounts[userLogin]
658 errorT := cc.NewErrReply(t, "Account does not exist.")
659 res = append(res, errorT)
663 res = append(res, cc.NewReply(t,
664 NewField(fieldUserName, []byte(account.Name)),
665 NewField(fieldUserLogin, negateString(t.GetField(fieldUserLogin).Data)),
666 NewField(fieldUserPassword, []byte(account.Password)),
667 NewField(fieldUserAccess, *account.Access),
672 func HandleListUsers(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
673 var userFields []Field
674 // TODO: make order deterministic
675 for _, acc := range cc.Server.Accounts {
676 userField := acc.MarshalBinary()
677 userFields = append(userFields, NewField(fieldData, userField))
680 res = append(res, cc.NewReply(t, userFields...))
684 // HandleNewUser creates a new user account
685 func HandleNewUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
686 login := DecodeUserString(t.GetField(fieldUserLogin).Data)
688 // If the account already exists, reply with an error
689 // TODO: make order deterministic
690 if _, ok := cc.Server.Accounts[login]; ok {
691 res = append(res, cc.NewErrReply(t, "Cannot create account "+login+" because there is already an account with that login."))
695 if err := cc.Server.NewUser(
697 string(t.GetField(fieldUserName).Data),
698 string(t.GetField(fieldUserPassword).Data),
699 t.GetField(fieldUserAccess).Data,
701 return []Transaction{}, err
704 res = append(res, cc.NewReply(t))
708 func HandleDeleteUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
709 // TODO: Handle case where account doesn't exist; e.g. delete race condition
710 login := DecodeUserString(t.GetField(fieldUserLogin).Data)
712 if err := cc.Server.DeleteUser(login); err != nil {
716 res = append(res, cc.NewReply(t))
720 // HandleUserBroadcast sends an Administrator Message to all connected clients of the server
721 func HandleUserBroadcast(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
724 NewField(fieldData, t.GetField(tranGetMsgs).Data),
725 NewField(fieldChatOptions, []byte{0}),
728 res = append(res, cc.NewReply(t))
732 func byteToInt(bytes []byte) (int, error) {
735 return int(binary.BigEndian.Uint16(bytes)), nil
737 return int(binary.BigEndian.Uint32(bytes)), nil
740 return 0, errors.New("unknown byte length")
743 func HandleGetClientConnInfoText(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
744 clientID, _ := byteToInt(t.GetField(fieldUserID).Data)
746 clientConn := cc.Server.Clients[uint16(clientID)]
747 if clientConn == nil {
748 return res, errors.New("invalid client")
751 // TODO: Implement non-hardcoded values
752 template := `Nickname: %s
757 -------- File Downloads ---------
761 ------- Folder Downloads --------
765 --------- File Uploads ----------
769 -------- Folder Uploads ---------
773 ------- Waiting Downloads -------
779 activeDownloads := clientConn.Transfers[FileDownload]
780 activeDownloadList := "None."
781 for _, dl := range activeDownloads {
782 activeDownloadList += dl.String() + "\n"
785 template = fmt.Sprintf(
788 clientConn.Account.Name,
789 clientConn.Account.Login,
790 clientConn.Connection.RemoteAddr().String(),
793 template = strings.Replace(template, "\n", "\r", -1)
795 res = append(res, cc.NewReply(t,
796 NewField(fieldData, []byte(template)),
797 NewField(fieldUserName, clientConn.UserName),
802 func HandleGetUserNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
803 res = append(res, cc.NewReply(t, cc.Server.connectedUsers()...))
808 func (cc *ClientConn) notifyNewUserHasJoined() (res []Transaction, err error) {
809 // Notify other ccs that a new user has connected
812 tranNotifyChangeUser, nil,
813 NewField(fieldUserName, cc.UserName),
814 NewField(fieldUserID, *cc.ID),
815 NewField(fieldUserIconID, *cc.Icon),
816 NewField(fieldUserFlags, *cc.Flags),
823 func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
825 cc.UserName = t.GetField(fieldUserName).Data
826 *cc.Icon = t.GetField(fieldUserIconID).Data
828 options := t.GetField(fieldOptions).Data
829 optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
831 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags)))
833 // Check refuse private PM option
834 if optBitmap.Bit(refusePM) == 1 {
835 flagBitmap.SetBit(flagBitmap, userFlagRefusePM, 1)
836 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
839 // Check refuse private chat option
840 if optBitmap.Bit(refuseChat) == 1 {
841 flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, 1)
842 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
845 // Check auto response
846 if optBitmap.Bit(autoResponse) == 1 {
847 *cc.AutoReply = t.GetField(fieldAutomaticResponse).Data
849 *cc.AutoReply = []byte{}
852 _, _ = cc.notifyNewUserHasJoined()
854 res = append(res, cc.NewReply(t))
859 const defaultNewsDateFormat = "Jan02 15:04" // Jun23 20:49
860 // "Mon, 02 Jan 2006 15:04:05 MST"
862 const defaultNewsTemplate = `From %s (%s):
866 __________________________________________________________`
868 // HandleTranOldPostNews updates the flat news
869 // Fields used in this request:
871 func HandleTranOldPostNews(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
872 cc.Server.flatNewsMux.Lock()
873 defer cc.Server.flatNewsMux.Unlock()
875 newsDateTemplate := defaultNewsDateFormat
876 if cc.Server.Config.NewsDateFormat != "" {
877 newsDateTemplate = cc.Server.Config.NewsDateFormat
880 newsTemplate := defaultNewsTemplate
881 if cc.Server.Config.NewsDelimiter != "" {
882 newsTemplate = cc.Server.Config.NewsDelimiter
885 newsPost := fmt.Sprintf(newsTemplate+"\r", cc.UserName, time.Now().Format(newsDateTemplate), t.GetField(fieldData).Data)
886 newsPost = strings.Replace(newsPost, "\n", "\r", -1)
888 // update news in memory
889 cc.Server.FlatNews = append([]byte(newsPost), cc.Server.FlatNews...)
891 // update news on disk
892 if err := ioutil.WriteFile(cc.Server.ConfigDir+"MessageBoard.txt", cc.Server.FlatNews, 0644); err != nil {
896 // Notify all clients of updated news
899 NewField(fieldData, []byte(newsPost)),
902 res = append(res, cc.NewReply(t))
906 func HandleDisconnectUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
907 clientConn := cc.Server.Clients[binary.BigEndian.Uint16(t.GetField(fieldUserID).Data)]
909 if authorize(clientConn.Account.Access, accessCannotBeDiscon) {
910 res = append(res, cc.NewErrReply(t, clientConn.Account.Login+" is not allowed to be disconnected."))
914 if err := clientConn.Connection.Close(); err != nil {
918 res = append(res, cc.NewReply(t))
922 func HandleGetNewsCatNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
923 // Fields used in the request:
924 // 325 News path (Optional)
926 newsPath := t.GetField(fieldNewsPath).Data
927 cc.Server.Logger.Infow("NewsPath: ", "np", string(newsPath))
929 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
930 cats := cc.Server.GetNewsCatByPath(pathStrs)
932 // To store the keys in slice in sorted order
933 keys := make([]string, len(cats))
935 for k := range cats {
941 var fieldData []Field
942 for _, k := range keys {
944 b, _ := cat.MarshalBinary()
945 fieldData = append(fieldData, NewField(
946 fieldNewsCatListData15,
951 res = append(res, cc.NewReply(t, fieldData...))
955 func HandleNewNewsCat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
956 name := string(t.GetField(fieldNewsCatName).Data)
957 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
959 cats := cc.Server.GetNewsCatByPath(pathStrs)
960 cats[name] = NewsCategoryListData15{
963 Articles: map[uint32]*NewsArtData{},
964 SubCats: make(map[string]NewsCategoryListData15),
967 if err := cc.Server.writeThreadedNews(); err != nil {
970 res = append(res, cc.NewReply(t))
974 func HandleNewNewsFldr(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
975 // Fields used in the request:
976 // 322 News category name
978 name := string(t.GetField(fieldFileName).Data)
979 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
981 cc.Server.Logger.Infof("Creating new news folder %s", name)
983 cats := cc.Server.GetNewsCatByPath(pathStrs)
984 cats[name] = NewsCategoryListData15{
987 Articles: map[uint32]*NewsArtData{},
988 SubCats: make(map[string]NewsCategoryListData15),
990 if err := cc.Server.writeThreadedNews(); err != nil {
993 res = append(res, cc.NewReply(t))
997 // Fields used in the request:
998 // 325 News path Optional
1001 // 321 News article list data Optional
1002 func HandleGetNewsArtNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1003 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1005 var cat NewsCategoryListData15
1006 cats := cc.Server.ThreadedNews.Categories
1008 for _, path := range pathStrs {
1010 cats = cats[path].SubCats
1013 nald := cat.GetNewsArtListData()
1015 res = append(res, cc.NewReply(t, NewField(fieldNewsArtListData, nald.Payload())))
1019 func HandleGetNewsArtData(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1022 // 326 News article ID
1023 // 327 News article data flavor
1025 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1027 var cat NewsCategoryListData15
1028 cats := cc.Server.ThreadedNews.Categories
1030 for _, path := range pathStrs {
1032 cats = cats[path].SubCats
1034 newsArtID := t.GetField(fieldNewsArtID).Data
1036 convertedArtID := binary.BigEndian.Uint16(newsArtID)
1038 art := cat.Articles[uint32(convertedArtID)]
1040 res = append(res, cc.NewReply(t))
1045 // 328 News article title
1046 // 329 News article poster
1047 // 330 News article date
1048 // 331 Previous article ID
1049 // 332 Next article ID
1050 // 335 Parent article ID
1051 // 336 First child article ID
1052 // 327 News article data flavor "Should be “text/plain”
1053 // 333 News article data Optional (if data flavor is “text/plain”)
1055 res = append(res, cc.NewReply(t,
1056 NewField(fieldNewsArtTitle, []byte(art.Title)),
1057 NewField(fieldNewsArtPoster, []byte(art.Poster)),
1058 NewField(fieldNewsArtDate, art.Date),
1059 NewField(fieldNewsArtPrevArt, art.PrevArt),
1060 NewField(fieldNewsArtNextArt, art.NextArt),
1061 NewField(fieldNewsArtParentArt, art.ParentArt),
1062 NewField(fieldNewsArt1stChildArt, art.FirstChildArt),
1063 NewField(fieldNewsArtDataFlav, []byte("text/plain")),
1064 NewField(fieldNewsArtData, []byte(art.Data)),
1069 func HandleDelNewsItem(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1070 // Access: News Delete Folder (37) or News Delete Category (35)
1072 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1074 // TODO: determine if path is a Folder (Bundle) or Category and check for permission
1076 cc.Server.Logger.Infof("DelNewsItem %v", pathStrs)
1078 cats := cc.Server.ThreadedNews.Categories
1080 delName := pathStrs[len(pathStrs)-1]
1081 if len(pathStrs) > 1 {
1082 for _, path := range pathStrs[0 : len(pathStrs)-1] {
1083 cats = cats[path].SubCats
1087 delete(cats, delName)
1089 err = cc.Server.writeThreadedNews()
1094 // Reply params: none
1095 res = append(res, cc.NewReply(t))
1100 func HandleDelNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1103 // 326 News article ID
1104 // 337 News article – recursive delete Delete child articles (1) or not (0)
1105 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1106 ID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data)
1108 // TODO: Delete recursive
1109 cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1])
1111 catName := pathStrs[len(pathStrs)-1]
1112 cat := cats[catName]
1114 delete(cat.Articles, uint32(ID))
1117 if err := cc.Server.writeThreadedNews(); err != nil {
1121 res = append(res, cc.NewReply(t))
1125 func HandlePostNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1128 // 326 News article ID ID of the parent article?
1129 // 328 News article title
1130 // 334 News article flags
1131 // 327 News article data flavor Currently “text/plain”
1132 // 333 News article data
1134 pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data)
1135 cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1])
1137 catName := pathStrs[len(pathStrs)-1]
1138 cat := cats[catName]
1140 newArt := NewsArtData{
1141 Title: string(t.GetField(fieldNewsArtTitle).Data),
1142 Poster: string(cc.UserName),
1144 PrevArt: []byte{0, 0, 0, 0},
1145 NextArt: []byte{0, 0, 0, 0},
1146 ParentArt: append([]byte{0, 0}, t.GetField(fieldNewsArtID).Data...),
1147 FirstChildArt: []byte{0, 0, 0, 0},
1148 DataFlav: []byte("text/plain"),
1149 Data: string(t.GetField(fieldNewsArtData).Data),
1153 for k := range cat.Articles {
1154 keys = append(keys, int(k))
1160 prevID := uint32(keys[len(keys)-1])
1163 binary.BigEndian.PutUint32(newArt.PrevArt, prevID)
1165 // Set next article ID
1166 binary.BigEndian.PutUint32(cat.Articles[prevID].NextArt, nextID)
1169 // Update parent article with first child reply
1170 parentID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data)
1172 parentArt := cat.Articles[uint32(parentID)]
1174 if bytes.Equal(parentArt.FirstChildArt, []byte{0, 0, 0, 0}) {
1175 binary.BigEndian.PutUint32(parentArt.FirstChildArt, nextID)
1179 cat.Articles[nextID] = &newArt
1182 if err := cc.Server.writeThreadedNews(); err != nil {
1186 res = append(res, cc.NewReply(t))
1190 // HandleGetMsgs returns the flat news data
1191 func HandleGetMsgs(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1192 res = append(res, cc.NewReply(t, NewField(fieldData, cc.Server.FlatNews)))
1197 func HandleDownloadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1198 fileName := t.GetField(fieldFileName).Data
1199 filePath := t.GetField(fieldFilePath).Data
1202 err = fp.UnmarshalBinary(filePath)
1207 ffo, err := NewFlattenedFileObject(cc.Server.Config.FileRoot, filePath, fileName)
1212 transactionRef := cc.Server.NewTransactionRef()
1213 data := binary.BigEndian.Uint32(transactionRef)
1215 ft := &FileTransfer{
1218 ReferenceNumber: transactionRef,
1222 cc.Server.FileTransfers[data] = ft
1223 cc.Transfers[FileDownload] = append(cc.Transfers[FileDownload], ft)
1225 res = append(res, cc.NewReply(t,
1226 NewField(fieldRefNum, transactionRef),
1227 NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count
1228 NewField(fieldTransferSize, ffo.TransferSize()),
1229 NewField(fieldFileSize, ffo.FlatFileDataForkHeader.DataSize),
1235 // Download all files from the specified folder and sub-folders
1248 // 00 6c // transfer size
1252 // 00 dc // field Folder item count
1256 // 00 6b // ref number
1259 func HandleDownloadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1260 transactionRef := cc.Server.NewTransactionRef()
1261 data := binary.BigEndian.Uint32(transactionRef)
1263 fileTransfer := &FileTransfer{
1264 FileName: t.GetField(fieldFileName).Data,
1265 FilePath: t.GetField(fieldFilePath).Data,
1266 ReferenceNumber: transactionRef,
1267 Type: FolderDownload,
1269 cc.Server.FileTransfers[data] = fileTransfer
1270 cc.Transfers[FolderDownload] = append(cc.Transfers[FolderDownload], fileTransfer)
1273 err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
1278 fullFilePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFilePath).Data, t.GetField(fieldFileName).Data)
1280 transferSize, err := CalcTotalSize(fullFilePath)
1284 itemCount, err := CalcItemCount(fullFilePath)
1288 res = append(res, cc.NewReply(t,
1289 NewField(fieldRefNum, transactionRef),
1290 NewField(fieldTransferSize, transferSize),
1291 NewField(fieldFolderItemCount, itemCount),
1292 NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count
1297 // Upload all files from the local folder and its subfolders to the specified path on the server
1298 // Fields used in the request
1301 // 108 transfer size Total size of all items in the folder
1302 // 220 Folder item count
1303 // 204 File transfer options "Optional Currently set to 1" (TODO: ??)
1304 func HandleUploadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1305 transactionRef := cc.Server.NewTransactionRef()
1306 data := binary.BigEndian.Uint32(transactionRef)
1308 fileTransfer := &FileTransfer{
1309 FileName: t.GetField(fieldFileName).Data,
1310 FilePath: t.GetField(fieldFilePath).Data,
1311 ReferenceNumber: transactionRef,
1313 FolderItemCount: t.GetField(fieldFolderItemCount).Data,
1314 TransferSize: t.GetField(fieldTransferSize).Data,
1316 cc.Server.FileTransfers[data] = fileTransfer
1318 res = append(res, cc.NewReply(t, NewField(fieldRefNum, transactionRef)))
1322 func HandleUploadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1323 // TODO: add permission handing for upload folders and drop boxes
1324 if !authorize(cc.Account.Access, accessUploadFile) {
1325 res = append(res, cc.NewErrReply(t, "You are not allowed to upload files."))
1329 fileName := t.GetField(fieldFileName).Data
1330 filePath := t.GetField(fieldFilePath).Data
1332 transactionRef := cc.Server.NewTransactionRef()
1333 data := binary.BigEndian.Uint32(transactionRef)
1335 cc.Server.FileTransfers[data] = &FileTransfer{
1338 ReferenceNumber: transactionRef,
1342 res = append(res, cc.NewReply(t, NewField(fieldRefNum, transactionRef)))
1353 func HandleSetClientUserInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1355 if len(t.GetField(fieldUserIconID).Data) == 4 {
1356 icon = t.GetField(fieldUserIconID).Data[2:]
1358 icon = t.GetField(fieldUserIconID).Data
1361 cc.UserName = t.GetField(fieldUserName).Data
1363 // the options field is only passed by the client versions > 1.2.3.
1364 options := t.GetField(fieldOptions).Data
1367 optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options)))
1368 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags)))
1370 // Check refuse private PM option
1371 if optBitmap.Bit(refusePM) == 1 {
1372 flagBitmap.SetBit(flagBitmap, userFlagRefusePM, 1)
1373 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
1376 // Check refuse private chat option
1377 if optBitmap.Bit(refuseChat) == 1 {
1378 flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, 1)
1379 binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
1382 // Check auto response
1383 if optBitmap.Bit(autoResponse) == 1 {
1384 *cc.AutoReply = t.GetField(fieldAutomaticResponse).Data
1386 *cc.AutoReply = []byte{}
1390 // Notify all clients of updated user info
1392 tranNotifyChangeUser,
1393 NewField(fieldUserID, *cc.ID),
1394 NewField(fieldUserIconID, *cc.Icon),
1395 NewField(fieldUserFlags, *cc.Flags),
1396 NewField(fieldUserName, cc.UserName),
1402 // HandleKeepAlive response to keepalive transactions with an empty reply
1403 // HL 1.9.2 Client sends keepalive msg every 3 minutes
1404 // HL 1.2.3 Client doesn't send keepalives
1405 func HandleKeepAlive(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1406 res = append(res, cc.NewReply(t))
1411 func HandleGetFileNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1412 fullPath, err := readPath(
1413 cc.Server.Config.FileRoot,
1414 t.GetField(fieldFilePath).Data,
1421 fileNames, err := getFileNameList(fullPath)
1426 res = append(res, cc.NewReply(t, fileNames...))
1431 // =================================
1432 // Hotline private chat flow
1433 // =================================
1434 // 1. ClientA sends tranInviteNewChat to server with user ID to invite
1435 // 2. Server creates new ChatID
1436 // 3. Server sends tranInviteToChat to invitee
1437 // 4. Server replies to ClientA with new Chat ID
1439 // A dialog box pops up in the invitee client with options to accept or decline the invitation.
1440 // If Accepted is clicked:
1441 // 1. ClientB sends tranJoinChat with fieldChatID
1443 // HandleInviteNewChat invites users to new private chat
1444 func HandleInviteNewChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1446 targetID := t.GetField(fieldUserID).Data
1447 newChatID := cc.Server.NewPrivateChat(cc)
1453 NewField(fieldChatID, newChatID),
1454 NewField(fieldUserName, cc.UserName),
1455 NewField(fieldUserID, *cc.ID),
1461 NewField(fieldChatID, newChatID),
1462 NewField(fieldUserName, cc.UserName),
1463 NewField(fieldUserID, *cc.ID),
1464 NewField(fieldUserIconID, *cc.Icon),
1465 NewField(fieldUserFlags, *cc.Flags),
1472 func HandleInviteToChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1474 targetID := t.GetField(fieldUserID).Data
1475 chatID := t.GetField(fieldChatID).Data
1481 NewField(fieldChatID, chatID),
1482 NewField(fieldUserName, cc.UserName),
1483 NewField(fieldUserID, *cc.ID),
1489 NewField(fieldChatID, chatID),
1490 NewField(fieldUserName, cc.UserName),
1491 NewField(fieldUserID, *cc.ID),
1492 NewField(fieldUserIconID, *cc.Icon),
1493 NewField(fieldUserFlags, *cc.Flags),
1500 func HandleRejectChatInvite(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1501 chatID := t.GetField(fieldChatID).Data
1502 chatInt := binary.BigEndian.Uint32(chatID)
1504 privChat := cc.Server.PrivateChats[chatInt]
1506 resMsg := append(cc.UserName, []byte(" declined invitation to chat")...)
1508 for _, c := range sortedClients(privChat.ClientConn) {
1513 NewField(fieldChatID, chatID),
1514 NewField(fieldData, resMsg),
1522 // HandleJoinChat is sent from a v1.8+ Hotline client when the joins a private chat
1523 // Fields used in the reply:
1524 // * 115 Chat subject
1525 // * 300 User name with info (Optional)
1526 // * 300 (more user names with info)
1527 func HandleJoinChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1528 chatID := t.GetField(fieldChatID).Data
1529 chatInt := binary.BigEndian.Uint32(chatID)
1531 privChat := cc.Server.PrivateChats[chatInt]
1533 // Send tranNotifyChatChangeUser to current members of the chat to inform of new user
1534 for _, c := range sortedClients(privChat.ClientConn) {
1537 tranNotifyChatChangeUser,
1539 NewField(fieldChatID, chatID),
1540 NewField(fieldUserName, cc.UserName),
1541 NewField(fieldUserID, *cc.ID),
1542 NewField(fieldUserIconID, *cc.Icon),
1543 NewField(fieldUserFlags, *cc.Flags),
1548 privChat.ClientConn[cc.uint16ID()] = cc
1550 replyFields := []Field{NewField(fieldChatSubject, []byte(privChat.Subject))}
1551 for _, c := range sortedClients(privChat.ClientConn) {
1556 Name: string(c.UserName),
1559 replyFields = append(replyFields, NewField(fieldUsernameWithInfo, user.Payload()))
1562 res = append(res, cc.NewReply(t, replyFields...))
1566 // HandleLeaveChat is sent from a v1.8+ Hotline client when the user exits a private chat
1567 // Fields used in the request:
1568 // * 114 fieldChatID
1569 // Reply is not expected.
1570 func HandleLeaveChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1571 chatID := t.GetField(fieldChatID).Data
1572 chatInt := binary.BigEndian.Uint32(chatID)
1574 privChat := cc.Server.PrivateChats[chatInt]
1576 delete(privChat.ClientConn, cc.uint16ID())
1578 // Notify members of the private chat that the user has left
1579 for _, c := range sortedClients(privChat.ClientConn) {
1582 tranNotifyChatDeleteUser,
1584 NewField(fieldChatID, chatID),
1585 NewField(fieldUserID, *cc.ID),
1593 // HandleSetChatSubject is sent from a v1.8+ Hotline client when the user sets a private chat subject
1594 // Fields used in the request:
1596 // * 115 Chat subject Chat subject string
1597 // Reply is not expected.
1598 func HandleSetChatSubject(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
1599 chatID := t.GetField(fieldChatID).Data
1600 chatInt := binary.BigEndian.Uint32(chatID)
1602 privChat := cc.Server.PrivateChats[chatInt]
1603 privChat.Subject = string(t.GetField(fieldChatSubject).Data)
1605 for _, c := range sortedClients(privChat.ClientConn) {
1608 tranNotifyChatSubject,
1610 NewField(fieldChatID, chatID),
1611 NewField(fieldChatSubject, t.GetField(fieldChatSubject).Data),