rearranged NeonServ code to be modular
[NeonServV5.git] / src / cmd_neonserv_myaccess.c
diff --git a/src/cmd_neonserv_myaccess.c b/src/cmd_neonserv_myaccess.c
deleted file mode 100644 (file)
index b9f6581..0000000
+++ /dev/null
@@ -1,190 +0,0 @@
-/* cmd_neonserv_myaccess.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] - nick / *auth
-*/
-static USERAUTH_CALLBACK(neonserv_cmd_myaccess_nick_lookup);
-static void neonserv_cmd_myaccess_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *nick, char *auth, char *chanmatch);
-
-struct neonserv_cmd_myaccess_cache {
-    struct ClientSocket *client, *textclient;
-    struct UserNode *user;
-    struct ChanNode *chan;
-    char *nick;
-    char *chanmatch;
-};
-
-CMD_BIND(neonserv_cmd_myaccess) {
-    char *chanmatch = NULL;
-    if(argc == 0 || argv[0][0] == '#') {
-        if(argc != 0) {
-            chanmatch = argv[0];
-        }
-        if(!(user->flags & USERFLAG_ISAUTHED)) {
-            struct neonserv_cmd_myaccess_cache *cache = malloc(sizeof(*cache));
-            if (!cache) {
-                perror("malloc() failed");
-                return;
-            }
-            cache->client = client;
-            cache->textclient = getTextBot();
-            cache->user = user;
-            cache->chan = chan;
-            cache->nick = strdup(argv[0]);
-            cache->chanmatch = (chanmatch ? strdup(chanmatch) : NULL);
-            get_userauth(user, neonserv_cmd_myaccess_nick_lookup, cache);
-        } else
-            neonserv_cmd_myaccess_async1(client, getTextBot(), user, chan, user->nick, user->auth, chanmatch);
-    }
-    else if(argv[0][0] == '*') {
-        //we've got an auth
-        if(argc > 1 && argv[1][0] == '#') {
-            chanmatch = argv[1];
-        }
-        argv[0]++;
-        neonserv_cmd_myaccess_async1(client, getTextBot(), user, chan, NULL, argv[0], chanmatch);
-    } else {
-        if(argc > 1 && argv[1][0] == '#') {
-            chanmatch = argv[1];
-        }
-        struct UserNode *cuser = getUserByNick(argv[0]);
-        if(!cuser) {
-            cuser = createTempUser(argv[0]);
-                       if(!cuser) {
-                reply(getTextBot(), user, "NS_USER_UNKNOWN", argv[0]);
-                return;
-            }
-            cuser->flags |= USERFLAG_ISTMPUSER;
-        }
-        if(cuser->flags & USERFLAG_ISAUTHED) {
-            neonserv_cmd_myaccess_async1(client, getTextBot(), user, chan, argv[0], cuser->auth, chanmatch);
-        } else {
-            struct neonserv_cmd_myaccess_cache *cache = malloc(sizeof(*cache));
-            if (!cache) {
-                perror("malloc() failed");
-                return;
-            }
-            cache->client = client;
-            cache->textclient = getTextBot();
-            cache->user = user;
-            cache->chan = chan;
-            cache->nick = strdup(argv[0]);
-            cache->chanmatch = (chanmatch ? strdup(chanmatch) : NULL);
-            get_userauth(cuser, neonserv_cmd_myaccess_nick_lookup, cache);
-        }
-    }
-}
-
-static USERAUTH_CALLBACK(neonserv_cmd_myaccess_nick_lookup) {
-    struct neonserv_cmd_myaccess_cache *cache = data;
-    if(!user) {
-        //USER_DOES_NOT_EXIST
-        reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
-    }
-    else if(!(user->flags & USERFLAG_ISAUTHED)) {
-        //USER_NOT_AUTHED
-        if(!strcmp(cache->nick, cache->user->nick))
-            reply(cache->textclient, cache->user, "NS_YOU_NEED_AUTH");
-        else
-            reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
-    }
-    else
-        neonserv_cmd_myaccess_async1(cache->client, cache->textclient, cache->user, cache->chan, user->nick, user->auth, cache->chanmatch);
-    if(cache->chanmatch)
-        free(cache->chanmatch);
-    free(cache->nick);
-    free(cache);
-}
-
-static void neonserv_cmd_myaccess_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *nick, char *auth, char *chanmatch) {
-    //we've got a valid auth now...
-    if(stricmp(user->auth, auth) && !isGodMode(user)) {
-        reply(textclient, user, "NS_MYACCESS_SELF_ONLY");
-        return;
-    }
-    MYSQL_RES *res, *default_res;
-    MYSQL_ROW user_row, chanuser_row, default_chan = NULL;
-    char flagBuf[5];
-    int userid, cflags, caccess, flagPos;
-    int i, total_count = 0, match_count = 0, owner_count = 0;
-    struct Table *table = NULL;
-    printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
-    res = mysql_use();
-    char *content[4];
-    content[0] = get_language_string(user, "NS_MYACCESS_HEADER_NAME");
-    content[1] = get_language_string(user, "NS_MYACCESS_HEADER_ACCESS");
-    content[2] = get_language_string(user, "NS_MYACCESS_HEADER_FLAGS");
-    content[3] = get_language_string(user, "NS_MYACCESS_HEADER_INFO");
-    if(chanmatch)
-        reply(textclient, user, "NS_MYACCESS_HEADER_MATCH", auth, chanmatch);
-    else
-        reply(textclient, user, "NS_MYACCESS_HEADER", auth);
-    if ((user_row = mysql_fetch_row(res)) != NULL) {
-        userid = atoi(user_row[0]);
-        //check if the user is already added
-        printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags`, `chanuser_infoline`, `channel_name`, `channel_getop`, `channel_getvoice`, `botid` FROM `chanusers` LEFT JOIN `channels` ON `chanuser_cid` = `channel_id` LEFT JOIN `bot_channels` ON `chanuser_cid` = `chanid` LEFT JOIN `bots` ON `bots`.`id` = `botid` WHERE `chanuser_uid` = '%d' AND `botclass` = '%d' AND `active` = '1' ORDER BY `chanuser_access` DESC, `channel_name` ASC", userid, client->botid);
-        res = mysql_use();
-        total_count = mysql_num_rows(res);
-        table = table_init(4, total_count + 1, 0);
-        table_add(table, content);
-        while ((chanuser_row = mysql_fetch_row(res)) != NULL) {
-            if(!strcmp(chanuser_row[0], "500")) owner_count++;
-            if(chanmatch && match(chanmatch, chanuser_row[0])) continue;
-            match_count++;
-            if((!chanuser_row[4] || !chanuser_row[5]) && !default_chan) {
-                printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'");
-                default_res = mysql_use();
-                default_chan = mysql_fetch_row(default_res);
-            }
-            flagPos = 0;
-            content[0] = chanuser_row[3];
-            content[1] = chanuser_row[0];
-            cflags = atoi(chanuser_row[1]);
-            caccess = atoi(chanuser_row[0]);
-            if((cflags & DB_CHANUSER_SUSPENDED))
-                flagPos += sprintf(flagBuf + flagPos, "s");
-            if(caccess >= (chanuser_row[4] ? atoi(chanuser_row[4]) : atoi(default_chan[0])))
-                flagPos += sprintf(flagBuf + flagPos, "o");
-            if(caccess >= (chanuser_row[5] ? atoi(chanuser_row[5]) : atoi(default_chan[1])))
-                flagPos += sprintf(flagBuf + flagPos, "v");
-            if((cflags & DB_CHANUSER_AUTOINVITE))
-                flagPos += sprintf(flagBuf + flagPos, "i");
-            content[2] = (flagPos ? flagBuf : "");
-            content[3] = chanuser_row[2];
-            table_add(table, content);
-        }
-    } else {
-        table = table_init(4, 1, 0);
-        table_add(table, content);
-    }
-    //send the table
-    char **table_lines = table_end(table);
-    for(i = 0; i < table->entrys; i++) {
-        reply(textclient, user, table_lines[i]);
-    }
-    if(!match_count)
-        reply(textclient, user, "NS_TABLE_NONE");
-    if(chanmatch) {
-        reply(textclient, user, "NS_MYACCESS_COUNT_MATCH", auth, total_count, owner_count, match_count, chanmatch);
-    } else {
-        reply(textclient, user, "NS_MYACCESS_COUNT", auth, total_count, owner_count);
-    }
-    table_free(table);
-}