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.
18 #include <lib-lib/mem.h>
31 static struct mapping_t UrlMap[] = {
46 static void url_pct_decode (char *s)
53 for (d = s; *s; s++) {
54 if (*s == '%' && s[1] && s[2] &&
55 isxdigit ((unsigned char) s[1]) &&
56 isxdigit ((unsigned char) s[2]) &&
57 hexval (s[1]) >= 0 && hexval (s[2]) >= 0) {
58 *d++ = (hexval (s[1]) << 4) | (hexval (s[2]));
67 url_scheme_t url_check_scheme (const char *s)
73 if (!s || !(t = strchr (s, ':')))
75 if ((t - s) + 1 >= sizeof (sbuf))
78 strfcpy (sbuf, s, t - s + 1);
79 for (t = sbuf; *t; t++)
80 *t = ascii_tolower (*t);
82 if ((i = mutt_getvaluebyname (sbuf, UrlMap)) == -1)
85 return (url_scheme_t) i;
88 int url_parse_file (char *d, const char *src, size_t dl)
90 if (ascii_strncasecmp (src, "file:", 5))
92 else if (!ascii_strncasecmp (src, "file://", 7)) /* we don't support remote files */
95 strfcpy (d, src + 5, dl);
101 /* ciss_parse_userhost: fill in components of ciss with info from src. Note
102 * these are pointers into src, which is altered with '\0's. Port of 0
103 * means no port given. */
104 static char *ciss_parse_userhost (ciss_url_t * ciss, char *src)
115 if (strncmp (src, "//", 2))
120 if ((path = strchr (src, '/')))
123 if ((t = strrchr (src, '@'))) {
125 if ((p = strchr (src, ':'))) {
128 url_pct_decode (ciss->pass);
131 url_pct_decode (ciss->user);
137 if ((p = strchr (t, ':'))) {
139 ciss->port = atoi (p);
145 url_pct_decode (ciss->host);
149 /* url_parse_ciss: Fill in ciss_url_t. char* elements are pointers into src,
150 * which is modified by this call (duplicate it first if you need to). */
151 int url_parse_ciss (ciss_url_t * ciss, char *src)
155 if ((ciss->scheme = url_check_scheme (src)) == U_UNKNOWN)
158 tmp = strchr (src, ':') + 1;
160 ciss->path = ciss_parse_userhost (ciss, tmp);
161 url_pct_decode (ciss->path);
166 /* url_ciss_tostring: output the URL string for a given CISS object. */
168 int url_ciss_tostring (ciss_url_t * ciss, char *dest, size_t len, int flags)
172 if (ciss->scheme == U_UNKNOWN)
175 snprintf (dest, len, "%s:", mutt_getnamebyvalue (ciss->scheme, UrlMap));
178 str_cat (dest, len, "//");
179 len -= (l = str_len (dest));
183 if (flags & U_DECODE_PASSWD && ciss->pass)
184 snprintf (dest, len, "%s:%s@", ciss->user, ciss->pass);
186 snprintf (dest, len, "%s@", ciss->user);
188 len -= (l = str_len (dest));
193 snprintf (dest, len, "%s:%hu/", ciss->host, ciss->port);
195 snprintf (dest, len, "%s/", ciss->host);
199 str_cat (dest, len, ciss->path);
204 int url_parse_mailto (ENVELOPE * e, char **body, const char *src)
210 char scratch[HUGE_STRING];
216 if (!(t = strchr (src, ':')))
219 if ((tmp = str_dup (t + 1)) == NULL)
222 if ((headers = strchr (tmp, '?')))
225 url_pct_decode (tmp);
226 e->to = rfc822_parse_adrlist (e->to, tmp);
228 tag = headers ? strtok (headers, "&") : NULL;
230 for (; tag; tag = strtok (NULL, "&")) {
231 if ((value = strchr (tag, '=')))
233 if (!value || !*value)
236 url_pct_decode (tag);
237 url_pct_decode (value);
239 if (!ascii_strcasecmp (tag, "body")) {
241 str_replace (body, value);
244 #define SAFEPFX (option (OPTSTRICTMAILTO) ? "" : "X-Mailto-")
245 taglen = str_len (tag) + str_len (SAFEPFX);
246 /* mutt_parse_rfc822_line makes some assumptions */
247 snprintf (scratch, sizeof (scratch), "%s%s: %s", SAFEPFX, tag, value);
249 scratch[taglen] = '\0';
250 value = &scratch[taglen + 1];
252 mutt_parse_rfc822_line (e, NULL, scratch, value, 1, 0, 0, &last);
253 /* if $strict_mailto is set, force editing headers to let
254 * users have a look at what we got */
255 if (!option (OPTSTRICTMAILTO)) {
256 set_option (OPTXMAILTO);
257 set_option (OPTEDITHDRS);