]> PatchyGit - patchyircd.git/commitdiff
move more stuff to ircfmt
authorEgor <egor@patchysicp.xyz>
Mon, 6 Mar 2023 23:27:28 +0000 (01:27 +0200)
committerEgor <egor@patchysicp.xyz>
Mon, 6 Mar 2023 23:27:28 +0000 (01:27 +0200)
irc.c
ircfmt.c
ircfmt.h

diff --git a/irc.c b/irc.c
index 9c85300..ae7128b 100644 (file)
--- a/irc.c
+++ b/irc.c
@@ -362,11 +362,8 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                        return err_notregistered(client), sendbuf(client), 0;
                if (argc < 1)
                        return err_needmoreparams(client, command), sendbuf(client), 0;
-               if (!(client->umode & UMODE_OPER)) {
-                       copy_strf(&databuf, ":%s 481 %s :Permission Denied- You're not an IRC operator\r\n", hostname, client->nick);
-                       sendbuf(client);
-                       return 0;
-               }
+               if (!(client->umode & UMODE_OPER))
+                       return err_noprivileges(client), sendbuf(client), 0;
                msg_wallops(client, args[0]);
                struct trie_find *f = NULL;
                if (trie_findfirst(&f, &client_map) == 1)
@@ -378,11 +375,8 @@ int processPacket(struct client_data *client, char *source, char *command, int a
        } else if(strcmp(command, "KILL") == 0) {
                if (!is_registered(client))
                        return err_notregistered(client), sendbuf(client), 0;
-               if (!(client->umode & UMODE_OPER)) {
-                       copy_strf(&databuf, ":%s 481 %s :Permission Denied- You're not an IRC operator\r\n", hostname, client->nick);
-                       sendbuf(client);
-                       return 0;
-               }
+               if (!(client->umode & UMODE_OPER))
+                       return err_noprivileges(client), sendbuf(client), 0;
                if (argc < 1)
                        return err_needmoreparams(client, command), sendbuf(client), 0;
                struct client_data *cl = findClient(args[0]);
@@ -403,11 +397,8 @@ int processPacket(struct client_data *client, char *source, char *command, int a
        } else if(strcmp(command, "JEDGAR") == 0) {
                if (!is_registered(client))
                        return err_notregistered(client), sendbuf(client), 0;
-               if (!(client->umode & UMODE_OPER)) {
-                       copy_strf(&databuf, ":%s 481 %s :Permission Denied- You're not an IRC operator\r\n", hostname, client->nick);
-                       sendbuf(client);
-                       return 0;
-               }
+               if (!(client->umode & UMODE_OPER))
+                       return err_noprivileges(client), sendbuf(client), 0;
                if (argc < 1)
                        return err_needmoreparams(client, command), sendbuf(client), 0;
                struct channel *chan = findChannel(args[0]);
@@ -510,10 +501,8 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                if (!chan)
                        return err_nosuchchannel(client, args[1]), sendbuf(client), 0;
                if (chan->dmode & DMODE_INVITEONLY) {
-                       if (!trie_get(&chan->o, client->key)) {
-                               copy_strf(&databuf, ":%s 482 %s :You're not channel operator\r\n", hostname, client->nick, chan->name);
-                               return sendbuf(client), 0;
-                       }
+                       if (!trie_get(&chan->o, client->key))
+                               return err_chanoprivsneeded(client, chan), sendbuf(client), 0;
                } else {
                        if (!trie_get(&chan->members, client->key))
                                return err_notonchannel(client, chan), sendbuf(client), 0;
@@ -538,10 +527,8 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                struct channel *chan = findChannel(args[0]);
                if (!chan)
                        return err_nosuchchannel(client, args[0]), sendbuf(client), 0;
-               if (!trie_get(&chan->o, client->key)) {
-                       copy_strf(&databuf, ":%s 482 %s :You're not channel operator\r\n", hostname, client->nick, chan->name);
-                       return sendbuf(client), 0;
-               }
+               if (!trie_get(&chan->o, client->key))
+                       return err_chanoprivsneeded(client, chan), sendbuf(client), 0;
                if (!trie_get(&chan->members, client->key))
                        return err_notonchannel(client, chan), sendbuf(client), 0;
                char *p = args[1];
@@ -624,17 +611,17 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                char line[512];
                FILE *f = fopen("help.txt", "r");
                if (f) {
-                       copy_strf(&databuf, ":%s 704 %s * :** /HELP **\r\n", hostname, client->nick), sendbuf(client);
+                       rpl_helpstart(client), sendbuf(client);
                        while (fgets(line, 512, f)) {
                                char *c = strchr(line, '\n');
                                if (*c)
                                        *c = '\0';
-                               copy_strf(&databuf, ":%s 705 %s * :%s\r\n", hostname, client->nick, line), sendbuf(client);
+                               rpl_helptxt(client, line), sendbuf(client);
                        }
                        fclose(f);
-                       copy_strf(&databuf, ":%s 706 %s * :End of /HELP\r\n", hostname, client->nick), sendbuf(client);
+                       rpl_endofhelp(client), sendbuf(client);
                } else {
-                       copy_strf(&databuf, ":%s 524 %s * :No help available on this topic\r\n", hostname, client->nick), sendbuf(client);
+                       err_helpnotfound(client), sendbuf(client);
                }
        } else if(strcmp(command, "VERSION") == 0) {
                if (!is_registered(client))
@@ -673,11 +660,8 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                                struct client_data *cl = findClient(nick);
                                if (!cl)
                                        return err_nosuchnick(client, nick), sendbuf(client), 0;
-                               if (client->FD != cl->FD) {
-                                       copy_strf(&databuf, ":%s 502 %s :Can't view mode for other users\n", hostname, client->nick);
-                                       sendbuf(client);
-                                       return 0;
-                               }
+                               if (client->FD != cl->FD)
+                                       return err_usersdontmatch(client, "Can't view mode for other users"), sendbuf(client), 0;
                                rpl_umodeis(client), sendbuf(client);
                        } else {
                                struct channel *chan = findChannel(nick);
@@ -691,11 +675,8 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                                struct client_data *cl = findClient(nick);
                                if (!cl)
                                        return err_nosuchnick(client, nick), sendbuf(client), 0;
-                               if (client->FD != cl->FD) {
-                                       copy_strf(&databuf, ":%s 502 %s :Can't change mode for other users\n", hostname, client->nick);
-                                       sendbuf(client);
-                                       return 0;
-                               }
+                               if (client->FD != cl->FD)
+                                       return err_usersdontmatch(client, "Can't change mode for other users"), sendbuf(client), 0;
                                int is_plus;
                                char *p = args[1];
                                switch (*p++) {
@@ -730,8 +711,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                                                sendbuf(client);
                                                break;
                                        default:
-                                               copy_strf(&databuf, ":%s 501 %s :Unknown MODE flag\n", hostname, client->nick);
-                                               sendbuf(client);
+                                               err_umodeunknownflag(client), sendbuf(client);
                                        }
                                        p++;
                                }
@@ -874,8 +854,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                                                                // FIXME: not enough args error
                                                                break;
                                                        } else if (strchr(*argp, ',') || strchr(*argp, ' ')) {
-                                                               copy_strf(&databuf, ":%s 525 %s %s :Key is not well-formed\r\n", hostname, client->nick, nick);
-                                                               sendbuf(client);
+                                                               err_invalidkey(client, chan), sendbuf(client);
                                                                argp++;
                                                                break;
                                                        }
@@ -1208,7 +1187,7 @@ void setNick(struct client_data *client, char* nickname) {
 
        // Update nickname
        if(client->nick) {
-               copy_strf(&databuf, ":%s!%s@%s NICK %s\n", client->nick, client->username, client->hostname, nickname);
+               msg_nick(client, nickname);
                sendbuf(client);
                sendbuf_visible(client);
                trie_set(&client_map, client->key, NULL, NULL);
index d47c701..3f9536f 100644 (file)
--- a/ircfmt.c
+++ b/ircfmt.c
@@ -573,21 +573,48 @@ void err_badchannelkey(struct client_data *client, struct channel *chan) // 475
        copy_strf(&databuf, ":%s 475 %s %s :Cannot join channel (+k)\r\n", hostname, client->nick, chan->name);
 }
 // 476 ERR_BADCHANMASK
-// 481 ERR_NOPRIVILEGES
-// 482 ERR_CHANOPRIVSNEEDED
+void err_noprivileges(struct client_data *client) // 481
+{
+       copy_strf(&databuf, ":%s 481 %s :Permission Denied- You're not an IRC operator\r\n", hostname, client->nick);
+}
+void err_chanoprivsneeded(struct client_data *client, struct channel *chan) // 481
+{
+       copy_strf(&databuf, ":%s 482 %s %s :You're not channel operator\r\n", hostname, client->nick, chan->name);
+}
 // 483 ERR_CANTKILLSERVER
 // 491 ERR_NOOPERHOST
-// 501 ERR_UMODEUNKNOWNFLAG
-// 502 ERR_USERSDONTMATCH
-// 524 ERR_HELPNOTFOUND
-// 525 ERR_INVALIDKEY
+void err_umodeunknownflag(struct client_data *client) // 501
+{
+       copy_strf(&databuf, ":%s 501 %s :Unknown MODE flag\r\n", hostname, client->nick);
+}
+void err_usersdontmatch(struct client_data *client, const char *what) // 502
+{
+       copy_strf(&databuf, ":%s 502 %s :%s\r\n", hostname, client->nick, what);
+}
+void err_helpnotfound(struct client_data *client) // 524
+{
+       copy_strf(&databuf, ":%s 524 %s * :No help available on this topic\r\n", hostname, client->nick);
+}
+void err_invalidkey(struct client_data *client, struct channel *chan) // 525
+{
+       copy_strf(&databuf, ":%s 525 %s %s :Key is not well-formed\r\n", hostname, client->nick, chan->name);
+}
 // 670 RPL_STARTTLS
 // 671 RPL_WHOISSECURE
 // 691 ERR_STARTTLS
 // 696 ERR_INVALIDMODEPARAM
-// 704 RPL_HELPSTART
-// 705 RPL_HELPTXT
-// 706 RPL_ENDOFHELP
+void rpl_helpstart(struct client_data *client) // 704
+{
+       copy_strf(&databuf, ":%s 704 %s * :** /HELP **\r\n", hostname, client->nick);
+}
+void rpl_helptxt(struct client_data *client, const char *line) // 705
+{
+       copy_strf(&databuf, ":%s 705 %s * :%s\r\n", hostname, client->nick, line);
+}
+void rpl_endofhelp(struct client_data *client) // 706
+{
+       copy_strf(&databuf, ":%s 706 %s * :End of /HELP\r\n", hostname, client->nick);
+}
 // 723 ERR_NOPRIVS
 void rpl_quietlist(struct client_data *client, struct channel *chan, const char *mask) // 728
 {
index 5df5350..6a5dffc 100644 (file)
--- a/ircfmt.h
+++ b/ircfmt.h
@@ -118,21 +118,21 @@ void err_inviteonlychan(struct client_data *client, struct channel *chan); // 47
 void err_bannedfromchan(struct client_data *client, struct channel *chan); // 474
 void err_badchannelkey(struct client_data *client, struct channel *chan); // 475
 // 476 ERR_BADCHANMASK
-// 481 ERR_NOPRIVILEGES
-// 482 ERR_CHANOPRIVSNEEDED
+void err_noprivileges(struct client_data *client); // 481
+void err_chanoprivsneeded(struct client_data *client, struct channel *chan); // 481
 // 483 ERR_CANTKILLSERVER
 // 491 ERR_NOOPERHOST
-// 501 ERR_UMODEUNKNOWNFLAG
-// 502 ERR_USERSDONTMATCH
-// 524 ERR_HELPNOTFOUND
-// 525 ERR_INVALIDKEY
+void err_umodeunknownflag(struct client_data *client); // 501
+void err_usersdontmatch(struct client_data *client, const char *what); // 502
+void err_helpnotfound(struct client_data *client); // 524
+void err_invalidkey(struct client_data *client, struct channel *chan); // 525
 // 670 RPL_STARTTLS
 // 671 RPL_WHOISSECURE
 // 691 ERR_STARTTLS
 // 696 ERR_INVALIDMODEPARAM
-// 704 RPL_HELPSTART
-// 705 RPL_HELPTXT
-// 706 RPL_ENDOFHELP
+void rpl_helpstart(struct client_data *client); // 704
+void rpl_helptxt(struct client_data *client, const char *line); // 705
+void rpl_endofhelp(struct client_data *client); // 706
 // 723 ERR_NOPRIVS
 void rpl_quietlist(struct client_data *client, struct channel *chan, const char *mask); // 728
 void rpl_endofquietlist(struct client_data *client, struct channel *chan); // 729