2022-12-30 21:54:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2020-10-18 14:46:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Outgoing message.
|
|
|
|
*
|
|
|
|
* 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>
|
2023-01-04 12:43:01 +01:00
|
|
|
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
|
2020-10-18 14:46:34 +02:00
|
|
|
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
|
|
|
* @link https://docs.madelineproto.xyz MadelineProto documentation
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace danog\MadelineProto\MTProto;
|
|
|
|
|
2023-06-02 20:49:50 +02:00
|
|
|
use Amp\Cancellation;
|
|
|
|
use Amp\CancelledException;
|
2022-12-30 20:24:13 +01:00
|
|
|
use Amp\DeferredFuture;
|
2022-12-30 21:43:58 +01:00
|
|
|
use Amp\Future;
|
2020-10-18 14:46:34 +02:00
|
|
|
use danog\MadelineProto\Exception;
|
2022-12-30 21:43:58 +01:00
|
|
|
use Revolt\EventLoop;
|
2023-01-08 16:06:50 +01:00
|
|
|
use Throwable;
|
2022-12-30 19:21:36 +01:00
|
|
|
|
|
|
|
use function time;
|
2020-10-18 14:46:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Outgoing message.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-07-05 21:28:17 +02:00
|
|
|
class MTProtoOutgoingMessage extends MTProtoMessage
|
2020-10-18 14:46:34 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The message was created.
|
|
|
|
*/
|
2023-11-11 16:55:29 +01:00
|
|
|
public const STATE_PENDING = 0;
|
2020-10-18 14:46:34 +02:00
|
|
|
/**
|
|
|
|
* The message was sent.
|
|
|
|
*/
|
2023-11-11 16:55:29 +01:00
|
|
|
public const STATE_SENT = 1;
|
2020-10-18 14:46:34 +02:00
|
|
|
/**
|
|
|
|
* The message was acked.
|
|
|
|
*/
|
2023-11-11 16:55:29 +01:00
|
|
|
public const STATE_ACKED = 2;
|
2020-10-18 14:46:34 +02:00
|
|
|
/**
|
|
|
|
* We got a reply to the message.
|
|
|
|
*/
|
2023-11-11 16:55:29 +01:00
|
|
|
public const STATE_REPLIED = self::STATE_ACKED | 4;
|
2020-10-18 14:46:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* State of message.
|
|
|
|
*
|
2023-01-13 14:36:10 +01:00
|
|
|
* @var self::STATE_*
|
2020-10-18 14:46:34 +02:00
|
|
|
*/
|
|
|
|
private int $state = self::STATE_PENDING;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send deferred.
|
2023-01-11 18:47:27 +01:00
|
|
|
*
|
|
|
|
* @var ?DeferredFuture<null>
|
2020-10-18 14:46:34 +02:00
|
|
|
*/
|
2023-01-20 17:24:13 +01:00
|
|
|
private ?DeferredFuture $sendDeferred = null;
|
2020-10-18 14:46:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialized body.
|
|
|
|
*/
|
|
|
|
private ?string $serializedBody = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether we should refresh references upon serialization of this message.
|
|
|
|
*/
|
|
|
|
private bool $refreshReferences = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When was this message sent.
|
|
|
|
*/
|
2023-01-14 19:51:23 +01:00
|
|
|
private ?int $sent = null;
|
2020-10-18 14:46:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of times this message was sent.
|
|
|
|
*/
|
|
|
|
private int $tries = 0;
|
|
|
|
|
2023-09-27 15:10:20 +02:00
|
|
|
/**
|
|
|
|
* Whether this message is related to a user, as in getting a successful reply means we have auth.
|
|
|
|
*/
|
|
|
|
public readonly bool $userRelated;
|
|
|
|
|
2020-10-18 14:46:34 +02:00
|
|
|
/**
|
|
|
|
* Create outgoing message.
|
|
|
|
*
|
2023-10-04 22:31:04 +02:00
|
|
|
* @param array $body Body
|
2023-01-08 19:02:49 +01:00
|
|
|
* @param string $constructor Constructor name
|
|
|
|
* @param string $type Constructor type
|
2023-09-27 15:10:20 +02:00
|
|
|
* @param boolean $isMethod Is this a method?
|
2023-01-08 19:02:49 +01:00
|
|
|
* @param boolean $unencrypted Is this an unencrypted message?
|
2020-10-18 14:46:34 +02:00
|
|
|
*/
|
2023-09-27 15:10:20 +02:00
|
|
|
public function __construct(
|
2023-10-04 22:31:04 +02:00
|
|
|
private ?array $body,
|
2023-09-27 15:10:20 +02:00
|
|
|
public readonly string $constructor,
|
|
|
|
public readonly string $type,
|
|
|
|
public readonly bool $isMethod,
|
|
|
|
public readonly bool $unencrypted,
|
2023-09-27 15:24:59 +02:00
|
|
|
public readonly ?string $subtype = null,
|
2023-09-27 15:10:20 +02:00
|
|
|
/**
|
|
|
|
* Whether this message is related to a file upload, as in getting a redirect should redirect to a media server.
|
|
|
|
*/
|
|
|
|
public readonly bool $fileRelated = false,
|
|
|
|
/**
|
2023-10-05 17:16:53 +02:00
|
|
|
* Previous queued message.
|
2023-09-27 15:10:20 +02:00
|
|
|
*/
|
2023-10-05 17:16:53 +02:00
|
|
|
public readonly ?self $previousQueuedMessage = null,
|
2023-09-27 15:10:20 +02:00
|
|
|
/**
|
|
|
|
* Custom flood wait limit for this message.
|
|
|
|
*/
|
|
|
|
public readonly ?int $floodWaitLimit = null,
|
2023-12-23 22:13:17 +01:00
|
|
|
public readonly ?int $takeoutId = null,
|
2023-09-27 15:10:20 +02:00
|
|
|
private ?DeferredFuture $resultDeferred = null,
|
2023-10-04 22:55:26 +02:00
|
|
|
public readonly ?Cancellation $cancellation = null
|
2023-09-27 15:10:20 +02:00
|
|
|
) {
|
|
|
|
$this->userRelated = $constructor === 'users.getUsers' && $body === ['id' => [['_' => 'inputUserSelf']]] || $constructor === 'auth.exportAuthorization' || $constructor === 'updates.getDifference';
|
2020-10-18 14:46:34 +02:00
|
|
|
|
2023-09-27 15:10:20 +02:00
|
|
|
parent::__construct(!isset(MTProtoMessage::NOT_CONTENT_RELATED[$constructor]));
|
2023-11-11 16:55:29 +01:00
|
|
|
$cancellation?->subscribe(fn (CancelledException $e) => $this->reply(static fn () => throw $e));
|
2023-06-02 20:49:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether cancellation is requested.
|
|
|
|
*/
|
2023-06-04 15:16:06 +02:00
|
|
|
public function isCancellationRequested(): bool
|
|
|
|
{
|
2023-06-02 20:49:50 +02:00
|
|
|
return $this->cancellation?->isRequested() ?? false;
|
2020-10-18 14:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signal that we're trying to send the message.
|
|
|
|
*/
|
|
|
|
public function trySend(): void
|
|
|
|
{
|
2023-01-20 17:24:13 +01:00
|
|
|
if (!isset($this->sendDeferred)) {
|
|
|
|
$this->sendDeferred = new DeferredFuture;
|
2020-10-18 14:46:34 +02:00
|
|
|
}
|
|
|
|
$this->tries++;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Signal that the message was sent.
|
|
|
|
*/
|
|
|
|
public function sent(): void
|
|
|
|
{
|
|
|
|
if ($this->state & self::STATE_REPLIED) {
|
2021-12-07 17:16:15 +01:00
|
|
|
//throw new Exception("Trying to resend already replied message $this!");
|
2020-10-18 14:46:34 +02:00
|
|
|
}
|
|
|
|
$this->state |= self::STATE_SENT;
|
2023-10-01 20:05:04 +02:00
|
|
|
$this->sent = time();
|
2023-01-20 17:24:13 +01:00
|
|
|
if (isset($this->sendDeferred)) {
|
|
|
|
$sendDeferred = $this->sendDeferred;
|
|
|
|
$this->sendDeferred = null;
|
|
|
|
$sendDeferred->complete();
|
2020-10-18 14:46:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Set reply to message.
|
2023-01-20 14:14:29 +01:00
|
|
|
*
|
|
|
|
* @param mixed|(callable(): Throwable) $result
|
2020-10-18 14:46:34 +02:00
|
|
|
*/
|
|
|
|
public function reply($result): void
|
|
|
|
{
|
|
|
|
if ($this->state & self::STATE_REPLIED) {
|
2023-04-28 18:58:46 +02:00
|
|
|
//throw new Exception("Trying to double reply to message $this!");
|
|
|
|
// It can happen, no big deal
|
|
|
|
return;
|
2020-10-18 14:46:34 +02:00
|
|
|
}
|
|
|
|
$this->serializedBody = null;
|
|
|
|
$this->body = null;
|
|
|
|
|
2023-10-04 22:55:26 +02:00
|
|
|
if (!($this->state & self::STATE_SENT)) {
|
|
|
|
$this->sent();
|
|
|
|
}
|
|
|
|
|
2020-10-18 14:46:34 +02:00
|
|
|
$this->state |= self::STATE_REPLIED;
|
2023-01-20 17:24:13 +01:00
|
|
|
if ($this->resultDeferred) { // Sometimes can get an RPC error for constructors
|
|
|
|
$promise = $this->resultDeferred;
|
|
|
|
$this->resultDeferred = null;
|
2023-01-21 21:21:35 +01:00
|
|
|
EventLoop::queue($promise->complete(...), $result);
|
2020-10-18 14:46:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ACK message.
|
|
|
|
*/
|
|
|
|
public function ack(): void
|
|
|
|
{
|
|
|
|
$this->state |= self::STATE_ACKED;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get state of message.
|
|
|
|
*
|
2023-01-04 16:04:05 +01:00
|
|
|
* @return self::STATE_*
|
2020-10-18 14:46:34 +02:00
|
|
|
*/
|
|
|
|
public function getState(): int
|
|
|
|
{
|
|
|
|
return $this->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get message body.
|
|
|
|
*/
|
2022-12-30 22:31:20 +01:00
|
|
|
public function getBody()
|
2020-10-18 14:46:34 +02:00
|
|
|
{
|
2023-10-04 22:31:04 +02:00
|
|
|
return $this->body;
|
2020-10-18 14:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get message body or empty array.
|
|
|
|
*/
|
|
|
|
public function getBodyOrEmpty(): array
|
|
|
|
{
|
2023-10-04 22:31:04 +02:00
|
|
|
return (array) $this->body;
|
2020-10-18 14:46:34 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Check if we have a body.
|
|
|
|
*/
|
|
|
|
public function hasBody(): bool
|
|
|
|
{
|
|
|
|
return $this->body !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get serialized body.
|
|
|
|
*/
|
|
|
|
public function getSerializedBody(): ?string
|
|
|
|
{
|
|
|
|
return $this->serializedBody;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Check if we have a serialized body.
|
|
|
|
*/
|
|
|
|
public function hasSerializedBody(): bool
|
|
|
|
{
|
|
|
|
return $this->serializedBody !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get number of times this message was sent.
|
|
|
|
*/
|
|
|
|
public function getTries(): int
|
|
|
|
{
|
|
|
|
return $this->tries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get whether we should refresh references upon serialization of this message.
|
|
|
|
*/
|
|
|
|
public function shouldRefreshReferences(): bool
|
|
|
|
{
|
|
|
|
return $this->refreshReferences;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set serialized body.
|
|
|
|
*
|
|
|
|
* @param string $serializedBody Serialized body.
|
|
|
|
*/
|
|
|
|
public function setSerializedBody(string $serializedBody): self
|
|
|
|
{
|
|
|
|
$this->serializedBody = $serializedBody;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether we should refresh references upon serialization of this message.
|
|
|
|
*
|
|
|
|
* @param bool $refreshReferences Whether we should refresh references upon serialization of this message.
|
|
|
|
*/
|
|
|
|
public function setRefreshReferences(bool $refreshReferences): self
|
|
|
|
{
|
|
|
|
$this->refreshReferences = $refreshReferences;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get when was this message sent.
|
|
|
|
*/
|
2023-01-14 19:51:23 +01:00
|
|
|
public function getSent(): ?int
|
2020-10-18 14:46:34 +02:00
|
|
|
{
|
|
|
|
return $this->sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the message was sent.
|
|
|
|
*/
|
|
|
|
public function wasSent(): bool
|
|
|
|
{
|
|
|
|
return (bool) ($this->state & self::STATE_SENT);
|
|
|
|
}
|
2023-09-28 17:28:40 +02:00
|
|
|
/**
|
|
|
|
* Check if the message has a reply.
|
|
|
|
*/
|
|
|
|
public function hasReply(): bool
|
|
|
|
{
|
|
|
|
return (bool) ($this->state & self::STATE_REPLIED);
|
|
|
|
}
|
2020-10-18 14:46:34 +02:00
|
|
|
/**
|
|
|
|
* Check if can garbage collect this message.
|
|
|
|
*/
|
|
|
|
public function canGarbageCollect(): bool
|
|
|
|
{
|
|
|
|
if ($this->state & self::STATE_REPLIED) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!$this->hasPromise()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* For logging.
|
|
|
|
*/
|
2022-12-30 19:21:36 +01:00
|
|
|
public function __toString(): string
|
2020-10-18 14:46:34 +02:00
|
|
|
{
|
2023-01-22 20:03:51 +01:00
|
|
|
if ($this->state & self::STATE_REPLIED) {
|
|
|
|
$state = 'acked (by reply)';
|
|
|
|
} elseif ($this->state & self::STATE_ACKED) {
|
|
|
|
$state = 'acked';
|
|
|
|
} elseif ($this->state & self::STATE_SENT) {
|
2023-10-01 20:05:04 +02:00
|
|
|
$state = 'sent '.(time() - $this->sent).' seconds ago';
|
2023-01-22 20:03:51 +01:00
|
|
|
} else {
|
|
|
|
$state = 'pending';
|
|
|
|
}
|
2020-10-18 14:46:34 +02:00
|
|
|
if ($this->msgId) {
|
2023-06-29 16:11:05 +02:00
|
|
|
return "{$this->constructor} with message ID {$this->msgId} $state";
|
2020-10-18 14:46:34 +02:00
|
|
|
}
|
2023-01-22 17:42:04 +01:00
|
|
|
return "{$this->constructor} $state";
|
2020-10-18 14:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for message to be sent.
|
2023-01-11 18:47:27 +01:00
|
|
|
*
|
|
|
|
* @return Future<null>
|
2020-10-18 14:46:34 +02:00
|
|
|
*/
|
2022-12-30 21:43:58 +01:00
|
|
|
public function getSendPromise(): Future
|
2020-10-18 14:46:34 +02:00
|
|
|
{
|
2023-01-20 17:24:13 +01:00
|
|
|
if (!$this->sendDeferred) {
|
2020-10-18 14:46:34 +02:00
|
|
|
throw new Exception("Message was already sent, can't get send promise!");
|
|
|
|
}
|
2023-01-20 17:24:13 +01:00
|
|
|
return $this->sendDeferred->getFuture();
|
2020-10-18 14:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we have a promise.
|
|
|
|
*/
|
|
|
|
public function hasPromise(): bool
|
|
|
|
{
|
2023-01-20 17:24:13 +01:00
|
|
|
return $this->resultDeferred !== null;
|
2020-10-18 14:46:34 +02:00
|
|
|
}
|
|
|
|
|
2023-09-19 14:49:53 +02:00
|
|
|
/**
|
|
|
|
* Get the promise.
|
|
|
|
*/
|
|
|
|
public function getResultPromise(): Future
|
|
|
|
{
|
|
|
|
\assert($this->resultDeferred !== null);
|
|
|
|
return $this->resultDeferred->getFuture();
|
|
|
|
}
|
|
|
|
|
2020-10-18 14:46:34 +02:00
|
|
|
/**
|
|
|
|
* Reset sent time to trigger resending.
|
|
|
|
*/
|
|
|
|
public function resetSent(): self
|
|
|
|
{
|
|
|
|
$this->sent = 0;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-27 15:10:20 +02:00
|
|
|
* Set when was this message sent.
|
2020-10-18 14:46:34 +02:00
|
|
|
*
|
2023-09-27 15:10:20 +02:00
|
|
|
* @param int $sent When was this message sent.
|
2020-10-18 14:46:34 +02:00
|
|
|
*/
|
2023-09-27 15:10:20 +02:00
|
|
|
public function setSent(int $sent): self
|
2020-10-18 14:46:34 +02:00
|
|
|
{
|
2023-09-27 15:10:20 +02:00
|
|
|
$this->sent = $sent;
|
2020-10-18 14:46:34 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|