2010-11-21 19:53:14 +00:00
|
|
|
package client
|
2009-11-29 20:23:15 +00:00
|
|
|
|
|
|
|
// this file contains the basic set of event handlers
|
|
|
|
// to manage tracking an irc connection etc.
|
|
|
|
|
2011-07-27 20:10:01 +00:00
|
|
|
import (
|
2011-11-13 14:07:19 +00:00
|
|
|
"github.com/fluffle/goevent/event"
|
2011-07-27 20:10:01 +00:00
|
|
|
"strings"
|
2014-04-06 21:14:16 +00:00
|
|
|
"time"
|
2011-07-27 20:10:01 +00:00
|
|
|
)
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2013-02-15 19:11:25 +00:00
|
|
|
// Consts for unnamed events.
|
|
|
|
const (
|
|
|
|
INIT = "init"
|
|
|
|
CONNECTED = "connected"
|
|
|
|
DISCONNECTED = "disconnected"
|
|
|
|
)
|
|
|
|
|
2011-07-30 18:41:50 +00:00
|
|
|
// An IRC handler looks like this:
|
2013-02-15 19:11:25 +00:00
|
|
|
type Handler func(*Conn, *Line)
|
2011-07-30 18:41:50 +00:00
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// AddHandler() adds an event handler for a specific IRC command.
|
|
|
|
//
|
|
|
|
// 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
|
2011-11-06 04:56:46 +00:00
|
|
|
// strings of digits like "332" (mainly because I really didn't feel like
|
2009-12-18 22:39:22 +00:00
|
|
|
// putting massive constant tables in).
|
2013-02-15 19:11:25 +00:00
|
|
|
func (conn *Conn) AddHandler(name string, f Handler) event.Handler {
|
2011-11-07 13:34:13 +00:00
|
|
|
h := NewHandler(f)
|
|
|
|
conn.ER.AddHandler(h, name)
|
|
|
|
return h
|
2011-07-27 23:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap f in an anonymous unboxing function
|
2013-02-15 19:11:25 +00:00
|
|
|
func NewHandler(f Handler) event.Handler {
|
2011-07-27 23:24:07 +00:00
|
|
|
return event.NewHandler(func(ev ...interface{}) {
|
2011-07-27 20:10:01 +00:00
|
|
|
f(ev[0].(*Conn), ev[1].(*Line))
|
2011-07-27 23:24:07 +00:00
|
|
|
})
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 04:56:46 +00:00
|
|
|
// sets up the internal event handlers to do essential IRC protocol things
|
2011-11-07 13:44:03 +00:00
|
|
|
var intHandlers map[string]event.Handler
|
2013-02-15 19:11:25 +00:00
|
|
|
|
2011-11-07 13:44:03 +00:00
|
|
|
func init() {
|
|
|
|
intHandlers = make(map[string]event.Handler)
|
2013-02-16 10:25:35 +00:00
|
|
|
intHandlers[INIT] = NewHandler((*Conn).h_INIT)
|
2011-11-07 13:44:03 +00:00
|
|
|
intHandlers["001"] = NewHandler((*Conn).h_001)
|
|
|
|
intHandlers["433"] = NewHandler((*Conn).h_433)
|
|
|
|
intHandlers["CTCP"] = NewHandler((*Conn).h_CTCP)
|
|
|
|
intHandlers["NICK"] = NewHandler((*Conn).h_NICK)
|
|
|
|
intHandlers["PING"] = NewHandler((*Conn).h_PING)
|
|
|
|
}
|
|
|
|
|
2011-11-06 04:56:46 +00:00
|
|
|
func (conn *Conn) addIntHandlers() {
|
2011-11-07 13:44:03 +00:00
|
|
|
for n, h := range intHandlers {
|
|
|
|
conn.ER.AddHandler(h, n)
|
|
|
|
}
|
2011-11-06 04:56:46 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 19:11:25 +00:00
|
|
|
// Password/User/Nick broadcast on connection.
|
2013-02-16 10:25:35 +00:00
|
|
|
func (conn *Conn) h_INIT(line *Line) {
|
2013-02-15 19:11:25 +00:00
|
|
|
if conn.password != "" {
|
|
|
|
conn.Pass(conn.password)
|
|
|
|
}
|
|
|
|
conn.Nick(conn.Me.Nick)
|
|
|
|
conn.User(conn.Me.Ident, conn.Me.Name)
|
|
|
|
}
|
|
|
|
|
2010-11-04 00:54:26 +00:00
|
|
|
// Basic ping/pong handler
|
|
|
|
func (conn *Conn) h_PING(line *Line) {
|
2010-11-23 22:07:21 +00:00
|
|
|
conn.Raw("PONG :" + line.Args[0])
|
2010-11-04 00:54:26 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2010-11-04 00:54:26 +00:00
|
|
|
// Handler to trigger a "CONNECTED" event on receipt of numeric 001
|
|
|
|
func (conn *Conn) h_001(line *Line) {
|
|
|
|
// we're connected!
|
2013-02-15 19:11:25 +00:00
|
|
|
conn.ED.Dispatch(CONNECTED, conn, line)
|
2010-11-04 00:54:26 +00:00
|
|
|
// and we're being given our hostname (from the server's perspective)
|
2010-11-23 22:07:21 +00:00
|
|
|
t := line.Args[len(line.Args)-1]
|
|
|
|
if idx := strings.LastIndex(t, " "); idx != -1 {
|
|
|
|
t = t[idx+1:]
|
|
|
|
if idx = strings.Index(t, "@"); idx != -1 {
|
|
|
|
conn.Me.Host = t[idx+1:]
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2010-11-04 00:54:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2010-11-04 00:54:26 +00:00
|
|
|
// XXX: do we need 005 protocol support message parsing here?
|
|
|
|
// probably in the future, but I can't quite be arsed yet.
|
|
|
|
/*
|
|
|
|
:irc.pl0rt.org 005 GoTest CMDS=KNOCK,MAP,DCCALLOW,USERIP UHNAMES NAMESX SAFELIST HCN MAXCHANNELS=20 CHANLIMIT=#:20 MAXLIST=b:60,e:60,I:60 NICKLEN=30 CHANNELLEN=32 TOPICLEN=307 KICKLEN=307 AWAYLEN=307 :are supported by this server
|
|
|
|
:irc.pl0rt.org 005 GoTest MAXTARGETS=20 WALLCHOPS WATCH=128 WATCHOPTS=A SILENCE=15 MODES=12 CHANTYPES=# PREFIX=(qaohv)~&@%+ CHANMODES=beI,kfL,lj,psmntirRcOAQKVCuzNSMT NETWORK=bb101.net CASEMAPPING=ascii EXTBAN=~,cqnr ELIST=MNUCT :are supported by this server
|
|
|
|
:irc.pl0rt.org 005 GoTest STATUSMSG=~&@%+ EXCEPTS INVEX :are supported by this server
|
|
|
|
*/
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2010-11-04 00:54:26 +00:00
|
|
|
// Handler to deal with "433 :Nickname already in use"
|
|
|
|
func (conn *Conn) h_433(line *Line) {
|
|
|
|
// Args[1] is the new nick we were attempting to acquire
|
2013-01-06 21:01:55 +00:00
|
|
|
neu := conn.NewNick(line.Args[1])
|
2011-11-06 04:56:46 +00:00
|
|
|
conn.Nick(neu)
|
2014-04-06 21:14:16 +00:00
|
|
|
// arm a timer to try get back our nick.
|
|
|
|
go func(old string) {
|
|
|
|
retry := 1 * time.Minute
|
|
|
|
for time.Sleep(retry); conn.cfg.Me.Nick != old; time.Sleep(retry) {
|
|
|
|
conn.Nick(old)
|
|
|
|
}
|
|
|
|
}(conn.cfg.Me.Nick)
|
2010-11-04 00:54:26 +00:00
|
|
|
// if this is happening before we're properly connected (i.e. the nick
|
|
|
|
// we sent in the initial NICK command is in use) we will not receive
|
|
|
|
// a NICK message to confirm our change of nick, so ReNick here...
|
2011-07-22 00:17:35 +00:00
|
|
|
if line.Args[1] == conn.Me.Nick {
|
2011-11-06 04:56:46 +00:00
|
|
|
if conn.st {
|
|
|
|
conn.ST.ReNick(conn.Me.Nick, neu)
|
|
|
|
} else {
|
|
|
|
conn.Me.Nick = neu
|
|
|
|
}
|
2010-11-04 00:54:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2010-11-04 00:54:26 +00:00
|
|
|
// Handle VERSION requests and CTCP PING
|
|
|
|
func (conn *Conn) h_CTCP(line *Line) {
|
|
|
|
if line.Args[0] == "VERSION" {
|
|
|
|
conn.CtcpReply(line.Nick, "VERSION", "powered by goirc...")
|
|
|
|
} else if line.Args[0] == "PING" {
|
2010-11-23 22:07:21 +00:00
|
|
|
conn.CtcpReply(line.Nick, "PING", line.Args[2])
|
2010-11-04 00:54:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2011-11-06 04:56:46 +00:00
|
|
|
// Handle updating our own NICK if we're not using the state tracker
|
|
|
|
func (conn *Conn) h_NICK(line *Line) {
|
|
|
|
if !conn.st && line.Nick == conn.Me.Nick {
|
|
|
|
conn.Me.Nick = line.Args[0]
|
|
|
|
}
|
|
|
|
}
|