rewrote IRC cache parser to be (hopefully) more stable
[NeonServV5.git] / src / modules / NeonSpam.mod / event_neonspam_join.c
1 /* event_neonspam_join.c - NeonServ v5.4
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
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.
8  * 
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.
13  * 
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/>. 
16  */
17
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);
20 static int neonspam_joinfloodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser);
21
22 struct neonspam_event_join_cache {
23     struct ClientSocket *client;
24     struct ChanUser *chanuser;
25     struct NeonSpamSettings *settings;
26     int action;
27 };
28
29 static void neonspam_event_join(struct ChanUser *chanuser) {
30     struct ClientSocket *client = getChannelBot(chanuser->chan, BOTID);
31     if(!client) return; //we can't "see" this event
32     if(chanuser->user == client->user) {
33         requestOp(client->user, chanuser->chan);
34         return;
35     }
36     if(chanuser->user->flags & USERFLAG_ISBOT) return;
37     loadNeonSpamSettings(chanuser->chan);
38     struct NeonSpamSettings *settings = chanuser->chan->spam_settings;
39     if(!settings || !(settings->flags & SPAMSETTINGS_JOINSCAN)) return;
40     if(settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX] == 0) return;
41     int result = neonspam_joinfloodscan(settings, chanuser);
42     if(result != SPAMSERV_CHECK_IGNORE) {
43         //whois the user to check against exceptlevel
44         if((chanuser->user->flags & USERFLAG_ISAUTHED) || settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX] == 501) {
45             neonspam_event_join_punish(client, chanuser, settings, result);
46         } else {
47             struct neonspam_event_join_cache *cache = malloc(sizeof(*cache));
48             if (!cache) {
49                 perror("malloc() failed");
50                 return;
51             }
52             cache->client = client;
53             cache->chanuser = chanuser;
54             cache->settings = settings;
55             cache->action = result;
56             get_userauth(chanuser->user, module_id, neonspam_event_join_nick_lookup, cache);
57         }
58     }
59 }
60
61 static USERAUTH_CALLBACK(neonspam_event_join_nick_lookup) {
62     struct neonspam_event_join_cache *cache = data;
63     neonspam_event_join_punish(cache->client, cache->chanuser, cache->settings, cache->action);
64     free(cache);
65 }
66
67 static void neonspam_event_join_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action) {
68     int uaccess = 0;
69     if(chanuser->user->flags & USERFLAG_ISAUTHED)
70         uaccess = getChannelAccess(chanuser->user, chanuser->chan);
71     if(uaccess >= settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX]) return;
72     //scanops / scanvoiced
73     MYSQL_RES *res;
74     MYSQL_ROW row, defaults = NULL;
75     loadChannelSettings(chanuser->chan);
76     printf_mysql_query("SELECT `channel_flood_reaction`, `channel_flood_reaction_duration`, `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_id` = '%d'", chanuser->chan->channel_id);
77     res = mysql_use();
78     row = mysql_fetch_row(res);
79     if(!row[0] || !row[1] || !row[2] || !row[3]) {
80         printf_mysql_query("SELECT `channel_flood_reaction`, `channel_flood_reaction_duration`, `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'");
81         res = mysql_use();
82         defaults = mysql_fetch_row(res);
83     }
84     if(!(settings->flags & SPAMSETTINGS_JOINSCAN_OPS) && uaccess >= atoi((row[2] ? row[2] : defaults[2]))) return;
85     if(!(settings->flags & SPAMSETTINGS_JOINSCAN_VOICE) && uaccess >= atoi((row[3] ? row[3] : defaults[3]))) return;
86     char reason[MAXLEN];
87     sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_JOINFLOOD);
88     if(action == SPAMSERV_CHECK_WARN) {
89         reply(client, chanuser->user, "%s", reason);
90     } else if(action == SPAMSERV_CHECK_PUNISH) {
91         int duration = atoi((row[1] ? row[1] : defaults[1]));
92         char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
93         char *banmask = NULL;
94         switch (atoi((row[0] ? row[0] : defaults[0]))) {
95             case 2: //TIMEBAN
96                 banmask = generate_banmask(chanuser->user, banmaskBuf);
97                 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) (duration ? (time(0) + duration) : 0), 0, escape_string(reason));
98                 if(duration) {
99                     int banid = (int) mysql_insert_id(get_mysql_conn());
100                     char nameBuf[MAXLEN];
101                     char banidBuf[20];
102                     sprintf(nameBuf, "ban_%d", banid);
103                     sprintf(banidBuf, "%d", banid);
104                     timeq_add_name(nameBuf, duration, module_id, channel_ban_timeout, strdup(banidBuf));
105                 }
106             case 1: //KICKBAN
107                 if(!banmask)
108                     banmask = generate_banmask(chanuser->user, banmaskBuf);
109                 putsock(client, "MODE %s +b %s", chanuser->chan->name, banmask);
110             case 0: //KICK
111                 putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, reason);
112                 break;
113         }
114     }
115 }
116
117 static int neonspam_update_join_penalty(struct NeonSpamSettings *settings, struct NeonSpamJoinNode *joinnode, int addjoin) {
118     int last_update = time(0) - joinnode->last_penalty_update;
119     if(last_update) {
120         if(last_update < MAX_JOIN_TIME && joinnode->joinpenalty) {
121             joinnode->joinpenalty -= last_update * (MAX_JOIN_TIME / settings->sensibility_time[SPAMSETTINGS_JOINSENINDEX]);
122             if(joinnode->joinpenalty < 0)
123                 joinnode->joinpenalty = 0;
124         } else
125             joinnode->joinpenalty = 0;
126         joinnode->last_penalty_update = time(0);
127     }
128     joinnode->joinpenalty += MAX_JOIN_TIME * addjoin;
129     return joinnode->joinpenalty / MAX_JOIN_TIME + ((joinnode->joinpenalty % MAX_JOIN_TIME) ? 1 : 0);
130 }
131
132 static int neonspam_joinfloodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser) {
133     if(!chanuser->spamnode)
134         createSpamNode(chanuser);
135     int joins_pending = neonspam_update_join_penalty(settings, getNeonSpamJoinNode(chanuser), 1);
136     if(joins_pending == settings->sensibility_amount[SPAMSETTINGS_JOINSENINDEX])
137         return SPAMSERV_CHECK_WARN;
138     else if(joins_pending > settings->sensibility_amount[SPAMSETTINGS_JOINSENINDEX])
139         return SPAMSERV_CHECK_PUNISH;
140     else
141         return SPAMSERV_CHECK_IGNORE;
142 }
143
144