From 40e2e6c09dbcdb595174e0c5a278268f6768a212 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Tue, 18 Jun 2019 12:31:44 +0200 Subject: [PATCH] Various bugfixes --- .gitignore | 1 + .travis.yml | 2 + composer.json | 3 +- multiprocess_bot.php | 67 ------------------- phar.php | 12 ++-- src/danog/MadelineProto/Coroutine.php | 2 +- src/danog/MadelineProto/DataCenter.php | 1 - .../MadelineProto/Loop/Update/UpdateLoop.php | 6 +- src/danog/MadelineProto/Tools.php | 2 +- src/polyfill.php | 17 +++++ tests/makephar.sh | 8 ++- 11 files changed, 38 insertions(+), 83 deletions(-) delete mode 100644 multiprocess_bot.php create mode 100644 src/polyfill.php diff --git a/.gitignore b/.gitignore index b67baa9a9..978a67f3e 100644 --- a/.gitignore +++ b/.gitignore @@ -115,3 +115,4 @@ madeline.php .phpunit.result.cache .vscode/* .vscode +custom.md diff --git a/.travis.yml b/.travis.yml index 1f51ac862..46e5123d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ language: php php: - '7.3' +- '7.0' +- '5.6' before_install: - echo "phar.readonly = 0" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini diff --git a/composer.json b/composer.json index 93ad557af..651994760 100644 --- a/composer.json +++ b/composer.json @@ -48,7 +48,8 @@ }, "files": [ "src/BigIntegor.php", - "src/YieldReturnValue.php" + "src/YieldReturnValue.php", + "src/polyfill.php" ] }, "repositories": [ diff --git a/multiprocess_bot.php b/multiprocess_bot.php deleted file mode 100644 index afcdef47a..000000000 --- a/multiprocess_bot.php +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env php -. -*/ -set_include_path(get_include_path().':'.realpath(dirname(__FILE__).'/MadelineProto/')); - -/* - * Various ways to load MadelineProto - */ -if (!file_exists(__DIR__.'/vendor/autoload.php')) { - echo 'You did not run composer update, using madeline.php'.PHP_EOL; - if (!file_exists('madeline.php')) { - copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php'); - } - include 'madeline.php'; -} else { - require_once 'vendor/autoload.php'; -} - -class EventHandler extends \danog\MadelineProto\EventHandler -{ - public function onUpdateNewChannelMessage($update) - { - $this->onUpdateNewMessage($update); - } - - public function onUpdateNewMessage($update) - { - if (isset($update['message']['out']) && $update['message']['out']) { - return; - } - $res = json_encode($update, JSON_PRETTY_PRINT); - if ($res == '') { - $res = var_export($update, true); - } - - try { - $this->messages->sendMessage(['peer' => $update, 'message' => $res, 'reply_to_msg_id' => $update['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]); - } catch (\danog\MadelineProto\RPCErrorException $e) { - $this->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); - } - - try { - if (isset($update['message']['media']) && ($update['message']['media']['_'] == 'messageMediaPhoto' || $update['message']['media']['_'] == 'messageMediaDocument')) { - $time = microtime(true); - $file = $this->download_to_dir($update, '/tmp'); - $this->messages->sendMessage(['peer' => $update, 'message' => 'Downloaded to '.$file.' in '.(microtime(true) - $time).' seconds', 'reply_to_msg_id' => $update['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]); - } - } catch (\danog\MadelineProto\RPCErrorException $e) { - $this->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); - } - } -} - -$MadelineProto = new \danog\MadelineProto\API('bot.madeline'); - -$MadelineProto->start(); -$MadelineProto->setEventHandler('\EventHandler'); -$MadelineProto->loop(-1); diff --git a/phar.php b/phar.php index a5ea78ecd..4f9041278 100644 --- a/phar.php +++ b/phar.php @@ -44,7 +44,7 @@ function ___install_madeline() } } - // MadelineProto update + // Template strings for madelineProto update URLs $release_template = 'https://phar.madelineproto.xyz/release%s?v=new'; $phar_template = 'https://phar.madelineproto.xyz/madeline%s.phar?v=new'; @@ -53,21 +53,21 @@ function ___install_madeline() if ($release_branch === '-') { $release_branch = ''; } - $release_default_branch = ''; + $release_fallback_branch = ''; if (PHP_MAJOR_VERSION <= 5) { $release_branch = '5'.$release_branch; - $release_default_branch = '5'; - } elseif (PHP_MINOR_VERSION >= 3) { + $release_fallback_branch = '5'.$release_fallback_branch; + } elseif (PHP_MAJOR_VERSION === 7 && PHP_MINOR_VERSION === 0) { $release_branch = ''; } // Checking if defined branch/default branch builds can be downloaded if (!($release = @file_get_contents(sprintf($release_template, $release_branch)))) { - if (!($release = @file_get_contents(sprintf($release_template, $release_default_branch)))) { + if (!($release = @file_get_contents(sprintf($release_template, $release_fallback_branch)))) { return; } - $release_branch = $release_default_branch; + $release_branch = $release_fallback_branch; } if (!file_exists('madeline.phar') || !file_exists('madeline.phar.version') || file_get_contents('madeline.phar.version') !== $release) { diff --git a/src/danog/MadelineProto/Coroutine.php b/src/danog/MadelineProto/Coroutine.php index 7caca3a8f..66fa9d4a3 100644 --- a/src/danog/MadelineProto/Coroutine.php +++ b/src/danog/MadelineProto/Coroutine.php @@ -196,7 +196,7 @@ final class Coroutine implements Promise, \ArrayAccess { return Tools::call((function () use ($offset, $value) { $result = yield $this; - return $result[$offset] = value; + return $result[$offset] = $value; })()); } public function offsetUnset($offset) diff --git a/src/danog/MadelineProto/DataCenter.php b/src/danog/MadelineProto/DataCenter.php index 3a85929c8..2ea77ec93 100644 --- a/src/danog/MadelineProto/DataCenter.php +++ b/src/danog/MadelineProto/DataCenter.php @@ -42,7 +42,6 @@ use danog\MadelineProto\Stream\Proxy\SocksProxy; use danog\MadelineProto\Stream\Transport\DefaultStream; use danog\MadelineProto\Stream\Transport\WssStream; use danog\MadelineProto\Stream\Transport\WsStream; -use danog\MadelineProto\TL\Conversion\Exception; use Amp\DoH\Nameserver; use function Amp\call; use Amp\Promise; diff --git a/src/danog/MadelineProto/Loop/Update/UpdateLoop.php b/src/danog/MadelineProto/Loop/Update/UpdateLoop.php index 6d6f04b36..36fd8c7cf 100644 --- a/src/danog/MadelineProto/Loop/Update/UpdateLoop.php +++ b/src/danog/MadelineProto/Loop/Update/UpdateLoop.php @@ -121,8 +121,8 @@ class UpdateLoop extends ResumableSignalLoop $API->logger->logger("The PTS ({$difference['pts']}) I got with getDifference is smaller than the PTS I requested ".$state->pts().', using '.($state->pts() + 1), \danog\MadelineProto\Logger::VERBOSE); $difference['pts'] = $request_pts + 1; } - $state->update($difference); $result += yield $feeder->feed($difference['other_updates']); + $state->update($difference); $feeder->saveMessages($difference['new_messages']); if (!$difference['final']) { @@ -160,8 +160,8 @@ class UpdateLoop extends ResumableSignalLoop } $result += yield $feeder->feed($difference['other_updates']); $result += yield $feeder->feed($difference['new_encrypted_messages']); - $feeder->saveMessages($difference['new_messages']); $state->update($difference['state']); + $feeder->saveMessages($difference['new_messages']); unset($difference); break 2; case 'updates.differenceSlice': @@ -170,8 +170,8 @@ class UpdateLoop extends ResumableSignalLoop } $result += yield $feeder->feed($difference['other_updates']); $result += yield $feeder->feed($difference['new_encrypted_messages']); - $feeder->saveMessages($difference['new_messages']); $state->update($difference['intermediate_state']); + $feeder->saveMessages($difference['new_messages']); if ($difference['intermediate_state']['pts'] >= $toPts) { unset($difference); break 2; diff --git a/src/danog/MadelineProto/Tools.php b/src/danog/MadelineProto/Tools.php index cdbc53fef..74e85123e 100644 --- a/src/danog/MadelineProto/Tools.php +++ b/src/danog/MadelineProto/Tools.php @@ -310,7 +310,7 @@ trait Tools $logger->logger("Well you know, this might actually not be the actual exception, scroll up in the logs to see the actual exception"); if (!$zis || !$zis->destructing) Promise\rethrow(new Failure($e)); } else { - $logger->logger($e); + if ($logger) $logger->logger($e); Promise\rethrow(new Failure($e)); } } diff --git a/src/polyfill.php b/src/polyfill.php new file mode 100644 index 000000000..1edebf86c --- /dev/null +++ b/src/polyfill.php @@ -0,0 +1,17 @@ +