*** VERSION 5.2.0 ***
[NeonServV5.git] / src / cmd_neonserv_addban.c
1 /* cmd_neonserv_addban.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 #include "cmd_neonserv.h"
19
20 /*
21 * argv[0]    nick|*auth|*!*@mask
22 * argv[1-*]  reason
23 */
24 static USERLIST_CALLBACK(neonserv_cmd_addban_userlist_lookup);
25 static void neonserv_cmd_addban_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *mas, char *reason);
26
27 struct neonserv_cmd_addban_cache {
28     struct ClientSocket *client, *textclient;
29     struct UserNode *user;
30     struct Event *event;
31     char *mask;
32     char *reason;
33 };
34
35 CMD_BIND(neonserv_cmd_addban) {
36     struct neonserv_cmd_addban_cache *cache = malloc(sizeof(*cache));
37     if (!cache) {
38         perror("malloc() failed");
39         return;
40     }
41     cache->client = client;
42     cache->textclient = getTextBot();
43     cache->user = user;
44     cache->event = event;
45     cache->mask = strdup(argv[0]);
46     if(argc > 1) {
47         cache->reason = strdup(merge_argv(argv, 1, argc));
48     } else
49         cache->reason = NULL;
50     get_userlist(chan, neonserv_cmd_addban_userlist_lookup, cache);
51 }
52
53 static USERLIST_CALLBACK(neonserv_cmd_addban_userlist_lookup) {
54     struct neonserv_cmd_addban_cache *cache = data;
55     neonserv_cmd_addban_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->mask, (cache->reason ? cache->reason : "Bye."));
56     free(cache->mask);
57     if(cache->reason)
58         free(cache->reason);
59     free(cache);
60 }
61
62 static void neonserv_cmd_addban_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *mask, char *reason) {
63     int match_count = 0;
64     char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
65     char usermask[NICKLEN+USERLEN+HOSTLEN+3];
66     struct UserNode *cuser;
67     struct ChanUser *chanuser;
68     mask = make_banmask(mask, hostmask_buffer);
69     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
70         cuser = chanuser->user;
71         sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
72         if(!match(mask, usermask)) {
73             if(isNetworkService(chanuser->user)) {
74                 reply(textclient, user, "NS_SERVICE_IMMUNE", chanuser->user->nick);
75                 return;
76             }
77             if(cuser == user || ((cuser->flags & USERFLAG_ISAUTHED) && !stricmp(user->auth, cuser->auth))) {
78                 reply(textclient, user, "NS_YOU_PROTECTED");
79                 return;
80             }
81             if(isUserProtected(chan, cuser, user)) {
82                 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
83                 return;
84             }
85             match_count++;
86             if(match_count > 4 && (match_count * 3) > chan->usercount && !isGodMode(user)) {
87                 reply(textclient, user, "NS_LAME_MASK", mask);
88                 return;
89             }
90         }
91     }
92     MYSQL_RES *res;
93     MYSQL_ROW row;
94     //check if the provided mask is already banned by another ban
95     char *ban = getBanAffectingMask(chan, mask);
96     if(ban != NULL) {
97         reply(textclient, user, "NS_BAN_ALREADY_ADDED", mask, chan->name);
98         return;
99     }
100     //check if the provided mask affects any existing bans
101     printf_mysql_query("SELECT `ban_mask`, `ban_id` FROM `bans` WHERE `ban_channel` = '%d'", chan->channel_id);
102     res = mysql_use();
103     while ((row = mysql_fetch_row(res)) != NULL) {
104         if(!match(mask, row[0])) {
105             //remove the ban
106             printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[1]);
107         }
108     }
109     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
110     int userid;
111     res = mysql_use();
112     if ((row = mysql_fetch_row(res)) != NULL)
113         userid = atoi(row[0]);
114     else
115         return;
116     //add the ban
117     printf_mysql_query("INSERT INTO `bans` (`ban_channel`, `ban_mask`, `ban_triggered`, `ban_owner`, `ban_reason`) VALUES ('%d', '%s', UNIX_TIMESTAMP(), '%d', '%s')", chan->channel_id, escape_string(mask), userid, escape_string(reason));
118     putsock(client, "MODE %s +b %s", chan->name, mask);
119     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
120         cuser = chanuser->user;
121         sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
122         if(!match(mask, usermask)) {
123             putsock(client, "KICK %s %s :%s", chan->name, cuser->nick, reason);
124         }
125     }
126     reply(textclient, user, "NS_ADDBAN_DONE", mask, chan->name, match_count);
127     logEvent(event);
128 }