2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
|
|
|
// Copyright (c) 2014-2015 Edmund Huber
|
|
|
|
// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
|
|
|
|
// released under the MIT license
|
|
|
|
|
2012-04-18 03:16:57 +02:00
|
|
|
package irc
|
|
|
|
|
2016-06-17 14:17:42 +02:00
|
|
|
import "github.com/DanielOaks/girc-go/ircmsg"
|
2012-12-09 21:51:50 +01:00
|
|
|
|
2016-06-19 04:53:52 +02:00
|
|
|
// Command represents a command accepted from a client.
|
2016-06-17 14:17:42 +02:00
|
|
|
type Command struct {
|
2016-06-19 13:59:18 +02:00
|
|
|
handler func(server *Server, client *Client, msg ircmsg.IrcMessage) bool
|
2016-06-20 02:04:53 +02:00
|
|
|
oper bool
|
2016-06-19 13:59:18 +02:00
|
|
|
usablePreReg bool
|
|
|
|
leaveClientActive bool // if true, leaves the client active time alone. reversed because we can't default a struct element to True
|
|
|
|
leaveClientIdle bool
|
|
|
|
minParams int
|
2014-03-13 01:52:25 +01:00
|
|
|
}
|
|
|
|
|
2016-06-19 04:53:52 +02:00
|
|
|
// Run runs this command with the given client/message.
|
2016-06-17 14:17:42 +02:00
|
|
|
func (cmd *Command) Run(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
|
2016-06-19 02:01:30 +02:00
|
|
|
if !client.registered && !cmd.usablePreReg {
|
2016-06-17 14:17:42 +02:00
|
|
|
// command silently ignored
|
|
|
|
return false
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2016-06-20 14:53:45 +02:00
|
|
|
if cmd.oper && !client.flags[Operator] {
|
2016-06-20 02:04:53 +02:00
|
|
|
client.Send(nil, server.nameString, ERR_NOPRIVILEGES, client.nickString, "Permission Denied - You're not an IRC operator")
|
|
|
|
return false
|
|
|
|
}
|
2016-06-17 14:17:42 +02:00
|
|
|
if len(msg.Params) < cmd.minParams {
|
2016-06-19 07:43:16 +02:00
|
|
|
client.Send(nil, server.nameString, ERR_NEEDMOREPARAMS, client.nickString, msg.Command, "Not enough parameters")
|
2016-06-17 14:17:42 +02:00
|
|
|
return false
|
2014-02-24 18:41:09 +01:00
|
|
|
}
|
2016-06-19 13:59:18 +02:00
|
|
|
if !cmd.leaveClientActive {
|
|
|
|
client.Active()
|
|
|
|
}
|
|
|
|
if !cmd.leaveClientIdle {
|
|
|
|
client.Touch()
|
|
|
|
}
|
2016-06-17 14:17:42 +02:00
|
|
|
exiting := cmd.handler(server, client, msg)
|
2012-04-18 03:16:57 +02:00
|
|
|
|
2016-06-17 14:17:42 +02:00
|
|
|
// after each command, see if we can send registration to the client
|
2016-06-19 02:01:30 +02:00
|
|
|
if !client.registered {
|
|
|
|
server.tryRegister(client)
|
2014-03-01 04:21:33 +01:00
|
|
|
}
|
2014-03-17 20:11:35 +01:00
|
|
|
|
2016-06-17 14:17:42 +02:00
|
|
|
return exiting
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commands holds all commands executable by a client connected to us.
|
|
|
|
var Commands = map[string]Command{
|
2016-09-06 08:31:59 +02:00
|
|
|
"AUTHENTICATE": {
|
|
|
|
handler: authenticateHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"AWAY": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: awayHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"CAP": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: capHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"DEBUG": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: debugHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"INVITE": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: inviteHandler,
|
|
|
|
minParams: 2,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"ISON": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: isonHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"JOIN": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: joinHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"KICK": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: kickHandler,
|
|
|
|
minParams: 2,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"KILL": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: killHandler,
|
|
|
|
minParams: 2,
|
2016-06-20 02:04:53 +02:00
|
|
|
oper: true,
|
2016-06-17 14:17:42 +02:00
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"LIST": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: listHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"MODE": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: modeHandler,
|
|
|
|
minParams: 1,
|
2016-06-20 14:53:45 +02:00
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"MOTD": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: motdHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"NAMES": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: namesHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"NICK": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: nickHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"NOTICE": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: noticeHandler,
|
|
|
|
minParams: 2,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"OPER": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: operHandler,
|
|
|
|
minParams: 2,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"PART": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: partHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"PASS": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: passHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"PING": {
|
2016-06-19 13:59:18 +02:00
|
|
|
handler: pingHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
leaveClientActive: true,
|
2016-06-17 14:17:42 +02:00
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"PONG": {
|
2016-06-19 13:59:18 +02:00
|
|
|
handler: pongHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
leaveClientActive: true,
|
2016-06-17 14:17:42 +02:00
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"PRIVMSG": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: privmsgHandler,
|
|
|
|
minParams: 2,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"PROXY": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: proxyHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 5,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"SANICK": {
|
2016-06-20 02:04:53 +02:00
|
|
|
handler: sanickHandler,
|
|
|
|
minParams: 2,
|
|
|
|
oper: true,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"QUIT": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: quitHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
2016-09-04 11:25:33 +02:00
|
|
|
"REG": {
|
|
|
|
handler: regHandler,
|
|
|
|
minParams: 3,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"TIME": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: timeHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"TOPIC": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: topicHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"USER": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: userHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 4,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"VERSION": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: versionHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"WHO": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: whoHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"WHOIS": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: whoisHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
2016-06-30 07:43:27 +02:00
|
|
|
"WHOWAS": {
|
2016-06-17 14:17:42 +02:00
|
|
|
handler: whowasHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|