Reorganise handlers into separate methods rather than anonymous functions.

This commit is contained in:
Alex Bramley 2010-11-04 00:54:26 +00:00
parent c419ef1ead
commit e611672b06
1 changed files with 413 additions and 401 deletions

View File

@ -75,24 +75,13 @@ func (conn *Conn) dispatchEvent(line *Line) {
} }
} }
// sets up the internal event handlers to do useful things with lines
// XXX: is there a better way of doing this?
// Turns out there may be but it's not actually implemented in the language yet
// according to the people on freenode/#go-nuts ... :-(
// see: http://golang.org/doc/go_spec.html#Method_expressions for details
// I think this means we should be able to do something along the lines of:
// conn.AddHandler("event", (*Conn).h_handler);
// where h_handler is declared in the irc package as:
// func (conn *Conn) h_handler(line *Line) {}
// in the future, but for now the compiler throws a hissy fit.
func (conn *Conn) setupEvents() {
conn.events = make(map[string][]func(*Conn, *Line))
// Basic ping/pong handler // Basic ping/pong handler
conn.AddHandler("PING", func(conn *Conn, line *Line) { conn.Raw("PONG :" + line.Text) }) func (conn *Conn) h_PING(line *Line) {
conn.Raw("PONG :" + line.Text)
}
// Handler to trigger a "CONNECTED" event on receipt of numeric 001 // Handler to trigger a "CONNECTED" event on receipt of numeric 001
conn.AddHandler("001", func(conn *Conn, line *Line) { func (conn *Conn) h_001(line *Line) {
// we're connected! // we're connected!
conn.connected = true conn.connected = true
conn.dispatchEvent(&Line{Cmd: "CONNECTED"}) conn.dispatchEvent(&Line{Cmd: "CONNECTED"})
@ -103,7 +92,7 @@ func (conn *Conn) setupEvents() {
conn.Me.Host = h[idx+1 : len(h)] conn.Me.Host = h[idx+1 : len(h)]
} }
} }
}) }
// XXX: do we need 005 protocol support message parsing here? // XXX: do we need 005 protocol support message parsing here?
// probably in the future, but I can't quite be arsed yet. // probably in the future, but I can't quite be arsed yet.
@ -114,7 +103,7 @@ func (conn *Conn) setupEvents() {
*/ */
// Handler to deal with "433 :Nickname already in use" // Handler to deal with "433 :Nickname already in use"
conn.AddHandler("433", func(conn *Conn, line *Line) { func (conn *Conn) h_433(line *Line) {
// Args[1] is the new nick we were attempting to acquire // Args[1] is the new nick we were attempting to acquire
conn.Nick(line.Args[1] + "_") conn.Nick(line.Args[1] + "_")
// if this is happening before we're properly connected (i.e. the nick // if this is happening before we're properly connected (i.e. the nick
@ -123,29 +112,29 @@ func (conn *Conn) setupEvents() {
if !conn.connected && line.Args[1] == conn.Me.Nick { if !conn.connected && line.Args[1] == conn.Me.Nick {
conn.Me.ReNick(line.Args[1] + "_") conn.Me.ReNick(line.Args[1] + "_")
} }
}) }
// Handler NICK messages to inform us about nick changes // Handler NICK messages to inform us about nick changes
conn.AddHandler("NICK", func(conn *Conn, line *Line) { func (conn *Conn) h_NICK(line *Line) {
// all nicks should be handled the same way, our own included // all nicks should be handled the same way, our own included
if n := conn.GetNick(line.Nick); n != nil { if n := conn.GetNick(line.Nick); n != nil {
n.ReNick(line.Text) n.ReNick(line.Text)
} else { } else {
conn.error("irc.NICK(): buh? unknown nick %s.", line.Nick) conn.error("irc.NICK(): buh? unknown nick %s.", line.Nick)
} }
}) }
// Handle VERSION requests and CTCP PING // Handle VERSION requests and CTCP PING
conn.AddHandler("CTCP", func(conn *Conn, line *Line) { func (conn *Conn) h_CTCP(line *Line) {
if line.Args[0] == "VERSION" { if line.Args[0] == "VERSION" {
conn.CtcpReply(line.Nick, "VERSION", "powered by goirc...") conn.CtcpReply(line.Nick, "VERSION", "powered by goirc...")
} else if line.Args[0] == "PING" { } else if line.Args[0] == "PING" {
conn.CtcpReply(line.Nick, "PING", line.Text) conn.CtcpReply(line.Nick, "PING", line.Text)
} }
}) }
// Handle JOINs to channels to maintain state // Handle JOINs to channels to maintain state
conn.AddHandler("JOIN", func(conn *Conn, line *Line) { func (conn *Conn) h_JOIN(line *Line) {
// Some IRCds (ircu) send ':n!u@h JOIN #chan' not ':n!u@h JOIN :#chan' // Some IRCds (ircu) send ':n!u@h JOIN #chan' not ':n!u@h JOIN :#chan'
// Unfortunately the RFCs aren't specific about this. In fact the // Unfortunately the RFCs aren't specific about this. In fact the
// examples indicate no colon should be sent, but it's unusual. // examples indicate no colon should be sent, but it's unusual.
@ -181,10 +170,10 @@ func (conn *Conn) setupEvents() {
} }
// this takes care of both nick and channel linking \o/ // this takes care of both nick and channel linking \o/
ch.AddNick(n) ch.AddNick(n)
}) }
// Handle PARTs from channels to maintain state // Handle PARTs from channels to maintain state
conn.AddHandler("PART", func(conn *Conn, line *Line) { func (conn *Conn) h_PART(line *Line) {
// Some IRCds (ircu) send 'PART :#chan' when there's no part message // Some IRCds (ircu) send 'PART :#chan' when there's no part message
// instead of 'PART #chan'. This is *questionable* behaviour... // instead of 'PART #chan'. This is *questionable* behaviour...
var chname string var chname string
@ -200,10 +189,10 @@ func (conn *Conn) setupEvents() {
} else { } else {
conn.error("irc.PART(): buh? PART of channel %s by nick %s", chname, line.Nick) conn.error("irc.PART(): buh? PART of channel %s by nick %s", chname, line.Nick)
} }
}) }
// Handle KICKs from channels to maintain state // Handle KICKs from channels to maintain state
conn.AddHandler("KICK", func(conn *Conn, line *Line) { func (conn *Conn) h_KICK(line *Line) {
// XXX: this won't handle autorejoining channels on KICK // XXX: this won't handle autorejoining channels on KICK
// it's trivial to do this in a seperate handler... // it's trivial to do this in a seperate handler...
ch := conn.GetChannel(line.Args[0]) ch := conn.GetChannel(line.Args[0])
@ -213,20 +202,20 @@ func (conn *Conn) setupEvents() {
} else { } else {
conn.error("irc.KICK(): buh? KICK from channel %s of nick %s", line.Args[0], line.Args[1]) conn.error("irc.KICK(): buh? KICK from channel %s of nick %s", line.Args[0], line.Args[1])
} }
}) }
// Handle other people's QUITs // Handle other people's QUITs
conn.AddHandler("QUIT", func(conn *Conn, line *Line) { func (conn *Conn) h_QUIT(line *Line) {
if n := conn.GetNick(line.Nick); n != nil { if n := conn.GetNick(line.Nick); n != nil {
n.Delete() n.Delete()
} else { } else {
conn.error("irc.QUIT(): buh? QUIT from unknown nick %s", line.Nick) conn.error("irc.QUIT(): buh? QUIT from unknown nick %s", line.Nick)
} }
}) }
// Handle MODE changes for channels we know about (and our nick personally) // Handle MODE changes for channels we know about (and our nick personally)
// this is moderately ugly. suggestions for improvement welcome // this is moderately ugly. suggestions for improvement welcome
conn.AddHandler("MODE", func(conn *Conn, line *Line) { func (conn *Conn) h_MODE(line *Line) {
// channel modes first // channel modes first
if ch := conn.GetChannel(line.Args[0]); ch != nil { if ch := conn.GetChannel(line.Args[0]); ch != nil {
modeargs := line.Args[2:len(line.Args)] modeargs := line.Args[2:len(line.Args)]
@ -326,19 +315,19 @@ func (conn *Conn) setupEvents() {
conn.error("irc.MODE(): buh? not sure what to do with chan MODE %s", strings.Join(line.Args, " ")) conn.error("irc.MODE(): buh? not sure what to do with chan MODE %s", strings.Join(line.Args, " "))
} }
} }
}) }
// Handle TOPIC changes for channels // Handle TOPIC changes for channels
conn.AddHandler("TOPIC", func(conn *Conn, line *Line) { func (conn *Conn) h_TOPIC(line *Line) {
if ch := conn.GetChannel(line.Args[0]); ch != nil { if ch := conn.GetChannel(line.Args[0]); ch != nil {
ch.Topic = line.Text ch.Topic = line.Text
} else { } else {
conn.error("irc.TOPIC(): buh? topic change on unknown channel %s", line.Args[0]) conn.error("irc.TOPIC(): buh? topic change on unknown channel %s", line.Args[0])
} }
}) }
// Handle 311 whois reply // Handle 311 whois reply
conn.AddHandler("311", func(conn *Conn, line *Line) { func (conn *Conn) h_311(line *Line) {
if n := conn.GetNick(line.Args[1]); n != nil { if n := conn.GetNick(line.Args[1]); n != nil {
n.Ident = line.Args[2] n.Ident = line.Args[2]
n.Host = line.Args[3] n.Host = line.Args[3]
@ -346,10 +335,10 @@ func (conn *Conn) setupEvents() {
} else { } else {
conn.error("irc.311(): buh? received WHOIS info for unknown nick %s", line.Args[1]) conn.error("irc.311(): buh? received WHOIS info for unknown nick %s", line.Args[1])
} }
}) }
// Handle 324 mode reply // Handle 324 mode reply
conn.AddHandler("324", func(conn *Conn, line *Line) { func (conn *Conn) h_324(line *Line) {
// XXX: copypasta from MODE, needs tidying. // XXX: copypasta from MODE, needs tidying.
if ch := conn.GetChannel(line.Args[1]); ch != nil { if ch := conn.GetChannel(line.Args[1]); ch != nil {
modeargs := line.Args[3:len(line.Args)] modeargs := line.Args[3:len(line.Args)]
@ -397,19 +386,19 @@ func (conn *Conn) setupEvents() {
} else { } else {
conn.error("irc.324(): buh? received MODE settings for unknown channel %s", line.Args[1]) conn.error("irc.324(): buh? received MODE settings for unknown channel %s", line.Args[1])
} }
}) }
// Handle 332 topic reply on join to channel // Handle 332 topic reply on join to channel
conn.AddHandler("332", func(conn *Conn, line *Line) { func (conn *Conn) h_332(line *Line) {
if ch := conn.GetChannel(line.Args[1]); ch != nil { if ch := conn.GetChannel(line.Args[1]); ch != nil {
ch.Topic = line.Text ch.Topic = line.Text
} else { } else {
conn.error("irc.332(): buh? received TOPIC value for unknown channel %s", line.Args[1]) conn.error("irc.332(): buh? received TOPIC value for unknown channel %s", line.Args[1])
} }
}) }
// Handle 352 who reply // Handle 352 who reply
conn.AddHandler("352", func(conn *Conn, line *Line) { func (conn *Conn) h_352(line *Line) {
if n := conn.GetNick(line.Args[5]); n != nil { if n := conn.GetNick(line.Args[5]); n != nil {
n.Ident = line.Args[2] n.Ident = line.Args[2]
n.Host = line.Args[3] n.Host = line.Args[3]
@ -427,10 +416,10 @@ func (conn *Conn) setupEvents() {
} else { } else {
conn.error("irc.352(): buh? got WHO reply for unknown nick %s", line.Args[5]) conn.error("irc.352(): buh? got WHO reply for unknown nick %s", line.Args[5])
} }
}) }
// Handle 353 names reply // Handle 353 names reply
conn.AddHandler("353", func(conn *Conn, line *Line) { func (conn *Conn) h_353(line *Line) {
if ch := conn.GetChannel(line.Args[2]); ch != nil { if ch := conn.GetChannel(line.Args[2]); ch != nil {
nicks := strings.Split(line.Text, " ", -1) nicks := strings.Split(line.Text, " ", -1)
for _, nick := range nicks { for _, nick := range nicks {
@ -471,14 +460,37 @@ func (conn *Conn) setupEvents() {
} else { } else {
conn.error("irc.353(): buh? received NAMES list for unknown channel %s", line.Args[2]) conn.error("irc.353(): buh? received NAMES list for unknown channel %s", line.Args[2])
} }
}) }
// Handle 671 whois reply (nick connected via SSL) // Handle 671 whois reply (nick connected via SSL)
conn.AddHandler("671", func(conn *Conn, line *Line) { func (conn *Conn) h_671(line *Line) {
if n := conn.GetNick(line.Args[1]); n != nil { if n := conn.GetNick(line.Args[1]); n != nil {
n.Modes.SSL = true n.Modes.SSL = true
} else { } else {
conn.error("irc.671(): buh? received WHOIS SSL info for unknown nick %s", line.Args[1]) conn.error("irc.671(): buh? received WHOIS SSL info for unknown nick %s", line.Args[1])
} }
}) }
// sets up the internal event handlers to do useful things with lines
func (conn *Conn) setupEvents() {
conn.events = make(map[string][]func(*Conn, *Line))
conn.AddHandler("CTCP", (*Conn).h_CTCP)
conn.AddHandler("JOIN", (*Conn).h_JOIN)
conn.AddHandler("KICK", (*Conn).h_KICK)
conn.AddHandler("MODE", (*Conn).h_MODE)
conn.AddHandler("NICK", (*Conn).h_NICK)
conn.AddHandler("PART", (*Conn).h_PART)
conn.AddHandler("PING", (*Conn).h_PING)
conn.AddHandler("QUIT", (*Conn).h_QUIT)
conn.AddHandler("TOPIC", (*Conn).h_TOPIC)
conn.AddHandler("001", (*Conn).h_001)
conn.AddHandler("311", (*Conn).h_311)
conn.AddHandler("324", (*Conn).h_324)
conn.AddHandler("332", (*Conn).h_332)
conn.AddHandler("352", (*Conn).h_352)
conn.AddHandler("353", (*Conn).h_353)
conn.AddHandler("433", (*Conn).h_433)
conn.AddHandler("671", (*Conn).h_671)
} }