From 7fe8fe6c9fff814b88929026384f1851ff321781 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 8 Dec 2024 10:13:58 +0800 Subject: Registration --- clients.go | 13 ++++++++++++- cmd_nick.go | 3 +++ cmd_user.go | 22 ++++++++++++++++++++++ numerics.go | 4 ++++ servers.go | 2 +- 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 cmd_user.go diff --git a/clients.go b/clients.go index 53fd134..15512c0 100644 --- a/clients.go +++ b/clients.go @@ -1,8 +1,8 @@ package main import ( - "net" "log/slog" + "net" "sync" ) @@ -11,6 +11,7 @@ type Client struct { UID string Nick string Ident string + Gecos string Host string Server Server State ClientState @@ -57,6 +58,16 @@ func (client *Client) Teardown() { } } +func (client *Client) checkRegistration() { + if client.State != ClientStatePreRegistration { + slog.Error("spurious call to checkRegistration", "client", client) + return // TODO: Return an error? + } + if client.Nick != "*" && client.Ident != "" { + client.Send(MakeMsg(self, RPL_WELCOME, client.Nick, "Welcome")) + } +} + type ClientState uint8 const ( diff --git a/cmd_nick.go b/cmd_nick.go index 89ee4d9..1c847e5 100644 --- a/cmd_nick.go +++ b/cmd_nick.go @@ -28,5 +28,8 @@ func handleClientNick(msg RMsg, client *Client) bool { } client.Nick = msg.Params[0] } + if client.State == ClientStatePreRegistration { + client.checkRegistration() + } return true } diff --git a/cmd_user.go b/cmd_user.go new file mode 100644 index 0000000..99d9027 --- /dev/null +++ b/cmd_user.go @@ -0,0 +1,22 @@ +package main + +import ( +// "log/slog" +) + +func init() { + commandHandlers["USER"] = handleClientUser +} + +func handleClientUser(msg RMsg, client *Client) bool { + if len(msg.Params) < 4 { + client.Send(MakeMsg(self, ERR_NEEDMOREPARAMS, "USER", "Not enough parameters")) + return true + } + client.Ident = "~" + msg.Params[0] + client.Gecos = msg.Params[3] + if client.State == ClientStatePreRegistration { + client.checkRegistration() + } + return true +} diff --git a/numerics.go b/numerics.go index 4af93dd..c8a351b 100644 --- a/numerics.go +++ b/numerics.go @@ -1,5 +1,9 @@ package main +const ( + RPL_WELCOME = "001" +) + const ( ERR_UNKNOWNCOMMAND = "421" ERR_INPUTTOOLONG = "417" diff --git a/servers.go b/servers.go index 343be53..4cc8b31 100644 --- a/servers.go +++ b/servers.go @@ -1,8 +1,8 @@ package main import ( - "net" "log/slog" + "net" ) type Server struct { -- cgit v1.2.3