added cmd_commands and changed cmd_command to a global command
[NeonServV5.git] / src / cmd_global_commands.c
1 /* cmd_global_commands.c - NeonServ v5.2
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "cmd_global.h"
19
20 /*
21 * argv[0]    mask
22 */
23
24 static int global_cmd_commands_sort(const void *a, const void *b);
25 static int global_cmd_commands_chanaccess(struct cmd_binding *cbind, struct ChanNode *chan);
26 static int global_cmd_commands_operaccess(struct cmd_binding *cbind);
27
28 CMD_BIND(global_cmd_commands) {
29     struct cmd_binding *cbind;
30     int bindcount = 0;
31     for(cbind = getAllBinds(NULL); cbind; cbind = getAllBinds(cbind)) {
32         if(cbind->botid == client->botid)
33             bindcount++;
34     }
35     struct cmd_binding *binds[bindcount];
36     bindcount = 0;
37     for(cbind = getAllBinds(NULL); cbind; cbind = getAllBinds(cbind)) {
38         if(cbind->botid == client->botid)
39             binds[bindcount++] = cbind;
40     }
41     qsort(binds, bindcount, sizeof(struct cmd_binding *), global_cmd_commands_sort);
42     int i;
43     struct Table *table;
44     table = table_init(4, bindcount + 1, 0);
45     char *content[4];
46     content[0] = get_language_string(user, "NS_COMMANDS_NAME");
47     content[1] = get_language_string(user, "NS_COMMANDS_ACCESS");
48     content[2] = get_language_string(user, "NS_COMMANDS_GACCESS");
49     content[3] = get_language_string(user, "NS_COMMANDS_FUNCTION");
50     table_add(table, content);
51     char caccess[5];
52     char gaccess[5];
53     for(i = 0; i < bindcount; i++) {
54         cbind = binds[i];
55         content[0] = cbind->cmd;
56         sprintf(caccess, "%d", global_cmd_commands_chanaccess(cbind, chan));
57         content[1] = caccess;
58         sprintf(gaccess, "%d", global_cmd_commands_operaccess(cbind));
59         content[2] = gaccess;
60         content[3] = cbind->func->name;
61         table_add(table, content);
62     }
63     //send the table
64     char **table_lines = table_end(table);
65     for(i = 0; i < table->entrys; i++) {
66         reply(getTextBot(), user, table_lines[i]);
67     }
68     
69 }
70
71 static int global_cmd_commands_sort(const void *a, const void *b) {
72     const struct cmd_binding *bind_a = *((struct cmd_binding * const *) a);
73     const struct cmd_binding *bind_b = *((struct cmd_binding * const *) b); 
74     int i = stricmp(bind_a->func->name, bind_b->func->name);
75     if(i == 0) {
76         return stricmp(bind_a->cmd, bind_b->cmd);
77     } else
78         return i;
79 }
80
81 static int global_cmd_commands_chanaccess(struct cmd_binding *cbind, struct ChanNode *chan) {
82     char access_list[256];
83     int access_pos = 0;
84     int access_count = 0;
85     int minaccess = 0;
86     char *str_a, *str_b = cbind->func->channel_access, *str_c;
87     if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
88         str_b = cbind->channel_access;
89     access_list[0] = '\0';
90     if(str_b) {
91         str_c = strdup(str_b);
92         str_b = str_c;
93         while((str_a = str_b)) {
94             str_b = strstr(str_a, ",");
95             if(str_b) {
96                 *str_b = '\0';
97                 str_b++;
98             }
99             if(*str_a == '#') {
100                 str_a++;
101                 access_pos += sprintf(access_list+access_pos, (access_pos ? ", `%s`" : "`%s`"), str_a);
102                 access_count++;
103             } else {
104                if(atoi(str_a) > minaccess)
105                      minaccess = atoi(str_a);
106             }
107         }
108         free(str_c);
109     }
110     if(access_count) {
111         if(!chan) {
112             return -1;
113         }
114         MYSQL_RES *res;
115         MYSQL_ROW row, defaults = NULL;
116         printf_mysql_query("SELECT %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
117         res = mysql_use();
118         if ((row = mysql_fetch_row(res)) != NULL) {
119             int i, caccess;
120             for(i = 0; i < access_count; i++) {
121                 if(!row[i] && !defaults) {
122                     printf_mysql_query("SELECT %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
123                     defaults = mysql_fetch_row(mysql_use());
124                 }
125                 caccess = (row[i] ? atoi(row[i]) : atoi(defaults[i]));
126                 if(caccess > minaccess)
127                      minaccess = caccess;
128             }
129         }
130     }
131     return minaccess;
132 }
133
134 static int global_cmd_commands_operaccess(struct cmd_binding *cbind) {
135     return ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
136 }