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

Revision 60, 2.3 KB (checked in by tobias382, 5 years ago)

Fixes #1, #8, #14, #10, #13
* Fixed some parameter parsing issues in the streams driver and Command plugin
* Fixed an issue in the streams driver where quitting would not cause the

connection to be terminated on the client side

* Added automated directory creation to the event handler class
* Added isInChannel() and TYPE_KICK to request event class
* Modified the bootstrap file to use a full path when referencing the Plugin

directory and to include plugin loading debugging messages

* Fixed a bug in the bootstrap file where it would not produce an error when

required configuration settings existed, but were empty

* Cleared default values and unused settings out of the config file
* Made modifications to the fromAdmin method in the AdminCommand? plugin to

optimize performance

* Fixed a bug in the parsing logic in the Acronym plugin and added a check for

instances where the per-IP bandwidth limit has been executed

* Added a check for cases where a server has no MOTD to the Autojoin plugin
* Modified the Drink plugin to filter coffee drinks when scraping data for the

coke table and to remove the profanity filter array from memory once the
init method is done using it

* Added an avogadro alias for the mole fixed karma and added support for

configurable fixed karma for the bot

* Modified the Logging plugin to extend the Command plugin and to use PDO in

place of the standalone SQLite driver

* Modified the Php plugin to use full URLs to function pages rather than the

shorthand version, which does not always resolve to the expected URL

* Renamed the Say plugin to Puppet and added support emulation of CTCP ACTION

commands

* Modified the Url plugin to use the MIME type of the resource in place of

the title in cases where it is not HTML-based

* Fixed an issue with the parsing logic in the onMode handler of the Users

plugin

* Uncommented the onPrivmsg handler and modified it to use the new debug

configuration setting

* Added alternate nick support to the Nickserv plugin
* Removed the CONTRIBUTE file, as its fairly out of date

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            $target = $this->event->getSource();
50            $max = 445 - strlen($target) - strlen($url);
51            if (strlen($contents) > $max) {
52                $contents = substr($contents, 0, $max);
53                $end = strrpos($contents, ' ');
54                if ($end === false) {
55                    $end = $max;
56                }
57                $contents = substr($contents, 0, $end) . '...';
58            }
59            $contents .=  $url;
60
61            $this->doPrivmsg($target, $contents);
62        }
63    }
64}
Note: See TracBrowser for help on using the browser.