mirror of https://github.com/fluffle/goirc
Move to using the new event registry internally.
This commit is contained in:
parent
a2b53e6fc3
commit
5e814babc7
|
@ -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),
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue