Skip to content

Instantly share code, notes, and snippets.

@addshore
Created January 20, 2015 15:18
Show Gist options
  • Save addshore/1d1aec0706bb5424570e to your computer and use it in GitHub Desktop.
Save addshore/1d1aec0706bb5424570e to your computer and use it in GitHub Desktop.
Phergie Hack
#!/usr/bin/env php
<?php
use Phergie\Irc\Bot\React\Bot;
use Phergie\Irc\Bot\React\EventQueueInterface as Queue;
use Phergie\Irc\Client\React\Client;
use Phergie\Irc\Client\React\WriteStream;
use Phergie\Irc\Connection;
use Phergie\Irc\ConnectionInterface;
use Phergie\Irc\Event\ServerEvent;
use Phergie\Irc\Event\UserEventInterface as UserEvent;
use Phergie\Irc\Plugin\React\AutoJoin\Plugin as AutoJoinPlugin;
use Phergie\Irc\Plugin\React\EventFilter\AndFilter;
use Phergie\Irc\Plugin\React\EventFilter\ChannelFilter;
use Phergie\Irc\Plugin\React\EventFilter\ConnectionFilter;
use Phergie\Irc\Plugin\React\EventFilter\Plugin as EventFilterPlugin;
use Phergie\Irc\Plugin\React\Pong\Plugin as PongPlugin;
use \Phergie\Irc\Plugin\React\Command\Plugin as CommandPlugin;
use Psr\Log\LoggerInterface;
/*
"require": {
"phergie/phergie-irc-bot-react": "1.0.*",
"phergie/phergie-irc-plugin-react-pong": "1.1.*",
"phergie/phergie-irc-plugin-react-autojoin": "1.0.*",
"phergie/phergie-irc-plugin-react-command": "1.1.*",
"phergie/phergie-irc-plugin-react-eventfilter": "1.0.*"
}
*/
require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
// ---------- Config Settings ------------------------------------------------------
$userConfig = array(
'username' => 'Addbot',
'servers' => array(
'irc.wikimedia.org' => array(
'channels' => array(
'#wikidata.wikipedia' => array(),
),
),
'irc.freenode.net' => array(
'channels' => array(
'##add' => array(
'commandPrefix' => '.',
'mirrors' => array(
array(
'connectionHostname' => 'irc.wikimedia.org',
'regex' => '/Addshore/i',
),
),
),
),
),
// 'irc.jasperswebsite.com' => array(
// 'channels' => array(
// '#Orain-feed' => array(),
// ),
// ),
),
);
// ---------- Config Building ------------------------------------------------------
/** @var WriteStream[] $writeStreams */
$writeStreams = new ArrayObject();
$config = array();
$client = new Client();
$config['client'] = $client;
$config['plugins'][] = new PongPlugin();
// Do stuff per server
foreach ( $userConfig['servers'] as $server => $serverDetails ) {
// Add the connection
$connection = new Connection( array(
'serverHostname' => $server,
'username' => $userConfig['username'],
'realname' => $userConfig['username'],
'nickname' => $userConfig['username']
) );
$config['connections'][] = $connection;
if ( array_key_exists( 'channels', $serverDetails ) ) {
// Autojoin channels
//NOTE: the below is broken.... and silly...
// $config['plugins'][] = new EventFilterPlugin( array(
// 'filter' => new ConnectionFilter( array( $connection ) ),
// 'plugins' => array(
// new AutoJoinPlugin( array( 'channels' => array_keys( $serverDetails['channels'] ) ) ),
// ),
// ) );
$client->on( 'irc.received.rpl_endofmotd', function( ServerEvent $event, Queue $queue ) use ( $serverDetails, $writeStreams, $server ) {
if( $event->getConnection()->getServerHostname() != $server ) {
return;
}
$writeStreams[$server]->ircJoin( implode( ',', array_keys( $serverDetails['channels'] ) ) );
} );
// Do stuff per channel
foreach( $serverDetails['channels'] as $channel => $channelDetails ) {
// Enable commands
if( array_key_exists( 'commandPrefix', $channelDetails ) ) {
$config['plugins'][] = new EventFilterPlugin( array(
'filter' => new AndFilter( array(
new ConnectionFilter( array( $connection ) ),
new ChannelFilter( array( $channel ) ),
) ),
'plugins' => array(
new CommandPlugin( array( 'prefix' => $channelDetails['commandPrefix'] ) ),
),
) );
}
if( array_key_exists( 'mirrors', $channelDetails ) ) {
foreach( $channelDetails['mirrors'] as $mirrorDetails ) {
$client->on('irc.received.privmsg', function( UserEvent $event, Queue $queue ) use ( $mirrorDetails, $writeStreams, $server, $channel ) {
if( $event->getConnection()->getServerHostname() != $mirrorDetails['connectionHostname'] ){
return;
}
$params = $event->getParams();
if( !preg_match( $mirrorDetails['regex'], $params['text'] ) ) {
return;
}
$writeStreams[$server]->ircPrivmsg( $channel, $params['text'] );
});
}
}
}
}
}
// On connection establish
$client->on('connect.after.each', function( ConnectionInterface $connection, WriteStream $write) use ( &$writeStreams ) {
$writeStreams[$connection->getServerHostname()] = $write;
});
// On connections close
$client->on('connect.end', function( ConnectionInterface $connection, LoggerInterface $logger) use ( &$writeStreams, $client ) {
unset( $writeStreams[$connection->getServerHostname()] );
$client->addConnection($connection);
});
// ---------- Bot Running --------------------------------------------------------------
$bot = new Bot;
$bot->setConfig( $config );
$bot->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment