From 5fec9aac1fe693e26b61300bed45e79b3dedd8b2 Mon Sep 17 00:00:00 2001 From: Egor Date: Sat, 7 May 2022 21:16:58 +0300 Subject: [PATCH] add proper registration --- common.h | 4 +++- irc.c | 36 ++++++++++++++++++++++++++---------- net.c | 4 ++++ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/common.h b/common.h index 72692bc..4251edc 100644 --- a/common.h +++ b/common.h @@ -13,7 +13,7 @@ #define UMODE_WALLOPS 16 #define REG_INIT 0 #define REG_CAPLS 1 -#define REG_CAPEND 2 +#define REG_REGISTERED 2 struct client_data { SOCKET FD; char type; @@ -23,6 +23,8 @@ struct client_data { char *realname; char *hostname; char *away; + char *nick_reg; + char *pass_reg; char buf[2048]; size_t bufLen; size_t bufCap; diff --git a/irc.c b/irc.c index 4e20cc1..9cc0223 100644 --- a/irc.c +++ b/irc.c @@ -7,7 +7,7 @@ struct trie_node *client_map = NULL; struct trie_node *channel_map = NULL; const char *hostname = "irc.patchysicp.xyz"; -#define is_registered(cl) ((cl)->nick != 0) +#define is_registered(cl) ((cl)->regstate == REG_REGISTERED) void sendbuf(struct client_data *client) { send_log(client->FD, databuf.buf, databuf.len, 0); } @@ -145,9 +145,9 @@ int processPacket(struct client_data *client, char *source, char *command, int a // at least one client hangs if it doesn't see a known extension copy_strf(&databuf, ":%s CAP * LS :multi-prefix\n", hostname); sendbuf(client); - if (client->regstate != REG_CAPEND || !client->nick || !client->username) + if (!is_registered(client)) { client->regstate = REG_CAPLS; - + } } else if (strcmp(args[0], "LIST") == 0) { copy_strf(&databuf, ":%s CAP * LIST :\n", hostname); sendbuf(client); @@ -157,8 +157,10 @@ int processPacket(struct client_data *client, char *source, char *command, int a copy_strf(&databuf, ":%s CAP * NAK :%s\n", hostname, args[1]); sendbuf(client); } else if (strcmp(args[0], "END") == 0) { - if (client->regstate == REG_CAPLS && client->nick && client->username) + if (!is_registered(client)) { + client->regstate = REG_INIT; newUser(client); + } } else { goto unknown; } @@ -167,14 +169,22 @@ int processPacket(struct client_data *client, char *source, char *command, int a // User now registered if (!args[0]) // FIXME: error message return 0; - setNick(client, args[0]); - if (client->regstate != REG_CAPLS && client->nick && client->username) + if (is_registered(client)) { + setNick(client, args[0]); + } else { + free(client->nick_reg); + client->nick_reg = mystrdup(args[0]); + if (!client->nick_reg) + abort(); newUser(client); + } } else if(strcmp(command, "USER") == 0) { if (argc < 4) // FIXME: error message return 0; // set username - free(client->username); // FIXME: maybe don't allow to change it after registration? + if (is_registered(client)) + return 0; // FIXME: error + free(client->username); client->username = malloc(strlen(args[0])+2); if (!client->username) abort(); @@ -189,8 +199,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a if (!client->hostname) abort(); // Send welcome - if (client->regstate != REG_CAPLS && client->nick && client->username) - newUser(client); + newUser(client); } else if(strcmp(command, "MOTD") == 0) { // Send welcome sendMOTD(client); @@ -607,7 +616,14 @@ unknown: } void newUser(struct client_data *client) { - client->regstate = REG_CAPEND; + if (client->regstate == REG_CAPLS || !client->nick_reg || !client->username) + return; + setNick(client, client->nick_reg); + free(client->nick_reg); + client->nick_reg = NULL; + if (!client->nick) + return; + client->regstate = REG_REGISTERED; rpl_welcome(client), sendbuf(client); rpl_yourhost(client), sendbuf(client); rpl_created(client), sendbuf(client); diff --git a/net.c b/net.c index 62613c6..d02795c 100644 --- a/net.c +++ b/net.c @@ -65,6 +65,10 @@ void killConnection(struct client_data *cl, bool byChoice) { free(cl->realname); free(cl->hostname); free(cl->away); + free(cl->nick_reg); + if (cl->pass_reg) + memset(cl->pass_reg, 0, strlen(cl->pass_reg)); + free(cl->pass_reg); free(cl); } -- 2.56.0