]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "bytes" | |
5 | "encoding/binary" | |
6 | "errors" | |
7 | "fmt" | |
8 | "gopkg.in/yaml.v3" | |
9 | "math/big" | |
10 | "os" | |
11 | "path" | |
12 | "path/filepath" | |
13 | "sort" | |
14 | "strings" | |
15 | "time" | |
16 | ) | |
17 | ||
18 | type TransactionType struct { | |
19 | Handler func(*ClientConn, *Transaction) ([]Transaction, error) // function for handling the transaction type | |
20 | Name string // Name of transaction as it will appear in logging | |
21 | RequiredFields []requiredField | |
22 | } | |
23 | ||
24 | var TransactionHandlers = map[uint16]TransactionType{ | |
25 | // Server initiated | |
26 | tranChatMsg: { | |
27 | Name: "tranChatMsg", | |
28 | }, | |
29 | // Server initiated | |
30 | tranNotifyChangeUser: { | |
31 | Name: "tranNotifyChangeUser", | |
32 | }, | |
33 | tranError: { | |
34 | Name: "tranError", | |
35 | }, | |
36 | tranShowAgreement: { | |
37 | Name: "tranShowAgreement", | |
38 | }, | |
39 | tranUserAccess: { | |
40 | Name: "tranUserAccess", | |
41 | }, | |
42 | tranNotifyDeleteUser: { | |
43 | Name: "tranNotifyDeleteUser", | |
44 | }, | |
45 | tranAgreed: { | |
46 | Name: "tranAgreed", | |
47 | Handler: HandleTranAgreed, | |
48 | }, | |
49 | tranChatSend: { | |
50 | Name: "tranChatSend", | |
51 | Handler: HandleChatSend, | |
52 | RequiredFields: []requiredField{ | |
53 | { | |
54 | ID: fieldData, | |
55 | minLen: 0, | |
56 | }, | |
57 | }, | |
58 | }, | |
59 | tranDelNewsArt: { | |
60 | Name: "tranDelNewsArt", | |
61 | Handler: HandleDelNewsArt, | |
62 | }, | |
63 | tranDelNewsItem: { | |
64 | Name: "tranDelNewsItem", | |
65 | Handler: HandleDelNewsItem, | |
66 | }, | |
67 | tranDeleteFile: { | |
68 | Name: "tranDeleteFile", | |
69 | Handler: HandleDeleteFile, | |
70 | }, | |
71 | tranDeleteUser: { | |
72 | Name: "tranDeleteUser", | |
73 | Handler: HandleDeleteUser, | |
74 | }, | |
75 | tranDisconnectUser: { | |
76 | Name: "tranDisconnectUser", | |
77 | Handler: HandleDisconnectUser, | |
78 | }, | |
79 | tranDownloadFile: { | |
80 | Name: "tranDownloadFile", | |
81 | Handler: HandleDownloadFile, | |
82 | }, | |
83 | tranDownloadFldr: { | |
84 | Name: "tranDownloadFldr", | |
85 | Handler: HandleDownloadFolder, | |
86 | }, | |
87 | tranGetClientInfoText: { | |
88 | Name: "tranGetClientInfoText", | |
89 | Handler: HandleGetClientInfoText, | |
90 | }, | |
91 | tranGetFileInfo: { | |
92 | Name: "tranGetFileInfo", | |
93 | Handler: HandleGetFileInfo, | |
94 | }, | |
95 | tranGetFileNameList: { | |
96 | Name: "tranGetFileNameList", | |
97 | Handler: HandleGetFileNameList, | |
98 | }, | |
99 | tranGetMsgs: { | |
100 | Name: "tranGetMsgs", | |
101 | Handler: HandleGetMsgs, | |
102 | }, | |
103 | tranGetNewsArtData: { | |
104 | Name: "tranGetNewsArtData", | |
105 | Handler: HandleGetNewsArtData, | |
106 | }, | |
107 | tranGetNewsArtNameList: { | |
108 | Name: "tranGetNewsArtNameList", | |
109 | Handler: HandleGetNewsArtNameList, | |
110 | }, | |
111 | tranGetNewsCatNameList: { | |
112 | Name: "tranGetNewsCatNameList", | |
113 | Handler: HandleGetNewsCatNameList, | |
114 | }, | |
115 | tranGetUser: { | |
116 | Name: "tranGetUser", | |
117 | Handler: HandleGetUser, | |
118 | }, | |
119 | tranGetUserNameList: { | |
120 | Name: "tranHandleGetUserNameList", | |
121 | Handler: HandleGetUserNameList, | |
122 | }, | |
123 | tranInviteNewChat: { | |
124 | Name: "tranInviteNewChat", | |
125 | Handler: HandleInviteNewChat, | |
126 | }, | |
127 | tranInviteToChat: { | |
128 | Name: "tranInviteToChat", | |
129 | Handler: HandleInviteToChat, | |
130 | }, | |
131 | tranJoinChat: { | |
132 | Name: "tranJoinChat", | |
133 | Handler: HandleJoinChat, | |
134 | }, | |
135 | tranKeepAlive: { | |
136 | Name: "tranKeepAlive", | |
137 | Handler: HandleKeepAlive, | |
138 | }, | |
139 | tranLeaveChat: { | |
140 | Name: "tranJoinChat", | |
141 | Handler: HandleLeaveChat, | |
142 | }, | |
143 | tranListUsers: { | |
144 | Name: "tranListUsers", | |
145 | Handler: HandleListUsers, | |
146 | }, | |
147 | tranMoveFile: { | |
148 | Name: "tranMoveFile", | |
149 | Handler: HandleMoveFile, | |
150 | }, | |
151 | tranNewFolder: { | |
152 | Name: "tranNewFolder", | |
153 | Handler: HandleNewFolder, | |
154 | }, | |
155 | tranNewNewsCat: { | |
156 | Name: "tranNewNewsCat", | |
157 | Handler: HandleNewNewsCat, | |
158 | }, | |
159 | tranNewNewsFldr: { | |
160 | Name: "tranNewNewsFldr", | |
161 | Handler: HandleNewNewsFldr, | |
162 | }, | |
163 | tranNewUser: { | |
164 | Name: "tranNewUser", | |
165 | Handler: HandleNewUser, | |
166 | }, | |
167 | tranUpdateUser: { | |
168 | Name: "tranUpdateUser", | |
169 | Handler: HandleUpdateUser, | |
170 | }, | |
171 | tranOldPostNews: { | |
172 | Name: "tranOldPostNews", | |
173 | Handler: HandleTranOldPostNews, | |
174 | }, | |
175 | tranPostNewsArt: { | |
176 | Name: "tranPostNewsArt", | |
177 | Handler: HandlePostNewsArt, | |
178 | }, | |
179 | tranRejectChatInvite: { | |
180 | Name: "tranRejectChatInvite", | |
181 | Handler: HandleRejectChatInvite, | |
182 | }, | |
183 | tranSendInstantMsg: { | |
184 | Name: "tranSendInstantMsg", | |
185 | Handler: HandleSendInstantMsg, | |
186 | RequiredFields: []requiredField{ | |
187 | { | |
188 | ID: fieldData, | |
189 | minLen: 0, | |
190 | }, | |
191 | { | |
192 | ID: fieldUserID, | |
193 | }, | |
194 | }, | |
195 | }, | |
196 | tranSetChatSubject: { | |
197 | Name: "tranSetChatSubject", | |
198 | Handler: HandleSetChatSubject, | |
199 | }, | |
200 | tranMakeFileAlias: { | |
201 | Name: "tranMakeFileAlias", | |
202 | Handler: HandleMakeAlias, | |
203 | RequiredFields: []requiredField{ | |
204 | {ID: fieldFileName, minLen: 1}, | |
205 | {ID: fieldFilePath, minLen: 1}, | |
206 | {ID: fieldFileNewPath, minLen: 1}, | |
207 | }, | |
208 | }, | |
209 | tranSetClientUserInfo: { | |
210 | Name: "tranSetClientUserInfo", | |
211 | Handler: HandleSetClientUserInfo, | |
212 | }, | |
213 | tranSetFileInfo: { | |
214 | Name: "tranSetFileInfo", | |
215 | Handler: HandleSetFileInfo, | |
216 | }, | |
217 | tranSetUser: { | |
218 | Name: "tranSetUser", | |
219 | Handler: HandleSetUser, | |
220 | }, | |
221 | tranUploadFile: { | |
222 | Name: "tranUploadFile", | |
223 | Handler: HandleUploadFile, | |
224 | }, | |
225 | tranUploadFldr: { | |
226 | Name: "tranUploadFldr", | |
227 | Handler: HandleUploadFolder, | |
228 | }, | |
229 | tranUserBroadcast: { | |
230 | Name: "tranUserBroadcast", | |
231 | Handler: HandleUserBroadcast, | |
232 | }, | |
233 | tranDownloadBanner: { | |
234 | Name: "tranDownloadBanner", | |
235 | Handler: HandleDownloadBanner, | |
236 | }, | |
237 | } | |
238 | ||
239 | func HandleChatSend(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
240 | if !cc.Authorize(accessSendChat) { | |
241 | res = append(res, cc.NewErrReply(t, "You are not allowed to participate in chat.")) | |
242 | return res, err | |
243 | } | |
244 | ||
245 | // Truncate long usernames | |
246 | trunc := fmt.Sprintf("%13s", cc.UserName) | |
247 | formattedMsg := fmt.Sprintf("\r%.14s: %s", trunc, t.GetField(fieldData).Data) | |
248 | ||
249 | // By holding the option key, Hotline chat allows users to send /me formatted messages like: | |
250 | // *** Halcyon does stuff | |
251 | // This is indicated by the presence of the optional field fieldChatOptions in the transaction payload | |
252 | if t.GetField(fieldChatOptions).Data != nil { | |
253 | formattedMsg = fmt.Sprintf("\r*** %s %s", cc.UserName, t.GetField(fieldData).Data) | |
254 | } | |
255 | ||
256 | chatID := t.GetField(fieldChatID).Data | |
257 | // a non-nil chatID indicates the message belongs to a private chat | |
258 | if chatID != nil { | |
259 | chatInt := binary.BigEndian.Uint32(chatID) | |
260 | privChat := cc.Server.PrivateChats[chatInt] | |
261 | ||
262 | clients := sortedClients(privChat.ClientConn) | |
263 | ||
264 | // send the message to all connected clients of the private chat | |
265 | for _, c := range clients { | |
266 | res = append(res, *NewTransaction( | |
267 | tranChatMsg, | |
268 | c.ID, | |
269 | NewField(fieldChatID, chatID), | |
270 | NewField(fieldData, []byte(formattedMsg)), | |
271 | )) | |
272 | } | |
273 | return res, err | |
274 | } | |
275 | ||
276 | for _, c := range sortedClients(cc.Server.Clients) { | |
277 | // Filter out clients that do not have the read chat permission | |
278 | if c.Authorize(accessReadChat) { | |
279 | res = append(res, *NewTransaction(tranChatMsg, c.ID, NewField(fieldData, []byte(formattedMsg)))) | |
280 | } | |
281 | } | |
282 | ||
283 | return res, err | |
284 | } | |
285 | ||
286 | // HandleSendInstantMsg sends instant message to the user on the current server. | |
287 | // Fields used in the request: | |
288 | // 103 User ID | |
289 | // 113 Options | |
290 | // One of the following values: | |
291 | // - User message (myOpt_UserMessage = 1) | |
292 | // - Refuse message (myOpt_RefuseMessage = 2) | |
293 | // - Refuse chat (myOpt_RefuseChat = 3) | |
294 | // - Automatic response (myOpt_AutomaticResponse = 4)" | |
295 | // 101 Data Optional | |
296 | // 214 Quoting message Optional | |
297 | // | |
298 | // Fields used in the reply: | |
299 | // None | |
300 | func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
301 | if !cc.Authorize(accessSendPrivMsg) { | |
302 | res = append(res, cc.NewErrReply(t, "You are not allowed to send private messages.")) | |
303 | return res, err | |
304 | } | |
305 | ||
306 | msg := t.GetField(fieldData) | |
307 | ID := t.GetField(fieldUserID) | |
308 | ||
309 | reply := NewTransaction( | |
310 | tranServerMsg, | |
311 | &ID.Data, | |
312 | NewField(fieldData, msg.Data), | |
313 | NewField(fieldUserName, cc.UserName), | |
314 | NewField(fieldUserID, *cc.ID), | |
315 | NewField(fieldOptions, []byte{0, 1}), | |
316 | ) | |
317 | ||
318 | // Later versions of Hotline include the original message in the fieldQuotingMsg field so | |
319 | // the receiving client can display both the received message and what it is in reply to | |
320 | if t.GetField(fieldQuotingMsg).Data != nil { | |
321 | reply.Fields = append(reply.Fields, NewField(fieldQuotingMsg, t.GetField(fieldQuotingMsg).Data)) | |
322 | } | |
323 | ||
324 | id, _ := byteToInt(ID.Data) | |
325 | otherClient, ok := cc.Server.Clients[uint16(id)] | |
326 | if !ok { | |
327 | return res, errors.New("invalid client ID") | |
328 | } | |
329 | ||
330 | // Check if target user has "Refuse private messages" flag | |
331 | flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(otherClient.Flags))) | |
332 | if flagBitmap.Bit(userFLagRefusePChat) == 1 { | |
333 | res = append(res, | |
334 | *NewTransaction( | |
335 | tranServerMsg, | |
336 | cc.ID, | |
337 | NewField(fieldData, []byte(string(otherClient.UserName)+" does not accept private messages.")), | |
338 | NewField(fieldUserName, otherClient.UserName), | |
339 | NewField(fieldUserID, *otherClient.ID), | |
340 | NewField(fieldOptions, []byte{0, 2}), | |
341 | ), | |
342 | ) | |
343 | } else { | |
344 | res = append(res, *reply) | |
345 | } | |
346 | ||
347 | // Respond with auto reply if other client has it enabled | |
348 | if len(otherClient.AutoReply) > 0 { | |
349 | res = append(res, | |
350 | *NewTransaction( | |
351 | tranServerMsg, | |
352 | cc.ID, | |
353 | NewField(fieldData, otherClient.AutoReply), | |
354 | NewField(fieldUserName, otherClient.UserName), | |
355 | NewField(fieldUserID, *otherClient.ID), | |
356 | NewField(fieldOptions, []byte{0, 1}), | |
357 | ), | |
358 | ) | |
359 | } | |
360 | ||
361 | res = append(res, cc.NewReply(t)) | |
362 | ||
363 | return res, err | |
364 | } | |
365 | ||
366 | func HandleGetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
367 | fileName := t.GetField(fieldFileName).Data | |
368 | filePath := t.GetField(fieldFilePath).Data | |
369 | ||
370 | fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName) | |
371 | if err != nil { | |
372 | return res, err | |
373 | } | |
374 | ||
375 | fw, err := newFileWrapper(cc.Server.FS, fullFilePath, 0) | |
376 | if err != nil { | |
377 | return res, err | |
378 | } | |
379 | ||
380 | res = append(res, cc.NewReply(t, | |
381 | NewField(fieldFileName, []byte(fw.name)), | |
382 | NewField(fieldFileTypeString, fw.ffo.FlatFileInformationFork.friendlyType()), | |
383 | NewField(fieldFileCreatorString, fw.ffo.FlatFileInformationFork.friendlyCreator()), | |
384 | NewField(fieldFileComment, fw.ffo.FlatFileInformationFork.Comment), | |
385 | NewField(fieldFileType, fw.ffo.FlatFileInformationFork.TypeSignature), | |
386 | NewField(fieldFileCreateDate, fw.ffo.FlatFileInformationFork.CreateDate), | |
387 | NewField(fieldFileModifyDate, fw.ffo.FlatFileInformationFork.ModifyDate), | |
388 | NewField(fieldFileSize, fw.totalSize()), | |
389 | )) | |
390 | return res, err | |
391 | } | |
392 | ||
393 | // HandleSetFileInfo updates a file or folder name and/or comment from the Get Info window | |
394 | // Fields used in the request: | |
395 | // * 201 File name | |
396 | // * 202 File path Optional | |
397 | // * 211 File new name Optional | |
398 | // * 210 File comment Optional | |
399 | // Fields used in the reply: None | |
400 | func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
401 | fileName := t.GetField(fieldFileName).Data | |
402 | filePath := t.GetField(fieldFilePath).Data | |
403 | ||
404 | fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName) | |
405 | if err != nil { | |
406 | return res, err | |
407 | } | |
408 | ||
409 | fi, err := cc.Server.FS.Stat(fullFilePath) | |
410 | if err != nil { | |
411 | return res, err | |
412 | } | |
413 | ||
414 | hlFile, err := newFileWrapper(cc.Server.FS, fullFilePath, 0) | |
415 | if err != nil { | |
416 | return res, err | |
417 | } | |
418 | if t.GetField(fieldFileComment).Data != nil { | |
419 | switch mode := fi.Mode(); { | |
420 | case mode.IsDir(): | |
421 | if !cc.Authorize(accessSetFolderComment) { | |
422 | res = append(res, cc.NewErrReply(t, "You are not allowed to set comments for folders.")) | |
423 | return res, err | |
424 | } | |
425 | case mode.IsRegular(): | |
426 | if !cc.Authorize(accessSetFileComment) { | |
427 | res = append(res, cc.NewErrReply(t, "You are not allowed to set comments for files.")) | |
428 | return res, err | |
429 | } | |
430 | } | |
431 | ||
432 | if err := hlFile.ffo.FlatFileInformationFork.setComment(t.GetField(fieldFileComment).Data); err != nil { | |
433 | return res, err | |
434 | } | |
435 | w, err := hlFile.infoForkWriter() | |
436 | if err != nil { | |
437 | return res, err | |
438 | } | |
439 | _, err = w.Write(hlFile.ffo.FlatFileInformationFork.MarshalBinary()) | |
440 | if err != nil { | |
441 | return res, err | |
442 | } | |
443 | } | |
444 | ||
445 | fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, t.GetField(fieldFileNewName).Data) | |
446 | if err != nil { | |
447 | return nil, err | |
448 | } | |
449 | ||
450 | fileNewName := t.GetField(fieldFileNewName).Data | |
451 | ||
452 | if fileNewName != nil { | |
453 | switch mode := fi.Mode(); { | |
454 | case mode.IsDir(): | |
455 | if !cc.Authorize(accessRenameFolder) { | |
456 | res = append(res, cc.NewErrReply(t, "You are not allowed to rename folders.")) | |
457 | return res, err | |
458 | } | |
459 | err = os.Rename(fullFilePath, fullNewFilePath) | |
460 | if os.IsNotExist(err) { | |
461 | res = append(res, cc.NewErrReply(t, "Cannot rename folder "+string(fileName)+" because it does not exist or cannot be found.")) | |
462 | return res, err | |
463 | } | |
464 | case mode.IsRegular(): | |
465 | if !cc.Authorize(accessRenameFile) { | |
466 | res = append(res, cc.NewErrReply(t, "You are not allowed to rename files.")) | |
467 | return res, err | |
468 | } | |
469 | fileDir, err := readPath(cc.Server.Config.FileRoot, filePath, []byte{}) | |
470 | if err != nil { | |
471 | return nil, err | |
472 | } | |
473 | hlFile.name = string(fileNewName) | |
474 | err = hlFile.move(fileDir) | |
475 | if os.IsNotExist(err) { | |
476 | res = append(res, cc.NewErrReply(t, "Cannot rename file "+string(fileName)+" because it does not exist or cannot be found.")) | |
477 | return res, err | |
478 | } | |
479 | if err != nil { | |
480 | return res, err | |
481 | } | |
482 | } | |
483 | } | |
484 | ||
485 | res = append(res, cc.NewReply(t)) | |
486 | return res, err | |
487 | } | |
488 | ||
489 | // HandleDeleteFile deletes a file or folder | |
490 | // Fields used in the request: | |
491 | // * 201 File name | |
492 | // * 202 File path | |
493 | // Fields used in the reply: none | |
494 | func HandleDeleteFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
495 | fileName := t.GetField(fieldFileName).Data | |
496 | filePath := t.GetField(fieldFilePath).Data | |
497 | ||
498 | fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName) | |
499 | if err != nil { | |
500 | return res, err | |
501 | } | |
502 | ||
503 | hlFile, err := newFileWrapper(cc.Server.FS, fullFilePath, 0) | |
504 | if err != nil { | |
505 | return res, err | |
506 | } | |
507 | ||
508 | fi, err := hlFile.dataFile() | |
509 | if err != nil { | |
510 | res = append(res, cc.NewErrReply(t, "Cannot delete file "+string(fileName)+" because it does not exist or cannot be found.")) | |
511 | return res, nil | |
512 | } | |
513 | ||
514 | switch mode := fi.Mode(); { | |
515 | case mode.IsDir(): | |
516 | if !cc.Authorize(accessDeleteFolder) { | |
517 | res = append(res, cc.NewErrReply(t, "You are not allowed to delete folders.")) | |
518 | return res, err | |
519 | } | |
520 | case mode.IsRegular(): | |
521 | if !cc.Authorize(accessDeleteFile) { | |
522 | res = append(res, cc.NewErrReply(t, "You are not allowed to delete files.")) | |
523 | return res, err | |
524 | } | |
525 | } | |
526 | ||
527 | if err := hlFile.delete(); err != nil { | |
528 | return res, err | |
529 | } | |
530 | ||
531 | res = append(res, cc.NewReply(t)) | |
532 | return res, err | |
533 | } | |
534 | ||
535 | // HandleMoveFile moves files or folders. Note: seemingly not documented | |
536 | func HandleMoveFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
537 | fileName := string(t.GetField(fieldFileName).Data) | |
538 | ||
539 | filePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFilePath).Data, t.GetField(fieldFileName).Data) | |
540 | if err != nil { | |
541 | return res, err | |
542 | } | |
543 | ||
544 | fileNewPath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFileNewPath).Data, nil) | |
545 | if err != nil { | |
546 | return res, err | |
547 | } | |
548 | ||
549 | cc.logger.Infow("Move file", "src", filePath+"/"+fileName, "dst", fileNewPath+"/"+fileName) | |
550 | ||
551 | hlFile, err := newFileWrapper(cc.Server.FS, filePath, 0) | |
552 | if err != nil { | |
553 | return res, err | |
554 | } | |
555 | ||
556 | fi, err := hlFile.dataFile() | |
557 | if err != nil { | |
558 | res = append(res, cc.NewErrReply(t, "Cannot delete file "+fileName+" because it does not exist or cannot be found.")) | |
559 | return res, err | |
560 | } | |
561 | if err != nil { | |
562 | return res, err | |
563 | } | |
564 | switch mode := fi.Mode(); { | |
565 | case mode.IsDir(): | |
566 | if !cc.Authorize(accessMoveFolder) { | |
567 | res = append(res, cc.NewErrReply(t, "You are not allowed to move folders.")) | |
568 | return res, err | |
569 | } | |
570 | case mode.IsRegular(): | |
571 | if !cc.Authorize(accessMoveFile) { | |
572 | res = append(res, cc.NewErrReply(t, "You are not allowed to move files.")) | |
573 | return res, err | |
574 | } | |
575 | } | |
576 | if err := hlFile.move(fileNewPath); err != nil { | |
577 | return res, err | |
578 | } | |
579 | // TODO: handle other possible errors; e.g. fileWrapper delete fails due to fileWrapper permission issue | |
580 | ||
581 | res = append(res, cc.NewReply(t)) | |
582 | return res, err | |
583 | } | |
584 | ||
585 | func HandleNewFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
586 | if !cc.Authorize(accessCreateFolder) { | |
587 | res = append(res, cc.NewErrReply(t, "You are not allowed to create folders.")) | |
588 | return res, err | |
589 | } | |
590 | folderName := string(t.GetField(fieldFileName).Data) | |
591 | ||
592 | folderName = path.Join("/", folderName) | |
593 | ||
594 | var subPath string | |
595 | ||
596 | // fieldFilePath is only present for nested paths | |
597 | if t.GetField(fieldFilePath).Data != nil { | |
598 | var newFp FilePath | |
599 | _, err := newFp.Write(t.GetField(fieldFilePath).Data) | |
600 | if err != nil { | |
601 | return nil, err | |
602 | } | |
603 | ||
604 | for _, pathItem := range newFp.Items { | |
605 | subPath = filepath.Join("/", subPath, string(pathItem.Name)) | |
606 | } | |
607 | } | |
608 | newFolderPath := path.Join(cc.Server.Config.FileRoot, subPath, folderName) | |
609 | ||
610 | // TODO: check path and folder name lengths | |
611 | ||
612 | if _, err := cc.Server.FS.Stat(newFolderPath); !os.IsNotExist(err) { | |
613 | msg := fmt.Sprintf("Cannot create folder \"%s\" because there is already a file or folder with that name.", folderName) | |
614 | return []Transaction{cc.NewErrReply(t, msg)}, nil | |
615 | } | |
616 | ||
617 | // TODO: check for disallowed characters to maintain compatibility for original client | |
618 | ||
619 | if err := cc.Server.FS.Mkdir(newFolderPath, 0777); err != nil { | |
620 | msg := fmt.Sprintf("Cannot create folder \"%s\" because an error occurred.", folderName) | |
621 | return []Transaction{cc.NewErrReply(t, msg)}, nil | |
622 | } | |
623 | ||
624 | res = append(res, cc.NewReply(t)) | |
625 | return res, err | |
626 | } | |
627 | ||
628 | func HandleSetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
629 | if !cc.Authorize(accessModifyUser) { | |
630 | res = append(res, cc.NewErrReply(t, "You are not allowed to modify accounts.")) | |
631 | return res, err | |
632 | } | |
633 | ||
634 | login := DecodeUserString(t.GetField(fieldUserLogin).Data) | |
635 | userName := string(t.GetField(fieldUserName).Data) | |
636 | ||
637 | newAccessLvl := t.GetField(fieldUserAccess).Data | |
638 | ||
639 | account := cc.Server.Accounts[login] | |
640 | account.Name = userName | |
641 | copy(account.Access[:], newAccessLvl) | |
642 | ||
643 | // If the password field is cleared in the Hotline edit user UI, the SetUser transaction does | |
644 | // not include fieldUserPassword | |
645 | if t.GetField(fieldUserPassword).Data == nil { | |
646 | account.Password = hashAndSalt([]byte("")) | |
647 | } | |
648 | if len(t.GetField(fieldUserPassword).Data) > 1 { | |
649 | account.Password = hashAndSalt(t.GetField(fieldUserPassword).Data) | |
650 | } | |
651 | ||
652 | out, err := yaml.Marshal(&account) | |
653 | if err != nil { | |
654 | return res, err | |
655 | } | |
656 | if err := os.WriteFile(filepath.Join(cc.Server.ConfigDir, "Users", login+".yaml"), out, 0666); err != nil { | |
657 | return res, err | |
658 | } | |
659 | ||
660 | // Notify connected clients logged in as the user of the new access level | |
661 | for _, c := range cc.Server.Clients { | |
662 | if c.Account.Login == login { | |
663 | // Note: comment out these two lines to test server-side deny messages | |
664 | newT := NewTransaction(tranUserAccess, c.ID, NewField(fieldUserAccess, newAccessLvl)) | |
665 | res = append(res, *newT) | |
666 | ||
667 | flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(c.Flags))) | |
668 | if c.Authorize(accessDisconUser) { | |
669 | flagBitmap.SetBit(flagBitmap, userFlagAdmin, 1) | |
670 | } else { | |
671 | flagBitmap.SetBit(flagBitmap, userFlagAdmin, 0) | |
672 | } | |
673 | binary.BigEndian.PutUint16(c.Flags, uint16(flagBitmap.Int64())) | |
674 | ||
675 | c.Account.Access = account.Access | |
676 | ||
677 | cc.sendAll( | |
678 | tranNotifyChangeUser, | |
679 | NewField(fieldUserID, *c.ID), | |
680 | NewField(fieldUserFlags, c.Flags), | |
681 | NewField(fieldUserName, c.UserName), | |
682 | NewField(fieldUserIconID, c.Icon), | |
683 | ) | |
684 | } | |
685 | } | |
686 | ||
687 | res = append(res, cc.NewReply(t)) | |
688 | return res, err | |
689 | } | |
690 | ||
691 | func HandleGetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
692 | if !cc.Authorize(accessOpenUser) { | |
693 | res = append(res, cc.NewErrReply(t, "You are not allowed to view accounts.")) | |
694 | return res, err | |
695 | } | |
696 | ||
697 | account := cc.Server.Accounts[string(t.GetField(fieldUserLogin).Data)] | |
698 | if account == nil { | |
699 | res = append(res, cc.NewErrReply(t, "Account does not exist.")) | |
700 | return res, err | |
701 | } | |
702 | ||
703 | res = append(res, cc.NewReply(t, | |
704 | NewField(fieldUserName, []byte(account.Name)), | |
705 | NewField(fieldUserLogin, negateString(t.GetField(fieldUserLogin).Data)), | |
706 | NewField(fieldUserPassword, []byte(account.Password)), | |
707 | NewField(fieldUserAccess, account.Access[:]), | |
708 | )) | |
709 | return res, err | |
710 | } | |
711 | ||
712 | func HandleListUsers(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
713 | if !cc.Authorize(accessOpenUser) { | |
714 | res = append(res, cc.NewErrReply(t, "You are not allowed to view accounts.")) | |
715 | return res, err | |
716 | } | |
717 | ||
718 | var userFields []Field | |
719 | for _, acc := range cc.Server.Accounts { | |
720 | b := make([]byte, 0, 100) | |
721 | n, err := acc.Read(b) | |
722 | if err != nil { | |
723 | return res, err | |
724 | } | |
725 | ||
726 | userFields = append(userFields, NewField(fieldData, b[:n])) | |
727 | } | |
728 | ||
729 | res = append(res, cc.NewReply(t, userFields...)) | |
730 | return res, err | |
731 | } | |
732 | ||
733 | // HandleUpdateUser is used by the v1.5+ multi-user editor to perform account editing for multiple users at a time. | |
734 | // An update can be a mix of these actions: | |
735 | // * Create user | |
736 | // * Delete user | |
737 | // * Modify user (including renaming the account login) | |
738 | // | |
739 | // The Transaction sent by the client includes one data field per user that was modified. This data field in turn | |
740 | // contains another data field encoded in its payload with a varying number of sub fields depending on which action is | |
741 | // performed. This seems to be the only place in the Hotline protocol where a data field contains another data field. | |
742 | func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
743 | for _, field := range t.Fields { | |
744 | subFields, err := ReadFields(field.Data[0:2], field.Data[2:]) | |
745 | if err != nil { | |
746 | return res, err | |
747 | } | |
748 | ||
749 | if len(subFields) == 1 { | |
750 | login := DecodeUserString(getField(fieldData, &subFields).Data) | |
751 | cc.logger.Infow("DeleteUser", "login", login) | |
752 | ||
753 | if !cc.Authorize(accessDeleteUser) { | |
754 | res = append(res, cc.NewErrReply(t, "You are not allowed to delete accounts.")) | |
755 | return res, err | |
756 | } | |
757 | ||
758 | if err := cc.Server.DeleteUser(login); err != nil { | |
759 | return res, err | |
760 | } | |
761 | continue | |
762 | } | |
763 | ||
764 | login := DecodeUserString(getField(fieldUserLogin, &subFields).Data) | |
765 | ||
766 | // check if the login dataFile; if so, we know we are updating an existing user | |
767 | if acc, ok := cc.Server.Accounts[login]; ok { | |
768 | cc.logger.Infow("UpdateUser", "login", login) | |
769 | ||
770 | // account dataFile, so this is an update action | |
771 | if !cc.Authorize(accessModifyUser) { | |
772 | res = append(res, cc.NewErrReply(t, "You are not allowed to modify accounts.")) | |
773 | return res, err | |
774 | } | |
775 | ||
776 | if getField(fieldUserPassword, &subFields) != nil { | |
777 | newPass := getField(fieldUserPassword, &subFields).Data | |
778 | acc.Password = hashAndSalt(newPass) | |
779 | } else { | |
780 | acc.Password = hashAndSalt([]byte("")) | |
781 | } | |
782 | ||
783 | if getField(fieldUserAccess, &subFields) != nil { | |
784 | copy(acc.Access[:], getField(fieldUserAccess, &subFields).Data) | |
785 | } | |
786 | ||
787 | err = cc.Server.UpdateUser( | |
788 | DecodeUserString(getField(fieldData, &subFields).Data), | |
789 | DecodeUserString(getField(fieldUserLogin, &subFields).Data), | |
790 | string(getField(fieldUserName, &subFields).Data), | |
791 | acc.Password, | |
792 | acc.Access, | |
793 | ) | |
794 | if err != nil { | |
795 | return res, err | |
796 | } | |
797 | } else { | |
798 | cc.logger.Infow("CreateUser", "login", login) | |
799 | ||
800 | if !cc.Authorize(accessCreateUser) { | |
801 | res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts.")) | |
802 | return res, err | |
803 | } | |
804 | ||
805 | newAccess := accessBitmap{} | |
806 | copy(newAccess[:], getField(fieldUserAccess, &subFields).Data[:]) | |
807 | ||
808 | // Prevent account from creating new account with greater permission | |
809 | for i := 0; i < 64; i++ { | |
810 | if newAccess.IsSet(i) { | |
811 | if !cc.Authorize(i) { | |
812 | return append(res, cc.NewErrReply(t, "Cannot create account with more access than yourself.")), err | |
813 | } | |
814 | } | |
815 | } | |
816 | ||
817 | err := cc.Server.NewUser(login, string(getField(fieldUserName, &subFields).Data), string(getField(fieldUserPassword, &subFields).Data), newAccess) | |
818 | if err != nil { | |
819 | return []Transaction{}, err | |
820 | } | |
821 | } | |
822 | } | |
823 | ||
824 | res = append(res, cc.NewReply(t)) | |
825 | return res, err | |
826 | } | |
827 | ||
828 | // HandleNewUser creates a new user account | |
829 | func HandleNewUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
830 | if !cc.Authorize(accessCreateUser) { | |
831 | res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts.")) | |
832 | return res, err | |
833 | } | |
834 | ||
835 | login := DecodeUserString(t.GetField(fieldUserLogin).Data) | |
836 | ||
837 | // If the account already dataFile, reply with an error | |
838 | if _, ok := cc.Server.Accounts[login]; ok { | |
839 | res = append(res, cc.NewErrReply(t, "Cannot create account "+login+" because there is already an account with that login.")) | |
840 | return res, err | |
841 | } | |
842 | ||
843 | newAccess := accessBitmap{} | |
844 | copy(newAccess[:], t.GetField(fieldUserAccess).Data[:]) | |
845 | ||
846 | // Prevent account from creating new account with greater permission | |
847 | for i := 0; i < 64; i++ { | |
848 | if newAccess.IsSet(i) { | |
849 | if !cc.Authorize(i) { | |
850 | res = append(res, cc.NewErrReply(t, "Cannot create account with more access than yourself.")) | |
851 | return res, err | |
852 | } | |
853 | } | |
854 | } | |
855 | ||
856 | if err := cc.Server.NewUser(login, string(t.GetField(fieldUserName).Data), string(t.GetField(fieldUserPassword).Data), newAccess); err != nil { | |
857 | return []Transaction{}, err | |
858 | } | |
859 | ||
860 | res = append(res, cc.NewReply(t)) | |
861 | return res, err | |
862 | } | |
863 | ||
864 | func HandleDeleteUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
865 | if !cc.Authorize(accessDeleteUser) { | |
866 | res = append(res, cc.NewErrReply(t, "You are not allowed to delete accounts.")) | |
867 | return res, err | |
868 | } | |
869 | ||
870 | // TODO: Handle case where account doesn't exist; e.g. delete race condition | |
871 | login := DecodeUserString(t.GetField(fieldUserLogin).Data) | |
872 | ||
873 | if err := cc.Server.DeleteUser(login); err != nil { | |
874 | return res, err | |
875 | } | |
876 | ||
877 | res = append(res, cc.NewReply(t)) | |
878 | return res, err | |
879 | } | |
880 | ||
881 | // HandleUserBroadcast sends an Administrator Message to all connected clients of the server | |
882 | func HandleUserBroadcast(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
883 | if !cc.Authorize(accessBroadcast) { | |
884 | res = append(res, cc.NewErrReply(t, "You are not allowed to send broadcast messages.")) | |
885 | return res, err | |
886 | } | |
887 | ||
888 | cc.sendAll( | |
889 | tranServerMsg, | |
890 | NewField(fieldData, t.GetField(tranGetMsgs).Data), | |
891 | NewField(fieldChatOptions, []byte{0}), | |
892 | ) | |
893 | ||
894 | res = append(res, cc.NewReply(t)) | |
895 | return res, err | |
896 | } | |
897 | ||
898 | func byteToInt(bytes []byte) (int, error) { | |
899 | switch len(bytes) { | |
900 | case 2: | |
901 | return int(binary.BigEndian.Uint16(bytes)), nil | |
902 | case 4: | |
903 | return int(binary.BigEndian.Uint32(bytes)), nil | |
904 | } | |
905 | ||
906 | return 0, errors.New("unknown byte length") | |
907 | } | |
908 | ||
909 | // HandleGetClientInfoText returns user information for the specific user. | |
910 | // | |
911 | // Fields used in the request: | |
912 | // 103 User ID | |
913 | // | |
914 | // Fields used in the reply: | |
915 | // 102 User name | |
916 | // 101 Data User info text string | |
917 | func HandleGetClientInfoText(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
918 | if !cc.Authorize(accessGetClientInfo) { | |
919 | res = append(res, cc.NewErrReply(t, "You are not allowed to get client info.")) | |
920 | return res, err | |
921 | } | |
922 | ||
923 | clientID, _ := byteToInt(t.GetField(fieldUserID).Data) | |
924 | ||
925 | clientConn := cc.Server.Clients[uint16(clientID)] | |
926 | if clientConn == nil { | |
927 | return append(res, cc.NewErrReply(t, "User not found.")), err | |
928 | } | |
929 | ||
930 | res = append(res, cc.NewReply(t, | |
931 | NewField(fieldData, []byte(clientConn.String())), | |
932 | NewField(fieldUserName, clientConn.UserName), | |
933 | )) | |
934 | return res, err | |
935 | } | |
936 | ||
937 | func HandleGetUserNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
938 | res = append(res, cc.NewReply(t, cc.Server.connectedUsers()...)) | |
939 | ||
940 | return res, err | |
941 | } | |
942 | ||
943 | func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
944 | cc.Agreed = true | |
945 | ||
946 | if t.GetField(fieldUserName).Data != nil { | |
947 | if cc.Authorize(accessAnyName) { | |
948 | cc.UserName = t.GetField(fieldUserName).Data | |
949 | } else { | |
950 | cc.UserName = []byte(cc.Account.Name) | |
951 | } | |
952 | } | |
953 | ||
954 | cc.Icon = t.GetField(fieldUserIconID).Data | |
955 | ||
956 | cc.logger = cc.logger.With("name", string(cc.UserName)) | |
957 | cc.logger.Infow("Login successful", "clientVersion", fmt.Sprintf("%v", func() int { i, _ := byteToInt(cc.Version); return i }())) | |
958 | ||
959 | options := t.GetField(fieldOptions).Data | |
960 | optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options))) | |
961 | ||
962 | flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(cc.Flags))) | |
963 | ||
964 | // Check refuse private PM option | |
965 | if optBitmap.Bit(refusePM) == 1 { | |
966 | flagBitmap.SetBit(flagBitmap, userFlagRefusePM, 1) | |
967 | binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64())) | |
968 | } | |
969 | ||
970 | // Check refuse private chat option | |
971 | if optBitmap.Bit(refuseChat) == 1 { | |
972 | flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, 1) | |
973 | binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64())) | |
974 | } | |
975 | ||
976 | // Check auto response | |
977 | if optBitmap.Bit(autoResponse) == 1 { | |
978 | cc.AutoReply = t.GetField(fieldAutomaticResponse).Data | |
979 | } else { | |
980 | cc.AutoReply = []byte{} | |
981 | } | |
982 | ||
983 | trans := cc.notifyOthers( | |
984 | *NewTransaction( | |
985 | tranNotifyChangeUser, nil, | |
986 | NewField(fieldUserName, cc.UserName), | |
987 | NewField(fieldUserID, *cc.ID), | |
988 | NewField(fieldUserIconID, cc.Icon), | |
989 | NewField(fieldUserFlags, cc.Flags), | |
990 | ), | |
991 | ) | |
992 | res = append(res, trans...) | |
993 | ||
994 | if cc.Server.Config.BannerFile != "" { | |
995 | res = append(res, *NewTransaction(tranServerBanner, cc.ID, NewField(fieldBannerType, []byte("JPEG")))) | |
996 | } | |
997 | ||
998 | res = append(res, cc.NewReply(t)) | |
999 | ||
1000 | return res, err | |
1001 | } | |
1002 | ||
1003 | const defaultNewsDateFormat = "Jan02 15:04" // Jun23 20:49 | |
1004 | // "Mon, 02 Jan 2006 15:04:05 MST" | |
1005 | ||
1006 | const defaultNewsTemplate = `From %s (%s): | |
1007 | ||
1008 | %s | |
1009 | ||
1010 | __________________________________________________________` | |
1011 | ||
1012 | // HandleTranOldPostNews updates the flat news | |
1013 | // Fields used in this request: | |
1014 | // 101 Data | |
1015 | func HandleTranOldPostNews(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1016 | if !cc.Authorize(accessNewsPostArt) { | |
1017 | res = append(res, cc.NewErrReply(t, "You are not allowed to post news.")) | |
1018 | return res, err | |
1019 | } | |
1020 | ||
1021 | cc.Server.flatNewsMux.Lock() | |
1022 | defer cc.Server.flatNewsMux.Unlock() | |
1023 | ||
1024 | newsDateTemplate := defaultNewsDateFormat | |
1025 | if cc.Server.Config.NewsDateFormat != "" { | |
1026 | newsDateTemplate = cc.Server.Config.NewsDateFormat | |
1027 | } | |
1028 | ||
1029 | newsTemplate := defaultNewsTemplate | |
1030 | if cc.Server.Config.NewsDelimiter != "" { | |
1031 | newsTemplate = cc.Server.Config.NewsDelimiter | |
1032 | } | |
1033 | ||
1034 | newsPost := fmt.Sprintf(newsTemplate+"\r", cc.UserName, time.Now().Format(newsDateTemplate), t.GetField(fieldData).Data) | |
1035 | newsPost = strings.Replace(newsPost, "\n", "\r", -1) | |
1036 | ||
1037 | // update news in memory | |
1038 | cc.Server.FlatNews = append([]byte(newsPost), cc.Server.FlatNews...) | |
1039 | ||
1040 | // update news on disk | |
1041 | if err := cc.Server.FS.WriteFile(filepath.Join(cc.Server.ConfigDir, "MessageBoard.txt"), cc.Server.FlatNews, 0644); err != nil { | |
1042 | return res, err | |
1043 | } | |
1044 | ||
1045 | // Notify all clients of updated news | |
1046 | cc.sendAll( | |
1047 | tranNewMsg, | |
1048 | NewField(fieldData, []byte(newsPost)), | |
1049 | ) | |
1050 | ||
1051 | res = append(res, cc.NewReply(t)) | |
1052 | return res, err | |
1053 | } | |
1054 | ||
1055 | func HandleDisconnectUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1056 | if !cc.Authorize(accessDisconUser) { | |
1057 | res = append(res, cc.NewErrReply(t, "You are not allowed to disconnect users.")) | |
1058 | return res, err | |
1059 | } | |
1060 | ||
1061 | clientConn := cc.Server.Clients[binary.BigEndian.Uint16(t.GetField(fieldUserID).Data)] | |
1062 | ||
1063 | if clientConn.Authorize(accessCannotBeDiscon) { | |
1064 | res = append(res, cc.NewErrReply(t, clientConn.Account.Login+" is not allowed to be disconnected.")) | |
1065 | return res, err | |
1066 | } | |
1067 | ||
1068 | // If fieldOptions is set, then the client IP is banned in addition to disconnected. | |
1069 | // 00 01 = temporary ban | |
1070 | // 00 02 = permanent ban | |
1071 | if t.GetField(fieldOptions).Data != nil { | |
1072 | switch t.GetField(fieldOptions).Data[1] { | |
1073 | case 1: | |
1074 | // send message: "You are temporarily banned on this server" | |
1075 | cc.logger.Infow("Disconnect & temporarily ban " + string(clientConn.UserName)) | |
1076 | ||
1077 | res = append(res, *NewTransaction( | |
1078 | tranServerMsg, | |
1079 | clientConn.ID, | |
1080 | NewField(fieldData, []byte("You are temporarily banned on this server")), | |
1081 | NewField(fieldChatOptions, []byte{0, 0}), | |
1082 | )) | |
1083 | ||
1084 | banUntil := time.Now().Add(tempBanDuration) | |
1085 | cc.Server.banList[strings.Split(clientConn.RemoteAddr, ":")[0]] = &banUntil | |
1086 | cc.Server.writeBanList() | |
1087 | case 2: | |
1088 | // send message: "You are permanently banned on this server" | |
1089 | cc.logger.Infow("Disconnect & ban " + string(clientConn.UserName)) | |
1090 | ||
1091 | res = append(res, *NewTransaction( | |
1092 | tranServerMsg, | |
1093 | clientConn.ID, | |
1094 | NewField(fieldData, []byte("You are permanently banned on this server")), | |
1095 | NewField(fieldChatOptions, []byte{0, 0}), | |
1096 | )) | |
1097 | ||
1098 | cc.Server.banList[strings.Split(clientConn.RemoteAddr, ":")[0]] = nil | |
1099 | cc.Server.writeBanList() | |
1100 | } | |
1101 | } | |
1102 | ||
1103 | // TODO: remove this awful hack | |
1104 | go func() { | |
1105 | time.Sleep(1 * time.Second) | |
1106 | clientConn.Disconnect() | |
1107 | }() | |
1108 | ||
1109 | return append(res, cc.NewReply(t)), err | |
1110 | } | |
1111 | ||
1112 | // HandleGetNewsCatNameList returns a list of news categories for a path | |
1113 | // Fields used in the request: | |
1114 | // 325 News path (Optional) | |
1115 | func HandleGetNewsCatNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1116 | if !cc.Authorize(accessNewsReadArt) { | |
1117 | res = append(res, cc.NewErrReply(t, "You are not allowed to read news.")) | |
1118 | return res, err | |
1119 | } | |
1120 | ||
1121 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
1122 | cats := cc.Server.GetNewsCatByPath(pathStrs) | |
1123 | ||
1124 | // To store the keys in slice in sorted order | |
1125 | keys := make([]string, len(cats)) | |
1126 | i := 0 | |
1127 | for k := range cats { | |
1128 | keys[i] = k | |
1129 | i++ | |
1130 | } | |
1131 | sort.Strings(keys) | |
1132 | ||
1133 | var fieldData []Field | |
1134 | for _, k := range keys { | |
1135 | cat := cats[k] | |
1136 | b, _ := cat.MarshalBinary() | |
1137 | fieldData = append(fieldData, NewField( | |
1138 | fieldNewsCatListData15, | |
1139 | b, | |
1140 | )) | |
1141 | } | |
1142 | ||
1143 | res = append(res, cc.NewReply(t, fieldData...)) | |
1144 | return res, err | |
1145 | } | |
1146 | ||
1147 | func HandleNewNewsCat(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1148 | if !cc.Authorize(accessNewsCreateCat) { | |
1149 | res = append(res, cc.NewErrReply(t, "You are not allowed to create news categories.")) | |
1150 | return res, err | |
1151 | } | |
1152 | ||
1153 | name := string(t.GetField(fieldNewsCatName).Data) | |
1154 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
1155 | ||
1156 | cats := cc.Server.GetNewsCatByPath(pathStrs) | |
1157 | cats[name] = NewsCategoryListData15{ | |
1158 | Name: name, | |
1159 | Type: []byte{0, 3}, | |
1160 | Articles: map[uint32]*NewsArtData{}, | |
1161 | SubCats: make(map[string]NewsCategoryListData15), | |
1162 | } | |
1163 | ||
1164 | if err := cc.Server.writeThreadedNews(); err != nil { | |
1165 | return res, err | |
1166 | } | |
1167 | res = append(res, cc.NewReply(t)) | |
1168 | return res, err | |
1169 | } | |
1170 | ||
1171 | // Fields used in the request: | |
1172 | // 322 News category name | |
1173 | // 325 News path | |
1174 | func HandleNewNewsFldr(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1175 | if !cc.Authorize(accessNewsCreateFldr) { | |
1176 | res = append(res, cc.NewErrReply(t, "You are not allowed to create news folders.")) | |
1177 | return res, err | |
1178 | } | |
1179 | ||
1180 | name := string(t.GetField(fieldFileName).Data) | |
1181 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
1182 | ||
1183 | cc.logger.Infof("Creating new news folder %s", name) | |
1184 | ||
1185 | cats := cc.Server.GetNewsCatByPath(pathStrs) | |
1186 | cats[name] = NewsCategoryListData15{ | |
1187 | Name: name, | |
1188 | Type: []byte{0, 2}, | |
1189 | Articles: map[uint32]*NewsArtData{}, | |
1190 | SubCats: make(map[string]NewsCategoryListData15), | |
1191 | } | |
1192 | if err := cc.Server.writeThreadedNews(); err != nil { | |
1193 | return res, err | |
1194 | } | |
1195 | res = append(res, cc.NewReply(t)) | |
1196 | return res, err | |
1197 | } | |
1198 | ||
1199 | // Fields used in the request: | |
1200 | // 325 News path Optional | |
1201 | // | |
1202 | // Reply fields: | |
1203 | // 321 News article list data Optional | |
1204 | func HandleGetNewsArtNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1205 | if !cc.Authorize(accessNewsReadArt) { | |
1206 | res = append(res, cc.NewErrReply(t, "You are not allowed to read news.")) | |
1207 | return res, err | |
1208 | } | |
1209 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
1210 | ||
1211 | var cat NewsCategoryListData15 | |
1212 | cats := cc.Server.ThreadedNews.Categories | |
1213 | ||
1214 | for _, fp := range pathStrs { | |
1215 | cat = cats[fp] | |
1216 | cats = cats[fp].SubCats | |
1217 | } | |
1218 | ||
1219 | nald := cat.GetNewsArtListData() | |
1220 | ||
1221 | res = append(res, cc.NewReply(t, NewField(fieldNewsArtListData, nald.Payload()))) | |
1222 | return res, err | |
1223 | } | |
1224 | ||
1225 | func HandleGetNewsArtData(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1226 | if !cc.Authorize(accessNewsReadArt) { | |
1227 | res = append(res, cc.NewErrReply(t, "You are not allowed to read news.")) | |
1228 | return res, err | |
1229 | } | |
1230 | ||
1231 | // Request fields | |
1232 | // 325 News fp | |
1233 | // 326 News article ID | |
1234 | // 327 News article data flavor | |
1235 | ||
1236 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
1237 | ||
1238 | var cat NewsCategoryListData15 | |
1239 | cats := cc.Server.ThreadedNews.Categories | |
1240 | ||
1241 | for _, fp := range pathStrs { | |
1242 | cat = cats[fp] | |
1243 | cats = cats[fp].SubCats | |
1244 | } | |
1245 | newsArtID := t.GetField(fieldNewsArtID).Data | |
1246 | ||
1247 | convertedArtID := binary.BigEndian.Uint16(newsArtID) | |
1248 | ||
1249 | art := cat.Articles[uint32(convertedArtID)] | |
1250 | if art == nil { | |
1251 | res = append(res, cc.NewReply(t)) | |
1252 | return res, err | |
1253 | } | |
1254 | ||
1255 | // Reply fields | |
1256 | // 328 News article title | |
1257 | // 329 News article poster | |
1258 | // 330 News article date | |
1259 | // 331 Previous article ID | |
1260 | // 332 Next article ID | |
1261 | // 335 Parent article ID | |
1262 | // 336 First child article ID | |
1263 | // 327 News article data flavor "Should be “text/plain” | |
1264 | // 333 News article data Optional (if data flavor is “text/plain”) | |
1265 | ||
1266 | res = append(res, cc.NewReply(t, | |
1267 | NewField(fieldNewsArtTitle, []byte(art.Title)), | |
1268 | NewField(fieldNewsArtPoster, []byte(art.Poster)), | |
1269 | NewField(fieldNewsArtDate, art.Date), | |
1270 | NewField(fieldNewsArtPrevArt, art.PrevArt), | |
1271 | NewField(fieldNewsArtNextArt, art.NextArt), | |
1272 | NewField(fieldNewsArtParentArt, art.ParentArt), | |
1273 | NewField(fieldNewsArt1stChildArt, art.FirstChildArt), | |
1274 | NewField(fieldNewsArtDataFlav, []byte("text/plain")), | |
1275 | NewField(fieldNewsArtData, []byte(art.Data)), | |
1276 | )) | |
1277 | return res, err | |
1278 | } | |
1279 | ||
1280 | // HandleDelNewsItem deletes an existing threaded news folder or category from the server. | |
1281 | // Fields used in the request: | |
1282 | // 325 News path | |
1283 | // Fields used in the reply: | |
1284 | // None | |
1285 | func HandleDelNewsItem(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1286 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
1287 | ||
1288 | cats := cc.Server.ThreadedNews.Categories | |
1289 | delName := pathStrs[len(pathStrs)-1] | |
1290 | if len(pathStrs) > 1 { | |
1291 | for _, fp := range pathStrs[0 : len(pathStrs)-1] { | |
1292 | cats = cats[fp].SubCats | |
1293 | } | |
1294 | } | |
1295 | ||
1296 | if bytes.Equal(cats[delName].Type, []byte{0, 3}) { | |
1297 | if !cc.Authorize(accessNewsDeleteCat) { | |
1298 | return append(res, cc.NewErrReply(t, "You are not allowed to delete news categories.")), nil | |
1299 | } | |
1300 | } else { | |
1301 | if !cc.Authorize(accessNewsDeleteFldr) { | |
1302 | return append(res, cc.NewErrReply(t, "You are not allowed to delete news folders.")), nil | |
1303 | } | |
1304 | } | |
1305 | ||
1306 | delete(cats, delName) | |
1307 | ||
1308 | if err := cc.Server.writeThreadedNews(); err != nil { | |
1309 | return res, err | |
1310 | } | |
1311 | ||
1312 | return append(res, cc.NewReply(t)), nil | |
1313 | } | |
1314 | ||
1315 | func HandleDelNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1316 | if !cc.Authorize(accessNewsDeleteArt) { | |
1317 | res = append(res, cc.NewErrReply(t, "You are not allowed to delete news articles.")) | |
1318 | return res, err | |
1319 | } | |
1320 | ||
1321 | // Request Fields | |
1322 | // 325 News path | |
1323 | // 326 News article ID | |
1324 | // 337 News article – recursive delete Delete child articles (1) or not (0) | |
1325 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
1326 | ID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data) | |
1327 | ||
1328 | // TODO: Delete recursive | |
1329 | cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1]) | |
1330 | ||
1331 | catName := pathStrs[len(pathStrs)-1] | |
1332 | cat := cats[catName] | |
1333 | ||
1334 | delete(cat.Articles, uint32(ID)) | |
1335 | ||
1336 | cats[catName] = cat | |
1337 | if err := cc.Server.writeThreadedNews(); err != nil { | |
1338 | return res, err | |
1339 | } | |
1340 | ||
1341 | res = append(res, cc.NewReply(t)) | |
1342 | return res, err | |
1343 | } | |
1344 | ||
1345 | // Request fields | |
1346 | // 325 News path | |
1347 | // 326 News article ID ID of the parent article? | |
1348 | // 328 News article title | |
1349 | // 334 News article flags | |
1350 | // 327 News article data flavor Currently “text/plain” | |
1351 | // 333 News article data | |
1352 | func HandlePostNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1353 | if !cc.Authorize(accessNewsPostArt) { | |
1354 | res = append(res, cc.NewErrReply(t, "You are not allowed to post news articles.")) | |
1355 | return res, err | |
1356 | } | |
1357 | ||
1358 | pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) | |
1359 | cats := cc.Server.GetNewsCatByPath(pathStrs[:len(pathStrs)-1]) | |
1360 | ||
1361 | catName := pathStrs[len(pathStrs)-1] | |
1362 | cat := cats[catName] | |
1363 | ||
1364 | newArt := NewsArtData{ | |
1365 | Title: string(t.GetField(fieldNewsArtTitle).Data), | |
1366 | Poster: string(cc.UserName), | |
1367 | Date: toHotlineTime(time.Now()), | |
1368 | PrevArt: []byte{0, 0, 0, 0}, | |
1369 | NextArt: []byte{0, 0, 0, 0}, | |
1370 | ParentArt: append([]byte{0, 0}, t.GetField(fieldNewsArtID).Data...), | |
1371 | FirstChildArt: []byte{0, 0, 0, 0}, | |
1372 | DataFlav: []byte("text/plain"), | |
1373 | Data: string(t.GetField(fieldNewsArtData).Data), | |
1374 | } | |
1375 | ||
1376 | var keys []int | |
1377 | for k := range cat.Articles { | |
1378 | keys = append(keys, int(k)) | |
1379 | } | |
1380 | ||
1381 | nextID := uint32(1) | |
1382 | if len(keys) > 0 { | |
1383 | sort.Ints(keys) | |
1384 | prevID := uint32(keys[len(keys)-1]) | |
1385 | nextID = prevID + 1 | |
1386 | ||
1387 | binary.BigEndian.PutUint32(newArt.PrevArt, prevID) | |
1388 | ||
1389 | // Set next article ID | |
1390 | binary.BigEndian.PutUint32(cat.Articles[prevID].NextArt, nextID) | |
1391 | } | |
1392 | ||
1393 | // Update parent article with first child reply | |
1394 | parentID := binary.BigEndian.Uint16(t.GetField(fieldNewsArtID).Data) | |
1395 | if parentID != 0 { | |
1396 | parentArt := cat.Articles[uint32(parentID)] | |
1397 | ||
1398 | if bytes.Equal(parentArt.FirstChildArt, []byte{0, 0, 0, 0}) { | |
1399 | binary.BigEndian.PutUint32(parentArt.FirstChildArt, nextID) | |
1400 | } | |
1401 | } | |
1402 | ||
1403 | cat.Articles[nextID] = &newArt | |
1404 | ||
1405 | cats[catName] = cat | |
1406 | if err := cc.Server.writeThreadedNews(); err != nil { | |
1407 | return res, err | |
1408 | } | |
1409 | ||
1410 | res = append(res, cc.NewReply(t)) | |
1411 | return res, err | |
1412 | } | |
1413 | ||
1414 | // HandleGetMsgs returns the flat news data | |
1415 | func HandleGetMsgs(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1416 | if !cc.Authorize(accessNewsReadArt) { | |
1417 | res = append(res, cc.NewErrReply(t, "You are not allowed to read news.")) | |
1418 | return res, err | |
1419 | } | |
1420 | ||
1421 | res = append(res, cc.NewReply(t, NewField(fieldData, cc.Server.FlatNews))) | |
1422 | ||
1423 | return res, err | |
1424 | } | |
1425 | ||
1426 | func HandleDownloadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1427 | if !cc.Authorize(accessDownloadFile) { | |
1428 | res = append(res, cc.NewErrReply(t, "You are not allowed to download files.")) | |
1429 | return res, err | |
1430 | } | |
1431 | ||
1432 | fileName := t.GetField(fieldFileName).Data | |
1433 | filePath := t.GetField(fieldFilePath).Data | |
1434 | resumeData := t.GetField(fieldFileResumeData).Data | |
1435 | ||
1436 | var dataOffset int64 | |
1437 | var frd FileResumeData | |
1438 | if resumeData != nil { | |
1439 | if err := frd.UnmarshalBinary(t.GetField(fieldFileResumeData).Data); err != nil { | |
1440 | return res, err | |
1441 | } | |
1442 | // TODO: handle rsrc fork offset | |
1443 | dataOffset = int64(binary.BigEndian.Uint32(frd.ForkInfoList[0].DataSize[:])) | |
1444 | } | |
1445 | ||
1446 | fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName) | |
1447 | if err != nil { | |
1448 | return res, err | |
1449 | } | |
1450 | ||
1451 | hlFile, err := newFileWrapper(cc.Server.FS, fullFilePath, dataOffset) | |
1452 | if err != nil { | |
1453 | return res, err | |
1454 | } | |
1455 | ||
1456 | xferSize := hlFile.ffo.TransferSize(0) | |
1457 | ||
1458 | ft := cc.newFileTransfer(FileDownload, fileName, filePath, xferSize) | |
1459 | ||
1460 | // TODO: refactor to remove this | |
1461 | if resumeData != nil { | |
1462 | var frd FileResumeData | |
1463 | if err := frd.UnmarshalBinary(t.GetField(fieldFileResumeData).Data); err != nil { | |
1464 | return res, err | |
1465 | } | |
1466 | ft.fileResumeData = &frd | |
1467 | } | |
1468 | ||
1469 | // Optional field for when a HL v1.5+ client requests file preview | |
1470 | // Used only for TEXT, JPEG, GIFF, BMP or PICT files | |
1471 | // The value will always be 2 | |
1472 | if t.GetField(fieldFileTransferOptions).Data != nil { | |
1473 | ft.options = t.GetField(fieldFileTransferOptions).Data | |
1474 | xferSize = hlFile.ffo.FlatFileDataForkHeader.DataSize[:] | |
1475 | } | |
1476 | ||
1477 | res = append(res, cc.NewReply(t, | |
1478 | NewField(fieldRefNum, ft.refNum[:]), | |
1479 | NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count | |
1480 | NewField(fieldTransferSize, xferSize), | |
1481 | NewField(fieldFileSize, hlFile.ffo.FlatFileDataForkHeader.DataSize[:]), | |
1482 | )) | |
1483 | ||
1484 | return res, err | |
1485 | } | |
1486 | ||
1487 | // Download all files from the specified folder and sub-folders | |
1488 | func HandleDownloadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1489 | if !cc.Authorize(accessDownloadFile) { | |
1490 | res = append(res, cc.NewErrReply(t, "You are not allowed to download folders.")) | |
1491 | return res, err | |
1492 | } | |
1493 | ||
1494 | fullFilePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFilePath).Data, t.GetField(fieldFileName).Data) | |
1495 | if err != nil { | |
1496 | return res, err | |
1497 | } | |
1498 | ||
1499 | transferSize, err := CalcTotalSize(fullFilePath) | |
1500 | if err != nil { | |
1501 | return res, err | |
1502 | } | |
1503 | itemCount, err := CalcItemCount(fullFilePath) | |
1504 | if err != nil { | |
1505 | return res, err | |
1506 | } | |
1507 | ||
1508 | fileTransfer := cc.newFileTransfer(FolderDownload, t.GetField(fieldFileName).Data, t.GetField(fieldFilePath).Data, transferSize) | |
1509 | ||
1510 | var fp FilePath | |
1511 | _, err = fp.Write(t.GetField(fieldFilePath).Data) | |
1512 | if err != nil { | |
1513 | return res, err | |
1514 | } | |
1515 | ||
1516 | res = append(res, cc.NewReply(t, | |
1517 | NewField(fieldRefNum, fileTransfer.ReferenceNumber), | |
1518 | NewField(fieldTransferSize, transferSize), | |
1519 | NewField(fieldFolderItemCount, itemCount), | |
1520 | NewField(fieldWaitingCount, []byte{0x00, 0x00}), // TODO: Implement waiting count | |
1521 | )) | |
1522 | return res, err | |
1523 | } | |
1524 | ||
1525 | // Upload all files from the local folder and its subfolders to the specified path on the server | |
1526 | // Fields used in the request | |
1527 | // 201 File name | |
1528 | // 202 File path | |
1529 | // 108 transfer size Total size of all items in the folder | |
1530 | // 220 Folder item count | |
1531 | // 204 File transfer options "Optional Currently set to 1" (TODO: ??) | |
1532 | func HandleUploadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1533 | var fp FilePath | |
1534 | if t.GetField(fieldFilePath).Data != nil { | |
1535 | if _, err = fp.Write(t.GetField(fieldFilePath).Data); err != nil { | |
1536 | return res, err | |
1537 | } | |
1538 | } | |
1539 | ||
1540 | // Handle special cases for Upload and Drop Box folders | |
1541 | if !cc.Authorize(accessUploadAnywhere) { | |
1542 | if !fp.IsUploadDir() && !fp.IsDropbox() { | |
1543 | res = append(res, cc.NewErrReply(t, fmt.Sprintf("Cannot accept upload of the folder \"%v\" because you are only allowed to upload to the \"Uploads\" folder.", string(t.GetField(fieldFileName).Data)))) | |
1544 | return res, err | |
1545 | } | |
1546 | } | |
1547 | ||
1548 | fileTransfer := cc.newFileTransfer(FolderUpload, | |
1549 | t.GetField(fieldFileName).Data, | |
1550 | t.GetField(fieldFilePath).Data, | |
1551 | t.GetField(fieldTransferSize).Data, | |
1552 | ) | |
1553 | ||
1554 | fileTransfer.FolderItemCount = t.GetField(fieldFolderItemCount).Data | |
1555 | ||
1556 | res = append(res, cc.NewReply(t, NewField(fieldRefNum, fileTransfer.ReferenceNumber))) | |
1557 | return res, err | |
1558 | } | |
1559 | ||
1560 | // HandleUploadFile | |
1561 | // Fields used in the request: | |
1562 | // 201 File name | |
1563 | // 202 File path | |
1564 | // 204 File transfer options "Optional | |
1565 | // Used only to resume download, currently has value 2" | |
1566 | // 108 File transfer size "Optional used if download is not resumed" | |
1567 | func HandleUploadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1568 | if !cc.Authorize(accessUploadFile) { | |
1569 | res = append(res, cc.NewErrReply(t, "You are not allowed to upload files.")) | |
1570 | return res, err | |
1571 | } | |
1572 | ||
1573 | fileName := t.GetField(fieldFileName).Data | |
1574 | filePath := t.GetField(fieldFilePath).Data | |
1575 | transferOptions := t.GetField(fieldFileTransferOptions).Data | |
1576 | transferSize := t.GetField(fieldTransferSize).Data // not sent for resume | |
1577 | ||
1578 | var fp FilePath | |
1579 | if filePath != nil { | |
1580 | if _, err = fp.Write(filePath); err != nil { | |
1581 | return res, err | |
1582 | } | |
1583 | } | |
1584 | ||
1585 | // Handle special cases for Upload and Drop Box folders | |
1586 | if !cc.Authorize(accessUploadAnywhere) { | |
1587 | if !fp.IsUploadDir() && !fp.IsDropbox() { | |
1588 | res = append(res, cc.NewErrReply(t, fmt.Sprintf("Cannot accept upload of the file \"%v\" because you are only allowed to upload to the \"Uploads\" folder.", string(fileName)))) | |
1589 | return res, err | |
1590 | } | |
1591 | } | |
1592 | fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName) | |
1593 | if err != nil { | |
1594 | return res, err | |
1595 | } | |
1596 | ||
1597 | if _, err := cc.Server.FS.Stat(fullFilePath); err == nil { | |
1598 | res = append(res, cc.NewErrReply(t, fmt.Sprintf("Cannot accept upload because there is already a file named \"%v\". Try choosing a different name.", string(fileName)))) | |
1599 | return res, err | |
1600 | } | |
1601 | ||
1602 | ft := cc.newFileTransfer(FileUpload, fileName, filePath, transferSize) | |
1603 | ||
1604 | replyT := cc.NewReply(t, NewField(fieldRefNum, ft.ReferenceNumber)) | |
1605 | ||
1606 | // client has requested to resume a partially transferred file | |
1607 | if transferOptions != nil { | |
1608 | ||
1609 | fileInfo, err := cc.Server.FS.Stat(fullFilePath + incompleteFileSuffix) | |
1610 | if err != nil { | |
1611 | return res, err | |
1612 | } | |
1613 | ||
1614 | offset := make([]byte, 4) | |
1615 | binary.BigEndian.PutUint32(offset, uint32(fileInfo.Size())) | |
1616 | ||
1617 | fileResumeData := NewFileResumeData([]ForkInfoList{ | |
1618 | *NewForkInfoList(offset), | |
1619 | }) | |
1620 | ||
1621 | b, _ := fileResumeData.BinaryMarshal() | |
1622 | ||
1623 | ft.TransferSize = offset | |
1624 | ||
1625 | replyT.Fields = append(replyT.Fields, NewField(fieldFileResumeData, b)) | |
1626 | } | |
1627 | ||
1628 | res = append(res, replyT) | |
1629 | return res, err | |
1630 | } | |
1631 | ||
1632 | func HandleSetClientUserInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1633 | if len(t.GetField(fieldUserIconID).Data) == 4 { | |
1634 | cc.Icon = t.GetField(fieldUserIconID).Data[2:] | |
1635 | } else { | |
1636 | cc.Icon = t.GetField(fieldUserIconID).Data | |
1637 | } | |
1638 | if cc.Authorize(accessAnyName) { | |
1639 | cc.UserName = t.GetField(fieldUserName).Data | |
1640 | } | |
1641 | ||
1642 | // the options field is only passed by the client versions > 1.2.3. | |
1643 | options := t.GetField(fieldOptions).Data | |
1644 | if options != nil { | |
1645 | optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options))) | |
1646 | flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(cc.Flags))) | |
1647 | ||
1648 | flagBitmap.SetBit(flagBitmap, userFlagRefusePM, optBitmap.Bit(refusePM)) | |
1649 | binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64())) | |
1650 | ||
1651 | flagBitmap.SetBit(flagBitmap, userFLagRefusePChat, optBitmap.Bit(refuseChat)) | |
1652 | binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64())) | |
1653 | ||
1654 | // Check auto response | |
1655 | if optBitmap.Bit(autoResponse) == 1 { | |
1656 | cc.AutoReply = t.GetField(fieldAutomaticResponse).Data | |
1657 | } else { | |
1658 | cc.AutoReply = []byte{} | |
1659 | } | |
1660 | } | |
1661 | ||
1662 | for _, c := range sortedClients(cc.Server.Clients) { | |
1663 | res = append(res, *NewTransaction( | |
1664 | tranNotifyChangeUser, | |
1665 | c.ID, | |
1666 | NewField(fieldUserID, *cc.ID), | |
1667 | NewField(fieldUserIconID, cc.Icon), | |
1668 | NewField(fieldUserFlags, cc.Flags), | |
1669 | NewField(fieldUserName, cc.UserName), | |
1670 | )) | |
1671 | } | |
1672 | ||
1673 | return res, err | |
1674 | } | |
1675 | ||
1676 | // HandleKeepAlive responds to keepalive transactions with an empty reply | |
1677 | // * HL 1.9.2 Client sends keepalive msg every 3 minutes | |
1678 | // * HL 1.2.3 Client doesn't send keepalives | |
1679 | func HandleKeepAlive(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1680 | res = append(res, cc.NewReply(t)) | |
1681 | ||
1682 | return res, err | |
1683 | } | |
1684 | ||
1685 | func HandleGetFileNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1686 | fullPath, err := readPath( | |
1687 | cc.Server.Config.FileRoot, | |
1688 | t.GetField(fieldFilePath).Data, | |
1689 | nil, | |
1690 | ) | |
1691 | if err != nil { | |
1692 | return res, err | |
1693 | } | |
1694 | ||
1695 | var fp FilePath | |
1696 | if t.GetField(fieldFilePath).Data != nil { | |
1697 | if _, err = fp.Write(t.GetField(fieldFilePath).Data); err != nil { | |
1698 | return res, err | |
1699 | } | |
1700 | } | |
1701 | ||
1702 | // Handle special case for drop box folders | |
1703 | if fp.IsDropbox() && !cc.Authorize(accessViewDropBoxes) { | |
1704 | res = append(res, cc.NewErrReply(t, "You are not allowed to view drop boxes.")) | |
1705 | return res, err | |
1706 | } | |
1707 | ||
1708 | fileNames, err := getFileNameList(fullPath, cc.Server.Config.IgnoreFiles) | |
1709 | if err != nil { | |
1710 | return res, err | |
1711 | } | |
1712 | ||
1713 | res = append(res, cc.NewReply(t, fileNames...)) | |
1714 | ||
1715 | return res, err | |
1716 | } | |
1717 | ||
1718 | // ================================= | |
1719 | // Hotline private chat flow | |
1720 | // ================================= | |
1721 | // 1. ClientA sends tranInviteNewChat to server with user ID to invite | |
1722 | // 2. Server creates new ChatID | |
1723 | // 3. Server sends tranInviteToChat to invitee | |
1724 | // 4. Server replies to ClientA with new Chat ID | |
1725 | // | |
1726 | // A dialog box pops up in the invitee client with options to accept or decline the invitation. | |
1727 | // If Accepted is clicked: | |
1728 | // 1. ClientB sends tranJoinChat with fieldChatID | |
1729 | ||
1730 | // HandleInviteNewChat invites users to new private chat | |
1731 | func HandleInviteNewChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1732 | if !cc.Authorize(accessOpenChat) { | |
1733 | res = append(res, cc.NewErrReply(t, "You are not allowed to request private chat.")) | |
1734 | return res, err | |
1735 | } | |
1736 | ||
1737 | // Client to Invite | |
1738 | targetID := t.GetField(fieldUserID).Data | |
1739 | newChatID := cc.Server.NewPrivateChat(cc) | |
1740 | ||
1741 | // Check if target user has "Refuse private chat" flag | |
1742 | binary.BigEndian.Uint16(targetID) | |
1743 | targetClient := cc.Server.Clients[binary.BigEndian.Uint16(targetID)] | |
1744 | ||
1745 | flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(targetClient.Flags))) | |
1746 | if flagBitmap.Bit(userFLagRefusePChat) == 1 { | |
1747 | res = append(res, | |
1748 | *NewTransaction( | |
1749 | tranServerMsg, | |
1750 | cc.ID, | |
1751 | NewField(fieldData, []byte(string(targetClient.UserName)+" does not accept private chats.")), | |
1752 | NewField(fieldUserName, targetClient.UserName), | |
1753 | NewField(fieldUserID, *targetClient.ID), | |
1754 | NewField(fieldOptions, []byte{0, 2}), | |
1755 | ), | |
1756 | ) | |
1757 | } else { | |
1758 | res = append(res, | |
1759 | *NewTransaction( | |
1760 | tranInviteToChat, | |
1761 | &targetID, | |
1762 | NewField(fieldChatID, newChatID), | |
1763 | NewField(fieldUserName, cc.UserName), | |
1764 | NewField(fieldUserID, *cc.ID), | |
1765 | ), | |
1766 | ) | |
1767 | } | |
1768 | ||
1769 | res = append(res, | |
1770 | cc.NewReply(t, | |
1771 | NewField(fieldChatID, newChatID), | |
1772 | NewField(fieldUserName, cc.UserName), | |
1773 | NewField(fieldUserID, *cc.ID), | |
1774 | NewField(fieldUserIconID, cc.Icon), | |
1775 | NewField(fieldUserFlags, cc.Flags), | |
1776 | ), | |
1777 | ) | |
1778 | ||
1779 | return res, err | |
1780 | } | |
1781 | ||
1782 | func HandleInviteToChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1783 | if !cc.Authorize(accessOpenChat) { | |
1784 | res = append(res, cc.NewErrReply(t, "You are not allowed to request private chat.")) | |
1785 | return res, err | |
1786 | } | |
1787 | ||
1788 | // Client to Invite | |
1789 | targetID := t.GetField(fieldUserID).Data | |
1790 | chatID := t.GetField(fieldChatID).Data | |
1791 | ||
1792 | res = append(res, | |
1793 | *NewTransaction( | |
1794 | tranInviteToChat, | |
1795 | &targetID, | |
1796 | NewField(fieldChatID, chatID), | |
1797 | NewField(fieldUserName, cc.UserName), | |
1798 | NewField(fieldUserID, *cc.ID), | |
1799 | ), | |
1800 | ) | |
1801 | res = append(res, | |
1802 | cc.NewReply( | |
1803 | t, | |
1804 | NewField(fieldChatID, chatID), | |
1805 | NewField(fieldUserName, cc.UserName), | |
1806 | NewField(fieldUserID, *cc.ID), | |
1807 | NewField(fieldUserIconID, cc.Icon), | |
1808 | NewField(fieldUserFlags, cc.Flags), | |
1809 | ), | |
1810 | ) | |
1811 | ||
1812 | return res, err | |
1813 | } | |
1814 | ||
1815 | func HandleRejectChatInvite(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1816 | chatID := t.GetField(fieldChatID).Data | |
1817 | chatInt := binary.BigEndian.Uint32(chatID) | |
1818 | ||
1819 | privChat := cc.Server.PrivateChats[chatInt] | |
1820 | ||
1821 | resMsg := append(cc.UserName, []byte(" declined invitation to chat")...) | |
1822 | ||
1823 | for _, c := range sortedClients(privChat.ClientConn) { | |
1824 | res = append(res, | |
1825 | *NewTransaction( | |
1826 | tranChatMsg, | |
1827 | c.ID, | |
1828 | NewField(fieldChatID, chatID), | |
1829 | NewField(fieldData, resMsg), | |
1830 | ), | |
1831 | ) | |
1832 | } | |
1833 | ||
1834 | return res, err | |
1835 | } | |
1836 | ||
1837 | // HandleJoinChat is sent from a v1.8+ Hotline client when the joins a private chat | |
1838 | // Fields used in the reply: | |
1839 | // * 115 Chat subject | |
1840 | // * 300 User name with info (Optional) | |
1841 | // * 300 (more user names with info) | |
1842 | func HandleJoinChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1843 | chatID := t.GetField(fieldChatID).Data | |
1844 | chatInt := binary.BigEndian.Uint32(chatID) | |
1845 | ||
1846 | privChat := cc.Server.PrivateChats[chatInt] | |
1847 | ||
1848 | // Send tranNotifyChatChangeUser to current members of the chat to inform of new user | |
1849 | for _, c := range sortedClients(privChat.ClientConn) { | |
1850 | res = append(res, | |
1851 | *NewTransaction( | |
1852 | tranNotifyChatChangeUser, | |
1853 | c.ID, | |
1854 | NewField(fieldChatID, chatID), | |
1855 | NewField(fieldUserName, cc.UserName), | |
1856 | NewField(fieldUserID, *cc.ID), | |
1857 | NewField(fieldUserIconID, cc.Icon), | |
1858 | NewField(fieldUserFlags, cc.Flags), | |
1859 | ), | |
1860 | ) | |
1861 | } | |
1862 | ||
1863 | privChat.ClientConn[cc.uint16ID()] = cc | |
1864 | ||
1865 | replyFields := []Field{NewField(fieldChatSubject, []byte(privChat.Subject))} | |
1866 | for _, c := range sortedClients(privChat.ClientConn) { | |
1867 | user := User{ | |
1868 | ID: *c.ID, | |
1869 | Icon: c.Icon, | |
1870 | Flags: c.Flags, | |
1871 | Name: string(c.UserName), | |
1872 | } | |
1873 | ||
1874 | replyFields = append(replyFields, NewField(fieldUsernameWithInfo, user.Payload())) | |
1875 | } | |
1876 | ||
1877 | res = append(res, cc.NewReply(t, replyFields...)) | |
1878 | return res, err | |
1879 | } | |
1880 | ||
1881 | // HandleLeaveChat is sent from a v1.8+ Hotline client when the user exits a private chat | |
1882 | // Fields used in the request: | |
1883 | // * 114 fieldChatID | |
1884 | // Reply is not expected. | |
1885 | func HandleLeaveChat(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1886 | chatID := t.GetField(fieldChatID).Data | |
1887 | chatInt := binary.BigEndian.Uint32(chatID) | |
1888 | ||
1889 | privChat, ok := cc.Server.PrivateChats[chatInt] | |
1890 | if !ok { | |
1891 | return res, nil | |
1892 | } | |
1893 | ||
1894 | delete(privChat.ClientConn, cc.uint16ID()) | |
1895 | ||
1896 | // Notify members of the private chat that the user has left | |
1897 | for _, c := range sortedClients(privChat.ClientConn) { | |
1898 | res = append(res, | |
1899 | *NewTransaction( | |
1900 | tranNotifyChatDeleteUser, | |
1901 | c.ID, | |
1902 | NewField(fieldChatID, chatID), | |
1903 | NewField(fieldUserID, *cc.ID), | |
1904 | ), | |
1905 | ) | |
1906 | } | |
1907 | ||
1908 | return res, err | |
1909 | } | |
1910 | ||
1911 | // HandleSetChatSubject is sent from a v1.8+ Hotline client when the user sets a private chat subject | |
1912 | // Fields used in the request: | |
1913 | // * 114 Chat ID | |
1914 | // * 115 Chat subject Chat subject string | |
1915 | // Reply is not expected. | |
1916 | func HandleSetChatSubject(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1917 | chatID := t.GetField(fieldChatID).Data | |
1918 | chatInt := binary.BigEndian.Uint32(chatID) | |
1919 | ||
1920 | privChat := cc.Server.PrivateChats[chatInt] | |
1921 | privChat.Subject = string(t.GetField(fieldChatSubject).Data) | |
1922 | ||
1923 | for _, c := range sortedClients(privChat.ClientConn) { | |
1924 | res = append(res, | |
1925 | *NewTransaction( | |
1926 | tranNotifyChatSubject, | |
1927 | c.ID, | |
1928 | NewField(fieldChatID, chatID), | |
1929 | NewField(fieldChatSubject, t.GetField(fieldChatSubject).Data), | |
1930 | ), | |
1931 | ) | |
1932 | } | |
1933 | ||
1934 | return res, err | |
1935 | } | |
1936 | ||
1937 | // HandleMakeAlias makes a filer alias using the specified path. | |
1938 | // Fields used in the request: | |
1939 | // 201 File name | |
1940 | // 202 File path | |
1941 | // 212 File new path Destination path | |
1942 | // | |
1943 | // Fields used in the reply: | |
1944 | // None | |
1945 | func HandleMakeAlias(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1946 | if !cc.Authorize(accessMakeAlias) { | |
1947 | res = append(res, cc.NewErrReply(t, "You are not allowed to make aliases.")) | |
1948 | return res, err | |
1949 | } | |
1950 | fileName := t.GetField(fieldFileName).Data | |
1951 | filePath := t.GetField(fieldFilePath).Data | |
1952 | fileNewPath := t.GetField(fieldFileNewPath).Data | |
1953 | ||
1954 | fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName) | |
1955 | if err != nil { | |
1956 | return res, err | |
1957 | } | |
1958 | ||
1959 | fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, fileNewPath, fileName) | |
1960 | if err != nil { | |
1961 | return res, err | |
1962 | } | |
1963 | ||
1964 | cc.logger.Debugw("Make alias", "src", fullFilePath, "dst", fullNewFilePath) | |
1965 | ||
1966 | if err := cc.Server.FS.Symlink(fullFilePath, fullNewFilePath); err != nil { | |
1967 | res = append(res, cc.NewErrReply(t, "Error creating alias")) | |
1968 | return res, nil | |
1969 | } | |
1970 | ||
1971 | res = append(res, cc.NewReply(t)) | |
1972 | return res, err | |
1973 | } | |
1974 | ||
1975 | // HandleDownloadBanner handles requests for a new banner from the server | |
1976 | // Fields used in the request: | |
1977 | // None | |
1978 | // Fields used in the reply: | |
1979 | // 107 fieldRefNum Used later for transfer | |
1980 | // 108 fieldTransferSize Size of data to be downloaded | |
1981 | func HandleDownloadBanner(cc *ClientConn, t *Transaction) (res []Transaction, err error) { | |
1982 | fi, err := cc.Server.FS.Stat(filepath.Join(cc.Server.ConfigDir, cc.Server.Config.BannerFile)) | |
1983 | if err != nil { | |
1984 | return res, err | |
1985 | } | |
1986 | ||
1987 | ft := cc.newFileTransfer(bannerDownload, []byte{}, []byte{}, make([]byte, 4)) | |
1988 | ||
1989 | binary.BigEndian.PutUint32(ft.TransferSize, uint32(fi.Size())) | |
1990 | ||
1991 | res = append(res, cc.NewReply(t, | |
1992 | NewField(fieldRefNum, ft.refNum[:]), | |
1993 | NewField(fieldTransferSize, ft.TransferSize), | |
1994 | )) | |
1995 | ||
1996 | return res, err | |
1997 | } |