]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
5 | "io" | |
6 | "slices" | |
7 | ) | |
8 | ||
9 | // User flags are stored as a 2 byte bitmap and represent various user states | |
10 | const ( | |
11 | UserFlagAway = iota // User is away | |
12 | UserFlagAdmin // User is admin | |
13 | UserFlagRefusePM // User refuses private messages | |
14 | UserFlagRefusePChat // User refuses private chat | |
15 | ) | |
16 | ||
17 | // FieldOptions flags are sent from v1.5+ clients as part of TranAgreed | |
18 | const ( | |
19 | UserOptRefusePM = iota // User has "Refuse private messages" pref set | |
20 | UserOptRefuseChat // User has "Refuse private chat" pref set | |
21 | UserOptAutoResponse // User has "Automatic response" pref set | |
22 | ) | |
23 | ||
24 | type User struct { | |
25 | ID []byte // Size 2 | |
26 | Icon []byte // Size 2 | |
27 | Flags []byte // Size 2 | |
28 | Name string // Variable length user name | |
29 | ||
30 | readOffset int // Internal offset to track read progress | |
31 | } | |
32 | ||
33 | func (u *User) Read(p []byte) (int, error) { | |
34 | nameLen := make([]byte, 2) | |
35 | binary.BigEndian.PutUint16(nameLen, uint16(len(u.Name))) | |
36 | ||
37 | if len(u.Icon) == 4 { | |
38 | u.Icon = u.Icon[2:] | |
39 | } | |
40 | ||
41 | if len(u.Flags) == 4 { | |
42 | u.Flags = u.Flags[2:] | |
43 | } | |
44 | ||
45 | b := slices.Concat( | |
46 | u.ID, | |
47 | u.Icon, | |
48 | u.Flags, | |
49 | nameLen, | |
50 | []byte(u.Name), | |
51 | ) | |
52 | ||
53 | if u.readOffset >= len(b) { | |
54 | return 0, io.EOF // All bytes have been read | |
55 | } | |
56 | ||
57 | n := copy(p, b) | |
58 | ||
59 | return n, io.EOF | |
60 | } | |
61 | ||
62 | func (u *User) Write(p []byte) (int, error) { | |
63 | namelen := int(binary.BigEndian.Uint16(p[6:8])) | |
64 | u.ID = p[0:2] | |
65 | u.Icon = p[2:4] | |
66 | u.Flags = p[4:6] | |
67 | u.Name = string(p[8 : 8+namelen]) | |
68 | ||
69 | return 8 + namelen, nil | |
70 | } | |
71 | ||
72 | // encodeString takes []byte s containing cleartext and rotates by 255 into obfuscated cleartext. | |
73 | // The Hotline protocol uses this format for sending passwords over network. | |
74 | // Not secure, but hey, it was the 90s! | |
75 | func encodeString(clearText []byte) []byte { | |
76 | obfuText := make([]byte, len(clearText)) | |
77 | for i := 0; i < len(clearText); i++ { | |
78 | obfuText[i] = 255 - clearText[i] | |
79 | } | |
80 | return obfuText | |
81 | } |