Changeset 60
- Timestamp:
- 02/17/08 04:25:26 (9 months ago)
- Files:
-
- trunk/Phergie/Bot.php (modified) (6 diffs)
- trunk/Phergie/CONTRIBUTE (deleted)
- trunk/Phergie/Driver/Abstract.php (modified) (1 diff)
- trunk/Phergie/Driver/Streams.php (modified) (3 diffs)
- trunk/Phergie/Event/Handler.php (modified) (3 diffs)
- trunk/Phergie/Event/Request.php (modified) (14 diffs)
- trunk/Phergie/Plugin/Abstract/AdminCommand.php (modified) (3 diffs)
- trunk/Phergie/Plugin/Abstract/Command.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Acronym.php (modified) (5 diffs)
- trunk/Phergie/Plugin/Autojoin.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) (7 diffs)
- trunk/Phergie/Plugin/Karma.php (modified) (4 diffs)
- trunk/Phergie/Plugin/Lart.php (modified) (5 diffs)
- trunk/Phergie/Plugin/Logging.php (modified) (6 diffs)
- trunk/Phergie/Plugin/Math.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Nickserv.php (modified) (1 diff)
- trunk/Phergie/Plugin/Php.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Puppet.php (moved) (moved from trunk/Phergie/Plugin/Say.php) (2 diffs)
- trunk/Phergie/Plugin/UrbanDictionary.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Url.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Users.php (modified) (3 diffs)
- trunk/Phergie/README (modified) (4 diffs)
- trunk/Phergie/phergie.ini (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Phergie/Bot.php
r59 r60 30 30 31 31 /** 32 * Path to the directory containing the Phergie directory 33 * 34 * @const string 35 */ 36 define('PHERGIE_DIR', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR); 37 38 /** 32 39 * Add the Phergie directory to the include path 33 40 */ … … 35 42 get_include_path() 36 43 . PATH_SEPARATOR . 37 dirname(dirname(__FILE__))44 PHERGIE_DIR 38 45 ); 39 46 … … 62 69 */ 63 70 $required = array('server', 'username', 'nick'); 64 $config = @parse_ini_file( $ini);71 $config = @parse_ini_file(PHERGIE_DIR . 'Phergie' . DIRECTORY_SEPARATOR . $ini); 65 72 66 73 if (count($config) == 0) { … … 70 77 71 78 foreach ($required as $setting) { 72 if (!isset($config[$setting]) ) {79 if (!isset($config[$setting]) || empty($config[$setting])) { 73 80 echo 'Required configuration setting missing: ' . $setting . "\n"; 74 81 return; … … 113 120 * Set up event handlers 114 121 */ 115 $iterator = new DirectoryIterator( 'Plugin');122 $iterator = new DirectoryIterator(PHERGIE_DIR . '/Phergie/Plugin'); 116 123 foreach ($iterator as $entry) { 117 124 if ($iterator->isFile() … … 120 127 require_once 'Phergie/Plugin/' . $entry; 121 128 $class = 'Phergie_Plugin_' . str_replace('.php', '', $entry); 122 $client->addEventHandler(new $class($client)); 129 $instance = new $class($client); 130 $client->addEventHandler($instance); 131 $client->debug('Loaded ' . $instance->getName()); 123 132 } 124 133 } trunk/Phergie/Driver/Abstract.php
r59 r60 76 76 { 77 77 if ($this->getIni('debug')) { 78 echo date('H:i:s') . ' driver' . $message . "\n";78 echo date('H:i:s') . ' ' . $message . "\n"; 79 79 } 80 80 } trunk/Phergie/Driver/Streams.php
r59 r60 152 152 153 153 switch ($cmd) { 154 case 'names': 155 case 'nick': 156 case 'quit': 157 case 'ping': 158 case 'join': 159 $args = array(ltrim($args, ':')); 160 break; 161 case 'notice': 162 $args = explode(' :', $args); 163 break; 154 164 case 'oper': 155 165 case 'topic': 156 166 case 'mode': 157 $args = explode(' ', $args); 158 break; 159 case 'join': 160 $args = explode(' ', $args); 161 $args[0] = explode(',', $args[0]); 162 if (isset ($args[1])) { 163 $args[1] = explode(',', $args[1]); 164 } 167 $args = preg_split('/ :?/', $args); 165 168 break; 166 169 case 'part': 167 case 'names': 168 $args = array(explode(',', $args)); 169 break; 170 case 'nick': 171 case 'quit': 172 case 'ping': 173 $args = array(ltrim($args, ':')); 174 break; 175 case 'notice': 176 $args = explode(' :', $args); 170 $args = preg_split('/ :?/', $args, 2); 171 break; 172 case 'kick': 173 $args = preg_split('/ :?/', $args, 3); 177 174 break; 178 175 case 'privmsg': … … 188 185 } else { 189 186 $args = explode(' :', $args); 190 $args[0] = explode(',', $args[0]);191 187 } 192 188 break; … … 234 230 } 235 231 $this->doQuit($reason); 232 break; 236 233 } 237 234 unset($this->queue, $event, $command, $arguments); trunk/Phergie/Event/Handler.php
r59 r60 23 23 { 24 24 /** 25 * Flag indicating whether or not the plugin requires its own directory 26 * for local storage 27 * 28 * @see $dir 29 * @var bool 30 */ 31 protected $needsDir = false; 32 33 /** 34 * Path to the directory for the plugin if needsDir is enabled 35 * 36 * @see $needsDir 37 */ 38 protected $dir; 39 40 /** 25 41 * Reference back to the client used to initiate commands 26 42 * … … 55 71 $name = get_class($this); 56 72 $this->name = substr($name, strrpos($name, '_') + 1); 73 74 if ($this->needsDir) { 75 $class = new ReflectionClass($name); 76 $dir = 77 dirname($class->getFilename()) . 78 DIRECTORY_SEPARATOR . 79 $this->name . 80 DIRECTORY_SEPARATOR; 81 if (!file_exists($dir)) { 82 mkdir($dir); 83 } 84 $this->dir = $dir; 85 } 57 86 } 58 87 … … 123 152 public function debug($message) 124 153 { 125 $this->client->debug( date('H:i:s') . ' ' . $this->name. ' -> ' . $message);154 $this->client->debug(strtolower($this->name) . ' -> ' . $message); 126 155 } 127 156 trunk/Phergie/Event/Request.php
r59 r60 10 10 /** 11 11 * Password message 12 * 13 * @const string 12 14 */ 13 15 const TYPE_PASS = 'pass'; … … 15 17 /** 16 18 * Nick message 19 * 20 * @const string 17 21 */ 18 22 const TYPE_NICK = 'nick'; … … 20 24 /** 21 25 * User message 26 * 27 * @const string 22 28 */ 23 29 const TYPE_USER = 'user'; … … 25 31 /** 26 32 * Operator command 33 * 34 * @const string 27 35 */ 28 36 const TYPE_OPER = 'oper'; … … 30 38 /** 31 39 * Quit command 40 * 41 * @const string 32 42 */ 33 43 const TYPE_QUIT = 'quit'; … … 35 45 /** 36 46 * Join message 47 * 48 * @const string 37 49 */ 38 50 const TYPE_JOIN = 'join'; 39 51 40 52 /** 53 * Kick message 54 * 55 * @const string 56 */ 57 const TYPE_KICK = 'kick'; 58 59 /** 41 60 * Part message 61 * 62 * @const string 42 63 */ 43 64 const TYPE_PART = 'part'; … … 45 66 /** 46 67 * Mode message 68 * 69 * @const string 47 70 */ 48 71 const TYPE_MODE = 'mode'; … … 50 73 /** 51 74 * Topic message 75 * 76 * @const string 52 77 */ 53 78 const TYPE_TOPIC = 'topic'; … … 55 80 /** 56 81 * Private message command 82 * 83 * @const string 57 84 */ 58 85 const TYPE_PRIVMSG = 'privmsg'; … … 60 87 /** 61 88 * Notice message 89 * 90 * @const string 62 91 */ 63 92 const TYPE_NOTICE = 'notice'; … … 65 94 /** 66 95 * Pong message 96 * 97 * @const string 67 98 */ 68 99 const TYPE_PONG = 'pong'; … … 70 101 /** 71 102 * CTCP ACTION command 103 * 104 * @const string 72 105 */ 73 106 const TYPE_ACTION = 'action'; … … 249 282 public function getSource() 250 283 { 251 if ($this->type == Phergie_Event_Request::TYPE_PRIVMSG 252 && $this->arguments[0][0] == '#') { 284 if ($this->arguments[0][0] == '#') { 253 285 return $this->arguments[0]; 254 286 } … … 257 289 258 290 /** 291 * Returns whether or not the event occurred within a channel. 292 * 293 * @return TRUE if the event is in a channel, FALSE otherwise 294 */ 295 public function isInChannel() 296 { 297 return (substr($this->getSource(), 0, 1) == '#'); 298 } 299 300 /** 259 301 * Returns whether or not the event originated from a user. 260 302 * trunk/Phergie/Plugin/Abstract/AdminCommand.php
r59 r60 82 82 $ini = $this->getIni('admins', 'admincommand'); 83 83 } 84 $admins = preg_split('#[\s\r\n,]+#', $ini); 85 foreach ($admins as $admin) { 86 // Find out which chars are present in the config mask and exclude them from the regex match 87 $excluded = ''; 88 if (strpos($admin, '!')!==false) { 89 $excluded .= '!'; 84 if (!empty ($ini)) { 85 $admins = preg_split('#[\s\r\n,]+#', $ini); 86 foreach ($admins as $admin) { 87 // Find out which chars are present in the config mask and exclude them from the regex match 88 $excluded = ''; 89 if (strpos($admin, '!')!==false) { 90 $excluded .= '!'; 91 } 92 if (strpos($admin, '@')!==false) { 93 $excluded .= '@'; 94 } 95 96 // Escape regex meta characters 97 $admin = str_replace( 98 array('\\', '^', '$', '.', '[', ']', '|', '(', ')', '?', '+', '{', '}'), 99 array('\\\\', '\\^', '\\$', '\\.', '\\[', '\\]', '\\|', '\\(', '\\)', '\\?', '\\+', '\\{', '\\}'), 100 $admin 101 ); 102 // Replace * so that they match correctly in a regex 103 $this->admins[$class][] = str_replace('*', ($excluded === '' ? '.*' : '[^'.$excluded.']*'), $admin); 90 104 } 91 if (strpos($admin, '@')!==false) { 92 $excluded .= '@'; 93 } 94 95 // Escape regex meta characters 96 $admin = str_replace( 97 array('\\', '^', '$', '.', '[', ']', '|', '(', ')', '?', '+', '{', '}'), 98 array('\\\\', '\\^', '\\$', '\\.', '\\[', '\\]', '\\|', '\\(', '\\)', '\\?', '\\+', '\\{', '\\}'), 99 $admin 100 ); 101 // Replace * so that they match correctly in a regex 102 $this->admins[$class][] = str_replace('*', ($excluded === '' ? '.*' : '[^'.$excluded.']*'), $admin); 105 $this->admins[$class] = '/^' . implode('|', $this->admins[$class]) . '$/'; 103 106 } 104 107 } … … 114 117 { 115 118 $class = $this->getName(); 116 $nick = $this->event->getSource(); 117 $mask = $this->event->getHostmask(); 119 // Try to match mask against admin masks 120 if (isset ($this->admins[$class]) 121 && preg_match($this->admins[$class], $this->event->getHostmask())) { 122 return true; 123 } 118 124 // Check if is op and ops are admins 119 if ($this->ops[$class]) { 120 foreach (Phergie_Plugin_Users::getChannels($nick) as $chan) { 121 if (Phergie_Plugin_Users::isOp($nick, $chan)) { 122 return true; 123 } 124 } 125 } 126 // Try to match mask against admin masks 127 if (isset ($this->admins[$class])) { 128 return in_array($mask, $this->admins[$class]); 125 $nick = $this->event->getNick(); 126 $source = $this->event->getSource(); 127 if ($this->ops[$class] && Phergie_Plugin_Users::isOp($nick, $source)) { 128 return true; 129 129 } 130 130 return false; … … 141 141 if ($this->fromAdmin()) { 142 142 $target = $this->event->getArgument(0); 143 $exp = $target[0] [0]== '#' ?143 $exp = $target[0] == '#' ? 144 144 '/^' . $this->getIni('nick') . '\s*:?\s+(.*)$/' : 145 145 '/^(?:' . $this->getIni('nick') . '\s*:?\s+)?(.*)$/'; trunk/Phergie/Plugin/Abstract/Command.php
r59 r60 45 45 $name = $method->getName(); 46 46 if (strpos($name, 'onDo') === 0) { 47 $this->methods[substr($name, 4)] = $method->getNumberOfRequiredParameters(); 47 $this->methods[strtolower(substr($name, 4))] = 48 $method->getNumberOfRequiredParameters(); 48 49 } 49 50 } … … 60 61 $this->$method(); 61 62 } 62 } else if ($this->methods[$command] <= count($params)){63 } else { 63 64 $params = preg_split('/\s+/', $params, $this->methods[$command]); 64 call_user_func_array(array($this, $method), $params); 65 if ($this->methods[$command] <= count($params)) { 66 call_user_func_array(array($this, $method), $params); 67 } 65 68 } 66 69 } trunk/Phergie/Plugin/Acronym.php
r59 r60 46 46 { 47 47 $limit = $this->getIni('limit'); 48 if ($limit < 0 ) {48 if ($limit < 0 || $limit === null) { 49 49 $this->limit = 5; 50 50 } else { … … 60 60 * @return void 61 61 */ 62 protected function _randomAction($target)62 protected function randomAction($target) 63 63 { 64 64 do { … … 87 87 $message = $this->event->getArgument(1); 88 88 89 if (!preg_match('/ ^(?:[^\s:]+\s*:?\s*)?((?:[A-Z]\.?){2,})\?$/', $message, $acronym)) {89 if (!preg_match('/((?:[A-Z]\.?){2,})\?/', $message, $acronym)) { 90 90 return; 91 91 } … … 110 110 $contents = @file_get_contents($url, false, $context); 111 111 112 if (empty($contents)) { 112 if (empty ($contents) 113 || strpos($contents, 'no abbreviation matches') !== false 114 || strpos($contents, 'has exceeded the daily query limit') !== false) { 113 115 $this->randomAction($target); 114 116 } else { … … 130 132 } while (($this->limit == 0 || count($matches) < $this->limit) && $count == 1); 131 133 132 if (count($matches) == 0) { 133 $this->randomAction($target); 134 } else { 135 $text = 'Possible matches for ' . $acronym . ': ' . implode(', ', $matches); 136 $this->doPrivmsg($target, $text); 137 } 134 $text = 'Possible matches for ' . $acronym . ': ' . implode(', ', $matches); 135 $this->doPrivmsg($target, $text); 138 136 } 139 137 } trunk/Phergie/Plugin/Autojoin.php
r59 r60 23 23 public function onResponse() 24 24 { 25 if ($this->event->getCode() == Phergie_Event_Response::RPL_ENDOFMOTD) { 26 $channels = $this->getIni('channels'); 27 if (!empty ($channels)) { 28 $this->doJoin(implode(' ', preg_split('#[, ]+#', $channels))); 29 } 25 switch ($this->event->getCode()) { 26 case Phergie_Event_Response::RPL_ENDOFMOTD: 27 case Phergie_Event_Response::ERR_NOMOTD: 28 $channels = $this->getIni('channels'); 29 if (!empty ($channels)) { 30 $this->doJoin(implode(' ', preg_split('#[, ]+#', $channels))); 31 } 32 break; 30 33 } 31 34 } trunk/Phergie/Plugin/Daddy.php
r59 r60 22 22 $bot = $this->getIni('nick'); 23 23 $text = $this->event->getArgument(1); 24 if (preg_match('/' . $bot . '\s*:?\s+?who\'?s your daddy\ \??/iAD', $text)) {25 $this->doPrivmsg($this-> getSource(), 'You\'re my daddy, ' . $this->event->getNick() . '!');24 if (preg_match('/' . $bot . '\s*:?\s+?who\'?s your daddy\??/iAD', $text)) { 25 $this->doPrivmsg($this->event->getSource(), 'You\'re my daddy, ' . $this->event->getNick() . '!'); 26 26 } 27 27 } trunk/Phergie/Plugin/Debug.php
r59 r60 21 21 { 22 22 $text = 'current : '.number_format(memory_get_usage() / 1024).'KB / peak : '.number_format(memory_get_peak_usage() / 1024).'KB'; 23 $this->doPrivmsg($this-> getSource(), $text);23 $this->doPrivmsg($this->event->getSource(), $text); 24 24 } 25 25 } trunk/Phergie/Plugin/Drink.php
r59 r60 18 18 class Phergie_Plugin_Drink extends Phergie_Plugin_Abstract_Command 19 19 { 20 /** 21 * Indicates that a local directory is required for this plugin 22 * 23 * @var bool 24 */ 25 protected $needsDir = true; 26 20 27 /** 21 28 * PDO resource for a SQLite database containing the drinks … … 76 83 77 84 /** 78 * Determines if a table does not exist or is empty.79 *80 * @param string $name Table name81 * @return bool TRUE if the table does not exist or is empty, FALSE82 * otherwise83 */84 private function needTable($name)85 {86 $table = $this->db87 ->query('SELECT COUNT(*) FROM sqlite_master WHERE name = ' . $this->db->quote($name))88 ->fetchColumn();89 90 if (!$table) {91 return true;92 }93 94 return !$this->db95 ->query('SELECT COUNT(*) FROM ' . $name)96 ->fetchColumn();97 }98 99 /**100 * Populates a source database table with a given set of data.101 *102 * @param string $table Name of the table103 * @param array $names List of drink names104 * @return void105 */106 private function populateTable($table, $names)107 {108 $this->db->exec('109 CREATE TABLE ' . $name . ' ( name VARCHAR(255) );110 CREATE UNIQUE INDEX ' . $name . ' ON ' . $name . ' (name);111 ');112 113 $stmt = $this->db->prepare('INSERT INTO ' . $table . ' (name) VALUES (:name)');114 $this->db->beginTransaction();115 foreach (array_unique($names) as $name) {116 foreach ($this->filter as $filter) {117 if (preg_match('/(^|[^a-z])' . $filter . '([^a-z]|$)/i', $name)) {118 $this->debug('Filtered out ' . $name . ' because it contains ' . $filter);119 continue 2;120 }121 }122 $this->debug('Inserted ' . $name);123 $stmt->execute(array('name' => $name));124 }125 $this->db->commit();126 }127 128 /**129 * Returns a random record value from a given table130 *131 * @string $table Name of the table132 * @return string Value of the name column for the selected record133 */134 private function getRandomRecord($table)135 {136 return $this->db137 ->query('SELECT name FROM ' . $table . ' ORDER BY Random() LIMIT 1')138 ->fetchColumn();139 }140 141 /**142 * Returns whether or not a given value has characters that may not be143 * displayed correctly144 *145 * @param string $name Value to check146 */147 private function hasBadChars($name)148 {149 return (max(array_map('ord', str_split($name))) > 126);150 }151 152 /**153 85 * Connects to the database and populates tables where needed. 154 86 * … … 162 94 } 163 95 164 // Create the plugin directory if needed165 $dir = dirname(__FILE__) . '/' . $this->getName();166 if (!file_exists($dir)) {167 mkdir($dir);168 }169 170 96 // Initialize the database connection 171 $this->db = new PDO('sqlite:' . $ dir . '/drink.db');97 $this->db = new PDO('sqlite:' . $this->dir . 'drink.db'); 172 98 173 99 // Populate the database if necessary … … 224 150 $contents = @file_get_contents('http://www.energyfiend.com/huge-caffeine-database/'); 225 151 if ($contents) { 152 // List of drinks to filter out 153 $filter = array( 154 'tea', 155 'coffee', 156 'starbucks' 157 ); 226 158 $start = stripos($contents, 'id="caffeinedb"'); 227 159 $end = stripos($contents, '</table>', $start); … … 231 163 foreach ($matches[2] as $name) { 232 164 $name = html_entity_decode(trim(preg_replace('/ \\([^)]+\\)| - .*$/', '', $name))); 233 if (!preg_match('/(?:^|\s+)(?: tea|coffee)(?:\s+|$)/', $name)) {165 if (!preg_match('/(?:^|\s+)(?:' . implode('|', $filter) . ')(?:\s+|$)/i', $name)) { 234 166 $names[] = $name; 235 167 } … … 250 182 } 251 183 } 184 185 unset($this->filter); 186 } 187 188 /** 189 * Determines if a table does not exist or is empty. 190 * 191 * @param string $name Table name 192 * @return bool TRUE if the table does not exist or is empty, FALSE 193 * otherwise 194 */ 195 private function needTable($name) 196 { 197 $table = $this->db 198 ->query('SELECT COUNT(*) FROM sqlite_master WHERE name = ' . $this->db->quote($name)) 199 ->fetchColumn(); 200 201 if (!$table) { 202 return true; 203 } 204 205 return !$this->db 206 ->query('SELECT COUNT(*) FROM ' . $name) 207 ->fetchColumn(); 208 } 209 210 /** 211 * Populates a source database table with a given set of data. 212 * 213 * @param string $table Name of the table 214 * @param array $names List of drink names 215 * @return void 216 */ 217 private function populateTable($table, $names) 218 { 219 $this->db->exec('CREATE TABLE ' . $table . ' (name VARCHAR(255))'); 220 $this->db->exec('CREATE UNIQUE INDEX ' . $table . '_name ON ' . $table . ' (name)'); 221 222 $stmt = $this->db->prepare('INSERT INTO ' . $table . ' (name) VALUES (:name)'); 223 $this->db->beginTransaction(); 224 foreach (array_unique($names) as $name) { 225 if (preg_match('/(?:^|[^a-z])(' . implode('|', $this->filter) . ')(?:[^a-z]|$)/i', $name, $match)) { 226 $this->debug('Filtered out ' . $name . ' because it contains ' . $match[1]); 227 continue; 228 } 229 $this->debug('Inserted ' . $name); 230 $stmt->execute(array('name' => $name)); 231 } 232 $this->db->commit(); 233 } 234 235 /** 236 * Returns a random record value from a given table 237 * 238 * @string $table Name of the table 239 * @return string Value of the name column for the selected record 240 */ 241 private function getRandomRecord($table) 242 { 243 return $this->db 244 ->query('SELECT name FROM ' . $table . ' ORDER BY Random() LIMIT 1') 245 ->fetchColumn(); 246 } 247 248 /** 249 * Returns whether or not a given value has characters that may not be 250 * displayed correctly 251 * 252 * @param string $name Value to check 253 */ 254 private function hasBadChars($name) 255 { 256 return (max(array_map('ord', str_split($name))) > 126); 252 257 } 253 258 … … 290 295 $text = 'pours '.$target.' a cup of '.$drink.' tea'; 291 296 } 292 $this->doAction($this->event->getSource(), $text );297 $this->doAction($this->event->getSource(), $text . '.'); 293 298 } 294 299 } trunk/Phergie/Plugin/Karma.php
r59 r60 13 13 class Phergie_Plugin_Karma extends Phergie_Event_Handler 14 14 { 15 /** 16 * Indicates that a local directory is required for this plugin 17 * 18 * @var bool 19 */ 20 protected $needsDir = true; 21 15 22 /** 16 23 * Stores the SQLite object … … 57 64 ( 58 65 'pi' => 'pi has karma of ' . M_PI, 59 'chucknorris' => ' pihas karma of Warning: Integer out of range',66 'chucknorris' => 'chucknorris has karma of Warning: Integer out of range', 60 67 'c' => 'c has karma of 299 792 458 m/s', 61 68 'e' => 'e has karma of ' . M_E, 62 69 'euler' => 'euler has karma of ' . M_EULER, 63 70 'mole' => 'mole has karma of 6.02214e23 molecules', 71 'avagadro' => 'avagadro has karma of 6.02214e23 molecules', 64 72 'spoon' => 'spoon has no karma. There is no spoon.', 65 strtolower($this->getIni('nick')) => $this->getIni('static'),66 73 'mc^2' => 'mc^2 has karma of e', 67 74 'mc2' => 'mc2 has karma of e', 68 75 'mc²' => 'mc² has karma of e', 69 76 ); 77 78 $static = $this->getIni('static'); 79 if ($static) { 80 $this->fixedKarma[strtolower($this->getIni('nick'))] = $static; 81 } 70 82 71 83 // Check to ensure that the SQLite extension is loaded … … 74 86 } 75 87 76 // Create a directory to contain the database if needed77 if (!file_exists(dirname(__FILE__).'/Karma'))78 mkdir(dirname(__FILE__).'/Karma');79 80 88 // Load or initialize the database 81 $db = dirname(__FILE__).'/Karma/karma.db';89 $db = $this->dir . 'karma.db'; 82 90 if (!file_exists($db)) { 83 91 $this->db = new SQLiteDatabase($db); … … 108 116 return; 109 117 } 110 $ target = $this->getSource();118 $source = $this->event->getSource(); 111 119 $message = $this->event->getArgument(1); 112 120 // Karma status request 113 121 if (preg_match('#^karma (\S+?)$#i', $message, $m)) { 122 $term = strtolower($m[1]); 114 123 // Return fixed value if set 115 if (isset($this->fixedKarma[strtolower($m[1])])) {116 $this->doPrivmsg($source, $ m[1] . ' ' . $this->fixedKarma[strtolower($m[1])]);124 if (isset ($this->fixedKarma[$term])) { 125 $this->doPrivmsg($source, $this->fixedKarma[$term]); 117 126 return; 118 127 } 119 128 // Return current karma or neutral if not set yet 120 $res = $this->db->query('SELECT karma FROM karmas WHERE word = \''.sqlite_escape_string( strtolower($m[1])).'\' LIMIT 1', SQLITE_NUM);129 $res = $this->db->query('SELECT karma FROM karmas WHERE word = \''.sqlite_escape_string($term).'\' LIMIT 1', SQLITE_NUM); 121 130 if ($res->numRows() && $res->column(0) != 0) { 122 131 $this->doPrivmsg($source, $m[1].' has karma of '.$res->column(0)); trunk/Phergie/Plugin/Lart.php
r59 r60 14 14 { 15 15 /** 16 * Indicates that a local directory is required for this plugin 17 * 18 * @var bool 19 */ 20 protected $needsDir = true; 21 22 /** 16 23 * Date string indicating the last time the cache was emptied 17 24 * … … 26 33 */ 27 34 protected $cache; 35 36 /** 37 * PDO instance for the database 38 * 39 * @var PDO 40 */ 41 protected $db; 28 42 29 43 /** … … 62 76 } 63 77 64 // Create the plugin directory if needed65 $dir = dirname(__FILE__) . '/' . $this->getName();66 if (!file_exists($dir)) {67 mkdir($dir);68 }69 70 78 // Initialize the database connection 71 $db = $ dir . '/lart.db';72 $create = !file_exists($db);79 $db = $this->dir . 'lart.db'; 80 $create = !file_exists($db); 73 81 $this->db = new PDO('sqlite:' . $db); 74 82 75 83 // Create database tables if necessary 76 84 if ($create) { 77 $this->db->exec('CREATE TABLE lart ( name VARCHAR(255), definition TEXT )'); 78 $this->db->exec('CREATE UNIQUE INDEX lart_name ON lart (name)'); 85 $this->db->exec(' 86 CREATE TABLE lart ( name VARCHAR(255), definition TEXT ); 87 CREATE UNIQUE INDEX lart_name ON lart (name) 88 '); 79 89 } 80 90 … … 141 151 } 142 152 $this->doAction( 143 $this->event->get Source(),153 $this->event->getArgument(0), 144 154 'puts her hands over her ears and cries, "Stop confusing me!"' 145 155 ); 156 return; 146 157 } 147 158 $definition = $redirect; … … 182 193 } 183 194 184 if (preg_match('/^(' . $nick . ': )?(.*) is (.*)$/U', $message, $match)) {195 if (preg_match('/^(' . $nick . ':?\s*)?(.*) is (.*)$/i', $message, $match)) { 185 196 list (, $address, $name, $definition) = $match; 186 if ( !empty($nick) || $source[0] != '#') {197 if (empty ($address) || $source[0] == '#') { 187 198 $name = strtolower($name); 188 199 $this->replace->execute(array(':name' => $name, ':definition' => $definition)); 189 200 $this->c