1 /* cmd_global_setbot.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"
26 static int global_cmd_setbot_active(struct UserNode *user, MYSQL_ROW bot, char *value);
27 static int global_cmd_setbot_nick(struct UserNode *user, MYSQL_ROW bot, char *value);
28 static int global_cmd_setbot_ident(struct UserNode *user, MYSQL_ROW bot, char *value);
29 static int global_cmd_setbot_realname(struct UserNode *user, MYSQL_ROW bot, char *value);
30 static int global_cmd_setbot_server(struct UserNode *user, MYSQL_ROW bot, char *value);
31 static int global_cmd_setbot_port(struct UserNode *user, MYSQL_ROW bot, char *value);
32 static int global_cmd_setbot_bind(struct UserNode *user, MYSQL_ROW bot, char *value);
33 static int global_cmd_setbot_ssl(struct UserNode *user, MYSQL_ROW bot, char *value);
34 static int global_cmd_setbot_serverpass(struct UserNode *user, MYSQL_ROW bot, char *value);
35 static int global_cmd_setbot_class(struct UserNode *user, MYSQL_ROW bot, char *value);
36 static int global_cmd_setbot_queue(struct UserNode *user, MYSQL_ROW bot, char *value);
37 static int global_cmd_setbot_prefered(struct UserNode *user, MYSQL_ROW bot, char *value);
38 static int global_cmd_setbot_maxchan(struct UserNode *user, MYSQL_ROW bot, char *value);
39 static int global_cmd_setbot_priority(struct UserNode *user, MYSQL_ROW bot, char *value);
40 static int global_cmd_setbot_trigger(struct UserNode *user, MYSQL_ROW bot, char *value);
42 CMD_BIND(global_cmd_setbot) {
45 int botid = atoi(argv[0]);
46 printf_mysql_query("SELECT `active`, `nick`, `server`, `port`, `pass`, `botclass`, `textbot`, `queue`, `defaulttrigger`, `max_channels`, `register_priority`, `bind`, `ident`, `realname`, `ssl`, `id` FROM `bots` WHERE `id` = '%d'", botid);
48 if(!(row = mysql_fetch_row(res))) {
49 reply(getTextBot(), user, "NS_SETBOT_UNKNOWN", botid);
55 value = merge_argv(argv, 2, argc);
59 if(!stricmp(argv[1], "active")) log_event = global_cmd_setbot_active(user, row, value);
60 else if(!stricmp(argv[1], "nick")) log_event = global_cmd_setbot_nick(user, row, value);
61 else if(!stricmp(argv[1], "ident")) log_event = global_cmd_setbot_ident(user, row, value);
62 else if(!stricmp(argv[1], "realname")) log_event = global_cmd_setbot_realname(user, row, value);
63 else if(!stricmp(argv[1], "server")) log_event = global_cmd_setbot_server(user, row, value);
64 else if(!stricmp(argv[1], "port")) log_event = global_cmd_setbot_port(user, row, value);
65 else if(!stricmp(argv[1], "bind")) log_event = global_cmd_setbot_bind(user, row, value);
66 else if(!stricmp(argv[1], "ssl")) log_event = global_cmd_setbot_ssl(user, row, value);
67 else if(!stricmp(argv[1], "serverpass")) log_event = global_cmd_setbot_serverpass(user, row, value);
68 else if(!stricmp(argv[1], "botclass")) log_event = global_cmd_setbot_class(user, row, value);
69 else if(!stricmp(argv[1], "queue")) log_event = global_cmd_setbot_queue(user, row, value);
70 else if(!stricmp(argv[1], "prefered")) log_event = global_cmd_setbot_prefered(user, row, value);
71 else if(!stricmp(argv[1], "maxchan")) log_event = global_cmd_setbot_maxchan(user, row, value);
72 else if(!stricmp(argv[1], "priority")) log_event = global_cmd_setbot_priority(user, row, value);
73 else if(!stricmp(argv[1], "trigger")) log_event = global_cmd_setbot_trigger(user, row, value);
75 reply(getTextBot(), user, "NS_SETBOT_SETTING", argv[1]);
78 if(!stricmp(argv[1], "serverpass") && value) { //censor server password
79 char cmd_args[MAXLEN];
80 sprintf(cmd_args, "%d SERVERPASS ***", botid);
81 free(event->arguments);
82 event->arguments = strdup(cmd_args);
87 reply(getTextBot(), user, "NS_SETBOT_HEADER", botid);
88 global_cmd_setbot_active(user, row, NULL);
89 global_cmd_setbot_nick(user, row, NULL);
90 global_cmd_setbot_ident(user, row, NULL);
91 global_cmd_setbot_realname(user, row, NULL);
92 global_cmd_setbot_server(user, row, NULL);
93 global_cmd_setbot_port(user, row, NULL);
94 global_cmd_setbot_bind(user, row, NULL);
95 global_cmd_setbot_ssl(user, row, NULL);
96 global_cmd_setbot_serverpass(user, row, NULL);
97 global_cmd_setbot_class(user, row, NULL);
98 global_cmd_setbot_queue(user, row, NULL);
99 global_cmd_setbot_prefered(user, row, NULL);
100 global_cmd_setbot_maxchan(user, row, NULL);
101 global_cmd_setbot_priority(user, row, NULL);
102 global_cmd_setbot_trigger(user, row, NULL);
106 static int global_cmd_setbot_active(struct UserNode *user, MYSQL_ROW bot, char *value) {
107 int val = ((bot[0] && !strcmp(bot[0], "1")) ? 1 : 0);
110 if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
112 } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
115 reply(getTextBot(), user, "NS_SET_INVALID_BOOLEAN", value);
118 if(val != ((bot[0] && !strcmp(bot[0], "1")) ? 1 : 0)) {
121 struct ClientSocket *client;
122 client = create_socket(bot[2], atoi(bot[3]), bot[11], bot[4], bot[1], bot[12], bot[13]);
123 client->flags |= (strcmp(bot[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
124 client->flags |= (strcmp(bot[7], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
125 client->flags |= (strcmp(bot[14], "0") ? SOCKET_FLAG_SSL : 0);
126 client->botid = atoi(bot[5]);
127 client->clientid = atoi(bot[15]);
128 connect_socket(client);
129 if(client->botid == 0) {
132 printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '0' AND `botid` = '%d'", client->clientid);
134 while ((row = mysql_fetch_row(res)) != NULL) {
135 if(bind_botwise_cmd_to_command(0, client->clientid, row[0], row[1])) {
136 if(row[2] && strcmp(row[2], "")) {
137 bind_botwise_set_parameters(0, client->clientid, row[0], row[2]);
140 bind_botwise_set_global_access(0, client->clientid, row[0], atoi(row[3]));
143 bind_botwise_set_channel_access(0, client->clientid, row[0], row[4]);
147 bind_botwise_unbound_required_functions(0, client->clientid);
151 struct ClientSocket *client;
152 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
153 if(client->clientid == atoi(bot[15])) {
154 unbind_botwise_allcmd(client->clientid);
155 close_socket(client);
160 printf_mysql_query("UPDATE `bots` SET `active` = '%d' WHERE `id` = '%s'", val, bot[15]);
164 reply(getTextBot(), user, "\002ACTIVE \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
168 static int global_cmd_setbot_nick(struct UserNode *user, MYSQL_ROW bot, char *value) {
172 if(!is_valid_nick(value)) {
173 reply(getTextBot(), user, "NS_SETBOT_NICK_INVALID", value);
177 struct ClientSocket *client;
178 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
179 if(client->clientid == atoi(bot[15])) {
182 client->nick = strdup(value);
183 if(client->flags & SOCKET_FLAG_READY)
184 putsock(client, "NICK %s", value);
188 printf_mysql_query("UPDATE `bots` SET `nick` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
192 reply(getTextBot(), user, "\002NICK \002 %s", val);
196 static int global_cmd_setbot_ident(struct UserNode *user, MYSQL_ROW bot, char *value) {
200 if(strlen(value) > 12)
203 struct ClientSocket *client;
204 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
205 if(client->clientid == atoi(bot[15])) {
208 client->ident = strdup(value);
212 printf_mysql_query("UPDATE `bots` SET `ident` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
216 reply(getTextBot(), user, "\002IDENT \002 %s", val);
220 static int global_cmd_setbot_realname(struct UserNode *user, MYSQL_ROW bot, char *value) {
224 if(strlen(value) > 255)
227 struct ClientSocket *client;
228 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
229 if(client->clientid == atoi(bot[15])) {
232 client->ident = strdup(value);
236 printf_mysql_query("UPDATE `bots` SET `realname` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
240 reply(getTextBot(), user, "\002REALNAME \002 %s", val);
244 static int global_cmd_setbot_server(struct UserNode *user, MYSQL_ROW bot, char *value) {
248 struct ClientSocket *client;
249 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
250 if(client->clientid == atoi(bot[15])) {
253 client->host = strdup(value);
254 if(client->flags & SOCKET_FLAG_READY)
255 reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
259 printf_mysql_query("UPDATE `bots` SET `server` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
263 reply(getTextBot(), user, "\002SERVER \002 %s", val);
267 static int global_cmd_setbot_port(struct UserNode *user, MYSQL_ROW bot, char *value) {
268 int val = atoi(bot[3]);
272 if(val <= 0 || val > 65534) {
273 reply(getTextBot(), user, "NS_SETBOT_PORT_INVALID", value);
276 struct ClientSocket *client;
277 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
278 if(client->clientid == atoi(bot[15])) {
280 if(client->flags & SOCKET_FLAG_READY)
281 reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
285 printf_mysql_query("UPDATE `bots` SET `port` = '%d' WHERE `id` = '%s'", val, bot[15]);
288 reply(getTextBot(), user, "\002PORT \002 %d", val);
292 static int global_cmd_setbot_bind(struct UserNode *user, MYSQL_ROW bot, char *value) {
296 if(!strcmp(value, "*"))
298 struct ClientSocket *client;
299 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
300 if(client->clientid == atoi(bot[15])) {
303 client->bind = (value ? strdup(value) : NULL);
304 if(client->flags & SOCKET_FLAG_READY)
305 reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
310 printf_mysql_query("UPDATE `bots` SET `bind` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
312 printf_mysql_query("UPDATE `bots` SET `bind` = NULL WHERE `id` = '%s'", bot[15]);
316 reply(getTextBot(), user, "\002BIND \002 %s", val);
320 static int global_cmd_setbot_ssl(struct UserNode *user, MYSQL_ROW bot, char *value) {
321 int val = (strcmp(bot[14], "0") ? 1 : 0);
324 if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
326 } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
329 reply(getTextBot(), user, "NS_SET_INVALID_BOOLEAN", value);
332 struct ClientSocket *client;
333 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
334 if(client->clientid == atoi(bot[15])) {
336 client->flags |= SOCKET_FLAG_SSL;
338 client->flags &= ~SOCKET_FLAG_SSL;
339 if(client->flags & SOCKET_FLAG_READY)
340 reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
344 printf_mysql_query("UPDATE `bots` SET `ssl` = '%d' WHERE `id` = '%s'", val, bot[15]);
347 reply(getTextBot(), user, "\002SSL \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
351 static int global_cmd_setbot_serverpass(struct UserNode *user, MYSQL_ROW bot, char *value) {
355 if(!strcmp(value, "*"))
357 struct ClientSocket *client;
358 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
359 if(client->clientid == atoi(bot[15])) {
362 client->pass = (value ? strdup(value) : NULL);
363 if(client->flags & SOCKET_FLAG_READY)
364 reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
368 printf_mysql_query("UPDATE `bots` SET `pass` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
372 reply(getTextBot(), user, "\002SERVERPASS \002 %s", val);
376 static int global_cmd_setbot_class(struct UserNode *user, MYSQL_ROW bot, char *value) {
377 int val = atoi(bot[5]);
380 if((val = resolve_botalias(value)) == -1) {
381 reply(getTextBot(), user, "NS_SETBOT_INVALID_CLASS", value);
384 if(val != atoi(bot[5])) {
385 struct ClientSocket *client;
386 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
387 if(client->clientid == atoi(bot[15])) {
388 unbind_botwise_allcmd(client->clientid);
390 if(client->botid == 0) {
393 printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '0' AND `botid` = '%d'", client->clientid);
395 while ((row = mysql_fetch_row(res)) != NULL) {
396 if(bind_botwise_cmd_to_command(client->botid, client->clientid, row[0], row[1])) {
397 if(row[2] && strcmp(row[2], "")) {
398 bind_botwise_set_parameters(client->botid, client->clientid, row[0], row[2]);
401 bind_botwise_set_global_access(client->botid, client->clientid, row[0], atoi(row[3]));
404 bind_botwise_set_channel_access(client->botid, client->clientid, row[0], row[4]);
408 bind_botwise_unbound_required_functions(client->botid, client->clientid);
413 printf_mysql_query("UPDATE `bots` SET `botclass` = '%d' WHERE `id` = '%s'", val, bot[15]);
417 reply(getTextBot(), user, "\002BOTCLASS \002 %s", resolve_botid(val));
421 static int global_cmd_setbot_queue(struct UserNode *user, MYSQL_ROW bot, char *value) {
422 int val = (strcmp(bot[7], "0") ? 1 : 0);
425 if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
427 } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
430 reply(getTextBot(), user, "NS_SET_INVALID_BOOLEAN", value);
433 struct ClientSocket *client;
434 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
435 if(client->clientid == atoi(bot[15])) {
437 client->flags |= SOCKET_FLAG_USE_QUEUE;
439 client->flags &= ~SOCKET_FLAG_USE_QUEUE;
443 printf_mysql_query("UPDATE `bots` SET `queue` = '%d' WHERE `id` = '%s'", val, bot[15]);
446 reply(getTextBot(), user, "\002QUEUE \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
450 static int global_cmd_setbot_prefered(struct UserNode *user, MYSQL_ROW bot, char *value) {
451 int val = (strcmp(bot[6], "0") ? 1 : 0);
454 if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
456 } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
459 reply(getTextBot(), user, "NS_SET_INVALID_BOOLEAN", value);
462 struct ClientSocket *client;
463 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
464 if(client->clientid == atoi(bot[15])) {
466 client->flags |= SOCKET_FLAG_PREFERRED;
468 client->flags &= ~SOCKET_FLAG_PREFERRED;
472 printf_mysql_query("UPDATE `bots` SET `prefered` = '%d' WHERE `id` = '%s'", val, bot[15]);
475 reply(getTextBot(), user, "\002PREFERED \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
479 static int global_cmd_setbot_maxchan(struct UserNode *user, MYSQL_ROW bot, char *value) {
480 int val = atoi(bot[9]);
484 if(val < 0 || val > 99999) {
485 reply(getTextBot(), user, "NS_SETBOT_MAXCHAN_INVALID", value);
488 printf_mysql_query("UPDATE `bots` SET `max_channels` = '%d' WHERE `id` = '%s'", val, bot[15]);
491 reply(getTextBot(), user, "\002MAXCHAN \002 %d", val);
495 static int global_cmd_setbot_priority(struct UserNode *user, MYSQL_ROW bot, char *value) {
496 int val = atoi(bot[10]);
500 if(val < 0 || val > 99) {
501 reply(getTextBot(), user, "NS_SETBOT_PRIORITY_INVALID", value);
504 printf_mysql_query("UPDATE `bots` SET `register_priority` = '%d' WHERE `id` = '%s'", val, bot[15]);
507 reply(getTextBot(), user, "\002PRIORITY \002 %d", val);
511 static int global_cmd_setbot_trigger(struct UserNode *user, MYSQL_ROW bot, char *value) {
515 if(!*value || strlen(value) > 10) {
516 reply(getTextBot(), user, "NS_SETBOT_TRIGGER_INVALID", value);
519 printf_mysql_query("UPDATE `bots` SET `defaulttrigger` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
520 flush_trigger_cache(atoi(bot[5]), atoi(bot[15]));
521 reply(getTextBot(), user, "NS_SETBOT_TRIGGER_NOTE");
525 reply(getTextBot(), user, "\002TRIGGER \002 %s", val);