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