2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
|
|
|
// Copyright (c) 2014-2015 Edmund Huber
|
2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
|
2016-06-15 13:50:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2012-04-18 03:16:57 +02:00
|
|
|
package irc
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
import (
|
|
|
|
"github.com/goshuirc/irc-go/ircmsg"
|
|
|
|
"github.com/oragono/oragono/irc/modes"
|
|
|
|
)
|
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 {
|
2018-01-30 05:20:12 +01:00
|
|
|
handler func(server *Server, client *Client, msg ircmsg.IrcMessage, rb *ResponseBuffer) 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
|
2016-10-23 03:13:08 +02:00
|
|
|
capabs []string
|
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 {
|
2018-01-22 12:26:01 +01:00
|
|
|
client.Send(nil, server.name, ERR_NOTREGISTERED, client.nick, client.t("You need to register before you can use that command"))
|
2016-06-17 14:17:42 +02:00
|
|
|
return false
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2018-02-12 05:48:58 +01:00
|
|
|
if cmd.oper && !client.HasMode(modes.Operator) {
|
2018-01-22 12:26:01 +01:00
|
|
|
client.Send(nil, server.name, ERR_NOPRIVILEGES, client.nick, client.t("Permission Denied - You're not an IRC operator"))
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
|
|
|
}
|
2017-09-29 04:11:06 +02:00
|
|
|
if len(cmd.capabs) > 0 && !client.HasRoleCapabs(cmd.capabs...) {
|
2018-01-22 12:26:01 +01:00
|
|
|
client.Send(nil, server.name, ERR_NOPRIVILEGES, client.nick, client.t("Permission Denied"))
|
2016-10-23 03:13:08 +02:00
|
|
|
return false
|
|
|
|
}
|
2016-06-17 14:17:42 +02:00
|
|
|
if len(msg.Params) < cmd.minParams {
|
2018-01-22 12:26:01 +01:00
|
|
|
client.Send(nil, server.name, ERR_NEEDMOREPARAMS, client.nick, msg.Command, client.t("Not enough parameters"))
|
2016-06-17 14:17:42 +02:00
|
|
|
return false
|
2014-02-24 18:41:09 +01:00
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
|
2018-03-22 16:04:21 +01:00
|
|
|
if client.registered {
|
|
|
|
client.fakelag.Touch()
|
|
|
|
}
|
|
|
|
|
2018-01-30 05:20:12 +01:00
|
|
|
rb := NewResponseBuffer(client)
|
|
|
|
rb.Label = GetLabel(msg)
|
|
|
|
exiting := cmd.handler(server, client, msg, rb)
|
|
|
|
rb.Send()
|
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
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
if !cmd.leaveClientIdle {
|
|
|
|
client.Touch()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cmd.leaveClientActive {
|
|
|
|
client.Active()
|
|
|
|
}
|
|
|
|
|
2016-06-17 14:17:42 +02:00
|
|
|
return exiting
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commands holds all commands executable by a client connected to us.
|
2018-01-22 19:50:58 +01:00
|
|
|
var Commands map[string]Command
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Commands = map[string]Command{
|
|
|
|
"ACC": {
|
|
|
|
handler: accHandler,
|
2018-02-12 07:09:30 +01:00
|
|
|
minParams: 3,
|
2018-01-22 19:50:58 +01:00
|
|
|
},
|
|
|
|
"AMBIANCE": {
|
|
|
|
handler: sceneHandler,
|
|
|
|
minParams: 2,
|
|
|
|
},
|
|
|
|
"AUTHENTICATE": {
|
|
|
|
handler: authenticateHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"AWAY": {
|
|
|
|
handler: awayHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
|
|
|
"CAP": {
|
|
|
|
handler: capHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"DEBUG": {
|
|
|
|
handler: debugHandler,
|
|
|
|
minParams: 1,
|
2018-02-11 11:30:40 +01:00
|
|
|
oper: true,
|
2018-01-22 19:50:58 +01:00
|
|
|
},
|
|
|
|
"DLINE": {
|
|
|
|
handler: dlineHandler,
|
|
|
|
minParams: 1,
|
|
|
|
oper: true,
|
|
|
|
},
|
|
|
|
"HELP": {
|
|
|
|
handler: helpHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
|
|
|
"HELPOP": {
|
|
|
|
handler: helpHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
|
|
|
"INFO": {
|
|
|
|
handler: infoHandler,
|
|
|
|
},
|
|
|
|
"INVITE": {
|
|
|
|
handler: inviteHandler,
|
|
|
|
minParams: 2,
|
|
|
|
},
|
|
|
|
"ISON": {
|
|
|
|
handler: isonHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"JOIN": {
|
|
|
|
handler: joinHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"KICK": {
|
|
|
|
handler: kickHandler,
|
|
|
|
minParams: 2,
|
|
|
|
},
|
|
|
|
"KILL": {
|
|
|
|
handler: killHandler,
|
|
|
|
minParams: 1,
|
|
|
|
oper: true,
|
|
|
|
capabs: []string{"oper:local_kill"}, //TODO(dan): when we have S2S, this will be checked in the command handler itself
|
|
|
|
},
|
|
|
|
"KLINE": {
|
|
|
|
handler: klineHandler,
|
|
|
|
minParams: 1,
|
|
|
|
oper: true,
|
|
|
|
},
|
|
|
|
"LANGUAGE": {
|
|
|
|
handler: languageHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"LIST": {
|
|
|
|
handler: listHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
|
|
|
"LUSERS": {
|
|
|
|
handler: lusersHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
|
|
|
"MODE": {
|
|
|
|
handler: modeHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"MONITOR": {
|
|
|
|
handler: monitorHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"MOTD": {
|
|
|
|
handler: motdHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
|
|
|
"NAMES": {
|
|
|
|
handler: namesHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
|
|
|
"NICK": {
|
|
|
|
handler: nickHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"NOTICE": {
|
|
|
|
handler: noticeHandler,
|
|
|
|
minParams: 2,
|
|
|
|
},
|
|
|
|
"NPC": {
|
|
|
|
handler: npcHandler,
|
|
|
|
minParams: 3,
|
|
|
|
},
|
|
|
|
"NPCA": {
|
|
|
|
handler: npcaHandler,
|
|
|
|
minParams: 3,
|
|
|
|
},
|
|
|
|
"OPER": {
|
|
|
|
handler: operHandler,
|
|
|
|
minParams: 2,
|
|
|
|
},
|
|
|
|
"PART": {
|
|
|
|
handler: partHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"PASS": {
|
|
|
|
handler: passHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"PING": {
|
|
|
|
handler: pingHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
leaveClientActive: true,
|
|
|
|
},
|
|
|
|
"PONG": {
|
|
|
|
handler: pongHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
leaveClientActive: true,
|
|
|
|
},
|
|
|
|
"PRIVMSG": {
|
|
|
|
handler: privmsgHandler,
|
|
|
|
minParams: 2,
|
|
|
|
},
|
|
|
|
"PROXY": {
|
|
|
|
handler: proxyHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 5,
|
|
|
|
},
|
|
|
|
"RENAME": {
|
|
|
|
handler: renameHandler,
|
|
|
|
minParams: 2,
|
|
|
|
},
|
|
|
|
"RESUME": {
|
|
|
|
handler: resumeHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
2018-05-25 08:46:36 +02:00
|
|
|
"SAJOIN": {
|
|
|
|
handler: sajoinHandler,
|
|
|
|
minParams: 1,
|
|
|
|
capabs: []string{"sajoin"},
|
|
|
|
},
|
2018-01-22 19:50:58 +01:00
|
|
|
"SANICK": {
|
|
|
|
handler: sanickHandler,
|
|
|
|
minParams: 2,
|
|
|
|
oper: true,
|
|
|
|
},
|
|
|
|
"SAMODE": {
|
|
|
|
handler: modeHandler,
|
|
|
|
minParams: 1,
|
|
|
|
capabs: []string{"samode"},
|
|
|
|
},
|
|
|
|
"SCENE": {
|
|
|
|
handler: sceneHandler,
|
|
|
|
minParams: 2,
|
|
|
|
},
|
|
|
|
"TAGMSG": {
|
|
|
|
handler: tagmsgHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"QUIT": {
|
|
|
|
handler: quitHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
|
|
|
"REHASH": {
|
|
|
|
handler: rehashHandler,
|
|
|
|
minParams: 0,
|
|
|
|
oper: true,
|
|
|
|
capabs: []string{"oper:rehash"},
|
|
|
|
},
|
|
|
|
"TIME": {
|
|
|
|
handler: timeHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
|
|
|
"TOPIC": {
|
|
|
|
handler: topicHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"UNDLINE": {
|
|
|
|
handler: unDLineHandler,
|
|
|
|
minParams: 1,
|
|
|
|
oper: true,
|
|
|
|
},
|
|
|
|
"UNKLINE": {
|
|
|
|
handler: unKLineHandler,
|
|
|
|
minParams: 1,
|
|
|
|
oper: true,
|
|
|
|
},
|
|
|
|
"USER": {
|
|
|
|
handler: userHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 4,
|
|
|
|
},
|
|
|
|
"USERHOST": {
|
|
|
|
handler: userhostHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"VERSION": {
|
|
|
|
handler: versionHandler,
|
|
|
|
minParams: 0,
|
|
|
|
},
|
|
|
|
"WEBIRC": {
|
|
|
|
handler: webircHandler,
|
|
|
|
usablePreReg: true,
|
|
|
|
minParams: 4,
|
|
|
|
},
|
|
|
|
"WHO": {
|
|
|
|
handler: whoHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"WHOIS": {
|
|
|
|
handler: whoisHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
"WHOWAS": {
|
|
|
|
handler: whowasHandler,
|
|
|
|
minParams: 1,
|
|
|
|
},
|
|
|
|
}
|
2018-04-19 08:48:19 +02:00
|
|
|
|
|
|
|
initializeServices()
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|