1 /* cmd_neonserv_addban.c - NeonServ v5.6
2 * Copyright (C) 2011-2012 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 #include "cmd_neonserv.h"
21 * argv[0] nick|*auth|*!*@mask
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);
27 struct neonserv_cmd_addban_cache {
28 struct ClientSocket *client, *textclient;
29 struct UserNode *user;
35 CMD_BIND(neonserv_cmd_addban) {
36 struct neonserv_cmd_addban_cache *cache = malloc(sizeof(*cache));
38 printf_log("neonserv", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
41 cache->client = client;
42 cache->textclient = textclient;
45 cache->mask = strdup(argv[0]);
47 cache->reason = strdup(merge_argv(argv, 1, argc));
50 get_userlist(chan, module_id, neonserv_cmd_addban_userlist_lookup, cache);
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."));
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) {
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);
77 if(cuser == user || ((cuser->flags & USERFLAG_ISAUTHED) && !stricmp(user->auth, cuser->auth))) {
78 reply(textclient, user, "NS_YOU_PROTECTED");
81 if(isUserProtected(chan, cuser, user)) {
82 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
86 if(match_count > 4 && (match_count * 3) > chan->usercount && !isGodMode(user)) {
87 reply(textclient, user, "NS_LAME_MASK", mask);
94 //check if the provided mask is already banned by another ban
95 char *ban = getBanAffectingMask(chan, mask);
97 reply(textclient, user, "NS_BAN_ALREADY_ADDED", mask, chan->name);
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);
103 while ((row = mysql_fetch_row(res)) != NULL) {
104 if(!match(mask, row[0])) {
106 printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[1]);
109 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
112 if ((row = mysql_fetch_row(res)) != NULL)
113 userid = atoi(row[0]);
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) %s", chan->name, cuser->nick, user->nick, reason);
126 reply(textclient, user, "NS_ADDBAN_DONE", mask, chan->name, match_count);