<commit_before>#!/usr/bin/php -q
<?php

class TimeoutException extends Exception {}

function echo_r($message)
{
	if (is_array($message)) {
		foreach ($message as $msg)
			echo_r($msg);
	}
	else
		echo date("d.m.Y H:i:s => ") . $message . EOL;
}

// not keeping the filehandle might not be the wisest idea.
function write_log_message($msg)
{
	$logFile = fopen("/var/log/irc/" . date("Ymd") . ".log", "a+");
	fwrite($logFile, round(microtime(true) * 1000) . ' ' . $msg . EOL);
	fclose($logFile);
}

function fill_string($str, $length)
{

	while (strlen($str) < $length)
		$str .= ' ';

	return $str;

}

// config file
include(realpath(dirname(__FILE__)) . '/../htdocs/config.inc');

include(LIB . '/Default/SmrMySqlDatabase.class.inc');

include(ENGINE . '/Default/smr.inc');

$address = 'ice.coldfront.net';
$port = 6667;
define('IRC_BOT_NICK', 'Caretaker');
$pass = '<PASSWORD>';

// timer events
$events = array();

// supply/demand list
$sds = array();

// on<something> actions
$actions = array();

$logging = false;
$debugging = false;

if ($argc > 1) {
	foreach ($argv as $arg) {
		if ($arg == '-log') {
			$logging = true;
		}
		if ($arg == '-debug') {
			$debugging = true;
		}
	}
}
define('IRC_LOGGING', $logging);
define('IRC_DEBUGGING', $debugging);

// include all sub files
require_once('irc/server.php');
require_once('irc/ctcp.php');
require_once('irc/invite.php');
//require_once('irc/rank.php');
//require_once('irc/ship.php');
require_once('irc/user.php');
require_once('irc/query.php');
require_once('irc/notice.php');
//require_once('irc/weapon.php');
//require_once('irc/level.php');
require_once('irc/channel.php');
require_once('irc/channel_action.php');
require_once('irc/channel_msg.php');
require_once('irc/channel_msg_op.php');
require_once('irc/channel_msg_sd.php');
require_once('irc/channel_msg_seed.php');
require_once('irc/channel_msg_sms.php');
require_once('irc/maintenance.php');

// delete all seen stats that appear to be on (we do not want to take something for granted that happend while we were away)
$db = new SmrMySqlDatabase();
$db->query('DELETE from irc_seen WHERE signed_off = 0');

// just in case we need to exit for good
$running = true;

// after a timeout we start over
while ($running) {
	try {
		echo_r('Connecting to ' . $address);
		$fp = fsockopen($address, $port);
		if ($fp) {
			echo_r('Socket ' . $fp . ' is connected... Identifying...');


			safefputs($fp, 'NICK CareGhost' . EOL);
			safefputs($fp, 'USER ' . strtolower(IRC_BOT_NICK) . ' oberon smrealms.de :Official SMR bot' . EOL);

			// kill any other user that is using our nick
			safefputs($fp, 'NICKSERV GHOST ' . IRC_BOT_NICK . ' ' . $pass . EOL);

			sleep(1);

			//4
			safefputs($fp, 'NICK ' . IRC_BOT_NICK . EOL);
			safefputs($fp, 'NICKSERV IDENTIFY ' . $pass . EOL);

			// join our public channel
			if (!IRC_DEBUGGING) {
				safefputs($fp, 'JOIN #smr' . EOL);
				safefputs($fp, 'JOIN #smr-bar' . EOL);
				sleep(1);
				safefputs($fp, 'WHO #smr' . EOL);
				safefputs($fp, 'WHO #smr-bar' . EOL);

				// join all alliance channels
				$db->query('SELECT    channel ' .
						'FROM      irc_alliance_has_channel ' .
						'JOIN game USING (game_id) ' .
						'WHERE     start_date < ' . time() .
						'  AND     end_date > ' . time());
				while ($db->nextRecord()) {
					$alliance_channel = $db->getField('channel');
					// join channels
					safefputs($fp, 'JOIN ' . $alliance_channel . EOL);
					sleep(1);
					safefputs($fp, 'WHO ' . $alliance_channel . EOL);
				}

			}

			stream_set_blocking($fp, true);
			while (!feof($fp)) {
				readFromStream($fp);
			}
			fclose($fp); // close socket

		} else {

			// network troubles
			echo_r('There was an error connecting to ' . $address . '/' . $port);

			// sleep and try again!
			sleep(60);

		}
	}
	catch (TimeoutException $e) {
		// Ignore the timeout exception, we'll loop round and reconnect.
	}
} // end of while running

function safefputs($fp, $text) {
	stream_set_blocking($fp, false);
	while(readFromStream($fp)!==false);
	fputs($fp, $text);
	stream_set_blocking($fp, true);
}

