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[] =
45 { "mailto", U_MAILTO },
50 static void url_pct_decode (char *s)
59 if (*s == '%' && s[1] && s[2] &&
60 isxdigit ((unsigned char) s[1]) &&
61 isxdigit ((unsigned char) s[2]) &&
62 hexval (s[1]) >= 0 && hexval (s[2]) >= 0)
64 *d++ = (hexval (s[1]) << 4) | (hexval (s[2]));
73 url_scheme_t url_check_scheme (const char *s)
79 if (!s || !(t = strchr (s, ':')))
81 if ((t - s) + 1 >= sizeof (sbuf))
84 strfcpy (sbuf, s, t - s + 1);
85 for (t = sbuf; *t; t++)
86 *t = ascii_tolower (*t);
88 if ((i = mutt_getvaluebyname (sbuf, UrlMap)) == -1)
91 return (url_scheme_t) i;
94 int url_parse_file (char *d, const char *src, size_t dl)
96 if (ascii_strncasecmp (src, "file:", 5))
98 else if (!ascii_strncasecmp (src, "file://", 7)) /* we don't support remote files */
101 strfcpy (d, src + 5, dl);
107 /* ciss_parse_userhost: fill in components of ciss with info from src. Note
108 * these are pointers into src, which is altered with '\0's. Port of 0
109 * means no port given. */
110 static char *ciss_parse_userhost (ciss_url_t *ciss, char *src)
121 if (strncmp (src, "//", 2))
126 if ((path = strchr (src, '/')))
129 if ((t = strrchr (src, '@')))
132 if ((p = strchr (src, ':')))
136 url_pct_decode (ciss->pass);
139 url_pct_decode (ciss->user);
145 if ((p = strchr (t, ':')))
148 ciss->port = atoi (p);
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 ciss->path = ciss_parse_userhost (ciss, tmp);
170 url_pct_decode (ciss->path);
175 /* url_ciss_tostring: output the URL string for a given CISS object. */
177 int url_ciss_tostring (ciss_url_t* ciss, char* dest, size_t len, int flags)
181 if (ciss->scheme == U_UNKNOWN)
184 snprintf (dest, len, "%s:", mutt_getnamebyvalue (ciss->scheme, UrlMap));
188 safe_strcat (dest, len, "//");
189 len -= (l = strlen (dest)); dest += l;
192 if (flags & U_DECODE_PASSWD && ciss->pass)
193 snprintf (dest, len, "%s:%s@", ciss->user, ciss->pass);
195 snprintf (dest, len, "%s@", ciss->user);
197 len -= (l = strlen (dest)); dest += l;
201 snprintf (dest, len, "%s:%hu/", ciss->host, ciss->port);
203 snprintf (dest, len, "%s/", ciss->host);
207 safe_strcat (dest, len, ciss->path);
212 int url_parse_mailto (ENVELOPE *e, char **body, const char *src)
218 char scratch[HUGE_STRING];
224 if (!(t = strchr (src, ':')))
227 if ((tmp = safe_strdup (t + 1)) == NULL)
230 if ((headers = strchr (tmp, '?')))
233 url_pct_decode (tmp);
234 e->to = rfc822_parse_adrlist (e->to, tmp);
236 tag = headers ? strtok (headers, "&") : NULL;
238 for (; tag; tag = strtok (NULL, "&"))
240 if ((value = strchr (tag, '=')))
242 if (!value || !*value)
245 url_pct_decode (tag);
246 url_pct_decode (value);
248 if (!ascii_strcasecmp (tag, "body"))
251 mutt_str_replace (body, value);
255 taglen = strlen (tag);
256 /* mutt_parse_rfc822_line makes some assumptions */
257 snprintf (scratch, sizeof (scratch), "%s: %s", tag, value);
258 scratch[taglen] = '\0';
259 value = &scratch[taglen+1];
261 mutt_parse_rfc822_line (e, NULL, scratch, value, 1, 0, 0, &last);