enanobot.php
author Dan
Sat, 10 Jan 2009 14:07:54 -0500
changeset 48 d793361a27ec
parent 41 1d854f22ac5a
child 49 4c4d69b2cd4d
permissions -rw-r--r--
Greeting: bot no longer greets itself
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     1
<?php
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
     2
/**
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
     3
 * EnanoBot - the Enano CMS IRC logging and help automation bot
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
     4
 * GPL and no warranty, see the LICENSE file for more info
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
     5
 */
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
     6
7
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
     7
// parse command line
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
     8
if ( isset($argv[1]) )
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
     9
{
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    10
  $arg =& $argv[1];
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    11
  if ( $arg == '--daemon' || $arg == '-d' )
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    12
  {
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    13
    // attempt to fork...
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    14
    if ( function_exists('pcntl_fork') )
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    15
    {
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    16
      $pid = pcntl_fork();
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    17
      if ( $pid == -1 )
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    18
      {
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    19
        echo "Forking process failed.\n";
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    20
        exit(1);
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    21
      }
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    22
      else if ( $pid )
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    23
      {
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    24
        echo "EnanoBot daemon started, pid $pid\n";
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    25
        exit(0);
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    26
      }
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    27
      else
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    28
      {
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    29
        // do nothing, just continue.
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    30
      }
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    31
    }
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    32
    else
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    33
    {
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    34
      echo "No pcntl support in PHP, continuing in foreground\n";
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    35
    }
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    36
  }
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    37
  else if ( $arg == '-v' || $arg == '--verbose' )
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    38
  {
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    39
    define('LIBIRC_DEBUG', '');
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    40
  }
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    41
  else
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    42
  {
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    43
    echo <<<EOF
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    44
Usage: {$argv[0]}
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    45
Options:
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    46
  -d, --daemon     Run in background (requires pcntl support)
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    47
  -v, --verbose    Log communication to stdout (ignored if -d specified)
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    48
  -h, --help       This help message
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    49
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    50
EOF;
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    51
    exit(1);
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    52
  }
1d6e762433fe Added daemonizing (forking) support.
Dan
parents: 6
diff changeset
    53
}
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
    54
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    55
$censored_words = array('cock', 'fuck', 'cuck', 'funt', 'cunt', 'bitch');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    56
$_shutdown = false;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    57
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    58
function eb_censor_words($text)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    59
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    60
  // return $text;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    61
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    62
  global $censored_words;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    63
  foreach ( $censored_words as $word )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    64
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    65
    $replacement = substr($word, 0, 1) . preg_replace('/./', '*', substr($word, 1));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    66
    while ( stristr($text, $word) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    67
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    68
      $text = preg_replace("/$word/i", $replacement, $text);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    69
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    70
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    71
  return $text;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    72
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    73
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    74
require('libirc.php');
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    75
require('hooks.php');
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    76
require('config.php');
23
df31a3872d19 Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents: 13
diff changeset
    77
require('database.php');
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    78
40
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
    79
$enanobot_version = '0.5-unstable';
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
    80
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    81
@ini_set('display_errors', 'on');
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    82
error_reporting(E_ALL);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    83
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    84
// load modules
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    85
foreach ( $modules as $module )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    86
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    87
  $modulefile = "modules/$module.php";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    88
  if ( file_exists($modulefile) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    89
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    90
    require($modulefile);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    91
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    92
}
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    93
23
df31a3872d19 Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents: 13
diff changeset
    94
mysql_reconnect();
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    95
23
df31a3872d19 Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents: 13
diff changeset
    96
eval(eb_fetch_hook('startup_early'));
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    97
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
    98
$libirc_channels = array();
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
    99
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   100
$irc = new Request_IRC($server);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   101
$irc->connect($nick, $user, $name, $pass);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   102
$irc->set_privmsg_handler('enanobot_privmsg_event');
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   103
$irc->set_timeout_handlers(false, 'enanobot_timeout_event');
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   104
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   105
foreach ( $channels as $channel )
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   106
{
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   107
  $libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event');
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   108
  $channel_clean = preg_replace('/^[#&]/', '', $channel);
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   109
  $libirc_channels[$channel_clean] =& $libirc_channels[$channel];
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   110
  $irc->privmsg('ChanServ', "OP $channel $nick");
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   111
}
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   112
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   113
$irc->event_loop();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   114
$irc->close();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   115
mysql_close($mysql_conn);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   116
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   117
function enanobot_channel_event($sockdata, $chan)
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   118
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   119
  global $irc, $nick, $mysql_conn, $privileged_list;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   120
  $sockdata = trim($sockdata);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   121
  $message = Request_IRC::parse_message($sockdata);
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   122
  $channelname = $chan->get_channel_name();
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   123
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   124
  eval(eb_fetch_hook('event_raw_message'));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   125
  
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   126
  switch ( $message['action'] )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   127
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   128
    case 'JOIN':
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   129
      eval(eb_fetch_hook('event_join'));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   130
      break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   131
    case 'PART':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   132
      eval(eb_fetch_hook('event_part'));
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   133
      break;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   134
    case 'PRIVMSG':
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   135
      enanobot_process_channel_message($sockdata, $chan, $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   136
      break;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   137
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   138
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   139
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   140
function enanobot_process_channel_message($sockdata, $chan, $message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   141
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   142
  global $irc, $nick, $mysql_conn, $privileged_list;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   143
  
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   144
  if ( strpos($message['message'], $nick) && !in_array($message['nick'], $privileged_list) && $message['nick'] != $nick )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   145
  {
9
4192954c29d1 Finalization of merge from devel branch
Dan
parents: 8
diff changeset
   146
    $target_nick =& $message['nick'];
13
f073c94a1477 Disabled annoying "I'm only a bot" message
Dan
parents: 9
diff changeset
   147
    // $chan->msg("{$target_nick}, I'm only a bot. :-) You should probably rely on the advice of humans if you need further assistance.", true);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   148
  }
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   149
  else
1
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   150
  {
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   151
    eval(eb_fetch_hook('event_channel_msg'));
1
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   152
  }
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   153
}
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   154
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   155
function enanobot_privmsg_event($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   156
{
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   157
  global $privileged_list, $irc, $nick;
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   158
  static $part_cache = array();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   159
  if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   160
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   161
    foreach ( $irc->channels as $channel )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   162
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   163
      $part_cache[] = array($channel->get_channel_name(), $channel->get_handler());
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   164
      $channel->msg("I've received a request from {$message['nick']} to stop responding to requests, messages, and activities. Don't forget to unsuspend me with /msg $nick Resume when finished.", true);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   165
      $channel->part("Logging and presence suspended by {$message['nick']}", true);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   166
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   167
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   168
  else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   169
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   170
    global $nick;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   171
    foreach ( $part_cache as $chan_data )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   172
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   173
      $chan_name = substr($chan_data[0], 1);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   174
      $GLOBALS[$chan_name] = $irc->join($chan_data[0], $chan_data[1]);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   175
      $GLOBALS[$chan_name]->msg("Bot resumed by {$message['nick']}.", true);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   176
      $irc->privmsg('ChanServ', "OP {$chan_data[0]} $nick");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   177
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   178
    $part_cache = array();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   179
  }
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   180
  else if ( in_array($message['nick'], $privileged_list) && preg_match('/^Shutdown(?: (.+))?$/i', $message['message'], $match) && $message['action'] == 'PRIVMSG' )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   181
  {
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   182
    $GLOBALS['_shutdown'] = true;
41
1d854f22ac5a Fixed: echo module was echoing back commands
Dan
parents: 40
diff changeset
   183
    $quitmessage = empty($match[1]) ? "Remote bot shutdown requested by {$message['nick']}" : $match[1];
23
df31a3872d19 Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents: 13
diff changeset
   184
    $irc->close($quitmessage, true);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   185
    return 'BREAK';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   186
  }
40
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   187
  else if ( in_array($message['nick'], $privileged_list) && preg_match('/^re(?:hash|load)?(?:config)?(?: |$)/', $message['message']) )
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   188
  {
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   189
    require('config.php');
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   190
    $GLOBALS['privileged_list'] = $privileged_list;
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   191
    $GLOBALS['alert_list'] = $alert_list;
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   192
    $irc->privmsg($message['nick'], "Reloaded privileged_list and alert_list. privileged = " . str_replace("\n", '', print_r($privileged_list, true)) . "; alert = " . str_replace("\n", '', print_r($alert_list, true)));
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   193
  }
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   194
  else if ( substr($message['message'], 0, 1) == "\x01" && substr($message['message'], -1) == "\x01" )
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   195
  {
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   196
    $msg = trim($message['message'], "\x01");
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   197
    list($ctcp) = explode(' ', $msg);
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   198
    $params = substr($msg, strlen($ctcp)+1);
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   199
    eval(eb_fetch_hook('event_ctcp'));
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   200
  }
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   201
  else if ( $message['action'] == 'PRIVMSG' )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   202
  {
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   203
    eval(eb_fetch_hook('event_privmsg'));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   204
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   205
  else
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   206
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   207
    eval(eb_fetch_hook('event_other'));
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   208
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   209
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   210
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   211
function enanobot_timeout_event($irc)
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   212
{
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   213
  // uh-oh.
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   214
  $irc->close('client ping timeout (restarting connection)');
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   215
  if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   216
  {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   217
    $now = date('r');
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   218
    echo "!!! [$now] Connection timed out; waiting 10 seconds and reconnecting\n";
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   219
  }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   220
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   221
  // re-init
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   222
  global $server, $nick, $user, $name, $pass, $channels, $libirc_channels;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   223
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   224
  // wait until we can get into the server
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   225
  while ( true )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   226
  {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   227
    sleep(10);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   228
    if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   229
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   230
      $now = date('r');
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   231
      echo "... [$now] Attempting reconnect\n";
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   232
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   233
    $conn = @fsockopen($server, 6667, $errno, $errstr, 5);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   234
    if ( $conn )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   235
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   236
      if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   237
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   238
        $now = date('r');
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   239
        echo "!!! [$now] Reconnection successful, ghosting old login (waiting 5 seconds to avoid throttling)\n";
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   240
      }
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   241
      fputs($conn, "QUIT :This bot needs better exception handling. But until then I'm going to need to make repeated TCP connection attempts when my ISP craps out. Sorry :-/\r\n");
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   242
      fclose($conn);
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   243
      sleep(5);
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   244
      break;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   245
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   246
    else
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   247
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   248
      if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   249
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   250
        $now = date('r');
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   251
        echo "!!! [$now] Still waiting for connection to come back up\n";
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   252
      }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   253
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   254
  }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   255
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   256
  $libirc_channels = array();
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   257
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   258
  // we were able to get back in; ask NickServ to GHOST the old nick
38
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 31
diff changeset
   259
  $irc->connect($nick, $user, $name, false);
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   260
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   261
  foreach ( $channels as $channel )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   262
  {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   263
    $libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event');
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   264
    $channel_clean = preg_replace('/^[#&]/', '', $channel);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   265
    $libirc_channels[$channel_clean] =& $libirc_channels[$channel];
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   266
    $irc->privmsg('ChanServ', "OP $channel $nick");
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   267
  }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   268
}
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   269
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   270
if ( $_shutdown )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   271
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   272
  exit(2);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   273
}