From b6a517ede52ca7c110af79e2765311e623543e2a Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 13 Sep 2019 16:55:48 +0200 Subject: [PATCH] Fix --- docs | 2 +- .../MadelineProto/AnnotationsBuilder.php | 2 + src/danog/MadelineProto/Connection.php | 4 +- .../MadelineProto/DocsBuilder/Methods.php | 5 +- src/danog/MadelineProto/Lang.php | 22 ++++---- .../Loop/Connection/ReadLoop.php | 5 +- .../MadelineProto/Loop/Update/UpdateLoop.php | 2 +- src/danog/MadelineProto/MTProto.php | 36 +++++++++++-- .../MTProtoTools/PeerHandler.php | 21 ++++---- .../MTProtoTools/ReferenceDatabase.php | 3 +- src/danog/MadelineProto/Magic.php | 4 +- src/danog/MadelineProto/TL/TLCallback.php | 54 +++++++++++++++++++ src/danog/MadelineProto/Wrappers/Login.php | 9 ++++ 13 files changed, 135 insertions(+), 34 deletions(-) diff --git a/docs b/docs index fe346258..9a873608 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit fe346258733323e5173e674f15059dea1a1eb880 +Subproject commit 9a873608310b43395032755c35bbdf17d35e23d7 diff --git a/src/danog/MadelineProto/AnnotationsBuilder.php b/src/danog/MadelineProto/AnnotationsBuilder.php index 4d113fce..ec03d2f8 100644 --- a/src/danog/MadelineProto/AnnotationsBuilder.php +++ b/src/danog/MadelineProto/AnnotationsBuilder.php @@ -44,6 +44,8 @@ class AnnotationsBuilder * Open file of class APIFactory * Insert properties * save the file with new content. + * + * @return void */ private function setProperties() { diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index 566ad05b..7ae281b1 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -423,7 +423,9 @@ class Connection extends Session */ public function flush() { - $this->writer->resume(); + if (isset($this->writer)) { + $this->writer->resume(); + } } /** * Connect main instance. diff --git a/src/danog/MadelineProto/DocsBuilder/Methods.php b/src/danog/MadelineProto/DocsBuilder/Methods.php index 5322f05d..a47d5f29 100644 --- a/src/danog/MadelineProto/DocsBuilder/Methods.php +++ b/src/danog/MadelineProto/DocsBuilder/Methods.php @@ -93,10 +93,11 @@ trait Methods '; - if (isset($this->td_descriptions['methods'][$data['method']])) - $this->human_docs_methods[$this->td_descriptions['methods'][$data['method']]['description'].': '.$data['method']] = '* '.$this->td_descriptions['methods'][$data['method']]['description'].': '.$data['method'].' + if (isset($this->td_descriptions['methods'][$data['method']])) { + $this->human_docs_methods[$this->td_descriptions['methods'][$data['method']]['description'].': '.$data['method']] = '* '.$this->td_descriptions['methods'][$data['method']]['description'].': '.$data['method'].' '; + } $params = ''; $lua_params = ''; diff --git a/src/danog/MadelineProto/Lang.php b/src/danog/MadelineProto/Lang.php index 8c58993a..0d76a5f3 100644 --- a/src/danog/MadelineProto/Lang.php +++ b/src/danog/MadelineProto/Lang.php @@ -1,6 +1,6 @@ - array ( + public static $lang = [ + 'it' => + [ 'phpseclib_fork' => 'Per favore installa questo fork di phpseclib: https://github.com/danog/phpseclib', 'inst_dc' => 'Istanziamento dei DataCenter...', 'load_rsa' => 'Caricamento delle chiavi RSA...', @@ -161,9 +161,9 @@ class Lang 'done' => 'Fatto!', 'cdn_reupload' => 'Il file non è disponibile sul nostro CDN, richiedo la copia!', 'stored_on_cdn' => 'Il file è scaricabile tramite CDN!', - ), - 'en' => - array ( + ], + 'en' => + [ 'req_pq' => 'Requesting pq...', 'done' => 'Done!', 'cdn_reupload' => 'File is not stored on CDN, requesting reupload!', @@ -5243,11 +5243,11 @@ class Lang 'object_account.themes_param_themes_type_Vector t' => 'Themes', 'method_account.installTheme_param_dark_type_true' => 'Whether to install the dark version', 'method_account.getThemes' => 'Get installed themes', - ), -); + ], +]; // THIS WILL BE OVERWRITTEN BY $lang["en"] - public static $current_lang = array ( + public static $current_lang = [ 'req_pq' => 'Requesting pq...', 'done' => 'Done!', 'cdn_reupload' => 'File is not stored on CDN, requesting reupload!', @@ -10327,5 +10327,5 @@ class Lang 'object_account.themes_param_themes_type_Vector t' => 'Themes', 'method_account.installTheme_param_dark_type_true' => 'Whether to install the dark version', 'method_account.getThemes' => 'Get installed themes', -); +]; } diff --git a/src/danog/MadelineProto/Loop/Connection/ReadLoop.php b/src/danog/MadelineProto/Loop/Connection/ReadLoop.php index 683ce31a..eb8594ce 100644 --- a/src/danog/MadelineProto/Loop/Connection/ReadLoop.php +++ b/src/danog/MadelineProto/Loop/Connection/ReadLoop.php @@ -206,7 +206,10 @@ class ReadLoop extends SignalLoop */ $session_id = \substr($decrypted_data, 8, 8); if ($session_id != $connection->session_id) { - throw new \danog\MadelineProto\Exception('Session id mismatch.'); + $connection->resetSession(); + Tools::callForkDefer($connection->reconnect()); + return; + throw new \danog\MadelineProto\Exception('Session id mismatch'); } $message_id = \substr($decrypted_data, 16, 8); $connection->check_message_id($message_id, ['outgoing' => false, 'container' => false]); diff --git a/src/danog/MadelineProto/Loop/Update/UpdateLoop.php b/src/danog/MadelineProto/Loop/Update/UpdateLoop.php index 3dc99092..2daaa1b7 100644 --- a/src/danog/MadelineProto/Loop/Update/UpdateLoop.php +++ b/src/danog/MadelineProto/Loop/Update/UpdateLoop.php @@ -84,7 +84,7 @@ class UpdateLoop extends ResumableSignalLoop try { $difference = yield $API->method_call_async_read('updates.getChannelDifference', ['channel' => 'channel#'.$this->channelId, 'filter' => ['_' => 'channelMessagesFilterEmpty'], 'pts' => $request_pts, 'limit' => $limit, 'force' => true], ['datacenter' => $API->datacenter->curdc, 'postpone' => $first]); } catch (RPCErrorException $e) { - if (\in_array($e->rpc, ['CHANNEL_PRIVATE', 'CHANNEL_INVALID', 'CHAT_FORBIDDEN'])) { + if (\in_array($e->rpc, ['CHANNEL_PRIVATE', 'CHAT_FORBIDDEN'])) { $feeder->signal(true); unset($API->updaters[$this->channelId], $API->feeders[$this->channelId]); diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index e4db0c09..9924f170 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -26,6 +26,7 @@ use danog\MadelineProto\Loop\Update\FeedLoop; use danog\MadelineProto\Loop\Update\SeqLoop; use danog\MadelineProto\Loop\Update\UpdateLoop; use danog\MadelineProto\MTProtoTools\CombinedUpdatesState; +use danog\MadelineProto\MTProtoTools\MinDatabase; use danog\MadelineProto\MTProtoTools\ReferenceDatabase; use danog\MadelineProto\MTProtoTools\UpdatesState; use danog\MadelineProto\TL\TLCallback; @@ -82,7 +83,7 @@ class MTProto extends AsyncConstruct implements TLCallback * * @var int */ - const V = 131; + const V = 132; /** * String release version. * @@ -347,6 +348,12 @@ class MTProto extends AsyncConstruct implements TLCallback * @var \danog\MadelineProto\MTProtoTools\ReferenceDatabase */ public $referenceDatabase; + /** + * min database. + * + * @var \danog\MadelineProto\MTProtoTools\MinDatabase + */ + public $minDatabase; /** * Phone config loop. * @@ -433,6 +440,9 @@ class MTProto extends AsyncConstruct implements TLCallback if (!isset($this->referenceDatabase)) { $this->referenceDatabase = new ReferenceDatabase($this); } + if (!isset($this->minDatabase)) { + $this->minDatabase = new MinDatabase($this); + } // Load rsa keys $this->logger->logger(\danog\MadelineProto\Lang::$current_lang['load_rsa'], Logger::ULTRA_VERBOSE); $this->rsa_keys = []; @@ -445,7 +455,11 @@ class MTProto extends AsyncConstruct implements TLCallback * Define some needed numbers for BigInteger */ $this->logger->logger(\danog\MadelineProto\Lang::$current_lang['TL_translation'], Logger::ULTRA_VERBOSE); - $this->construct_TL($this->settings['tl_schema']['src'], [$this, $this->referenceDatabase]); + $callbacks = [$this, $this->referenceDatabase]; + if (!($this->authorization['user']['bot'] ?? false)) { + $callbacks []= $this->minDatabase; + } + $this->construct_TL($this->settings['tl_schema']['src'], $callbacks); yield $this->connect_to_all_dcs_async(); $this->startLoops(); $this->datacenter->curdc = 2; @@ -472,7 +486,7 @@ class MTProto extends AsyncConstruct implements TLCallback if ($this->settings['serialization']['cleanup_before_serialization']) { $this->cleanup(); } - return ['supportUser', 'referenceDatabase', 'channel_participants', 'event_handler', 'event_handler_instance', 'loop_callback', 'web_template', 'encrypted_layer', 'settings', 'config', 'authorization', 'authorized', 'rsa_keys', 'dh_config', 'chats', 'last_stored', 'qres', 'got_state', 'channels_state', 'updates', 'updates_key', 'full_chats', 'msg_ids', 'dialog_params', 'datacenter', 'v', 'constructors', 'td_constructors', 'methods', 'td_methods', 'td_descriptions', 'tl_callbacks', 'temp_requested_secret_chats', 'temp_rekeyed_secret_chats', 'secret_chats', 'hook_url', 'storage', 'authorized_dc', 'tos']; + return ['supportUser', 'referenceDatabase', 'minDatabase', 'channel_participants', 'event_handler', 'event_handler_instance', 'loop_callback', 'web_template', 'encrypted_layer', 'settings', 'config', 'authorization', 'authorized', 'rsa_keys', 'dh_config', 'chats', 'last_stored', 'qres', 'got_state', 'channels_state', 'updates', 'updates_key', 'full_chats', 'msg_ids', 'dialog_params', 'datacenter', 'v', 'constructors', 'td_constructors', 'methods', 'td_methods', 'td_descriptions', 'tl_callbacks', 'temp_requested_secret_chats', 'temp_rekeyed_secret_chats', 'secret_chats', 'hook_url', 'storage', 'authorized_dc', 'tos']; } @@ -484,7 +498,11 @@ class MTProto extends AsyncConstruct implements TLCallback private function cleanup() { $this->referenceDatabase = new ReferenceDatabase($this); - $this->update_callbacks([$this, $this->referenceDatabase]); + $callbacks = [$this, $this->referenceDatabase]; + if (!($this->authorization['user']['bot'] ?? false)) { + $callbacks []= $this->minDatabase; + } + $this->update_callbacks($callbacks); return $this; } @@ -603,7 +621,14 @@ class MTProto extends AsyncConstruct implements TLCallback if (!isset($this->referenceDatabase)) { $this->referenceDatabase = new ReferenceDatabase($this); } - $this->update_callbacks([$this, $this->referenceDatabase]); + if (!isset($this->minDatabase)) { + $this->minDatabase = new MinDatabase($this); + } + $callbacks = [$this, $this->referenceDatabase]; + if (!($this->authorization['user']['bot'] ?? false)) { + $callbacks []= $this->minDatabase; + } + $this->update_callbacks($callbacks); $this->settings['connection_settings']['all']['ipv6'] = \danog\MadelineProto\Magic::$ipv6; /*if (isset($this->settings['pwr']['update_handler']) && $this->settings['pwr']['update_handler'] === $this->settings['updates']['callback']) { @@ -1243,6 +1268,7 @@ class MTProto extends AsyncConstruct implements TLCallback $this->users = []; $this->tos = ['expires' => 0, 'accepted' => true]; $this->referenceDatabase = new ReferenceDatabase($this); + $this->minDatabase = new MinDatabase($this); $this->dialog_params = ['_' => 'MadelineProto.dialogParams', 'limit' => 0, 'offset_date' => 0, 'offset_id' => 0, 'offset_peer' => ['_' => 'inputPeerEmpty'], 'count' => 0]; $this->full_chats = []; } diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index 3382f880..20916b12 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -79,12 +79,12 @@ trait PeerHandler } switch ($user['_']) { case 'user': - if (!isset($this->chats[$user['id']]) || $this->chats[$user['id']] !== $user) { + if (!isset($this->chats[$user['id']]) || ($this->chats[$user['id']] !== $user && !$user['min'])) { $this->logger->logger("Updated user {$user['id']}", \danog\MadelineProto\Logger::ULTRA_VERBOSE); $this->chats[$user['id']] = $user; $this->cache_pwr_chat($user['id'], false, true); } - // no break + break; case 'userEmpty': break; default: @@ -127,7 +127,7 @@ trait PeerHandler return; } - if (!isset($this->chats[$bot_api_id]) || $this->chats[$bot_api_id] !== $chat) { + if (!isset($this->chats[$bot_api_id]) || ($this->chats[$bot_api_id] !== $chat && !$chat['min'])) { $this->logger->logger("Updated chat $bot_api_id", \danog\MadelineProto\Logger::ULTRA_VERBOSE); $this->chats[$bot_api_id] = $chat; @@ -169,9 +169,6 @@ trait PeerHandler if ($e->rpc === 'CHANNEL_PRIVATE') { return true; } - if ($e->rpc === 'CHANNEL_INVALID') { - return true; - } return false; } @@ -259,7 +256,11 @@ trait PeerHandler case 'inputPeerUser': case 'inputUser': case 'peerUser': + case 'messageEntityMentionName': + case 'messageActionChatDeleteUser': return $id['user_id']; + case 'messageActionChatDeleteUser': + return $id['inviter_id']; case 'chat': case 'chatForbidden': case 'chatFull': @@ -494,8 +495,8 @@ trait PeerHandler $res['InputPeer'] = ['_' => 'inputPeerSelf']; $res['InputUser'] = ['_' => 'inputUserSelf']; } elseif (isset($constructor['access_hash'])) { - $res['InputPeer'] = ['_' => 'inputPeerUser', 'user_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']]; - $res['InputUser'] = ['_' => 'inputUser', 'user_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']]; + $res['InputPeer'] = ['_' => 'inputPeerUser', 'user_id' => $constructor['id'], 'access_hash' => $constructor['access_hash'], 'min' => $constructor['min']]; + $res['InputUser'] = ['_' => 'inputUser', 'user_id' => $constructor['id'], 'access_hash' => $constructor['access_hash'], 'min' => $constructor['min']]; } else { throw new \danog\MadelineProto\Exception('This peer is not present in the internal peer database'); } @@ -524,13 +525,13 @@ trait PeerHandler if (!isset($constructor['access_hash'])) { throw new \danog\MadelineProto\Exception('This peer is not present in the internal peer database'); } - $res['InputPeer'] = ['_' => 'inputPeerChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']]; + $res['InputPeer'] = ['_' => 'inputPeerChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash'], 'min' => $constructor['min']]; $res['Peer'] = ['_' => 'peerChannel', 'channel_id' => $constructor['id']]; $res['DialogPeer'] = ['_' => 'dialogPeer', 'peer' => $res['Peer']]; $res['NotifyPeer'] = ['_' => 'notifyPeer', 'peer' => $res['Peer']]; $res['InputDialogPeer'] = ['_' => 'inputDialogPeer', 'peer' => $res['InputPeer']]; $res['InputNotifyPeer'] = ['_' => 'inputNotifyPeer', 'peer' => $res['InputPeer']]; - $res['InputChannel'] = ['_' => 'inputChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']]; + $res['InputChannel'] = ['_' => 'inputChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash'], 'min' => $constructor['min']]; $res['channel_id'] = $constructor['id']; $res['bot_api_id'] = $this->to_supergroup($constructor['id']); $res['type'] = $constructor['megagroup'] ? 'supergroup' : 'channel'; diff --git a/src/danog/MadelineProto/MTProtoTools/ReferenceDatabase.php b/src/danog/MadelineProto/MTProtoTools/ReferenceDatabase.php index 5de1a843..045eb3f1 100644 --- a/src/danog/MadelineProto/MTProtoTools/ReferenceDatabase.php +++ b/src/danog/MadelineProto/MTProtoTools/ReferenceDatabase.php @@ -20,6 +20,7 @@ namespace danog\MadelineProto\MTProtoTools; use danog\MadelineProto\Exception; +use danog\MadelineProto\MTProto; use danog\MadelineProto\TL\TLCallback; use danog\MadelineProto\Tools; @@ -105,7 +106,7 @@ class ReferenceDatabase implements TLCallback private $refresh = false; private $refreshCount = 0; - public function __construct($API) + public function __construct(MTProto $API) { $this->API = $API; $this->init(); diff --git a/src/danog/MadelineProto/Magic.php b/src/danog/MadelineProto/Magic.php index 75afac7f..645b18d4 100644 --- a/src/danog/MadelineProto/Magic.php +++ b/src/danog/MadelineProto/Magic.php @@ -235,7 +235,9 @@ class Magic public static function shutdown(int $code = 0) { self::$signaled = true; - if (defined(STDIN::class)) getStdin()->unreference(); + if (\defined(STDIN::class)) { + getStdin()->unreference(); + } getInputBufferStream()->unreference(); if ($code !== 0) { $driver = Loop::get(); diff --git a/src/danog/MadelineProto/TL/TLCallback.php b/src/danog/MadelineProto/TL/TLCallback.php index e669203e..07bcce30 100644 --- a/src/danog/MadelineProto/TL/TLCallback.php +++ b/src/danog/MadelineProto/TL/TLCallback.php @@ -18,24 +18,78 @@ namespace danog\MadelineProto\TL; +/** + * Interface for managing TL serialization callbacks. + */ interface TLCallback { + /** + * + * + * @var int + */ const METHOD_CALLBACK = 0; const METHOD_BEFORE_CALLBACK = 1; const CONSTRUCTOR_CALLBACK = 2; const CONSTRUCTOR_BEFORE_CALLBACK = 3; const CONSTRUCTOR_SERIALIZE_CALLBACK = 4; + /** + * Called if objects of the specified type cannot be serialized. + * + * @var int + */ const TYPE_MISMATCH_CALLBACK = 5; + /** + * Called after serialization of method. + * + * Pass the method name and arguments + * + * @var array + */ public function getMethodCallbacks(): array; + /** + * Called right before serialization of method starts. + * + * Pass the method name + * + * @var array + */ public function getMethodBeforeCallbacks(): array; + /** + * Called right after deserialization of object, passing the final object. + * + * @var array + */ public function getConstructorCallbacks(): array; + /** + * Called right before deserialization of object. + * + * Pass only the constructor name + * + * @var array + */ public function getConstructorBeforeCallbacks(): array; + /** + * Called right before serialization of constructor. + * + * Passed the object, will return a modified version. + * + * @var array + */ public function getConstructorSerializeCallbacks(): array; + /** + * Called if objects of the specified type cannot be serialized. + * + * Passed the unserializable object, + * will try to convert it to an object of the proper type. + * + * @var array + */ public function getTypeMismatchCallbacks(): array; } diff --git a/src/danog/MadelineProto/Wrappers/Login.php b/src/danog/MadelineProto/Wrappers/Login.php index f6049cb3..6679ffb1 100644 --- a/src/danog/MadelineProto/Wrappers/Login.php +++ b/src/danog/MadelineProto/Wrappers/Login.php @@ -43,6 +43,9 @@ trait Login $this->logger->logger(\danog\MadelineProto\Lang::$current_lang['already_logged_in'], \danog\MadelineProto\Logger::NOTICE); yield $this->logout_async(); } + $callbacks = [$this, $this->referenceDatabase]; + $this->update_callbacks($callbacks); + $this->logger->logger(\danog\MadelineProto\Lang::$current_lang['login_bot'], \danog\MadelineProto\Logger::NOTICE); $this->authorization = yield $this->method_call_async_read('auth.importBotAuthorization', ['bot_auth_token' => $token, 'api_id' => $this->settings['app_info']['api_id'], 'api_hash' => $this->settings['app_info']['api_hash']], ['datacenter' => $this->datacenter->curdc]); $this->authorized = self::LOGGED_IN; @@ -152,6 +155,12 @@ trait Login $res = yield $this->get_self_async(); + $callbacks = [$this, $this->referenceDatabase]; + if (!($this->authorization['user']['bot'] ?? false)) { + $callbacks []= $this->minDatabase; + } + $this->update_callbacks($callbacks); + $this->startUpdateSystem(); return $res;