3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-26 05:49:25 +01:00
ergo/irc/commands.go

567 lines
11 KiB
Go
Raw Normal View History

package irc
2012-12-09 21:51:50 +01:00
import (
"errors"
2013-05-11 22:55:01 +02:00
"fmt"
2012-12-09 21:51:50 +01:00
"strconv"
"strings"
)
type editableCommand interface {
Command
2014-02-09 08:51:51 +01:00
SetClient(*Client)
}
type parseCommandFunc func([]string) (editableCommand, error)
var (
2013-05-09 20:05:10 +02:00
NotEnoughArgsError = errors.New("not enough arguments")
ErrParseCommand = errors.New("failed to parse message")
parseCommandFuncs = map[string]parseCommandFunc{
"JOIN": NewJoinCommand,
"MODE": NewModeCommand,
"NICK": NewNickCommand,
2014-02-09 19:07:40 +01:00
"OPER": NewOperCommand,
"PART": NewPartCommand,
"PASS": NewPassCommand,
"PING": NewPingCommand,
"PONG": NewPongCommand,
"PRIVMSG": NewPrivMsgCommand,
"QUIT": NewQuitCommand,
"TOPIC": NewTopicCommand,
"USER": NewUserMsgCommand,
2014-02-09 03:49:52 +01:00
"WHO": NewWhoCommand,
2014-02-09 02:43:59 +01:00
"WHOIS": NewWhoisCommand,
}
)
2013-05-09 20:05:10 +02:00
type BaseCommand struct {
client *Client
}
func (command *BaseCommand) Client() *Client {
2013-05-11 22:55:01 +02:00
return command.client
2013-05-09 20:05:10 +02:00
}
2014-02-09 08:51:51 +01:00
func (command *BaseCommand) SetClient(c *Client) {
command.client = c
2013-05-09 20:05:10 +02:00
}
func (command *BaseCommand) Source() Identifier {
2013-06-03 07:07:50 +02:00
return command.client
}
func (command *BaseCommand) Reply(reply Reply) {
2014-02-09 17:48:11 +01:00
command.client.Reply(reply)
}
func ParseCommand(line string) (editableCommand, error) {
command, args := parseLine(line)
constructor := parseCommandFuncs[command]
if constructor == nil {
return NewUnknownCommand(command, args), nil
}
return constructor(args)
}
func parseArg(line string) (arg string, rest string) {
if line == "" {
return
}
if strings.HasPrefix(line, ":") {
arg = line[1:]
} else {
parts := strings.SplitN(line, " ", 2)
arg = parts[0]
if len(parts) > 1 {
rest = parts[1]
}
}
return
}
func parseLine(line string) (command string, args []string) {
args = make([]string, 0)
for arg, rest := parseArg(line); arg != ""; arg, rest = parseArg(rest) {
args = append(args, arg)
}
command, args = strings.ToUpper(args[0]), args[1:]
return
}
// <command> [args...]
type UnknownCommand struct {
BaseCommand
command string
args []string
}
func (cmd *UnknownCommand) String() string {
return fmt.Sprintf("UNKNOWN(command=%s, args=%s)", cmd.command, cmd.args)
}
func NewUnknownCommand(command string, args []string) *UnknownCommand {
return &UnknownCommand{
2013-06-03 07:07:50 +02:00
command: command,
args: args,
}
}
2012-12-09 21:51:50 +01:00
2012-12-13 08:27:17 +01:00
// PING <server1> [ <server2> ]
type PingCommand struct {
BaseCommand
server string
server2 string
}
func (cmd *PingCommand) String() string {
2013-05-11 22:55:01 +02:00
return fmt.Sprintf("PING(server=%s, server2=%s)", cmd.server, cmd.server2)
}
func NewPingCommand(args []string) (editableCommand, error) {
2012-12-09 21:51:50 +01:00
if len(args) < 1 {
2012-12-10 05:24:53 +01:00
return nil, NotEnoughArgsError
2012-12-09 21:51:50 +01:00
}
msg := &PingCommand{
2013-06-03 07:07:50 +02:00
server: args[0],
2012-12-13 08:27:17 +01:00
}
2012-12-09 21:51:50 +01:00
if len(args) > 1 {
msg.server2 = args[1]
}
return msg, nil
}
2012-12-13 08:27:17 +01:00
// PONG <server> [ <server2> ]
type PongCommand struct {
BaseCommand
server1 string
server2 string
}
func (cmd *PongCommand) String() string {
2013-05-11 22:55:01 +02:00
return fmt.Sprintf("PONG(server1=%s, server2=%s)", cmd.server1, cmd.server2)
}
func NewPongCommand(args []string) (editableCommand, error) {
2012-12-09 21:51:50 +01:00
if len(args) < 1 {
2012-12-10 05:24:53 +01:00
return nil, NotEnoughArgsError
2012-12-09 21:51:50 +01:00
}
message := &PongCommand{
2013-06-03 07:07:50 +02:00
server1: args[0],
}
2012-12-09 21:51:50 +01:00
if len(args) > 1 {
message.server2 = args[1]
}
return message, nil
}
2012-12-10 05:24:53 +01:00
// PASS <password>
type PassCommand struct {
BaseCommand
2012-12-10 05:24:53 +01:00
password string
}
func (cmd *PassCommand) String() string {
return fmt.Sprintf("PASS(password=%s)", cmd.password)
}
func NewPassCommand(args []string) (editableCommand, error) {
2012-12-10 05:24:53 +01:00
if len(args) < 1 {
return nil, NotEnoughArgsError
}
return &PassCommand{
2013-06-03 07:07:50 +02:00
password: args[0],
}, nil
2012-12-10 05:24:53 +01:00
}
2012-12-13 08:27:17 +01:00
// NICK <nickname>
type NickCommand struct {
BaseCommand
nickname string
}
func (m *NickCommand) String() string {
2013-05-11 22:55:01 +02:00
return fmt.Sprintf("NICK(nickname=%s)", m.nickname)
}
func NewNickCommand(args []string) (editableCommand, error) {
2012-12-09 21:51:50 +01:00
if len(args) != 1 {
2012-12-10 05:24:53 +01:00
return nil, NotEnoughArgsError
2012-12-09 21:51:50 +01:00
}
return &NickCommand{
2013-06-03 07:07:50 +02:00
nickname: args[0],
2012-12-13 08:27:17 +01:00
}, nil
2012-12-09 21:51:50 +01:00
}
2012-12-13 08:27:17 +01:00
// USER <user> <mode> <unused> <realname>
type UserMsgCommand struct {
BaseCommand
user string
mode uint8
unused string
realname string
}
func (cmd *UserMsgCommand) String() string {
2013-05-11 22:55:01 +02:00
return fmt.Sprintf("USER(user=%s, mode=%o, unused=%s, realname=%s)",
cmd.user, cmd.mode, cmd.unused, cmd.realname)
}
func NewUserMsgCommand(args []string) (editableCommand, error) {
2012-12-09 21:51:50 +01:00
if len(args) != 4 {
2012-12-10 05:24:53 +01:00
return nil, NotEnoughArgsError
2012-12-09 21:51:50 +01:00
}
msg := &UserMsgCommand{
2013-06-03 07:07:50 +02:00
user: args[0],
unused: args[2],
realname: args[3],
2012-12-09 21:51:50 +01:00
}
mode, err := strconv.ParseUint(args[1], 10, 8)
if err == nil {
msg.mode = uint8(mode)
}
return msg, nil
}
// QUIT [ <Quit Command> ]
type QuitCommand struct {
BaseCommand
message string
}
func (cmd *QuitCommand) String() string {
2013-05-11 22:55:01 +02:00
return fmt.Sprintf("QUIT(message=%s)", cmd.message)
}
func NewQuitCommand(args []string) (editableCommand, error) {
2013-06-03 07:07:50 +02:00
msg := &QuitCommand{}
2012-12-09 21:51:50 +01:00
if len(args) > 0 {
msg.message = args[0]
}
return msg, nil
}
2012-12-10 05:24:53 +01:00
// JOIN ( <channel> *( "," <channel> ) [ <key> *( "," <key> ) ] ) / "0"
type JoinCommand struct {
BaseCommand
channels map[string]string
zero bool
}
func (cmd *JoinCommand) String() string {
return fmt.Sprintf("JOIN(channels=%s, zero=%t)", cmd.channels, cmd.zero)
}
func NewJoinCommand(args []string) (editableCommand, error) {
msg := &JoinCommand{
2013-06-03 07:07:50 +02:00
channels: make(map[string]string),
}
if len(args) == 0 {
return nil, NotEnoughArgsError
2012-12-13 08:27:17 +01:00
}
2012-12-09 21:51:50 +01:00
if args[0] == "0" {
msg.zero = true
return msg, nil
}
channels := strings.Split(args[0], ",")
keys := make([]string, len(channels))
if len(args) > 1 {
for i, key := range strings.Split(args[1], ",") {
keys[i] = key
2012-12-09 21:51:50 +01:00
}
}
for i, channel := range channels {
msg.channels[channel] = keys[i]
}
2012-12-09 21:51:50 +01:00
return msg, nil
}
// PART <channel> *( "," <channel> ) [ <Part Command> ]
type PartCommand struct {
BaseCommand
channels []string
message string
}
2014-02-09 02:53:06 +01:00
func (cmd *PartCommand) Message() string {
if cmd.message == "" {
return cmd.Source().Nick()
}
return cmd.message
}
func (cmd *PartCommand) String() string {
return fmt.Sprintf("PART(channels=%s, message=%s)", cmd.channels, cmd.message)
}
func NewPartCommand(args []string) (editableCommand, error) {
2012-12-09 21:51:50 +01:00
if len(args) < 1 {
2012-12-10 05:24:53 +01:00
return nil, NotEnoughArgsError
2012-12-09 21:51:50 +01:00
}
msg := &PartCommand{
2013-06-03 07:07:50 +02:00
channels: strings.Split(args[0], ","),
2012-12-13 08:27:17 +01:00
}
2012-12-09 21:51:50 +01:00
if len(args) > 1 {
msg.message = args[1]
}
return msg, nil
}
2012-12-13 08:27:17 +01:00
// PRIVMSG <target> <message>
type PrivMsgCommand struct {
BaseCommand
target string
message string
}
func (cmd *PrivMsgCommand) String() string {
2013-05-11 22:55:01 +02:00
return fmt.Sprintf("PRIVMSG(target=%s, message=%s)", cmd.target, cmd.message)
}
func NewPrivMsgCommand(args []string) (editableCommand, error) {
2012-12-09 21:51:50 +01:00
if len(args) < 2 {
2012-12-10 05:24:53 +01:00
return nil, NotEnoughArgsError
2012-12-09 21:51:50 +01:00
}
return &PrivMsgCommand{
2013-06-03 07:07:50 +02:00
target: args[0],
message: args[1],
2012-12-09 21:51:50 +01:00
}, nil
}
func (m *PrivMsgCommand) TargetIsChannel() bool {
2014-02-09 03:14:39 +01:00
return IsChannel(m.target)
}
2012-12-09 23:59:28 +01:00
// TOPIC [newtopic]
type TopicCommand struct {
BaseCommand
channel string
topic string
}
func (cmd *TopicCommand) String() string {
return fmt.Sprintf("TOPIC(channel=%s, topic=%s)", cmd.channel, cmd.topic)
}
func NewTopicCommand(args []string) (editableCommand, error) {
2012-12-09 21:51:50 +01:00
if len(args) < 1 {
2012-12-10 05:24:53 +01:00
return nil, NotEnoughArgsError
2012-12-09 21:51:50 +01:00
}
msg := &TopicCommand{
2013-06-03 07:07:50 +02:00
channel: args[0],
2012-12-13 08:27:17 +01:00
}
2012-12-09 21:51:50 +01:00
if len(args) > 1 {
msg.topic = args[1]
}
return msg, nil
}
type ModeChange struct {
mode UserMode
2014-02-09 07:42:14 +01:00
op ModeOp
}
func (change *ModeChange) String() string {
2014-02-09 07:42:14 +01:00
return fmt.Sprintf("%s%s", change.op, change.mode)
}
type ModeCommand struct {
BaseCommand
nickname string
changes []ModeChange
2012-12-10 05:24:53 +01:00
}
2014-02-09 07:42:14 +01:00
// MODE <nickname> *( ( "+" / "-" ) *( "i" / "w" / "o" / "O" / "r" ) )
func NewUserModeCommand(args []string) (editableCommand, error) {
cmd := &ModeCommand{
nickname: args[0],
changes: make([]ModeChange, 0),
}
2014-02-09 07:42:14 +01:00
for _, modeChange := range args[1:] {
op := ModeOp(modeChange[0])
if (op != Add) && (op != Remove) {
return nil, ErrParseCommand
}
2014-02-09 07:42:14 +01:00
for _, mode := range modeChange[1:] {
cmd.changes = append(cmd.changes, ModeChange{
mode: UserMode(mode),
op: op,
})
}
}
return cmd, nil
}
func (cmd *ModeCommand) String() string {
return fmt.Sprintf("MODE(nickname=%s, changes=%s)", cmd.nickname, cmd.changes)
2013-05-11 22:55:01 +02:00
}
type ChannelModeOp struct {
mode ChannelMode
op ModeOp
arg string
}
func (op *ChannelModeOp) String() string {
return fmt.Sprintf("{%s %s %s}", op.op, op.mode, op.arg)
}
2014-02-09 03:14:39 +01:00
type ChannelModeCommand struct {
BaseCommand
channel string
modeOps []ChannelModeOp
2014-02-09 03:14:39 +01:00
}
// MODE <channel> *( ( "-" / "+" ) *<modes> *<modeparams> )
func NewChannelModeCommand(args []string) (editableCommand, error) {
2014-02-09 03:14:39 +01:00
cmd := &ChannelModeCommand{
channel: args[0],
modeOps: make([]ChannelModeOp, 0),
}
args = args[1:]
for len(args) > 0 {
modeArg := args[0]
2014-02-09 07:42:14 +01:00
op := ModeOp(modeArg[0])
if (op == Add) || (op == Remove) {
modeArg = modeArg[1:]
2014-02-09 07:42:14 +01:00
} else {
op = List
}
2014-02-09 07:42:14 +01:00
skipArgs := 1
2014-02-09 07:42:14 +01:00
for _, mode := range modeArg {
modeOp := ChannelModeOp{
mode: ChannelMode(mode),
op: op,
}
switch modeOp.mode {
2014-02-09 07:42:14 +01:00
case Key, BanMask, ExceptionMask, InviteMask, UserLimit:
if len(args) > skipArgs {
modeOp.arg = args[skipArgs]
skipArgs += 1
}
}
cmd.modeOps = append(cmd.modeOps, modeOp)
}
args = args[skipArgs:]
2012-12-10 05:24:53 +01:00
}
2014-02-09 03:14:39 +01:00
return cmd, nil
}
func (msg *ChannelModeCommand) String() string {
return fmt.Sprintf("MODE(channel=%s, modeOps=%s)", msg.channel, msg.modeOps)
}
func NewModeCommand(args []string) (editableCommand, error) {
2014-02-09 03:14:39 +01:00
if len(args) == 0 {
return nil, NotEnoughArgsError
}
if IsChannel(args[0]) {
return NewChannelModeCommand(args)
} else {
return NewUserModeCommand(args)
}
}
2014-02-09 02:43:59 +01:00
type WhoisCommand struct {
BaseCommand
target string
masks []string
}
2014-02-09 03:49:52 +01:00
// WHOIS [ <target> ] <mask> *( "," <mask> )
func NewWhoisCommand(args []string) (editableCommand, error) {
2014-02-09 02:43:59 +01:00
if len(args) < 1 {
return nil, NotEnoughArgsError
}
var masks string
var target string
if len(args) > 1 {
target = args[0]
masks = args[1]
} else {
masks = args[0]
}
return &WhoisCommand{
target: target,
masks: strings.Split(masks, ","),
}, nil
}
2014-02-09 03:49:52 +01:00
func (msg *WhoisCommand) String() string {
return fmt.Sprintf("WHOIS(target=%s, masks=%s)", msg.target, msg.masks)
}
2014-02-09 03:49:52 +01:00
type WhoCommand struct {
BaseCommand
2014-02-09 07:42:14 +01:00
mask Mask
2014-02-09 03:49:52 +01:00
operatorOnly bool
}
// WHO [ <mask> [ "o" ] ]
func NewWhoCommand(args []string) (editableCommand, error) {
2014-02-09 03:49:52 +01:00
cmd := &WhoCommand{}
if len(args) > 0 {
2014-02-09 07:42:14 +01:00
cmd.mask = Mask(args[0])
2014-02-09 03:49:52 +01:00
}
if (len(args) > 1) && (args[1] == "o") {
cmd.operatorOnly = true
}
return cmd, nil
}
func (msg *WhoCommand) String() string {
return fmt.Sprintf("WHO(mask=%s, operatorOnly=%s)", msg.mask, msg.operatorOnly)
}
2014-02-09 19:07:40 +01:00
type OperCommand struct {
BaseCommand
name string
password string
}
func (msg *OperCommand) String() string {
return fmt.Sprintf("OPER(name=%s, password=%s)", msg.name, msg.password)
}
// OPER <name> <password>
func NewOperCommand(args []string) (editableCommand, error) {
if len(args) < 2 {
return nil, NotEnoughArgsError
}
return &OperCommand{
name: args[0],
password: args[1],
}, nil
}