aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 75afd0833908f53f05447e7a059d878c7697ba51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main

import (
	"bufio"
	"log"
	"log/slog"
	"net"
	"os"
)

func main() {
	logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}))
	slog.SetDefault(logger)

	self = Server{
		conn: nil,
		SID:  "001",
		Name: "irc.runxiyu.org",
	}

	listener, err := net.Listen("tcp", ":6667")
	if err != nil {
		log.Fatal(err)
	}
	defer listener.Close()

	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Fatal(err)
		}

		client := &Client{
			conn:   &conn,
			Server: self,
			State:  ClientStatePreRegistration,
			UID:    "blah",
			Nick:   "*",
		}
		// TODO: Add to the UID table and make actually unique UIDs
		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)
				}
			}()
			client.handleConnection()
		}()
	}
}

func (client *Client) handleConnection() {
	reader := bufio.NewReader(*client.conn)
messageLoop:
	for {
		line, err := reader.ReadString('\n')
		if err != nil {
			slog.Error("error while reading from connection", "error", err)
			(*client.conn).Close()
			return
		}
		slog.Debug("recv", "line", line, "conn", client.conn)
		msg, err := parseIRCMsg(line)
		if err != nil {
			switch err {
			case ErrEmptyMessage:
				continue messageLoop
			case ErrIllegalByte:
				client.Send(MakeMsg(self, "ERROR", err.Error()))
				break messageLoop
			case ErrTagsTooLong:
				fallthrough
			case ErrBodyTooLong:
				client.Send(MakeMsg(self, ERR_INPUTTOOLONG, err.Error()))
				continue messageLoop
			default:
				client.Send(MakeMsg(self, "ERROR", err.Error()))
				break messageLoop
			}
		}

		handler, ok := commandHandlers[msg.Command]
		if !ok {
			client.Send(MakeMsg(self, ERR_UNKNOWNCOMMAND, msg.Command, "Unknown command"))
			continue
		}

		cont := handler(msg, client)
		if !cont {
			break
		}
	}
}