enanobot.php
author Dan
Wed, 07 May 2008 23:33:36 -0400
changeset 3 02e1c3f8f0ef
parent 2 c474904ed707
child 4 8f62a406d9d9
permissions -rw-r--r--
Hopefully made MySQL reconnection possible
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     1
<?php
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     2
// define('LIBIRC_DEBUG', '');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     3
require('libirc.php');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     4
require('config.php');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     5
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     6
@ini_set('display_errors', 'on');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     7
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     8
$mysql_conn = false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     9
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    10
function mysql_reconnect()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    11
{
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    12
  global $mysql_conn, $mysql_host, $mysql_user, $mysql_pass, $mysql_dbname;
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    13
  if ( $mysql_conn )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    14
    @mysql_close($mysql_conn);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    15
  // connect to MySQL
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    16
  $mysql_conn = @mysql_connect($mysql_host, $mysql_user, $mysql_pass);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    17
  if ( !$mysql_conn )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    18
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    19
    $m_e = mysql_error();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    20
    echo "Error connecting to MySQL: $m_e\n";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    21
    exit(1);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    22
  }
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    23
  $q = @mysql_query("USE `$mysql_dbname`;", $mysql_conn);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    24
  if ( !$q )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    25
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    26
    $m_e = mysql_error();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    27
    echo "Error selecting database: $m_e\n";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    28
    exit(1);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    29
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    30
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    31
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    32
function eb_mysql_query($sql, $conn = false)
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    33
{
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    34
  global $mysql_conn, $irc;
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    35
  $m_et = false;
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    36
  while ( true )
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    37
  {
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    38
    $q = mysql_query($sql, $mysql_conn);
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    39
    if ( !$q )
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    40
    {
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    41
      $m_e = mysql_error();
3
02e1c3f8f0ef Hopefully made MySQL reconnection possible
Dan
parents: 2
diff changeset
    42
      if ( strpos($m_e, 'gone away') && !$m_et )
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    43
      {
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    44
        mysql_reconnect();
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    45
        continue;
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    46
      }
3
02e1c3f8f0ef Hopefully made MySQL reconnection possible
Dan
parents: 2
diff changeset
    47
      $m_et = true;
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    48
      $irc->close("MySQL query error: $m_e");
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    49
      exit(1);
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    50
    }
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    51
    break;
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    52
  }
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    53
  return $q;
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    54
}
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
    55
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    56
mysql_reconnect();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    57
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    58
$irc = new Request_IRC('irc.freenode.net');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    59
$irc->connect($nick, $user, $name, $pass);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    60
$irc->set_privmsg_handler('enanobot_privmsg_event');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    61
$enano = $irc->join('#enano', 'enanobot_channel_event_enano');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    62
$enano_dev = $irc->join('#enano-dev', 'enanobot_channel_event_enanodev');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    63
$irc->privmsg('ChanServ', 'OP #enano EnanoBot');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    64
$irc->privmsg('ChanServ', 'OP #enano-dev EnanoBot');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    65
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    66
$irc->event_loop();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    67
$irc->close();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    68
mysql_close($mysql_conn);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    69
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    70
function enanobot_channel_event_enano($sockdata, $chan)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    71
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    72
  global $irc, $nick, $mysql_conn, $privileged_list;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    73
  $sockdata = trim($sockdata);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    74
  $message = Request_IRC::parse_message($sockdata);
