]> PatchyGit - patchyircd.git/commitdiff
add copy_strf
authorEgor <egor@patchysicp.xyz>
Mon, 2 May 2022 19:06:17 +0000 (22:06 +0300)
committerEgor <egor@patchysicp.xyz>
Mon, 2 May 2022 19:06:17 +0000 (22:06 +0300)
irc.c
util.h

diff --git a/irc.c b/irc.c
index fc5f5bc..7da1c1d 100644 (file)
--- a/irc.c
+++ b/irc.c
@@ -74,7 +74,7 @@ int processPacket(struct client_data *client) {
        if(strcmp(command, "PING") == 0) {
                if (!args[0]) // FIXME: error message
                        return 0;
-               append_strf(&databuf, ":%s PONG %s :%s", hostname, hostname, args[0]);
+               copy_strf(&databuf, ":%s PONG %s :%s", hostname, hostname, args[0]);
                send_log(client->FD, databuf.buf, databuf.len, 0);
        } else if(strcmp(command, "NICK") == 0) {
                // User now registered
@@ -97,27 +97,25 @@ int processPacket(struct client_data *client) {
                                *p = '\0';
                                break;
                        }
-               append_strf(&databuf, ":%s JOIN :%s\n", client->nick, args[0]);
+               copy_strf(&databuf, ":%s JOIN :%s\n", client->nick, args[0]);
                // End GitHub Copilot
                send_log(client->FD, databuf.buf, databuf.len, 0);
                sendall_log(client->FD, databuf.buf, databuf.len, 0);
                for(int i=0; i<CLIENTS; i++)
                        if(clients[i] && clients[i]->nick[0]) {
-                               databuf.len = 0;
-                               append_strf(&databuf, ":%s 353 %s = %s :%s\n", hostname, client->nick, args[0], clients[i]->nick);
+                               copy_strf(&databuf, ":%s 353 %s = %s :%s\n", hostname, client->nick, args[0], clients[i]->nick);
                                send_log(client->FD, databuf.buf, databuf.len, 0);
                        }
-               databuf.len = 0;
-               append_strf(&databuf, ":%s 366 %s :End of /NAMES list\n", hostname, client->nick);
+               copy_strf(&databuf, ":%s 366 %s :End of /NAMES list\n", hostname, client->nick);
                send_log(client->FD, databuf.buf, databuf.len, 0);
        } else if(strcmp(command, "PRIVMSG") == 0) {
                if (argc < 2) // FIXME: error message
                        return 0;
-               append_strf(&databuf, ":%s PRIVMSG %s :%s\n", client->nick, args[0], args[1]);
+               copy_strf(&databuf, ":%s PRIVMSG %s :%s\n", client->nick, args[0], args[1]);
                sendall_log(client->FD, databuf.buf, databuf.len, 0);
        // Start GitHub Copilot - this shit actually be copy pasting my code lmao
        } else if(strcmp(command, "QUIT") == 0) {
-               append_strf(&databuf, ":%s QUIT :%s\n", client->nick, args[0] ? args[0] : "Client Quit");
+               copy_strf(&databuf, ":%s QUIT :%s\n", client->nick, args[0] ? args[0] : "Client Quit");
                sendall_log(client->FD, databuf.buf, databuf.len, 0);
                killConnection(client, true);
                return 1;
@@ -131,11 +129,11 @@ int processPacket(struct client_data *client) {
                        if (*nick != '#') {
                                struct client_data *cl = findClient(nick);
                                if (!cl) {
-                                       append_strf(&databuf, ":%s 401 %s %s :No such nick/channel\n", hostname, client->nick, nick);
+                                       copy_strf(&databuf, ":%s 401 %s %s :No such nick/channel\n", hostname, client->nick, nick);
                                        send_log(client->FD, databuf.buf, databuf.len, 0);
                                        return 0;
                                }
-                               append_strf(&databuf, ":%s 221 %s +", hostname, client->nick);
+                               copy_strf(&databuf, ":%s 221 %s +", hostname, client->nick);
                                if (cl->umode & UMODE_INVISIBLE) append_str(&databuf, "i");
                                if (cl->umode & UMODE_OPER) append_str(&databuf, "o");
                                if (cl->umode & UMODE_LOPER) append_str(&databuf, "O");
@@ -144,19 +142,19 @@ int processPacket(struct client_data *client) {
                                append_str(&databuf, "\n");
                                send_log(client->FD, databuf.buf, databuf.len, 0);
                        } else {
-                               append_strf(&databuf, ":%s 324 %s %s +nt\n", hostname, client->nick, nick);
+                               copy_strf(&databuf, ":%s 324 %s %s +nt\n", hostname, client->nick, nick);
                                send_log(client->FD, databuf.buf, databuf.len, 0);
                        }
                } else { // set mode
                        if (*nick != '#') {
                                struct client_data *cl = findClient(nick);
                                if (!cl) {
-                                       append_strf(&databuf, ":%s 401 %s %s :No such nick/channel\n", hostname, client->nick, nick);
+                                       copy_strf(&databuf, ":%s 401 %s %s :No such nick/channel\n", hostname, client->nick, nick);
                                        send_log(client->FD, databuf.buf, databuf.len, 0);
                                        return 0;
                                }
                                if (client->FD != cl->FD) {
-                                       append_strf(&databuf, ":%s 502 %s :Can't change mode for other users\n", hostname, client->nick);
+                                       copy_strf(&databuf, ":%s 502 %s :Can't change mode for other users\n", hostname, client->nick);
                                        send_log(client->FD, databuf.buf, databuf.len, 0);
                                        return 0;
                                }
@@ -168,20 +166,19 @@ int processPacket(struct client_data *client) {
                                        default: goto unknown;
                                }
                                while (*p) {
-                                       databuf.len = 0;
                                        switch (*p) {
                                        case 'i':
                                                if (off) {
-                                                       append_strf(&databuf, ":%s MODE %s -i\n", hostname, client->nick);
+                                                       copy_strf(&databuf, ":%s MODE %s -i\n", hostname, client->nick);
                                                        cl->umode &= ~UMODE_INVISIBLE;
                                                } else {
-                                                       append_strf(&databuf, ":%s MODE %s +i\n", hostname, client->nick);
+                                                       copy_strf(&databuf, ":%s MODE %s +i\n", hostname, client->nick);
                                                        cl->umode |= UMODE_INVISIBLE;
                                                }
                                                send_log(client->FD, databuf.buf, databuf.len, 0);
                                                break;
                                        default:
-                                               append_strf(&databuf, ":%s 501 %s :Unknown MODE flag\n", hostname, client->nick);
+                                               copy_strf(&databuf, ":%s 501 %s :Unknown MODE flag\n", hostname, client->nick);
                                                send_log(client->FD, databuf.buf, databuf.len, 0);
                                        }
                                        p++;
@@ -196,17 +193,15 @@ int processPacket(struct client_data *client) {
                        return 0;
                for(int i=0; i<CLIENTS; i++)
                        if(clients[i] && clients[i]->nick[0]) {
-                               databuf.len = 0;
-                               append_strf(&databuf, ":%s 353 %s = %s :%s\n", hostname, client->nick, args[0], clients[i]->nick);
+                               copy_strf(&databuf, ":%s 353 %s = %s :%s\n", hostname, client->nick, args[0], clients[i]->nick);
                                send_log(client->FD, databuf.buf, databuf.len, 0);
                        }
-               databuf.len = 0;
-               append_strf(&databuf, ":%s 366 %s :End of /NAMES list\n", hostname, client->nick);
+               copy_strf(&databuf, ":%s 366 %s :End of /NAMES list\n", hostname, client->nick);
                send_log(client->FD, databuf.buf, databuf.len, 0);
        } else {
 unknown:
                // Inform user of error
-               append_strf(&databuf, ":%s 421 %s :Unknown command %s\n", hostname, client->nick, command);
+               copy_strf(&databuf, ":%s 421 %s :Unknown command %s\n", hostname, client->nick, command);
                send_log(client->FD, databuf.buf, databuf.len, 0);
                fflush(stdout);
        }
@@ -214,37 +209,27 @@ unknown:
 }
 
 void newUser(struct client_data *client) {
-       // Copy in basics
-       databuf.len = 0;
-       append_strf(&databuf, ":%s 001 %s :Welcome to PatchyIRCD!!!\n", hostname, client->nick);
+       copy_strf(&databuf, ":%s 001 %s :Welcome to PatchyIRCD!!!\n", hostname, client->nick);
        send_log(client->FD, databuf.buf, databuf.len, 0);
-       databuf.len = 0;
-       append_strf(&databuf, ":%s 002 %s :Your host is PatchyIRCD V0.0.1\n", hostname, client->nick);
+       copy_strf(&databuf, ":%s 002 %s :Your host is PatchyIRCD V0.0.1\n", hostname, client->nick);
        send_log(client->FD, databuf.buf, databuf.len, 0);
-       databuf.len = 0;
-       append_strf(&databuf, ":%s 003 %s :This server was created when I had cybersex with your mother.\n", hostname, client->nick);
+       copy_strf(&databuf, ":%s 003 %s :This server was created when I had cybersex with your mother.\n", hostname, client->nick);
        send_log(client->FD, databuf.buf, databuf.len, 0);
-       databuf.len = 0;
-       append_strf(&databuf, ":%s 251 %s :This server exists\n", hostname, client->nick);
+       copy_strf(&databuf, ":%s 251 %s :This server exists\n", hostname, client->nick);
        send_log(client->FD, databuf.buf, databuf.len, 0);
-       databuf.len = 0;
-       append_strf(&databuf, ":%s 252 %s :Jannies don't exist\n", hostname, client->nick);
+       copy_strf(&databuf, ":%s 252 %s :Jannies don't exist\n", hostname, client->nick);
        send_log(client->FD, databuf.buf, databuf.len, 0);
        sendMOTD(client);
 }
 
 void sendMOTD(struct client_data *client) {
-       databuf.len = 0;
-       append_strf(&databuf, ":%s 375 %s :PatchySICP /MOTD\n", hostname, client->nick);
+       copy_strf(&databuf, ":%s 375 %s :PatchySICP /MOTD\n", hostname, client->nick);
        send_log(client->FD, databuf.buf, databuf.len, 0);
-       databuf.len = 0;
-       append_strf(&databuf, ":%s 372 %s :Welcome to Montreal!\n", hostname, client->nick);
+       copy_strf(&databuf, ":%s 372 %s :Welcome to Montreal!\n", hostname, client->nick);
        send_log(client->FD, databuf.buf, databuf.len, 0);
-       databuf.len = 0;
-       append_strf(&databuf, ":%s 372 %s :You are retarded and I will have cybersex with your mom.\n", hostname, client->nick);
+       copy_strf(&databuf, ":%s 372 %s :You are retarded and I will have cybersex with your mom.\n", hostname, client->nick);
        send_log(client->FD, databuf.buf, databuf.len, 0);
-       databuf.len = 0;
-       append_strf(&databuf, ":%s 376 %s :/MOTD over\n", hostname, client->nick);
+       copy_strf(&databuf, ":%s 376 %s :/MOTD over\n", hostname, client->nick);
        send_log(client->FD, databuf.buf, databuf.len, 0);
 }
 
@@ -267,8 +252,7 @@ void setNick(struct client_data *client, char* nickname, size_t size) {
                if(clients[i] == 0) continue;
                if(strncmp(clients[i]->nick, nickname, size) == 0) {
                        // Nickname is already taken
-                       databuf.len = 0;
-                       append_strf(&databuf, ":%s 443 * %s :Nickname is already in use\n", hostname, nickname);
+                       copy_strf(&databuf, ":%s 443 * %s :Nickname is already in use\n", hostname, nickname);
                        send_log(client->FD, databuf.buf, databuf.len, 0);
                        return;
                }
@@ -277,8 +261,7 @@ void setNick(struct client_data *client, char* nickname, size_t size) {
 
        // Update nickname
        if(client->nickLen) {
-               databuf.len = 0;
-               append_strf(&databuf, ":%s NICK %s\n", client->nick, nickname);
+               copy_strf(&databuf, ":%s NICK %s\n", client->nick, nickname);
                send_log(client->FD, databuf.buf, databuf.len, 0);
                sendall_log(client->FD, databuf.buf, databuf.len, 0);
        }
diff --git a/util.h b/util.h
index c84345e..ab11d47 100644 (file)
--- a/util.h
+++ b/util.h
@@ -8,5 +8,6 @@ struct dynarr {
 
 void append_str(struct dynarr *d, const char *s);
 void append_strf(struct dynarr *d, const char *s, ...);
+#define copy_strf(d, ...) append_strf(((d)->len=0, (d)), __VA_ARGS__)
 void stringupper(char *s);
 void stringlower(char *s);