Compare commits
99 Commits
nmdc-log-s
...
v0.15.0
| 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 | |||
| 7c3069eabb | |||
| 3cc31323ad | |||
| 7f898dff32 | |||
| 58c1151278 | |||
| d1c549f1f2 | |||
| 9eb9cf0726 | |||
| f2abf8893c | |||
| 732622f4db | |||
| 95311e1479 | |||
| 084b629ad7 | |||
| 5713b58c7c | |||
| 56c6b7b352 | |||
| ea97afb01f | |||
| 482b0d3ad8 | |||
| 231bfeb247 | |||
| bd0425d6d4 | |||
| 5564eccf22 | |||
| d373e9791a | |||
| 7e249acd6c | |||
| b592e8ef7e | |||
| 11564b8c32 | |||
| 0a53963ec0 | |||
| b63a240e9b | |||
| 51b08dad3d | |||
| 1a3e4fe072 | |||
| 086281dab2 | |||
| 7392cbbea5 | |||
| 265c0a43ce | |||
| 417deff347 | |||
| a996f1668c | |||
| 9b290ebb96 | |||
| 756b347592 | |||
| d4e442bd84 | |||
| bbdc18698d | |||
| b049acad90 | |||
| df0211c67d | |||
| 861090b0e9 | |||
| cc15a571a8 | |||
| 3c4972d930 | |||
| 43db3a0d64 | |||
| 8e4a45a2b9 | |||
| f350afa0fe | |||
| dea4f996f9 | |||
| 0fee719b90 | |||
| 15455ff031 | |||
| a287b3adcc | |||
| d6d9a1711c | |||
| 1858dfa102 | |||
| 89b67f8559 | |||
| 649b3dfcab | |||
| ed64887ddc | |||
| 2113139cdc | |||
| 07441628d9 | |||
| 34dd9d515c | |||
| 32f64690c9 | |||
| 3f66cd93c7 | |||
| 9b32682c8a | |||
| c9e95df0df | |||
| a2b94e8724 | |||
| 3dfef302ee | |||
| 611a934a4e | |||
| 1a3617b960 | |||
| a37b1773d0 | |||
| 09301f138c | |||
| 8d7b0b46af | |||
| 73f6d66cbd | |||
| 019f67a7de | |||
| debc14cea3 | |||
| ba378a8245 | |||
| e3a92da5f6 | |||
| 99e4ebb842 | |||
| 4499c7306d | |||
| d391159abe | |||
| e111c99886 | |||
| 393f156b0e | |||
| 7cd49c2c8a | |||
| 163b6623f8 |
20
.hgtags
20
.hgtags
@@ -1,3 +1,19 @@
|
||||
945ab4b16d05aa084f71bf5da9a3f687e0ec8bbd libnmdc-r1
|
||||
945ab4b16d05aa084f71bf5da9a3f687e0ec8bbd v0.1.0
|
||||
02a360e95480b97ddad83add5db48b2766339a99 nmdc-log-service-1.0.0
|
||||
137c1b65039e03c80379826a6efdfd808f6fbc8f libnmdc-r2
|
||||
137c1b65039e03c80379826a6efdfd808f6fbc8f v0.2.0
|
||||
d8b64d5527c2a5e4d76872e5bc3d69f7646135c6 v0.3.0
|
||||
fca41372e400853775b02e951f9db91d87f41adb nmdc-log-service-1.0.1
|
||||
050b424a7c5d5a27c9323c8810f3afbead1f5b96 v0.4.0
|
||||
da9f123633f9c28be6435ed7898139665d4c39d9 nmdc-log-service-1.0.2
|
||||
75a78f6a78f249a2cd8aa3d29f7e5e6319b4e03b v0.5.0
|
||||
4116422bb10229d887f9296970a166fa1ef8c5fd nmdc-log-service-1.0.3
|
||||
cb86f3a40115cc46f450c0c83fd9b9d3b740e820 nmdc-log-service-1.0.4
|
||||
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
|
||||
|
||||
29
ConnectionMode.go
Normal file
29
ConnectionMode.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type ConnectionMode rune
|
||||
|
||||
const (
|
||||
CONNECTIONMODE_ACTIVE ConnectionMode = 'A' // 65
|
||||
CONNECTIONMODE_PASSIVE ConnectionMode = 'P' // 49
|
||||
CONNECTIONMODE_SOCKS5 ConnectionMode = '5' // 53
|
||||
)
|
||||
|
||||
func (this ConnectionMode) String() string {
|
||||
switch this {
|
||||
case CONNECTIONMODE_ACTIVE:
|
||||
return "Active"
|
||||
|
||||
case CONNECTIONMODE_PASSIVE:
|
||||
return "Passive"
|
||||
|
||||
case CONNECTIONMODE_SOCKS5:
|
||||
return "SOCKS5"
|
||||
|
||||
default:
|
||||
return fmt.Sprintf("ConnectionMode(\"%s\")", string(this))
|
||||
}
|
||||
}
|
||||
40
ConnectionState.go
Normal file
40
ConnectionState.go
Normal 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) String() 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
|
||||
}
|
||||
}
|
||||
58
Example_test.go
Normal file
58
Example_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func ExampleHubConnectionOptions_Connect() {
|
||||
opts := HubConnectionOptions{
|
||||
Address: "127.0.0.1",
|
||||
Self: *NewUserInfo("slowpoke9"),
|
||||
}
|
||||
hub := opts.Connect()
|
||||
|
||||
for {
|
||||
event := <-hub.OnEvent
|
||||
switch event.EventType {
|
||||
case EVENT_CONNECTION_STATE_CHANGED:
|
||||
fmt.Printf("Connection -- %s (%s)\n", event.StateChange, event.Message)
|
||||
|
||||
case EVENT_PUBLIC:
|
||||
fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message)
|
||||
if event.Message == "how are you" {
|
||||
hub.SayPublic("good thanks!")
|
||||
}
|
||||
|
||||
default:
|
||||
fmt.Printf("%+v\n", event)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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, event.Message)
|
||||
|
||||
case EVENT_PUBLIC:
|
||||
fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message)
|
||||
if event.Message == "how are you" {
|
||||
hub.SayPublic("good thanks!")
|
||||
}
|
||||
|
||||
default:
|
||||
fmt.Printf("%+v\n", event)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
opts := HubConnectionOptions{
|
||||
Address: "127.0.0.1",
|
||||
Self: *NewUserInfo("slowpoke9"),
|
||||
OnEventSync: cb,
|
||||
}
|
||||
|
||||
opts.ConnectSync() // blocking
|
||||
}
|
||||
35
HubAddress.go
Normal file
35
HubAddress.go
Normal 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
|
||||
}
|
||||
445
HubConnection.go
Normal file
445
HubConnection.go
Normal file
@@ -0,0 +1,445 @@
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"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
|
||||
lastDataRecieved time.Time
|
||||
|
||||
supports map[string]struct{}
|
||||
}
|
||||
|
||||
// Thread-safe user accessor.
|
||||
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 + "> " + Escape(message) + "|")
|
||||
}
|
||||
|
||||
func (this *HubConnection) SayPrivate(recipient string, message string) {
|
||||
this.SayRaw("$To: " + recipient + " From: " + this.Hco.Self.Nick + " $<" + this.Hco.Self.Nick + "> " + Escape(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()
|
||||
this.users[nick] = *NewUserInfo(nick)
|
||||
this.userLock.Unlock() // Don't lock over a processEvent boundary
|
||||
|
||||
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()
|
||||
_, userExisted := this.users[uinf.Nick] // don't use UserExists as it would deadlock the mutex
|
||||
this.users[uinf.Nick] = *uinf
|
||||
this.userLock.Unlock() // Don't lock over a processEvent boundary
|
||||
|
||||
if !userExisted {
|
||||
this.processEvent(HubEvent{EventType: EVENT_USER_JOINED, Nick: uinf.Nick})
|
||||
} else {
|
||||
this.processEvent(HubEvent{EventType: EVENT_USER_UPDATED_INFO, Nick: uinf.Nick})
|
||||
}
|
||||
}
|
||||
|
||||
// SayRaw sends raw bytes over the TCP socket. Callers should add the protocol
|
||||
// terminating character `|` themselves.
|
||||
// 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: Unescape(pubchat_parts[2])})
|
||||
return
|
||||
}
|
||||
|
||||
// System messages
|
||||
// ```````````````
|
||||
if message[0] != '$' {
|
||||
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_HUB, Nick: this.HubName, Message: Unescape(message)})
|
||||
return
|
||||
}
|
||||
|
||||
// Protocol messages
|
||||
// `````````````````
|
||||
|
||||
commandParts := strings.SplitN(message, " ", 2)
|
||||
switch commandParts[0] {
|
||||
|
||||
case "$Lock":
|
||||
this.SayRaw("$Supports NoHello NoGetINFO UserCommand UserIP2 QuickList ChatOnly|" +
|
||||
"$Key " + unlock([]byte(commandParts[1])) + "|")
|
||||
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":
|
||||
if len(this.Hco.NickPassword) == 0 {
|
||||
// We've got a problem. MyPass with no arguments is a syntax error with no message = instant close
|
||||
// Just drop the connection
|
||||
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "This account is passworded."})
|
||||
this.Disconnect()
|
||||
} else {
|
||||
this.SayRaw("$MyPass " + Escape(this.Hco.NickPassword) + "|")
|
||||
}
|
||||
|
||||
case "$Quit":
|
||||
this.userLock.Lock()
|
||||
delete(this.users, commandParts[1])
|
||||
this.userLock.Unlock() // Don't lock over a processEvent boundary
|
||||
|
||||
this.processEvent(HubEvent{EventType: EVENT_USER_PART, Nick: commandParts[1]})
|
||||
|
||||
case "$MyINFO":
|
||||
u := UserInfo{}
|
||||
err := u.fromMyINFO(commandParts[1])
|
||||
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 {
|
||||
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: Unescape(txparts[4])})
|
||||
valid = true
|
||||
}
|
||||
}
|
||||
|
||||
if !valid {
|
||||
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Malformed private message '" + commandParts[1] + "'"})
|
||||
}
|
||||
|
||||
case "$UserIP":
|
||||
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":
|
||||
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: Unescape(usc[4]),
|
||||
}
|
||||
|
||||
this.processEvent(HubEvent{EventType: EVENT_USERCOMMAND, UserCommand: &uscStruct})
|
||||
|
||||
} else {
|
||||
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Malformed usercommand '" + commandParts[1] + "'"})
|
||||
}
|
||||
|
||||
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":
|
||||
|
||||
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 {
|
||||
this.lastDataRecieved = time.Now()
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
68
HubConnectionOptions.go
Normal file
68
HubConnectionOptions.go
Normal file
@@ -0,0 +1,68 @@
|
||||
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(*HubConnection, HubEvent)
|
||||
}
|
||||
|
||||
func (this *HubConnectionOptions) prepareConnection() *HubConnection {
|
||||
if this.Self.ClientTag == "" {
|
||||
this.Self.ClientTag = DEFAULT_CLIENT_TAG
|
||||
this.Self.ClientVersion = DEFAULT_CLIENT_VERSION
|
||||
}
|
||||
|
||||
// Shouldn't be blank either
|
||||
if this.Self.ClientVersion == "" {
|
||||
this.Self.ClientVersion = "0"
|
||||
}
|
||||
|
||||
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.OnEvent = nil
|
||||
hc.processEvent = func(ev HubEvent) {
|
||||
this.OnEventSync(hc, ev)
|
||||
}
|
||||
hc.worker()
|
||||
}
|
||||
25
HubEvent.go
Normal file
25
HubEvent.go
Normal 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
26
UserCommand.go
Normal 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
|
||||
}
|
||||
17
UserFlag.go
Normal file
17
UserFlag.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package libnmdc
|
||||
|
||||
type UserFlag byte
|
||||
|
||||
const (
|
||||
FLAG_NORMAL UserFlag = 1
|
||||
FLAG_AWAY_1 UserFlag = 2
|
||||
FLAG_AWAY_2 UserFlag = 3
|
||||
FLAG_SERVER_1 UserFlag = 4
|
||||
FLAG_SERVER_2 UserFlag = 5
|
||||
FLAG_SERVER_AWAY_1 UserFlag = 6
|
||||
FLAG_SERVER_AWAY_2 UserFlag = 7
|
||||
FLAG_FIREBALL_1 UserFlag = 8
|
||||
FLAG_FIREBALL_2 UserFlag = 9
|
||||
FLAG_FIREBALL_AWAY_1 UserFlag = 10
|
||||
FLAG_FIREBALL_AWAY_2 UserFlag = 11
|
||||
)
|
||||
133
UserInfo.go
Normal file
133
UserInfo.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// This structure represents a user connected to a hub.
|
||||
type UserInfo struct {
|
||||
Nick string
|
||||
Description string
|
||||
ClientTag string
|
||||
ClientVersion string
|
||||
Email string
|
||||
ShareSize uint64
|
||||
ConnectionMode ConnectionMode
|
||||
Flag UserFlag
|
||||
Slots uint64
|
||||
Speed string
|
||||
HubsUnregistered uint64
|
||||
HubsRegistered uint64
|
||||
HubsOperator uint64
|
||||
IsOperator bool
|
||||
IPAddress string
|
||||
}
|
||||
|
||||
var rx_myinfo *regexp.Regexp
|
||||
var rx_myinfo_notag *regexp.Regexp
|
||||
|
||||
func init() {
|
||||
// Format: $ALL <nick> <description>$ $<connection><flag>$<e-mail>$<sharesize>$
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func NewUserInfo(username string) *UserInfo {
|
||||
return &UserInfo{
|
||||
Nick: username,
|
||||
ConnectionMode: CONNECTIONMODE_PASSIVE,
|
||||
HubsUnregistered: 1,
|
||||
}
|
||||
}
|
||||
|
||||
func maybeParse(str string, dest *uint64, default_val uint64) {
|
||||
sz, err := strconv.ParseUint(str, 10, 64)
|
||||
if err == nil {
|
||||
*dest = sz
|
||||
} else {
|
||||
*dest = default_val
|
||||
}
|
||||
}
|
||||
|
||||
func (this *UserInfo) fromMyINFO(protomsg string) error {
|
||||
|
||||
// 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 = Unescape(matches[2])
|
||||
this.ClientTag = Unescape(matches[3])
|
||||
this.ClientVersion = matches[4]
|
||||
this.ConnectionMode = ConnectionMode(matches[5][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 = Unescape(matches[11])
|
||||
maybeParse(matches[12], &this.ShareSize, 0)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// No-tag format, used in early connection
|
||||
matches = rx_myinfo_notag.FindStringSubmatch(protomsg)
|
||||
if matches != nil {
|
||||
this.Nick = matches[1]
|
||||
this.Description = Unescape(matches[2])
|
||||
this.ClientTag = ""
|
||||
this.ClientVersion = "0"
|
||||
this.ConnectionMode = CONNECTIONMODE_PASSIVE
|
||||
this.HubsUnregistered = 0
|
||||
this.HubsRegistered = 0
|
||||
this.HubsOperator = 0
|
||||
this.Slots = 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 = Unescape(matches[4])
|
||||
maybeParse(matches[5], &this.ShareSize, 0)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Couldn't get anything out of it...
|
||||
return errors.New("Malformed MyINFO")
|
||||
}
|
||||
|
||||
// Returns the MyINFO command, WITH leading $MyINFO, and WITHOUT trailing pipe
|
||||
func (this *UserInfo) toMyINFO() string {
|
||||
return fmt.Sprintf(
|
||||
"$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,
|
||||
this.HubsOperator,
|
||||
this.Slots,
|
||||
this.Speed,
|
||||
this.Flag,
|
||||
this.Email,
|
||||
this.ShareSize,
|
||||
)
|
||||
}
|
||||
88
UserInfo_test.go
Normal file
88
UserInfo_test.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type myInfoTestPair struct {
|
||||
in string
|
||||
expect UserInfo
|
||||
}
|
||||
|
||||
func TestMyINFOParse(t *testing.T) {
|
||||
|
||||
cases := []myInfoTestPair{
|
||||
|
||||
myInfoTestPair{
|
||||
in: "$ALL Bxxxy description<ApexDC++ V:1.4.3,M:P,H:9/0/2,S:1>$ $0.01\x01$xyz@example.com$53054999578$",
|
||||
expect: UserInfo{
|
||||
Nick: "Bxxxy",
|
||||
Description: "description",
|
||||
ClientTag: "ApexDC++",
|
||||
ClientVersion: "1.4.3",
|
||||
Email: "xyz@example.com",
|
||||
ShareSize: 53054999578,
|
||||
ConnectionMode: CONNECTIONMODE_PASSIVE,
|
||||
Flag: FLAG_NORMAL,
|
||||
Slots: 1,
|
||||
Speed: "0.0",
|
||||
HubsUnregistered: 9,
|
||||
HubsRegistered: 0,
|
||||
HubsOperator: 2,
|
||||
},
|
||||
},
|
||||
myInfoTestPair{
|
||||
in: "$ALL ixxxxxxx0 $P$10A$$0$",
|
||||
expect: UserInfo{
|
||||
Nick: "ixxxxxxx0",
|
||||
ClientVersion: "0", // Auto-inserted by the parser for short-format MyINFO strings
|
||||
ConnectionMode: CONNECTIONMODE_PASSIVE,
|
||||
Flag: UserFlag(rune('A')),
|
||||
Speed: "1",
|
||||
},
|
||||
},
|
||||
myInfoTestPair{
|
||||
in: "$ALL SXXXX_XXXXXXR <ncdc V:1.19.1-12-g5561,M:P,H:1/0/0,S:10>$ $0.005Q$$0$",
|
||||
expect: UserInfo{
|
||||
Nick: "SXXXX_XXXXXXR",
|
||||
ClientTag: "ncdc",
|
||||
ClientVersion: "1.19.1-12-g5561",
|
||||
ConnectionMode: CONNECTIONMODE_PASSIVE,
|
||||
Flag: UserFlag(rune('Q')),
|
||||
Slots: 10,
|
||||
Speed: "0.00",
|
||||
HubsUnregistered: 1,
|
||||
},
|
||||
},
|
||||
myInfoTestPair{
|
||||
in: "$ALL mxxxu desccccc<HexChat V:2.12.1,M:P,H:1/0/0,S:0>$ $p$$0$",
|
||||
expect: UserInfo{
|
||||
Nick: "mxxxu",
|
||||
Description: "desccccc",
|
||||
ClientTag: "HexChat",
|
||||
ClientVersion: "2.12.1",
|
||||
ConnectionMode: CONNECTIONMODE_PASSIVE,
|
||||
Flag: UserFlag(rune('p')),
|
||||
HubsUnregistered: 1,
|
||||
Slots: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, v := range cases {
|
||||
|
||||
got := UserInfo{}
|
||||
err := got.fromMyINFO(v.in)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("MyINFO parse warning (%s)", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
if got != v.expect {
|
||||
t.Errorf("MyINFO parse failure\nExpected:\n%+v\nGot:\n%+v\n", v.expect, got)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
84
__dist/README.txt
Normal file
84
__dist/README.txt
Normal file
@@ -0,0 +1,84 @@
|
||||
An NMDC client protocol library for Golang.
|
||||
|
||||
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
|
||||
|
||||
2016-11-29 0.11
|
||||
- BREAKING: Remove some exported methods
|
||||
- BREAKING: Fix an issue with missing sufficient parameters in the synchronous API
|
||||
- Enhancement: Improve output under godoc
|
||||
- Fix an issue with special characters appearing in recieved private messages
|
||||
- Fix an issue with parsing active/passive connection modes
|
||||
- Fix an issue with errors appearing on stdout
|
||||
|
||||
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
|
||||
68
libnmdc.go
Normal file
68
libnmdc.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_CLIENT_TAG string = "libnmdc.go"
|
||||
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
|
||||
var rx_publicChat *regexp.Regexp
|
||||
var rx_incomingTo *regexp.Regexp
|
||||
var rx_userCommand *regexp.Regexp
|
||||
var ErrNotConnected error = errors.New("Not connected")
|
||||
|
||||
func init() {
|
||||
// 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 {
|
||||
v1 := strings.Replace(encoded, "$", "$", -1)
|
||||
v2 := strings.Replace(v1, "|", "|", -1)
|
||||
return strings.Replace(v2, "&", "&", -1)
|
||||
}
|
||||
|
||||
func Escape(plaintext string) string {
|
||||
v1 := strings.Replace(plaintext, "&", "&", -1)
|
||||
v2 := strings.Replace(v1, "|", "|", -1)
|
||||
return strings.Replace(v2, "$", "$", -1)
|
||||
}
|
||||
|
||||
func unlock(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
|
||||
}
|
||||
@@ -1,152 +0,0 @@
|
||||
// libnmdc project UserInfo.go
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type UserFlag byte
|
||||
|
||||
const (
|
||||
FLAG_NORMAL UserFlag = 1
|
||||
FLAG_AWAY_1 UserFlag = 2
|
||||
FLAG_AWAY_2 UserFlag = 3
|
||||
FLAG_SERVER_1 UserFlag = 4
|
||||
FLAG_SERVER_2 UserFlag = 5
|
||||
FLAG_SERVER_AWAY_1 UserFlag = 6
|
||||
FLAG_SERVER_AWAY_2 UserFlag = 7
|
||||
FLAG_FIREBALL_1 UserFlag = 8
|
||||
FLAG_FIREBALL_2 UserFlag = 9
|
||||
FLAG_FIREBALL_AWAY_1 UserFlag = 10
|
||||
FLAG_FIREBALL_AWAY_2 UserFlag = 11
|
||||
)
|
||||
|
||||
type ConnectionMode rune
|
||||
|
||||
const (
|
||||
CONNECTIONMODE_ACTIVE ConnectionMode = 'A'
|
||||
CONNECTIONMODE_PASSIVE ConnectionMode = 'P'
|
||||
CONNECTIONMODE_SOCKS5 ConnectionMode = '5'
|
||||
)
|
||||
|
||||
// This structure represents a user connected to a hub.
|
||||
type UserInfo struct {
|
||||
Nick string
|
||||
Description string
|
||||
ClientTag string
|
||||
Email string
|
||||
ShareSize uint64
|
||||
ConnectionMode ConnectionMode
|
||||
Flag UserFlag
|
||||
Slots uint64
|
||||
Speed string
|
||||
HubsUnregistered uint64
|
||||
HubsRegistered uint64
|
||||
HubsOperator uint64
|
||||
}
|
||||
|
||||
var rx_myinfo *regexp.Regexp
|
||||
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
|
||||
|
||||
/*
|
||||
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$"
|
||||
|
||||
u := UserInfo{}
|
||||
err := u.fromMyINFO(sample)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
} else {
|
||||
fmt.Printf("%+v\n", u)
|
||||
}
|
||||
|
||||
os.Exit(1)
|
||||
*/
|
||||
}
|
||||
|
||||
func NewUserInfo(username string) *UserInfo {
|
||||
return &UserInfo{
|
||||
Nick: username,
|
||||
ConnectionMode: CONNECTIONMODE_PASSIVE,
|
||||
HubsUnregistered: 1,
|
||||
}
|
||||
}
|
||||
|
||||
func maybeParse(str string, dest *uint64, default_val uint64) {
|
||||
sz, err := strconv.ParseUint(str, 10, 64)
|
||||
if err == nil {
|
||||
*dest = sz
|
||||
} else {
|
||||
*dest = default_val
|
||||
}
|
||||
}
|
||||
|
||||
func (this *UserInfo) fromMyINFO(protomsg string) error {
|
||||
// Normal format (with tag in exact 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.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])
|
||||
this.Email = NMDCUnescape(matches[11])
|
||||
maybeParse(matches[12], &this.ShareSize, 0)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// No-tag format, used in early connection
|
||||
matches = rx_myinfo_notag.FindStringSubmatch(protomsg)
|
||||
if matches != nil {
|
||||
this.Nick = matches[1]
|
||||
this.Description = NMDCUnescape(matches[2])
|
||||
this.ClientTag = ""
|
||||
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)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Couldn't get anything out of it...
|
||||
return errors.New("Malformed MyINFO")
|
||||
}
|
||||
|
||||
// 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$",
|
||||
this.Nick,
|
||||
this.Description,
|
||||
this.ClientTag,
|
||||
this.ConnectionMode,
|
||||
this.HubsUnregistered,
|
||||
this.HubsRegistered,
|
||||
this.HubsOperator,
|
||||
this.Slots,
|
||||
this.Speed,
|
||||
this.Flag,
|
||||
this.Email,
|
||||
this.ShareSize,
|
||||
)
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
An NMDC client protocol library for Golang.
|
||||
|
||||
- Channel-based API.
|
||||
- Includes a sample logging client.
|
||||
- This code hosting site isn't (yet) compatible with `go get`.
|
||||
|
||||
Written in golang
|
||||
Tags: nmdc
|
||||
|
||||
=CHANGELOG=
|
||||
|
||||
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
|
||||
@@ -1,410 +0,0 @@
|
||||
// libnmdc project libnmdc.go
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ConnectionState int
|
||||
type HubEventType int
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
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: ([^ ]+) \\$<([^>]*)> (.*)")
|
||||
}
|
||||
|
||||
type HubConnectionOptions struct {
|
||||
Address HubAddress
|
||||
SkipVerifyTLS bool // using a negative verb, because bools default to false
|
||||
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 // this is an interface
|
||||
connValid bool
|
||||
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, "$", "$", -1)
|
||||
v2 := strings.Replace(v1, "|", "|", -1)
|
||||
return strings.Replace(v2, "&", "&", -1)
|
||||
}
|
||||
|
||||
func NMDCEscape(plaintext string) string {
|
||||
v1 := strings.Replace(plaintext, "&", "&", -1)
|
||||
v2 := strings.Replace(v1, "|", "|", -1)
|
||||
return strings.Replace(v2, "$", "$", -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 {
|
||||
if this.connValid {
|
||||
_, err := this.conn.Write([]byte(protocolCommand))
|
||||
return err
|
||||
} else {
|
||||
return ErrNotConnected
|
||||
}
|
||||
}
|
||||
|
||||
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:":
|
||||
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.OnEvent <- HubEvent{EventType: EVENT_PRIVATE, Nick: txparts[2], Message: txparts[4]}
|
||||
valid = true
|
||||
}
|
||||
}
|
||||
|
||||
if !valid {
|
||||
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
|
||||
var err error = nil
|
||||
var nbytes int = 0
|
||||
|
||||
for {
|
||||
|
||||
// If we're not connected, attempt reconnect
|
||||
if this.conn == nil {
|
||||
|
||||
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.OnEvent <- HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTING}
|
||||
this.connValid = false
|
||||
} else {
|
||||
this.connValid = true
|
||||
}
|
||||
}
|
||||
|
||||
// Read from socket into our local buffer (blocking)
|
||||
if this.connValid {
|
||||
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, Message: err.Error()}
|
||||
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
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
// +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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"libnmdc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
opts := libnmdc.HubConnectionOptions{
|
||||
Address: "127.0.0.1",
|
||||
Self: libnmdc.UserInfo{Nick: "slowpoke9"},
|
||||
}
|
||||
hub := opts.Connect()
|
||||
|
||||
for {
|
||||
event := <-hub.OnEvent
|
||||
switch event.EventType {
|
||||
case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
|
||||
fmt.Printf("Connection -- %s (%s)\n", event.StateChange.Format(), event.Message)
|
||||
|
||||
case libnmdc.EVENT_PUBLIC:
|
||||
fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message)
|
||||
if event.Message == "how are you" {
|
||||
hub.SayPublic("good thanks!")
|
||||
}
|
||||
|
||||
default:
|
||||
fmt.Printf("%+v\n", event)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
A logging service for NMDC hubs.
|
||||
|
||||
It logs public chat messages to a file, categorised by months. Binaries are provided for Windows/Linux amd64/i386.
|
||||
|
||||
Written in golang
|
||||
Tags: nmdc
|
||||
|
||||
=USAGE=
|
||||
|
||||
`$nmdc-log-service -Help
|
||||
|
||||
Usage of nmdc-log-service:
|
||||
-Debug
|
||||
Print additional information on stdout
|
||||
-Dir string
|
||||
Output directory (default ".")
|
||||
-LogConnectionState
|
||||
Include connection state changes in log (default true)
|
||||
-Nick string
|
||||
Nick (default "nmdc-log-service")
|
||||
-PMResponse string
|
||||
Message to respond with on PM (default "This is an automated service. For enquiries, please contact an administrator.")
|
||||
-Password string
|
||||
Registered nick password
|
||||
-Server string
|
||||
Addresses to connect to (comma-separated)`
|
||||
|
||||
=CHANGELOG=
|
||||
|
||||
2016-04-02 1.0.0
|
||||
- Initial public release
|
||||
@@ -1,49 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -eu
|
||||
|
||||
export GOPATH=$(
|
||||
cd ../../
|
||||
cygpath -w "$(pwd)"
|
||||
)
|
||||
|
||||
main() {
|
||||
|
||||
local version=""
|
||||
read -p "Enter version string (blank for timestamp)> " version
|
||||
if [[ $version == "" ]] ; then
|
||||
version=$(date +%s)
|
||||
fi
|
||||
echo "Using '${version}' as the version."
|
||||
|
||||
if [[ -f nmdc-log-service.exe ]] ; then
|
||||
rm ./nmdc-log-service.exe
|
||||
fi
|
||||
if [[ -f nmdc-log-service ]] ; then
|
||||
rm ./nmdc-log-service
|
||||
fi
|
||||
|
||||
echo "Building win64..."
|
||||
GOARCH=amd64 GOOS=windows go build -a -ldflags -s -o nmdc-log-service.exe
|
||||
7z a -mx9 nmdc-log-service-${version}-win64.7z nmdc-log-service.exe >/dev/null
|
||||
rm ./nmdc-log-service.exe
|
||||
|
||||
echo "Building win32..."
|
||||
GOARCH=386 GOOS=windows go build -a -ldflags -s -o nmdc-log-service.exe
|
||||
7z a -mx9 nmdc-log-service-${version}-win32.7z nmdc-log-service.exe >/dev/null
|
||||
rm ./nmdc-log-service.exe
|
||||
|
||||
echo "Building linux64..."
|
||||
GOARCH=amd64 GOOS=linux go build -a -ldflags -s -o nmdc-log-service
|
||||
XZ_OPT=-9 tar caf nmdc-log-service-${version}-linux64.tar.xz nmdc-log-service --owner=0 --group=0
|
||||
rm ./nmdc-log-service
|
||||
|
||||
echo "Building linux32..."
|
||||
GOARCH=386 GOOS=linux go build -a -ldflags -s -o nmdc-log-service
|
||||
XZ_OPT=-9 tar caf nmdc-log-service-${version}-linux32.tar.xz nmdc-log-service --owner=0 --group=0
|
||||
rm ./nmdc-log-service
|
||||
|
||||
echo "Build complete."
|
||||
}
|
||||
|
||||
main "$@"
|
||||
@@ -1,153 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"libnmdc"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var BaseDir string = "."
|
||||
var PMResponse string = ""
|
||||
var LogConnectionState bool
|
||||
var DebugMode bool
|
||||
var VerifyTLS bool
|
||||
var CharacterMatcher *regexp.Regexp
|
||||
|
||||
func init() {
|
||||
CharacterMatcher = regexp.MustCompile("[^a-zA-Z0-9]")
|
||||
}
|
||||
|
||||
func GetDirectoryNameForHub(hub string) string {
|
||||
return filepath.Join(BaseDir, CharacterMatcher.ReplaceAllString(hub, "_"))
|
||||
}
|
||||
|
||||
func LogMessage(hub, message string) {
|
||||
|
||||
instant := time.Now() // using system timezone
|
||||
|
||||
// Log file path
|
||||
monthStamp := instant.Format("2006-01")
|
||||
logFilePath := filepath.Join(GetDirectoryNameForHub(hub), monthStamp+".log")
|
||||
|
||||
// Produce a timestamp
|
||||
timeStamp := instant.Format("2006-01-02 15:04:05")
|
||||
|
||||
fh, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Couldn't open file '%s': %s\n", logFilePath, err.Error())
|
||||
return
|
||||
}
|
||||
defer fh.Close()
|
||||
|
||||
_, err = fh.WriteString("[" + timeStamp + "] " + message + "\n")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error writing to file '%s': %s\n", logFilePath, err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func HubWorker(addr, nick, password string) {
|
||||
|
||||
opts := libnmdc.HubConnectionOptions{
|
||||
Address: libnmdc.HubAddress(addr),
|
||||
SkipVerifyTLS: !VerifyTLS,
|
||||
Self: libnmdc.UserInfo{Nick: nick},
|
||||
NickPassword: password,
|
||||
}
|
||||
hub := opts.Connect()
|
||||
|
||||
for {
|
||||
event := <-hub.OnEvent
|
||||
|
||||
if DebugMode {
|
||||
fmt.Printf("DEBUG: %s %v\n", addr, event)
|
||||
}
|
||||
|
||||
switch event.EventType {
|
||||
case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
|
||||
if LogConnectionState {
|
||||
if len(event.Message) > 0 {
|
||||
LogMessage(addr, "* "+event.StateChange.Format()+" ("+event.Message+")")
|
||||
} else {
|
||||
LogMessage(addr, "* "+event.StateChange.Format())
|
||||
}
|
||||
}
|
||||
|
||||
case libnmdc.EVENT_PUBLIC:
|
||||
LogMessage(addr, "<"+event.Nick+"> "+event.Message)
|
||||
|
||||
case libnmdc.EVENT_PRIVATE:
|
||||
fmt.Printf("Got PM %v\n", event)
|
||||
hub.SayPrivate(event.Nick, PMResponse)
|
||||
|
||||
case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_SYSTEM_MESSAGE_FROM_HUB:
|
||||
if strings.HasPrefix(event.Message, "* ") {
|
||||
LogMessage(addr, event.Message)
|
||||
} else {
|
||||
LogMessage(addr, "* "+event.Message)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
// Parse arguments
|
||||
hubs := flag.String("Server", "", "Addresses to connect to (comma-separated)")
|
||||
nick := flag.String("Nick", "nmdc-log-service", "Nick")
|
||||
password := flag.String("Password", "", "Registered nick password")
|
||||
flag.StringVar(&BaseDir, "Dir", ".", "Output directory")
|
||||
flag.BoolVar(&LogConnectionState, "LogConnectionState", true, "Include connection state changes in log")
|
||||
flag.StringVar(&PMResponse, "PMResponse", "This is an automated service. For enquiries, please contact an administrator.", "Message to respond with on PM")
|
||||
flag.BoolVar(&DebugMode, "Debug", false, "Print additional information on stdout")
|
||||
flag.BoolVar(&VerifyTLS, "VerifyTLS", true, "Verify TLS certificates")
|
||||
flag.Parse()
|
||||
|
||||
// Assert dir exists
|
||||
dinfo, err := os.Stat(BaseDir)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "FATAL: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
if !dinfo.IsDir() {
|
||||
fmt.Fprintf(os.Stderr, "FATAL: Path '%s' is not a directory\n", BaseDir)
|
||||
}
|
||||
|
||||
if !VerifyTLS {
|
||||
fmt.Fprintf(os.Stderr, "WARNING: TLS certificates will not be verified!\n")
|
||||
}
|
||||
|
||||
// Launch loggers
|
||||
all_hubs := strings.Split(*hubs, ",")
|
||||
launch_ct := 0
|
||||
for _, hubaddr := range all_hubs {
|
||||
if len(hubaddr) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// Assert target directory exists
|
||||
os.MkdirAll(GetDirectoryNameForHub(hubaddr), 0755)
|
||||
|
||||
// Launch logger
|
||||
go HubWorker(hubaddr, *nick, *password)
|
||||
|
||||
launch_ct++
|
||||
}
|
||||
|
||||
if launch_ct == 0 {
|
||||
fmt.Fprintln(os.Stderr, "FATAL: No hubs specified")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Wait forever
|
||||
var forever chan bool = nil
|
||||
select {
|
||||
case <-forever:
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user