Added support for logging mode messages and join/part messages
authorDan
Sat, 15 Mar 2008 20:15:16 -0400
changeset 1 739423b66116
parent 0 d02690a8552c
child 2 c474904ed707
Added support for logging mode messages and join/part messages
enanobot.php
--- a/enanobot.php	Sat Mar 15 19:53:27 2008 -0400
+++ b/enanobot.php	Sat Mar 15 20:15:16 2008 -0400
@@ -48,6 +48,7 @@
   global $irc, $nick, $mysql_conn, $privileged_list;
   $sockdata = trim($sockdata);
   $message = Request_IRC::parse_message($sockdata);
+  enanobot_log_message($chan, $message);
   switch ( $message['action'] )
   {
     case 'JOIN':
@@ -68,6 +69,7 @@
   global $irc, $privileged_list;
   $sockdata = trim($sockdata);
   $message = Request_IRC::parse_message($sockdata);
+  enanobot_log_message($chan, $message);
   switch ( $message['action'] )
   {
     case 'JOIN':
@@ -85,32 +87,6 @@
 {
   global $irc, $nick, $mysql_conn, $privileged_list;
   
-  // Log the message
-  $chan_db = mysql_real_escape_string($chan->get_channel_name());
-  $nick_db = mysql_real_escape_string($message['nick']);
-  $line_db = mysql_real_escape_string($message['message']);
-  $day     = date('Y-m-d');
-  $time    = time();
-  $m_et = false;
-  while ( true )
-  {
-    $q = @mysql_query("INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
-                         ( '$chan_db', '$day', '$nick_db', '$time', '$line_db' );");
-    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;
-  }
-  
   if ( preg_match('/^\!echo /', $message['message']) && in_array($message['nick'], $privileged_list) )
   {
     $chan->msg(preg_replace('/^\!echo /', '', $message['message']), true);
@@ -165,6 +141,60 @@
   }
 }
 
+function enanobot_log_message($chan, $message)
+{
+  global $nick;
+  
+  // Log the message
+  $chan_db = mysql_real_escape_string($chan->get_channel_name());
+  $nick_db = mysql_real_escape_string($message['nick']);
+  $line_db = mysql_real_escape_string($message['message']);
+  $day     = date('Y-m-d');
+  $time    = time();
+  $m_et = false;
+  $sql = false;
+  switch($message['action'])
+  {
+    case 'PRIVMSG':
+      $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
+                ( '$chan_db', '$day', '$nick_db', '$time', '$line_db' );";
+      break;
+    case 'JOIN':
+      $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
+                ( '$chan_db', '$day', '', '$time', '$nick_db has joined $chan_db' );";
+      break;
+    case 'PART':
+      $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
+                ( '$chan_db', '$day', '', '$time', '$nick_db has left $chan_db' );";
+      break;
+    case 'MODE':
+      list($mode, $target_nick) = explode(' ', $line_db);
+      if ( $message['nick'] != 'ChanServ' && $target_nick != $nick )
+      {
+        $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
+                  ( '$chan_db', '$day', '', '$time', '$nick_db set mode $mode on $target_nick' );";
+      }
+      break;
+  }
+  while ( true && $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;
+  }
+}
+
 function enanobot_privmsg_event($message)
 {
   global $privileged_list, $irc;