Assembla home | Assembla project page
 

Changeset 60

Show
Ignore:
Timestamp:
02/17/08 04:25:26 (9 months ago)
Author:
tobias382
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Phergie/Bot.php

    r59 r60  
    3030 
    3131/** 
     32* Path to the directory containing the Phergie directory 
     33* 
     34* @const string 
     35*/ 
     36define('PHERGIE_DIR', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR); 
     37 
     38/** 
    3239* Add the Phergie directory to the include path 
    3340*/ 
     
    3542    get_include_path() 
    3643    . PATH_SEPARATOR . 
    37     dirname(dirname(__FILE__)) 
     44    PHERGIE_DIR 
    3845); 
    3946 
     
    6269*/ 
    6370$required = array('server', 'username', 'nick'); 
    64 $config = @parse_ini_file($ini); 
     71$config = @parse_ini_file(PHERGIE_DIR . 'Phergie' . DIRECTORY_SEPARATOR . $ini); 
    6572 
    6673if (count($config) == 0) { 
     
    7077 
    7178foreach ($required as $setting) { 
    72     if (!isset($config[$setting])) { 
     79    if (!isset($config[$setting]) || empty($config[$setting])) { 
    7380        echo 'Required configuration setting missing: ' . $setting . "\n"; 
    7481        return; 
     
    113120* Set up event handlers 
    114121*/ 
    115 $iterator = new DirectoryIterator('Plugin'); 
     122$iterator = new DirectoryIterator(PHERGIE_DIR . '/Phergie/Plugin'); 
    116123foreach ($iterator as $entry) { 
    117124    if ($iterator->isFile() 
     
    120127        require_once 'Phergie/Plugin/' . $entry; 
    121128        $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()); 
    123132    } 
    124133} 
  • trunk/Phergie/Driver/Abstract.php

    r59 r60  
    7676    { 
    7777        if ($this->getIni('debug')) { 
    78             echo date('H:i:s') . ' driver ' . $message . "\n"; 
     78            echo date('H:i:s') . ' ' . $message . "\n"; 
    7979        } 
    8080    } 
  • trunk/Phergie/Driver/Streams.php

    r59 r60  
    152152 
    153153            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; 
    154164                case 'oper': 
    155165                case 'topic': 
    156166                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); 
    165168                    break; 
    166169                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); 
    177174                    break; 
    178175                case 'privmsg': 
     
    188185                    } else { 
    189186                        $args = explode(' :', $args); 
    190                         $args[0] = explode(',', $args[0]); 
    191187                    } 
    192188                    break; 
     
    234230                } 
    235231                $this->doQuit($reason); 
     232                break; 
    236233            } 
    237234            unset($this->queue, $event, $command, $arguments); 
  • trunk/Phergie/Event/Handler.php

    r59 r60  
    2323{ 
    2424    /** 
     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    /** 
    2541    * Reference back to the client used to initiate commands 
    2642    * 
     
    5571        $name = get_class($this); 
    5672        $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        } 
    5786    } 
    5887 
     
    123152    public function debug($message) 
    124153    { 
    125         $this->client->debug(date('H:i:s') . ' ' . $this->name . ' -> ' . $message); 
     154        $this->client->debug(strtolower($this->name) . ' -> ' . $message); 
    126155    } 
    127156 
  • trunk/Phergie/Event/Request.php

    r59 r60  
    1010    /** 
    1111    * Password message 
     12    * 
     13    * @const string 
    1214    */ 
    1315    const TYPE_PASS = 'pass'; 
     
    1517    /** 
    1618    * Nick message 
     19    * 
     20    * @const string 
    1721    */ 
    1822    const TYPE_NICK = 'nick'; 
     
    2024    /** 
    2125    * User message 
     26    * 
     27    * @const string 
    2228    */ 
    2329    const TYPE_USER = 'user'; 
     
    2531    /** 
    2632    * Operator command 
     33    * 
     34    * @const string 
    2735    */ 
    2836    const TYPE_OPER = 'oper'; 
     
    3038    /** 
    3139    * Quit command 
     40    * 
     41    * @const string 
    3242    */ 
    3343    const TYPE_QUIT = 'quit'; 
     
    3545    /** 
    3646    * Join message 
     47    * 
     48    * @const string 
    3749    */ 
    3850    const TYPE_JOIN = 'join'; 
    3951 
    4052    /** 
     53    * Kick message 
     54    * 
     55    * @const string 
     56    */ 
     57    const TYPE_KICK = 'kick'; 
     58 
     59    /** 
    4160    * Part message 
     61    * 
     62    * @const string 
    4263    */ 
    4364    const TYPE_PART = 'part'; 
     
    4566    /** 
    4667    * Mode message 
     68    * 
     69    * @const string 
    4770    */ 
    4871    const TYPE_MODE = 'mode'; 
     
    5073    /** 
    5174    * Topic message 
     75    * 
     76    * @const string 
    5277    */ 
    5378    const TYPE_TOPIC = 'topic'; 
     
    5580    /** 
    5681    * Private message command 
     82    * 
     83    * @const string 
    5784    */ 
    5885    const TYPE_PRIVMSG = 'privmsg'; 
     
    6087    /** 
    6188    * Notice message 
     89    * 
     90    * @const string 
    6291    */ 
    6392    const TYPE_NOTICE = 'notice'; 
     
    6594    /** 
    6695    * Pong message 
     96    * 
     97    * @const string 
    6798    */ 
    6899    const TYPE_PONG = 'pong'; 
     
    70101    /** 
    71102    * CTCP ACTION command 
     103    * 
     104    * @const string 
    72105    */ 
    73106    const TYPE_ACTION = 'action'; 
     
    249282    public function getSource() 
    250283    { 
    251         if ($this->type == Phergie_Event_Request::TYPE_PRIVMSG 
    252             && $this->arguments[0][0] == '#') { 
     284        if ($this->arguments[0][0] == '#') { 
    253285            return $this->arguments[0]; 
    254286        } 
     
    257289 
    258290    /** 
     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    /** 
    259301    * Returns whether or not the event originated from a user. 
    260302    * 
  • trunk/Phergie/Plugin/Abstract/AdminCommand.php

    r59 r60  
    8282            $ini = $this->getIni('admins', 'admincommand'); 
    8383        } 
    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); 
    90104            } 
    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]) . '$/'; 
    103106        } 
    104107    } 
     
    114117    { 
    115118        $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        } 
    118124        // 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; 
    129129        } 
    130130        return false; 
     
    141141        if ($this->fromAdmin()) { 
    142142            $target = $this->event->getArgument(0); 
    143             $exp = $target[0][0] == '#' ? 
     143            $exp = $target[0] == '#' ? 
    144144                '/^' . $this->getIni('nick') . '\s*:?\s+(.*)$/' : 
    145145                '/^(?:' . $this->getIni('nick') . '\s*:?\s+)?(.*)$/'; 
  • trunk/Phergie/Plugin/Abstract/Command.php

    r59 r60  
    4545                $name = $method->getName(); 
    4646                if (strpos($name, 'onDo') === 0) { 
    47                     $this->methods[substr($name, 4)] = $method->getNumberOfRequiredParameters(); 
     47                    $this->methods[strtolower(substr($name, 4))] = 
     48                        $method->getNumberOfRequiredParameters(); 
    4849                } 
    4950            } 
     
    6061                    $this->$method(); 
    6162                } 
    62             } elseif ($this->methods[$command] <= count($params))
     63            } else
    6364                $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                } 
    6568            } 
    6669        } 
  • trunk/Phergie/Plugin/Acronym.php

    r59 r60  
    4646    { 
    4747        $limit = $this->getIni('limit'); 
    48         if ($limit < 0) { 
     48        if ($limit < 0 || $limit === null) { 
    4949            $this->limit = 5; 
    5050        } else { 
     
    6060    * @return void 
    6161    */ 
    62     protected function _randomAction($target) 
     62    protected function randomAction($target) 
    6363    { 
    6464        do { 
     
    8787        $message = $this->event->getArgument(1); 
    8888 
    89         if (!preg_match('/^(?:[^\s:]+\s*:?\s*)?((?:[A-Z]\.?){2,})\?$/', $message, $acronym)) { 
     89        if (!preg_match('/((?:[A-Z]\.?){2,})\?/', $message, $acronym)) { 
    9090            return; 
    9191        } 
     
    110110        $contents = @file_get_contents($url, false, $context); 
    111111 
    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) { 
    113115            $this->randomAction($target); 
    114116        } else { 
     
    130132            } while (($this->limit == 0 || count($matches) < $this->limit) && $count == 1); 
    131133 
    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); 
    138136        } 
    139137    } 
  • trunk/Phergie/Plugin/Autojoin.php

    r59 r60  
    2323    public function onResponse() 
    2424    { 
    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; 
    3033        } 
    3134    } 
  • trunk/Phergie/Plugin/Daddy.php

    r59 r60  
    2222        $bot = $this->getIni('nick'); 
    2323        $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() . '!'); 
    2626        } 
    2727    } 
  • trunk/Phergie/Plugin/Debug.php

    r59 r60  
    2121    { 
    2222        $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); 
    2424    } 
    2525} 
  • trunk/Phergie/Plugin/Drink.php

    r59 r60  
    1818class Phergie_Plugin_Drink extends Phergie_Plugin_Abstract_Command 
    1919{ 
     20    /** 
     21    * Indicates that a local directory is required for this plugin 
     22    * 
     23    * @var bool 
     24    */ 
     25    protected $needsDir = true; 
     26 
    2027    /** 
    2128    * PDO resource for a SQLite database containing the drinks 
     
    7683 
    7784    /** 
    78     * Determines if a table does not exist or is empty. 
    79     * 
    80     * @param string $name Table name 
    81     * @return bool TRUE if the table does not exist or is empty, FALSE 
    82     *              otherwise 
    83     */ 
    84     private function needTable($name) 
    85     { 
    86         $table = $this->db 
    87             ->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->db 
    95             ->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 table 
    103     * @param array $names List of drink names 
    104     * @return void 
    105     */ 
    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 table 
    130     * 
    131     * @string $table Name of the table 
    132     * @return string Value of the name column for the selected record 
    133     */ 
    134     private function getRandomRecord($table) 
    135     { 
    136         return $this->db 
    137             ->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 be 
    143     * displayed correctly 
    144     * 
    145     * @param string $name Value to check 
    146     */ 
    147     private function hasBadChars($name) 
    148     { 
    149         return (max(array_map('ord', str_split($name))) > 126); 
    150     } 
    151  
    152     /** 
    15385    * Connects to the database and populates tables where needed. 
    15486    * 
     
    16294        } 
    16395 
    164         // Create the plugin directory if needed 
    165         $dir = dirname(__FILE__) . '/' . $this->getName(); 
    166         if (!file_exists($dir)) { 
    167             mkdir($dir); 
    168         } 
    169  
    17096        // Initialize the database connection 
    171         $this->db = new PDO('sqlite:' . $dir . '/drink.db'); 
     97        $this->db = new PDO('sqlite:' . $this->dir . 'drink.db'); 
    17298 
    17399        // Populate the database if necessary 
     
    224150            $contents = @file_get_contents('http://www.energyfiend.com/huge-caffeine-database/'); 
    225151            if ($contents) { 
     152                // List of drinks to filter out 
     153                $filter = array( 
     154                    'tea', 
     155                    'coffee', 
     156                    'starbucks' 
     157                ); 
    226158                $start = stripos($contents, 'id="caffeinedb"'); 
    227159                $end = stripos($contents, '</table>', $start); 
     
    231163                foreach ($matches[2] as $name) { 
    232164                    $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)) { 
    234166                        $names[] = $name; 
    235167                    } 
     
    250182            } 
    251183        } 
     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); 
    252257    } 
    253258 
     
    290295                $text = 'pours '.$target.' a cup of '.$drink.' tea'; 
    291296            } 
    292             $this->doAction($this->event->getSource(), $text); 
     297            $this->doAction($this->event->getSource(), $text . '.'); 
    293298        } 
    294299    } 
  • trunk/Phergie/Plugin/Karma.php

    r59 r60  
    1313class Phergie_Plugin_Karma extends Phergie_Event_Handler 
    1414{ 
     15    /** 
     16    * Indicates that a local directory is required for this plugin 
     17    * 
     18    * @var bool 
     19    */ 
     20    protected $needsDir = true; 
     21 
    1522    /** 
    1623    * Stores the SQLite object 
     
    5764        ( 
    5865            'pi' => 'pi has karma of ' . M_PI, 
    59             'chucknorris' => 'pi has karma of Warning: Integer out of range', 
     66            'chucknorris' => 'chucknorris has karma of Warning: Integer out of range', 
    6067            'c' => 'c has karma of 299 792 458 m/s', 
    6168            'e' => 'e has karma of ' . M_E, 
    6269            'euler' => 'euler has karma of ' . M_EULER, 
    6370            'mole' => 'mole has karma of 6.02214e23 molecules', 
     71            'avagadro' => 'avagadro has karma of 6.02214e23 molecules', 
    6472            'spoon' => 'spoon has no karma. There is no spoon.', 
    65             strtolower($this->getIni('nick')) => $this->getIni('static'), 
    6673            'mc^2' => 'mc^2 has karma of e', 
    6774            'mc2' => 'mc2 has karma of e', 
    6875            'mc²' => 'mc² has karma of e', 
    6976        ); 
     77 
     78        $static = $this->getIni('static'); 
     79        if ($static) { 
     80            $this->fixedKarma[strtolower($this->getIni('nick'))] = $static; 
     81        } 
    7082 
    7183        // Check to ensure that the SQLite extension is loaded 
     
    7486        } 
    7587 
    76         // Create a directory to contain the database if needed 
    77         if (!file_exists(dirname(__FILE__).'/Karma')) 
    78             mkdir(dirname(__FILE__).'/Karma'); 
    79  
    8088        // Load or initialize the database 
    81         $db = dirname(__FILE__).'/Karma/karma.db'; 
     89        $db = $this->dir . 'karma.db'; 
    8290        if (!file_exists($db)) { 
    8391            $this->db = new SQLiteDatabase($db); 
     
    108116            return; 
    109117        } 
    110         $target = $this->getSource(); 
     118        $source = $this->event->getSource(); 
    111119        $message = $this->event->getArgument(1); 
    112120            // Karma status request 
    113121        if (preg_match('#^karma (\S+?)$#i', $message, $m)) { 
     122            $term = strtolower($m[1]); 
    114123            // 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]); 
    117126                return; 
    118127            } 
    119128            // 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); 
    121130            if ($res->numRows() && $res->column(0) != 0) { 
    122131                    $this->doPrivmsg($source, $m[1].' has karma of '.$res->column(0)); 
  • trunk/Phergie/Plugin/Lart.php

    r59 r60  
    1414{ 
    1515    /** 
     16    * Indicates that a local directory is required for this plugin 
     17    * 
     18    * @var bool 
     19    */ 
     20    protected $needsDir = true; 
     21 
     22    /** 
    1623    * Date string indicating the last time the cache was emptied 
    1724    * 
     
    2633    */ 
    2734    protected $cache; 
     35 
     36    /** 
     37    * PDO instance for the database 
     38    * 
     39    * @var PDO 
     40    */ 
     41    protected $db; 
    2842 
    2943    /** 
     
    6276        } 
    6377 
    64         // Create the plugin directory if needed 
    65         $dir = dirname(__FILE__) . '/' . $this->getName(); 
    66         if (!file_exists($dir)) { 
    67             mkdir($dir); 
    68         } 
    69  
    7078        // 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); 
    7381        $this->db = new PDO('sqlite:' . $db); 
    7482 
    7583        // Create database tables if necessary 
    7684        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            '); 
    7989        } 
    8090 
     
    141151                        } 
    142152                        $this->doAction( 
    143                             $this->event->getSource(), 
     153                            $this->event->getArgument(0), 
    144154                            'puts her hands over her ears and cries, "Stop confusing me!"' 
    145155                        ); 
     156                        return; 
    146157                    } 
    147158                    $definition = $redirect; 
     
    182193        } 
    183194 
    184         if (preg_match('/^(' . $nick . ': )?(.*) is (.*)$/U', $message, $match)) { 
     195        if (preg_match('/^(' . $nick . ':?\s*)?(.*) is (.*)$/i', $message, $match)) { 
    185196            list (, $address, $name, $definition) = $match; 
    186             if (!empty($nick) || $source[0] != '#') { 
     197            if (empty ($address) || $source[0] == '#') { 
    187198                $name = strtolower($name); 
    188199                $this->replace->execute(array(':name' => $name, ':definition' => $definition)); 
    189200                $this->c