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>
19 #include <lib-lib/ascii.h>
20 #include <lib-lib/mapping.h>
22 #include <lib-mime/mime.h>
30 static struct mapping_t UrlMap[] = {
45 static void url_pct_decode (char *s)
52 for (d = s; *s; s++) {
53 if (*s == '%' && hexval (s[1]) >= 0 && hexval (s[2]) >= 0) {
54 *d++ = (hexval (s[1]) << 4) | (hexval (s[2]));
63 url_scheme_t url_check_scheme (const char *s)
69 if (!s || !(t = strchr (s, ':')))
71 if ((t - s) + 1 >= sizeof (sbuf))
74 m_strcpy(sbuf, t - s + 1, s);
75 for (t = sbuf; *t; t++)
76 *t = ascii_tolower (*t);
78 if ((i = mutt_getvaluebyname (sbuf, UrlMap)) == -1)
81 return (url_scheme_t) i;
84 int url_parse_file (char *d, const char *src, size_t dl)
86 if (ascii_strncasecmp (src, "file:", 5))
88 else if (!ascii_strncasecmp (src, "file://", 7)) /* we don't support remote files */
91 m_strcpy(d, dl, src + 5);
97 /* ciss_parse_userhost: fill in components of ciss with info from src. Note
98 * these are pointers into src, which is altered with '\0's. Port of 0
99 * means no port given. */
100 static char *ciss_parse_userhost (ciss_url_t * ciss, char *src)
111 if (strncmp (src, "//", 2))
116 if ((path = strchr (src, '/')))
119 if ((t = strrchr (src, '@'))) {
121 if ((p = strchr (src, ':'))) {
124 url_pct_decode (ciss->pass);
127 url_pct_decode (ciss->user);
133 if ((p = strchr (t, ':'))) {
135 ciss->port = atoi (p);
141 url_pct_decode (ciss->host);
145 /* url_parse_ciss: Fill in ciss_url_t. char* elements are pointers into src,
146 * which is modified by this call (duplicate it first if you need to). */
147 int url_parse_ciss (ciss_url_t * ciss, char *src)
151 if ((ciss->scheme = url_check_scheme (src)) == U_UNKNOWN)
154 tmp = strchr (src, ':') + 1;
156 ciss->path = ciss_parse_userhost (ciss, tmp);
157 url_pct_decode (ciss->path);
162 /* url_ciss_tostring: output the URL string for a given CISS object. */
164 int url_ciss_tostring (ciss_url_t * ciss, char *dest, size_t len, int flags)
168 if (ciss->scheme == U_UNKNOWN)
171 snprintf (dest, len, "%s:", mutt_getnamebyvalue (ciss->scheme, UrlMap));
174 m_strcat(dest, len, "//");
175 len -= (l = m_strlen(dest));
179 if (flags & U_DECODE_PASSWD && ciss->pass)
180 snprintf (dest, len, "%s:%s@", ciss->user, ciss->pass);
182 snprintf (dest, len, "%s@", ciss->user);
184 len -= (l = m_strlen(dest));
189 snprintf (dest, len, "%s:%hu/", ciss->host, ciss->port);
191 snprintf (dest, len, "%s/", ciss->host);
195 m_strcat(dest, len, ciss->path);
200 int url_parse_mailto (ENVELOPE * e, char **body, const char *src)
206 char scratch[HUGE_STRING];
212 if (!(t = strchr (src, ':')))
215 if ((tmp = m_strdup(t + 1)) == NULL)
218 if ((headers = strchr (tmp, '?')))
221 url_pct_decode (tmp);
222 e->to = rfc822_parse_adrlist (e->to, tmp);
224 tag = headers ? strtok (headers, "&") : NULL;
226 for (; tag; tag = strtok (NULL, "&")) {
227 if ((value = strchr (tag, '=')))
229 if (!value || !*value)
232 url_pct_decode (tag);
233 url_pct_decode (value);
235 if (!ascii_strcasecmp (tag, "body")) {
237 m_strreplace(body, value);
240 #define SAFEPFX (option (OPTSTRICTMAILTO) ? "" : "X-Mailto-")
241 taglen = m_strlen(tag) + m_strlen(SAFEPFX);
242 /* mutt_parse_rfc822_line makes some assumptions */
243 snprintf (scratch, sizeof (scratch), "%s%s: %s", SAFEPFX, tag, value);
245 scratch[taglen] = '\0';
246 value = vskipspaces(&scratch[taglen + 1]);
247 mutt_parse_rfc822_line (e, NULL, scratch, value, 1, 0, 0, &last);
248 /* if $strict_mailto is set, force editing headers to let
249 * users have a look at what we got */
250 if (!option (OPTSTRICTMAILTO)) {
251 set_option (OPTXMAILTO);
252 set_option (OPTEDITHDRS);