+++ /dev/null
-/* event_neonspam_chanmsg.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 int neonspam_spamscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message);
-static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser);
-static int neonspam_botnetscan(struct ClientSocket *client, struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message);
-static int neonspam_capsscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message);
-static int neonspam_digitscan(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, unsigned int warn, unsigned int punish);
-
-struct neonspam_event_chanmsg_cache {
- struct ClientSocket *client;
- struct ChanUser *chanuser;
- struct NeonSpamSettings *settings;
- 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 || (user->flags & USERFLAG_ISBOT)) return; //we can't "see" this event
- loadNeonSpamSettings(chan);
- struct NeonSpamSettings *settings = chan->spam_settings;
- struct ChanUser *chanuser = getChanUser(user, chan);
- if(!settings || !chanuser) 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;
- 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:
- warn |= SPAMSETTINGS_SPAMSCAN;
- if(NEONSPAM_CHANMSG_NEED_WHO(SPAMSETTINGS_SPAMEXCINDEX))
- needwho = 1;
- break;
- case SPAMSERV_CHECK_PUNISH:
- punish |= SPAMSETTINGS_SPAMSCAN;
- if(NEONSPAM_CHANMSG_NEED_WHO(SPAMSETTINGS_SPAMEXCINDEX))
- needwho = 1;
- break;
- }
- }
- 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:
- warn |= SPAMSETTINGS_FLOODSCAN;
- if(NEONSPAM_CHANMSG_NEED_WHO(SPAMSETTINGS_FLOODEXCINDEX))
- needwho = 1;
- break;
- case SPAMSERV_CHECK_PUNISH:
- punish |= SPAMSETTINGS_FLOODSCAN;
- if(NEONSPAM_CHANMSG_NEED_WHO(SPAMSETTINGS_FLOODEXCINDEX))
- needwho = 1;
- break;
- }
- }
- 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:
- return;
- case SPAMSERV_CHECK_IGNORE:
- break;
- }
- }
- if((settings->flags & SPAMSETTINGS_CAPSSCAN) && NEONSPAM_CHANMSG_DO_SCANOPS(SPAMSETTINGS_CAPSSCAN_OPS) && NEONSPAM_CHANMSG_DO_SCANVOICE(SPAMSETTINGS_CAPSSCAN_VOICE) && NEONSPAM_CHANMSG_DO_EXCEPT(SPAMSETTINGS_CAPSEXCINDEX)) {
- result = neonspam_capsscan(settings, chanuser, message);
- switch(result) {
- case SPAMSERV_CHECK_IGNORE:
- break;
- case SPAMSERV_CHECK_WARN:
- warn |= SPAMSETTINGS_CAPSSCAN;
- if(NEONSPAM_CHANMSG_NEED_WHO(SPAMSETTINGS_CAPSEXCINDEX))
- needwho = 1;
- break;
- case SPAMSERV_CHECK_PUNISH:
- punish |= SPAMSETTINGS_CAPSSCAN;
- if(NEONSPAM_CHANMSG_NEED_WHO(SPAMSETTINGS_CAPSEXCINDEX))
- needwho = 1;
- break;
- }
- }
- if((settings->flags & SPAMSETTINGS_DIGITSCAN) && NEONSPAM_CHANMSG_DO_SCANOPS(SPAMSETTINGS_DIGITSCAN_OPS) && NEONSPAM_CHANMSG_DO_SCANVOICE(SPAMSETTINGS_DIGITSCAN_VOICE) && NEONSPAM_CHANMSG_DO_EXCEPT(SPAMSETTINGS_DIGITEXCINDEX)) {
- result = neonspam_digitscan(settings, chanuser, message);
- switch(result) {
- case SPAMSERV_CHECK_IGNORE:
- break;
- case SPAMSERV_CHECK_WARN:
- warn |= SPAMSETTINGS_DIGITSCAN;
- if(NEONSPAM_CHANMSG_NEED_WHO(SPAMSETTINGS_DIGITEXCINDEX))
- needwho = 1;
- break;
- case SPAMSERV_CHECK_PUNISH:
- punish |= SPAMSETTINGS_DIGITSCAN;
- if(NEONSPAM_CHANMSG_NEED_WHO(SPAMSETTINGS_DIGITEXCINDEX))
- needwho = 1;
- break;
- }
- }
- //some other checks?
-
- if(warn || punish) {
- //whois the user to check against exceptlevel
- 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) {
- perror("malloc() failed");
- return;
- }
- cache->client = client;
- cache->chanuser = chanuser;
- cache->settings = settings;
- 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->warn, cache->punish);
- free(cache);
-}
-
-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, defaults;
- loadChannelSettings(chanuser->chan);
- int uaccess = 0;
- if(chanuser->user->flags & USERFLAG_ISAUTHED)
- uaccess = getChannelAccess(chanuser->user, chanuser->chan);
- 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] || !row[1]) {
- printf_mysql_query("SELECT `channel_spam_reaction`, `channel_spam_reaction_duration` FROM `channels` WHERE `channel_name` = 'defaults'");
- res = mysql_use();
- defaults = mysql_fetch_row(res);
- }
- sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
- punishment = atoi((row[0] ? row[0] : defaults[0])) + 1;
- punish_time = atoi((row[1] ? row[1] : defaults[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] || !row[1]) {
- printf_mysql_query("SELECT `channel_flood_reaction`, `channel_flood_reaction_duration` FROM `channels` WHERE `channel_name` = 'defaults'");
- res = mysql_use();
- defaults = mysql_fetch_row(res);
- }
- sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_FLOOD);
- punishment = atoi((row[0] ? row[0] : defaults[0])) + 1;
- punish_time = atoi((row[1] ? row[1] : defaults[1]));
- }
- if(!punishment && (punish & SPAMSETTINGS_CAPSSCAN) && settings->exceptlevel[SPAMSETTINGS_CAPSEXCINDEX] > uaccess) {
- printf_mysql_query("SELECT `channel_caps_reaction`, `channel_caps_reaction_duration` FROM `channels` WHERE `channel_id` = '%d'", chanuser->chan->channel_id);
- res = mysql_use();
- row = mysql_fetch_row(res);
- if(!row[0] || !row[1]) {
- printf_mysql_query("SELECT `channel_caps_reaction`, `channel_caps_reaction_duration` FROM `channels` WHERE `channel_name` = 'defaults'");
- res = mysql_use();
- defaults = mysql_fetch_row(res);
- }
- sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_CAPS);
- punishment = atoi((row[0] ? row[0] : defaults[0])) + 1;
- punish_time = atoi((row[1] ? row[1] : defaults[1]));
- }
- if(!punishment && (punish & SPAMSETTINGS_DIGITSCAN) && settings->exceptlevel[SPAMSETTINGS_DIGITEXCINDEX] > uaccess) {
- printf_mysql_query("SELECT `channel_digit_reaction`, `channel_digit_reaction_duration` FROM `channels` WHERE `channel_id` = '%d'", chanuser->chan->channel_id);
- res = mysql_use();
- row = mysql_fetch_row(res);
- if(!row[0] || !row[1]) {
- printf_mysql_query("SELECT `channel_digit_reaction`, `channel_digit_reaction_duration` FROM `channels` WHERE `channel_name` = 'defaults'");
- res = mysql_use();
- defaults = mysql_fetch_row(res);
- }
- sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_DIGIT);
- punishment = atoi((row[0] ? row[0] : defaults[0])) + 1;
- punish_time = atoi((row[1] ? row[1] : defaults[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 && (warn & SPAMSETTINGS_CAPSSCAN) && settings->exceptlevel[SPAMSETTINGS_CAPSEXCINDEX] > uaccess) {
- sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_CAPS);
- }
- if(!punishment && (warn & SPAMSETTINGS_DIGITSCAN) && settings->exceptlevel[SPAMSETTINGS_DIGITEXCINDEX] > uaccess) {
- sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_DIGIT);
- }
- if(punishment) {
- char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
- char *banmask = NULL;
- switch (punishment) {
- case 3: //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) (punish_time ? (time(0) + punish_time) : 0), 0, escape_string(reason));
- if(punish_time) {
- 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, 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 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) {
- //crc32 hash of the message
- unsigned long msghash = crc32(message);
- if(chanuser->spamnode) {
- if(chanuser->spamnode->lastmsg == msghash) {
- chanuser->spamnode->spamcount++;
- if(chanuser->spamnode->spamcount == settings->spam_amount)
- return SPAMSERV_CHECK_WARN;
- else if(chanuser->spamnode->spamcount > settings->spam_amount)
- return SPAMSERV_CHECK_PUNISH;
- else
- return SPAMSERV_CHECK_IGNORE;
- }
- } else
- createSpamNode(chanuser);
- chanuser->spamnode->lastmsg = msghash;
- chanuser->spamnode->spamcount = 1;
- return SPAMSERV_CHECK_IGNORE;
-}
-
-static int neonspam_update_penalty(struct NeonSpamSettings *settings, struct ChanUser *chanuser, int addmsg) {
- 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->sensibility_time[SPAMSETTINGS_FLOODSENINDEX]);
- if(chanuser->spamnode->floodpenalty < 0)
- chanuser->spamnode->floodpenalty = 0;
- } else
- chanuser->spamnode->floodpenalty = 0;
- chanuser->spamnode->last_penalty_update = time(0);
- }
- chanuser->spamnode->floodpenalty += MAX_FLOOD_TIME * addmsg;
- return chanuser->spamnode->floodpenalty / MAX_FLOOD_TIME + ((chanuser->spamnode->floodpenalty % MAX_FLOOD_TIME) ? 1 : 0);
-}
-
-static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser) {
- if(!chanuser->spamnode)
- createSpamNode(chanuser);
- int messages_pending = neonspam_update_penalty(settings, chanuser, 1);
- if(messages_pending == settings->sensibility_amount[SPAMSETTINGS_FLOODSENINDEX])
- return SPAMSERV_CHECK_WARN;
- else if(messages_pending > settings->sensibility_amount[SPAMSETTINGS_FLOODSENINDEX])
- return SPAMSERV_CHECK_PUNISH;
- else
- return SPAMSERV_CHECK_IGNORE;
-}
-
-static int neonspam_botnetscan(struct ClientSocket *client, struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message) {
- //crc32 hash of the message
- unsigned long msghash = crc32(message);
- if((time(0) - settings->lastmsg_time) > BOTNETSCAN_TIME || settings->lastmsg != msghash) {
- int i;
- for(i = 0; i < BOTNETSCAN_USERS; i++) {
- if(settings->botnicks[i]) {
- free(settings->botnicks[i]);
- settings->botnicks[i] = NULL;
- }
- }
- settings->flags &= ~SPAMSETTINGS_KICKEDBOTQUEUE;
- settings->lastmsg = msghash;
- } else if(settings->lastmsg == msghash) {
- int i;
- for(i = 0; i < BOTNETSCAN_USERS; i++) {
- if(!settings->botnicks[i]) {
- settings->botnicks[i] = strdup(chanuser->user->nick);
- break;
- } else if(!stricmp(chanuser->user->nick, settings->botnicks[i])) {
- return SPAMSERV_CHECK_IGNORE;
- }
- }
- if(i == BOTNETSCAN_USERS) {
- //BOTNETSCAN_USERS exceeded
- if(!(settings->flags & SPAMSETTINGS_KICKEDBOTQUEUE)) {
- for(i = 0; i < BOTNETSCAN_USERS; i++) {
- putsock(client, "KICK %s %s :%s", chanuser->chan->name, settings->botnicks[i], SPAMSERV_MSG_BOTNET);
- }
- settings->flags |= SPAMSETTINGS_KICKEDBOTQUEUE;
- }
- putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, SPAMSERV_MSG_BOTNET);
- return SPAMSERV_CHECK_DEAD;
- }
- }
- settings->lastmsg_time = time(0);
- return SPAMSERV_CHECK_IGNORE;
-}
-
-static int neonspam_capsscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message) {
- int caps = 0, msglen = strlen(message);
- int i;
- if(msglen <= 4) return SPAMSERV_CHECK_IGNORE;
- for(i = 0; i < msglen; i++) {
- if(isupper(message[i])) caps++;
- }
- caps = 100*caps/msglen;
- if(caps >= settings->percent[SPAMSETTINGS_CAPSPERCENTINDEX]) {
- if(!chanuser->spamnode)
- createSpamNode(chanuser);
- if(chanuser->spamnode->flags & NEONSPAMNODE_FLAG_CAPSSCAN_WARNED)
- return SPAMSERV_CHECK_PUNISH;
- else {
- chanuser->spamnode->flags |= NEONSPAMNODE_FLAG_CAPSSCAN_WARNED;
- return SPAMSERV_CHECK_WARN;
- }
- }
- return SPAMSERV_CHECK_IGNORE;
-}
-
-static int neonspam_digitscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message) {
- int digit = 0, msglen = strlen(message);
- int i;
- if(msglen <= 4) return SPAMSERV_CHECK_IGNORE;
- for(i = 0; i < msglen; i++) {
- if(isdigit(message[i])) digit++;
- }
- digit = 100*digit/msglen;
- if(digit >= settings->percent[SPAMSETTINGS_DIGITPERCENTINDEX]) {
- if(!chanuser->spamnode)
- createSpamNode(chanuser);
- if(chanuser->spamnode->flags & NEONSPAMNODE_FLAG_DIGITSCAN_WARNED)
- return SPAMSERV_CHECK_PUNISH;
- else {
- chanuser->spamnode->flags |= NEONSPAMNODE_FLAG_DIGITSCAN_WARNED;
- return SPAMSERV_CHECK_WARN;
- }
- }
- return SPAMSERV_CHECK_IGNORE;
-}
-