diff options
author | Runxi Yu <me@runxiyu.org> | 2024-12-08 09:49:40 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2024-12-08 09:49:40 +0800 |
commit | 112722f1c8de842f6c8b672ea73b3cae182274ba (patch) | |
tree | 41cc23dc14b36c3c53b1dbb18cad7d334e9758af | |
parent | Primitive client states (diff) | |
download | meseircd-112722f1c8de842f6c8b672ea73b3cae182274ba.tar.gz meseircd-112722f1c8de842f6c8b672ea73b3cae182274ba.tar.zst meseircd-112722f1c8de842f6c8b672ea73b3cae182274ba.zip |
Primitive nick registration
-rw-r--r-- | clients.go | 8 | ||||
-rw-r--r-- | cmd_nick.go | 22 | ||||
-rw-r--r-- | main.go | 5 | ||||
-rw-r--r-- | numerics.go | 1 | ||||
-rw-r--r-- | servers.go | 4 |
5 files changed, 35 insertions, 5 deletions
@@ -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 +} @@ -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" ) @@ -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 |