Add simple tests for libirc.
[jelmer/ctrlproxy.git] / lib / url.c
1 /*
2         ctrlproxy: A modular IRC proxy
3         (c) 2007 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 3 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
21 #include "internals.h"
22 #include "irc.h"
23
24 char *irc_create_url(const char *server, const char *port, gboolean ssl)
25 {
26         if (ssl && (!strcmp("ircs", port) || !strcmp("994", port)))
27                 return g_strdup_printf("ircs://%s", server);
28         if (!ssl && (!strcmp("ircd", port) || !strcmp("6667", port)))
29                 return g_strdup_printf("irc://%s", server);
30         return g_strdup_printf("%s://%s:%s", (ssl?"ircs":"irc"), server, port);
31 }
32
33 gboolean irc_parse_url(const char *url, char **server, char **port, gboolean *ssl)
34 {
35         char *p, *q;
36
37         if (!strncmp(url, "irc://", strlen("irc://"))) {
38                 *ssl = FALSE;
39                 url += strlen("irc://");
40         } else if (!strncmp(url, "ircs://", strlen("ircs://"))) {
41                 *ssl = TRUE;
42                 url += strlen("ircs://");
43         } else if (strstr(url, "://")) {
44                 *server = NULL;
45                 *port = NULL;
46                 *ssl = FALSE;
47                 return FALSE;
48         } else {
49                 *ssl = FALSE;
50         }
51
52         q = strchr(url, '/');
53         p = strchr(url, ':');
54         if (p != NULL && (q == NULL || p < q)) {
55                 *port = g_strdup(p+1);
56                 *server = g_strndup(url, p-url);
57                 return TRUE;
58         } 
59         
60         if (*ssl) {
61                 *port = g_strdup(IRCS_PORT);
62         } else {
63                 *port = g_strdup(IRC_PORT);
64         }
65
66         if (q != NULL) {
67                 *server = g_strndup(url, q - url);
68         } else {
69                 *server = g_strdup(url);
70         }
71
72         return TRUE;
73 }