sircd/sircd/message.go

66 lines
1.3 KiB
Go
Raw Normal View History

2019-08-20 03:48:43 +00:00
package sircd
import (
"strings"
"fmt"
"regexp"
)
type ircMessage struct {
from *ircClient
//received
tags string
source string
raw string
command string
params []string
}
func NewIrcMessageFromString (line string, from *ircClient) *ircMessage {
//FIXME do this at compile or start time instead of every message
ircregex, err := regexp.Compile(`^(\@(\S+) )?(?:[:](\S+) )?(\S+)(?: ([^:].+?))?(?: [:](.+))?$`)
if err != nil {
panic("can't happen")
}
line = strings.TrimRight(line, "\r\n")
m := new(ircMessage)
m.raw = line
if ircregex.MatchString(m.raw) == false {
m.command = "UNKNOWN"
return m
}
matches := ircregex.FindAllStringSubmatch(m.raw, -1)
if len(matches) == 0 {
m.command = "UNKNOWN"
return m
}
match := matches[0]
fmt.Printf("%+v\n", match)
fmt.Printf("%+v\n", len(match))
m.tags = match[2]
m.source = match[3]
m.command = match[4]
if len(match[5]) > 0 {
m.params = strings.Fields(match[5])
}
if len(match[6]) > 0 {
m.params = append(m.params, match[6])
}
fmt.Printf("%+v\n", &m)
fmt.Println(m)
return m
}
func (m *ircMessage) String() string {
return fmt.Sprintf("IRCMessage<%s>('%s')", m.command, m.raw)
}