<commit_before><?php

/*
* This file is part of Skybot
*
* (c) 2012 <NAME> <<EMAIL>>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Skybot;

use Skybot\DriverInterface;
use Skybot\Config;
use Skybot\Storage;
use Skybot\PluginContainer;

use Skybot\Message\Chat;
use Skybot\Message\Reply;
use Skybot\Message\Direct;

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Finder\Finder;

class Main extends EventDispatcher
{
	private $timestamp;

	private $user;

	private $personalchats = array();
	private $chatnames = array();

	private $clients = array();
	private $socket;

	private $messages_served = 0;

	private $driver;
	private $config;
	private $log;
	private $plugincontainer;

	public function __construct(DriverInterface $driver, Config $config, \Monolog\Logger $log)
	{
		$this->driver = $driver;
		$this->config = $config;
		$this->log = $log;

		$this->user = new User($config->getContactName(), $this);
		$this->timestamp = time();

		$port = $config->getServerPort();

		if ($port and is_numeric($port)) {
			$this->socket = socket_create_listen($port);
			socket_set_nonblock($this->socket);
		} else {
			$port = "<NO PORT>";
		}

		$log->addInfo("Starting Skybot as ".$this->user->getContactName()." and listening on port $port");
	}

	public function isDebug()
	{
		return $this->config->isDebug();;
	}

	public function getUser()
	{
		return $this->user;
	}

	public function getStartupTime()
	{
		return $this->timestamp;
	}

	public function getMessagesServed()
	{
		return $this->messages_served;
	}

	public function setPluginContainer(PluginContainer $plugincontainer)
	{
		$this->plugincontainer = $plugincontainer;
	}

	public function getPluginContainer()
	{
		return $this->plugincontainer;
	}

	public function getLog()
	{
		return $this->log;
	}

	public function getConfig()
	{
		return $this->config;
	}

	public function getDriver()
	{
		return $this->driver;
	}

	public function handle()
	{
		$this->_refuseCalls();

		$this->_handleMessagesFromPort();

		$this->_handleChatMessages();
	}

	private function _handleChatMessages()
	{
		$chats = array();

		$missed_chats = $this->_getMissedChats();

		if (!count($missed_chats)) {
			$this->log->addDebug("No missed chats were found.");
			return false;
		}

		$recent_chats = $this->_getRecentChats();

		if (!count($recent_chats)) {
			$this->log->addDebug("No recent chats were found.");
			return false;
		}

		$chats = array_merge($missed_chats, $recent_chats);

		if (!count($chats)) return true;

		foreach ($chats as $chatid) {
			if (!$chatid) {
				$this->log->addDebug("Chatid was empty.");
				continue;
			}

			$this->_loadAndEmitChatMessages($chatid);
		}
	}

	private function _loadAndEmitChatMessages($chatid)
	{
		$recentmessages = $this->getDriver()->getRecentMessagesForChat($chatid, $this);

		if (!count($recentmessages)) {
			$this->log->addDebug("There are no recent messages.");

			return true;
		}

		foreach ($recentmessages as $chatmsg) {
			if (!$chatmsg->getMessageId() or $chatmsg->isEmpty()) continue;

			if ($chatmsg->getUser()->getContactName() == $this->getUser()->getContactName() or $chatmsg->getTimestamp() < $this->timestamp) {

				$this->log->addDebug("Skip msg because: ".
					$chatmsg->getUser()->getContactName()."==".$this->getUser()->getContactName()." or ".
					$chatmsg->getTimestamp() . " < " . $this->timestamp . " or empty message");

				continue;
			}

			$this->dispatch('skybot.message', $chatmsg);

			$chatmsg->mark();
		}
	}

	private function _getMissedChats()
	{
		return $this->getDriver()->getMissedChats();
	}

	private function _getRecentChats()
	{
		return $this->getDriver()->getRecentChats();
	}<commit_msg>cleanup<commit_after><?php

/*
* This file is part of Skybot
*
* (c) 2012 <NAME> <<EMAIL>>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Skybot;

use Skybot\DriverInterface;
use Skybot\Config;
use Skybot\Storage;
use Skybot\PluginContainer;

use Skybot\Message\Chat;
use Skybot\Message\Reply;
use Skybot\Message\Direct;

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Finder\Finder;

class Main extends EventDispatcher
{
	private $timestamp;

	private $user;

	private $personalchats = array();
	private $chatnames = array();

	private $clients = array();
	private $socket;

	private $messages_served = 0;

	private $driver;
	private $config;
	private $log;
	private $plugincontainer;

	public function __construct(DriverInterface $driver, Config $config, \Monolog\Logger $log)
	{
		$this->driver = $driver;
		$this->config = $config;
		$this->log = $log;

		$this->user = new User($config->getContactName(), $this);
		$this->timestamp = time();

		$port = $config->getServerPort();

		if ($port and is_numeric($port)) {
			$this->socket = socket_create_listen($port);
			socket_set_nonblock($this->socket);
		} else {
			$port = "<NO PORT>";
		}

		$log->addInfo("Starting Skybot as ".$this->user->getContactName()." and listening on port $port");
	}

	public function isDebug()
	{
		return $this->config->isDebug();
	}

	public function getUser()
	{
		return $this->user;
	}

	public function getStartupTime()
	{
		return $this->timestamp;
	}

	public function getMessagesServed()
	{
		return $this->messages_served;
	}

	public function setPluginContainer(PluginContainer $plugincontainer)
	{
		$this->plugincontainer = $plugincontainer;
	}

	public function getPluginContainer()
	{
		return $this->plugincontainer;
	}

	public function getLog()
	{
		return $this->log;
	}

	public function getConfig()
	{
		return $this->config;
	}

	public function getDriver()
	{
		return $this->driver;
	}

	public function handle()
	{
		$this->_refuseCalls();

		$this->_handleMessagesFromPort();

		$this->_handleChatMessages();
	}

	private function _handleChatMessages()
	{
		$chats = array();

		$missed_chats = $this->_getMissedChats();

		if (!count($missed_chats)) {
			$this->log->addDebug("No missed chats were found.");
			return false;
		}

		$recent_chats = $this->_getRecentChats();

		if (!count($recent_chats)) {
			$this->log->addDebug("No recent chats were found.");
			return false;
		}

		$chats = array_merge($missed_chats, $recent_chats);

		if (!count($chats)) return true;

		foreach ($chats as $chatid) {
			if (!$chatid) {
				$this->log->addDebug("Chatid was empty.");
				continue;
			}

			$this->_loadAndEmitChatMessages($chatid);
		}
	}

	private function _loadAndEmitChatMessages($chatid)
	{
		$recentmessages = $this->getDriver()->getRecentMessagesForChat($chatid, $this);

		if (!count($recentmessages)) {
			$this->log->addDebug("There are no recent messages.");

			return true;
		}

		foreach ($recentmessages as $chatmsg) {
			if (!$chatmsg->getMessageId() or $chatmsg->isEmpty()) continue;

			if ($chatmsg->getUser()->getContactName() == $this->getUser()->getContactName() or $chatmsg->getTimestamp() < $this->timestamp) {

				$this->log->addDebug("Skip msg because: ".
					$chatmsg->getUser()->getContactName()."==".$this->getUser()->getContactName()." or ".
					$chatmsg->getTimestamp() . " < " . $this->timestamp);

				continue;
			}

			$this->dispatch('skybot.message', $chatmsg);

			$chatmsg->mark();
		}
	}

	private function _getMissedChats()
	{
		return $this->getDriver()->getMissedChats();
	}

	private function _getRecentChats()
	{
		return $this->getDriver()->getRecentChats();
	}
{'fraction_non_alphanumeric': 0.15899137358991375, 'fraction_numerical': 0.0013271400132714001, 'mean_word_length': 4.288421052631579, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 131, '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': '3582177', 'n_tokens_mistral': 2757, 'n_tokens_neox': 2461, 'n_words': 543}