enanobot.php
changeset 2 c474904ed707
parent 1 739423b66116
child 3 02e1c3f8f0ef
equal deleted inserted replaced
1:739423b66116 2:c474904ed707
     7 
     7 
     8 $mysql_conn = false;
     8 $mysql_conn = false;
     9 
     9 
    10 function mysql_reconnect()
    10 function mysql_reconnect()
    11 {
    11 {
    12   global $mysql_conn, $mysql_host, $mysql_user, $mysql_pass;
    12   global $mysql_conn, $mysql_host, $mysql_user, $mysql_pass, $mysql_dbname;
    13   if ( $mysql_conn )
    13   if ( $mysql_conn )
    14     @mysql_close($mysql_conn);
    14     @mysql_close($mysql_conn);
    15   // connect to MySQL
    15   // connect to MySQL
    16   $mysql_conn = @mysql_connect($mysql_host, $mysql_user, $mysql_pass);
    16   $mysql_conn = @mysql_connect($mysql_host, $mysql_user, $mysql_pass);
    17   if ( !$mysql_conn )
    17   if ( !$mysql_conn )
    18   {
    18   {
    19     $m_e = mysql_error();
    19     $m_e = mysql_error();
    20     echo "Error connecting to MySQL: $m_e\n";
    20     echo "Error connecting to MySQL: $m_e\n";
    21     exit(1);
    21     exit(1);
    22   }
    22   }
    23   $q = @mysql_query('USE enanobot;', $mysql_conn);
    23   $q = @mysql_query("USE `$mysql_dbname`;", $mysql_conn);
    24   if ( !$q )
    24   if ( !$q )
    25   {
    25   {
    26     $m_e = mysql_error();
    26     $m_e = mysql_error();
    27     echo "Error selecting database: $m_e\n";
    27     echo "Error selecting database: $m_e\n";
    28     exit(1);
    28     exit(1);
    29   }
    29   }
       
    30 }
       
    31 
       
    32 function eb_mysql_query($sql, $conn = false)
       
    33 {
       
    34   global $mysql_conn, $irc;
       
    35   $m_et = false;
       
    36   while ( true )
       
    37   {
       
    38     $q = mysql_query($sql, $mysql_conn);
       
    39     if ( !$q )
       
    40     {
       
    41       $m_e = mysql_error();
       
    42       $m_et = true;
       
    43       if ( $m_e == 'MySQL server has gone away' && !$m_et )
       
    44       {
       
    45         mysql_reconnect();
       
    46         continue;
       
    47       }
       
    48       $irc->close("MySQL query error: $m_e");
       
    49       exit(1);
       
    50     }
       
    51     break;
       
    52   }
       
    53   return $q;
    30 }
    54 }
    31 
    55 
    32 mysql_reconnect();
    56 mysql_reconnect();
    33 
    57 
    34 $irc = new Request_IRC('irc.freenode.net');
    58 $irc = new Request_IRC('irc.freenode.net');
    95   {
   119   {
    96     $snippet =& $match[1];
   120     $snippet =& $match[1];
    97     if ( @$match[3] === 'me' )
   121     if ( @$match[3] === 'me' )
    98       $match[3] = $message['nick'];
   122       $match[3] = $message['nick'];
    99     $target_nick = ( !empty($match[3]) ) ? "{$match[3]}, " : "{$message['nick']}, ";
   123     $target_nick = ( !empty($match[3]) ) ? "{$match[3]}, " : "{$message['nick']}, ";
   100     // Look for the snippet...
   124     if ( $snippet == 'snippets' )
   101     $m_et = false;
   125     {
   102     while ( true )
   126       // list available snippets
   103     {
   127       $m_et = false;
   104       $q = mysql_query('SELECT snippet_text, snippet_channels FROM snippets WHERE snippet_code = \'' . mysql_real_escape_string($snippet) . '\';', $mysql_conn);
   128       $q = eb_mysql_query('SELECT snippet_code, snippet_channels FROM snippets;');
   105       if ( !$q )
   129       if ( mysql_num_rows($q) < 1 )
   106       {
   130       {
   107         $m_e = mysql_error();
   131         $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
   108         $m_et = true;
   132       }
   109         if ( $m_e == 'MySQL server has gone away' && !$m_et )
   133       else
       
   134       {
       
   135         $snippets = array();
       
   136         while ( $row = mysql_fetch_assoc($q) )
   110         {
   137         {
   111           mysql_reconnect();
   138           $channels = explode('|', $row['snippet_channels']);
   112           continue;
   139           if ( in_array($chan->get_channel_name(), $channels) )
       
   140           {
       
   141             $snippets[] = $row['snippet_code'];
       
   142           }
   113         }
   143         }
   114         $irc->close("MySQL query error: $m_e");
   144         $snippets = implode(', ', $snippets);
   115         exit(1);
   145         $chan->msg("{$message['nick']}, the following snippets are available: $snippets", true);
   116       }
   146       }
   117       break;
   147       @mysql_free_result($q);
   118     }
       
   119     if ( mysql_num_rows($q) < 1 )
       
   120     {
       
   121       $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
       
   122     }
   148     }
   123     else
   149     else
   124     {
   150     {
   125       $row = mysql_fetch_assoc($q);
   151       // Look for the snippet...
   126       $channels = explode('|', $row['snippet_channels']);
   152       $q = eb_mysql_query('SELECT snippet_text, snippet_channels FROM snippets WHERE snippet_code = \'' . mysql_real_escape_string($snippet) . '\';');
   127       if ( in_array($chan->get_channel_name(), $channels) )
   153       if ( mysql_num_rows($q) < 1 )
   128       {
   154       {
   129         $chan->msg("{$target_nick}{$row['snippet_text']}", true);
   155         $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
   130       }
   156       }
   131       else
   157       else
   132       {
   158       {
   133         $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
   159         $row = mysql_fetch_assoc($q);
   134       }
   160         $channels = explode('|', $row['snippet_channels']);
       
   161         if ( in_array($chan->get_channel_name(), $channels) )
       
   162         {
       
   163           $chan->msg("{$target_nick}{$row['snippet_text']}", true);
       
   164         }
       
   165         else
       
   166         {
       
   167           $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
       
   168         }
       
   169       }
       
   170       @mysql_free_result($q);
   135     }
   171     }
   136   }
   172   }
   137   else if ( strpos($message['message'], $nick) && !in_array($message['nick'], $privileged_list) && $message['nick'] != $nick )
   173   else if ( strpos($message['message'], $nick) && !in_array($message['nick'], $privileged_list) && $message['nick'] != $nick )
   138   {
   174   {
   139     $target_nick =& $message['nick'];
   175     $target_nick =& $message['nick'];
   174         $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
   210         $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
   175                   ( '$chan_db', '$day', '', '$time', '$nick_db set mode $mode on $target_nick' );";
   211                   ( '$chan_db', '$day', '', '$time', '$nick_db set mode $mode on $target_nick' );";
   176       }
   212       }
   177       break;
   213       break;
   178   }
   214   }
   179   while ( true && $sql )
   215   if ( $sql )
   180   {
   216   {
   181     $q = @mysql_query($sql);
   217     eb_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   }
   218   }
   196 }
   219 }
   197 
   220 
   198 function enanobot_privmsg_event($message)
   221 function enanobot_privmsg_event($message)
   199 {
   222 {