0 ? $row["id"] : 0); } #************************************************************************************************************* # FUNCTION: MainLoop() // Parses the stream and saves messages. #************************************************************************************************************* function MainLoop(&$ssh) { // set variables $timeout = 300; $time_last_activity = time(); // loop while (true) { $data = ""; // read stream if ($ssh->authed) { // fetch data while ($buf = $ssh->ReadStream()) { $data .= $buf; } // parse data if ($data) { $time_last_activity = time(); $data_array = explode("\r\n", $data); // echo any data to console foreach ($data_array as $data) { if ($data != "") { // get timestamp and action $array = explode(" ", substr($data, 27)); $timestamp = time(); #strtotime(substr($data, 0, 19)); // use local timestamp time() $action = $array[0]; // get action and ident if ($action == "BC") { // get ident $ident = (array_key_exists(1, $array) ? $array[1] : ""); // get network, frequency and source $i = 0; $array = explode(",", substr($data, strpos($data, $ident) + strlen($ident) + 1)); $network = $array[0]; $frequency = (array_key_exists(1, $array) ? $array[1] : ""); $source = (array_key_exists(2, $array) ? $array[2] : ""); $message = ""; // get nick and message $array = explode("','", substr($data, strpos($data, $source) + strlen($source) + 2)); foreach($array as $item) { if ($i == 0) { $nick = $item; } else { $message .= $item; } $i++; } if (strlen($message) > 0) { $message = substr($message, 0, strrpos($message, "'")); } // save message to database if ($network == "QWalt" && $frequency == "-qw-") { SaveMessage($timestamp, $ident, $network, $frequency, 0, $nick, $source, $message); } } // print anything but ping messages if (substr($data, 27) != "PING alive?") { echo $data."\n"; } } } } } // break on exceeded time limit or sleep 2 seconds before itteration if ($timeout >= 0 && time() - $time_last_activity >= $timeout) { echo "Connection timeout. Reconnecting...\n"; $ssh->Connect(QWNET_HOST, QWNET_PORT, QWNET_FP, QWNET_USR, "", QWNET_KEY_PATH_PUB, QWNET_KEY_PATH_PRIV); echo $ssh->Shell("vt102", 1); $time_last_activity = time(); } else { sleep(3); } } } ############################################################################################################## # INLINE CODE ############################################################################################################## // disable script timeout set_time_limit(0); // ssh settings define("QWNET_HOST", "b4r.org"); define("QWNET_PORT", "22"); define("QWNET_FP", "90C300C5853F3679C87240DAAF5F32A1"); define("QWNET_USR", "stomp"); define("QWNET_KEY_PATH_PUB", ""); define("QWNET_KEY_PATH_PRIV", ""); // open ssh connection and shell, then jump into main loop $ssh = new SSH; try { echo "Connecting to QWnet central...\n"; $ssh->Connect(QWNET_HOST, QWNET_PORT, QWNET_FP, QWNET_USR, "", QWNET_KEY_PATH_PUB, QWNET_KEY_PATH_PRIV); $ssh->cmd_exit = "PART"; echo $ssh->Shell("vt102", 1); MainLoop($ssh); } catch (Exception $e) { die("SSH error: ".$e->getMessage()); }