iconv: Bail out of the loop when an illegal sequence of bytes occurs.
[elinks/elinks-j605.git] / src / protocol / rewrite / rewrite.c
blob7e8c4c510322437c8e3636f1c41e521c0072f98b
1 /* URI rewriting module */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include "elinks.h"
9 #include "config/options.h"
10 #include "intl/gettext/libintl.h"
11 #include "main/event.h"
12 #include "main/module.h"
13 #include "protocol/rewrite/rewrite.h"
14 #include "protocol/uri.h"
15 #include "session/location.h"
16 #include "session/session.h"
17 #include "util/string.h"
20 enum uri_rewrite_type {
21 URI_REWRITE_DUMB,
22 URI_REWRITE_SMART,
26 /* TODO: An event hook for follow-url might also feel at home here. --jonas */
28 enum uri_rewrite_option {
29 URI_REWRITE_TREE,
31 URI_REWRITE_ENABLE_DUMB,
32 URI_REWRITE_ENABLE_SMART,
34 URI_REWRITE_DUMB_TREE,
35 URI_REWRITE_DUMB_TEMPLATE,
37 URI_REWRITE_SMART_TREE,
38 URI_REWRITE_SMART_TEMPLATE,
40 URI_REWRITE_OPTIONS,
43 static union option_info uri_rewrite_options[] = {
44 INIT_OPT_TREE("protocol", N_("URI rewriting"),
45 "rewrite", OPT_SORT,
46 N_("Rules for rewriting URIs entered in the goto dialog. "
47 "It makes it possible to define a set of prefixes that will "
48 "be expanded if they match a string entered in the goto "
49 "dialog. The prefixes can be dumb, meaning that they work "
50 "only like URI abbreviations, or smart ones, making it "
51 "possible to pass arguments to them like search engine "
52 "keywords.")),
54 INIT_OPT_BOOL("protocol.rewrite", N_("Enable dumb prefixes"),
55 "enable-dumb", 0, 1,
56 N_("Enable dumb prefixes - simple URI abbreviations which "
57 "can be written to the Goto URL dialog instead of actual URIs "
58 "- i.e. if you write 'elinks' there, you are directed to "
59 "http://elinks.cz/.")),
61 INIT_OPT_BOOL("protocol.rewrite", N_("Enable smart prefixes"),
62 "enable-smart", 0, 1,
63 N_("Enable smart prefixes - URI templates triggered by "
64 "writing given abbreviation to the Goto URL dialog followed "
65 "by a list of arguments from which the actual URI is composed "
66 "- i.e. 'gg:search keywords' or 'gn search keywords for "
67 "news'.")),
69 INIT_OPT_TREE("protocol.rewrite", N_("Dumb Prefixes"),
70 "dumb", OPT_AUTOCREATE | OPT_SORT,
71 N_("Dumb prefixes, see enable-dumb description for details.")),
73 INIT_OPT_STRING("protocol.rewrite.dumb", NULL,
74 "_template_", 0, "",
75 /* xgettext:no-c-format */
76 N_("Replacement URI for this dumbprefix:\n"
77 "%c in the string means the current URL\n"
78 "%% in the string means '%'")),
80 INIT_OPT_TREE("protocol.rewrite", N_("Smart Prefixes"),
81 "smart", OPT_AUTOCREATE | OPT_SORT,
82 N_("Smart prefixes, see enable-smart description for "
83 "details.")),
85 /* TODO: In some rare occations current link URI and referrer might
86 * also be useful and dare I mention some kind of proxy argument. --jonas */
87 INIT_OPT_STRING("protocol.rewrite.smart", NULL,
88 "_template_", 0, "",
89 /* xgettext:no-c-format */
90 N_("Replacement URI for this smartprefix:\n"
91 "%c in the string means the current URL\n"
92 "%s in the string means the whole argument to smartprefix\n"
93 "%0,%1,...,%9 means argument 0, 1, ..., 9\n"
94 "%% in the string means '%'")),
96 INIT_OPT_STRING("protocol.rewrite", N_("Default template"),
97 "default_template", 0, "",
98 /* xgettext:no-c-format */
99 N_("Default URI template used when the string entered in "
100 "the goto dialog does not appear to be a URI or a filename "
101 "(i.e. contains no '.', ':' or '/' characters), and does "
102 "not match any defined prefixes. Set the value to \"\" to "
103 "disable use of the default template rewrite rule.\n"
104 "\n"
105 "%c in the template means the current URL,\n"
106 "%s in the template means the whole string from the goto\n"
107 " dialog,\n"
108 "%0,%1,...,%9 mean the 1st,2nd,...,10th space-delimited part\n"
109 " of %s,\n"
110 "%% in the template means '%'.")),
112 #define INIT_OPT_DUMB_PREFIX(prefix, uri) \
113 INIT_OPT_STRING("protocol.rewrite.dumb", NULL, prefix, 0, uri, NULL)
115 INIT_OPT_DUMB_PREFIX("elinks", ELINKS_WEBSITE_URL),
116 INIT_OPT_DUMB_PREFIX("documentation", ELINKS_DOC_URL),
117 INIT_OPT_DUMB_PREFIX("bz", ELINKS_BUGS_URL),
118 INIT_OPT_DUMB_PREFIX("bug", ELINKS_BUGS_URL),
120 INIT_OPT_DUMB_PREFIX("arc", "http://web.archive.org/web/*/%c"),
121 INIT_OPT_DUMB_PREFIX("cia", "http://cia.vc/"),
122 INIT_OPT_DUMB_PREFIX("d", "http://www.dict.org"),
123 INIT_OPT_DUMB_PREFIX("ddg", "http://duckduckgo.com/?t=elinks"),
124 INIT_OPT_DUMB_PREFIX("g", "http://www.google.com/"),
125 INIT_OPT_DUMB_PREFIX("gg", "http://www.google.com/"),
126 INIT_OPT_DUMB_PREFIX("go", "http://www.google.com/"),
127 INIT_OPT_DUMB_PREFIX("fm", "http://freshmeat.net/"),
128 INIT_OPT_DUMB_PREFIX("sf", "http://www.sourceforge.net/"),
129 INIT_OPT_DUMB_PREFIX("dbug", "http://bugs.debian.org/"),
130 INIT_OPT_DUMB_PREFIX("dpkg", "http://packages.debian.org/"),
131 /* Hm, is this Debian-centric? -- Miciah */
132 /* Well, does anyone but Debian use lua40 naming convention? --pasky */
133 /* lua5.1 may be a more standard format. --KON */
134 INIT_OPT_DUMB_PREFIX("lua", "file:///usr/share/doc/lua5.1-doc/doc/contents.html#index"),
135 INIT_OPT_DUMB_PREFIX("pycur", "http://www.python.org/doc/current/"),
136 INIT_OPT_DUMB_PREFIX("pydev", "http://www.python.org/dev/doc/devel/"),
137 INIT_OPT_DUMB_PREFIX("e2", "http://www.everything2.org/"),
138 INIT_OPT_DUMB_PREFIX("sd", "http://slashdot.org/"),
139 INIT_OPT_DUMB_PREFIX("vhtml", "http://validator.w3.org/check?uri=%c"),
140 INIT_OPT_DUMB_PREFIX("vcss", "http://jigsaw.w3.org/css-validator/validator?uri=%c"),
142 #define INIT_OPT_SMART_PREFIX(prefix, uri) \
143 INIT_OPT_STRING("protocol.rewrite.smart", NULL, prefix, 0, uri, NULL)
144 #define bugzilla_prefix(prefix) (ELINKS_BUGS_URL prefix)
146 INIT_OPT_SMART_PREFIX("bug", bugzilla_prefix("show_bug.cgi?id=%s")),
148 #ifdef CONFIG_DEBUG
149 INIT_OPT_SMART_PREFIX("milestone-bugs", bugzilla_prefix("buglist.cgi?target_milestone=%s")),
150 INIT_OPT_SMART_PREFIX("search-bugs", bugzilla_prefix("buglist.cgi?short_desc_type=allwordssubstr&short_desc=%s")),
151 #endif
153 INIT_OPT_SMART_PREFIX("arc", "http://web.archive.org/web/*/%s"),
154 INIT_OPT_SMART_PREFIX("cambridge", "http://dictionary.cambridge.org/results.asp?searchword=%s"),
155 INIT_OPT_SMART_PREFIX("cliki", "http://www.cliki.net/site/search?words=%s"),
156 INIT_OPT_SMART_PREFIX("d", "http://www.dict.org/bin/Dict?Query=%s&Form=Dict1&Strategy=*&Database=*&submit=Submit+query"),
157 INIT_OPT_SMART_PREFIX("ddg", "http://duckduckgo.com/?q=%s&t=elinks"),
158 INIT_OPT_SMART_PREFIX("dmoz", "http://search.dmoz.org/cgi-bin/search?search=%s"),
159 INIT_OPT_SMART_PREFIX("foldoc", "http://foldoc.org/?%s"),
160 INIT_OPT_SMART_PREFIX("g", "http://www.google.com/search?q=%s&btnG=Google+Search"),
161 /* Whose idea was it to use 'gg' for websearches? -- Miciah */
162 /* INIT_OPT_SMART_PREFIX("gg", "http://groups.google.com/groups?q=%s"), */
163 INIT_OPT_SMART_PREFIX("gg", "http://www.google.com/search?q=%s&btnG=Google+Search"),
164 INIT_OPT_SMART_PREFIX("gi", "http://images.google.com/images?q=%s"),
165 INIT_OPT_SMART_PREFIX("gn", "http://news.google.com/news?q=%s"),
166 INIT_OPT_SMART_PREFIX("go", "http://www.google.com/search?q=%s&btnG=Google+Search"),
167 INIT_OPT_SMART_PREFIX("gr", "http://groups.google.com/groups?q=%s"),
168 INIT_OPT_SMART_PREFIX("google", "http://www.google.com/search?q=%s"),
169 INIT_OPT_SMART_PREFIX("gwho", "http://www.googlism.com/search/?ism=%s&type=1"),
170 INIT_OPT_SMART_PREFIX("gwhat", "http://www.googlism.com/search/?ism=%s&type=2"),
171 INIT_OPT_SMART_PREFIX("gwhere", "http://www.googlism.com/search/?ism=%s&type=3"),
172 INIT_OPT_SMART_PREFIX("gwhen", "http://www.googlism.com/search/?ism=%s&type=4"),
173 INIT_OPT_SMART_PREFIX("fm", "http://freshmeat.net/search/?q=%s"),
174 INIT_OPT_SMART_PREFIX("savannah", "http://savannah.nongnu.org/search/?words=%s&type_of_search=soft&exact=1"),
175 INIT_OPT_SMART_PREFIX("sf", "http://sourceforge.net/search/?q=%s"),
176 INIT_OPT_SMART_PREFIX("sfp", "http://sourceforge.net/projects/%s"),
177 INIT_OPT_SMART_PREFIX("dbug", "http://bugs.debian.org/%s"),
178 INIT_OPT_SMART_PREFIX("dpkg", "http://packages.debian.org/%s"),
179 INIT_OPT_SMART_PREFIX("emacs", "http://www.emacswiki.org/cgi-bin/wiki.pl?search=%s"),
180 INIT_OPT_SMART_PREFIX("lxr", "http://lxr.linux.no/ident?i=%s"),
181 INIT_OPT_SMART_PREFIX("onelook", "http://onelook.com/?w=%s&ls=a"),
182 INIT_OPT_SMART_PREFIX("e2", "http://www.everything2.org/?node=%s"),
183 INIT_OPT_SMART_PREFIX("encz", "http://www.slovnik.cz/bin/ecd?ecd_il=1&ecd_vcb=%s&ecd_trn=translate&ecd_trn_dir=0&ecd_lines=15&ecd_hptxt=0"),
184 INIT_OPT_SMART_PREFIX("czen", "http://www.slovnik.cz/bin/ecd?ecd_il=1&ecd_vcb=%s&ecd_trn=translate&ecd_trn_dir=1&ecd_lines=15&ecd_hptxt=0"),
185 INIT_OPT_SMART_PREFIX("dict", "http://dictionary.reference.com/search?q=%s"),
186 INIT_OPT_SMART_PREFIX("thes", "http://thesaurus.reference.com/search?q=%s"),
187 INIT_OPT_SMART_PREFIX("a", "http://www.acronymfinder.com/af-query.asp?String=exact&Acronym=%s"),
188 INIT_OPT_SMART_PREFIX("imdb", "http://www.imdb.com/find?q=%s"),
189 INIT_OPT_SMART_PREFIX("mw", "http://www.m-w.com/cgi-bin/dictionary?book=Dictionary&va=%s"),
190 INIT_OPT_SMART_PREFIX("mwt", "http://www.m-w.com/cgi-bin/thesaurus?book=Thesaurus&va=%s"),
191 INIT_OPT_SMART_PREFIX("wiki", "http://en.wikipedia.org/w/wiki.phtml?search=%s"),
192 INIT_OPT_SMART_PREFIX("wn", "http://wordnetweb.princeton.edu/perl/webwn?s=%s"),
193 /* Search the Free Software Directory */
194 INIT_OPT_SMART_PREFIX("fsd", "http://directory.fsf.org/wiki?title=Special%3ASearch&search=%s"),
195 /* rfc by number */
196 INIT_OPT_SMART_PREFIX("rfc", "http://www.rfc-editor.org/rfc/rfc%s.txt"),
197 /* rfc search */
198 INIT_OPT_SMART_PREFIX("rfcs", "http://www.rfc-editor.org/cgi-bin/rfcsearch.pl?searchwords=%s&filefmt=txt&format=http&abstract=abson&keywords=keyon&num=25"),
199 INIT_OPT_SMART_PREFIX("cr", "http://www.rfc-editor.org/cgi-bin/rfcsearch.pl?searchwords=%s&filefmt=txt&format=http&abstract=abson&keywords=keyon&num=25"),
201 NULL_OPTION_INFO,
204 #define get_opt_rewrite(which) uri_rewrite_options[(which)].option
205 #define get_dumb_enable() get_opt_rewrite(URI_REWRITE_ENABLE_DUMB).value.number
206 #define get_smart_enable() get_opt_rewrite(URI_REWRITE_ENABLE_SMART).value.number
208 static inline struct option *
209 get_prefix_tree(enum uri_rewrite_option tree)
211 assert(tree == URI_REWRITE_DUMB_TREE
212 || tree == URI_REWRITE_SMART_TREE);
213 return &get_opt_rewrite(tree);
216 #define MAX_URI_ARGS 10
218 static unsigned char *
219 rewrite_uri(unsigned char *url, struct uri *current_uri, unsigned char *arg)
221 struct string n = NULL_STRING;
222 unsigned char *args[MAX_URI_ARGS];
223 int argslen[MAX_URI_ARGS];
224 int argc = 0;
225 int i;
227 if (!init_string(&n)) return NULL;
229 /* Extract space separated list of arguments */
230 args[argc] = arg;
231 for (i = 0; ; i++) {
232 if (args[argc][i] == ' ') {
233 argslen[argc] = i;
234 argc++;
235 if (argc == MAX_URI_ARGS) break;
236 args[argc] = &args[argc - 1][i];
237 i = 0;
238 for (; *args[argc] == ' '; args[argc]++);
239 } else if (!args[argc][i]) {
240 argslen[argc] = i;
241 argc++;
242 break;
246 while (*url) {
247 int p;
248 int value;
250 for (p = 0; url[p] && url[p] != '%'; p++);
252 add_bytes_to_string(&n, url, p);
253 url += p;
255 if (*url != '%') continue;
257 url++;
258 switch (*url) {
259 case 'c':
260 if (!current_uri) break;
261 add_uri_to_string(&n, current_uri, URI_ORIGINAL);
262 break;
263 case 's':
264 if (arg) encode_uri_string(&n, arg, -1, 1);
265 break;
266 case '%':
267 add_char_to_string(&n, '%');
268 break;
269 case '0':
270 case '1':
271 case '2':
272 case '3':
273 case '4':
274 case '5':
275 case '6':
276 case '7':
277 case '8':
278 case '9':
279 value = *url - '0';
280 if (value >= argc) break;
281 encode_uri_string(&n, args[value],
282 argslen[value], 1);
283 break;
284 default:
285 add_bytes_to_string(&n, url - 1, 2);
286 break;
288 if (*url) url++;
291 return n.source;
294 static unsigned char *
295 get_uri_rewrite_prefix(enum uri_rewrite_type type, unsigned char *url)
297 enum uri_rewrite_option tree = type == URI_REWRITE_DUMB
298 ? URI_REWRITE_DUMB_TREE : URI_REWRITE_SMART_TREE;
299 struct option *prefix_tree = get_prefix_tree(tree);
300 struct option *opt = get_opt_rec_real(prefix_tree, url);
301 unsigned char *exp = opt ? opt->value.string : NULL;
303 return (exp && *exp) ? exp : NULL;
306 static enum evhook_status
307 goto_url_hook(va_list ap, void *data)
309 unsigned char **url = va_arg(ap, unsigned char **);
310 struct session *ses = va_arg(ap, struct session *);
311 unsigned char *uu = NULL;
312 unsigned char *arg = "";
313 unsigned char *argstart = *url + strcspn(*url, " :");
315 if (get_smart_enable() && *argstart) {
316 unsigned char bucket = *argstart;
318 *argstart = '\0';
319 uu = get_uri_rewrite_prefix(URI_REWRITE_SMART, *url);
320 *argstart = bucket;
321 arg = argstart + 1;
324 if (get_dumb_enable() && !uu && !*argstart)
325 uu = get_uri_rewrite_prefix(URI_REWRITE_DUMB, *url);
327 if (!uu
328 && !strchr(*url, ':')
329 && !strchr(*url, '.')
330 && !strchr(*url, '/')) {
331 uu = get_opt_str("protocol.rewrite.default_template", NULL);
332 if (uu && *uu) {
333 arg = *url;
334 } else {
335 uu = NULL;
339 if (uu) {
340 struct uri *uri = ses && have_location(ses)
341 ? cur_loc(ses)->vs.uri : NULL;
343 uu = rewrite_uri(uu, uri, arg);
344 if (uu) {
345 mem_free(*url);
346 *url = uu;
350 return EVENT_HOOK_STATUS_NEXT;
353 struct event_hook_info uri_rewrite_hooks[] = {
354 { "goto-url", -1, goto_url_hook },
356 NULL_EVENT_HOOK_INFO
359 struct module uri_rewrite_module = struct_module(
360 /* name: */ N_("URI rewrite"),
361 /* options: */ uri_rewrite_options,
362 /* hooks: */ uri_rewrite_hooks,
363 /* submodules: */ NULL,
364 /* data: */ NULL,
365 /* init: */ NULL,
366 /* done: */ NULL