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 (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"os"
|
|
||||||
"net"
|
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"event"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -23,7 +24,7 @@ type Conn struct {
|
||||||
Network string
|
Network string
|
||||||
|
|
||||||
// Event handler mapping
|
// Event handler mapping
|
||||||
events map[string][]func(*Conn, *Line)
|
Registry event.EventRegistry
|
||||||
// Map of channels we're on
|
// Map of channels we're on
|
||||||
chans map[string]*Channel
|
chans map[string]*Channel
|
||||||
// Map of nicks we know about
|
// 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.
|
// that you can add event handlers to it. See AddHandler() for details.
|
||||||
func New(nick, user, name string) *Conn {
|
func New(nick, user, name string) *Conn {
|
||||||
conn := &Conn{
|
conn := &Conn{
|
||||||
|
Registry: event.NewRegistry(),
|
||||||
in: make(chan *Line, 32),
|
in: make(chan *Line, 32),
|
||||||
out: make(chan string, 32),
|
out: make(chan string, 32),
|
||||||
Err: make(chan os.Error, 4),
|
Err: make(chan os.Error, 4),
|
||||||
|
|
|
@ -3,7 +3,10 @@ package client
|
||||||
// this file contains the basic set of event handlers
|
// this file contains the basic set of event handlers
|
||||||
// to manage tracking an irc connection etc.
|
// to manage tracking an irc connection etc.
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"event"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// AddHandler() adds an event handler for a specific IRC command.
|
// 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
|
// strings of digits like "332" (mainly because I really didn't feel like
|
||||||
// putting massive constant tables in).
|
// putting massive constant tables in).
|
||||||
func (conn *Conn) AddHandler(name string, f func(*Conn, *Line)) {
|
func (conn *Conn) AddHandler(name string, f func(*Conn, *Line)) {
|
||||||
n := strings.ToUpper(name)
|
// Wrap f in an anonymous unboxing function
|
||||||
if e, ok := conn.events[n]; ok {
|
conn.Registry.AddHandler(name, event.NewHandler(func(ev ...interface{}) {
|
||||||
conn.events[n] = append(e, f)
|
f(ev[0].(*Conn), ev[1].(*Line))
|
||||||
} else {
|
}))
|
||||||
e := make([]func(*Conn, *Line), 1, 10)
|
|
||||||
e[0] = f
|
|
||||||
conn.events[n] = e
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// loops through all event handlers for line.Cmd, running each in a goroutine
|
// 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...)
|
line.Args = append([]string{c}, line.Args...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if funcs, ok := conn.events[line.Cmd]; ok {
|
conn.Registry.Dispatch(line.Cmd, conn, line)
|
||||||
for _, f := range funcs {
|
|
||||||
go f(conn, line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Basic ping/pong handler
|
// 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
|
// sets up the internal event handlers to do useful things with lines
|
||||||
func (conn *Conn) setupEvents() {
|
func (conn *Conn) setupEvents() {
|
||||||
conn.events = make(map[string][]func(*Conn, *Line))
|
|
||||||
|
|
||||||
conn.AddHandler("CTCP", (*Conn).h_CTCP)
|
conn.AddHandler("CTCP", (*Conn).h_CTCP)
|
||||||
conn.AddHandler("JOIN", (*Conn).h_JOIN)
|
conn.AddHandler("JOIN", (*Conn).h_JOIN)
|
||||||
conn.AddHandler("KICK", (*Conn).h_KICK)
|
conn.AddHandler("KICK", (*Conn).h_KICK)
|
||||||
|
|
Loading…
Reference in New Issue