enanobot.php
changeset 6 a802bb5b3e08
parent 4 8f62a406d9d9
child 7 1d6e762433fe
equal deleted inserted replaced
5:fcc1eac04772 6:a802bb5b3e08
     1 <?php
     1 <?php
     2 define('LIBIRC_DEBUG', '');
     2 
       
     3 /**
       
     4  * EnanoBot - the Enano CMS IRC logging and help automation bot
       
     5  * GPL and no warranty, see the LICENSE file for more info
       
     6  */
       
     7 
       
     8 // define('LIBIRC_DEBUG', '');
       
     9 
     3 require('libirc.php');
    10 require('libirc.php');
     4 require('config.php');
    11 require('config.php');
     5 
    12 
     6 @ini_set('display_errors', 'on');
    13 @ini_set('display_errors', 'on');
     7 
    14 
    59   return $q;
    66   return $q;
    60 }
    67 }
    61 
    68 
    62 mysql_reconnect();
    69 mysql_reconnect();
    63 
    70 
       
    71 $libirc_channels = array();
       
    72 
    64 $irc = new Request_IRC('irc.freenode.net');
    73 $irc = new Request_IRC('irc.freenode.net');
    65 $irc->connect($nick, $user, $name, $pass);
    74 $irc->connect($nick, $user, $name, $pass);
    66 $irc->set_privmsg_handler('enanobot_privmsg_event');
    75 $irc->set_privmsg_handler('enanobot_privmsg_event');
    67 $enano = $irc->join('#enano', 'enanobot_channel_event_enano');
    76 
    68 $enano_dev = $irc->join('#enano-dev', 'enanobot_channel_event_enanodev');
    77 foreach ( $channels as $channel )
    69 $irc->privmsg('ChanServ', 'OP #enano EnanoBot');
    78 {
    70 $irc->privmsg('ChanServ', 'OP #enano-dev EnanoBot');
    79   $libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event');
       
    80   $channel_clean = preg_replace('/^[#&]/', '', $channel);
       
    81   $libirc_channels[$channel_clean] =& $libirc_channels[$channel];
       
    82   $irc->privmsg('ChanServ', "OP $channel $nick");
       
    83 }
    71 
    84 
    72 $irc->event_loop();
    85 $irc->event_loop();
    73 $irc->close();
    86 $irc->close();
    74 mysql_close($mysql_conn);
    87 mysql_close($mysql_conn);
    75 
    88 
    76 function enanobot_channel_event_enano($sockdata, $chan)
    89 function enanobot_channel_event($sockdata, $chan)
    77 {
    90 {
    78   global $irc, $nick, $mysql_conn, $privileged_list;
    91   global $irc, $nick, $mysql_conn, $privileged_list;
    79   $sockdata = trim($sockdata);
    92   $sockdata = trim($sockdata);
    80   $message = Request_IRC::parse_message($sockdata);
    93   $message = Request_IRC::parse_message($sockdata);
       
    94   $channelname = $chan->get_channel_name();
    81   enanobot_log_message($chan, $message);
    95   enanobot_log_message($chan, $message);
    82   switch ( $message['action'] )
    96   switch ( $message['action'] )
    83   {
    97   {
    84     case 'JOIN':
    98     case 'JOIN':
    85       // if a known op joins the channel, send mode +o
    99       // if a known op joins the channel, send mode +o
    86       if ( in_array($message['nick'], $privileged_list) )
   100       if ( in_array($message['nick'], $privileged_list) )
    87       {
   101       {
    88         $chan->parent->put("MODE #enano +o {$message['nick']}\r\n");
   102         $chan->parent->put("MODE $channelname +o {$message['nick']}\r\n");
    89       }
   103       }
    90       break;
       
    91     case 'PRIVMSG':
       
    92       enanobot_process_channel_message($sockdata, $chan, $message);
       
    93       break;
       
    94   }
       
    95 }
       
    96 
       
    97 function enanobot_channel_event_enanodev($sockdata, $chan)
       
    98 {
       
    99   global $irc, $privileged_list;
       
   100   $sockdata = trim($sockdata);
       
   101   $message = Request_IRC::parse_message($sockdata);
       
   102   enanobot_log_message($chan, $message);
       
   103   switch ( $message['action'] )
       
   104   {
       
   105     case 'JOIN':
       
   106       // if dandaman32 joins the channel, use mode +o
       
   107       if ( in_array($message['nick'], $privileged_list) )
       
   108         $chan->parent->put("MODE #enano-dev +o {$message['nick']}\r\n");
       
   109       break;
   104       break;
   110     case 'PRIVMSG':
   105     case 'PRIVMSG':
   111       enanobot_process_channel_message($sockdata, $chan, $message);
   106       enanobot_process_channel_message($sockdata, $chan, $message);
   112       break;
   107       break;
   113   }
   108   }
   227   }
   222   }
   228 }
   223 }
   229 
   224 
   230 function enanobot_privmsg_event($message)
   225 function enanobot_privmsg_event($message)
   231 {
   226 {
   232   global $privileged_list, $irc;
   227   global $privileged_list, $irc, $nick;
   233   static $part_cache = array();
   228   static $part_cache = array();
   234   if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' )
   229   if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' )
   235   {
   230   {
   236     foreach ( $irc->channels as $channel )
   231     foreach ( $irc->channels as $channel )
   237     {
   232     {
   238       $part_cache[] = array($channel->get_channel_name(), $channel->get_handler());
   233       $part_cache[] = array($channel->get_channel_name(), $channel->get_handler());
   239       $channel->msg("I've received a request to stop logging messages and responding to requests from {$message['nick']}. Don't forget to unsuspend me with /msg EnanoBot Resume when finished.", true);
   234       $channel->msg("I've received a request to stop logging messages and responding to requests from {$message['nick']}. Don't forget to unsuspend me with /msg $nick Resume when finished.", true);
   240       $channel->part("Logging and presence suspended by {$message['nick']}", true);
   235       $channel->part("Logging and presence suspended by {$message['nick']}", true);
   241     }
   236     }
   242   }
   237   }
   243   else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' )
   238   else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' )
   244   {
   239   {
   255   else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Shutdown' && $message['action'] == 'PRIVMSG' )
   250   else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Shutdown' && $message['action'] == 'PRIVMSG' )
   256   {
   251   {
   257     $irc->close("Remote bot shutdown ordered by {$message['nick']}", true);
   252     $irc->close("Remote bot shutdown ordered by {$message['nick']}", true);
   258     return 'BREAK';
   253     return 'BREAK';
   259   }
   254   }
   260   else if ( in_array($message['nick'], $privileged_list) && preg_match('/^\!echo-enano /', $message['message']) )
   255   else if ( in_array($message['nick'], $privileged_list) && preg_match("/^\!echo-([^\007, \r\n\a\t]+) /", $message['message'], $match) )
   261   {
   256   {
   262     global $enano;
   257     global $libirc_channels;
   263     $enano->msg(preg_replace('/^\!echo-enano /', '', $message['message']), true);
   258     $channel_name =& $match[1];
   264   }
   259     if ( isset($libirc_channels[$channel_name]) && is_object($libirc_channels[$channel_name]) )
   265 }
   260     {
   266 
   261       $libirc_channels[$channel_name]->msg(preg_replace("/^\!echo-([^\007, \r\n\a\t]+) /", '', $message['message']), true);
       
   262     }
       
   263   }
       
   264 }
       
   265