1 /* cmd_global_setbot.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"
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_nick(user, row, value);
75 reply(getTextBot(), user, "NS_SETBOT_SETTING", argv[1]);
81 reply(getTextBot(), user, "NS_SETBOT_HEADER", botid);
82 global_cmd_setbot_active(user, row, NULL);
83 global_cmd_setbot_nick(user, row, NULL);
84 global_cmd_setbot_ident(user, row, NULL);
85 global_cmd_setbot_realname(user, row, NULL);
86 global_cmd_setbot_server(user, row, NULL);
87 global_cmd_setbot_port(user, row, NULL);
88 global_cmd_setbot_bind(user, row, NULL);
89 global_cmd_setbot_ssl(user, row, NULL);
90 global_cmd_setbot_serverpass(user, row, NULL);
91 global_cmd_setbot_class(user, row, NULL);
92 global_cmd_setbot_queue(user, row, NULL);
93 global_cmd_setbot_prefered(user, row, NULL);
94 global_cmd_setbot_maxchan(user, row, NULL);
95 global_cmd_setbot_priority(user, row, NULL);
96 global_cmd_setbot_trigger(user, row, NULL);
100 static int global_cmd_setbot_active(struct UserNode *user, MYSQL_ROW bot, char *value) {
101 int val = ((bot[0] && !strcmp(bot[0], "1")) ? 1 : 0);
104 if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
106 } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
109 reply(getTextBot(), user, "NS_SET_INVALID_BOOLEAN", value);
112 if(val != ((bot[0] && !strcmp(bot[0], "1")) ? 1 : 0)) {
115 struct ClientSocket *client;
116 client = create_socket(bot[2], atoi(bot[3]), bot[11], bot[4], bot[1], bot[12], bot[13]);
117 client->flags |= (strcmp(bot[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
118 client->flags |= (strcmp(bot[7], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
119 client->flags |= (strcmp(bot[14], "0") ? SOCKET_FLAG_SSL : 0);
120 client->botid = atoi(bot[5]);
121 client->clientid = atoi(bot[15]);
122 connect_socket(client);
125 struct ClientSocket *client;
126 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
127 if(client->clientid == atoi(bot[15])) {
128 close_socket(client);
133 printf_mysql_query("UPDATE `bots` SET `active` = '%d' WHERE `id` = '%s'", val, bot[15]);
137 reply(getTextBot(), user, "\002ACTIVE \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
141 static int global_cmd_setbot_nick(struct UserNode *user, MYSQL_ROW bot, char *value) {
145 if(!is_valid_nick(value)) {
146 reply(getTextBot(), user, "NS_SETBOT_NICK_INVALID", value);
150 struct ClientSocket *client;
151 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
152 if(client->clientid == atoi(bot[15])) {
155 client->nick = strdup(value);
156 if(client->flags & SOCKET_FLAG_READY)
157 putsock(client, "NICK %s", value);
161 printf_mysql_query("UPDATE `bots` SET `nick` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
165 reply(getTextBot(), user, "\002NICK \002 %s", val);
169 static int global_cmd_setbot_ident(struct UserNode *user, MYSQL_ROW bot, char *value) {
173 if(strlen(value) > 12)
176 struct ClientSocket *client;
177 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
178 if(client->clientid == atoi(bot[15])) {
181 client->ident = strdup(value);
185 printf_mysql_query("UPDATE `bots` SET `ident` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
189 reply(getTextBot(), user, "\002IDENT \002 %s", val);
193 static int global_cmd_setbot_realname(struct UserNode *user, MYSQL_ROW bot, char *value) {
197 if(strlen(value) > 255)
200 struct ClientSocket *client;
201 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
202 if(client->clientid == atoi(bot[15])) {
205 client->ident = strdup(value);
209 printf_mysql_query("UPDATE `bots` SET `realname` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
213 reply(getTextBot(), user, "\002REALNAME \002 %s", val);
217 static int global_cmd_setbot_server(struct UserNode *user, MYSQL_ROW bot, char *value) {
221 struct ClientSocket *client;
222 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
223 if(client->clientid == atoi(bot[15])) {
226 client->host = strdup(value);
227 if(client->flags & SOCKET_FLAG_READY)
228 reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
232 printf_mysql_query("UPDATE `bots` SET `server` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
236 reply(getTextBot(), user, "\002SERVER \002 %s", val);
240 static int global_cmd_setbot_port(struct UserNode *user, MYSQL_ROW bot, char *value) {
241 int val = atoi(bot[3]);
245 if(val <= 0 || val > 65534) {
246 reply(getTextBot(), user, "NS_SETBOT_PORT_INVALID", value);
249 struct ClientSocket *client;
250 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
251 if(client->clientid == atoi(bot[15])) {
253 if(client->flags & SOCKET_FLAG_READY)
254 reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
258 printf_mysql_query("UPDATE `bots` SET `port` = '%d' WHERE `id` = '%s'", val, bot[15]);
261 reply(getTextBot(), user, "\002PORT \002 %d", val);
265 static int global_cmd_setbot_bind(struct UserNode *user, MYSQL_ROW bot, char *value) {
269 if(!strcmp(value, "*"))
271 struct ClientSocket *client;
272 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
273 if(client->clientid == atoi(bot[15])) {
276 client->bind = (value ? strdup(value) : NULL);
277 if(client->flags & SOCKET_FLAG_READY)
278 reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
283 printf_mysql_query("UPDATE `bots` SET `bind` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
285 printf_mysql_query("UPDATE `bots` SET `bind` = NULL WHERE `id` = '%s'", bot[15]);
289 reply(getTextBot(), user, "\002BIND \002 %s", val);
293 static int global_cmd_setbot_ssl(struct UserNode *user, MYSQL_ROW bot, char *value) {
294 int val = (strcmp(bot[14], "0") ? 1 : 0);
297 if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
299 } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
302 reply(getTextBot(), user, "NS_SET_INVALID_BOOLEAN", value);
305 struct ClientSocket *client;
306 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
307 if(client->clientid == atoi(bot[15])) {
309 client->flags |= SOCKET_FLAG_SSL;
311 client->flags &= ~SOCKET_FLAG_SSL;
312 if(client->flags & SOCKET_FLAG_READY)
313 reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
317 printf_mysql_query("UPDATE `bots` SET `ssl` = '%d' WHERE `id` = '%s'", val, bot[15]);
320 reply(getTextBot(), user, "\002SSL \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
324 static int global_cmd_setbot_serverpass(struct UserNode *user, MYSQL_ROW bot, char *value) {
328 struct ClientSocket *client;
329 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
330 if(client->clientid == atoi(bot[15])) {
333 client->pass = strdup(value);
334 if(client->flags & SOCKET_FLAG_READY)
335 reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
339 printf_mysql_query("UPDATE `bots` SET `pass` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
343 reply(getTextBot(), user, "\002SERVERPASS \002 %s", val);
347 static int global_cmd_setbot_class(struct UserNode *user, MYSQL_ROW bot, char *value) {
348 int val = atoi(bot[5]);
351 if((val = resolve_botalias(value)) != -1) {
352 reply(getTextBot(), user, "NS_SETBOT_INVALID_CLASS", value);
355 if(val != atoi(bot[5])) {
356 struct ClientSocket *client;
357 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
358 if(client->clientid == atoi(bot[15])) {
359 unbind_botwise_allcmd(client->clientid);
361 if(client->botid == 0) {
364 printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '0' AND `botid` = '%d'", client->clientid);
366 while ((row = mysql_fetch_row(res)) != NULL) {
367 if(bind_botwise_cmd_to_command(client->botid, client->clientid, row[0], row[1])) {
368 if(row[2] && strcmp(row[2], "")) {
369 bind_botwise_set_parameters(client->botid, client->clientid, row[0], row[2]);
372 bind_botwise_set_global_access(client->botid, client->clientid, row[0], atoi(row[3]));
375 bind_botwise_set_channel_access(client->botid, client->clientid, row[0], row[4]);
379 bind_botwise_unbound_required_functions(client->botid, client->clientid);
384 printf_mysql_query("UPDATE `bots` SET `botclass` = '%d' WHERE `id` = '%s'", val, bot[15]);
388 reply(getTextBot(), user, "\002BOTCLASS \002 %s", resolve_botid(val));
392 static int global_cmd_setbot_queue(struct UserNode *user, MYSQL_ROW bot, char *value) {
393 int val = (strcmp(bot[7], "0") ? 1 : 0);
396 if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
398 } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
401 reply(getTextBot(), user, "NS_SET_INVALID_BOOLEAN", value);
404 struct ClientSocket *client;
405 for(client = getBots(0, NULL); client; client = getBots(0, client)) {
406 if(client->clientid == atoi(bot[15])) {
408 client->flags |= SOCKET_FLAG_USE_QUEUE;
410 client->flags &= ~SOCKET_FLAG_USE_QUEUE;
414 printf_mysql_query("UPDATE `bots` SET `queue` = '%d' WHERE `id` = '%s'", val, bot[15]);
417 reply(getTextBot(), user, "\002QUEUE \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
421 static int global_cmd_setbot_prefered(struct UserNode *user, MYSQL_ROW bot, char *value) {
422 int val = (strcmp(bot[6], "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_PREFERRED;
439 client->flags &= ~SOCKET_FLAG_PREFERRED;
443 printf_mysql_query("UPDATE `bots` SET `queue` = '%d' WHERE `id` = '%s'", val, bot[15]);
446 reply(getTextBot(), user, "\002PREFERED \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
450 static int global_cmd_setbot_maxchan(struct UserNode *user, MYSQL_ROW bot, char *value) {
451 int val = atoi(bot[9]);
455 if(val < 0 || val > 99999) {
456 reply(getTextBot(), user, "NS_SETBOT_MAXCHAN_INVALID", value);
459 printf_mysql_query("UPDATE `bots` SET `maxchan` = '%d' WHERE `id` = '%s'", val, bot[15]);
462 reply(getTextBot(), user, "\002MAXCHAN \002 %d", val);
466 static int global_cmd_setbot_priority(struct UserNode *user, MYSQL_ROW bot, char *value) {
467 int val = atoi(bot[10]);
471 if(val < 0 || val > 99) {
472 reply(getTextBot(), user, "NS_SETBOT_PRIORITY_INVALID", value);
475 printf_mysql_query("UPDATE `bots` SET `register_priority` = '%d' WHERE `id` = '%s'", val, bot[15]);
478 reply(getTextBot(), user, "\002PRIORITY \002 %d", val);
482 static int global_cmd_setbot_trigger(struct UserNode *user, MYSQL_ROW bot, char *value) {
486 if(!*value || strlen(value) > 10) {
487 reply(getTextBot(), user, "NS_SETBOT_TRIGGER_INVALID", value);
490 printf_mysql_query("UPDATE `bots` SET `defaulttrigger` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
491 reply(getTextBot(), user, "NS_SETBOT_TRIGGER_NOTE");
495 reply(getTextBot(), user, "\002TRIGGER \002 %s", val);