| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | require_once 'Phergie/Plugin/Abstract/Base.php'; |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | class Phergie_Plugin_Acronym extends Phergie_Plugin_Abstract_Base |
|---|
| 21 | { |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | protected $limit; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | protected $filter; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | protected $reactions = array( |
|---|
| 42 | 'shrugs', |
|---|
| 43 | 'blinks', |
|---|
| 44 | 'giggles', |
|---|
| 45 | 'sighs', |
|---|
| 46 | 'yawns', |
|---|
| 47 | 'hides behind %randomuser%' |
|---|
| 48 | ); |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | public function init() |
|---|
| 56 | { |
|---|
| 57 | $limit = $this->getPluginIni('limit'); |
|---|
| 58 | if ($limit < 0 || $limit === null) { |
|---|
| 59 | $this->limit = 5; |
|---|
| 60 | } else { |
|---|
| 61 | $this->limit = (int) $limit; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | $this->filter = array_filter(preg_split('/[ ,]/', $this->getPluginIni('filter')), 'strlen'); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | protected function randomAction($target) |
|---|
| 75 | { |
|---|
| 76 | do { |
|---|
| 77 | $reaction = $this->reactions[rand(0, count($this->reactions) - 1)]; |
|---|
| 78 | $randomUser = strpos($reaction, '%randomuser%'); |
|---|
| 79 | } while ($target[0] != '#' && $randomUser); |
|---|
| 80 | if ($randomUser) { |
|---|
| 81 | $nick = $this->getIni('nick'); |
|---|
| 82 | do { |
|---|
| 83 | $user = Phergie_Plugin_Users::getRandomUser($target); |
|---|
| 84 | } while ($user == $nick); |
|---|
| 85 | $reaction = str_replace('%randomuser%', $user, $reaction); |
|---|
| 86 | } |
|---|
| 87 | $this->doAction($target, $reaction . '.'); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | public function onPrivmsg() |
|---|
| 97 | { |
|---|
| 98 | $target = $this->event->getSource(); |
|---|
| 99 | $message = $this->event->getArgument(1); |
|---|
| 100 | |
|---|
| 101 | if (!preg_match('/((?:[A-Z]\.?){2,})\?/AD', $message, $acronym)) { |
|---|
| 102 | return; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | $acronym = str_replace('.', '', $acronym[1]); |
|---|
| 106 | |
|---|
| 107 | if (in_array($acronym, $this->filter)) { |
|---|
| 108 | return; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | if (in_array($acronym, array('WHO', 'WHAT', 'WHERE', 'WHEN', 'WHY', 'HOW'))) { |
|---|
| 112 | $this->doAction($target, 'shrugs.'); |
|---|
| 113 | return; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | $opts = array('http' => |
|---|
| 117 | array( |
|---|
| 118 | 'timeout' => 5, |
|---|
| 119 | 'method' => 'GET', |
|---|
| 120 | 'header' => 'Content-type: application/x-www-form-urlencoded', |
|---|
| 121 | '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', |
|---|
| 122 | 'content' => http_build_query(array('terms' => $acronym, 'andor' => 'or', 'acronym' => 'on')) |
|---|
| 123 | ) |
|---|
| 124 | ); |
|---|
| 125 | $context = stream_context_create($opts); |
|---|
| 126 | $url = 'http://www.acronymfinder.com/af-query.asp?Acronym=' . urlencode($acronym) . '&Find=find'; |
|---|
| 127 | $contents = @file_get_contents($url, false, $context); |
|---|
| 128 | |
|---|
| 129 | if (empty ($contents) |
|---|
| 130 | || strpos($contents, 'no abbreviation matches') !== false |
|---|
| 131 | || strpos($contents, 'has exceeded the daily query limit') !== false) { |
|---|
| 132 | $this->randomAction($target); |
|---|
| 133 | } else { |
|---|
| 134 | $matches = array(); |
|---|
| 135 | $offset = 0; |
|---|
| 136 | |
|---|
| 137 | do { |
|---|
| 138 | $count = preg_match( |
|---|
| 139 | '/<td width="65%"[^>]+>([^<]+)</i', |
|---|
| 140 | $contents, |
|---|
| 141 | $match, |
|---|
| 142 | PREG_OFFSET_CAPTURE, |
|---|
| 143 | $offset |
|---|
| 144 | ); |
|---|
| 145 | if ($count == 1) { |
|---|
| 146 | $matches[] = $match[1][0]; |
|---|
| 147 | $offset = $match[1][1]; |
|---|
| 148 | } |
|---|
| 149 | } while (($this->limit == 0 || count($matches) < $this->limit) && $count == 1); |
|---|
| 150 | |
|---|
| 151 | $text = 'Possible matches for ' . $acronym . ': ' . implode(', ', $matches); |
|---|
| 152 | $this->doPrivmsg($target, $text); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | } |
|---|