1 /* cmd_global_commands.c - NeonServ v5.2
2 * Copyright (C) 2011 Philipp Kreil (pk910)
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.
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.
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/>.
18 #include "cmd_global.h"
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);
28 CMD_BIND(global_cmd_commands) {
29 struct cmd_binding *cbind;
31 for(cbind = getAllBinds(NULL); cbind; cbind = getAllBinds(cbind)) {
32 if(cbind->botid == client->botid && !(cbind->func->flags & CMDFLAG_FUNCMD))
35 struct cmd_binding *binds[bindcount];
37 for(cbind = getAllBinds(NULL); cbind; cbind = getAllBinds(cbind)) {
38 if(cbind->botid == client->botid && !(cbind->func->flags & CMDFLAG_FUNCMD))
39 binds[bindcount++] = cbind;
41 qsort(binds, bindcount, sizeof(struct cmd_binding *), global_cmd_commands_sort);
44 table = table_init(4, bindcount + 1, 0);
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);
53 for(i = 0; i < bindcount; i++) {
55 content[0] = cbind->cmd;
56 sprintf(caccess, "%d", global_cmd_commands_chanaccess(cbind, chan));
58 sprintf(gaccess, "%d", global_cmd_commands_operaccess(cbind));
60 content[3] = cbind->func->name;
61 table_add(table, content);
64 char **table_lines = table_end(table);
65 for(i = 0; i < table->entrys; i++) {
66 reply(getTextBot(), user, table_lines[i]);
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);
76 return stricmp(bind_a->cmd, bind_b->cmd);
81 static int global_cmd_commands_chanaccess(struct cmd_binding *cbind, struct ChanNode *chan) {
82 char access_list[256];
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';
91 str_c = strdup(str_b);
93 while((str_a = str_b)) {
94 str_b = strstr(str_a, ",");
101 access_pos += sprintf(access_list+access_pos, (access_pos ? ", `%s`" : "`%s`"), str_a);
104 if(atoi(str_a) > minaccess)
105 minaccess = atoi(str_a);
115 MYSQL_ROW row, defaults = NULL;
116 printf_mysql_query("SELECT %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
118 if ((row = mysql_fetch_row(res)) != NULL) {
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());
125 caccess = (row[i] ? atoi(row[i]) : atoi(defaults[i]));
126 if(caccess > minaccess)
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);