enanobot.php
author Dan
Tue, 20 Jan 2009 22:08:17 -0500
changeset 52 a8f0e99883d1
parent 51 508400fc5282
permissions -rw-r--r--
Web interface can now update with AJAX
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');
51
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    78
require('permissions.php');
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    79
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    80
if ( !isset($permissions) )
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    81
{
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    82
  foreach ( $privileged_list as $user )
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    83
  {
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    84
    $permissions[$user] = array('admin');
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    85
  }
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    86
  if ( isset($alert_list) )
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    87
  {
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    88
    foreach ( $alert_list as $user )
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    89
    {
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    90
      $permissions[$user] = array('admin', 'alert');
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    91
    }
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    92
  }
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
    93
}
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    94
40
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
    95
$enanobot_version = '0.5-unstable';
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
    96
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    97
@ini_set('display_errors', 'on');
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    98
error_reporting(E_ALL);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
    99
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   100
// load modules
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   101
foreach ( $modules as $module )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   102
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   103
  $modulefile = "modules/$module.php";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   104
  if ( file_exists($modulefile) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   105
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   106
    require($modulefile);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   107
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   108
}
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   109
23
df31a3872d19 Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents: 13
diff changeset
   110
mysql_reconnect();
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   111
23
df31a3872d19 Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents: 13
diff changeset
   112
