aboutsummaryrefslogtreecommitdiff
path: root/conn.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-03-22 01:17:06 +0800
committerRunxi Yu <me@runxiyu.org>2025-03-22 02:02:39 +0800
commit62045c958b0d58d61e6d22deb88eed7b9ae28a4e (patch)
tree93ae68d77f6cc21de45a9f4edbdffb6cd0b5a1ae /conn.go
parentUse MIT because miniirc (diff)
downloadgo-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.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/conn.go b/conn.go
new file mode 100644
index 0000000..ac3787c
--- /dev/null
+++ b/conn.go
@@ -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))
+}