]> git.r.bdr.sh - rbdr/mobius/blob - hotline/user.go
Convert more bespoke methods to io.Reader/io.Writer interfaces
[rbdr/mobius] / hotline / user.go
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 = 0 // User is away
12 UserFlagAdmin = 1 // User is admin
13 UserFlagRefusePM = 2 // User refuses private messages
14 UserFlagRefusePChat = 3 // User refuses private chat
15 )
16
17 // FieldOptions flags are sent from v1.5+ clients as part of TranAgreed
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
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
31 func (u *User) Read(p []byte) (int, error) {
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
48 return copy(p, slices.Concat(
49 u.ID,
50 u.Icon,
51 u.Flags,
52 nameLen,
53 []byte(u.Name),
54 )), io.EOF
55 }
56
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
65 }
66
67 // decodeString decodes an obfuscated user string from a client
68 // e.g. 98 8a 9a 8c 8b => "guest"
69 func decodeString(obfuText []byte) (clearText string) {
70 for _, char := range obfuText {
71 clearText += string(rune(255 - uint(char)))
72 }
73 return clearText
74 }
75
76 // encodeString takes []byte s containing cleartext and rotates by 255 into obfuscated cleartext.
77 // The Hotline protocol uses this format for sending passwords over network.
78 // Not secure, but hey, it was the 90s!
79 func encodeString(clearText []byte) []byte {
80 obfuText := make([]byte, len(clearText))
81 for i := 0; i < len(clearText); i++ {
82 obfuText[i] = 255 - clearText[i]
83 }
84 return obfuText
85 }