diff options
author | Runxi Yu <me@runxiyu.org> | 2024-12-30 01:23:40 +0000 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2024-12-30 01:23:40 +0000 |
commit | dce03955174abe5ad83c01c930e3bf47fe39d8ce (patch) | |
tree | cf4cddc3f83bc6eb9efd5be959d349fa47606c84 /main.go | |
parent | Reformat (diff) | |
download | meseircd-dce03955174abe5ad83c01c930e3bf47fe39d8ce.tar.gz meseircd-dce03955174abe5ad83c01c930e3bf47fe39d8ce.tar.zst meseircd-dce03955174abe5ad83c01c930e3bf47fe39d8ce.zip |
Revamp
Diffstat (limited to '')
-rw-r--r-- | main.go | 90 |
1 files changed, 11 insertions, 79 deletions
@@ -1,109 +1,41 @@ package main import ( - "bufio" - "log" "net" - "git.sr.ht/~runxiyu/meseircd/meselog" + "go.lindenii.runxiyu.org/lindenii-common/clog" ) const VERSION = "MeseIRCd-0.0.0" func main() { - setupCapls() + // TODO: Parse config and add TLS - self = &Server{ + self = &server_t{ conn: nil, - SID: 0, - Name: "irc.runxiyu.org", + sid: 0, + name: "irc.runxiyu.org", } listener, err := net.Listen("tcp", ":6667") if err != nil { - log.Fatal(err) + clog.Fatal(1, "listen", "error", err) } defer listener.Close() for { conn, err := listener.Accept() if err != nil { - log.Fatal(err) + clog.Fatal(1, "accept", "error", err) } - go func() { - defer func() { - raised := recover() - if raised != nil { - meselog.Error("connection routine panicked", "raised", raised) - } - }() defer conn.Close() - client, err := NewLocalClient(&conn) + client, err := new_local_client(&conn) if err != nil { - meselog.Error("cannot make new local client", "error", err) + clog.Error("new_local_client", "error", err, "conn", &conn) } - defer client.Teardown() - client.handleConnection() + defer client.teardown() + client.handle_connection() }() } } - -func (client *Client) handleConnection() { - reader := bufio.NewReader(*client.conn) -messageLoop: - for { - line, err := reader.ReadString('\n') - if err != nil { - meselog.Error("error while reading from connection", "error", err) - (*client.conn).Close() - return - } - meselog.Debug("recv", "line", line, "client", client.CID) - msg, err := parseIRCMsg(line) - if err != nil { - switch err { - case ErrEmptyMessage: - continue messageLoop - case ErrIllegalByte: - err := client.Send(MakeMsg(self, "ERROR", err.Error())) - if err != nil { - meselog.Error("error while reporting illegal byte", "error", err, "client", client) - return - } - return - case ErrTagsTooLong: - fallthrough - case ErrBodyTooLong: - err := client.Send(MakeMsg(self, ERR_INPUTTOOLONG, err.Error())) - if err != nil { - meselog.Error("error while reporting body too long", "error", err, "client", client) - return - } - continue messageLoop - default: - err := client.Send(MakeMsg(self, "ERROR", err.Error())) - if err != nil { - meselog.Error("error while reporting parser error", "error", err, "client", client) - } - return - } - } - - handler, ok := CommandHandlers[msg.Command] - if !ok { - err := client.Send(MakeMsg(self, ERR_UNKNOWNCOMMAND, msg.Command, "Unknown command")) - if err != nil { - meselog.Error("error while reporting unknown command", "error", err, "client", client) - return - } - continue - } - - err = handler(msg, client) - if err != nil { - meselog.Error("handler error", "error", err, "client", client) - return - } - } -} |