80 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
df0211c67d parse $OpList, set IsOperator bool in UserInfo structs 2016-05-08 12:49:36 +12:00
861090b0e9 add UserCount() helper (along with previous UserExists() helper) 2016-05-08 12:37:05 +12:00
cc15a571a8 threadsafe access to HubConnection.Users [[BREAKING CHANGE]] 2016-05-08 12:33:49 +12:00
3c4972d930 HubConnection.Disconnect() function (via autoreconnect abuse) 2016-05-04 19:17:20 +12:00
43db3a0d64 option to disable autoreconnect, move magic to constant 2016-05-04 19:15:02 +12:00
8e4a45a2b9 split off all other files, too 2016-05-04 19:03:36 +12:00
f350afa0fe split off HubAddress struct 2016-05-04 18:59:11 +12:00
dea4f996f9 move nmdc-log-service to separate repo 2016-04-30 19:48:04 +12:00
0fee719b90 restructure repo 2016-04-30 19:47:56 +12:00
15455ff031 nmdc-log-service/scripts: fix wrong permissions on binary files inside tar archive 2016-04-16 19:22:25 +12:00
a287b3adcc Added tag libnmdc-r6 for changeset cb86f3a40115 2016-04-16 19:22:05 +12:00
d6d9a1711c Added tag nmdc-log-service-1.0.4 for changeset cb86f3a40115 2016-04-16 19:21:57 +12:00
1858dfa102 update README 2016-04-16 19:12:44 +12:00
89b67f8559 nmdc-log-service: track sample systemd unit 2016-04-16 19:01:20 +12:00
649b3dfcab libnmdc: patch panic on read() returning an error (interface type) that can't be cast to net.error 2016-04-16 18:27:14 +12:00
ed64887ddc nmdc-log-service: prevent logging the same connection-state message repeatedly 2016-04-16 18:21:38 +12:00
2113139cdc scripts: strip sensitive gopath string out of release binaries 2016-04-16 18:18:09 +12:00
07441628d9 nmdc-log-service: use NewUserInfo helper to set passive/1-connected in tag 2016-04-10 16:37:21 +12:00
34dd9d515c Added tag nmdc-log-service-1.0.3 for changeset 4116422bb102 2016-04-04 19:04:32 +12:00
32f64690c9 update README 2016-04-04 19:04:27 +12:00
3f66cd93c7 Added tag libnmdc-r5 for changeset 75a78f6a78f2 2016-04-04 19:02:56 +12:00
9b32682c8a libnmdc: support hub-redirects 2016-04-04 19:01:55 +12:00
c9e95df0df libnmdc: fix for previous 2016-04-04 19:01:47 +12:00
a2b94e8724 libnmdc: send protocol-level keepalives 2016-04-04 18:57:50 +12:00
3dfef302ee Added tag nmdc-log-service-1.0.2 for changeset da9f123633f9 2016-04-03 19:24:12 +12:00
611a934a4e nmdc-log-service: readme 2016-04-03 19:24:03 +12:00
1a3617b960 libnmdc: readme 2016-04-03 19:23:14 +12:00
a37b1773d0 Added tag libnmdc-r4 for changeset 050b424a7c5d 2016-04-03 19:22:02 +12:00
09301f138c libnmdc: (comment) 2016-04-03 19:21:32 +12:00
8d7b0b46af libnmdc: fix bugs with buffer splitting 2016-04-03 19:20:58 +12:00
73f6d66cbd libnmdc: reinstate backward-compatibility re new sync api 2016-04-03 18:59:11 +12:00
019f67a7de libnmdc: ensure we always flush protocol commands that happened alongside a disconnection event 2016-04-03 18:54:56 +12:00
debc14cea3 libnmdc: isolate constants 2016-04-03 18:53:14 +12:00
ba378a8245 libnmdc: optional synchronous-based API instead of channel-based API 2016-04-03 18:52:19 +12:00
e3a92da5f6 libnmdc: remove redundant bool=false initialisation (handled in $Lock callback) 2016-04-03 18:38:43 +12:00
99e4ebb842 libnmdc: set our own state before emitting statechange event 2016-04-03 18:38:06 +12:00
4499c7306d libnmdc: clear fullbuffer prior to connection attempt 2016-04-03 18:36:46 +12:00
d391159abe libnmdc: avoid meaningless cast 2016-04-03 18:35:11 +12:00
e111c99886 libnmdc: assert state parameter is set correctly 2016-04-03 18:35:05 +12:00
393f156b0e Added tag nmdc-log-service-1.0.1 for changeset fca41372e400 2016-04-03 13:36:47 +12:00
7cd49c2c8a update README files 2016-04-03 13:36:41 +12:00
163b6623f8 Added tag libnmdc-r3 for changeset d8b64d5527c2 2016-04-03 13:33:48 +12:00
2f0ab758b8 nmdc-log-service: add warning message if on TLS verification disabled 2016-04-03 13:32:19 +12:00
3b71ea3ce3 nmdc-log-service: add missing LF 2016-04-03 13:29:58 +12:00
b0e1c3e5d8 nmdc-log-service: fix opening files on linux 2016-04-03 13:29:49 +12:00
3a67b02bd9 libnmdc: patch wrong test 2016-04-03 13:29:34 +12:00
27c21572b3 libnmdc: use extra variable instead of reflect package 2016-04-03 13:22:09 +12:00
da92afa51e libnmdc: add SkipVerifyTLS option 2016-04-03 13:17:29 +12:00
ec0a781538 libnmdc: fix panic() on connection failure, owing to storing nil in an interface 2016-04-03 13:15:57 +12:00
052c3e2cba nmdc-log-service/build: build with -a 2016-04-03 11:50:37 +12:00
db0e1a5c2f nmdc-log-service: track readme 2016-04-02 14:31:34 +13:00
ead04a1d4d Added tag libnmdc-r2 for changeset 137c1b65039e 2016-04-02 14:28:42 +13:00
f8ac216379 libnmdc: update readme 2016-04-02 14:28:35 +13:00
28ad3c8e6a libnmdc: reintroduce sample client as part of main package 2016-04-02 14:27:48 +13:00
820fa72b77 Added tag nmdc-log-service-1.0.0 for changeset 02a360e95480 2016-04-02 14:22:24 +13:00
f4b868b0c7 Added tag libnmdc-r1 for changeset 945ab4b16d05 2016-04-02 14:22:15 +13:00
7a590755cf nmdc-log-service: buildscript 2016-04-02 14:17:56 +13:00
08d048a6d3 fix double-"default" appearing in --help output 2016-04-02 14:17:47 +13:00
a09bf67b8f nmdc-log-service: initial commit 2016-04-02 14:04:58 +13:00
d6339667fe libnmdc: fix PM detection 2016-04-02 14:01:58 +13:00
c532e2fe4f libnmdc: include error messages with EVENT_CONNECTION_STATE_CHANGED 2016-04-02 13:49:49 +13:00
433c1ddac9 libnmdc: patch panic() on nil connection 2016-04-02 13:49:38 +13:00
83792a4241 libnmdc: add default protocol/port if not present 2016-04-02 13:49:24 +13:00
2c34fe7fcb hgignore 2016-04-02 12:51:54 +13:00
786e99d743 don't track our test script 2016-04-02 12:51:37 +13:00
59ca9f8251 nmdcs support 2016-04-02 12:51:23 +13:00
4fad66857a hgignore 2016-04-02 12:51:02 +13:00
1cd8190a58 doc: track README 2016-04-02 12:50:25 +13:00
13 changed files with 746 additions and 374 deletions

