2 * Copyright notice from original mutt:
3 * Copyright (C) 2000 Thomas Roessler <roessler@does-not-exist.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
11 * A simple URL parser.
26 static struct mapping_t UrlMap[] = {
40 static void url_pct_decode (char *s)
47 for (d = s; *s; s++) {
48 if (*s == '%' && s[1] && s[2] &&
49 isxdigit ((unsigned char) s[1]) &&
50 isxdigit ((unsigned char) s[2]) &&
51 hexval (s[1]) >= 0 && hexval (s[2]) >= 0) {
52 *d++ = (hexval (s[1]) << 4) | (hexval (s[2]));
61 url_scheme_t url_check_scheme (const char *s)
67 if (!s || !(t = strchr (s, ':')))
69 if ((t - s) + 1 >= sizeof (sbuf))
72 strfcpy (sbuf, s, t - s + 1);
73 for (t = sbuf; *t; t++)
74 *t = ascii_tolower (*t);
76 if ((i = mutt_getvaluebyname (sbuf, UrlMap)) == -1)
79 return (url_scheme_t) i;
82 int url_parse_file (char *d, const char *src, size_t dl)
84 if (ascii_strncasecmp (src, "file:", 5))
86 else if (!ascii_strncasecmp (src, "file://", 7)) /* we don't support remote files */
89 strfcpy (d, src + 5, dl);
95 /* ciss_parse_userhost: fill in components of ciss with info from src. Note
96 * these are pointers into src, which is altered with '\0's. Port of 0
97 * means no port given. */
98 static char *ciss_parse_userhost (ciss_url_t * ciss, char *src)
109 if (strncmp (src, "//", 2))
114 if ((path = strchr (src, '/')))
117 if ((t = strrchr (src, '@'))) {
119 if ((p = strchr (src, ':'))) {
122 url_pct_decode (ciss->pass);
125 url_pct_decode (ciss->user);
131 if ((p = strchr (t, ':'))) {
133 ciss->port = atoi (p);
139 url_pct_decode (ciss->host);
143 /* url_parse_ciss: Fill in ciss_url_t. char* elements are pointers into src,
144 * which is modified by this call (duplicate it first if you need to). */
145 int url_parse_ciss (ciss_url_t * ciss, char *src)
149 if ((ciss->scheme = url_check_scheme (src)) == U_UNKNOWN)
152 tmp = strchr (src, ':') + 1;
154 ciss->path = ciss_parse_userhost (ciss, tmp);
155 url_pct_decode (ciss->path);
160 /* url_ciss_tostring: output the URL string for a given CISS object. */
162 int url_ciss_tostring (ciss_url_t * ciss, char *dest, size_t len, int flags)
166 if (ciss->scheme == U_UNKNOWN)
169 snprintf (dest, len, "%s:", mutt_getnamebyvalue (ciss->scheme, UrlMap));
172 safe_strcat (dest, len, "//");
173 len -= (l = strlen (dest));
177 if (flags & U_DECODE_PASSWD && ciss->pass)
178 snprintf (dest, len, "%s:%s@", ciss->user, ciss->pass);
180 snprintf (dest, len, "%s@", ciss->user);
182 len -= (l = strlen (dest));
187 snprintf (dest, len, "%s:%hu/", ciss->host, ciss->port);
189 snprintf (dest, len, "%s/", ciss->host);
193 safe_strcat (dest, len, ciss->path);
198 int url_parse_mailto (ENVELOPE * e, char **body, const char *src)
204 char scratch[HUGE_STRING];
210 if (!(t = strchr (src, ':')))
213 if ((tmp = safe_strdup (t + 1)) == NULL)
216 if ((headers = strchr (tmp, '?')))
219 url_pct_decode (tmp);
220 e->to = rfc822_parse_adrlist (e->to, tmp);
222 tag = headers ? strtok (headers, "&") : NULL;
224 for (; tag; tag = strtok (NULL, "&")) {
225 if ((value = strchr (tag, '=')))
227 if (!value || !*value)
230 url_pct_decode (tag);
231 url_pct_decode (value);
233 if (!ascii_strcasecmp (tag, "body")) {
235 mutt_str_replace (body, value);
238 taglen = strlen (tag);
239 /* mutt_parse_rfc822_line makes some assumptions */
240 snprintf (scratch, sizeof (scratch), "%s: %s", tag, value);
241 scratch[taglen] = '\0';
242 value = &scratch[taglen + 1];
244 mutt_parse_rfc822_line (e, NULL, scratch, value, 1, 0, 0, &last);