rearranged NeonServ code to be modular
[NeonServV5.git] / src / cmd_neonserv_resync.c
diff --git a/src/cmd_neonserv_resync.c b/src/cmd_neonserv_resync.c
deleted file mode 100644 (file)
index 7c60a6e..0000000
+++ /dev/null
@@ -1,167 +0,0 @@
-/* cmd_neonserv_resync.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) usermask
-* argv[1] - (optional) min access
-* argv[2] - (optional) max access
-* argv[1/3] - (optional) FORCE (override NoAutoOp)
-*/
-static USERLIST_CALLBACK(neonserv_cmd_resync_userlist_lookup);
-static void neonserv_cmd_resync_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *usermask, int min_access, int max_access, char override_noautoop);
-
-struct neonserv_cmd_resync_cache {
-    struct ClientSocket *client, *textclient;
-    struct UserNode *user;
-    char *usermask;
-    int min_access;
-    int max_access;
-    char override_noautoop;
-};
-
-CMD_BIND(neonserv_cmd_resync) {
-    int min_access = 0, max_access = 500;
-    char *usermask = NULL;
-    char override_noautoop = 0;
-    if(argc > 0) {
-        usermask = argv[0];
-        if(argc > 2) {
-            min_access = atoi(argv[1]);
-            max_access = atoi(argv[2]);
-            if(argc > 3)
-                override_noautoop = (!stricmp(argv[3], "FORCE") ? 1 : 0);
-        } else if(argc > 1)
-            override_noautoop = (!stricmp(argv[1], "FORCE") ? 1 : 0);
-    }
-    struct neonserv_cmd_resync_cache *cache = malloc(sizeof(*cache));
-    if (!cache) {
-        perror("malloc() failed");
-        return;
-    }
-    cache->client = client;
-    cache->textclient = getTextBot();
-    cache->user = user;
-    cache->usermask = (usermask ? strdup(usermask) : NULL);
-    cache->min_access = min_access;
-    cache->max_access = max_access;
-    cache->override_noautoop = override_noautoop;
-    get_userlist_with_invisible(chan, neonserv_cmd_resync_userlist_lookup, cache);
-}
-
-static USERLIST_CALLBACK(neonserv_cmd_resync_userlist_lookup) {
-    struct neonserv_cmd_resync_cache *cache = data;
-    neonserv_cmd_resync_async1(cache->client, cache->textclient, cache->user, chan, cache->usermask, cache->min_access, cache->max_access, cache->override_noautoop);
-    if(cache->usermask)
-        free(cache->usermask);
-    free(cache);
-}
-
-static void neonserv_cmd_resync_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *usermask, int min_access, int max_access, char override_noautoop) {
-    MYSQL_RES *res;
-    MYSQL_ROW row, defaults = NULL;
-    int i;
-    int resync_op = 1;
-    int with_halfop = get_int_field("General.have_halfop");
-    int resync_halfop = with_halfop;
-    int resync_voice = 1;
-    if(usermask && usermask[0] == '@') {
-        resync_voice = 0;
-        resync_halfop = 0;
-        usermask++;
-        if(!*usermask) usermask = NULL;
-    } else if(usermask && with_halfop && usermask[0] == 'h') {
-        resync_op = 0;
-        resync_voice = 0;
-        usermask++;
-        if(!*usermask) usermask = NULL;
-    } else if(usermask && usermask[0] == '+') {
-        resync_op = 0;
-        resync_halfop = 0;
-        usermask++;
-        if(!*usermask) usermask = NULL;
-    }
-    struct ChanUser *chanuser;
-    int db_enfops, db_enfhalfop, db_enfvoice;
-    printf_mysql_query("SELECT `channel_getop`, `channel_getvoice`, `channel_gethalfop` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
-    row = mysql_fetch_row(mysql_use());
-    if(row[0] == NULL || row[1] == NULL) {
-        printf_mysql_query("SELECT `channel_getop`, `channel_getvoice`, `channel_gethalfop` FROM `channels` WHERE `channel_name` = 'defaults'");
-        defaults = mysql_fetch_row(mysql_use());
-    }
-    db_enfops = atoi((row[0] ? row[0] : defaults[0]));
-    db_enfvoice = atoi((row[1] ? row[1] : defaults[1]));
-    db_enfhalfop = (with_halfop ? atoi((row[2] ? row[2] : defaults[2])) : 0);
-    printf_mysql_query("SELECT `chanuser_access`, `user_user`, `chanuser_flags` FROM `chanusers` LEFT JOIN `users` ON `chanuser_uid` = `user_id` WHERE `chanuser_cid` = '%d' ORDER BY `chanuser_access` DESC, `user_user` ASC", chan->channel_id);
-    res = mysql_use();
-    char *db_users[mysql_num_rows(res)];
-    int db_access[mysql_num_rows(res)];
-    int db_flags[mysql_num_rows(res)];
-    int db_count = 0;
-    while ((row = mysql_fetch_row(res)) != NULL) {
-        db_users[db_count] = row[1];
-        db_access[db_count] = atoi(row[0]);
-        db_flags[db_count] = atoi(row[2]);
-        db_count++;
-    }
-    int caccess, cflags;
-    struct ModeBuffer *modeBuf;
-    modeBuf = initModeBuffer(client, chan);
-    for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
-        caccess = 0;
-        cflags = 0;
-        if((chanuser->user->flags & USERFLAG_ISAUTHED)) {
-            for(i = 0; i < db_count; i++) {
-                if(!stricmp(db_users[i], chanuser->user->auth)) {
-                    caccess = db_access[i];
-                    cflags = db_flags[i];
-                    if(cflags & DB_CHANUSER_SUSPENDED)
-                        caccess = 0;
-                    break;
-                }
-            }
-        }
-        if((usermask && *usermask && match(usermask, chanuser->user->nick)) || caccess < min_access || caccess > max_access) continue;
-        if(caccess >= db_enfops) {
-            if(!(chanuser->flags & CHANUSERFLAG_OPPED) && resync_op && (override_noautoop || !(cflags & DB_CHANUSER_NOAUTOOP)))
-                modeBufferOp(modeBuf, chanuser->user->nick);
-        } else if(with_halfop && caccess >= db_enfhalfop) {
-            if((chanuser->flags & CHANUSERFLAG_OPPED) && resync_op && !(chanuser->user->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP)))
-                modeBufferDeop(modeBuf, chanuser->user->nick);
-            if(!(chanuser->flags & CHANUSERFLAG_HALFOPPED) && resync_halfop && (override_noautoop || !(cflags & DB_CHANUSER_NOAUTOOP)))
-                modeBufferHalfop(modeBuf, chanuser->user->nick);
-        } else if(caccess >= db_enfvoice) {
-            if((chanuser->flags & CHANUSERFLAG_OPPED) && resync_op && !(chanuser->user->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP)))
-                modeBufferDeop(modeBuf, chanuser->user->nick);
-            if((chanuser->flags & CHANUSERFLAG_HALFOPPED) && resync_halfop && !(chanuser->user->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP)))
-                modeBufferDehalfop(modeBuf, chanuser->user->nick);
-            if(!(chanuser->flags & CHANUSERFLAG_VOICED) && resync_voice && (override_noautoop || !(cflags & DB_CHANUSER_NOAUTOOP)))
-                modeBufferVoice(modeBuf, chanuser->user->nick);
-        } else {
-            if((chanuser->flags & CHANUSERFLAG_OPPED) && resync_op && !(chanuser->user->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP)))
-                modeBufferDeop(modeBuf, chanuser->user->nick);
-            if((chanuser->flags & CHANUSERFLAG_HALFOPPED) && resync_halfop && !(chanuser->user->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP)))
-                modeBufferDehalfop(modeBuf, chanuser->user->nick);
-            if((chanuser->flags & CHANUSERFLAG_VOICED) && resync_voice && !(chanuser->user->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP)))
-                modeBufferDevoice(modeBuf, chanuser->user->nick);
-        }
-        
-    }
-    freeModeBuffer(modeBuf);
-    reply(textclient, user, "NS_RESYNC_DONE", chan->name);
-}