Another year is about to end... So we have to update these damn copyright information :P
[NeonServV5.git] / src / cmd_global_commands.c
1 /* cmd_global_commands.c - NeonServ v5.3
2  * Copyright (C) 2011-2012  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->botid || cbind->clientid == client->clientid) && !(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->botid || cbind->clientid == client->clientid) && !(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     char funcname[MAXLEN];
56     int funcpos;
57     for(i = 0; i < bindcount; i++) {
58         cbind = binds[i];
59         content[0] = cbind->cmd;
60         sprintf(caccess, "%d", global_cmd_commands_chanaccess(cbind, chan));
61         content[1] = caccess;
62         sprintf(gaccess, "%d", global_cmd_commands_operaccess(cbind));
63         content[2] = gaccess;
64         sprintf(triggered, "%d", cbind->triggered);
65         content[3] = triggered;
66         funcpos = sprintf(funcname, "%s", cbind->func->name);
67         if(cbind->paramcount) {
68             int j;
69             for(j = 0; j < cbind->paramcount; j++) {
70                 funcpos += sprintf(funcname + funcpos, " %s", cbind->parameters[j]);
71             }
72         }
73         content[4] = funcname;
74         table_add(table, content);
75     }
76     //send the table
77     char **table_lines = table_end(table);
78     for(i = 0; i < table->entrys; i++) {
79         reply(getTextBot(), user, table_lines[i]);
80     }
81     
82 }
83
84 static int global_cmd_commands_sort(const void *a, const void *b) {
85     const struct cmd_binding *bind_a = *((struct cmd_binding * const *) a);
86     const struct cmd_binding *bind_b = *((struct cmd_binding * const *) b); 
87     int i = stricmp(bind_a->func->name, bind_b->func->name);
88     if(i == 0) {
89         return stricmp(bind_a->cmd, bind_b->cmd);
90     } else
91         return i;
92 }
93
94 static int global_cmd_commands_chanaccess(struct cmd_binding *cbind, struct ChanNode *chan) {
95     char access_list[256];
96     int access_pos = 0;
97     int access_count = 0;
98     int minaccess = 0;
99     char *str_a, *str_b = cbind->func->channel_access, *str_c;
100     if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
101         str_b = cbind->channel_access;
102     access_list[0] = '\0';
103     if(str_b) {
104         str_c = strdup(str_b);
105         str_b = str_c;
106         while((str_a = str_b)) {
107             str_b = strstr(str_a, ",");
108             if(str_b) {
109                 *str_b = '\0';
110                 str_b++;
111             }
112             if(*str_a == '#') {
113                 str_a++;
114                 access_pos += sprintf(access_list+access_pos, (access_pos ? ", `%s`" : "`%s`"), str_a);
115                 access_count++;
116             } else {
117                if(atoi(str_a) > minaccess)
118                      minaccess = atoi(str_a);
119             }
120         }
121         free(str_c);
122     }
123     if(access_count) {
124         if(!chan) {
125             return -1;
126         }
127         MYSQL_RES *res;
128         MYSQL_ROW row, defaults = NULL;
129         printf_mysql_query("SELECT %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
130         res = mysql_use();
131         if ((row = mysql_fetch_row(res)) != NULL) {
132             int i, caccess;
133             for(i = 0; i < access_count; i++) {
134                 if(!row[i] && !defaults) {
135                     printf_mysql_query("SELECT %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
136                     defaults = mysql_fetch_row(mysql_use());
137                 }
138                 caccess = (row[i] ? atoi(row[i]) : atoi(defaults[i]));
139                 if(caccess > minaccess)
140                      minaccess = caccess;
141             }
142         }
143     }
144     return minaccess;
145 }
146
147 static int global_cmd_commands_operaccess(struct cmd_binding *cbind) {
148     return ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
149 }