]> git.r.bdr.sh - rbdr/mobius/blame - hotline/user.go
Fix string negation bug
[rbdr/mobius] / hotline / user.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
4 "encoding/binary"
5)
6
7// User flags are stored as a 2 byte bitmap with the following values:
8const (
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
15type 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
22func (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
42func 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"
b25c4a19
JH
54func DecodeUserString(obfuText []byte) (clearText string) {
55 for _, char := range obfuText {
56 clearText += string(rune(255 - uint(char)))
6988a057 57 }
b25c4a19 58 return clearText
6988a057
JH
59}
60
b25c4a19
JH
61// negateString takes []byte s containing cleartext and rotates by 255 into obfuscated cleartext.
62// The Hotline protocol uses this format for sending passwords over network.
63// Not secure, but hey, it was the 90s!
64func negateString(clearText []byte) []byte {
65 obfuText := make([]byte, len(clearText))
66 for i := 0; i < len(clearText); i++ {
67 obfuText[i] = 255 - clearText[i]
6988a057 68 }
b25c4a19 69 return obfuText
6988a057 70}