2 ctrlproxy: A modular IRC proxy
3 (c) 2005 Jelmer Vernooij <jelmer@nl.linux.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "internals.h"
22 #define DEFAULT_PREFIX "(ov)@+"
23 #define DEFAULT_CHANTYPES "#&"
24 #define DEFAULT_CHARSET "iso8859-15"
26 void network_info_init(struct network_info *info)
28 memset(info, 0, sizeof(struct network_info));
29 info->prefix = g_strdup(DEFAULT_PREFIX);
30 info->chantypes = g_strdup(DEFAULT_CHANTYPES);
31 info->charset = g_strdup(DEFAULT_CHARSET);
34 void free_network_info(struct network_info *info)
37 g_free(info->chantypes);
38 g_free(info->charset);
41 g_free(info->supported_user_modes);
42 g_free(info->supported_channel_modes);
43 g_strfreev(info->chanmodes);
44 g_free(info->chanlimit);
45 g_free(info->maxlist);
47 g_free(info->statusmsg);
50 char *network_info_string(struct network_info *info)
53 GList *fs = NULL, *gl;
56 if (info->name != NULL)
57 fs = g_list_append(fs, g_strdup_printf("NETWORK=%s", info->name));
59 switch (info->casemapping) {
62 casemap = g_strdup("CASEMAPPING=rfc1459");
64 case CASEMAP_STRICT_RFC1459:
65 casemap = g_strdup("CASEMAPPING=strict-rfc1459");
68 casemap = g_strdup("CASEMAPPING=ascii");
73 fs = g_list_append(fs, casemap);
75 if (info->forced_nick_changes)
76 fs = g_list_append(fs, g_strdup("FNC"));
78 if (info->charset != NULL)
79 fs = g_list_append(fs, g_strdup_printf("CHARSET=%s", info->charset));
81 if (info->nicklen != 0)
82 fs = g_list_append(fs, g_strdup_printf("NICKLEN=%d", info->nicklen));
84 if (info->userlen != 0)
85 fs = g_list_append(fs, g_strdup_printf("USERLEN=%d", info->userlen));
87 if (info->hostlen != 0)
88 fs = g_list_append(fs, g_strdup_printf("HOSTLEN=%d", info->hostlen));
90 if (info->channellen != 0)
91 fs = g_list_append(fs, g_strdup_printf("CHANNELLEN=%d", info->channellen));
93 if (info->awaylen != 0)
94 fs = g_list_append(fs, g_strdup_printf("AWAYLEN=%d", info->awaylen));
96 if (info->kicklen != 0)
97 fs = g_list_append(fs, g_strdup_printf("KICKLEN=%d", info->kicklen));
99 if (info->topiclen != 0)
100 fs = g_list_append(fs, g_strdup_printf("TOPICLEN=%d", info->topiclen));
102 if (info->maxchannels != 0)
103 fs = g_list_append(fs, g_strdup_printf("MAXCHANNELS=%d", info->maxchannels));
105 if (info->maxtargets != 0)
106 fs = g_list_append(fs, g_strdup_printf("MAXTARGETS=%d", info->maxtargets));
108 if (info->maxbans != 0)
109 fs = g_list_append(fs, g_strdup_printf("MAXBANS=%d", info->maxbans));
111 if (info->maxmodes != 0)
112 fs = g_list_append(fs, g_strdup_printf("MODES=%d", info->maxmodes));
115 fs = g_list_append(fs, g_strdup("WALLCHOPS"));
117 if (info->wallvoices)
118 fs = g_list_append(fs, g_strdup("WALLVOICES"));
121 fs = g_list_append(fs, g_strdup("RFC2812"));
124 fs = g_list_append(fs, g_strdup("PENALTY"));
127 fs = g_list_append(fs, g_strdup("SAFELIST"));
130 fs = g_list_append(fs, g_strdup("USERIP"));
133 fs = g_list_append(fs, g_strdup("CAPAB"));
136 fs = g_list_append(fs, g_strdup("CPRIVMSG"));
139 fs = g_list_append(fs, g_strdup("CNOTICE"));
142 fs = g_list_append(fs, g_strdup("KNOCK"));
145 fs = g_list_append(fs, g_strdup("VCHANNELS"));
148 fs = g_list_append(fs, g_strdup("WHOX"));
151 fs = g_list_append(fs, g_strdup("CALLERID"));
154 fs = g_list_append(fs, g_strdup("ACCEPT"));
156 if (info->keylen != 0)
157 fs = g_list_append(fs, g_strdup_printf("KEYLEN=%d", info->keylen));
159 if (info->silence != 0)
160 fs = g_list_append(fs, g_strdup_printf("SILENCE=%d", info->silence));
162 if (info->chantypes != NULL)
163 fs = g_list_append(fs, g_strdup_printf("CHANTYPES=%s", info->chantypes));
165 if (info->chanmodes != NULL) {
166 char *tmp = g_strjoinv(",", info->chanmodes);
167 fs = g_list_append(fs, g_strdup_printf("CHANMODES=%s", tmp));
171 if (info->chanlimit != NULL) {
172 fs = g_list_append(fs, g_strdup_printf("CHANLIMIT=%s", info->chanlimit));
175 if (info->excepts_mode != '\0')
176 fs = g_list_append(fs, g_strdup_printf("EXCEPTS=%c", info->excepts_mode));
178 if (info->statusmsg != NULL)
179 fs = g_list_append(fs, g_strdup_printf("STATUSMSG=%s", info->statusmsg));
181 if (info->invex_mode != '\0')
182 fs = g_list_append(fs, g_strdup_printf("INVEX=%c", info->invex_mode));
184 if (info->maxlist != NULL)
185 fs = g_list_append(fs, g_strdup_printf("MAXLIST=%s", info->maxlist));
187 if (info->idchan != NULL)
188 fs = g_list_append(fs, g_strdup_printf("IDCHAN=%s", info->idchan));
190 if (info->prefix != NULL)
191 fs = g_list_append(fs, g_strdup_printf("PREFIX=%s", info->prefix));
193 ret = list_make_string(fs);
195 for (gl = fs; gl; gl = gl->next)
203 void network_info_parse(struct network_info *info, const char *parameter)
208 sep = strchr(parameter, '=');
211 key = g_strdup(parameter);
214 key = g_strndup(parameter, sep - parameter);
215 val = g_strdup(sep+1);
218 if(!g_strcasecmp(key, "CASEMAPPING")) {
219 if(!g_strcasecmp(val, "rfc1459")) {
220 info->casemapping = CASEMAP_RFC1459;
221 } else if(!g_strcasecmp(val, "strict-rfc1459")) {
222 info->casemapping = CASEMAP_STRICT_RFC1459;
223 } else if(!g_strcasecmp(val, "ascii")) {
224 info->casemapping = CASEMAP_ASCII;
226 info->casemapping = CASEMAP_UNKNOWN;
227 log_global(LOG_WARNING, "Unknown CASEMAPPING value '%s'", val);
229 } else if (!g_strcasecmp(key, "NETWORK")) {
231 info->name = g_strdup(val);
232 } else if (!g_strcasecmp(key, "NICKLEN") || !g_strcasecmp(key, "MAXNICKLEN")) {
233 info->nicklen = atoi(val);
234 } else if (!g_strcasecmp(key, "USERLEN")) {
235 info->userlen = atoi(val);
236 } else if (!g_strcasecmp(key, "HOSTLEN")) {
237 info->hostlen = atoi(val);
238 } else if (!g_strcasecmp(key, "CHANNELLEN") || !g_strcasecmp(key, "MAXCHANNELLEN")) {
239 info->channellen = atoi(val);
240 } else if (!g_strcasecmp(key, "AWAYLEN")) {
241 info->awaylen = atoi(val);
242 } else if (!g_strcasecmp(key, "KICKLEN")) {
243 info->kicklen = atoi(val);
244 } else if (!g_strcasecmp(key, "TOPICLEN")) {
245 info->topiclen = atoi(val);
246 } else if (!g_strcasecmp(key, "MAXCHANNELS")) {
247 info->maxchannels = atoi(val);
248 } else if (!g_strcasecmp(key, "MAXTARGETS")) {
249 info->maxtargets = atoi(val);
250 } else if (!g_strcasecmp(key, "MAXBANS")) {
251 info->maxbans = atoi(val);
252 } else if (!g_strcasecmp(key, "MODES")) {
253 info->maxmodes = atoi(val);
254 } else if (!g_strcasecmp(key, "WALLCHOPS")) {
255 info->wallchops = TRUE;
256 } else if (!g_strcasecmp(key, "WALLVOICES")) {
257 info->wallvoices = TRUE;
258 } else if (!g_strcasecmp(key, "RFC2812")) {
259 info->rfc2812 = TRUE;
260 } else if (!g_strcasecmp(key, "PENALTY")) {
261 info->penalty = TRUE;
262 } else if (!g_strcasecmp(key, "FNC")) {
263 info->forced_nick_changes = TRUE;
264 } else if (!g_strcasecmp(key, "SAFELIST")) {
265 info->safelist = TRUE;
266 } else if (!g_strcasecmp(key, "USERIP")) {
268 } else if (!g_strcasecmp(key, "CPRIVMSG")) {
269 info->cprivmsg = TRUE;
270 } else if (!g_strcasecmp(key, "CNOTICE")) {
271 info->cnotice = TRUE;
272 } else if (!g_strcasecmp(key, "KNOCK")) {
274 } else if (!g_strcasecmp(key, "CAPAB")) {
276 } else if (!g_strcasecmp(key, "VCHANNELS")) {
277 info->vchannels = TRUE;
278 } else if (!g_strcasecmp(key, "WHOX")) {
280 } else if (!g_strcasecmp(key, "CALLERID")) {
281 info->callerid = TRUE;
282 } else if (!g_strcasecmp(key, "ACCEPT")) {
284 } else if (!g_strcasecmp(key, "KEYLEN")) {
285 info->keylen = atoi(val);
286 } else if (!g_strcasecmp(key, "SILENCE")) {
287 info->silence = atoi(val);
288 } else if (!g_strcasecmp(key, "CHANTYPES")) {
289 g_free(info->chantypes);
290 info->chantypes = g_strdup(val);
291 } else if (!g_strcasecmp(key, "CHANMODES")) {
292 g_strfreev(info->chanmodes);
293 info->chanmodes = g_strsplit(val, ",", 4);
294 /* FIXME: Make sure info->chanmodes length is exactly 4 */
295 } else if (!g_strcasecmp(key, "CHANLIMIT")) {
296 g_free(info->chanlimit);
297 info->chanlimit = g_strdup(val);
298 } else if (!g_strcasecmp(key, "EXCEPTS")) {
300 info->excepts_mode = 'e';
301 else if (strlen(val) > 1)
302 log_global(LOG_WARNING, "Invalid length excepts value: %s", val);
304 info->excepts_mode = val[0];
305 } else if (!g_strcasecmp(key, "INVEX")) {
307 info->invex_mode = 'I';
308 else if (strlen(val) > 1)
309 log_global(LOG_WARNING, "Invalid length invex value: %s", val);
311 info->invex_mode = val[0];
312 } else if (!g_strcasecmp(key, "MAXLIST")) {
313 g_free(info->maxlist);
314 info->maxlist = g_strdup(val);
315 } else if (!g_strcasecmp(key, "IDCHAN")) {
316 g_free(info->idchan);
317 info->idchan = g_strdup(val);
318 } else if (!g_strcasecmp(key, "STATUSMSG")) {
319 g_free(info->statusmsg);
320 info->statusmsg = g_strdup(val);
321 } else if (!g_strcasecmp(key, "PREFIX")) {
322 g_free(info->prefix);
323 info->prefix = g_strdup(val);
324 } else if (!g_strcasecmp(key, "CHARSET")) {
325 g_free(info->charset);
326 info->charset = g_strdup(val);
328 log_global(LOG_WARNING, "Unknown 005 parameter `%s'", key);
334 void handle_005(struct network_state *s, struct line *l)
340 g_assert(l->argc >= 1);
342 for (i = 3; i < l->argc-1; i++)
343 network_info_parse(&s->info, l->args[i]);
346 int irccmp(const struct network_info *n, const char *a, const char *b)
348 switch(n != NULL?n->casemapping:CASEMAP_UNKNOWN) {
350 case CASEMAP_UNKNOWN:
351 case CASEMAP_RFC1459:
352 return str_rfc1459cmp(a,b);
354 return str_asciicmp(a,b);
355 case CASEMAP_STRICT_RFC1459:
356 return str_strictrfc1459cmp(a,b);
362 gboolean is_channelname(const char *name, const struct network_info *n)
365 g_assert(n->chantypes != NULL);
367 g_assert(name != NULL);
369 if (strchr(n->chantypes, name[0]))
375 gboolean is_prefix(char p, const struct network_info *n)
377 const char *pref_end;
380 g_assert(n->prefix != NULL);
382 pref_end = strchr(n->prefix, ')');
383 if (!pref_end)pref_end = n->prefix; else pref_end++;
385 if(strchr(pref_end, p)) return TRUE;
389 const char *get_charset(const struct network_info *n)
392 g_assert(n->charset != NULL);
396 char get_prefix_by_mode(char mode, const struct network_info *n)
403 g_assert(n->prefix != NULL);
407 pref_end = strchr(prefix, ')');
408 if(prefix[0] != '(' || !pref_end) {
409 log_global(LOG_WARNING, "Malformed PREFIX data `%s'", prefix);
415 for(i = 0; pref_end[i]; i++) {
416 if(prefix[i] == mode) return pref_end[i];
421 const static char *default_chanmodes[] = {
422 "beI", "k" , "l" , "imnpsta"
425 int network_chanmode_type(char m, struct network_info *n)
428 for (i = 0; i < 4; i++) {
429 if (strchr((n && n->chanmodes)?n->chanmodes[i]:default_chanmodes[i], m)) {