dispatch/irc/message.go

60 lines
1008 B
Go
Raw Normal View History

2015-06-05 22:34:13 +00:00
package irc
import (
"strings"
)
type Message struct {
2016-12-12 06:19:22 +00:00
Prefix string
Nick string
Command string
Params []string
2015-06-05 22:34:13 +00:00
}
func parseMessage(line string) *Message {
line = strings.Trim(line, "\r\n")
msg := Message{}
cmdStart := 0
cmdEnd := len(line)
if strings.HasPrefix(line, ":") {
cmdStart = strings.Index(line, " ") + 1
if cmdStart > 0 {
msg.Prefix = line[1 : cmdStart-1]
} else {
// Invalid message
return &msg
}
2015-06-05 22:34:13 +00:00
if i := strings.Index(msg.Prefix, "!"); i > 0 {
msg.Nick = msg.Prefix[:i]
} else if i := strings.Index(msg.Prefix, "@"); i > 0 {
msg.Nick = msg.Prefix[:i]
2015-06-05 22:34:13 +00:00
} else {
msg.Nick = msg.Prefix
}
}
2016-12-12 06:19:22 +00:00
var usesTrailing bool
var trailing string
2015-06-05 22:34:13 +00:00
if i := strings.Index(line, " :"); i > 0 {
cmdEnd = i
2016-12-12 06:19:22 +00:00
trailing = line[i+2:]
usesTrailing = true
2015-06-05 22:34:13 +00:00
}
cmd := strings.Split(line[cmdStart:cmdEnd], " ")
msg.Command = cmd[0]
if len(cmd) > 1 {
msg.Params = cmd[1:]
}
2016-12-12 06:19:22 +00:00
if usesTrailing {
msg.Params = append(msg.Params, trailing)
2015-06-05 22:34:13 +00:00
}
return &msg
}