Skip to content

Instantly share code, notes, and snippets.

@hjr3
Created January 30, 2012 21:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hjr3/1706840 to your computer and use it in GitHub Desktop.
Save hjr3/1706840 to your computer and use it in GitHub Desktop.
A simple irc async push message script for Phergie IRC Bot
# simple example on how to push message
# assuming the plugin is listening on localhost:12345
echo "this is a pushed message" | nc localhost 12345
<?php
class Phergie_Plugin_PushMessage extends Phergie_Plugin_Cron
{
public function onLoad()
{
$settings = $this->config['push_message.listen'];
foreach ($settings as $listen) {
$host = $listen['host'];
$port = $listen['port'];
$channel = $listen['channel'];
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, $host, $port);
socket_listen($socket);
socket_set_nonblock($socket);
$this->registerCallback(array($this, 'message'), 1, array($socket, $channel), true);
}
}
protected function message($socket, $channel)
{
while($inc = @socket_accept($socket)) {
$output = '';
while (1) {
$data = socket_read($inc, 1028);
if ($data == '') {
$this->doPrivmsg($channel, $output);
socket_close($inc);
break;
}
$output .= $data;
}
}
}
}
<?php
class Phergie_Plugin_PushMessageStream extends Phergie_Plugin_Cron
{
public function onLoad()
{
$settings = $this->config['push_message.listen'];
foreach ($settings as $listen) {
$host = $listen['host'];
$port = $listen['port'];
$channel = $listen['channel'];
$server = stream_socket_server("tcp://{$host}:{$port}");
$this->registerCallback(array($this, 'message'), 1, array($server, $channel), true);
}
}
protected function message($server, $channel)
{
// stream_set_blocking() has no effect on steam_socket_accept()
// so use a timeout of 0 to emulate
while($socket = @stream_socket_accept($server, 0)) {
$output = stream_get_contents($socket);
$this->doPrivmsg($channel, $output);
fclose($socket);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment