} } public function _joinChannels(&$irc) { global $channels; foreach ($channels as $prj_id => $options) { foreach ($options as $chan => $categories) { $irc->join($chan); } } } } 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');
/** * Create IRC Bot, connect, login and listen for events, and finally disconnect. */ public function run() { $config = $this->config; // setup logging // redirect stderr to a log ini_set('log_errors', 'On'); ini_set('error_log', $config['error_log']); $this->irc = $irc = new Net_SmartIRC(); if (isset($config['debuglevel'])) { $irc->setDebugLevel($config['debuglevel']); } if (isset($config['logfile'])) { $irc->setLogdestination(SMARTIRC_FILE); $irc->setLogfile($config['logfile']); } // reconnect is poorly designed, do not use it // @see https://pear.php.net/bugs/bug.php?id=20974 //$irc->setAutoRetry(true); $irc->setAutoRetry(false); $irc->setAutoRetryMax(PHP_INT_MAX); $irc->setReconnectDelay(10000); $irc->setReceiveTimeout(600); $irc->setTransmitTimeout(600); // enable user and channel syncing, // users are accessible via $irc->user array, i.e $irc->user['meebey']->host; $irc->setChannelSyncing(true); $irc->setUserSyncing(true); $this->registerHandlers($this->irc); $irc->connect($config['hostname'], $config['port']); if (empty($config['username'])) { $irc->login($config['nickname'], $config['realname']); } elseif (empty($config['password'])) { $irc->login($config['nickname'], $config['realname'], 0, $config['username']); } else { $irc->login($config['nickname'], $config['realname'], 0, $config['username'], $config['password']); } $this->joinChannels($irc); // loop forever, reconnect and retry // @see https://pear.php.net/bugs/bug.php?id=20974 while (!$this->shutdown) { $irc->listen(); $irc->reconnect(); } $irc->disconnect(); }