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