]> git.r.bdr.sh - rbdr/mobius/blob - hotline/user.go
5d09b2d449e069fc0f9ffaa296e6567ccd510248
[rbdr/mobius] / hotline / user.go
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(obfuText []byte) (clearText string) {
55 for _, char := range obfuText {
56 clearText += string(rune(255 - uint(char)))
57 }
58 return clearText
59 }
60
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!
64 func negateString(clearText []byte) []byte {
65 obfuText := make([]byte, len(clearText))
66 for i := 0; i < len(clearText); i++ {
67 obfuText[i] = 255 - clearText[i]
68 }
69 return obfuText
70 }