]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
5 | ) | |
6 | ||
7 | // User flags are stored as a 2 byte bitmap with the following values: | |
8 | const ( | |
9 | userFlagAway = 0 // User is away | |
10 | userFlagAdmin = 1 // User is admin | |
11 | userFlagRefusePM = 2 // User refuses private messages | |
12 | userFLagRefusePChat = 3 // User refuses private chat | |
13 | ) | |
14 | ||
15 | type User struct { | |
16 | ID []byte // Size 2 | |
17 | Icon []byte // Size 2 | |
18 | Flags []byte // Size 2 | |
19 | Name string // Variable length user name | |
20 | } | |
21 | ||
22 | func (u User) Payload() []byte { | |
23 | nameLen := make([]byte, 2) | |
24 | binary.BigEndian.PutUint16(nameLen, uint16(len(u.Name))) | |
25 | ||
26 | if len(u.Icon) == 4 { | |
27 | u.Icon = u.Icon[2:] | |
28 | } | |
29 | ||
30 | if len(u.Flags) == 4 { | |
31 | u.Flags = u.Flags[2:] | |
32 | } | |
33 | ||
34 | out := append(u.ID[:2], u.Icon[:2]...) | |
35 | out = append(out, u.Flags[:2]...) | |
36 | out = append(out, nameLen...) | |
37 | out = append(out, u.Name...) | |
38 | ||
39 | return out | |
40 | } | |
41 | ||
42 | func ReadUser(b []byte) (*User, error) { | |
43 | u := &User{ | |
44 | ID: b[0:2], | |
45 | Icon: b[2:4], | |
46 | Flags: b[4:6], | |
47 | Name: string(b[8:]), | |
48 | } | |
49 | return u, nil | |
50 | } | |
51 | ||
52 | // DecodeUserString decodes an obfuscated user string from a client | |
53 | // e.g. 98 8a 9a 8c 8b => "guest" | |
54 | func DecodeUserString(encodedString []byte) (decodedString string) { | |
55 | for _, char := range encodedString { | |
56 | decodedString += string(rune(255 - uint(char))) | |
57 | } | |
58 | return decodedString | |
59 | } | |
60 | ||
61 | // Take a []byte of uncoded ascii as input and encode it | |
62 | // TODO: change the method signature to take a string and return []byte | |
63 | func NegatedUserString(encodedString []byte) string { | |
64 | var decodedString string | |
65 | for _, char := range encodedString { | |
66 | decodedString += string(255 - uint8(char))[1:] | |
67 | } | |
68 | return decodedString | |
69 | } |