From 80503205b017062f1be737d33694d99271f6e182 Mon Sep 17 00:00:00 2001 From: Mikko Ahlroth Date: Sat, 28 Sep 2013 22:14:52 +0300 Subject: [PATCH] Initial commit --- main.php | 48 ++++++++++ plugins/0001_echoer.php | 31 +++++++ plugins/0020_autojoin.php | 34 +++++++ plugins/9998_pingresponse.php | 9 ++ utils.php | 166 ++++++++++++++++++++++++++++++++++ 5 files changed, 288 insertions(+) create mode 100644 main.php create mode 100644 plugins/0001_echoer.php create mode 100644 plugins/0020_autojoin.php create mode 100644 plugins/9998_pingresponse.php create mode 100644 utils.php diff --git a/main.php b/main.php new file mode 100644 index 0000000..c4d8a23 --- /dev/null +++ b/main.php @@ -0,0 +1,48 @@ + ', $input, PHP_EOL; +}; + +$_plugins['onoutput'][] = function($id, $output) +{ + echo '[', $id, '] <- ', rtrim($output), PHP_EOL; +}; + +$_plugins['onconnect'][] = function($id, $host, $port) +{ + echo 'Connected to ', $host, ':', $port, ' as ', $id, '.', PHP_EOL; +}; + +$_plugins['ondisconnect'][] = function($id) +{ + echo 'Disconnecting from ', $id, '...', PHP_EOL; +}; diff --git a/plugins/0020_autojoin.php b/plugins/0020_autojoin.php new file mode 100644 index 0000000..c534735 --- /dev/null +++ b/plugins/0020_autojoin.php @@ -0,0 +1,34 @@ + 'BaseT', + 'altnick' => 'AltBase', + 'username' => 'baset', + 'realname' => '10/100Base-T Ports' + ]; + +// No need for & since this will only be run at start +$_plugins['onload'][] = function() use ($_autojoin_servers, $_started, $_connect) +{ + if (!$_started) + { + foreach ($_autojoin_servers as $server) + { + $_connect($server[0], $server[1]); + } + } +}; + +$_plugins['onconnect'][] = function($id, $host, $port) use (&$_write, &$_user_info) +{ + $_write($id, sprintf("NICK %s\r\n", $_user_info['nick'])); + $_write($id, sprintf("USER %s 8 * :%s\r\n", $_user_info['username'], + $_user_info['realname'])); +}; diff --git a/plugins/9998_pingresponse.php b/plugins/9998_pingresponse.php new file mode 100644 index 0000000..12302ac --- /dev/null +++ b/plugins/9998_pingresponse.php @@ -0,0 +1,9 @@ + $stream) + { + if ($stream === $sock) + { + $newid = $id; + } + } + + $_on_connect($newid, $host, $port); +}; + +$_disconnect = function($id) use (&$_iostreams, &$_on_disconnect) +{ + $_on_disconnect($id); + fclose($_iostreams[$id]); + unset($_iostreams[$id]); +}; + +$_write = function($id, $output) use (&$_iostreams, &$_stream_fwrite, &$_on_output) +{ + $_on_output($id, $output); + $_stream_fwrite($_iostreams[$id], $output); +}; + + + +$_loop = function() use (&$_iostreams, &$_plugins, &$_disconnect, &$_on_input, &$_reload) +{ + while (true) + { + if ($_reload()) return; + if (count($_iostreams) == 0) exit; + + $r = $_iostreams; + $w = []; + $e = []; + @stream_select($r, $w, $e, null); + + foreach ($r as $instream) + { + $inid = 0; + foreach ($_iostreams as $id => $stream) + { + if ($stream === $instream) + { + $inid = $id; + } + } + + if (feof($instream)) + { + $_disconnect($inid); + } + else + { + $read = []; + while (true) + { + $line = rtrim(fgets($instream)); + if ($line == '') break; + $read[] = $line; + } + + foreach ($read as $line) + { + $_on_input($inid, $line); + } + } + } + } +}; + +// Reload code when appropriate signal is received +$_sig_handler = function($signo) use (&$_reload) +{ + echo 'Got SIGUSR1, scheduling reload...', PHP_EOL; + $_reload(true); +}; + +pcntl_signal(SIGUSR1, $_sig_handler);