Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4ea6cbb6f1 | |||
| b2ae91959e | |||
| f5fbce3ee6 | |||
| 2cbc8f8496 | |||
| 5bf30ed95f | |||
| a20fce3725 | |||
| 5c6435cf81 | |||
| b7dfcbc2f3 | |||
| 21847db9fd | |||
| b8122710f5 | |||
| f8c0fc54d8 | |||
| 4490444d1c | |||
| c5f655ff78 | |||
| 4415109174 | |||
| 0adc13ddfa | |||
| 59c56fea1b | |||
| 7f8f98aa5a | |||
| 8536cb0a5c | |||
| 553ec20850 | |||
| a403dac461 | |||
| 59a37b975c | |||
| a4e7d876cc |
25
.hgtags
25
.hgtags
@@ -1,16 +1,19 @@
|
||||
945ab4b16d05aa084f71bf5da9a3f687e0ec8bbd libnmdc-r1
|
||||
945ab4b16d05aa084f71bf5da9a3f687e0ec8bbd v0.1.0
|
||||
02a360e95480b97ddad83add5db48b2766339a99 nmdc-log-service-1.0.0
|
||||
137c1b65039e03c80379826a6efdfd808f6fbc8f libnmdc-r2
|
||||
d8b64d5527c2a5e4d76872e5bc3d69f7646135c6 libnmdc-r3
|
||||
137c1b65039e03c80379826a6efdfd808f6fbc8f v0.2.0
|
||||
d8b64d5527c2a5e4d76872e5bc3d69f7646135c6 v0.3.0
|
||||
fca41372e400853775b02e951f9db91d87f41adb nmdc-log-service-1.0.1
|
||||
050b424a7c5d5a27c9323c8810f3afbead1f5b96 libnmdc-r4
|
||||
050b424a7c5d5a27c9323c8810f3afbead1f5b96 v0.4.0
|
||||
da9f123633f9c28be6435ed7898139665d4c39d9 nmdc-log-service-1.0.2
|
||||
75a78f6a78f249a2cd8aa3d29f7e5e6319b4e03b libnmdc-r5
|
||||
75a78f6a78f249a2cd8aa3d29f7e5e6319b4e03b v0.5.0
|
||||
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
|
||||
3ecc037cf2d7080572fe87c2e39ecd153fb0e947 libnmdc-r10
|
||||
5149ffe70ea8475e480b682345b31aa45a3352db release-0.11
|
||||
cb86f3a40115cc46f450c0c83fd9b9d3b740e820 v0.6.0
|
||||
71343a2c641a438206d30ea7e75dc89a11dbef00 v0.7.0
|
||||
b0e57a5fcffdf4102d669db51a3648ddf66a0792 v0.8.0
|
||||
e7c2c71ef24b386add728fad35fff4a996fccbac v0.9.0
|
||||
3ecc037cf2d7080572fe87c2e39ecd153fb0e947 v0.10.0
|
||||
5149ffe70ea8475e480b682345b31aa45a3352db v0.11.0
|
||||
22b156a6fc2f6161765317f4ec9ab3731a26e0e2 v0.12.0
|
||||
3ee0f4ea5142d66079a9500bdcd48a53bdcf362f v0.13.0
|
||||
6422ed687cd308c339b6dc188bbe1034ed93f893 v0.14.0
|
||||
|
||||
@@ -15,7 +15,7 @@ func ExampleHubConnectionOptions_Connect() {
|
||||
event := <-hub.OnEvent
|
||||
switch event.EventType {
|
||||
case EVENT_CONNECTION_STATE_CHANGED:
|
||||
fmt.Printf("Connection -- %s (%s)\n", event.StateChange.Format(), event.Message)
|
||||
fmt.Printf("Connection -- %s (%s)\n", event.StateChange, event.Message)
|
||||
|
||||
case EVENT_PUBLIC:
|
||||
fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message)
|
||||
@@ -34,7 +34,7 @@ func ExampleHubConnectionOptions_ConnectSync() {
|
||||
cb := func(hub *HubConnection, event HubEvent) {
|
||||
switch event.EventType {
|
||||
case EVENT_CONNECTION_STATE_CHANGED:
|
||||
fmt.Printf("Connection -- %s (%s)\n", event.StateChange.Format(), event.Message)
|
||||
fmt.Printf("Connection -- %s (%s)\n", event.StateChange, event.Message)
|
||||
|
||||
case EVENT_PUBLIC:
|
||||
fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message)
|
||||
|
||||
@@ -2,6 +2,7 @@ package libnmdc
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -28,6 +29,9 @@ type HubConnection struct {
|
||||
connValid bool
|
||||
sentOurHello bool
|
||||
autoReconnect bool
|
||||
lastDataRecieved time.Time
|
||||
|
||||
supports map[string]struct{}
|
||||
}
|
||||
|
||||
// Thread-safe user accessor.
|
||||
@@ -85,6 +89,8 @@ func (this *HubConnection) userJoined_Full(uinf *UserInfo) {
|
||||
|
||||
if !userExisted {
|
||||
this.processEvent(HubEvent{EventType: EVENT_USER_JOINED, Nick: uinf.Nick})
|
||||
} else {
|
||||
this.processEvent(HubEvent{EventType: EVENT_USER_UPDATED_INFO, Nick: uinf.Nick})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,9 +137,8 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
||||
switch commandParts[0] {
|
||||
|
||||
case "$Lock":
|
||||
this.SayRaw("$Supports NoGetINFO UserCommand UserIP2|" +
|
||||
"$Key " + unlock([]byte(commandParts[1])) + "|" +
|
||||
"$ValidateNick " + Escape(this.Hco.Self.Nick) + "|")
|
||||
this.SayRaw("$Supports NoHello NoGetINFO UserCommand UserIP2 QuickList ChatOnly|" +
|
||||
"$Key " + unlock([]byte(commandParts[1])) + "|")
|
||||
this.sentOurHello = false
|
||||
|
||||
case "$Hello":
|
||||
@@ -185,12 +190,13 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
||||
case "$MyINFO":
|
||||
u := UserInfo{}
|
||||
err := u.fromMyINFO(commandParts[1])
|
||||
if err == nil {
|
||||
this.userJoined_Full(&u)
|
||||
} else {
|
||||
if err != nil {
|
||||
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
this.userJoined_Full(&u)
|
||||
|
||||
case "$NickList":
|
||||
nicklist := strings.Split(commandParts[1], "$$")
|
||||
for _, nick := range nicklist {
|
||||
@@ -241,11 +247,39 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
||||
}
|
||||
|
||||
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
|
||||
this.userLock.Lock()
|
||||
|
||||
pairs := strings.Split(commandParts[1], "$$")
|
||||
notifyOfUpdate := make([]string, 0, len(pairs))
|
||||
|
||||
nextIPPair:
|
||||
for _, pair := range pairs {
|
||||
parts := strings.SplitN(pair, " ", 2)
|
||||
if len(parts) != 2 {
|
||||
// ????
|
||||
continue nextIPPair
|
||||
}
|
||||
|
||||
ip2nick := parts[0]
|
||||
ip2addr := parts[1]
|
||||
|
||||
uinfo, ok := this.users[ip2nick]
|
||||
if !ok {
|
||||
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Recieved IP '" + ip2addr + "' for unknown user '" + ip2nick + "'"})
|
||||
continue nextIPPair
|
||||
}
|
||||
|
||||
if uinfo.IPAddress != ip2addr {
|
||||
uinfo.IPAddress = ip2addr
|
||||
notifyOfUpdate = append(notifyOfUpdate, ip2nick)
|
||||
this.users[ip2nick] = uinfo
|
||||
}
|
||||
}
|
||||
|
||||
this.userLock.Unlock()
|
||||
|
||||
for _, nick := range notifyOfUpdate {
|
||||
this.processEvent(HubEvent{EventType: EVENT_USER_UPDATED_INFO, Nick: nick})
|
||||
}
|
||||
|
||||
case "$ForceMove":
|
||||
@@ -273,8 +307,34 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
||||
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Malformed usercommand '" + commandParts[1] + "'"})
|
||||
}
|
||||
|
||||
// IGNORABLE COMMANDS
|
||||
case "$Supports":
|
||||
this.supports = make(map[string]struct{})
|
||||
for _, s := range strings.Split(commandParts[1], " ") {
|
||||
this.supports[s] = struct{}{}
|
||||
}
|
||||
|
||||
if !this.sentOurHello {
|
||||
|
||||
// Need to log in.
|
||||
// If the hub supports QuickList, we can skip one network roundtrip
|
||||
if _, ok := this.supports["QuickList"]; ok {
|
||||
this.SayInfo()
|
||||
this.SayRaw("$GetNickList|")
|
||||
} else {
|
||||
this.SayRaw("$ValidateNick " + Escape(this.Hco.Self.Nick) + "|")
|
||||
}
|
||||
|
||||
// This also counts as the end of the handshake from our POV. Consider
|
||||
// ourselves logged in
|
||||
this.sentOurHello = true
|
||||
if this.State != CONNECTIONSTATE_CONNECTED {
|
||||
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTED})
|
||||
this.State = CONNECTIONSTATE_CONNECTED
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// IGNORABLE COMMANDS
|
||||
case "$HubTopic":
|
||||
case "$Search":
|
||||
case "$ConnectToMe":
|
||||
@@ -340,6 +400,7 @@ func (this *HubConnection) worker() {
|
||||
}
|
||||
|
||||
if nbytes > 0 {
|
||||
this.lastDataRecieved = time.Now()
|
||||
fullBuffer += string(readBuff[0:nbytes])
|
||||
}
|
||||
}
|
||||
@@ -358,6 +419,10 @@ func (this *HubConnection) worker() {
|
||||
}
|
||||
}
|
||||
|
||||
if err == nil && time.Now().Sub(this.lastDataRecieved) > RECONNECT_IF_NO_DATA_RECIEVED_IN {
|
||||
err = fmt.Errorf("No packets recieved since %s, connection presumed lost", this.lastDataRecieved.Format(time.RFC3339))
|
||||
}
|
||||
|
||||
// Maybe we disconnected
|
||||
// Perform this check *last*, to ensure we've had a final shot at
|
||||
// clearing out any queued messages
|
||||
|
||||
@@ -24,6 +24,7 @@ type UserInfo struct {
|
||||
HubsRegistered uint64
|
||||
HubsOperator uint64
|
||||
IsOperator bool
|
||||
IPAddress string
|
||||
}
|
||||
|
||||
var rx_myinfo *regexp.Regexp
|
||||
|
||||
@@ -1,14 +1,32 @@
|
||||
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`.
|
||||
This package offers both channel-based and synchronous APIs. Example is included.
|
||||
|
||||
Written in golang
|
||||
Tags: nmdc
|
||||
|
||||
=GO GET=
|
||||
|
||||
This package can be installed via go get: `go get code.ivysaur.me/libnmdc`
|
||||
[go-get]code.ivysaur.me/libnmdc git https://git.ivysaur.me/code.ivysaur.me/libnmdc.git[/go-get]
|
||||
|
||||
=CHANGELOG=
|
||||
|
||||
2017-11-14 0.15
|
||||
- Feature: Fallback reconnection if no data (not even keepalives) are recieved from the hub in 24 hours
|
||||
- Fix an issue with detecting protocol messages inside multi-line chat messages
|
||||
- Update examples and the default client version number
|
||||
|
||||
2017-02-09 0.14
|
||||
- Fix an issue with crashing on malformed IP addresses supplied by the hub
|
||||
|
||||
2017-02-09 0.13
|
||||
- Feature: Implement UserIP2 extension, to retrieve IP addresses of other users
|
||||
- Enhancement: Implement QuickList extension (reduce one network roundtrip during initial connection)
|
||||
- Enhancement: Implement NoHello extension (faster connection performance)
|
||||
- Enhancement: Implement ChatOnly extension
|
||||
- Fix an issue with not notifying client on all MyINFO updates
|
||||
|
||||
2017-02-05 0.12
|
||||
- Fix an issue with mutex deadlock when accessing user information from a callback
|
||||
- Fix an issue with silent disconnection if a password was required but not present
|
||||
|
||||
14
libnmdc.go
14
libnmdc.go
@@ -10,10 +10,11 @@ import (
|
||||
|
||||
const (
|
||||
DEFAULT_CLIENT_TAG string = "libnmdc.go"
|
||||
DEFAULT_CLIENT_VERSION string = "0.11"
|
||||
DEFAULT_CLIENT_VERSION string = "0.15"
|
||||
DEFAULT_HUB_NAME string = "(unknown)"
|
||||
SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second
|
||||
AUTO_RECONNECT_AFTER time.Duration = 30 * time.Second
|
||||
RECONNECT_IF_NO_DATA_RECIEVED_IN time.Duration = 24 * time.Hour // we expect keepalives wayyyy more frequently than this
|
||||
)
|
||||
|
||||
var rx_protocolMessage *regexp.Regexp
|
||||
@@ -23,10 +24,13 @@ 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?([^\$]*)\$?(.*)`)
|
||||
// With the `m` flag, use \A instead of ^ to anchor to start
|
||||
// This fixes accidentally finding a better match in the middle of a multi-line message
|
||||
|
||||
rx_protocolMessage = regexp.MustCompile(`(?ms)\A[^|]*\|`)
|
||||
rx_publicChat = regexp.MustCompile(`(?ms)\A<([^>]*)> (.*)$`)
|
||||
rx_incomingTo = regexp.MustCompile(`(?ms)\A([^ ]+) From: ([^ ]+) \$<([^>]*)> (.*)`)
|
||||
rx_userCommand = regexp.MustCompile(`(?ms)\A(\d+) (\d+)\s?([^\$]*)\$?(.*)`)
|
||||
}
|
||||
|
||||
func Unescape(encoded string) string {
|
||||
|
||||
Reference in New Issue
Block a user