completely changed the NeonSpam settings / scanner management
[NeonServV5.git] / src / event_neonspam_chanmsg.c
index 7bcd4b5016dcc6526cb31cee7f0d147c88791bed..b7f124fbf8154ed3f7f0069d5ab752e12674d4ac 100644 (file)
@@ -20,17 +20,18 @@ static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser
 static int neonspam_botnetscan(struct ClientSocket *client, struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message);
 
 static USERAUTH_CALLBACK(neonspam_event_chanmsg_nick_lookup);
-static void neonspam_event_chanmsg_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction);
+static void neonspam_event_chanmsg_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, unsigned int warn, unsigned int punish);
 
 struct neonspam_event_chanmsg_cache {
     struct ClientSocket *client;
     struct ChanUser *chanuser;
     struct NeonSpamSettings *settings;
-    int action;
-    char *reason;
-    char *reaction;
+    unsigned int warn;
+    unsigned int punish;
 };
 
+
+
 static void neonspam_event_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
     struct ClientSocket *client = getChannelBot(chan, BOTID);
     if(!client) return; //we can't "see" this event
@@ -38,57 +39,50 @@ static void neonspam_event_chanmsg(struct UserNode *user, struct ChanNode *chan,
     struct NeonSpamSettings *settings = chan->spam_settings;
     struct ChanUser *chanuser = getChanUser(user, chan);
     if(!settings || !chanuser) return;
-    
-    //ignore messages from ops/voices if we ignore them
-    if(!(settings->flags & SPAMSETTINGS_SCANOPS) && (chanuser->flags & CHANUSERFLAG_OPPED)) return;
-    if(!(settings->flags & SPAMSETTINGS_SCANVOICE) && (chanuser->flags & CHANUSERFLAG_VOICED)) return;
-    if(settings->exceptlevel == 0) return;
+    #define NEONSPAM_CHANMSG_DO_SCANOPS(FLAG) ((settings->flags & FLAG) || !(chanuser->flags & CHANUSERFLAG_OPPED))
+    #define NEONSPAM_CHANMSG_DO_SCANVOICE(FLAG) ((settings->flags & FLAG) || !(chanuser->flags & CHANUSERFLAG_VOICED))
+    #define NEONSPAM_CHANMSG_DO_EXCEPT(INDEX) (settings->exceptlevel[INDEX] != 0)
+    #define NEONSPAM_CHANMSG_NEED_WHO(INDEX) (settings->exceptlevel[INDEX] != 501)
     //scan the message
     int result = 0;
-    int action = SPAMSERV_CHECK_IGNORE;
-    char reason[MAXLEN];
-    char *reaction = NULL;
-    if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_SPAMSCAN)) {
+    unsigned int warn = 0;
+    unsigned int punish = 0;
+    int needwho = 0;
+    if((settings->flags & SPAMSETTINGS_SPAMSCAN) && NEONSPAM_CHANMSG_DO_SCANOPS(SPAMSETTINGS_SPAMSCAN_OPS) && NEONSPAM_CHANMSG_DO_SCANVOICE(SPAMSETTINGS_SPAMSCAN_VOICE) && NEONSPAM_CHANMSG_DO_EXCEPT(SPAMSETTINGS_SPAMEXCINDEX)) {
         result = neonspam_spamscan(settings, chanuser, message);
         switch(result) {
             case SPAMSERV_CHECK_IGNORE:
                 break;
             case SPAMSERV_CHECK_WARN:
-                if(action == SPAMSERV_CHECK_IGNORE) {
-                    action = result;
-                    sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
-                }
+                warn |= SPAMSETTINGS_SPAMSCAN;
+                if(NEONSPAM_CHANMSG_NEED_WHO(SPAMSETTINGS_SPAMEXCINDEX))
+                    needwho = 1;
                 break;
             case SPAMSERV_CHECK_PUNISH:
-                if(action != SPAMSERV_CHECK_PUNISH) {
-                    action = result;
-                    sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
-                    reaction = "channel_repeatreaction";
-                }
+                punish |= SPAMSETTINGS_SPAMSCAN;
+                if(NEONSPAM_CHANMSG_NEED_WHO(SPAMSETTINGS_SPAMEXCINDEX))
+                    needwho = 1;
                 break;
         }
     }
-    if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_FLOODSCAN)) {
+    if((settings->flags & SPAMSETTINGS_FLOODSCAN) && NEONSPAM_CHANMSG_DO_SCANOPS(SPAMSETTINGS_FLOODSCAN_OPS) && NEONSPAM_CHANMSG_DO_SCANVOICE(SPAMSETTINGS_FLOODSCAN_VOICE) && NEONSPAM_CHANMSG_DO_EXCEPT(SPAMSETTINGS_FLOODEXCINDEX)) {
         result = neonspam_floodscan(settings, chanuser);
         switch(result) {
             case SPAMSERV_CHECK_IGNORE:
                 break;
             case SPAMSERV_CHECK_WARN:
-                if(action == SPAMSERV_CHECK_IGNORE) {
-                    action = result;
-                    sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_FLOOD);
-                }
+                warn |= SPAMSETTINGS_FLOODSCAN;
+                if(NEONSPAM_CHANMSG_NEED_WHO(SPAMSETTINGS_FLOODEXCINDEX))
+                    needwho = 1;
                 break;
             case SPAMSERV_CHECK_PUNISH:
-                if(action != SPAMSERV_CHECK_PUNISH) {
-                    action = result;
-                    sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_FLOOD);
-                    reaction = "channel_floodreaction";
-                }
+                punish |= SPAMSETTINGS_FLOODSCAN;
+                if(NEONSPAM_CHANMSG_NEED_WHO(SPAMSETTINGS_FLOODEXCINDEX))
+                    needwho = 1;
                 break;
         }
     }