function readFromStream($fp) {
	static $last_ping = 0;
	if($last_ping == 0) {
		$last_ping = time();
	}

	$rdata = fgets($fp, 4096);
	$rdata = preg_replace('/\s+/', ' ', $rdata);

	// log for reports (if enabled via command line (-log)
	if (IRC_LOGGING && strlen($rdata) > 0)
		write_log_message($rdata);

	// remember the last time we got something from the server
	if (strlen($rdata) > 0)
		$last_ping = time();

	// timeout detection!
	if ($last_ping < time() - 300) {
		echo_r('TIMEOUT detected!');
		fclose($fp); // close socket
		throw new TimeoutException();
	}

	// we simply do some poll stuff here
	check_planet_builds($fp);
	check_events($fp);
	check_sms_dlr($fp);<commit_msg>Reset last ping each time we try connecting, this stops things breaking when trying to reconnect after a timeout.<commit_after>#!/usr/bin/php -q
<?php

class TimeoutException extends Exception {}

function echo_r($message)
{
	if (is_array($message)) {
		foreach ($message as $msg)
			echo_r($msg);
	}
	else
		echo date("d.m.Y H:i:s => ") . $message . EOL;
}

// not keeping the filehandle might not be the wisest idea.
function write_log_message($msg)
{
	$logFile = fopen("/var/log/irc/" . date("Ymd") . ".log", "a+");
	fwrite($logFile, round(microtime(true) * 1000) . ' ' . $msg . EOL);
	fclose($logFile);
}

function fill_string($str, $length)
{

	while (strlen($str) < $length)
		$str .= ' ';

	return $str;

}

// config file
include(realpath(dirname(__FILE__)) . '/../htdocs/config.inc');

include(LIB . '/Default/SmrMySqlDatabase.class.inc');

include(ENGINE . '/Default/smr.inc');

$address = 'ice.coldfront.net';
$port = 6667;
define('IRC_BOT_NICK', 'Caretaker');
$pass = '<PASSWORD>';

// timer events
$events = array();

// supply/demand list
$sds = array();

// on<something> actions
$actions = array();

$logging = false;
$debugging = false;

if ($argc > 1) {
	foreach ($argv as $arg) {
		if ($arg == '-log') {
			$logging = true;
		}
		if ($arg == '-debug') {
			$debugging = true;
		}
	}
}
define('IRC_LOGGING', $logging);
define('IRC_DEBUGGING', $debugging);

// include all sub files
require_once('irc/server.php');
require_once('irc/ctcp.php');
require_once('irc/invite.php');
//require_once('irc/rank.php');
//require_once('irc/ship.php');
require_once('irc/user.php');
require_once('irc/query.php');
require_once('irc/notice.php');
//require_once('irc/weapon.php');
//require_once('irc/level.php');
require_once('irc/channel.php');
require_once('irc/channel_action.php');
require_once('irc/channel_msg.php');
require_once('irc/channel_msg_op.php');
require_once('irc/channel_msg_sd.php');
require_once('irc/channel_msg_seed.php');
require_once('irc/channel_msg_sms.php');
require_once('irc/maintenance.php');

// delete all seen stats that appear to be on (we do not want to take something for granted that happend while we were away)
$db = new SmrMySqlDatabase();
$db->query('DELETE from irc_seen WHERE signed_off = 0');

// just in case we need to exit for good
$running = true;

// after a timeout we start over
while ($running) {
	try {
		// Reset last ping each time we try connecting.
		$last_ping = time();
		echo_r('Connecting to ' . $address);
		$fp = fsockopen($address, $port);
		if ($fp) {
			echo_r('Socket ' . $fp . ' is connected... Identifying...');


			safefputs($fp, 'NICK CareGhost' . EOL);
			safefputs($fp, 'USER ' . strtolower(IRC_BOT_NICK) . ' oberon smrealms.de :Official SMR bot' . EOL);

			// kill any other user that is using our nick
			safefputs($fp, 'NICKSERV GHOST ' . IRC_BOT_NICK . ' ' . $pass . EOL);

			sleep(1);

			//4
			safefputs($fp, 'NICK ' . IRC_BOT_NICK . EOL);
			safefputs($fp, 'NICKSERV IDENTIFY ' . $pass . EOL);

			// join our public channel
			if (!IRC_DEBUGGING) {
				safefputs($fp, 'JOIN #smr' . EOL);
				safefputs($fp, 'JOIN #smr-bar' . EOL);
				sleep(1);
				safefputs($fp, 'WHO #smr' . EOL);
				safefputs($fp, 'WHO #smr-bar' . EOL);

				// join all alliance channels
				$db->query('SELECT    channel ' .
						'FROM      irc_alliance_has_channel ' .
						'JOIN game USING (game_id) ' .
						'WHERE     start_date < ' . time() .
						'  AND     end_date > ' . time());
				while ($db->nextRecord()) {
					$alliance_channel = $db->getField('channel');
					// join channels
					safefputs($fp, 'JOIN ' . $alliance_channel . EOL);
					sleep(1);
					safefputs($fp, 'WHO ' . $alliance_channel . EOL);
				}

			}

			stream_set_blocking($fp, true);
			while (!feof($fp)) {
				readFromStream($fp);
			}
			fclose($fp); // close socket

		} else {

			// network troubles
			echo_r('There was an error connecting to ' . $address . '/' . $port);

			// sleep and try again!
			sleep(60);

		}
	}
	catch (TimeoutException $e) {
		// Ignore the timeout exception, we'll loop round and reconnect.
	}
} // end of while running

function safefputs($fp, $text) {
	stream_set_blocking($fp, false);
	while(readFromStream($fp)!==false);
	fputs($fp, $text);
	stream_set_blocking($fp, true);
}

function readFromStream($fp) {
	global $last_ping;

	$rdata = fgets($fp, 4096);
	$rdata = preg_replace('/\s+/', ' ', $rdata);

	// log for reports (if enabled via command line (-log)
	if (IRC_LOGGING && strlen($rdata) > 0)
		write_log_message($rdata);

	// remember the last time we got something from the server
	if (strlen($rdata) > 0)
		$last_ping = time();

	// timeout detection!
	if ($last_ping < time() - 300) {
		echo_r('TIMEOUT detected!');
		fclose($fp); // close socket
		throw new TimeoutException();
	}

	// we simply do some poll stuff here
	check_planet_builds($fp);
	check_events($fp);
	check_sms_dlr($fp);
{'fraction_non_alphanumeric': 0.17802402651201327, 'fraction_numerical': 0.005385252692626346, 'mean_word_length': 3.557338367154318, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2678219', 'n_tokens_mistral': 3996, 'n_tokens_neox': 3560, 'n_words': 838}