Support MAXNICKLEN and MAXCHANNELLEN
[jelmer/ctrlproxy.git] / src / isupport.c
1 /*
2         ctrlproxy: A modular IRC proxy
3         (c) 2005 Jelmer Vernooij <jelmer@nl.linux.org>
4
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.
9
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.
14
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.
18 */
19
20 #include "internals.h"
21
22 #define DEFAULT_PREFIX          "(ov)@+"
23 #define DEFAULT_CHANTYPES       "#&"
24 #define DEFAULT_CHARSET         "iso8859-15"
25
26 void network_info_init(struct network_info *info)
27 {
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);
32 }
33
34 void free_network_info(struct network_info *info)
35 {
36         g_free(info->prefix);
37         g_free(info->chantypes);
38         g_free(info->charset);
39         g_free(info->name);
40         g_free(info->server);
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);
46         g_free(info->idchan);
47         g_free(info->statusmsg);
48 }
49
50 char *network_info_string(struct network_info *info)
51 {
52         char *ret = NULL;
53         GList *fs = NULL, *gl;
54         char *casemap = NULL;
55
56         if (info->name != NULL)
57                 fs = g_list_append(fs, g_strdup_printf("NETWORK=%s", info->name));
58
59         switch (info->casemapping) {
60         default:
61         case CASEMAP_RFC1459:
62                 casemap = g_strdup("CASEMAPPING=rfc1459");
63                 break;
64         case CASEMAP_STRICT_RFC1459:
65                 casemap = g_strdup("CASEMAPPING=strict-rfc1459");
66                 break;
67         case CASEMAP_ASCII:
68                 casemap = g_strdup("CASEMAPPING=ascii");
69                 break;
70         }
71
72         if (casemap != NULL)
73                 fs = g_list_append(fs, casemap);
74
75         if (info->forced_nick_changes)
76                 fs = g_list_append(fs, g_strdup("FNC"));
77
78         if (info->charset != NULL)
79                 fs = g_list_append(fs, g_strdup_printf("CHARSET=%s", info->charset));
80
81         if (info->nicklen != 0)
82                 fs = g_list_append(fs, g_strdup_printf("NICKLEN=%d", info->nicklen));
83
84         if (info->userlen != 0)
85                 fs = g_list_append(fs, g_strdup_printf("USERLEN=%d", info->userlen));
86
87         if (info->hostlen != 0)
88                 fs = g_list_append(fs, g_strdup_printf("HOSTLEN=%d", info->hostlen));
89
90         if (info->channellen != 0)
91                 fs = g_list_append(fs, g_strdup_printf("CHANNELLEN=%d", info->channellen));
92
93         if (info->awaylen != 0)
94                 fs = g_list_append(fs, g_strdup_printf("AWAYLEN=%d", info->awaylen));
95
96         if (info->kicklen != 0)
97                 fs = g_list_append(fs, g_strdup_printf("KICKLEN=%d", info->kicklen));
98
99         if (info->topiclen != 0)
100                 fs = g_list_append(fs, g_strdup_printf("TOPICLEN=%d", info->topiclen));
101
102         if (info->maxchannels != 0)
103                 fs = g_list_append(fs, g_strdup_printf("MAXCHANNELS=%d", info->maxchannels));
104
105         if (info->maxtargets != 0)
106                 fs = g_list_append(fs, g_strdup_printf("MAXTARGETS=%d", info->maxtargets));
107
108         if (info->maxbans != 0)
109                 fs = g_list_append(fs, g_strdup_printf("MAXBANS=%d", info->maxbans));
110
111         if (info->maxmodes != 0)
112                 fs = g_list_append(fs, g_strdup_printf("MODES=%d", info->maxmodes));
113
114         if (info->wallchops)
115                 fs = g_list_append(fs, g_strdup("WALLCHOPS"));
116
117         if (info->wallvoices)
118                 fs = g_list_append(fs, g_strdup("WALLVOICES"));
119
120         if (info->rfc2812)
121                 fs = g_list_append(fs, g_strdup("RFC2812"));
122
123         if (info->penalty)
124                 fs = g_list_append(fs, g_strdup("PENALTY"));
125
126         if (info->safelist)
127                 fs = g_list_append(fs, g_strdup("SAFELIST"));
128         
129         if (info->userip)
130                 fs = g_list_append(fs, g_strdup("USERIP"));
131
132         if (info->capab)
133                 fs = g_list_append(fs, g_strdup("CAPAB"));
134
135         if (info->cprivmsg)
136                 fs = g_list_append(fs, g_strdup("CPRIVMSG"));
137
138         if (info->cnotice)
139                 fs = g_list_append(fs, g_strdup("CNOTICE"));
140
141         if (info->knock)
142                 fs = g_list_append(fs, g_strdup("KNOCK"));
143
144         if (info->vchannels)
145                 fs = g_list_append(fs, g_strdup("VCHANNELS"));
146
147         if (info->whox)
148                 fs = g_list_append(fs, g_strdup("WHOX"));
149
150         if (info->callerid)
151                 fs = g_list_append(fs, g_strdup("CALLERID"));
152
153         if (info->accept)
154                 fs = g_list_append(fs, g_strdup("ACCEPT"));
155
156         if (info->keylen != 0)
157                 fs = g_list_append(fs, g_strdup_printf("KEYLEN=%d", info->keylen));
158
159         if (info->silence != 0)
160                 fs = g_list_append(fs, g_strdup_printf("SILENCE=%d", info->silence));
161
162         if (info->chantypes != NULL)
163                 fs = g_list_append(fs, g_strdup_printf("CHANTYPES=%s", info->chantypes));
164
165         if (info->chanmodes != NULL) {
166                 char *tmp = g_strjoinv(",", info->chanmodes);
167                 fs = g_list_append(fs, g_strdup_printf("CHANMODES=%s", tmp));
168                 g_free(tmp);
169         }
170
171         if (info->chanlimit != NULL) {
172                 fs = g_list_append(fs, g_strdup_printf("CHANLIMIT=%s", info->chanlimit));
173         }
174
175         if (info->excepts_mode != '\0')
176                 fs = g_list_append(fs, g_strdup_printf("EXCEPTS=%c", info->excepts_mode));
177
178         if (info->statusmsg != NULL)
179                 fs = g_list_append(fs, g_strdup_printf("STATUSMSG=%s", info->statusmsg));
180
181         if (info->invex_mode != '\0')
182                 fs = g_list_append(fs, g_strdup_printf("INVEX=%c", info->invex_mode));
183
184         if (info->maxlist != NULL)
185                 fs = g_list_append(fs, g_strdup_printf("MAXLIST=%s", info->maxlist));
186
187         if (info->idchan != NULL)
188                 fs = g_list_append(fs, g_strdup_printf("IDCHAN=%s", info->idchan));
189
190         if (info->prefix != NULL) 
191                 fs = g_list_append(fs, g_strdup_printf("PREFIX=%s", info->prefix));
192
193         ret = list_make_string(fs);
194
195         for (gl = fs; gl; gl = gl->next)
196                 g_free(gl->data);
197
198         g_list_free(fs);
199
200         return ret;
201 }
202
203 void network_info_parse(struct network_info *info, const char *parameter)
204 {
205         char *sep;
206         char *key, *val;
207
208         sep = strchr(parameter, '=');
209
210         if (!sep) { 
211                 key = g_strdup(parameter);
212                 val = NULL; 
213         } else {
214                 key = g_strndup(parameter, sep - parameter);
215                 val = g_strdup(sep+1);
216         }
217         
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;
225                 } else {
226                         info->casemapping = CASEMAP_UNKNOWN;
227                         log_global(LOG_WARNING, "Unknown CASEMAPPING value '%s'", val);
228                 }
229         } else if (!g_strcasecmp(key, "NETWORK")) {
230                 g_free(info->name);
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")) {
267                 info->userip = TRUE;
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")) {
273                 info->knock = TRUE;
274         } else if (!g_strcasecmp(key, "CAPAB")) {
275                 info->capab = TRUE;
276         } else if (!g_strcasecmp(key, "VCHANNELS")) {
277                 info->vchannels = TRUE;
278         } else if (!g_strcasecmp(key, "WHOX")) {
279                 info->whox = TRUE;
280         } else if (!g_strcasecmp(key, "CALLERID")) {
281                 info->callerid = TRUE;
282         } else if (!g_strcasecmp(key, "ACCEPT")) {
283                 info->accept = TRUE;
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")) {
299                 if (val == NULL) 
300                         info->excepts_mode = 'e';
301                 else if (strlen(val) > 1)
302                         log_global(LOG_WARNING, "Invalid length excepts value: %s", val);
303                 else
304                         info->excepts_mode = val[0];
305         } else if (!g_strcasecmp(key, "INVEX")) {
306                 if (val == NULL) 
307                         info->invex_mode = 'I';
308                 else if (strlen(val) > 1)
309                         log_global(LOG_WARNING, "Invalid length invex value: %s", val);
310                 else
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);
327         } else {
328                 log_global(LOG_WARNING, "Unknown 005 parameter `%s'", key);
329         }
330         g_free(key);
331         g_free(val);
332 }
333
334 void handle_005(struct network_state *s, struct line *l)
335 {
336         int i;
337         g_assert(s);
338         g_assert(l);
339
340         g_assert(l->argc >= 1);
341
342         for (i = 3; i < l->argc-1; i++) 
343                 network_info_parse(&s->info, l->args[i]);
344 }
345
346 int irccmp(const struct network_info *n, const char *a, const char *b)
347 {
348         switch(n != NULL?n->casemapping:CASEMAP_UNKNOWN) {
349         default:
350         case CASEMAP_UNKNOWN:
351         case CASEMAP_RFC1459:
352                 return str_rfc1459cmp(a,b);
353         case CASEMAP_ASCII:
354                 return str_asciicmp(a,b);
355         case CASEMAP_STRICT_RFC1459:
356                 return str_strictrfc1459cmp(a,b);
357         }
358
359         return 0;
360 }
361
362 gboolean is_channelname(const char *name, const struct network_info *n)
363 {
364         g_assert(n != NULL);
365         g_assert(n->chantypes != NULL);
366
367         g_assert(name != NULL);
368         
369         if (strchr(n->chantypes, name[0])) 
370                 return TRUE;
371
372         return FALSE;
373 }
374
375 gboolean is_prefix(char p, const struct network_info *n)
376 {
377         const char *pref_end;
378         
379         g_assert(n != NULL);
380         g_assert(n->prefix != NULL);
381
382         pref_end = strchr(n->prefix, ')');
383         if (!pref_end)pref_end = n->prefix; else pref_end++;
384
385         if(strchr(pref_end, p)) return TRUE;
386         return FALSE;
387 }
388
389 const char *get_charset(const struct network_info *n)
390 {
391         g_assert(n != NULL);
392         g_assert(n->charset != NULL);
393         return n->charset;
394 }
395
396 char get_prefix_by_mode(char mode, const struct network_info *n)
397 {
398         int i;
399         char *pref_end;
400         const char *prefix;
401         
402         g_assert(n != NULL);
403         g_assert(n->prefix != NULL);
404
405         prefix = n->prefix;
406         
407         pref_end = strchr(prefix, ')');
408         if(prefix[0] != '(' || !pref_end) {
409                 log_global(LOG_WARNING, "Malformed PREFIX data `%s'", prefix);
410                 return ' ';
411         }
412         pref_end++;
413         prefix++;
414
415         for(i = 0; pref_end[i]; i++) {
416                 if(prefix[i] == mode) return pref_end[i];
417         }
418         return ' ';
419 }
420
421 const static char *default_chanmodes[] = { 
422         "beI", "k" , "l" , "imnpsta" 
423 };
424
425 int network_chanmode_type(char m, struct network_info *n)
426 {
427         int i;
428         for (i = 0; i < 4; i++) {
429                 if (strchr((n && n->chanmodes)?n->chanmodes[i]:default_chanmodes[i], m)) {
430                         return i;
431                 }
432         }
433
434         return 3;
435 }