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