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