|
|
|
|
@@ -12,6 +12,11 @@ import (
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
DEFAULT_CLIENT_TAG string = "libnmdc.go"
|
|
|
|
|
DEFAULT_HUB_NAME string = "(unknown)"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ConnectionState int
|
|
|
|
|
type HubEventType int
|
|
|
|
|
|
|
|
|
|
@@ -69,17 +74,22 @@ var rx_incomingTo *regexp.Regexp
|
|
|
|
|
var ErrNotConnected error = errors.New("Not connected")
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
rx_protocolMessage = regexp.MustCompile("(?ms)^[^|]*|")
|
|
|
|
|
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
|
|
|
|
|
Address HubAddress
|
|
|
|
|
SkipVerifyTLS bool // using a negative verb, because bools default to false
|
|
|
|
|
Self UserInfo
|
|
|
|
|
NickPassword string
|
|
|
|
|
|
|
|
|
|
// Returning messages in async mode
|
|
|
|
|
NumEventsToBuffer uint
|
|
|
|
|
|
|
|
|
|
// Returning messages in sync mode
|
|
|
|
|
OnEventSync func(HubEvent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type HubConnection struct {
|
|
|
|
|
@@ -92,7 +102,8 @@ type HubConnection struct {
|
|
|
|
|
Users map[string]UserInfo
|
|
|
|
|
|
|
|
|
|
// Streamed events
|
|
|
|
|
OnEvent chan HubEvent
|
|
|
|
|
processEvent func(HubEvent)
|
|
|
|
|
OnEvent chan HubEvent
|
|
|
|
|
|
|
|
|
|
// Private state
|
|
|
|
|
conn net.Conn // this is an interface
|
|
|
|
|
@@ -148,7 +159,7 @@ 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}
|
|
|
|
|
this.processEvent(HubEvent{EventType: EVENT_USER_JOINED, Nick: nick})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -156,7 +167,7 @@ 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}
|
|
|
|
|
this.processEvent(HubEvent{EventType: EVENT_USER_JOINED, Nick: uinf.Nick})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -205,14 +216,14 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
|
|
|
|
// ```````````
|
|
|
|
|
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])}
|
|
|
|
|
this.processEvent(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)}
|
|
|
|
|
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_HUB, Nick: this.HubName, Message: NMDCUnescape(message)})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -242,27 +253,27 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
|
|
|
|
|
|
|
|
|
case "$HubName":
|
|
|
|
|
this.HubName = commandParts[1]
|
|
|
|
|
this.OnEvent <- HubEvent{EventType: EVENT_HUBNAME_CHANGED, Nick: commandParts[1]}
|
|
|
|
|
this.processEvent(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."}
|
|
|
|
|
this.processEvent(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."}
|
|
|
|
|
this.processEvent(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."}
|
|
|
|
|
this.processEvent(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."}
|
|
|
|
|
this.processEvent(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]}
|
|
|
|
|
this.processEvent(HubEvent{EventType: EVENT_USER_PART, Nick: commandParts[1]})
|
|
|
|
|
|
|
|
|
|
case "$MyINFO":
|
|
|
|
|
u := UserInfo{}
|
|
|
|
|
@@ -270,7 +281,7 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
|
|
|
|
if err == nil {
|
|
|
|
|
this.userJoined_Full(&u)
|
|
|
|
|
} else {
|
|
|
|
|
this.OnEvent <- HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: err.Error()}
|
|
|
|
|
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: err.Error()})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "$NickList":
|
|
|
|
|
@@ -286,24 +297,25 @@ 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.OnEvent <- HubEvent{EventType: EVENT_PRIVATE, Nick: txparts[2], Message: txparts[4]}
|
|
|
|
|
this.processEvent(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] + "'"}
|
|
|
|
|
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Malformed private message '" + commandParts[1] + "'"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "$UserIP":
|
|
|
|
|
// Final message in PtokaX connection handshake - trigger connection callback.
|
|
|
|
|
// This might not be the case for other hubsofts, though
|
|
|
|
|
if this.State != CONNECTIONSTATE_CONNECTED {
|
|
|
|
|
this.OnEvent <- HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTED}
|
|
|
|
|
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTED})
|
|
|
|
|
this.State = CONNECTIONSTATE_CONNECTED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "$UserCommand":
|
|
|
|
|
// $UserCommand 1 1 Group chat\New group chat$<%[mynick]> !groupchat_new||
|
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
|
|
case "$ForceMove":
|
|
|
|
|
@@ -318,7 +330,7 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
|
|
|
|
case "$ConnectToMe":
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
this.OnEvent <- HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Unhandled protocol command '" + commandParts[0] + "'"}
|
|
|
|
|
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Unhandled protocol command '" + commandParts[0] + "'"})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -333,6 +345,8 @@ func (this *HubConnection) worker() {
|
|
|
|
|
// 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,
|
|
|
|
|
@@ -342,30 +356,26 @@ func (this *HubConnection) worker() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.OnEvent <- HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTING}
|
|
|
|
|
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, 4096)
|
|
|
|
|
readBuff := make([]byte, 1024)
|
|
|
|
|
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] == '|' {
|
|
|
|
|
@@ -373,38 +383,66 @@ func (this *HubConnection) worker() {
|
|
|
|
|
}
|
|
|
|
|
protocolMessage := rx_protocolMessage.FindString(fullBuffer)
|
|
|
|
|
if len(protocolMessage) > 0 {
|
|
|
|
|
this.processProtocolMessage(string(protocolMessage))
|
|
|
|
|
this.processProtocolMessage(protocolMessage[:len(protocolMessage)-1])
|
|
|
|
|
fullBuffer = fullBuffer[len(protocolMessage):]
|
|
|
|
|
} else {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Maybe we disconnected
|
|
|
|
|
// Perform this check *last*, to ensure we've had a final shot at
|
|
|
|
|
// clearing out any queued messages
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.State = CONNECTIONSTATE_DISCONNECTED
|
|
|
|
|
this.conn = nil
|
|
|
|
|
this.connValid = false
|
|
|
|
|
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()})
|
|
|
|
|
|
|
|
|
|
time.Sleep(30 * time.Second) // Wait before reconnect
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *HubConnectionOptions) prepareConnection() *HubConnection {
|
|
|
|
|
if this.Self.ClientTag == "" {
|
|
|
|
|
this.Self.ClientTag = DEFAULT_CLIENT_TAG
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hc := HubConnection{
|
|
|
|
|
Hco: this,
|
|
|
|
|
HubName: DEFAULT_HUB_NAME,
|
|
|
|
|
State: CONNECTIONSTATE_DISCONNECTED,
|
|
|
|
|
Users: make(map[string]UserInfo),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &hc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
|
hc := this.prepareConnection()
|
|
|
|
|
hc.OnEvent = make(chan HubEvent, this.NumEventsToBuffer)
|
|
|
|
|
hc.processEvent = func(ev HubEvent) {
|
|
|
|
|
hc.OnEvent <- ev
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go hc.worker()
|
|
|
|
|
|
|
|
|
|
return &hc
|
|
|
|
|
return hc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Connects to an NMDC server, and blocks forever to handle protocol messages.
|
|
|
|
|
// Client code should supply an event handling function.
|
|
|
|
|
func (this *HubConnectionOptions) ConnectSync() {
|
|
|
|
|
hc := this.prepareConnection()
|
|
|
|
|
hc.worker()
|
|
|
|
|
}
|
|
|
|
|
|