Update to latest madtty.
[apps/madmutt.git] / lib-lib / url.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19 /*
20  * Copyright notice from original mutt:
21  * Copyright (C) 2000 Thomas Roessler <roessler@does-not-exist.org>
22  *
23  * This file is part of mutt-ng, see http://www.muttng.org/.
24  * It's licensed under the GNU General Public License,
25  * please see the file GPL in the top level source directory.
26  */
27
28 /*
29  * A simple URL parser.
30  */
31
32 #include "lib-lib.h"
33
34 static struct mapping_t UrlMap[] = {
35     {"file",   U_FILE},
36     {"imap",   U_IMAP},
37     {"imaps",  U_IMAPS},
38     {"pop",    U_POP},
39     {"pops",   U_POPS},
40     {"mailto", U_MAILTO},
41     {NULL,     U_UNKNOWN}
42 };
43
44 /* decode url escaping */
45 char *url_decode(char *p)
46 {
47     char *q = p;
48
49     if (!p)
50         return NULL;
51
52     while (*q) {
53         if (*q == '%' && hexval(q[1]) >= 0 && hexval(q[2]) >= 0) {
54             *p++ = (hexval(q[1]) << 4) | hexval(q[2]);
55             q += 3;
56         } else {
57             *p++ = *q++;
58         }
59     }
60
61     *p = '\0';
62     return p;
63 }
64
65 url_scheme_t url_check_scheme(const char *s)
66 {
67     char sbuf[STRING];
68     char *t;
69     int i;
70
71     if (!s || !(t = strchr(s, ':')))
72         return U_UNKNOWN;
73
74     m_strncpy(sbuf, sizeof(sbuf), s, t - s);
75     i = mutt_getvaluebyname(sbuf, UrlMap);
76     return i == -1 ? U_UNKNOWN : i;
77 }
78
79 /* ciss_parse_userhost: fill in components of ciss with info from src. Note
80  *   these are pointers into src, which is altered with '\0's. Port of 0
81  *   means no port given.
82  * FIXME: THIS IS TASTELESS
83  */
84 static char *ciss_parse_userhost(ciss_url_t *ciss, char *src)
85 {
86     char *t;
87     char *p;
88     char *path;
89
90     ciss->user = NULL;
91     ciss->pass = NULL;
92     ciss->host = NULL;
93     ciss->port = 0;
94
95     if (m_strncmp(src, "//", 2))
96         return src;
97
98     src += 2;
99
100     if ((path = strchr(src, '/')))
101         *path++ = '\0';
102
103     if ((t = strrchr(src, '@'))) {
104         *t = '\0';
105         if ((p = strchr (src, ':'))) {
106             *p = '\0';
107             ciss->pass = p + 1;
108             url_decode (ciss->pass);
109         }
110         ciss->user = src;
111         url_decode (ciss->user);
112         t++;
113     }
114     else
115         t = src;
116
117     if ((p = strchr (t, ':'))) {
118         *p++ = '\0';
119         ciss->port = atoi (p);
120     }
121     else
122         ciss->port = 0;
123
124     ciss->host = t;
125     url_decode(ciss->host);
126     return path;
127 }
128
129 /* url_parse_ciss: Fill in ciss_url_t. char* elements are pointers into src,
130  *   which is modified by this call (duplicate it first if you need to).
131  * FIXME: THIS IS TASTELESS
132  */
133 int url_parse_ciss(ciss_url_t *ciss, char *src)
134 {
135     ciss->scheme = url_check_scheme(src);
136     if (ciss->scheme == U_UNKNOWN)
137         return -1;
138
139     ciss->path = ciss_parse_userhost(ciss, strchr(src, ':') + 1);
140     url_decode(ciss->path);
141
142     return 0;
143 }
144
145 /* url_ciss_tostring: output the URL string for a given CISS object. */
146 int url_ciss_tostring(const ciss_url_t *ciss, char *dst, ssize_t len, int flags)
147 {
148     ssize_t l = 0;
149
150     if (ciss->scheme == U_UNKNOWN)
151         return -1;
152
153     snprintf(dst, len, "%s:", mutt_getnamebyvalue(ciss->scheme, UrlMap));
154
155     if (ciss->host) {
156         l = m_strcat(dst, len, "//");
157
158         if (ciss->user) {
159             if ((flags & U_DECODE_PASSWD) && ciss->pass) {
160                 l += snprintf(dst + l, len - l, "%s:%s@", ciss->user, ciss->pass);
161             } else {
162                 l += snprintf(dst + l, len - l, "%s@", ciss->user);
163             }
164         }
165
166         if (ciss->port) {
167             l += snprintf(dst + l, len - l, "%s:%hu/", ciss->host, ciss->port);
168         } else {
169             l += snprintf(dst + l, len - l, "%s/", ciss->host);
170         }
171     }
172
173     l += m_strcpy(dst + l, len - l, ciss->path);
174
175     return 0;
176 }