From ea8495857e2d08a3bd970596085e6beb6fd214ea Mon Sep 17 00:00:00 2001 From: tj Date: Thu, 3 Feb 2011 01:16:13 +0100 Subject: [PATCH] Better nick recapture --- irc.go | 4 ++++ irc_callback.go | 20 +++++++++++++------- irc_struct.go | 3 ++- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/irc.go b/irc.go index 64e09de..11396e5 100644 --- a/irc.go +++ b/irc.go @@ -88,6 +88,10 @@ func pinger(i *IRCConnection) { case <-i.ticker2: //Ping every 15 minutes. i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds())) + //Try to recapture nickname if it's not as configured. + if i.nick != i.nickcurrent { + i.SendRaw(fmt.Sprintf("NICK %s", i.nick)) + } } } } diff --git a/irc_callback.go b/irc_callback.go index d68f92b..ac9cfba 100644 --- a/irc_callback.go +++ b/irc_callback.go @@ -84,21 +84,27 @@ func (irc *IRCConnection) setupCallbacks() { irc.AddCallback("CTCP_PING", func(e *IRCEvent) { irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01%s\x01", e.Nick, e.Message)) }) irc.AddCallback("437", func(e *IRCEvent) { - irc.nick = irc.nick + "_" - irc.SendRaw(fmt.Sprintf("NICK %s", irc.nick)) + irc.nickcurrent = irc.nickcurrent + "_" + irc.SendRaw(fmt.Sprintf("NICK %s", irc.nickcurrent)) }) irc.AddCallback("433", func(e *IRCEvent) { - if len(irc.nick) > 8 { - irc.nick = "_" + irc.nick + if len(irc.nickcurrent) > 8 { + irc.nickcurrent = "_" + irc.nickcurrent } else { - irc.nick = irc.nick + "_" + irc.nickcurrent = irc.nickcurrent + "_" } - irc.SendRaw(fmt.Sprintf("NICK %s", irc.nick)) + irc.SendRaw(fmt.Sprintf("NICK %s", irc.nickcurrent)) }) irc.AddCallback("PONG", func(e *IRCEvent) { ns, _ := strconv.Atoi64(e.Message) - fmt.Printf("Lag: %fs\n", float((time.Nanoseconds()-ns))/1000/1000/1000) + fmt.Printf("Lag: %fs\n", float32((time.Nanoseconds()-ns))/1000/1000/1000) + }) + + irc.AddCallback("NICK", func(e *IRCEvent) { + if e.Nick == irc.nick { + irc.nickcurrent = e.Arguments[0] + } }) } diff --git a/irc_struct.go b/irc_struct.go index c9fd948..23b6c09 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -14,7 +14,8 @@ type IRCConnection struct { pread, pwrite chan string Error chan os.Error syncreader, syncwriter chan bool - nick string + nick string //The nickname we want. + nickcurrent string //The nickname we currently have. user string registered bool server string