b2639035f75ca6e73593b4c07628eea4b17f73b0
[NeonServV5.git] / src / event_neonspam_join.c
1 /* event_neonspam_join.c - NeonServ v5.1
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, char *reason, char *reaction);
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     char *reason;
28     char *reaction;
29 };
30
31 static void neonspam_event_join(struct ChanUser *chanuser) {
32     struct ClientSocket *client = getChannelBot(chanuser->chan, BOTID);
33     if(!client) return; //we can't "see" this event
34     loadNeonSpamSettings(chanuser->chan);
35     struct NeonSpamSettings *settings = chanuser->chan->spam_settings;
36     if(!settings) return;
37     if(settings->exceptlevel == 0) return;
38     //scan the message
39     int result = 0;
40     int action = SPAMSERV_CHECK_IGNORE;
41     char reason[MAXLEN];
42     char *reaction = NULL;
43     if(action != SPAMSERV_CHECK_PUNISH && (settings->flags & SPAMSETTINGS_JOINSCAN)) {
44         result = neonspam_joinfloodscan(settings, chanuser);
45         switch(result) {
46             case SPAMSERV_CHECK_IGNORE:
47                 break;
48             case SPAMSERV_CHECK_WARN:
49                 if(action == SPAMSERV_CHECK_IGNORE) {
50                     action = result;
51                     sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_JOINFLOOD);
52                 }
53                 break;
54             case SPAMSERV_CHECK_PUNISH:
55                 if(action != SPAMSERV_CHECK_PUNISH) {
56                     action = result;
57                     sprintf(reason, SPAMSERV_MSG_WARNING, SPAMSERV_MSG_JOINFLOOD);
58                     reaction = "channel_joinreaction";
59                 }
60                 break;
61         }
62     }
63     //some other checks?
64     if(action != SPAMSERV_CHECK_IGNORE) {
65         //whois the user to check against exceptlevel
66         if((chanuser->user->flags & USERFLAG_ISAUTHED) || settings->exceptlevel == 501) {
67             neonspam_event_join_punish(client, chanuser, settings, action, reason, reaction);
68         } else {
69             struct neonspam_event_join_cache *cache = malloc(sizeof(*cache));
70             if (!cache) {
71                 perror("malloc() failed");
72                 return;
73             }
74             cache->client = client;
75             cache->chanuser = chanuser;
76             cache->settings = settings;
77             cache->action = action;
78             cache->reason = strdup(reason);
79             cache->reaction = reaction;
80             get_userauth(chanuser->user, neonspam_event_join_nick_lookup, cache);
81         }
82         
83     }
84 }
85
86 static USERAUTH_CALLBACK(neonspam_event_join_nick_lookup) {
87     struct neonspam_event_join_cache *cache = data;
88     neonspam_event_join_punish(cache->client, cache->chanuser, cache->settings, cache->action, cache->reason, cache->reaction);
89     free(cache->reason);
90     free(cache);
91 }
92
93 static void neonspam_event_join_punish(struct ClientSocket *client, struct ChanUser *chanuser, struct NeonSpamSettings *settings, int action, char *reason, char *reaction) {
94     int uaccess = 0;
95     if(chanuser->user->flags & USERFLAG_ISAUTHED)
96         uaccess = getChannelAccess(chanuser->user, chanuser->chan, 0);
97     if(uaccess >= settings->exceptlevel) return;
98     //scanops / scanvoiced
99     MYSQL_RES *res;
100     MYSQL_ROW row, defaults = NULL;
101     loadChannelSettings(chanuser->chan);
102     printf_mysql_query("SELECT `%s`, `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_id` = '%d'", reaction, chanuser->chan->channel_id);
103     res = mysql_use();
104     row = mysql_fetch_row(res);
105     if(!row[0] || !row[1] || !row[2]) {
106         printf_mysql_query("SELECT `%s`, `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'", reaction);
107         res = mysql_use();
108         defaults = mysql_fetch_row(res);
109     }
110     if(!(settings->flags & SPAMSETTINGS_SCANOPS) && uaccess >= atoi((row[1] ? row[1] : defaults[1]))) return;
111     if(!(settings->flags & SPAMSETTINGS_SCANVOICE) && uaccess >= atoi((row[2] ? row[2] : defaults[2]))) return;
112     if(action == SPAMSERV_CHECK_WARN) {
113         reply(client, chanuser->user, "%s", reason);
114     } else if(action == SPAMSERV_CHECK_PUNISH) {
115         int duration = 0;
116         char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
117         char *banmask = NULL;
118         switch (atoi((row[0] ? row[0] : defaults[0]))) {
119             case 2: //TIMEBAN: 3min
120                 duration = 180;
121             case 3: //TIMEBAN: 1h
122                 if(!duration)
123                     duration = 3600;
124                 banmask = generate_banmask(chanuser->user, banmaskBuf);
125                 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));
126                 int banid = (int) mysql_insert_id(mysql_conn);
127                 char nameBuf[MAXLEN];
128                 char banidBuf[20];
129                 sprintf(nameBuf, "ban_%d", banid);
130                 sprintf(banidBuf, "%d", banid);
131                 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
132             case 1: //KICKBAN
133                 if(!banmask)
134                     banmask = generate_banmask(chanuser->user, banmaskBuf);
135                 putsock(client, "MODE %s +b %s", chanuser->chan->name, banmask);
136             case 0: //KICK
137                 putsock(client, "KICK %s %s :%s", chanuser->chan->name, chanuser->user->nick, reason);
138                 break;
139         }
140     }
141 }
142
143 static int neonspam_update_join_penalty(struct NeonSpamSettings *settings, struct NeonSpamJoinNode *joinnode, int addjoin) {
144     int last_update = time(0) - joinnode->last_penalty_update;
145     if(last_update) {
146         if(last_update < MAX_JOIN_TIME && joinnode->joinpenalty) {
147             joinnode->joinpenalty -= last_update * (MAX_JOIN_TIME / settings->join_time);
148             if(joinnode->joinpenalty < 0)
149                 joinnode->joinpenalty = 0;
150         } else
151             joinnode->joinpenalty = 0;
152         joinnode->last_penalty_update = time(0);
153     }
154     joinnode->joinpenalty += MAX_JOIN_TIME * addjoin;
155     return joinnode->joinpenalty / MAX_JOIN_TIME + ((joinnode->joinpenalty % MAX_JOIN_TIME) ? 1 : 0);
156 }
157
158 static int neonspam_joinfloodscan(struct NeonSpamSettings *settings, struct ChanUser *chanuser) {
159     if(!chanuser->spamnode)
160         createSpamNode(chanuser);
161     int joins_pending = neonspam_update_join_penalty(settings, getNeonSpamJoinNode(chanuser), 1);
162     if(joins_pending == settings->join_amount)
163         return SPAMSERV_CHECK_WARN;
164     else if(joins_pending > settings->join_amount)
165         return SPAMSERV_CHECK_PUNISH;
166     else
167         return SPAMSERV_CHECK_IGNORE;
168 }
169
170