From 485f68f5d110e67f0b124c9766f66d48bc152915 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 24 Mar 2018 14:24:28 +1300 Subject: [PATCH] move client tag parsing to separate file, add unit tests --HG-- branch : nmdc-ircfrontend --- clientTag.go | 39 ++++++++++++++++++++++++++++++++++ clientTag_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++ server.go | 37 ++++++--------------------------- 3 files changed, 98 insertions(+), 31 deletions(-) create mode 100644 clientTag.go create mode 100644 clientTag_test.go diff --git a/clientTag.go b/clientTag.go new file mode 100644 index 0000000..50fb802 --- /dev/null +++ b/clientTag.go @@ -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 +} diff --git a/clientTag_test.go b/clientTag_test.go new file mode 100644 index 0000000..4dae915 --- /dev/null +++ b/clientTag_test.go @@ -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]) + } + } +} diff --git a/server.go b/server.go index 26df75d..fe1fc52 100644 --- a/server.go +++ b/server.go @@ -549,46 +549,21 @@ func (s *Server) maybeStartUpstream() { } 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. - // Heuristic: keep the first word, and the the first word containing digits + ct := parseVersion(ver) - tag := APP_NAME - version := APP_VERSION + s.verbosef("Replacing client tag with '%s' version '%s'", ct.AppName, ct.Version) - words := strings.Split(ver, " ") - - 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.upstreamLauncher.Self.ClientTag = ct.AppName + s.upstreamLauncher.Self.ClientVersion = ct.Version s.recievedCtcpVersion = true s.ClientStateLock.Lock() defer s.ClientStateLock.Unlock() if s.upstream != nil { - s.upstream.Hco.Self.ClientTag = tag - s.upstream.Hco.Self.ClientVersion = version + s.upstream.Hco.Self.ClientTag = ct.AppName + s.upstream.Hco.Self.ClientVersion = ct.Version s.upstream.SayInfo() } else {