+++ /dev/null
-/* cmd_neonserv_addban.c - NeonServ v5.3
- * Copyright (C) 2011-2012 Philipp Kreil (pk910)
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "cmd_neonserv.h"
-
-/*
-* argv[0] nick|*auth|*!*@mask
-* argv[1-*] reason
-*/
-static USERLIST_CALLBACK(neonserv_cmd_addban_userlist_lookup);
-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);
-
-struct neonserv_cmd_addban_cache {
- struct ClientSocket *client, *textclient;
- struct UserNode *user;
- struct Event *event;
- char *mask;
- char *reason;
-};
-
-CMD_BIND(neonserv_cmd_addban) {
- struct neonserv_cmd_addban_cache *cache = malloc(sizeof(*cache));
- if (!cache) {
- perror("malloc() failed");
- return;
- }
- cache->client = client;
- cache->textclient = getTextBot();
- cache->user = user;
- cache->event = event;
- cache->mask = strdup(argv[0]);
- if(argc > 1) {
- cache->reason = strdup(merge_argv(argv, 1, argc));
- } else
- cache->reason = NULL;
- get_userlist(chan, neonserv_cmd_addban_userlist_lookup, cache);
-}
-
-static USERLIST_CALLBACK(neonserv_cmd_addban_userlist_lookup) {
- struct neonserv_cmd_addban_cache *cache = data;
- neonserv_cmd_addban_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->mask, (cache->reason ? cache->reason : "Bye."));
- free(cache->mask);
- if(cache->reason)
- free(cache->reason);
- free(cache);
-}
-
-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) {
- int match_count = 0;
- char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
- char usermask[NICKLEN+USERLEN+HOSTLEN+3];
- struct UserNode *cuser;
- struct ChanUser *chanuser;
- mask = make_banmask(mask, hostmask_buffer);
- for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
- cuser = chanuser->user;
- sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
- if(!match(mask, usermask)) {
- if(isNetworkService(chanuser->user)) {
- reply(textclient, user, "NS_SERVICE_IMMUNE", chanuser->user->nick);
- return;
- }
- if(cuser == user || ((cuser->flags & USERFLAG_ISAUTHED) && !stricmp(user->auth, cuser->auth))) {
- reply(textclient, user, "NS_YOU_PROTECTED");
- return;
- }
- if(isUserProtected(chan, cuser, user)) {
- reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
- return;
- }
- match_count++;
- if(match_count > 4 && (match_count * 3) > chan->usercount && !isGodMode(user)) {
- reply(textclient, user, "NS_LAME_MASK", mask);
- return;
- }
- }
- }
- MYSQL_RES *res;
- MYSQL_ROW row;
- //check if the provided mask is already banned by another ban
- char *ban = getBanAffectingMask(chan, mask);
- if(ban != NULL) {
- reply(textclient, user, "NS_BAN_ALREADY_ADDED", mask, chan->name);
- return;
- }
- //check if the provided mask affects any existing bans
- printf_mysql_query("SELECT `ban_mask`, `ban_id` FROM `bans` WHERE `ban_channel` = '%d'", chan->channel_id);
- res = mysql_use();
- while ((row = mysql_fetch_row(res)) != NULL) {
- if(!match(mask, row[0])) {
- //remove the ban
- printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[1]);
- }
- }
- printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
- int userid;
- res = mysql_use();
- if ((row = mysql_fetch_row(res)) != NULL)
- userid = atoi(row[0]);
- else
- return;
- //add the ban
- 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));
- putsock(client, "MODE %s +b %s", chan->name, mask);
- for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
- cuser = chanuser->user;
- sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
- if(!match(mask, usermask)) {
- putsock(client, "KICK %s %s :(%s) %s", chan->name, cuser->nick, user->nick, reason);
- }
- }
- reply(textclient, user, "NS_ADDBAN_DONE", mask, chan->name, match_count);
- logEvent(event);
-}