added basic NeonSpam code (no functions)
[NeonServV5.git] / src / bots.c
1 /* bots.c - NeonServ v5.1
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
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.
8  * 
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.
13  * 
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/>. 
16  */
17
18 #include "bots.h"
19 #include "timeq.h"
20 #include "mysqlConn.h"
21 #include "ClientSocket.h"
22 #include "UserNode.h"
23 #include "ChanNode.h"
24 #include "ChanUser.h"
25
26 #include "bot_NeonServ.h"
27 #include "bot_NeonSpam.h"
28
29 void init_bots() {
30     init_NeonServ();
31     init_NeonSpam();
32     
33     MYSQL_RES *res;
34     MYSQL_ROW row;
35     //load all timed bans
36     printf_mysql_query("SELECT `ban_id`, `ban_timeout` FROM `bans` WHERE `ban_timeout` > 0");
37     res = mysql_use();
38     char nameBuf[20];
39     while ((row = mysql_fetch_row(res)) != NULL) {
40         if(atol(row[1]) - time(0) > 0) {
41             sprintf(nameBuf, "ban_%s", row[0]);
42             timeq_add_name(nameBuf, atol(row[1]) - time(0), channel_ban_timeout, strdup(row[0]));
43         } else {
44             //timed out
45             printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[0]);
46         }
47     }
48 }
49
50 void loop_bots() {
51     loop_NeonServ();
52     loop_NeonSpam();
53 }
54
55 void free_bots() {
56     free_NeonServ();
57     free_NeonSpam();
58 }
59
60 struct ClientSocket *getChannelBot(struct ChanNode *chan, int botid) {
61     struct ClientSocket *bot, *use_bot = NULL, *second_bot = NULL, *third_bot = NULL;
62     struct ChanUser *chanuser;
63     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
64         if(botid && bot->botid != botid) continue;
65         if((chanuser = getChanUser(bot->user, chan)) != NULL) {
66             if((chanuser->flags & CHANUSERFLAG_OPPED)) {
67                 use_bot = bot;
68                 if(bot->flags & SOCKET_FLAG_PREFERRED) break;
69             } else if(bot->flags & SOCKET_FLAG_PREFERRED)
70                 second_bot = bot;
71             else
72                 third_bot = bot;
73         }
74     }
75     if(!use_bot) use_bot = second_bot;
76     if(!use_bot) use_bot = third_bot;
77     return use_bot;
78 }
79
80 TIMEQ_CALLBACK(channel_ban_timeout) {
81     char *str_banid = data;
82     MYSQL_RES *res;
83     MYSQL_ROW row;
84     printf_mysql_query("SELECT `ban_mask`, `channel_name` FROM `bans` LEFT JOIN `channels` ON `ban_channel` = `channel_id` WHERE `ban_id` = '%s'", str_banid);
85     res = mysql_use();
86     struct ChanNode *chan;
87     if((row = mysql_fetch_row(res)) != NULL && (chan = getChanByName(row[1])) != NULL) {
88         struct ClientSocket *use_bot = getChannelBot(chan, 0);
89         if(use_bot) {
90             putsock(use_bot, "MODE %s -b %s", chan->name, row[0]);
91         }
92         printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", str_banid);
93     }
94     free(str_banid);
95 }