1 /* URI rewriting module */
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
{
26 /* TODO: An event hook for follow-url might also feel at home here. --jonas */
28 enum uri_rewrite_option
{
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
,
43 static union option_info uri_rewrite_options
[] = {
44 INIT_OPT_TREE("protocol", N_("URI rewriting"),
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 "
54 INIT_OPT_BOOL("protocol.rewrite", N_("Enable dumb prefixes"),
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"),
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 "
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
,
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 "
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
,
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"
105 "%c in the template means the current URL,\n"
106 "%s in the template means the whole string from the goto\n"
108 "%0,%1,...,%9 mean the 1st,2nd,...,10th space-delimited part\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")),
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")),
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"),
196 INIT_OPT_SMART_PREFIX("rfc", "http://www.rfc-editor.org/rfc/rfc%s.txt"),
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"),
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
];
227 if (!init_string(&n
)) return NULL
;
229 /* Extract space separated list of arguments */
232 if (args
[argc
][i
] == ' ') {
235 if (argc
== MAX_URI_ARGS
) break;
236 args
[argc
] = &args
[argc
- 1][i
];
238 for (; *args
[argc
] == ' '; args
[argc
]++);
239 } else if (!args
[argc
][i
]) {
250 for (p
= 0; url
[p
] && url
[p
] != '%'; p
++);
252 add_bytes_to_string(&n
, url
, p
);
255 if (*url
!= '%') continue;
260 if (!current_uri
) break;
261 add_uri_to_string(&n
, current_uri
, URI_ORIGINAL
);
264 if (arg
) encode_uri_string(&n
, arg
, -1, 1);
267 add_char_to_string(&n
, '%');
280 if (value
>= argc
) break;
281 encode_uri_string(&n
, args
[value
],
285 add_bytes_to_string(&n
, url
- 1, 2);
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
;
319 uu
= get_uri_rewrite_prefix(URI_REWRITE_SMART
, *url
);
324 if (get_dumb_enable() && !uu
&& !*argstart
)
325 uu
= get_uri_rewrite_prefix(URI_REWRITE_DUMB
, *url
);
328 && !strchr(*url
, ':')
329 && !strchr(*url
, '.')
330 && !strchr(*url
, '/')) {
331 uu
= get_opt_str("protocol.rewrite.default_template", NULL
);
340 struct uri
*uri
= ses
&& have_location(ses
)
341 ? cur_loc(ses
)->vs
.uri
: NULL
;
343 uu
= rewrite_uri(uu
, uri
, arg
);
350 return EVENT_HOOK_STATUS_NEXT
;
353 struct event_hook_info uri_rewrite_hooks
[] = {
354 { "goto-url", -1, goto_url_hook
},
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
,