PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendors/php-irc/modules/google_search/google_search.php

http://github.com/cakephp/cakebot
PHP | 82 lines | 64 code | 16 blank | 2 comment | 9 complexity | 60207768386d64f8e5de6999ff1d621b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. class google_search extends module {
  4. public $title = "Google Search";
  5. public $author = "Mad_Clog";
  6. public $version = "0.1";
  7. public $max_results = 2;
  8. public $response_type = 0; // 0 = channel; 1 = query/pm; 2 = notice
  9. public function init()
  10. {
  11. if (!defined('GOOGLE_SEARCH_PATTERN'))
  12. define('GOOGLE_SEARCH_PATTERN', '#\<h2 class\=r\>\<a href\="([^"]*)" class\=l\>(([^<]|<[^a][^ ])*)\</a\>\</h2\>#i');
  13. // we can't have more then 10 results
  14. if ($this->max_results > 10)
  15. $this->max_results = 10;
  16. }
  17. public function destroy()
  18. {
  19. }
  20. public function priv_google($line, $args)
  21. {
  22. if ($args['nargs'] < 1)
  23. {
  24. $this->sendMsg($line, $args, 'You need to supply a search string');
  25. return;
  26. }
  27. $query = 'q='.urlencode($args['query']);
  28. $getQuery = socket::generateGetQuery($query, 'www.google.com', '/search');
  29. $this->ircClass->addQuery('www.google.com', 80, $getQuery, $line, $this, 'sendResults');
  30. }
  31. public function sendResults($line, $args, $result, $response)
  32. {
  33. if ($result == QUERY_SUCCESS) {
  34. $count = preg_match_all(GOOGLE_SEARCH_PATTERN, $response, $matches, PREG_SET_ORDER);
  35. if ($count == 0) {
  36. $this->sendMsg($line, $args, 'Your search - '.BOLD.$args['query'].BOLD.' - did not match any documents.');
  37. return;
  38. }
  39. $numResults = ($count < $this->max_results) ? $count : $this->max_results;
  40. for ($i = 0;$i < $numResults;$i++)
  41. {
  42. $this->sendMsg($line, $args, strip_tags(html_entity_decode($matches[$i][2])).' - '.$matches[$i][1]);
  43. }
  44. } else {
  45. $this->sendMsg($line, $args, 'Google says NO! (server didn\'t respond)');
  46. }
  47. }
  48. public function sendMsg($line, $args, $message)
  49. {
  50. switch($this->response_type)
  51. {
  52. case 0:
  53. $this->ircClass->privMsg($line['to'], $message);
  54. break;
  55. case 1:
  56. $this->ircClass->privMsg($line['fromNick'], $message);
  57. break;
  58. case 2;
  59. $this->ircClass->notice($line['fromNick'], $message);
  60. break;
  61. default:
  62. $this->ircClass->privMsg($line['to'], $message);
  63. }
  64. }
  65. }
  66. ?>