yield from fixes

This commit is contained in:
Daniil Gentili 2020-01-31 20:28:47 +01:00
parent 7ea0d9da91
commit 2ba80af424
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
20 changed files with 44 additions and 28 deletions

View File

@ -23,6 +23,7 @@ use Amp\Deferred;
use Amp\Promise;
use Amp\Success;
use danog\MadelineProto\Async\AsyncParameters;
use danog\MadelineProto\TL\Exception;
use danog\MadelineProto\Tools;
use function Amp\Promise\all;
@ -61,7 +62,7 @@ trait CallHandler
$this->ackOutgoingMessageId($message_id);
$this->gotResponseForOutgoingMessageId($message_id);
} else {
$this->logger->logger('Could not resend ' . (isset($this->outgoing_messages[$message_id]['_']) ? $this->outgoing_messages[$message_id]['_'] : $message_id));
$this->logger->logger('Could not resend '.(isset($this->outgoing_messages[$message_id]['_']) ? $this->outgoing_messages[$message_id]['_'] : $message_id));
}
}
if (!$postpone) {
@ -130,9 +131,9 @@ trait CallHandler
$aargs['datacenter'] = $args['id']['dc_id'];
return $this->API->methodCallAsyncWriteGenerator($method, $args, $aargs);
}
if (($aargs['file'] ?? false) && !$this->isMedia() && $this->API->datacenter->has($this->datacenter . '_media')) {
if (($aargs['file'] ?? false) && !$this->isMedia() && $this->API->datacenter->has($this->datacenter.'_media')) {
$this->logger->logger('Using media DC');
$aargs['datacenter'] = $this->datacenter . '_media';
$aargs['datacenter'] = $this->datacenter.'_media';
return $this->API->methodCallAsyncWriteGenerator($method, $args, $aargs);
}
if (\in_array($method, ['messages.setEncryptedTyping', 'messages.readEncryptedHistory', 'messages.sendEncrypted', 'messages.sendEncryptedFile', 'messages.sendEncryptedService', 'messages.receivedQueue'])) {
@ -169,7 +170,21 @@ trait CallHandler
}
}
$deferred = new Deferred();
$message = \array_merge($aargs, ['_' => $method, 'type' => $this->API->getTL()->getMethods()->findByMethod($method)['type'], 'contentRelated' => $this->contentRelated($method), 'promise' => $deferred, 'method' => true, 'unencrypted' => !$this->shared->hasTempAuthKey() && \strpos($method, '.') === false]);
$methodInfo = $this->API->getTL()->getMethods()->findByMethod($method);
if (!$methodInfo) {
throw new Exception("Could not find method $method!");
}
$message = \array_merge(
$aargs,
[
'_' => $method,
'type' => $methodInfo['type'],
'contentRelated' => $this->contentRelated($method),
'promise' => $deferred,
'method' => true,
'unencrypted' => !$this->shared->hasTempAuthKey() && \strpos($method, '.') === false
]
);
if (\is_object($args) && $args instanceof AsyncParameters) {
$message['body'] = yield $args->fetchParameters();
} else {

View File

@ -286,7 +286,7 @@ trait UpdateHandler
case 'updateShortChatMessage':
$from_id = isset($updates['from_id']) ? $updates['from_id'] : ($updates['out'] ? $this->authorization['user']['id'] : $updates['user_id']);
$to_id = isset($updates['chat_id']) ? -$updates['chat_id'] : ($updates['out'] ? $updates['user_id'] : $this->authorization['user']['id']);
if (!(yield from $this->peerIsset($from_id) || !(yield from $this->peerIsset($to_id) || isset($updates['via_bot_id']) && !(yield from $this->peerIsset($updates['via_bot_id']) || isset($updates['entities']) && !(yield from $this->entitiesPeerIsset($updates['entities']) || isset($updates['fwd_from']) && !(yield from $this->fwdPeerIsset($updates['fwd_from']))))))) {
if (!((yield from $this->peerIsset($from_id)) || !((yield from $this->peerIsset($to_id)) || isset($updates['via_bot_id']) && !((yield from $this->peerIsset($updates['via_bot_id'])) || isset($updates['entities']) && !((yield from $this->entitiesPeerIsset($updates['entities'])) || isset($updates['fwd_from']) && !(yield from $this->fwdPeerIsset($updates['fwd_from']))))))) {
yield $this->updaters[false]->resume();
return;
}

View File

@ -62,7 +62,7 @@ class RSA
$this->n = Tools::getVar($key, 'modulus');
$this->e = Tools::getVar($key, 'exponent');
\danog\MadelineProto\Logger::log(\danog\MadelineProto\Lang::$current_lang['computing_fingerprint'], Logger::ULTRA_VERBOSE);
$this->fp = \substr(\sha1(yield from $TL->serializeObject(['type' => 'bytes'], $this->n->toBytes(), 'key') . (yield from $TL->serializeObject(['type' => 'bytes'], $this->e->toBytes(), 'key')), true), -8);
$this->fp = \substr(\sha1((yield from $TL->serializeObject(['type' => 'bytes'], $this->n->toBytes(), 'key')) . (yield from $TL->serializeObject(['type' => 'bytes'], $this->e->toBytes(), 'key')), true), -8);
return $this;
}
/**

View File

@ -79,7 +79,7 @@ trait ResponseHandler
yield from $this->saveUpdate($update);
break;
case 'decryptedMessageLayer':
if (yield from $this->checkSecretOutSeqNo($update['message']['chat_id'], $update['message']['decrypted_message']['out_seq_no']) && (yield from $this->checkSecretInSeqNo($update['message']['chat_id'], $update['message']['decrypted_message']['in_seq_no']))) {
if ((yield from $this->checkSecretOutSeqNo($update['message']['chat_id'], $update['message']['decrypted_message']['out_seq_no'])) && (yield from $this->checkSecretInSeqNo($update['message']['chat_id'], $update['message']['decrypted_message']['in_seq_no']))) {
$this->secret_chats[$update['message']['chat_id']]['in_seq_no']++;
if ($update['message']['decrypted_message']['layer'] >= 17) {
$this->secret_chats[$update['message']['chat_id']]['layer'] = $update['message']['decrypted_message']['layer'];

View File

@ -48,7 +48,7 @@ class ADNLStream implements BufferedStreamInterface, MTProtoBufferInterface
*/
public function connectGenerator(ConnectionContext $ctx, string $header = ''): \Generator
{
$this->stream = (yield from $ctx->getStream($header));
$this->stream = (yield $ctx->getStream($header));
}
/**
* Async close.

View File

@ -51,7 +51,7 @@ class BufferedRawStream implements BufferedStreamInterface, BufferInterface, Raw
*/
public function connectGenerator(ConnectionContext $ctx, string $header = ''): \Generator
{
$this->stream = (yield from $ctx->getStream($header));
$this->stream = (yield $ctx->getStream($header));
$this->memory_stream = \fopen('php://memory', 'r+');
return true;
}

View File

@ -65,7 +65,7 @@ class CtrStream implements BufferedProxyStreamInterface, BufferInterface
$this->decrypt->enableContinuousBuffer();
$this->decrypt->setKey($this->extra['decrypt']['key']);
$this->decrypt->setIV($this->extra['decrypt']['iv']);
$this->stream = (yield from $ctx->getStream($header));
$this->stream = (yield $ctx->getStream($header));
}
/**
* Async close.

View File

@ -197,7 +197,7 @@ class HashedBufferedStream implements BufferedProxyStreamInterface, BufferInterf
$this->read_hash = null;
$this->read_check_after = 0;
$this->read_check_pos = 0;
$this->stream = (yield from $ctx->getStream($header));
$this->stream = (yield $ctx->getStream($header));
}
/**
* Async close.

View File

@ -421,7 +421,7 @@ class ConnectionContext
if ($obj instanceof ProxyStreamInterface) {
$obj->setExtra($extra);
}
yield from $obj->connect($this, $buffer);
yield $obj->connect($this, $buffer);
return $obj;
}
/**

View File

@ -45,7 +45,7 @@ class AbridgedStream implements BufferedStreamInterface, MTProtoBufferInterface
*/
public function connectGenerator(ConnectionContext $ctx, string $header = ''): \Generator
{
$this->stream = (yield from $ctx->getStream(\chr(239) . $header));
$this->stream = (yield $ctx->getStream(\chr(239) . $header));
}
/**
* Async close.
@ -87,7 +87,7 @@ class AbridgedStream implements BufferedStreamInterface, MTProtoBufferInterface
$buffer = yield $this->stream->getReadBuffer($l);
$length = \ord(yield $buffer->bufferRead(1));
if ($length >= 127) {
$length = \unpack('V', yield from $buffer->bufferRead(3) . "\0")[1];
$length = \unpack('V', (yield $buffer->bufferRead(3)) . "\0")[1];
}
$length <<= 2;
return $buffer;

View File

@ -56,7 +56,7 @@ class HttpStream implements MTProtoBufferInterface, BufferedProxyStreamInterface
public function connectGenerator(ConnectionContext $ctx, string $header = ''): \Generator
{
$this->ctx = $ctx->getCtx();
$this->stream = (yield from $ctx->getStream($header));
$this->stream = (yield $ctx->getStream($header));
$this->uri = $ctx->getUri();
}
/**
@ -147,7 +147,7 @@ class HttpStream implements MTProtoBufferInterface, BufferedProxyStreamInterface
}
if ($close) {
$this->disconnect();
yield from $this->connect($this->ctx);
yield $this->connect($this->ctx);
}
\danog\MadelineProto\Logger::log($read);
$this->code = \danog\MadelineProto\Tools::packSignedInt(-$code);
@ -156,7 +156,7 @@ class HttpStream implements MTProtoBufferInterface, BufferedProxyStreamInterface
}
if ($close) {
$this->stream->disconnect();
yield from $this->stream->connect($this->ctx);
yield $this->stream->connect($this->ctx);
}
if (isset($headers['content-length'])) {
$length = (int) $headers['content-length'];

View File

@ -47,7 +47,7 @@ class IntermediatePaddedStream implements BufferedStreamInterface, MTProtoBuffer
*/
public function connectGenerator(ConnectionContext $ctx, string $header = ''): \Generator
{
$this->stream = (yield from $ctx->getStream(\str_repeat(\chr(221), 4) . $header));
$this->stream = (yield $ctx->getStream(\str_repeat(\chr(221), 4) . $header));
}
/**
* Async close.

View File

@ -47,7 +47,7 @@ class IntermediateStream implements BufferedStreamInterface, MTProtoBufferInterf
*/
public function connectGenerator(ConnectionContext $ctx, string $header = ''): \Generator
{
$this->stream = (yield from $ctx->getStream(\str_repeat(\chr(238), 4) . $header));
$this->stream = (yield $ctx->getStream(\str_repeat(\chr(238), 4) . $header));
}
/**
* Async close.

View File

@ -67,7 +67,7 @@ class ObfuscatedStream extends CtrStream implements BufferedProxyStreamInterface
$iv = \substr($random, 40, 16);
$ivRev = \substr($reversed, 40, 16);
parent::setExtra(['encrypt' => ['key' => $key, 'iv' => $iv], 'decrypt' => ['key' => $keyRev, 'iv' => $ivRev]]);
yield from parent::connectGenerator($ctx);
yield parent::connectGenerator($ctx);
$random = \substr_replace($random, \substr(@$this->getEncryptor()->encrypt($random), 56, 8), 56, 8);
yield $this->getStream()->write($random);
}

View File

@ -53,7 +53,7 @@ class HttpProxy implements RawProxyStreamInterface, BufferedProxyStreamInterface
$ctx->setSocketContext($ctx->getSocketContext()->withTlsContext(new ClientTlsContext($uri->getHost())));
}
$ctx->setUri('tcp://' . $this->extra['address'] . ':' . $this->extra['port'])->secure(false);
$this->stream = (yield from $ctx->getStream());
$this->stream = (yield $ctx->getStream());
$address = $uri->getHost();
$port = $uri->getPort();
try {
@ -106,14 +106,14 @@ class HttpProxy implements RawProxyStreamInterface, BufferedProxyStreamInterface
}
if ($close) {
$this->disconnect();
yield from $this->connect($ctx);
yield $this->connect($ctx);
}
\danog\MadelineProto\Logger::log(\trim($read));
throw new \danog\MadelineProto\Exception($description, $code);
}
if ($close) {
yield $this->stream->disconnect();
yield from $this->stream->connect($ctx);
yield $this->stream->connect($ctx);
}
if (isset($headers['content-length'])) {
$length = (int) $headers['content-length'];

View File

@ -58,7 +58,7 @@ class SocksProxy implements RawProxyStreamInterface, BufferedProxyStreamInterfac
if (isset($this->extra['username']) && isset($this->extra['password'])) {
$methods .= \chr(2);
}
$this->stream = (yield from $ctx->getStream(\chr(5) . \chr(\strlen($methods)) . $methods));
$this->stream = (yield $ctx->getStream(\chr(5) . \chr(\strlen($methods)) . $methods));
$l = 2;
$buffer = yield $this->stream->getReadBuffer($l);
$version = \ord(yield $buffer->bufferRead(1));

View File

@ -68,7 +68,7 @@ class DefaultStream implements RawStreamInterface, ProxyStreamInterface
if ($secure) {
$ctx->setSocketContext($ctx->getSocketContext()->withTlsContext(new ClientTlsContext($uri->getHost())));
}
$this->stream = (yield from ($this->connector ?? connector())->connect((string) $uri, $ctx->getSocketContext(), $ctx->getCancellationToken()));
$this->stream = (yield ($this->connector ?? connector())->connect((string) $uri, $ctx->getSocketContext(), $ctx->getCancellationToken()));
if ($secure) {
yield $this->stream->setupTls();
}

View File

@ -69,7 +69,7 @@ class WsStream implements RawStreamInterface, ProxyStreamInterface
}
$this->dc = $ctx->getIntDc();
$handshake = new Handshake(\str_replace('tcp://', $ctx->isSecure() ? 'wss://' : 'ws://', $ctx->getStringUri()));
$this->stream = (yield from ($this->connector ?? connector())->connect($handshake, $ctx->getCancellationToken()));
$this->stream = (yield ($this->connector ?? connector())->connect($handshake, $ctx->getCancellationToken()));
if (\strlen($header)) {
yield $this->write($header);
}

View File

@ -34,7 +34,7 @@ trait BotAPIFiles
$photoSize['location']['secret'] = $photo['location']['secret'] ?? 0;
$photoSize['location']['dc_id'] = $photo['dc_id'] ?? 0;
$photoSize['location']['_'] = $thumbnail ? 'bot_thumbnail' : 'bot_photo';
$data = (yield from $this->TL->serializeObject(['type' => 'File'], $photoSize['location'], 'File') . \chr(2));
$data = (yield from $this->TL->serializeObject(['type' => 'File'], $photoSize['location'], 'File')) . \chr(2);
return ['file_id' => \danog\MadelineProto\Tools::base64urlEncode(\danog\MadelineProto\Tools::rleEncode($data)), 'width' => $photoSize['w'], 'height' => $photoSize['h'], 'file_size' => isset($photoSize['size']) ? $photoSize['size'] : \strlen($photoSize['bytes']), 'mime_type' => 'image/jpeg', 'file_name' => $photoSize['location']['volume_id'] . '_' . $photoSize['location']['local_id'] . $ext];
}
/**

View File

@ -525,7 +525,8 @@ class TL
if ($type['type'] === 'InputMessage' && !\is_array($object)) {
$object = ['_' => 'inputMessageID', 'id' => $object];
} elseif (isset($this->callbacks[TLCallback::TYPE_MISMATCH_CALLBACK][$type['type']]) && (!\is_array($object) || isset($object['_']) && $this->constructors->findByPredicate($object['_'])['type'] !== $type['type'])) {
$object = yield $this->callbacks[TLCallback::TYPE_MISMATCH_CALLBACK][$type['type']]($object);
$object = $this->callbacks[TLCallback::TYPE_MISMATCH_CALLBACK][$type['type']]($object);
$object = $object instanceof \Generator ? yield from $object : yield $object;
if (!isset($object[$type['type']])) {
throw new \danog\MadelineProto\Exception("Could not convert {$type['type']} object");
}