--- /dev/null
+/* event_neonspam_join.c - NeonServ v5.1
+ * Copyright (C) 2011 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, char *reason, char *reaction);
+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;
+ char *reason;
+ char *reaction;
+};
+
+static void neonspam_event_join(struct ChanUser *chanuser) {
+ struct ClientSocket *client = getChannelBot(chanuser->chan, BOTID);
+ if(!client) return; //we can't "see" this event
+ loadNeonSpamSettings(chanuser->chan);
+ struct NeonSpamSettings *settings = chanuser->chan->spam_settings;
+ if(!settings) return;
+ if(settings->exceptlevel == 0) return;
+ //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_FLOODSCAN)) {
+ result = neonspam_joinfloodscan(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);
+ }
+ break;
+ case SPAMSERV_CHECK_PUNISH:
+ if(action != SPAMSERV_CHECK_PUNISH) {
+ action = result;
+ sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_FLOOD);
+ reaction = "channel_joinreaction";
+ }
+ break;
+ }
+ }
+ //some other checks?
+ if(action != SPAMSERV_CHECK_IGNORE) {
+ //whois the user to check against exceptlevel
+ if((chanuser->user->flags & USERFLAG_ISAUTHED) || settings->exceptlevel == 501) {
+ neonspam_event_join_punish(client, chanuser, settings, action, reason, reaction);
+ } 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 = action;
+ cache->reason = strdup(reason);
+ cache->reaction = reaction;
+ 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, cache->reason, cache->reaction);
+ free(cache->reason);
+ free(cache);
+}
+
+static void neonspam_event_join_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction) {
+ 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) {
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ loadChannelSettings(chanuser->chan);
+ printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", reaction, chanuser->chan->channel_id);
+ 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);
+ }
+ int duration = 0;
+ char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
+ char *banmask = NULL;
+ switch (atoi(row[0])) {
+ case 2: //TIMEBAN: 3min
+ duration = 180;
+ 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
+ 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->join_time);
+ 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->join_amount)
+ return SPAMSERV_CHECK_WARN;
+ else if(joins_pending > settings->join_amount)
+ return SPAMSERV_CHECK_PUNISH;
+ else
+ return SPAMSERV_CHECK_IGNORE;
+}
+
+