enanobot.php
changeset 1 739423b66116
parent 0 d02690a8552c
child 2 c474904ed707
equal deleted inserted replaced
0:d02690a8552c 1:739423b66116
    46 function enanobot_channel_event_enano($sockdata, $chan)
    46 function enanobot_channel_event_enano($sockdata, $chan)
    47 {
    47 {
    48   global $irc, $nick, $mysql_conn, $privileged_list;
    48   global $irc, $nick, $mysql_conn, $privileged_list;
    49   $sockdata = trim($sockdata);
    49   $sockdata = trim($sockdata);
    50   $message = Request_IRC::parse_message($sockdata);
    50   $message = Request_IRC::parse_message($sockdata);
       
    51   enanobot_log_message($chan, $message);
    51   switch ( $message['action'] )
    52   switch ( $message['action'] )
    52   {
    53   {
    53     case 'JOIN':
    54     case 'JOIN':
    54       // if a known op joins the channel, send mode +o
    55       // if a known op joins the channel, send mode +o
    55       if ( in_array($message['nick'], $privileged_list) )
    56       if ( in_array($message['nick'], $privileged_list) )
    66 function enanobot_channel_event_enanodev($sockdata, $chan)
    67 function enanobot_channel_event_enanodev($sockdata, $chan)
    67 {
    68 {
    68   global $irc, $privileged_list;
    69   global $irc, $privileged_list;
    69   $sockdata = trim($sockdata);
    70   $sockdata = trim($sockdata);
    70   $message = Request_IRC::parse_message($sockdata);
    71   $message = Request_IRC::parse_message($sockdata);
       
    72   enanobot_log_message($chan, $message);
    71   switch ( $message['action'] )
    73   switch ( $message['action'] )
    72   {
    74   {
    73     case 'JOIN':
    75     case 'JOIN':
    74       // if dandaman32 joins the channel, use mode +o
    76       // if dandaman32 joins the channel, use mode +o
    75       if ( in_array($message['nick'], $privileged_list) )
    77       if ( in_array($message['nick'], $privileged_list) )
    82 }
    84 }
    83 
    85 
    84 function enanobot_process_channel_message($sockdata, $chan, $message)
    86 function enanobot_process_channel_message($sockdata, $chan, $message)
    85 {
    87 {
    86   global $irc, $nick, $mysql_conn, $privileged_list;
    88   global $irc, $nick, $mysql_conn, $privileged_list;
    87   
       
    88   // Log the message
       
    89   $chan_db = mysql_real_escape_string($chan->get_channel_name());
       
    90   $nick_db = mysql_real_escape_string($message['nick']);
       
    91   $line_db = mysql_real_escape_string($message['message']);
       
    92   $day     = date('Y-m-d');
       
    93   $time    = time();
       
    94   $m_et = false;
       
    95   while ( true )
       
    96   {
       
    97     $q = @mysql_query("INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
       
    98                          ( '$chan_db', '$day', '$nick_db', '$time', '$line_db' );");
       
    99     if ( !$q )
       
   100     {
       
   101       $m_e = mysql_error();
       
   102       $m_et = true;
       
   103       if ( $m_e == 'MySQL server has gone away' && !$m_et )
       
   104       {
       
   105         mysql_reconnect();
       
   106         continue;
       
   107       }
       
   108       $irc->close("MySQL query error: $m_e");
       
   109       exit(1);
       
   110     }
       
   111     break;
       
   112   }
       
   113   
    89   
   114   if ( preg_match('/^\!echo /', $message['message']) && in_array($message['nick'], $privileged_list) )
    90   if ( preg_match('/^\!echo /', $message['message']) && in_array($message['nick'], $privileged_list) )
   115   {
    91   {
   116     $chan->msg(preg_replace('/^\!echo /', '', $message['message']), true);
    92     $chan->msg(preg_replace('/^\!echo /', '', $message['message']), true);
   117   }
    93   }
   163     $target_nick =& $message['nick'];
   139     $target_nick =& $message['nick'];
   164     $chan->msg("{$target_nick}, I'm only a bot. :-) You should probably rely on the advice of humans if you need further assistance.", true);
   140     $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   }
   141   }
   166 }
   142 }
   167 
   143 
       
   144 function enanobot_log_message($chan, $message)
       
   145 {
       
   146   global $nick;
       
   147   
       
   148   // Log the message
       
   149   $chan_db = mysql_real_escape_string($chan->get_channel_name());
       
   150   $nick_db = mysql_real_escape_string($message['nick']);
       
   151   $line_db = mysql_real_escape_string($message['message']);
       
   152   $day     = date('Y-m-d');
       
   153   $time    = time();
       
   154   $m_et = false;
       
   155   $sql = false;
       
   156   switch($message['action'])
       
   157   {
       
   158     case 'PRIVMSG':
       
   159       $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
       
   160                 ( '$chan_db', '$day', '$nick_db', '$time', '$line_db' );";
       
   161       break;
       
   162     case 'JOIN':
       
   163       $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
       
   164                 ( '$chan_db', '$day', '', '$time', '$nick_db has joined $chan_db' );";
       
   165       break;
       
   166     case 'PART':
       
   167       $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
       
   168                 ( '$chan_db', '$day', '', '$time', '$nick_db has left $chan_db' );";
       
   169       break;
       
   170     case 'MODE':
       
   171       list($mode, $target_nick) = explode(' ', $line_db);
       
   172       if ( $message['nick'] != 'ChanServ' && $target_nick != $nick )
       
   173       {
       
   174         $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
       
   175                   ( '$chan_db', '$day', '', '$time', '$nick_db set mode $mode on $target_nick' );";
       
   176       }
       
   177       break;
       
   178   }
       
   179   while ( true && $sql )
       
   180   {
       
   181     $q = @mysql_query($sql);
       
   182     if ( !$q )
       
   183     {
       
   184       $m_e = mysql_error();
       
   185       $m_et = true;
       
   186       if ( $m_e == 'MySQL server has gone away' && !$m_et )
       
   187       {
       
   188         mysql_reconnect();
       
   189         continue;
       
   190       }
       
   191       $irc->close("MySQL query error: $m_e");
       
   192       exit(1);
       
   193     }
       
   194     break;
       
   195   }
       
   196 }
       
   197 
   168 function enanobot_privmsg_event($message)
   198 function enanobot_privmsg_event($message)
   169 {
   199 {
   170   global $privileged_list, $irc;
   200   global $privileged_list, $irc;
   171   static $part_cache = array();
   201   static $part_cache = array();
   172   if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' )
   202   if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' )