1
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
    75
  enanobot_log_message($chan, $message);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    76
  switch ( $message['action'] )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    77
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    78
    case 'JOIN':
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    79
      // if a known op joins the channel, send mode +o
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    80
      if ( in_array($message['nick'], $privileged_list) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    81
      {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    82
        $chan->parent->put("MODE #enano +o {$message['nick']}\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    83
      }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    84
      break;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    85
    case 'PRIVMSG':
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    86
      enanobot_process_channel_message($sockdata, $chan, $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    87
      break;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    88
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    89
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    90
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    91
function enanobot_channel_event_enanodev($sockdata, $chan)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    92
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    93
  global $irc, $privileged_list;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    94
  $sockdata = trim($sockdata);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    95
  $message = Request_IRC::parse_message($sockdata);
1
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
    96
  enanobot_log_message($chan, $message);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    97
  switch ( $message['action'] )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    98
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    99
    case 'JOIN':
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   100
      // if dandaman32 joins the channel, use mode +o
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   101
      if ( in_array($message['nick'], $privileged_list) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   102
        $chan->parent->put("MODE #enano-dev +o {$message['nick']}\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   103
      break;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   104
    case 'PRIVMSG':
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   105
      enanobot_process_channel_message($sockdata, $chan, $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   106
      break;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   107
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   108
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   109
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   110
function enanobot_process_channel_message($sockdata, $chan, $message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   111
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   112
  global $irc, $nick, $mysql_conn, $privileged_list;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   113
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   114
  if ( preg_match('/^\!echo /', $message['message']) && in_array($message['nick'], $privileged_list) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   115
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   116
    $chan->msg(preg_replace('/^\!echo /', '', $message['message']), true);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   117
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   118
  else if ( preg_match('/^\![\s]*([a-z0-9_-]+)([\s]*\|[\s]*([^ ]+))?$/', $message['message'], $match) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   119
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   120
    $snippet =& $match[1];
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   121
    if ( @$match[3] === 'me' )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   122
      $match[3] = $message['nick'];
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   123
    $target_nick = ( !empty($match[3]) ) ? "{$match[3]}, " : "{$message['nick']}, ";
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   124
    if ( $snippet == 'snippets' )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   125
    {
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   126
      // list available snippets
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   127
      $m_et = false;
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   128
      $q = eb_mysql_query('SELECT snippet_code, snippet_channels FROM snippets;');
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   129
      if ( mysql_num_rows($q) < 1 )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   130
      {
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   131
        $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   132
      }
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   133
      else
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   134
      {
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   135
        $snippets = array();
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   136
        while ( $row = mysql_fetch_assoc($q) )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   137
        {
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   138
          $channels = explode('|', $row['snippet_channels']);
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   139
          if ( in_array($chan->get_channel_name(), $channels) )
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   140
          {
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   141
            $snippets[] = $row['snippet_code'];
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   142
          }
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   143
        }
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   144
        $snippets = implode(', ', $snippets);
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   145
        $chan->msg("{$message['nick']}, the following snippets are available: $snippets", true);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   146
      }
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   147
      @mysql_free_result($q);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   148
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   149
    else
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   150
    {
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   151
      // Look for the snippet...
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   152
      $q = eb_mysql_query('SELECT snippet_text, snippet_channels FROM snippets WHERE snippet_code = \'' . mysql_real_escape_string($snippet) . '\';');
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   153
      if ( mysql_num_rows($q) < 1 )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   154
      {
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   155
        $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   156
      }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   157
      else
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   158
      {
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   159
        $row = mysql_fetch_assoc($q);
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   160
        $channels = explode('|', $row['snippet_channels']);
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   161
        if ( in_array($chan->get_channel_name(), $channels) )
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   162
        {
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   163
          $chan->msg("{$target_nick}{$row['snippet_text']}", true);
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   164
        }
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   165
        else
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   166
        {
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   167
          $chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   168
        }
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   169
      }
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   170
      @mysql_free_result($q);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   171
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   172
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   173
  else if ( strpos($message['message'], $nick) && !in_array($message['nick'], $privileged_list) && $message['nick'] != $nick )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   174
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   175
    $target_nick =& $message['nick'];
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   176
    $chan->msg("{$target_nick}, I'm only a bot. :-) You should probably rely on the advice of humans if you need further assistance.", true);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   177
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   178
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   179
1
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   180
function enanobot_log_message($chan, $message)
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   181
{
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   182
  global $nick;
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   183
  
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   184
  // Log the message
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   185
  $chan_db = mysql_real_escape_string($chan->get_channel_name());
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   186
  $nick_db = mysql_real_escape_string($message['nick']);
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   187
  $line_db = mysql_real_escape_string($message['message']);
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   188
  $day     = date('Y-m-d');
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   189
  $time    = time();
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   190
  $m_et = false;
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   191
  $sql = false;
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   192
  switch($message['action'])
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   193
  {
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   194
    case 'PRIVMSG':
3
02e1c3f8f0ef Hopefully made MySQL reconnection possible
Dan
parents: 2
diff changeset
   195
      if ( substr($line_db, 0, 5) != '[off]' )
02e1c3f8f0ef Hopefully made MySQL reconnection possible
Dan
parents: 2
diff changeset
   196
      {
02e1c3f8f0ef Hopefully made MySQL reconnection possible
Dan
parents: 2
diff changeset
   197
        $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
02e1c3f8f0ef Hopefully made MySQL reconnection possible
Dan
parents: 2
diff changeset
   198
                  ( '$chan_db', '$day', '$nick_db', '$time', '$line_db' );";
02e1c3f8f0ef Hopefully made MySQL reconnection possible
Dan
parents: 2
diff changeset
   199
      }
1
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   200
      break;
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   201
    case 'JOIN':
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   202
      $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   203
                ( '$chan_db', '$day', '', '$time', '$nick_db has joined $chan_db' );";
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   204
      break;
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   205
    case 'PART':
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   206
      $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   207
                ( '$chan_db', '$day', '', '$time', '$nick_db has left $chan_db' );";
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   208
      break;
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   209
    case 'MODE':
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   210
      list($mode, $target_nick) = explode(' ', $line_db);
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   211
      if ( $message['nick'] != 'ChanServ' && $target_nick != $nick )
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   212
      {
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   213
        $sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   214
                  ( '$chan_db', '$day', '', '$time', '$nick_db set mode $mode on $target_nick' );";
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   215
      }
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   216
      break;
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   217
  }
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   218
  if ( $sql )
1
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   219
  {
2
c474904ed707 Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents: 1
diff changeset
   220
    eb_mysql_query($sql);
1
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   221
  }
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   222
}
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   223
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   224
function enanobot_privmsg_event($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   225
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   226
  global $privileged_list, $irc;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   227
  static $part_cache = array();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   228
  if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   229
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   230
    foreach ( $irc->channels as $channel )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   231
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   232
      $part_cache[] = array($channel->get_channel_name(), $channel->get_handler());
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   233
      $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);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   234
      $channel->part("Logging and presence suspended by {$message['nick']}", true);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   235
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   236
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   237
  else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   238
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   239
    global $nick;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   240
    foreach ( $part_cache as $chan_data )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   241
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   242
      $chan_name = substr($chan_data[0], 1);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   243
      $GLOBALS[$chan_name] = $irc->join($chan_data[0], $chan_data[1]);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   244
      $GLOBALS[$chan_name]->msg("Bot resumed by {$message['nick']}.", true);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   245
      $irc->privmsg('ChanServ', "OP {$chan_data[0]} $nick");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   246
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   247
    $part_cache = array();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   248
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   249
  else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Shutdown' && $message['action'] == 'PRIVMSG' )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   250
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   251
    $irc->close("Remote bot shutdown ordered by {$message['nick']}", true);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   252
    return 'BREAK';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   253
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   254
  else if ( in_array($message['nick'], $privileged_list) && preg_match('/^\!echo-enano /', $message['message']) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   255
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   256
    global $enano;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   257
    $enano->msg(preg_replace('/^\!echo-enano /', '', $message['message']), true);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   258
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   259
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   260