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

Revision 60, 4.2 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_Event_Handler
5*/
6require_once 'Phergie/Event/Handler.php';
7
8/**
9* Searches received messages for consecutive sequences of two or more capital
10* letters followed by a question mark, performs an acronym lookup for each
11* sequence, and either returns a limited number of possible meanings for the
12* acronym or performs a random action should no results be returned.
13*
14* The limit configuration setting should be set to the maximum number of
15* potential meanings to return for any single given acronym.
16*/
17class Phergie_Plugin_Acronym extends Phergie_Event_Handler
18{
19    /**
20    * Maximum number of meanings to return for a single acronym
21    *
22    * @var int
23    */
24    protected $limit;
25
26    /**
27    * Possible reactions to return when no result is returned
28    *
29    * @var array
30    */
31    protected $reactions = array(
32        'shrugs',
33        'blinks',
34        'giggles',
35        'sighs',
36        'yawns',
37        'hides behind %randomuser%'
38    );
39
40    /**
41    * Initializes the limit of meanings to return per acronym.
42    *
43    * @return void
44    */
45    public function init()
46    {
47        $limit = $this->getIni('limit');
48        if ($limit < 0 || $limit === null) {
49            $this->limit = 5;
50        } else {
51            $this->limit = (int) $limit;
52        }
53    }
54
55    /**
56    * Returns a random action, meant for cases where an acronym lookup
57    * returns no results.
58    *
59    * @param string $target Channel name or user nick to receive the action
60    * @return void
61    */
62    protected function randomAction($target)
63    {
64        do {
65            $reaction = $this->reactions[rand(0, count($this->reactions) - 1)];
66            $randomUser = strpos($reaction, '%randomuser%');
67        } while ($target[0] != '#' && $randomUser);
68        if ($randomUser) {
69            $nick = $this->getIni('nick');
70            do {
71                $user = Phergie_Plugin_Users::getRandomUser($target);
72            } while ($user == $nick);
73            $reaction = str_replace('%randomuser%', $user, $reaction);
74        }
75        $this->doAction($target, $reaction . '.');
76    }
77
78    /**
79    * Processes acronym lookups and returns results when available, or
80    * returns a random action when a lookup returns no results.
81    *
82    * @return void
83    */
84    public function onPrivmsg()
85    {
86        $target = $this->event->getSource();
87        $message = $this->event->getArgument(1);
88
89        if (!preg_match('/((?:[A-Z]\.?){2,})\?/', $message, $acronym)) {
90            return;
91        }
92
93        $acronym = str_replace('.', '', $acronym[1]);
94
95        if (in_array($acronym, array('WHO', 'WHAT', 'WHERE', 'WHEN', 'WHY', 'HOW'))) {
96            $this->doAction($target, 'shrugs.');
97            return;
98        }
99
100        $opts = array('http' =>
101            array(
102                'method' => 'GET',
103                'header' => 'Content-type: application/x-www-form-urlencoded',
104                'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12',
105                'content' => http_build_query(array('terms' => $acronym, 'andor' => 'or', 'acronym' => 'on'))
106            )
107        );
108        $context = stream_context_create($opts);
109        $url = 'http://www.acronymfinder.com/af-query.asp?Acronym=' . urlencode($acronym) . '&Find=find';
110        $contents = @file_get_contents($url, false, $context);
111
112        if (empty ($contents)
113            || strpos($contents, 'no abbreviation matches') !== false
114            || strpos($contents, 'has exceeded the daily query limit') !== false) {
115            $this->randomAction($target);
116        } else {
117            $matches = array();
118            $offset = 0;
119
120            do {
121                $count = preg_match(
122                    '/<td width="65%"[^>]+>([^<]+)</i',
123                    $contents,
124                    $match,
125                    PREG_OFFSET_CAPTURE,
126                    $offset
127                );
128                if ($count == 1) {
129                    $matches[] = $match[1][0];
130                    $offset = $match[1][1];
131                }
132            } while (($this->limit == 0 || count($matches) < $this->limit) && $count == 1);
133
134            $text = 'Possible matches for ' . $acronym . ': ' . implode(', ', $matches);
135            $this->doPrivmsg($target, $text);
136        }
137    }
138}
Note: See TracBrowser for help on using the browser.