]> PatchyGit - patchyircd.git/commitdiff
limit NICK/QUIT to users that share a common channel
authorEgor <egor@patchysicp.xyz>
Fri, 6 May 2022 19:25:16 +0000 (22:25 +0300)
committerEgor <egor@patchysicp.xyz>
Fri, 6 May 2022 19:25:16 +0000 (22:25 +0300)
common.h
irc.c
net.c
net.h

index 0266b8f..72692bc 100644 (file)
--- a/common.h
+++ b/common.h
@@ -29,6 +29,7 @@ struct client_data {
        char   umode;
        char   regstate;
        struct trie_node *channels;
+       int    cookie;
 };
 struct channel {
        char *key;
diff --git a/irc.c b/irc.c
index c83f36a..4e20cc1 100644 (file)
--- a/irc.c
+++ b/irc.c
@@ -28,6 +28,32 @@ void sendbuf_chan_sans_self(struct client_data *client, struct channel *chan) {
                                sendbuf(cl);
                } while (trie_findnext(&f) == 1);
 }
+void sendbuf_visible(struct client_data *client) {
+       static int cookie = 0;
+       if (++cookie == 0) {
+               // handle overflow
+               struct trie_find *f = NULL;
+               if (trie_findfirst(&f, &client_map) == 1)
+                       do {
+                               struct client_data *cl = f->n->value;
+                               cl->cookie = 0;
+                       } while (trie_findnext(&f) == 1);
+               cookie++;
+       }
+       struct trie_find *f1 = NULL, *f2 = NULL;
+       if (trie_findfirst(&f1, &client->channels) == 1)
+               do {
+                       struct channel *chan = f1->n->value;
+                       if (trie_findfirst(&f2, &chan->members) == 1)
+                               do {
+                                       struct client_data *cl = f2->n->value;
+                                       if (cl->cookie != cookie) {
+                                               sendbuf(cl);
+                                               cl->cookie = cookie;
+                                       }
+                               } while (trie_findnext(&f2) == 1);
+               } while (trie_findnext(&f1) == 1);
+}
 int parsePacket(struct client_data *client) {
        char *source = NULL;
        char *command = NULL;
@@ -254,7 +280,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a
        // Start GitHub Copilot - this shit actually be copy pasting my code lmao
        } else if(strcmp(command, "QUIT") == 0) {
                msg_quit(client, "Quit: ", args[0] ? args[0] : "");
-               sendall_log(client->FD, databuf.buf, databuf.len, 0); // TODO: limit visibility
+               sendbuf_visible(client);
                killConnection(client, true);
                return 1;
        } else if(strcmp(command, "PONG") == 0) {
@@ -632,7 +658,7 @@ void setNick(struct client_data *client, char* nickname) {
        if(client->nick) {
                copy_strf(&databuf, ":%s!%s@%s NICK %s\n", client->nick, client->username, client->hostname, nickname);
                sendbuf(client);
-               sendall_log(client->FD, databuf.buf, databuf.len, 0); // TODO: limit visibility
+               sendbuf_visible(client);
                trie_set(&client_map, client->key, NULL, NULL);
                free(client->nick);
                free(client->key);
diff --git a/net.c b/net.c
index 15a9ffa..62613c6 100644 (file)
--- a/net.c
+++ b/net.c
@@ -3,18 +3,6 @@
 #include "ircfmt.h"
 #include <stdbool.h>
 
-void sendall_log(SOCKET socket, const char *buffer, size_t length, int flags) {
-       int i;
-       for(i=0;i<CLIENTS;i++) {
-               if(clients[i] == 0) continue;
-               if(clients[i]->FD == socket) {
-                       continue;
-               } else {
-                       send_log(clients[i]->FD, buffer, length, flags);
-               }
-       }
-}
-
 struct client_data *allocClient(){
        struct client_data *NewClient = malloc(sizeof(struct client_data));
        memset(NewClient, 0, sizeof(struct client_data));
@@ -54,7 +42,7 @@ void killConnection(struct client_data *cl, bool byChoice) {
        // Start GitHub Copilot
        if(!byChoice && cl->nick && cl->username) {
                msg_quit(cl, "", "Has left the building (Forced)");
-               sendall_log(cl->FD, databuf.buf, databuf.len, 0);
+               sendbuf_visible(cl);
        }
        // Send error to client informing them that they will be disconnected
        msg_error("Closing Link");
diff --git a/net.h b/net.h
index 5edc6d0..19c1ae1 100644 (file)
--- a/net.h
+++ b/net.h
@@ -3,8 +3,8 @@
 #include "common.h"
 #include <stdbool.h>
 
-void sendall_log(SOCKET socket, const char *buffer, size_t length, int flags);
 void sendbuf(struct client_data *client);
+void sendbuf_visible(struct client_data *client);
 struct client_data *allocClient();
 void killConnection(struct client_data *client, bool byChoice);
 void recvProc(struct client_data *cl);