get default reaction if nothing else is set
[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         if(!row[0]) {
146             printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", reaction);
147             res = mysql_use();
148             row = mysql_fetch_row(res);
149         }
150         int duration = 0;
151         char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
152         char *banmask = NULL;
153         switch (atoi(row[0])) {
154             case 2: //TIMEBAN: 3min
155                 duration = 180;
156             case 3: //TIMEBAN: 1h
157                 if(!duration)
158                     duration = 3600;
159                 banmask = generate_banmask(chanuser->user, banmaskBuf);
160                 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));
161                 int banid = (int) mysql_insert_id(mysql_conn);
162                 char nameBuf[MAXLEN];
163                 char banidBuf[20];
164                 sprintf(nameBuf, "ban_%d", banid);
165                 sprintf(banidBuf, "%d", banid);
166                 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
167             case 1: //KICKBAN
168                 if(!banmask)
169                     banmask = generate_banmask(chanuser->user, banmaskBuf);
170                 putsock(client, "MODE %s +b %s", chanuser->chan->name, banmask);
171             case 0: //KICK
172                 putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, reason);
173                 break;
174         }
175     }
176 }
177
178 static int neonspam_spamscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message) {
179     //crc32 hash of the message
180     unsigned long msghash = crc32(message);
181     if(chanuser->spamnode) {
182         if(chanuser->spamnode->lastmsg == msghash) {
183             chanuser->spamnode->spamcount++;
184             if(chanuser->spamnode->spamcount == settings->spam_amount)
185                 return SPAMSERV_CHECK_WARN;
186             else if(chanuser->spamnode->spamcount > settings->spam_amount)
187                 return SPAMSERV_CHECK_PUNISH;
188             else
189                 return SPAMSERV_CHECK_IGNORE;
190         }
191     } else
192         createSpamNode(chanuser);
193     chanuser->spamnode->lastmsg = msghash;
194     chanuser->spamnode->spamcount = 1;
195     return SPAMSERV_CHECK_IGNORE;
196 }
197
198 static int neonspam_update_penalty(struct NeonSpamSettings *settings, struct ChanUser *chanuser, int addmsg) {
199     int last_update = time(0) - chanuser->spamnode->last_penalty_update;
200     if(last_update) {
201         if(last_update < MAX_FLOOD_TIME && chanuser->spamnode->floodpenalty) {
202             chanuser->spamnode->floodpenalty -= last_update * (MAX_FLOOD_TIME / settings->flood_time);
203             if(chanuser->spamnode->floodpenalty < 0)
204                 chanuser->spamnode->floodpenalty = 0;
205         } else
206             chanuser->spamnode->floodpenalty = 0;
207         chanuser->spamnode->last_penalty_update = time(0);
208     }
209     chanuser->spamnode->floodpenalty += MAX_FLOOD_TIME * addmsg;
210     return chanuser->spamnode->floodpenalty / MAX_FLOOD_TIME + ((chanuser->spamnode->floodpenalty % MAX_FLOOD_TIME) ? 1 : 0);
211 }
212
213 static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser) {
214     if(!chanuser->spamnode)
215         createSpamNode(chanuser);
216     int messages_pending = neonspam_update_penalty(settings, chanuser, 1);
217     if(messages_pending == settings->flood_amount)
218         return SPAMSERV_CHECK_WARN;
219     else if(messages_pending > settings->flood_amount)
220         return SPAMSERV_CHECK_PUNISH;
221     else
222         return SPAMSERV_CHECK_IGNORE;
223 }
224
225