1 /* event_neonspam_join.c - NeonServ v5.3
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
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);
22 struct neonspam_event_join_cache {
23 struct ClientSocket *client;
24 struct ChanUser *chanuser;
25 struct NeonSpamSettings *settings;
29 static void neonspam_event_join(struct ChanUser *chanuser) {
30 if(chanuser->user->flags & USERFLAG_WAS_REGISTRING) return;
31 struct ClientSocket *client = getChannelBot(chanuser->chan, BOTID);
32 if(!client) return; //we can't "see" this event
33 if(chanuser->user == client->user) {
34 requestOp(client->user, chanuser->chan);
37 if(chanuser->user->flags & USERFLAG_ISBOT) return;
38 loadNeonSpamSettings(chanuser->chan);
39 struct NeonSpamSettings *settings = chanuser->chan->spam_settings;
40 if(!settings || !(settings->flags & SPAMSETTINGS_JOINSCAN)) return;
41 if(settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX] == 0) return;
42 int result = neonspam_joinfloodscan(settings, chanuser);
43 if(result != SPAMSERV_CHECK_IGNORE) {
44 //whois the user to check against exceptlevel
45 if((chanuser->user->flags & USERFLAG_ISAUTHED) || settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX] == 501) {
46 neonspam_event_join_punish(client, chanuser, settings, result);
48 struct neonspam_event_join_cache *cache = malloc(sizeof(*cache));
50 perror("malloc() failed");
53 cache->client = client;
54 cache->chanuser = chanuser;
55 cache->settings = settings;
56 cache->action = result;
57 get_userauth(chanuser->user, neonspam_event_join_nick_lookup, cache);
62 static USERAUTH_CALLBACK(neonspam_event_join_nick_lookup) {
63 struct neonspam_event_join_cache *cache = data;
64 neonspam_event_join_punish(cache->client, cache->chanuser, cache->settings, cache->action);
68 static void neonspam_event_join_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action) {
70 if(chanuser->user->flags & USERFLAG_ISAUTHED)
71 uaccess = getChannelAccess(chanuser->user, chanuser->chan);
72 if(uaccess >= settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX]) return;
73 //scanops / scanvoiced
75 MYSQL_ROW row, defaults = NULL;
76 loadChannelSettings(chanuser->chan);
77 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);
79 row = mysql_fetch_row(res);
80 if(!row[0] || !row[1] || !row[2] || !row[3]) {
81 printf_mysql_query("SELECT `channel_flood_reaction`, `channel_flood_reaction_duration`, `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'");
83 defaults = mysql_fetch_row(res);
85 if(!(settings->flags & SPAMSETTINGS_JOINSCAN_OPS) && uaccess >= atoi((row[2] ? row[2] : defaults[2]))) return;
86 if(!(settings->flags & SPAMSETTINGS_JOINSCAN_VOICE) && uaccess >= atoi((row[3] ? row[3] : defaults[3]))) return;
88 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_JOINFLOOD);
89 if(action == SPAMSERV_CHECK_WARN) {
90 reply(client, chanuser->user, "%s", reason);
91 } else if(action == SPAMSERV_CHECK_PUNISH) {
92 int duration = atoi((row[1] ? row[1] : defaults[1]));
93 char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
95 switch (atoi((row[0] ? row[0] : defaults[0]))) {
97 banmask = generate_banmask(chanuser->user, banmaskBuf);
98 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));
100 int banid = (int) mysql_insert_id(get_mysql_conn());
101 char nameBuf[MAXLEN];
103 sprintf(nameBuf, "ban_%d", banid);
104 sprintf(banidBuf, "%d", banid);
105 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
109 banmask = generate_banmask(chanuser->user, banmaskBuf);
110 putsock(client, "MODE %s +b %s", chanuser->chan->name, banmask);
112 putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, reason);
118 static int neonspam_update_join_penalty(struct NeonSpamSettings *settings, struct NeonSpamJoinNode *joinnode, int addjoin) {
119 int last_update = time(0) - joinnode->last_penalty_update;
121 if(last_update < MAX_JOIN_TIME && joinnode->joinpenalty) {
122 joinnode->joinpenalty -= last_update * (MAX_JOIN_TIME / settings->sensibility_time[SPAMSETTINGS_JOINSENINDEX]);
123 if(joinnode->joinpenalty < 0)
124 joinnode->joinpenalty = 0;
126 joinnode->joinpenalty = 0;
127 joinnode->last_penalty_update = time(0);
129 joinnode->joinpenalty += MAX_JOIN_TIME * addjoin;
130 return joinnode->joinpenalty / MAX_JOIN_TIME + ((joinnode->joinpenalty % MAX_JOIN_TIME) ? 1 : 0);
133 static int neonspam_joinfloodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser) {
134 if(!chanuser->spamnode)
135 createSpamNode(chanuser);
136 int joins_pending = neonspam_update_join_penalty(settings, getNeonSpamJoinNode(chanuser), 1);
137 if(joins_pending == settings->sensibility_amount[SPAMSETTINGS_JOINSENINDEX])
138 return SPAMSERV_CHECK_WARN;
139 else if(joins_pending > settings->sensibility_amount[SPAMSETTINGS_JOINSENINDEX])
140 return SPAMSERV_CHECK_PUNISH;
142 return SPAMSERV_CHECK_IGNORE;