From afed447bdb7ed3ebc08d2993e51146481444065d Mon Sep 17 00:00:00 2001 From: Egor Date: Thu, 27 Mar 2025 07:10:16 +0200 Subject: [PATCH] ircfmt: move SASL replies into functions --- src/irc.cpp | 28 +++++++++++++--------------- src/ircfmt.cpp | 42 ++++++++++++++++++++++++++++++++++-------- src/ircfmt.h | 16 ++++++++-------- 3 files changed, 55 insertions(+), 31 deletions(-) diff --git a/src/irc.cpp b/src/irc.cpp index 5c14123..871845d 100644 --- a/src/irc.cpp +++ b/src/irc.cpp @@ -211,10 +211,10 @@ static void cont_oper(struct client_data *client, int res) { static void cont_sasl(struct client_data *client, int res) { if (client->saslusername) { if (res == 0) { - copy_strf(&databuf, ":%s 903 * :SASL authentication successful\r\n", hostname), sendbuf(client); + rpl_saslsuccess(), sendbuf(client); client_chghost(client, client->saslusername, 0); } else { - copy_strf(&databuf, ":%s 904 * :SASL authentication failed\r\n", hostname), sendbuf(client); + err_saslfail(), sendbuf(client); } free(client->saslusername); client->saslusername = 0; @@ -322,14 +322,14 @@ int processPacket(struct client_data *client, char *source, char *command, int a if (!args[0]) return err_needmoreparams(client, command), sendbuf(client), 0; if (client->username && client->username[0] != '~') - return copy_strf(&databuf, ":%s 907 * :You have already authenticated using SASL\r\n", hostname), sendbuf(client), 0; + return err_saslalready(), sendbuf(client), 0; if (!(client->umode & UMODE_SECURE)) - return copy_strf(&databuf, ":%s 904 * :SASL authentication failed\r\n", hostname), sendbuf(client), 0; + return err_saslfail(), sendbuf(client), 0; if (client->saslusername) return 0; // not ideal, but we can't respond to the new message before responding to the old one if (!client->saslbuf) { if (strcmp(args[0], "PLAIN") != 0 && strcmp(args[0], "EXTERNAL") != 0) - return copy_strf(&databuf, ":%s 908 * PLAIN,EXTERNAL :are available SASL mechanisms\r\n", hostname), sendbuf(client), 0; + return rpl_saslmechs(), sendbuf(client), 0; copy_strf(&databuf, ":%s AUTHENTICATE +\r\n", hostname), sendbuf(client); // RFC 4616 spec says the three parts of the response can be up to 255 bytes long // That's 255*3 + 2, but we'll add another NUL byte after the pass, so it's 768. @@ -346,16 +346,16 @@ int processPacket(struct client_data *client, char *source, char *command, int a // len < 400 - copy the data, finish // data is + - don't copy, finish if (len == 1 && args[0][0] == '*') { - copy_strf(&databuf, ":%s 906 * :SASL auhtentication aborted\r\n", hostname), sendbuf(client); + err_saslaborted(), sendbuf(client); goto saslcleanup; } if (len != 1 || args[0][0] != '+') { if (len > 400 || client->saslbufLen + len > 1024) { memset(args[0], 0, len); if (len > 400) - copy_strf(&databuf, ":%s 905 * :SASL message too long\r\n", hostname), sendbuf(client); + err_sasltoolong(), sendbuf(client); else - copy_strf(&databuf, ":%s 904 * :SASL authentication failed\r\n", hostname), sendbuf(client); + err_saslfail(), sendbuf(client); goto saslcleanup; } memcpy(client->saslbuf + client->saslbufLen, args[0], len); @@ -430,11 +430,11 @@ int processPacket(struct client_data *client, char *source, char *command, int a #endif } if (res != -1) { - copy_strf(&databuf, ":%s 903 * :SASL authentication successful\r\n", hostname), sendbuf(client); + rpl_saslsuccess(), sendbuf(client); client_chghost(client, username, 0); } else { saslerr: - copy_strf(&databuf, ":%s 904 * :SASL authentication failed\r\n", hostname), sendbuf(client); + err_saslfail(), sendbuf(client); } saslcleanup: memset(client->saslbuf, 0, 1024); @@ -1189,7 +1189,7 @@ void newUser(struct client_data *client) { if (client->regstate != REG_INIT || !client->nick_reg || !client->username || client->passwait || client->webircwait || client->saslusername) return; if (client->saslbuf) { - copy_strf(&databuf, ":%s 906 * :SASL auhtentication aborted\r\n", hostname), sendbuf(client); + err_saslaborted(), sendbuf(client); if (client->saslbuf) { memset(client->saslbuf, 0, 1024); free(client->saslbuf); @@ -1427,13 +1427,11 @@ void client_chghost(struct client_data *client, const char *newusername, const c // notify the user of their new account name, and update the +r mode if (willbeloggedin) { if (newusername) - copy_strf(&databuf, ":%s 900 %s %s %s :You are now logged in as %s\r\n", - hostname, client->nick ? client->nick : "*", client->nuh, client->username, client->username), sendbuf(client); + rpl_loggedin(client), sendbuf(client); client->umode |= UMODE_REGISTERED; } else { if (wasloggedin) - copy_strf(&databuf, ":%s 901 %s %s :You are now logged out\r\n", - hostname, client->nick ? client->nick : "*", client->nuh), sendbuf(client); + rpl_loggedout(client), sendbuf(client); client->umode &= ~UMODE_REGISTERED; } if (is_registered(client) && wasloggedin != willbeloggedin) diff --git a/src/ircfmt.cpp b/src/ircfmt.cpp index f2d8d56..c60edfd 100644 --- a/src/ircfmt.cpp +++ b/src/ircfmt.cpp @@ -727,15 +727,41 @@ void rpl_monlistfull(struct client_data *client, int limit, const char *targets) { copy_strf(&databuf, ":%s 734 %s %d %s :Monitor list is full.\r\n", hostname, client->nick, limit, targets); } -// 900 RPL_LOGGEDIN -// 901 RPL_LOGGEDOUT +void rpl_loggedin(struct client_data *client) // 900 +{ + copy_strf(&databuf, ":%s 900 %s %s %s :You are now logged in as %s\r\n", + hostname, client->nick ? client->nick : "*", client->nuh, client->username, client->username); +} +void rpl_loggedout(struct client_data *client) // 901 +{ + copy_strf(&databuf, ":%s 901 %s %s :You are now logged out\r\n", + hostname, client->nick ? client->nick : "*", client->nuh); +} // 902 ERR_NICKLOCKED -// 903 RPL_SASLSUCCESS -// 904 ERR_SASLFAIL -// 905 ERR_SASLTOOLONG -// 906 ERR_SASLABORTED -// 907 ERR_SASLALREADY -// 908 RPL_SASLMECHS +void rpl_saslsuccess(void) // 903 +{ + copy_strf(&databuf, ":%s 903 * :SASL authentication successful\r\n", hostname); +} +void err_saslfail(void) // 904 +{ + copy_strf(&databuf, ":%s 904 * :SASL authentication failed\r\n", hostname); +} +void err_sasltoolong(void) // 905 +{ + copy_strf(&databuf, ":%s 905 * :SASL message too long\r\n", hostname); +} +void err_saslaborted(void) // 906 +{ + copy_strf(&databuf, ":%s 906 * :SASL auhtentication aborted\r\n", hostname); +} +void err_saslalready(void) // 907 +{ + copy_strf(&databuf, ":%s 907 * :You have already authenticated using SASL\r\n", hostname); +} +void rpl_saslmechs(void) // 908 +{ + copy_strf(&databuf, ":%s 908 * PLAIN,EXTERNAL :are available SASL mechanisms\r\n", hostname); +} void rpl_accesslist(struct client_data *client, struct channel *chan, char mode, const char *mask) // 910 { char modes[2] = {mode,0}; diff --git a/src/ircfmt.h b/src/ircfmt.h index 8757660..f1b1825 100644 --- a/src/ircfmt.h +++ b/src/ircfmt.h @@ -147,15 +147,15 @@ void rpl_monoffline(struct client_data *client, const char *nick); // 731 void rpl_monlist(struct client_data *client, const char *nick); // 732 void rpl_endofmonlist(struct client_data *client); // 733 void rpl_monlistfull(struct client_data *client, int limit, const char *targets); // 734 -// 900 RPL_LOGGEDIN -// 901 RPL_LOGGEDOUT +void rpl_loggedin(struct client_data *client); // 900 +void rpl_loggedout(struct client_data *client); // 901 // 902 ERR_NICKLOCKED -// 903 RPL_SASLSUCCESS -// 904 ERR_SASLFAIL -// 905 ERR_SASLTOOLONG -// 906 ERR_SASLABORTED -// 907 ERR_SASLALREADY -// 908 RPL_SASLMECHS +void rpl_saslsuccess(void); // 903 +void err_saslfail(void); // 904 +void err_sasltoolong(void); // 905 +void err_saslaborted(void); // 906 +void err_saslalready(void); // 907 +void rpl_saslmechs(void); // 908 void rpl_accesslist(struct client_data *client, struct channel *chan, char mode, const char *mask); // 910 void rpl_endofaccesslist(struct client_data *client, struct channel *chan); // 911 #endif -- 2.56.0