Created
January 30, 2012 21:32
-
-
Save hjr3/1706840 to your computer and use it in GitHub Desktop.
A simple irc async push message script for Phergie IRC Bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# simple example on how to push message | |
# assuming the plugin is listening on localhost:12345 | |
echo "this is a pushed message" | nc localhost 12345 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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