2013-02-16 00:17:31 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2013-09-27 21:19:40 +00:00
|
|
|
"github.com/fluffle/goirc/logging"
|
2013-03-10 13:30:00 +00:00
|
|
|
"runtime"
|
2013-02-16 00:17:31 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2013-03-10 13:16:14 +00:00
|
|
|
// An IRC Handler looks like this:
|
2013-02-16 00:17:31 +00:00
|
|
|
type Handler interface {
|
|
|
|
Handle(*Conn, *Line)
|
|
|
|
}
|
|
|
|
|
|
|
|
// And when they've been added to the client they are removable.
|
|
|
|
type Remover interface {
|
|
|
|
Remove()
|
|
|
|
}
|
|
|
|
|
2013-03-10 13:16:14 +00:00
|
|
|
// A HandlerFunc implements Handler.
|
2013-02-16 00:17:31 +00:00
|
|
|
type HandlerFunc func(*Conn, *Line)
|
|
|
|
|
|
|
|
func (hf HandlerFunc) Handle(conn *Conn, line *Line) {
|
|
|
|
hf(conn, line)
|
|
|
|
}
|
|
|
|
|
2013-03-10 13:16:14 +00:00
|
|
|
// Handlers are organised using a map of linked-lists, with each map
|
|
|
|
// key representing an IRC verb or numeric, and the linked list values
|
|
|
|
// being handlers that are executed in parallel when a Line from the
|
|
|
|
// server with that verb or numeric arrives.
|
|
|
|
type hSet struct {
|
|
|
|
set map[string]*hList
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2013-02-16 00:17:31 +00:00
|
|
|
type hList struct {
|
|
|
|
start, end *hNode
|
|
|
|
}
|
|
|
|
|
2013-03-10 13:16:14 +00:00
|
|
|
// Storing the forward and backward links in the node allows O(1) removal.
|
|
|
|
// This probably isn't strictly necessary but I think it's kinda nice.
|
2013-02-16 00:17:31 +00:00
|
|
|
type hNode struct {
|
|
|
|
next, prev *hNode
|
|
|
|
set *hSet
|
|
|
|
event string
|
|
|
|
handler Handler
|
|
|
|
}
|
|
|
|
|
2013-03-10 13:30:00 +00:00
|
|
|
// A hNode implements both Handler (with configurable panic recovery)...
|
2013-02-16 00:17:31 +00:00
|
|
|
func (hn *hNode) Handle(conn *Conn, line *Line) {
|
2013-03-10 13:30:00 +00:00
|
|
|
defer conn.cfg.Recover(conn, line)
|
2013-02-16 00:17:31 +00:00
|
|
|
hn.handler.Handle(conn, line)
|
|
|
|
}
|
|
|
|
|
2013-03-10 13:16:14 +00:00
|
|
|
// ... and Remover.
|
2013-02-16 00:17:31 +00:00
|
|
|
func (hn *hNode) Remove() {
|
|
|
|
hn.set.remove(hn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerSet() *hSet {
|
|
|
|
return &hSet{set: make(map[string]*hList)}
|
|
|
|
}
|
|
|
|
|
2013-03-10 13:16:14 +00:00
|
|
|
// When a new Handler is added for an event, it is wrapped in a hNode and
|
|
|
|
// returned as a Remover so the caller can remove it at a later time.
|
2013-02-16 00:17:31 +00:00
|
|
|
func (hs *hSet) add(ev string, h Handler) Remover {
|
|
|
|
hs.Lock()
|
|
|
|
defer hs.Unlock()
|
|
|
|
ev = strings.ToLower(ev)
|
|
|
|
l, ok := hs.set[ev]
|
|
|
|
if !ok {
|
|
|
|
l = &hList{}
|
|
|
|
}
|
|
|
|
hn := &hNode{
|
|
|
|
set: hs,
|
|
|
|
event: ev,
|
|
|
|
handler: h,
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
l.start = hn
|
|
|
|
} else {
|
|
|
|
hn.prev = l.end
|
|
|
|
l.end.next = hn
|
|
|
|
}
|
|
|
|
l.end = hn
|
|
|
|
hs.set[ev] = l
|
|
|
|
return hn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *hSet) remove(hn *hNode) {
|
|
|
|
hs.Lock()
|
|
|
|
defer hs.Unlock()
|
|
|
|
l, ok := hs.set[hn.event]
|
|
|
|
if !ok {
|
|
|
|
logging.Error("Removing node for unknown event '%s'", hn.event)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if hn.next == nil {
|
|
|
|
l.end = hn.prev
|
|
|
|
} else {
|
|
|
|
hn.next.prev = hn.prev
|
|
|
|
}
|
|
|
|
if hn.prev == nil {
|
|
|
|
l.start = hn.next
|
|
|
|
} else {
|
|
|
|
hn.prev.next = hn.next
|
|
|
|
}
|
|
|
|
hn.next = nil
|
|
|
|
hn.prev = nil
|
|
|
|
hn.set = nil
|
|
|
|
if l.start == nil || l.end == nil {
|
|
|
|
delete(hs.set, hn.event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *hSet) dispatch(conn *Conn, line *Line) {
|
|
|
|
hs.RLock()
|
|
|
|
defer hs.RUnlock()
|
|
|
|
ev := strings.ToLower(line.Cmd)
|
|
|
|
list, ok := hs.set[ev]
|
2013-02-16 10:33:15 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2013-02-16 00:17:31 +00:00
|
|
|
for hn := list.start; hn != nil; hn = hn.next {
|
2013-04-08 17:04:40 +00:00
|
|
|
go hn.Handle(conn, line.Copy())
|
2013-02-16 00:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) Handle(name string, h Handler) Remover {
|
|
|
|
return conn.handlers.add(name, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Conn) HandleFunc(name string, hf HandlerFunc) Remover {
|
|
|
|
return conn.Handle(name, hf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Conn) dispatch(line *Line) {
|
|
|
|
conn.handlers.dispatch(conn, line)
|
|
|
|
}
|
2013-03-10 13:30:00 +00:00
|
|
|
|
|
|
|
func (conn *Conn) LogPanic(line *Line) {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
_, f, l, _ := runtime.Caller(2)
|
|
|
|
logging.Error("%s:%d: panic: %v", f, l, err)
|
2013-03-10 13:31:04 +00:00
|
|
|
}
|
2013-03-10 13:30:00 +00:00
|
|
|
}
|