google-search.pl 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 2 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, write to the
  15. # Free Software Foundation, Inc.
  16. # 59 Temple Place, Suite 330
  17. # Boston, MA 02111-1307 USA
  18. use strict;
  19. use v5.10;
  20. AddModuleDescription('google-search.pl', 'Use Google For Searches');
  21. our ($q, %Action, $ScriptName, @MyInitVariables);
  22. our ($GoogleSearchDomain, $GoogleSearchExclusive);
  23. $GoogleSearchDomain = undef;
  24. $GoogleSearchExclusive = 1;
  25. $Action{search} = \&DoGoogleSearch;
  26. push(@MyInitVariables, \&GoogleSearchInit);
  27. sub GoogleSearchInit {
  28. # If $ScriptName does not contain a hostname, this extension will
  29. # have no effect. Domain regexp based on RFC 2396 section 3.2.2.
  30. if (!$GoogleSearchDomain) {
  31. my $alpha = '[a-zA-Z]';
  32. my $alphanum = '[a-zA-Z0-9]';
  33. my $alphanumdash = '[-a-zA-Z0-9]';
  34. my $domainlabel = "$alphanum($alphanumdash*$alphanum)?";
  35. my $toplabel = "$alpha($alphanumdash*$alphanum)?";
  36. if ($ScriptName =~ m!^(https?://)?([^/]+\.)?($domainlabel\.$toplabel)\.?(:|/|\z)!) {
  37. $GoogleSearchDomain = $3;
  38. }
  39. }
  40. if ($GoogleSearchDomain
  41. and GetParam('search', undef)
  42. and not GetParam('action', undef)
  43. and not GetParam('old', 0)) {
  44. SetParam('action', 'search');
  45. }
  46. *SearchTitleAndBody = \&GoogleSearchDoNothing if $GoogleSearchExclusive;
  47. }
  48. # disable all other searches
  49. sub GoogleSearchDoNothing {
  50. undef;
  51. }
  52. sub DoGoogleSearch {
  53. my $search = GetParam('search', undef);
  54. print $q->redirect({-uri=>"http://www.google.com/search?q=site%3A$GoogleSearchDomain+$search"});
  55. }