2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
levels.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.
|
|
|
|
|
|
|
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "levels.h"
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
static const char *levels[] = {
|
2000-04-26 04:03:38 -04:00
|
|
|
"CRAP",
|
|
|
|
"MSGS",
|
|
|
|
"PUBLICS",
|
|
|
|
"NOTICES",
|
|
|
|
"SNOTES",
|
|
|
|
"CTCPS",
|
|
|
|
"ACTIONS",
|
|
|
|
"JOINS",
|
|
|
|
"PARTS",
|
|
|
|
"QUITS",
|
|
|
|
"KICKS",
|
|
|
|
"MODES",
|
|
|
|
"TOPICS",
|
|
|
|
"WALLOPS",
|
|
|
|
"INVITES",
|
|
|
|
"NICKS",
|
|
|
|
"DCC",
|
2000-07-15 09:04:03 -04:00
|
|
|
"DCCMSGS",
|
2000-04-26 04:03:38 -04:00
|
|
|
"CLIENTNOTICES",
|
|
|
|
"CLIENTCRAP",
|
|
|
|
"CLIENTERRORS",
|
2002-01-08 02:36:40 -05:00
|
|
|
"HILIGHTS",
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
"NOHILIGHT",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
int level_get(const char *level)
|
|
|
|
{
|
2000-07-16 16:18:05 -04:00
|
|
|
int n, len, match;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-03-14 21:22:44 -05:00
|
|
|
if (g_strcasecmp(level, "ALL") == 0 || strcmp(level, "*") == 0)
|
2000-04-26 04:03:38 -04:00
|
|
|
return MSGLEVEL_ALL;
|
|
|
|
|
2001-02-19 07:10:36 -05:00
|
|
|
if (g_strcasecmp(level, "NEVER") == 0)
|
2000-04-26 04:03:38 -04:00
|
|
|
return MSGLEVEL_NEVER;
|
|
|
|
|
|
|
|
len = strlen(level);
|
2000-07-16 16:18:05 -04:00
|
|
|
if (len == 0) return 0;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
/* partial match allowed, as long as it's the only one that matches */
|
|
|
|
match = 0;
|
2000-04-26 04:03:38 -04:00
|
|
|
for (n = 0; levels[n] != NULL; n++) {
|
2001-02-19 07:10:36 -05:00
|
|
|
if (g_strncasecmp(levels[n], level, len) == 0) {
|
2000-11-18 10:31:28 -05:00
|
|
|
if ((int)strlen(levels[n]) == len) {
|
2000-11-08 18:40:34 -05:00
|
|
|
/* full match */
|
|
|
|
return 1L << n;
|
|
|
|
}
|
2000-07-16 16:18:05 -04:00
|
|
|
if (match > 0) {
|
|
|
|
/* ambiguous - abort */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
match = 1L << n;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
return match;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int level2bits(const char *level)
|
|
|
|
{
|
|
|
|
char *orig, *str, *ptr;
|
2000-07-16 16:18:05 -04:00
|
|
|
int ret, singlelevel, negative;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_return_val_if_fail(level != NULL, 0);
|
|
|
|
|
|
|
|
if (*level == '\0')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
orig = str = g_strdup(level);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
for (ptr = str; ; str++) {
|
|
|
|
if (*str == ' ')
|
|
|
|
*str++ = '\0';
|
|
|
|
else if (*str != '\0')
|
|
|
|
continue;
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
negative = *ptr == '-';
|
2000-04-26 04:03:38 -04:00
|
|
|
if (*ptr == '-' || *ptr == '+') ptr++;
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
singlelevel = level_get(ptr);
|
|
|
|
if (singlelevel != 0) {
|
|
|
|
ret = !negative ? (ret | singlelevel) :
|
|
|
|
(ret & ~singlelevel);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
while (*str == ' ') str++;
|
|
|
|
if (*str == '\0') break;
|
|
|
|
|
|
|
|
ptr = str;
|
|
|
|
}
|
|
|
|
g_free(orig);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *bits2level(int bits)
|
|
|
|
{
|
|
|
|
GString *str;
|
|
|
|
char *ret;
|
|
|
|
int n;
|
|
|
|
|
2000-06-03 14:54:59 -04:00
|
|
|
if (bits == 0)
|
|
|
|
return g_strdup("");
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
if (bits == MSGLEVEL_ALL)
|
|
|
|
return g_strdup("ALL");
|
|
|
|
|
|
|
|
str = g_string_new(NULL);
|
|
|
|
if (bits & MSGLEVEL_NEVER)
|
|
|
|
g_string_append(str, "NEVER ");
|
|
|
|
|
|
|
|
for (n = 0; levels[n] != NULL; n++) {
|
2000-07-16 16:18:05 -04:00
|
|
|
if (bits & (1L << n))
|
2000-04-26 04:03:38 -04:00
|
|
|
g_string_sprintfa(str, "%s ", levels[n]);
|
|
|
|
}
|
2000-07-16 16:18:05 -04:00
|
|
|
if (str->len > 0)
|
|
|
|
g_string_truncate(str, str->len-1);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
ret = str->str;
|
|
|
|
g_string_free(str, FALSE);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int combine_level(int dest, const char *src)
|
|
|
|
{
|
2000-07-16 16:18:05 -04:00
|
|
|
char **list, **item, *itemname;
|
2000-04-26 04:03:38 -04:00
|
|
|
int itemlevel;
|
|
|
|
|
|
|
|
g_return_val_if_fail(src != NULL, dest);
|
|
|
|
|
|
|
|
list = g_strsplit(src, " ", -1);
|
|
|
|
for (item = list; *item != NULL; item++) {
|
2000-07-16 16:18:05 -04:00
|
|
|
itemname = *item + (**item == '+' || **item == '-' ? 1 : 0);
|
|
|
|
g_strup(itemname);
|
|
|
|
itemlevel = level_get(itemname);
|
|
|
|
|
2000-09-26 17:44:20 -04:00
|
|
|
if (strcmp(itemname, "NONE") == 0)
|
|
|
|
dest = 0;
|
|
|
|
else if (**item == '-')
|
2000-04-26 04:03:38 -04:00
|
|
|
dest &= ~(itemlevel);
|
|
|
|
else
|
|
|
|
dest |= itemlevel;
|
|
|
|
}
|
|
|
|
g_strfreev(list);
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|