diff --git a/client/connection.go b/client/connection.go index 12d507a..cd2598e 100644 --- a/client/connection.go +++ b/client/connection.go @@ -2,10 +2,11 @@ package client import ( "bufio" - "os" - "net" "crypto/tls" + "event" "fmt" + "net" + "os" "strings" "time" ) @@ -23,7 +24,7 @@ type Conn struct { Network string // Event handler mapping - events map[string][]func(*Conn, *Line) + Registry event.EventRegistry // Map of channels we're on chans map[string]*Channel // Map of nicks we know about @@ -72,6 +73,7 @@ type Conn struct { // that you can add event handlers to it. See AddHandler() for details. func New(nick, user, name string) *Conn { conn := &Conn{ + Registry: event.NewRegistry(), in: make(chan *Line, 32), out: make(chan string, 32), Err: make(chan os.Error, 4), diff --git a/client/handlers.go b/client/handlers.go index aab0e14..3359ce0 100644 --- a/client/handlers.go +++ b/client/handlers.go @@ -3,7 +3,10 @@ package client // this file contains the basic set of event handlers // to manage tracking an irc connection etc. -import "strings" +import ( + "event" + "strings" +) // AddHandler() adds an event handler for a specific IRC command. // @@ -19,14 +22,10 @@ import "strings" // 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)) { - n := strings.ToUpper(name) - if e, ok := conn.events[n]; ok { - conn.events[n] = append(e, f) - } else { - e := make([]func(*Conn, *Line), 1, 10) - e[0] = f - conn.events[n] = e - } + // Wrap f in an anonymous unboxing function + conn.Registry.AddHandler(name, event.NewHandler(func(ev ...interface{}) { + f(ev[0].(*Conn), ev[1].(*Line)) + })) } // loops through all event handlers for line.Cmd, running each in a goroutine @@ -61,11 +60,7 @@ func (conn *Conn) dispatchEvent(line *Line) { line.Args = append([]string{c}, line.Args...) } } - if funcs, ok := conn.events[line.Cmd]; ok { - for _, f := range funcs { - go f(conn, line) - } - } + conn.Registry.Dispatch(line.Cmd, conn, line) } // Basic ping/pong handler @@ -324,8 +319,6 @@ func (conn *Conn) h_671(line *Line) { // 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)