| 1 |
<?php
|
| 2 |
// $Id: bot.module,v 1.8 2006/11/16 21:03:01 morbus Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Enables a network and plugin framework for IRC bots.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_help().
|
| 11 |
*/
|
| 12 |
function bot_help($section) {
|
| 13 |
switch ($section) {
|
| 14 |
case 'bot':
|
| 15 |
return '<p>'.t('Listed here are the bot\'s enabled features and settings. Information about the bot\'s features is also available by asking it directly for "help", and then for more detail with "help <feature>" (such as "help Drupal URLs"). This would best be done in a private message, so as not to disrupt regular channel activity.').'</p>';
|
| 16 |
case 'admin/settings/bot':
|
| 17 |
return '<p>'.t('Configure your bot framework with these settings.').'</p>';
|
| 18 |
}
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Implementation of hook_perm().
|
| 23 |
*/
|
| 24 |
function bot_perm() {
|
| 25 |
return array('administer bot');
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Implementation of hook_menu().
|
| 30 |
*/
|
| 31 |
function bot_menu($may_cache) {
|
| 32 |
$items = array();
|
| 33 |
|
| 34 |
if ($may_cache) {
|
| 35 |
$items[] = array(
|
| 36 |
'access' => user_access('access content'),
|
| 37 |
'callback' => 'bot_overview',
|
| 38 |
'description' => t('View the bot\'s enabled features and settings.'),
|
| 39 |
'path' => 'bot',
|
| 40 |
'title' => t('Bot'),
|
| 41 |
);
|
| 42 |
$items[] = array(
|
| 43 |
'access' => user_access('administer bot'),
|
| 44 |
'callback' => 'drupal_get_form',
|
| 45 |
'callback arguments' => 'bot_settings',
|
| 46 |
'description' => t('Configure your bot framework with these settings.'),
|
| 47 |
'path' => 'admin/settings/bot',
|
| 48 |
'title' => t('Bot'),
|
| 49 |
);
|
| 50 |
}
|
| 51 |
|
| 52 |
// minor CSS width fix for module names with more than one word.
|
| 53 |
if (arg(0) == 'admin' && arg(1) == 'build' && arg(2) == 'modules') {
|
| 54 |
drupal_add_css(drupal_get_path('module', 'bot') .'/bot.css');
|
| 55 |
}
|
| 56 |
|
| 57 |
return $items;
|
| 58 |
}
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Framework related messages and features.
|
| 62 |
*
|
| 63 |
* @param $data
|
| 64 |
* The regular $data object prepared by the IRC library.
|
| 65 |
* @param $from_query
|
| 66 |
* Boolean; whether this was a queried request.
|
| 67 |
*/
|
| 68 |
function bot_irc_msg_channel($data, $from_query = 0) {
|
| 69 |
$addressed = $from_query ? '' : variable_get('bot_nickname', 'bot_module') .':? ?';
|
| 70 |
$to = $from_query ? $data->nick : $data->channel;
|
| 71 |
|
| 72 |
// our IRC help interface which piggybacks off of Drupal's hook_help().
|
| 73 |
if (preg_match('/^'.$addressed.'help\??(.*)?/', $data->message, $help_matches)) {
|
| 74 |
if (!$help_matches[1]) { // no specific help was asked for.
|
| 75 |
$irc_features = module_invoke_all('help', 'irc:features');
|
| 76 |
asort($irc_features); // alphabetical listing of features.
|
| 77 |
bot_message($to, t('Detailed information is available by asking for "help <feature>" where <feature> is one of: !features.', array('!features' => implode(', ', $irc_features))));
|
| 78 |
}
|
| 79 |
else { // a specific type of help was required, so load up just that bit of text.
|
| 80 |
$feature_name = 'irc:features#'. preg_replace('/[^\w\d]/', '_', strtolower(trim($help_matches[1])));
|
| 81 |
$feature_help = module_invoke_all('help', $feature_name);
|
| 82 |
bot_message($to, array_shift($feature_help));
|
| 83 |
}
|
| 84 |
}
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* All responses are available via a query.
|
| 89 |
*/
|
| 90 |
function bot_irc_msg_query($data) {
|
| 91 |
bot_irc_msg_channel($data, 1);
|
| 92 |
}
|
| 93 |
|
| 94 |
/**
|
| 95 |
* Send a message to a channel or user.
|
| 96 |
*
|
| 97 |
* @param $to
|
| 98 |
* A channel or user.
|
| 99 |
* @param $message
|
| 100 |
* The message string to send.
|
| 101 |
*/
|
| 102 |
function bot_message($to, $message) {
|
| 103 |
global $irc; // from bot_start.php.
|
| 104 |
$type = strpos($to, '#') == 0 ? 'CHANNEL' : 'QUERY';
|
| 105 |
$irc->message(constant('SMARTIRC_TYPE_'.$type), $to, $message);
|
| 106 |
}
|
| 107 |
|
| 108 |
/**
|
| 109 |
* Displays a quick page listing all the enabled features of the bot.
|
| 110 |
* This is a wrapper around the IRC help features, and spits those helps
|
| 111 |
* verbatim (meaning URLs won't be linked, etc.). @todo Someday, urlfilter.
|
| 112 |
*/
|
| 113 |
function bot_overview() {
|
| 114 |
drupal_add_css(drupal_get_path('module', 'bot') .'/bot.css');
|
| 115 |
$output = '<p>'.t('The bot connects to server %server as nick %name.',
|
| 116 |
array('%server' => variable_get('bot_server', 'irc.freenode.net'),
|
| 117 |
'%name' => variable_get('bot_nickname', 'bot_module'))).'</p>';
|
| 118 |
$output .= '<ul id="bot_features">';
|
| 119 |
|
| 120 |
$irc_features = module_invoke_all('help', 'irc:features');
|
| 121 |
asort($irc_features); // alphabetical listing of features.
|
| 122 |
foreach ($irc_features as $irc_feature) {
|
| 123 |
$feature_help = module_invoke_all('help', 'irc:features#'. preg_replace('/[^\w\d]/', '_', strtolower(trim($irc_feature))));
|
| 124 |
$output .= '<li><span class="bot_feature">'.check_plain($irc_feature).':</span> '.check_plain(array_shift($feature_help)).'</li>';
|
| 125 |
}
|
| 126 |
|
| 127 |
$output .= '</ul>';
|
| 128 |
return $output;
|
| 129 |
}
|
| 130 |
|
| 131 |
/**
|
| 132 |
* Configures the bot framework; system_settings_form().
|
| 133 |
*/
|
| 134 |
function bot_settings() {
|
| 135 |
$form = array();
|
| 136 |
|
| 137 |
$form['bot_server'] = array(
|
| 138 |
'#default_value' => variable_get('bot_server', 'irc.freenode.net'),
|
| 139 |
'#description' => t('Enter the IRC server the bot will connect to.'),
|
| 140 |
'#title' => t('IRC server'),
|
| 141 |
'#type' => 'textfield',
|
| 142 |
);
|
| 143 |
$form['bot_server_port'] = array(
|
| 144 |
'#default_value' => variable_get('bot_server_port', 6667),
|
| 145 |
'#description' => t('Enter the IRC port of the IRC server. 6667 is the most common configuration.'),
|
| 146 |
'#title' => t('IRC server port'),
|
| 147 |
'#type' => 'textfield',
|
| 148 |
);
|
| 149 |
$form['bot_nickname'] = array(
|
| 150 |
'#default_value' => variable_get('bot_nickname', 'bot_module'),
|
| 151 |
'#description' => t('Enter the nickname the bot will login as.'),
|
| 152 |
'#title' => t('Bot nickname'),
|
| 153 |
'#type' => 'textfield',
|
| 154 |
);
|
| 155 |
$form['bot_channels'] = array(
|
| 156 |
'#default_value' => variable_get('bot_channels', '#test'),
|
| 157 |
'#description' => t('Enter a comma-separated list of channels the bot will automatically join.'),
|
| 158 |
'#rows' => 3,
|
| 159 |
'#title' => t('Bot channels'),
|
| 160 |
'#type' => 'textarea',
|
| 161 |
);
|
| 162 |
$form['bot_debugging'] = array(
|
| 163 |
'#default_value' => variable_get('bot_debugging', 0), // spits out a TON of (useful) stuff.
|
| 164 |
'#description' => t('If checked, send Net_SmartIRC\'s SMARTIRC_DEBUG_ALL to the shell.'),
|
| 165 |
'#options' => array('0' => t('No'), '1' => t('Yes')),
|
| 166 |
'#title' => t('Enable debugging'),
|
| 167 |
'#type' => 'checkbox',
|
| 168 |
);
|
| 169 |
|
| 170 |
return system_settings_form($form);
|
| 171 |
}
|