From 77d053ccac23bdd65939a09f61a76ff8f654525e Mon Sep 17 00:00:00 2001 From: Jeremy Latt Date: Sat, 8 Mar 2014 14:24:17 -0800 Subject: [PATCH] get rid of editableCommand --- irc/client.go | 4 +-- irc/commands.go | 72 +++++++++++++++++++++++-------------------------- irc/socket.go | 4 +-- irc/types.go | 2 ++ 4 files changed, 39 insertions(+), 43 deletions(-) diff --git a/irc/client.go b/irc/client.go index 2adb4309..7545840d 100644 --- a/irc/client.go +++ b/irc/client.go @@ -18,7 +18,7 @@ type Client struct { capabilities CapabilitySet capState CapState channels ChannelSet - commands chan editableCommand + commands chan Command ctime time.Time flags map[UserMode]bool hasQuit bool @@ -43,7 +43,7 @@ func NewClient(server *Server, conn net.Conn) *Client { capState: CapNone, capabilities: make(CapabilitySet), channels: make(ChannelSet), - commands: make(chan editableCommand), + commands: make(chan Command), ctime: now, flags: make(map[UserMode]bool), phase: Registration, diff --git a/irc/commands.go b/irc/commands.go index 976d8987..071bc811 100644 --- a/irc/commands.go +++ b/irc/commands.go @@ -9,18 +9,12 @@ import ( "strings" ) -type editableCommand interface { - Command - SetCode(StringCode) - SetClient(*Client) -} - type checkPasswordCommand interface { LoadPassword(*Server) CheckPassword() } -type parseCommandFunc func([]string) (editableCommand, error) +type parseCommandFunc func([]string) (Command, error) var ( NotEnoughArgsError = errors.New("not enough arguments") @@ -79,7 +73,7 @@ func (command *BaseCommand) SetCode(code StringCode) { command.code = code } -func ParseCommand(line string) (cmd editableCommand, err error) { +func ParseCommand(line string) (cmd Command, err error) { code, args := ParseLine(line) constructor := parseCommandFuncs[code] if constructor == nil { @@ -155,7 +149,7 @@ func (cmd *PingCommand) String() string { return fmt.Sprintf("PING(server=%s, server2=%s)", cmd.server, cmd.server2) } -func NewPingCommand(args []string) (editableCommand, error) { +func NewPingCommand(args []string) (Command, error) { if len(args) < 1 { return nil, NotEnoughArgsError } @@ -180,7 +174,7 @@ func (cmd *PongCommand) String() string { return fmt.Sprintf("PONG(server1=%s, server2=%s)", cmd.server1, cmd.server2) } -func NewPongCommand(args []string) (editableCommand, error) { +func NewPongCommand(args []string) (Command, error) { if len(args) < 1 { return nil, NotEnoughArgsError } @@ -217,7 +211,7 @@ func (cmd *PassCommand) CheckPassword() { cmd.err = ComparePassword(cmd.hash, cmd.password) } -func NewPassCommand(args []string) (editableCommand, error) { +func NewPassCommand(args []string) (Command, error) { if len(args) < 1 { return nil, NotEnoughArgsError } @@ -237,7 +231,7 @@ func (m *NickCommand) String() string { return fmt.Sprintf("NICK(nickname=%s)", m.nickname) } -func NewNickCommand(args []string) (editableCommand, error) { +func NewNickCommand(args []string) (Command, error) { if len(args) != 1 { return nil, NotEnoughArgsError } @@ -287,7 +281,7 @@ func (cmd *RFC2812UserCommand) Flags() []UserMode { return flags } -func NewUserCommand(args []string) (editableCommand, error) { +func NewUserCommand(args []string) (Command, error) { if len(args) != 4 { return nil, NotEnoughArgsError } @@ -322,7 +316,7 @@ func (cmd *QuitCommand) String() string { return fmt.Sprintf("QUIT(message=%s)", cmd.message) } -func NewQuitCommand(args []string) (editableCommand, error) { +func NewQuitCommand(args []string) (Command, error) { msg := &QuitCommand{} if len(args) > 0 { msg.message = args[0] @@ -342,7 +336,7 @@ func (cmd *JoinCommand) String() string { return fmt.Sprintf("JOIN(channels=%s, zero=%t)", cmd.channels, cmd.zero) } -func NewJoinCommand(args []string) (editableCommand, error) { +func NewJoinCommand(args []string) (Command, error) { msg := &JoinCommand{ channels: make(map[string]string), } @@ -389,7 +383,7 @@ func (cmd *PartCommand) String() string { return fmt.Sprintf("PART(channels=%s, message=%s)", cmd.channels, cmd.message) } -func NewPartCommand(args []string) (editableCommand, error) { +func NewPartCommand(args []string) (Command, error) { if len(args) < 1 { return nil, NotEnoughArgsError } @@ -414,7 +408,7 @@ func (cmd *PrivMsgCommand) String() string { return fmt.Sprintf("PRIVMSG(target=%s, message=%s)", cmd.target, cmd.message) } -func NewPrivMsgCommand(args []string) (editableCommand, error) { +func NewPrivMsgCommand(args []string) (Command, error) { if len(args) < 2 { return nil, NotEnoughArgsError } @@ -437,7 +431,7 @@ func (cmd *TopicCommand) String() string { return fmt.Sprintf("TOPIC(channel=%s, topic=%s)", cmd.channel, cmd.topic) } -func NewTopicCommand(args []string) (editableCommand, error) { +func NewTopicCommand(args []string) (Command, error) { if len(args) < 1 { return nil, NotEnoughArgsError } @@ -487,7 +481,7 @@ type ModeCommand struct { } // MODE *( ( "+" / "-" ) *( "i" / "w" / "o" / "O" / "r" ) ) -func NewUserModeCommand(args []string) (editableCommand, error) { +func NewUserModeCommand(args []string) (Command, error) { cmd := &ModeCommand{ nickname: args[0], changes: make(ModeChanges, 0), @@ -564,7 +558,7 @@ type ChannelModeCommand struct { } // MODE *( ( "-" / "+" ) * * ) -func NewChannelModeCommand(args []string) (editableCommand, error) { +func NewChannelModeCommand(args []string) (Command, error) { cmd := &ChannelModeCommand{ channel: args[0], changes: make(ChannelModeChanges, 0), @@ -611,7 +605,7 @@ func (msg *ChannelModeCommand) String() string { return fmt.Sprintf("MODE(channel=%s, changes=%s)", msg.channel, msg.changes) } -func NewModeCommand(args []string) (editableCommand, error) { +func NewModeCommand(args []string) (Command, error) { if len(args) == 0 { return nil, NotEnoughArgsError } @@ -630,7 +624,7 @@ type WhoisCommand struct { } // WHOIS [ ] *( "," ) -func NewWhoisCommand(args []string) (editableCommand, error) { +func NewWhoisCommand(args []string) (Command, error) { if len(args) < 1 { return nil, NotEnoughArgsError } @@ -662,7 +656,7 @@ type WhoCommand struct { } // WHO [ [ "o" ] ] -func NewWhoCommand(args []string) (editableCommand, error) { +func NewWhoCommand(args []string) (Command, error) { cmd := &WhoCommand{} if len(args) > 0 { @@ -694,7 +688,7 @@ func (msg *OperCommand) LoadPassword(server *Server) { } // OPER -func NewOperCommand(args []string) (editableCommand, error) { +func NewOperCommand(args []string) (Command, error) { if len(args) < 2 { return nil, NotEnoughArgsError } @@ -717,7 +711,7 @@ func (msg *CapCommand) String() string { msg.subCommand, msg.capabilities) } -func NewCapCommand(args []string) (editableCommand, error) { +func NewCapCommand(args []string) (Command, error) { if len(args) < 1 { return nil, NotEnoughArgsError } @@ -751,7 +745,7 @@ func (msg *ProxyCommand) String() string { return fmt.Sprintf("PROXY(sourceIP=%s, sourcePort=%s)", msg.sourceIP, msg.sourcePort) } -func NewProxyCommand(args []string) (editableCommand, error) { +func NewProxyCommand(args []string) (Command, error) { if len(args) < 5 { return nil, NotEnoughArgsError } @@ -775,7 +769,7 @@ func (msg *AwayCommand) String() string { return fmt.Sprintf("AWAY(%s)", msg.text) } -func NewAwayCommand(args []string) (editableCommand, error) { +func NewAwayCommand(args []string) (Command, error) { cmd := &AwayCommand{} if len(args) > 0 { @@ -795,7 +789,7 @@ func (msg *IsOnCommand) String() string { return fmt.Sprintf("ISON(nicks=%s)", msg.nicks) } -func NewIsOnCommand(args []string) (editableCommand, error) { +func NewIsOnCommand(args []string) (Command, error) { if len(args) == 0 { return nil, NotEnoughArgsError } @@ -810,7 +804,7 @@ type MOTDCommand struct { target string } -func NewMOTDCommand(args []string) (editableCommand, error) { +func NewMOTDCommand(args []string) (Command, error) { cmd := &MOTDCommand{} if len(args) > 0 { cmd.target = args[0] @@ -828,7 +822,7 @@ func (cmd *NoticeCommand) String() string { return fmt.Sprintf("NOTICE(target=%s, message=%s)", cmd.target, cmd.message) } -func NewNoticeCommand(args []string) (editableCommand, error) { +func NewNoticeCommand(args []string) (Command, error) { if len(args) < 2 { return nil, NotEnoughArgsError } @@ -851,7 +845,7 @@ func (msg *KickCommand) Comment() string { return msg.comment } -func NewKickCommand(args []string) (editableCommand, error) { +func NewKickCommand(args []string) (Command, error) { if len(args) < 2 { return nil, NotEnoughArgsError } @@ -882,7 +876,7 @@ type ListCommand struct { target string } -func NewListCommand(args []string) (editableCommand, error) { +func NewListCommand(args []string) (Command, error) { cmd := &ListCommand{} if len(args) > 0 { cmd.channels = strings.Split(args[0], ",") @@ -899,7 +893,7 @@ type NamesCommand struct { target string } -func NewNamesCommand(args []string) (editableCommand, error) { +func NewNamesCommand(args []string) (Command, error) { cmd := &NamesCommand{} if len(args) > 0 { cmd.channels = strings.Split(args[0], ",") @@ -915,7 +909,7 @@ type DebugCommand struct { subCommand string } -func NewDebugCommand(args []string) (editableCommand, error) { +func NewDebugCommand(args []string) (Command, error) { if len(args) == 0 { return nil, NotEnoughArgsError } @@ -930,7 +924,7 @@ type VersionCommand struct { target string } -func NewVersionCommand(args []string) (editableCommand, error) { +func NewVersionCommand(args []string) (Command, error) { cmd := &VersionCommand{} if len(args) > 0 { cmd.target = args[0] @@ -944,7 +938,7 @@ type InviteCommand struct { channel string } -func NewInviteCommand(args []string) (editableCommand, error) { +func NewInviteCommand(args []string) (Command, error) { if len(args) < 2 { return nil, NotEnoughArgsError } @@ -960,7 +954,7 @@ type TimeCommand struct { target string } -func NewTimeCommand(args []string) (editableCommand, error) { +func NewTimeCommand(args []string) (Command, error) { cmd := &TimeCommand{} if len(args) > 0 { cmd.target = args[0] @@ -974,7 +968,7 @@ type KillCommand struct { comment string } -func NewKillCommand(args []string) (editableCommand, error) { +func NewKillCommand(args []string) (Command, error) { if len(args) < 2 { return nil, NotEnoughArgsError } @@ -991,7 +985,7 @@ type WhoWasCommand struct { target string } -func NewWhoWasCommand(args []string) (editableCommand, error) { +func NewWhoWasCommand(args []string) (Command, error) { if len(args) < 1 { return nil, NotEnoughArgsError } diff --git a/irc/socket.go b/irc/socket.go index 48a663e9..1710cbf7 100644 --- a/irc/socket.go +++ b/irc/socket.go @@ -20,7 +20,7 @@ type Socket struct { writer *bufio.Writer } -func NewSocket(conn net.Conn, commands chan<- editableCommand) *Socket { +func NewSocket(conn net.Conn, commands chan<- Command) *Socket { socket := &Socket{ conn: conn, reader: bufio.NewReader(conn), @@ -43,7 +43,7 @@ func (socket *Socket) Close() { } } -func (socket *Socket) readLines(commands chan<- editableCommand) { +func (socket *Socket) readLines(commands chan<- Command) { commands <- &ProxyCommand{ hostname: AddrLookupHostname(socket.conn.RemoteAddr()), } diff --git a/irc/types.go b/irc/types.go index 19014012..75938807 100644 --- a/irc/types.go +++ b/irc/types.go @@ -193,6 +193,8 @@ type Replier interface { type Command interface { Code() StringCode Client() *Client + SetCode(StringCode) + SetClient(*Client) } type ServerCommand interface {