1 /* cmd_global_commands.c - NeonServ v5.3
2 * Copyright (C) 2011-2012 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->botid || cbind->clientid == client->clientid) && !(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->botid || cbind->clientid == client->clientid) && !(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(5, 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_TRIGGERED");
50 content[4] = get_language_string(user, "NS_COMMANDS_FUNCTION");
51 table_add(table, content);
55 char funcname[MAXLEN];
57 for(i = 0; i < bindcount; i++) {
59 content[0] = cbind->cmd;
60 sprintf(caccess, "%d", global_cmd_commands_chanaccess(cbind, chan));
62 sprintf(gaccess, "%d", global_cmd_commands_operaccess(cbind));
64 sprintf(triggered, "%d", cbind->triggered);
65 content[3] = triggered;
66 funcpos = sprintf(funcname, "%s", cbind->func->name);
67 if(cbind->paramcount) {
69 for(j = 0; j < cbind->paramcount; j++) {
70 funcpos += sprintf(funcname + funcpos, " %s", cbind->parameters[j]);
73 content[4] = funcname;
74 table_add(table, content);
77 char **table_lines = table_end(table);
78 for(i = 0; i < table->entrys; i++) {
79 reply(getTextBot(), user, table_lines[i]);
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);
89 return stricmp(bind_a->cmd, bind_b->cmd);
94 static int global_cmd_commands_chanaccess(struct cmd_binding *cbind, struct ChanNode *chan) {
95 char access_list[256];
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';
104 str_c = strdup(str_b);
106 while((str_a = str_b)) {
107 str_b = strstr(str_a, ",");
112 if(*str_a == '@' || *str_a == '+') {
113 //privs can override this access requirement
118 access_pos += sprintf(access_list+access_pos, (access_pos ? ", `%s`" : "`%s`"), str_a);
121 if(atoi(str_a) > minaccess)
122 minaccess = atoi(str_a);
132 MYSQL_ROW row, defaults = NULL;
133 printf_mysql_query("SELECT %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
135 if ((row = mysql_fetch_row(res)) != NULL) {
137 for(i = 0; i < access_count; i++) {
138 if(!row[i] && !defaults) {
139 printf_mysql_query("SELECT %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
140 defaults = mysql_fetch_row(mysql_use());
142 caccess = (row[i] ? atoi(row[i]) : atoi(defaults[i]));
143 if(caccess > minaccess)
151 static int global_cmd_commands_operaccess(struct cmd_binding *cbind) {
152 return ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);