12 Commits

Author SHA1 Message Date
51b08dad3d readme 2016-10-08 16:32:10 +13:00
1a3e4fe072 support parsing usercommands 2016-10-08 15:19:21 +13:00
086281dab2 Added tag libnmdc-r9 for changeset e7c2c71ef24b 2016-08-27 17:39:00 +12:00
7392cbbea5 readme 2016-08-27 17:38:53 +12:00
265c0a43ce fix an issue not applying updated user profiles 2016-08-27 15:31:00 +12:00
417deff347 remove debug logging in previous 2016-08-27 15:24:18 +12:00
a996f1668c better myinfo parsing for zero-length speed strings 2016-08-27 15:22:39 +12:00
9b290ebb96 Added tag libnmdc-r8 for changeset b0e57a5fcffd 2016-05-10 19:18:11 +12:00
756b347592 readme 2016-05-10 19:18:04 +12:00
d4e442bd84 separate ClientTag and ClientVersion in UserInfo 2016-05-09 18:13:38 +12:00
bbdc18698d changelog 2016-05-08 14:46:41 +12:00
b049acad90 Added tag libnmdc-r7 for changeset 71343a2c641a 2016-05-08 14:44:45 +12:00
7 changed files with 122 additions and 22 deletions

View File

@@ -9,3 +9,6 @@ da9f123633f9c28be6435ed7898139665d4c39d9 nmdc-log-service-1.0.2
4116422bb10229d887f9296970a166fa1ef8c5fd nmdc-log-service-1.0.3
cb86f3a40115cc46f450c0c83fd9b9d3b740e820 nmdc-log-service-1.0.4
cb86f3a40115cc46f450c0c83fd9b9d3b740e820 libnmdc-r6
71343a2c641a438206d30ea7e75dc89a11dbef00 libnmdc-r7
b0e57a5fcffdf4102d669db51a3648ddf66a0792 libnmdc-r8
e7c2c71ef24b386add728fad35fff4a996fccbac libnmdc-r9

View File

