mirror of
https://github.com/ergochat/ergo.git
synced 2025-12-21 18:41:58 -08:00
Implement mode [+-]i.
This commit is contained in:
parent
8f0bd1b07a
commit
99364e8b5f
7 changed files with 94 additions and 29 deletions
|
|
@ -1,10 +1,33 @@
|
|||
package irc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var commands = map[string]func([]string) Message {
|
||||
"MODE": NewModeMessage,
|
||||
"NICK": NewNickMessage,
|
||||
"PING": NewPingMessage,
|
||||
"QUIT": NewQuitMessage,
|
||||
"USER": NewUserMessage,
|
||||
}
|
||||
|
||||
func ParseMessage(line string) Message {
|
||||
command, args := parseLine(line)
|
||||
constructor, ok := commands[command]
|
||||
var msg Message
|
||||
if ok {
|
||||
msg = constructor(args)
|
||||
}
|
||||
if msg == nil {
|
||||
msg = &UnknownMessage{command}
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
func parseArg(line string) (string, string) {
|
||||
if line == "" {
|
||||
return "", ""
|
||||
|
|
@ -31,25 +54,6 @@ func parseLine(line string) (string, []string) {
|
|||
return args[0], args[1:]
|
||||
}
|
||||
|
||||
var commands = map[string]func([]string) Message {
|
||||
"NICK": NewNickMessage,
|
||||
"PING": NewPingMessage,
|
||||
"QUIT": NewQuitMessage,
|
||||
"USER": NewUserMessage,
|
||||
}
|
||||
|
||||
func ParseMessage(line string) Message {
|
||||
command, args := parseLine(line)
|
||||
constructor, ok := commands[command]
|
||||
var msg Message
|
||||
if ok {
|
||||
msg = constructor(args)
|
||||
}
|
||||
if msg == nil {
|
||||
msg = &UnknownMessage{command}
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
// []string => Message constructors
|
||||
|
||||
|
|
@ -86,3 +90,25 @@ func NewUserMessage(args []string) Message {
|
|||
msg.realname = args[3]
|
||||
return msg
|
||||
}
|
||||
|
||||
var MODE_RE = regexp.MustCompile("^[-+][a-zA-Z]+$")
|
||||
|
||||
func NewModeMessage(args []string) Message {
|
||||
if len(args) < 1 {
|
||||
return nil
|
||||
}
|
||||
msg := new(ModeMessage)
|
||||
msg.nickname = args[0]
|
||||
for _, arg := range args[1:] {
|
||||
if !MODE_RE.MatchString(arg) {
|
||||
// TODO invalid args
|
||||
return nil
|
||||
}
|
||||
prefix := arg[0]
|
||||
for _, c := range arg[1:] {
|
||||
mode := fmt.Sprintf("%c%c", prefix, c)
|
||||
msg.modes = append(msg.modes, mode)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue