7bcd4b5016dcc6526cb31cee7f0d147c88791bed
[NeonServV5.git] / src / event_neonspam_chanmsg.c
1 /* event_neonspam_chanmsg.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 static int neonspam_spamscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message);
19 static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser);
20 static int neonspam_botnetscan(struct ClientSocket *client, struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message);
21
22 static USERAUTH_CALLBACK(neonspam_event_chanmsg_nick_lookup);
23 static void neonspam_event_chanmsg_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction);
24
25 struct neonspam_event_chanmsg_cache {
26     struct ClientSocket *client;
27     struct ChanUser *chanuser;
28     struct NeonSpamSettings *settings;
29     int action;
30     char *reason;
31     char *reaction;
32 };
33
34 static void neonspam_event_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
35     struct ClientSocket *client = getChannelBot(chan, BOTID);
36     if(!client) return; //we can't "see" this event
37     loadNeonSpamSettings(chan);
38     struct NeonSpamSettings *settings = chan->spam_settings;
39     struct ChanUser *chanuser = getChanUser(user, chan);
40     if(!settings || !chanuser) return;
41     
42     //ignore messages from ops/voices if we ignore them
43     if(!(settings->flags & SPAMSETTINGS_SCANOPS) && (chanuser->flags & CHANUSERFLAG_OPPED)) return;
44     if(!(settings->flags & SPAMSETTINGS_SCANVOICE) && (chanuser->flags & CHANUSERFLAG_VOICED)) return;
45     if(settings->exceptlevel == 0) return;
46     //scan the message
47     int result = 0;
48     int action = SPAMSERV_CHECK_IGNORE;
49     char reason[MAXLEN];
50     char *reaction = NULL;
51     if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_SPAMSCAN)) {
52         result = neonspam_spamscan(settings, chanuser, message);
53         switch(result) {
54             case SPAMSERV_CHECK_IGNORE:
55                 break;
56             case SPAMSERV_CHECK_WARN:
57                 if(action == SPAMSERV_CHECK_IGNORE) {
58                     action = result;
59                     sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
60                 }
61                 break;
62             case SPAMSERV_CHECK_PUNISH:
63                 if(action != SPAMSERV_CHECK_PUNISH) {
64                     action = result;
65                     sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
66                     reaction = "channel_repeatreaction";
67                 }
68                 break;
69         }
70     }
71     if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_FLOODSCAN)) {
72         result = neonspam_floodscan(settings, chanuser);
73         switch(result) {
74             case SPAMSERV_CHECK_IGNORE:
75                 break;
76             case SPAMSERV_CHECK_WARN:
77                 if(action == SPAMSERV_CHECK_IGNORE) {
78                     action = result;
79                     sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_FLOOD);
80                 }
81                 break;
82             case SPAMSERV_CHECK_PUNISH:
83                 if(action != SPAMSERV_CHECK_PUNISH) {
84                     action = result;
85                     sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_FLOOD);
86                     reaction = "channel_floodreaction";
87                 }
88                 break;
89         }
90     }
91     if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_BOTNETSCAN)) {
92         result = neonspam_botnetscan(client, settings, chanuser, message);
93         switch(result) {
94             case SPAMSERV_CHECK_DEAD:
95                 return;
96             case SPAMSERV_CHECK_IGNORE:
97                 break;
98         }
99     }
100     //some other checks?
101     
102     if(action != SPAMSERV_CHECK_IGNORE) {
103         //whois the user to check against exceptlevel
104         if((user->flags & USERFLAG_ISAUTHED) || settings->exceptlevel == 501) {
105             neonspam_event_chanmsg_punish(client, chanuser, settings, action, reason, reaction);
106         } else {
107             struct neonspam_event_chanmsg_cache *cache = malloc(sizeof(*cache));
108             if (!cache) {
109                 perror("malloc() failed");
110                 return;
111             }
112             cache->client = client;
113             cache->chanuser = chanuser;
114             cache->settings = settings;
115             cache->action = action;
116             cache->reason = strdup(reason);
117             cache->reaction = reaction;
118             get_userauth(user, neonspam_event_chanmsg_nick_lookup, cache);
119         }
120         
121     }
122 }
123
124 static USERAUTH_CALLBACK(neonspam_event_chanmsg_nick_lookup) {
125     struct neonspam_event_chanmsg_cache *cache = data;
126     neonspam_event_chanmsg_punish(cache->client, cache->chanuser, cache->settings, cache->action, cache->reason, cache->reaction);
127     free(cache->reason);
128     free(cache);
129 }
130
131 static void neonspam_event_chanmsg_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction) {
132     int uaccess = 0;
133     if(chanuser->user->flags & USERFLAG_ISAUTHED)
134         uaccess = getChannelAccess(chanuser->user, chanuser->chan, 0);
135     if(uaccess >= settings->exceptlevel) return;
136     if(action == SPAMSERV_CHECK_WARN) {
137         reply(client, chanuser->user, "%s", reason);
138     } else if(action == SPAMSERV_CHECK_PUNISH) {
139         loadChannelSettings(chanuser->chan);
140         int reactionid;
141         if(!(reactionid = atoi(reaction))) {
142             MYSQL_RES *res;
143             MYSQL_ROW row;
144             printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", reaction, chanuser->chan->channel_id);
145             res = mysql_use();
146             row = mysql_fetch_row(res);
147             if(!row[0]) {
148                 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", reaction);
149                 res = mysql_use();
150                 row = mysql_fetch_row(res);
151             }
152             reactionid = atoi(row[0]);
153         }
154             
155         int duration = 0;
156         char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
157         char *banmask = NULL;
158         switch (reactionid) {
159             case 2: //TIMEBAN: 3min
160                 duration = 180;
161             case 3: //TIMEBAN: 1h
162                 if(!duration)
163                     duration = 3600;
164                 banmask = generate_banmask(chanuser->user, banmaskBuf);
165                 printf_mysql_query("INSERT INTO `bans` (`ban_channel`, `ban_mask`, `ban_triggered`, `ban_timeout`, `ban_owner`, `ban_reason`) VALUES ('%d', '%s', UNIX_TIMESTAMP(), '%lu', '%d', '%s')", chanuser->chan->channel_id, escape_string(banmask), (unsigned long) (time(0) + duration), 0, escape_string(reason));
166                 int banid = (int) mysql_insert_id(mysql_conn);
167                 char nameBuf[MAXLEN];
168                 char banidBuf[20];
169                 sprintf(nameBuf, "ban_%d", banid);
170                 sprintf(banidBuf, "%d", banid);
171                 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
172             case 1: //KICKBAN
173                 if(!banmask)
174                     banmask = generate_banmask(chanuser->user, banmaskBuf);
175                 putsock(client, "MODE %s +b %s", chanuser->chan->name, banmask);
176             case 0: //KICK
177                 putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, reason);
178                 break;
179         }
180     }
181 }
182
183 static int neonspam_spamscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message) {
184     //crc32 hash of the message
185     unsigned long msghash = crc32(message);
186     if(chanuser->spamnode) {
187         if(chanuser->spamnode->lastmsg == msghash) {
188             chanuser->spamnode->spamcount++;
189             if(chanuser->spamnode->spamcount == settings->spam_amount)
190                 return SPAMSERV_CHECK_WARN;
191             else if(chanuser->spamnode->spamcount > settings->spam_amount)
192                 return SPAMSERV_CHECK_PUNISH;
193             else
194                 return SPAMSERV_CHECK_IGNORE;
195         }
196     } else
197         createSpamNode(chanuser);
198     chanuser->spamnode->lastmsg = msghash;
199     chanuser->spamnode->spamcount = 1;
200     return SPAMSERV_CHECK_IGNORE;
201 }
202
203 static int neonspam_update_penalty(struct NeonSpamSettings *settings, struct ChanUser *chanuser, int addmsg) {
204     int last_update = time(0) - chanuser->spamnode->last_penalty_update;
205     if(last_update) {
206         if(last_update < MAX_FLOOD_TIME && chanuser->spamnode->floodpenalty) {
207             chanuser->spamnode->floodpenalty -= last_update * (MAX_FLOOD_TIME / settings->flood_time);
208             if(chanuser->spamnode->floodpenalty < 0)
209                 chanuser->spamnode->floodpenalty = 0;
210         } else
211             chanuser->spamnode->floodpenalty = 0;
212         chanuser->spamnode->last_penalty_update = time(0);
213     }
214     chanuser->spamnode->floodpenalty += MAX_FLOOD_TIME * addmsg;
215     return chanuser->spamnode->floodpenalty / MAX_FLOOD_TIME + ((chanuser->spamnode->floodpenalty % MAX_FLOOD_TIME) ? 1 : 0);
216 }
217
218 static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser) {
219     if(!chanuser->spamnode)
220         createSpamNode(chanuser);
221     int messages_pending = neonspam_update_penalty(settings, chanuser, 1);
222     if(messages_pending == settings->flood_amount)
223         return SPAMSERV_CHECK_WARN;
224     else if(messages_pending > settings->flood_amount)
225         return SPAMSERV_CHECK_PUNISH;
226     else
227         return SPAMSERV_CHECK_IGNORE;
228 }
229
230 static int neonspam_botnetscan(struct ClientSocket *client, struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message) {
231     //crc32 hash of the message
232     unsigned long msghash = crc32(message);
233     if((time(0) - settings->lastmsg_time) > BOTNETSCAN_TIME || settings->lastmsg != msghash) {
234         int i;
235         for(i = 0; i < BOTNETSCAN_USERS; i++) {
236             if(settings->botnicks[i]) {
237                 free(settings->botnicks[i]);
238                 settings->botnicks[i] = NULL;
239             }
240         }
241         settings->flags &= ~SPAMSETTINGS_KICKEDBOTQUEUE;
242         settings->lastmsg = msghash;
243     } else if(settings->lastmsg == msghash) {
244         int i;
245         for(i = 0; i < BOTNETSCAN_USERS; i++) {
246             if(!settings->botnicks[i]) {
247                 settings->botnicks[i] = strdup(chanuser->user->nick);
248                 break;
249             } else if(!stricmp(chanuser->user->nick, settings->botnicks[i])) {
250                 return SPAMSERV_CHECK_IGNORE;
251             }
252         }
253         if(i == BOTNETSCAN_USERS) {
254             //BOTNETSCAN_USERS exceeded
255             if(!(settings->flags & SPAMSETTINGS_KICKEDBOTQUEUE)) {
256                 for(i = 0; i < BOTNETSCAN_USERS; i++) {
257                     putsock(client, "KICK %s %s :%s", chanuser->chan->name, settings->botnicks[i], SPAMSERV_MSG_BOTNET);
258                 }
259                 settings->flags |= SPAMSETTINGS_KICKEDBOTQUEUE;
260             }
261             putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, SPAMSERV_MSG_BOTNET);
262             return SPAMSERV_CHECK_DEAD;
263         }
264     }
265     settings->lastmsg_time = time(0);
266     return SPAMSERV_CHECK_IGNORE;
267 }