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);
20 static int neonspam_botnetscan(struct ClientSocket *client, struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message);
22 static USERAUTH_CALLBACK(neonspam_event_chanmsg_nick_lookup);
23 static void neonspam_event_chanmsg_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction);
25 struct neonspam_event_chanmsg_cache {
26 struct ClientSocket *client;
27 struct ChanUser *chanuser;
28 struct NeonSpamSettings *settings;
34 static void neonspam_event_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
35 struct ClientSocket *client = getChannelBot(chan, BOTID);
36 if(!client) return; //we can't "see" this event
37 loadNeonSpamSettings(chan);
38 struct NeonSpamSettings *settings = chan->spam_settings;
39 struct ChanUser *chanuser = getChanUser(user, chan);
40 if(!settings || !chanuser) return;
42 //ignore messages from ops/voices if we ignore them
43 if(!(settings->flags & SPAMSETTINGS_SCANOPS) && (chanuser->flags & CHANUSERFLAG_OPPED)) return;
44 if(!(settings->flags & SPAMSETTINGS_SCANVOICE) && (chanuser->flags & CHANUSERFLAG_VOICED)) return;
45 if(settings->exceptlevel == 0) return;
48 int action = SPAMSERV_CHECK_IGNORE;
50 char *reaction = NULL;
51 if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_SPAMSCAN)) {
52 result = neonspam_spamscan(settings, chanuser, message);
54 case SPAMSERV_CHECK_IGNORE:
56 case SPAMSERV_CHECK_WARN:
57 if(action == SPAMSERV_CHECK_IGNORE) {
59 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
62 case SPAMSERV_CHECK_PUNISH:
63 if(action != SPAMSERV_CHECK_PUNISH) {
65 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_SPAM);
66 reaction = "channel_repeatreaction";
71 if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_FLOODSCAN)) {
72 result = neonspam_floodscan(settings, chanuser);
74 case SPAMSERV_CHECK_IGNORE:
76 case SPAMSERV_CHECK_WARN:
77 if(action == SPAMSERV_CHECK_IGNORE) {
79 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_FLOOD);
82 case SPAMSERV_CHECK_PUNISH:
83 if(action != SPAMSERV_CHECK_PUNISH) {
85 sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_FLOOD);
86 reaction = "channel_floodreaction";
91 if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_BOTNETSCAN)) {
92 result = neonspam_botnetscan(client, settings, chanuser, message);
94 case SPAMSERV_CHECK_DEAD:
96 case SPAMSERV_CHECK_IGNORE:
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) {
139 loadChannelSettings(chanuser->chan);
141 if(!(reactionid = atoi(reaction))) {
144 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", reaction, chanuser->chan->channel_id);
146 row = mysql_fetch_row(res);
148 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", reaction);
150 row = mysql_fetch_row(res);
152 reactionid = atoi(row[0]);
156 char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
157 char *banmask = NULL;
158 switch (reactionid) {
159 case 2: //TIMEBAN: 3min
161 case 3: //TIMEBAN: 1h
164 banmask = generate_banmask(chanuser->user, banmaskBuf);
165 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));
166 int banid = (int) mysql_insert_id(mysql_conn);
167 char nameBuf[MAXLEN];
169 sprintf(nameBuf, "ban_%d", banid);
170 sprintf(banidBuf, "%d", banid);
171 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
174 banmask = generate_banmask(chanuser->user, banmaskBuf);
175 putsock(client, "MODE %s +b %s", chanuser->chan->name, banmask);
177 putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, reason);
183 static int neonspam_spamscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message) {
184 //crc32 hash of the message
185 unsigned long msghash = crc32(message);
186 if(chanuser->spamnode) {
187 if(chanuser->spamnode->lastmsg == msghash) {
188 chanuser->spamnode->spamcount++;
189 if(chanuser->spamnode->spamcount == settings->spam_amount)
190 return SPAMSERV_CHECK_WARN;
191 else if(chanuser->spamnode->spamcount > settings->spam_amount)
192 return SPAMSERV_CHECK_PUNISH;
194 return SPAMSERV_CHECK_IGNORE;
197 createSpamNode(chanuser);
198 chanuser->spamnode->lastmsg = msghash;
199 chanuser->spamnode->spamcount = 1;
200 return SPAMSERV_CHECK_IGNORE;
203 static int neonspam_update_penalty(struct NeonSpamSettings *settings, struct ChanUser *chanuser, int addmsg) {
204 int last_update = time(0) - chanuser->spamnode->last_penalty_update;
206 if(last_update < MAX_FLOOD_TIME && chanuser->spamnode->floodpenalty) {
207 chanuser->spamnode->floodpenalty -= last_update * (MAX_FLOOD_TIME / settings->flood_time);
208 if(chanuser->spamnode->floodpenalty < 0)
209 chanuser->spamnode->floodpenalty = 0;
211 chanuser->spamnode->floodpenalty = 0;
212 chanuser->spamnode->last_penalty_update = time(0);
214 chanuser->spamnode->floodpenalty += MAX_FLOOD_TIME * addmsg;
215 return chanuser->spamnode->floodpenalty / MAX_FLOOD_TIME + ((chanuser->spamnode->floodpenalty % MAX_FLOOD_TIME) ? 1 : 0);
218 static int neonspam_floodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser) {
219 if(!chanuser->spamnode)
220 createSpamNode(chanuser);
221 int messages_pending = neonspam_update_penalty(settings, chanuser, 1);
222 if(messages_pending == settings->flood_amount)
223 return SPAMSERV_CHECK_WARN;
224 else if(messages_pending > settings->flood_amount)
225 return SPAMSERV_CHECK_PUNISH;
227 return SPAMSERV_CHECK_IGNORE;
230 static int neonspam_botnetscan(struct ClientSocket *client, struct NeonSpamSettings *settings, struct ChanUser *chanuser, char *message) {
231 //crc32 hash of the message
232 unsigned long msghash = crc32(message);
233 if((time(0) - settings->lastmsg_time) > BOTNETSCAN_TIME || settings->lastmsg != msghash) {
235 for(i = 0; i < BOTNETSCAN_USERS; i++) {
236 if(settings->botnicks[i]) {
237 free(settings->botnicks[i]);
238 settings->botnicks[i] = NULL;
241 settings->flags &= ~SPAMSETTINGS_KICKEDBOTQUEUE;
242 settings->lastmsg = msghash;
243 } else if(settings->lastmsg == msghash) {
245 for(i = 0; i < BOTNETSCAN_USERS; i++) {
246 if(!settings->botnicks[i]) {
247 settings->botnicks[i] = strdup(chanuser->user->nick);
249 } else if(!stricmp(chanuser->user->nick, settings->botnicks[i])) {
250 return SPAMSERV_CHECK_IGNORE;
253 if(i == BOTNETSCAN_USERS) {
254 //BOTNETSCAN_USERS exceeded
255 if(!(settings->flags & SPAMSETTINGS_KICKEDBOTQUEUE)) {
256 for(i = 0; i < BOTNETSCAN_USERS; i++) {
257 putsock(client, "KICK %s %s :%s", chanuser->chan->name, settings->botnicks[i], SPAMSERV_MSG_BOTNET);
259 settings->flags |= SPAMSETTINGS_KICKEDBOTQUEUE;
261 putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, SPAMSERV_MSG_BOTNET);
262 return SPAMSERV_CHECK_DEAD;
265 settings->lastmsg_time = time(0);
266 return SPAMSERV_CHECK_IGNORE;