2020-02-25 18:02:32 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* API wrapper module.
|
|
|
|
*
|
|
|
|
* This file is part of MadelineProto.
|
|
|
|
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU Affero General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License along with MadelineProto.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author Daniil Gentili <daniil@daniil.it>
|
|
|
|
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
|
|
|
|
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
|
|
|
*
|
|
|
|
* @link https://docs.madelineproto.xyz MadelineProto documentation
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace danog\MadelineProto;
|
|
|
|
|
|
|
|
use Amp\Promise;
|
|
|
|
use Amp\Success;
|
2020-09-23 00:57:49 +02:00
|
|
|
use danog\MadelineProto\Ipc\Client;
|
2020-02-25 18:02:32 +01:00
|
|
|
|
2020-09-22 23:10:56 +02:00
|
|
|
use function Amp\File\open;
|
2020-02-25 18:02:32 +01:00
|
|
|
|
|
|
|
final class APIWrapper
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* MTProto instance.
|
|
|
|
*
|
2020-09-22 23:10:56 +02:00
|
|
|
* @var MTProto|null|Client
|
2020-02-25 18:02:32 +01:00
|
|
|
*/
|
2020-09-22 23:10:56 +02:00
|
|
|
private $API = null;
|
2020-02-25 18:02:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Session path.
|
|
|
|
*/
|
2020-09-22 17:30:07 +02:00
|
|
|
public SessionPaths $session;
|
2020-02-25 18:02:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Getting API ID flag.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private bool $gettingApiId = false;
|
|
|
|
|
|
|
|
/**
|
2020-02-26 11:47:30 +01:00
|
|
|
* Web API template.
|
|
|
|
*
|
2020-02-25 18:02:32 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private string $webApiTemplate = '';
|
|
|
|
|
|
|
|
/**
|
2020-02-26 11:47:30 +01:00
|
|
|
* My.telegram.org wrapper.
|
2020-02-25 18:02:32 +01:00
|
|
|
*
|
|
|
|
* @var ?MyTelegramOrgWrapper
|
|
|
|
*/
|
|
|
|
private $myTelegramOrgWrapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialization date.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
private int $serialized = 0;
|
2020-02-26 18:12:00 +01:00
|
|
|
/**
|
|
|
|
* Whether lua is being used.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private bool $lua = false;
|
|
|
|
/**
|
|
|
|
* Whether async is enabled.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private bool $async = false;
|
2020-02-25 18:02:32 +01:00
|
|
|
|
|
|
|
/**
|
2020-02-26 11:47:30 +01:00
|
|
|
* AbstractAPIFactory instance.
|
|
|
|
*
|
2020-02-25 18:02:32 +01:00
|
|
|
* @var AbstractAPIFactory
|
|
|
|
*/
|
|
|
|
private AbstractAPIFactory $factory;
|
2020-10-04 14:55:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Property storage.
|
|
|
|
*/
|
|
|
|
public array $storage = [];
|
2020-02-25 18:02:32 +01:00
|
|
|
/**
|
2020-02-26 11:47:30 +01:00
|
|
|
* API wrapper.
|
2020-02-25 18:02:32 +01:00
|
|
|
*
|
|
|
|
* @param API $API API instance to wrap
|
|
|
|
* @param AbstractAPIFactory $factory Factory
|
|
|
|
*/
|
|
|
|
public function __construct(API $API, AbstractAPIFactory $factory)
|
|
|
|
{
|
|
|
|
self::link($this, $API);
|
|
|
|
$this->factory = $factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-26 11:47:30 +01:00
|
|
|
* Link two APIWrapper and API instances.
|
2020-02-25 18:02:32 +01:00
|
|
|
*
|
|
|
|
* @param API|APIWrapper $a Instance to which link
|
|
|
|
* @param API|APIWrapper $b Instance from which link
|
2020-02-26 11:47:30 +01:00
|
|
|
*
|
2020-02-25 18:02:32 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function link($a, $b): void
|
|
|
|
{
|
|
|
|
foreach (self::__sleep() as $var) {
|
|
|
|
Tools::setVar($a, $var, Tools::getVar($b, $var));
|
|
|
|
}
|
|
|
|
Tools::setVar($a, 'session', Tools::getVar($b, 'session'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sleep function.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function __sleep(): array
|
|
|
|
{
|
2020-09-28 11:56:14 +02:00
|
|
|
return ['API', 'webApiTemplate', 'gettingApiId', 'myTelegramOrgWrapper', 'storage', 'lua'];
|
2020-02-25 18:02:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-26 11:47:30 +01:00
|
|
|
* Get MTProto instance.
|
2020-02-25 18:02:32 +01:00
|
|
|
*
|
2020-10-01 21:03:25 +02:00
|
|
|
* @return Client|MTProto|null
|
2020-02-25 18:02:32 +01:00
|
|
|
*/
|
2020-10-01 21:03:25 +02:00
|
|
|
public function &getAPI()
|
2020-02-25 18:02:32 +01:00
|
|
|
{
|
|
|
|
return $this->API;
|
|
|
|
}
|
|
|
|
|
2020-02-26 18:12:00 +01:00
|
|
|
/**
|
|
|
|
* Whether async is being used.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isAsync(): bool
|
|
|
|
{
|
|
|
|
return $this->async;
|
|
|
|
}
|
|
|
|
|
2020-02-25 18:02:32 +01:00
|
|
|
/**
|
2020-02-26 11:47:30 +01:00
|
|
|
* Get API factory.
|
2020-02-25 18:02:32 +01:00
|
|
|
*
|
|
|
|
* @return AbstractAPIFactory
|
|
|
|
*/
|
|
|
|
public function getFactory(): AbstractAPIFactory
|
|
|
|
{
|
|
|
|
return $this->factory;
|
|
|
|
}
|
|
|
|
|
2020-07-11 20:01:54 +02:00
|
|
|
/**
|
|
|
|
* Get IPC path.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getIpcPath(): string
|
|
|
|
{
|
2020-09-22 17:30:07 +02:00
|
|
|
return $this->session->getIpcPath();
|
2020-07-11 20:01:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-25 18:02:32 +01:00
|
|
|
/**
|
2020-07-10 14:50:00 +02:00
|
|
|
* Serialize session.
|
2020-02-25 18:02:32 +01:00
|
|
|
*
|
2020-09-22 23:10:56 +02:00
|
|
|
* @return Promise<bool>
|
2020-02-25 18:02:32 +01:00
|
|
|
*/
|
|
|
|
public function serialize(): Promise
|
|
|
|
{
|
2020-09-22 17:30:07 +02:00
|
|
|
if ($this->API === null && !$this->gettingApiId) {
|
|
|
|
return new Success(false);
|
2020-07-11 20:01:54 +02:00
|
|
|
}
|
2020-09-23 00:57:49 +02:00
|
|
|
if ($this->API instanceof Client) {
|
|
|
|
return new Success(false);
|
|
|
|
}
|
2020-02-25 18:02:32 +01:00
|
|
|
return Tools::callFork((function (): \Generator {
|
|
|
|
if ($this->API) {
|
|
|
|
yield from $this->API->initAsynchronously();
|
|
|
|
}
|
2020-09-22 17:30:07 +02:00
|
|
|
|
2020-09-24 23:25:54 +02:00
|
|
|
yield from $this->session->serialize(
|
2020-09-26 21:57:06 +02:00
|
|
|
$this->API ? yield from $this->API->serializeSession($this) : $this,
|
2020-09-24 23:25:54 +02:00
|
|
|
$this->session->getSessionPath()
|
|
|
|
);
|
2020-09-22 17:30:07 +02:00
|
|
|
|
2020-09-22 23:10:56 +02:00
|
|
|
if ($this->API) {
|
2020-09-24 11:45:20 +02:00
|
|
|
yield from $this->session->storeLightState($this->API);
|
2020-09-22 23:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Truncate legacy session
|
|
|
|
yield (yield open($this->session->getLegacySessionPath(), 'w'))->close();
|
|
|
|
|
2020-09-22 17:30:07 +02:00
|
|
|
Logger::log('Saved session!');
|
2020-09-22 23:10:56 +02:00
|
|
|
return true;
|
2020-02-25 18:02:32 +01:00
|
|
|
})());
|
|
|
|
}
|
2020-10-07 19:31:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get session path.
|
|
|
|
*
|
|
|
|
* @return SessionPaths
|
|
|
|
*/
|
|
|
|
public function getSession(): SessionPaths
|
|
|
|
{
|
|
|
|
return $this->session;
|
|
|
|
}
|
2020-02-25 18:02:32 +01:00
|
|
|
}
|