From 303d3724e2c624916d89077bbb632861b5b4b7e7 Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Thu, 28 Jul 2011 00:24:07 +0100 Subject: [PATCH] Break out unboxing code; pass conn/line objects for other events. --- client/connection.go | 2 +- client/handlers.go | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/client/connection.go b/client/connection.go index eb7ff29..8a632b9 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.Dispatcher.Dispatch("disconnected", conn, nil) // reinit datastructures ready for next connection // do this here rather than after runLoop()'s for due to race conn.initialise() diff --git a/client/handlers.go b/client/handlers.go index 4e4ef02..85f3368 100644 --- a/client/handlers.go +++ b/client/handlers.go @@ -22,10 +22,14 @@ import ( // 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)) { - // Wrap f in an anonymous unboxing function - conn.Registry.AddHandler(name, event.NewHandler(func(ev ...interface{}) { + conn.Registry.AddHandler(name, IRCHandler(f)) +} + +// Wrap f in an anonymous unboxing function +func IRCHandler(f func(*Conn, *Line)) event.Handler { + return event.NewHandler(func(ev ...interface{}) { f(ev[0].(*Conn), ev[1].(*Line)) - })) + }) } // Basic ping/pong handler @@ -36,7 +40,7 @@ func (conn *Conn) h_PING(line *Line) { // Handler to trigger a "CONNECTED" event on receipt of numeric 001 func (conn *Conn) h_001(line *Line) { // we're connected! - conn.Dispatcher.Dispatch("connected") + conn.Dispatcher.Dispatch("connected", conn, line) // and we're being given our hostname (from the server's perspective) t := line.Args[len(line.Args)-1] if idx := strings.LastIndex(t, " "); idx != -1 {