added joinflood scanner
[NeonServV5.git] / src / bot_NeonSpam.c
index d4a10d44fcd27b64e0366952d0d5a33f459d3689..5a200c1f3dd5ef7d8f1b93e787fe214bf93424dd 100644 (file)
@@ -46,9 +46,21 @@ static const struct default_language_entry msgtab[] = {
 
 static int loadNeonSpamSettings(struct ChanNode *chan);
 static void createSpamNode(struct ChanUser *chanuser);
+static void freeJoinNode(struct NeonSpamJoinNode *joinnode);
+static struct NeonSpamJoinNode *getNeonSpamJoinNode(struct ChanUser *chanuser);
+
+#define SPAMSERV_CHECK_IGNORE 0
+#define SPAMSERV_CHECK_WARN   1
+#define SPAMSERV_CHECK_PUNISH 2
+
+#define SPAMSERV_MSG_SPAM       "Spamming"
+#define SPAMSERV_MSG_FLOOD      "Flooding the channel/network"
+#define SPAMSERV_MSG_ADV        "Advertising"
+#define SPAMSERV_MSG_JOINFLOOD  "Join flooding the channel"
+#define SPAMSERV_MSG_WARNING    "%s is against the channel rules"
 
 //EVENTS
-//#include "event_neonspam_join.c"
+#include "event_neonspam_join.c"
 #include "event_neonspam_chanmsg.c"
 
 static void neonspam_bot_ready(struct ClientSocket *client) {
@@ -126,6 +138,7 @@ static int loadNeonSpamSettings(struct ChanNode *chan) {
     }
     MYSQL_RES *res;
     MYSQL_ROW row, defaults = NULL;
+    loadChannelSettings(chan);
     printf_mysql_query("SELECT `channel_scanstate`, `channel_maxrepeat`, `channel_maxflood`, `channel_floodtime`, `channel_maxjoin`, `channel_jointime`, `channel_scanexcept` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
     res = mysql_use();
     row = mysql_fetch_row(res);
@@ -141,10 +154,58 @@ static int loadNeonSpamSettings(struct ChanNode *chan) {
     settings->join_amount = atoi(row[4] ? row[4] : defaults[4]);
     settings->join_time = atoi(row[5] ? row[5] : defaults[5]);
     settings->exceptlevel = atoi(row[6] ? row[6] : defaults[6]);
+    settings->join_nodes = NULL;
     chan->spam_settings = settings;
     return 1;
 }
 
+void freeNeonSpamSettings(struct NeonSpamSettings *settings) {
+    struct NeonSpamJoinNode *joinnode, *nextjoinnode;
+    for(joinnode = settings->join_nodes; joinnode; joinnode = nextjoinnode) {
+        nextjoinnode = joinnode->next;
+        freeJoinNode(joinnode);
+    }
+    free(settings);
+}
+
+static void freeJoinNode(struct NeonSpamJoinNode *joinnode) {
+    free(joinnode->ident);
+    free(joinnode->host);
+    free(joinnode);
+}
+
+static struct NeonSpamJoinNode *getNeonSpamJoinNode(struct ChanUser *chanuser) {
+    struct NeonSpamJoinNode *joinnode, *prevjoinnode = NULL, *nextjoinnode, *result = NULL;
+    for(joinnode = chanuser->chan->spam_settings->join_nodes; joinnode; joinnode = nextjoinnode) {
+        nextjoinnode = joinnode->next;
+        if(!stricmp(joinnode->ident, chanuser->user->ident) && !stricmp(joinnode->host, chanuser->user->host)) {
+            prevjoinnode = joinnode;
+            result = joinnode;
+        } else if(time(0) - joinnode->last_penalty_update > MAX_JOIN_TIME) {
+            freeJoinNode(joinnode);
+            if(prevjoinnode)
+                prevjoinnode->next = nextjoinnode;
+            else
+                chanuser->chan->spam_settings->join_nodes = nextjoinnode;
+        } else 
+            prevjoinnode = joinnode;
+    }
+    if(result)
+        return result;
+    joinnode = malloc(sizeof(*joinnode));
+    if(!joinnode) {
+        perror("malloc() failed");
+        return NULL;
+    }
+    joinnode->ident = strdup(chanuser->user->ident);
+    joinnode->host = strdup(chanuser->user->host);
+    joinnode->last_penalty_update = time(0);
+    joinnode->joinpenalty = 0;
+    joinnode->next = chanuser->chan->spam_settings->join_nodes;
+    chanuser->chan->spam_settings->join_nodes = joinnode;
+    return joinnode;
+}
+
 static void createSpamNode(struct ChanUser *chanuser) {
     struct NeonSpamNode *spamnode = malloc(sizeof(*spamnode));
     if(!spamnode) {
@@ -186,7 +247,7 @@ void init_NeonSpam() {
     
     //register events
     bind_bot_ready(neonspam_bot_ready);
-    //bind_join(neonspam_event_join);
+    bind_join(neonspam_event_join);
     bind_chanmsg(neonspam_event_chanmsg);
     bind_privctcp(general_event_privctcp);