-    if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_BOTNETSCAN)) {
+    if((settings->flags & SPAMSETTINGS_BOTNETSCAN) && NEONSPAM_CHANMSG_DO_SCANOPS(SPAMSETTINGS_BOTNETSCAN_OPS) && NEONSPAM_CHANMSG_DO_SCANVOICE(SPAMSETTINGS_BOTNETSCAN_VOICE)) {
         result = neonspam_botnetscan(client, settings, chanuser, message);
         switch(result) {
             case SPAMSERV_CHECK_DEAD:
@@ -99,10 +93,10 @@ static void neonspam_event_chanmsg(struct UserNode *user, struct ChanNode *chan,
     }
     //some other checks?
     
-    if(action != SPAMSERV_CHECK_IGNORE) {
+    if(warn || punish) {
         //whois the user to check against exceptlevel
-        if((user->flags & USERFLAG_ISAUTHED) || settings->exceptlevel == 501) {
-            neonspam_event_chanmsg_punish(client, chanuser, settings, action, reason, reaction);
+        if(!needwho || (user->flags & USERFLAG_ISAUTHED)) {
+            neonspam_event_chanmsg_punish(client, chanuser, settings, warn, punish);
         } else {
             struct neonspam_event_chanmsg_cache *cache = malloc(sizeof(*cache));
             if (!cache) {
@@ -112,72 +106,92 @@ static void neonspam_event_chanmsg(struct UserNode *user, struct ChanNode *chan,
             cache->client = client;
             cache->chanuser = chanuser;
             cache->settings = settings;
-            cache->action = action;
-            cache->reason = strdup(reason);
-            cache->reaction = reaction;
+            cache->warn = warn;
+            cache->punish = punish;
             get_userauth(user, neonspam_event_chanmsg_nick_lookup, cache);
         }
         
     }
+    #undef NEONSPAM_CHANMSG_DO_SCANOPS
+    #undef NEONSPAM_CHANMSG_DO_SCANVOICE
+    #undef NEONSPAM_CHANMSG_DO_EXCEPT
+    #undef NEONSPAM_CHANMSG_NEED_WHO
 }
 
 static USERAUTH_CALLBACK(neonspam_event_chanmsg_nick_lookup) {
     struct neonspam_event_chanmsg_cache *cache = data;
-    neonspam_event_chanmsg_punish(cache->client, cache->chanuser, cache->settings, cache->action, cache->reason, cache->reaction);
-    free(cache->reason);
+    neonspam_event_chanmsg_punish(cache->client, cache->chanuser, cache->settings, cache->warn, cache->punish);
     free(cache);
 }
 
-static void neonspam_event_chanmsg_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction) {
+static void neonspam_event_chanmsg_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, unsigned int warn, unsigned int punish) {
+    MYSQL_RES *res;
+    MYSQL_ROW row;
+    loadChannelSettings(chanuser->chan);
     int uaccess = 0;
     if(chanuser->user->flags & USERFLAG_ISAUTHED)
         uaccess = getChannelAccess(chanuser->user, chanuser->chan, 0);
-    if(uaccess >= settings->exceptlevel) return;
-    if(action == SPAMSERV_CHECK_WARN) {
-        reply(client, chanuser->user, "%s", reason);
-    } else if(action == SPAMSERV_CHECK_PUNISH) {
-        loadChannelSettings(chanuser->chan);
-        int reactionid;
-        if(!(reactionid = atoi(reaction))) {
-            MYSQL_RES *res;
-            MYSQL_ROW row;
-            printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", reaction, chanuser->chan->channel_id);
+    char reason[MAXLEN];
+    reason[0] = '\0';
+    int punishment = 0;
+    int punish_time = 0;
+    if(!punishment && (punish & SPAMSETTINGS_SPAMSCAN) && settings->exceptlevel[SPAMSETTINGS_SPAMEXCINDEX] > uaccess) {
+        printf_mysql_query("SELECT `channel_spam_reaction`, `channel_spam_reaction_duration` FROM `channels` WHERE `channel_id` = '%d'", chanuser->chan->channel_id);
+        res = mysql_use();
+        row = mysql_fetch_row(res);
+        if(!row[0]) {
+            printf_mysql_query("SELECT `channel_spam_reaction`, `channel_spam_reaction_duration` FROM `channels` WHERE `channel_name` = 'defaults'");
             res = mysql_use();
             row = mysql_fetch_row(res);
-            if(!row[0]) {
-                printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", reaction);
-                res = mysql_use();
-                row = mysql_fetch_row(res);
-            }
-            reactionid = atoi(row[0]);
         }
-            
-        int duration = 0;
+        sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
+        punishment = atoi(row[0]) + 1;
+        punish_time = atoi(row[1]);
+    }
+    if(!punishment && (punish & SPAMSETTINGS_FLOODSCAN) && settings->exceptlevel[SPAMSETTINGS_FLOODEXCINDEX] > uaccess) {
+        printf_mysql_query("SELECT `channel_flood_reaction`, `channel_flood_reaction_duration` FROM `channels` WHERE `channel_id` = '%d'", chanuser->chan->channel_id);
+        res = mysql_use();
+        row = mysql_fetch_row(res);
+        if(!row[0]) {
+            printf_mysql_query("SELECT `channel_flood_reaction`, `channel_flood_reaction_duration` FROM `channels` WHERE `channel_name` = 'defaults'");
+            res = mysql_use();
+            row = mysql_fetch_row(res);
+        }
+        sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_FLOOD);
+        punishment = atoi(row[0]) + 1;
+        punish_time = atoi(row[1]);
+    }
+    if(!punishment && (warn & SPAMSETTINGS_SPAMSCAN) && settings->exceptlevel[SPAMSETTINGS_SPAMEXCINDEX] > uaccess) {
+        sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
+    }
+    if(!punishment && (warn & SPAMSETTINGS_FLOODSCAN) && settings->exceptlevel[SPAMSETTINGS_FLOODEXCINDEX] > uaccess) {
+        sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_FLOOD);
+    }
+    if(punishment) {
         char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
         char *banmask = NULL;
-        switch (reactionid) {
-            case 2: //TIMEBAN: 3min
-                duration = 180;
+        switch (punishment) {
             case 3: //TIMEBAN: 1h
-                if(!duration)
-                    duration = 3600;
                 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) (time(0) + duration), 0, escape_string(reason));
-                int banid = (int) mysql_insert_id(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
+                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) (punish_time ? (time(0) + punish_time) : 0), 0, escape_string(reason));
+                if(punish_time) {
+                    int banid = (int) mysql_insert_id(mysql_conn);
+                    char nameBuf[MAXLEN];
+                    char banidBuf[20];
+                    sprintf(nameBuf, "ban_%d", banid);
+                    sprintf(banidBuf, "%d", banid);
+                    timeq_add_name(nameBuf, punish_time, channel_ban_timeout, strdup(banidBuf));
+                }
+            case 2: //KICKBAN
                 if(!banmask)
                     banmask = generate_banmask(chanuser->user, banmaskBuf);
                 putsock(client, "MODE %s +b %s", chanuser->chan->name, banmask);
-            case 0: //KICK
+            case 1: //KICK
                 putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, reason);
                 break;
         }
-    }
+    } else if(*reason)
+        reply(client, chanuser->user, "%s", reason);
 }
 
 static int neonspam_spamscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message) {
@@ -204,7 +218,7 @@ static int neonspam_update_penalty(struct NeonSpamSettings *settings, struct Cha
     int last_update = time(0) - chanuser->spamnode->last_penalty_update;
     if(last_update) {
         if(last_update < MAX_FLOOD_TIME && chanuser->spamnode->floodpenalty) {
-            chanuser->spamnode->floodpenalty -= last_update * (MAX_FLOOD_TIME / settings->flood_time);
+            chanuser->spamnode->floodpenalty -= last_update * (MAX_FLOOD_TIME / settings->sensibility_time[SPAMSETTINGS_FLOODSENINDEX]);
             if(chanuser->spamnode->floodpenalty < 0)
                 chanuser->spamnode->floodpenalty = 0;
         } else
@@ -219,9 +233,9 @@ static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser
     if(!chanuser->spamnode)
         createSpamNode(chanuser);
     int messages_pending = neonspam_update_penalty(settings, chanuser, 1);
-    if(messages_pending == settings->flood_amount)
+    if(messages_pending == settings->sensibility_amount[SPAMSETTINGS_FLOODSENINDEX])
         return SPAMSERV_CHECK_WARN;
-    else if(messages_pending > settings->flood_amount)
+    else if(messages_pending > settings->sensibility_amount[SPAMSETTINGS_FLOODSENINDEX])
         return SPAMSERV_CHECK_PUNISH;
     else
         return SPAMSERV_CHECK_IGNORE;