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