1 /* bots.c - NeonServ v5.5
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/>.
20 #include "mysqlConn.h"
21 #include "ClientSocket.h"
28 #include "IRCEvents.h"
30 struct cmd_bot_alias {
33 struct cmd_bot_alias *next;
36 static struct cmd_bot_alias *bot_aliases = NULL;
38 static void start_zero_bots() {
39 struct ClientSocket *client;
40 MYSQL_RES *res, *res2;
43 printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind`, `secret` FROM `bots` WHERE `botclass` = '0' AND `active` = '1'");
46 while ((row = mysql_fetch_row(res)) != NULL) {
47 client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
48 client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
49 client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
50 client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
51 client->flags |= (strcmp(row[11], "0") ? SOCKET_FLAG_SECRET_BOT : 0);
53 client->clientid = atoi(row[7]);
54 connect_socket(client);
56 printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '0' AND `botid` = '%d'", client->clientid);
58 while ((row = mysql_fetch_row(res2)) != NULL) {
59 if(bind_botwise_cmd_to_command(0, client->clientid, row[0], row[1])) {
60 if(row[2] && strcmp(row[2], "")) {
61 bind_botwise_set_parameters(0, client->clientid, row[0], row[2]);
64 bind_botwise_set_global_access(0, client->clientid, row[0], atoi(row[3]));
67 bind_botwise_set_channel_access(0, client->clientid, row[0], row[4]);
69 if(strcmp(row[5], "0"))
70 bind_botwise_set_bind_flags(0, client->clientid, row[0], atoi(row[5]));
73 bind_botwise_unbound_required_functions(0, client->clientid);
77 static void zero_bots_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
80 loadChannelSettings(chan);
81 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
85 printf_mysql_query("SELECT `trigger`, `defaulttrigger` FROM `bot_channels` LEFT JOIN `bots` ON `botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '0' AND `botid` = '%d'", chan->channel_id, clientid);
87 if(!(row = mysql_fetch_row(res))) {
92 strcpy(trigger, row[0]);
94 strcpy(trigger, ((row[1] && *row[1]) ? row[1] : "+"));
97 static void zero_bots_bot_ready(struct ClientSocket *client) {
98 if(client->botid != 0)
103 printf_mysql_query("SELECT `automodes`, `oper_user`, `oper_pass` FROM `bots` WHERE `id` = '%d'", client->clientid);
105 if ((row = mysql_fetch_row(res)) != NULL) {
106 if(row[1] && row[2]) {
107 putsock(client, "OPER %s %s", row[1], row[2]);
109 putsock(client, "MODE %s +%s", client->user->nick, row[0]);
112 printf_mysql_query("SELECT `channel_name`, `channel_key` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `botid` = '%d' AND `suspended` = '0'", client->clientid);
115 while ((row = mysql_fetch_row(res)) != NULL) {
116 putsock(client, "JOIN %s %s", row[0], row[1]);
121 set_bot_alias(0, "0");
123 set_trigger_callback(0, 0, zero_bots_trigger_callback);
124 bind_bot_ready(zero_bots_bot_ready, 0);
128 //load all timed bans
129 printf_mysql_query("SELECT `ban_id`, `ban_timeout` FROM `bans` WHERE `ban_timeout` > 0");
132 while ((row = mysql_fetch_row(res)) != NULL) {
133 if(atol(row[1]) - time(0) > 0) {
134 sprintf(nameBuf, "ban_%s", row[0]);
135 timeq_add_name(nameBuf, atol(row[1]) - time(0), 0, channel_ban_timeout, strdup(row[0]));
138 printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[0]);
143 struct ClientSocket *getChannelBot(struct ChanNode *chan, int botid) {
144 struct ClientSocket *bot, *use_bot = NULL, *second_bot = NULL, *third_bot = NULL;
145 struct ChanUser *chanuser;
146 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
147 if(botid && bot->botid != botid) continue;
148 if((chanuser = getChanUser(bot->user, chan)) != NULL) {
149 if((chanuser->flags & CHANUSERFLAG_OPPED)) {
151 if(bot->flags & SOCKET_FLAG_PREFERRED) break;
152 } else if(bot->flags & SOCKET_FLAG_PREFERRED)
158 if(!use_bot) use_bot = second_bot;
159 if(!use_bot) use_bot = third_bot;
163 void requestOp(struct UserNode *user, struct ChanNode *chan) {
164 struct ClientSocket *bot, *userbot = NULL;
165 struct ChanUser *chanuser = getChanUser(user, chan);
167 if(!chanuser) return;
168 if((chanuser->flags & CHANUSERFLAG_OPPED)) return;
169 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
170 if(!opped && (chanuser = getChanUser(bot->user, chan)) != NULL && (chanuser->flags & CHANUSERFLAG_OPPED)) {
172 putsock(bot, "MODE %s +o %s", chan->name, user->nick);
174 if(bot->user == user) {
179 if(userbot && (isUserModeSet(user, 'o') || isUserModeSet(user, 'O') || isUserModeSet(user, 'k') || isUserModeSet(user, 'X'))) {
180 putsock(userbot, "MODE %s +o %s", chan->name, user->nick);
181 putsock(userbot, "OPMODE %s +o %s", chan->name, user->nick);
186 void requestInvite(struct UserNode *user, struct ChanNode *chan) {
187 struct ClientSocket *bot, *userbot = NULL;
188 struct ChanUser *chanuser = getChanUser(user, chan);
191 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
192 if(!invited && (chanuser = getChanUser(bot->user, chan)) != NULL && (chanuser->flags & CHANUSERFLAG_OPPED)) {
194 putsock(bot, "INVITE %s %s", user->nick, chan->name);
196 if(bot->user == user) {
202 TIMEQ_CALLBACK(channel_ban_timeout) {
203 char *str_banid = data;
206 printf_mysql_query("SELECT `ban_mask`, `channel_name` FROM `bans` LEFT JOIN `channels` ON `ban_channel` = `channel_id` WHERE `ban_id` = '%s'", str_banid);
208 struct ChanNode *chan;
209 if((row = mysql_fetch_row(res)) != NULL && (chan = getChanByName(row[1])) != NULL) {
210 struct ClientSocket *use_bot = getChannelBot(chan, 0);
212 putsock(use_bot, "MODE %s -b %s", chan->name, row[0]);
214 printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", str_banid);
219 static int general_ctcp(char *buffer, char *command, char *text);
221 void general_event_privctcp(struct UserNode *user, struct UserNode *target, char *command, char *text) {
222 char ctcpBuf[MAXLEN];
223 if(general_ctcp(ctcpBuf, command, text)) {
224 struct ClientSocket *bot;
225 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
226 if(bot->user == target) break;
229 putsock(bot, "NOTICE %s :\001%s\001", user->nick, ctcpBuf);
233 static int general_ctcp(char *buffer, char *command, char *text) {
234 if(!stricmp(command, "VERSION")) {
235 sprintf(buffer, "VERSION NeonServ v%s.%d by pk910 (%s)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"));
238 if(!stricmp(command, "FINGER")) {
239 sprintf(buffer, "FINGER NeonServ v%s.%d (%s) build %s lines C code using " COMPILER " (see +netinfo)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"), codelines);
242 if(!stricmp(command, "PING")) {
243 sprintf(buffer, "PING %s", (text ? text : "0"));
246 if(!stricmp(command, "TIME")) {
251 timeinfo = localtime(&rawtime);
252 strftime(timeBuf, 80, "%c", timeinfo);
253 sprintf(buffer, "TIME %s", timeBuf);
259 void set_bot_alias(int botid, char *alias) {
260 struct cmd_bot_alias *botalias, *existing_alias = NULL;
261 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
262 if(!stricmp(botalias->alias, alias))
264 if(botalias->botid == botid)
265 existing_alias = botalias;
268 free(existing_alias->alias);
269 existing_alias->alias = strdup(alias);
272 botalias = malloc(sizeof(*botalias));
274 perror("malloc() failed");
277 botalias->botid = botid;
278 botalias->alias = strdup(alias);
279 botalias->next = bot_aliases;
280 bot_aliases = botalias;
283 const char *resolve_botid(int botid) {
284 struct cmd_bot_alias *botalias;
285 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
286 if(botalias->botid == botid)
287 return botalias->alias;
292 int resolve_botalias(const char *alias) {
293 struct cmd_bot_alias *botalias;
294 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
295 if(!stricmp(botalias->alias, alias))
296 return botalias->botid;