]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import ( | |
4 | "bytes" | |
5 | "encoding/binary" | |
6 | "errors" | |
7 | "fmt" | |
6988a057 JH |
8 | "gopkg.in/yaml.v2" |
9 | "io/ioutil" | |
10 | "math/big" | |
11 | "os" | |
12 | "sort" | |
13 | "strings" | |
14 | "time" | |
15 | ) | |
16 | ||
17 | type TransactionType struct { | |
18 | Access int // Specifies access privilege required to perform the transaction | |
19 | DenyMsg string // The error reply message when user does not have access | |
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 | |
23 | } | |
24 | ||
25 | var TransactionHandlers = map[uint16]TransactionType{ | |
26 | // Server initiated | |
27 | tranChatMsg: { | |
28 | Name: "tranChatMsg", | |
29 | }, | |
30 | // Server initiated | |
31 | tranNotifyChangeUser: { | |
32 | Name: "tranNotifyChangeUser", | |
33 | }, | |
34 | tranError: { | |
35 | Name: "tranError", | |
36 | }, | |
37 | tranShowAgreement: { | |
38 | Name: "tranShowAgreement", | |
39 | }, | |
40 | tranUserAccess: { | |
41 | Name: "tranUserAccess", | |
42 | }, | |
5454019c JH |
43 | tranNotifyDeleteUser: { |
44 | Name: "tranNotifyDeleteUser", | |
45 | }, | |
6988a057 JH |
46 | tranAgreed: { |
47 | Access: accessAlwaysAllow, | |
48 | Name: "tranAgreed", | |
49 | Handler: HandleTranAgreed, | |
50 | }, | |
51 | tranChatSend: { | |
52 | Access: accessSendChat, | |
53 | DenyMsg: "You are not allowed to participate in chat.", | |
54 | Handler: HandleChatSend, | |
55 | Name: "tranChatSend", | |
56 | RequiredFields: []requiredField{ | |
57 | { | |
58 | ID: fieldData, | |
59 | minLen: 0, | |
60 | }, | |
61 | }, | |
62 | }, | |
63 | tranDelNewsArt: { | |
64 | Access: accessNewsDeleteArt, | |
65 | DenyMsg: "You are not allowed to delete news articles.", | |
66 | Name: "tranDelNewsArt", | |
67 | Handler: HandleDelNewsArt, | |
68 | }, | |
69 | tranDelNewsItem: { | |
70 | Access: accessAlwaysAllow, // Granular access enforced inside the handler | |
71 | // Has multiple access flags: News Delete Folder (37) or News Delete Category (35) | |
72 | // TODO: Implement inside the handler | |
73 | Name: "tranDelNewsItem", | |
74 | Handler: HandleDelNewsItem, | |
75 | }, | |
76 | tranDeleteFile: { | |
77 | Access: accessAlwaysAllow, // Granular access enforced inside the handler | |
78 | Name: "tranDeleteFile", | |
79 | Handler: HandleDeleteFile, | |
80 | }, | |
81 | tranDeleteUser: { | |
82 | Access: accessDeleteUser, | |
83 | DenyMsg: "You are not allowed to delete accounts.", | |
84 | Name: "tranDeleteUser", | |
85 | Handler: HandleDeleteUser, | |
86 | }, | |
87 | tranDisconnectUser: { | |
88 | Access: accessDisconUser, | |
89 | DenyMsg: "You are not allowed to disconnect users.", | |
90 | Name: "tranDisconnectUser", | |
91 | Handler: HandleDisconnectUser, | |
92 | }, | |
93 | tranDownloadFile: { | |
94 | Access: accessDownloadFile, | |
95 | DenyMsg: "You are not allowed to download files.", | |
96 | Name: "tranDownloadFile", | |
97 | Handler: HandleDownloadFile, | |
98 | }, | |
99 | tranDownloadFldr: { | |
100 | Access: accessDownloadFile, // There is no specific access flag for folder vs file download | |
101 | DenyMsg: "You are not allowed to download files.", | |
102 | Name: "tranDownloadFldr", | |
103 | Handler: HandleDownloadFolder, | |
104 | }, | |
105 | tranGetClientInfoText: { | |
106 | Access: accessGetClientInfo, | |
107 | DenyMsg: "You are not allowed to get client info", | |
108 | Name: "tranGetClientInfoText", | |
109 | Handler: HandleGetClientConnInfoText, | |
110 | }, | |
111 | tranGetFileInfo: { | |
112 | Access: accessAlwaysAllow, | |
113 | Name: "tranGetFileInfo", | |
114 | Handler: HandleGetFileInfo, | |
115 | }, | |
116 | tranGetFileNameList: { | |
117 | Access: accessAlwaysAllow, | |
118 | Name: "tranGetFileNameList", | |
119 | Handler: HandleGetFileNameList, | |
120 | }, | |
121 | tranGetMsgs: { | |
5454019c JH |
122 | Access: accessNewsReadArt, |
123 | DenyMsg: "You are not allowed to read news.", | |
6988a057 JH |
124 | Name: "tranGetMsgs", |
125 | Handler: HandleGetMsgs, | |
126 | }, | |
127 | tranGetNewsArtData: { | |
5454019c JH |
128 | Access: accessNewsReadArt, |
129 | DenyMsg: "You are not allowed to read news.", | |
6988a057 JH |
130 | Name: "tranGetNewsArtData", |
131 | Handler: HandleGetNewsArtData, | |
132 | }, | |
133 | tranGetNewsArtNameList: { | |
5454019c JH |
134 | Access: accessNewsReadArt, |
135 | DenyMsg: "You are not allowed to read news.", | |
6988a057 JH |
136 | Name: "tranGetNewsArtNameList", |
137 | Handler: HandleGetNewsArtNameList, | |
138 | }, | |
139 | tranGetNewsCatNameList: { | |
5454019c JH |
140 | Access: accessNewsReadArt, |
141 | DenyMsg: "You are not allowed to read news.", | |
6988a057 JH |
142 | Name: "tranGetNewsCatNameList", |
143 | Handler: HandleGetNewsCatNameList, | |
144 | }, | |
145 | tranGetUser: { | |
146 | Access: accessOpenUser, | |
147 | DenyMsg: "You are not allowed to view accounts.", | |
148 | Name: "tranGetUser", | |
149 | Handler: HandleGetUser, | |
150 | }, | |
151 | tranGetUserNameList: { | |
152 | Access: accessAlwaysAllow, | |
153 | Name: "tranHandleGetUserNameList", | |
154 | Handler: HandleGetUserNameList, | |
155 | }, | |
156 | tranInviteNewChat: { | |
157 | Access: accessOpenChat, | |
158 | DenyMsg: "You are not allowed to request private chat.", | |
159 | Name: "tranInviteNewChat", | |
160 | Handler: HandleInviteNewChat, | |
161 | }, | |
162 | tranInviteToChat: { | |
5454019c JH |
163 | Access: accessOpenChat, |
164 | DenyMsg: "You are not allowed to request private chat.", | |
6988a057 JH |
165 | Name: "tranInviteToChat", |
166 | Handler: HandleInviteToChat, | |
167 | }, | |
168 | tranJoinChat: { | |
5454019c | 169 | Access: accessAlwaysAllow, |
6988a057 JH |
170 | Name: "tranJoinChat", |
171 | Handler: HandleJoinChat, | |
172 | }, | |
173 | tranKeepAlive: { | |
5454019c | 174 | Access: accessAlwaysAllow, |
6988a057 JH |
175 | Name: "tranKeepAlive", |
176 | Handler: HandleKeepAlive, | |
177 | }, | |
178 | tranLeaveChat: { | |
5454019c | 179 | Access: accessAlwaysAllow, |
6988a057 JH |
180 | Name: "tranJoinChat", |
181 | Handler: HandleLeaveChat, | |
182 | }, | |
5454019c | 183 | |
6988a057 JH |
184 | tranListUsers: { |
185 | Access: accessOpenUser, | |
186 | DenyMsg: "You are not allowed to view accounts.", | |
187 | Name: "tranListUsers", | |
188 | Handler: HandleListUsers, | |
189 | }, | |
190 | tranMoveFile: { | |
191 | Access: accessMoveFile, | |
192 | DenyMsg: "You are not allowed to move files.", | |
193 | Name: "tranMoveFile", | |
194 | Handler: HandleMoveFile, | |
195 | }, | |
196 | tranNewFolder: { | |
5454019c JH |
197 | Access: accessCreateFolder, |
198 | DenyMsg: "You are not allow to create folders.", | |
6988a057 JH |
199 | Name: "tranNewFolder", |
200 | Handler: HandleNewFolder, | |
201 | }, | |
202 | tranNewNewsCat: { | |
5454019c JH |
203 | Access: accessNewsCreateCat, |
204 | DenyMsg: "You are not allowed to create news categories.", | |
6988a057 JH |
205 | Name: "tranNewNewsCat", |
206 | Handler: HandleNewNewsCat, | |
207 | }, | |
208 | tranNewNewsFldr: { | |
5454019c JH |
209 | Access: accessNewsCreateFldr, |
210 | DenyMsg: "You are not allowed to create news folders.", | |
6988a057 JH |
211 | Name: "tranNewNewsFldr", |
212 | Handler: HandleNewNewsFldr, | |
213 | }, | |
214 | tranNewUser: { | |
215 | Access: accessCreateUser, | |
216 | DenyMsg: "You are not allowed to create new accounts.", | |
217 | Name: "tranNewUser", | |
218 | Handler: HandleNewUser, | |
219 | }, | |
220 | tranOldPostNews: { | |
5454019c JH |
221 | Access: accessNewsPostArt, |
222 | DenyMsg: "You are not allowed to post news.", | |
6988a057 JH |
223 | Name: "tranOldPostNews", |
224 | Handler: HandleTranOldPostNews, | |
225 | }, | |
226 | tranPostNewsArt: { | |
227 | Access: accessNewsPostArt, | |
228 | DenyMsg: "You are not allowed to post news articles.", | |
229 | Name: "tranPostNewsArt", | |
230 | Handler: HandlePostNewsArt, | |
231 | }, | |
232 | tranRejectChatInvite: { | |
5454019c | 233 | Access: accessAlwaysAllow, |
6988a057 JH |
234 | Name: "tranRejectChatInvite", |
235 | Handler: HandleRejectChatInvite, | |
236 | }, | |
237 | tranSendInstantMsg: { | |
5454019c | 238 | Access: accessAlwaysAllow, |
6988a057 JH |
239 | //Access: accessSendPrivMsg, |
240 | //DenyMsg: "You are not allowed to send private messages", | |
241 | Name: "tranSendInstantMsg", | |
242 | Handler: HandleSendInstantMsg, | |
243 | RequiredFields: []requiredField{ | |
244 | { | |
245 | ID: fieldData, | |
246 | minLen: 0, | |
247 | }, | |
248 | { | |
249 | ID: fieldUserID, | |
250 | }, | |
251 | }, | |
252 | }, | |
253 | tranSetChatSubject: { | |
5454019c | 254 | Access: accessAlwaysAllow, |
6988a057 JH |
255 | Name: "tranSetChatSubject", |
256 | Handler: HandleSetChatSubject, | |
257 | }, | |
258 | tranSetClientUserInfo: { | |
259 | Access: accessAlwaysAllow, | |
260 | Name: "tranSetClientUserInfo", | |
261 | Handler: HandleSetClientUserInfo, | |
262 | }, | |
263 | tranSetFileInfo: { | |
5454019c | 264 | Access: accessAlwaysAllow, // granular access is in the handler |
6988a057 JH |
265 | Name: "tranSetFileInfo", |
266 | Handler: HandleSetFileInfo, | |
267 | }, | |
268 | tranSetUser: { | |
269 | Access: accessModifyUser, | |
270 | DenyMsg: "You are not allowed to modify accounts.", | |
271 | Name: "tranSetUser", | |
272 | Handler: HandleSetUser, | |
273 | }, | |
274 | tranUploadFile: { | |
275 | Access: accessUploadFile, | |
276 | DenyMsg: "You are not allowed to upload files.", | |
277 | Name: "tranUploadFile", | |
278 | Handler: HandleUploadFile, | |
279 | }, | |
280 | tranUploadFldr: { | |
5454019c | 281 | Access: accessAlwaysAllow, // TODO: what should this be? |
6988a057 JH |
282 | Name: "tranUploadFldr", |
283 | Handler: HandleUploadFolder, | |
284 | }, | |
285 | tranUserBroadcast: { | |
286 | Access: accessBroadcast, | |
287 | DenyMsg: "You are not allowed to send broadcast messages.", | |
288 | Name: "tranUserBroadcast", | |
289 | Handler: HandleUserBroadcast, | |
290 | }, | |
291 | } | |
292 | ||
293 | func HandleChatSend(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
294 | // Truncate long usernames | |
72dd37f1 | 295 | trunc := fmt.Sprintf("%13s", cc.UserName) |
6988a057 JH |
296 | formattedMsg := fmt.Sprintf("\r%.14s: %s", trunc, t.GetField(fieldData).Data) |
297 | ||
298 | // By holding the option key, Hotline chat allows users to send /me formatted messages like: | |
299 | // *** Halcyon does stuff | |
300 | // This is indicated by the presence of the optional field fieldChatOptions in the transaction payload | |
301 | if t.GetField(fieldChatOptions).Data != nil { | |
72dd37f1 | 302 | formattedMsg = fmt.Sprintf("\r*** %s %s", cc.UserName, t.GetField(fieldData).Data) |
6988a057 JH |
303 | } |
304 | ||
305 | if bytes.Equal(t.GetField(fieldData).Data, []byte("/stats")) { | |
306 | formattedMsg = strings.Replace(cc.Server.Stats.String(), "\n", "\r", -1) | |
307 | } | |
308 | ||
309 | chatID := t.GetField(fieldChatID).Data | |
310 | // a non-nil chatID indicates the message belongs to a private chat | |
311 | if chatID != nil { | |
312 | chatInt := binary.BigEndian.Uint32(chatID) | |
313 | privChat := cc.Server.PrivateChats[chatInt] | |
314 | ||
315 | // send the message to all connected clients of the private chat | |
316 | for _, c := range privChat.ClientConn { | |
317 | res = append(res, *NewTransaction( | |
318 | tranChatMsg, | |
319 | c.ID, | |
320 | NewField(fieldChatID, chatID), | |
321 | NewField(fieldData, []byte(formattedMsg)), | |
322 | )) | |
323 | } | |
324 | return res, err | |
325 | } | |
326 | ||
327 | for _, c := range sortedClients(cc.Server.Clients) { | |
328 | // Filter out clients that do not have the read chat permission | |
329 | if authorize(c.Account.Access, accessReadChat) { | |
330 | res = append(res, *NewTransaction(tranChatMsg, c.ID, NewField(fieldData, []byte(formattedMsg)))) | |
331 | } | |
332 | } | |
333 | ||
334 | return res, err | |
335 | } | |
336 | ||
337 | // HandleSendInstantMsg sends instant message to the user on the current server. | |
338 | // Fields used in the request: | |
339 | // 103 User ID | |
340 | // 113 Options | |
341 | // One of the following values: | |
342 | // - User message (myOpt_UserMessage = 1) | |
343 | // - Refuse message (myOpt_RefuseMessage = 2) | |
344 | // - Refuse chat (myOpt_RefuseChat = 3) | |
345 | // - Automatic response (myOpt_AutomaticResponse = 4)" | |
346 | // 101 Data Optional | |
347 | // 214 Quoting message Optional | |
348 | // | |
349 | //Fields used in the reply: | |
350 | // None | |
351 | func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
352 | msg := t.GetField(fieldData) | |
353 | ID := t.GetField(fieldUserID) | |
354 | // TODO: Implement reply quoting | |
355 | //options := transaction.GetField(hotline.fieldOptions) | |
356 | ||
357 | res = append(res, | |
358 | *NewTransaction( | |
359 | tranServerMsg, | |
360 | &ID.Data, | |
361 | NewField(fieldData, msg.Data), | |
72dd37f1 | 362 | NewField(fieldUserName, cc.UserName), |
6988a057 JH |
363 | NewField(fieldUserID, *cc.ID), |
364 | NewField(fieldOptions, []byte{0, 1}), | |
365 | ), | |
366 | ) | |
367 | id, _ := byteToInt(ID.Data) | |
368 | ||
369 | //keys := make([]uint16, 0, len(cc.Server.Clients)) | |
370 | //for k := range cc.Server.Clients { | |
371 | // keys = append(keys, k) | |
372 | //} | |
373 | ||
374 | otherClient := cc.Server.Clients[uint16(id)] | |
375 | if otherClient == nil { | |
376 | return res, errors.New("ohno") | |
377 | } | |
378 | ||
379 | // Respond with auto reply if other client has it enabled | |
380 | if len(*otherClient.AutoReply) > 0 { | |
381 | res = append(res, | |
382 | *NewTransaction( | |
383 | tranServerMsg, | |
384 | cc.ID, | |
385 | NewField(fieldData, *otherClient.AutoReply), | |
72dd37f1 | 386 | NewField(fieldUserName, otherClient.UserName), |
6988a057 JH |
387 | NewField(fieldUserID, *otherClient.ID), |
388 | NewField(fieldOptions, []byte{0, 1}), | |
389 | ), | |
390 | ) | |
391 | } | |
392 | ||
393 | res = append(res, cc.NewReply(t)) | |
394 | ||
395 | return res, err | |
396 | } | |
397 | ||
398 | func HandleGetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
399 | fileName := string(t.GetField(fieldFileName).Data) | |
400 | filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data) | |
6988a057 JH |
401 | |
402 | ffo, err := NewFlattenedFileObject(filePath, fileName) | |
403 | if err != nil { | |
404 | return res, err | |
405 | } | |
406 | ||
407 | res = append(res, cc.NewReply(t, | |
408 | NewField(fieldFileName, []byte(fileName)), | |
409 | NewField(fieldFileTypeString, ffo.FlatFileInformationFork.TypeSignature), | |
410 | NewField(fieldFileCreatorString, ffo.FlatFileInformationFork.CreatorSignature), | |
411 | NewField(fieldFileComment, ffo.FlatFileInformationFork.Comment), | |
412 | NewField(fieldFileType, ffo.FlatFileInformationFork.TypeSignature), | |
413 | NewField(fieldFileCreateDate, ffo.FlatFileInformationFork.CreateDate), | |
414 | NewField(fieldFileModifyDate, ffo.FlatFileInformationFork.ModifyDate), | |
415 | NewField(fieldFileSize, ffo.FlatFileDataForkHeader.DataSize), | |
416 | )) | |
417 | return res, err | |
418 | } | |
419 | ||
420 | // HandleSetFileInfo updates a file or folder name and/or comment from the Get Info window | |
421 | // TODO: Implement support for comments | |
422 | // Fields used in the request: | |
423 | // * 201 File name | |
424 | // * 202 File path Optional | |
425 | // * 211 File new name Optional | |
426 | // * 210 File comment Optional | |
427 | // Fields used in the reply: None | |
428 | func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
429 | fileName := string(t.GetField(fieldFileName).Data) | |
430 | filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data) | |
431 | //fileComment := t.GetField(fieldFileComment).Data | |
432 | fileNewName := t.GetField(fieldFileNewName).Data | |
433 | ||
434 | if fileNewName != nil { | |
435 | path := filePath + "/" + fileName | |
436 | fi, err := os.Stat(path) | |
437 | if err != nil { | |
438 | return res, err | |
439 | } | |
440 | switch mode := fi.Mode(); { | |
441 | case mode.IsDir(): | |
442 | if !authorize(cc.Account.Access, accessRenameFolder) { | |
443 | res = append(res, cc.NewErrReply(t, "You are not allowed to rename folders.")) | |
444 | return res, err | |
445 | } | |
446 | case mode.IsRegular(): | |
447 | if !authorize(cc.Account.Access, accessRenameFile) { | |
448 | res = append(res, cc.NewErrReply(t, "You are not allowed to rename files.")) | |
449 | return res, err | |
450 | } | |
451 | } | |
452 | ||
453 | err = os.Rename(filePath+"/"+fileName, filePath+"/"+string(fileNewName)) | |
454 | if os.IsNotExist(err) { | |
455 | res = append(res, cc.NewErrReply(t, "Cannot rename file "+fileName+" because it does not exist or cannot be found.")) | |
456 | return res, err | |
457 | } | |
458 | } | |
459 | ||
460 | res = append(res, cc.NewReply(t)) | |
461 | return res, err | |
462 | } | |
463 | ||
464 | // HandleDeleteFile deletes a file or folder | |
465 | // Fields used in the request: | |
466 | // * 201 File name | |
467 | // * 202 File path | |
468 | // Fields used in the reply: none | |
469 | func HandleDeleteFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
470 | fileName := string(t.GetField(fieldFileName).Data) | |
471 | filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data) | |
472 | ||
f6d08d0a | 473 | path := filePath + fileName |
6988a057 | 474 | |
f6d08d0a | 475 | cc.Server.Logger.Debugw("Delete file", "src", path) |
6988a057 JH |
476 | |
477 | fi, err := os.Stat(path) | |
478 | if err != nil { | |
479 | res = append(res, cc.NewErrReply(t, "Cannot delete file "+fileName+" because it does not exist or cannot be found.")) | |
480 | return res, nil | |
481 | } | |
482 | switch mode := fi.Mode(); { | |
483 | case mode.IsDir(): | |
484 | if !authorize(cc.Account.Access, accessDeleteFolder) { | |
485 | res = append(res, cc.NewErrReply(t, "You are not allowed to delete folders.")) | |
486 | return res, err | |
487 | } | |
488 | case mode.IsRegular(): | |
489 | if !authorize(cc.Account.Access, accessDeleteFile) { | |
490 | res = append(res, cc.NewErrReply(t, "You are not allowed to delete files.")) | |
491 | return res, err | |
492 | } | |
493 | } | |
494 | ||
495 | if err := os.RemoveAll(path); err != nil { | |
496 | return res, err | |
497 | } | |
498 | ||
499 | res = append(res, cc.NewReply(t)) | |
500 | return res, err | |
501 | } | |
502 | ||
503 | // HandleMoveFile moves files or folders. Note: seemingly not documented | |
504 | func HandleMoveFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
505 | fileName := string(t.GetField(fieldFileName).Data) | |
f6d08d0a JH |
506 | filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data) |
507 | fileNewPath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFileNewPath).Data) | |
6988a057 JH |
508 | |
509 | cc.Server.Logger.Debugw("Move file", "src", filePath+"/"+fileName, "dst", fileNewPath+"/"+fileName) | |
510 | ||
511 | path := filePath + "/" + fileName | |
512 | fi, err := os.Stat(path) | |
513 | if err != nil { | |
514 | return res, err | |
515 | } | |
516 | switch mode := fi.Mode(); { | |
517 | case mode.IsDir(): | |
518 | if !authorize(cc.Account.Access, accessMoveFolder) { | |
519 | res = append(res, cc.NewErrReply(t, "You are not allowed to move folders.")) | |
520 | return res, err | |
521 | } | |
522 | case mode.IsRegular(): | |
523 | if !authorize(cc.Account.Access, accessMoveFile) { | |
524 | res = append(res, cc.NewErrReply(t, "You are not allowed to move files.")) | |
525 | return res, err | |
526 | } | |
527 | } | |
528 | ||
529 | err = os.Rename(filePath+"/"+fileName, fileNewPath+"/"+fileName) | |
530 | if os.IsNotExist(err) { | |
531 | res = append(res, cc.NewErrReply(t, "Cannot delete file "+fileName+" because it does not exist or cannot be found.")) | |
532 | return res, err | |
533 | } | |
534 | if err != nil { | |
535 | return []Transaction{}, err | |
536 | } | |
537 | // TODO: handle other possible errors; e.g. file delete fails due to file permission issue | |
538 | ||
539 | res = append(res, cc.NewReply(t)) | |
540 | return res, err | |
541 | } | |
542 | ||
543 | func HandleNewFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
544 | newFolderPath := cc.Server.Config.FileRoot | |
545 | ||
546 | // fieldFilePath is only present for nested paths | |
547 | if t.GetField(fieldFilePath).Data != nil { | |
72dd37f1 JH |
548 | var newFp FilePath |
549 | newFp.UnmarshalBinary(t.GetField(fieldFilePath).Data) | |
6988a057 JH |
550 | newFolderPath += newFp.String() |
551 | } | |
552 | newFolderPath += "/" + string(t.GetField(fieldFileName).Data) | |
553 | ||
554 | if err := os.Mkdir(newFolderPath, 0777); err != nil { | |
555 | // TODO: Send error response to client | |
556 | return []Transaction{}, err | |
557 | } | |
558 | ||
559 | res = append(res, cc.NewReply(t)) | |
560 | return res, err | |
561 | } | |
562 | ||
563 | func HandleSetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
564 | login := DecodeUserString(t.GetField(fieldUserLogin).Data) | |
565 | userName := string(t.GetField(fieldUserName).Data) | |
566 | ||
567 | newAccessLvl := t.GetField(fieldUserAccess).Data | |
568 | ||
569 | account := cc.Server.Accounts[login] | |
570 | account.Access = &newAccessLvl | |
571 | account.Name = userName | |
572 | ||
573 | // If the password field is cleared in the Hotline edit user UI, the SetUser transaction does | |
574 | // not include fieldUserPassword | |
575 | if t.GetField(fieldUserPassword).Data == nil { | |
576 | account.Password = hashAndSalt([]byte("")) | |
577 | } | |
578 | if len(t.GetField(fieldUserPassword).Data) > 1 { | |
579 | account.Password = hashAndSalt(t.GetField(fieldUserPassword).Data) | |
580 | } | |
581 | ||
582 | file := cc.Server.ConfigDir + "Users/" + login + ".yaml" | |
583 | out, err := yaml.Marshal(&account) | |
584 | if err != nil { | |
585 | return res, err | |
586 | } | |
587 | if err := ioutil.WriteFile(file, out, 0666); err != nil { | |
588 | return res, err | |
589 | } | |
590 | ||
591 | // Notify connected clients logged in as the user of the new access level | |
592 | for _, c := range cc.Server.Clients { | |
593 | if c.Account.Login == login { | |
594 | // Note: comment out these two lines to test server-side deny messages | |
595 | newT := NewTransaction(tranUserAccess, c.ID, NewField(fieldUserAccess, newAccessLvl)) | |
596 | res = append(res, *newT) | |
597 | ||
598 | flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*c.Flags))) | |
599 | if authorize(c.Account.Access, accessDisconUser) { | |
600 | flagBitmap.SetBit(flagBitmap, userFlagAdmin, 1) | |
601 | } else { | |
602 | flagBitmap.SetBit(flagBitmap, userFlagAdmin, 0) | |
603 | } | |
604 | binary.BigEndian.PutUint16(*c.Flags, uint16(flagBitmap.Int64())) | |
605 | ||
606 | c.Account.Access = account.Access | |
607 | ||
608 | cc.sendAll( | |
609 | tranNotifyChangeUser, | |
610 | NewField(fieldUserID, *c.ID), | |
611 | NewField(fieldUserFlags, *c.Flags), | |
72dd37f1 | 612 | NewField(fieldUserName, c.UserName), |
6988a057 JH |
613 | NewField(fieldUserIconID, *c.Icon), |
614 | ) | |
615 | } | |
616 | } | |
617 | ||
618 | // TODO: If we have just promoted a connected user to admin, notify | |
619 | // connected clients to turn the user red | |
620 | ||
621 | res = append(res, cc.NewReply(t)) | |
622 | return res, err | |
623 | } | |
624 | ||
625 | func HandleGetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
626 | userLogin := string(t.GetField(fieldUserLogin).Data) | |
6988a057 JH |
627 | account := cc.Server.Accounts[userLogin] |
628 | if account == nil { | |
629 | errorT := cc.NewErrReply(t, "Account does not exist.") | |
630 | res = append(res, errorT) | |
631 | return res, err | |
632 | } | |
633 | ||
634 | res = append(res, cc.NewReply(t, | |
635 | NewField(fieldUserName, []byte(account.Name)), | |
b25c4a19 | 636 | NewField(fieldUserLogin, negateString(t.GetField(fieldUserLogin).Data)), |
6988a057 JH |
637 | NewField(fieldUserPassword, []byte(account.Password)), |
638 | NewField(fieldUserAccess, *account.Access), | |
639 | )) | |
640 | return res, err | |
641 | } | |
642 | ||
643 | func HandleListUsers(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
644 | var userFields []Field | |
645 | // TODO: make order deterministic | |
646 | for _, acc := range cc.Server.Accounts { | |
c5d9af5a | 647 | userField := acc.MarshalBinary() |
6988a057 JH |
648 | userFields = append(userFields, NewField(fieldData, userField)) |
649 | } | |
650 | ||
651 | res = append(res, cc.NewReply(t, userFields...)) | |
652 | return res, err | |
653 | } | |
654 | ||
655 | // HandleNewUser creates a new user account | |
656 | func HandleNewUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
657 | login := DecodeUserString(t.GetField(fieldUserLogin).Data) | |
658 | ||
659 | // If the account already exists, reply with an error | |
660 | // TODO: make order deterministic | |
661 | if _, ok := cc.Server.Accounts[login]; ok { | |
662 | res = append(res, cc.NewErrReply(t, "Cannot create account "+login+" because there is already an account with that login.")) | |
663 | return res, err | |
664 | } | |
665 | ||
666 | if err := cc.Server.NewUser( | |
667 | login, | |
668 | string(t.GetField(fieldUserName).Data), | |
669 | string(t.GetField(fieldUserPassword).Data), | |
670 | t.GetField(fieldUserAccess).Data, | |
671 | ); err != nil { | |
672 | return []Transaction{}, err | |
673 | } | |
674 | ||
675 | res = append(res, cc.NewReply(t)) | |
676 | return res, err | |
677 | } | |
678 | ||
679 | func HandleDeleteUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
680 | // TODO: Handle case where account doesn't exist; e.g. delete race condition | |
681 | login := DecodeUserString(t.GetField(fieldUserLogin).Data) | |
682 | ||
683 | if err := cc.Server.DeleteUser(login); err != nil { | |
684 | return res, err | |
685 | } | |
686 | ||
687 | res = append(res, cc.NewReply(t)) | |
688 | return res, err | |
689 | } | |
690 | ||
691 | // HandleUserBroadcast sends an Administrator Message to all connected clients of the server | |
692 | func HandleUserBroadcast(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
693 | cc.sendAll( | |
694 | tranServerMsg, | |
695 | NewField(fieldData, t.GetField(tranGetMsgs).Data), | |
696 | NewField(fieldChatOptions, []byte{0}), | |
697 | ) | |
698 | ||
699 | res = append(res, cc.NewReply(t)) | |
700 | return res, err | |
701 | } | |
702 | ||
703 | func byteToInt(bytes []byte) (int, error) { | |
704 | switch len(bytes) { | |
705 | case 2: | |
706 | return int(binary.BigEndian.Uint16(bytes)), nil | |
707 | case 4: | |
708 | return int(binary.BigEndian.Uint32(bytes)), nil | |
709 | } | |
710 | ||
711 | return 0, errors.New("unknown byte length") | |
712 | } | |
713 | ||
714 | func HandleGetClientConnInfoText(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
715 | clientID, _ := byteToInt(t.GetField(fieldUserID).Data) | |
716 | ||
717 | clientConn := cc.Server.Clients[uint16(clientID)] | |
718 | if clientConn == nil { | |
719 | return res, errors.New("invalid client") | |
720 | } | |
721 | ||
722 | // TODO: Implement non-hardcoded values | |
723 | template := `Nickname: %s | |
724 | Name: %s | |
725 | Account: %s | |
726 | Address: %s | |
727 | ||
728 | -------- File Downloads --------- | |
729 | ||
730 | %s | |
731 | ||
732 | ------- Folder Downloads -------- | |
733 | ||
734 | None. | |
735 | ||
736 | --------- File Uploads ---------- | |
737 | ||
738 | None. | |
739 | ||
740 | -------- Folder Uploads --------- | |
741 | ||
742 | None. | |
743 | ||
744 | ------- Waiting Downloads ------- | |
745 | ||
746 | None. | |
747 | ||
748 | ` | |
749 | ||
750 | activeDownloads := clientConn.Transfers[FileDownload] | |
751 | activeDownloadList := "None." | |
752 | for _, dl := range activeDownloads { | |
753 | activeDownloadList += dl.String() + "\n" | |
754 | } | |
755 | ||
756 | template = fmt.Sprintf( | |
757 | template, | |
72dd37f1 | 758 | clientConn.UserName, |
6988a057 JH |
759 | clientConn.Account.Name, |
760 | clientConn.Account.Login, | |
761 | clientConn.Connection.RemoteAddr().String(), | |
762 | activeDownloadList, | |
763 | ) | |
764 | template = strings.Replace(template, "\n", "\r", -1) | |
765 | ||
766 | res = append(res, cc.NewReply(t, | |
767 | NewField(fieldData, []byte(template)), | |
72dd37f1 | 768 | NewField(fieldUserName, clientConn.UserName), |
6988a057 JH |
769 | )) |
770 | return res, err | |
771 | } | |
772 | ||
773 | func HandleGetUserNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
774 | res = append(res, cc.NewReply(t, cc.Server.connectedUsers()...)) | |
775 | ||
776 | return res, err | |
777 | } | |
778 | ||
779 | func (cc *ClientConn) notifyNewUserHasJoined() (res []Transaction, err error) { | |
780 | // Notify other ccs that a new user has connected | |
781 | cc.NotifyOthers( | |
782 | *NewTransaction( | |
783 | tranNotifyChangeUser, nil, | |
72dd37f1 | 784 | NewField(fieldUserName, cc.UserName), |
6988a057 JH |
785 | NewField(fieldUserID, *cc.ID), |
786 | NewField(fieldUserIconID, *cc.Icon), | |
787 | NewField(fieldUserFlags, *cc.Flags), | |
788 | ), | |
789 | ) | |
790 | ||
791 | return res, nil | |
792 | } | |
793 | ||
794 | func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
795 | bs := make([]byte, 2) | |
796 | binary.BigEndian.PutUint16(bs, *cc.Server.NextGuestID) | |
797 | ||
72dd37f1 | 798 | cc.UserName = t.GetField(fieldUserName).Data |
6988a057 JH |
799 | *cc.ID = bs |
800 | *cc.Icon = t.GetField(fieldUserIconID).Data | |
801 | ||
802 | options := t.GetField(fieldOptions).Data | |
803 | optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options))) | |
804 | ||
805 | flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags))) | |
806 | ||
807 | // Check refuse private PM option | |
808 | if optBitmap.Bit(refusePM) == 1 { | |
809 | flagBitmap.SetBit(flagBitmap, userFlagRefusePM, 1) | |
810 | binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64())) | |
811 | } | |
812 | ||
813 | // Check refuse private chat option | |
814 | if optBitmap.Bit(refuseChat) == 1 { | |
815 | flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, 1) | |
816 | binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64())) | |
817 | } | |
818 | ||
819 | // Check auto response | |
820 | if optBitmap.Bit(autoResponse) == 1 { | |
821 | *cc.AutoReply = t.GetField(fieldAutomaticResponse).Data | |
822 | } else { | |
823 | *cc.AutoReply = []byte{} | |
824 | } | |
825 | ||
826 | _, _ = cc.notifyNewUserHasJoined() | |
827 | ||
828 | res = append(res, cc.NewReply(t)) | |
829 | ||
830 | return res, err | |
831 | } | |
832 | ||
833 | const defaultNewsDateFormat = "Jan02 15:04" // Jun23 20:49 | |
834 | // "Mon, 02 Jan 2006 15:04:05 MST" | |
835 | ||
836 | const defaultNewsTemplate = `From %s (%s): | |
837 | ||
838 | %s | |
839 | ||
840 | __________________________________________________________` | |
841 | ||
842 | // HandleTranOldPostNews updates the flat news | |
843 | // Fields used in this request: | |
844 | // 101 Data | |
845 | func HandleTranOldPostNews(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
846 | cc.Server.flatNewsMux.Lock() | |
847 | defer cc.Server.flatNewsMux.Unlock() | |
848 | ||
849 | newsDateTemplate := defaultNewsDateFormat | |
850 | if cc.Server.Config.NewsDateFormat != "" { | |
851 | newsDateTemplate = cc.Server.Config.NewsDateFormat | |
852 | } | |
853 | ||
854 | newsTemplate := defaultNewsTemplate | |
855 | if cc.Server.Config.NewsDelimiter != "" { | |
856 | newsTemplate = cc.Server.Config.NewsDelimiter | |
857 | } | |
858 | ||
72dd37f1 | 859 | newsPost := fmt.Sprintf(newsTemplate+"\r", cc.UserName, time.Now().Format(newsDateTemplate), t.GetField(fieldData).Data) |
6988a057 JH |
860 | newsPost = strings.Replace(newsPost, "\n", "\r", -1) |
861 | ||
862 | // update news in memory | |
863 | cc.Server.FlatNews = append([]byte(newsPost), cc.Server.FlatNews...) | |
864 | ||
865 | // update news on disk | |
866 | if err := ioutil.WriteFile(cc.Server.ConfigDir+"MessageBoard.txt", cc.Server.FlatNews, 0644); err != nil { | |
867 | return res, err | |
868 | } | |
869 | ||
870 | // Notify all clients of updated news | |
871 | cc.sendAll( | |
872 | tranNewMsg, | |
873 | NewField(fieldData, []byte(newsPost)), | |
874 | ) | |
875 | ||
876 | res = append(res, cc.NewReply(t)) | |
877 | return res, err | |
878 | } | |
879 | ||
880 | func HandleDisconnectUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
881 | clientConn := cc.Server.Clients[binary.BigEndian.Uint16(t.GetField(fieldUserID).Data)] | |
882 | ||
883 | if authorize(clientConn.Account.Access, accessCannotBeDiscon) { | |
884 | res = append(res, cc.NewErrReply(t, clientConn.Account.Login+" is not allowed to be disconnected.")) | |
885 | return res, err | |
886 | } | |
887 | ||
888 | if err := clientConn.Connection.Close(); err != nil { | |
889 | return res, err | |
890 | } | |
891 | ||
892 | res = append(res, cc.NewReply(t)) | |
893 | return res, err | |
894 | } | |
895 | ||
896 | func HandleGetNewsCatNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
897 | // Fields used in the request: | |
898 | // 325 News path (Optional) | |
899 | ||
900 | newsPath := t.GetField(fieldNewsPath).Data | |
901 | cc.Server.Logger.Infow("NewsPath: ", "np", string(newsPath)) | |
902 | ||
903 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
904 | cats := cc.Server.GetNewsCatByPath(pathStrs) | |
905 | ||
906 | // To store the keys in slice in sorted order | |
907 | keys := make([]string, len(cats)) | |
908 | i := 0 | |
909 | for k := range cats { | |
910 | keys[i] = k | |
911 | i++ | |
912 | } | |
913 | sort.Strings(keys) | |
914 | ||
915 | var fieldData []Field | |
916 | for _, k := range keys { | |
917 | cat := cats[k] | |
72dd37f1 | 918 | b, _ := cat.MarshalBinary() |
6988a057 JH |
919 | fieldData = append(fieldData, NewField( |
920 | fieldNewsCatListData15, | |
72dd37f1 | 921 | b, |
6988a057 JH |
922 | )) |
923 | } | |
924 | ||
925 | res = append(res, cc.NewReply(t, fieldData...)) | |
926 | return res, err | |
927 | } | |
928 | ||
929 | func HandleNewNewsCat(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
930 | name := string(t.GetField(fieldNewsCatName).Data) | |
931 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
932 | ||
933 | cats := cc.Server.GetNewsCatByPath(pathStrs) | |
934 | cats[name] = NewsCategoryListData15{ | |
935 | Name: name, | |
936 | Type: []byte{0, 3}, | |
937 | Articles: map[uint32]*NewsArtData{}, | |
938 | SubCats: make(map[string]NewsCategoryListData15), | |
939 | } | |
940 | ||
941 | if err := cc.Server.writeThreadedNews(); err != nil { | |
942 | return res, err | |
943 | } | |
944 | res = append(res, cc.NewReply(t)) | |
945 | return res, err | |
946 | } | |
947 | ||
948 | func HandleNewNewsFldr(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
949 | // Fields used in the request: | |
950 | // 322 News category name | |
951 | // 325 News path | |
952 | name := string(t.GetField(fieldFileName).Data) | |
953 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
954 | ||
955 | cc.Server.Logger.Infof("Creating new news folder %s", name) | |
956 | ||
957 | cats := cc.Server.GetNewsCatByPath(pathStrs) | |
958 | cats[name] = NewsCategoryListData15{ | |
959 | Name: name, | |
960 | Type: []byte{0, 2}, | |
961 | Articles: map[uint32]*NewsArtData{}, | |
962 | SubCats: make(map[string]NewsCategoryListData15), | |
963 | } | |
964 | if err := cc.Server.writeThreadedNews(); err != nil { | |
965 | return res, err | |
966 | } | |
967 | res = append(res, cc.NewReply(t)) | |
968 | return res, err | |
969 | } | |
970 | ||
971 | // Fields used in the request: | |
972 | // 325 News path Optional | |
973 | // | |
974 | // Reply fields: | |
975 | // 321 News article list data Optional | |
976 | func HandleGetNewsArtNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
977 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
978 | ||
979 | var cat NewsCategoryListData15 | |
980 | cats := cc.Server.ThreadedNews.Categories | |
981 | ||
982 | for _, path := range pathStrs { | |
983 | cat = cats[path] | |
984 | cats = cats[path].SubCats | |
985 | } | |
986 | ||
987 | nald := cat.GetNewsArtListData() | |
988 | ||
989 | res = append(res, cc.NewReply(t, NewField(fieldNewsArtListData, nald.Payload()))) | |
990 | return res, err | |
991 | } | |
992 | ||
993 | func HandleGetNewsArtData(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
994 | // Request fields | |
995 | // 325 News path | |
996 | // 326 News article ID | |
997 | // 327 News article data flavor | |
998 | ||
999 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
1000 | ||
1001 | var cat NewsCategoryListData15 | |
1002 | cats := cc.Server.ThreadedNews.Categories | |
1003 | ||
1004 | for _, path := range pathStrs { | |
1005 | cat = cats[path] | |
1006 | cats = cats[path].SubCats | |
1007 | } | |
1008 | newsArtID := t.GetField(fieldNewsArtID).Data | |
1009 | ||
1010 | convertedArtID := binary.BigEndian.Uint16(newsArtID) | |
1011 | ||
1012 | art := cat.Articles[uint32(convertedArtID)] | |
1013 | if art == nil { | |
1014 | res = append(res, cc.NewReply(t)) | |
1015 | return res, err | |
1016 | } | |
1017 | ||
1018 | // Reply fields | |
1019 | // 328 News article title | |
1020 | // 329 News article poster | |
1021 | // 330 News article date | |
1022 | // 331 Previous article ID | |
1023 | // 332 Next article ID | |
1024 | // 335 Parent article ID | |
1025 | // 336 First child article ID | |
1026 | // 327 News article data flavor "Should be “text/plain” | |
1027 | // 333 News article data Optional (if data flavor is “text/plain”) | |
1028 | ||
1029 | res = append(res, cc.NewReply(t, | |
1030 | NewField(fieldNewsArtTitle, []byte(art.Title)), | |
1031 | NewField(fieldNewsArtPoster, []byte(art.Poster)), | |
1032 | NewField(fieldNewsArtDate, art.Date), | |
1033 | NewField(fieldNewsArtPrevArt, art.PrevArt), | |
1034 | NewField(fieldNewsArtNextArt, art.NextArt), | |
1035 | NewField(fieldNewsArtParentArt, art.ParentArt), | |
1036 | NewField(fieldNewsArt1stChildArt, art.FirstChildArt), | |
1037 | NewField(fieldNewsArtDataFlav, []byte("text/plain")), | |
1038 | NewField(fieldNewsArtData, []byte(art.Data)), | |
1039 | )) | |
1040 | return res, err | |
1041 | } | |
1042 | ||
1043 | func HandleDelNewsItem(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1044 | // Access: News Delete Folder (37) or News Delete Category (35) | |
1045 | ||
1046 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
1047 | ||
1048 | // TODO: determine if path is a Folder (Bundle) or Category and check for permission | |
1049 | ||
1050 | cc.Server.Logger.Infof("DelNewsItem %v", pathStrs) | |
1051 | ||
1052 | cats := cc.Server.ThreadedNews.Categories | |
1053 | ||
1054 | delName := pathStrs[len(pathStrs)-1] | |
1055 | if len(pathStrs) > 1 { | |
1056 | for _, path := range pathStrs[0 : len(pathStrs)-1] { | |
1057 | cats = cats[path].SubCats | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | delete(cats, delName) | |
1062 | ||
1063 | err = cc.Server.writeThreadedNews() | |
1064 | if err != nil { | |
1065 | return res, err | |
1066 | } | |
1067 | ||
1068 | // Reply params: none | |
1069 | res = append(res, cc.NewReply(t)) | |
1070 | ||
1071 | return res, err | |
1072 | } | |
1073 | ||
1074 | func HandleDelNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1075 | // Request Fields | |
1076 | // 325 News path | |
1077 | // 326 News article ID | |
1078 | // 337 News article – recursive delete Delete child articles (1) or not (0) | |
1079 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
1080 | ID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data) | |
1081 | ||
1082 | // TODO: Delete recursive | |
1083 | cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1]) | |
1084 | ||
1085 | catName := pathStrs[len(pathStrs)-1] | |
1086 | cat := cats[catName] | |
1087 | ||
1088 | delete(cat.Articles, uint32(ID)) | |
1089 | ||
1090 | cats[catName] = cat | |
1091 | if err := cc.Server.writeThreadedNews(); err != nil { | |
1092 | return res, err | |
1093 | } | |
1094 | ||
1095 | res = append(res, cc.NewReply(t)) | |
1096 | return res, err | |
1097 | } | |
1098 | ||
1099 | func HandlePostNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1100 | // Request fields | |
1101 | // 325 News path | |
1102 | // 326 News article ID ID of the parent article? | |
1103 | // 328 News article title | |
1104 | // 334 News article flags | |
1105 | // 327 News article data flavor Currently “text/plain” | |
1106 | // 333 News article data | |
1107 | ||
1108 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
1109 | cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1]) | |
1110 | ||
1111 | catName := pathStrs[len(pathStrs)-1] | |
1112 | cat := cats[catName] | |
1113 | ||
1114 | newArt := NewsArtData{ | |
1115 | Title: string(t.GetField(fieldNewsArtTitle).Data), | |
72dd37f1 | 1116 | Poster: string(cc.UserName), |
6988a057 JH |
1117 | Date: NewsDate(), |
1118 | PrevArt: []byte{0, 0, 0, 0}, | |
1119 | NextArt: []byte{0, 0, 0, 0}, | |
1120 | ParentArt: append([]byte{0, 0}, t.GetField(fieldNewsArtID).Data...), | |
1121 | FirstChildArt: []byte{0, 0, 0, 0}, | |
1122 | DataFlav: []byte("text/plain"), | |
1123 | Data: string(t.GetField(fieldNewsArtData).Data), | |
1124 | } | |
1125 | ||
1126 | var keys []int | |
1127 | for k := range cat.Articles { | |
1128 | keys = append(keys, int(k)) | |
1129 | } | |
1130 | ||
1131 | nextID := uint32(1) | |
1132 | if len(keys) > 0 { | |
1133 | sort.Ints(keys) | |
1134 | prevID := uint32(keys[len(keys)-1]) | |
1135 | nextID = prevID + 1 | |
1136 | ||
1137 | binary.BigEndian.PutUint32(newArt.PrevArt, prevID) | |
1138 | ||
1139 | // Set next article ID | |
1140 | binary.BigEndian.PutUint32(cat.Articles[prevID].NextArt, nextID) | |
1141 | } | |
1142 | ||
1143 | // Update parent article with first child reply | |
1144 | parentID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data) | |
1145 | if parentID != 0 { | |
1146 | parentArt := cat.Articles[uint32(parentID)] | |
1147 | ||
1148 | if bytes.Equal(parentArt.FirstChildArt, []byte{0, 0, 0, 0}) { | |
1149 | binary.BigEndian.PutUint32(parentArt.FirstChildArt, nextID) | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | cat.Articles[nextID] = &newArt | |
1154 | ||
1155 | cats[catName] = cat | |
1156 | if err := cc.Server.writeThreadedNews(); err != nil { | |
1157 | return res, err | |
1158 | } | |
1159 | ||
1160 | res = append(res, cc.NewReply(t)) | |
1161 | return res, err | |
1162 | } | |
1163 | ||
1164 | // HandleGetMsgs returns the flat news data | |
1165 | func HandleGetMsgs(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1166 | res = append(res, cc.NewReply(t, NewField(fieldData, cc.Server.FlatNews))) | |
1167 | ||
1168 | return res, err | |
1169 | } | |
1170 | ||
1171 | func HandleDownloadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1172 | fileName := t.GetField(fieldFileName).Data | |
1173 | filePath := ReadFilePath(t.GetField(fieldFilePath).Data) | |
1174 | ||
1175 | ffo, err := NewFlattenedFileObject(cc.Server.Config.FileRoot+filePath, string(fileName)) | |
1176 | if err != nil { | |
1177 | return res, err | |
1178 | } | |
1179 | ||
1180 | transactionRef := cc.Server.NewTransactionRef() | |
1181 | data := binary.BigEndian.Uint32(transactionRef) | |
1182 | ||
1183 | cc.Server.Logger.Infow("File download", "path", filePath) | |
1184 | ||
1185 | ft := &FileTransfer{ | |
1186 | FileName: fileName, | |
1187 | FilePath: []byte(filePath), | |
1188 | ReferenceNumber: transactionRef, | |
1189 | Type: FileDownload, | |
1190 | } | |
1191 | ||
1192 | cc.Server.FileTransfers[data] = ft | |
1193 | cc.Transfers[FileDownload] = append(cc.Transfers[FileDownload], ft) | |
1194 | ||
1195 | res = append(res, cc.NewReply(t, | |
1196 | NewField(fieldRefNum, transactionRef), | |
1197 | NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count | |
1198 | NewField(fieldTransferSize, ffo.TransferSize()), | |
1199 | NewField(fieldFileSize, ffo.FlatFileDataForkHeader.DataSize), | |
1200 | )) | |
1201 | ||
1202 | return res, err | |
1203 | } | |
1204 | ||
1205 | // Download all files from the specified folder and sub-folders | |
1206 | // response example | |
1207 | // | |
1208 | // 00 | |
1209 | // 01 | |
1210 | // 00 00 | |
1211 | // 00 00 00 11 | |
1212 | // 00 00 00 00 | |
1213 | // 00 00 00 18 | |
1214 | // 00 00 00 18 | |
1215 | // | |
1216 | // 00 03 | |
1217 | // | |
1218 | // 00 6c // transfer size | |
1219 | // 00 04 // len | |
1220 | // 00 0f d5 ae | |
1221 | // | |
1222 | // 00 dc // field Folder item count | |
1223 | // 00 02 // len | |
1224 | // 00 02 | |
1225 | // | |
1226 | // 00 6b // ref number | |
1227 | // 00 04 // len | |
1228 | // 00 03 64 b1 | |
1229 | func HandleDownloadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1230 | transactionRef := cc.Server.NewTransactionRef() | |
1231 | data := binary.BigEndian.Uint32(transactionRef) | |
1232 | ||
1233 | fileTransfer := &FileTransfer{ | |
1234 | FileName: t.GetField(fieldFileName).Data, | |
1235 | FilePath: t.GetField(fieldFilePath).Data, | |
1236 | ReferenceNumber: transactionRef, | |
1237 | Type: FolderDownload, | |
1238 | } | |
1239 | cc.Server.FileTransfers[data] = fileTransfer | |
1240 | cc.Transfers[FolderDownload] = append(cc.Transfers[FolderDownload], fileTransfer) | |
1241 | ||
72dd37f1 | 1242 | var fp FilePath |
c5d9af5a JH |
1243 | err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data) |
1244 | if err != nil { | |
1245 | return res, err | |
1246 | } | |
6988a057 | 1247 | |
f6d08d0a | 1248 | fullFilePath := fmt.Sprintf("%v%v", cc.Server.Config.FileRoot+fp.String(), string(fileTransfer.FileName)) |
6988a057 JH |
1249 | transferSize, err := CalcTotalSize(fullFilePath) |
1250 | if err != nil { | |
1251 | return res, err | |
1252 | } | |
1253 | itemCount, err := CalcItemCount(fullFilePath) | |
1254 | if err != nil { | |
1255 | return res, err | |
1256 | } | |
1257 | res = append(res, cc.NewReply(t, | |
1258 | NewField(fieldRefNum, transactionRef), | |
1259 | NewField(fieldTransferSize, transferSize), | |
1260 | NewField(fieldFolderItemCount, itemCount), | |
1261 | NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count | |
1262 | )) | |
1263 | return res, err | |
1264 | } | |
1265 | ||
1266 | // Upload all files from the local folder and its subfolders to the specified path on the server | |
1267 | // Fields used in the request | |
1268 | // 201 File name | |
1269 | // 202 File path | |
df2735b2 | 1270 | // 108 transfer size Total size of all items in the folder |
6988a057 JH |
1271 | // 220 Folder item count |
1272 | // 204 File transfer options "Optional Currently set to 1" (TODO: ??) | |
1273 | func HandleUploadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1274 | transactionRef := cc.Server.NewTransactionRef() | |
1275 | data := binary.BigEndian.Uint32(transactionRef) | |
1276 | ||
1277 | fileTransfer := &FileTransfer{ | |
1278 | FileName: t.GetField(fieldFileName).Data, | |
1279 | FilePath: t.GetField(fieldFilePath).Data, | |
1280 | ReferenceNumber: transactionRef, | |
1281 | Type: FolderUpload, | |
1282 | FolderItemCount: t.GetField(fieldFolderItemCount).Data, | |
1283 | TransferSize: t.GetField(fieldTransferSize).Data, | |
1284 | } | |
1285 | cc.Server.FileTransfers[data] = fileTransfer | |
1286 | ||
1287 | res = append(res, cc.NewReply(t, NewField(fieldRefNum, transactionRef))) | |
1288 | return res, err | |
1289 | } | |
1290 | ||
1291 | func HandleUploadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1292 | fileName := t.GetField(fieldFileName).Data | |
1293 | filePath := t.GetField(fieldFilePath).Data | |
1294 | ||
1295 | transactionRef := cc.Server.NewTransactionRef() | |
1296 | data := binary.BigEndian.Uint32(transactionRef) | |
1297 | ||
1298 | fileTransfer := &FileTransfer{ | |
1299 | FileName: fileName, | |
1300 | FilePath: filePath, | |
1301 | ReferenceNumber: transactionRef, | |
1302 | Type: FileUpload, | |
1303 | } | |
1304 | ||
1305 | cc.Server.FileTransfers[data] = fileTransfer | |
1306 | ||
1307 | res = append(res, cc.NewReply(t, NewField(fieldRefNum, transactionRef))) | |
1308 | return res, err | |
1309 | } | |
1310 | ||
1311 | // User options | |
1312 | const ( | |
1313 | refusePM = 0 | |
1314 | refuseChat = 1 | |
1315 | autoResponse = 2 | |
1316 | ) | |
1317 | ||
1318 | func HandleSetClientUserInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1319 | var icon []byte | |
1320 | if len(t.GetField(fieldUserIconID).Data) == 4 { | |
1321 | icon = t.GetField(fieldUserIconID).Data[2:] | |
1322 | } else { | |
1323 | icon = t.GetField(fieldUserIconID).Data | |
1324 | } | |
1325 | *cc.Icon = icon | |
72dd37f1 | 1326 | cc.UserName = t.GetField(fieldUserName).Data |
6988a057 JH |
1327 | |
1328 | // the options field is only passed by the client versions > 1.2.3. | |
1329 | options := t.GetField(fieldOptions).Data | |
1330 | ||
1331 | if options != nil { | |
1332 | optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options))) | |
1333 | flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags))) | |
1334 | ||
1335 | // Check refuse private PM option | |
1336 | if optBitmap.Bit(refusePM) == 1 { | |
1337 | flagBitmap.SetBit(flagBitmap, userFlagRefusePM, 1) | |
1338 | binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64())) | |
1339 | } | |
1340 | ||
1341 | // Check refuse private chat option | |
1342 | if optBitmap.Bit(refuseChat) == 1 { | |
1343 | flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, 1) | |
1344 | binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64())) | |
1345 | } | |
1346 | ||
1347 | // Check auto response | |
1348 | if optBitmap.Bit(autoResponse) == 1 { | |
1349 | *cc.AutoReply = t.GetField(fieldAutomaticResponse).Data | |
1350 | } else { | |
1351 | *cc.AutoReply = []byte{} | |
1352 | } | |
1353 | } | |
1354 | ||
1355 | // Notify all clients of updated user info | |
1356 | cc.sendAll( | |
1357 | tranNotifyChangeUser, | |
1358 | NewField(fieldUserID, *cc.ID), | |
1359 | NewField(fieldUserIconID, *cc.Icon), | |
1360 | NewField(fieldUserFlags, *cc.Flags), | |
72dd37f1 | 1361 | NewField(fieldUserName, cc.UserName), |
6988a057 JH |
1362 | ) |
1363 | ||
1364 | return res, err | |
1365 | } | |
1366 | ||
1367 | // HandleKeepAlive response to keepalive transactions with an empty reply | |
1368 | // HL 1.9.2 Client sends keepalive msg every 3 minutes | |
1369 | // HL 1.2.3 Client doesn't send keepalives | |
1370 | func HandleKeepAlive(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1371 | res = append(res, cc.NewReply(t)) | |
1372 | ||
1373 | return res, err | |
1374 | } | |
1375 | ||
1376 | func HandleGetFileNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1377 | filePath := cc.Server.Config.FileRoot | |
1378 | ||
1379 | path := t.GetField(fieldFilePath).Data | |
1380 | if len(path) > 0 { | |
1381 | filePath = cc.Server.Config.FileRoot + ReadFilePath(path) | |
1382 | } | |
1383 | ||
1384 | fileNames, err := getFileNameList(filePath) | |
1385 | if err != nil { | |
1386 | return res, err | |
1387 | } | |
1388 | ||
1389 | res = append(res, cc.NewReply(t, fileNames...)) | |
1390 | ||
1391 | return res, err | |
1392 | } | |
1393 | ||
1394 | // ================================= | |
1395 | // Hotline private chat flow | |
1396 | // ================================= | |
1397 | // 1. ClientA sends tranInviteNewChat to server with user ID to invite | |
1398 | // 2. Server creates new ChatID | |
1399 | // 3. Server sends tranInviteToChat to invitee | |
1400 | // 4. Server replies to ClientA with new Chat ID | |
1401 | // | |
1402 | // A dialog box pops up in the invitee client with options to accept or decline the invitation. | |
1403 | // If Accepted is clicked: | |
1404 | // 1. ClientB sends tranJoinChat with fieldChatID | |
1405 | ||
1406 | // HandleInviteNewChat invites users to new private chat | |
1407 | func HandleInviteNewChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1408 | // Client to Invite | |
1409 | targetID := t.GetField(fieldUserID).Data | |
1410 | newChatID := cc.Server.NewPrivateChat(cc) | |
1411 | ||
1412 | res = append(res, | |
1413 | *NewTransaction( | |
1414 | tranInviteToChat, | |
1415 | &targetID, | |
1416 | NewField(fieldChatID, newChatID), | |
72dd37f1 | 1417 | NewField(fieldUserName, cc.UserName), |
6988a057 JH |
1418 | NewField(fieldUserID, *cc.ID), |
1419 | ), | |
1420 | ) | |
1421 | ||
1422 | res = append(res, | |
1423 | cc.NewReply(t, | |
1424 | NewField(fieldChatID, newChatID), | |
72dd37f1 | 1425 | NewField(fieldUserName, cc.UserName), |
6988a057 JH |
1426 | NewField(fieldUserID, *cc.ID), |
1427 | NewField(fieldUserIconID, *cc.Icon), | |
1428 | NewField(fieldUserFlags, *cc.Flags), | |
1429 | ), | |
1430 | ) | |
1431 | ||
1432 | return res, err | |
1433 | } | |
1434 | ||
1435 | func HandleInviteToChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1436 | // Client to Invite | |
1437 | targetID := t.GetField(fieldUserID).Data | |
1438 | chatID := t.GetField(fieldChatID).Data | |
1439 | ||
1440 | res = append(res, | |
1441 | *NewTransaction( | |
1442 | tranInviteToChat, | |
1443 | &targetID, | |
1444 | NewField(fieldChatID, chatID), | |
72dd37f1 | 1445 | NewField(fieldUserName, cc.UserName), |
6988a057 JH |
1446 | NewField(fieldUserID, *cc.ID), |
1447 | ), | |
1448 | ) | |
1449 | res = append(res, | |
1450 | cc.NewReply( | |
1451 | t, | |
1452 | NewField(fieldChatID, chatID), | |
72dd37f1 | 1453 | NewField(fieldUserName, cc.UserName), |
6988a057 JH |
1454 | NewField(fieldUserID, *cc.ID), |
1455 | NewField(fieldUserIconID, *cc.Icon), | |
1456 | NewField(fieldUserFlags, *cc.Flags), | |
1457 | ), | |
1458 | ) | |
1459 | ||
1460 | return res, err | |
1461 | } | |
1462 | ||
1463 | func HandleRejectChatInvite(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1464 | chatID := t.GetField(fieldChatID).Data | |
1465 | chatInt := binary.BigEndian.Uint32(chatID) | |
1466 | ||
1467 | privChat := cc.Server.PrivateChats[chatInt] | |
1468 | ||
72dd37f1 | 1469 | resMsg := append(cc.UserName, []byte(" declined invitation to chat")...) |
6988a057 JH |
1470 | |
1471 | for _, c := range sortedClients(privChat.ClientConn) { | |
1472 | res = append(res, | |
1473 | *NewTransaction( | |
1474 | tranChatMsg, | |
1475 | c.ID, | |
1476 | NewField(fieldChatID, chatID), | |
1477 | NewField(fieldData, resMsg), | |
1478 | ), | |
1479 | ) | |
1480 | } | |
1481 | ||
1482 | return res, err | |
1483 | } | |
1484 | ||
1485 | // HandleJoinChat is sent from a v1.8+ Hotline client when the joins a private chat | |
1486 | // Fields used in the reply: | |
1487 | // * 115 Chat subject | |
1488 | // * 300 User name with info (Optional) | |
1489 | // * 300 (more user names with info) | |
1490 | func HandleJoinChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1491 | chatID := t.GetField(fieldChatID).Data | |
1492 | chatInt := binary.BigEndian.Uint32(chatID) | |
1493 | ||
1494 | privChat := cc.Server.PrivateChats[chatInt] | |
1495 | ||
1496 | // Send tranNotifyChatChangeUser to current members of the chat to inform of new user | |
1497 | for _, c := range sortedClients(privChat.ClientConn) { | |
1498 | res = append(res, | |
1499 | *NewTransaction( | |
1500 | tranNotifyChatChangeUser, | |
1501 | c.ID, | |
1502 | NewField(fieldChatID, chatID), | |
72dd37f1 | 1503 | NewField(fieldUserName, cc.UserName), |
6988a057 JH |
1504 | NewField(fieldUserID, *cc.ID), |
1505 | NewField(fieldUserIconID, *cc.Icon), | |
1506 | NewField(fieldUserFlags, *cc.Flags), | |
1507 | ), | |
1508 | ) | |
1509 | } | |
1510 | ||
1511 | privChat.ClientConn[cc.uint16ID()] = cc | |
1512 | ||
1513 | replyFields := []Field{NewField(fieldChatSubject, []byte(privChat.Subject))} | |
1514 | for _, c := range sortedClients(privChat.ClientConn) { | |
1515 | user := User{ | |
1516 | ID: *c.ID, | |
1517 | Icon: *c.Icon, | |
1518 | Flags: *c.Flags, | |
72dd37f1 | 1519 | Name: string(c.UserName), |
6988a057 JH |
1520 | } |
1521 | ||
1522 | replyFields = append(replyFields, NewField(fieldUsernameWithInfo, user.Payload())) | |
1523 | } | |
1524 | ||
1525 | res = append(res, cc.NewReply(t, replyFields...)) | |
1526 | return res, err | |
1527 | } | |
1528 | ||
1529 | // HandleLeaveChat is sent from a v1.8+ Hotline client when the user exits a private chat | |
1530 | // Fields used in the request: | |
1531 | // * 114 fieldChatID | |
1532 | // Reply is not expected. | |
1533 | func HandleLeaveChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1534 | chatID := t.GetField(fieldChatID).Data | |
1535 | chatInt := binary.BigEndian.Uint32(chatID) | |
1536 | ||
1537 | privChat := cc.Server.PrivateChats[chatInt] | |
1538 | ||
1539 | delete(privChat.ClientConn, cc.uint16ID()) | |
1540 | ||
1541 | // Notify members of the private chat that the user has left | |
1542 | for _, c := range sortedClients(privChat.ClientConn) { | |
1543 | res = append(res, | |
1544 | *NewTransaction( | |
1545 | tranNotifyChatDeleteUser, | |
1546 | c.ID, | |
1547 | NewField(fieldChatID, chatID), | |
1548 | NewField(fieldUserID, *cc.ID), | |
1549 | ), | |
1550 | ) | |
1551 | } | |
1552 | ||
1553 | return res, err | |
1554 | } | |
1555 | ||
1556 | // HandleSetChatSubject is sent from a v1.8+ Hotline client when the user sets a private chat subject | |
1557 | // Fields used in the request: | |
1558 | // * 114 Chat ID | |
1559 | // * 115 Chat subject Chat subject string | |
1560 | // Reply is not expected. | |
1561 | func HandleSetChatSubject(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1562 | chatID := t.GetField(fieldChatID).Data | |
1563 | chatInt := binary.BigEndian.Uint32(chatID) | |
1564 | ||
1565 | privChat := cc.Server.PrivateChats[chatInt] | |
1566 | privChat.Subject = string(t.GetField(fieldChatSubject).Data) | |
1567 | ||
1568 | for _, c := range sortedClients(privChat.ClientConn) { | |
1569 | res = append(res, | |
1570 | *NewTransaction( | |
1571 | tranNotifyChatSubject, | |
1572 | c.ID, | |
1573 | NewField(fieldChatID, chatID), | |
1574 | NewField(fieldChatSubject, t.GetField(fieldChatSubject).Data), | |
1575 | ), | |
1576 | ) | |
1577 | } | |
1578 | ||
1579 | return res, err | |
1580 | } |