root/trunk/Phergie/Plugin/UrbanDictionary.php @ 59

Revision 59, 2.2 KB (checked in by tobias382, 5 years ago)

Unstable commit - driver seems to work, most plugins still need testing
* Modified the streams driver to correctly parse the first argument for

PRIVMSG events into an array

* Added a new debug flag setting to the bundled configuration file
* Moved the debug method from the streams driver to the base driver and

modified it to use the new debug flag setting

* Renamed CtcpAction? methods to Action in order to make logic for

construction and parsing of related requests more compatible with the
same for other commands

* Moved Event_Handler::getEventSource to Event_Request::getSource
* Added Event_Handler::tinyUrl convenience method for converting URLs to

their TinyURL equivalents

* Improved docblock coverage in various classes
* Changed all plugin calls to getConnection()->getNick() to getIni('nick')
* Fixed a memory leak in the Command plugin class
* Added alternate nick support to the Nickserv plugin
* Too many more to name!

Line 
1<?php
2
3/**
4* @see Phergie_Plugin_Abstract_Command
5*/
6require_once 'Phergie/Plugin/Abstract/Command.php';
7
8/**
9* Performs a lookup on request for a given term on the Urban Dictionary web
10* site and responds with a message containing the first result.
11*/
12class Phergie_Plugin_UrbanDictionary extends Phergie_Plugin_Abstract_Command
13{
14    /**
15    * Handles Urban Dictionary definition requests.
16    *
17    * @param string $term Term to search for
18    * @return void
19    */
20    public function onDoUd($term)
21    {
22        $url = 'http://www.urbandictionary.com/define.php?term=' . urlencode($term);
23        $contents = file_get_contents($url);
24
25        if (strpos($contents, '<i>' . $term . '</i> isn\'t defined') !== false) {
26            $url = $this->tinyUrl('http://urbandictionary.com/insert.php?word=' . $term);
27            $this->doPrivmsg(
28                $this->getSource(),
29                $term . ' is not defined yet [ ' . $url . ' ]'
30            );
31        } else {
32            $start = strpos($contents, '<div class="def_p">');
33            $end = strpos($contents, '<div', $start + 1);
34            if ($end === false) {
35                $end = strpos($contents, '</div>', $start);
36            }
37            $contents = substr($contents, $start, $end - $start);
38            $contents = html_entity_decode(strip_tags($contents));
39            $contents = $term . ': ' . trim(preg_replace('/[\r\n\t ]+/', ' ', $contents));
40
41            $url = ' [ ' . $this->tinyUrl($url) . ' ]';
42
43            /**
44            * Not sure why, but this seems to be the magic number for
45            * ensuring that the text isn't truncated. The hostmask isn't
46            * included in what's sent to the server. The maximum message
47            * length should be 510 characters according to the IRC RFC.
48            */
49            $max = 445 - strlen($channel) - strlen($url);
50            if (strlen($contents) > $max) {
51                $contents = substr($contents, 0, $max);
52                $end = strrpos($contents, ' ');
53                if ($end === false) {
54                    $end = $max;
55                }
56                $contents = substr($contents, 0, $end) . '...' . $url;
57            }
58
59            $this->doPrivmsg($this->getSource(), $contents);
60        }
61    }
62}
Note: See TracBrowser for help on using the browser.