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.
29 static struct mapping_t UrlMap[] = {
44 static void url_pct_decode (char *s)
51 for (d = s; *s; s++) {
52 if (*s == '%' && s[1] && s[2] &&
53 isxdigit ((unsigned char) s[1]) &&
54 isxdigit ((unsigned char) s[2]) &&
55 hexval (s[1]) >= 0 && hexval (s[2]) >= 0) {
56 *d++ = (hexval (s[1]) << 4) | (hexval (s[2]));
65 url_scheme_t url_check_scheme (const char *s)
71 if (!s || !(t = strchr (s, ':')))
73 if ((t - s) + 1 >= sizeof (sbuf))
76 strfcpy (sbuf, s, t - s + 1);
77 for (t = sbuf; *t; t++)
78 *t = ascii_tolower (*t);
80 if ((i = mutt_getvaluebyname (sbuf, UrlMap)) == -1)
83 return (url_scheme_t) i;
86 int url_parse_file (char *d, const char *src, size_t dl)
88 if (ascii_strncasecmp (src, "file:", 5))
90 else if (!ascii_strncasecmp (src, "file://", 7)) /* we don't support remote files */
93 strfcpy (d, src + 5, dl);
99 /* ciss_parse_userhost: fill in components of ciss with info from src. Note
100 * these are pointers into src, which is altered with '\0's. Port of 0
101 * means no port given. */
102 static char *ciss_parse_userhost (ciss_url_t * ciss, char *src)
113 if (strncmp (src, "//", 2))
118 if ((path = strchr (src, '/')))
121 if ((t = strrchr (src, '@'))) {
123 if ((p = strchr (src, ':'))) {
126 url_pct_decode (ciss->pass);
129 url_pct_decode (ciss->user);
135 if ((p = strchr (t, ':'))) {
137 ciss->port = atoi (p);
143 url_pct_decode (ciss->host);
147 /* url_parse_ciss: Fill in ciss_url_t. char* elements are pointers into src,
148 * which is modified by this call (duplicate it first if you need to). */
149 int url_parse_ciss (ciss_url_t * ciss, char *src)
153 if ((ciss->scheme = url_check_scheme (src)) == U_UNKNOWN)
156 tmp = strchr (src, ':') + 1;
158 ciss->path = ciss_parse_userhost (ciss, tmp);
159 url_pct_decode (ciss->path);
164 /* url_ciss_tostring: output the URL string for a given CISS object. */
166 int url_ciss_tostring (ciss_url_t * ciss, char *dest, size_t len, int flags)
170 if (ciss->scheme == U_UNKNOWN)
173 snprintf (dest, len, "%s:", mutt_getnamebyvalue (ciss->scheme, UrlMap));
176 str_cat (dest, len, "//");
177 len -= (l = str_len (dest));
181 if (flags & U_DECODE_PASSWD && ciss->pass)
182 snprintf (dest, len, "%s:%s@", ciss->user, ciss->pass);
184 snprintf (dest, len, "%s@", ciss->user);
186 len -= (l = str_len (dest));
191 snprintf (dest, len, "%s:%hu/", ciss->host, ciss->port);
193 snprintf (dest, len, "%s/", ciss->host);
197 str_cat (dest, len, ciss->path);
202 int url_parse_mailto (ENVELOPE * e, char **body, const char *src)
208 char scratch[HUGE_STRING];
214 if (!(t = strchr (src, ':')))
217 if ((tmp = str_dup (t + 1)) == NULL)
220 if ((headers = strchr (tmp, '?')))
223 url_pct_decode (tmp);
224 e->to = rfc822_parse_adrlist (e->to, tmp);
226 tag = headers ? strtok (headers, "&") : NULL;
228 for (; tag; tag = strtok (NULL, "&")) {
229 if ((value = strchr (tag, '=')))
231 if (!value || !*value)
234 url_pct_decode (tag);
235 url_pct_decode (value);
237 if (!ascii_strcasecmp (tag, "body")) {
239 str_replace (body, value);
242 #define SAFEPFX (option (OPTSTRICTMAILTO) ? "" : "X-Mailto-")
243 taglen = str_len (tag) + str_len (SAFEPFX);
244 /* mutt_parse_rfc822_line makes some assumptions */
245 snprintf (scratch, sizeof (scratch), "%s%s: %s", SAFEPFX, tag, value);
247 scratch[taglen] = '\0';
248 value = &scratch[taglen + 1];
250 mutt_parse_rfc822_line (e, NULL, scratch, value, 1, 0, 0, &last);
251 /* if $strict_mailto is set, force editing headers to let
252 * users have a look at what we got */
253 if (!option (OPTSTRICTMAILTO)) {
254 set_option (OPTXMAILTO);
255 set_option (OPTEDITHDRS);