From d201e74fff4e6d82d639d858826bdb4190936c07 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 8 Dec 2024 08:52:23 +0800 Subject: Server and self awareness --- main.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index c320df0..f7e0f2d 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,12 @@ import ( ) func main() { + self = Server{ + conn: nil, + SID: [3]byte{'1', 'H', 'C'}, + Name: "irc.runxiyu.org", + } + listener, err := net.Listen("tcp", ":6667") if err != nil { log.Fatal(err) @@ -21,7 +27,8 @@ func main() { } client := &Client{ - conn: conn, + conn: conn, + Server: self, } go func() { defer func() { @@ -41,7 +48,8 @@ func main() { func (client *Client) handleConnection() { reader := bufio.NewReader(client.conn) - messageLoop: for { +messageLoop: + for { line, err := reader.ReadString('\n') if err != nil { slog.Error("error while reading from connection", "error", err) @@ -50,32 +58,31 @@ func (client *Client) handleConnection() { } msg, err := parseIRCMsg(line) if err != nil { - switch (err) { + switch err { case ErrEmptyMessage: continue messageLoop case ErrIllegalByte: - client.Send(SMsg{Command: "ERROR", Params: []string{err.Error()}}) + client.Send(MakeMsg(self, "ERROR", err.Error())) break messageLoop case ErrTagsTooLong: fallthrough case ErrBodyTooLong: - client.Send(SMsg{Command: ERR_INPUTTOOLONG, Params: []string{err.Error()}}) + client.Send(MakeMsg(self, ERR_INPUTTOOLONG, err.Error())) continue messageLoop default: - client.Send(SMsg{Command: "ERROR", Params: []string{err.Error()}}) + client.Send(MakeMsg(self, "ERROR", err.Error())) break messageLoop } } handler, ok := commandHandlers[msg.Command] if !ok { - client.Send(SMsg{Command: ERR_UNKNOWNCOMMAND, Params: []string{msg.Command, "Unknown command"}}) + client.Send(MakeMsg(self, ERR_UNKNOWNCOMMAND, msg.Command, "Unknown command")) continue } - err = handler(msg, client) - if err != nil { - client.Send(SMsg{Command: "ERROR", Params: []string{err.Error()}}) + cont := handler(msg, client) + if !cont { break } } -- cgit v1.2.3