added joinflood scanner
[NeonServV5.git] / src / bot_NeonSpam.c
1 /* bot_NeonSpam.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 "bot_NeonSpam.h"
19 #include "modcmd.h"
20 #include "IRCParser.h"
21 #include "IRCEvents.h"
22 #include "UserNode.h"
23 #include "ChanNode.h"
24 #include "ChanUser.h"
25 #include "ModeNode.h"
26 #include "BanNode.h"
27 #include "ClientSocket.h"
28 #include "mysqlConn.h"
29 #include "lang.h"
30 #include "HandleInfoHandler.h"
31 #include "WHOHandler.h"
32 #include "DBHelper.h"
33 #include "tools.h"
34 #include "timeq.h"
35 #include "version.h"
36 #include "EventLogger.h"
37 #include "bots.h"
38 #include "cmd_neonserv.h"
39 #include "cmd_neonspam.h"
40
41 #define BOTID 2
42
43 static const struct default_language_entry msgtab[] = {
44     {NULL, NULL}
45 };
46
47 static int loadNeonSpamSettings(struct ChanNode *chan);
48 static void createSpamNode(struct ChanUser *chanuser);
49 static void freeJoinNode(struct NeonSpamJoinNode *joinnode);
50 static struct NeonSpamJoinNode *getNeonSpamJoinNode(struct ChanUser *chanuser);
51
52 #define SPAMSERV_CHECK_IGNORE 0
53 #define SPAMSERV_CHECK_WARN   1
54 #define SPAMSERV_CHECK_PUNISH 2
55
56 #define SPAMSERV_MSG_SPAM       "Spamming"
57 #define SPAMSERV_MSG_FLOOD      "Flooding the channel/network"
58 #define SPAMSERV_MSG_ADV        "Advertising"
59 #define SPAMSERV_MSG_JOINFLOOD  "Join flooding the channel"
60 #define SPAMSERV_MSG_WARNING    "%s is against the channel rules"
61
62 //EVENTS
63 #include "event_neonspam_join.c"
64 #include "event_neonspam_chanmsg.c"
65
66 static void neonspam_bot_ready(struct ClientSocket *client) {
67     MYSQL_RES *res;
68     MYSQL_ROW row;
69     
70     printf_mysql_query("SELECT `automodes` FROM `bots` WHERE `id` = '%d'", client->clientid);
71     res = mysql_use();
72     if ((row = mysql_fetch_row(res)) != NULL) {
73         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
74     }
75     
76     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);
77     res = mysql_use();
78     
79     while ((row = mysql_fetch_row(res)) != NULL) {
80         putsock(client, "JOIN %s %s", row[0], row[1]);
81     }
82 }
83
84 static void neonspam_trigger_callback(struct ChanNode *chan, char *trigger) {
85     MYSQL_RES *res;
86     MYSQL_ROW row;
87     loadChannelSettings(chan);
88     printf_mysql_query("SELECT `trigger` FROM `bot_channels` LEFT JOIN `bots` ON `botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chan->channel_id, BOTID);
89     res = mysql_use();
90     row = mysql_fetch_row(res);
91     strcpy(trigger, (strlen(row[0]) ? row[0] : "~"));
92 }
93
94 static void start_bots() {
95     struct UserNode *user;
96     struct ClientSocket *client;
97     MYSQL_RES *res, *res2;
98     MYSQL_ROW row;
99     
100     printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
101     res = mysql_use();
102     
103     while ((row = mysql_fetch_row(res)) != NULL) {
104         
105         user = addUser(row[0]);
106         strcpy(user->ident, row[1]);
107         strcpy(user->realname, row[2]);
108         user->flags |= USERFLAG_ISBOT;
109         client = create_socket(row[3], atoi(row[4]), row[5], user);
110         client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
111         client->botid = BOTID;
112         client->clientid = atoi(row[7]);
113         connect_socket(client);
114         printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '%d'", client->botid);
115         res2 = mysql_use();
116         while ((row = mysql_fetch_row(res2)) != NULL) {
117             if(bind_cmd_to_command(BOTID, row[0], row[1])) {
118                 if(row[2] && strcmp(row[2], "")) {
119                     bind_set_parameters(BOTID, row[0], row[2]);
120                 }
121                 if(row[3]) {
122                     bind_set_global_access(BOTID, row[0], atoi(row[3]));
123                 }
124                 if(row[4]) {
125                     bind_set_channel_access(BOTID, row[0], row[4]);
126                 }
127             }
128         }
129     }
130 }
131
132 static int loadNeonSpamSettings(struct ChanNode *chan) {
133     if(chan->spam_settings) return 0;
134     struct NeonSpamSettings *settings = malloc(sizeof(*settings));
135     if(!settings) {
136         perror("malloc() failed");
137         return 0;
138     }
139     MYSQL_RES *res;
140     MYSQL_ROW row, defaults = NULL;
141     loadChannelSettings(chan);
142     printf_mysql_query("SELECT `channel_scanstate`, `channel_maxrepeat`, `channel_maxflood`, `channel_floodtime`, `channel_maxjoin`, `channel_jointime`, `channel_scanexcept` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
143     res = mysql_use();
144     row = mysql_fetch_row(res);
145     if(!row[0] || !row[1] || !row[2] || !row[3] || !row[4] || !row[5]) {
146         printf_mysql_query("SELECT `channel_scanstate`, `channel_maxrepeat`, `channel_maxflood`, `channel_floodtime`, `channel_maxjoin`, `channel_jointime`, `channel_scanexcept` FROM `channels` WHERE `channel_name` = 'defaults'");
147         res = mysql_use();
148         defaults = mysql_fetch_row(res);
149     }
150     settings->flags = atoi(row[0] ? row[0] : defaults[0]);
151     settings->spam_amount = atoi(row[1] ? row[1] : defaults[1]);
152     settings->flood_amount = atoi(row[2] ? row[2] : defaults[2]);
153     settings->flood_time = atoi(row[3] ? row[3] : defaults[3]);
154     settings->join_amount = atoi(row[4] ? row[4] : defaults[4]);
155     settings->join_time = atoi(row[5] ? row[5] : defaults[5]);
156     settings->exceptlevel = atoi(row[6] ? row[6] : defaults[6]);
157     settings->join_nodes = NULL;
158     chan->spam_settings = settings;
159     return 1;
160 }
161
162 void freeNeonSpamSettings(struct NeonSpamSettings *settings) {
163     struct NeonSpamJoinNode *joinnode, *nextjoinnode;
164     for(joinnode = settings->join_nodes; joinnode; joinnode = nextjoinnode) {
165         nextjoinnode = joinnode->next;
166         freeJoinNode(joinnode);
167     }
168     free(settings);
169 }
170
171 static void freeJoinNode(struct NeonSpamJoinNode *joinnode) {
172     free(joinnode->ident);
173     free(joinnode->host);
174     free(joinnode);
175 }
176
177 static struct NeonSpamJoinNode *getNeonSpamJoinNode(struct ChanUser *chanuser) {
178     struct NeonSpamJoinNode *joinnode, *prevjoinnode = NULL, *nextjoinnode, *result = NULL;
179     for(joinnode = chanuser->chan->spam_settings->join_nodes; joinnode; joinnode = nextjoinnode) {
180         nextjoinnode = joinnode->next;
181         if(!stricmp(joinnode->ident, chanuser->user->ident) && !stricmp(joinnode->host, chanuser->user->host)) {
182             prevjoinnode = joinnode;
183             result = joinnode;
184         } else if(time(0) - joinnode->last_penalty_update > MAX_JOIN_TIME) {
185             freeJoinNode(joinnode);
186             if(prevjoinnode)
187                 prevjoinnode->next = nextjoinnode;
188             else
189                 chanuser->chan->spam_settings->join_nodes = nextjoinnode;
190         } else 
191             prevjoinnode = joinnode;
192     }
193     if(result)
194         return result;
195     joinnode = malloc(sizeof(*joinnode));
196     if(!joinnode) {
197         perror("malloc() failed");
198         return NULL;
199     }
200     joinnode->ident = strdup(chanuser->user->ident);
201     joinnode->host = strdup(chanuser->user->host);
202     joinnode->last_penalty_update = time(0);
203     joinnode->joinpenalty = 0;
204     joinnode->next = chanuser->chan->spam_settings->join_nodes;
205     chanuser->chan->spam_settings->join_nodes = joinnode;
206     return joinnode;
207 }
208
209 static void createSpamNode(struct ChanUser *chanuser) {
210     struct NeonSpamNode *spamnode = malloc(sizeof(*spamnode));
211     if(!spamnode) {
212         perror("malloc() failed");
213         return;
214     }
215     spamnode->lastmsg = 0;
216     spamnode->spamcount = 0;
217     spamnode->floodpenalty = 0;
218     spamnode->last_penalty_update = time(0);
219     chanuser->spamnode = spamnode;
220 }
221
222 void init_NeonSpam() {
223     
224     #define USER_COMMAND(NAME,FUNCTION,PARAMCOUNT,PRIVS,FLAGS) register_command(BOTID, NAME, FUNCTION, PARAMCOUNT, PRIVS, 0, FLAGS)
225     //               NAME              FUNCTION        PARAMS     PRIVS                FLAGS
226     USER_COMMAND("netinfo",      neonserv_cmd_netinfo,   0, NULL,                   0);
227     USER_COMMAND("version",      neonserv_cmd_version,   0, NULL,                   0);
228     USER_COMMAND("set",          neonspam_cmd_set,       0, "#channel_setters",     CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_LOG);
229     #undef USER_COMMAND
230     
231     #define OPER_COMMAND(NAME,FUNCTION,PARAMCOUNT,GACCESS,FLAGS) register_command(BOTID, NAME, FUNCTION, PARAMCOUNT, NULL, GACCESS, FLAGS)
232     //            NAME            FUNCTION              PARAMS  ACCS  FLAGS
233     OPER_COMMAND("register",     neonserv_cmd_register,  1,     200,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_CHAN_PARAM | CMDFLAG_OPLOG);
234     OPER_COMMAND("unregister",   neonserv_cmd_unregister,1,     200,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_CHAN_PARAM | CMDFLAG_OPLOG);
235     OPER_COMMAND("bind",         neonserv_cmd_bind,      2,     900,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_OPLOG);
236     OPER_COMMAND("unbind",       neonserv_cmd_unbind,    1,     900,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_OPLOG);
237     OPER_COMMAND("csuspend",     neonserv_cmd_csuspend,  1,     100,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_CHAN_PARAM | CMDFLAG_OPLOG);
238     OPER_COMMAND("cunsuspend",   neonserv_cmd_cunsuspend,1,     100,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_CHAN_PARAM | CMDFLAG_OPLOG);
239     OPER_COMMAND("say",          neonserv_cmd_say,       2,     600,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_CHAN_PARAM | CMDFLAG_OPLOG);
240     OPER_COMMAND("emote",        neonserv_cmd_emote,     2,     600,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_CHAN_PARAM | CMDFLAG_OPLOG);
241     OPER_COMMAND("notice",       neonserv_cmd_notice,    2,     600,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_CHAN_PARAM | CMDFLAG_OPLOG);
242     OPER_COMMAND("raw",          neonserv_cmd_raw,       1,     800,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_OPLOG);
243     OPER_COMMAND("god",          neonserv_cmd_god,       0,     1,    CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_OPLOG);
244     #undef OPER_COMMAND
245     
246     start_bots();
247     
248     //register events
249     bind_bot_ready(neonspam_bot_ready);
250     bind_join(neonspam_event_join);
251     bind_chanmsg(neonspam_event_chanmsg);
252     bind_privctcp(general_event_privctcp);
253     
254     set_trigger_callback(BOTID, neonspam_trigger_callback);
255     
256     register_default_language_table(msgtab);
257 }
258
259 void loop_NeonSpam() {
260     
261 }
262
263 void free_NeonSpam() {
264     
265 }
266
267 #undef BOTID