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 #define SPAMSERV_CHECK_IGNORE 0
19 #define SPAMSERV_CHECK_WARN 1
20 #define SPAMSERV_CHECK_PUNISH 2
22 #define SPAMSERV_MSG_SPAM "Spamming"
23 #define SPAMSERV_MSG_FLOOD "Flooding the channel/network"
24 #define SPAMSERV_MSG_ADV "Advertising"
25 #define SPAMSERV_MSG_JOINFLOOD "Join flooding the channel"
26 #define SPAMSERV_MSG_WARNING "%s is against the network rules"
28 static int neonspam_spamscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message);
29 static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser);
31 static USERAUTH_CALLBACK(neonspam_event_chanmsg_nick_lookup);
32 static void neonspam_event_chanmsg_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction);
34 struct neonspam_event_chanmsg_cache {
35 struct ClientSocket *client;
36 struct ChanUser *chanuser;
37 struct NeonSpamSettings *settings;
43 static void neonspam_event_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
44 struct ClientSocket *client = getChannelBot(chan, BOTID);
45 if(!client) return; //we can't "see" this event
46 loadNeonSpamSettings(chan);
47 struct NeonSpamSettings *settings = chan->spam_settings;
48 struct ChanUser *chanuser = getChanUser(user, chan);
49 if(!settings || !chanuser) return;
51 //ignore messages from ops/voices if we ignore them
52 if(!(settings->flags & SPAMSETTINGS_SCANOPS) && (chanuser->flags & CHANUSERFLAG_OPPED)) return;
53 if(!(settings->flags & SPAMSETTINGS_SCANVOICE) && (chanuser->flags & CHANUSERFLAG_VOICED)) return;
54 if(settings->exceptlevel == 0) return;
57 int action = SPAMSERV_CHECK_IGNORE;
59 char *reaction = NULL;
60 if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_SPAMSCAN)) {
61 result = neonspam_spamscan(settings, chanuser, message);
63 case SPAMSERV_CHECK_IGNORE:
65 case SPAMSERV_CHECK_WARN:
66 if(action == SPAMSERV_CHECK_IGNORE) {
68 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
71 case SPAMSERV_CHECK_PUNISH:
72 if(action != SPAMSERV_CHECK_PUNISH) {
74 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
75 reaction = "channel_repeatreaction";
80 if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_FLOODSCAN)) {
81 result = neonspam_floodscan(settings, chanuser);
83 case SPAMSERV_CHECK_IGNORE:
85 case SPAMSERV_CHECK_WARN:
86 if(action == SPAMSERV_CHECK_IGNORE) {
88 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
91 case SPAMSERV_CHECK_PUNISH:
92 if(action != SPAMSERV_CHECK_PUNISH) {
94 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
95 reaction = "channel_floodreaction";
102 if(action != SPAMSERV_CHECK_IGNORE) {
103 //whois the user to check against exceptlevel
104 if((user->flags & USERFLAG_ISAUTHED) || settings->exceptlevel == 501) {
105 neonspam_event_chanmsg_punish(client, chanuser, settings, action, reason, reaction);
107 struct neonspam_event_chanmsg_cache *cache = malloc(sizeof(*cache));
109 perror("malloc() failed");
112 cache->client = client;
113 cache->chanuser = chanuser;
114 cache->settings = settings;
115 cache->action = action;
116 cache->reason = strdup(reason);
117 cache->reaction = reaction;
118 get_userauth(user, neonspam_event_chanmsg_nick_lookup, cache);
124 static USERAUTH_CALLBACK(neonspam_event_chanmsg_nick_lookup) {
125 struct neonspam_event_chanmsg_cache *cache = data;
126 neonspam_event_chanmsg_punish(cache->client, cache->chanuser, cache->settings, cache->action, cache->reason, cache->reaction);
131 static void neonspam_event_chanmsg_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction) {
133 if(chanuser->user->flags & USERFLAG_ISAUTHED)
134 uaccess = getChannelAccess(chanuser->user, chanuser->chan, 0);
135 if(uaccess >= settings->exceptlevel) return;
136 if(action == SPAMSERV_CHECK_WARN) {
137 reply(client, chanuser->user, "%s", reason);
138 } else if(action == SPAMSERV_CHECK_PUNISH) {
141 loadChannelSettings(chanuser->chan);
142 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", reaction, chanuser->chan->channel_id);
144 row = mysql_fetch_row(res);
146 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", reaction);
148 row = mysql_fetch_row(res);
151 char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
152 char *banmask = NULL;
153 switch (atoi(row[0])) {
154 case 2: //TIMEBAN: 3min
156 case 3: //TIMEBAN: 1h
159 banmask = generate_banmask(chanuser->user, banmaskBuf);
160 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));
161 int banid = (int) mysql_insert_id(mysql_conn);
162 char nameBuf[MAXLEN];
164 sprintf(nameBuf, "ban_%d", banid);
165 sprintf(banidBuf, "%d", banid);
166 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
169 banmask = generate_banmask(chanuser->user, banmaskBuf);
170 putsock(client, "MODE %s +b %s", chanuser->chan->name, banmask);
172 putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, reason);
178 static int neonspam_spamscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message) {
179 //crc32 hash of the message
180 unsigned long msghash = crc32(message);
181 if(chanuser->spamnode) {
182 if(chanuser->spamnode->lastmsg == msghash) {
183 chanuser->spamnode->spamcount++;
184 if(chanuser->spamnode->spamcount == settings->spam_amount)
185 return SPAMSERV_CHECK_WARN;
186 else if(chanuser->spamnode->spamcount > settings->spam_amount)
187 return SPAMSERV_CHECK_PUNISH;
189 return SPAMSERV_CHECK_IGNORE;
192 createSpamNode(chanuser);
193 chanuser->spamnode->lastmsg = msghash;
194 chanuser->spamnode->spamcount = 1;
195 return SPAMSERV_CHECK_IGNORE;
198 static int neonspam_update_penalty(struct NeonSpamSettings *settings, struct ChanUser *chanuser, int addmsg) {
199 int last_update = time(0) - chanuser->spamnode->last_penalty_update;
201 if(last_update < MAX_FLOOD_TIME && chanuser->spamnode->floodpenalty) {
202 chanuser->spamnode->floodpenalty -= last_update * (MAX_FLOOD_TIME / settings->flood_time);
203 if(chanuser->spamnode->floodpenalty < 0)
204 chanuser->spamnode->floodpenalty = 0;
206 chanuser->spamnode->floodpenalty = 0;
207 chanuser->spamnode->last_penalty_update = time(0);
209 chanuser->spamnode->floodpenalty += MAX_FLOOD_TIME * addmsg;
210 return chanuser->spamnode->floodpenalty / MAX_FLOOD_TIME + ((chanuser->spamnode->floodpenalty % MAX_FLOOD_TIME) ? 1 : 0);
213 static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser) {
214 if(!chanuser->spamnode)
215 createSpamNode(chanuser);
216 int messages_pending = neonspam_update_penalty(settings, chanuser, 1);
217 if(messages_pending == settings->flood_amount)
218 return SPAMSERV_CHECK_WARN;
219 else if(messages_pending > settings->flood_amount)
220 return SPAMSERV_CHECK_PUNISH;
222 return SPAMSERV_CHECK_IGNORE;