2 * Copyright (C) 2000 Thomas Roessler <roessler@does-not-exist.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
20 * A simple URL parser.
35 static struct mapping_t UrlMap[] = {
49 static void url_pct_decode (char *s)
56 for (d = s; *s; s++) {
57 if (*s == '%' && s[1] && s[2] &&
58 isxdigit ((unsigned char) s[1]) &&
59 isxdigit ((unsigned char) s[2]) &&
60 hexval (s[1]) >= 0 && hexval (s[2]) >= 0) {
61 *d++ = (hexval (s[1]) << 4) | (hexval (s[2]));
70 url_scheme_t url_check_scheme (const char *s)
76 if (!s || !(t = strchr (s, ':')))
78 if ((t - s) + 1 >= sizeof (sbuf))
81 strfcpy (sbuf, s, t - s + 1);
82 for (t = sbuf; *t; t++)
83 *t = ascii_tolower (*t);
85 if ((i = mutt_getvaluebyname (sbuf, UrlMap)) == -1)
88 return (url_scheme_t) i;
91 int url_parse_file (char *d, const char *src, size_t dl)
93 if (ascii_strncasecmp (src, "file:", 5))
95 else if (!ascii_strncasecmp (src, "file://", 7)) /* we don't support remote files */
98 strfcpy (d, src + 5, dl);
104 /* ciss_parse_userhost: fill in components of ciss with info from src. Note
105 * these are pointers into src, which is altered with '\0's. Port of 0
106 * means no port given. */
107 static char *ciss_parse_userhost (ciss_url_t * ciss, char *src)
118 if (strncmp (src, "//", 2))
123 if ((path = strchr (src, '/')))
126 if ((t = strrchr (src, '@'))) {
128 if ((p = strchr (src, ':'))) {
131 url_pct_decode (ciss->pass);
134 url_pct_decode (ciss->user);
140 if ((p = strchr (t, ':'))) {
142 ciss->port = atoi (p);
148 url_pct_decode (ciss->host);
152 /* url_parse_ciss: Fill in ciss_url_t. char* elements are pointers into src,
153 * which is modified by this call (duplicate it first if you need to). */
154 int url_parse_ciss (ciss_url_t * ciss, char *src)
158 if ((ciss->scheme = url_check_scheme (src)) == U_UNKNOWN)
161 tmp = strchr (src, ':') + 1;
163 ciss->path = ciss_parse_userhost (ciss, tmp);
164 url_pct_decode (ciss->path);
169 /* url_ciss_tostring: output the URL string for a given CISS object. */
171 int url_ciss_tostring (ciss_url_t * ciss, char *dest, size_t len, int flags)
175 if (ciss->scheme == U_UNKNOWN)
178 snprintf (dest, len, "%s:", mutt_getnamebyvalue (ciss->scheme, UrlMap));
181 safe_strcat (dest, len, "//");
182 len -= (l = strlen (dest));
186 if (flags & U_DECODE_PASSWD && ciss->pass)
187 snprintf (dest, len, "%s:%s@", ciss->user, ciss->pass);
189 snprintf (dest, len, "%s@", ciss->user);
191 len -= (l = strlen (dest));
196 snprintf (dest, len, "%s:%hu/", ciss->host, ciss->port);
198 snprintf (dest, len, "%s/", ciss->host);
202 safe_strcat (dest, len, ciss->path);
207 int url_parse_mailto (ENVELOPE * e, char **body, const char *src)
213 char scratch[HUGE_STRING];
219 if (!(t = strchr (src, ':')))
222 if ((tmp = safe_strdup (t + 1)) == NULL)
225 if ((headers = strchr (tmp, '?')))
228 url_pct_decode (tmp);
229 e->to = rfc822_parse_adrlist (e->to, tmp);
231 tag = headers ? strtok (headers, "&") : NULL;
233 for (; tag; tag = strtok (NULL, "&")) {
234 if ((value = strchr (tag, '=')))
236 if (!value || !*value)
239 url_pct_decode (tag);
240 url_pct_decode (value);
242 if (!ascii_strcasecmp (tag, "body")) {
244 mutt_str_replace (body, value);
247 taglen = strlen (tag);
248 /* mutt_parse_rfc822_line makes some assumptions */
249 snprintf (scratch, sizeof (scratch), "%s: %s", tag, value);
250 scratch[taglen] = '\0';
251 value = &scratch[taglen + 1];
253 mutt_parse_rfc822_line (e, NULL, scratch, value, 1, 0, 0, &last);