View File

@@ -1,6 +1,14 @@
mode:regex mode:regex
# Compilation output
\.(?:exe|a)$ \.(?:exe|a)$
^pkg/ ^pkg/
# Dependencies
^src/(?:github.com|gopkg.in|golang.org)/ ^src/(?:github.com|gopkg.in|golang.org)/
# Scratch space
^src/nmdc/
# Binary release artefacts
/?__dist/

14
.hgtags Normal file
View File

@@ -0,0 +1,14 @@
945ab4b16d05aa084f71bf5da9a3f687e0ec8bbd libnmdc-r1
02a360e95480b97ddad83add5db48b2766339a99 nmdc-log-service-1.0.0
137c1b65039e03c80379826a6efdfd808f6fbc8f libnmdc-r2
d8b64d5527c2a5e4d76872e5bc3d69f7646135c6 libnmdc-r3
fca41372e400853775b02e951f9db91d87f41adb nmdc-log-service-1.0.1
050b424a7c5d5a27c9323c8810f3afbead1f5b96 libnmdc-r4
da9f123633f9c28be6435ed7898139665d4c39d9 nmdc-log-service-1.0.2
75a78f6a78f249a2cd8aa3d29f7e5e6319b4e03b libnmdc-r5
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

40
ConnectionState.go Normal file
View File

@@ -0,0 +1,40 @@
package libnmdc
import (
"net"
)
type ConnectionState int
const (
CONNECTIONSTATE_DISCONNECTED = 1
CONNECTIONSTATE_CONNECTING = 2 // Handshake in progress
CONNECTIONSTATE_CONNECTED = 3
)
func (cs ConnectionState) Format() string {
switch cs {
case CONNECTIONSTATE_DISCONNECTED:
return "Disconnected"
case CONNECTIONSTATE_CONNECTING:
return "Connecting"
case CONNECTIONSTATE_CONNECTED:
return "Connected"
default:
return "?"
}
}
func CheckIsNetTimeout(err error) bool {
if err == nil {
return false
}
switch err.(type) {
case net.Error:
return err.(net.Error).Timeout()
default:
return false
}
}

35
HubAddress.go Normal file
View File

@@ -0,0 +1,35 @@
package libnmdc
import (
"net/url"
"strings"
)
type HubAddress string
func (this *HubAddress) parse() url.URL {
parsed, err := url.Parse(strings.ToLower(string(*this)))
if err != nil || len(parsed.Host) == 0 {
parsed = &url.URL{
Scheme: "nmdc",
Host: string(*this),
}
}
// Add default port if not specified
if !strings.ContainsRune(parsed.Host, ':') {
parsed.Host = parsed.Host + ":411"
}
return *parsed
}
func (this *HubAddress) IsSecure() bool {
parsed := this.parse()
return parsed.Scheme == "nmdcs" || parsed.Scheme == "dchubs"
}
func (this *HubAddress) GetHostOnly() string {
return this.parse().Host
}

