<commit_before><?php
	require_once 'Lang.php';
	require_once 'WhatsBotExceptions.php';
	
	require_once 'whatsapi/whatsprot.class.php';
	require_once 'WhatsApp.php';

	require_once 'Listener.php';
	require_once 'Parser.php';

	require_once 'ModuleManager.php';

	require_once 'IncludeManager.php';


	require_once 'ConfigManager.php';


	class WhatsBot
	{
		private $WhatsProt = null;
		private $WhatsApp = null;

		private $Listener = null;
		private $Parser = null;

		private $ModuleManager = null;

		private $IncludeManager = null;


		private $Lang = null;


		private $Debug = false;


		public function __construct($Debug = false)
		{
			$this->IncludeManager = new IncludeManager;
			$this->IncludeManager->LoadIncludes();


			$this->Lang = new Lang('Main');


			$this->Debug = $Debug;


			$Config = Config::Get('WhatsBot');

			if(!empty($Config['WhatsApp']['Username']) && !empty($Config['WhatsApp']['Nickname']))
			{
				# WhatsApp

				$this->WhatsProt = new WhatsProt($Config['WhatsApp']['Username'], null, $Config['WhatsApp']['Nickname'], $Debug);

				$this->WhatsApp = new WhatsApp($this->WhatsProt);

				# WhatsBot

				$this->ModuleManager = new ModuleManager($this->WhatsApp);

				$this->Parser = new WhatsBotParser($this->WhatsApp, $this->ModuleManager);
				
				$this->Listener = new WhatsBotListener($this->WhatsApp, $this->Parser);

				# Load

				$this->ModuleManager->LoadModules();
				// ThreadManager (into Start())

				# Bind Event Listener

				$this->WhatsApp->EventManager()->BindListener($this->Listener);
			}
			else
				throw new WhatsBotException($this->Lang->Get('exception:config_empty', 'WhatsBot.json'));
		}

		public function Start()
		{
			$Config = Config::Get('WhatsBot');

			if(!empty($Config['WhatsApp']['Password']))
			{
				$this->WhatsApp->Connect();

				$this->WhatsApp->LoginWithPassword($Config['WhatsApp']['Password']);
			}
			else
				throw new WhatsBotException($this->Lang->Get('exception:config_empty_password', 'WhatsBot.json'));
		}

		public function Listen()
		{
			$StartTime = time();

			while(true)
			{
				if(!$this->WhatsApp->IsConnected())
					$this->Start();

				$this->WhatsApp->PollMessage();

				if(time() >= $StartTime + 60)
				{
					$this->WhatsApp->SendPing();

					$StartTime = time();
				}
			}
		}
	}

	/* To do: 
	 * Make an parser for modules (With https://github.com/nikic/PHP-Parser ?)
	 * Flood detection / protection
	 * 
	 * https://github.com/mgp25/WhatsAPI-Official/issues/164#issuecomment-64790667
	 * Add syncing before send message (Array with numbers synceds? [IF DISCONNECT?])
	 * 
	 * Implement? https://github.com/mgp25/WhatsAPI-Official/issues/169
	 * 
	 * Only CLI use
	 * 
	 * Delete references? http://php.net/manual/es/language.oop5.references.php
	 */

	/* To do (new-structure): 
	 * Fix Utils::IsAdmin
	 * Fix !setstatus
	 * Test /soundcloud/
	 * Test !search (updated)
	 */

	/*
	 * Implement: https://github.com/mgp25/WhatsAPI-Official/wiki/WhatsAPI-Documentation#whatsapp-workflow
	 */

	/*
	 * Ideas: 
	 * Detect lang from country code
	 */<commit_msg>To do<commit_after><?php
	require_once 'Lang.php';
	require_once 'WhatsBotExceptions.php';
	
	require_once 'whatsapi/whatsprot.class.php';
	require_once 'WhatsApp.php';

	require_once 'Listener.php';
	require_once 'Parser.php';

	require_once 'ModuleManager.php';

	require_once 'IncludeManager.php';


	require_once 'ConfigManager.php';


	class WhatsBot
	{
		private $WhatsProt = null;
		private $WhatsApp = null;

		private $Listener = null;
		private $Parser = null;

		private $ModuleManager = null;

		private $IncludeManager = null;


		private $Lang = null;


		private $Debug = false;


		public function __construct($Debug = false)
		{
			$this->IncludeManager = new IncludeManager;
			$this->IncludeManager->LoadIncludes();


			$this->Lang = new Lang('Main');


			$this->Debug = $Debug;


			$Config = Config::Get('WhatsBot');

			if(!empty($Config['WhatsApp']['Username']) && !empty($Config['WhatsApp']['Nickname']))
			{
				# WhatsApp

				$this->WhatsProt = new WhatsProt($Config['WhatsApp']['Username'], null, $Config['WhatsApp']['Nickname'], $Debug);

				$this->WhatsApp = new WhatsApp($this->WhatsProt);

				# WhatsBot

				$this->ModuleManager = new ModuleManager($this->WhatsApp);

				$this->Parser = new WhatsBotParser($this->WhatsApp, $this->ModuleManager);
				
				$this->Listener = new WhatsBotListener($this->WhatsApp, $this->Parser);

				# Load

				$this->ModuleManager->LoadModules();
				// ThreadManager (into Start())

				# Bind Event Listener

				$this->WhatsApp->EventManager()->BindListener($this->Listener);
			}
			else
				throw new WhatsBotException($this->Lang->Get('exception:config_empty', 'WhatsBot.json'));
		}

		public function Start()
		{
			$Config = Config::Get('WhatsBot');

			if(!empty($Config['WhatsApp']['Password']))
			{
				$this->WhatsApp->Connect();

				$this->WhatsApp->LoginWithPassword($Config['WhatsApp']['Password']);
			}
			else
				throw new WhatsBotException($this->Lang->Get('exception:config_empty_password', 'WhatsBot.json'));
		}

		public function Listen()
		{
			$StartTime = time();

			while(true)
			{
				if(!$this->WhatsApp->IsConnected())
					$this->Start();

				$this->WhatsApp->PollMessage();

				if(time() >= $StartTime + 60)
				{
					$this->WhatsApp->SendPing();

					$StartTime = time();
				}
			}
		}
	}

	/* 
	 * To do: 
	 * 
	 * Flood protection
	 * https://github.com/mgp25/WhatsAPI-Official/issues/164#issuecomment-64821350
	 *  - Sync before send message. Sync if not in array. If disconnect reset array
	 * Fix #28 reconnect issue
	 * CLI only
	 * Implement WhatsApp workflow (https://github.com/mgp25/WhatsAPI-Official/wiki/WhatsAPI-Documentation#whatsapp-workflow)
	 * 
	 *****
	 * 
	 * Ideas: 
	 * 
	 * Detect lang from country code
	 */
{'fraction_non_alphanumeric': 0.1652084405558415, 'fraction_numerical': 0.007205352547606794, 'mean_word_length': 3.678972712680578, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, 'https://': 6, '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': '305250', 'n_tokens_mistral': 2330, 'n_tokens_neox': 1870, 'n_words': 339}