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

Language PHP Lines 82
MD5 Hash 60207768386d64f8e5de6999ff1d621b Estimated Cost $1,458 (why?)
Repository git://github.com/cakephp/cakebot.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/* SVN FILE: $Id$ */
class google_search extends module {

	public $title = "Google Search";
	public $author = "Mad_Clog";
	public $version = "0.1";

	public $max_results = 2;
	public $response_type = 0; // 0 = channel; 1 = query/pm; 2 = notice

	public function init()
	{
		if (!defined('GOOGLE_SEARCH_PATTERN'))
			define('GOOGLE_SEARCH_PATTERN', '#\<h2 class\=r\>\<a href\="([^"]*)" class\=l\>(([^<]|<[^a][^ ])*)\</a\>\</h2\>#i');

		// we can't have more then 10 results
		if ($this->max_results > 10)
			$this->max_results = 10;
	}

	public function destroy()
	{
	}

	public function priv_google($line, $args)
	{
    if ($args['nargs'] < 1)
    {
    	$this->sendMsg($line, $args, 'You need to supply a search string');
    	return;
    }

    $query = 'q='.urlencode($args['query']);
    $getQuery = socket::generateGetQuery($query, 'www.google.com', '/search');
    $this->ircClass->addQuery('www.google.com', 80, $getQuery, $line, $this, 'sendResults');
	}

	public function sendResults($line, $args, $result, $response)
	{
		if ($result == QUERY_SUCCESS) {
			$count = preg_match_all(GOOGLE_SEARCH_PATTERN, $response, $matches, PREG_SET_ORDER);
			if ($count == 0) {
				$this->sendMsg($line, $args, 'Your search - '.BOLD.$args['query'].BOLD.' - did not match any documents.');
				return;
			}

			$numResults = ($count < $this->max_results) ? $count : $this->max_results;

			for ($i = 0;$i < $numResults;$i++)
			{
				$this->sendMsg($line, $args, strip_tags(html_entity_decode($matches[$i][2])).' - '.$matches[$i][1]);
			}
		} else {
			$this->sendMsg($line, $args, 'Google says NO! (server didn\'t respond)');
		}
	}

	public function sendMsg($line, $args, $message)
	{
		switch($this->response_type)
		{
			case 0:
				$this->ircClass->privMsg($line['to'], $message);
			break;

			case 1:
				$this->ircClass->privMsg($line['fromNick'], $message);
			break;

			case 2;
				$this->ircClass->notice($line['fromNick'], $message);
			break;

			default:
				$this->ircClass->privMsg($line['to'], $message);
		}
	}

}

?>
Back to Top