3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00
ergo/irc/commands.go

1010 lines
19 KiB
Go
Raw Normal View History

package irc
2012-12-09 21:51:50 +01:00
import (
"code.google.com/p/go.text/unicode/norm"
2012-12-09 21:51:50 +01:00
"errors"
2013-05-11 22:55:01 +02:00
"fmt"
"regexp"
2012-12-09 21:51:50 +01:00
"strconv"
"strings"
)
type Command interface {
Client() *Client
Code() StringCode
SetClient(*Client)
SetCode(StringCode)
}
type checkPasswordCommand interface {
LoadPassword(*Server)
CheckPassword()
}
2014-03-08 23:24:17 +01:00
type parseCommandFunc func([]string) (Command, error)
var (
2013-05-09 20:05:10 +02:00
NotEnoughArgsError = errors.New("not enough arguments")
ErrParseCommand = errors.New("failed to parse message")
2014-02-17 08:29:11 +01:00
parseCommandFuncs = map[StringCode]parseCommandFunc{
AWAY: NewAwayCommand,
CAP: NewCapCommand,
DEBUG: NewDebugCommand,
2014-02-25 16:28:09 +01:00
INVITE: NewInviteCommand,
2014-02-17 08:29:11 +01:00
ISON: NewIsOnCommand,
JOIN: NewJoinCommand,
KICK: NewKickCommand,
2014-02-25 18:10:16 +01:00
KILL: NewKillCommand,
2014-02-17 08:51:27 +01:00
LIST: NewListCommand,
2014-02-17 08:29:11 +01:00
MODE: NewModeCommand,
MOTD: NewMOTDCommand,
2014-02-18 06:02:03 +01:00
NAMES: NewNamesCommand,
2014-02-17 08:29:11 +01:00
NICK: NewNickCommand,
NOTICE: NewNoticeCommand,
OPER: NewOperCommand,
PART: NewPartCommand,
PASS: NewPassCommand,
PING: NewPingCommand,
PONG: NewPongCommand,
PRIVMSG: NewPrivMsgCommand,
PROXY: NewProxyCommand,
QUIT: NewQuitCommand,
2014-02-25 16:45:40 +01:00
TIME: NewTimeCommand,
2014-02-17 08:29:11 +01:00
TOPIC: NewTopicCommand,
USER: NewUserCommand,
2014-02-25 07:04:11 +01:00
VERSION: NewVersionCommand,
2014-02-17 08:29:11 +01:00
WHO: NewWhoCommand,
WHOIS: NewWhoisCommand,
2014-03-06 21:14:21 +01:00
WHOWAS: NewWhoWasCommand,
}
)
2013-05-09 20:05:10 +02:00
type BaseCommand struct {
client *Client
2014-02-17 08:29:11 +01:00
code StringCode
2013-05-09 20:05:10 +02:00
}
func (command *BaseCommand) Client() *Client {
2013-05-11 22:55:01 +02:00
return command.client
2013-05-09 20:05:10 +02:00
}
func (command *BaseCommand) SetClient(client *Client) {
command.client = client
2013-05-09 20:05:10 +02:00
}
2014-02-17 08:29:11 +01:00
func (command *BaseCommand) Code() StringCode {
return command.code
2014-02-15 03:28:36 +01:00
}
2014-02-17 08:29:11 +01:00
func (command *BaseCommand) SetCode(code StringCode) {
command.code = code
2014-02-15 03:28:36 +01:00
}
2014-03-08 23:24:17 +01:00
func ParseCommand(line string) (cmd Command, err error) {
2014-02-28 06:18:05 +01:00
code, args := ParseLine(line)
2014-02-17 08:29:11 +01:00
constructor := parseCommandFuncs[code]
if constructor == nil {
2014-02-17 08:29:11 +01:00
cmd = NewUnknownCommand(args)
2014-02-15 03:28:36 +01:00
} else {
cmd, err = constructor(args)
}
2014-02-15 03:28:36 +01:00
if cmd != nil {
2014-02-17 08:29:11 +01:00
cmd.SetCode(code)
2014-02-15 03:28:36 +01:00
}
return
}
var (
spacesExpr = regexp.MustCompile(` +`)
)
2014-02-28 06:18:05 +01:00
func splitArg(line string) (arg string, rest string) {
parts := spacesExpr.Split(line, 2)
if len(parts) > 0 {
arg = parts[0]
}
if len(parts) > 1 {
rest = parts[1]
}
return
}
func ParseLine(line string) (command StringCode, args []string) {
args = make([]string, 0)
if strings.HasPrefix(line, ":") {
_, line = splitArg(line)
2014-02-17 07:22:46 +01:00
}
2014-02-28 06:18:05 +01:00
arg, line := splitArg(line)
command = StringCode(strings.ToUpper(arg))
for len(line) > 0 {
if strings.HasPrefix(line, ":") {
args = append(args, norm.NFC.String(line[len(":"):]))
break
}
arg, line = splitArg(line)
args = append(args, norm.NFKC.String(arg))
}
return
}
// <command> [args...]
type UnknownCommand struct {
BaseCommand
2014-02-15 03:28:36 +01:00
args []string
}
func (cmd *UnknownCommand) String() string {
2014-02-17 08:29:11 +01:00
return fmt.Sprintf("UNKNOWN(command=%s, args=%s)", cmd.Code(), cmd.args)
}
2014-02-17 08:29:11 +01:00
func NewUnknownCommand(args []string) *UnknownCommand {
return &UnknownCommand{
2014-02-15 03:28:36 +01:00
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)
}
2014-03-08 23:24:17 +01:00
func NewPingCommand(args []string) (Command, 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)
}
2014-03-08 23:24:17 +01:00
func NewPongCommand(args []string) (Command, 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
hash []byte
password []byte
err error
2012-12-10 05:24:53 +01:00
}
func (cmd *PassCommand) String() string {
return fmt.Sprintf("PASS(password=%s)", cmd.password)
}
func (cmd *PassCommand) LoadPassword(server *Server) {
cmd.hash = server.password
}
func (cmd *PassCommand) CheckPassword() {
if cmd.hash == nil {
return
}
cmd.err = ComparePassword(cmd.hash, cmd.password)
}
2014-03-08 23:24:17 +01:00
func NewPassCommand(args []string) (Command, error) {
2012-12-10 05:24:53 +01:00
if len(args) < 1 {
return nil, NotEnoughArgsError
}
return &PassCommand{
password: []byte(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)
}
2014-03-08 23:24:17 +01:00
func NewNickCommand(args []string) (Command, 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
}
type UserCommand struct {
BaseCommand
username string
realname string
}
// USER <username> <hostname> <servername> <realname>
type RFC1459UserCommand struct {
UserCommand
hostname string
servername string
}
func (cmd *RFC1459UserCommand) String() string {
return fmt.Sprintf("USER(username=%s, hostname=%s, servername=%s, realname=%s)",
cmd.username, cmd.hostname, cmd.servername, cmd.realname)
}
// USER <user> <mode> <unused> <realname>
type RFC2812UserCommand struct {
UserCommand
mode uint8
unused string
}
func (cmd *RFC2812UserCommand) String() string {
return fmt.Sprintf("USER(username=%s, mode=%d, unused=%s, realname=%s)",
cmd.username, cmd.mode, cmd.unused, cmd.realname)
2013-05-11 22:55:01 +02:00
}
func (cmd *RFC2812UserCommand) Flags() []UserMode {
flags := make([]UserMode, 0)
if (cmd.mode & 4) == 4 {
flags = append(flags, WallOps)
}
if (cmd.mode & 8) == 8 {
flags = append(flags, Invisible)
}
return flags
}
2014-03-08 23:24:17 +01:00
func NewUserCommand(args []string) (Command, 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
}
mode, err := strconv.ParseUint(args[1], 10, 8)
if err == nil {
msg := &RFC2812UserCommand{
mode: uint8(mode),
unused: args[2],
}
msg.username = args[0]
msg.realname = args[3]
return msg, nil
}
msg := &RFC1459UserCommand{
hostname: args[1],
servername: args[2],
2012-12-09 21:51:50 +01:00
}
msg.username = args[0]
msg.realname = args[3]
2012-12-09 21:51:50 +01:00
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)
}
2014-03-08 23:24:17 +01:00
func NewQuitCommand(args []string) (Command, 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)
}
2014-03-08 23:24:17 +01:00
func NewJoinCommand(args []string) (Command, 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.Client().Nick()
2014-02-09 02:53:06 +01:00
}
return cmd.message
}
func (cmd *PartCommand) String() string {
return fmt.Sprintf("PART(channels=%s, message=%s)", cmd.channels, cmd.message)
}
2014-03-08 23:24:17 +01:00
func NewPartCommand(args []string) (Command, 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)
}
2014-03-08 23:24:17 +01:00
func NewPrivMsgCommand(args []string) (Command, 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
}
2012-12-09 23:59:28 +01:00
// TOPIC [newtopic]
type TopicCommand struct {
BaseCommand
channel string
setTopic bool
topic string
}
func (cmd *TopicCommand) String() string {
return fmt.Sprintf("TOPIC(channel=%s, topic=%s)", cmd.channel, cmd.topic)
}
2014-03-08 23:24:17 +01:00
func NewTopicCommand(args []string) (Command, 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.setTopic = true
2012-12-09 21:51:50 +01:00
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 ModeChanges []*ModeChange
func (changes ModeChanges) String() string {
if len(changes) == 0 {
return ""
}
op := changes[0].op
str := changes[0].op.String()
for _, change := range changes {
if change.op == op {
str += change.mode.String()
} else {
op = change.op
str += " " + change.op.String()
}
}
return str
}
type ModeCommand struct {
BaseCommand
nickname string
2014-02-18 18:45:10 +01:00
changes ModeChanges
2012-12-10 05:24:53 +01:00
}
2014-02-09 07:42:14 +01:00
// MODE <nickname> *( ( "+" / "-" ) *( "i" / "w" / "o" / "O" / "r" ) )
2014-03-08 23:24:17 +01:00
func NewUserModeCommand(args []string) (Command, error) {
2014-02-09 07:42:14 +01:00
cmd := &ModeCommand{
nickname: args[0],
2014-02-18 18:45:10 +01:00
changes: make(ModeChanges, 0),
2014-02-09 07:42:14 +01:00
}
2014-02-09 07:42:14 +01:00
for _, modeChange := range args[1:] {
if len(modeChange) == 0 {
continue
}
2014-02-09 07:42:14 +01:00
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{
2014-02-09 07:42:14 +01:00
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 ChannelModeChange struct {
mode ChannelMode
op ModeOp
arg string
}
2014-02-17 20:46:40 +01:00
func (change *ChannelModeChange) String() (str string) {
if (change.op == Add) || (change.op == Remove) {
str = change.op.String()
}
str += change.mode.String()
if change.arg != "" {
str += " " + change.arg
}
return
}
type ChannelModeChanges []*ChannelModeChange
2014-02-17 20:46:40 +01:00
func (changes ChannelModeChanges) String() (str string) {
if len(changes) == 0 {
2014-02-17 20:46:40 +01:00
return
}
2014-02-17 20:46:40 +01:00
str = "+"
if changes[0].op == Remove {
str = "-"
}
for _, change := range changes {
str += change.mode.String()
}
for _, change := range changes {
2014-02-17 20:46:40 +01:00
if change.arg == "" {
continue
}
str += " " + change.arg
}
2014-02-17 20:46:40 +01:00
return
}
2014-02-09 03:14:39 +01:00
type ChannelModeCommand struct {
BaseCommand
channel string
changes ChannelModeChanges
2014-02-09 03:14:39 +01:00
}
// MODE <channel> *( ( "-" / "+" ) *<modes> *<modeparams> )
2014-03-08 23:24:17 +01:00
func NewChannelModeCommand(args []string) (Command, error) {
2014-02-09 03:14:39 +01:00
cmd := &ChannelModeCommand{
channel: args[0],
changes: make(ChannelModeChanges, 0),
}
args = args[1:]
for len(args) > 0 {
if len(args[0]) == 0 {
args = args[1:]
continue
}
2014-02-09 07:42:14 +01:00
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 {
change := &ChannelModeChange{
mode: ChannelMode(mode),
op: op,
}
switch change.mode {
2014-02-23 00:01:11 +01:00
case Key, BanMask, ExceptMask, InviteMask, UserLimit,
2014-02-17 20:46:40 +01:00
ChannelOperator, ChannelCreator, Voice:
if len(args) > skipArgs {
change.arg = args[skipArgs]
skipArgs += 1
}
}
cmd.changes = append(cmd.changes, change)
}
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, changes=%s)", msg.channel, msg.changes)
}
2014-03-08 23:24:17 +01:00
func NewModeCommand(args []string) (Command, 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> )
2014-03-08 23:24:17 +01:00
func NewWhoisCommand(args []string) (Command, 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
mask string
2014-02-09 03:49:52 +01:00
operatorOnly bool
}
// WHO [ <mask> [ "o" ] ]
2014-03-08 23:24:17 +01:00
func NewWhoCommand(args []string) (Command, error) {
2014-02-09 03:49:52 +01:00
cmd := &WhoCommand{}
if len(args) > 0 {
cmd.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 {
2014-02-21 04:22:08 +01:00
return fmt.Sprintf("WHO(mask=%s, operatorOnly=%t)", msg.mask, msg.operatorOnly)
}
2014-02-09 19:07:40 +01:00
type OperCommand struct {
PassCommand
name string
2014-02-09 19:07:40 +01:00
}
func (msg *OperCommand) String() string {
return fmt.Sprintf("OPER(name=%s, password=%s)", msg.name, msg.password)
}
func (msg *OperCommand) LoadPassword(server *Server) {
msg.hash = server.operators[msg.name]
}
2014-02-09 19:07:40 +01:00
// OPER <name> <password>
2014-03-08 23:24:17 +01:00
func NewOperCommand(args []string) (Command, error) {
2014-02-09 19:07:40 +01:00
if len(args) < 2 {
return nil, NotEnoughArgsError
}
cmd := &OperCommand{
name: args[0],
}
cmd.password = []byte(args[1])
return cmd, nil
2014-02-09 19:07:40 +01:00
}
2014-02-10 20:14:34 +01:00
type CapCommand struct {
BaseCommand
2014-03-02 21:54:48 +01:00
subCommand CapSubCommand
capabilities CapabilitySet
2014-02-10 20:14:34 +01:00
}
func (msg *CapCommand) String() string {
2014-03-02 21:54:48 +01:00
return fmt.Sprintf("CAP(subCommand=%s, capabilities=%s)",
msg.subCommand, msg.capabilities)
2014-02-10 20:14:34 +01:00
}
2014-03-08 23:24:17 +01:00
func NewCapCommand(args []string) (Command, error) {
if len(args) < 1 {
return nil, NotEnoughArgsError
}
cmd := &CapCommand{
2014-03-02 21:54:48 +01:00
subCommand: CapSubCommand(strings.ToUpper(args[0])),
capabilities: make(CapabilitySet),
}
if len(args) > 1 {
strs := spacesExpr.Split(args[1], -1)
for _, str := range strs {
cmd.capabilities[Capability(str)] = true
}
}
return cmd, nil
2014-02-10 20:14:34 +01:00
}
2014-02-11 03:40:06 +01:00
// HAPROXY support
type ProxyCommand struct {
BaseCommand
net string
sourceIP string
destIP string
sourcePort string
destPort string
hostname string // looked up in socket thread
2014-02-11 03:40:06 +01:00
}
func (msg *ProxyCommand) String() string {
return fmt.Sprintf("PROXY(sourceIP=%s, sourcePort=%s)", msg.sourceIP, msg.sourcePort)
}
2014-03-08 23:24:17 +01:00
func NewProxyCommand(args []string) (Command, error) {
2014-02-11 03:40:06 +01:00
if len(args) < 5 {
return nil, NotEnoughArgsError
}
return &ProxyCommand{
net: args[0],
sourceIP: args[1],
destIP: args[2],
sourcePort: args[3],
destPort: args[4],
hostname: LookupHostname(args[1]),
2014-02-11 03:40:06 +01:00
}, nil
}
2014-02-12 00:44:58 +01:00
type AwayCommand struct {
BaseCommand
text string
away bool
}
func (msg *AwayCommand) String() string {
return fmt.Sprintf("AWAY(%s)", msg.text)
}
2014-03-08 23:24:17 +01:00
func NewAwayCommand(args []string) (Command, error) {
2014-02-12 00:44:58 +01:00
cmd := &AwayCommand{}
if len(args) > 0 {
cmd.text = args[0]
cmd.away = true
}
return cmd, nil
}
2014-02-12 00:58:54 +01:00
type IsOnCommand struct {
BaseCommand
nicks []string
}
func (msg *IsOnCommand) String() string {
return fmt.Sprintf("ISON(nicks=%s)", msg.nicks)
}
2014-03-08 23:24:17 +01:00
func NewIsOnCommand(args []string) (Command, error) {
2014-02-12 00:58:54 +01:00
if len(args) == 0 {
return nil, NotEnoughArgsError
}
return &IsOnCommand{
nicks: args,
}, nil
}
2014-02-12 01:35:32 +01:00
type MOTDCommand struct {
BaseCommand
target string
}
2014-03-08 23:24:17 +01:00
func NewMOTDCommand(args []string) (Command, error) {
2014-02-12 01:35:32 +01:00
cmd := &MOTDCommand{}
if len(args) > 0 {
cmd.target = args[0]
}
return cmd, nil
}
2014-02-12 02:11:59 +01:00
type NoticeCommand struct {
BaseCommand
target string
message string
}
func (cmd *NoticeCommand) String() string {
return fmt.Sprintf("NOTICE(target=%s, message=%s)", cmd.target, cmd.message)
}
2014-03-08 23:24:17 +01:00
func NewNoticeCommand(args []string) (Command, error) {
2014-02-12 02:11:59 +01:00
if len(args) < 2 {
return nil, NotEnoughArgsError
}
return &NoticeCommand{
target: args[0],
message: args[1],
}, nil
}
2014-02-17 08:29:11 +01:00
type KickCommand struct {
BaseCommand
kicks map[string]string
comment string
}
func (msg *KickCommand) Comment() string {
if msg.comment == "" {
return msg.Client().Nick()
2014-02-17 08:29:11 +01:00
}
return msg.comment
}
2014-03-08 23:24:17 +01:00
func NewKickCommand(args []string) (Command, error) {
2014-02-17 08:29:11 +01:00
if len(args) < 2 {
return nil, NotEnoughArgsError
}
channels := strings.Split(args[0], ",")
users := strings.Split(args[1], ",")
if (len(channels) != len(users)) && (len(users) != 1) {
return nil, NotEnoughArgsError
}
cmd := &KickCommand{
kicks: make(map[string]string),
}
for index, channel := range channels {
if len(users) == 1 {
cmd.kicks[channel] = users[0]
} else {
cmd.kicks[channel] = users[index]
}
}
if len(args) > 2 {
cmd.comment = args[2]
}
return cmd, nil
}
2014-02-17 08:51:27 +01:00
type ListCommand struct {
BaseCommand
channels []string
target string
}
2014-03-08 23:24:17 +01:00
func NewListCommand(args []string) (Command, error) {
2014-02-17 08:51:27 +01:00
cmd := &ListCommand{}
if len(args) > 0 {
cmd.channels = strings.Split(args[0], ",")
}
if len(args) > 1 {
cmd.target = args[1]
}
return cmd, nil
}
2014-02-18 06:02:03 +01:00
type NamesCommand struct {
BaseCommand
channels []string
target string
}
2014-03-08 23:24:17 +01:00
func NewNamesCommand(args []string) (Command, error) {
2014-02-18 06:02:03 +01:00
cmd := &NamesCommand{}
if len(args) > 0 {
cmd.channels = strings.Split(args[0], ",")
}
if len(args) > 1 {
cmd.target = args[1]
}
return cmd, nil
}
type DebugCommand struct {
BaseCommand
subCommand string
}
2014-03-08 23:24:17 +01:00
func NewDebugCommand(args []string) (Command, error) {
if len(args) == 0 {
return nil, NotEnoughArgsError
}
return &DebugCommand{
subCommand: strings.ToUpper(args[0]),
}, nil
}
2014-02-25 07:04:11 +01:00
type VersionCommand struct {
BaseCommand
target string
}
2014-03-08 23:24:17 +01:00
func NewVersionCommand(args []string) (Command, error) {
2014-02-25 07:04:11 +01:00
cmd := &VersionCommand{}
if len(args) > 0 {
cmd.target = args[0]
}
return cmd, nil
}
2014-02-25 16:28:09 +01:00
type InviteCommand struct {
BaseCommand
nickname string
channel string
}
2014-03-08 23:24:17 +01:00
func NewInviteCommand(args []string) (Command, error) {
2014-02-25 16:28:09 +01:00
if len(args) < 2 {
return nil, NotEnoughArgsError
}
return &InviteCommand{
nickname: args[0],
channel: args[1],
}, nil
}
2014-02-25 16:45:40 +01:00
type TimeCommand struct {
BaseCommand
target string
}
2014-03-08 23:24:17 +01:00
func NewTimeCommand(args []string) (Command, error) {
2014-02-25 16:45:40 +01:00
cmd := &TimeCommand{}
if len(args) > 0 {
cmd.target = args[0]
}
return cmd, nil
}
2014-02-25 18:10:16 +01:00
type KillCommand struct {
BaseCommand
nickname string
comment string
}
2014-03-08 23:24:17 +01:00
func NewKillCommand(args []string) (Command, error) {
2014-02-25 18:10:16 +01:00
if len(args) < 2 {
return nil, NotEnoughArgsError
}
return &KillCommand{
nickname: args[0],
comment: args[1],
}, nil
}
2014-03-06 21:14:21 +01:00
type WhoWasCommand struct {
BaseCommand
nicknames []string
count int64
target string
}
2014-03-08 23:24:17 +01:00
func NewWhoWasCommand(args []string) (Command, error) {
2014-03-06 21:14:21 +01:00
if len(args) < 1 {
return nil, NotEnoughArgsError
}
cmd := &WhoWasCommand{
nicknames: strings.Split(args[0], ","),
}
if len(args) > 1 {
cmd.count, _ = strconv.ParseInt(args[1], 10, 64)
}
if len(args) > 2 {
cmd.target = args[2]
}
return cmd, nil
}