Assembla home | Assembla project page
 

root/trunk/Phergie/Plugin/UrbanDictionary.php

Revision 293, 2.9 kB (checked in by tobias382, 3 weeks ago)

Fixes #68 UrbanDictionary? plugin was outputting stray scripting data when no term definition was found

Line 
1 <?php
2
3 /**
4  * Performs a lookup on request for a given term on the Urban Dictionary web
5  * site and responds with a message containing the first result.
6  */
7 class Phergie_Plugin_UrbanDictionary 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 (!self::staticPluginLoaded('TinyUrl', $client, $plugins)) {
21             return 'TinyUrl plugin must be enabled';
22         }
23
24         return true;
25     }
26
27     /**
28      * Handles Urban Dictionary definition requests.
29      *
30      * @param string $term Term to search for
31      * @return void
32      */
33     public function onDoUd($term)
34     {
35         $target = $this->event->getNick();
36         $source = $this->event->getSource();
37
38         $url = 'http://www.urbandictionary.com/define.php?term=' . urlencode($term);
39         $contents = file_get_contents($url);
40
41         if (strpos($contents, $term . ' isn\'t defined') !== false) {
42             $url = Phergie_Plugin_TinyUrl::get('http://urbandictionary.com/insert.php?word=' . $term);
43             $this->doNotice($target, $term . ' is not defined yet [ ' . $url . ' ]');
44         } else {
45             $start = strpos($contents, '<div class=\'definition\'>');
46             $end = strpos($contents, '<div', $start + 1);
47             if ($end === false) {
48                 $end = strpos($contents, '</div>', $start);
49             }
50             $contents = substr($contents, $start, $end - $start);
51             $contents = html_entity_decode(strip_tags($contents));
52             $contents = $term . ': ' . trim(preg_replace('/[\r\n\t ]+/', ' ', $contents));
53
54             $url = '[ ' . Phergie_Plugin_TinyUrl::get($url) . ' ] ';
55
56             /**
57              * Not sure why, but this seems to be the magic number for
58              * ensuring that the text isn't truncated. The hostmask isn't
59              * included in what's sent to the server. The maximum message
60              * length should be 510 characters according to the IRC RFC.
61              */
62             $max = 445 - strlen($source) - strlen($url);
63             if (strlen($contents) > $max) {
64                 $contents = substr($contents, 0, $max);
65                 $end = strrpos($contents, ' ');
66                 if ($end === false) {
67                     $end = $max;
68                 }
69                 $contents = substr($contents, 0, $end) . '...';
70             }
71             $contents = $url . $contents;
72
73             $this->doPrivmsg($source, $contents);
74         }
75     }
76 }
77
Note: See TracBrowser for help on using the browser.