| 1 |
<?php
|
| 2 |
// $Id: bot_start.php,v 1.1 2006/11/12 17:01:25 morbus Exp $
|
| 3 |
|
| 4 |
/*
|
| 5 |
* @file
|
| 6 |
* Wrapper around the Drupal bootstrap and Net_SmartIRC. The first half is
|
| 7 |
* devoted to fooling Drupal into thinking it's being run through the web,
|
| 8 |
* and the other half inserts the IRC library into Drupal's hook system.
|
| 9 |
*/
|
| 10 |
|
| 11 |
set_time_limit(0); // ignore max_execution_time.
|
| 12 |
$script_name = array_shift($_SERVER['argv']);
|
| 13 |
|
| 14 |
// --root allows us to keep this script in the bot.module directory
|
| 15 |
// without moving it to the root of the Drupal install, and --url
|
| 16 |
// is required to trick Drupal into thinking it's a web request.
|
| 17 |
// --url should be the same thing as your Drupal $base_url.
|
| 18 |
while ($param = array_shift($_SERVER['argv'])) {
|
| 19 |
switch ($param) {
|
| 20 |
case '--root':
|
| 21 |
$drupal_root = array_shift($_SERVER['argv']);
|
| 22 |
if (is_dir($drupal_root)) { chdir($drupal_root); }
|
| 23 |
else { die("ERROR: $drupal_root not found.\n"); }
|
| 24 |
break;
|
| 25 |
|
| 26 |
case '--url':
|
| 27 |
$drupal_base_url = parse_url(array_shift($_SERVER['argv']));
|
| 28 |
$_SERVER['HTTP_HOST'] = $drupal_base_url['host'];
|
| 29 |
$_SERVER['PHP_SELF'] = $drupal_base_url['path'].'/'.$script_name;
|
| 30 |
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
|
| 31 |
$_SERVER['REMOTE_ADDR'] = NULL; // any values here do rather...
|
| 32 |
$_SERVER['REQUEST_METHOD'] = NULL; // ...odd things. uh huh.
|
| 33 |
break;
|
| 34 |
}
|
| 35 |
}
|
| 36 |
|
| 37 |
// load in our required libraries.
|
| 38 |
require_once './includes/bootstrap.inc';
|
| 39 |
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
|
| 40 |
require_once('Net/SmartIRC.php');
|
| 41 |
|
| 42 |
// initialize the bot with some sane defaults.
|
| 43 |
global $irc; $bot = new drupal_wrapper(); $irc = new Net_SmartIRC();
|
| 44 |
$irc->setDebug( variable_get('bot_debugging', 0) ? SMARTIRC_DEBUG_ALL : SMARTIRC_DEBUG_NONE );
|
| 45 |
$irc->setAutoReconnect(TRUE); // reconnect to the server if disconnected.
|
| 46 |
$irc->setAutoRetry(TRUE); // retry if a server connection fails.
|
| 47 |
$irc->setChannelSyncing(TRUE); // keep a list of joined users per channel.
|
| 48 |
$irc->setUseSockets(TRUE); // uses real sockets instead of fsock().
|
| 49 |
|
| 50 |
// send every message type the library supports to our wrapper class.
|
| 51 |
// we can automate the creation of these actionhandlers, but not the
|
| 52 |
// class methods below (only PHP 5 supports default methods easily).
|
| 53 |
$irc_message_types = array(
|
| 54 |
'UNKNOWN', 'CHANNEL', 'QUERY', 'CTCP', 'NOTICE', 'WHO',
|
| 55 |
'JOIN', 'INVITE', 'ACTION', 'TOPICCHANGE', 'NICKCHANGE', 'KICK',
|
| 56 |
'QUIT', 'LOGIN', 'INFO', 'LIST', 'NAME', 'MOTD',
|
| 57 |
'MODECHANGE', 'PART', 'ERROR', 'BANLIST', 'TOPIC', 'NONRELEVANT',
|
| 58 |
'WHOIS', 'WHOWAS', 'USERMODE', 'CHANNELMODE', 'CTCP_REQUEST', 'CTCP_REPLY',
|
| 59 |
);
|
| 60 |
|
| 61 |
foreach ($irc_message_types as $irc_message_type) {
|
| 62 |
$irc->registerActionhandler(constant('SMARTIRC_TYPE_'.$irc_message_type), '.*', $bot, 'invoke_irc_msg_'.strtolower($irc_message_type));
|
| 63 |
}
|
| 64 |
|
| 65 |
// connect and begin listening.
|
| 66 |
$irc->connect( variable_get('bot_server', 'irc.freenode.net'), variable_get('bot_server_port', 6667) );
|
| 67 |
$irc->login( variable_get('bot_nickname', 'bot_module'), variable_get('bot_nickname', 'bot_module').' :http://drupal.org/project/bot', 8, variable_get('bot_nickname', 'bot_module') );
|
| 68 |
$irc->join(preg_split('/\s*,\s*/', variable_get('bot_channels', '#test')));
|
| 69 |
$irc->listen(); // go into the forever loop - no code after this is run.
|
| 70 |
$irc->disconnect(); // if we stop listening, disconnect properly.
|
| 71 |
|
| 72 |
// pass off IRC messages to our modules via Drupal's hook system.
|
| 73 |
class drupal_wrapper {
|
| 74 |
function invoke_irc_msg_unknown(&$irc, &$data) { module_invoke_all('irc_msg_unknown', $data); }
|
| 75 |
function invoke_irc_msg_channel(&$irc, &$data) { module_invoke_all('irc_msg_channel', $data); }
|
| 76 |
function invoke_irc_msg_query(&$irc, &$data) { module_invoke_all('irc_msg_query', $data); }
|
| 77 |
function invoke_irc_msg_ctcp(&$irc, &$data) { module_invoke_all('irc_msg_ctcp', $data); }
|
| 78 |
function invoke_irc_msg_notice(&$irc, &$data) { module_invoke_all('irc_msg_notice', $data); }
|
| 79 |
function invoke_irc_msg_who(&$irc, &$data) { module_invoke_all('irc_msg_who', $data); }
|
| 80 |
function invoke_irc_msg_join(&$irc, &$data) { module_invoke_all('irc_msg_join', $data); }
|
| 81 |
function invoke_irc_msg_invite(&$irc, &$data) { module_invoke_all('irc_msg_invite', $data); }
|
| 82 |
function invoke_irc_msg_action(&$irc, &$data) { module_invoke_all('irc_msg_action', $data); }
|
| 83 |
function invoke_irc_msg_topicchange(&$irc, &$data) { module_invoke_all('irc_msg_topicchange', $data); }
|
| 84 |
function invoke_irc_msg_nickchange(&$irc, &$data) { module_invoke_all('irc_msg_nickchange', $data); }
|
| 85 |
function invoke_irc_msg_kick(&$irc, &$data) { module_invoke_all('irc_msg_kick', $data); }
|
| 86 |
function invoke_irc_msg_quit(&$irc, &$data) { module_invoke_all('irc_msg_quit', $data); }
|
| 87 |
function invoke_irc_msg_login(&$irc, &$data) { module_invoke_all('irc_msg_login', $data); }
|
| 88 |
function invoke_irc_msg_info(&$irc, &$data) { module_invoke_all('irc_msg_info', $data); }
|
| 89 |
function invoke_irc_msg_list(&$irc, &$data) { module_invoke_all('irc_msg_list', $data); }
|
| 90 |
function invoke_irc_msg_name(&$irc, &$data) { module_invoke_all('irc_msg_name', $data); }
|
| 91 |
function invoke_irc_msg_motd(&$irc, &$data) { module_invoke_all('irc_msg_motd', $data); }
|
| 92 |
function invoke_irc_msg_modechange(&$irc, &$data) { module_invoke_all('irc_msg_modechange', $data); }
|
| 93 |
function invoke_irc_msg_part(&$irc, &$data) { module_invoke_all('irc_msg_part', $data); }
|
| 94 |
function invoke_irc_msg_error(&$irc, &$data) { module_invoke_all('irc_msg_error', $data); }
|
| 95 |
function invoke_irc_msg_banlist(&$irc, &$data) { module_invoke_all('irc_msg_banlist', $data); }
|
| 96 |
function invoke_irc_msg_topic(&$irc, &$data) { module_invoke_all('irc_msg_topic', $data); }
|
| 97 |
function invoke_irc_msg_nonrelevant(&$irc, &$data) { module_invoke_all('irc_msg_nonrelevant', $data); }
|
| 98 |
function invoke_irc_msg_whois(&$irc, &$data) { module_invoke_all('irc_msg_whois', $data); }
|
| 99 |
function invoke_irc_msg_whowas(&$irc, &$data) { module_invoke_all('irc_msg_whowas', $data); }
|
| 100 |
function invoke_irc_msg_usermode(&$irc, &$data) { module_invoke_all('irc_msg_usermode', $data); }
|
| 101 |
function invoke_irc_msg_channelmode(&$irc, &$data) { module_invoke_all('irc_msg_channelmode', $data); }
|
| 102 |
function invoke_irc_msg_ctcp_request(&$irc, &$data) { module_invoke_all('irc_msg_ctcp_request', $data); }
|
| 103 |
function invoke_irc_msg_ctcp_reply(&$irc, &$data) { module_invoke_all('irc_msg_ctcp_reply', $data); }
|
| 104 |
}
|
| 105 |
|