From 9b32d7d6b84c67af40c909940ef5c738b1c9be46 Mon Sep 17 00:00:00 2001 From: Daniel Oaks Date: Thu, 14 Apr 2016 18:41:58 +1000 Subject: [PATCH] mode: Parse more advanced mode changes --- irc/commands.go | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/irc/commands.go b/irc/commands.go index 7ad41283..dff59a09 100644 --- a/irc/commands.go +++ b/irc/commands.go @@ -428,12 +428,11 @@ func (changes ModeChanges) String() string { op := changes[0].op str := changes[0].op.String() for _, change := range changes { - if change.op == op { - str += change.mode.String() - } else { + if change.op != op { op = change.op - str += " " + change.op.String() + str += change.op.String() } + str += change.mode.String() } return str } @@ -444,7 +443,7 @@ type ModeCommand struct { changes ModeChanges } -// MODE *( ( "+" / "-" ) *( "i" / "w" / "o" / "O" / "r" ) ) +// MODE ( "+" / "-" )? *( "+" / "-" / ) func ParseUserModeCommand(nickname Name, args []string) (Command, error) { cmd := &ModeCommand{ nickname: nickname, @@ -461,6 +460,10 @@ func ParseUserModeCommand(nickname Name, args []string) (Command, error) { } for _, mode := range modeChange[1:] { + if mode == '-' || mode == '+' { + op = ModeOp(mode) + continue + } cmd.changes = append(cmd.changes, &ModeChange{ mode: UserMode(mode), op: op, @@ -490,25 +493,29 @@ func (change *ChannelModeChange) String() (str string) { type ChannelModeChanges []*ChannelModeChange -func (changes ChannelModeChanges) String() (str string) { +func (changes ChannelModeChanges) String() string { if len(changes) == 0 { - return + return "" } - str = "+" - if changes[0].op == Remove { - str = "-" - } + op := changes[0].op + str := changes[0].op.String() + for _, change := range changes { + if change.op != op { + op = change.op + str += change.op.String() + } str += change.mode.String() } + for _, change := range changes { if change.arg == "" { continue } str += " " + change.arg } - return + return str } type ChannelModeCommand struct { @@ -517,7 +524,7 @@ type ChannelModeCommand struct { changes ChannelModeChanges } -// MODE *( ( "-" / "+" ) * * ) +// MODE ( "+" / "-" )? *( "+" / "-" / ) * func ParseChannelModeCommand(channel Name, args []string) (Command, error) { cmd := &ChannelModeCommand{ channel: channel, @@ -540,6 +547,10 @@ func ParseChannelModeCommand(channel Name, args []string) (Command, error) { skipArgs := 1 for _, mode := range modeArg { + if mode == '-' || mode == '+' { + op = ModeOp(mode) + continue + } change := &ChannelModeChange{ mode: ChannelMode(mode), op: op,