| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | require_once 'Phergie/Plugin/Abstract/Cron.php'; |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | class Phergie_Plugin_FeedTicker extends Phergie_Plugin_Abstract_Cron |
|---|
| 12 | { |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | protected $defaultDelay = 1800; |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | protected $feeds = null; |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | protected $filterTitle = null; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | protected $filterUrl = null; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | protected $filterArticle = null; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | protected $cache = null; |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | protected $queue = array(); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | protected $nextOutput = null; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | protected $format = '%title% [ %link% ]'; |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | public function init() |
|---|
| 92 | { |
|---|
| 93 | |
|---|
| 94 | $globalTitle = trim($this->getPluginIni('filter_title')); |
|---|
| 95 | $globalUrl = trim($this->getPluginIni('filter_url')); |
|---|
| 96 | $globalArticle = trim($this->getPluginIni('filter_article')); |
|---|
| 97 | |
|---|
| 98 | $i = 0; |
|---|
| 99 | $this->feeds = array(); |
|---|
| 100 | do { |
|---|
| 101 | |
|---|
| 102 | $feed = $this->getPluginIni('feed' . $i); |
|---|
| 103 | $chans = $this->getPluginIni('chans' . $i); |
|---|
| 104 | |
|---|
| 105 | $filterTitle = $this->getPluginIni('filter_title' . $i); |
|---|
| 106 | $filterUrl = $this->getPluginIni('filter_url' . $i); |
|---|
| 107 | $filterArticle = $this->getPluginIni('filter_article' . $i); |
|---|
| 108 | |
|---|
| 109 | if (!empty($feed) && !empty($chans)) { |
|---|
| 110 | $this->feeds[] = array($feed, preg_split('#[\s\r\n,]+#', $chans)); |
|---|
| 111 | |
|---|
| 112 | $filterTrim = "| \t\n\r\0\v\0xa0"; |
|---|
| 113 | $this->filterTitle[] = trim(implode('|', array($globalTitle, $filterTitle)), $filterTrim); |
|---|
| 114 | $this->filterUrl[] = trim(implode('|', array($globalUrl, $filterUrl)), $filterTrim); |
|---|
| 115 | $this->filterArticle[] = trim(implode('|', array($globalArticle, $filterArticle)), $filterTrim); |
|---|
| 116 | } |
|---|
| 117 | } while (++$i < 10); |
|---|
| 118 | if($this->getPluginIni('format') != null) { |
|---|
| 119 | $this->format = $this->getPluginIni('format'); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | public function onPrivmsg() |
|---|
| 133 | { |
|---|
| 134 | parent::onPrivmsg(); |
|---|
| 135 | |
|---|
| 136 | $this->checkQueue(); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | protected function checkQueue() |
|---|
| 147 | { |
|---|
| 148 | if (!empty ($this->queue) && time() > $this->nextOutput) { |
|---|
| 149 | list ($title, $url, $chans, $feedTitle) = array_shift($this->queue); |
|---|
| 150 | foreach ($chans as $chan) { |
|---|
| 151 | $this->doPrivmsg( |
|---|
| 152 | $chan, |
|---|
| 153 | str_replace( |
|---|
| 154 | array('%title%', '%link%', '%feed%'), |
|---|
| 155 | array($title, $url, $feedTitle), |
|---|
| 156 | $this->format |
|---|
| 157 | ) |
|---|
| 158 | ); |
|---|
| 159 | } |
|---|
| 160 | $this->nextOutput = time() + 60; |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | protected function run() |
|---|
| 183 | { |
|---|
| 184 | $retrieved = array(); |
|---|
| 185 | |
|---|
| 186 | $context = stream_context_create(array('http' => array( |
|---|
| 187 | 'timeout' => 5, |
|---|
| 188 | 'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9b3) Gecko/2008020514 Firefox/3.0b3' |
|---|
| 189 | ))); |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | foreach ($this->feeds as $id => $feed) { |
|---|
| 193 | list ($url, $chans) = $feed; |
|---|
| 194 | |
|---|
| 195 | $content = @file_get_contents($url, null, $context); |
|---|
| 196 | if (empty ($content)) { |
|---|
| 197 | $this->debug('Feed empty: ' . $url); |
|---|
| 198 | continue; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | if (strpos($content, '<rss version=') !== false || strpos($content, '<rdf') !== false) { |
|---|
| 203 | $xml = new SimpleXMLElement($content); |
|---|
| 204 | $feedTitle = (string) $xml->channel->title; |
|---|
| 205 | foreach ($xml->channel->item as $item) { |
|---|
| 206 | $retrieved[$id][] = array((string) $item->title, (string) $item->link, $chans, $feedTitle); |
|---|
| 207 | } |
|---|
| 208 | } elseif(strpos($content, '/Atom') !== false) { |
|---|
| 209 | $xml = new SimpleXMLElement($content); |
|---|
| 210 | $feedTitle = (string) $xml->title; |
|---|
| 211 | foreach($xml->entry as $item) { |
|---|
| 212 | $retrieved[$id][] = array((string) $item->title, (string) $item->link[0]['href'], $chans, $feedTitle); |
|---|
| 213 | } |
|---|
| 214 | } else { |
|---|
| 215 | $this->debug('Feed format unrecognized: ' . $url); |
|---|
| 216 | continue; |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | if($this->cache === null) { |
|---|
| 222 | $this->cache = $retrieved; |
|---|
| 223 | return; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | foreach ($retrieved as $id => $articles) { |
|---|
| 228 | $articles = array_reverse($articles); |
|---|
| 229 | foreach ($articles as $article) { |
|---|
| 230 | if ((!isset ($this->cache[$id]) |
|---|
| 231 | || array_search($article, $this->cache[$id]) === false) && |
|---|
| 232 | $this->filterCheck($id, $article[0], $article[1], $article[3])) { |
|---|
| 233 | |
|---|
| 234 | $article[0] = $this->decode($article[0], 40); |
|---|
| 235 | |
|---|
| 236 | $article[1] = $this->tinyUrl($article[1]); |
|---|
| 237 | |
|---|
| 238 | $article[3] = $this->decode($article[3], 20); |
|---|
| 239 | $this->queue[] = $article; |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | $this->cache[$id] = $articles; |
|---|
| 244 | } |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | protected function filterCheck($id, $title, $url, $article) { |
|---|
| 249 | |
|---|
| 250 | $filterTitle = $this->filterTitle[$id]; |
|---|
| 251 | $filterUrl = $this->filterUrl[$id]; |
|---|
| 252 | $filterArticle = $this->filterArticle[$id]; |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | if (($filterTitle && preg_match('{'.$filterTitle.'}im', $title, $match)) || |
|---|
| 256 | ($filterUrl && preg_match('{'.$filterUrl.'}im', $url, $match)) || |
|---|
| 257 | ($filterArticle && preg_match('{'.$filterArticle.'}im', $article, $match))) { |
|---|
| 258 | return false; |
|---|
| 259 | } |
|---|
| 260 | return true; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | protected function decode($str, $trim = null) { |
|---|
| 273 | $out = $this->decodeTranslit($str); |
|---|
| 274 | if($trim > 0) { |
|---|
| 275 | $out = substr($out, 0, $trim) . (strlen($out) > $trim ? '...' : ''); |
|---|
| 276 | } |
|---|
| 277 | return $out; |
|---|
| 278 | } |
|---|
| 279 | } |
|---|