2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
bans.c : irssi
|
|
|
|
|
|
|
|
Copyright (C) 1999-2000 Timo Sirainen
|
|
|
|
|
|
|
|
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 2 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.
|
|
|
|
|
2007-05-08 14:41:10 -04:00
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2000-04-26 04:03:38 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
2000-08-26 11:39:44 -04:00
|
|
|
#include "signals.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "misc.h"
|
2001-03-03 15:48:23 -05:00
|
|
|
#include "settings.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-11-01 20:05:14 -05:00
|
|
|
#include "irc-servers.h"
|
|
|
|
#include "irc-channels.h"
|
2000-08-26 11:39:44 -04:00
|
|
|
#include "irc-masks.h"
|
2001-11-01 20:05:14 -05:00
|
|
|
#include "irc-commands.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "modes.h"
|
|
|
|
#include "mode-lists.h"
|
|
|
|
#include "nicklist.h"
|
|
|
|
|
2001-08-08 12:22:01 -04:00
|
|
|
#define BAN_TYPE_NORMAL (IRC_MASK_USER | IRC_MASK_DOMAIN)
|
|
|
|
#define BAN_TYPE_USER (IRC_MASK_USER)
|
|
|
|
#define BAN_TYPE_HOST (IRC_MASK_HOST | IRC_MASK_DOMAIN)
|
|
|
|
#define BAN_TYPE_DOMAIN (IRC_MASK_DOMAIN)
|
2001-10-23 12:01:53 -04:00
|
|
|
#define BAN_FIRST "1"
|
|
|
|
#define BAN_LAST "-1"
|
2001-08-08 12:22:01 -04:00
|
|
|
|
2001-03-03 15:48:23 -05:00
|
|
|
static char *default_ban_type_str;
|
|
|
|
static int default_ban_type;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-03-03 15:48:23 -05:00
|
|
|
char *ban_get_mask(IRC_CHANNEL_REC *channel, const char *nick, int ban_type)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
NICK_REC *rec;
|
|
|
|
char *str, *user, *host;
|
2002-03-31 07:04:57 -05:00
|
|
|
int size;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
g_return_val_if_fail(IS_IRC_CHANNEL(channel), NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_return_val_if_fail(nick != NULL, NULL);
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
rec = nicklist_find(CHANNEL(channel), nick);
|
2008-11-28 15:43:59 -05:00
|
|
|
if (rec == NULL) return NULL;
|
|
|
|
if (rec->host == NULL) {
|
|
|
|
g_warning("channel %s is not synced, using nick ban for %s", channel->name, nick);
|
|
|
|
return g_strdup_printf("%s!*@*", nick);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-03-03 15:48:23 -05:00
|
|
|
if (ban_type <= 0)
|
|
|
|
ban_type = default_ban_type;
|
|
|
|
|
|
|
|
str = irc_get_mask(nick, rec->host, ban_type);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* there's a limit of 10 characters in user mask. so, banning
|
|
|
|
someone with user mask of 10 characters gives us "*1234567890",
|
2002-03-31 07:04:57 -05:00
|
|
|
which is one too much.. so, remove the first character after "*"
|
|
|
|
so we'll get "*234567890" */
|
2000-04-26 04:03:38 -04:00
|
|
|
user = strchr(str, '!');
|
|
|
|
if (user == NULL) return str;
|
|
|
|
|
|
|
|
host = strchr(++user, '@');
|
|
|
|
if (host == NULL) return str;
|
|
|
|
|
2002-03-31 07:04:57 -05:00
|
|
|
size = (int) (host-user);
|
|
|
|
if (size >= 10) {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* too long user mask */
|
2002-03-31 07:04:57 -05:00
|
|
|
g_memmove(user+1, user+(size-9), strlen(user+(size-9))+1);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2001-03-03 15:48:23 -05:00
|
|
|
char *ban_get_masks(IRC_CHANNEL_REC *channel, const char *nicks, int ban_type)
|
2000-12-16 21:59:16 -05:00
|
|
|
{
|
|
|
|
GString *str;
|
|
|
|
char **ban, **banlist, *realban, *ret;
|
|
|
|
|
|
|
|
str = g_string_new(NULL);
|
|
|
|
banlist = g_strsplit(nicks, " ", -1);
|
|
|
|
for (ban = banlist; *ban != NULL; ban++) {
|
|
|
|
if (strchr(*ban, '!') != NULL) {
|
|
|
|
/* explicit ban */
|
2009-02-08 12:22:42 -05:00
|
|
|
g_string_append_printf(str, "%s ", *ban);
|
2000-12-16 21:59:16 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ban nick */
|
2001-03-03 15:48:23 -05:00
|
|
|
realban = ban_get_mask(channel, *ban, ban_type);
|
2000-12-16 21:59:16 -05:00
|
|
|
if (realban != NULL) {
|
2009-02-08 12:22:42 -05:00
|
|
|
g_string_append_printf(str, "%s ", realban);
|
2000-12-16 21:59:16 -05:00
|
|
|
g_free(realban);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_strfreev(banlist);
|
|
|
|
|
|
|
|
if (str->len > 0)
|
|
|
|
g_string_truncate(str, str->len-1);
|
|
|
|
|
|
|
|
ret = str->str;
|
|
|
|
g_string_free(str, FALSE);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-03-03 15:48:23 -05:00
|
|
|
void ban_set(IRC_CHANNEL_REC *channel, const char *bans, int ban_type)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-12-16 21:59:16 -05:00
|
|
|
char *masks;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_return_if_fail(bans != NULL);
|
|
|
|
|
2001-03-03 15:48:23 -05:00
|
|
|
if (ban_type <= 0)
|
|
|
|
ban_type = default_ban_type;
|
|
|
|
|
|
|
|
masks = ban_get_masks(channel, bans, ban_type);
|
2001-06-11 20:08:14 -04:00
|
|
|
channel_set_singlemode(channel, masks, "+b");
|
2000-12-16 21:59:16 -05:00
|
|
|
g_free(masks);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
void ban_remove(IRC_CHANNEL_REC *channel, const char *bans)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
GString *str;
|
|
|
|
GSList *tmp;
|
2001-10-23 12:01:53 -04:00
|
|
|
BAN_REC *rec;
|
2000-04-26 04:03:38 -04:00
|
|
|
char **ban, **banlist;
|
2001-05-14 13:35:24 -04:00
|
|
|
int found;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-09-29 19:57:30 -04:00
|
|
|
g_return_if_fail(bans != NULL);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
str = g_string_new(NULL);
|
|
|
|
banlist = g_strsplit(bans, " ", -1);
|
|
|
|
for (ban = banlist; *ban != NULL; ban++) {
|
2001-05-14 13:35:24 -04:00
|
|
|
found = FALSE;
|
2000-04-26 04:03:38 -04:00
|
|
|
for (tmp = channel->banlist; tmp != NULL; tmp = tmp->next) {
|
2001-10-23 12:01:53 -04:00
|
|
|
rec = tmp->data;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-05-14 13:35:24 -04:00
|
|
|
if (match_wildcards(*ban, rec->ban)) {
|
2009-02-08 12:22:42 -05:00
|
|
|
g_string_append_printf(str, "%s ", rec->ban);
|
2001-05-14 13:35:24 -04:00
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-23 12:01:53 -04:00
|
|
|
if (!found) {
|
|
|
|
rec = NULL;
|
2014-06-10 12:06:19 -04:00
|
|
|
if (!g_ascii_strcasecmp(*ban, BAN_LAST)) {
|
2001-10-23 12:01:53 -04:00
|
|
|
/* unnbanning last set ban */
|
|
|
|
rec = g_slist_nth_data(channel->banlist,
|
|
|
|
g_slist_length(channel->banlist) - 1);
|
|
|
|
}
|
|
|
|
else if (is_numeric(*ban, '\0')) {
|
|
|
|
/* unbanning with ban# */
|
|
|
|
rec = g_slist_nth_data(channel->banlist,
|
2001-05-14 13:35:24 -04:00
|
|
|
atoi(*ban)-1);
|
2001-10-23 12:01:53 -04:00
|
|
|
}
|
2001-05-14 13:35:24 -04:00
|
|
|
if (rec != NULL)
|
2009-02-08 12:22:42 -05:00
|
|
|
g_string_append_printf(str, "%s ", rec->ban);
|
2008-11-28 15:43:59 -05:00
|
|
|
else if (!channel->synced)
|
|
|
|
g_warning("channel %s is not synced", channel->name);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
g_strfreev(banlist);
|
|
|
|
|
2000-06-01 13:22:17 -04:00
|
|
|
if (str->len > 0)
|
2001-06-11 20:08:14 -04:00
|
|
|
channel_set_singlemode(channel, str->str, "-b");
|
2000-04-26 04:03:38 -04:00
|
|
|
g_string_free(str, TRUE);
|
|
|
|
}
|
|
|
|
|
2001-01-04 12:28:26 -05:00
|
|
|
static void command_set_ban(const char *data, IRC_SERVER_REC *server,
|
2001-03-03 15:48:23 -05:00
|
|
|
WI_ITEM_REC *item, int set, int ban_type)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-08-26 11:39:44 -04:00
|
|
|
IRC_CHANNEL_REC *chanrec;
|
2000-06-17 21:18:12 -04:00
|
|
|
char *channel, *nicks;
|
|
|
|
void *free_arg;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_return_if_fail(data != NULL);
|
2000-08-26 11:39:44 -04:00
|
|
|
if (server == NULL || !server->connected || !IS_IRC_SERVER(server))
|
2000-04-26 04:03:38 -04:00
|
|
|
cmd_return_error(CMDERR_NOT_CONNECTED);
|
|
|
|
|
2000-06-17 21:18:12 -04:00
|
|
|
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTCHAN | PARAM_FLAG_GETREST,
|
|
|
|
item, &channel, &nicks)) return;
|
2000-04-26 04:03:38 -04:00
|
|
|
if (!ischannel(*channel)) cmd_param_error(CMDERR_NOT_JOINED);
|
2000-06-01 13:22:17 -04:00
|
|
|
if (*nicks == '\0') {
|
|
|
|
if (strcmp(data, "*") != 0)
|
|
|
|
cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
|
|
|
|
/* /BAN * or /UNBAN * - ban/unban everyone */
|
2000-06-03 20:04:02 -04:00
|
|
|
nicks = (char *) data;
|
2000-06-01 13:22:17 -04:00
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
chanrec = irc_channel_find(server, channel);
|
|
|
|
if (chanrec == NULL)
|
|
|
|
cmd_param_error(CMDERR_CHAN_NOT_FOUND);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (set)
|
2001-03-03 15:48:23 -05:00
|
|
|
ban_set(chanrec, nicks, ban_type);
|
2001-05-14 13:35:24 -04:00
|
|
|
else
|
2000-04-26 04:03:38 -04:00
|
|
|
ban_remove(chanrec, nicks);
|
|
|
|
|
2000-06-17 21:18:12 -04:00
|
|
|
cmd_params_free(free_arg);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-08-08 12:22:01 -04:00
|
|
|
static int parse_custom_ban(const char *type)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-03-03 15:48:23 -05:00
|
|
|
char **list;
|
|
|
|
int n, ban_type;
|
|
|
|
|
2001-08-08 12:22:01 -04:00
|
|
|
ban_type = 0;
|
|
|
|
list = g_strsplit(type, " ", -1);
|
|
|
|
for (n = 0; list[n] != NULL; n++) {
|
2002-01-27 15:45:59 -05:00
|
|
|
if (i_toupper(list[n][0]) == 'N')
|
2001-08-08 12:22:01 -04:00
|
|
|
ban_type |= IRC_MASK_NICK;
|
2002-01-27 15:45:59 -05:00
|
|
|
else if (i_toupper(list[n][0]) == 'U')
|
2001-08-08 12:22:01 -04:00
|
|
|
ban_type |= IRC_MASK_USER;
|
2002-01-27 15:45:59 -05:00
|
|
|
else if (i_toupper(list[n][0]) == 'H')
|
2001-08-08 12:22:01 -04:00
|
|
|
ban_type |= IRC_MASK_HOST | IRC_MASK_DOMAIN;
|
2002-01-27 15:45:59 -05:00
|
|
|
else if (i_toupper(list[n][0]) == 'D')
|
2001-08-08 12:22:01 -04:00
|
|
|
ban_type |= IRC_MASK_DOMAIN;
|
|
|
|
}
|
|
|
|
g_strfreev(list);
|
|
|
|
|
|
|
|
return ban_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_ban_type(const char *type)
|
|
|
|
{
|
|
|
|
const char *pos;
|
|
|
|
|
2001-03-03 15:48:23 -05:00
|
|
|
g_return_val_if_fail(type != NULL, 0);
|
|
|
|
|
2002-01-27 15:45:59 -05:00
|
|
|
if (i_toupper(type[0]) == 'N')
|
2001-08-08 12:22:01 -04:00
|
|
|
return BAN_TYPE_NORMAL;
|
2002-01-27 15:45:59 -05:00
|
|
|
if (i_toupper(type[0]) == 'U')
|
2001-08-08 12:22:01 -04:00
|
|
|
return BAN_TYPE_USER;
|
2002-01-27 15:45:59 -05:00
|
|
|
if (i_toupper(type[0]) == 'H')
|
2001-08-08 12:22:01 -04:00
|
|
|
return BAN_TYPE_HOST;
|
2002-01-27 15:45:59 -05:00
|
|
|
if (i_toupper(type[0]) == 'D')
|
2001-08-08 12:22:01 -04:00
|
|
|
return BAN_TYPE_DOMAIN;
|
2002-01-27 15:45:59 -05:00
|
|
|
if (i_toupper(type[0]) == 'C') {
|
2001-08-08 12:22:01 -04:00
|
|
|
pos = strchr(type, ' ');
|
|
|
|
if (pos != NULL)
|
|
|
|
return parse_custom_ban(pos+1);
|
2001-03-03 15:48:23 -05:00
|
|
|
}
|
|
|
|
|
2001-08-08 12:22:01 -04:00
|
|
|
return 0;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-08-08 12:22:01 -04:00
|
|
|
/* SYNTAX: BAN [-normal | -user | -host | -domain | -custom <type>] <nicks/masks> */
|
2000-08-26 11:39:44 -04:00
|
|
|
static void cmd_ban(const char *data, IRC_SERVER_REC *server, void *item)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-03-03 15:48:23 -05:00
|
|
|
GHashTable *optlist;
|
2001-08-08 12:22:01 -04:00
|
|
|
const char *custom_type;
|
|
|
|
char *ban;
|
2001-03-03 15:48:23 -05:00
|
|
|
int ban_type;
|
|
|
|
void *free_arg;
|
|
|
|
|
2001-11-01 20:05:14 -05:00
|
|
|
CMD_IRC_SERVER(server);
|
|
|
|
|
2001-03-03 15:48:23 -05:00
|
|
|
if (!cmd_get_params(data, &free_arg, 1 |
|
|
|
|
PARAM_FLAG_OPTIONS | PARAM_FLAG_GETREST,
|
|
|
|
"ban", &optlist, &ban))
|
|
|
|
return;
|
|
|
|
|
2001-08-08 12:22:01 -04:00
|
|
|
if (g_hash_table_lookup(optlist, "normal") != NULL)
|
|
|
|
ban_type = BAN_TYPE_NORMAL;
|
|
|
|
else if (g_hash_table_lookup(optlist, "user") != NULL)
|
|
|
|
ban_type = BAN_TYPE_USER;
|
|
|
|
else if (g_hash_table_lookup(optlist, "host") != NULL)
|
|
|
|
ban_type = BAN_TYPE_HOST;
|
|
|
|
else if (g_hash_table_lookup(optlist, "domain") != NULL)
|
|
|
|
ban_type = BAN_TYPE_DOMAIN;
|
|
|
|
else {
|
|
|
|
custom_type = g_hash_table_lookup(optlist, "custom");
|
|
|
|
if (custom_type != NULL)
|
|
|
|
ban_type = parse_custom_ban(custom_type);
|
|
|
|
else
|
|
|
|
ban_type = default_ban_type;
|
|
|
|
}
|
2001-03-03 15:48:23 -05:00
|
|
|
|
|
|
|
command_set_ban(ban, server, item, TRUE, ban_type);
|
|
|
|
|
|
|
|
cmd_params_free(free_arg);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-10-23 12:01:53 -04:00
|
|
|
/* SYNTAX: UNBAN -first | -last | <id> | <masks> */
|
2000-08-26 11:39:44 -04:00
|
|
|
static void cmd_unban(const char *data, IRC_SERVER_REC *server, void *item)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-10-23 12:01:53 -04:00
|
|
|
GHashTable *optlist;
|
|
|
|
char *ban;
|
|
|
|
void *free_arg;
|
2001-11-01 20:05:14 -05:00
|
|
|
|
|
|
|
CMD_IRC_SERVER(server);
|
|
|
|
|
2001-10-23 12:01:53 -04:00
|
|
|
if (!cmd_get_params(data, &free_arg, 1 |
|
|
|
|
PARAM_FLAG_OPTIONS | PARAM_FLAG_GETREST,
|
|
|
|
"unban", &optlist, &ban))
|
|
|
|
return;
|
2001-11-01 20:05:14 -05:00
|
|
|
|
2001-10-23 12:01:53 -04:00
|
|
|
ban = NULL;
|
|
|
|
if (g_hash_table_lookup(optlist, "first") != NULL)
|
|
|
|
ban = g_strdup(BAN_FIRST);
|
|
|
|
else if (g_hash_table_lookup(optlist, "last") != NULL)
|
|
|
|
ban = g_strdup(BAN_LAST);
|
|
|
|
|
|
|
|
command_set_ban(ban ? ban : data, server, item, FALSE, 0);
|
|
|
|
|
|
|
|
g_free(ban);
|
2005-03-06 14:30:08 -05:00
|
|
|
|
|
|
|
cmd_params_free(free_arg);
|
2001-03-03 15:48:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void read_settings(void)
|
|
|
|
{
|
|
|
|
if (default_ban_type_str != NULL &&
|
|
|
|
strcmp(default_ban_type_str, settings_get_str("ban_type")) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_free_not_null(default_ban_type_str);
|
2001-08-08 12:22:01 -04:00
|
|
|
default_ban_type = parse_ban_type(settings_get_str("ban_type"));
|
2001-03-03 15:48:23 -05:00
|
|
|
if (default_ban_type <= 0 || default_ban_type_str != NULL) {
|
|
|
|
signal_emit("ban type changed", 1,
|
|
|
|
GINT_TO_POINTER(default_ban_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (default_ban_type <= 0)
|
|
|
|
default_ban_type = IRC_MASK_USER|IRC_MASK_DOMAIN;
|
|
|
|
|
|
|
|
default_ban_type_str = g_strdup(settings_get_str("ban_type"));
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void bans_init(void)
|
|
|
|
{
|
2001-03-03 15:48:23 -05:00
|
|
|
default_ban_type_str = NULL;
|
|
|
|
settings_add_str("misc", "ban_type", "normal");
|
|
|
|
|
2001-11-01 20:05:14 -05:00
|
|
|
command_bind_irc("ban", NULL, (SIGNAL_FUNC) cmd_ban);
|
|
|
|
command_bind_irc("unban", NULL, (SIGNAL_FUNC) cmd_unban);
|
2001-08-08 12:22:01 -04:00
|
|
|
command_set_options("ban", "normal user host domain +custom");
|
2001-10-23 12:01:53 -04:00
|
|
|
command_set_options("unban", "first last");
|
2001-03-03 15:48:23 -05:00
|
|
|
|
|
|
|
read_settings();
|
|
|
|
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void bans_deinit(void)
|
|
|
|
{
|
2001-03-03 15:48:23 -05:00
|
|
|
g_free_not_null(default_ban_type_str);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
command_unbind("ban", (SIGNAL_FUNC) cmd_ban);
|
|
|
|
command_unbind("unban", (SIGNAL_FUNC) cmd_unban);
|
2001-03-03 15:48:23 -05:00
|
|
|
|
|
|
|
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|