373
HubConnection.go Normal file
View File

@@ -0,0 +1,373 @@
package libnmdc
import (
"crypto/tls"
"net"
"strconv"
"strings"
"sync"
"time"
)
type HubConnection struct {
// Supplied parameters
Hco *HubConnectionOptions
// Current remote status
HubName string
State ConnectionState
users map[string]UserInfo
userLock sync.RWMutex
// Streamed events
processEvent func(HubEvent)
OnEvent chan HubEvent
// Private state
conn net.Conn // this is an interface
connValid bool
sentOurHello bool
autoReconnect bool
}
func (this *HubConnection) Users(cb func(*map[string]UserInfo) error) error {
this.userLock.Lock()
defer this.userLock.Unlock()
return cb(&this.users)
}
func (this *HubConnection) SayPublic(message string) {
this.SayRaw("<" + this.Hco.Self.Nick + "> " + NMDCEscape(message) + "|")
}
func (this *HubConnection) SayPrivate(recipient string, message string) {
this.SayRaw("$To: " + recipient + " From: " + this.Hco.Self.Nick + " $<" + this.Hco.Self.Nick + "> " + NMDCEscape(message) + "|")
}
func (this *HubConnection) SayInfo() {
this.SayRaw(this.Hco.Self.toMyINFO() + "|")
}
func (this *HubConnection) UserExists(nick string) bool {
this.userLock.RLock()
defer this.userLock.RUnlock()
_, already_existed := this.users[nick]
return already_existed
}
func (this *HubConnection) UserCount() int {
this.userLock.RLock()
defer this.userLock.RUnlock()
return len(this.users)
}
func (this *HubConnection) userJoined_NameOnly(nick string) {
if !this.UserExists(nick) {
this.userLock.Lock()
defer this.userLock.Unlock()
this.users[nick] = *NewUserInfo(nick)
this.processEvent(HubEvent{EventType: EVENT_USER_JOINED, Nick: nick})
}
}
func (this *HubConnection) userJoined_Full(uinf *UserInfo) {
// n.b. also called when we get a replacement MyINFO for someone
this.userLock.Lock()
defer this.userLock.Unlock()
_, 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})
}
}
// Note that protocol messages are transmitted on the caller thread, not from
// any internal libnmdc thread.
func (this *HubConnection) SayRaw(protocolCommand string) error {
if this.connValid {
_, err := this.conn.Write([]byte(protocolCommand))
return err
} else {
return ErrNotConnected
}
}
func (this *HubConnection) processProtocolMessage(message string) {
// Zero-length protocol message
// ````````````````````````````
if len(message) == 0 {
return
}
// Public chat
// ```````````
if rx_publicChat.MatchString(message) {
pubchat_parts := rx_publicChat.FindStringSubmatch(message)
this.processEvent(HubEvent{EventType: EVENT_PUBLIC, Nick: pubchat_parts[1], Message: NMDCUnescape(pubchat_parts[2])})
return
}
// System messages
// ```````````````
if message[0] != '$' {
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_HUB, Nick: this.HubName, Message: NMDCUnescape(message)})
return
}
// Protocol messages
// `````````````````
commandParts := strings.SplitN(message, " ", 2)
switch commandParts[0] {
case "$Lock":
this.SayRaw("$Supports NoGetINFO UserCommand UserIP2|" +
"$Key " + NMDCUnlock([]byte(commandParts[1])) + "|" +
"$ValidateNick " + NMDCEscape(this.Hco.Self.Nick) + "|")
this.sentOurHello = false
case "$Hello":
if commandParts[1] == this.Hco.Self.Nick && !this.sentOurHello {
this.SayRaw("$Version 1,0091|")
this.SayRaw("$GetNickList|")
this.SayInfo()
this.sentOurHello = true
} else {
this.userJoined_NameOnly(commandParts[1])
}
case "$HubName":
this.HubName = commandParts[1]
this.processEvent(HubEvent{EventType: EVENT_HUBNAME_CHANGED, Nick: commandParts[1]})
case "$ValidateDenide": // sic
if len(this.Hco.NickPassword) > 0 {
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "Incorrect password."})
} else {
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "Nick already in use."})
}
case "$HubIsFull":
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "Hub is full."})
case "$BadPass":
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "Incorrect password."})
case "$GetPass":
this.SayRaw("$MyPass " + NMDCEscape(this.Hco.NickPassword) + "|")
case "$Quit":
func() {
this.userLock.Lock()
defer this.userLock.Unlock()
delete(this.users, commandParts[1])
}()
this.processEvent(HubEvent{EventType: EVENT_USER_PART, Nick: commandParts[1]})
case "$MyINFO":
u := UserInfo{}
err := u.fromMyINFO(commandParts[1])
if err == nil {
this.userJoined_Full(&u)
} else {
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: err.Error()})
}
case "$NickList":
nicklist := strings.Split(commandParts[1], "$$")
for _, nick := range nicklist {
if len(nick) > 0 {
this.userJoined_NameOnly(nick)
}
}
case "$OpList":
oplist := strings.Split(commandParts[1], "$$")
opmap := map[string]struct{}{}
// Organise/sort the list, and ensure we're not meeting an operator for
// the first time
for _, nick := range oplist {
if len(nick) > 0 {
opmap[nick] = struct{}{}
this.userJoined_NameOnly(nick) // assert existence; noop otherwise
}
}
// Mark all mentioned nicks as being operators, and all unmentioned nicks
// as being /not/ an operator. (second pass minimises RW mutex use)
func() {
this.userLock.Lock()
defer this.userLock.Unlock()
for nick, userinfo := range this.users {
_, isop := opmap[nick]
userinfo.IsOperator = isop
this.users[nick] = userinfo
}
}()
case "$To:":
valid := false
if rx_incomingTo.MatchString(commandParts[1]) {
txparts := rx_incomingTo.FindStringSubmatch(commandParts[1])
if txparts[1] == this.Hco.Self.Nick && txparts[2] == txparts[3] {
this.processEvent(HubEvent{EventType: EVENT_PRIVATE, Nick: txparts[2], Message: txparts[4]})
valid = true
}
}
if !valid {
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Malformed private message '" + commandParts[1] + "'"})
}
case "$UserIP":
// Final message in PtokaX connection handshake - trigger connection callback.
// This might not be the case for other hubsofts, though
if this.State != CONNECTIONSTATE_CONNECTED {
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTED})
this.State = CONNECTIONSTATE_CONNECTED
}
case "$ForceMove":
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 "$HubTopic":
case "$Search":
case "$ConnectToMe":
default:
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Unhandled protocol command '" + commandParts[0] + "'"})
}
}
func (this *HubConnection) Disconnect() {
this.autoReconnect = false
if this.conn != nil {
this.conn.Close()
}
// A CONNECTIONSTATE_DISCONNECTED message will be emitted by the worker.
}
func (this *HubConnection) worker() {
var fullBuffer string
var err error = nil
var nbytes int = 0
for {
// If we're not connected, attempt reconnect
if this.conn == nil {
fullBuffer = "" // clear
if this.Hco.Address.IsSecure() {
this.conn, err = tls.Dial("tcp", this.Hco.Address.GetHostOnly(), &tls.Config{
InsecureSkipVerify: this.Hco.SkipVerifyTLS,
})
} else {
this.conn, err = net.Dial("tcp", this.Hco.Address.GetHostOnly())
}
if err != nil {
this.State = CONNECTIONSTATE_DISCONNECTED
this.connValid = false
} else {
this.State = CONNECTIONSTATE_CONNECTING
this.connValid = true
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTING})
}
}
// Read from socket into our local buffer (blocking)
if this.connValid {
readBuff := make([]byte, 1024)
this.conn.SetReadDeadline(time.Now().Add(SEND_KEEPALIVE_EVERY))
nbytes, err = this.conn.Read(readBuff)
if CheckIsNetTimeout(err) {
// No data before read deadline
err = nil
// Send KA packet
_, err = this.conn.Write([]byte("|"))
}
if nbytes > 0 {
fullBuffer += string(readBuff[0:nbytes])
}
}
// Attempt to parse a message block
for len(fullBuffer) > 0 {
for len(fullBuffer) > 0 && fullBuffer[0] == '|' {
fullBuffer = fullBuffer[1:]
}
protocolMessage := rx_protocolMessage.FindString(fullBuffer)
if len(protocolMessage) > 0 {
this.processProtocolMessage(protocolMessage[:len(protocolMessage)-1])
fullBuffer = fullBuffer[len(protocolMessage):]
} else {
break
}
}
// Maybe we disconnected
// Perform this check *last*, to ensure we've had a final shot at
// clearing out any queued messages
if err != nil {
this.State = CONNECTIONSTATE_DISCONNECTED
this.conn = nil
this.connValid = false
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()})
if this.autoReconnect {
time.Sleep(AUTO_RECONNECT_AFTER) // Wait before reconnect
continue
} else {
return // leave the worker for good
}
}
}
}

