From 6bbe00f0c7aae6c468b1a3e12983a74a170e92b8 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 8 Dec 2024 10:32:29 +0800 Subject: Handle send failures --- cmd_nick.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'cmd_nick.go') diff --git a/cmd_nick.go b/cmd_nick.go index 1c847e5..0d844b8 100644 --- a/cmd_nick.go +++ b/cmd_nick.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log/slog" ) @@ -8,28 +9,36 @@ func init() { commandHandlers["NICK"] = handleClientNick } -func handleClientNick(msg RMsg, client *Client) bool { +func handleClientNick(msg RMsg, client *Client) error { if len(msg.Params) < 1 { - client.Send(MakeMsg(self, ERR_NEEDMOREPARAMS, "NICK", "Not enough parameters")) - return true + return client.Send(MakeMsg(self, ERR_NEEDMOREPARAMS, "NICK", "Not enough parameters")) } already, exists := nickToClient.LoadOrStore(msg.Params[0], client) if exists { if already != client { - client.Send(MakeMsg(self, ERR_NICKNAMEINUSE, client.Nick, msg.Params[0], "Nickname is already in use")) + err := client.Send(MakeMsg(self, ERR_NICKNAMEINUSE, client.Nick, msg.Params[0], "Nickname is already in use")) + if err != nil { + return err + } } } else { if client.State == ClientStateRegistered { if !nickToClient.CompareAndDelete(client.Nick, client) { slog.Error("nick inconsistent", "nick", client.Nick, "client", client) - return false + return fmt.Errorf("%w: %v", ErrInconsistent, client) + } + err := client.Send(MakeMsg(client, "NICK", msg.Params[0])) + if err != nil { + return err } - client.Send(MakeMsg(client, "NICK", msg.Params[0])) } client.Nick = msg.Params[0] } if client.State == ClientStatePreRegistration { - client.checkRegistration() + err := client.checkRegistration() + if err != nil { + return err + } } - return true + return nil } -- cgit v1.2.3