1 /* event_neonspam_join.c - NeonServ v5.1
2 * Copyright (C) 2011 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 static USERAUTH_CALLBACK(neonspam_event_join_nick_lookup);
19 static void neonspam_event_join_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction);
20 static int neonspam_joinfloodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser);
22 struct neonspam_event_join_cache {
23 struct ClientSocket *client;
24 struct ChanUser *chanuser;
25 struct NeonSpamSettings *settings;
31 static void neonspam_event_join(struct ChanUser *chanuser) {
32 struct ClientSocket *client = getChannelBot(chanuser->chan, BOTID);
33 if(!client) return; //we can't "see" this event
34 loadNeonSpamSettings(chanuser->chan);
35 struct NeonSpamSettings *settings = chanuser->chan->spam_settings;
37 if(settings->exceptlevel == 0) return;
40 int action = SPAMSERV_CHECK_IGNORE;
42 char *reaction = NULL;
43 if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_JOINSCAN)) {
44 result = neonspam_joinfloodscan(settings, chanuser);
46 case SPAMSERV_CHECK_IGNORE:
48 case SPAMSERV_CHECK_WARN:
49 if(action == SPAMSERV_CHECK_IGNORE) {
51 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_JOINFLOOD);
54 case SPAMSERV_CHECK_PUNISH:
55 if(action != SPAMSERV_CHECK_PUNISH) {
57 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_JOINFLOOD);
58 reaction = "channel_joinreaction";
64 if(action != SPAMSERV_CHECK_IGNORE) {
65 //whois the user to check against exceptlevel
66 if((chanuser->user->flags & USERFLAG_ISAUTHED) || settings->exceptlevel == 501) {
67 neonspam_event_join_punish(client, chanuser, settings, action, reason, reaction);
69 struct neonspam_event_join_cache *cache = malloc(sizeof(*cache));
71 perror("malloc() failed");
74 cache->client = client;
75 cache->chanuser = chanuser;
76 cache->settings = settings;
77 cache->action = action;
78 cache->reason = strdup(reason);
79 cache->reaction = reaction;
80 get_userauth(chanuser->user, neonspam_event_join_nick_lookup, cache);
86 static USERAUTH_CALLBACK(neonspam_event_join_nick_lookup) {
87 struct neonspam_event_join_cache *cache = data;
88 neonspam_event_join_punish(cache->client, cache->chanuser, cache->settings, cache->action, cache->reason, cache->reaction);
93 static void neonspam_event_join_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction) {
95 if(chanuser->user->flags & USERFLAG_ISAUTHED)
96 uaccess = getChannelAccess(chanuser->user, chanuser->chan, 0);
97 if(uaccess >= settings->exceptlevel) return;
98 if(action == SPAMSERV_CHECK_WARN) {
99 reply(client, chanuser->user, "%s", reason);
100 } else if(action == SPAMSERV_CHECK_PUNISH) {
103 loadChannelSettings(chanuser->chan);
104 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", reaction, chanuser->chan->channel_id);
106 row = mysql_fetch_row(res);
108 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", reaction);
110 row = mysql_fetch_row(res);
113 char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
114 char *banmask = NULL;
115 switch (atoi(row[0])) {
116 case 2: //TIMEBAN: 3min
118 case 3: //TIMEBAN: 1h
121 banmask = generate_banmask(chanuser->user, banmaskBuf);
122 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));
123 int banid = (int) mysql_insert_id(mysql_conn);
124 char nameBuf[MAXLEN];
126 sprintf(nameBuf, "ban_%d", banid);
127 sprintf(banidBuf, "%d", banid);
128 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
131 banmask = generate_banmask(chanuser->user, banmaskBuf);
132 putsock(client, "MODE %s +b %s", chanuser->chan->name, banmask);
134 putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, reason);
140 static int neonspam_update_join_penalty(struct NeonSpamSettings *settings, struct NeonSpamJoinNode *joinnode, int addjoin) {
141 int last_update = time(0) - joinnode->last_penalty_update;
143 if(last_update < MAX_JOIN_TIME && joinnode->joinpenalty) {
144 joinnode->joinpenalty -= last_update * (MAX_JOIN_TIME / settings->join_time);
145 if(joinnode->joinpenalty < 0)
146 joinnode->joinpenalty = 0;
148 joinnode->joinpenalty = 0;
149 joinnode->last_penalty_update = time(0);
151 joinnode->joinpenalty += MAX_JOIN_TIME * addjoin;
152 return joinnode->joinpenalty / MAX_JOIN_TIME + ((joinnode->joinpenalty % MAX_JOIN_TIME) ? 1 : 0);
155 static int neonspam_joinfloodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser) {
156 if(!chanuser->spamnode)
157 createSpamNode(chanuser);
158 int joins_pending = neonspam_update_join_penalty(settings, getNeonSpamJoinNode(chanuser), 1);
159 if(joins_pending == settings->join_amount)
160 return SPAMSERV_CHECK_WARN;
161 else if(joins_pending > settings->join_amount)
162 return SPAMSERV_CHECK_PUNISH;
164 return SPAMSERV_CHECK_IGNORE;