From 1a3e4fe0725d04180e9fb91e6f1df055e002801e Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 8 Oct 2016 15:19:21 +1300 Subject: [PATCH] support parsing usercommands --- HubConnection.go | 23 ++++++++++++++++++++++- HubEvent.go | 2 ++ UserCommand.go | 26 ++++++++++++++++++++++++++ libnmdc.go | 2 ++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 UserCommand.go diff --git a/HubConnection.go b/HubConnection.go index 2943861..a2058f9 100644 --- a/HubConnection.go +++ b/HubConnection.go @@ -3,6 +3,7 @@ package libnmdc import ( "crypto/tls" "net" + "strconv" "strings" "sync" "time" @@ -244,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|| + 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|| case "$HubTopic": case "$Search": case "$ConnectToMe": diff --git a/HubEvent.go b/HubEvent.go index 5f66de3..82ac88d 100644 --- a/HubEvent.go +++ b/HubEvent.go @@ -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 } diff --git a/UserCommand.go b/UserCommand.go new file mode 100644 index 0000000..9032223 --- /dev/null +++ b/UserCommand.go @@ -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 +} diff --git a/libnmdc.go b/libnmdc.go index 00b390b..85bde0d 100644 --- a/libnmdc.go +++ b/libnmdc.go @@ -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 {