From 05ee3c8b152b41452a44865ba621d8eb7a201ae5 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 26 Dec 2019 15:14:27 +0100 Subject: [PATCH] Multiple improvements for secret chat files, event handler, exception traces, update handling, file download --- composer.json | 4 +- examples/secret_bot.php | 24 +++--- src/danog/MadelineProto/Connection.php | 1 + src/danog/MadelineProto/DataCenter.php | 2 +- .../MTProtoTools/AuthKeyHandler.php | 2 +- .../MadelineProto/MTProtoTools/Files.php | 74 ++++++++++--------- .../MTProtoTools/UpdateHandler.php | 8 +- .../MadelineProto/TL/PrettyException.php | 2 +- src/danog/MadelineProto/TL/TL.php | 3 +- src/danog/MadelineProto/Wrappers/Events.php | 2 +- 10 files changed, 69 insertions(+), 53 deletions(-) diff --git a/composer.json b/composer.json index e43314d6..8e0e4b99 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,9 @@ "phpunit/phpunit": "^8", "amphp/php-cs-fixer-config": "dev-master", "haydenpierce/class-finder": "^0.4", - "ext-ctype":"*" + "ext-ctype":"*", + "danog/7to70": "^1", + "danog/7to5": "^1" }, "suggest": { "ext-libtgvoip": "Install the php-libtgvoip extension to make phone calls (https://github.com/danog/php-libtgvoip)" diff --git a/examples/secret_bot.php b/examples/secret_bot.php index 6dcf2224..e4c3712e 100755 --- a/examples/secret_bot.php +++ b/examples/secret_bot.php @@ -24,7 +24,7 @@ /* * Various ways to load MadelineProto */ -if (\file_exists(__DIR__.'/vendor/autoload.php')) { +if (\file_exists(__DIR__.'/../vendor/autoload.php')) { include 'vendor/autoload.php'; } else { if (!\file_exists('madeline.php')) { @@ -41,7 +41,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler { try { if (isset($update['message']['decrypted_message']['media'])) { - \danog\MadelineProto\Logger::log($this->downloadToDir($update, '.')); + $this->logger(yield $this->downloadToDir($update, '.')); } if (isset($this->sent[$update['message']['chat_id']])) { return; @@ -49,35 +49,35 @@ class EventHandler extends \danog\MadelineProto\EventHandler $secret_media = []; // Photo uploaded as document, secret chat - $secret_media['document_photo'] = ['peer' => $update, 'file' => 'tests/faust.jpg', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => \mime_content_type('tests/faust.jpg'), 'caption' => 'This file was uploaded using MadelineProto', 'key' => $inputEncryptedFile['key'], 'iv' => $inputEncryptedFile['iv'], 'file_name' => 'faust.jpg', 'size' => \filesize('tests/faust.jpg'), 'attributes' => [['_' => 'documentAttributeImageSize', 'w' => 1280, 'h' => 914]]]]]; + $secret_media['document_photo'] = ['peer' => $update, 'file' => 'tests/faust.jpg', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => \mime_content_type('tests/faust.jpg'), 'caption' => 'This file was uploaded using MadelineProto', 'file_name' => 'faust.jpg', 'size' => \filesize('tests/faust.jpg'), 'attributes' => [['_' => 'documentAttributeImageSize', 'w' => 1280, 'h' => 914]]]]]; // Photo, secret chat - $secret_media['photo'] = ['peer' => $update, 'file' => 'tests/faust.jpg', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaPhoto', 'thumb' => \file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'caption' => 'This file was uploaded using MadelineProto', 'key' => $inputEncryptedFile['key'], 'iv' => $inputEncryptedFile['iv'], 'size' => \filesize('tests/faust.jpg'), 'w' => 1280, 'h' => 914]]]; + $secret_media['photo'] = ['peer' => $update, 'file' => 'tests/faust.jpg', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaPhoto', 'thumb' => \file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'caption' => 'This file was uploaded using MadelineProto', 'size' => \filesize('tests/faust.jpg'), 'w' => 1280, 'h' => 914]]]; // GIF, secret chat - $secret_media['gif'] = ['peer' => $update, 'file' => 'tests/pony.mp4', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/pony.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => \mime_content_type('tests/pony.mp4'), 'caption' => 'test', 'key' => $inputEncryptedFile['key'], 'iv' => $inputEncryptedFile['iv'], 'file_name' => 'pony.mp4', 'size' => \filesize('tests/faust.jpg'), 'attributes' => [['_' => 'documentAttributeAnimated']]]]]; + $secret_media['gif'] = ['peer' => $update, 'file' => 'tests/pony.mp4', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/pony.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => \mime_content_type('tests/pony.mp4'), 'caption' => 'test', 'file_name' => 'pony.mp4', 'size' => \filesize('tests/faust.jpg'), 'attributes' => [['_' => 'documentAttributeAnimated']]]]]; // Sticker, secret chat - $secret_media['sticker'] = ['peer' => $update, 'file' => 'tests/lel.webp', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/lel.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => \mime_content_type('tests/lel.webp'), 'caption' => 'test', 'key' => $inputEncryptedFile['key'], 'iv' => $inputEncryptedFile['iv'], 'file_name' => 'lel.webp', 'size' => \filesize('tests/lel.webp'), 'attributes' => [['_' => 'documentAttributeSticker', 'alt' => 'LEL', 'stickerset' => ['_' => 'inputStickerSetEmpty']]]]]]; + $secret_media['sticker'] = ['peer' => $update, 'file' => 'tests/lel.webp', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/lel.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => \mime_content_type('tests/lel.webp'), 'caption' => 'test', 'file_name' => 'lel.webp', 'size' => \filesize('tests/lel.webp'), 'attributes' => [['_' => 'documentAttributeSticker', 'alt' => 'LEL', 'stickerset' => ['_' => 'inputStickerSetEmpty']]]]]]; // Document, secrey chat - $secret_media['document'] = ['peer' => $update, 'file' => 'tests/60', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => 'magic/magic', 'caption' => 'test', 'key' => $inputEncryptedFile['key'], 'iv' => $inputEncryptedFile['iv'], 'file_name' => 'magic.magic', 'size' => \filesize('tests/60'), 'attributes' => [['_' => 'documentAttributeFilename', 'file_name' => 'fairy']]]]]; + $secret_media['document'] = ['peer' => $update, 'file' => 'tests/60', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => 'magic/magic', 'caption' => 'test', 'file_name' => 'magic.magic', 'size' => \filesize('tests/60'), 'attributes' => [['_' => 'documentAttributeFilename', 'file_name' => 'fairy']]]]]; // Video, secret chat - $secret_media['video'] = ['peer' => $update, 'file' => 'tests/swing.mp4', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/swing.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => \mime_content_type('tests/swing.mp4'), 'caption' => 'test', 'key' => $inputEncryptedFile['key'], 'iv' => $inputEncryptedFile['iv'], 'file_name' => 'swing.mp4', 'size' => \filesize('tests/swing.mp4'), 'attributes' => [['_' => 'documentAttributeVideo', 'duration' => 5, 'w' => 1280, 'h' => 720]]]]]; + $secret_media['video'] = ['peer' => $update, 'file' => 'tests/swing.mp4', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/swing.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => \mime_content_type('tests/swing.mp4'), 'caption' => 'test', 'file_name' => 'swing.mp4', 'size' => \filesize('tests/swing.mp4'), 'attributes' => [['_' => 'documentAttributeVideo', 'duration' => 5, 'w' => 1280, 'h' => 720]]]]]; // audio, secret chat - $secret_media['audio'] = ['peer' => $update, 'file' => 'tests/mosconi.mp3', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => \mime_content_type('tests/mosconi.mp3'), 'caption' => 'test', 'key' => $inputEncryptedFile['key'], 'iv' => $inputEncryptedFile['iv'], 'file_name' => 'mosconi.mp3', 'size' => \filesize('tests/mosconi.mp3'), 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => false, 'duration' => 1, 'title' => 'AH NON LO SO IO', 'performer' => 'IL DIO GERMANO MOSCONI']]]]]; + $secret_media['audio'] = ['peer' => $update, 'file' => 'tests/mosconi.mp3', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => \mime_content_type('tests/mosconi.mp3'), 'caption' => 'test', 'file_name' => 'mosconi.mp3', 'size' => \filesize('tests/mosconi.mp3'), 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => false, 'duration' => 1, 'title' => 'AH NON LO SO IO', 'performer' => 'IL DIO GERMANO MOSCONI']]]]]; - $secret_media['voice'] = ['peer' => $update, 'file' => 'tests/mosconi.mp3', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => \mime_content_type('tests/mosconi.mp3'), 'caption' => 'test', 'key' => $inputEncryptedFile['key'], 'iv' => $inputEncryptedFile['iv'], 'file_name' => 'mosconi.mp3', 'size' => \filesize('tests/mosconi.mp3'), 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => true, 'duration' => 1, 'title' => 'AH NON LO SO IO', 'performer' => 'IL DIO GERMANO MOSCONI']]]]]; + $secret_media['voice'] = ['peer' => $update, 'file' => 'tests/mosconi.mp3', 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => \file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => \mime_content_type('tests/mosconi.mp3'), 'caption' => 'test', 'file_name' => 'mosconi.mp3', 'size' => \filesize('tests/mosconi.mp3'), 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => true, 'duration' => 1, 'title' => 'AH NON LO SO IO', 'performer' => 'IL DIO GERMANO MOSCONI']]]]]; foreach ($secret_media as $type => $smessage) { - $type = yield $this->messages->sendEncryptedFile($smessage); + yield $this->messages->sendEncryptedFile($smessage); } $i = 0; while ($i < 10) { - echo "SENDING MESSAGE $i TO ".$update['message']['chat_id'].PHP_EOL; + $this->logger("SENDING MESSAGE $i TO ".$update['message']['chat_id']); // You can also use the sendEncrypted parameter for more options in secret chats //yield $this->messages->sendEncrypted(['peer' => $update, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => (string) ($i++)]]); yield $this->messages->sendMessage(['peer' => $update, 'message' => (string) ($i++)]); diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index 22b4a194..22a70b41 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -441,6 +441,7 @@ class Connection extends Session if ($message['method']) { $body = yield $this->API->getTL()->serializeMethod($message['_'], $body); } else { + $body['_'] = $message['_']; $body = yield $this->API->getTL()->serializeObject(['type' => ''], $body, $message['_']); } if ($refreshNext) { diff --git a/src/danog/MadelineProto/DataCenter.php b/src/danog/MadelineProto/DataCenter.php index 4a5c58a9..f542822b 100644 --- a/src/danog/MadelineProto/DataCenter.php +++ b/src/danog/MadelineProto/DataCenter.php @@ -543,7 +543,7 @@ class DataCenter * * @return MTProto */ - public function getAPI(): MTProto + public function getAPI() { return $this->API; } diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index c23765dc..10208734 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -522,7 +522,7 @@ trait AuthKeyHandler { $dh_config = yield $this->methodCallAsyncRead('messages.getDhConfig', ['version' => $this->dh_config['version'], 'random_length' => 0], ['datacenter' => $this->datacenter->curdc]); if ($dh_config['_'] === 'messages.dhConfigNotModified') { - $this->logger->logger(\danog\MadelineProto\Logger::VERBOSE, ['DH configuration not modified']); + $this->logger->logger('DH configuration not modified', \danog\MadelineProto\Logger::VERBOSE); return $this->dh_config; } diff --git a/src/danog/MadelineProto/MTProtoTools/Files.php b/src/danog/MadelineProto/MTProtoTools/Files.php index a501d968..0262aa90 100644 --- a/src/danog/MadelineProto/MTProtoTools/Files.php +++ b/src/danog/MadelineProto/MTProtoTools/Files.php @@ -244,12 +244,12 @@ trait Files $bytes = yield $callable($part_num * $part_size, $part_size); - if (!$already_fetched) { - \hash_update($ctx, $bytes); - } if ($ige) { $bytes = $ige->encrypt(\str_pad($bytes, $part_size, \chr(0))); } + if (!$already_fetched) { + \hash_update($ctx, $bytes); + } return ['file_id' => $file_id, 'file_part' => $part_num, 'file_total_parts' => $part_total_num, 'bytes' => $bytes]; }, @@ -1070,38 +1070,46 @@ trait Files ]; } - try { - $res = yield $this->methodCallAsyncRead( - $method[$cdn], - $basic_param + $offset, - [ - 'heavy' => true, - 'file' => true, - 'FloodWaitLimit' => 0, - 'datacenter' => &$datacenter, - 'postpone' => $postpone, - ] - ); - } catch (\danog\MadelineProto\RPCErrorException $e) { - if (\strpos($e->rpc, 'FLOOD_WAIT_') === 0) { - if (isset($message_media['MessageMedia']) && !$this->authorization['user']['bot'] && $this->settings['download']['report_broken_media']) { - try { - yield $this->methodCallAsyncRead('messages.sendMedia', ['peer' => 'support', 'media' => $message_media['MessageMedia'], 'message' => "I can't download this file, could you please help?"], ['datacenter' => $this->datacenter->curdc]); - } catch (RPCErrorException $e) { - $this->logger->logger('An error occurred while reporting the broken file: '.$e->rpc, Logger::FATAL_ERROR); - } catch (Exception $e) { - $this->logger->logger('An error occurred while reporting the broken file: '.$e->getMessage(), Logger::FATAL_ERROR); + $x = 0; + while (true) { + try { + $res = yield $this->methodCallAsyncRead( + $method[$cdn], + $basic_param + $offset, + [ + 'heavy' => true, + 'file' => true, + 'FloodWaitLimit' => 0, + 'datacenter' => &$datacenter, + 'postpone' => $postpone, + ] + ); + break; + } catch (\danog\MadelineProto\RPCErrorException $e) { + if (\strpos($e->rpc, 'FLOOD_WAIT_') === 0) { + if (isset($message_media['MessageMedia']) && !$this->authorization['user']['bot'] && $this->settings['download']['report_broken_media']) { + try { + yield $this->methodCallAsyncRead('messages.sendMedia', ['peer' => 'support', 'media' => $message_media['MessageMedia'], 'message' => "I can't download this file, could you please help?"], ['datacenter' => $this->datacenter->curdc]); + } catch (RPCErrorException $e) { + $this->logger->logger('An error occurred while reporting the broken file: '.$e->rpc, Logger::FATAL_ERROR); + } catch (Exception $e) { + $this->logger->logger('An error occurred while reporting the broken file: '.$e->getMessage(), Logger::FATAL_ERROR); + } } - } - throw new \danog\MadelineProto\Exception('The media server where this file is hosted is offline/overloaded, please try again later. Send the media to the telegram devs or to @danogentili to fix this.'); - } - switch ($e->rpc) { - case 'FILE_TOKEN_INVALID': - $cdn = false; - continue 2; - default: - throw $e; + if ($x++ === 5) { + throw new \danog\MadelineProto\Exception('The media server where this file is hosted is offline/overloaded, please try again later. Send the media to the telegram devs or to @danogentili to fix this.'); + } + yield Tools::sleep(1); + continue; + } + switch ($e->rpc) { + case 'FILE_TOKEN_INVALID': + $cdn = false; + continue 3; + default: + throw $e; + } } } diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index de23aee6..1ee560d6 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -22,6 +22,8 @@ namespace danog\MadelineProto\MTProtoTools; use Amp\Deferred; use Amp\Http\Client\Request; use Amp\Loop; +use danog\MadelineProto\Logger; +use danog\MadelineProto\RPCErrorException; /** * Manages updates. @@ -340,7 +342,11 @@ trait UpdateHandler return; } $this->logger->logger('Accepting secret chat '.$update['chat']['id'], \danog\MadelineProto\Logger::NOTICE); - yield $this->acceptSecretChat($update['chat']); + try { + yield $this->acceptSecretChat($update['chat']); + } catch (RPCErrorException $e) { + $this->logger->logger("Error while accepting secret chat: $e", Logger::FATAL_ERROR); + } break; case 'encryptedChatDiscarded': $this->logger->logger('Deleting secret chat '.$update['chat']['id'].' because it was revoked by the other user', \danog\MadelineProto\Logger::NOTICE); diff --git a/src/danog/MadelineProto/TL/PrettyException.php b/src/danog/MadelineProto/TL/PrettyException.php index 6a762a89..1b597064 100644 --- a/src/danog/MadelineProto/TL/PrettyException.php +++ b/src/danog/MadelineProto/TL/PrettyException.php @@ -89,7 +89,7 @@ trait PrettyException $tl = false; foreach (\array_reverse($trace ?? $this->getTrace()) as $k => $frame) { if (isset($frame['function']) && \in_array($frame['function'], ['serializeParams', 'serializeObject'])) { - if ($frame['args'][2] !== '') { + if (($frame['args'][2] ?? '') !== '') { $this->tl_trace .= $tl ? "['".$frame['args'][2]."']" : "While serializing: \t".$frame['args'][2]; $tl = true; } diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index a4356920..17975823 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -636,8 +636,7 @@ class TL } } elseif ($method === 'messages.sendEncryptedFile') { if (isset($arguments['file'])) { - if ( - ( + if (( !\is_array($arguments['file']) || !(isset($arguments['file']['_']) && $this->constructors->findByPredicate($arguments['file']['_']) === 'InputEncryptedFile') ) && diff --git a/src/danog/MadelineProto/Wrappers/Events.php b/src/danog/MadelineProto/Wrappers/Events.php index a1be6941..03a2b435 100644 --- a/src/danog/MadelineProto/Wrappers/Events.php +++ b/src/danog/MadelineProto/Wrappers/Events.php @@ -71,7 +71,7 @@ trait Events if ($method === 'onLoop') { $this->loop_callback = [$this->event_handler_instance, 'onLoop']; } elseif ($method === 'onAny') { - foreach ($this->constructors->by_id as $id => $constructor) { + foreach ($this->getTL()->getConstructors()->by_id as $id => $constructor) { if ($constructor['type'] === 'Update' && !isset($this->event_handler_methods[$constructor['predicate']])) { $this->event_handler_methods[$constructor['predicate']] = [$this->event_handler_instance, 'onAny']; }