10b2c4b4628888e71edcc58e9ec5dc7e294e2e3d
[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 && !(cbind->func->flags & CMDFLAG_FUNCMD))
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 && !(cbind->func->flags & CMDFLAG_FUNCMD))
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(5, bindcount + 1, 0);
45     char *content[5];
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_TRIGGERED");
50     content[4] = get_language_string(user, "NS_COMMANDS_FUNCTION");
51     table_add(table, content);
52     char caccess[5];
53     char gaccess[5];
54     char triggered[10];
55     for(i = 0; i < bindcount; i++) {
56         cbind = binds[i];
57         content[0] = cbind->cmd;
58         sprintf(caccess, "%d", global_cmd_commands_chanaccess(cbind, chan));
59         content[1] = caccess;
60         sprintf(gaccess, "%d", global_cmd_commands_operaccess(cbind));
61         content[2] = gaccess;
62         sprintf(triggered, "%d", cbind->func->triggered);
63         content[3] = triggered;
64         content[4] = cbind->func->name;
65         table_add(table, content);
66     }
67     //send the table
68     char **table_lines = table_end(table);
69     for(i = 0; i < table->entrys; i++) {
70         reply(getTextBot(), user, table_lines[i]);
71     }
72     
73 }
74
75 static int global_cmd_commands_sort(const void *a, const void *b) {
76     const struct cmd_binding *bind_a = *((struct cmd_binding * const *) a);
77     const struct cmd_binding *bind_b = *((struct cmd_binding * const *) b); 
78     int i = stricmp(bind_a->func->name, bind_b->func->name);
79     if(i == 0) {
80         return stricmp(bind_a->cmd, bind_b->cmd);
81     } else
82         return i;
83 }
84
85 static int global_cmd_commands_chanaccess(struct cmd_binding *cbind, struct ChanNode *chan) {
86     char access_list[256];
87     int access_pos = 0;
88     int access_count = 0;
89     int minaccess = 0;
90     char *str_a, *str_b = cbind->func->channel_access, *str_c;
91     if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
92         str_b = cbind->channel_access;
93     access_list[0] = '\0';
94     if(str_b) {
95         str_c = strdup(str_b);
96         str_b = str_c;
97         while((str_a = str_b)) {
98             str_b = strstr(str_a, ",");
99             if(str_b) {
100                 *str_b = '\0';
101                 str_b++;
102             }
103             if(*str_a == '#') {
104                 str_a++;
105                 access_pos += sprintf(access_list+access_pos, (access_pos ? ", `%s`" : "`%s`"), str_a);
106                 access_count++;
107             } else {
108                if(atoi(str_a) > minaccess)
109                      minaccess = atoi(str_a);
110             }
111         }
112         free(str_c);
113     }
114     if(access_count) {
115         if(!chan) {
116             return -1;
117         }
118         MYSQL_RES *res;
119         MYSQL_ROW row, defaults = NULL;
120         printf_mysql_query("SELECT %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
121         res = mysql_use();
122         if ((row = mysql_fetch_row(res)) != NULL) {
123             int i, caccess;
124             for(i = 0; i < access_count; i++) {
125                 if(!row[i] && !defaults) {
126                     printf_mysql_query("SELECT %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
127                     defaults = mysql_fetch_row(mysql_use());
128                 }
129                 caccess = (row[i] ? atoi(row[i]) : atoi(defaults[i]));
130                 if(caccess > minaccess)
131                      minaccess = caccess;
132             }
133         }
134     }
135     return minaccess;
136 }
137
138 static int global_cmd_commands_operaccess(struct cmd_binding *cbind) {
139     return ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
140 }