root/Apache-CustomKeywords/trunk/lib/Apache/CustomKeywords.pm

Revision 718 (checked in by miyagawa, 18 years ago)

default

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 package Apache::CustomKeywords;
2
3 use strict;
4 use vars qw($VERSION);
5 $VERSION = 0.03;
6
7 use Apache::Constants qw(:response);
8 use Apache::ModuleConfig;
9 use Apache::Util;
10 use vars qw($MSN_URL $MSN_KEY @ISA);
11 @ISA = qw(DynaLoader);
12
13 $MSN_URL = 'http://auto.search.msn.com/response.asp';
14 $MSN_KEY = 'MT';
15
16 __PACKAGE__->bootstrap($VERSION) if $ENV{MOD_PERL};
17
18 sub handler($$) {
19     my($class, $r) = @_;
20     if ($r->proxyreq) {
21         my $uri = $r->uri;
22         if ($uri =~ /^$MSN_URL/) {
23             my $location = $class->convert_query($r);
24             if (defined $location) {
25                 $r->header_out(Location => $location);
26                 return REDIRECT;
27             }
28         }
29         $r->filename("proxy:$uri");
30         $r->handler('proxy-server');
31         return OK;
32     } else {
33         my $location = $class->convert_query($r);
34         if (defined $location) {
35             $r->header_out(Location => $location);
36             return REDIRECT;
37         }
38         $r->send_http_header('text/html');
39         $r->print(__PACKAGE__ . ": Can't parse tokens.");
40         return OK;
41     }
42 }
43
44 sub convert_query {
45     my($class, $r) = @_;
46     my $cfg = Apache::ModuleConfig->get($r) || {};
47     my $keyword = $cfg->{CustomKeywords} or return;
48     my $query = $class->query($r);
49     $query =~ s/^(\S+)\s*// or return;
50     if (my $engine = $keyword->{$1}) {
51         return $class->interpolate($engine, $query);
52         return $engine;
53     }
54     elsif (my $default = $cfg->{CustomKeywordsDefault}) {
55         my $engine = $keyword->{$default};
56         return $class->interpolate($engine, join(' ', $1, $query));
57     }
58 }
59
60 sub interpolate {
61     my($class, $engine, $query) = @_;
62     $engine =~ s/%s/$class->escape_it($query)/eg;
63     return $engine;
64 }
65
66 sub escape_it {
67     my($class, $query) = @_;
68     $query =~ s/ /+/g;
69     return Apache::Util::escape_uri($query);
70 }
71
72 sub query {
73     my($class, $r) = @_;
74     my %args = $r->args;
75     return $args{$MSN_KEY};
76 }
77
78 sub CustomKeyword($$$$) {
79     my($cfg, $parms, $arg1, $arg2) = @_;
80     $cfg->{CustomKeywordsDefault} ||= $arg1;
81     $cfg->{CustomKeywords} ||= {};
82     $cfg->{CustomKeywords}->{$arg1} = $arg2;
83 }
84
85 1;
86 __END__
87
88 =head1 NAME
89
90 Apache::CustomKeywords - Customizable toolbar for MSIE
91
92 =head1 SYNOPSIS
93
94   # 1. As an Apache proxy
95   Listen 8888
96   <VirtualHost *:8888>
97   PerlTransHandler +Apache::CustomKeywords
98   CustomKeyword cpan http://search.cpan.org/search?mode=module&query=%s
99   CustomKeyword perldoc http://perldoc.com/cgi-bin/htsearch?words=%s&restrict=perl5.8.0
100   CustomKeyword google http://www.google.com/search?q=%s
101   </VirtualHost>
102
103   # 2. As a pseudo-MSN
104   <Location /response.asp>
105   SetHandler perl-script
106   PerlHandler +Apache::CustomKeywords
107   CustomKeyword cpan http://search.cpan.org/search?mode=module&query=%s
108   # ...
109   </Location>
110
111 =head1 DESCRIPTION
112
113 Apache::CustomKeywords is a customizable proxy/webapp to change your
114 MSIE's Location box to your favourite toolbar!
115
116 See http://www.mozilla.org/docs/end-user/keywords.html for what Custom
117 Keywords means in Mozilla. This module enables Custom Keywords in MSIE.
118
119 With C<CustomKeyword> settings shown in L</"SYNOPSIS">, you type
120 C<cpan CustomKeywords> or C<google blah blah> in your browser's
121 Location: box, then you will be redirected to the page you wanna go
122 to!
123
124 If your command is not recognized by this module, the first
125 C<CustomKeyword> entry is used as default.
126
127 Here's the way this handler works:
128
129 =over 4
130
131 =item *
132
133 Type "foo bar" in Location: box
134
135 =item *
136
137 MSIE sends request to C<auto.search.msn.com>
138
139 =item *
140
141 Apache::CustomKeywords detects it and redirects browser to your
142 favourite search engine.
143
144 =back
145
146 =head1 CONFIGURATION
147
148 There're two ways to let your browser use this module:
149
150 =over 4
151
152 =item As a proxy server
153
154 configure C<httpd.conf> with a proxy version and set up your browser's
155 proxy setting.
156
157 =item As a pseudo MSN
158
159 configure C<httpd.conf> with psuedo MSN version and set up your Hosts
160 file (C</etc/hosts> in Un*x, C<Windows/Hosts> or
161 C<windows/system/drivers/etc/Hosts> in Win32) or local DNS so that
162 C<auto.search.msn.com> points to the server where this module is
163 installed.
164
165 =back
166
167 =head1 NOTE
168
169 If you put C<:> or C<@> as a query in Location: box, MSIE recognizes
170 it as protocol or authentication password stuff, hence this module
171 might not work.
172
173 =head1 TODO
174
175 =over 4
176
177 =item *
178
179 User-definable conversion of query paramers, including encoding
180 conversions. Currenty only C<%s> interpolates to URI-Escaped string
181 encoded in UTF8.
182
183 =back
184
185 =head1 AUTHOR
186
187 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
188
189 This library is free software; you can redistribute it and/or modify
190 it under the same terms as Perl itself.
191
192 =head1 SEE ALSO
193
194 L<mod_perl>, http://www.mozilla.org/docs/end-user/keywords.html
195
196 =cut
Note: See TracBrowser for help on using the browser.