sircd/sircd/client.go

60 lines
1.6 KiB
Go

package sircd
import (
"bytes"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"net"
)
func newIrcClient(conn net.Conn, log *logrus.Logger, mc chan *ircMessage) *ircClient {
c := new(ircClient)
c.Id = uuid.New().String()
c.conn = conn
c.log = log
c.mc = mc
c.inputBytes = new(bytes.Buffer)
c.outputBytes = new(bytes.Buffer)
return c
}
type ircClient struct {
Id string
conn net.Conn
log *logrus.Logger
session *ircUserSession
inputBytes *bytes.Buffer
outputBytes *bytes.Buffer
mc chan *ircMessage
}
func (c *ircClient) Close() {
//client cleanup
c.log.Infof("client %d disconnect", c.Id)
}
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.Id, c.inputBytes.Len())
c.ParseInputBuffer()
}
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)
}