enanobot.php
changeset 30 2cfcd2801e5a
parent 23 df31a3872d19
child 31 d75124700259
equal deleted inserted replaced
29:300f673fbbdc 30:2cfcd2801e5a
    96 $libirc_channels = array();
    96 $libirc_channels = array();
    97 
    97 
    98 $irc = new Request_IRC($server);
    98 $irc = new Request_IRC($server);
    99 $irc->connect($nick, $user, $name, $pass);
    99 $irc->connect($nick, $user, $name, $pass);
   100 $irc->set_privmsg_handler('enanobot_privmsg_event');
   100 $irc->set_privmsg_handler('enanobot_privmsg_event');
       
   101 $irc->set_timeout_handlers(false, 'enanobot_timeout_event');
   101 
   102 
   102 foreach ( $channels as $channel )
   103 foreach ( $channels as $channel )
   103 {
   104 {
   104   $libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event');
   105   $libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event');
   105   $channel_clean = preg_replace('/^[#&]/', '', $channel);
   106   $channel_clean = preg_replace('/^[#&]/', '', $channel);
   189   {
   190   {
   190     eval(eb_fetch_hook('event_other'));
   191     eval(eb_fetch_hook('event_other'));
   191   }
   192   }
   192 }
   193 }
   193 
   194 
       
   195 function enanobot_timeout_event($irc)
       
   196 {
       
   197   // uh-oh.
       
   198   $irc->close();
       
   199   if ( defined('LIBIRC_DEBUG') )
       
   200   {
       
   201     $now = date('r');
       
   202     echo "!!! [$now] Connection timed out; waiting 10 seconds and reconnecting\n";
       
   203   }
       
   204   
       
   205   // re-init
       
   206   global $server, $nick, $user, $name, $pass, $channels, $libirc_channels;
       
   207   
       
   208   // wait until we can get into the server
       
   209   while ( true )
       
   210   {
       
   211     sleep(10);
       
   212     if ( defined('LIBIRC_DEBUG') )
       
   213     {
       
   214       $now = date('r');
       
   215       echo "... [$now] Attempting reconnect\n";
       
   216     }
       
   217     $conn = @fsockopen($server, 6667, $errno, $errstr, 5);
       
   218     if ( $conn )
       
   219     {
       
   220       if ( defined('LIBIRC_DEBUG') )
       
   221       {
       
   222         $now = date('r');
       
   223         echo "!!! [$now] Reconnection succesful, ghosting old login\n";
       
   224       }
       
   225       fclose($conn);
       
   226       break;
       
   227     }
       
   228     else
       
   229     {
       
   230       if ( defined('LIBIRC_DEBUG') )
       
   231       {
       
   232         $now = date('r');
       
   233         echo "!!! [$now] Still waiting for connection to come back up\n";
       
   234       }
       
   235     }
       
   236   }
       
   237   
       
   238   $libirc_channels = array();
       
   239   
       
   240   // we were able to get back in; ask NickServ to GHOST the old nick
       
   241   $irc->connect("$nick`gh", $user, $name, false);
       
   242   $irc->privmsg('NickServ', "GHOST $nick $pass");
       
   243   $irc->change_nick($nick, $pass);
       
   244   
       
   245   foreach ( $channels as $channel )
       
   246   {
       
   247     $libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event');
       
   248     $channel_clean = preg_replace('/^[#&]/', '', $channel);
       
   249     $libirc_channels[$channel_clean] =& $libirc_channels[$channel];
       
   250     $irc->privmsg('ChanServ', "OP $channel $nick");
       
   251   }
       
   252 }
       
   253 
   194 if ( $_shutdown )
   254 if ( $_shutdown )
   195 {
   255 {
   196   exit(2);
   256   exit(2);
   197 }
   257 }