2 * Copyright (C) 2000-2,2004 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * A simple URL parser.
35 static struct mapping_t UrlMap[] =
42 { "mailto", U_MAILTO },
49 static void url_pct_decode (char *s)
58 if (*s == '%' && s[1] && s[2] &&
59 isxdigit ((unsigned char) s[1]) &&
60 isxdigit ((unsigned char) s[2]) &&
61 hexval (s[1]) >= 0 && hexval (s[2]) >= 0)
63 *d++ = (hexval (s[1]) << 4) | (hexval (s[2]));
72 url_scheme_t url_check_scheme (const char *s)
78 if (!s || !(t = strchr (s, ':')))
80 if ((t - s) + 1 >= sizeof (sbuf))
83 strfcpy (sbuf, s, t - s + 1);
84 for (t = sbuf; *t; t++)
85 *t = ascii_tolower (*t);
87 if ((i = mutt_getvaluebyname (sbuf, UrlMap)) == -1)
90 return (url_scheme_t) i;
93 int url_parse_file (char *d, const char *src, size_t dl)
95 if (ascii_strncasecmp (src, "file:", 5))
97 else if (!ascii_strncasecmp (src, "file://", 7)) /* we don't support remote files */
100 strfcpy (d, src + 5, dl);
106 /* ciss_parse_userhost: fill in components of ciss with info from src. Note
107 * these are pointers into src, which is altered with '\0's. Port of 0
108 * means no port given. */
109 static char *ciss_parse_userhost (ciss_url_t *ciss, char *src)
120 if (strncmp (src, "//", 2))
125 if ((path = strchr (src, '/')))
128 if ((t = strrchr (src, '@')))
131 if ((p = strchr (src, ':')))
135 url_pct_decode (ciss->pass);
138 url_pct_decode (ciss->user);
144 if ((p = strchr (t, ':')))
147 if (mutt_atos (p, (short*) &ciss->port) < 0)
154 url_pct_decode (ciss->host);
158 /* url_parse_ciss: Fill in ciss_url_t. char* elements are pointers into src,
159 * which is modified by this call (duplicate it first if you need to). */
160 int url_parse_ciss (ciss_url_t *ciss, char *src)
164 if ((ciss->scheme = url_check_scheme (src)) == U_UNKNOWN)
167 tmp = strchr (src, ':') + 1;
169 if ((ciss->path = ciss_parse_userhost (ciss, tmp)) == NULL)
171 url_pct_decode (ciss->path);
176 /* url_ciss_tostring: output the URL string for a given CISS object. */
178 int url_ciss_tostring (ciss_url_t* ciss, char* dest, size_t len, int flags)
182 if (ciss->scheme == U_UNKNOWN)
185 snprintf (dest, len, "%s:", mutt_getnamebyvalue (ciss->scheme, UrlMap));
189 if (!(flags & U_PATH))
190 safe_strcat (dest, len, "//");
191 len -= (l = strlen (dest)); dest += l;
194 if (flags & U_DECODE_PASSWD && ciss->pass)
195 snprintf (dest, len, "%s:%s@", ciss->user, ciss->pass);
197 snprintf (dest, len, "%s@", ciss->user);
199 len -= (l = strlen (dest)); dest += l;
203 snprintf (dest, len, "%s:%hu/", ciss->host, ciss->port);
205 snprintf (dest, len, "%s/", ciss->host);
209 safe_strcat (dest, len, ciss->path);
214 int url_parse_mailto (ENVELOPE *e, char **body, const char *src)
220 char scratch[HUGE_STRING];
226 if (!(t = strchr (src, ':')))
229 if ((tmp = safe_strdup (t + 1)) == NULL)
232 if ((headers = strchr (tmp, '?')))
235 url_pct_decode (tmp);
236 e->to = rfc822_parse_adrlist (e->to, tmp);
238 tag = headers ? strtok_r (headers, "&", &p) : NULL;
240 for (; tag; tag = strtok_r (NULL, "&", &p))
242 if ((value = strchr (tag, '=')))
244 if (!value || !*value)
247 url_pct_decode (tag);
248 url_pct_decode (value);
250 if (!ascii_strcasecmp (tag, "body"))
253 mutt_str_replace (body, value);
255 else if ((taglen = mutt_strlen (tag)) <= sizeof (scratch) - 2)
257 /* only try to parse if we can format it as header for
258 * mutt_parse_rfc822_line (tag fits in scratch) */
259 snprintf (scratch, sizeof (scratch), "%s: %s", tag, value);
260 scratch[taglen] = '\0';
261 value = &scratch[taglen+1];
263 mutt_parse_rfc822_line (e, NULL, scratch, value, 1, 0, 0, &last);