libnmdc/UserInfo.go
2016-04-30 19:47:56 +12:00

153 lines
3.8 KiB
Go

// libnmdc project UserInfo.go
package libnmdc
import (
"errors"
"fmt"
"regexp"
"strconv"
)
type UserFlag byte
const (
FLAG_NORMAL UserFlag = 1
FLAG_AWAY_1 UserFlag = 2
FLAG_AWAY_2 UserFlag = 3
FLAG_SERVER_1 UserFlag = 4
FLAG_SERVER_2 UserFlag = 5
FLAG_SERVER_AWAY_1 UserFlag = 6
FLAG_SERVER_AWAY_2 UserFlag = 7
FLAG_FIREBALL_1 UserFlag = 8
FLAG_FIREBALL_2 UserFlag = 9
FLAG_FIREBALL_AWAY_1 UserFlag = 10
FLAG_FIREBALL_AWAY_2 UserFlag = 11
)
type ConnectionMode rune
const (
CONNECTIONMODE_ACTIVE ConnectionMode = 'A'
CONNECTIONMODE_PASSIVE ConnectionMode = 'P'
CONNECTIONMODE_SOCKS5 ConnectionMode = '5'
)
// This structure represents a user connected to a hub.
type UserInfo struct {
Nick string
Description string
ClientTag string
Email string
ShareSize uint64
ConnectionMode ConnectionMode
Flag UserFlag
Slots uint64
Speed string
HubsUnregistered uint64
HubsRegistered uint64
HubsOperator uint64
}
var rx_myinfo *regexp.Regexp
var rx_myinfo_notag *regexp.Regexp
func init() {
// $ALL <nick> <description>$ $<connection><flag>$<e-mail>$<sharesize>$
rx_myinfo = regexp.MustCompile("(?ms)^\\$ALL ([^ ]+) ([^<]*)<([^,]*),M:(.),H:([0-9]+)/([0-9]+)/([0-9]+),S:([0-9]+)>\\$.\\$(.+?)(.)\\$([^$]*)\\$([0-9]*)\\$$")
rx_myinfo_notag = regexp.MustCompile("(?ms)^\\$ALL ([^ ]+) ([^$]*)\\$.\\$(.*)(.)\\$([^$]*)\\$([0-9]*)\\$$") // Fallback for no tag
/*
sample := "$ALL Betty description<ApexDC++ V:1.4.3,M:P,H:9/0/2,S:1>$ $0.01\x01$xyz@xyz.com$53054999578$"
sample = "$ALL ivysaur80 $P$10A$$0$"
u := UserInfo{}
err := u.fromMyINFO(sample)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Printf("%+v\n", u)
}
os.Exit(1)
*/
}
func NewUserInfo(username string) *UserInfo {
return &UserInfo{
Nick: username,
ConnectionMode: CONNECTIONMODE_PASSIVE,
HubsUnregistered: 1,
}
}
func maybeParse(str string, dest *uint64, default_val uint64) {
sz, err := strconv.ParseUint(str, 10, 64)
if err == nil {
*dest = sz
} else {
*dest = default_val
}
}
func (this *UserInfo) fromMyINFO(protomsg string) error {
// Normal format (with tag in exact M/H/S order)
matches := rx_myinfo.FindStringSubmatch(protomsg)
if matches != nil {
this.Nick = matches[1]
this.Description = NMDCUnescape(matches[2])
this.ClientTag = NMDCUnescape(matches[3])
this.ConnectionMode = ConnectionMode(matches[4][0])
maybeParse(matches[5], &this.HubsUnregistered, 0)
maybeParse(matches[6], &this.HubsRegistered, 0)
maybeParse(matches[7], &this.HubsOperator, 0)
maybeParse(matches[8], &this.Slots, 0)
this.Speed = matches[9]
this.Flag = UserFlag(matches[10][0])
this.Email = NMDCUnescape(matches[11])
maybeParse(matches[12], &this.ShareSize, 0)
return nil
}
// No-tag format, used in early connection
matches = rx_myinfo_notag.FindStringSubmatch(protomsg)
if matches != nil {
this.Nick = matches[1]
this.Description = NMDCUnescape(matches[2])
this.ClientTag = ""
this.ConnectionMode = CONNECTIONMODE_PASSIVE
this.HubsUnregistered = 0
this.HubsRegistered = 0
this.HubsOperator = 0
this.Slots = 0
this.Speed = matches[3]
this.Flag = UserFlag(matches[4][0])
this.Email = NMDCUnescape(matches[5])
maybeParse(matches[6], &this.ShareSize, 0)
return nil
}
// Couldn't get anything out of it...
return errors.New("Malformed MyINFO")
}
// Returns the MyINFO command, WITH leading $MyINFO, and WITHOUT trailing pipe
func (this *UserInfo) toMyINFO() string {
return fmt.Sprintf(
"$MyINFO $ALL %s %s<%s,M:%c,H:%d/%d/%d,S:%d>$ $%s%c$%s$%d$",
this.Nick,
this.Description,
this.ClientTag,
this.ConnectionMode,
this.HubsUnregistered,
this.HubsRegistered,
this.HubsOperator,
this.Slots,
this.Speed,
this.Flag,
this.Email,
this.ShareSize,
)
}