From 1d82af0080f0309754ec0b5bfa3cf6b5cc57eb4f Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Sat, 30 Jul 2011 19:34:55 +0100 Subject: [PATCH 1/2] Impolite to break the 'IRC event provides Conn, Line' contract. --- client/connection.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/connection.go b/client/connection.go index f01b474..90b783f 100644 --- a/client/connection.go +++ b/client/connection.go @@ -266,7 +266,7 @@ func (conn *Conn) shutdown() { conn.sock.Close() conn.cSend <- true conn.cLoop <- true - conn.Dispatcher.Dispatch("disconnected", conn, nil) + conn.Dispatcher.Dispatch("disconnected", conn, &Line{}) // reinit datastructures ready for next connection // do this here rather than after runLoop()'s for due to race conn.initialise() From 43a2bf08f129337dceb4886396fdf160fc9b9361 Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Sat, 30 Jul 2011 19:41:50 +0100 Subject: [PATCH 2/2] Make IRCHandler the function type, make NewHandler the factory. --- client/handlers.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/client/handlers.go b/client/handlers.go index 89d6312..089c103 100644 --- a/client/handlers.go +++ b/client/handlers.go @@ -8,25 +8,23 @@ import ( "strings" ) +// An IRC handler looks like this: +type IRCHandler func(*Conn, *Line) + // AddHandler() adds an event handler for a specific IRC command. // -// Handlers take the form of an anonymous function (currently): -// func(conn *irc.Conn, line *irc.Line) { -// // handler code here -// } -// // Handlers are triggered on incoming Lines from the server, with the handler // "name" being equivalent to Line.Cmd. Read the RFCs for details on what // replies could come from the server. They'll generally be things like // "PRIVMSG", "JOIN", etc. but all the numeric replies are left as ascii // strings of digits like "332" (mainly because I really didn't feel like // putting massive constant tables in). -func (conn *Conn) AddHandler(name string, f func(*Conn, *Line)) { - conn.Registry.AddHandler(name, IRCHandler(f)) +func (conn *Conn) AddHandler(name string, f IRCHandler) { + conn.Registry.AddHandler(name, NewHandler(f)) } // Wrap f in an anonymous unboxing function -func IRCHandler(f func(*Conn, *Line)) event.Handler { +func NewHandler(f IRCHandler) event.Handler { return event.NewHandler(func(ev ...interface{}) { f(ev[0].(*Conn), ev[1].(*Line)) })