no more SHORT_STRING's. begin some doc.
[apps/madmutt.git] / lib-lib / url.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19 /*
20  * Copyright notice from original mutt:
21  * Copyright (C) 2000 Thomas Roessler <roessler@does-not-exist.org>
22  *
23  * This file is part of mutt-ng, see http://www.muttng.org/.
24  * It's licensed under the GNU General Public License,
25  * please see the file GPL in the top level source directory.
26  */
27
28 /*
29  * A simple URL parser.
30  */
31
32 #include "lib-lib.h"
33
34 static struct mapping_t UrlMap[] = {
35     {"file",   U_FILE},
36     {"imap",   U_IMAP},
37     {"imaps",  U_IMAPS},
38     {"pop",    U_POP},
39     {"pops",   U_POPS},
40     {"nntp",   U_NNTP},
41     {"news",   U_NNTP},
42     {"nntps",  U_NNTPS},
43     {"snews",  U_NNTPS},
44     {"mailto", U_MAILTO},
45     {NULL,     U_UNKNOWN}
46 };
47
48 /* decode url escaping */
49 char *url_decode(char *p)
50 {
51     char *q = p;
52
53     if (!p)
54         return NULL;
55
56     while (*q) {
57         if (*q == '%' && hexval(q[1]) >= 0 && hexval(q[2]) >= 0) {
58             *p++ = (hexval(q[1]) << 4) | hexval(q[2]);
59             q += 3;
60         } else {
61             *p++ = *q++;
62         }
63     }
64
65     *p = '\0';
66     return p;
67 }
68
69 url_scheme_t url_check_scheme(const char *s)
70 {
71     char sbuf[STRING];
72     char *t;
73     int i;
74
75     if (!s || !(t = strchr(s, ':')))
76         return U_UNKNOWN;
77
78     m_strncpy(sbuf, sizeof(sbuf), s, t - s);
79     i = mutt_getvaluebyname(sbuf, UrlMap);
80     return i == -1 ? U_UNKNOWN : i;
81 }
82
83 /* ciss_parse_userhost: fill in components of ciss with info from src. Note
84  *   these are pointers into src, which is altered with '\0's. Port of 0
85  *   means no port given.
86  * FIXME: THIS IS TASTELESS
87  */
88 static char *ciss_parse_userhost(ciss_url_t *ciss, char *src)
89 {
90     char *t;
91     char *p;
92     char *path;
93
94     ciss->user = NULL;
95     ciss->pass = NULL;
96     ciss->host = NULL;
97     ciss->port = 0;
98
99     if (m_strncmp(src, "//", 2))
100         return src;
101
102     src += 2;
103
104     if ((path = strchr(src, '/')))
105         *path++ = '\0';
106
107     if ((t = strrchr(src, '@'))) {
108         *t = '\0';
109         if ((p = strchr (src, ':'))) {
110             *p = '\0';
111             ciss->pass = p + 1;
112             url_decode (ciss->pass);
113         }
114         ciss->user = src;
115         url_decode (ciss->user);
116         t++;
117     }
118     else
119         t = src;
120
121     if ((p = strchr (t, ':'))) {
122         *p++ = '\0';
123         ciss->port = atoi (p);
124     }
125     else
126         ciss->port = 0;
127
128     ciss->host = t;
129     url_decode(ciss->host);
130     return path;
131 }
132
133 /* url_parse_ciss: Fill in ciss_url_t. char* elements are pointers into src,
134  *   which is modified by this call (duplicate it first if you need to).
135  * FIXME: THIS IS TASTELESS
136  */
137 int url_parse_ciss(ciss_url_t *ciss, char *src)
138 {
139     ciss->scheme = url_check_scheme(src);
140     if (ciss->scheme == U_UNKNOWN)
141         return -1;
142
143     ciss->path = ciss_parse_userhost(ciss, strchr(src, ':') + 1);
144     url_decode(ciss->path);
145
146     return 0;
147 }
148
149 /* url_ciss_tostring: output the URL string for a given CISS object. */
150 int url_ciss_tostring(ciss_url_t *ciss, char *dst, ssize_t len, int flags)
151 {
152     ssize_t l = 0;
153
154     if (ciss->scheme == U_UNKNOWN)
155         return -1;
156
157     snprintf(dst, len, "%s:", mutt_getnamebyvalue(ciss->scheme, UrlMap));
158
159     if (ciss->host) {
160         l = m_strcat(dst, len, "//");
161
162         if (ciss->user) {
163             if ((flags & U_DECODE_PASSWD) && ciss->pass) {
164                 l += snprintf(dst + l, len - l, "%s:%s@", ciss->user, ciss->pass);
165             } else {
166                 l += snprintf(dst + l, len - l, "%s@", ciss->user);
167             }
168         }
169
170         if (ciss->port) {
171             l += snprintf(dst + l, len - l, "%s:%hu/", ciss->host, ciss->port);
172         } else {
173             l += snprintf(dst + l, len - l, "%s/", ciss->host);
174         }
175     }
176
177     l += m_strcpy(dst + l, len - l, ciss->path);
178
179     return 0;
180 }