aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2024-12-08 08:45:13 +0800
committerRunxi Yu <me@runxiyu.org>2024-12-08 01:20:26 +0800
commitd810d12355873d3eaf61ed3f7f0ae52db06c2c96 (patch)
tree5cfa6d11c31377b459b06f4e3399bcc0542c57d8 /main.go
parentSlight refactor (diff)
downloadmeseircd-d810d12355873d3eaf61ed3f7f0ae52db06c2c96.tar.gz
meseircd-d810d12355873d3eaf61ed3f7f0ae52db06c2c96.tar.zst
meseircd-d810d12355873d3eaf61ed3f7f0ae52db06c2c96.zip
Add basic command handling
Diffstat (limited to 'main.go')
-rw-r--r--main.go41
1 files changed, 35 insertions, 6 deletions
diff --git a/main.go b/main.go
index 0898bd5..c320df0 100644
--- a/main.go
+++ b/main.go
@@ -7,10 +7,6 @@ import (
"net"
)
-type Client struct {
- conn net.Conn
-}
-
func main() {
listener, err := net.Listen("tcp", ":6667")
if err != nil {
@@ -29,6 +25,10 @@ func main() {
}
go func() {
defer func() {
+ client.conn.Close()
+ // TODO: Unified client clean-up
+ }()
+ defer func() {
raised := recover()
if raised != nil {
slog.Error("connection routine panicked", "raised", raised)
@@ -41,13 +41,42 @@ func main() {
func (client *Client) handleConnection() {
reader := bufio.NewReader(client.conn)
- for {
+ messageLoop: for {
line, err := reader.ReadString('\n')
if err != nil {
slog.Error("error while reading from connection", "error", err)
client.conn.Close()
return
}
- _, _ = parseIRCMsg(line)
+ msg, err := parseIRCMsg(line)
+ if err != nil {
+ switch (err) {
+ case ErrEmptyMessage:
+ continue messageLoop
+ case ErrIllegalByte:
+ client.Send(SMsg{Command: "ERROR", Params: []string{err.Error()}})
+ break messageLoop
+ case ErrTagsTooLong:
+ fallthrough
+ case ErrBodyTooLong:
+ client.Send(SMsg{Command: ERR_INPUTTOOLONG, Params: []string{err.Error()}})
+ continue messageLoop
+ default:
+ client.Send(SMsg{Command: "ERROR", Params: []string{err.Error()}})
+ break messageLoop
+ }
+ }
+
+ handler, ok := commandHandlers[msg.Command]
+ if !ok {
+ client.Send(SMsg{Command: ERR_UNKNOWNCOMMAND, Params: []string{msg.Command, "Unknown command"}})
+ continue
+ }
+
+ err = handler(msg, client)
+ if err != nil {
+ client.Send(SMsg{Command: "ERROR", Params: []string{err.Error()}})
+ break
+ }
}
}