tried to reorder the program structure and build process
[NeonServV5.git] / src / cmd_neonserv_ban.c
diff --git a/src/cmd_neonserv_ban.c b/src/cmd_neonserv_ban.c
new file mode 100644 (file)
index 0000000..eec7326
--- /dev/null
@@ -0,0 +1,101 @@
+
+#include "cmd_neonserv.h"
+
+/*
+* argv[0-*]    nick[,*auth[,*!*@mask[...]]]
+*/
+static USERLIST_CALLBACK(neonserv_cmd_ban_userlist_lookup);
+static void neonserv_cmd_ban_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *masks);
+
+struct neonserv_cmd_ban_cache {
+    struct ClientSocket *client, *textclient;
+    struct UserNode *user;
+    struct Event *event;
+    char *masks;
+};
+
+CMD_BIND(neonserv_cmd_ban) {
+    struct neonserv_cmd_ban_cache *cache = malloc(sizeof(*cache));
+    if (!cache) {
+        perror("malloc() failed");
+        return;
+    }
+    cache->client = client;
+    cache->textclient = getTextBot();
+    cache->user = user;
+    cache->event = event;
+    cache->masks = strdup(merge_argv_char(argv, 0, argc, ','));
+    get_userlist_with_invisible(chan, neonserv_cmd_ban_userlist_lookup, cache);
+}
+
+static USERLIST_CALLBACK(neonserv_cmd_ban_userlist_lookup) {
+    struct neonserv_cmd_ban_cache *cache = data;
+    neonserv_cmd_ban_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->masks);
+    free(cache->masks);
+    free(cache);
+}
+
+static void neonserv_cmd_ban_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *masks) {
+    int done_masks = 0, provided_masks = 0, skip, match_count, total_match;
+    char *mask, *nextmask;
+    char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
+    char usermask[NICKLEN+USERLEN+HOSTLEN+3];
+    struct UserNode *cuser;
+    struct ChanUser *chanuser;
+    struct ModeBuffer *modeBuf;
+    modeBuf = initModeBuffer(client, chan);
+    nextmask = masks;
+    while((mask = nextmask)) {
+        nextmask = strstr(mask, ",");
+        if(nextmask) {
+            *nextmask = '\0';
+            nextmask++;
+        }
+        provided_masks++;
+        skip = 0;
+        match_count = 0;
+        mask = make_banmask(mask, hostmask_buffer);
+        for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
+            cuser = chanuser->user;
+            sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
+            if(!match(mask, usermask)) {
+                cuser->flags |= USERFLAG_SCRIPTFLAG1; //we mark the user as 'matching'
+                if(isNetworkService(chanuser->user)) {
+                    reply(textclient, user, "NS_SERVICE_IMMUNE", chanuser->user->nick);
+                    skip = 1;
+                    break;
+                }
+                if(isUserProtected(chan, cuser, user)) {
+                    reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
+                    skip = 1;
+                    break;
+                }
+                match_count++;
+                if(match_count > 4 && (match_count * 3) > chan->usercount && !isGodMode(user)) {
+                    skip = 1;
+                    reply(textclient, user, "NS_LAME_MASK", mask);
+                    break;
+                }
+            }
+        }
+        if(!skip) {
+            done_masks++;
+            modeBufferBan(modeBuf, mask);
+        }
+    }
+    total_match = 0; // count all users marked as 'matching'
+    for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
+        cuser = chanuser->user;
+        if(cuser->flags & USERFLAG_SCRIPTFLAG1) {
+            cuser->flags &= ~USERFLAG_SCRIPTFLAG1;
+            total_match++;
+        }
+    }
+    freeModeBuffer(modeBuf);
+    if(done_masks == provided_masks)
+        reply(getTextBot(), user, "NS_BAN_DONE", done_masks, chan->name, total_match);
+    else
+        reply(getTextBot(), user, "NS_BAN_FAIL", client->user->nick);
+    if(done_masks)
+        logEvent(event);
+}