Changeset 58
- Timestamp:
- 02/12/08 07:13:09 (10 months ago)
- Files:
-
- trunk/Phergie/Event/Handler.php (modified) (1 diff)
- trunk/Phergie/Plugin/Abstract/AdminCommand.php (moved) (moved from trunk/Phergie/Plugin/AdminCommand.php)
- trunk/Phergie/Plugin/Abstract/Command.php (moved) (moved from trunk/Phergie/Plugin/Command.php) (3 diffs)
- trunk/Phergie/Plugin/Abstract/Cron.php (moved) (moved from trunk/Phergie/Plugin/Cron.php) (3 diffs)
- trunk/Phergie/Plugin/Autojoin.php (modified) (1 diff)
- trunk/Phergie/Plugin/DNSLookup.php (modified) (1 diff)
- trunk/Phergie/Plugin/Daddy.php (modified) (1 diff)
- trunk/Phergie/Plugin/Debug.php (modified) (1 diff)
- trunk/Phergie/Plugin/Drink.php (modified) (8 diffs)
- trunk/Phergie/Plugin/JoinPart.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Karma.php (modified) (8 diffs)
- trunk/Phergie/Plugin/Lart.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Logging.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Users.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Phergie/Event/Handler.php
r57 r58 133 133 134 134 /** 135 * Shorthand for the underlying driver's debugging function. 136 * 137 * @param string $message Message to log 138 * @return void 139 */ 140 public function debug($message) 141 { 142 $this->client->debug($this->name . ' -> ' . $message); 143 } 144 145 /** 135 146 * Initializes the plugin. Should it require initialization, just 136 147 * override this method. trunk/Phergie/Plugin/Abstract/Command.php
r57 r58 6 6 require_once 'Phergie/Event/Handler.php'; 7 7 8 abstract class Phergie_Plugin_ Command extends Phergie_Event_Handler8 abstract class Phergie_Plugin_Abstract_Command extends Phergie_Event_Handler 9 9 { 10 10 protected $cache = array(); 11 11 12 public function onPrivmsg( Phergie_Event_Request $event)12 public function onPrivmsg() 13 13 { 14 $this->processCommand($ event->getArgument(1), $event);14 $this->processCommand($this->event->getArgument(1)); 15 15 } 16 16 17 protected function processCommand($message , $event)17 protected function processCommand($message) 18 18 { 19 19 preg_match('/^\S+/', $message, $match); 20 20 $command = 'onDo' . ucfirst(strtolower($match[0])); 21 21 22 $class = get_class($this);22 $class = $this->getName(); 23 23 if (!isset($this->cache[$class])) { 24 24 $this->cache[$class] = array(); … … 28 28 $cache = array('exists' => false); 29 29 if (method_exists($this, $command)) { 30 $cache['exists'] = true; 30 31 $method = new ReflectionMethod($this, $command); 31 $cache['exists'] = true; 32 $cache['num_args'] = $method->getNumberOfParameters(); 33 $cache['needs_event'] = false; 34 foreach ($method->getParameters() as $key => $param) { 35 $paramClass = $param->getClass(); 36 if ($paramClass && $paramClass->getName() == 'Phergie_Event_Request') { 37 $cache['needs_event'] = true; 38 $cache['event_position'] = $key; 39 break; 40 } 41 } 32 $cache['num_args'] = $method->getNumberOfRequiredParameters(); 42 33 } 43 34 $this->cache[$class][$command] = $cache; … … 47 38 48 39 if ($method['exists']) { 49 $split = $method['num_args'] + 1; 50 if ($method['needs_event']) { 51 $split--; 52 } 53 $params = array_slice(preg_split('/\s+/', $message, $split), 1); 40 $params = array_slice(preg_split('/\s+/', $message, $method['num_args'] + 1), 1); 54 41 $paramsCount = count($params); 55 if ($method['num_args'] > $paramsCount) { 56 $params += array_fill(0, $method['num_args'] - $paramsCount, null); 57 } 58 if ($method['needs_event']) { 59 $params[$method['event_position']] = $event; 60 } 61 if (count($params) == $method['num_args']) { 42 if ($method['num_args'] <= $paramsCount) { 62 43 call_user_func_array(array($this, $command), $params); 63 44 } trunk/Phergie/Plugin/Abstract/Cron.php
r57 r58 12 12 * overload it with your own value if needed 13 13 */ 14 protected $defaultT TL= 60;14 protected $defaultTtl = 60; 15 15 16 16 /** … … 36 36 { 37 37 if($this->ttl === null) { 38 if ((($time = $this->getIni('time_to_live')) !== null) || $time = $this->defaultT TL) {38 if ((($time = $this->getIni('time_to_live')) !== null) || $time = $this->defaultTtl) { 39 39 $this->ttl = $time; 40 40 } … … 46 46 } 47 47 48 public function onPing( Phergie_Event_Request $event)48 public function onPing() 49 49 { 50 50 $this->check(); 51 51 } 52 52 53 public function onPrivmsg( Phergie_Event_Request $event)53 public function onPrivmsg() 54 54 { 55 55 $this->check(); trunk/Phergie/Plugin/Autojoin.php
r57 r58 1 1 <?php 2 2 3 require_once 'Phergie/Event/Handler /Autojoin.php';3 require_once 'Phergie/Event/Handler.php'; 4 4 5 5 class Phergie_Plugin_Autojoin extends Phergie_Event_Handler 6 6 { 7 public function onResponse( Phergie_Event_Response $response)7 public function onResponse() 8 8 { 9 9 $channels = $this->getIni('channels'); 10 10 if (!empty($channels) 11 && $ response->getCode() == Phergie_Event_Response::RPL_ENDOFMOTD) {11 && $this->event->getCode() == Phergie_Event_Response::RPL_ENDOFMOTD) { 12 12 $this->doJoin(implode(' ', preg_split('#[, ]+#', $channels))); 13 13 } trunk/Phergie/Plugin/DNSLookup.php
r57 r58 8 8 class Phergie_Plugin_DNSLookup extends Phergie_Event_Handler 9 9 { 10 public function onPrivmsg( Phergie_Event_Request $event)10 public function onPrivmsg() 11 11 { 12 $source = $this->event->getSource(); 13 $text = $this->event->getArgument(1); 14 12 15 // Check if we have [rev]dns IP-address and if the IP is valid 13 if (preg_match('#^(?:rev)?dns ((?:[0-9]{1,3}\.){3}[0-9]{1,3})$#i', $event->getArgument(1), $m) && ip2long($m[1]) !== false) { 14 $this->doPrivmsg($event->getArgument(0), $m[1].' resolved as '.gethostbyaddr(long2ip(ip2long($m[1])))); 15 } 16 if (preg_match('#^(?:rev)?dns ((?:[0-9]{1,3}\.){3}[0-9]{1,3})$#i', $text, $m) && ip2long($m[1]) !== false) { 17 $this->doPrivmsg($source, $m[1] . ' resolved as ' . gethostbyaddr(long2ip(ip2long($m[1])))); 16 18 // Check if we have [rev]dns host 17 elseif (preg_match('#^(?:rev)?dns ((?:[a-z0-9]+\.)+[a-z]{2,6})$#i', $event->getArgument(1), $m)) {19 } elseif (preg_match('#^(?:rev)?dns ((?:[a-z0-9]+\.)+[a-z]{2,6})$#i', $text, $m)) { 18 20 if(($ip = gethostbyname($m[1])) !== $m[1]) { 19 $this->doPrivmsg($ event->getArgument(0), $m[1] .' resolved as '.$ip);21 $this->doPrivmsg($source, $m[1] . ' resolved as '.$ip); 20 22 } else { 21 $this->doPrivmsg($ event->getArgument(0), $m[1] .' can not be resolved');23 $this->doPrivmsg($source, $m[1] . ' can not be resolved'); 22 24 } 23 25 } trunk/Phergie/Plugin/Daddy.php
r57 r58 8 8 class Phergie_Plugin_Daddy extends Phergie_Event_Handler 9 9 { 10 public function onPrivmsg( Phergie_Event_Request $event)10 public function onPrivmsg() 11 11 { 12 12 $bot = $this->getConnection()->getNick(); 13 $ msg = $event->getArgument(1);14 if (preg_match('/' . $bot . '\s*:?\s+?who\'?s your daddy\\??/iAD', $ msg)) {15 $this->doPrivmsg($ event->getArgument(0), 'You\'re my daddy, ' . $event->getNick() . '!');13 $text = $this->event->getArgument(1); 14 if (preg_match('/' . $bot . '\s*:?\s+?who\'?s your daddy\\??/iAD', $text)) { 15 $this->doPrivmsg($this->getSource(), 'You\'re my daddy, ' . $this->event->getNick() . '!'); 16 16 } 17 17 } trunk/Phergie/Plugin/Debug.php
r57 r58 2 2 3 3 /** 4 * @see Phergie_Plugin_A dminCommand4 * @see Phergie_Plugin_Abstract_AdminCommand 5 5 */ 6 require_once 'Phergie/ Event/Handler/AdminCommand.php';6 require_once 'Phergie/Plugin/Abstract/AdminCommand.php'; 7 7 8 class Phergie_Plugin_Debug extends Phergie_ Event_Handler_AdminCommand8 class Phergie_Plugin_Debug extends Phergie_Plugin_Abstract_AdminCommand 9 9 { 10 public function onDoMem( Phergie_Event_Request $event)10 public function onDoMem() 11 11 { 12 12 $text = 'current : '.number_format(memory_get_usage() / 1024).'KB / peak : '.number_format(memory_get_peak_usage() / 1024).'KB'; 13 $this->doPrivmsg($ event->getArgument(0), $text);13 $this->doPrivmsg($this->getSource(), $text); 14 14 } 15 15 } trunk/Phergie/Plugin/Drink.php
r57 r58 2 2 3 3 /** 4 * @see Phergie_Plugin_ Command4 * @see Phergie_Plugin_Abstract_Command 5 5 */ 6 require_once 'Phergie/ Event/Handler/Command.php';7 8 class Phergie_Plugin_Drink extends Phergie_ Event_Handler_Command6 require_once 'Phergie/Plugin/Abstract/Command.php'; 7 8 class Phergie_Plugin_Drink extends Phergie_Plugin_Abstract_Command 9 9 { 10 10 /** … … 64 64 ); 65 65 66 private function createTable($name) 66 private function needTable($name) 67 { 68 $table = $this->db 69 ->query('SELECT COUNT(*) FROM sqlite_master WHERE name = ' . $this->db->quote($name)) 70 ->fetchColumn(); 71 72 if (!$table) { 73 return true; 74 } 75 76 return !$this->db 77 ->query('SELECT COUNT(*) FROM ' . $name) 78 ->fetchColumn(); 79 } 80 81 private function populateTable($table, $names) 67 82 { 68 83 $this->db->exec(' … … 70 85 CREATE UNIQUE INDEX ' . $name . ' ON ' . $name . ' (name); 71 86 '); 72 } 73 74 private function populateTable($table, $names) 75 { 87 76 88 $stmt = $this->db->prepare('INSERT INTO ' . $table . ' (name) VALUES (:name)'); 77 89 $this->db->beginTransaction(); … … 79 91 foreach ($this->filter as $filter) { 80 92 if (preg_match('/(^|[^a-z])' . $filter . '([^a-z]|$)/i', $name)) { 81 echo 'Filtered out ' . $name . ' because it contains ' . $filter . "\n";93 $this->debug('Filtered out ' . $name . ' because it contains ' . $filter); 82 94 continue 2; 83 95 } 84 96 } 85 echo 'Inserted ' . $name . "\n";97 $this->debug('Inserted ' . $name); 86 98 $stmt->execute(array('name' => $name)); 87 99 } … … 115 127 116 128 // Initialize the database connection 117 $db = $dir . '/drink.db'; 118 $populate = !file_exists($db); 119 $this->db = new PDO('sqlite:' . $db); 129 $this->db = new PDO('sqlite:' . $dir . '/drink.db'); 120 130 121 131 // Populate the database if necessary 122 if ($populate) { 123 $this->createTable('beer'); 124 $contents = file_get_contents('http://beerme.com/beerlist.php'); 125 preg_match_all('/brewery\.php\?[0-9]+#[0-9]+\'>([^<]+)/', $contents, $matches); 126 $names = array(); 127 foreach ($matches[1] as $key => $name) { 128 if ($this->hasBadChars($name) 129 || strpos($name, '(discontinued)') !== false) { 130 continue; 131 } 132 $name = trim(array_shift(explode('/', preg_replace('/\([^)]+\)/', '', $name)))); 133 if (!empty($name)) { 134 $name = html_entity_decode($name); 135 $names[] = $name; 136 } 137 } 138 $this->populateTable('beer', $names); 139 unset($names); 140 141 $this->createTable('cocktail'); 132 if ($this->needTable('beer')) { 133 $contents = @file_get_contents('http://beerme.com/beerlist.php'); 134 if ($contents !== false) { 135 preg_match_all('/brewery\.php\?[0-9]+#[0-9]+\'>([^<]+)/', $contents, $matches); 136 $names = array(); 137 foreach ($matches[1] as $key => $name) { 138 if ($this->hasBadChars($name) 139 || strpos($name, '(discontinued)') !== false) { 140 continue; 141 } 142 $name = trim(array_shift(explode('/', preg_replace('/\([^)]+\)/', '', $name)))); 143 if (!empty($name)) { 144 $name = html_entity_decode($name); 145 $names[] = $name; 146 } 147 } 148 $this->populateTable('beer', $names); 149 unset($names); 150 } 151 } 152 153 if ($this->needTable('cocktail')) { 142 154 $limit = 2; 143 155 $names = array(); 144 156 for ($i = 1; $i <= $limit; $i += 150) { 145 $contents = file_get_contents('http://www.webtender.com/db/browse?level=2&dir=drinks&char=%2A&start=' . $i); 157 $contents = @file_get_contents('http://www.webtender.com/db/browse?level=2&dir=drinks&char=%2A&start=' . $i); 158 if ($contents === false) { 159 break; 160 } 146 161 if ($i == 1) { 147 162 preg_match('/>([0-9]+) found\\.</', $contents, $match); … … 157 172 } 158 173 } 159 $this->populateTable('cocktail', $names); 174 if ($contents) { 175 $this->populateTable('cocktail', $names); 176 } 160 177 unset($names); 161 162 $this->createTable('coke'); 163 $contents = file_get_contents('http://www.energyfiend.com/huge-caffeine-database/'); 164 $start = stripos($contents, 'id="caffeinedb"'); 165 $end = stripos($contents, '</table>', $start); 166 $contents = substr($contents, $start, $end - $start); 167 preg_match_all('/<tr[^>]*><td>(<[^>]+>)?([^<]+)/is', $contents, $matches); 168 $names = array(); 169 foreach ($matches[2] as $name) { 170 $name = html_entity_decode(trim(preg_replace('/ \\([^)]+\\)| - .*$/', '', $name))); 171 $names[] = $name; 172 } 173 $this->populateTable('coke', $names); 174 unset($names); 175 176 $this->createTable('tea'); 177 $names = file('http://www.midnight-labs.org/tea.txt'); 178 $names = array_map('trim', $names); 179 $this->populateTable('tea', $names); 180 unset($names); 181 } 182 } 183 184 protected function throwDrink($type, $target, Phergie_Event_Request $event) 178 } 179 180 if ($this->needTable('coke')) { 181 $contents = @file_get_contents('http://www.energyfiend.com/huge-caffeine-database/'); 182 if ($contents) { 183 $start = stripos($contents, 'id="caffeinedb"'); 184 $end = stripos($contents, '</table>', $start); 185 $contents = substr($contents, $start, $end - $start); 186 preg_match_all('/<tr[^>]*><td>(<[^>]+>)?([^<]+)/is', $contents, $matches); 187 $names = array(); 188 foreach ($matches[2] as $name) { 189 $name = html_entity_decode(trim(preg_replace('/ \\([^)]+\\)| - .*$/', '', $name))); 190 if (!preg_match('/(?:^|\s+)tea(?:\s+|$)/', $name)) { 191 $names[] = $name; 192 } 193 } 194 $this->populateTable('coke', $names); 195 unset($names); 196 } 197 } 198 199 if ($this->needTable('tea')) { 200 $names = @file('http://www.midnight-labs.org/tea.txt'); 201 if ($names) { 202 foreach ($names as $key => $value) { 203 $names[$key] = ucwords(rtrim($value)); 204 } 205 $this->populateTable('tea', $names); 206 unset($names); 207 } 208 } 209 } 210 211 protected function throwDrink($type, $target) 185 212 { 186 213 if (!$this->db) { … … 189 216 190 217 if (preg_match('/^me(\s|$)/', $target)) { 191 $target = preg_replace('/^me/', $ event->getNick(), $target);218 $target = preg_replace('/^me/', $this->event->getNick(), $target); 192 219 } else if ($target == $this->getConnection()->getNick()) { 193 220 $gender = $this->getIni('gender'); … … 198 225 } 199 226 } 200 $channel = $event->getArgument(0);227 201 228 $drink = $this->getRandomRecord($type); 202 229 203 if ($type != 'tea') { 204 $text = 'throws ' . $target . ' a'; 205 if (preg_match('/^[aeoiu]/i', $drink)) { 206 $text .= 'n'; 207 } 208 $text .= ' ' . $drink; 209 } else { 210 // One must be gentle with tea 211 $text = 'pours '.$target.' a cup of '.$drink.' tea'; 212 } 213 214 $this->doCtcpAction($channel, $text); 215 } 216 217 public function onDoBeer($target, Phergie_Event_Request $event) 218 { 219 $this->throwDrink('beer', $target, $event); 220 } 221 222 public function onDoCocktail($target, Phergie_Event_Request $event) 223 { 224 $this->throwDrink('cocktail', $target, $event); 225 } 226 227 public function onDoCoke($target, Phergie_Event_Request $event) 228 { 229 $this->throwDrink('coke', $target, $event); 230 } 231 232 public function onDoTea( $target, Phergie_Event_Request $event ) 233 { 234 $this->throwDrink('tea', $target, $event); 230 if ($drink) { 231 if ($type != 'tea') { 232 $text = 'throws ' . $target . ' a'; 233 if (preg_match('/^[aeoiu]/i', $drink)) { 234 $text .= 'n'; 235 } 236 $text .= ' ' . $drink; 237 } else { 238 // One must be gentle with tea 239 $text = 'pours '.$target.' a cup of '.$drink.' tea'; 240 } 241 $this->doCtcpAction($this->event->getSource(), $text); 242 } 243 } 244 245 public function onDoBeer($target) 246 { 247 $this->throwDrink('beer', $target); 248 } 249 250 public function onDoCocktail($target) 251 { 252 $this->throwDrink('cocktail', $target); 253 } 254 255 public function onDoCoke($target) 256 { 257 $this->throwDrink('coke', $target); 258 } 259 260 public function onDoTea( $target ) 261 { 262 $this->throwDrink('tea', $target); 235 263 } 236 264 } trunk/Phergie/Plugin/JoinPart.php
r57 r58 2 2 3 3 /** 4 * @see Phergie_Plugin_A dminCommand4 * @see Phergie_Plugin_Abstract_AdminCommand 5 5 */ 6 require_once 'Phergie/ Event/Handler/AdminCommand.php';6 require_once 'Phergie/Plugin/Abstract/AdminCommand.php'; 7 7 8 class Phergie_Plugin_JoinPart extends Phergie_ Event_Handler_AdminCommand8 class Phergie_Plugin_JoinPart extends Phergie_Plugin_Abstract_AdminCommand 9 9 { 10 10 /** … … 25 25 * @param Phergie_Event_Request $event Intercepted event 26 26 */ 27 public function onDoPart($channel = null , Phergie_Event_Request $event)27 public function onDoPart($channel = null) 28 28 { 29 29 if (empty($channel)) { 30 $this->doPart($ event->getArgument(0));30 $this->doPart($this->event->getArgument(0)); 31 31 } else { 32 32 $this->doPart($channel); trunk/Phergie/Plugin/Karma.php
r57 r58 16 16 * Retains the last garbage collection date 17 17 */ 18 protected $lastG C= null;18 protected $lastGc = null; 19 19 20 20 /** … … 33 33 public function init() 34 34 { 35 $this->db = null; 36 $this->lastGc = null; 37 $this->log = null; 38 35 39 $this->fixedKarma = array 36 40 ( … … 78 82 * Plugin code, handles requests 79 83 */ 80 public function onPrivmsg( Phergie_Event_Request $event)84 public function onPrivmsg() 81 85 { 82 86 if ($this->db === null) { 83 87 return; 84 88 } 89 $target = $this->getSource(); 90 $message = $this->event->getArgument(1); 85 91 // Karma status request 86 if (preg_match('#^karma (\S+?)$#i', $ event->getArgument(1), $m)) {92 if (preg_match('#^karma (\S+?)$#i', $message, $m)) { 87 93 // Return fixed value if set 88 94 if(isset($this->fixedKarma[strtolower($m[1])])) { 89 $this->doPrivmsg($ event->getArgument(0), $m[1] . ' ' . $this->fixedKarma[strtolower($m[1])]);95 $this->doPrivmsg($source, $m[1] . ' ' . $this->fixedKarma[strtolower($m[1])]); 90 96 return; 91 97 } … … 93 99 $res = $this->db->query('SELECT karma FROM karmas WHERE word = \''.sqlite_escape_string(strtolower($m[1])).'\' LIMIT 1', SQLITE_NUM); 94 100 if ($res->numRows() && $res->column(0) != 0) { 95 $this->doPrivmsg($ event->getArgument(0), $m[1].' has karma of '.$res->column(0));101 $this->doPrivmsg($source, $m[1].' has karma of '.$res->column(0)); 96 102 } else { 97 $this->doPrivmsg($ event->getArgument(0), $m[1].' has neutral karma');103 $this->doPrivmsg($source, $m[1].' has neutral karma'); 98 104 } 99 }100 105 // Incrementation/decrementation request 101 elseif (preg_match('#^(\S+?)(\+\+|--)\s*(.*)$#i', $event->getArgument(1), $m)) {106 } elseif (preg_match('#^(\S+?)(\+\+|--)\s*(.*)$#i', $message, $m)) { 102 107 $word = strtolower($m[1]); 103 108 // Do nothing if it's fixed … … 106 111 } 107 112 // Forces a decrementation if someone tries to update his own karma 108 if ($word == strtolower($ event->getNick())) {113 if ($word == strtolower($this->event->getNick())) { 109 114 $m[2] = '--'; 110 115 } 111 116 // Flood check 112 if (isset($this->log[$event->getHost()][$word]) && $this->log[$event->getHost()][$word] > time()) { 117 $host = $this->event->getHost(); 118 if (isset($this->log[$host][$word]) && $this->log[$host][$word] > time()) { 113 119 return; 114 120 } 115 $this->log[$ event->getHost()][$word] = time() + 86400;121 $this->log[$host][$word] = time() + 86400; 116 122 // Get the current value then update or create entry 117 123 $res = $this->db->query('SELECT karma, ROWID FROM karmas WHERE word = \''.sqlite_escape_string($word).'\' LIMIT 1', SQLITE_NUM); … … 129 135 } 130 136 // Collect garbage off the anti-flood log once a day 131 if (date('d') !== $this->lastG C) {132 $this->doG C();137 if (date('d') !== $this->lastGc) { 138 $this->doGc(); 133 139 } 134 140 } … … 138 144 * Performs garbage collection on the anti-flood log 139 145 */ 140 public function doG C()146 public function doGc() 141 147 { 142 148 $now = time(); … … 151 157 } 152 158 } 153 $this->lastG C= date('d');159 $this->lastGc = date('d'); 154 160 } 155 161 } trunk/Phergie/Plugin/Lart.php
r57 r58 94 94 } 95 95 96 public function onPrivmsg( Phergie_Event_Request $event)96 public function onPrivmsg() 97 97 { 98 $channel = $event->getArgument(0);99 98 $message = $event->getArgument(1); 100 99 $nick = $this->getConnection()->getNick(); … … 133 132 } while($redirect); 134 133 if ($definition) { 135 $this->doPrivmsg($ channel, $definition);134 $this->doPrivmsg($this->event->getSource(), $definition); 136 135 } 137 136 } trunk/Phergie/Plugin/Logging.php
r57 r58 68 68 69 69 // Create plugin dir if not there yet 70 if (!file_exists(dirname(__FILE__).'/Logging')) 71 mkdir(dirname(__FILE__).'/Logging'); 70 $dir = dirname(__FILE__) . '/Logging'; 71 if (!file_exists($dir)) { 72 mkdir($dir); 73 } 72 74 73 75 // Load or initialize de logging database 74 $db = dirname(__FILE__).'/Logging/logging.db';76 $db = $dir . '/logging.db'; 75 77 if (!file_exists($db)) { 76 78 $this->db = new SQLiteDatabase($db); … … 88 90 * Tracks text and answers requests 89 91 */ 90 public function onPrivmsg(Phergie_Event_Request $event) { 92 public function onPrivmsg() 93 { 91 94 if (preg_match('#^search (\S+)$#', $event->getArgument(1), $m)) { 92 95 // Search DB trunk/Phergie/Plugin/Users.php
r57 r58 8 8 class Phergie_Plugin_Users extends Phergie_Event_Handler 9 9 { 10 protected static $list = array(); 11 const OP = 8; 12 const HALFOP = 4; 13 const VOICE = 2; 14 const REGULAR = 1; 15 16 /** 17 * Tracks mode changes 18 */ 19 public function onMode(Phergie_Event_Request $event) 20 { 21 if (preg_match('{(\S+)\s((?:\+|-)[hov+-]+)\s((?:\s*\S+)+)$}i', $event->getArgument(0), $m)) { 22 $chan = $m[1]; 23 $modes = str_split($m[2], 1); 24 $nicks = explode(' ', $m[3]); 25 while ($char = array_shift($modes)) { 26 switch($char) { 27 case '+': 28 $mode = '+'; 29 break; 30 case '-': 31 $mode = '-'; 32 break; 33 case 'o': 34 $nick = array_shift($nicks); 35 if ($mode == '+') { 36 self::$list[$chan][$nick] |= self::OP; 37 } elseif ($mode == '-') { 38 self::$list[$chan][$nick] ^= self::OP; 39 } 40 break; 41 case 'h': 42 $nick = array_shift($nicks); 43 if ($mode == '+') { 44 self::$list[$chan][$nick] |= self::OP; 45 } elseif ($mode == '-') { 46 self::$list[$chan][$nick] ^= self::OP; 47 } 48 break; 49 case 'v': 50 $nick = array_shift($nicks); 51 if ($mode == '+') { 52 self::$list[$chan][$nick] |= self::OP; 53 } elseif ($mode == '-') { 54 self::$list[$chan][$nick] ^= self::OP; 55 } 56 break; 57 } 58 } 59 } 60 } 61 62 /* DEBUG Func * / 63 public function onPrivmsg(Phergie_Event_Request $event) { 64 if(preg_match('#^ishere (\S+)$#', $event->getArgument(1), $m)) { 65 $this->doPrivmsg($event->getArgument(0), self::isIn($m[1], $event->getArgument(0)) ? 'true':'false'); 66 } 67 if(preg_match('#^isop (\S+)$#', $event->getArgument(1), $m)) { 68 $this->doPrivmsg($event->getArgument(0), self::isOp($m[1], $event->getArgument(0)) ? 'true':'false'); 69 } 70 if(preg_match('#^isvoice (\S+)$#', $event->getArgument(1), $m)) { 71 $this->doPrivmsg($event->getArgument(0), self::isVoice($m[1], $event->getArgument(0)) ? 'true':'false'); 72 } 73 } 74 //*/ 75 76 /** 77 * Tracks users joining 78 */ 79 public function onJoin(Phergie_Event_Request $event) 80 { 81 self::$list[$event->getArgument(0)][$event->getNick()] = self::REGULAR; 82 } 83 84 /** 85 * Tracks users parting 86 */ 87 public function onPart(Phergie_Event_Request $event) 88 { 89 if (isset(self::$list[$event->getArgument(0)][$event->getNick()])) { 90 unset(self::$list[$event->getArgument(0)][$event->getNick()]); 91 } 92 } 93 94 /** 95 * Tracks users quitting 96 */ 97 public function onQuit(Phergie_Event_Request $event) 98 { 99 $nick = $event->getNick(); 100 foreach (self::$list as $channame=>$chan) { 101 if (isset($chan[$nick])) { 102 unset(self::$list[$channame][$nick]); 103 } 104 } 105 } 106 107 /** 108 * Tracks users changing nick 109 */ 110 public function onNick(Phergie_Event_Request $event) 111 { 112 $nick = $event->getNick(); 113 $newNick = $event->getArgument(0); 114 echo "\n$nick => $newNick\n\n"; 115 foreach (self::$list as $channame=>$chan) { 116 if (isset($chan[$nick])) { 117 self::$list[$channame][$newNick] = $chan[$nick]; 118 unset(self::$list[$channame][$nick]); 119 } 120 } 121 } 122 123 /** 124 * Populates the list of a channel when the bot joins it 125 */ 126 public function onResponse(Phergie_Event_Response $event) 127 { 128 if ($event->getCode() == Phergie_Event_Response::RPL_NAMREPLY) { 129 $desc = $event->getDescription(); 130 $desc = substr($desc, strpos($desc, '=')+2); 131 list($chan, $users) = explode(' :', $desc); 132 $users = explode(' ', $users); 133 foreach ($users as $user) { 134 $flag = self::REGULAR; 135 if (substr($user, 0, 1) === '@') { 136 $user = substr($user, 1); 137 $flag |= self::OP; 138 } 139 if (substr($user, 0, 1) === '%') { 140 $user = substr($user, 1); 141 $flag |= self::HALFOP; 142 } 143 if (substr($user, 0, 1) === '+') { 144 $user = substr($user, 1); 145 $flag |= self::VOICE; 146 } 147 self::$list[$chan][$user] = $flag; 148 } 149 } 150 } 151 152 /** 153 * Checks whether someone has op (@) status 154 * 155 * @param string $nick The nick to check 156 * @param string $chan The chan where to check 157 * @return bool 158 */ 159 public static function isOp($nick, $chan) 160 { 161 return isset(self::$list[$chan][$nick]) && (self::$list[$chan][$nick] & self::OP) != 0; 162 } 163 164 /** 165 * Checks whether someone has voice (+) status 166 * 167 * @param string $nick The nick to check 168 * @param string $chan The chan where to check 169