]> git.r.bdr.sh - rbdr/mobius/blame - hotline/user.go
Fix io.Reader implementation for FileNameWithInfo
[rbdr/mobius] / hotline / user.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
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 10const (
95159e55
JH
11 UserFlagAway = iota // User is away
12 UserFlagAdmin // User is admin
13 UserFlagRefusePM // User refuses private messages
14 UserFlagRefusePChat // User refuses private chat
6988a057
JH
15)
16
d005ef04 17// FieldOptions flags are sent from v1.5+ clients as part of TranAgreed
003a743e 18const (
95159e55
JH
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
003a743e
JH
22)
23
6988a057
JH
24type User struct {
25 ID []byte // Size 2
26 Icon []byte // Size 2
27 Flags []byte // Size 2
28 Name string // Variable length user name
95159e55
JH
29
30 readOffset int // Internal offset to track read progress
6988a057
JH
31}
32
9cf66aea 33func (u *User) Read(p []byte) (int, error) {
6988a057
JH
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
95159e55 45 b := slices.Concat(
9cf66aea
JH
46 u.ID,
47 u.Icon,
48 u.Flags,
49 nameLen,
50 []byte(u.Name),
95159e55
JH
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
6988a057
JH
60}
61
9cf66aea
JH
62func (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
6988a057
JH
70}
71
76d0c1f6 72// encodeString takes []byte s containing cleartext and rotates by 255 into obfuscated cleartext.
b25c4a19
JH
73// The Hotline protocol uses this format for sending passwords over network.
74// Not secure, but hey, it was the 90s!
76d0c1f6 75func encodeString(clearText []byte) []byte {
b25c4a19
JH
76 obfuText := make([]byte, len(clearText))
77 for i := 0; i < len(clearText); i++ {
78 obfuText[i] = 255 - clearText[i]
6988a057 79 }
b25c4a19 80 return obfuText
6988a057 81}