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

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

fix syntax

  • 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         return $class->interpolate($keyword->{$default}, join(' ', $1, $query));
56     }
57 }
58
59 sub interpolate {
60     my($class, $engine, $query) = @_;
61     $engine =~ s/%s/$class->escape_it($query)/eg;
62     return $engine;
63 }
64
65 sub escape_it {
66     my($class, $query) = @_;
67     $query =~ s/ /+/g;
68     return Apache::Util::escape_uri($query);
69 }
70
71 sub query {
72     my($class, $r) = @_;
73     my %args = $r->args;
74     return $args{$MSN_KEY};
75 }
76
77 sub CustomKeyword($$$$) {
78     my($cfg, $parms, $arg1, $arg2) = @_;
79     $cfg->{CustomKeywordsDefault} ||= $arg1;
80     $cfg->{CustomKeywords} ||= {};
81     $cfg->{CustomKeywords}->{$arg1} = $arg2;
82 }
83
84 1;
85 __END__
86
87 =head1 NAME
88
89 Apache::CustomKeywords - Customizable toolbar for MSIE
90
91 =head1 SYNOPSIS
92
93   # 1. As an Apache proxy
94   Listen 8888
95   <VirtualHost *:8888>
96   PerlTransHandler +Apache::CustomKeywords
97   CustomKeyword cpan http://search.cpan.org/search?mode=module&query=%s
98   CustomKeyword perldoc http://perldoc.com/cgi-bin/htsearch?words=%s&restrict=perl5.8.0
99   CustomKeyword google http://www.google.com/search?q=%s
100   </VirtualHost>
101
102   # 2. As a pseudo-MSN
103   <Location /response.asp>
104   SetHandler perl-script
105   PerlHandler +Apache::CustomKeywords
106   CustomKeyword cpan http://search.cpan.org/search?mode=module&query=%s
107   # ...
108   </Location>
109
110 =head1 DESCRIPTION
111
112 Apache::CustomKeywords is a customizable proxy/webapp to change your
113 MSIE's Location box to your favourite toolbar!
114
115 See http://www.mozilla.org/docs/end-user/keywords.html for what Custom
116 Keywords means in Mozilla. This module enables Custom Keywords in MSIE.
117
118 With C<CustomKeyword> settings shown in L</"SYNOPSIS">, you type
119 C<cpan CustomKeywords> or C<google blah blah> in your browser's
120 Location: box, then you will be redirected to the page you wanna go
121 to!
122
123 If your command is not recognized by this module, the first
124 C<CustomKeyword> entry is used as default.
125
126 Here's the way this handler works:
127
128 =over 4
129
130 =item *
131
132 Type "foo bar" in Location: box
133
134 =item *
135
136 MSIE sends request to C<auto.search.msn.com>
137
138 =item *
139
140 Apache::CustomKeywords detects it and redirects browser to your
141 favourite search engine.
142
143 =back
144
145 =head1 CONFIGURATION
146
147 There're two ways to let your browser use this module:
148
149 =over 4
150
151 =item As a proxy server
152
153 configure C<httpd.conf> with a proxy version and set up your browser's
154 proxy setting.
155
156 =item As a pseudo MSN
157
158 configure C<httpd.conf> with psuedo MSN version and set up your Hosts
159 file (C</etc/hosts> in Un*x, C<Windows/Hosts> or
160 C<windows/system/drivers/etc/Hosts> in Win32) or local DNS so that
161 C<auto.search.msn.com> points to the server where this module is
162 installed.
163
164 =back
165
166 =head1 NOTE
167
168 If you put C<:> or C<@> as a query in Location: box, MSIE recognizes
169 it as protocol or authentication password stuff, hence this module
170 might not work.
171
172 =head1 TODO
173
174 =over 4
175
176 =item *
177
178 User-definable conversion of query paramers, including encoding
179 conversions. Currenty only C<%s> interpolates to URI-Escaped string
180 encoded in UTF8.
181
182 =back
183
184 =head1 AUTHOR
185
186 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
187
188 This library is free software; you can redistribute it and/or modify
189 it under the same terms as Perl itself.
190
191 =head1 SEE ALSO
192
193 L<mod_perl>, http://www.mozilla.org/docs/end-user/keywords.html
194
195 =cut
Note: See TracBrowser for help on using the browser.