1 /* event_neonspam_chanmsg.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 int neonspam_spamscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message);
19 static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser);
21 static USERAUTH_CALLBACK(neonspam_event_chanmsg_nick_lookup);
22 static void neonspam_event_chanmsg_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction);
24 struct neonspam_event_chanmsg_cache {
25 struct ClientSocket *client;
26 struct ChanUser *chanuser;
27 struct NeonSpamSettings *settings;
33 static void neonspam_event_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
34 struct ClientSocket *client = getChannelBot(chan, BOTID);
35 if(!client) return; //we can't "see" this event
36 loadNeonSpamSettings(chan);
37 struct NeonSpamSettings *settings = chan->spam_settings;
38 struct ChanUser *chanuser = getChanUser(user, chan);
39 if(!settings || !chanuser) return;
41 //ignore messages from ops/voices if we ignore them
42 if(!(settings->flags & SPAMSETTINGS_SCANOPS) && (chanuser->flags & CHANUSERFLAG_OPPED)) return;
43 if(!(settings->flags & SPAMSETTINGS_SCANVOICE) && (chanuser->flags & CHANUSERFLAG_VOICED)) return;
44 if(settings->exceptlevel == 0) return;
47 int action = SPAMSERV_CHECK_IGNORE;
49 char *reaction = NULL;
50 if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_SPAMSCAN)) {
51 result = neonspam_spamscan(settings, chanuser, message);
53 case SPAMSERV_CHECK_IGNORE:
55 case SPAMSERV_CHECK_WARN:
56 if(action == SPAMSERV_CHECK_IGNORE) {
58 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
61 case SPAMSERV_CHECK_PUNISH:
62 if(action != SPAMSERV_CHECK_PUNISH) {
64 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
65 reaction = "channel_repeatreaction";
70 if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_FLOODSCAN)) {
71 result = neonspam_floodscan(settings, chanuser);
73 case SPAMSERV_CHECK_IGNORE:
75 case SPAMSERV_CHECK_WARN:
76 if(action == SPAMSERV_CHECK_IGNORE) {
78 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_FLOOD);
81 case SPAMSERV_CHECK_PUNISH:
82 if(action != SPAMSERV_CHECK_PUNISH) {
84 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_FLOOD);
85 reaction = "channel_floodreaction";
92 if(action != SPAMSERV_CHECK_IGNORE) {
93 //whois the user to check against exceptlevel
94 if((user->flags & USERFLAG_ISAUTHED) || settings->exceptlevel == 501) {
95 neonspam_event_chanmsg_punish(client, chanuser, settings, action, reason, reaction);
97 struct neonspam_event_chanmsg_cache *cache = malloc(sizeof(*cache));
99 perror("malloc() failed");
102 cache->client = client;
103 cache->chanuser = chanuser;
104 cache->settings = settings;
105 cache->action = action;
106 cache->reason = strdup(reason);
107 cache->reaction = reaction;
108 get_userauth(user, neonspam_event_chanmsg_nick_lookup, cache);
114 static USERAUTH_CALLBACK(neonspam_event_chanmsg_nick_lookup) {
115 struct neonspam_event_chanmsg_cache *cache = data;
116 neonspam_event_chanmsg_punish(cache->client, cache->chanuser, cache->settings, cache->action, cache->reason, cache->reaction);
121 static void neonspam_event_chanmsg_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction) {
123 if(chanuser->user->flags & USERFLAG_ISAUTHED)
124 uaccess = getChannelAccess(chanuser->user, chanuser->chan, 0);
125 if(uaccess >= settings->exceptlevel) return;
126 if(action == SPAMSERV_CHECK_WARN) {
127 reply(client, chanuser->user, "%s", reason);
128 } else if(action == SPAMSERV_CHECK_PUNISH) {
131 loadChannelSettings(chanuser->chan);
132 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", reaction, chanuser->chan->channel_id);
134 row = mysql_fetch_row(res);
136 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", reaction);
138 row = mysql_fetch_row(res);
141 char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
142 char *banmask = NULL;
143 switch (atoi(row[0])) {
144 case 2: //TIMEBAN: 3min
146 case 3: //TIMEBAN: 1h
149 banmask = generate_banmask(chanuser->user, banmaskBuf);
150 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));
151 int banid = (int) mysql_insert_id(mysql_conn);
152 char nameBuf[MAXLEN];
154 sprintf(nameBuf, "ban_%d", banid);
155 sprintf(banidBuf, "%d", banid);
156 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
159 banmask = generate_banmask(chanuser->user, banmaskBuf);
160 putsock(client, "MODE %s +b %s", chanuser->chan->name, banmask);
162 putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, reason);
168 static int neonspam_spamscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message) {
169 //crc32 hash of the message
170 unsigned long msghash = crc32(message);
171 if(chanuser->spamnode) {
172 if(chanuser->spamnode->lastmsg == msghash) {
173 chanuser->spamnode->spamcount++;
174 if(chanuser->spamnode->spamcount == settings->spam_amount)
175 return SPAMSERV_CHECK_WARN;
176 else if(chanuser->spamnode->spamcount > settings->spam_amount)
177 return SPAMSERV_CHECK_PUNISH;
179 return SPAMSERV_CHECK_IGNORE;
182 createSpamNode(chanuser);
183 chanuser->spamnode->lastmsg = msghash;
184 chanuser->spamnode->spamcount = 1;
185 return SPAMSERV_CHECK_IGNORE;
188 static int neonspam_update_penalty(struct NeonSpamSettings *settings, struct ChanUser *chanuser, int addmsg) {
189 int last_update = time(0) - chanuser->spamnode->last_penalty_update;
191 if(last_update < MAX_FLOOD_TIME && chanuser->spamnode->floodpenalty) {
192 chanuser->spamnode->floodpenalty -= last_update * (MAX_FLOOD_TIME / settings->flood_time);
193 if(chanuser->spamnode->floodpenalty < 0)
194 chanuser->spamnode->floodpenalty = 0;
196 chanuser->spamnode->floodpenalty = 0;
197 chanuser->spamnode->last_penalty_update = time(0);
199 chanuser->spamnode->floodpenalty += MAX_FLOOD_TIME * addmsg;
200 return chanuser->spamnode->floodpenalty / MAX_FLOOD_TIME + ((chanuser->spamnode->floodpenalty % MAX_FLOOD_TIME) ? 1 : 0);
203 static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser) {
204 if(!chanuser->spamnode)
205 createSpamNode(chanuser);
206 int messages_pending = neonspam_update_penalty(settings, chanuser, 1);
207 if(messages_pending == settings->flood_amount)
208 return SPAMSERV_CHECK_WARN;
209 else if(messages_pending > settings->flood_amount)
210 return SPAMSERV_CHECK_PUNISH;
212 return SPAMSERV_CHECK_IGNORE;