From a4d40a90d5546d540a61acab133da373af55ca0f Mon Sep 17 00:00:00 2001 From: tpltnt Date: Sat, 15 Feb 2014 23:42:54 +0100 Subject: [PATCH 1/7] catching empty arguments in IRC() + tests --- irc.go | 11 ++++++++++- irc_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/irc.go b/irc.go index 4e13886..2efc26f 100644 --- a/irc.go +++ b/irc.go @@ -359,8 +359,17 @@ func (irc *Connection) Connect(server string) error { // Create a connection with the (publicly visible) nickname and username. -// The nickname is later used to address the user. +// The nickname is later used to address the user. Returns nil if nick +// or user are empty. func IRC(nick, user string) *Connection { + // catch invalid values + if 0 == len(nick) { + return nil + } + if 0 == len(user) { + return nil + } + irc := &Connection{ nick: nick, user: user, diff --git a/irc_test.go b/irc_test.go index 02fe5b4..42e090f 100644 --- a/irc_test.go +++ b/irc_test.go @@ -127,3 +127,38 @@ func TestClearCallback(t *testing.T) { t.Error("Callbacks not cleared") } } + +func TestIRCemptyNick(t *testing.T) { + irccon := IRC("", "go-eventirc") + irccon = nil + if nil != irccon { + t.Error("empty nick didn't result in error") + t.Fail() + } +/* + irccon.VerboseCallbackHandler = true + irccon.Debug = true + irccon.UseTLS = true + err := irccon.Connect("irc.freenode.net:7000") + if err != nil { + t.Fatal("Can't connect to freenode.") + } + irccon.AddCallback("001", func(e *Event) { irccon.Join("#go-eventirc") }) + + irccon.AddCallback("366", func(e *Event) { + irccon.Privmsg("#go-eventirc", "Test Message\n") + time.Sleep(2 * time.Second) + irccon.Quit() + }) + + irccon.Loop() +*/ +} + +func TestIRCemptyUser(t *testing.T) { + irccon := IRC("go-eventirc", "") + irccon = nil + if nil != irccon { + t.Error("empty user didn't result in error") + } +} From 28bf28292475d18afdb8a32edf9694979542cfa8 Mon Sep 17 00:00:00 2001 From: tpltnt Date: Sun, 16 Feb 2014 00:32:02 +0100 Subject: [PATCH 2/7] hasValidValues() + tests started --- irc.go | 14 ++++++++++++++ irc_test.go | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/irc.go b/irc.go index 2efc26f..b8e116e 100644 --- a/irc.go +++ b/irc.go @@ -388,3 +388,17 @@ func IRC(nick, user string) *Connection { irc.setupCallbacks() return irc } + +// Returns true if all values in struct make +// somewhat sense. +func (irc *Connection) hasValidValues() bool { + if 0 == len(irc.nick) { + return false + } + + if 0 == len(irc.Version) { + return false + } + + return true +} diff --git a/irc_test.go b/irc_test.go index 42e090f..2faed38 100644 --- a/irc_test.go +++ b/irc_test.go @@ -157,8 +157,43 @@ func TestIRCemptyNick(t *testing.T) { func TestIRCemptyUser(t *testing.T) { irccon := IRC("go-eventirc", "") - irccon = nil if nil != irccon { t.Error("empty user didn't result in error") } } + + +func TestHasValidValues0(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + if nil == irccon { + t.Error("creating IRC struct failed") + } + + if false == irccon.hasValidValues() { + t.Error("valid struct not detected as such") + } +} + +func TestHasValidValues1(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + if nil == irccon { + t.Error("creating IRC struct failed") + } + + irccon.Version = "" + if irccon.hasValidValues() { + t.Error("empty 'Version' not detected") + } +} + +func TestHasValidValues2(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + if nil == irccon { + t.Error("creating IRC struct failed") + } + + irccon.nick = "" + if irccon.hasValidValues() { + t.Error("empty 'nick' not detected") + } +} From 4ba3d1c0d20276aa5891e2fac7d082a465de9dd9 Mon Sep 17 00:00:00 2001 From: tpltnt Date: Sun, 16 Feb 2014 00:56:21 +0100 Subject: [PATCH 3/7] more hasValidValues() --- irc.go | 8 ++++++++ irc_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/irc.go b/irc.go index b8e116e..7fb086c 100644 --- a/irc.go +++ b/irc.go @@ -400,5 +400,13 @@ func (irc *Connection) hasValidValues() bool { return false } + if 0 == len(irc.user) { + return false + } + + if nil == irc.Log { + return false + } + return true } diff --git a/irc_test.go b/irc_test.go index 2faed38..8a71e2b 100644 --- a/irc_test.go +++ b/irc_test.go @@ -197,3 +197,27 @@ func TestHasValidValues2(t *testing.T) { t.Error("empty 'nick' not detected") } } + +func TestHasValidValues3(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + if nil == irccon { + t.Error("creating IRC struct failed") + } + + irccon.user = "" + if irccon.hasValidValues() { + t.Error("empty 'user' not detected") + } +} + +func TestHasValidValues4(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + if nil == irccon { + t.Error("creating IRC struct failed") + } + + irccon.Log = nil + if irccon.hasValidValues() { + t.Error("nil pointer 'Log' not detected") + } +} From 4dcf7d03c591fda57b24606280ce742f13c166a9 Mon Sep 17 00:00:00 2001 From: tpltnt Date: Sun, 16 Feb 2014 01:10:08 +0100 Subject: [PATCH 4/7] hasValidValues() -> has ConnectionValues() --- irc.go | 10 +++++++--- irc_test.go | 38 +++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/irc.go b/irc.go index 7fb086c..5948ee8 100644 --- a/irc.go +++ b/irc.go @@ -389,9 +389,9 @@ func IRC(nick, user string) *Connection { return irc } -// Returns true if all values in struct make -// somewhat sense. -func (irc *Connection) hasValidValues() bool { +// Returns true if all values in struct allow for +// establishing a connection. +func (irc *Connection) hasConnectionValues() bool { if 0 == len(irc.nick) { return false } @@ -404,6 +404,10 @@ func (irc *Connection) hasValidValues() bool { return false } + if 0 == len(irc.server) { + return false + } + if nil == irc.Log { return false } diff --git a/irc_test.go b/irc_test.go index 8a71e2b..a829c3c 100644 --- a/irc_test.go +++ b/irc_test.go @@ -163,25 +163,27 @@ func TestIRCemptyUser(t *testing.T) { } -func TestHasValidValues0(t *testing.T) { +func TestHasConnectionValues0(t *testing.T) { irccon := IRC("go-eventirc", "go-eventirc") if nil == irccon { t.Error("creating IRC struct failed") } - if false == irccon.hasValidValues() { + irccon.server = "foo" + if false == irccon.hasConnectionValues() { t.Error("valid struct not detected as such") } } -func TestHasValidValues1(t *testing.T) { +func TestHasConnectionValues1(t *testing.T) { irccon := IRC("go-eventirc", "go-eventirc") if nil == irccon { t.Error("creating IRC struct failed") } + irccon.server = "foo" irccon.Version = "" - if irccon.hasValidValues() { + if irccon.hasConnectionValues() { t.Error("empty 'Version' not detected") } } @@ -191,33 +193,47 @@ func TestHasValidValues2(t *testing.T) { if nil == irccon { t.Error("creating IRC struct failed") } + irccon.server = "foo" irccon.nick = "" - if irccon.hasValidValues() { + if irccon.hasConnectionValues() { t.Error("empty 'nick' not detected") } } -func TestHasValidValues3(t *testing.T) { +func TestHasConnectionValues3(t *testing.T) { irccon := IRC("go-eventirc", "go-eventirc") if nil == irccon { t.Error("creating IRC struct failed") } + irccon.server = "foo" irccon.user = "" - if irccon.hasValidValues() { + if irccon.hasConnectionValues() { t.Error("empty 'user' not detected") } } -func TestHasValidValues4(t *testing.T) { +func TestHasConnectionValues4(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + if nil == irccon { + t.Error("creating IRC struct failed") + } + irccon.server = "foo" + + irccon.Log = nil + if irccon.hasConnectionValues() { + t.Error("nil pointer 'Log' not detected") + } +} + +func TestHasConnectionValues5(t *testing.T) { irccon := IRC("go-eventirc", "go-eventirc") if nil == irccon { t.Error("creating IRC struct failed") } - irccon.Log = nil - if irccon.hasValidValues() { - t.Error("nil pointer 'Log' not detected") + if irccon.hasConnectionValues() { + t.Error("empty 'server' not detected") } } From 48983c2abfd73403834892e4555ba8dcce13e209 Mon Sep 17 00:00:00 2001 From: tpltnt Date: Sun, 16 Feb 2014 02:01:53 +0100 Subject: [PATCH 5/7] refactoring: hasConnectionValues() integrated into Connection() + more detailed errors --- irc.go | 48 ++++++++-------- irc_test.go | 155 +++++++++++++++++++++------------------------------- 2 files changed, 84 insertions(+), 119 deletions(-) diff --git a/irc.go b/irc.go index 5948ee8..2b923a9 100644 --- a/irc.go +++ b/irc.go @@ -328,6 +328,29 @@ func (irc *Connection) Connect(server string) error { irc.server = server irc.stopped = false + // make sure everything is ready for connection + if 0 == len(irc.server) { + return errors.New("empty 'server'") + } + if 1 != strings.Count(irc.server, ":") { + return errors.New("wrong number of ':' in address") + } + if 0 == strings.Index(irc.server, ":") { + return errors.New("hostname is missing") + } + if (len(irc.server) - 1) == strings.Index(irc.server, ":") { + return errors.New("port missing") + } + if nil == irc.Log { + return errors.New("'Log' points to nil") + } + if 0 == len(irc.nick) { + return errors.New("empty 'user'") + } + if 0 == len(irc.user) { + return errors.New("empty 'user'") + } + var err error if irc.UseTLS { if irc.netsock, err = net.DialTimeout("tcp", irc.server, irc.Timeout); err == nil { @@ -389,28 +412,3 @@ func IRC(nick, user string) *Connection { return irc } -// Returns true if all values in struct allow for -// establishing a connection. -func (irc *Connection) hasConnectionValues() bool { - if 0 == len(irc.nick) { - return false - } - - if 0 == len(irc.Version) { - return false - } - - if 0 == len(irc.user) { - return false - } - - if 0 == len(irc.server) { - return false - } - - if nil == irc.Log { - return false - } - - return true -} diff --git a/irc_test.go b/irc_test.go index a829c3c..1a8b58c 100644 --- a/irc_test.go +++ b/irc_test.go @@ -49,6 +49,67 @@ func TestConnectionSSL(t *testing.T) { irccon.Loop() } +func TestConnectionEmtpyServer(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + err := irccon.Connect("") + if err == nil { + t.Fatal("emtpy server string not detected") + } +} + +func TestConnectionDoubleColon(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + err := irccon.Connect("::") + if err == nil { + t.Fatal("wrong number of ':' not detected") + } +} + +func TestConnectionMissingHost(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + err := irccon.Connect(":6667") + if err == nil { + t.Fatal("missing host not detected") + } +} + +func TestConnectionMissingPort(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + err := irccon.Connect("chat.freenode.net:") + if err == nil { + t.Fatal("missing port not detected") + } +} + +func TestConnectionMissingLog(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + irccon.Log = nil + err := irccon.Connect("chat.freenode.net:6667") + if err == nil { + t.Fatal("missing 'Log' not detected") + } +} + +func TestConnectionEmptyUser(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + // user may be changed after creation + irccon.user = "" + err := irccon.Connect("chat.freenode.net:6667") + if err == nil { + t.Fatal("empty 'user' not detected") + } +} + +func TestConnectionEmptyNick(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + // nick may be changed after creation + irccon.nick = "" + err := irccon.Connect("chat.freenode.net:6667") + if err == nil { + t.Fatal("empty 'nick' not detected") + } +} + func TestRemoveCallback(t *testing.T) { irccon := IRC("go-eventirc", "go-eventirc") irccon.VerboseCallbackHandler = true @@ -135,24 +196,6 @@ func TestIRCemptyNick(t *testing.T) { t.Error("empty nick didn't result in error") t.Fail() } -/* - irccon.VerboseCallbackHandler = true - irccon.Debug = true - irccon.UseTLS = true - err := irccon.Connect("irc.freenode.net:7000") - if err != nil { - t.Fatal("Can't connect to freenode.") - } - irccon.AddCallback("001", func(e *Event) { irccon.Join("#go-eventirc") }) - - irccon.AddCallback("366", func(e *Event) { - irccon.Privmsg("#go-eventirc", "Test Message\n") - time.Sleep(2 * time.Second) - irccon.Quit() - }) - - irccon.Loop() -*/ } func TestIRCemptyUser(t *testing.T) { @@ -161,79 +204,3 @@ func TestIRCemptyUser(t *testing.T) { t.Error("empty user didn't result in error") } } - - -func TestHasConnectionValues0(t *testing.T) { - irccon := IRC("go-eventirc", "go-eventirc") - if nil == irccon { - t.Error("creating IRC struct failed") - } - - irccon.server = "foo" - if false == irccon.hasConnectionValues() { - t.Error("valid struct not detected as such") - } -} - -func TestHasConnectionValues1(t *testing.T) { - irccon := IRC("go-eventirc", "go-eventirc") - if nil == irccon { - t.Error("creating IRC struct failed") - } - irccon.server = "foo" - - irccon.Version = "" - if irccon.hasConnectionValues() { - t.Error("empty 'Version' not detected") - } -} - -func TestHasValidValues2(t *testing.T) { - irccon := IRC("go-eventirc", "go-eventirc") - if nil == irccon { - t.Error("creating IRC struct failed") - } - irccon.server = "foo" - - irccon.nick = "" - if irccon.hasConnectionValues() { - t.Error("empty 'nick' not detected") - } -} - -func TestHasConnectionValues3(t *testing.T) { - irccon := IRC("go-eventirc", "go-eventirc") - if nil == irccon { - t.Error("creating IRC struct failed") - } - irccon.server = "foo" - - irccon.user = "" - if irccon.hasConnectionValues() { - t.Error("empty 'user' not detected") - } -} - -func TestHasConnectionValues4(t *testing.T) { - irccon := IRC("go-eventirc", "go-eventirc") - if nil == irccon { - t.Error("creating IRC struct failed") - } - irccon.server = "foo" - - irccon.Log = nil - if irccon.hasConnectionValues() { - t.Error("nil pointer 'Log' not detected") - } -} - -func TestHasConnectionValues5(t *testing.T) { - irccon := IRC("go-eventirc", "go-eventirc") - if nil == irccon { - t.Error("creating IRC struct failed") - } - - if irccon.hasConnectionValues() { - t.Error("empty 'server' not detected") - } -} From 6e0280dae64f1b7fe0bb42716c6e8469042174eb Mon Sep 17 00:00:00 2001 From: tpltnt Date: Sun, 16 Feb 2014 11:41:38 +0100 Subject: [PATCH 6/7] port range checking added --- irc.go | 12 +++++++++++- irc_test.go | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/irc.go b/irc.go index 2b923a9..4ba006e 100644 --- a/irc.go +++ b/irc.go @@ -26,6 +26,7 @@ import ( "log" "net" "os" + "strconv" "strings" "time" ) @@ -341,6 +342,15 @@ func (irc *Connection) Connect(server string) error { if (len(irc.server) - 1) == strings.Index(irc.server, ":") { return errors.New("port missing") } + // check for valid range + ports := strings.Split(irc.server, ":")[1] + port, err := strconv.Atoi(ports) + if nil != err { + return errors.New("extracting port failed") + } + if !((port >= 0) && (port <= 65535)) { + return errors.New("port number outside valid range") + } if nil == irc.Log { return errors.New("'Log' points to nil") } @@ -351,7 +361,7 @@ func (irc *Connection) Connect(server string) error { return errors.New("empty 'user'") } - var err error +// var err error if irc.UseTLS { if irc.netsock, err = net.DialTimeout("tcp", irc.server, irc.Timeout); err == nil { irc.socket = tls.Client(irc.netsock, irc.TLSConfig) diff --git a/irc_test.go b/irc_test.go index 1a8b58c..ecef112 100644 --- a/irc_test.go +++ b/irc_test.go @@ -12,6 +12,7 @@ func TestConnection(t *testing.T) { irccon.Debug = true err := irccon.Connect("irc.freenode.net:6667") if err != nil { + t.Log(err.Error()) t.Fatal("Can't connect to freenode.") } irccon.AddCallback("001", func(e *Event) { irccon.Join("#go-eventirc") }) @@ -36,6 +37,7 @@ func TestConnectionSSL(t *testing.T) { irccon.UseTLS = true err := irccon.Connect("irc.freenode.net:7000") if err != nil { + t.Log(err.Error()) t.Fatal("Can't connect to freenode.") } irccon.AddCallback("001", func(e *Event) { irccon.Join("#go-eventirc") }) @@ -81,6 +83,22 @@ func TestConnectionMissingPort(t *testing.T) { } } +func TestConnectionNegativePort(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + err := irccon.Connect("chat.freenode.net:-1") + if err == nil { + t.Fatal("negative port number not detected") + } +} + +func TestConnectionTooLargePort(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + err := irccon.Connect("chat.freenode.net:65536") + if err == nil { + t.Fatal("too large port number not detected") + } +} + func TestConnectionMissingLog(t *testing.T) { irccon := IRC("go-eventirc", "go-eventirc") irccon.Log = nil From f073b9b25cb99ccca78f3e83f7ccb7e44ad8dfaa Mon Sep 17 00:00:00 2001 From: tpltnt Date: Sun, 16 Feb 2014 14:20:13 +0100 Subject: [PATCH 7/7] style fixes --- irc.go | 54 +++++++++++++++-------------------------------------- irc_test.go | 4 ++-- 2 files changed, 17 insertions(+), 41 deletions(-) diff --git a/irc.go b/irc.go index 4ba006e..10c0391 100644 --- a/irc.go +++ b/irc.go @@ -3,9 +3,9 @@ // license that can be found in the LICENSE file. /* -This package provides an event based IRC client library. It allows to -register callbacks for the events you need to handle. Its features -include handling standard CTCP, reconnecting on errors and detecting +This package provides an event based IRC client library. It allows to +register callbacks for the events you need to handle. Its features +include handling standard CTCP, reconnecting on errors and detecting stones servers. Details of the IRC protocol can be found in the following RFCs: https://tools.ietf.org/html/rfc1459 @@ -35,7 +35,6 @@ const ( VERSION = "go-ircevent v2.1" ) - // Read data from a connection. To be used as a goroutine. func (irc *Connection) readLoop() { br := bufio.NewReaderSize(irc.socket, 512) @@ -102,7 +101,6 @@ func (irc *Connection) readLoop() { irc.readerExit <- true } - // Loop to write to a connection. To be used as a goroutine. func (irc *Connection) writeLoop() { for { @@ -140,7 +138,6 @@ func (irc *Connection) writeLoop() { irc.writerExit <- true } - // Pings the server if we have not received any messages for 5 minutes // to keep the connection alive. To be used as a goroutine. func (irc *Connection) pingLoop() { @@ -170,7 +167,6 @@ func (irc *Connection) pingLoop() { } } - // Main loop to control the connection. func (irc *Connection) Loop() { for !irc.stopped { @@ -191,7 +187,6 @@ func (irc *Connection) Loop() { } } - // Quit the current connection and disconnect from the server // RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.1.6 func (irc *Connection) Quit() { @@ -200,60 +195,51 @@ func (irc *Connection) Quit() { irc.Disconnect() } - // Use the connection to join a given channel. // RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.2.1 func (irc *Connection) Join(channel string) { irc.pwrite <- fmt.Sprintf("JOIN %s\r\n", channel) } - // Leave a given channel. // RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.2.2 func (irc *Connection) Part(channel string) { irc.pwrite <- fmt.Sprintf("PART %s\r\n", channel) } - // Send a notification to a nickname. This is similar to Privmsg but must not receive replies. // RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.4.2 func (irc *Connection) Notice(target, message string) { irc.pwrite <- fmt.Sprintf("NOTICE %s :%s\r\n", target, message) } - // Send a formated notification to a nickname. // RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.4.2 func (irc *Connection) Noticef(target, format string, a ...interface{}) { irc.Notice(target, fmt.Sprintf(format, a...)) } - // Send (private) message to a target (channel or nickname). // RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.4.1 func (irc *Connection) Privmsg(target, message string) { irc.pwrite <- fmt.Sprintf("PRIVMSG %s :%s\r\n", target, message) } - // Send formated string to specified target (channel or nickname). func (irc *Connection) Privmsgf(target, format string, a ...interface{}) { irc.Privmsg(target, fmt.Sprintf(format, a...)) } - // Send raw string. func (irc *Connection) SendRaw(message string) { irc.pwrite <- message + "\r\n" } - // Send raw formated string. func (irc *Connection) SendRawf(format string, a ...interface{}) { irc.SendRaw(fmt.Sprintf(format, a...)) } - // Set (new) nickname. // RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.1.2 func (irc *Connection) Nick(n string) { @@ -261,27 +247,23 @@ func (irc *Connection) Nick(n string) { irc.SendRawf("NICK %s", n) } - // Determine nick currently used with the connection. func (irc *Connection) GetNick() string { return irc.nickcurrent } - // Query information about a particular nickname. // RFC 1459: https://tools.ietf.org/html/rfc1459#section-4.5.2 func (irc *Connection) Whois(nick string) { irc.SendRawf("WHOIS %s", nick) } - // Query information about a given nickname in the server. // RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.5.1 func (irc *Connection) Who(nick string) { irc.SendRawf("WHO %s", nick) } - // Set different modes for a target (channel or nickname). // RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.2.3 func (irc *Connection) Mode(target string, modestring ...string) { @@ -293,8 +275,7 @@ func (irc *Connection) Mode(target string, modestring ...string) { irc.SendRawf("MODE %s", target) } - -// A disconnect sends all buffered messages (if possible), +// A disconnect sends all buffered messages (if possible), // stops all goroutines and then closes the socket. func (irc *Connection) Disconnect() { irc.endping <- true @@ -315,13 +296,11 @@ func (irc *Connection) Disconnect() { irc.Error <- errors.New("Disconnect Called") } - // Reconnect to a server using the current connection. func (irc *Connection) Reconnect() error { return irc.Connect(irc.server) } - // Connect to a given server using the current connection configuration. // This function also takes care of identification if a password is provided. // RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.1 @@ -330,38 +309,37 @@ func (irc *Connection) Connect(server string) error { irc.stopped = false // make sure everything is ready for connection - if 0 == len(irc.server) { + if len(irc.server) == 0 { return errors.New("empty 'server'") } - if 1 != strings.Count(irc.server, ":") { + if strings.Count(irc.server, ":") != 1 { return errors.New("wrong number of ':' in address") } - if 0 == strings.Index(irc.server, ":") { + if strings.Index(irc.server, ":") == 0 { return errors.New("hostname is missing") } - if (len(irc.server) - 1) == strings.Index(irc.server, ":") { + if strings.Index(irc.server, ":") == len(irc.server)-1 { return errors.New("port missing") } // check for valid range ports := strings.Split(irc.server, ":")[1] port, err := strconv.Atoi(ports) - if nil != err { + if err != nil { return errors.New("extracting port failed") } if !((port >= 0) && (port <= 65535)) { return errors.New("port number outside valid range") } - if nil == irc.Log { + if irc.Log == nil { return errors.New("'Log' points to nil") } - if 0 == len(irc.nick) { + if len(irc.nick) == 0 { return errors.New("empty 'user'") } - if 0 == len(irc.user) { + if len(irc.user) == 0 { return errors.New("empty 'user'") } -// var err error if irc.UseTLS { if irc.netsock, err = net.DialTimeout("tcp", irc.server, irc.Timeout); err == nil { irc.socket = tls.Client(irc.netsock, irc.TLSConfig) @@ -390,16 +368,15 @@ func (irc *Connection) Connect(server string) error { return nil } - // Create a connection with the (publicly visible) nickname and username. -// The nickname is later used to address the user. Returns nil if nick +// The nickname is later used to address the user. Returns nil if nick // or user are empty. func IRC(nick, user string) *Connection { // catch invalid values - if 0 == len(nick) { + if len(nick) == 0 { return nil } - if 0 == len(user) { + if len(user) == 0 { return nil } @@ -421,4 +398,3 @@ func IRC(nick, user string) *Connection { irc.setupCallbacks() return irc } - diff --git a/irc_test.go b/irc_test.go index ecef112..36c4374 100644 --- a/irc_test.go +++ b/irc_test.go @@ -210,7 +210,7 @@ func TestClearCallback(t *testing.T) { func TestIRCemptyNick(t *testing.T) { irccon := IRC("", "go-eventirc") irccon = nil - if nil != irccon { + if irccon != nil { t.Error("empty nick didn't result in error") t.Fail() } @@ -218,7 +218,7 @@ func TestIRCemptyNick(t *testing.T) { func TestIRCemptyUser(t *testing.T) { irccon := IRC("go-eventirc", "") - if nil != irccon { + if irccon != nil { t.Error("empty user didn't result in error") } }