58
HubConnectionOptions.go Normal file
View File

@@ -0,0 +1,58 @@
package libnmdc
type HubConnectionOptions struct {
Address HubAddress
SkipVerifyTLS bool // using a negative verb, because bools default to false
SkipAutoReconnect bool // as above
Self UserInfo
NickPassword string
// Returning messages in async mode
NumEventsToBuffer uint
// Returning messages in sync mode
OnEventSync func(HubEvent)
}
func (this *HubConnectionOptions) prepareConnection() *HubConnection {
if this.Self.ClientTag == "" {
this.Self.ClientTag = DEFAULT_CLIENT_TAG
}
hc := HubConnection{
Hco: this,
HubName: DEFAULT_HUB_NAME,
State: CONNECTIONSTATE_DISCONNECTED,
users: make(map[string]UserInfo),
autoReconnect: !this.SkipAutoReconnect,
}
return &hc
}
// Connects to an NMDC server, and spawns a background goroutine to handle
// protocol messages. Events will be sent by channel to the returned hc.OnEvent,
// the client is responsible for selecting off this.
func (this *HubConnectionOptions) Connect() *HubConnection {
if this.NumEventsToBuffer < 1 {
this.NumEventsToBuffer = 1
}
hc := this.prepareConnection()
hc.OnEvent = make(chan HubEvent, this.NumEventsToBuffer)
hc.processEvent = func(ev HubEvent) {
hc.OnEvent <- ev
}
go hc.worker()
return hc
}
// Connects to an NMDC server, and blocks forever to handle protocol messages.
// Client code should supply an event handling function as hco.OnEventSync.
func (this *HubConnectionOptions) ConnectSync() {
hc := this.prepareConnection()
hc.worker()
}

