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