eval(eb_fetch_hook('startup_early'));
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   113
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   114
$libirc_channels = array();
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   115
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   116
$irc = new Request_IRC($server);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   117
$irc->connect($nick, $user, $name, $pass);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   118
$irc->set_privmsg_handler('enanobot_privmsg_event');
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   119
$irc->set_timeout_handlers(false, 'enanobot_timeout_event');
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   120
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   121
foreach ( $channels as $channel )
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   122
{
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   123
  $libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event');
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   124
  $channel_clean = preg_replace('/^[#&]/', '', $channel);
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   125
  $libirc_channels[$channel_clean] =& $libirc_channels[$channel];
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   126
  $irc->privmsg('ChanServ', "OP $channel $nick");
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   127
}
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   128
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   129
$irc->event_loop();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   130
$irc->close();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   131
mysql_close($mysql_conn);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   132
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   133
function enanobot_channel_event($sockdata, $chan)
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   134
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   135
  global $irc, $nick, $mysql_conn, $privileged_list;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   136
  $sockdata = trim($sockdata);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   137
  $message = Request_IRC::parse_message($sockdata);
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   138
  $channelname = $chan->get_channel_name();
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   139
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   140
  eval(eb_fetch_hook('event_raw_message'));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   141
  
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   142
  switch ( $message['action'] )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   143
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   144
    case 'JOIN':
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   145
      eval(eb_fetch_hook('event_join'));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   146
      break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   147
    case 'PART':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   148
      eval(eb_fetch_hook('event_part'));
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   149
      break;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   150
    case 'PRIVMSG':
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   151
      enanobot_process_channel_message($sockdata, $chan, $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   152
      break;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   153
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   154
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   155
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   156
function enanobot_process_channel_message($sockdata, $chan, $message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   157
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   158
  global $irc, $nick, $mysql_conn, $privileged_list;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   159
  
51
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   160
  if ( strpos($message['message'], $nick) && !check_permissions($message['nick'], array('context' => 'channel', 'channel' => $chan->get_channel_name())) && $message['nick'] != $nick )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   161
  {
9
4192954c29d1 Finalization of merge from devel branch
Dan
parents: 8
diff changeset
   162
    $target_nick =& $message['nick'];
13
f073c94a1477 Disabled annoying "I'm only a bot" message
Dan
parents: 9
diff changeset
   163
    // $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
   164
  }
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   165
  else
1
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   166
  {
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   167
    eval(eb_fetch_hook('event_channel_msg'));
1
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   168
  }
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   169
}
739423b66116 Added support for logging mode messages and join/part messages
Dan
parents: 0
diff changeset
   170
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   171
function enanobot_privmsg_event($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   172
{
6
a802bb5b3e08 Made the channel list completely dynamic now
Dan
parents: 4
diff changeset
   173
  global $privileged_list, $irc, $nick;
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   174
  static $part_cache = array();
51
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   175
  if ( $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' && check_permissions($message['nick'], array('context' => 'suspend'), false) )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   176
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   177
    foreach ( $irc->channels as $channel )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   178
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   179
      $part_cache[] = array($channel->get_channel_name(), $channel->get_handler());
51
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   180
      $channel->part("suspended by {$message['nick']}", true);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   181
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   182
  }
51
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   183
  else if ( $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' && check_permissions($message['nick'], array('context' => 'suspend'), false) )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   184
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   185
    global $nick;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   186
    foreach ( $part_cache as $chan_data )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   187
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   188
      $chan_name = substr($chan_data[0], 1);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   189
      $GLOBALS[$chan_name] = $irc->join($chan_data[0], $chan_data[1]);
51
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   190
      $GLOBALS[$chan_name]->msg("(resumed by {$message['nick']})", true);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   191
      $irc->privmsg('ChanServ', "OP {$chan_data[0]} $nick");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   192
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   193
    $part_cache = array();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   194
  }
51
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   195
  else if ( preg_match('/^Shutdown(?: (.+))?$/i', $message['message'], $match) && $message['action'] == 'PRIVMSG' && check_permissions($message['nick'], array('context' => 'shutdown'), false) )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   196
  {
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   197
    $GLOBALS['_shutdown'] = true;
41
1d854f22ac5a Fixed: echo module was echoing back commands
Dan
parents: 40
diff changeset
   198
    $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
   199
    $irc->close($quitmessage, true);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   200
    return 'BREAK';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   201
  }
51
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   202
  else if ( preg_match('/^re(?:hash|load)?(?:config)?(?: |$)/', $message['message']) && check_permissions($message['nick'], array('context' => 'rehash'), false) )
40
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   203
  {
51
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   204
    $oldnick = $GLOBALS['nick'];
40
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   205
    require('config.php');
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   206
    $GLOBALS['privileged_list'] = $privileged_list;
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   207
    $GLOBALS['alert_list'] = $alert_list;
49
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   208
    $GLOBALS['channels'] = $channels;
51
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   209
    $GLOBALS['permissions'] = $permissions;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   210
    if ( $nick != $oldnick )
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   211
    {
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   212
      $irc->change_nick($nick, $pass);
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   213
      $GLOBALS['nick'] = $nick;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   214
      $GLOBALS['pass'] = $pass;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 50
diff changeset
   215
    }
49
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   216
    $in = array();
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   217
    foreach ( $irc->channels as $channel )
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   218
    {
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   219
      $channame = $channel->get_channel_name();
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   220
      if ( !in_array($channame, $channels) )
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   221
      {
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   222
        $channel->part("Leaving");
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   223
      }
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   224
      else
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   225
      {
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   226
        $in[] = $channame;
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   227
      }
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   228
    }
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   229
    unset($channel);
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   230
    foreach ( $channels as $channel )
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   231
    {
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   232
      if ( !in_array($channel, $in) )
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   233
      {
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   234
        $GLOBALS[ preg_replace('/^(#|&)/', '', $channel) ] = $irc->join($channel, 'enanobot_channel_event');
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   235
      }
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   236
    }
4c4d69b2cd4d Rehash: added support for channel changing
Dan
parents: 41
diff changeset
   237
    $irc->privmsg($message['nick'], "Config has been reloaded.");
40
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   238
  }
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   239
  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
   240
  {
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   241
    $msg = trim($message['message'], "\x01");
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   242
    list($ctcp) = explode(' ', $msg);
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   243
    $params = substr($msg, strlen($ctcp)+1);
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   244
    eval(eb_fetch_hook('event_ctcp'));
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   245
  }
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   246
  else if ( $message['action'] == 'PRIVMSG' )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   247
  {
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   248
    eval(eb_fetch_hook('event_privmsg'));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   249
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   250
  else
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   251
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   252
    eval(eb_fetch_hook('event_other'));
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   253
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   254
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   255
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   256
function enanobot_timeout_event($irc)
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   257
{
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   258
  // uh-oh.
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   259
  $irc->close('client ping timeout (restarting connection)');
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   260
  if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   261
  {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   262
    $now = date('r');
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   263
    echo "!!! [$now] Connection timed out; waiting 10 seconds and reconnecting\n";
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   264
  }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   265
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   266
  // re-init
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   267
  global $server, $nick, $user, $name, $pass, $channels, $libirc_channels;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   268
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   269
  // wait until we can get into the server
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   270
  while ( true )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   271
  {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   272
    sleep(10);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   273
    if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   274
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   275
      $now = date('r');
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   276
      echo "... [$now] Attempting reconnect\n";
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   277
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   278
    $conn = @fsockopen($server, 6667, $errno, $errstr, 5);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   279
    if ( $conn )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   280
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   281
      if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   282
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   283
        $now = date('r');
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   284
        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
   285
      }
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   286
      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
   287
      fclose($conn);
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   288
      sleep(5);
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   289
      break;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   290
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   291
    else
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   292
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   293
      if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   294
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   295
        $now = date('r');
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   296
        echo "!!! [$now] Still waiting for connection to come back up\n";
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   297
      }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   298
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   299
  }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   300
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   301
  $libirc_channels = array();
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   302
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   303
  // 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
   304
  $irc->connect($nick, $user, $name, false);
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   305
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   306
  foreach ( $channels as $channel )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   307
  {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   308
    $libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event');
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   309
    $channel_clean = preg_replace('/^[#&]/', '', $channel);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   310
    $libirc_channels[$channel_clean] =& $libirc_channels[$channel];
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   311
    $irc->privmsg('ChanServ', "OP $channel $nick");
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   312
  }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   313
}
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 23
diff changeset
   314
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   315
if ( $_shutdown )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   316
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   317
  exit(2);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 7
diff changeset
   318
}