support parsing usercommands

This commit is contained in:
mappu 2016-10-08 15:19:21 +13:00
parent 086281dab2
commit 1a3e4fe072
4 changed files with 52 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package libnmdc
import ( import (
"crypto/tls" "crypto/tls"
"net" "net"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -244,9 +245,29 @@ func (this *HubConnection) processProtocolMessage(message string) {
this.Hco.Address = HubAddress(commandParts[1]) this.Hco.Address = HubAddress(commandParts[1])
this.conn.Close() // we'll reconnect onto the new address 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 // IGNORABLE COMMANDS
case "$Supports": case "$Supports":
case "$UserCommand": // TODO $UserCommand 1 1 Group chat\New group chat$<%[mynick]> !groupchat_new&#124;|
case "$HubTopic": case "$HubTopic":
case "$Search": case "$Search":
case "$ConnectToMe": case "$ConnectToMe":

View File

@ -11,6 +11,7 @@ const (
EVENT_CONNECTION_STATE_CHANGED = 8 EVENT_CONNECTION_STATE_CHANGED = 8
EVENT_HUBNAME_CHANGED = 9 EVENT_HUBNAME_CHANGED = 9
EVENT_DEBUG_MESSAGE = 10 EVENT_DEBUG_MESSAGE = 10
EVENT_USERCOMMAND = 11
) )
type HubEventType int type HubEventType int
@ -20,4 +21,5 @@ type HubEvent struct {
Nick string Nick string
Message string Message string
StateChange ConnectionState 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

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