2016-03-09 21:26:24 +00:00
|
|
|
// vi:ts=4:sts=4:sw=4:noet:tw=72
|
|
|
|
//
|
|
|
|
// Copyright (c) 2014-2016 Andreas Neue
|
|
|
|
|
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
Pre, Cmd, Trail, Raw string
|
|
|
|
Args []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func M(pre, cmd, args, trail string) *Message {
|
|
|
|
argv := []string{}
|
|
|
|
if args != "" {
|
|
|
|
argv = strings.Split(args, " ")
|
|
|
|
}
|
|
|
|
return &Message{Pre: pre, Cmd: cmd, Args: argv, Trail: trail}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(raw string) *Message {
|
|
|
|
var i int
|
|
|
|
pre, cmd, trail := "", "", ""
|
|
|
|
args := []string{}
|
|
|
|
|
|
|
|
s := raw
|
|
|
|
if strings.HasPrefix(s, ":") {
|
|
|
|
i = strings.Index(s, " ")
|
|
|
|
pre = s[1:i]
|
|
|
|
if i+1 < len(s) {
|
|
|
|
s = s[i+1:]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pre = ""
|
|
|
|
}
|
|
|
|
i = strings.Index(s, " ")
|
|
|
|
if i != -1 {
|
|
|
|
cmd = s[0:i]
|
|
|
|
if i+1 < len(s) {
|
|
|
|
s = s[i+1:]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cmd = s
|
|
|
|
}
|
|
|
|
i = strings.Index(s, ":")
|
|
|
|
if i != -1 {
|
|
|
|
if i+1 < len(s) {
|
|
|
|
trail = s[i+1:]
|
|
|
|
if i > 2 {
|
|
|
|
s = s[0 : i-1]
|
|
|
|
} else {
|
|
|
|
s = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args = strings.Split(s, " ")
|
|
|
|
|
|
|
|
return &Message{Pre: pre, Cmd: cmd, Args: args, Trail: trail, Raw: raw}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) String() string {
|
|
|
|
args := ""
|
|
|
|
for _, arg := range m.Args {
|
|
|
|
args += " " + arg
|
|
|
|
}
|
|
|
|
if m.Trail != "" {
|
2016-08-02 15:40:50 +00:00
|
|
|
return fmt.Sprintf(":%s %s%s :%s", m.Pre, m.Cmd, args, strings.Replace(m.Trail, "%", "%%", -1))
|
2016-03-09 21:26:24 +00:00
|
|
|
}
|
|
|
|
return fmt.Sprintf(":%s %s%s", m.Pre, m.Cmd, args)
|
|
|
|
}
|