]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/user.go
Extensive refactor and clean up
[rbdr/mobius] / hotline / user.go
index f80fd721a88058bac73ffb98635cc5fa1caa6b43..f3754d36328141114d5ef6dd8139b86ee91134ff 100644 (file)
@@ -2,24 +2,49 @@ package hotline
 
 import (
        "encoding/binary"
+       "io"
+       "math/big"
+       "slices"
 )
 
-// User flags are stored as a 2 byte bitmap with the following values:
+// User flags are stored as a 2 byte bitmap and represent various user states
 const (
-       userFlagAway        = 0 // User is away
-       userFlagAdmin       = 1 // User is admin
-       userFlagRefusePM    = 2 // User refuses private messages
-       userFLagRefusePChat = 3 // User refuses private chat
+       UserFlagAway        = 0 // User is away
+       UserFlagAdmin       = 1 // User is admin
+       UserFlagRefusePM    = 2 // User refuses private messages
+       UserFlagRefusePChat = 3 // User refuses private chat
 )
 
+// User options are sent from clients and represent options set in the client's preferences.
+const (
+       UserOptRefusePM     = 0 // User has "Refuse private messages" pref set
+       UserOptRefuseChat   = 1 // User has "Refuse private chat" pref set
+       UserOptAutoResponse = 2 // User has "Automatic response" pref set
+)
+
+type UserFlags [2]byte
+
+func (f *UserFlags) IsSet(i int) bool {
+       flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(f[:])))
+       return flagBitmap.Bit(i) == 1
+}
+
+func (f *UserFlags) Set(i int, newVal uint) {
+       flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(f[:])))
+       flagBitmap.SetBit(flagBitmap, i, newVal)
+       binary.BigEndian.PutUint16(f[:], uint16(flagBitmap.Int64()))
+}
+
 type User struct {
-       ID    []byte // Size 2
+       ID    [2]byte
        Icon  []byte // Size 2
        Flags []byte // Size 2
        Name  string // Variable length user name
+
+       readOffset int // Internal offset to track read progress
 }
 
-func (u User) Payload() []byte {
+func (u *User) Read(p []byte) (int, error) {
        nameLen := make([]byte, 2)
        binary.BigEndian.PutUint16(nameLen, uint16(len(u.Name)))
 
@@ -31,39 +56,41 @@ func (u User) Payload() []byte {
                u.Flags = u.Flags[2:]
        }
 
-       out := append(u.ID[:2], u.Icon[:2]...)
-       out = append(out, u.Flags[:2]...)
-       out = append(out, nameLen...)
-       out = append(out, u.Name...)
-
-       return out
-}
+       b := slices.Concat(
+               u.ID[:],
+               u.Icon,
+               u.Flags,
+               nameLen,
+               []byte(u.Name),
+       )
 
-func ReadUser(b []byte) (*User, error) {
-       u := &User{
-               ID:    b[0:2],
-               Icon:  b[2:4],
-               Flags: b[4:6],
-               Name:  string(b[8:]),
+       if u.readOffset >= len(b) {
+               return 0, io.EOF // All bytes have been read
        }
-       return u, nil
+
+       n := copy(p, b)
+       u.readOffset = n
+
+       return n, nil
 }
 
-// DecodeUserString decodes an obfuscated user string from a client
-// e.g. 98 8a 9a 8c 8b => "guest"
-func DecodeUserString(encodedString []byte) (decodedString string) {
-       for _, char := range encodedString {
-               decodedString += string(rune(255 - uint(char)))
-       }
-       return decodedString
+func (u *User) Write(p []byte) (int, error) {
+       namelen := int(binary.BigEndian.Uint16(p[6:8]))
+       u.ID = [2]byte(p[0:2])
+       u.Icon = p[2:4]
+       u.Flags = p[4:6]
+       u.Name = string(p[8 : 8+namelen])
+
+       return 8 + namelen, nil
 }
 
-// Take a []byte of uncoded ascii as input and encode it
-// TODO: change the method signature to take a string and return []byte
-func NegatedUserString(encodedString []byte) string {
-       var decodedString string
-       for _, char := range encodedString {
-               decodedString += string(255 - uint8(char))[1:]
+// encodeString takes []byte s containing cleartext and rotates by 255 into obfuscated cleartext.
+// The Hotline protocol uses this format for sending passwords over network.
+// Not secure, but hey, it was the 90s!
+func encodeString(clearText []byte) []byte {
+       obfuText := make([]byte, len(clearText))
+       for i := 0; i < len(clearText); i++ {
+               obfuText[i] = 255 - clearText[i]
        }
-       return decodedString
+       return obfuText
 }