enanobot.php
changeset 8 0acb8d9a3194
parent 7 1d6e762433fe
child 9 4192954c29d1
equal deleted inserted replaced
7:1d6e762433fe 8:0acb8d9a3194
     1 <?php
     1 <?php
     2 
       
     3 /**
     2 /**
     4  * EnanoBot - the Enano CMS IRC logging and help automation bot
     3  * EnanoBot - the Enano CMS IRC logging and help automation bot
     5  * GPL and no warranty, see the LICENSE file for more info
     4  * GPL and no warranty, see the LICENSE file for more info
     6  */
     5  */
     7 
     6 
    51 EOF;
    50 EOF;
    52     exit(1);
    51     exit(1);
    53   }
    52   }
    54 }
    53 }
    55 
    54 
       
    55 $censored_words = array('cock', 'fuck', 'cuck', 'funt', 'cunt', 'bitch');
       
    56 $_shutdown = false;
       
    57 
       
    58 function eb_censor_words($text)
       
    59 {
       
    60   // return $text;
       
    61   
       
    62   global $censored_words;
       
    63   foreach ( $censored_words as $word )
       
    64   {
       
    65     $replacement = substr($word, 0, 1) . preg_replace('/./', '*', substr($word, 1));
       
    66     while ( stristr($text, $word) )
       
    67     {
       
    68       $text = preg_replace("/$word/i", $replacement, $text);
       
    69     }
       
    70   }
       
    71   return $text;
       
    72 }
       
    73 
    56 require('libirc.php');
    74 require('libirc.php');
       
    75 require('hooks.php');
    57 require('config.php');
    76 require('config.php');
    58 
    77 
    59 @ini_set('display_errors', 'on');
    78 @ini_set('display_errors', 'on');
       
    79 error_reporting(E_ALL);
       
    80 
       
    81 // load modules
       
    82 foreach ( $modules as $module )
       
    83 {
       
    84   $modulefile = "modules/$module.php";
       
    85   if ( file_exists($modulefile) )
       
    86   {
       
    87     require($modulefile);
       
    88   }
       
    89 }
    60 
    90 
    61 $mysql_conn = false;
    91 $mysql_conn = false;
       
    92 $doctor = array();
    62 
    93 
    63 function mysql_reconnect()
    94 function mysql_reconnect()
    64 {
    95 {
    65   global $mysql_conn, $mysql_host, $mysql_user, $mysql_pass, $mysql_dbname;
    96   global $mysql_conn, $mysql_host, $mysql_user, $mysql_pass, $mysql_dbname;
    66   if ( $mysql_conn )
    97   if ( $mysql_conn )
   114 
   145 
   115 mysql_reconnect();
   146 mysql_reconnect();
   116 
   147 
   117 $libirc_channels = array();
   148 $libirc_channels = array();
   118 
   149 
   119 $irc = new Request_IRC('irc.freenode.net');
   150 $irc = new Request_IRC($server);
   120 $irc->connect($nick, $user, $name, $pass);
   151 $irc->connect($nick, $user, $name, $pass);
   121 $irc->set_privmsg_handler('enanobot_privmsg_event');
   152 $irc->set_privmsg_handler('enanobot_privmsg_event');
   122 
   153 
   123 foreach ( $channels as $channel )
   154 foreach ( $channels as $channel )
   124 {
   155 {
   136 {
   167 {
   137   global $irc, $nick, $mysql_conn, $privileged_list;
   168   global $irc, $nick, $mysql_conn, $privileged_list;
   138   $sockdata = trim($sockdata);
   169   $sockdata = trim($sockdata);
   139   $message = Request_IRC::parse_message($sockdata);
   170   $message = Request_IRC::parse_message($sockdata);
   140   $channelname = $chan->get_channel_name();
   171   $channelname = $chan->get_channel_name();
   141   enanobot_log_message($chan, $message);
   172   
       
   173   eval(eb_fetch_hook('event_raw_message'));
       
   174   
   142   switch ( $message['action'] )
   175   switch ( $message['action'] )
   143   {
   176   {
   144     case 'JOIN':
   177     case 'JOIN':
   145       // if a known op joins the channel, send mode +o
   178       eval(eb_fetch_hook('event_join'));
   146       if ( in_array($message['nick'], $privileged_list) )
   179       break;
   147       {
   180     case 'PART':
   148         $chan->parent->put("MODE $channelname +o {$message['nick']}\r\n");
   181       eval(eb_fetch_hook('event_part'));
   149       }
       
   150       break;
   182       break;
   151     case 'PRIVMSG':
   183     case 'PRIVMSG':
   152       enanobot_process_channel_message($sockdata, $chan, $message);
   184       enanobot_process_channel_message($sockdata, $chan, $message);
   153       break;
   185       break;
   154   }
   186   }
   156 
   188 
   157 function enanobot_process_channel_message($sockdata, $chan, $message)
   189 function enanobot_process_channel_message($sockdata, $chan, $message)
   158 {
   190 {
   159   global $irc, $nick, $mysql_conn, $privileged_list;
   191   global $irc, $nick, $mysql_conn, $privileged_list;
   160   
   192   
   161   if ( preg_match('/^\!echo /', $message['message']) && in_array($message['nick'], $privileged_list) )
   193   if ( strpos($message['message'], $nick) && !in_array($message['nick'], $privileged_list) && $message['nick'] != $nick )
   162   {
   194   {
   163     $chan->msg(preg_replace('/^\!echo /', '', $message['message']), true);
   195     //$target_nick =& $message['nick'];
   164   }
   196     //$chan->msg("{$target_nick}, I'm only a bot. :-) You should probably rely on the advice of humans if you need further assistance.", true);
   165   else if ( preg_match('/^\![\s]*([a-z0-9_-]+)([\s]*\|[\s]*([^ ]+))?$/', $message['message'], $match) )
   197   }
   166   {
   198   else
   167     $snippet =& $match[1];
   199   {
   168     if ( @$match[3] === 'me' )
   200     eval(eb_fetch_hook('event_channel_msg'));
   169       $match[3] = $message['nick'];
       
   170     $target_nick = ( !empty($match[3]) ) ? "{$match[3]}, " : "{$message['nick']}, ";
       
   171     if ( $snippet == 'snippets' )
       
   172     {
       
   173       // list available snippets
       
   174       $m_et = false;
       
   175       $q = eb_mysql_query('SELECT snippet_code, snippet_channels FROM snippets;');
       
   176       if ( mysql_num_rows($q) < 1 )
       
   177       {
       
   178         $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
       
   179       }
       
   180       else
       
   181       {
       
   182         $snippets = array();
       
   183         while ( $row = mysql_fetch_assoc($q) )
       
   184         {
       
   185           $channels = explode('|', $row['snippet_channels']);
       
   186           if ( in_array($chan->get_channel_name(), $channels) )
       
   187           {
       
   188             $snippets[] = $row['snippet_code'];
       
   189           }
       
   190         }
       
   191         $snippets = implode(', ', $snippets);
       
   192         $chan->msg("{$message['nick']}, the following snippets are available: $snippets", true);
       
   193       }
       
   194       @mysql_free_result($q);
       
   195     }
       
   196     else
       
   197     {
       
   198       // Look for the snippet...
       
   199       $q = eb_mysql_query('SELECT snippet_text, snippet_channels FROM snippets WHERE snippet_code = \'' . mysql_real_escape_string($snippet) . '\';');
       
   200       if ( mysql_num_rows($q) < 1 )
       
   201       {
       
   202         $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
       
   203       }
       
   204       else
       
   205       {
       
   206         $row = mysql_fetch_assoc($q);
       
   207         $channels = explode('|', $row['snippet_channels']);
       
   208         if ( in_array($chan->get_channel_name(), $channels) )
       
   209         {
       
   210           $chan->msg("{$target_nick}{$row['snippet_text']}", true);
       
   211         }
       
   212         else
       
   213         {
       
   214           $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
       
   215         }
       
   216       }
       
   217       @mysql_free_result($q);
       
   218     }
       
   219   }
       
   220   else if ( strpos($message['message'], $nick) && !in_array($message['nick'], $privileged_list) && $message['nick'] != $nick )
       
   221   {
       
   222     $target_nick =& $message['nick'];
       
   223     $chan->msg("{$target_nick}, I'm only a bot. :-) You should probably rely on the advice of humans if you need further assistance.", true);
       
   224   }
       
   225 }
       
   226 
       
   227 function enanobot_log_message($chan, $message)
       
   228 {
       
   229   global $nick;
       
   230   
       
   231   // Log the message
       
   232   $chan_db = mysql_real_escape_string($chan->get_channel_name());
       
   233   $nick_db = mysql_real_escape_string($message['nick']);
       
   234   $line_db = mysql_real_escape_string($message['message']);
       
   235   $day     = date('Y-m-d');
       
   236   $time    = time();
       
   237   $m_et = false;
       
   238   $sql = false;
       
   239   switch($message['action'])
       
   240   {
       
   241     case 'PRIVMSG':
       
   242       if ( substr($line_db, 0, 5) != '[off]' )
       
   243       {
       
   244         $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
       
   245                   ( '$chan_db', '$day', '$nick_db', '$time', '$line_db' );";
       
   246       }
       
   247       break;
       
   248     case 'JOIN':
       
   249       $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
       
   250                 ( '$chan_db', '$day', '', '$time', '$nick_db has joined $chan_db' );";
       
   251       break;
       
   252     case 'PART':
       
   253       $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
       
   254                 ( '$chan_db', '$day', '', '$time', '$nick_db has left $chan_db' );";
       
   255       break;
       
   256     case 'MODE':
       
   257       list($mode, $target_nick) = explode(' ', $line_db);
       
   258       if ( $message['nick'] != 'ChanServ' && $target_nick != $nick )
       
   259       {
       
   260         $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
       
   261                   ( '$chan_db', '$day', '', '$time', '$nick_db set mode $mode on $target_nick' );";
       
   262       }
       
   263       break;
       
   264   }
       
   265   if ( $sql )
       
   266   {
       
   267     eb_mysql_query($sql);
       
   268   }
   201   }
   269 }
   202 }
   270 
   203 
   271 function enanobot_privmsg_event($message)
   204 function enanobot_privmsg_event($message)
   272 {
   205 {
   275   if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' )
   208   if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' )
   276   {
   209   {
   277     foreach ( $irc->channels as $channel )
   210     foreach ( $irc->channels as $channel )
   278     {
   211     {
   279       $part_cache[] = array($channel->get_channel_name(), $channel->get_handler());
   212       $part_cache[] = array($channel->get_channel_name(), $channel->get_handler());
   280       $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);
   213       $channel->msg("I've received a request from {$message['nick']} to stop responding to requests, messages, and activities. Don't forget to unsuspend me with /msg $nick Resume when finished.", true);
   281       $channel->part("Logging and presence suspended by {$message['nick']}", true);
   214       $channel->part("Logging and presence suspended by {$message['nick']}", true);
   282     }
   215     }
   283   }
   216   }
   284   else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' )
   217   else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' )
   285   {
   218   {
   293     }
   226     }
   294     $part_cache = array();
   227     $part_cache = array();
   295   }
   228   }
   296   else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Shutdown' && $message['action'] == 'PRIVMSG' )
   229   else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Shutdown' && $message['action'] == 'PRIVMSG' )
   297   {
   230   {
       
   231     $GLOBALS['_shutdown'] = true;
   298     $irc->close("Remote bot shutdown ordered by {$message['nick']}", true);
   232     $irc->close("Remote bot shutdown ordered by {$message['nick']}", true);
   299     return 'BREAK';
   233     return 'BREAK';
   300   }
   234   }
   301   else if ( in_array($message['nick'], $privileged_list) && preg_match("/^\!echo-([^\007, \r\n\a\t]+) /", $message['message'], $match) )
   235   else if ( $message['action'] == 'PRIVMSG' )
   302   {
   236   {
   303     global $libirc_channels;
   237     eval(eb_fetch_hook('event_privmsg'));
   304     $channel_name =& $match[1];
   238   }
   305     if ( isset($libirc_channels[$channel_name]) && is_object($libirc_channels[$channel_name]) )
   239   else
   306     {
   240   {
   307       $libirc_channels[$channel_name]->msg(preg_replace("/^\!echo-([^\007, \r\n\a\t]+) /", '', $message['message']), true);
   241     eval(eb_fetch_hook('event_other'));
   308     }
   242   }
   309   }
   243 }
   310 }
   244 
   311 
   245 if ( $_shutdown )
       
   246 {
       
   247   exit(2);
       
   248 }