| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
class Phergie_Plugin_Define extends Phergie_Plugin_Abstract_Base |
|---|
| 7 |
{ |
|---|
| 8 |
|
|---|
| 9 |
* Returns whether or not the plugin's dependencies are met. |
|---|
| 10 |
* |
|---|
| 11 |
* @param Phergie_Driver_Abstract $client Client instance |
|---|
| 12 |
* @param array $plugins List of short names for plugins that the |
|---|
| 13 |
* bootstrap file intends to instantiate |
|---|
| 14 |
* @see Phergie_Plugin_Abstract_Base::checkDependencies() |
|---|
| 15 |
* @return bool TRUE if dependencies are met, FALSE otherwise |
|---|
| 16 |
*/ |
|---|
| 17 |
public static function checkDependencies(Phergie_Driver_Abstract $client, array $plugins) |
|---|
| 18 |
{ |
|---|
| 19 |
if (!self::staticPluginLoaded('TinyUrl', $client, $plugins)) { |
|---|
| 20 |
return 'TinyUrl plugin must be enabled'; |
|---|
| 21 |
} |
|---|
| 22 |
if (!extension_loaded('tidy')) { |
|---|
| 23 |
return 'tidy extension must be enabled'; |
|---|
| 24 |
} |
|---|
| 25 |
return true; |
|---|
| 26 |
} |
|---|
| 27 |
|
|---|
| 28 |
public function onPrivmsg() |
|---|
| 29 |
{ |
|---|
| 30 |
$text = $this->event->getText(); |
|---|
| 31 |
if (strpos($text, $this->getIni('command_prefix') . 'define ') !== 0) { |
|---|
| 32 |
return; |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
$split = explode(' ', $text); |
|---|
| 36 |
array_shift($split); |
|---|
| 37 |
if (ctype_digit(end($split))) { |
|---|
| 38 |
$offset = array_pop($split); |
|---|
| 39 |
} else { |
|---|
| 40 |
$offset = 1; |
|---|
| 41 |
} |
|---|
| 42 |
$term = implode(' ', $split); |
|---|
| 43 |
|
|---|
| 44 |
$url = "http://www.google.com/search?q=define%3A" . urlencode($term) . "&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a"; |
|---|
| 45 |
$result = file_get_contents($url); |
|---|
| 46 |
|
|---|
| 47 |
$config = array( |
|---|
| 48 |
'indent' => true, |
|---|
| 49 |
'output-html' => true, |
|---|
| 50 |
'wrap' => 200, |
|---|
| 51 |
); |
|---|
| 52 |
$tidy = new tidy(); |
|---|
| 53 |
$tidy->parseString($result, $config, 'utf8'); |
|---|
| 54 |
$string = tidy_get_output($tidy); |
|---|
| 55 |
|
|---|
| 56 |
$dom = new DOMDocument(); |
|---|
| 57 |
@$dom->loadHTML($string); |
|---|
| 58 |
$xpath = new DOMXPath($dom); |
|---|
| 59 |
$lines = $xpath->query('//li[' . $offset . ']'); |
|---|
| 60 |
|
|---|
| 61 |
if (!$lines->length) { |
|---|
| 62 |
$this->doPrivmsg($this->event->getNick(), 'Definition ' . $offset . ' not found for term ' . $term); |
|---|
| 63 |
return; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
$def = $lines->item(0)->nodeValue; |
|---|
| 67 |
$max = $this->getPluginIni('max_length'); |
|---|
| 68 |
if (!$max) { |
|---|
| 69 |
$max = 30; |
|---|
| 70 |
} |
|---|
| 71 |
if (strlen($def) > $max) { |
|---|
| 72 |
$def = substr($def, 0, $max) . '...'; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
$tinyurl = Phergie_Plugin_TinyUrl::get($url); |
|---|
| 76 |
$msg = $this->event->getNick() . ': ' . $term . ' - ' . $def . ' [ ' . $tinyurl . ' ]'; |
|---|
| 77 |
$this->doPrivmsg($this->event->getSource(), $msg); |
|---|
| 78 |
} |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|