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

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

0.02

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