| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
class Phergie_Plugin_Convert extends Phergie_Plugin_Abstract_Command |
|---|
| 8 |
{ |
|---|
| 9 |
|
|---|
| 10 |
* Returns whether or not the plugin's dependencies are met. |
|---|
| 11 |
* |
|---|
| 12 |
* @param Phergie_Driver_Abstract $client Client instance |
|---|
| 13 |
* @param array $plugins List of short names for plugins that the |
|---|
| 14 |
* bootstrap file intends to instantiate |
|---|
| 15 |
* @see Phergie_Plugin_Abstract_Base::checkDependencies() |
|---|
| 16 |
* @return bool TRUE if dependencies are met, FALSE otherwise |
|---|
| 17 |
*/ |
|---|
| 18 |
public static function checkDependencies(Phergie_Driver_Abstract $client, array $plugins) |
|---|
| 19 |
{ |
|---|
| 20 |
if (!extension_loaded('Dom')) { |
|---|
| 21 |
return 'Dom php extension is required'; |
|---|
| 22 |
} |
|---|
| 23 |
|
|---|
| 24 |
return true; |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
* Performs a unit conversion and returns the result in a message. |
|---|
| 29 |
* |
|---|
| 30 |
* @param string $convert Conversion to perform |
|---|
| 31 |
* @return void |
|---|
| 32 |
*/ |
|---|
| 33 |
public function onDoConvert($convert) |
|---|
| 34 |
{ |
|---|
| 35 |
$target = $this->event->getNick(); |
|---|
| 36 |
|
|---|
| 37 |
$context = stream_context_create(array( |
|---|
| 38 |
'http' => array( |
|---|
| 39 |
'timeout' => 5, |
|---|
| 40 |
'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9b3) Gecko/2008020514 Firefox/3.0b3' |
|---|
| 41 |
) |
|---|
| 42 |
)); |
|---|
| 43 |
|
|---|
| 44 |
$url = 'http://www.google.com/search?q=' . urlencode($convert) . '&btnG=Google+Search&aq=f'; |
|---|
| 45 |
$contents = @file_get_contents($url, null, $context); |
|---|
| 46 |
if (empty($contents)) { |
|---|
| 47 |
$this->debug('Empty response'); |
|---|
| 48 |
return; |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
$doc = new DomDocument(); |
|---|
| 52 |
@$doc->loadHTML($contents); |
|---|
| 53 |
$xpath = new DomXPath($doc); |
|---|
| 54 |
$result = $xpath->query('//h2/font/b'); |
|---|
| 55 |
if ($result->length) { |
|---|
| 56 |
$children = $result->item(0)->childNodes; |
|---|
| 57 |
$text = str_replace(array(chr(195), chr(151)), array('x', ''), $children->item(0)->nodeValue); |
|---|
| 58 |
if ($children->length >= 3) { |
|---|
| 59 |
$text .= '^' . $children->item(1)->nodeValue . $children->item(2)->nodeValue; |
|---|
| 60 |
} |
|---|
| 61 |
$this->doPrivmsg($this->event->getSource(), $target . ': ' . $text); |
|---|
| 62 |
} else { |
|---|
| 63 |
$this->doNotice($target, 'Computation error, nothing was returned'); |
|---|
| 64 |
} |
|---|
| 65 |
} |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|