diff options
author | Runxi Yu <me@runxiyu.org> | 2025-03-22 01:17:06 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-03-22 02:02:39 +0800 |
commit | 62045c958b0d58d61e6d22deb88eed7b9ae28a4e (patch) | |
tree | 93ae68d77f6cc21de45a9f4edbdffb6cd0b5a1ae /conn.go | |
parent | Use MIT because miniirc (diff) | |
download | go-lindenii-irc-62045c958b0d58d61e6d22deb88eed7b9ae28a4e.tar.gz go-lindenii-irc-62045c958b0d58d61e6d22deb88eed7b9ae28a4e.tar.zst go-lindenii-irc-62045c958b0d58d61e6d22deb88eed7b9ae28a4e.zip |
Basic IRCv3 message parser
Diffstat (limited to 'conn.go')
-rw-r--r-- | conn.go | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -0,0 +1,42 @@ +package irc + +import ( + "bufio" + "net" + "slices" +) + +type Conn struct { + netConn net.Conn + bufReader *bufio.Reader +} + +func NewConn(netConn net.Conn) Conn { + return Conn{ + netConn: netConn, + bufReader: bufio.NewReader(netConn), + } +} + +func (c *Conn) ReadMessage() (msg Message, err error) { + raw, err := c.bufReader.ReadSlice('\n') + if err != nil { + return + } + + if raw[len(raw) - 1] == '\r' { + raw = raw[:len(raw) - 1] + } + + msg, err = Parse(slices.Clone(raw)) + + return +} + +func (c *Conn) Write(p []byte) (n int, err error) { + return c.netConn.Write(p) +} + +func (c *Conn) WriteString(s string) (n int, err error) { + return c.netConn.Write(stringToBytes(s)) +} |