Changeset 102

Show
Ignore:
Timestamp:
03/01/08 23:10:48 (5 years ago)
Author:
Seldaek
Message:

* Forces the bot to reload ini values when it reconnects

Location:
trunk/Phergie
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/Phergie/Bot.php

    r99 r102  
    7272 
    7373/** 
    74 * Obtain and validate the contents of the configuration file 
    75 */ 
    76 $required = array('server', 'username', 'nick'); 
    77 $config = @parse_ini_file(PHERGIE_DIR . 'Phergie' . DIRECTORY_SEPARATOR . $ini); 
     74 * Start a runtime loop, if the bot disconnects and reconnects it will reload 
     75 * all settings from the ini file, allowing you to modify it without really shutting 
     76 * it down 
     77 */ 
     78while (true) { 
    7879 
    79 if (count($config) == 0) { 
    80     echo 'Configuration file inaccessible or empty: ' . $ini . "\n"; 
    81     return; 
    82 } 
     80        /** 
     81        * Obtain and validate the contents of the configuration file 
     82        */ 
     83        $required = array('server', 'username', 'nick'); 
     84        $config = @parse_ini_file(PHERGIE_DIR . 'Phergie' . DIRECTORY_SEPARATOR . $ini); 
    8385 
    84 foreach ($required as $setting) { 
    85     if (!isset($config[$setting]) || empty($config[$setting])) { 
    86         echo 'Required configuration setting missing: ' . $setting . "\n"; 
    87         return; 
    88     } 
    89 } 
     86        if (count($config) == 0) { 
     87            echo 'Configuration file inaccessible or empty: ' . $ini . "\n"; 
     88            return; 
     89        } 
    9090 
    91 unset ($required); 
     91        foreach ($required as $setting) { 
     92            if (!isset($config[$setting]) || empty($config[$setting])) { 
     93                echo 'Required configuration setting missing: ' . $setting . "\n"; 
     94                return; 
     95            } 
     96        } 
    9297 
    93 /** 
    94 * Configure the client 
    95 */ 
    96 if (isset($config['driver'])) { 
    97     $driver = ucfirst(strtolower($config['driver'])); 
    98 } else { 
    99     $driver = 'Streams'; 
    100 } 
    101 require_once 'Phergie/Driver/' . $driver . '.php'; 
    102 $class = 'Phergie_Driver_' . $driver; 
    103 $client = new $class(); 
     98        unset ($required); 
    10499 
    105 foreach ($config as $setting => $value) { 
    106     $client->setIni($setting, $value); 
    107 } 
     100        /** 
     101        * Configure the client 
     102        */ 
     103        if (isset($config['driver'])) { 
     104            $driver = ucfirst(strtolower($config['driver'])); 
     105        } else { 
     106            $driver = 'Streams'; 
     107        } 
     108        require_once 'Phergie/Driver/' . $driver . '.php'; 
     109        $class = 'Phergie_Driver_' . $driver; 
     110        $client = new $class(); 
    108111 
    109 unset ($setting, $value, $driver, $class); 
     112        foreach ($config as $setting => $value) { 
     113            $client->setIni($setting, $value); 
     114        } 
    110115 
    111 /** 
    112 * Determine which plugins should be loaded 
    113 */ 
    114 $all = true; 
    115 $include = array(); 
    116 if (isset($config['plugins']) 
    117     && preg_match('/(all|none)(?: except (.+))?/ADi', $config['plugins'], $match)) { 
    118     $all = $match[1] != 'none'; 
    119     if (isset($match[2])) { 
    120         $include = array_map('strtolower', preg_split('/[, ]+/', $match[2])); 
    121     } 
    122 } 
     116        unset ($setting, $value, $driver, $class); 
    123117 
    124 unset ($config); 
     118        /** 
     119        * Determine which plugins should be loaded 
     120        */ 
     121        $all = true; 
     122        $include = array(); 
     123        if (isset($config['plugins']) 
     124            && preg_match('/(all|none)(?: except (.+))?/ADi', $config['plugins'], $match)) { 
     125            $all = $match[1] != 'none'; 
     126            if (isset($match[2])) { 
     127                $include = array_map('strtolower', preg_split('/[, ]+/', $match[2])); 
     128            } 
     129        } 
    125130 
    126 /** 
    127 * Set up plugins 
    128 */ 
    129 $iterator = new DirectoryIterator(PHERGIE_DIR . '/Phergie/Plugin'); 
    130 $plugins = array(); 
    131 foreach ($iterator as $entry) { 
    132     if ($iterator->isFile()) { 
    133         $name = substr($entry, 0, -4); 
    134         if ($all xor in_array(strtolower($name), $include)) { 
    135             $plugins[$name] = true; 
    136         } else { 
    137                 $plugins[$name] = false; 
    138         } 
    139     } 
    140 } 
     131        unset ($config); 
    141132 
    142 ksort($plugins); 
     133        /** 
     134        * Set up plugins 
     135        */ 
     136        $iterator = new DirectoryIterator(PHERGIE_DIR . '/Phergie/Plugin'); 
     137        $plugins = array(); 
     138        foreach ($iterator as $entry) { 
     139            if ($iterator->isFile()) { 
     140                $name = substr($entry, 0, -4); 
     141                if ($all xor in_array(strtolower($name), $include)) { 
     142                    $plugins[$name] = true; 
     143                } else { 
     144                        $plugins[$name] = false; 
     145                } 
     146            } 
     147        } 
    143148 
    144 unset ($iterator, $entry, $name, $all, $include); 
     149        ksort($plugins); 
    145150 
    146 foreach ($plugins as $plugin=>$enabled) { 
    147     require_once 'Phergie/Plugin/' . $plugin . '.php'; 
    148     $class = 'Phergie_Plugin_' . $plugin; 
    149     if (call_user_func(array($class, 'checkDependencies'), $plugins)) { 
    150         $instance = new $class($client); 
    151         $instance->enabled = $enabled; 
    152         $client->addPlugin($instance); 
    153         $client->debug('Loaded ' . $plugin . ($enabled ? '':' [Disabled]')); 
    154     } else { 
    155         $client->debug('Unable to load ' . $plugin); 
    156     } 
    157 } 
     151        unset ($iterator, $entry, $name, $all, $include); 
    158152 
    159 unset ($plugins, $plugin, $class, $instance); 
     153        foreach ($plugins as $plugin=>$enabled) { 
     154            require_once 'Phergie/Plugin/' . $plugin . '.php'; 
     155            $class = 'Phergie_Plugin_' . $plugin; 
     156            if (call_user_func(array($class, 'checkDependencies'), $plugins)) { 
     157                $instance = new $class($client); 
     158                $instance->enabled = $enabled; 
     159                $client->addPlugin($instance); 
     160                $client->debug('Loaded ' . $plugin . ($enabled ? '':' [Disabled]')); 
     161            } else { 
     162                $client->debug('Unable to load ' . $plugin); 
     163            } 
     164        } 
    160165 
    161 /** 
    162 * Execute the event handling loop for the client 
    163 */ 
    164 while(true) { 
     166        unset ($plugins, $plugin, $class, $instance); 
     167 
     168        /** 
     169        * Execute the event handling loop for the client 
     170        */ 
    165171        $state = $client->run(); 
    166172 
  • trunk/Phergie/phergie.ini

    r99 r102  
    3131; keepalive : 
    3232;    boolean flag indicating whether or not the bot should reconnect when it 
    33 ;    gets disconnected from the server, defaults to false. 
     33;    gets disconnected from the server, defaults to false if not set, but 
     34;    setting it to true is recommended. 
    3435;    if this is set to true, you should set timeout value to 6 or above. 
     36;    if not, timeout would better be set to zero or a high number such as 15. 
    3537keepalive = true 
    3638 
     
    195197 
    196198;----------------------------------------------------------------------------- 
    197 ; Weather  
     199; Weather 
    198200;----------------------------------------------------------------------------- 
    199201 
     
    201203;    Partner ID provided by weather.com 
    202204;    see http://www.weather.com/services/xmloap.html for more details 
    203 weather.partner_id =  
     205weather.partner_id = 
    204206 
    205207; weather.license_key 
    206208;    License Key provided by weather.com 
    207209;    see http://www.weather.com/services/xmloap.html for more details 
    208 weather.license_key =  
     210weather.license_key =