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

remove unnecessary String()s; rename parse functions

This commit is contained in:
Jeremy Latt 2014-03-28 13:47:49 -07:00
parent 21a86c3216
commit b0bb0faadc
6 changed files with 93 additions and 185 deletions

View File

@ -77,9 +77,7 @@ func (client *Client) run() {
}
func (client *Client) connectionTimeout() {
client.commands <- &QuitCommand{
message: "connection timeout",
}
client.commands <- NewQuitCommand("connection timeout")
}
//

View File

@ -26,37 +26,37 @@ var (
NotEnoughArgsError = errors.New("not enough arguments")
ErrParseCommand = errors.New("failed to parse message")
parseCommandFuncs = map[StringCode]parseCommandFunc{
AWAY: NewAwayCommand,
CAP: NewCapCommand,
DEBUG: NewDebugCommand,
INVITE: NewInviteCommand,
ISON: NewIsOnCommand,
JOIN: NewJoinCommand,
KICK: NewKickCommand,
KILL: NewKillCommand,
LIST: NewListCommand,
MODE: NewModeCommand,
MOTD: NewMOTDCommand,
NAMES: NewNamesCommand,
NICK: NewNickCommand,
NOTICE: NewNoticeCommand,
ONICK: NewOperNickCommand,
OPER: NewOperCommand,
PART: NewPartCommand,
PASS: NewPassCommand,
PING: NewPingCommand,
PONG: NewPongCommand,
PRIVMSG: NewPrivMsgCommand,
PROXY: NewProxyCommand,
QUIT: NewQuitCommand,
THEATER: NewTheaterCommand, // nonstandard
TIME: NewTimeCommand,
TOPIC: NewTopicCommand,
USER: NewUserCommand,
VERSION: NewVersionCommand,
WHO: NewWhoCommand,
WHOIS: NewWhoisCommand,
WHOWAS: NewWhoWasCommand,
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,
}
)
@ -85,7 +85,7 @@ func ParseCommand(line string) (cmd Command, err error) {
code, args := ParseLine(line)
constructor := parseCommandFuncs[code]
if constructor == nil {
cmd = NewUnknownCommand(args)
cmd = ParseUnknownCommand(args)
} else {
cmd, err = constructor(args)
}
@ -135,11 +135,7 @@ type UnknownCommand struct {
args []string
}
func (cmd *UnknownCommand) String() string {
return fmt.Sprintf("UNKNOWN(command=%s, args=%s)", cmd.Code(), cmd.args)
}
func NewUnknownCommand(args []string) *UnknownCommand {
func ParseUnknownCommand(args []string) *UnknownCommand {
return &UnknownCommand{
args: args,
}
@ -153,11 +149,7 @@ type PingCommand struct {
server2 Name
}
func (cmd *PingCommand) String() string {
return fmt.Sprintf("PING(server=%s, server2=%s)", cmd.server, cmd.server2)
}
func NewPingCommand(args []string) (Command, error) {
func ParsePingCommand(args []string) (Command, error) {
if len(args) < 1 {
return nil, NotEnoughArgsError
}
@ -178,11 +170,7 @@ type PongCommand struct {
server2 Name
}
func (cmd *PongCommand) String() string {
return fmt.Sprintf("PONG(server1=%s, server2=%s)", cmd.server1, cmd.server2)
}
func NewPongCommand(args []string) (Command, error) {
func ParsePongCommand(args []string) (Command, error) {
if len(args) < 1 {
return nil, NotEnoughArgsError
}
@ -204,10 +192,6 @@ type PassCommand struct {
err error
}
func (cmd *PassCommand) String() string {
return fmt.Sprintf("PASS(password=%s)", cmd.password)
}
func (cmd *PassCommand) LoadPassword(server *Server) {
cmd.hash = server.password
}
@ -219,7 +203,7 @@ func (cmd *PassCommand) CheckPassword() {
cmd.err = ComparePassword(cmd.hash, cmd.password)
}
func NewPassCommand(args []string) (Command, error) {
func ParsePassCommand(args []string) (Command, error) {
if len(args) < 1 {
return nil, NotEnoughArgsError
}
@ -230,7 +214,7 @@ func NewPassCommand(args []string) (Command, error) {
// NICK <nickname>
func NewNickCommand(args []string) (Command, error) {
func ParseNickCommand(args []string) (Command, error) {
if len(args) != 1 {
return nil, NotEnoughArgsError
}
@ -252,11 +236,6 @@ type RFC1459UserCommand struct {
servername Name
}
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
@ -264,11 +243,6 @@ type RFC2812UserCommand struct {
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)
}
func (cmd *RFC2812UserCommand) Flags() []UserMode {
flags := make([]UserMode, 0)
if (cmd.mode & 4) == 4 {
@ -280,7 +254,7 @@ func (cmd *RFC2812UserCommand) Flags() []UserMode {
return flags
}
func NewUserCommand(args []string) (Command, error) {
func ParseUserCommand(args []string) (Command, error) {
if len(args) != 4 {
return nil, NotEnoughArgsError
}
@ -311,11 +285,15 @@ type QuitCommand struct {
message Text
}
func (cmd *QuitCommand) String() string {
return fmt.Sprintf("QUIT(message=%s)", cmd.message)
func NewQuitCommand(message Text) *QuitCommand {
cmd := &QuitCommand{
message: message,
}
cmd.code = QUIT
return cmd
}
func NewQuitCommand(args []string) (Command, error) {
func ParseQuitCommand(args []string) (Command, error) {
msg := &QuitCommand{}
if len(args) > 0 {
msg.message = NewText(args[0])
@ -331,11 +309,7 @@ type JoinCommand struct {
zero bool
}
func (cmd *JoinCommand) String() string {
return fmt.Sprintf("JOIN(channels=%s, zero=%t)", cmd.channels, cmd.zero)
}
func NewJoinCommand(args []string) (Command, error) {
func ParseJoinCommand(args []string) (Command, error) {
msg := &JoinCommand{
channels: make(map[Name]Text),
}
@ -378,11 +352,7 @@ func (cmd *PartCommand) Message() Text {
return cmd.message
}
func (cmd *PartCommand) String() string {
return fmt.Sprintf("PART(channels=%s, message=%s)", cmd.channels, cmd.message)
}
func NewPartCommand(args []string) (Command, error) {
func ParsePartCommand(args []string) (Command, error) {
if len(args) < 1 {
return nil, NotEnoughArgsError
}
@ -403,11 +373,7 @@ type PrivMsgCommand struct {
message Text
}
func (cmd *PrivMsgCommand) String() string {
return fmt.Sprintf("PRIVMSG(target=%s, message=%s)", cmd.target, cmd.message)
}
func NewPrivMsgCommand(args []string) (Command, error) {
func ParsePrivMsgCommand(args []string) (Command, error) {
if len(args) < 2 {
return nil, NotEnoughArgsError
}
@ -426,11 +392,7 @@ type TopicCommand struct {
topic Text
}
func (cmd *TopicCommand) String() string {
return fmt.Sprintf("TOPIC(channel=%s, topic=%s)", cmd.channel, cmd.topic)
}
func NewTopicCommand(args []string) (Command, error) {
func ParseTopicCommand(args []string) (Command, error) {
if len(args) < 1 {
return nil, NotEnoughArgsError
}
@ -480,7 +442,7 @@ type ModeCommand struct {
}
// MODE <nickname> *( ( "+" / "-" ) *( "i" / "w" / "o" / "O" / "r" ) )
func NewUserModeCommand(nickname Name, args []string) (Command, error) {
func ParseUserModeCommand(nickname Name, args []string) (Command, error) {
cmd := &ModeCommand{
nickname: nickname,
changes: make(ModeChanges, 0),
@ -506,10 +468,6 @@ func NewUserModeCommand(nickname Name, args []string) (Command, error) {
return cmd, nil
}
func (cmd *ModeCommand) String() string {
return fmt.Sprintf("MODE(nickname=%s, changes=%s)", cmd.nickname, cmd.changes)
}
type ChannelModeChange struct {
mode ChannelMode
op ModeOp
@ -557,7 +515,7 @@ type ChannelModeCommand struct {
}
// MODE <channel> *( ( "-" / "+" ) *<modes> *<modeparams> )
func NewChannelModeCommand(channel Name, args []string) (Command, error) {
func ParseChannelModeCommand(channel Name, args []string) (Command, error) {
cmd := &ChannelModeCommand{
channel: channel,
changes: make(ChannelModeChanges, 0),
@ -599,20 +557,16 @@ func NewChannelModeCommand(channel Name, args []string) (Command, error) {
return cmd, nil
}
func (msg *ChannelModeCommand) String() string {
return fmt.Sprintf("MODE(channel=%s, changes=%s)", msg.channel, msg.changes)
}
func NewModeCommand(args []string) (Command, error) {
func ParseModeCommand(args []string) (Command, error) {
if len(args) == 0 {
return nil, NotEnoughArgsError
}
name := NewName(args[0])
if name.IsChannel() {
return NewChannelModeCommand(name, args[1:])
return ParseChannelModeCommand(name, args[1:])
} else {
return NewUserModeCommand(name, args[1:])
return ParseUserModeCommand(name, args[1:])
}
}
@ -623,7 +577,7 @@ type WhoisCommand struct {
}
// WHOIS [ <target> ] <mask> *( "," <mask> )
func NewWhoisCommand(args []string) (Command, error) {
func ParseWhoisCommand(args []string) (Command, error) {
if len(args) < 1 {
return nil, NotEnoughArgsError
}
@ -644,10 +598,6 @@ func NewWhoisCommand(args []string) (Command, error) {
}, nil
}
func (msg *WhoisCommand) String() string {
return fmt.Sprintf("WHOIS(target=%s, masks=%s)", msg.target, msg.masks)
}
type WhoCommand struct {
BaseCommand
mask Name
@ -655,7 +605,7 @@ type WhoCommand struct {
}
// WHO [ <mask> [ "o" ] ]
func NewWhoCommand(args []string) (Command, error) {
func ParseWhoCommand(args []string) (Command, error) {
cmd := &WhoCommand{}
if len(args) > 0 {
@ -669,25 +619,17 @@ func NewWhoCommand(args []string) (Command, error) {
return cmd, nil
}
func (msg *WhoCommand) String() string {
return fmt.Sprintf("WHO(mask=%s, operatorOnly=%t)", msg.mask, msg.operatorOnly)
}
type OperCommand struct {
PassCommand
name Name
}
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]
}
// OPER <name> <password>
func NewOperCommand(args []string) (Command, error) {
func ParseOperCommand(args []string) (Command, error) {
if len(args) < 2 {
return nil, NotEnoughArgsError
}
@ -705,12 +647,7 @@ type CapCommand struct {
capabilities CapabilitySet
}
func (msg *CapCommand) String() string {
return fmt.Sprintf("CAP(subCommand=%s, capabilities=%s)",
msg.subCommand, msg.capabilities)
}
func NewCapCommand(args []string) (Command, error) {
func ParseCapCommand(args []string) (Command, error) {
if len(args) < 1 {
return nil, NotEnoughArgsError
}
@ -740,11 +677,15 @@ type ProxyCommand struct {
hostname Name // looked up in socket thread
}
func (msg *ProxyCommand) String() string {
return fmt.Sprintf("PROXY(sourceIP=%s, sourcePort=%s)", msg.sourceIP, msg.sourcePort)
func NewProxyCommand(hostname Name) *ProxyCommand {
cmd := &ProxyCommand{
hostname: hostname,
}
cmd.code = PROXY
return cmd
}
func NewProxyCommand(args []string) (Command, error) {
func ParseProxyCommand(args []string) (Command, error) {
if len(args) < 5 {
return nil, NotEnoughArgsError
}
@ -764,11 +705,7 @@ type AwayCommand struct {
away bool
}
func (msg *AwayCommand) String() string {
return fmt.Sprintf("AWAY(%s)", msg.text)
}
func NewAwayCommand(args []string) (Command, error) {
func ParseAwayCommand(args []string) (Command, error) {
cmd := &AwayCommand{}
if len(args) > 0 {
@ -784,11 +721,7 @@ type IsOnCommand struct {
nicks []Name
}
func (msg *IsOnCommand) String() string {
return fmt.Sprintf("ISON(nicks=%s)", msg.nicks)
}
func NewIsOnCommand(args []string) (Command, error) {
func ParseIsOnCommand(args []string) (Command, error) {
if len(args) == 0 {
return nil, NotEnoughArgsError
}
@ -803,7 +736,7 @@ type MOTDCommand struct {
target Name
}
func NewMOTDCommand(args []string) (Command, error) {
func ParseMOTDCommand(args []string) (Command, error) {
cmd := &MOTDCommand{}
if len(args) > 0 {
cmd.target = NewName(args[0])
@ -817,11 +750,7 @@ type NoticeCommand struct {
message Text
}
func (cmd *NoticeCommand) String() string {
return fmt.Sprintf("NOTICE(target=%s, message=%s)", cmd.target, cmd.message)
}
func NewNoticeCommand(args []string) (Command, error) {
func ParseNoticeCommand(args []string) (Command, error) {
if len(args) < 2 {
return nil, NotEnoughArgsError
}
@ -844,7 +773,7 @@ func (msg *KickCommand) Comment() Text {
return msg.comment
}
func NewKickCommand(args []string) (Command, error) {
func ParseKickCommand(args []string) (Command, error) {
if len(args) < 2 {
return nil, NotEnoughArgsError
}
@ -875,7 +804,7 @@ type ListCommand struct {
target Name
}
func NewListCommand(args []string) (Command, error) {
func ParseListCommand(args []string) (Command, error) {
cmd := &ListCommand{}
if len(args) > 0 {
cmd.channels = NewNames(strings.Split(args[0], ","))
@ -892,7 +821,7 @@ type NamesCommand struct {
target Name
}
func NewNamesCommand(args []string) (Command, error) {
func ParseNamesCommand(args []string) (Command, error) {
cmd := &NamesCommand{}
if len(args) > 0 {
cmd.channels = NewNames(strings.Split(args[0], ","))
@ -908,7 +837,7 @@ type DebugCommand struct {
subCommand Name
}
func NewDebugCommand(args []string) (Command, error) {
func ParseDebugCommand(args []string) (Command, error) {
if len(args) == 0 {
return nil, NotEnoughArgsError
}
@ -923,7 +852,7 @@ type VersionCommand struct {
target Name
}
func NewVersionCommand(args []string) (Command, error) {
func ParseVersionCommand(args []string) (Command, error) {
cmd := &VersionCommand{}
if len(args) > 0 {
cmd.target = NewName(args[0])
@ -937,7 +866,7 @@ type InviteCommand struct {
channel Name
}
func NewInviteCommand(args []string) (Command, error) {
func ParseInviteCommand(args []string) (Command, error) {
if len(args) < 2 {
return nil, NotEnoughArgsError
}
@ -948,7 +877,7 @@ func NewInviteCommand(args []string) (Command, error) {
}, nil
}
func NewTheaterCommand(args []string) (Command, error) {
func ParseTheaterCommand(args []string) (Command, error) {
if len(args) < 1 {
return nil, NotEnoughArgsError
} else if upperSubCmd := strings.ToUpper(args[0]); upperSubCmd == "IDENTIFY" && len(args) == 3 {
@ -978,7 +907,7 @@ type TimeCommand struct {
target Name
}
func NewTimeCommand(args []string) (Command, error) {
func ParseTimeCommand(args []string) (Command, error) {
cmd := &TimeCommand{}
if len(args) > 0 {
cmd.target = NewName(args[0])
@ -992,7 +921,7 @@ type KillCommand struct {
comment Text
}
func NewKillCommand(args []string) (Command, error) {
func ParseKillCommand(args []string) (Command, error) {
if len(args) < 2 {
return nil, NotEnoughArgsError
}
@ -1009,7 +938,7 @@ type WhoWasCommand struct {
target Name
}
func NewWhoWasCommand(args []string) (Command, error) {
func ParseWhoWasCommand(args []string) (Command, error) {
if len(args) < 1 {
return nil, NotEnoughArgsError
}
@ -1025,7 +954,7 @@ func NewWhoWasCommand(args []string) (Command, error) {
return cmd, nil
}
func NewOperNickCommand(args []string) (Command, error) {
func ParseOperNickCommand(args []string) (Command, error) {
if len(args) < 2 {
return nil, NotEnoughArgsError
}

View File

@ -1,18 +1,10 @@
package irc
import (
"fmt"
)
type NickCommand struct {
BaseCommand
nickname Name
}
func (m *NickCommand) String() string {
return fmt.Sprintf("NICK(nickname=%s)", m.nickname)
}
func (m *NickCommand) HandleRegServer(s *Server) {
client := m.Client()
if !client.authorized {
@ -89,11 +81,20 @@ func (msg *OperNickCommand) HandleServer(server *Server) {
return
}
if msg.nick == client.nick {
return
}
target := server.clients.Get(msg.target)
if target == nil {
client.ErrNoSuchNick(msg.target)
return
}
if server.clients.Get(msg.nick) != nil {
client.ErrNickNameInUse(msg.nick)
return
}
target.ChangeNickname(msg.nick)
}

View File

@ -121,7 +121,7 @@ func (server *Server) loadChannels() {
func (server *Server) processCommand(cmd Command) {
client := cmd.Client()
Log.debug.Printf("%s → %s %s", client, server, cmd)
Log.debug.Printf("%s → %+v", client, cmd)
if !client.registered {
regCmd, ok := cmd.(RegServerCommand)

View File

@ -38,9 +38,7 @@ func (socket *Socket) Close() {
}
func (socket *Socket) readLines(commands chan<- Command) {
commands <- &ProxyCommand{
hostname: AddrLookupHostname(socket.conn.RemoteAddr()),
}
commands <- NewProxyCommand(AddrLookupHostname(socket.conn.RemoteAddr()))
scanner := bufio.NewScanner(socket.conn)
for scanner.Scan() {
@ -62,9 +60,7 @@ func (socket *Socket) readLines(commands chan<- Command) {
Log.debug.Printf("%s error: %s", socket, err)
}
commands <- &QuitCommand{
message: "connection closed",
}
commands <- NewQuitCommand("connection closed")
close(commands)
}

View File

@ -1,9 +1,5 @@
package irc
import (
"fmt"
)
type TheaterClient Name
func (c TheaterClient) Id() Name {
@ -29,10 +25,6 @@ func (m *TheaterIdentifyCommand) LoadPassword(s *Server) {
m.hash = s.theaters[m.channel]
}
func (cmd *TheaterIdentifyCommand) String() string {
return fmt.Sprintf("THEATER_IDENTIFY(channel=%s)", cmd.channel)
}
func (m *TheaterIdentifyCommand) HandleServer(s *Server) {
client := m.Client()
if !m.channel.IsChannel() {
@ -66,10 +58,6 @@ type TheaterPrivMsgCommand struct {
message Text
}
func (cmd *TheaterPrivMsgCommand) String() string {
return fmt.Sprintf("THEATER_PRIVMSG(channel=%s, asNick=%s, message=%s)", cmd.channel, cmd.asNick, cmd.message)
}
func (m *TheaterPrivMsgCommand) HandleServer(s *Server) {
client := m.Client()
@ -102,10 +90,6 @@ type TheaterActionCommand struct {
action CTCPText
}
func (cmd *TheaterActionCommand) String() string {
return fmt.Sprintf("THEATER_ACTION(channel=%s, asNick=%s, action=%s)", cmd.channel, cmd.asNick, cmd.action)
}
func (m *TheaterActionCommand) HandleServer(s *Server) {
client := m.Client()