enanobot.php
changeset 23 df31a3872d19
parent 13 f073c94a1477
child 30 2cfcd2801e5a
equal deleted inserted replaced
22:04c2f743c6ec 23:df31a3872d19
    72 }
    72 }
    73 
    73 
    74 require('libirc.php');
    74 require('libirc.php');
    75 require('hooks.php');
    75 require('hooks.php');
    76 require('config.php');
    76 require('config.php');
       
    77 require('database.php');
    77 
    78 
    78 @ini_set('display_errors', 'on');
    79 @ini_set('display_errors', 'on');
    79 error_reporting(E_ALL);
    80 error_reporting(E_ALL);
    80 
    81 
    81 // load modules
    82 // load modules
    86   {
    87   {
    87     require($modulefile);
    88     require($modulefile);
    88   }
    89   }
    89 }
    90 }
    90 
    91 
    91 $mysql_conn = false;
    92 mysql_reconnect();
    92 $doctor = array();
       
    93 
    93 
    94 function mysql_reconnect()
    94 eval(eb_fetch_hook('startup_early'));
    95 {
       
    96   global $mysql_conn, $mysql_host, $mysql_user, $mysql_pass, $mysql_dbname;
       
    97   if ( $mysql_conn )
       
    98   {
       
    99     @mysql_close($mysql_conn);
       
   100     if ( defined('LIBIRC_DEBUG') )
       
   101     {
       
   102       echo "< > Reconnecting to MySQL\n";
       
   103     }
       
   104   }
       
   105   // connect to MySQL
       
   106   $mysql_conn = @mysql_connect($mysql_host, $mysql_user, $mysql_pass);
       
   107   if ( !$mysql_conn )
       
   108   {
       
   109     $m_e = mysql_error();
       
   110     echo "Error connecting to MySQL: $m_e\n";
       
   111     exit(1);
       
   112   }
       
   113   $q = @mysql_query("USE `$mysql_dbname`;", $mysql_conn);
       
   114   if ( !$q )
       
   115   {
       
   116     $m_e = mysql_error();
       
   117     echo "Error selecting database: $m_e\n";
       
   118     exit(1);
       
   119   }
       
   120 }
       
   121 
       
   122 function eb_mysql_query($sql, $conn = false)
       
   123 {
       
   124   global $mysql_conn, $irc;
       
   125   $m_et = false;
       
   126   while ( true )
       
   127   {
       
   128     $q = mysql_query($sql, $mysql_conn);
       
   129     if ( !$q )
       
   130     {
       
   131       $m_e = mysql_error();
       
   132       if ( strpos($m_e, 'gone away') && !$m_et )
       
   133       {
       
   134         mysql_reconnect();
       
   135         continue;
       
   136       }
       
   137       $m_et = true;
       
   138       $irc->close("MySQL query error: $m_e");
       
   139       exit(1);
       
   140     }
       
   141     break;
       
   142   }
       
   143   return $q;
       
   144 }
       
   145 
       
   146 mysql_reconnect();
       
   147 
    95 
   148 $libirc_channels = array();
    96 $libirc_channels = array();
   149 
    97 
   150 $irc = new Request_IRC($server);
    98 $irc = new Request_IRC($server);
   151 $irc->connect($nick, $user, $name, $pass);
    99 $irc->connect($nick, $user, $name, $pass);
   224       $GLOBALS[$chan_name]->msg("Bot resumed by {$message['nick']}.", true);
   172       $GLOBALS[$chan_name]->msg("Bot resumed by {$message['nick']}.", true);
   225       $irc->privmsg('ChanServ', "OP {$chan_data[0]} $nick");
   173       $irc->privmsg('ChanServ', "OP {$chan_data[0]} $nick");
   226     }
   174     }
   227     $part_cache = array();
   175     $part_cache = array();
   228   }
   176   }
   229   else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Shutdown' && $message['action'] == 'PRIVMSG' )
   177   else if ( in_array($message['nick'], $privileged_list) && preg_match('/^Shutdown(?: (.+))$/i', $message['message'], $match) && $message['action'] == 'PRIVMSG' )
   230   {
   178   {
   231     $GLOBALS['_shutdown'] = true;
   179     $GLOBALS['_shutdown'] = true;
   232     $irc->close("Remote bot shutdown ordered by {$message['nick']}", true);
   180     $quitmessage = empty($match[1]) ? "Remote bot shutdown ordered by {$message['nick']}" : $match[1];
       
   181     $irc->close($quitmessage, true);
   233     return 'BREAK';
   182     return 'BREAK';
   234   }
   183   }
   235   else if ( $message['action'] == 'PRIVMSG' )
   184   else if ( $message['action'] == 'PRIVMSG' )
   236   {
   185   {
   237     eval(eb_fetch_hook('event_privmsg'));
   186     eval(eb_fetch_hook('event_privmsg'));