1 /* cmd_neonserv_addtimeban.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
25 static USERLIST_CALLBACK(neonserv_cmd_addtimeban_userlist_lookup);
26 static void neonserv_cmd_addtimeban_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *mask, int duration, char *reason);
28 struct neonserv_cmd_addtimeban_cache {
29 struct ClientSocket *client, *textclient;
30 struct UserNode *user;
37 CMD_BIND(neonserv_cmd_addtimeban) {
38 int duration = strToTime(user, argv[1]);
40 reply(textclient, user, "NS_TIMEBAN_DURATION_TOO_SHORT", 5);
43 struct neonserv_cmd_addtimeban_cache *cache = malloc(sizeof(*cache));
45 printf_log("neonserv", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
48 cache->client = client;
49 cache->textclient = textclient;
52 cache->mask = strdup(argv[0]);
53 cache->duration = duration;
55 cache->reason = strdup(merge_argv(argv, 2, argc));
58 get_userlist(chan, module_id, neonserv_cmd_addtimeban_userlist_lookup, cache);
61 static USERLIST_CALLBACK(neonserv_cmd_addtimeban_userlist_lookup) {
62 struct neonserv_cmd_addtimeban_cache *cache = data;
63 neonserv_cmd_addtimeban_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->mask, cache->duration, (cache->reason ? cache->reason : "Bye."));
70 static void neonserv_cmd_addtimeban_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *mask, int duration, char *reason) {
72 char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
73 char usermask[NICKLEN+USERLEN+HOSTLEN+3];
74 struct UserNode *cuser;
75 struct ChanUser *chanuser;
76 mask = make_banmask(mask, hostmask_buffer);
77 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
78 cuser = chanuser->user;
79 sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
80 if(!match(mask, usermask)) {
81 if(isNetworkService(chanuser->user)) {
82 reply(textclient, user, "NS_SERVICE_IMMUNE", chanuser->user->nick);
85 if(isUserProtected(chan, cuser, user)) {
86 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
90 if(match_count > 4 && (match_count * 3) > chan->usercount && !isGodMode(user)) {
91 reply(textclient, user, "NS_LAME_MASK", mask);
98 //check if the provided mask is already banned by another ban
99 char *ban = getBanAffectingMask(chan, mask);
101 reply(textclient, user, "NS_BAN_ALREADY_ADDED", mask, chan->name);
104 //check if the provided mask affects any existing bans
105 printf_mysql_query("SELECT `ban_mask`, `ban_id` FROM `bans` WHERE `ban_channel` = '%d'", chan->channel_id);
107 while ((row = mysql_fetch_row(res)) != NULL) {
108 if(!match(mask, row[0])) {
110 printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[1]);
113 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
116 if ((row = mysql_fetch_row(res)) != NULL)
117 userid = atoi(row[0]);
121 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')", chan->channel_id, escape_string(mask), (unsigned long) (time(0) + duration), userid, escape_string(reason));
122 int banid = (int) mysql_insert_id(get_mysql_conn());
123 putsock(client, "MODE %s +b %s", chan->name, mask);
124 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
125 cuser = chanuser->user;
126 sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
127 if(!match(mask, usermask)) {
128 putsock(client, "KICK %s %s :(%s) %s", chan->name, cuser->nick, user->nick, reason);
131 char nameBuf[MAXLEN];
132 if(duration < 86400*7) {
134 sprintf(nameBuf, "ban_%d", banid);
135 sprintf(banidBuf, "%d", banid);
136 timeq_add_name(nameBuf, duration, module_id, channel_ban_timeout, strdup(banidBuf));
138 reply(textclient, user, "NS_TIMEBAN_DONE", mask, chan->name, timeToStr(user, duration, 2, nameBuf), match_count);