/** * Register commands. * All public final methods are registered. the method name is the prefix of the command * * @param Net_SmartIRC $irc */ public function register(Net_SmartIRC $irc) { // register timer to handle events from database $irc->registerTimehandler(3000, $this, 'notifyEvents'); // register all commands $methods = $this->getMethods(); foreach ($methods as $methodName => $method) { $commandName = $this->getCommandName($methodName); $regex = "^!?{$commandName}\\b"; $irc->registerActionhandler(SMARTIRC_TYPE_QUERY, $regex, $this, $methodName); } }
$irc->setDebug(variable_get('bot_debugging', 0) ? SMARTIRC_DEBUG_ALL : SMARTIRC_DEBUG_NONE); // the (boolean) here is required, as Net_SmartIRC doesn't respect a FAPI checkbox value of 1, only TRUE. $irc->setAutoReconnect((bool) variable_get('bot_auto_reconnect', 1)); // reconnect to the server if disconnected. $irc->setAutoRetry((bool) variable_get('bot_auto_retry', 1)); // retry if a server connection fails. $irc->setChannelSyncing(TRUE); // keep a list of joined users per channel. $irc->setUseSockets(TRUE); // uses real sockets instead of fsock(). // send every message type the library supports to our wrapper class. // we can automate the creation of these actionhandlers, but not the // class methods below (only PHP 5 supports default methods easily). $irc_message_types = array('UNKNOWN', 'CHANNEL', 'QUERY', 'CTCP', 'NOTICE', 'WHO', 'JOIN', 'INVITE', 'ACTION', 'TOPICCHANGE', 'NICKCHANGE', 'KICK', 'QUIT', 'LOGIN', 'INFO', 'LIST', 'NAME', 'MOTD', 'MODECHANGE', 'PART', 'ERROR', 'BANLIST', 'TOPIC', 'NONRELEVANT', 'WHOIS', 'WHOWAS', 'USERMODE', 'CHANNELMODE', 'CTCP_REQUEST', 'CTCP_REPLY'); foreach ($irc_message_types as $irc_message_type) { $irc->registerActionhandler(constant('SMARTIRC_TYPE_' . $irc_message_type), '.*', $bot, 'invoke_irc_msg_' . strtolower($irc_message_type)); } // set up a timers similar to Drupal's hook_cron(), multiple types. I would have // liked to just pass a parameter to a single function, but SmartIRC can't do that. $irc->registerTimehandler(300000, $bot, 'invoke_irc_bot_cron'); // 5 minutes. $irc->registerTimehandler(60000, $bot, 'invoke_irc_bot_cron_faster'); // 1 minute. $irc->registerTimehandler(15000, $bot, 'invoke_irc_bot_cron_fastest'); // 15 seconds. // connect and begin listening. $irc->connect(variable_get('bot_server', 'irc.freenode.net'), variable_get('bot_server_port', 6667)); $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'), variable_get('bot_password', '') != '' ? variable_get('bot_password', '') : NULL); // channel joining has moved to bot_irc_bot_cron_fastest(). // read that function for the rationale, and what we gain from it. $irc->listen();
} } } require_once 'Net/SmartIRC.php'; $bot = new Eventum_Bot(); $irc = new Net_SmartIRC(); $irc->setLogdestination(SMARTIRC_FILE); $irc->setLogfile(APP_IRC_LOG); $irc->setUseSockets(true); $irc->setAutoReconnect(true); $irc->setAutoRetry(true); $irc->setReceiveTimeout(600); $irc->setTransmitTimeout(600); $irc->registerTimehandler(3000, $bot, 'notifyEvents'); // methods that keep track of who is authenticated $irc->registerActionhandler(SMARTIRC_TYPE_QUERY, '^!?list-auth', $bot, 'listAuthenticatedUsers'); $irc->registerActionhandler(SMARTIRC_TYPE_NICKCHANGE, '.*', $bot, '_updateAuthenticatedUser'); $irc->registerActionhandler(SMARTIRC_TYPE_KICK | SMARTIRC_TYPE_QUIT | SMARTIRC_TYPE_PART, '.*', $bot, '_removeAuthenticatedUser'); $irc->registerActionhandler(SMARTIRC_TYPE_LOGIN, '.*', $bot, '_joinChannels'); // real bot commands $irc->registerActionhandler(SMARTIRC_TYPE_QUERY, '^!?help', $bot, 'listAvailableCommands'); $irc->registerActionhandler(SMARTIRC_TYPE_QUERY, '^!?auth ', $bot, 'authenticate'); $irc->registerActionhandler(SMARTIRC_TYPE_QUERY, '^!?clock', $bot, 'clockUser'); $irc->registerActionhandler(SMARTIRC_TYPE_QUERY, '^!?list-clocked-in', $bot, 'listClockedInUsers'); $irc->registerActionhandler(SMARTIRC_TYPE_QUERY, '^!?list-quarantined', $bot, 'listQuarantinedIssues'); $irc->connect($irc_server_hostname, $irc_server_port); if (empty($username)) { $irc->login($nickname, $realname); } elseif (empty($password)) { $irc->login($nickname, $realname, 0, $username); } else {
/** * @param Net_SmartIRC $irc */ private function registerHandlers(Net_SmartIRC $irc) { // doing it cleanly with dispatch is not possible currently // @see http://pear.php.net/bugs/bug.php?id=20973 if ($this->have_pcntl) { declare (ticks=1); //$irc->registerTimehandler(1000, $this, 'signalDispatch'); } // methods that keep track of who is authenticated $irc->registerActionhandler(SMARTIRC_TYPE_NICKCHANGE, '.*', $this, 'updateAuthenticatedUser'); $irc->registerActionhandler(SMARTIRC_TYPE_KICK | SMARTIRC_TYPE_QUIT | SMARTIRC_TYPE_PART, '.*', $this, 'removeAuthenticatedUser'); // register bot commands $commands = new BotCommands($this, $irc, $this->config); $commands->register($irc); }