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