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 (
|
|
|
|
"strings"
|
2014-11-30 23:58:57 +00:00
|
|
|
"time"
|
2011-07-27 20:10:01 +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
|
2013-02-16 00:17:31 +00:00
|
|
|
var intHandlers = map[string]HandlerFunc{
|
2013-02-18 01:36:17 +00:00
|
|
|
REGISTER: (*Conn).h_REGISTER,
|
2013-02-18 06:28:22 +00:00
|
|
|
"001": (*Conn).h_001,
|
|
|
|
"433": (*Conn).h_433,
|
2013-03-08 01:33:56 +00:00
|
|
|
CTCP: (*Conn).h_CTCP,
|
|
|
|
NICK: (*Conn).h_NICK,
|
|
|
|
PING: (*Conn).h_PING,
|
2011-11-07 13:44:03 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2013-02-16 00:17:31 +00:00
|
|
|
// internal handlers are essential for the IRC client
|
|
|
|
// to function, so we don't save their Removers here
|
2013-09-27 23:15:54 +00:00
|
|
|
conn.handle(n, h)
|
2011-11-07 13:44:03 +00:00
|
|
|
}
|
2011-11-06 04:56:46 +00:00
|
|
|
}
|
|
|
|
|
2010-11-04 00:54:26 +00:00
|
|
|
// Basic ping/pong handler
|
|
|
|
func (conn *Conn) h_PING(line *Line) {
|
2013-03-08 01:33:56 +00:00
|
|
|
conn.Pong(line.Args[0])
|
2010-11-04 00:54:26 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2013-02-18 01:36:17 +00:00
|
|
|
// Handler for initial registration with server once tcp connection is made.
|
|
|
|
func (conn *Conn) h_REGISTER(line *Line) {
|
|
|
|
if conn.cfg.Pass != "" {
|
|
|
|
conn.Pass(conn.cfg.Pass)
|
|
|
|
}
|
|
|
|
conn.Nick(conn.cfg.Me.Nick)
|
|
|
|
conn.User(conn.cfg.Me.Ident, conn.cfg.Me.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler to trigger a CONNECTED event on receipt of numeric 001
|
2010-11-04 00:54:26 +00:00
|
|
|
func (conn *Conn) h_001(line *Line) {
|
|
|
|
// we're connected!
|
2014-11-30 23:58:57 +00:00
|
|
|
conn.dispatch(&Line{Cmd: CONNECTED, Time: time.Now()})
|
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 {
|
2015-01-02 12:58:50 +00:00
|
|
|
if conn.st != nil {
|
|
|
|
me := conn.Me()
|
|
|
|
conn.st.NickInfo(me.Nick, me.Ident, t[idx+1:], me.Name)
|
|
|
|
} else {
|
|
|
|
conn.cfg.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
|
2015-01-02 12:58:50 +00:00
|
|
|
me := conn.Me()
|
2013-02-16 18:05:56 +00:00
|
|
|
neu := conn.cfg.NewNick(line.Args[1])
|
2011-11-06 04:56:46 +00:00
|
|
|
conn.Nick(neu)
|
2015-11-29 03:28:05 +00:00
|
|
|
if !line.argslen(1) {
|
|
|
|
return
|
|
|
|
}
|
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...
|
2015-01-02 12:58:50 +00:00
|
|
|
if line.Args[1] == me.Nick {
|
2013-02-16 11:29:56 +00:00
|
|
|
if conn.st != nil {
|
2015-01-02 12:58:50 +00:00
|
|
|
conn.cfg.Me = conn.st.ReNick(me.Nick, neu)
|
2011-11-06 04:56:46 +00:00
|
|
|
} else {
|
2013-02-18 01:36:17 +00:00
|
|
|
conn.cfg.Me.Nick = neu
|
2011-11-06 04:56:46 +00:00
|
|
|
}
|
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) {
|
2013-03-08 01:33:56 +00:00
|
|
|
if line.Args[0] == VERSION {
|
|
|
|
conn.CtcpReply(line.Nick, VERSION, conn.cfg.Version)
|
2015-11-29 03:28:05 +00:00
|
|
|
} else if line.Args[0] == PING && line.argslen(2) {
|
2013-03-08 01:33:56 +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) {
|
2013-02-18 01:36:17 +00:00
|
|
|
if conn.st == nil && line.Nick == conn.cfg.Me.Nick {
|
|
|
|
conn.cfg.Me.Nick = line.Args[0]
|
2011-11-06 04:56:46 +00:00
|
|
|
}
|
|
|
|
}
|