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

modes: Migrating useful mode structs and functions directly to modes

This commit is contained in:
Daniel Oaks 2016-06-28 16:22:35 +10:00
parent 41473bb444
commit 670cf51452
2 changed files with 79 additions and 173 deletions

View File

@ -5,179 +5,6 @@
package irc package irc
import "fmt"
type ModeChange struct {
mode UserMode
op ModeOp
}
func (change *ModeChange) String() string {
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 {
op = change.op
str += change.op.String()
}
str += change.mode.String()
}
return str
}
/*
type ModeCommand struct {
BaseCommand
nickname Name
changes ModeChanges
}
// MODE <nickname> ( "+" / "-" )? *( "+" / "-" / <mode character> )
func ParseUserModeCommand(nickname Name, args []string) (Command, error) {
cmd := &ModeCommand{
nickname: nickname,
changes: make(ModeChanges, 0),
}
// account for MODE command with no args to list things
if len(args) < 1 {
// don't do any further processing
return cmd, nil
}
modeArg := args[0]
op := ModeOp(modeArg[0])
if (op == Add) || (op == Remove) {
modeArg = modeArg[1:]
} else {
return nil, ErrParseCommand
}
for _, mode := range modeArg {
if mode == '-' || mode == '+' {
op = ModeOp(mode)
continue
}
cmd.changes = append(cmd.changes, &ModeChange{
mode: UserMode(mode),
op: op,
})
}
return cmd, nil
}
*/
type ChannelModeChange struct {
mode ChannelMode
op ModeOp
arg string
}
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
func (changes ChannelModeChanges) String() string {
if len(changes) == 0 {
return ""
}
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 str
}
type ChannelModeCommand struct {
channel Name
changes ChannelModeChanges
}
/*
// MODE <channel> ( "+" / "-" )? *( "+" / "-" / <mode character> ) *<modeparams>
func ParseChannelModeCommand(channel Name, args []string) (Command, error) {
cmd := &ChannelModeCommand{
channel: channel,
changes: make(ChannelModeChanges, 0),
}
// account for MODE command with no args to list things
if len(args) < 1 {
// don't do any further processing
return cmd, nil
}
modeArg := args[0]
op := ModeOp(modeArg[0])
if (op == Add) || (op == Remove) {
modeArg = modeArg[1:]
} else {
return nil, ErrParseCommand
}
currentArgIndex := 1
for _, mode := range modeArg {
if mode == '-' || mode == '+' {
op = ModeOp(mode)
continue
}
change := &ChannelModeChange{
mode: ChannelMode(mode),
op: op,
}
switch change.mode {
// TODO(dan): separate this into the type A/B/C/D args and use those lists here
case Key, BanMask, ExceptMask, InviteMask, UserLimit,
ChannelOperator, ChannelFounder, ChannelAdmin, Halfop, Voice:
if len(args) > currentArgIndex {
change.arg = args[currentArgIndex]
currentArgIndex++
} else {
// silently skip this mode
continue
}
}
cmd.changes = append(cmd.changes, change)
}
return cmd, nil
}
/* /*
func ParseModeCommand(args []string) (Command, error) { func ParseModeCommand(args []string) (Command, error) {

View File

@ -6,6 +6,7 @@
package irc package irc
import ( import (
"fmt"
"strconv" "strconv"
"strings" "strings"
@ -29,6 +30,34 @@ func (modes UserModes) String() string {
return strings.Join(strs, "") return strings.Join(strs, "")
} }
type ModeChange struct {
mode UserMode
op ModeOp
}
func (change *ModeChange) String() string {
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 {
op = change.op
str += change.op.String()
}
str += change.mode.String()
}
return str
}
// channel mode flags // channel mode flags
type ChannelMode rune type ChannelMode rune
@ -46,6 +75,55 @@ func (modes ChannelModes) String() string {
return strings.Join(strs, "") return strings.Join(strs, "")
} }
type ChannelModeChange struct {
mode ChannelMode
op ModeOp
arg string
}
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
func (changes ChannelModeChanges) String() string {
if len(changes) == 0 {
return ""
}
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 str
}
type ChannelModeCommand struct {
channel Name
changes ChannelModeChanges
}
type ModeOp rune type ModeOp rune
func (op ModeOp) String() string { func (op ModeOp) String() string {
@ -240,6 +318,7 @@ func cmodeHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
changes := make(ChannelModeChanges, 0) changes := make(ChannelModeChanges, 0)
applied := make(ChannelModeChanges, 0) applied := make(ChannelModeChanges, 0)
// TODO(dan): look at separating these into the type A/B/C/D args and using those lists here
if len(msg.Params) > 1 { if len(msg.Params) > 1 {
modeArg := msg.Params[1] modeArg := msg.Params[1]
op := ModeOp(modeArg[0]) op := ModeOp(modeArg[0])