1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
/*
* levels.c - Sorting things by category -- Window/Lastlog, Ignore, and Floods
*
* Copyright (c) 1990 Michael Sandroff.
* Copyright (c) 1991, 1992 Troy Rollo.
* Copyright (c) 1992-1996 Matthew Green.
* Copyright 1993, 2005 EPIC Software Labs.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notices, the above paragraph (the one permitting redistribution),
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the author(s) may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "irc.h"
#include "levels.h"
#include "ircaux.h"
#include "functions.h"
#include "output.h"
int LEVEL_NONE, LEVEL_CURRENT, LEVEL_OTHER;
int LEVEL_PUBLIC, LEVEL_MSG, LEVEL_NOTICE, LEVEL_WALL;
int LEVEL_WALLOP, LEVEL_OPNOTE, LEVEL_SNOTE, LEVEL_ACTION;
int LEVEL_DCC, LEVEL_CTCP, LEVEL_INVITE, LEVEL_JOIN;
int LEVEL_NICK, LEVEL_TOPIC, LEVEL_PART, LEVEL_QUIT;
int LEVEL_KICK, LEVEL_MODE, LEVEL_OPERWALL, LEVEL_SYSERR;
int LEVEL_USER1, LEVEL_USER2;
int LEVEL_USER3, LEVEL_USER4, LEVEL_USER5, LEVEL_USER6;
int LEVEL_USER7, LEVEL_USER8, LEVEL_USER9, LEVEL_USER10;
int LEVEL_ALL;
static Bucket *level_bucket = NULL;
static int next_level = 1;
void init_levels (void)
{
level_bucket = new_bucket();
LEVEL_NONE = 0;
LEVEL_OTHER = add_new_level("OTHER");
add_new_level_alias(LEVEL_OTHER, "CRAP");
LEVEL_PUBLIC = add_new_level("PUBLICS");
LEVEL_MSG = add_new_level("MSGS");
LEVEL_NOTICE = add_new_level("NOTICES");
LEVEL_WALL = add_new_level("WALLS");
LEVEL_WALLOP = add_new_level("WALLOPS");
LEVEL_OPNOTE = add_new_level("OPNOTES");
LEVEL_SNOTE = add_new_level("SNOTES");
LEVEL_ACTION = add_new_level("ACTIONS");
LEVEL_DCC = add_new_level("DCCS");
LEVEL_CTCP = add_new_level("CTCPS");
LEVEL_INVITE = add_new_level("INVITES");
LEVEL_JOIN = add_new_level("JOINS");
LEVEL_NICK = add_new_level("NICKS");
LEVEL_TOPIC = add_new_level("TOPICS");
LEVEL_PART = add_new_level("PARTS");
LEVEL_QUIT = add_new_level("QUITS");
LEVEL_KICK = add_new_level("KICKS");
LEVEL_MODE = add_new_level("MODES");
LEVEL_OPERWALL = add_new_level("OPERWALL");
LEVEL_SYSERR = add_new_level("SYSERR");
LEVEL_USER1 = add_new_level("USER1");
LEVEL_USER2 = add_new_level("USER2");
LEVEL_USER3 = add_new_level("USER3");
LEVEL_USER4 = add_new_level("USER4");
LEVEL_USER5 = add_new_level("USER5");
LEVEL_USER6 = add_new_level("USER6");
LEVEL_USER7 = add_new_level("USER7");
LEVEL_USER8 = add_new_level("USER8");
LEVEL_USER9 = add_new_level("USER9");
LEVEL_USER10 = add_new_level("USER10");
LEVEL_ALL = 0x7FFFFFFF;
}
#define LEVELNAME(i) level_bucket->list[i].name
#define LEVELNUM(i) (*(int *)(level_bucket->list[i].stuff))
int add_new_level (const char *name)
{
const char *name_copy;
int * levelnum;
int i;
if ((i = str_to_level(name)) != -1)
return i;
/* Don't allow overflow */
if (next_level >= BIT_MAXBIT)
return -1;
name_copy = malloc_strdup(name);
levelnum = new_malloc(sizeof(int));
*levelnum = next_level++;
add_to_bucket(level_bucket, name_copy, levelnum);
return *levelnum;
}
int add_new_level_alias (int level, const char *name)
{
const char *name_copy;
int * levelnum;
int i;
if ((i = str_to_level(name)) != -1)
return i;
name_copy = malloc_strdup(name);
levelnum = new_malloc(sizeof(int));
*levelnum = level;
add_to_bucket(level_bucket, name_copy, levelnum);
return *levelnum;
}
char * get_all_levels (void)
{
char *buffer = NULL;
int i;
int next = 1;
for (i = 0; i < level_bucket->numitems; i++)
{
/* This is done to skip aliases... */
if (LEVELNUM(i) == next)
{
malloc_strcat_word(&buffer, space, LEVELNAME(i), DWORD_NO);
next++;
}
}
return buffer;
}
static const char * mask_to_positive_str (const Mask *mask)
{
static char buffer[512];
int i;
int next = 1;
*buffer = 0;
for (i = 0; i < level_bucket->numitems; i++)
{
/* This is done to skip aliases... */
if (LEVELNUM(i) == next)
{
if (mask_isset(mask, next))
{
if (*buffer)
strlcat(buffer, " ", sizeof buffer);
strlcat(buffer, LEVELNAME(i), sizeof buffer);
}
next++;
}
}
return buffer;
}
static const char * mask_to_negative_str (const Mask *mask)
{
static char buffer[512];
int i;
int next = 1;
*buffer = 0;
strlcpy(buffer, "ALL", sizeof buffer);
for (i = 0; i < level_bucket->numitems; i++)
{
/* This is done to skip aliases... */
if (LEVELNUM(i) == next)
{
if (!mask_isset(mask, next))
{
if (*buffer)
strlcat(buffer, " -", sizeof buffer);
strlcat(buffer, LEVELNAME(i), sizeof buffer);
}
next++;
}
}
return buffer;
}
const char * mask_to_str (const Mask *mask)
{
static char buffer[512]; /* this *should* be enough for this */
const char *str1, *str2;
if (mask_isall(mask))
{
strlcpy(buffer, "ALL", sizeof buffer);
return buffer;
}
if (mask_isnone(mask))
{
strlcpy(buffer, "NONE", sizeof buffer);
return buffer;
}
str1 = mask_to_positive_str(mask);
str2 = mask_to_negative_str(mask);
if (strlen(str1) <= strlen(str2))
return str1;
else
return str2;
}
int str_to_mask (Mask *mask, const char *orig, char **rejects)
{
char *ptr,
*rest;
int len,
i,
neg;
char * str;
mask_unsetall(mask);
if (!orig)
return 0; /* Whatever */
if (rejects == NULL || *rejects != NULL)
panic(1, "str_to_mask: rejects must be a pointer to null");
str = LOCAL_COPY(orig);
while ((str = next_arg(str, &rest)) != NULL)
{
while (str)
{
if ((ptr = strchr(str, ',')) != NULL)
*ptr++ = 0;
if ((len = strlen(str)) != 0)
{
if (my_strnicmp(str, "ALL", len) == 0)
mask_setall(mask);
else if (my_strnicmp(str, "NONE", len) == 0)
mask_unsetall(mask);
else
{
if (*str == '-')
{
str++, len--;
neg = 1;
}
else
neg = 0;
for (i = 0; i < level_bucket->numitems; i++)
{
if (!my_strnicmp(str, LEVELNAME(i), len))
{
if (neg)
mask_unset(mask, LEVELNUM(i));
else
mask_set(mask, LEVELNUM(i));
break;
}
}
if (i == level_bucket->numitems)
malloc_strcat_word(rejects, space, str, DWORD_NO);
}
}
str = ptr;
}
str = rest;
}
if (rejects && *rejects)
return -1;
return 0;
}
void standard_level_warning (const char *who, char **rejects)
{
if (rejects && *rejects)
{
char *s;
say("%s ignored the these unsupported levels: %s",
who, *rejects);
s = get_all_levels();
say("The valid levels are: %s", s);
new_free(&s);
new_free(rejects);
}
}
int str_to_level (const char *orig)
{
int i, len;
len = strlen(orig);
for (i = 0; i < level_bucket->numitems; i++)
if (!my_strnicmp(orig, LEVELNAME(i), len))
return LEVELNUM(i);
return -1;
}
const char * level_to_str (int l)
{
int i;
if (l == LEVEL_NONE)
return "NONE";
else if (l == LEVEL_ALL)
return "ALL";
else
{
for (i = 0; i < level_bucket->numitems; i++)
if (l == LEVELNUM(i))
return LEVELNAME(i);
}
return empty_string;
}
/*
* $levelctl(LEVELS)
* $levelctl(ADD name)
* $levelctl(ALIAS old-name new-name)
* $levelctl(LOOKUP name-or-number)
* $levelctl(NORMALIZE string)
*/
char *levelctl (char *input)
{
char *listc, *retval;
const char *newlevel, *oldlevel;
int oldnum, newnum;
GET_FUNC_ARG(listc, input);
if (!my_strnicmp(listc, "LEVELS", 2)) {
retval = get_all_levels();
RETURN_MSTR(retval);
} else if (!my_strnicmp(listc, "ADD", 2)) {
GET_FUNC_ARG(newlevel, input);
newnum = add_new_level(newlevel);
RETURN_INT(newnum);
} else if (!my_strnicmp(listc, "ALIAS", 2)) {
GET_FUNC_ARG(oldlevel, input);
GET_FUNC_ARG(newlevel, input);
oldnum = str_to_level(oldlevel);
newnum = add_new_level_alias(oldnum, newlevel);
RETURN_INT(newnum);
} else if (!my_strnicmp(listc, "LOOKUP", 2)) {
GET_FUNC_ARG(newlevel, input);
if (is_number(newlevel)) {
oldnum = STR2INT(newlevel);
oldlevel = level_to_str(oldnum);
RETURN_STR(oldlevel);
} else {
oldnum = str_to_level(newlevel);
RETURN_INT(oldnum);
}
} else if (!my_strnicmp(listc, "NORMALIZE", 1)) {
Mask m;
const char *r;
char *err = NULL;
mask_unsetall(&m);
str_to_mask(&m, input, &err); /* Errors are ignored */
r = mask_to_str(&m);
RETURN_STR(r);
}
RETURN_EMPTY;
}
|