google-search.pl 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (C) 2007 Alex Schroeder <alex@emacswiki.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('google-search.pl', 'Use Google For Searches');
  18. our ($q, %Action, $ScriptName, @MyInitVariables);
  19. our ($GoogleSearchDomain, $GoogleSearchExclusive);
  20. $GoogleSearchDomain = undef;
  21. $GoogleSearchExclusive = 1;
  22. $Action{search} = \&DoGoogleSearch;
  23. push(@MyInitVariables, \&GoogleSearchInit);
  24. sub GoogleSearchInit {
  25. # If $ScriptName does not contain a hostname, this extension will
  26. # have no effect. Domain regexp based on RFC 2396 section 3.2.2.
  27. if (!$GoogleSearchDomain) {
  28. my $alpha = '[a-zA-Z]';
  29. my $alphanum = '[a-zA-Z0-9]';
  30. my $alphanumdash = '[-a-zA-Z0-9]';
  31. my $domainlabel = "$alphanum($alphanumdash*$alphanum)?";
  32. my $toplabel = "$alpha($alphanumdash*$alphanum)?";
  33. if ($ScriptName =~ m!^(https?://)?([^/]+\.)?($domainlabel\.$toplabel)\.?(:|/|\z)!) {
  34. $GoogleSearchDomain = $3;
  35. }
  36. }
  37. if ($GoogleSearchDomain
  38. and GetParam('search', undef)
  39. and not GetParam('action', undef)
  40. and not GetParam('old', 0)) {
  41. SetParam('action', 'search');
  42. }
  43. *SearchTitleAndBody = \&GoogleSearchDoNothing if $GoogleSearchExclusive;
  44. }
  45. # disable all other searches
  46. sub GoogleSearchDoNothing {
  47. undef;
  48. }
  49. sub DoGoogleSearch {
  50. my $search = GetParam('search', undef);
  51. print $q->redirect({-uri=>"http://www.google.com/search?q=site%3A$GoogleSearchDomain+$search"});
  52. }