1 /* cmd_global_modcmd.c - NeonServ v5.6
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"
26 static int global_cmd_modcmd_params(struct ClientSocket *textclient, struct UserNode *user, struct cmd_binding *cbind, char *value);
27 static int global_cmd_modcmd_flags(struct ClientSocket *textclient, struct UserNode *user, struct cmd_binding *cbind, char *value);
28 static int global_cmd_modcmd_caccess(struct ClientSocket *textclient, struct UserNode *user, struct cmd_binding *cbind, char *value);
29 static int global_cmd_modcmd_oaccess(struct ClientSocket *textclient, struct UserNode *user, struct cmd_binding *cbind, char *value);
31 CMD_BIND(global_cmd_modcmd) {
34 struct cmd_binding *cbind = find_botwise_cmd_binding(client->botid, client->clientid, argv[0]);
36 reply(textclient, user, "NS_UNBIND_NOT_FOUND", argv[0]);
40 printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
42 if ((row = mysql_fetch_row(res)) != NULL) {
43 uaccess = atoi(row[0]);
45 int gaccess = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
46 if(gaccess > uaccess) {
47 reply(textclient, user, "NS_MODCMD_OUTRANKED", cbind->cmd, gaccess);
53 value = merge_argv(argv, 2, argc);
57 if(!stricmp(argv[1], "caccess")) log_event = global_cmd_modcmd_caccess(textclient, user, cbind, value);
58 else if(!stricmp(argv[1], "oaccess")) log_event = global_cmd_modcmd_oaccess(textclient, user, cbind, value);
59 else if(!stricmp(argv[1], "parameters")) log_event = global_cmd_modcmd_params(textclient, user, cbind, value);
60 else if(!stricmp(argv[1], "flags")) log_event = global_cmd_modcmd_flags(textclient, user, cbind, value);
62 reply(textclient, user, "NS_MODCMD_SETTING", argv[1]);
68 reply(textclient, user, "NS_MODCMD_HEADER", cbind->cmd);
69 global_cmd_modcmd_params(textclient, user, cbind, NULL);
70 global_cmd_modcmd_caccess(textclient, user, cbind, NULL);
71 global_cmd_modcmd_oaccess(textclient, user, cbind, NULL);
72 global_cmd_modcmd_flags(textclient, user, cbind, NULL);
76 static int global_cmd_modcmd_params(struct ClientSocket *textclient, struct UserNode *user, struct cmd_binding *cbind, char *value) {
77 char parameters[MAXLEN];
79 if(cbind->paramcount) {
81 for(i = 0; i < cbind->paramcount; i++) {
82 parampos += sprintf(parameters + parampos, (i ? " %s" : "%s"), cbind->parameters[i]);
87 if(!strcmp(value, "*"))
89 bind_botwise_set_parameters(cbind->botid, cbind->clientid, cbind->cmd, value);
91 printf_mysql_query("UPDATE `bot_binds` SET `parameters` = '%s' WHERE `botclass` = '0' AND `botid` = '%d' AND `command` = '%s'", (value ? escape_string(value) : ""), cbind->clientid, escape_string(cbind->cmd));
93 printf_mysql_query("UPDATE `bot_binds` SET `parameters` = '%s' WHERE `botclass` = '%d' AND `command` = '%s'", (value ? escape_string(value) : ""), cbind->botid, escape_string(cbind->cmd));
94 strcpy(parameters, (value ? value : ""));
97 reply(textclient, user, "\002PARAMETERS \002 %s", parameters);
101 static const struct {
104 } global_cmd_modcmd_show_flags[] = {
105 {"REQUIRE_CHAN", CMDFLAG_REQUIRE_CHAN}, //The command requires a valid channel (at least one bot in it)
106 {"REQUIRE_AUTH", CMDFLAG_REQUIRE_AUTH}, //The command requires the user to be authenticated
107 {"REQUIRE_GOD", CMDFLAG_REQUIRE_GOD}, //The command requires the user to have security override enabled
108 {"REQUIRE_REGISTERED", CMDFLAG_REGISTERED_CHAN}, //The command requires the channel to be registered (database entry)
109 {"CHECK_AUTH", CMDFLAG_CHECK_AUTH}, //WHO the user if no auth is known, yet
110 {"CHANNEL_ARGS", CMDFLAG_CHAN_PARAM}, //don't interpret channel arguments as channel - just pass them as a string
111 {"LOG", CMDFLAG_LOG},
112 {"OPLOG", CMDFLAG_OPLOG},
113 {"FUNCMD", CMDFLAG_FUNCMD},
114 {"ESCAPED_ARGS", CMDFLAG_ESCAPE_ARGS}, //allows arguments to be escaped ("a\ b" = "a b" as one argument)
115 {"NO_CROSSCHAN", CMDFLAG_NO_CROSSCHAN},
116 {"SUB_LINKER", CMDFLAG_SUB_LINKER}, //adds a "quiet" subcommand linker with the binding function as default
120 static int global_cmd_modcmd_flags(struct ClientSocket *textclient, struct UserNode *user, struct cmd_binding *cbind, char *value) {
124 unsigned int visible_flags = 0;
126 while(global_cmd_modcmd_show_flags[i].name) {
127 if(cbind->func->flags & global_cmd_modcmd_show_flags[i].flag) {
128 flagpos += sprintf(flags + flagpos, (flagpos ? " %s" : "\00314%s"), global_cmd_modcmd_show_flags[i].name);
130 visible_flags |= global_cmd_modcmd_show_flags[i].flag;
134 flags[flagpos++] = '\003';
137 unsigned int current_flag = 0;
138 if(value[0] == '+') {
140 } else if(value[0] == '-') {
146 while(global_cmd_modcmd_show_flags[i].name) {
147 if(!stricmp(global_cmd_modcmd_show_flags[i].name, value)) {
148 current_flag = global_cmd_modcmd_show_flags[i].flag;
154 if(cbind->func->flags & current_flag) {
155 reply(textclient, user, "NS_MODCMD_STATIC_FLAG");
159 cbind->flags |= current_flag;
161 cbind->flags &= ~current_flag;
162 if(cbind->botid == 0)
163 printf_mysql_query("UPDATE `bot_binds` SET `flags` = '%u' WHERE `botclass` = '0' AND `botid` = '%d' AND `command` = '%s'", (cbind->flags & visible_flags), cbind->clientid, escape_string(cbind->cmd));
165 printf_mysql_query("UPDATE `bot_binds` SET `flags` = '%u' WHERE `botclass` = '%d' AND `command` = '%s'", (cbind->flags & visible_flags), cbind->botid, escape_string(cbind->cmd));
169 while(global_cmd_modcmd_show_flags[i].name) {
170 if(cbind->flags & global_cmd_modcmd_show_flags[i].flag) {
171 flagpos += sprintf(flags + flagpos, (flagpos ? " %s" : "%s"), global_cmd_modcmd_show_flags[i].name);
175 flags[flagpos] = '\0';
176 reply(textclient, user, "\002FLAGS \002 %s", flags);
180 static int global_cmd_modcmd_caccess(struct ClientSocket *textclient, struct UserNode *user, struct cmd_binding *cbind, char *value) {
181 char caccess[MAXLEN];
184 if(!strcmp(value, "*"))
186 bind_botwise_set_channel_access(cbind->botid, cbind->clientid, cbind->cmd, value);
187 if(cbind->botid == 0)
188 printf_mysql_query("UPDATE `bot_binds` SET `chan_access` = %s%s%s WHERE `botclass` = '0' AND `botid` = '%d' AND `command` = '%s'", (value ? "'" : ""), (value ? escape_string(value) : "NULL"), (value ? "'" : ""), cbind->clientid, escape_string(cbind->cmd));
190 printf_mysql_query("UPDATE `bot_binds` SET `chan_access` = %s%s%s WHERE `botclass` = '%d' AND `command` = '%s'", (value ? "'" : ""), (value ? escape_string(value) : "NULL"), (value ? "'" : ""), cbind->botid, escape_string(cbind->cmd));
194 if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
195 sprintf(caccess, "%s", cbind->channel_access);
197 sprintf(caccess, "\00314%s\003", (cbind->func->channel_access ? cbind->func->channel_access : "0"));
198 reply(textclient, user, "\002CACCESS \002 %s", caccess);
202 static int global_cmd_modcmd_oaccess(struct ClientSocket *textclient, struct UserNode *user, struct cmd_binding *cbind, char *value) {
203 char oaccess[MAXLEN];
206 if(!strcmp(value, "*"))
208 bind_botwise_set_global_access(cbind->botid, cbind->clientid, cbind->cmd, atoi(value));
209 if(cbind->botid == 0)
210 printf_mysql_query("UPDATE `bot_binds` SET `global_access` = %s%s%s WHERE `botclass` = '0' AND `botid` = '%d' AND `command` = '%s'", (value ? "'" : ""), (value ? escape_string(value) : "NULL"), (value ? "'" : ""), cbind->clientid, escape_string(cbind->cmd));
212 printf_mysql_query("UPDATE `bot_binds` SET `global_access` = %s%s%s WHERE `botclass` = '%d' AND `command` = '%s'", (value ? "'" : ""), (value ? escape_string(value) : "NULL"), (value ? "'" : ""), cbind->botid, escape_string(cbind->cmd));
216 if(cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS)
217 sprintf(oaccess, "%d", cbind->global_access);
219 sprintf(oaccess, "\00314%d\003", (cbind->func->global_access ? cbind->func->global_access : 0));
220 reply(textclient, user, "\002OACCESS \002 %s", oaccess);