Better nick recapture

This commit is contained in:
tj 2011-02-03 01:16:13 +01:00
parent cf4e9f829a
commit ea8495857e
3 changed files with 19 additions and 8 deletions

4
irc.go
View File

@ -88,6 +88,10 @@ func pinger(i *IRCConnection) {
case <-i.ticker2: case <-i.ticker2:
//Ping every 15 minutes. //Ping every 15 minutes.
i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds())) 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))
}
} }
} }
} }

View File

@ -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("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.AddCallback("437", func(e *IRCEvent) {
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("433", func(e *IRCEvent) { irc.AddCallback("433", func(e *IRCEvent) {
if len(irc.nick) > 8 { if len(irc.nickcurrent) > 8 {
irc.nick = "_" + irc.nick irc.nickcurrent = "_" + irc.nickcurrent
} else { } 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) { irc.AddCallback("PONG", func(e *IRCEvent) {
ns, _ := strconv.Atoi64(e.Message) 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]
}
}) })
} }

View File

@ -14,7 +14,8 @@ type IRCConnection struct {
pread, pwrite chan string pread, pwrite chan string
Error chan os.Error Error chan os.Error
syncreader, syncwriter chan bool syncreader, syncwriter chan bool
nick string nick string //The nickname we want.
nickcurrent string //The nickname we currently have.
user string user string
registered bool registered bool
server string server string