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