From: Egor Date: Tue, 3 May 2022 21:44:56 +0000 (+0300) Subject: add basic cap negotiation X-Git-Url: https://git.patchysicp.xyz/?a=commitdiff_plain;h=8d559705aa7be564c8bed3feffa3aaa66ca109c0;p=patchyircd.git add basic cap negotiation --- diff --git a/common.h b/common.h index 101518a..ed89f98 100644 --- a/common.h +++ b/common.h @@ -10,6 +10,9 @@ #define UMODE_LOPER 4 #define UMODE_REGISTERED 8 #define UMODE_WALLOPS 16 +#define REG_INIT 0 +#define REG_CAPLS 1 +#define REG_CAPEND 2 struct client_data { SOCKET FD; char type; @@ -23,6 +26,7 @@ struct client_data { size_t bufLen; size_t bufCap; char umode; + char regstate; struct trie_node *channels; }; struct channel { diff --git a/irc.c b/irc.c index e876e76..369e98b 100644 --- a/irc.c +++ b/irc.c @@ -81,8 +81,32 @@ int processPacket(struct client_data *client) { if(strcmp(command, "PING") == 0) { if (!args[0]) // FIXME: error message return 0; - copy_strf(&databuf, ":%s PONG %s :%s", hostname, hostname, args[0]); + copy_strf(&databuf, ":%s PONG %s :%s\n", hostname, hostname, args[0]); send_log(client->FD, databuf.buf, databuf.len, 0); + } else if(strcmp(command, "CAP") == 0) { + if (!args[0]) // FIXME: error message + return 0; + stringupper(args[0]); + if (strcmp(args[0], "LS") == 0) { + // at least one client hangs if it doesn't see a known extension + copy_strf(&databuf, ":%s CAP * LS :multi-prefix\n", hostname); + send_log(client->FD, databuf.buf, databuf.len, 0); + if (client->regstate != REG_CAPEND || !client->nick || !client->username) + client->regstate = REG_CAPLS; + } else if (strcmp(args[0], "LIST") == 0) { + copy_strf(&databuf, ":%s CAP * LIST :\n", hostname); + send_log(client->FD, databuf.buf, databuf.len, 0); + } else if (strcmp(args[0], "REQ") == 0) { + if (!args[1]) // FIXME: error message + return 0; + copy_strf(&databuf, ":%s CAP * NAK :%s\n", hostname, args[1]); + send_log(client->FD, databuf.buf, databuf.len, 0); + } else if (strcmp(args[0], "END") == 0) { + if (client->regstate == REG_CAPLS && client->nick && client->username) + newUser(client); + } else { + goto unknown; + } } else if(strcmp(command, "NICK") == 0) { // User now registered if (!args[0]) // FIXME: error message @@ -91,7 +115,7 @@ int processPacket(struct client_data *client) { if (client->nick) printf("USER SET NICKNAME %s\n", client->nick); fflush(stdout); - if (client->nick && client->username) + if (client->regstate != REG_CAPLS && client->nick && client->username) newUser(client); } else if(strcmp(command, "USER") == 0) { if (argc < 4) // FIXME: error message @@ -112,7 +136,7 @@ int processPacket(struct client_data *client) { if (!client->hostname) abort(); // Send welcome - if (client->nick && client->username) + if (client->regstate != REG_CAPLS && client->nick && client->username) newUser(client); } else if(strcmp(command, "MOTD") == 0) { // Send welcome @@ -402,6 +426,7 @@ unknown: } void newUser(struct client_data *client) { + client->regstate = REG_CAPEND; copy_strf(&databuf, ":%s 001 %s :Welcome to the Patchy Network, %s\n", hostname, client->nick, client->nick); send_log(client->FD, databuf.buf, databuf.len, 0); copy_strf(&databuf, ":%s 002 %s :Your host is PatchyIRCD, running version 0.0.1\n", hostname, client->nick);