25
HubEvent.go Normal file
View File

@@ -0,0 +1,25 @@
package libnmdc
const (
EVENT_PUBLIC = 1
EVENT_PRIVATE = 2
EVENT_SYSTEM_MESSAGE_FROM_HUB = 3
EVENT_SYSTEM_MESSAGE_FROM_CONN = 4
EVENT_USER_JOINED = 5
EVENT_USER_PART = 6
EVENT_USER_UPDATED_INFO = 7
EVENT_CONNECTION_STATE_CHANGED = 8
EVENT_HUBNAME_CHANGED = 9
EVENT_DEBUG_MESSAGE = 10
EVENT_USERCOMMAND = 11
)
type HubEventType int
type HubEvent struct {
EventType HubEventType
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" "fmt"
"regexp" "regexp"
"strconv" "strconv"
"strings"
) )
type UserFlag byte type UserFlag byte
@@ -37,6 +38,7 @@ type UserInfo struct {
Nick string Nick string
Description string Description string
ClientTag string ClientTag string
ClientVersion string
Email string Email string
ShareSize uint64 ShareSize uint64
ConnectionMode ConnectionMode ConnectionMode ConnectionMode
@@ -46,6 +48,7 @@ type UserInfo struct {
HubsUnregistered uint64 HubsUnregistered uint64
HubsRegistered uint64 HubsRegistered uint64
HubsOperator uint64 HubsOperator uint64
IsOperator bool
} }
var rx_myinfo *regexp.Regexp var rx_myinfo *regexp.Regexp
@@ -53,12 +56,19 @@ var rx_myinfo_notag *regexp.Regexp
func init() { func init() {
// $ALL <nick> <description>$ $<connection><flag>$<e-mail>$<sharesize>$ // $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 := ""
sample = "$ALL ivysaur80 $P$10A$$0$" // 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{} u := UserInfo{}
err := u.fromMyINFO(sample) err := u.fromMyINFO(sample)
@@ -68,7 +78,7 @@ func init() {
fmt.Printf("%+v\n", u) fmt.Printf("%+v\n", u)
} }
os.Exit(1) panic("")
*/ */
} }
@@ -90,19 +100,25 @@ func maybeParse(str string, dest *uint64, default_val uint64) {
} }
func (this *UserInfo) fromMyINFO(protomsg string) error { 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) matches := rx_myinfo.FindStringSubmatch(protomsg)
if matches != nil { if matches != nil {
this.Nick = matches[1] this.Nick = matches[1]
this.Description = NMDCUnescape(matches[2]) this.Description = NMDCUnescape(matches[2])
this.ClientTag = NMDCUnescape(matches[3]) this.ClientTag = NMDCUnescape(matches[3])
this.ClientVersion = matches[4]
this.ConnectionMode = ConnectionMode(matches[4][0]) this.ConnectionMode = ConnectionMode(matches[4][0])
maybeParse(matches[5], &this.HubsUnregistered, 0) maybeParse(matches[6], &this.HubsUnregistered, 0)
maybeParse(matches[6], &this.HubsRegistered, 0) maybeParse(matches[7], &this.HubsRegistered, 0)
maybeParse(matches[7], &this.HubsOperator, 0) maybeParse(matches[8], &this.HubsOperator, 0)
maybeParse(matches[8], &this.Slots, 0) maybeParse(matches[9], &this.Slots, 0)
this.Speed = matches[9] if len(matches[10]) > 1 {
this.Flag = UserFlag(matches[10][0]) 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]) this.Email = NMDCUnescape(matches[11])
maybeParse(matches[12], &this.ShareSize, 0) maybeParse(matches[12], &this.ShareSize, 0)
@@ -115,19 +131,27 @@ func (this *UserInfo) fromMyINFO(protomsg string) error {
this.Nick = matches[1] this.Nick = matches[1]
this.Description = NMDCUnescape(matches[2]) this.Description = NMDCUnescape(matches[2])
this.ClientTag = "" this.ClientTag = ""
this.ClientVersion = "0"
this.ConnectionMode = CONNECTIONMODE_PASSIVE this.ConnectionMode = CONNECTIONMODE_PASSIVE
this.HubsUnregistered = 0 this.HubsUnregistered = 0
this.HubsRegistered = 0 this.HubsRegistered = 0
this.HubsOperator = 0 this.HubsOperator = 0
this.Slots = 0 this.Slots = 0
this.Speed = matches[3]
this.Flag = UserFlag(matches[4][0]) if len(matches[3]) > 1 {
this.Email = NMDCUnescape(matches[5]) this.Speed = matches[3][:len(matches[3])-2]
maybeParse(matches[6], &this.ShareSize, 0) } 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 return nil
} }
fmt.Printf("PARSE: malformed\n")
// Couldn't get anything out of it... // Couldn't get anything out of it...
return errors.New("Malformed MyINFO") return errors.New("Malformed MyINFO")
} }
@@ -135,10 +159,11 @@ func (this *UserInfo) fromMyINFO(protomsg string) error {
// Returns the MyINFO command, WITH leading $MyINFO, and WITHOUT trailing pipe // Returns the MyINFO command, WITH leading $MyINFO, and WITHOUT trailing pipe
func (this *UserInfo) toMyINFO() string { func (this *UserInfo) toMyINFO() string {
return fmt.Sprintf( 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.Nick,
this.Description, this.Description,
this.ClientTag, this.ClientTag,
strings.Replace(this.ClientVersion, ",", "-", -1), // just in case
this.ConnectionMode, this.ConnectionMode,
this.HubsUnregistered, this.HubsUnregistered,
this.HubsRegistered, this.HubsRegistered,

54
__dist/README.txt Normal file
View File

@@ -0,0 +1,54 @@
An NMDC client protocol library for Golang.
- Offers both channel-based and synchronous APIs.
- Includes a sample logging client using the channel-based API.
- This code hosting site isn't (yet) compatible with `go get`.
Written in golang
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
2016-04-04 r5
- Enhancement: Support protocol keepalives
- Enhancement: Support hub redirects (`$ForceMove`)
2016-04-03 r4
- Feature: Add synchronous API
- Fix an issue with reading HubConnection's state parameter
- Fix an issue with buffered protocol commands
2016-04-03 r3
- Feature: Add `SkipVerifyTLS` option
- Fix an issue with calling `panic()` if connection failed
2016-04-02 r2
- Enhancement: Support NMDC-over-TLS (NMDCS)
- Fix an issue recieving private messages
- Fix an issue with calling `panic()` if connection failed
- Fix an issue parsing URIs without a specified port
- Move sample content into directory with excluded build
2016-02-12 r1
- Initial public release

64
libnmdc.go Normal file
View File

@@ -0,0 +1,64 @@
// libnmdc project libnmdc.go
package libnmdc
import (
"errors"
"fmt"
"regexp"
"strings"
"time"
)
const (
DEFAULT_CLIENT_TAG string = "libnmdc.go"
DEFAULT_HUB_NAME string = "(unknown)"
SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second
AUTO_RECONNECT_AFTER time.Duration = 30 * time.Second
)
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 {
v1 := strings.Replace(encoded, "&#36;", "$", -1)
v2 := strings.Replace(v1, "&#124;", "|", -1)
return strings.Replace(v2, "&amp;", "&", -1)
}
func NMDCEscape(plaintext string) string {
v1 := strings.Replace(plaintext, "&", "&amp;", -1)
v2 := strings.Replace(v1, "|", "&#124;", -1)
return strings.Replace(v2, "$", "&#36;", -1)
}
func NMDCUnlock(lock []byte) string {
nibble_swap := func(b byte) byte {
return ((b << 4) & 0xF0) | ((b >> 4) & 0x0F)
}
chr := func(b byte) string {
if b == 0 || b == 5 || b == 36 || b == 96 || b == 124 || b == 126 {
return fmt.Sprintf("/%%DCN%04d%%/", b)
} else {
return string(b)
}
}
key := chr(nibble_swap(lock[0] ^ lock[len(lock)-2] ^ lock[len(lock)-3] ^ 5))
for i := 1; i < len(lock); i += 1 {
key += chr(nibble_swap(lock[i] ^ lock[i-1]))
}
return key
}

View File

@@ -1,4 +1,8 @@
// nmdc project main.go // +build ignore
// This sample file demonstrates use of the libnmdc.go library. It's excluded
// when building the library package, but you can run it via `go run libnmdc_sample.go`.
package main package main
import ( import (
@@ -9,7 +13,7 @@ import (
func main() { func main() {
opts := libnmdc.HubConnectionOptions{ opts := libnmdc.HubConnectionOptions{
Address: "127.0.0.1:411", Address: "127.0.0.1",
Self: libnmdc.UserInfo{Nick: "slowpoke9"}, Self: libnmdc.UserInfo{Nick: "slowpoke9"},
} }
hub := opts.Connect() hub := opts.Connect()
@@ -18,7 +22,7 @@ func main() {
event := <-hub.OnEvent event := <-hub.OnEvent
switch event.EventType { switch event.EventType {
case libnmdc.EVENT_CONNECTION_STATE_CHANGED: case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
fmt.Printf("Connection -- %s\n", event.StateChange.Format()) fmt.Printf("Connection -- %s (%s)\n", event.StateChange.Format(), event.Message)
case libnmdc.EVENT_PUBLIC: case libnmdc.EVENT_PUBLIC:
fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message) fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message)

View File

@@ -1,354 +0,0 @@
// libnmdc project libnmdc.go
package libnmdc
import (
"fmt"
"net"
"regexp"
"strings"
"time"
)
type ConnectionState int
type HubAddress string
type HubEventType int
const (
CONNECTIONSTATE_DISCONNECTED = 1
CONNECTIONSTATE_CONNECTING = 2 // Handshake in progress
CONNECTIONSTATE_CONNECTED = 3
)
const (
EVENT_PUBLIC = 1
EVENT_PRIVATE = 2
EVENT_SYSTEM_MESSAGE_FROM_HUB = 3
EVENT_SYSTEM_MESSAGE_FROM_CONN = 4
EVENT_USER_JOINED = 5
EVENT_USER_PART = 6
EVENT_USER_UPDATED_INFO = 7
EVENT_CONNECTION_STATE_CHANGED = 8
EVENT_HUBNAME_CHANGED = 9
EVENT_DEBUG_MESSAGE = 10
)
var rx_protocolMessage *regexp.Regexp
var rx_publicChat *regexp.Regexp
var rx_incomingTo *regexp.Regexp
func init() {
rx_protocolMessage = regexp.MustCompile("(?ms)^[^|]*|")
rx_publicChat = regexp.MustCompile("(?ms)^<([^>]*)> (.*)$")
rx_incomingTo = regexp.MustCompile("(?ms)^([^ ]+) From: ([^ ]+) $<([^>]*)> (.*)$")
}
type HubConnectionOptions struct {
Address HubAddress
Self UserInfo
NickPassword string
NumEventsToBuffer uint
}
type HubConnection struct {
// Supplied parameters
Hco *HubConnectionOptions
// Current remote status
HubName string
State ConnectionState
Users map[string]UserInfo
// Streamed events
OnEvent chan HubEvent
// Private state
conn net.Conn
sentOurHello bool
}
type HubEvent struct {
EventType HubEventType
Nick string
Message string
StateChange ConnectionState
}
func (cs ConnectionState) Format() string {
switch cs {
case CONNECTIONSTATE_DISCONNECTED:
return "Disconnected"
case CONNECTIONSTATE_CONNECTING:
return "Connecting"
case CONNECTIONSTATE_CONNECTED:
return "Connected"
default:
return "?"
}
}
func NMDCUnescape(encoded string) string {
v1 := strings.Replace(encoded, "&#36;", "$", -1)
v2 := strings.Replace(v1, "&#124;", "|", -1)
return strings.Replace(v2, "&amp;", "&", -1)
}
func NMDCEscape(plaintext string) string {
v1 := strings.Replace(plaintext, "&", "&amp;", -1)
v2 := strings.Replace(v1, "|", "&#124;", -1)
return strings.Replace(v2, "$", "&#36;", -1)
}
func (this *HubConnection) SayPublic(message string) {
this.SayRaw("<" + this.Hco.Self.Nick + "> " + NMDCEscape(message) + "|")
}
func (this *HubConnection) SayPrivate(recipient string, message string) {
this.SayRaw("$To: " + recipient + " From: " + this.Hco.Self.Nick + " $<" + this.Hco.Self.Nick + "> " + NMDCEscape(message) + "|")
}
func (this *HubConnection) SayInfo() {
this.SayRaw(this.Hco.Self.toMyINFO() + "|")
}
func (this *HubConnection) userJoined_NameOnly(nick string) {
_, already_existed := this.Users[nick]
if !already_existed {
this.Users[nick] = *NewUserInfo(nick)
this.OnEvent <- HubEvent{EventType: EVENT_USER_JOINED, Nick: nick}
}
}
func (this *HubConnection) userJoined_Full(uinf *UserInfo) {
_, already_existed := this.Users[uinf.Nick]
if !already_existed {
this.Users[uinf.Nick] = *uinf
this.OnEvent <- HubEvent{EventType: EVENT_USER_JOINED, Nick: uinf.Nick}
}
}
// Note that protocol messages are transmitted on the caller thread, not from
// any internal libnmdc thread.
func (this *HubConnection) SayRaw(protocolCommand string) error {
_, err := this.conn.Write([]byte(protocolCommand))
return err
}
func parseLock(lock []byte) string {
nibble_swap := func(b byte) byte {
return ((b << 4) & 0xF0) | ((b >> 4) & 0x0F)
}
chr := func(b byte) string {
if b == 0 || b == 5 || b == 36 || b == 96 || b == 124 || b == 126 {
return fmt.Sprintf("/%%DCN%04d%%/", b)
} else {
return string(b)
}
}
key := chr(nibble_swap(lock[0] ^ lock[len(lock)-2] ^ lock[len(lock)-3] ^ 5))
for i := 1; i < len(lock); i += 1 {
key += chr(nibble_swap(lock[i] ^ lock[i-1]))
}
return key
}
func (this *HubConnection) processProtocolMessage(message string) {
// Zero-length protocol message
// ````````````````````````````
if len(message) == 0 {
return
}
// Public chat
// ```````````
if rx_publicChat.MatchString(message) {
pubchat_parts := rx_publicChat.FindStringSubmatch(message)
this.OnEvent <- HubEvent{EventType: EVENT_PUBLIC, Nick: pubchat_parts[1], Message: NMDCUnescape(pubchat_parts[2])}
return
}
// System messages
// ```````````````
if message[0] != '$' {
this.OnEvent <- HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_HUB, Nick: this.HubName, Message: NMDCUnescape(message)}
return
}
// Protocol messages
// `````````````````
commandParts := strings.SplitN(message, " ", 2)
switch commandParts[0] {
case "$Lock":
this.SayRaw("$Supports NoGetINFO UserCommand UserIP2|" +
"$Key " + parseLock([]byte(commandParts[1])) + "|" +
"$ValidateNick " + NMDCEscape(this.Hco.Self.Nick) + "|")
this.sentOurHello = false
case "$Hello":
if commandParts[1] == this.Hco.Self.Nick && !this.sentOurHello {
this.SayRaw("$Version 1,0091|")
this.SayRaw("$GetNickList|")
this.SayInfo()
this.sentOurHello = true
} else {
this.userJoined_NameOnly(commandParts[1])
}
case "$HubName":
this.HubName = commandParts[1]
this.OnEvent <- HubEvent{EventType: EVENT_HUBNAME_CHANGED, Nick: commandParts[1]}
case "$ValidateDenide": // sic
if len(this.Hco.NickPassword) > 0 {
this.OnEvent <- HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "Incorrect password."}
} else {
this.OnEvent <- HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "Nick already in use."}
}
case "$HubIsFull":
this.OnEvent <- HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "Hub is full."}
case "$BadPass":
this.OnEvent <- HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "Incorrect password."}
case "$GetPass":
this.SayRaw("$MyPass " + NMDCEscape(this.Hco.NickPassword) + "|")
case "$Quit":
delete(this.Users, commandParts[1])
this.OnEvent <- HubEvent{EventType: EVENT_USER_PART, Nick: commandParts[1]}
case "$MyINFO":
u := UserInfo{}
err := u.fromMyINFO(commandParts[1])
if err == nil {
this.userJoined_Full(&u)
} else {
this.OnEvent <- HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: err.Error()}
}
case "$NickList":
nicklist := strings.Split(commandParts[1], "$$")
for _, nick := range nicklist {
if len(nick) > 0 {
this.userJoined_NameOnly(nick)
}
}
case "$To":
if rx_incomingTo.MatchString(commandParts[1]) {
txparts := rx_incomingTo.FindStringSubmatch(commandParts[1])
if txparts[0] == this.Hco.Self.Nick && txparts[1] == txparts[2] {
this.OnEvent <- HubEvent{EventType: EVENT_PRIVATE, Nick: txparts[1], Message: txparts[3]}
break
}
}
this.OnEvent <- HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Malformed private message '" + commandParts[1] + "'"}
case "$UserIP":
// Final message in PtokaX connection handshake - trigger connection callback.
// This might not be the case for other hubsofts, though
if this.State != CONNECTIONSTATE_CONNECTED {
this.OnEvent <- HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTED}
this.State = CONNECTIONSTATE_CONNECTED
}
case "$UserCommand":
// TODO
case "$ForceMove":
// TODO
// IGNORABLE COMMANDS
case "$Supports":
case "$UserList":
case "$OpList":
case "$HubTopic":
case "$Search":
case "$ConnectToMe":
default:
this.OnEvent <- HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Unhandled protocol command '" + commandParts[0] + "'"}
}
}
func (this *HubConnection) worker() {
var fullBuffer string
for {
// If we're not connected, attempt reconnect
if this.conn == nil {
tmp, err := net.Dial("tcp", string(this.Hco.Address))
this.conn = tmp
if err == nil {
this.OnEvent <- HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTING}
}
}
// Read from socket into our local buffer (blocking)
readBuff := make([]byte, 4096)
nbytes, err := this.conn.Read(readBuff)
if nbytes > 0 {
fullBuffer += string(readBuff[0:nbytes])
}
// Maybe we disconnected
if err != nil {
this.OnEvent <- HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED}
this.conn = nil
time.Sleep(30 * time.Second) // Wait before reconnect
continue
}
// Attempt to parse a message block
for len(fullBuffer) > 0 {
for len(fullBuffer) > 0 && fullBuffer[0] == '|' {
fullBuffer = fullBuffer[1:]
}
protocolMessage := rx_protocolMessage.FindString(fullBuffer)
if len(protocolMessage) > 0 {
this.processProtocolMessage(string(protocolMessage))
fullBuffer = fullBuffer[len(protocolMessage):]
} else {
break
}
}
}
}
// Connects to an NMDC server, and spawns a background goroutine to handle
// protocol messages. Client code should select on all the interface channels.
func (this *HubConnectionOptions) Connect() *HubConnection {
if this.Self.ClientTag == "" {
this.Self.ClientTag = "libnmdc.go"
}
if this.NumEventsToBuffer < 1 {
this.NumEventsToBuffer = 1
}
hc := HubConnection{
Hco: this,
HubName: "(unknown)",
State: CONNECTIONSTATE_DISCONNECTED,
Users: make(map[string]UserInfo),
OnEvent: make(chan HubEvent, this.NumEventsToBuffer),
sentOurHello: false,
}
go hc.worker()
return &hc
}