]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
9cf66aea JH |
5 | "io" |
6 | "slices" | |
6988a057 JH |
7 | ) |
8 | ||
b1658a46 | 9 | // User flags are stored as a 2 byte bitmap and represent various user states |
6988a057 | 10 | const ( |
b1658a46 JH |
11 | UserFlagAway = 0 // User is away |
12 | UserFlagAdmin = 1 // User is admin | |
13 | UserFlagRefusePM = 2 // User refuses private messages | |
14 | UserFlagRefusePChat = 3 // User refuses private chat | |
6988a057 JH |
15 | ) |
16 | ||
d005ef04 | 17 | // FieldOptions flags are sent from v1.5+ clients as part of TranAgreed |
003a743e JH |
18 | const ( |
19 | refusePM = 0 // User has "Refuse private messages" pref set | |
20 | refuseChat = 1 // User has "Refuse private chat" pref set | |
21 | autoResponse = 2 // User has "Automatic response" pref set | |
22 | ) | |
23 | ||
6988a057 JH |
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 | ||
9cf66aea | 31 | func (u *User) Read(p []byte) (int, error) { |
6988a057 JH |
32 | nameLen := make([]byte, 2) |
33 | binary.BigEndian.PutUint16(nameLen, uint16(len(u.Name))) | |
34 | ||
35 | if len(u.Icon) == 4 { | |
36 | u.Icon = u.Icon[2:] | |
37 | } | |
38 | ||
39 | if len(u.Flags) == 4 { | |
40 | u.Flags = u.Flags[2:] | |
41 | } | |
42 | ||
43 | out := append(u.ID[:2], u.Icon[:2]...) | |
44 | out = append(out, u.Flags[:2]...) | |
45 | out = append(out, nameLen...) | |
46 | out = append(out, u.Name...) | |
47 | ||
9cf66aea JH |
48 | return copy(p, slices.Concat( |
49 | u.ID, | |
50 | u.Icon, | |
51 | u.Flags, | |
52 | nameLen, | |
53 | []byte(u.Name), | |
54 | )), io.EOF | |
6988a057 JH |
55 | } |
56 | ||
9cf66aea JH |
57 | func (u *User) Write(p []byte) (int, error) { |
58 | namelen := int(binary.BigEndian.Uint16(p[6:8])) | |
59 | u.ID = p[0:2] | |
60 | u.Icon = p[2:4] | |
61 | u.Flags = p[4:6] | |
62 | u.Name = string(p[8 : 8+namelen]) | |
63 | ||
64 | return 8 + namelen, nil | |
6988a057 JH |
65 | } |
66 | ||
76d0c1f6 | 67 | // encodeString takes []byte s containing cleartext and rotates by 255 into obfuscated cleartext. |
b25c4a19 JH |
68 | // The Hotline protocol uses this format for sending passwords over network. |
69 | // Not secure, but hey, it was the 90s! | |
76d0c1f6 | 70 | func encodeString(clearText []byte) []byte { |
b25c4a19 JH |
71 | obfuText := make([]byte, len(clearText)) |
72 | for i := 0; i < len(clearText); i++ { | |
73 | obfuText[i] = 255 - clearText[i] | |
6988a057 | 74 | } |
b25c4a19 | 75 | return obfuText |
6988a057 | 76 | } |