1 /* cmd_global_setbot.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_setbot_active(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
27 static int global_cmd_setbot_nick(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
28 static int global_cmd_setbot_ident(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
29 static int global_cmd_setbot_realname(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
30 static int global_cmd_setbot_server(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
31 static int global_cmd_setbot_port(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
32 static int global_cmd_setbot_bind(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
33 static int global_cmd_setbot_ssl(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
34 static int global_cmd_setbot_serverpass(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
35 static int global_cmd_setbot_class(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
36 static int global_cmd_setbot_queue(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
37 static int global_cmd_setbot_prefered(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
38 static int global_cmd_setbot_secret(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
39 static int global_cmd_setbot_maxchan(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
40 static int global_cmd_setbot_priority(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
41 static int global_cmd_setbot_trigger(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value);
43 CMD_BIND(global_cmd_setbot) {
46 int botid = atoi(argv[0]);
47 printf_mysql_query("SELECT `active`, `nick`, `server`, `port`, `pass`, `botclass`, `textbot`, `queue`, `defaulttrigger`, `max_channels`, `register_priority`, `bind`, `ident`, `realname`, `ssl`, `id`, `secret` FROM `bots` WHERE `id` = '%d'", botid);
49 if(!(row = mysql_fetch_row(res))) {
50 reply(textclient, user, "NS_SETBOT_UNKNOWN", botid);
56 value = merge_argv(argv, 2, argc);
60 if(!stricmp(argv[1], "active")) log_event = global_cmd_setbot_active(textclient, user, row, value);
61 else if(!stricmp(argv[1], "nick")) log_event = global_cmd_setbot_nick(textclient, user, row, value);
62 else if(!stricmp(argv[1], "ident")) log_event = global_cmd_setbot_ident(textclient, user, row, value);
63 else if(!stricmp(argv[1], "realname")) log_event = global_cmd_setbot_realname(textclient, user, row, value);
64 else if(!stricmp(argv[1], "server")) log_event = global_cmd_setbot_server(textclient, user, row, value);
65 else if(!stricmp(argv[1], "port")) log_event = global_cmd_setbot_port(textclient, user, row, value);
66 else if(!stricmp(argv[1], "bind")) log_event = global_cmd_setbot_bind(textclient, user, row, value);
67 else if(!stricmp(argv[1], "ssl")) log_event = global_cmd_setbot_ssl(textclient, user, row, value);
68 else if(!stricmp(argv[1], "serverpass")) log_event = global_cmd_setbot_serverpass(textclient, user, row, value);
69 else if(!stricmp(argv[1], "botclass")) log_event = global_cmd_setbot_class(textclient, user, row, value);
70 else if(!stricmp(argv[1], "queue")) log_event = global_cmd_setbot_queue(textclient, user, row, value);
71 else if(!stricmp(argv[1], "prefered")) log_event = global_cmd_setbot_prefered(textclient, user, row, value);
72 else if(!stricmp(argv[1], "secret")) log_event = global_cmd_setbot_secret(textclient, user, row, value);
73 else if(!stricmp(argv[1], "maxchan")) log_event = global_cmd_setbot_maxchan(textclient, user, row, value);
74 else if(!stricmp(argv[1], "priority")) log_event = global_cmd_setbot_priority(textclient, user, row, value);
75 else if(!stricmp(argv[1], "trigger")) log_event = global_cmd_setbot_trigger(textclient, user, row, value);
77 reply(textclient, user, "NS_SETBOT_SETTING", argv[1]);
80 if(!stricmp(argv[1], "serverpass") && value) { //censor server password
81 char cmd_args[MAXLEN];
82 sprintf(cmd_args, "%d SERVERPASS ***", botid);
83 free(event->arguments);
84 event->arguments = strdup(cmd_args);
89 reply(textclient, user, "NS_SETBOT_HEADER", botid);
90 global_cmd_setbot_active(textclient, user, row, NULL);
91 global_cmd_setbot_nick(textclient, user, row, NULL);
92 global_cmd_setbot_ident(textclient, user, row, NULL);
93 global_cmd_setbot_realname(textclient, user, row, NULL);
94 global_cmd_setbot_server(textclient, user, row, NULL);
95 global_cmd_setbot_port(textclient, user, row, NULL);
96 global_cmd_setbot_bind(textclient, user, row, NULL);
97 global_cmd_setbot_ssl(textclient, user, row, NULL);
98 global_cmd_setbot_serverpass(textclient, user, row, NULL);
99 global_cmd_setbot_class(textclient, user, row, NULL);
100 global_cmd_setbot_queue(textclient, user, row, NULL);
101 global_cmd_setbot_prefered(textclient, user, row, NULL);
102 global_cmd_setbot_secret(textclient, user, row, NULL);
103 global_cmd_setbot_maxchan(textclient, user, row, NULL);
104 global_cmd_setbot_priority(textclient, user, row, NULL);
105 global_cmd_setbot_trigger(textclient, user, row, NULL);
109 static int global_cmd_setbot_active(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
110 int val = ((bot[0] && !strcmp(bot[0], "1")) ? 1 : 0);
113 if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
115 } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
118 reply(textclient, user, "NS_SET_INVALID_BOOLEAN", value);
121 if(val != ((bot[0] && !strcmp(bot[0], "1")) ? 1 : 0)) {
124 struct ClientSocket *client;
125 client = create_socket(bot[2], atoi(bot[3]), bot[11], bot[4], bot[1], bot[12], bot[13]);
126 client->flags |= (strcmp(bot[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
127 client->flags |= (strcmp(bot[7], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
128 client->flags |= (strcmp(bot[14], "0") ? SOCKET_FLAG_SSL : 0);
129 client->botid = atoi(bot[5]);
130 client->clientid = atoi(bot[15]);
131 connect_socket(client);
132 if(client->botid == 0) {
135 printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '0' AND `botid` = '%d'", client->clientid);
137 while ((row = mysql_fetch_row(res)) != NULL) {
138 if(bind_botwise_cmd_to_command(0, client->clientid, row[0], row[1])) {
139 if(row[2] && strcmp(row[2], "")) {
140 bind_botwise_set_parameters(0, client->clientid, row[0], row[2]);
143 bind_botwise_set_global_access(0, client->clientid, row[0], atoi(row[3]));
146 bind_botwise_set_channel_access(0, client->clientid, row[0], row[4]);
150 bind_botwise_unbound_required_functions(0, client->clientid);
154 struct ClientSocket *client;
155 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
156 if(client->clientid == atoi(bot[15])) {
157 unbind_botwise_allcmd(0, client->clientid);
158 close_socket(client);
163 printf_mysql_query("UPDATE `bots` SET `active` = '%d' WHERE `id` = '%s'", val, bot[15]);
167 reply(textclient, user, "\002ACTIVE \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
171 static int global_cmd_setbot_nick(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
175 if(!is_valid_nick(value)) {
176 reply(textclient, user, "NS_SETBOT_NICK_INVALID", value);
180 struct ClientSocket *client;
181 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
182 if(client->clientid == atoi(bot[15])) {
185 client->nick = strdup(value);
186 if(client->flags & SOCKET_FLAG_READY)
187 putsock(client, "NICK %s", value);
191 printf_mysql_query("UPDATE `bots` SET `nick` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
195 reply(textclient, user, "\002NICK \002 %s", val);
199 static int global_cmd_setbot_ident(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
203 if(strlen(value) > 12)
206 struct ClientSocket *client;
207 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
208 if(client->clientid == atoi(bot[15])) {
211 client->ident = strdup(value);
215 printf_mysql_query("UPDATE `bots` SET `ident` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
219 reply(textclient, user, "\002IDENT \002 %s", val);
223 static int global_cmd_setbot_realname(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
227 if(strlen(value) > 255)
230 struct ClientSocket *client;
231 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
232 if(client->clientid == atoi(bot[15])) {
235 client->ident = strdup(value);
239 printf_mysql_query("UPDATE `bots` SET `realname` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
243 reply(textclient, user, "\002REALNAME \002 %s", val);
247 static int global_cmd_setbot_server(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
251 struct ClientSocket *client;
252 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
253 if(client->clientid == atoi(bot[15])) {
256 client->host = strdup(value);
257 if(client->flags & SOCKET_FLAG_READY)
258 reply(textclient, user, "NS_SETBOT_NEED_RESTART");
262 printf_mysql_query("UPDATE `bots` SET `server` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
266 reply(textclient, user, "\002SERVER \002 %s", val);
270 static int global_cmd_setbot_port(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
271 int val = atoi(bot[3]);
275 if(val <= 0 || val > 65534) {
276 reply(textclient, user, "NS_SETBOT_PORT_INVALID", value);
279 struct ClientSocket *client;
280 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
281 if(client->clientid == atoi(bot[15])) {
283 if(client->flags & SOCKET_FLAG_READY)
284 reply(textclient, user, "NS_SETBOT_NEED_RESTART");
288 printf_mysql_query("UPDATE `bots` SET `port` = '%d' WHERE `id` = '%s'", val, bot[15]);
291 reply(textclient, user, "\002PORT \002 %d", val);
295 static int global_cmd_setbot_bind(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
299 if(!strcmp(value, "*"))
301 struct ClientSocket *client;
302 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
303 if(client->clientid == atoi(bot[15])) {
306 client->bind = (value ? strdup(value) : NULL);
307 if(client->flags & SOCKET_FLAG_READY)
308 reply(textclient, user, "NS_SETBOT_NEED_RESTART");
313 printf_mysql_query("UPDATE `bots` SET `bind` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
315 printf_mysql_query("UPDATE `bots` SET `bind` = NULL WHERE `id` = '%s'", bot[15]);
319 reply(textclient, user, "\002BIND \002 %s", val);
323 static int global_cmd_setbot_ssl(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
324 int val = (strcmp(bot[14], "0") ? 1 : 0);
327 if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
329 } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
332 reply(textclient, user, "NS_SET_INVALID_BOOLEAN", value);
335 struct ClientSocket *client;
336 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
337 if(client->clientid == atoi(bot[15])) {
339 client->flags |= SOCKET_FLAG_SSL;
341 client->flags &= ~SOCKET_FLAG_SSL;
342 if(client->flags & SOCKET_FLAG_READY)
343 reply(textclient, user, "NS_SETBOT_NEED_RESTART");
347 printf_mysql_query("UPDATE `bots` SET `ssl` = '%d' WHERE `id` = '%s'", val, bot[15]);
350 reply(textclient, user, "\002SSL \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
354 static int global_cmd_setbot_serverpass(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
358 if(!strcmp(value, "*"))
360 struct ClientSocket *client;
361 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
362 if(client->clientid == atoi(bot[15])) {
365 client->pass = (value ? strdup(value) : NULL);
366 if(client->flags & SOCKET_FLAG_READY)
367 reply(textclient, user, "NS_SETBOT_NEED_RESTART");
371 printf_mysql_query("UPDATE `bots` SET `pass` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
375 reply(textclient, user, "\002SERVERPASS \002 %s", val);
379 static int global_cmd_setbot_class(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
380 int val = atoi(bot[5]);
383 if((val = resolve_botalias(value)) == -1) {
384 reply(textclient, user, "NS_SETBOT_INVALID_CLASS", value);
387 if(val != atoi(bot[5])) {
388 struct ClientSocket *client;
389 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
390 if(client->clientid == atoi(bot[15])) {
391 unbind_botwise_allcmd(0, client->clientid);
393 if(client->botid == 0) {
396 printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '0' AND `botid` = '%d'", client->clientid);
398 while ((row = mysql_fetch_row(res)) != NULL) {
399 if(bind_botwise_cmd_to_command(client->botid, client->clientid, row[0], row[1])) {
400 if(row[2] && strcmp(row[2], "")) {
401 bind_botwise_set_parameters(client->botid, client->clientid, row[0], row[2]);
404 bind_botwise_set_global_access(client->botid, client->clientid, row[0], atoi(row[3]));
407 bind_botwise_set_channel_access(client->botid, client->clientid, row[0], row[4]);
411 bind_botwise_unbound_required_functions(client->botid, client->clientid);
416 printf_mysql_query("UPDATE `bots` SET `botclass` = '%d' WHERE `id` = '%s'", val, bot[15]);
420 reply(textclient, user, "\002BOTCLASS \002 %s", resolve_botid(val));
424 static int global_cmd_setbot_queue(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
425 int val = (strcmp(bot[7], "0") ? 1 : 0);
428 if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
430 } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
433 reply(textclient, user, "NS_SET_INVALID_BOOLEAN", value);
436 struct ClientSocket *client;
437 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
438 if(client->clientid == atoi(bot[15])) {
440 client->flags |= SOCKET_FLAG_USE_QUEUE;
442 client->flags &= ~SOCKET_FLAG_USE_QUEUE;
446 printf_mysql_query("UPDATE `bots` SET `queue` = '%d' WHERE `id` = '%s'", val, bot[15]);
449 reply(textclient, user, "\002QUEUE \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
453 static int global_cmd_setbot_prefered(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
454 int val = (strcmp(bot[6], "0") ? 1 : 0);
457 if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
459 } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
462 reply(textclient, user, "NS_SET_INVALID_BOOLEAN", value);
465 struct ClientSocket *client;
466 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
467 if(client->clientid == atoi(bot[15])) {
469 client->flags |= SOCKET_FLAG_PREFERRED;
471 client->flags &= ~SOCKET_FLAG_PREFERRED;
475 printf_mysql_query("UPDATE `bots` SET `textbot` = '%d' WHERE `id` = '%s'", val, bot[15]);
478 reply(textclient, user, "\002PREFERED \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
482 static int global_cmd_setbot_secret(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
483 int val = (strcmp(bot[16], "0") ? 1 : 0);
486 if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
488 } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
491 reply(textclient, user, "NS_SET_INVALID_BOOLEAN", value);
494 struct ClientSocket *client;
495 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
496 if(client->clientid == atoi(bot[15])) {
498 client->flags |= SOCKET_FLAG_SECRET_BOT;
500 client->flags &= ~SOCKET_FLAG_SECRET_BOT;
504 printf_mysql_query("UPDATE `bots` SET `secret` = '%d' WHERE `id` = '%s'", val, bot[15]);
507 reply(textclient, user, "\002SECRET \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
511 static int global_cmd_setbot_maxchan(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
512 int val = atoi(bot[9]);
516 if(val < 0 || val > 99999) {
517 reply(textclient, user, "NS_SETBOT_MAXCHAN_INVALID", value);
520 printf_mysql_query("UPDATE `bots` SET `max_channels` = '%d' WHERE `id` = '%s'", val, bot[15]);
523 reply(textclient, user, "\002MAXCHAN \002 %d", val);
527 static int global_cmd_setbot_priority(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
528 int val = atoi(bot[10]);
532 if(val < 0 || val > 99) {
533 reply(textclient, user, "NS_SETBOT_PRIORITY_INVALID", value);
536 printf_mysql_query("UPDATE `bots` SET `register_priority` = '%d' WHERE `id` = '%s'", val, bot[15]);
539 reply(textclient, user, "\002PRIORITY \002 %d", val);
543 static int global_cmd_setbot_trigger(struct ClientSocket *textclient, struct UserNode *user, MYSQL_ROW bot, char *value) {
547 if(!*value || strlen(value) > 10) {
548 reply(textclient, user, "NS_SETBOT_TRIGGER_INVALID", value);
551 printf_mysql_query("UPDATE `bots` SET `defaulttrigger` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
552 flush_trigger_cache(atoi(bot[5]), atoi(bot[15]));
553 reply(textclient, user, "NS_SETBOT_TRIGGER_NOTE");
557 reply(textclient, user, "\002TRIGGER \002 %s", val);