added full half-op support
[NeonServV5.git] / src / cmd_neonserv_dehalfopall.c
diff --git a/src/cmd_neonserv_dehalfopall.c b/src/cmd_neonserv_dehalfopall.c
new file mode 100644 (file)
index 0000000..8dd8d61
--- /dev/null
@@ -0,0 +1,84 @@
+/* cmd_neonserv_dehalfopall.c - NeonServ v5.3
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+#include "cmd_neonserv.h"
+
+/*
+* argv[0]    (optional) nick mask
+*/
+static USERLIST_CALLBACK(neonserv_cmd_dehalfopall_userlist_lookup);
+static void neonserv_cmd_dehalfopall_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char **argv, int argc);
+
+struct neonserv_cmd_dehalfopall_cache {
+    struct ClientSocket *client, *textclient;
+    struct UserNode *user;
+    struct Event *event;
+    char **argv;
+    int argc;
+};
+
+CMD_BIND(neonserv_cmd_dehalfopall) {
+    struct neonserv_cmd_dehalfopall_cache *cache = malloc(sizeof(*cache));
+    if (!cache) {
+        perror("malloc() failed");
+        return;
+    }
+    cache->client = client;
+    cache->textclient = getTextBot();
+    cache->user = user;
+    cache->event = event;
+    cache->argv = calloc(argc, sizeof(char*));
+    int i;
+    for(i = 0; i < argc; i++) {
+        cache->argv[i] = strdup(argv[i]);
+    }
+    cache->argc = argc;
+    get_userlist(chan, neonserv_cmd_dehalfopall_userlist_lookup, cache);
+}
+
+static USERLIST_CALLBACK(neonserv_cmd_dehalfopall_userlist_lookup) {
+    struct neonserv_cmd_dehalfopall_cache *cache = data;
+    neonserv_cmd_dehalfopall_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->argv, cache->argc);
+    int i;
+    for(i = 0; i < cache->argc; i++) {
+        free(cache->argv[i]);
+    }
+    free(cache);
+}
+
+static void neonserv_cmd_dehalfopall_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char **argv, int argc) {
+    int issuer_access, victim_access, done_users = 0;
+    char *nickmask = NULL;
+    struct ChanUser *chanuser;
+    struct ModeBuffer *modeBuf;
+    if(argc > 0)
+        nickmask = argv[0];
+    modeBuf = initModeBuffer(client, chan);
+    issuer_access = getChannelAccess(user, chan);
+    for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
+        if(nickmask && match(nickmask, chanuser->user->nick)) continue;
+        victim_access = getChannelAccess(chanuser->user, chan);
+        if(victim_access >= issuer_access || isNetworkService(chanuser->user)) continue;
+        if(!(chanuser->flags & CHANUSERFLAG_OPPED)) continue;
+        modeBufferDehalfop(modeBuf, chanuser->user->nick);
+        done_users++;
+    }
+    freeModeBuffer(modeBuf);
+    reply(getTextBot(), user, "NS_DEHALFOPALL_DONE", done_users, chan->name);
+    if(done_users)
+        logEvent(event);
+}