@@ -3,6 +3,7 @@ package libnmdc
import (
"crypto/tls"
"net"
"strconv"
"strings"
"sync"
"time"
@@ -74,11 +75,15 @@ func (this *HubConnection) userJoined_NameOnly(nick string) {
}
func (this *HubConnection) userJoined_Full(uinf *UserInfo) {
if !this.UserExists(uinf.Nick) {
this.userLock.Lock()
defer this.userLock.Unlock()
// n.b. also called when we get a replacement MyINFO for someone
this.userLock.Lock()
defer this.userLock.Unlock()
this.users[uinf.Nick] = *uinf
_, userExisted := this.users[uinf.Nick] // don't use UserExists as it would deadlock the mutex
this.users[uinf.Nick] = *uinf
if !userExisted {
this.processEvent(HubEvent{EventType: EVENT_USER_JOINED, Nick: uinf.Nick})
}
}
@@ -240,9 +245,29 @@ func (this *HubConnection) processProtocolMessage(message string) {
this.Hco.Address = HubAddress(commandParts[1])
this.conn.Close() // we'll reconnect onto the new address
case "$UserCommand":
// $UserCommand 1 1 Group chat\New group chat$<%[mynick]> !groupchat_new&#124;|
if rx_userCommand.MatchString(commandParts[1]) {
usc := rx_userCommand.FindStringSubmatch(commandParts[1])
typeInt, _ := strconv.Atoi(usc[1])
contextInt, _ := strconv.Atoi(usc[2])
uscStruct := UserCommand{
Type: UserCommandType(typeInt),
Context: UserCommandContext(contextInt),
Message: usc[3],
Command: NMDCUnescape(usc[4]),
}
this.processEvent(HubEvent{EventType: EVENT_USERCOMMAND, UserCommand: &uscStruct})
} else {
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Malformed usercommand '" + commandParts[1] + "'"})
}
// IGNORABLE COMMANDS
case "$Supports":
case "$UserCommand": // TODO $UserCommand 1 1 Group chat\New group chat$<%[mynick]> !groupchat_new&#124;|
case "$HubTopic":
case "$Search":
case "$ConnectToMe":

View File

@@ -11,6 +11,7 @@ const (
EVENT_CONNECTION_STATE_CHANGED = 8
EVENT_HUBNAME_CHANGED = 9
EVENT_DEBUG_MESSAGE = 10
EVENT_USERCOMMAND = 11
)
type HubEventType int
@@ -20,4 +21,5 @@ type HubEvent struct {
Nick string
Message string
StateChange ConnectionState
UserCommand *UserCommand
}

26
UserCommand.go Normal file
View File

@@ -0,0 +1,26 @@
package libnmdc
type UserCommandType uint8
const (
USERCOMMAND_TYPE_SEPARATOR UserCommandType = 0
USERCOMMAND_TYPE_RAW UserCommandType = 1
USERCOMMAND_TYPE_NICKLIMITED UserCommandType = 2
USERCOMMAND_TYPE_CLEARALL UserCommandType = 255
)
type UserCommandContext uint8
const (
USERCOMMAND_CONTEXT_HUB UserCommandContext = 1
USERCOMMAND_CONTEXT_USER UserCommandContext = 2
USERCOMMAND_CONTEXT_SEARCH UserCommandContext = 4
USERCOMMAND_CONTEXT_FILELIST UserCommandContext = 8
)
type UserCommand struct {
Type UserCommandType
Context UserCommandContext
Message string
Command string
}

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"regexp"
"strconv"
"strings"
)
type UserFlag byte
@@ -37,6 +38,7 @@ type UserInfo struct {
Nick string
Description string
ClientTag string
ClientVersion string
Email string
ShareSize uint64
ConnectionMode ConnectionMode
@@ -54,12 +56,19 @@ 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
HEAD := `(?ms)^\$ALL ([^ ]+) `
FOOT := `\$.\$([^$]+)\$([^$]*)\$([0-9]*)\$$`
rx_myinfo = regexp.MustCompile(HEAD + `([^<]*)<(.+?) V:([^,]+),M:(.),H:([0-9]+)/([0-9]+)/([0-9]+),S:([0-9]+)>` + FOOT)
rx_myinfo_notag = regexp.MustCompile(HEAD + `([^$]*)` + FOOT) // 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$"
sample := ""
// 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$"
// sample = "$ALL SHINY_IVYSAUR <ncdc V:1.19.1-12-g5561,M:P,H:1/0/0,S:10>$ $0.005Q$$0$"
// sample = "$ALL mappu desccccc<HexChat V:2.12.1,M:P,H:1/0/0,S:0>$ $p$$0$"
u := UserInfo{}
err := u.fromMyINFO(sample)
@@ -69,7 +78,7 @@ func init() {
fmt.Printf("%+v\n", u)
}
os.Exit(1)
panic("")
*/
}
@@ -91,19 +100,25 @@ func maybeParse(str string, dest *uint64, default_val uint64) {
}
func (this *UserInfo) fromMyINFO(protomsg string) error {
// Normal format (with tag in exact M/H/S order)
// Normal format (with tag in exact V/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.ClientVersion = matches[4]
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])
maybeParse(matches[6], &this.HubsUnregistered, 0)
maybeParse(matches[7], &this.HubsRegistered, 0)
maybeParse(matches[8], &this.HubsOperator, 0)
maybeParse(matches[9], &this.Slots, 0)
if len(matches[10]) > 1 {
this.Speed = matches[10][:len(matches[10])-2]
} else {
this.Speed = ""
}
this.Flag = UserFlag(matches[10][len(matches[10])-1])
this.Email = NMDCUnescape(matches[11])
maybeParse(matches[12], &this.ShareSize, 0)
@@ -116,19 +131,27 @@ func (this *UserInfo) fromMyINFO(protomsg string) error {
this.Nick = matches[1]
this.Description = NMDCUnescape(matches[2])
this.ClientTag = ""
this.ClientVersion = "0"
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)
if len(matches[3]) > 1 {
this.Speed = matches[3][:len(matches[3])-2]
} else {
this.Speed = ""
}
this.Flag = UserFlag(matches[3][len(matches[3])-1])
this.Email = NMDCUnescape(matches[4])
maybeParse(matches[5], &this.ShareSize, 0)
return nil
}
fmt.Printf("PARSE: malformed\n")
// Couldn't get anything out of it...
return errors.New("Malformed MyINFO")
}
@@ -136,10 +159,11 @@ func (this *UserInfo) fromMyINFO(protomsg string) error {
// 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$",
"$MyINFO $ALL %s %s<%s V:%s,M:%c,H:%d/%d/%d,S:%d>$ $%s%c$%s$%d$",
this.Nick,
this.Description,
this.ClientTag,
strings.Replace(this.ClientVersion, ",", "-", -1), // just in case
this.ConnectionMode,
this.HubsUnregistered,
this.HubsRegistered,

View File

@@ -9,6 +9,24 @@ Tags: nmdc
=CHANGELOG=
2016-10-08 r10
- Feature: Support `$UserCommand`
2016-08-27 r9
- Fix an issue with parsing MyINFO strings with zero-length speed descriptions
- Fix an issue with not storing updated profile information
2016-05-10 r8
- Enhancement: Separate `ClientTag` and `ClientVersion` in `UserInfo` structs
2016-05-08 r7
- BREAKING: Remove direct access to `HubConnection.Users` map
- Feature: Threadsafe user map accessor
- Feature: Option to disable auto-reconnection
- Feature: New `Disconnect()`, `UserCount()`, `UserExists()` functions
- Enhancement: Support `$OpList`, add `IsOperator` member to `UserInfo` structs
- Refactor into multiple files
2016-04-16 r6
- Fix an issue with calling `panic()` on certain types of abnormal network failure

View File

@@ -19,12 +19,14 @@ const (
var rx_protocolMessage *regexp.Regexp
var rx_publicChat *regexp.Regexp
var rx_incomingTo *regexp.Regexp
var rx_userCommand *regexp.Regexp
var ErrNotConnected error = errors.New("Not connected")
func init() {
rx_protocolMessage = regexp.MustCompile("(?ms)^[^|]*\\|")
rx_publicChat = regexp.MustCompile("(?ms)^<([^>]*)> (.*)$")
rx_incomingTo = regexp.MustCompile("(?ms)^([^ ]+) From: ([^ ]+) \\$<([^>]*)> (.*)")
rx_userCommand = regexp.MustCompile(`(?ms)^(\d+) (\d+)\s?([^\$]*)\$?(.*)`)
}
func NMDCUnescape(encoded string) string {