enanobot.php
author Dan
Tue, 30 Dec 2008 06:16:54 -0500
changeset 37 c3cbefda414a
parent 31 d75124700259
child 38 e6a4b7f91e91
permissions -rw-r--r--
Merging apparently split branches
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
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    79
@ini_set('display_errors', 'on');
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    80
error_reporting(E_ALL);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    81
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    82
// load modules
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    83
foreach ( $modules as $module )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    84
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    85
  $modulefile = "modules/$module.php";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    86
  if ( file_exists($modulefile) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    87
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    88
    require($modulefile);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    89
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    90
}
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    91
23
df31a3872d19 Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents: 13
diff changeset
    92
mysql_reconnect();
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
eval(eb_fetch_hook('startup_early'));
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    95
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
    96
$libirc_channels = array();
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
    97
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    98
$irc = new Request_IRC($server);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    99
$irc->connect($nick, $user, $name, $pass);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   100
$irc->set_privmsg_handler('enanobot_privmsg_event');
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   101
$irc->set_timeout_handlers(false, 'enanobot_timeout_event');
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   102
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   103
foreach ( $channels as $channel )
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
  $libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event');
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   106
  $channel_clean = preg_replace('/^[#&]/', '', $channel);
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   107
  $libirc_channels[$channel_clean] =& $libirc_channels[$channel];
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   108
  $irc->privmsg('ChanServ', "OP $channel $nick");
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   109
}
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   110
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   111
$irc->event_loop();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   112
$irc->close();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   113
mysql_close($mysql_conn);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   114
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   115
function enanobot_channel_event($sockdata, $chan)
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   116
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   117
  global $irc, $nick, $mysql_conn, $privileged_list;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   118
  $sockdata = trim($sockdata);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   119
  $message = Request_IRC::parse_message($sockdata);
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   120
  $channelname = $chan->get_channel_name();
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   121
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   122
  eval(eb_fetch_hook('event_raw_message'));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   123
  
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   124
  switch ( $message['action'] )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   125
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   126
    case 'JOIN':
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   127
      eval(eb_fetch_hook('event_join'));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   128
      break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   129
    case 'PART':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   130
      eval(eb_fetch_hook('event_part'));
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   131
      break;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   132
    case 'PRIVMSG':
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   133
      enanobot_process_channel_message($sockdata, $chan, $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   134
      break;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   135
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   136
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   137
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   138
function enanobot_process_channel_message($sockdata, $chan, $message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   139
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   140
  global $irc, $nick, $mysql_conn, $privileged_list;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   141
  
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   142
  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
   143
  {
9
4192954c29d1 Finalization of merge from devel branch
Dan
parents: 8
diff changeset
   144
    $target_nick =& $message['nick'];
13
f073c94a1477 Disabled annoying "I'm only a bot" message
Dan
parents: 9
diff changeset
   145
    // $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
   146
  }
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   147
  else
1
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   148
  {
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   149
    eval(eb_fetch_hook('event_channel_msg'));
1
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   150
  }
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   151
}
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   152
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   153
function enanobot_privmsg_event($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   154
{
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   155
  global $privileged_list, $irc, $nick;
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   156
  static $part_cache = array();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   157
  if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   158
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   159
    foreach ( $irc->channels as $channel )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   160
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   161
      $part_cache[] = array($channel->get_channel_name(), $channel->get_handler());
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   162
      $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
   163
      $channel->part("Logging and presence suspended by {$message['nick']}", true);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   164
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   165
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   166
  else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   167
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   168
    global $nick;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   169
    foreach ( $part_cache as $chan_data )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   170
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   171
      $chan_name = substr($chan_data[0], 1);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   172
      $GLOBALS[$chan_name] = $irc->join($chan_data[0], $chan_data[1]);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   173
      $GLOBALS[$chan_name]->msg("Bot resumed by {$message['nick']}.", true);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   174
      $irc->privmsg('ChanServ', "OP {$chan_data[0]} $nick");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   175
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   176
    $part_cache = array();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   177
  }
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   178
  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
   179
  {
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   180
    $GLOBALS['_shutdown'] = true;
23
df31a3872d19 Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents: 13
diff changeset
   181
    $quitmessage = empty($match[1]) ? "Remote bot shutdown ordered by {$message['nick']}" : $match[1];
df31a3872d19 Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents: 13
diff changeset
   182
    $irc->close($quitmessage, true);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   183
    return 'BREAK';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   184
  }
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   185
  else if ( $message['action'] == 'PRIVMSG' )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   186
  {
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   187
    eval(eb_fetch_hook('event_privmsg'));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   188
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   189
  else
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   190
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   191
    eval(eb_fetch_hook('event_other'));
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   192
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   193
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   194
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   195
function enanobot_timeout_event($irc)
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   196
{
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   197
  // uh-oh.
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   198
  $irc->close('client ping timeout (restarting connection)');
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   199
  if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   200
  {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   201
    $now = date('r');
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   202
    echo "!!! [$now] Connection timed out; waiting 10 seconds and reconnecting\n";
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   203
  }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   204
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   205
  // re-init
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   206
  global $server, $nick, $user, $name, $pass, $channels, $libirc_channels;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   207
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   208
  // wait until we can get into the server
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   209
  while ( true )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   210
  {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   211
    sleep(10);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   212
    if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   213
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   214
      $now = date('r');
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   215
      echo "... [$now] Attempting reconnect\n";
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   216
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   217
    $conn = @fsockopen($server, 6667, $errno, $errstr, 5);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   218
    if ( $conn )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   219
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   220
      if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   221
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   222
        $now = date('r');
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   223
        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
   224
      }
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   225
      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
   226
      fclose($conn);
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   227
      sleep(5);
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   228
      break;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   229
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   230
    else
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   231
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   232
      if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   233
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   234
        $now = date('r');
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   235
        echo "!!! [$now] Still waiting for connection to come back up\n";
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   236
      }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   237
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   238
  }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   239
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   240
  $libirc_channels = array();
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   241
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   242
  // we were able to get back in; ask NickServ to GHOST the old nick
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   243
  $irc->connect("$nick`gh", $user, $name, false);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   244
  $irc->privmsg('NickServ', "GHOST $nick $pass");
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   245
  $irc->change_nick($nick, $pass);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   246
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   247
  foreach ( $channels as $channel )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   248
  {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   249
    $libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event');
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   250
    $channel_clean = preg_replace('/^[#&]/', '', $channel);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   251
    $libirc_channels[$channel_clean] =& $libirc_channels[$channel];
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   252
    $irc->privmsg('ChanServ', "OP $channel $nick");
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
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   256
if ( $_shutdown )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   257
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   258
  exit(2);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   259
}