landing, this is latest
This commit is contained in:
parent
b936973154
commit
a825e63113
2
main.go
2
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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue