From 112722f1c8de842f6c8b672ea73b3cae182274ba Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 8 Dec 2024 09:49:40 +0800 Subject: Primitive nick registration --- clients.go | 8 ++++++-- cmd_nick.go | 22 ++++++++++++++++++++++ main.go | 5 ++++- numerics.go | 1 + servers.go | 4 ++-- 5 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 cmd_nick.go diff --git a/clients.go b/clients.go index 1a363cc..a181f99 100644 --- a/clients.go +++ b/clients.go @@ -3,11 +3,12 @@ package main import ( "net" "log/slog" + "sync" ) type Client struct { conn *net.Conn - UID [6]byte + UID string Nick string Ident string Host string @@ -41,7 +42,7 @@ func (client Client) ClientSource() string { } func (client Client) ServerSource() string { - return string(client.Server.SID[:]) + string(client.UID[:]) + return client.UID } type ClientState uint8 @@ -51,3 +52,6 @@ const ( ClientStatePreRegistration ClientStateRegistered ) + +var uidToClient = sync.Map{} +var nickToClient = sync.Map{} diff --git a/cmd_nick.go b/cmd_nick.go new file mode 100644 index 0000000..ad68cc6 --- /dev/null +++ b/cmd_nick.go @@ -0,0 +1,22 @@ +package main + +func init() { + commandHandlers["NICK"] = handleClientNick +} + +func handleClientNick(msg RMsg, client *Client) bool { + if len(msg.Params) < 1 { + client.Send(MakeMsg(self, ERR_NEEDMOREPARAMS, "NICK", "Not enough parameters")) + return true + } + _, exists := nickToClient.LoadOrStore(msg.Params[0], client) + if exists { + client.Send(MakeMsg(self, ERR_NICKNAMEINUSE, client.Nick, msg.Params[0], "Nickname is already in use")) + } else { + client.Nick = msg.Params[0] + if client.State == ClientStateRegistered { + client.Send(MakeMsg(client, "NICK", msg.Params[0])) + } + } + return true +} diff --git a/main.go b/main.go index fdff8b2..75afd08 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ func main() { self = Server{ conn: nil, - SID: [3]byte{'1', 'H', 'C'}, + SID: "001", Name: "irc.runxiyu.org", } @@ -34,7 +34,10 @@ func main() { 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() diff --git a/numerics.go b/numerics.go index ec3a09f..4af93dd 100644 --- a/numerics.go +++ b/numerics.go @@ -4,4 +4,5 @@ const ( ERR_UNKNOWNCOMMAND = "421" ERR_INPUTTOOLONG = "417" ERR_NEEDMOREPARAMS = "461" + ERR_NICKNAMEINUSE = "433" ) diff --git a/servers.go b/servers.go index e878911..343be53 100644 --- a/servers.go +++ b/servers.go @@ -7,7 +7,7 @@ import ( type Server struct { conn *net.Conn - SID [3]byte + SID string Name string } @@ -39,7 +39,7 @@ func (server Server) ClientSource() string { } func (server Server) ServerSource() string { - return string(server.SID[:]) + return server.SID } var self Server -- cgit v1.2.3