Scrape horse doc constants, handle all nick collision error types

This commit is contained in:
Ken-Håvard Lieng 2020-05-23 02:30:48 +02:00
parent e937f5d8b9
commit be8b785813
6 changed files with 202 additions and 110 deletions

View File

@ -208,25 +208,25 @@ func (c *Client) recv() {
}
switch msg.Command {
case Ping:
case PING:
go c.write("PONG :" + msg.LastParam())
case Join:
case JOIN:
if c.EqualFold(msg.Nick, c.GetNick()) {
c.addChannel(msg.Params[0])
}
case Nick:
case NICK:
if c.EqualFold(msg.Nick, c.GetNick()) {
c.setNick(msg.LastParam())
}
case Privmsg:
case PRIVMSG:
if ctcp := msg.ToCTCP(); ctcp != nil {
c.handleCTCP(ctcp, msg)
}
case ReplyWelcome:
case RPL_WELCOME:
c.setNick(msg.Params[0])
c.setRegistered(true)
c.flushChannels()
@ -235,15 +235,15 @@ func (c *Client) recv() {
c.sendRecv.Add(1)
go c.send()
case ReplyISupport:
case RPL_ISUPPORT:
c.Features.Parse(msg.Params)
case ErrNicknameInUse:
case ERR_NICKNAMEINUSE, ERR_NICKCOLLISION, ERR_UNAVAILRESOURCE:
if c.HandleNickInUse != nil {
go c.writeNick(c.HandleNickInUse(msg.Params[1]))
}
case Error:
case ERROR:
c.Messages <- msg
c.connChange(false, nil)
time.Sleep(5 * time.Second)

View File

@ -136,8 +136,8 @@ func TestRecv(t *testing.T) {
assert.Equal(t, "PONG :test\r\n", <-conn.hook)
assert.Equal(t, &Message{Command: "CMD"}, <-c.Messages)
assert.Equal(t, &Message{Command: Ping, Params: []string{"test"}}, <-c.Messages)
assert.Equal(t, &Message{Command: ReplyWelcome, Params: []string{"foo"}}, <-c.Messages)
assert.Equal(t, &Message{Command: PING, Params: []string{"test"}}, <-c.Messages)
assert.Equal(t, &Message{Command: RPL_WELCOME, Params: []string{"foo"}}, <-c.Messages)
}
func TestRecvTriggersReconnect(t *testing.T) {

View File

@ -1,43 +1,133 @@
package irc
const (
Error = "ERROR"
Join = "JOIN"
Mode = "MODE"
Nick = "NICK"
Notice = "NOTICE"
Part = "PART"
Ping = "PING"
Privmsg = "PRIVMSG"
Quit = "QUIT"
Topic = "TOPIC"
CAP = "CAP"
AUTHENTICATE = "AUTHENTICATE"
PASS = "PASS"
NICK = "NICK"
USER = "USER"
OPER = "OPER"
QUIT = "QUIT"
JOIN = "JOIN"
PART = "PART"
TOPIC = "TOPIC"
NAMES = "NAMES"
LIST = "LIST"
MOTD = "MOTD"
VERSION = "VERSION"
ADMIN = "ADMIN"
CONNECT = "CONNECT"
TIME = "TIME"
STATS = "STATS"
INFO = "INFO"
MODE = "MODE"
PRIVMSG = "PRIVMSG"
NOTICE = "NOTICE"
USERHOST = "USERHOST"
KILL = "KILL"
ERROR = "ERROR"
PING = "PING"
PONG = "PONG"
ReplyWelcome = "001"
ReplyYourHost = "002"
ReplyCreated = "003"
ReplyISupport = "005"
ReplyLUserClient = "251"
ReplyLUserOp = "252"
ReplyLUserUnknown = "253"
ReplyLUserChannels = "254"
ReplyLUserMe = "255"
ReplyAway = "301"
ReplyWhoisUser = "311"
ReplyWhoisServer = "312"
ReplyWhoisOperator = "313"
ReplyWhoisIdle = "317"
ReplyEndOfWhois = "318"
ReplyWhoisChannels = "319"
ReplyList = "322"
ReplyListEnd = "323"
ReplyNoTopic = "331"
ReplyTopic = "332"
ReplyNamReply = "353"
ReplyEndOfNames = "366"
ReplyMotd = "372"
ReplyMotdStart = "375"
ReplyEndOfMotd = "376"
ErrErroneousNickname = "432"
ErrNicknameInUse = "433"
ErrForward = "470"
RPL_WELCOME = "001"
RPL_YOURHOST = "002"
RPL_CREATED = "003"
RPL_MYINFO = "004"
RPL_ISUPPORT = "005"
RPL_BOUNCE = "010"
RPL_UMODEIS = "221"
RPL_LUSERCLIENT = "251"
RPL_LUSEROP = "252"
RPL_LUSERUNKNOWN = "253"
RPL_LUSERCHANNELS = "254"
RPL_LUSERME = "255"
RPL_ADMINME = "256"
RPL_ADMINLOC1 = "257"
RPL_ADMINLOC2 = "258"
RPL_ADMINEMAIL = "259"
RPL_TRYAGAIN = "263"
RPL_LOCALUSERS = "265"
RPL_GLOBALUSERS = "266"
RPL_WHOISCERTFP = "276"
RPL_NONE = "300"
RPL_AWAY = "301"
RPL_USERHOST = "302"
RPL_ISON = "303"
RPL_UNAWAY = "305"
RPL_NOWAWAY = "306"
RPL_WHOISUSER = "311"
RPL_WHOISSERVER = "312"
RPL_WHOISOPERATOR = "313"
RPL_WHOWASUSER = "314"
RPL_WHOISIDLE = "317"
RPL_ENDOFWHOIS = "318"
RPL_WHOISCHANNELS = "319"
RPL_LISTSTART = "321"
RPL_LIST = "322"
RPL_LISTEND = "323"
RPL_CHANNELMODEIS = "324"
RPL_CREATIONTIME = "329"
RPL_NOTOPIC = "331"
RPL_TOPIC = "332"
RPL_TOPICWHOTIME = "333"
RPL_INVITING = "341"
RPL_INVITELIST = "346"
RPL_ENDOFINVITELIST = "347"
RPL_EXCEPTLIST = "348"
RPL_ENDOFEXCEPTLIST = "349"
RPL_VERSION = "351"
RPL_NAMREPLY = "353"
RPL_ENDOFNAMES = "366"
RPL_BANLIST = "367"
RPL_ENDOFBANLIST = "368"
RPL_ENDOFWHOWAS = "369"
RPL_MOTDSTART = "375"
RPL_MOTD = "372"
RPL_ENDOFMOTD = "376"
RPL_YOUREOPER = "381"
RPL_REHASHING = "382"
ERR_UNKNOWNERROR = "400"
ERR_NOSUCHNICK = "401"
ERR_NOSUCHSERVER = "402"
ERR_NOSUCHCHANNEL = "403"
ERR_CANNOTSENDTOCHAN = "404"
ERR_TOOMANYCHANNELS = "405"
ERR_UNKNOWNCOMMAND = "421"
ERR_NOMOTD = "422"
ERR_ERRONEUSNICKNAME = "432"
ERR_NICKNAMEINUSE = "433"
ERR_NICKCOLLISION = "436"
ERR_UNAVAILRESOURCE = "437"
ERR_USERNOTINCHANNEL = "441"
ERR_NOTONCHANNEL = "442"
ERR_USERONCHANNEL = "443"
ERR_NOTREGISTERED = "451"
ERR_NEEDMOREPARAMS = "461"
ERR_ALREADYREGISTERED = "462"
ERR_PASSWDMISMATCH = "464"
ERR_YOUREBANNEDCREEP = "465"
ERR_FORWARD = "470"
ERR_CHANNELISFULL = "471"
ERR_UNKNOWNMODE = "472"
ERR_INVITEONLYCHAN = "473"
ERR_BANNEDFROMCHAN = "474"
ERR_BADCHANNELKEY = "475"
ERR_NOPRIVILEGES = "481"
ERR_CHANOPRIVSNEEDED = "482"
ERR_CANTKILLSERVER = "483"
ERR_NOOPERHOST = "491"
ERR_UMODEUNKNOWNFLAG = "501"
ERR_USERSDONTMATCH = "502"
RPL_STARTTLS = "670"
ERR_STARTTLS = "691"
ERR_NOPRIVS = "723"
RPL_LOGGEDIN = "900"
RPL_LOGGEDOUT = "901"
ERR_NICKLOCKED = "902"
RPL_SASLSUCCESS = "903"
ERR_SASLFAIL = "904"
ERR_SASLTOOLONG = "905"
ERR_SASLABORTED = "906"
ERR_SASLALREADY = "907"
RPL_SASLMECHS = "908"
)

View File

@ -23,7 +23,7 @@ func createNickInUseHandler(i *irc.Client, state *State) func(string) string {
state.sendJSON("error", IRCError{
Server: i.Host,
Message: fmt.Sprintf("Nickname %s is already in use, using %s instead", nick, newNick),
Message: fmt.Sprintf("Nickname %s is unavailable, trying %s instead", nick, newNick),
})
return newNick

View File

@ -16,8 +16,10 @@ import (
)
var excludedErrors = []string{
irc.ErrNicknameInUse,
irc.ErrForward,
irc.ERR_NICKNAMEINUSE,
irc.ERR_NICKCOLLISION,
irc.ERR_UNAVAILRESOURCE,
irc.ERR_FORWARD,
}
type ircHandler struct {
@ -229,7 +231,7 @@ func (i *ircHandler) quit(msg *irc.Message) {
}
func (i *ircHandler) info(msg *irc.Message) {
if msg.Command == irc.ReplyWelcome {
if msg.Command == irc.RPL_WELCOME {
i.state.sendJSON("nick", Nick{
Server: i.client.Host,
New: msg.Params[0],
@ -293,7 +295,7 @@ func (i *ircHandler) topic(msg *irc.Message) {
var channel string
var nick string
if msg.Command == irc.Topic {
if msg.Command == irc.TOPIC {
channel = msg.Params[0]
nick = msg.Nick
} else {
@ -437,39 +439,39 @@ func (i *ircHandler) receiveDCCSend(pack *irc.DCCSend, msg *irc.Message) {
func (i *ircHandler) initHandlers() {
i.handlers = map[string]func(*irc.Message){
irc.Nick: i.nick,
irc.Join: i.join,
irc.Part: i.part,
irc.Mode: i.mode,
irc.Privmsg: i.message,
irc.Notice: i.message,
irc.Quit: i.quit,
irc.Topic: i.topic,
irc.Error: i.error,
irc.ReplyWelcome: i.info,
irc.ReplyYourHost: i.info,
irc.ReplyCreated: i.info,
irc.ReplyISupport: i.features,
irc.ReplyLUserClient: i.info,
irc.ReplyLUserOp: i.info,
irc.ReplyLUserUnknown: i.info,
irc.ReplyLUserChannels: i.info,
irc.ReplyLUserMe: i.info,
irc.ReplyWhoisUser: i.whoisUser,
irc.ReplyWhoisServer: i.whoisServer,
irc.ReplyWhoisChannels: i.whoisChannels,
irc.ReplyEndOfWhois: i.whoisEnd,
irc.ReplyNoTopic: i.noTopic,
irc.ReplyTopic: i.topic,
irc.ReplyNamReply: i.names,
irc.ReplyEndOfNames: i.namesEnd,
irc.ReplyMotdStart: i.motdStart,
irc.ReplyMotd: i.motd,
irc.ReplyEndOfMotd: i.motdEnd,
irc.ReplyList: i.list,
irc.ReplyListEnd: i.listEnd,
irc.ErrErroneousNickname: i.badNick,
irc.ErrForward: i.forward,
irc.NICK: i.nick,
irc.JOIN: i.join,
irc.PART: i.part,
irc.MODE: i.mode,
irc.PRIVMSG: i.message,
irc.NOTICE: i.message,
irc.QUIT: i.quit,
irc.TOPIC: i.topic,
irc.ERROR: i.error,
irc.RPL_WELCOME: i.info,
irc.RPL_YOURHOST: i.info,
irc.RPL_CREATED: i.info,
irc.RPL_ISUPPORT: i.features,
irc.RPL_LUSERCLIENT: i.info,
irc.RPL_LUSEROP: i.info,
irc.RPL_LUSERUNKNOWN: i.info,
irc.RPL_LUSERCHANNELS: i.info,
irc.RPL_LUSERME: i.info,
irc.RPL_WHOISUSER: i.whoisUser,
irc.RPL_WHOISSERVER: i.whoisServer,
irc.RPL_WHOISCHANNELS: i.whoisChannels,
irc.RPL_ENDOFWHOIS: i.whoisEnd,
irc.RPL_NOTOPIC: i.noTopic,
irc.RPL_TOPIC: i.topic,
irc.RPL_NAMREPLY: i.names,
irc.RPL_ENDOFNAMES: i.namesEnd,
irc.RPL_MOTDSTART: i.motdStart,
irc.RPL_MOTD: i.motd,
irc.RPL_ENDOFMOTD: i.motdEnd,
irc.RPL_LIST: i.list,
irc.RPL_LISTEND: i.listEnd,
irc.ERR_ERRONEUSNICKNAME: i.badNick,
irc.ERR_FORWARD: i.forward,
}
}

View File

@ -70,7 +70,7 @@ func checkResponse(t *testing.T, expectedType string, expectedData interface{},
func TestHandleIRCNick(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.Nick,
Command: irc.NICK,
Nick: "old",
Params: []string{"new"},
})
@ -84,7 +84,7 @@ func TestHandleIRCNick(t *testing.T) {
func TestHandleIRCJoin(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.Join,
Command: irc.JOIN,
Nick: "joining",
Params: []string{"#chan"},
})
@ -98,7 +98,7 @@ func TestHandleIRCJoin(t *testing.T) {
func TestHandleIRCPart(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.Part,
Command: irc.PART,
Nick: "parting",
Params: []string{"#chan", "the reason"},
})
@ -111,7 +111,7 @@ func TestHandleIRCPart(t *testing.T) {
}, res)
res = dispatchMessage(&irc.Message{
Command: irc.Part,
Command: irc.PART,
Nick: "parting",
Params: []string{"#chan"},
})
@ -125,7 +125,7 @@ func TestHandleIRCPart(t *testing.T) {
func TestHandleIRCMode(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.Mode,
Command: irc.MODE,
Params: []string{"#chan", "+o-v", "nick"},
})
@ -140,7 +140,7 @@ func TestHandleIRCMode(t *testing.T) {
func TestHandleIRCMessage(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.Privmsg,
Command: irc.PRIVMSG,
Nick: "nick",
Params: []string{"#chan", "the message"},
})
@ -153,7 +153,7 @@ func TestHandleIRCMessage(t *testing.T) {
assert.Equal(t, "the message", msg.Content)
res = dispatchMessage(&irc.Message{
Command: irc.Privmsg,
Command: irc.PRIVMSG,
Nick: "someone",
Params: []string{"nick", "the message"},
})
@ -168,7 +168,7 @@ func TestHandleIRCMessage(t *testing.T) {
func TestHandleIRCQuit(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.Quit,
Command: irc.QUIT,
Nick: "nick",
Params: []string{"the reason"},
})
@ -182,7 +182,7 @@ func TestHandleIRCQuit(t *testing.T) {
func TestHandleIRCWelcome(t *testing.T) {
res := dispatchMessageMulti(&irc.Message{
Command: irc.ReplyWelcome,
Command: irc.RPL_WELCOME,
Nick: "nick",
Params: []string{"nick", "some", "text"},
})
@ -206,18 +206,18 @@ func TestHandleIRCWhois(t *testing.T) {
i := newIRCHandler(c, s)
i.dispatchMessage(&irc.Message{
Command: irc.ReplyWhoisUser,
Command: irc.RPL_WHOISUSER,
Params: []string{"", "nick", "user", "host", "", "realname"},
})
i.dispatchMessage(&irc.Message{
Command: irc.ReplyWhoisServer,
Command: irc.RPL_WHOISSERVER,
Params: []string{"", "", "srv.com"},
})
i.dispatchMessage(&irc.Message{
Command: irc.ReplyWhoisChannels,
Command: irc.RPL_WHOISCHANNELS,
Params: []string{"#chan #chan1"},
})
i.dispatchMessage(&irc.Message{Command: irc.ReplyEndOfWhois})
i.dispatchMessage(&irc.Message{Command: irc.RPL_ENDOFWHOIS})
checkResponse(t, "whois", WhoisReply{
Nick: "nick",
@ -231,7 +231,7 @@ func TestHandleIRCWhois(t *testing.T) {
func TestHandleIRCTopic(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.ReplyTopic,
Command: irc.RPL_TOPIC,
Params: []string{"target", "#chan", "the topic"},
})
@ -242,7 +242,7 @@ func TestHandleIRCTopic(t *testing.T) {
}, res)
res = dispatchMessage(&irc.Message{
Command: irc.Topic,
Command: irc.TOPIC,
Params: []string{"#chan", "the topic"},
Nick: "bob",
})
@ -257,7 +257,7 @@ func TestHandleIRCTopic(t *testing.T) {
func TestHandleIRCNoTopic(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.ReplyNoTopic,
Command: irc.RPL_NOTOPIC,
Params: []string{"target", "#chan", "No topic set."},
})
@ -274,15 +274,15 @@ func TestHandleIRCNames(t *testing.T) {
i := newIRCHandler(c, s)
i.dispatchMessage(&irc.Message{
Command: irc.ReplyNamReply,
Command: irc.RPL_NAMREPLY,
Params: []string{"", "", "#chan", "a b c"},
})
i.dispatchMessage(&irc.Message{
Command: irc.ReplyNamReply,
Command: irc.RPL_NAMREPLY,
Params: []string{"", "", "#chan", "d"},
})
i.dispatchMessage(&irc.Message{
Command: irc.ReplyEndOfNames,
Command: irc.RPL_ENDOFNAMES,
Params: []string{"", "#chan"},
})
@ -300,18 +300,18 @@ func TestHandleIRCMotd(t *testing.T) {
i := newIRCHandler(c, s)
i.dispatchMessage(&irc.Message{
Command: irc.ReplyMotdStart,
Command: irc.RPL_MOTDSTART,
Params: []string{"motd title"},
})
i.dispatchMessage(&irc.Message{
Command: irc.ReplyMotd,
Command: irc.RPL_MOTD,
Params: []string{"line 1"},
})
i.dispatchMessage(&irc.Message{
Command: irc.ReplyMotd,
Command: irc.RPL_MOTD,
Params: []string{"line 2"},
})
i.dispatchMessage(&irc.Message{Command: irc.ReplyEndOfMotd})
i.dispatchMessage(&irc.Message{Command: irc.RPL_ENDOFMOTD})
checkResponse(t, "motd", MOTD{
Server: "host.com",
@ -327,7 +327,7 @@ func TestHandleIRCBadNick(t *testing.T) {
i := newIRCHandler(c, s)
i.dispatchMessage(&irc.Message{
Command: irc.ErrErroneousNickname,
Command: irc.ERR_ERRONEUSNICKNAME,
})
// It should print the error message first