--- a/enanobot.php Sat Mar 15 20:15:16 2008 -0400
+++ b/enanobot.php Sat Mar 15 20:45:32 2008 -0400
@@ -9,7 +9,7 @@
function mysql_reconnect()
{
- global $mysql_conn, $mysql_host, $mysql_user, $mysql_pass;
+ global $mysql_conn, $mysql_host, $mysql_user, $mysql_pass, $mysql_dbname;
if ( $mysql_conn )
@mysql_close($mysql_conn);
// connect to MySQL
@@ -20,7 +20,7 @@
echo "Error connecting to MySQL: $m_e\n";
exit(1);
}
- $q = @mysql_query('USE enanobot;', $mysql_conn);
+ $q = @mysql_query("USE `$mysql_dbname`;", $mysql_conn);
if ( !$q )
{
$m_e = mysql_error();
@@ -29,6 +29,30 @@
}
}
+function eb_mysql_query($sql, $conn = false)
+{
+ global $mysql_conn, $irc;
+ $m_et = false;
+ while ( true )
+ {
+ $q = mysql_query($sql, $mysql_conn);
+ if ( !$q )
+ {
+ $m_e = mysql_error();
+ $m_et = true;
+ if ( $m_e == 'MySQL server has gone away' && !$m_et )
+ {
+ mysql_reconnect();
+ continue;
+ }
+ $irc->close("MySQL query error: $m_e");
+ exit(1);
+ }
+ break;
+ }
+ return $q;
+}
+
mysql_reconnect();
$irc = new Request_IRC('irc.freenode.net');
@@ -97,41 +121,53 @@
if ( @$match[3] === 'me' )
$match[3] = $message['nick'];
$target_nick = ( !empty($match[3]) ) ? "{$match[3]}, " : "{$message['nick']}, ";
- // Look for the snippet...
- $m_et = false;
- while ( true )
+ if ( $snippet == 'snippets' )
{
- $q = mysql_query('SELECT snippet_text, snippet_channels FROM snippets WHERE snippet_code = \'' . mysql_real_escape_string($snippet) . '\';', $mysql_conn);
- if ( !$q )
+ // list available snippets
+ $m_et = false;
+ $q = eb_mysql_query('SELECT snippet_code, snippet_channels FROM snippets;');
+ if ( mysql_num_rows($q) < 1 )
{
- $m_e = mysql_error();
- $m_et = true;
- if ( $m_e == 'MySQL server has gone away' && !$m_et )
+ $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
+ }
+ else
+ {
+ $snippets = array();
+ while ( $row = mysql_fetch_assoc($q) )
{
- mysql_reconnect();
- continue;
+ $channels = explode('|', $row['snippet_channels']);
+ if ( in_array($chan->get_channel_name(), $channels) )
+ {
+ $snippets[] = $row['snippet_code'];
+ }
}
- $irc->close("MySQL query error: $m_e");
- exit(1);
+ $snippets = implode(', ', $snippets);
+ $chan->msg("{$message['nick']}, the following snippets are available: $snippets", true);
}
- break;
- }
- if ( mysql_num_rows($q) < 1 )
- {
- $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
+ @mysql_free_result($q);
}
else
{
- $row = mysql_fetch_assoc($q);
- $channels = explode('|', $row['snippet_channels']);
- if ( in_array($chan->get_channel_name(), $channels) )
+ // Look for the snippet...
+ $q = eb_mysql_query('SELECT snippet_text, snippet_channels FROM snippets WHERE snippet_code = \'' . mysql_real_escape_string($snippet) . '\';');
+ if ( mysql_num_rows($q) < 1 )
{
- $chan->msg("{$target_nick}{$row['snippet_text']}", true);
+ $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
}
else
{
- $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
+ $row = mysql_fetch_assoc($q);
+ $channels = explode('|', $row['snippet_channels']);
+ if ( in_array($chan->get_channel_name(), $channels) )
+ {
+ $chan->msg("{$target_nick}{$row['snippet_text']}", true);
+ }
+ else
+ {
+ $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
+ }
}
+ @mysql_free_result($q);
}
}
else if ( strpos($message['message'], $nick) && !in_array($message['nick'], $privileged_list) && $message['nick'] != $nick )
@@ -176,22 +212,9 @@
}
break;
}
- while ( true && $sql )
+ if ( $sql )
{
- $q = @mysql_query($sql);
- if ( !$q )
- {
- $m_e = mysql_error();
- $m_et = true;
- if ( $m_e == 'MySQL server has gone away' && !$m_et )
- {
- mysql_reconnect();
- continue;
- }
- $irc->close("MySQL query error: $m_e");
- exit(1);
- }
- break;
+ eb_mysql_query($sql);
}
}