Compare commits
46 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 |
6
.hgtags
6
.hgtags
@@ -9,3 +9,9 @@ da9f123633f9c28be6435ed7898139665d4c39d9 nmdc-log-service-1.0.2
|
||||
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
|
||||
22b156a6fc2f6161765317f4ec9ab3731a26e0e2 release-0.12
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ const (
|
||||
CONNECTIONSTATE_CONNECTED = 3
|
||||
)
|
||||
|
||||
func (cs ConnectionState) Format() string {
|
||||
func (cs ConnectionState) String() string {
|
||||
switch cs {
|
||||
case CONNECTIONSTATE_DISCONNECTED:
|
||||
return "Disconnected"
|
||||
@@ -25,7 +25,7 @@ func (cs ConnectionState) Format() string {
|
||||
}
|
||||
}
|
||||
|
||||
func CheckIsNetTimeout(err error) bool {
|
||||
func checkIsNetTimeout(err error) bool {
|
||||
if err == nil {
|
||||
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.Format(), 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.Format(), 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
|
||||
}
|
||||
143
HubConnection.go
143
HubConnection.go
@@ -3,6 +3,7 @@ package libnmdc
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -27,8 +28,11 @@ type HubConnection struct {
|
||||
connValid bool
|
||||
sentOurHello bool
|
||||
autoReconnect bool
|
||||
|
||||
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()
|
||||
@@ -37,11 +41,11 @@ func (this *HubConnection) Users(cb func(*map[string]UserInfo) error) error {
|
||||
}
|
||||
|
||||
func (this *HubConnection) SayPublic(message string) {
|
||||
this.SayRaw("<" + this.Hco.Self.Nick + "> " + NMDCEscape(message) + "|")
|
||||
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 + "> " + NMDCEscape(message) + "|")
|
||||
this.SayRaw("$To: " + recipient + " From: " + this.Hco.Self.Nick + " $<" + this.Hco.Self.Nick + "> " + Escape(message) + "|")
|
||||
}
|
||||
|
||||
func (this *HubConnection) SayInfo() {
|
||||
@@ -65,24 +69,31 @@ func (this *HubConnection) UserCount() int {
|
||||
|
||||
func (this *HubConnection) userJoined_NameOnly(nick string) {
|
||||
if !this.UserExists(nick) {
|
||||
this.userLock.Lock()
|
||||
defer this.userLock.Unlock()
|
||||
|
||||
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) {
|
||||
if !this.UserExists(uinf.Nick) {
|
||||
// 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
|
||||
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 {
|
||||
@@ -106,14 +117,14 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
||||
// ```````````
|
||||
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])})
|
||||
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: NMDCUnescape(message)})
|
||||
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_HUB, Nick: this.HubName, Message: Unescape(message)})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -124,9 +135,8 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
||||
switch commandParts[0] {
|
||||
|
||||
case "$Lock":
|
||||
this.SayRaw("$Supports NoGetINFO UserCommand UserIP2|" +
|
||||
"$Key " + NMDCUnlock([]byte(commandParts[1])) + "|" +
|
||||
"$ValidateNick " + NMDCEscape(this.Hco.Self.Nick) + "|")
|
||||
this.SayRaw("$Supports NoHello NoGetINFO UserCommand UserIP2 QuickList ChatOnly|" +
|
||||
"$Key " + unlock([]byte(commandParts[1])) + "|")
|
||||
this.sentOurHello = false
|
||||
|
||||
case "$Hello":
|
||||
@@ -159,26 +169,32 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
||||
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "Incorrect password."})
|
||||
|
||||
case "$GetPass":
|
||||
this.SayRaw("$MyPass " + NMDCEscape(this.Hco.NickPassword) + "|")
|
||||
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":
|
||||
func() {
|
||||
this.userLock.Lock()
|
||||
defer this.userLock.Unlock()
|
||||
|
||||
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.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 {
|
||||
@@ -219,7 +235,7 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
||||
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]})
|
||||
this.processEvent(HubEvent{EventType: EVENT_PRIVATE, Nick: txparts[2], Message: Unescape(txparts[4])})
|
||||
valid = true
|
||||
}
|
||||
}
|
||||
@@ -229,20 +245,89 @@ 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)
|
||||
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
|
||||
|
||||
// IGNORABLE COMMANDS
|
||||
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":
|
||||
case "$UserCommand": // TODO $UserCommand 1 1 Group chat\New group chat$<%[mynick]> !groupchat_new||
|
||||
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":
|
||||
@@ -299,7 +384,7 @@ func (this *HubConnection) worker() {
|
||||
this.conn.SetReadDeadline(time.Now().Add(SEND_KEEPALIVE_EVERY))
|
||||
nbytes, err = this.conn.Read(readBuff)
|
||||
|
||||
if CheckIsNetTimeout(err) {
|
||||
if checkIsNetTimeout(err) {
|
||||
// No data before read deadline
|
||||
err = nil
|
||||
|
||||
|
||||
@@ -11,12 +11,18 @@ type HubConnectionOptions struct {
|
||||
NumEventsToBuffer uint
|
||||
|
||||
// Returning messages in sync mode
|
||||
OnEventSync func(HubEvent)
|
||||
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{
|
||||
@@ -54,5 +60,9 @@ func (this *HubConnectionOptions) Connect() *HubConnection {
|
||||
// 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()
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ const (
|
||||
EVENT_CONNECTION_STATE_CHANGED = 8
|
||||
EVENT_HUBNAME_CHANGED = 9
|
||||
EVENT_DEBUG_MESSAGE = 10
|
||||
EVENT_USERCOMMAND = 11
|
||||
)
|
||||
|
||||
type HubEventType int
|
||||
@@ -20,4 +21,5 @@ type HubEvent struct {
|
||||
Nick string
|
||||
Message string
|
||||
StateChange ConnectionState
|
||||
UserCommand *UserCommand
|
||||
}
|
||||
|
||||
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
|
||||
)
|
||||
96
UserInfo.go
96
UserInfo.go
@@ -1,4 +1,3 @@
|
||||
// libnmdc project UserInfo.go
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
@@ -6,30 +5,7 @@ import (
|
||||
"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'
|
||||
"strings"
|
||||
)
|
||||
|
||||
// This structure represents a user connected to a hub.
|
||||
@@ -37,6 +13,7 @@ type UserInfo struct {
|
||||
Nick string
|
||||
Description string
|
||||
ClientTag string
|
||||
ClientVersion string
|
||||
Email string
|
||||
ShareSize uint64
|
||||
ConnectionMode ConnectionMode
|
||||
@@ -47,30 +24,20 @@ type UserInfo struct {
|
||||
HubsRegistered uint64
|
||||
HubsOperator uint64
|
||||
IsOperator bool
|
||||
IPAddress string
|
||||
}
|
||||
|
||||
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
|
||||
// Format: $ALL <nick> <description>$ $<connection><flag>$<e-mail>$<sharesize>$
|
||||
|
||||
/*
|
||||
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$"
|
||||
HEAD := `(?ms)^\$ALL ([^ ]+) `
|
||||
FOOT := `\$.\$([^$]+)\$([^$]*)\$([0-9]*)\$$`
|
||||
|
||||
u := UserInfo{}
|
||||
err := u.fromMyINFO(sample)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
} else {
|
||||
fmt.Printf("%+v\n", u)
|
||||
}
|
||||
|
||||
os.Exit(1)
|
||||
*/
|
||||
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 {
|
||||
@@ -91,20 +58,26 @@ func maybeParse(str string, dest *uint64, default_val uint64) {
|
||||
}
|
||||
|
||||
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)
|
||||
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])
|
||||
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
|
||||
@@ -114,17 +87,23 @@ func (this *UserInfo) fromMyINFO(protomsg string) error {
|
||||
matches = rx_myinfo_notag.FindStringSubmatch(protomsg)
|
||||
if matches != nil {
|
||||
this.Nick = matches[1]
|
||||
this.Description = NMDCUnescape(matches[2])
|
||||
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
|
||||
this.Speed = matches[3]
|
||||
this.Flag = UserFlag(matches[4][0])
|
||||
this.Email = NMDCUnescape(matches[5])
|
||||
maybeParse(matches[6], &this.ShareSize, 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
|
||||
}
|
||||
@@ -136,10 +115,11 @@ func (this *UserInfo) fromMyINFO(protomsg string) error {
|
||||
// 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$",
|
||||
"$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,
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,43 @@ Tags: nmdc
|
||||
|
||||
=CHANGELOG=
|
||||
|
||||
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
|
||||
|
||||
|
||||
10
libnmdc.go
10
libnmdc.go
@@ -1,4 +1,3 @@
|
||||
// libnmdc project libnmdc.go
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
@@ -11,6 +10,7 @@ import (
|
||||
|
||||
const (
|
||||
DEFAULT_CLIENT_TAG string = "libnmdc.go"
|
||||
DEFAULT_CLIENT_VERSION string = "0.11"
|
||||
DEFAULT_HUB_NAME string = "(unknown)"
|
||||
SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second
|
||||
AUTO_RECONNECT_AFTER time.Duration = 30 * time.Second
|
||||
@@ -19,27 +19,29 @@ const (
|
||||
var rx_protocolMessage *regexp.Regexp
|
||||
var rx_publicChat *regexp.Regexp
|
||||
var rx_incomingTo *regexp.Regexp
|
||||
var rx_userCommand *regexp.Regexp
|
||||
var ErrNotConnected error = errors.New("Not connected")
|
||||
|
||||
func init() {
|
||||
rx_protocolMessage = regexp.MustCompile("(?ms)^[^|]*\\|")
|
||||
rx_publicChat = regexp.MustCompile("(?ms)^<([^>]*)> (.*)$")
|
||||
rx_incomingTo = regexp.MustCompile("(?ms)^([^ ]+) From: ([^ ]+) \\$<([^>]*)> (.*)")
|
||||
rx_userCommand = regexp.MustCompile(`(?ms)^(\d+) (\d+)\s?([^\$]*)\$?(.*)`)
|
||||
}
|
||||
|
||||
func NMDCUnescape(encoded string) string {
|
||||
func Unescape(encoded string) string {
|
||||
v1 := strings.Replace(encoded, "$", "$", -1)
|
||||
v2 := strings.Replace(v1, "|", "|", -1)
|
||||
return strings.Replace(v2, "&", "&", -1)
|
||||
}
|
||||
|
||||
func NMDCEscape(plaintext string) string {
|
||||
func Escape(plaintext string) string {
|
||||
v1 := strings.Replace(plaintext, "&", "&", -1)
|
||||
v2 := strings.Replace(v1, "|", "|", -1)
|
||||
return strings.Replace(v2, "$", "$", -1)
|
||||
}
|
||||
|
||||
func NMDCUnlock(lock []byte) string {
|
||||
func unlock(lock []byte) string {
|
||||
|
||||
nibble_swap := func(b byte) byte {
|
||||
return ((b << 4) & 0xF0) | ((b >> 4) & 0x0F)
|
||||
|
||||
@@ -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)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user