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
|
/*
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"
static const char *levels[] = {
"CRAP",
"MSGS",
"PUBLICS",
"NOTICES",
"SNOTES",
"CTCPS",
"ACTIONS",
"JOINS",
"PARTS",
"QUITS",
"KICKS",
"MODES",
"TOPICS",
"WALLOPS",
"INVITES",
"NICKS",
"DCC",
"DCCMSGS",
"CLIENTNOTICES",
"CLIENTCRAP",
"CLIENTERRORS",
"HILIGHT",
"NOHILIGHT",
NULL
};
int level_get(const char *level)
{
int n, len, match;
if (strcmp(level, "ALL") == 0)
return MSGLEVEL_ALL;
if (strcmp(level, "NEVER") == 0)
return MSGLEVEL_NEVER;
len = strlen(level);
if (len == 0) return 0;
/* partial match allowed, as long as it's the only one that matches */
match = 0;
for (n = 0; levels[n] != NULL; n++) {
if (strncmp(levels[n], level, len) == 0) {
if (match > 0) {
/* ambiguous - abort */
return 0;
}
match = 1L << n;
}
}
return match;
}
int level2bits(const char *level)
{
char *orig, *str, *ptr;
int ret, singlelevel, negative;
g_return_val_if_fail(level != NULL, 0);
if (*level == '\0')
return 0;
orig = str = g_strdup(level);
g_strup(str);
ret = 0;
for (ptr = str; ; str++) {
if (*str == ' ')
*str++ = '\0';
else if (*str != '\0')
continue;
negative = *ptr == '-';
if (*ptr == '-' || *ptr == '+') ptr++;
singlelevel = level_get(ptr);
if (singlelevel != 0) {
ret = !negative ? (ret | singlelevel) :
(ret & ~singlelevel);
}
while (*str == ' ') str++;
if (*str == '\0') break;
ptr = str;
}
g_free(orig);
return ret;
}
char *bits2level(int bits)
{
GString *str;
char *ret;
int n;
if (bits == 0)
return g_strdup("");
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++) {
if (bits & (1L << n))
g_string_sprintfa(str, "%s ", levels[n]);
}
if (str->len > 0)
g_string_truncate(str, str->len-1);
ret = str->str;
g_string_free(str, FALSE);
return ret;
}
int combine_level(int dest, const char *src)
{
char **list, **item, *itemname;
int itemlevel;
g_return_val_if_fail(src != NULL, dest);
list = g_strsplit(src, " ", -1);
for (item = list; *item != NULL; item++) {
itemname = *item + (**item == '+' || **item == '-' ? 1 : 0);
g_strup(itemname);
itemlevel = level_get(itemname);
if (**item == '-')
dest &= ~(itemlevel);
else
dest |= itemlevel;
}
g_strfreev(list);
return dest;
}
|