added cmd_commands and changed cmd_command to a global command
[NeonServV5.git] / src / cmd_global_commands.c
diff --git a/src/cmd_global_commands.c b/src/cmd_global_commands.c
new file mode 100644 (file)
index 0000000..53f90bd
--- /dev/null
@@ -0,0 +1,136 @@
+/* cmd_global_commands.c - NeonServ v5.2
+ * Copyright (C) 2011  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_global.h"
+
+/*
+* argv[0]    mask
+*/
+
+static int global_cmd_commands_sort(const void *a, const void *b);
+static int global_cmd_commands_chanaccess(struct cmd_binding *cbind, struct ChanNode *chan);
+static int global_cmd_commands_operaccess(struct cmd_binding *cbind);
+
+CMD_BIND(global_cmd_commands) {
+    struct cmd_binding *cbind;
+    int bindcount = 0;
+    for(cbind = getAllBinds(NULL); cbind; cbind = getAllBinds(cbind)) {
+        if(cbind->botid == client->botid)
+            bindcount++;
+    }
+    struct cmd_binding *binds[bindcount];
+    bindcount = 0;
+    for(cbind = getAllBinds(NULL); cbind; cbind = getAllBinds(cbind)) {
+        if(cbind->botid == client->botid)
+            binds[bindcount++] = cbind;
+    }
+    qsort(binds, bindcount, sizeof(struct cmd_binding *), global_cmd_commands_sort);
+    int i;
+    struct Table *table;
+    table = table_init(4, bindcount + 1, 0);
+    char *content[4];
+    content[0] = get_language_string(user, "NS_COMMANDS_NAME");
+    content[1] = get_language_string(user, "NS_COMMANDS_ACCESS");
+    content[2] = get_language_string(user, "NS_COMMANDS_GACCESS");
+    content[3] = get_language_string(user, "NS_COMMANDS_FUNCTION");
+    table_add(table, content);
+    char caccess[5];
+    char gaccess[5];
+    for(i = 0; i < bindcount; i++) {
+        cbind = binds[i];
+        content[0] = cbind->cmd;
+        sprintf(caccess, "%d", global_cmd_commands_chanaccess(cbind, chan));
+        content[1] = caccess;
+        sprintf(gaccess, "%d", global_cmd_commands_operaccess(cbind));
+        content[2] = gaccess;
+        content[3] = cbind->func->name;
+        table_add(table, content);
+    }
+    //send the table
+    char **table_lines = table_end(table);
+    for(i = 0; i < table->entrys; i++) {
+        reply(getTextBot(), user, table_lines[i]);
+    }
+    
+}
+
+static int global_cmd_commands_sort(const void *a, const void *b) {
+    const struct cmd_binding *bind_a = *((struct cmd_binding * const *) a);
+    const struct cmd_binding *bind_b = *((struct cmd_binding * const *) b); 
+    int i = stricmp(bind_a->func->name, bind_b->func->name);
+    if(i == 0) {
+        return stricmp(bind_a->cmd, bind_b->cmd);
+    } else
+        return i;
+}
+
+static int global_cmd_commands_chanaccess(struct cmd_binding *cbind, struct ChanNode *chan) {
+    char access_list[256];
+    int access_pos = 0;
+    int access_count = 0;
+    int minaccess = 0;
+    char *str_a, *str_b = cbind->func->channel_access, *str_c;
+    if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
+        str_b = cbind->channel_access;
+    access_list[0] = '\0';
+    if(str_b) {
+        str_c = strdup(str_b);
+        str_b = str_c;
+        while((str_a = str_b)) {
+            str_b = strstr(str_a, ",");
+            if(str_b) {
+                *str_b = '\0';
+                str_b++;
+            }
+            if(*str_a == '#') {
+                str_a++;
+                access_pos += sprintf(access_list+access_pos, (access_pos ? ", `%s`" : "`%s`"), str_a);
+                access_count++;
+            } else {
+               if(atoi(str_a) > minaccess)
+                     minaccess = atoi(str_a);
+            }
+        }
+        free(str_c);
+    }
+    if(access_count) {
+        if(!chan) {
+            return -1;
+        }
+        MYSQL_RES *res;
+        MYSQL_ROW row, defaults = NULL;
+        printf_mysql_query("SELECT %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
+        res = mysql_use();
+        if ((row = mysql_fetch_row(res)) != NULL) {
+            int i, caccess;
+            for(i = 0; i < access_count; i++) {
+                if(!row[i] && !defaults) {
+                    printf_mysql_query("SELECT %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
+                    defaults = mysql_fetch_row(mysql_use());
+                }
+                caccess = (row[i] ? atoi(row[i]) : atoi(defaults[i]));
+                if(caccess > minaccess)
+                     minaccess = caccess;
+            }
+        }
+    }
+    return minaccess;
+}
+
+static int global_cmd_commands_operaccess(struct cmd_binding *cbind) {
+    return ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
+}