move client tag parsing to separate file, add unit tests

--HG--
branch : nmdc-ircfrontend
This commit is contained in:
mappu 2018-03-24 14:24:28 +13:00
parent d465d742c6
commit 485f68f5d1
3 changed files with 98 additions and 31 deletions

39
clientTag.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"strings"
)
type clientTag struct {
AppName string
Version string
}
func parseVersion(ver string) clientTag {
// A bit long and unwieldy.
// Heuristic: keep the first word, and the the first word containing digits
ret := clientTag{
AppName: APP_NAME,
Version: APP_VERSION,
}
ver = strings.Trim(strings.Replace(ver, ":", " ", -1), " ")
words := strings.Split(ver, " ")
for _, word := range words[1:] {
if strings.ContainsAny(word, "0123456789") {
ret.AppName = words[0]
ret.Version = strings.Replace(word, "(", "", -1) // AndroIRC
break
}
}
// Strip leading v from mIRC
if len(ret.Version) >= 2 && (ret.Version[0] == 'v' || ret.Version[0] == 'V') && strings.ContainsAny(string(ret.Version[1]), "0123456789") {
ret.Version = ret.Version[1:]
}
return ret
}

53
clientTag_test.go Normal file
View File

@ -0,0 +1,53 @@
package main
import (
"testing"
)
func TestParseVersion(t *testing.T) {
tests := [][3]string{
[3]string{
"HexChat 2.12.1 [x64] / Microsoft Windows 10 Pro (x64) [Intel(R) Core(TM) i5-2500 CPU @ 3.30GHz (3.60GHz)]",
"HexChat", "2.12.1",
},
[3]string{
"AndroIRC - Android IRC Client (5.2 - Build 6830152) - http://www.androirc.com",
"AndroIRC", "5.2",
},
[3]string{
"AndChat 1.4.3.2 http://www.andchat.ne",
"AndChat", "1.4.3.2",
},
[3]string{
"liteIRC for Android 1.1.8",
"liteIRC", "1.1.8",
},
[3]string{
":Relay:1.0:Android",
"Relay", "1.0",
},
[3]string{
"mIRC v7.45",
"mIRC", "7.45",
},
[3]string{
"Revolution IRC:0.3.2:Android",
"Revolution", "0.3.2",
},
}
for _, test := range tests {
result := parseVersion(test[0])
if result.AppName != test[1] || result.Version != test[2] {
t.Fatalf("Got <%s,%s> expecting <%s,%s>\n", result.AppName, result.Version, test[1], test[2])
}
}
}

View File

@ -549,46 +549,21 @@ func (s *Server) maybeStartUpstream() {
} }
func (s *Server) SetClientSoftwareVersion(ver string) { func (s *Server) SetClientSoftwareVersion(ver string) {
// "HexChat 2.12.1 [x64] / Microsoft Windows 10 Pro (x64) [Intel(R) Core(TM) i5-2500 CPU @ 3.30GHz (3.60GHz)]"
// "AndroIRC - Android IRC Client (5.2 - Build 6830152) - http://www.androirc.com"
// "AndChat 1.4.3.2 http://www.andchat.net"
// "liteIRC for Android 1.1.8"
// ":Relay:1.0:Android"
// "mIRC v7.45"
// A bit long and unwieldy. ct := parseVersion(ver)
// Heuristic: keep the first word, and the the first word containing digits
tag := APP_NAME s.verbosef("Replacing client tag with '%s' version '%s'", ct.AppName, ct.Version)
version := APP_VERSION
words := strings.Split(ver, " ") s.upstreamLauncher.Self.ClientTag = ct.AppName
s.upstreamLauncher.Self.ClientVersion = ct.Version
for _, word := range words[1:] {
if strings.ContainsAny(word, "0123456789") {
tag = words[0]
version = strings.Replace(word, "(", "", -1) // AndroIRC
break
}
}
// Strip leading v from mIRC
if len(version) >= 2 && (version[0] == 'v' || version[0] == 'V') && strings.ContainsAny(string(version[1]), "0123456789") {
version = version[1:]
}
s.verbosef("Replacing client tag with '%s' version '%s'", tag, version)
s.upstreamLauncher.Self.ClientTag = tag
s.upstreamLauncher.Self.ClientVersion = version
s.recievedCtcpVersion = true s.recievedCtcpVersion = true
s.ClientStateLock.Lock() s.ClientStateLock.Lock()
defer s.ClientStateLock.Unlock() defer s.ClientStateLock.Unlock()
if s.upstream != nil { if s.upstream != nil {
s.upstream.Hco.Self.ClientTag = tag s.upstream.Hco.Self.ClientTag = ct.AppName
s.upstream.Hco.Self.ClientVersion = version s.upstream.Hco.Self.ClientVersion = ct.Version
s.upstream.SayInfo() s.upstream.SayInfo()
} else { } else {