| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
* |
| 4 |
* Copyright (c) 2015-2016 plexus development team |
| 5 |
* Copyright (c) 2019-2020 ircd-hybrid development team |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 20 |
* USA |
| 21 |
*/ |
| 22 |
|
| 23 |
/*! \file extban.c |
| 24 |
* \brief Implements extended channel bans. |
| 25 |
* \version $Id$ |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "stdinc.h" |
| 29 |
#include "ircd_defs.h" |
| 30 |
#include "list.h" |
| 31 |
#include "irc_string.h" |
| 32 |
#include "user.h" |
| 33 |
#include "isupport.h" |
| 34 |
#include "extban.h" |
| 35 |
|
| 36 |
|
| 37 |
static dlink_list extban_list; |
| 38 |
static unsigned int matching_mask, acting_mask; |
| 39 |
|
| 40 |
|
| 41 |
static unsigned int |
| 42 |
extban_find_mask(void) |
| 43 |
{ |
| 44 |
dlink_node *node; |
| 45 |
unsigned int used = 0; |
| 46 |
unsigned int i; |
| 47 |
|
| 48 |
DLINK_FOREACH(node, extban_list.head) |
| 49 |
{ |
| 50 |
const struct Extban *extban = node->data; |
| 51 |
used |= extban->flag; |
| 52 |
} |
| 53 |
|
| 54 |
for (i = 1; (i < EXTBAN_MASK) && (used & i); i <<= 1) |
| 55 |
; |
| 56 |
|
| 57 |
return i; |
| 58 |
} |
| 59 |
|
| 60 |
void |
| 61 |
extban_init(void) |
| 62 |
{ |
| 63 |
extban_add(&extban_account); |
| 64 |
extban_add(&extban_channel); |
| 65 |
extban_add(&extban_fingerprint); |
| 66 |
extban_add(&extban_gecos); |
| 67 |
extban_add(&extban_join); |
| 68 |
extban_add(&extban_mute); |
| 69 |
extban_add(&extban_operclass); |
| 70 |
extban_add(&extban_server); |
| 71 |
extban_add(&extban_usermode); |
| 72 |
|
| 73 |
const char *ptr = extban_get_isupport(); |
| 74 |
if (ptr) |
| 75 |
isupport_add("EXTBAN", ptr, -1); |
| 76 |
} |
| 77 |
|
| 78 |
void |
| 79 |
extban_add(struct Extban *extban) |
| 80 |
{ |
| 81 |
unsigned int mask = extban_find_mask(); |
| 82 |
|
| 83 |
if (mask == 0) |
| 84 |
return; |
| 85 |
|
| 86 |
extban->flag = mask; |
| 87 |
dlinkAdd(extban, &extban->node, &extban_list); |
| 88 |
|
| 89 |
if (extban->type & EXTBAN_MATCHING) |
| 90 |
matching_mask |= mask; |
| 91 |
if (extban->type & EXTBAN_ACTING) |
| 92 |
acting_mask |= mask; |
| 93 |
} |
| 94 |
|
| 95 |
void |
| 96 |
extban_del(struct Extban *extban) |
| 97 |
{ |
| 98 |
if (extban->flag == 0) |
| 99 |
return; |
| 100 |
|
| 101 |
dlinkDelete(&extban->node, &extban_list); |
| 102 |
|
| 103 |
matching_mask &= ~extban->flag; |
| 104 |
acting_mask &= ~extban->flag; |
| 105 |
} |
| 106 |
|
| 107 |
struct Extban * |
| 108 |
extban_find(unsigned char c) |
| 109 |
{ |
| 110 |
dlink_node *node; |
| 111 |
|
| 112 |
DLINK_FOREACH(node, extban_list.head) |
| 113 |
{ |
| 114 |
struct Extban *extban = node->data; |
| 115 |
|
| 116 |
if (extban->character == c) |
| 117 |
return extban; |
| 118 |
} |
| 119 |
|
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
struct Extban * |
| 124 |
extban_find_flag(unsigned int flag) |
| 125 |
{ |
| 126 |
dlink_node *node; |
| 127 |
|
| 128 |
DLINK_FOREACH(node, extban_list.head) |
| 129 |
{ |
| 130 |
struct Extban *extban = node->data; |
| 131 |
|
| 132 |
if (extban->flag == flag) |
| 133 |
return extban; |
| 134 |
} |
| 135 |
|
| 136 |
return NULL; |
| 137 |
} |
| 138 |
|
| 139 |
enum extban_type |
| 140 |
extban_parse(const char *mask, unsigned int *input_extbans, unsigned int *offset) |
| 141 |
{ |
| 142 |
*input_extbans = 0; |
| 143 |
*offset = 0; |
| 144 |
|
| 145 |
if (*mask == '$' && IsAlpha(*(mask + 1)) && *(mask + 2) == ':') |
| 146 |
{ |
| 147 |
struct Extban *extban = extban_find(*(mask + 1)); |
| 148 |
if (extban == NULL) |
| 149 |
return EXTBAN_INVALID; |
| 150 |
|
| 151 |
*input_extbans |= extban->flag; |
| 152 |
*offset += 3; |
| 153 |
mask += 3; |
| 154 |
|
| 155 |
/* Matching extbans take a special parameter, so stop reading */ |
| 156 |
if (extban->type == EXTBAN_MATCHING) |
| 157 |
return EXTBAN_MATCHING; |
| 158 |
|
| 159 |
if (IsAlpha(*mask) && *(mask + 1) == ':') |
| 160 |
{ |
| 161 |
extban = extban_find(*mask); |
| 162 |
if (extban == NULL) |
| 163 |
return EXTBAN_INVALID; |
| 164 |
|
| 165 |
/* Two acting extbans make no sense */ |
| 166 |
if (extban->type == EXTBAN_ACTING) |
| 167 |
return EXTBAN_INVALID; |
| 168 |
|
| 169 |
/* Check parameter */ |
| 170 |
if (extban->is_valid && extban->is_valid(mask) == EXTBAN_INVALID) |
| 171 |
return EXTBAN_INVALID; |
| 172 |
|
| 173 |
*input_extbans |= extban->flag; |
| 174 |
*offset += 2; |
| 175 |
mask += 2; |
| 176 |
|
| 177 |
return EXTBAN_MATCHING; |
| 178 |
} |
| 179 |
|
| 180 |
return EXTBAN_ACTING; |
| 181 |
} |
| 182 |
|
| 183 |
return EXTBAN_NONE; |
| 184 |
} |
| 185 |
|
| 186 |
unsigned int |
| 187 |
extban_format(unsigned int e, char *buf) |
| 188 |
{ |
| 189 |
dlink_node *node; |
| 190 |
unsigned int written = 0; |
| 191 |
|
| 192 |
DLINK_FOREACH(node, extban_list.head) |
| 193 |
{ |
| 194 |
struct Extban *extban = node->data; |
| 195 |
|
| 196 |
if (extban->type != EXTBAN_ACTING) |
| 197 |
continue; |
| 198 |
|
| 199 |
if (extban->flag & e) |
| 200 |
{ |
| 201 |
if (written == 0) |
| 202 |
{ |
| 203 |
written++; |
| 204 |
*buf++ = '$'; |
| 205 |
} |
| 206 |
|
| 207 |
*buf++ = extban->character; |
| 208 |
*buf++ = ':'; |
| 209 |
|
| 210 |
written += 2; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
DLINK_FOREACH(node, extban_list.head) |
| 215 |
{ |
| 216 |
struct Extban *extban = node->data; |
| 217 |
|
| 218 |
if (extban->type != EXTBAN_MATCHING) |
| 219 |
continue; |
| 220 |
|
| 221 |
if (extban->flag & e) |
| 222 |
{ |
| 223 |
if (written == 0) |
| 224 |
{ |
| 225 |
written++; |
| 226 |
*buf++ = '$'; |
| 227 |
} |
| 228 |
|
| 229 |
*buf++ = extban->character; |
| 230 |
*buf++ = ':'; |
| 231 |
written += 2; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
return written; |
| 236 |
} |
| 237 |
|
| 238 |
unsigned int |
| 239 |
extban_matching_mask(void) |
| 240 |
{ |
| 241 |
return matching_mask; |
| 242 |
} |
| 243 |
|
| 244 |
unsigned int |
| 245 |
extban_acting_mask(void) |
| 246 |
{ |
| 247 |
return acting_mask; |
| 248 |
} |
| 249 |
|
| 250 |
const char * |
| 251 |
extban_get_isupport(void) |
| 252 |
{ |
| 253 |
dlink_node *node; |
| 254 |
char extban_chars[256] = { 0 }; |
| 255 |
static char buf[sizeof(extban_chars) + 3 /* +3 = $,\0 */ ]; |
| 256 |
|
| 257 |
if (dlink_list_length(&extban_list) == 0) |
| 258 |
return NULL; |
| 259 |
|
| 260 |
DLINK_FOREACH(node, extban_list.head) |
| 261 |
{ |
| 262 |
struct Extban *extban = node->data; |
| 263 |
extban_chars[extban->character] = extban->character; |
| 264 |
} |
| 265 |
|
| 266 |
char *p = buf + strlcpy(buf, "$,", sizeof(buf)); |
| 267 |
|
| 268 |
for (unsigned int i = 0; i < 256; ++i) |
| 269 |
if (extban_chars[i]) |
| 270 |
*p++ = extban_chars[i]; |
| 271 |
*p++ = '\0'; |
| 272 |
|
| 273 |
return buf; |
| 274 |
} |
| 275 |
|