*** VERSION 5.2.0 ***
[NeonServV5.git] / src / event_neonspam_join.c
1 /* event_neonspam_join.c - NeonServ v5.2
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 USERAUTH_CALLBACK(neonspam_event_join_nick_lookup);
19 static void neonspam_event_join_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action);
20 static int neonspam_joinfloodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser);
21
22 struct neonspam_event_join_cache {
23     struct ClientSocket *client;
24     struct ChanUser *chanuser;
25     struct NeonSpamSettings *settings;
26     int action;
27 };
28
29 static void neonspam_event_join(struct ChanUser *chanuser) {
30     struct ClientSocket *client = getChannelBot(chanuser->chan, BOTID);
31     if(!client) return; //we can't "see" this event
32     loadNeonSpamSettings(chanuser->chan);
33     struct NeonSpamSettings *settings = chanuser->chan->spam_settings;
34     if(!settings || !(settings->flags & SPAMSETTINGS_JOINSCAN)) return;
35     if(settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX] == 0) return;
36     int result = neonspam_joinfloodscan(settings, chanuser);
37     if(result != SPAMSERV_CHECK_IGNORE) {
38         //whois the user to check against exceptlevel
39         if((chanuser->user->flags & USERFLAG_ISAUTHED) || settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX] == 501) {
40             neonspam_event_join_punish(client, chanuser, settings, result);
41         } else {
42             struct neonspam_event_join_cache *cache = malloc(sizeof(*cache));
43             if (!cache) {
44                 perror("malloc() failed");
45                 return;
46             }
47             cache->client = client;
48             cache->chanuser = chanuser;
49             cache->settings = settings;
50             cache->action = result;
51             get_userauth(chanuser->user, neonspam_event_join_nick_lookup, cache);
52         }
53     }
54 }
55
56 static USERAUTH_CALLBACK(neonspam_event_join_nick_lookup) {
57     struct neonspam_event_join_cache *cache = data;
58     neonspam_event_join_punish(cache->client, cache->chanuser, cache->settings, cache->action);
59     free(cache);
60 }
61
62 static void neonspam_event_join_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action) {
63     int uaccess = 0;
64     if(chanuser->user->flags & USERFLAG_ISAUTHED)
65         uaccess = getChannelAccess(chanuser->user, chanuser->chan, 0);
66     if(uaccess >= settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX]) return;
67     //scanops / scanvoiced
68     MYSQL_RES *res;
69     MYSQL_ROW row, defaults = NULL;
70     loadChannelSettings(chanuser->chan);
71     printf_mysql_query("SELECT `channel_flood_reaction`, `channel_flood_reaction_duration`, `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_id` = '%d'", chanuser->chan->channel_id);
72     res = mysql_use();
73     row = mysql_fetch_row(res);
74     if(!row[0] || !row[1] || !row[2] || !row[3]) {
75         printf_mysql_query("SELECT `channel_flood_reaction`, `channel_flood_reaction_duration`, `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'");
76         res = mysql_use();
77         defaults = mysql_fetch_row(res);
78     }
79     if(!(settings->flags & SPAMSETTINGS_JOINSCAN_OPS) && uaccess >= atoi((row[2] ? row[2] : defaults[2]))) return;
80     if(!(settings->flags & SPAMSETTINGS_JOINSCAN_VOICE) && uaccess >= atoi((row[3] ? row[3] : defaults[3]))) return;
81     char reason[MAXLEN];
82     sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_JOINFLOOD);
83     if(action == SPAMSERV_CHECK_WARN) {
84         reply(client, chanuser->user, "%s", reason);
85     } else if(action == SPAMSERV_CHECK_PUNISH) {
86         int duration = atoi((row[1] ? row[1] : defaults[1]));
87         char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
88         char *banmask = NULL;
89         switch (atoi((row[0] ? row[0] : defaults[0]))) {
90             case 3: //TIMEBAN: 1h
91                 banmask = generate_banmask(chanuser->user, banmaskBuf);
92                 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) (duration ? (time(0) + duration) : 0), 0, escape_string(reason));
93                 if(duration) {
94                     int banid = (int) mysql_insert_id(mysql_conn);
95                     char nameBuf[MAXLEN];
96                     char banidBuf[20];
97                     sprintf(nameBuf, "ban_%d", banid);
98                     sprintf(banidBuf, "%d", banid);
99                     timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
100                 }
101             case 1: //KICKBAN
102                 if(!banmask)
103                     banmask = generate_banmask(chanuser->user, banmaskBuf);
104                 putsock(client, "MODE %s +b %s", chanuser->chan->name, banmask);
105             case 0: //KICK
106                 putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, reason);
107                 break;
108         }
109     }
110 }
111
112 static int neonspam_update_join_penalty(struct NeonSpamSettings *settings, struct NeonSpamJoinNode *joinnode, int addjoin) {
113     int last_update = time(0) - joinnode->last_penalty_update;
114     if(last_update) {
115         if(last_update < MAX_JOIN_TIME && joinnode->joinpenalty) {
116             joinnode->joinpenalty -= last_update * (MAX_JOIN_TIME / settings->sensibility_time[SPAMSETTINGS_JOINSENINDEX]);
117             if(joinnode->joinpenalty < 0)
118                 joinnode->joinpenalty = 0;
119         } else
120             joinnode->joinpenalty = 0;
121         joinnode->last_penalty_update = time(0);
122     }
123     joinnode->joinpenalty += MAX_JOIN_TIME * addjoin;
124     return joinnode->joinpenalty / MAX_JOIN_TIME + ((joinnode->joinpenalty % MAX_JOIN_TIME) ? 1 : 0);
125 }
126
127 static int neonspam_joinfloodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser) {
128     if(!chanuser->spamnode)
129         createSpamNode(chanuser);
130     int joins_pending = neonspam_update_join_penalty(settings, getNeonSpamJoinNode(chanuser), 1);
131     if(joins_pending == settings->sensibility_amount[SPAMSETTINGS_JOINSENINDEX])
132         return SPAMSERV_CHECK_WARN;
133     else if(joins_pending > settings->sensibility_amount[SPAMSETTINGS_JOINSENINDEX])
134         return SPAMSERV_CHECK_PUNISH;
135     else
136         return SPAMSERV_CHECK_IGNORE;
137 }
138
139