added spam/flood punishments
[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 #define SPAMSERV_CHECK_IGNORE 0
19 #define SPAMSERV_CHECK_WARN   1
20 #define SPAMSERV_CHECK_PUNISH 2
21
22 #define SPAMSERV_MSG_SPAM       "Spamming"
23 #define SPAMSERV_MSG_FLOOD      "Flooding the channel/network"
24 #define SPAMSERV_MSG_ADV        "Advertising"
25 #define SPAMSERV_MSG_JOINFLOOD  "Join flooding the channel"
26 #define SPAMSERV_MSG_WARNING    "%s is against the network rules"
27
28 static int neonspam_spamscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message);
29 static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser);
30
31 static USERAUTH_CALLBACK(neonspam_event_chanmsg_nick_lookup);
32 static void neonspam_event_chanmsg_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction);
33
34 struct neonspam_event_chanmsg_cache {
35     struct ClientSocket *client;
36     struct ChanUser *chanuser;
37     struct NeonSpamSettings *settings;
38     int action;
39     char *reason;
40     char *reaction;
41 };
42
43 static void neonspam_event_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
44     struct ClientSocket *client = getChannelBot(chan, BOTID);
45     if(!client) return; //we can't "see" this event
46     loadNeonSpamSettings(chan);
47     struct NeonSpamSettings *settings = chan->spam_settings;
48     struct ChanUser *chanuser = getChanUser(user, chan);
49     if(!settings || !chanuser) return;
50     
51     //ignore messages from ops/voices if we ignore them
52     if(!(settings->flags & SPAMSETTINGS_SCANOPS) && (chanuser->flags & CHANUSERFLAG_OPPED)) return;
53     if(!(settings->flags & SPAMSETTINGS_SCANVOICE) && (chanuser->flags & CHANUSERFLAG_VOICED)) return;
54     if(settings->exceptlevel == 0) return;
55     //scan the message
56     int result = 0;
57     int action = SPAMSERV_CHECK_IGNORE;
58     char reason[MAXLEN];
59     char *reaction = NULL;
60     if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_SPAMSCAN)) {
61         result = neonspam_spamscan(settings, chanuser, message);
62         switch(result) {
63             case SPAMSERV_CHECK_IGNORE:
64                 break;
65             case SPAMSERV_CHECK_WARN:
66                 if(action == SPAMSERV_CHECK_IGNORE) {
67                     action = result;
68                     sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
69                 }
70                 break;
71             case SPAMSERV_CHECK_PUNISH:
72                 if(action != SPAMSERV_CHECK_PUNISH) {
73                     action = result;
74                     sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
75                     reaction = "channel_repeatreaction";
76                 }
77                 break;
78         }
79     }
80     if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_FLOODSCAN)) {
81         result = neonspam_floodscan(settings, chanuser);
82         switch(result) {
83             case SPAMSERV_CHECK_IGNORE:
84                 break;
85             case SPAMSERV_CHECK_WARN:
86                 if(action == SPAMSERV_CHECK_IGNORE) {
87                     action = result;
88                     sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
89                 }
90                 break;
91             case SPAMSERV_CHECK_PUNISH:
92                 if(action != SPAMSERV_CHECK_PUNISH) {
93                     action = result;
94                     sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
95                     reaction = "channel_floodreaction";
96                 }
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         MYSQL_RES *res;
140         MYSQL_ROW row;
141         loadChannelSettings(chanuser->chan);
142         printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", reaction, chanuser->chan->channel_id);
143         res = mysql_use();
144         row = mysql_fetch_row(res);
145         int duration = 0;
146         char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
147         char *banmask = NULL;
148         switch (atoi(row[0])) {
149             case 2: //TIMEBAN: 3min
150                 duration = 180;
151             case 3: //TIMEBAN: 1h
152                 if(!duration)
153                     duration = 3600;
154                 banmask = generate_banmask(chanuser->user, banmaskBuf);
155                 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));
156                 int banid = (int) mysql_insert_id(mysql_conn);
157                 char nameBuf[MAXLEN];
158                 char banidBuf[20];
159                 sprintf(nameBuf, "ban_%d", banid);
160                 sprintf(banidBuf, "%d", banid);
161                 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
162             case 1: //KICKBAN
163                 if(!banmask)
164                     banmask = generate_banmask(chanuser->user, banmaskBuf);
165                 putsock(client, "MODE %s +b %s", chanuser->chan->name, banmask);
166             case 0: //KICK
167                 putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, reason);
168                 break;
169         }
170     }
171 }
172
173 static int neonspam_spamscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message) {
174     //crc32 hash of the message
175     unsigned long msghash = crc32(message);
176     if(chanuser->spamnode) {
177         if(chanuser->spamnode->lastmsg == msghash) {
178             chanuser->spamnode->spamcount++;
179             if(chanuser->spamnode->spamcount == settings->spam_amount)
180                 return SPAMSERV_CHECK_WARN;
181             else if(chanuser->spamnode->spamcount > settings->spam_amount)
182                 return SPAMSERV_CHECK_PUNISH;
183             else
184                 return SPAMSERV_CHECK_IGNORE;
185         }
186     } else
187         createSpamNode(chanuser);
188     chanuser->spamnode->lastmsg = msghash;
189     chanuser->spamnode->spamcount = 1;
190     return SPAMSERV_CHECK_IGNORE;
191 }
192
193 static int neonspam_update_penalty(struct NeonSpamSettings *settings, struct ChanUser *chanuser, int addmsg) {
194     int last_update = time(0) - chanuser->spamnode->last_penalty_update;
195     if(last_update) {
196         if(last_update < MAX_FLOOD_TIME && chanuser->spamnode->floodpenalty) {
197             chanuser->spamnode->floodpenalty -= last_update * (MAX_FLOOD_TIME / settings->flood_time);
198             if(chanuser->spamnode->floodpenalty < 0)
199                 chanuser->spamnode->floodpenalty = 0;
200         } else
201             chanuser->spamnode->floodpenalty = 0;
202         chanuser->spamnode->last_penalty_update = time(0);
203     }
204     chanuser->spamnode->floodpenalty += MAX_FLOOD_TIME * addmsg;
205     return chanuser->spamnode->floodpenalty / MAX_FLOOD_TIME + ((chanuser->spamnode->floodpenalty % MAX_FLOOD_TIME) ? 1 : 0);
206 }
207
208 static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser) {
209     if(!chanuser->spamnode)
210         createSpamNode(chanuser);
211     int messages_pending = neonspam_update_penalty(settings, chanuser, 1);
212     if(messages_pending == settings->flood_amount)
213         return SPAMSERV_CHECK_WARN;
214     else if(messages_pending > settings->flood_amount)
215         return SPAMSERV_CHECK_PUNISH;
216     else
217         return SPAMSERV_CHECK_IGNORE;
218 }
219
220