further reduce protocol interface surface

--HG--
branch : adc
This commit is contained in:
mappu 2017-11-22 19:11:34 +13:00
parent 2a60330e7c
commit 5c4d6d8355
3 changed files with 24 additions and 26 deletions

View File

@ -101,6 +101,14 @@ func (this *HubConnection) SayRaw(protocolCommand string) error {
return err return err
} }
func (this *HubConnection) SayKeepalive() error {
if !this.connValid {
return ErrNotConnected
}
return this.SayRaw(this.proto.ProtoMessageSeparator())
}
func (this *HubConnection) Disconnect() { func (this *HubConnection) Disconnect() {
this.autoReconnect = false this.autoReconnect = false
if this.conn != nil { if this.conn != nil {
@ -152,7 +160,7 @@ func (this *HubConnection) worker() {
err = nil err = nil
// Send KA packet // Send KA packet
err = this.proto.SayKeepalive() err = this.SayKeepalive()
} }
if nbytes > 0 { if nbytes > 0 {

View File

@ -57,14 +57,14 @@ func (this *NmdcProtocol) ProcessCommand(message string) {
// ``````````` // ```````````
if this.rxPublicChat.MatchString(message) { if this.rxPublicChat.MatchString(message) {
pubchat_parts := this.rxPublicChat.FindStringSubmatch(message) pubchat_parts := this.rxPublicChat.FindStringSubmatch(message)
this.hc.processEvent(HubEvent{EventType: EVENT_PUBLIC, Nick: pubchat_parts[1], Message: this.Unescape(pubchat_parts[2])}) this.hc.processEvent(HubEvent{EventType: EVENT_PUBLIC, Nick: pubchat_parts[1], Message: this.unescape(pubchat_parts[2])})
return return
} }
// System messages // System messages
// ``````````````` // ```````````````
if message[0] != '$' { if message[0] != '$' {
this.hc.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_HUB, Nick: this.hc.HubName, Message: this.Unescape(message)}) this.hc.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_HUB, Nick: this.hc.HubName, Message: this.unescape(message)})
return return
} }
@ -115,7 +115,7 @@ func (this *NmdcProtocol) ProcessCommand(message string) {
this.hc.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "This account is passworded."}) this.hc.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "This account is passworded."})
this.hc.Disconnect() this.hc.Disconnect()
} else { } else {
this.hc.SayRaw("$MyPass " + this.Escape(this.hc.Hco.NickPassword) + "|") this.hc.SayRaw("$MyPass " + this.escape(this.hc.Hco.NickPassword) + "|")
} }
case "$Quit": case "$Quit":
@ -174,7 +174,7 @@ func (this *NmdcProtocol) ProcessCommand(message string) {
if this.rxIncomingTo.MatchString(commandParts[1]) { if this.rxIncomingTo.MatchString(commandParts[1]) {
txparts := this.rxIncomingTo.FindStringSubmatch(commandParts[1]) txparts := this.rxIncomingTo.FindStringSubmatch(commandParts[1])
if txparts[1] == this.hc.Hco.Self.Nick && txparts[2] == txparts[3] { if txparts[1] == this.hc.Hco.Self.Nick && txparts[2] == txparts[3] {
this.hc.processEvent(HubEvent{EventType: EVENT_PRIVATE, Nick: txparts[2], Message: this.Unescape(txparts[4])}) this.hc.processEvent(HubEvent{EventType: EVENT_PRIVATE, Nick: txparts[2], Message: this.unescape(txparts[4])})
valid = true valid = true
} }
} }
@ -235,7 +235,7 @@ func (this *NmdcProtocol) ProcessCommand(message string) {
Type: UserCommandType(typeInt), Type: UserCommandType(typeInt),
Context: UserCommandContext(contextInt), Context: UserCommandContext(contextInt),
Message: usc[3], Message: usc[3],
Command: this.Unescape(usc[4]), Command: this.unescape(usc[4]),
} }
this.hc.processEvent(HubEvent{EventType: EVENT_USERCOMMAND, UserCommand: &uscStruct}) this.hc.processEvent(HubEvent{EventType: EVENT_USERCOMMAND, UserCommand: &uscStruct})
@ -258,7 +258,7 @@ func (this *NmdcProtocol) ProcessCommand(message string) {
this.sayInfo() this.sayInfo()
this.hc.SayRaw("$GetNickList|") this.hc.SayRaw("$GetNickList|")
} else { } else {
this.hc.SayRaw("$ValidateNick " + this.Escape(this.hc.Hco.Self.Nick) + "|") this.hc.SayRaw("$ValidateNick " + this.escape(this.hc.Hco.Self.Nick) + "|")
} }
// This also counts as the end of the handshake from our POV. Consider // This also counts as the end of the handshake from our POV. Consider
@ -282,24 +282,24 @@ func (this *NmdcProtocol) ProcessCommand(message string) {
} }
} }
func (this *NmdcProtocol) Escape(plaintext string) string { func (this *NmdcProtocol) escape(plaintext string) string {
v1 := strings.Replace(plaintext, "&", "&", -1) v1 := strings.Replace(plaintext, "&", "&", -1)
v2 := strings.Replace(v1, "|", "|", -1) v2 := strings.Replace(v1, "|", "|", -1)
return strings.Replace(v2, "$", "$", -1) return strings.Replace(v2, "$", "$", -1)
} }
func (this *NmdcProtocol) Unescape(encoded string) string { func (this *NmdcProtocol) unescape(encoded string) string {
v1 := strings.Replace(encoded, "$", "$", -1) v1 := strings.Replace(encoded, "$", "$", -1)
v2 := strings.Replace(v1, "|", "|", -1) v2 := strings.Replace(v1, "|", "|", -1)
return strings.Replace(v2, "&", "&", -1) return strings.Replace(v2, "&", "&", -1)
} }
func (this *NmdcProtocol) SayPublic(message string) { func (this *NmdcProtocol) SayPublic(message string) {
this.hc.SayRaw("<" + this.hc.Hco.Self.Nick + "> " + this.Escape(message) + "|") this.hc.SayRaw("<" + this.hc.Hco.Self.Nick + "> " + this.escape(message) + "|")
} }
func (this *NmdcProtocol) SayPrivate(recipient, message string) { func (this *NmdcProtocol) SayPrivate(recipient, message string) {
this.hc.SayRaw("$To: " + recipient + " From: " + this.hc.Hco.Self.Nick + " $<" + this.hc.Hco.Self.Nick + "> " + this.Escape(message) + "|") this.hc.SayRaw("$To: " + recipient + " From: " + this.hc.Hco.Self.Nick + " $<" + this.hc.Hco.Self.Nick + "> " + this.escape(message) + "|")
} }
func (this *NmdcProtocol) sayInfo() { func (this *NmdcProtocol) sayInfo() {
@ -313,8 +313,8 @@ func (this *NmdcProtocol) parseMyINFO(protomsg string) (*UserInfo, error) {
matches := this.rxMyInfo.FindStringSubmatch(protomsg) matches := this.rxMyInfo.FindStringSubmatch(protomsg)
if matches != nil { if matches != nil {
ret.Nick = matches[1] ret.Nick = matches[1]
ret.Description = this.Unescape(matches[2]) ret.Description = this.unescape(matches[2])
ret.ClientTag = this.Unescape(matches[3]) ret.ClientTag = this.unescape(matches[3])
ret.ClientVersion = matches[4] ret.ClientVersion = matches[4]
ret.ConnectionMode = ConnectionMode(matches[5][0]) ret.ConnectionMode = ConnectionMode(matches[5][0])
maybeParse(matches[6], &ret.HubsUnregistered, 0) maybeParse(matches[6], &ret.HubsUnregistered, 0)
@ -327,7 +327,7 @@ func (this *NmdcProtocol) parseMyINFO(protomsg string) (*UserInfo, error) {
ret.Speed = "" ret.Speed = ""
} }
ret.Flag = UserFlag(matches[10][len(matches[10])-1]) ret.Flag = UserFlag(matches[10][len(matches[10])-1])
ret.Email = this.Unescape(matches[11]) ret.Email = this.unescape(matches[11])
maybeParse(matches[12], &ret.ShareSize, 0) maybeParse(matches[12], &ret.ShareSize, 0)
return &ret, nil return &ret, nil
@ -337,7 +337,7 @@ func (this *NmdcProtocol) parseMyINFO(protomsg string) (*UserInfo, error) {
matches = this.rxMyInfoNoTag.FindStringSubmatch(protomsg) matches = this.rxMyInfoNoTag.FindStringSubmatch(protomsg)
if matches != nil { if matches != nil {
ret.Nick = matches[1] ret.Nick = matches[1]
ret.Description = this.Unescape(matches[2]) ret.Description = this.unescape(matches[2])
ret.ClientTag = "" ret.ClientTag = ""
ret.ClientVersion = "0" ret.ClientVersion = "0"
ret.ConnectionMode = CONNECTIONMODE_PASSIVE ret.ConnectionMode = CONNECTIONMODE_PASSIVE
@ -352,7 +352,7 @@ func (this *NmdcProtocol) parseMyINFO(protomsg string) (*UserInfo, error) {
ret.Speed = "" ret.Speed = ""
} }
ret.Flag = UserFlag(matches[3][len(matches[3])-1]) ret.Flag = UserFlag(matches[3][len(matches[3])-1])
ret.Email = this.Unescape(matches[4]) ret.Email = this.unescape(matches[4])
maybeParse(matches[5], &ret.ShareSize, 0) maybeParse(matches[5], &ret.ShareSize, 0)
return &ret, nil return &ret, nil
@ -382,10 +382,6 @@ func (this *NmdcProtocol) getUserMyINFO(u *UserInfo) string {
) )
} }
func (this *NmdcProtocol) SayKeepalive() error {
return this.hc.SayRaw("|")
}
func (this *NmdcProtocol) ProtoMessageSeparator() string { func (this *NmdcProtocol) ProtoMessageSeparator() string {
return "|" return "|"
} }

View File

@ -3,15 +3,9 @@ package libnmdc
type Protocol interface { type Protocol interface {
ProcessCommand(msg string) ProcessCommand(msg string)
Escape(string) string
Unescape(string) string
SayPublic(string) SayPublic(string)
SayPrivate(user, message string) SayPrivate(user, message string)
SayKeepalive() error
ProtoMessageSeparator() string ProtoMessageSeparator() string
} }