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