Exemple #1
0
 /**
  * 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();
 }
    function help_cmd(&$irc, &$data)
    {
        $message = sprintf('For ContriBot Help, see %s', HELP_URL);
        $irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, $message);
    }
}
/**
 * Instantiate our bot class and the SmartIRC framework
 */
$bot = new bot();
$doc_bot = new DocBot();
$irc = new Net_SmartIRC();
/**
 * Set connection-wide configurations
 */
$irc->setDebugLevel(SMARTIRC_DEBUG_ALL);
// Set debug mode
$irc->setUseSockets(true);
// We want to use actual sockets, if this is false fsock will be used, which is not as ideal
$irc->setChannelSyncing(true);
// Channel sync allows us to get user details which we use in our logs, this is how we can check if users are in the channel or not
/**
 * Set up hooks for events to trigger on
 */
$irc->registerActionHandler(SMARTIRC_TYPE_CHANNEL, '/./', $bot, 'channel_query');
$irc->registerActionHandler(SMARTIRC_TYPE_ACTION, '/./', $bot, 'channel_query');
$irc->registerActionHandler(SMARTIRC_TYPE_KICK, '/(.*)/', $bot, 'log_kick');
$irc->registerActionHandler(SMARTIRC_TYPE_PART, '/(.*)/', $bot, 'log_part');
$irc->registerActionHandler(SMARTIRC_TYPE_QUIT, '/(.*)/', $bot, 'log_quit');
$irc->registerActionHandler(SMARTIRC_TYPE_JOIN, '/(.*)/', $bot, 'log_join');
$irc->registerActionHandler(SMARTIRC_TYPE_NICKCHANGE, '/(.*)/', $bot, 'log_nickchange');