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