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