From a825e63113c3444fbb1273de6ac0a53a0b431745 Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 19 Aug 2019 22:48:43 -0500 Subject: [PATCH] landing, this is latest --- main.go | 2 +- sircd/client.go | 29 ++++++++++++--------- sircd/message.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 sircd/message.go diff --git a/main.go b/main.go index b20d777..ec6cba0 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,7 @@ import "time" func main() { var log = logrus.New() - log.SetLevel(log.DebugLevel) + log.SetLevel(logrus.DebugLevel) log.Println("sircd starting up") s := sircd.NewSircd() s.SetLogger(log) diff --git a/sircd/client.go b/sircd/client.go index b22e3f4..175c8df 100644 --- a/sircd/client.go +++ b/sircd/client.go @@ -28,12 +28,6 @@ type ircClient struct { mc chan *ircMessage } -type ircMessage struct { - from *ircClient - //received - //command -} - func (c *ircClient) Close() { //client cleanup c.log.Infof("client %d disconnect", c.Id) @@ -43,12 +37,23 @@ func (c *ircClient) AppendInputBuffer(input []byte) { // Read the incoming connection into the buffer. c.log.Printf("conn<%s>: got %d bytes from net", c.Id, len(input)) c.inputBytes.Write(input) - c.log.Debugf("conn<%s> buffer now %d bytes total", c.inputBytes.Len()) - c.ParseMessages() + c.log.Debugf("conn<%s> buffer now %d bytes total", c.Id, c.inputBytes.Len()) + c.ParseInputBuffer() } -func (c *ircClient) ParseMessages() { - c.log.Debugln("my input buffer is %d", c.inputBytes.Len()) - c.log.Debugln("my input buffer is: '%s'", c.inputBytes.String()) - //c.log.Fatalln("not implemented") +func (c *ircClient) ParseInputBuffer() { + c.log.Debugf("my input buffer is %d bytes", c.inputBytes.Len()) + c.log.Debugf("my input buffer is: '%s'", c.inputBytes.String()) + for line, err := c.inputBytes.ReadString(byte('\n')) + if err == nil { + c.mc <- c.ParseSingleInput(line) //FIXME make this return val, err + } else { + c.log.Debugf("error parsing input buffer: ", err.Error()) + } + c.log.Debugf("my input buffer is %d bytes", c.inputBytes.Len()) + c.log.Debugf("my input buffer is: '%s'", c.inputBytes.String()) +} + +func (c *ircClient) ParseSingleInput(line string) *ircMessage { + return NewIrcMessageFromString(line, c) } diff --git a/sircd/message.go b/sircd/message.go new file mode 100644 index 0000000..b55230b --- /dev/null +++ b/sircd/message.go @@ -0,0 +1,65 @@ +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) +}