1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-26 21:34:41 +01:00

Various bugfixes

This commit is contained in:
Daniil Gentili 2019-06-18 12:31:44 +02:00
parent 47f0285881
commit 40e2e6c09d
11 changed files with 38 additions and 83 deletions

1
.gitignore vendored
View File

@ -115,3 +115,4 @@ madeline.php
.phpunit.result.cache
.vscode/*
.vscode
custom.md

View File

@ -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

View File

@ -48,7 +48,8 @@
},
"files": [
"src/BigIntegor.php",
"src/YieldReturnValue.php"
"src/YieldReturnValue.php",
"src/polyfill.php"
]
},
"repositories": [

View File

@ -1,67 +0,0 @@
#!/usr/bin/env php
<?php
/*
Copyright 2016-2019 Daniil Gentili
(https://daniil.it)
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/>.
*/
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);

View File

@ -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) {

View File

@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -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));
}
}

17
src/polyfill.php Normal file
View File

@ -0,0 +1,17 @@
<?php
// Polyfill for some PHP 5 functions
function callMe($allable, ...$args)
{
return $allable(...$args);
}
function returnMe($res) {
return $res;
}
if (!class_exists('\Throwable')) {
class Throwable extends \Exception {}
}
if (!function_exists('is_iterable')) {
function is_iterable($var) {
return is_array($var) || $var instanceof Traversable;
}
}

View File

@ -1,17 +1,19 @@
#!/bin/bash -e
#composer global require spatie/7to5 dev-master#7b3e0f4254aadd81cf1a7ef2ddad68d5fcdadcc1
# Configure
composer global require spatie/7to5 dev-master#80de80c7ebb0dd0805a79e939b0426883ffd9403
[ -f $HOME/.composer/vendor/bin/php7to5 ] && php7to5=$HOME/.composer/vendor/bin/php7to5
[ -f $HOME/.config/composer/vendor/bin/php7to5 ] && php7to5=$HOME/.config/composer/vendor/bin/php7to5
# Clean up
rm -rf phar7 phar5 MadelineProtoPhar
madelinePath=$PWD
cd
rm -rf phar7 phar5 MadelineProtoPhar
mkdir phar7
cd phar7
# Install
echo '{
"name": "danog/madelineprototests",
"minimum-stability":"dev",