2012-04-18 03:16:57 +02:00
|
|
|
package irc
|
|
|
|
|
2012-12-09 21:51:50 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
2013-05-11 22:55:01 +02:00
|
|
|
"fmt"
|
2014-02-21 06:09:56 +01:00
|
|
|
"regexp"
|
2012-12-09 21:51:50 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-03-13 01:52:25 +01:00
|
|
|
type Command interface {
|
|
|
|
Client() *Client
|
|
|
|
Code() StringCode
|
|
|
|
SetClient(*Client)
|
|
|
|
SetCode(StringCode)
|
|
|
|
}
|
|
|
|
|
2014-02-24 18:41:09 +01:00
|
|
|
type checkPasswordCommand interface {
|
|
|
|
LoadPassword(*Server)
|
|
|
|
CheckPassword()
|
|
|
|
}
|
|
|
|
|
2014-03-08 23:24:17 +01:00
|
|
|
type parseCommandFunc func([]string) (Command, error)
|
2014-02-09 07:06:10 +01:00
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
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{
|
2014-03-28 21:47:49 +01:00
|
|
|
AWAY: ParseAwayCommand,
|
|
|
|
CAP: ParseCapCommand,
|
|
|
|
DEBUG: ParseDebugCommand,
|
|
|
|
INVITE: ParseInviteCommand,
|
|
|
|
ISON: ParseIsOnCommand,
|
|
|
|
JOIN: ParseJoinCommand,
|
|
|
|
KICK: ParseKickCommand,
|
|
|
|
KILL: ParseKillCommand,
|
|
|
|
LIST: ParseListCommand,
|
|
|
|
MODE: ParseModeCommand,
|
|
|
|
MOTD: ParseMOTDCommand,
|
|
|
|
NAMES: ParseNamesCommand,
|
|
|
|
NICK: ParseNickCommand,
|
|
|
|
NOTICE: ParseNoticeCommand,
|
|
|
|
ONICK: ParseOperNickCommand,
|
|
|
|
OPER: ParseOperCommand,
|
|
|
|
PART: ParsePartCommand,
|
|
|
|
PASS: ParsePassCommand,
|
|
|
|
PING: ParsePingCommand,
|
|
|
|
PONG: ParsePongCommand,
|
|
|
|
PRIVMSG: ParsePrivMsgCommand,
|
|
|
|
PROXY: ParseProxyCommand,
|
|
|
|
QUIT: ParseQuitCommand,
|
|
|
|
THEATER: ParseTheaterCommand, // nonstandard
|
|
|
|
TIME: ParseTimeCommand,
|
|
|
|
TOPIC: ParseTopicCommand,
|
|
|
|
USER: ParseUserCommand,
|
|
|
|
VERSION: ParseVersionCommand,
|
|
|
|
WHO: ParseWhoCommand,
|
|
|
|
WHOIS: ParseWhoisCommand,
|
|
|
|
WHOWAS: ParseWhoWasCommand,
|
2012-12-17 04:13:53 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +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
|
|
|
}
|
|
|
|
|
2014-02-24 02:04:24 +01: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]
|
2012-12-17 04:13:53 +01:00
|
|
|
if constructor == nil {
|
2014-03-28 21:47:49 +01:00
|
|
|
cmd = ParseUnknownCommand(args)
|
2014-02-15 03:28:36 +01:00
|
|
|
} else {
|
|
|
|
cmd, err = constructor(args)
|
2012-12-17 04:13:53 +01:00
|
|
|
}
|
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
|
2012-12-17 04:13:53 +01:00
|
|
|
}
|
|
|
|
|
2014-02-21 06:09:56 +01:00
|
|
|
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)
|
2014-03-09 21:45:36 +01:00
|
|
|
command = StringCode(NewName(strings.ToUpper(arg)))
|
2014-02-28 06:18:05 +01:00
|
|
|
for len(line) > 0 {
|
|
|
|
if strings.HasPrefix(line, ":") {
|
2014-03-09 21:45:36 +01:00
|
|
|
args = append(args, line[len(":"):])
|
2014-02-28 06:18:05 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
arg, line = splitArg(line)
|
2014-03-09 21:45:36 +01:00
|
|
|
args = append(args, arg)
|
2014-02-28 06:18:05 +01:00
|
|
|
}
|
|
|
|
return
|
2012-12-17 04:13:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// <command> [args...]
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
type UnknownCommand struct {
|
2013-05-09 18:12:03 +02:00
|
|
|
BaseCommand
|
2014-02-15 03:28:36 +01:00
|
|
|
args []string
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseUnknownCommand(args []string) *UnknownCommand {
|
2012-12-15 23:34:20 +01:00
|
|
|
return &UnknownCommand{
|
2014-02-15 03:28:36 +01:00
|
|
|
args: args,
|
2012-12-15 23:34:20 +01:00
|
|
|
}
|
|
|
|
}
|
2012-12-09 21:51:50 +01:00
|
|
|
|
2012-12-13 08:27:17 +01:00
|
|
|
// PING <server1> [ <server2> ]
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
type PingCommand struct {
|
2013-05-09 18:12:03 +02:00
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
server Name
|
|
|
|
server2 Name
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParsePingCommand(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
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
msg := &PingCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
server: NewName(args[0]),
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
2012-12-09 21:51:50 +01:00
|
|
|
if len(args) > 1 {
|
2014-03-09 21:45:36 +01:00
|
|
|
msg.server2 = NewName(args[1])
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2012-12-13 08:27:17 +01:00
|
|
|
// PONG <server> [ <server2> ]
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
type PongCommand struct {
|
2013-05-09 18:12:03 +02:00
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
server1 Name
|
|
|
|
server2 Name
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParsePongCommand(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
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
message := &PongCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
server1: NewName(args[0]),
|
2012-12-15 23:34:20 +01:00
|
|
|
}
|
2012-12-09 21:51:50 +01:00
|
|
|
if len(args) > 1 {
|
2014-03-09 21:45:36 +01:00
|
|
|
message.server2 = NewName(args[1])
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2012-12-10 05:24:53 +01:00
|
|
|
// PASS <password>
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
type PassCommand struct {
|
2013-05-09 18:12:03 +02:00
|
|
|
BaseCommand
|
2014-02-24 18:41:09 +01:00
|
|
|
hash []byte
|
|
|
|
password []byte
|
|
|
|
err error
|
2012-12-10 05:24:53 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 18:41:09 +01:00
|
|
|
func (cmd *PassCommand) LoadPassword(server *Server) {
|
|
|
|
cmd.hash = server.password
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd *PassCommand) CheckPassword() {
|
|
|
|
if cmd.hash == nil {
|
|
|
|
return
|
|
|
|
}
|
2014-03-02 00:10:04 +01:00
|
|
|
cmd.err = ComparePassword(cmd.hash, cmd.password)
|
2014-02-24 18:41:09 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParsePassCommand(args []string) (Command, error) {
|
2012-12-10 05:24:53 +01:00
|
|
|
if len(args) < 1 {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
return &PassCommand{
|
2014-02-24 18:41:09 +01:00
|
|
|
password: []byte(args[0]),
|
2012-12-12 07:34:22 +01:00
|
|
|
}, nil
|
2012-12-10 05:24:53 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 08:27:17 +01:00
|
|
|
// NICK <nickname>
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseNickCommand(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
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
return &NickCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
nickname: NewName(args[0]),
|
2012-12-13 08:27:17 +01:00
|
|
|
}, nil
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
|
|
|
|
2014-02-18 07:10:48 +01:00
|
|
|
type UserCommand struct {
|
2013-05-09 18:12:03 +02:00
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
username Name
|
|
|
|
realname Text
|
2012-04-18 03:16:57 +02:00
|
|
|
}
|
|
|
|
|
2014-02-18 07:10:48 +01:00
|
|
|
// USER <username> <hostname> <servername> <realname>
|
|
|
|
type RFC1459UserCommand struct {
|
|
|
|
UserCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
hostname Name
|
|
|
|
servername Name
|
2014-02-18 07:10:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// USER <user> <mode> <unused> <realname>
|
|
|
|
type RFC2812UserCommand struct {
|
|
|
|
UserCommand
|
|
|
|
mode uint8
|
|
|
|
unused string
|
|
|
|
}
|
|
|
|
|
|
|
|
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-28 21:47:49 +01:00
|
|
|
func ParseUserCommand(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 {
|
2014-02-18 07:10:48 +01:00
|
|
|
msg := &RFC2812UserCommand{
|
|
|
|
mode: uint8(mode),
|
|
|
|
unused: args[2],
|
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
msg.username = NewName(args[0])
|
|
|
|
msg.realname = NewText(args[3])
|
2014-02-18 07:10:48 +01:00
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &RFC1459UserCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
hostname: NewName(args[1]),
|
|
|
|
servername: NewName(args[2]),
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
msg.username = NewName(args[0])
|
|
|
|
msg.realname = NewText(args[3])
|
2012-12-09 21:51:50 +01:00
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
// QUIT [ <Quit Command> ]
|
2012-04-18 03:16:57 +02:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
type QuitCommand struct {
|
2013-05-09 18:12:03 +02:00
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
message Text
|
2012-04-18 03:16:57 +02:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func NewQuitCommand(message Text) *QuitCommand {
|
|
|
|
cmd := &QuitCommand{
|
|
|
|
message: message,
|
|
|
|
}
|
|
|
|
cmd.code = QUIT
|
|
|
|
return cmd
|
2013-05-11 22:55:01 +02:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseQuitCommand(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 {
|
2014-03-09 21:45:36 +01:00
|
|
|
msg.message = NewText(args[0])
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2012-12-10 05:24:53 +01:00
|
|
|
// JOIN ( <channel> *( "," <channel> ) [ <key> *( "," <key> ) ] ) / "0"
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
type JoinCommand struct {
|
2013-05-09 18:12:03 +02:00
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
channels map[Name]Text
|
2012-12-09 07:54:58 +01:00
|
|
|
zero bool
|
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseJoinCommand(args []string) (Command, error) {
|
2012-12-15 23:34:20 +01:00
|
|
|
msg := &JoinCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
channels: make(map[Name]Text),
|
2012-12-17 04:13:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
return nil, NotEnoughArgsError
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
2012-12-09 21:51:50 +01:00
|
|
|
|
2012-12-17 04:13:53 +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], ",") {
|
2015-06-07 03:26:28 +02:00
|
|
|
if i >= len(channels) {
|
|
|
|
break
|
|
|
|
}
|
2012-12-17 04:13:53 +01:00
|
|
|
keys[i] = key
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
|
|
|
}
|
2012-12-17 04:13:53 +01:00
|
|
|
for i, channel := range channels {
|
2014-03-09 21:45:36 +01:00
|
|
|
msg.channels[NewName(channel)] = NewText(keys[i])
|
2012-12-17 04:13:53 +01:00
|
|
|
}
|
|
|
|
|
2012-12-09 21:51:50 +01:00
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
// PART <channel> *( "," <channel> ) [ <Part Command> ]
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
type PartCommand struct {
|
2013-05-09 18:12:03 +02:00
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
channels []Name
|
|
|
|
message Text
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (cmd *PartCommand) Message() Text {
|
2014-02-09 02:53:06 +01:00
|
|
|
if cmd.message == "" {
|
2014-03-09 21:45:36 +01:00
|
|
|
return cmd.Client().Nick().Text()
|
2014-02-09 02:53:06 +01:00
|
|
|
}
|
|
|
|
return cmd.message
|
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParsePartCommand(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
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
msg := &PartCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
channels: NewNames(strings.Split(args[0], ",")),
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
2012-12-09 21:51:50 +01:00
|
|
|
if len(args) > 1 {
|
2014-03-09 21:45:36 +01:00
|
|
|
msg.message = NewText(args[1])
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2012-12-13 08:27:17 +01:00
|
|
|
// PRIVMSG <target> <message>
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
type PrivMsgCommand struct {
|
2013-05-09 18:12:03 +02:00
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
target Name
|
|
|
|
message Text
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParsePrivMsgCommand(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
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
return &PrivMsgCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
target: NewName(args[0]),
|
|
|
|
message: NewText(args[1]),
|
2012-12-09 21:51:50 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2012-12-09 23:59:28 +01:00
|
|
|
// TOPIC [newtopic]
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
type TopicCommand struct {
|
2013-05-09 18:12:03 +02:00
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
channel Name
|
2014-02-13 03:14:19 +01:00
|
|
|
setTopic bool
|
2014-03-09 21:45:36 +01:00
|
|
|
topic Text
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseTopicCommand(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
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
msg := &TopicCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
channel: NewName(args[0]),
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
2012-12-09 21:51:50 +01:00
|
|
|
if len(args) > 1 {
|
2014-02-13 03:14:19 +01:00
|
|
|
msg.setTopic = true
|
2014-03-09 21:45:36 +01:00
|
|
|
msg.topic = NewText(args[1])
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2014-02-08 23:20:23 +01:00
|
|
|
type ModeChange struct {
|
2014-02-09 07:06:10 +01:00
|
|
|
mode UserMode
|
2014-02-09 07:42:14 +01:00
|
|
|
op ModeOp
|
2014-02-08 23:20:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (change *ModeChange) String() string {
|
2014-02-09 07:42:14 +01:00
|
|
|
return fmt.Sprintf("%s%s", change.op, change.mode)
|
2014-02-08 23:20:23 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 03:05:06 +01:00
|
|
|
type ModeChanges []*ModeChange
|
2014-02-16 04:49:20 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
type ModeCommand struct {
|
2013-05-09 18:12:03 +02:00
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
nickname Name
|
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-28 21:47:49 +01:00
|
|
|
func ParseUserModeCommand(nickname Name, args []string) (Command, error) {
|
2014-02-09 07:42:14 +01:00
|
|
|
cmd := &ModeCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
nickname: nickname,
|
2014-02-18 18:45:10 +01:00
|
|
|
changes: make(ModeChanges, 0),
|
2014-02-09 07:42:14 +01:00
|
|
|
}
|
2014-02-08 23:20:23 +01:00
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
for _, modeChange := range args {
|
2014-02-26 21:08:01 +01:00
|
|
|
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-08 23:20:23 +01:00
|
|
|
}
|
2014-02-09 07:42:14 +01:00
|
|
|
|
|
|
|
for _, mode := range modeChange[1:] {
|
2014-02-24 03:05:06 +01:00
|
|
|
cmd.changes = append(cmd.changes, &ModeChange{
|
2014-02-09 07:42:14 +01:00
|
|
|
mode: UserMode(mode),
|
|
|
|
op: op,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd, nil
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:49:20 +01:00
|
|
|
type ChannelModeChange struct {
|
2014-02-09 07:06:10 +01:00
|
|
|
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
|
2014-02-09 07:06:10 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 03:05:06 +01:00
|
|
|
type ChannelModeChanges []*ChannelModeChange
|
2014-02-16 04:49:20 +01:00
|
|
|
|
2014-02-17 20:46:40 +01:00
|
|
|
func (changes ChannelModeChanges) String() (str string) {
|
2014-02-16 04:49:20 +01:00
|
|
|
if len(changes) == 0 {
|
2014-02-17 20:46:40 +01:00
|
|
|
return
|
2014-02-16 04:49:20 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 20:46:40 +01:00
|
|
|
str = "+"
|
2014-02-16 04:49:20 +01:00
|
|
|
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
|
|
|
|
}
|
2014-02-16 04:49:20 +01:00
|
|
|
str += " " + change.arg
|
|
|
|
}
|
2014-02-17 20:46:40 +01:00
|
|
|
return
|
2014-02-16 04:49:20 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 03:14:39 +01:00
|
|
|
type ChannelModeCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
channel Name
|
2014-02-16 04:49:20 +01:00
|
|
|
changes ChannelModeChanges
|
2014-02-09 03:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MODE <channel> *( ( "-" / "+" ) *<modes> *<modeparams> )
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseChannelModeCommand(channel Name, args []string) (Command, error) {
|
2014-02-09 03:14:39 +01:00
|
|
|
cmd := &ChannelModeCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
channel: channel,
|
2014-02-16 04:49:20 +01:00
|
|
|
changes: make(ChannelModeChanges, 0),
|
2014-02-09 07:06:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for len(args) > 0 {
|
2014-02-26 21:08:01 +01:00
|
|
|
if len(args[0]) == 0 {
|
|
|
|
args = args[1:]
|
|
|
|
continue
|
|
|
|
}
|
2014-02-09 07:42:14 +01:00
|
|
|
|
2014-02-26 21:08:01 +01:00
|
|
|
modeArg := args[0]
|
2014-02-09 07:42:14 +01:00
|
|
|
op := ModeOp(modeArg[0])
|
|
|
|
if (op == Add) || (op == Remove) {
|
2014-02-09 07:06:10 +01:00
|
|
|
modeArg = modeArg[1:]
|
2014-02-09 07:42:14 +01:00
|
|
|
} else {
|
|
|
|
op = List
|
2014-02-09 07:06:10 +01:00
|
|
|
}
|
2014-02-09 07:42:14 +01:00
|
|
|
|
2014-02-09 07:06:10 +01:00
|
|
|
skipArgs := 1
|
2014-02-09 07:42:14 +01:00
|
|
|
for _, mode := range modeArg {
|
2014-02-24 03:05:06 +01:00
|
|
|
change := &ChannelModeChange{
|
2014-02-09 07:06:10 +01:00
|
|
|
mode: ChannelMode(mode),
|
|
|
|
op: op,
|
|
|
|
}
|
2014-02-16 04:49:20 +01:00
|
|
|
switch change.mode {
|
2016-04-14 01:35:36 +02:00
|
|
|
// TODO(dan): separate this into the type A/B/C/D args and use those lists here
|
2014-02-23 00:01:11 +01:00
|
|
|
case Key, BanMask, ExceptMask, InviteMask, UserLimit,
|
2016-04-14 01:35:36 +02:00
|
|
|
ChannelOperator, ChannelFounder, ChannelAdmin, Halfop, Voice:
|
2014-02-09 07:06:10 +01:00
|
|
|
if len(args) > skipArgs {
|
2014-02-16 04:49:20 +01:00
|
|
|
change.arg = args[skipArgs]
|
2014-02-09 07:06:10 +01:00
|
|
|
skipArgs += 1
|
|
|
|
}
|
|
|
|
}
|
2014-02-16 04:49:20 +01:00
|
|
|
cmd.changes = append(cmd.changes, change)
|
2014-02-09 07:06:10 +01:00
|
|
|
}
|
|
|
|
args = args[skipArgs:]
|
2012-12-10 05:24:53 +01:00
|
|
|
}
|
2014-02-09 07:06:10 +01:00
|
|
|
|
2014-02-09 03:14:39 +01:00
|
|
|
return cmd, nil
|
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseModeCommand(args []string) (Command, error) {
|
2014-02-09 03:14:39 +01:00
|
|
|
if len(args) == 0 {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
name := NewName(args[0])
|
|
|
|
if name.IsChannel() {
|
2014-03-28 21:47:49 +01:00
|
|
|
return ParseChannelModeCommand(name, args[1:])
|
2014-02-09 03:14:39 +01:00
|
|
|
} else {
|
2014-03-28 21:47:49 +01:00
|
|
|
return ParseUserModeCommand(name, args[1:])
|
2014-02-09 03:14:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:43:59 +01:00
|
|
|
type WhoisCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
target Name
|
|
|
|
masks []Name
|
2014-02-09 02:43:59 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 03:49:52 +01:00
|
|
|
// WHOIS [ <target> ] <mask> *( "," <mask> )
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseWhoisCommand(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{
|
2014-03-09 21:45:36 +01:00
|
|
|
target: NewName(target),
|
|
|
|
masks: NewNames(strings.Split(masks, ",")),
|
2014-02-09 02:43:59 +01:00
|
|
|
}, nil
|
|
|
|
}
|
2014-02-09 03:49:52 +01:00
|
|
|
|
|
|
|
type WhoCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
mask Name
|
2014-02-09 03:49:52 +01:00
|
|
|
operatorOnly bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// WHO [ <mask> [ "o" ] ]
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseWhoCommand(args []string) (Command, error) {
|
2014-02-09 03:49:52 +01:00
|
|
|
cmd := &WhoCommand{}
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
2014-03-09 21:45:36 +01:00
|
|
|
cmd.mask = NewName(args[0])
|
2014-02-09 03:49:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len(args) > 1) && (args[1] == "o") {
|
|
|
|
cmd.operatorOnly = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd, nil
|
|
|
|
}
|
2014-02-09 07:06:10 +01:00
|
|
|
|
2014-02-09 19:07:40 +01:00
|
|
|
type OperCommand struct {
|
2014-02-24 18:41:09 +01:00
|
|
|
PassCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
name Name
|
2014-02-09 19:07:40 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 18:41:09 +01:00
|
|
|
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-28 21:47:49 +01:00
|
|
|
func ParseOperCommand(args []string) (Command, error) {
|
2014-02-09 19:07:40 +01:00
|
|
|
if len(args) < 2 {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
}
|
|
|
|
|
2014-02-24 18:41:09 +01:00
|
|
|
cmd := &OperCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
name: NewName(args[0]),
|
2014-02-24 18:41:09 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseCapCommand(args []string) (Command, error) {
|
2014-03-01 04:21:33 +01:00
|
|
|
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
|
|
|
|
}
|
2014-03-01 04:21:33 +01:00
|
|
|
}
|
|
|
|
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
|
2014-03-09 21:45:36 +01:00
|
|
|
net Name
|
|
|
|
sourceIP Name
|
|
|
|
destIP Name
|
|
|
|
sourcePort Name
|
|
|
|
destPort Name
|
|
|
|
hostname Name // looked up in socket thread
|
2014-02-11 03:40:06 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func NewProxyCommand(hostname Name) *ProxyCommand {
|
|
|
|
cmd := &ProxyCommand{
|
|
|
|
hostname: hostname,
|
|
|
|
}
|
|
|
|
cmd.code = PROXY
|
|
|
|
return cmd
|
2014-02-11 03:40:06 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseProxyCommand(args []string) (Command, error) {
|
2014-02-11 03:40:06 +01:00
|
|
|
if len(args) < 5 {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
}
|
|
|
|
return &ProxyCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
net: NewName(args[0]),
|
|
|
|
sourceIP: NewName(args[1]),
|
|
|
|
destIP: NewName(args[2]),
|
|
|
|
sourcePort: NewName(args[3]),
|
|
|
|
destPort: NewName(args[4]),
|
|
|
|
hostname: LookupHostname(NewName(args[1])),
|
2014-02-11 03:40:06 +01:00
|
|
|
}, nil
|
|
|
|
}
|
2014-02-12 00:44:58 +01:00
|
|
|
|
|
|
|
type AwayCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
text Text
|
2014-02-12 00:44:58 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseAwayCommand(args []string) (Command, error) {
|
2014-02-12 00:44:58 +01:00
|
|
|
cmd := &AwayCommand{}
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
2014-03-09 21:45:36 +01:00
|
|
|
cmd.text = NewText(args[0])
|
2014-02-12 00:44:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return cmd, nil
|
|
|
|
}
|
2014-02-12 00:58:54 +01:00
|
|
|
|
|
|
|
type IsOnCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
nicks []Name
|
2014-02-12 00:58:54 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseIsOnCommand(args []string) (Command, error) {
|
2014-02-12 00:58:54 +01:00
|
|
|
if len(args) == 0 {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
}
|
|
|
|
|
|
|
|
return &IsOnCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
nicks: NewNames(args),
|
2014-02-12 00:58:54 +01:00
|
|
|
}, nil
|
|
|
|
}
|
2014-02-12 01:35:32 +01:00
|
|
|
|
|
|
|
type MOTDCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
target Name
|
2014-02-12 01:35:32 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseMOTDCommand(args []string) (Command, error) {
|
2014-02-12 01:35:32 +01:00
|
|
|
cmd := &MOTDCommand{}
|
|
|
|
if len(args) > 0 {
|
2014-03-09 21:45:36 +01:00
|
|
|
cmd.target = NewName(args[0])
|
2014-02-12 01:35:32 +01:00
|
|
|
}
|
|
|
|
return cmd, nil
|
|
|
|
}
|
2014-02-12 02:11:59 +01:00
|
|
|
|
|
|
|
type NoticeCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
target Name
|
|
|
|
message Text
|
2014-02-12 02:11:59 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseNoticeCommand(args []string) (Command, error) {
|
2014-02-12 02:11:59 +01:00
|
|
|
if len(args) < 2 {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
}
|
|
|
|
return &NoticeCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
target: NewName(args[0]),
|
|
|
|
message: NewText(args[1]),
|
2014-02-12 02:11:59 +01:00
|
|
|
}, nil
|
|
|
|
}
|
2014-02-17 08:29:11 +01:00
|
|
|
|
|
|
|
type KickCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
kicks map[Name]Name
|
|
|
|
comment Text
|
2014-02-17 08:29:11 +01:00
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (msg *KickCommand) Comment() Text {
|
2014-02-17 08:29:11 +01:00
|
|
|
if msg.comment == "" {
|
2014-03-09 21:45:36 +01:00
|
|
|
return msg.Client().Nick().Text()
|
2014-02-17 08:29:11 +01:00
|
|
|
}
|
|
|
|
return msg.comment
|
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseKickCommand(args []string) (Command, error) {
|
2014-02-17 08:29:11 +01:00
|
|
|
if len(args) < 2 {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
channels := NewNames(strings.Split(args[0], ","))
|
|
|
|
users := NewNames(strings.Split(args[1], ","))
|
2014-02-17 08:29:11 +01:00
|
|
|
if (len(channels) != len(users)) && (len(users) != 1) {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
}
|
|
|
|
cmd := &KickCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
kicks: make(map[Name]Name),
|
2014-02-17 08:29:11 +01:00
|
|
|
}
|
|
|
|
for index, channel := range channels {
|
|
|
|
if len(users) == 1 {
|
|
|
|
cmd.kicks[channel] = users[0]
|
|
|
|
} else {
|
|
|
|
cmd.kicks[channel] = users[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(args) > 2 {
|
2014-03-09 21:45:36 +01:00
|
|
|
cmd.comment = NewText(args[2])
|
2014-02-17 08:29:11 +01:00
|
|
|
}
|
|
|
|
return cmd, nil
|
|
|
|
}
|
2014-02-17 08:51:27 +01:00
|
|
|
|
|
|
|
type ListCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
channels []Name
|
|
|
|
target Name
|
2014-02-17 08:51:27 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseListCommand(args []string) (Command, error) {
|
2014-02-17 08:51:27 +01:00
|
|
|
cmd := &ListCommand{}
|
|
|
|
if len(args) > 0 {
|
2014-03-09 21:45:36 +01:00
|
|
|
cmd.channels = NewNames(strings.Split(args[0], ","))
|
2014-02-17 08:51:27 +01:00
|
|
|
}
|
|
|
|
if len(args) > 1 {
|
2014-03-09 21:45:36 +01:00
|
|
|
cmd.target = NewName(args[1])
|
2014-02-17 08:51:27 +01:00
|
|
|
}
|
|
|
|
return cmd, nil
|
|
|
|
}
|
2014-02-18 06:02:03 +01:00
|
|
|
|
|
|
|
type NamesCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
channels []Name
|
|
|
|
target Name
|
2014-02-18 06:02:03 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseNamesCommand(args []string) (Command, error) {
|
2014-02-18 06:02:03 +01:00
|
|
|
cmd := &NamesCommand{}
|
|
|
|
if len(args) > 0 {
|
2014-03-09 21:45:36 +01:00
|
|
|
cmd.channels = NewNames(strings.Split(args[0], ","))
|
2014-02-18 06:02:03 +01:00
|
|
|
}
|
|
|
|
if len(args) > 1 {
|
2014-03-09 21:45:36 +01:00
|
|
|
cmd.target = NewName(args[1])
|
2014-02-18 06:02:03 +01:00
|
|
|
}
|
|
|
|
return cmd, nil
|
|
|
|
}
|
2014-02-23 19:04:31 +01:00
|
|
|
|
|
|
|
type DebugCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
subCommand Name
|
2014-02-23 19:04:31 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseDebugCommand(args []string) (Command, error) {
|
2014-02-23 19:04:31 +01:00
|
|
|
if len(args) == 0 {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DebugCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
subCommand: NewName(strings.ToUpper(args[0])),
|
2014-02-23 19:04:31 +01:00
|
|
|
}, nil
|
|
|
|
}
|
2014-02-25 07:04:11 +01:00
|
|
|
|
|
|
|
type VersionCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
target Name
|
2014-02-25 07:04:11 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseVersionCommand(args []string) (Command, error) {
|
2014-02-25 07:04:11 +01:00
|
|
|
cmd := &VersionCommand{}
|
|
|
|
if len(args) > 0 {
|
2014-03-09 21:45:36 +01:00
|
|
|
cmd.target = NewName(args[0])
|
2014-02-25 07:04:11 +01:00
|
|
|
}
|
|
|
|
return cmd, nil
|
|
|
|
}
|
2014-02-25 16:28:09 +01:00
|
|
|
|
|
|
|
type InviteCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
nickname Name
|
|
|
|
channel Name
|
2014-02-25 16:28:09 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseInviteCommand(args []string) (Command, error) {
|
2014-02-25 16:28:09 +01:00
|
|
|
if len(args) < 2 {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
}
|
|
|
|
|
|
|
|
return &InviteCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
nickname: NewName(args[0]),
|
|
|
|
channel: NewName(args[1]),
|
2014-02-25 16:28:09 +01:00
|
|
|
}, nil
|
|
|
|
}
|
2014-02-25 16:45:40 +01:00
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseTheaterCommand(args []string) (Command, error) {
|
2014-03-13 09:55:46 +01:00
|
|
|
if len(args) < 1 {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
} else if upperSubCmd := strings.ToUpper(args[0]); upperSubCmd == "IDENTIFY" && len(args) == 3 {
|
|
|
|
return &TheaterIdentifyCommand{
|
|
|
|
channel: NewName(args[1]),
|
|
|
|
PassCommand: PassCommand{password: []byte(args[2])},
|
|
|
|
}, nil
|
|
|
|
} else if upperSubCmd == "PRIVMSG" && len(args) == 4 {
|
|
|
|
return &TheaterPrivMsgCommand{
|
|
|
|
channel: NewName(args[1]),
|
|
|
|
asNick: NewName(args[2]),
|
|
|
|
message: NewText(args[3]),
|
|
|
|
}, nil
|
|
|
|
} else if upperSubCmd == "ACTION" && len(args) == 4 {
|
|
|
|
return &TheaterActionCommand{
|
|
|
|
channel: NewName(args[1]),
|
|
|
|
asNick: NewName(args[2]),
|
2014-03-23 06:47:21 +01:00
|
|
|
action: NewCTCPText(args[3]),
|
2014-03-13 09:55:46 +01:00
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
return nil, ErrParseCommand
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-25 16:45:40 +01:00
|
|
|
type TimeCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
target Name
|
2014-02-25 16:45:40 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseTimeCommand(args []string) (Command, error) {
|
2014-02-25 16:45:40 +01:00
|
|
|
cmd := &TimeCommand{}
|
|
|
|
if len(args) > 0 {
|
2014-03-09 21:45:36 +01:00
|
|
|
cmd.target = NewName(args[0])
|
2014-02-25 16:45:40 +01:00
|
|
|
}
|
|
|
|
return cmd, nil
|
|
|
|
}
|
2014-02-25 18:10:16 +01:00
|
|
|
|
|
|
|
type KillCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
nickname Name
|
|
|
|
comment Text
|
2014-02-25 18:10:16 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseKillCommand(args []string) (Command, error) {
|
2014-02-25 18:10:16 +01:00
|
|
|
if len(args) < 2 {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
}
|
|
|
|
return &KillCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
nickname: NewName(args[0]),
|
|
|
|
comment: NewText(args[1]),
|
2014-02-25 18:10:16 +01:00
|
|
|
}, nil
|
|
|
|
}
|
2014-03-06 21:14:21 +01:00
|
|
|
|
|
|
|
type WhoWasCommand struct {
|
|
|
|
BaseCommand
|
2014-03-09 21:45:36 +01:00
|
|
|
nicknames []Name
|
2014-03-06 21:14:21 +01:00
|
|
|
count int64
|
2014-03-09 21:45:36 +01:00
|
|
|
target Name
|
2014-03-06 21:14:21 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseWhoWasCommand(args []string) (Command, error) {
|
2014-03-06 21:14:21 +01:00
|
|
|
if len(args) < 1 {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
}
|
|
|
|
cmd := &WhoWasCommand{
|
2014-03-09 21:45:36 +01:00
|
|
|
nicknames: NewNames(strings.Split(args[0], ",")),
|
2014-03-06 21:14:21 +01:00
|
|
|
}
|
|
|
|
if len(args) > 1 {
|
|
|
|
cmd.count, _ = strconv.ParseInt(args[1], 10, 64)
|
|
|
|
}
|
|
|
|
if len(args) > 2 {
|
2014-03-09 21:45:36 +01:00
|
|
|
cmd.target = NewName(args[2])
|
2014-03-06 21:14:21 +01:00
|
|
|
}
|
|
|
|
return cmd, nil
|
|
|
|
}
|
2014-03-17 20:11:35 +01:00
|
|
|
|
2014-03-28 21:47:49 +01:00
|
|
|
func ParseOperNickCommand(args []string) (Command, error) {
|
2014-03-17 20:11:35 +01:00
|
|
|
if len(args) < 2 {
|
|
|
|
return nil, NotEnoughArgsError
|
|
|
|
}
|
|
|
|
|
|
|
|
return &OperNickCommand{
|
|
|
|
target: NewName(args[0]),
|
|
|
|
nick: NewName(args[1]),
|
|
|
|
}, nil
|
|
|
|
}
|