rearranged NeonServ code to be modular
[NeonServV5.git] / src / modules / NeonSpam.mod / event_neonspam_join.c
diff --git a/src/modules/NeonSpam.mod/event_neonspam_join.c b/src/modules/NeonSpam.mod/event_neonspam_join.c
new file mode 100644 (file)
index 0000000..5a169f2
--- /dev/null
@@ -0,0 +1,145 @@
+/* event_neonspam_join.c - NeonServ v5.3
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+static USERAUTH_CALLBACK(neonspam_event_join_nick_lookup);
+static void neonspam_event_join_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action);
+static int neonspam_joinfloodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser);
+
+struct neonspam_event_join_cache {
+    struct ClientSocket *client;
+    struct ChanUser *chanuser;
+    struct NeonSpamSettings *settings;
+    int action;
+};
+
+static void neonspam_event_join(struct ChanUser *chanuser) {
+    if(chanuser->user->flags & USERFLAG_WAS_REGISTRING) return;
+    struct ClientSocket *client = getChannelBot(chanuser->chan, BOTID);
+    if(!client) return; //we can't "see" this event
+    if(chanuser->user == client->user) {
+        requestOp(client->user, chanuser->chan);
+        return;
+    }
+    if(chanuser->user->flags & USERFLAG_ISBOT) return;
+    loadNeonSpamSettings(chanuser->chan);
+    struct NeonSpamSettings *settings = chanuser->chan->spam_settings;
+    if(!settings || !(settings->flags & SPAMSETTINGS_JOINSCAN)) return;
+    if(settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX] == 0) return;
+    int result = neonspam_joinfloodscan(settings, chanuser);
+    if(result != SPAMSERV_CHECK_IGNORE) {
+        //whois the user to check against exceptlevel
+        if((chanuser->user->flags & USERFLAG_ISAUTHED) || settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX] == 501) {
+            neonspam_event_join_punish(client, chanuser, settings, result);
+        } else {
+            struct neonspam_event_join_cache *cache = malloc(sizeof(*cache));
+            if (!cache) {
+                perror("malloc() failed");
+                return;
+            }
+            cache->client = client;
+            cache->chanuser = chanuser;
+            cache->settings = settings;
+            cache->action = result;
+            get_userauth(chanuser->user, neonspam_event_join_nick_lookup, cache);
+        }
+    }
+}
+
+static USERAUTH_CALLBACK(neonspam_event_join_nick_lookup) {
+    struct neonspam_event_join_cache *cache = data;
+    neonspam_event_join_punish(cache->client, cache->chanuser, cache->settings, cache->action);
+    free(cache);
+}
+
+static void neonspam_event_join_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action) {
+    int uaccess = 0;
+    if(chanuser->user->flags & USERFLAG_ISAUTHED)
+        uaccess = getChannelAccess(chanuser->user, chanuser->chan);
+    if(uaccess >= settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX]) return;
+    //scanops / scanvoiced
+    MYSQL_RES *res;
+    MYSQL_ROW row, defaults = NULL;
+    loadChannelSettings(chanuser->chan);
+    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);
+    res = mysql_use();
+    row = mysql_fetch_row(res);
+    if(!row[0] || !row[1] || !row[2] || !row[3]) {
+        printf_mysql_query("SELECT `channel_flood_reaction`, `channel_flood_reaction_duration`, `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'");
+        res = mysql_use();
+        defaults = mysql_fetch_row(res);
+    }
+    if(!(settings->flags & SPAMSETTINGS_JOINSCAN_OPS) && uaccess >= atoi((row[2] ? row[2] : defaults[2]))) return;
+    if(!(settings->flags & SPAMSETTINGS_JOINSCAN_VOICE) && uaccess >= atoi((row[3] ? row[3] : defaults[3]))) return;
+    char reason[MAXLEN];
+    sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_JOINFLOOD);
+    if(action == SPAMSERV_CHECK_WARN) {
+        reply(client, chanuser->user, "%s", reason);
+    } else if(action == SPAMSERV_CHECK_PUNISH) {
+        int duration = atoi((row[1] ? row[1] : defaults[1]));
+        char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
+        char *banmask = NULL;
+        switch (atoi((row[0] ? row[0] : defaults[0]))) {
+            case 2: //TIMEBAN
+                banmask = generate_banmask(chanuser->user, banmaskBuf);
+                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));
+                if(duration) {
+                    int banid = (int) mysql_insert_id(get_mysql_conn());
+                    char nameBuf[MAXLEN];
+                    char banidBuf[20];
+                    sprintf(nameBuf, "ban_%d", banid);
+                    sprintf(banidBuf, "%d", banid);
+                    timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
+                }
+            case 1: //KICKBAN
+                if(!banmask)
+                    banmask = generate_banmask(chanuser->user, banmaskBuf);
+                putsock(client, "MODE %s +b %s", chanuser->chan->name, banmask);
+            case 0: //KICK
+                putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, reason);
+                break;
+        }
+    }
+}
+
+static int neonspam_update_join_penalty(struct NeonSpamSettings *settings, struct NeonSpamJoinNode *joinnode, int addjoin) {
+    int last_update = time(0) - joinnode->last_penalty_update;
+    if(last_update) {
+        if(last_update < MAX_JOIN_TIME && joinnode->joinpenalty) {
+            joinnode->joinpenalty -= last_update * (MAX_JOIN_TIME / settings->sensibility_time[SPAMSETTINGS_JOINSENINDEX]);
+            if(joinnode->joinpenalty < 0)
+                joinnode->joinpenalty = 0;
+        } else
+            joinnode->joinpenalty = 0;
+        joinnode->last_penalty_update = time(0);
+    }
+    joinnode->joinpenalty += MAX_JOIN_TIME * addjoin;
+    return joinnode->joinpenalty / MAX_JOIN_TIME + ((joinnode->joinpenalty % MAX_JOIN_TIME) ? 1 : 0);
+}
+
+static int neonspam_joinfloodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser) {
+    if(!chanuser->spamnode)
+        createSpamNode(chanuser);
+    int joins_pending = neonspam_update_join_penalty(settings, getNeonSpamJoinNode(chanuser), 1);
+    if(joins_pending == settings->sensibility_amount[SPAMSETTINGS_JOINSENINDEX])
+        return SPAMSERV_CHECK_WARN;
+    else if(joins_pending > settings->sensibility_amount[SPAMSETTINGS_JOINSENINDEX])
+        return SPAMSERV_CHECK_PUNISH;
+    else
+        return SPAMSERV_CHECK_IGNORE;
+}
+
+