Initial population. EnanoBot v0.1.
authorDan
Sat, 15 Mar 2008 19:53:27 -0400
changeset 0 d02690a8552c
child 1 739423b66116
Initial population. EnanoBot v0.1.
config-sample.php
database.sql
enanobot.php
libirc.php
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config-sample.php	Sat Mar 15 19:53:27 2008 -0400
@@ -0,0 +1,14 @@
+<?php
+
+// Rename this to config.php and run php ./enanobot.php to start.
+
+$nick = 'EnanoBot';
+$pass = '';
+$name = 'Enano CMS logging/message bot';
+$user = 'enano';
+$privileged_list = array('your', 'nick', 'list', 'here');
+$mysql_host = 'localhost';
+$mysql_user = '';
+$mysql_pass = '';
+
+?>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/database.sql	Sat Mar 15 19:53:27 2008 -0400
@@ -0,0 +1,19 @@
+CREATE TABLE snippets(
+  snippet_id int(12) NOT NULL auto_increment,
+  snippet_code varchar(32) NOT NULL DEFAULT '',
+  snippet_text text,
+  snippet_channels text,
+  PRIMARY KEY ( snippet_id )
+);
+
+CREATE TABLE irclog (
+  id int(11) NOT NULL auto_increment,
+  channel varchar(30) default NULL,
+  day char(10) default NULL,
+  nick varchar(40) default NULL,
+  timestamp int(11) default NULL,
+  line text,
+  spam tinyint(1) default '0',
+  PRIMARY KEY  (id)
+);
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/enanobot.php	Sat Mar 15 19:53:27 2008 -0400
@@ -0,0 +1,204 @@
+<?php
+// define('LIBIRC_DEBUG', '');
+require('libirc.php');
+require('config.php');
+
+@ini_set('display_errors', 'on');
+
+$mysql_conn = false;
+
+function mysql_reconnect()
+{
+  global $mysql_conn, $mysql_host, $mysql_user, $mysql_pass;
+  if ( $mysql_conn )
+    @mysql_close($mysql_conn);
+  // connect to MySQL
+  $mysql_conn = @mysql_connect($mysql_host, $mysql_user, $mysql_pass);
+  if ( !$mysql_conn )
+  {
+    $m_e = mysql_error();
+    echo "Error connecting to MySQL: $m_e\n";
+    exit(1);
+  }
+  $q = @mysql_query('USE enanobot;', $mysql_conn);
+  if ( !$q )
+  {
+    $m_e = mysql_error();
+    echo "Error selecting database: $m_e\n";
+    exit(1);
+  }
+}
+
+mysql_reconnect();
+
+$irc = new Request_IRC('irc.freenode.net');
+$irc->connect($nick, $user, $name, $pass);
+$irc->set_privmsg_handler('enanobot_privmsg_event');
+$enano = $irc->join('#enano', 'enanobot_channel_event_enano');
+$enano_dev = $irc->join('#enano-dev', 'enanobot_channel_event_enanodev');
+$irc->privmsg('ChanServ', 'OP #enano EnanoBot');
+$irc->privmsg('ChanServ', 'OP #enano-dev EnanoBot');
+
+$irc->event_loop();
+$irc->close();
+mysql_close($mysql_conn);
+
+function enanobot_channel_event_enano($sockdata, $chan)
+{
+  global $irc, $nick, $mysql_conn, $privileged_list;
+  $sockdata = trim($sockdata);
+  $message = Request_IRC::parse_message($sockdata);
+  switch ( $message['action'] )
+  {
+    case 'JOIN':
+      // if a known op joins the channel, send mode +o
+      if ( in_array($message['nick'], $privileged_list) )
+      {
+        $chan->parent->put("MODE #enano +o {$message['nick']}\r\n");
+      }
+      break;
+    case 'PRIVMSG':
+      enanobot_process_channel_message($sockdata, $chan, $message);
+      break;
+  }
+}
+
+function enanobot_channel_event_enanodev($sockdata, $chan)
+{
+  global $irc, $privileged_list;
+  $sockdata = trim($sockdata);
+  $message = Request_IRC::parse_message($sockdata);
+  switch ( $message['action'] )
+  {
+    case 'JOIN':
+      // if dandaman32 joins the channel, use mode +o
+      if ( in_array($message['nick'], $privileged_list) )
+        $chan->parent->put("MODE #enano-dev +o {$message['nick']}\r\n");
+      break;
+    case 'PRIVMSG':
+      enanobot_process_channel_message($sockdata, $chan, $message);
+      break;
+  }
+}
+
+function enanobot_process_channel_message($sockdata, $chan, $message)
+{
+  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);
+  }
+  else if ( preg_match('/^\![\s]*([a-z0-9_-]+)([\s]*\|[\s]*([^ ]+))?$/', $message['message'], $match) )
+  {
+    $snippet =& $match[1];
+    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 )
+    {
+      $q = mysql_query('SELECT snippet_text, snippet_channels FROM snippets WHERE snippet_code = \'' . mysql_real_escape_string($snippet) . '\';', $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;
+    }
+    if ( mysql_num_rows($q) < 1 )
+    {
+      $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
+    }
+    else
+    {
+      $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);
+      }
+    }
+  }
+  else if ( strpos($message['message'], $nick) && !in_array($message['nick'], $privileged_list) && $message['nick'] != $nick )
+  {
+    $target_nick =& $message['nick'];
+    $chan->msg("{$target_nick}, I'm only a bot. :-) You should probably rely on the advice of humans if you need further assistance.", true);
+  }
+}
+
+function enanobot_privmsg_event($message)
+{
+  global $privileged_list, $irc;
+  static $part_cache = array();
+  if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' )
+  {
+    foreach ( $irc->channels as $channel )
+    {
+      $part_cache[] = array($channel->get_channel_name(), $channel->get_handler());
+      $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 EnanoBot Resume when finished.", true);
+      $channel->part("Logging and presence suspended by {$message['nick']}", true);
+    }
+  }
+  else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' )
+  {
+    global $nick;
+    foreach ( $part_cache as $chan_data )
+    {
+      $chan_name = substr($chan_data[0], 1);
+      $GLOBALS[$chan_name] = $irc->join($chan_data[0], $chan_data[1]);
+      $GLOBALS[$chan_name]->msg("Bot resumed by {$message['nick']}.", true);
+      $irc->privmsg('ChanServ', "OP {$chan_data[0]} $nick");
+    }
+    $part_cache = array();
+  }
+  else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Shutdown' && $message['action'] == 'PRIVMSG' )
+  {
+    $irc->close("Remote bot shutdown ordered by {$message['nick']}", true);
+    return 'BREAK';
+  }
+  else if ( in_array($message['nick'], $privileged_list) && preg_match('/^\!echo-enano /', $message['message']) )
+  {
+    global $enano;
+    $enano->msg(preg_replace('/^\!echo-enano /', '', $message['message']), true);
+  }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libirc.php	Sat Mar 15 19:53:27 2008 -0400
@@ -0,0 +1,476 @@
+<?php
+
+/**
+ * PHP IRC Client library
+ * Copyright (C) 2008 Dan Fuhry. All rights reserved.
+ */
+
+/**
+ * Version number
+ * @const string
+ */
+
+define('REQUEST_IRC_VERSION', '0.1');
+
+/**
+ * The base class for an IRC session.
+ */
+
+class Request_IRC
+{
+  
+  /**
+   * Hostname
+   * @var string
+   */
+  
+  private $host = '';
+  
+  /**
+   * Port number
+   * @var int
+   */
+  
+  private $port = 6667;
+  
+  /**
+   * The socket for the connection.
+   * @var resource
+   */
+  
+  public $sock = false;
+  
+  /**
+   * Channel objects, associative array
+   * @var array
+   */
+  
+  public $channels = array();
+  
+  /**
+   * The function called when a private message is received.
+   * @var string
+   */
+  
+  private $privmsg_handler = false;
+  
+  /**
+   * Switch to track if quitted or not. Helps avoid quitting the connection twice thus causing write errors.
+   * @var bool
+   * @access private
+   */
+  
+  protected $quitted = false;
+  
+  /**
+   * The nickname we're connected as. Not modified once connected.
+   * @var string
+   */
+  
+  public $nick = '';
+  
+  /**
+   * The username we're connected as. Not modified once connected.
+   * @var string
+   */
+  
+  public $user = '';
+  
+  /**
+   * Constructor.
+   * @param string Hostname
+   * @param int Port number, defaults to 6667
+   */
+  
+  public function __construct($host, $port = 6667)
+  {
+    // Check hostname
+    if ( !preg_match('/^(([a-z0-9-]+\.)*?)([a-z0-9-]+)$/', $host) )
+      die(__CLASS__ . ': Invalid hostname');
+    $this->host = $host;
+    
+    // Check port
+    if ( is_int($port) && $port >= 1 && $port <= 65535 )
+      $this->port = $port;
+    else
+      die(__CLASS__ . ': Invalid port');
+  }
+  
+  /**
+   * Sets parameters and opens the connection.
+   * @param string Nick
+   * @param string User
+   * @param string Real name
+   * @param string NickServ password
+   * @param int Flags, defaults to 0.
+   */
+  
+  public function connect($nick, $username, $realname, $pass, $flags = 0)
+  {
+    // Init connection
+    $this->sock = fsockopen($this->host, $this->port);
+    if ( !$this->sock )
+      throw new Exception('Could not make socket connection to host.');
+    
+    stream_set_timeout($this->sock, 5);
+    
+    // Wait for initial ident messages
+    while ( $msg = $this->get() )
+    {
+    }
+    
+    // Send nick and username
+    $this->put("NICK $nick\r\n");
+    $this->put("USER $username 0 * :$realname\r\n");
+    
+    // Wait for response and end of motd
+    $motd = '';
+    while ( $msg = $this->get() )
+    {
+      // Match particles
+      $msg = trim($msg);
+      $mc = preg_match('/^:([A-z0-9\.-]+) ([0-9]+) [A-z0-9_-]+ :(.+)$/', $msg, $match);
+      if ( !$mc )
+      {
+        $mc = preg_match('/^:([A-z0-9_-]+)!([A-z0-9_-]+)@([A-z0-9_\.-]+) NOTICE [A-z0-9_-]+ :(.+)$/', $msg, $match);
+        if ( !$mc )
+          continue;
+        // Look for a response from NickServ
+        if ( $match[1] == 'NickServ' )
+        {
+          // Asking for auth?
+          if ( strpos($match[4], 'IDENTIFY') )
+          {
+            // Yes, send password
+            $this->privmsg('NickServ', "IDENTIFY $pass");
+          }
+        }
+      }
+      list(, $host, $stat, $msg) = $match;
+      $motd .= "$msg";
+    }
+    
+    $this->nick = $nick;
+    $this->user = $username;
+  }
+  
+  /**
+   * Writes some data to the socket, abstracted for debugging purposes.
+   * @param string Message to send, this should include a CRLF.
+   */
+  
+  public function put($message)
+  {
+    if ( !$this->sock )
+    {
+      if ( defined('LIBIRC_DEBUG') )
+        echo ">>> WRITE FAILED: $message";
+      return false;
+    }
+    if ( defined('LIBIRC_DEBUG') )
+      echo ">>> $message";
+    fwrite($this->sock, $message);
+  }
+  
+  /**
+   * Reads from the socket...
+   * @return string
+   */
+  
+  public function get()
+  {
+    if ( !$this->sock )
+    {
+      if ( defined('LIBIRC_DEBUG') )
+        echo "<<< READ FAILED\n";
+      return false;
+    }
+    $out = fgets($this->sock, 4096);
+    if ( defined('LIBIRC_DEBUG') )
+      if ( !empty($out) )
+        echo "<<< $out";
+    return $out;
+  }
+  
+  /**
+   * Sends a message to a nick or channel.
+   * @param string Nick or channel
+   * @param string Message
+   */
+  
+  public function privmsg($nick, $message)
+  {
+    $message = str_replace("\r\n", "\n", $message);
+    $message = explode("\n", $message);
+    foreach ( $message as $line )
+    {
+      $this->put("PRIVMSG $nick :$line\r\n");
+    }
+  }
+  
+  /**
+   * The main event loop.
+   */
+  
+  public function event_loop()
+  {
+    stream_set_timeout($this->sock, 0xFFFFFFFE);
+    while ( $data = $this->get() )
+    {
+      $data_trim = trim($data);
+      $match = self::parse_message($data_trim);
+      if ( preg_match('/^PING :(.+?)$/', $data_trim, $pmatch) )
+      {
+        $this->put("PONG :{$pmatch[1]}\r\n");
+      }
+      else if ( $match )
+      {
+        // Received PRIVMSG or other mainstream action
+        if ( $match['action'] == 'JOIN' )
+          $channel =& $match['message'];
+        else
+          $channel =& $match['target'];
+          
+        if ( !preg_match('/^[#!&\+]/', $channel) )
+        {
+          // Private message from user
+          $result = $this->handle_privmsg($data);
+          stream_set_timeout($this->sock, 0xFFFFFFFE);
+        }
+        else if ( isset($this->channels[strtolower($channel)]) )
+        {
+          // Message into channel
+          $chan =& $this->channels[strtolower($channel)];
+          $func = $chan->get_handler();
+          $result = @call_user_func($func, $data, $chan);
+          stream_set_timeout($this->sock, 0xFFFFFFFE);
+        }
+        if ( $result == 'BREAK' )
+        {
+          break;
+        }
+      }
+    }
+  }
+  
+  /**
+   * Processor for when a private message is received.
+   * @access private
+   */
+  
+  private function handle_privmsg($message)
+  {
+    $message = self::parse_message($message);
+    $ph = $this->privmsg_handler;
+    if ( @function_exists($ph) )
+      return @call_user_func($ph, $message);
+  }
+  
+  /**
+   * Changes the function called upon receipt of a private message.
+   * @param string Function to call, will be passed a parsed message.
+   */
+  
+  function set_privmsg_handler($func)
+  {
+    if ( !function_exists($func) )
+      return false;
+    $this->privmsg_handler = $func;
+    return true;
+  }
+  
+  /**
+   * Parses a message line.
+   * @param string Message text
+   * @return array Associative with keys: nick, user, host, action, target, message
+   */
+   
+  public static function parse_message($message)
+  {
+    // Indices:          12       3       4        5        67                         8
+    $mc = preg_match('/^:(([^ ]+)!([^ ]+)@([^ ]+)) ([A-Z]+) (([#!&\+]*[A-z0-9_-]+) )?:?(.*?)$/', $message, $match);
+    if ( !$mc )
+    {
+      return false;
+    }
+    // Indices: 0 1 2      3      4      5        6 7        8
+    list(       , , $nick, $user, $host, $action, , $target, $message) = $match;
+    return array(
+        'nick' => $nick,
+        'user' => $user,
+        'host' => $host,
+        'action' => $action,
+        'target' => $target,
+        'message' => trim($message)
+      );
+  }
+  
+  /**
+   * Joins a channel, and returns a Request_IRC_Channel object.
+   * @param string Channel name (remember # prefix)
+   * @param string Event handler function, will be called with param 0 = socket output and param 1 = channel object
+   * @return object
+   */
+  
+  function join($channel, $handler)
+  {
+    $chan = new Request_IRC_Channel(strtolower($channel), $handler, $this);
+    $this->channels[strtolower($channel)] = $chan;
+    return $chan;
+  }
+  
+  /**
+   * Closes the connection and quits.
+   * @param string Optional part message
+   */
+  
+  public function close($partmsg = false)
+  {
+    if ( $this->quitted )
+      return true;
+    
+    $this->quitted = true;
+    // Part all channels
+    if ( !$partmsg )
+      $partmsg = 'IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION;
+    
+    foreach ( $this->channels as $channel )
+    {
+      $channel->part($partmsg);
+    }
+    
+    $this->put("QUIT\r\n");
+    
+    while ( $msg = $this->get() )
+    {
+      // Do nothing.
+    }
+    
+    fclose($this->sock);
+  }
+  
+}
+
+/**
+ * Wrapper for channels.
+ */
+
+class Request_IRC_Channel extends Request_IRC
+{
+  
+  /**
+   * The name of the channel
+   * @var string
+   */
+  
+  private $channel_name = '';
+  
+  /**
+   * The event handler function.
+   * @var string
+   */
+  
+  private $handler = '';
+  
+  /**
+   * The parent connection.
+   * @var object
+   */
+  
+  public $parent = false;
+  
+  /**
+   * Whether the channel has been parted or not, used to kill the destructor.
+   * @var bool
+   */
+  
+  protected $parted = false;
+  
+  /**
+   * Constructor.
+   * @param string Channel name
+   * @param string Handler function
+   * @param object IRC connection (Request_IRC object)
+   */
+  
+  function __construct($channel, $handler, $parent)
+  {
+    $this->parent = $parent;
+    $this->parent->put("JOIN $channel\r\n");
+    stream_set_timeout($this->parent->sock, 3);
+    while ( $msg = $this->parent->get() )
+    {
+      // Do nothing
+    }
+    $this->channel_name = $channel;
+    $this->handler = $handler;
+  }
+  
+  /**
+   * Returns the channel name
+   * @return string
+   */
+  
+  function get_channel_name()
+  {
+    return $this->channel_name;
+  }
+  
+  /**
+   * Returns the handler function
+   * @return string
+   */
+  
+  function get_handler()
+  {
+    return $this->handler;
+  }
+  
+  /**
+   * Sends a message.
+   * @param string message
+   * @param bool If true, will fire a message event when the message is sent.
+   */
+  
+  function msg($msg, $fire_event = false)
+  {
+    $this->parent->privmsg($this->channel_name, $msg);
+    if ( $fire_event )
+    {
+      $func = $this->get_handler();
+      // format: :nick!user@host PRIVMSG #channel :msg.
+      $lines = explode("\n", $msg);
+      foreach ( $lines as $line )
+      {
+        $data = ":{$this->parent->nick}!{$this->parent->user}@localhost PRIVMSG {$this->channel_name} :$line";
+        $result = @call_user_func($func, $data, $this);
+        stream_set_timeout($this->parent->sock, 0xFFFFFFFE);
+      }
+    }
+  }
+  
+  /**
+   * Destructor, automatically parts the channel.
+   */
+  
+  function __destruct()
+  {
+    if ( !$this->parted )
+      $this->part('IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION);
+  }
+  
+  /**
+   * Parts the channel.
+   * @param string Optional message
+   */
+  
+  function part($msg = '')
+  {
+    $this->parent->put("PART {$this->channel_name} :$msg\r\n");
+    $this->parted = true;
+    unset($this->parent->channels[$this->channel_name]);
+  }
+  
+}
+
+?>