diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index c98370fd..80d94412 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -17,7 +17,7 @@ namespace danog\MadelineProto\MTProtoTools; */ trait CallHandler { - public function method_call($method, $args = [], $message_id = null) + public function method_call($method, $args = [], $message_id = null, $heavy = false) { if (!is_array($args)) { throw new \danog\MadelineProto\Exception("Arguments aren't an array."); @@ -38,10 +38,13 @@ trait CallHandler $this->recv_message(); // This method receives data from the socket, and parses stuff if (!isset($this->datacenter->outgoing_messages[$int_message_id]['response']) || !isset($this->datacenter->incoming_messages[$this->datacenter->outgoing_messages[$int_message_id]['response']]['content'])) { // Checks if I have received the response to the called method, if not continue looping - + if ($this->only_updates) $res_count--; continue; } $server_answer = $this->datacenter->incoming_messages[$this->datacenter->outgoing_messages[$int_message_id]['response']]['content']; // continue was not called, so I got a response + if ($heavy) { + unset($this->datacenter->incoming_messages[$this->datacenter->outgoing_messages[$int_message_id]['response']]); + } } catch (\danog\MadelineProto\Exception $e) { \danog\MadelineProto\Logger::log('An error getting response of method '.$method.': '.$e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine().'. Retrying...'); continue; @@ -105,6 +108,10 @@ trait CallHandler $this->datacenter->close_and_reopen(); sleep(1); // To avoid flooding continue; + } finally { + if ($heavy) { + unset($this->datacenter->outgoing_messages[$int_message_id]); + } } if ($server_answer == null) { throw new \danog\MadelineProto\Exception('An error occurred while calling method '.$method.'.'); diff --git a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php index e488e6b8..70064d1d 100644 --- a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php @@ -70,6 +70,7 @@ trait ResponseHandler public function handle_messages() { foreach ($this->datacenter->new_incoming as $current_msg_id) { + $this->only_updates = false; $response = $this->datacenter->incoming_messages[$current_msg_id]['content']; \danog\MadelineProto\Logger::log('Received '.$response['_'].'.'); @@ -244,6 +245,7 @@ trait ResponseHandler case 'Updates': unset($this->datacenter->new_incoming[$current_msg_id]); $this->handle_updates($response); + $this->only_updates = true; break; default: \danog\MadelineProto\Logger::log('Trying to assign a response of type '.$response_type.' to its request...'); diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index be58e40e..28116e86 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -182,9 +182,8 @@ trait UpdateHandler case 'updateNewChannelMessage': case 'updateEditChannelMessage': if ($update['message']['_'] == 'messageEmpty') { - \danog\MadelineProto\Logger::log('Got message empty, saving...'); - - return $this->save_update($update); + \danog\MadelineProto\Logger::log('Got message empty, not saving'); + return false; } $channel_id = $update['message']['to_id']['channel_id']; break; @@ -206,13 +205,13 @@ trait UpdateHandler } else { $cur_state = &$this->get_channel_state($channel_id, (isset($update['pts']) ? $update['pts'] : 0) - (isset($update['pts_count']) ? $update['pts_count'] : 0)); } - /* + if ($cur_state['sync_loading']) { \danog\MadelineProto\Logger::log('Sync loading, not handling update'); return false; } - */ + switch ($update['_']) { case 'updateChannelTooLong': $this->get_channel_difference($channel_id); @@ -222,12 +221,11 @@ trait UpdateHandler case 'updateEditMessage': case 'updateNewChannelMessage': case 'updateEditChannelMessage': - $message = &$update['message']; - if ((isset($message['from_id']) && !$this->peer_isset($message['from_id'])) || - !$this->peer_isset($message['to_id']) || - (isset($message['via_bot_id']) && !$this->peer_isset($message['via_bot_id'])) || - (isset($message['entities']) && !$this->entities_peer_isset($message['entities'])) || - (isset($message['fwd_from']) && !$this->fwd_peer_isset($message['fwd_from']))) { + if ((isset($update['message']['from_id']) && !$this->peer_isset($update['message']['from_id'])) || + !$this->peer_isset($update['message']['to_id']) || + (isset($update['message']['via_bot_id']) && !$this->peer_isset($update['message']['via_bot_id'])) || + (isset($update['message']['entities']) && !$this->entities_peer_isset($update['message']['entities'])) || + (isset($update['message']['fwd_from']) && !$this->fwd_peer_isset($update['message']['fwd_from']))) { \danog\MadelineProto\Logger::log('Not enough data for message update, getting difference...'); if ($channel_id !== false && $this->peer_isset('-100'.$channel_id)) { @@ -238,9 +236,6 @@ trait UpdateHandler return false; } - if (isset($message['from_id']) && $message['from_id'] == $this->datacenter->authorization['user']['id']) { - $message['out'] = true; - } break; default: if ($channel_id !== false && !$this->peer_isset('channel#'.$channel_id)) { @@ -373,11 +368,11 @@ trait UpdateHandler $this->handle_update($update, $options); continue 2; } - $this->handle_update($update); + $this->save_update($update); } } else { foreach ($updates as $update) { - $this->handle_update($update); + $this->save_update($update); } } } @@ -385,7 +380,7 @@ trait UpdateHandler public function handle_update_messages($messages, $channel = false) { foreach ($messages as $message) { - $this->handle_update(['_' => $channel == false ? 'updateNewMessage' : 'updateNewChannelMessage', 'message' => $message, 'pts' => $channel == false ? $this->get_update_state()['pts'] : $this->get_channel_state($channel)['pts'], 'pts_count' => 0]); + $this->save_update(['_' => $channel == false ? 'updateNewMessage' : 'updateNewChannelMessage', 'message' => $message, 'pts' => $channel == false ? $this->get_update_state()['pts'] : $this->get_channel_state($channel)['pts'], 'pts_count' => 0]); } } @@ -394,6 +389,9 @@ trait UpdateHandler if (!$this->settings['updates']['handle_updates']) { return; } + if (isset($update['message']['from_id']) && $update['message']['from_id'] == $this->datacenter->authorization['user']['id']) { + $update['message']['out'] = true; + } \danog\MadelineProto\Logger::log('Saving an update of type '.$update['_'].'...'); $this->settings['updates']['callback']($update); } diff --git a/src/danog/MadelineProto/Wrappers/FilesHandler.php b/src/danog/MadelineProto/Wrappers/FilesHandler.php index 4c372bbd..0d0c2eff 100644 --- a/src/danog/MadelineProto/Wrappers/FilesHandler.php +++ b/src/danog/MadelineProto/Wrappers/FilesHandler.php @@ -428,7 +428,7 @@ trait FilesHandler $f = fopen($file, 'r'); fseek($f, 0); while (ftell($f) !== $file_size) { - if (!$this->API->method_call($method, ['file_id' => $file_id, 'file_part' => $part_num++, 'file_total_parts' => $part_total_num, 'bytes' => stream_get_contents($f, $part_size)])) { + if (!$this->API->method_call($method, ['file_id' => $file_id, 'file_part' => $part_num++, 'file_total_parts' => $part_total_num, 'bytes' => stream_get_contents($f, $part_size)], null, true)) { throw new \danog\MadelineProto\Exception('An error occurred while uploading file part '.$part_num); } $cb(ftell($f) * 100 / $file_size); @@ -531,7 +531,7 @@ trait FilesHandler $info = $this->get_download_info($message_media); $offset = ftell($stream); $part_size = 512 * 1024; - while (fwrite($stream, $this->API->method_call('upload.getFile', ['location' => $info['InputFileLocation'], 'offset' => $offset += $part_size, 'limit' => $part_size])['bytes']) != false) { + while (fwrite($stream, $this->API->method_call('upload.getFile', ['location' => $info['InputFileLocation'], 'offset' => $offset += $part_size, 'limit' => $part_size], null, true)['bytes']) != false) { $cb(ftell($stream) * 100 / $info['size']); } diff --git a/testing.php b/testing.php index 8db05325..c7f7ffcb 100755 --- a/testing.php +++ b/testing.php @@ -111,29 +111,3 @@ foreach (['@pwrtelegramgroup', '@pwrtelegramgroupita'] as $peer) { } sleep(5); var_dump($MadelineProto->API->get_updates()); - -$MadelineProto = \danog\MadelineProto\Serialization::deserialize('session_old.madeline'); - -if (file_exists('number.php') && $MadelineProto === false) { - include_once 'number.php'; - $settings['tl_schema'] = ['layer' => 46, 'src' => ['telegram' => __DIR__.'/src/danog/MadelineProto/TL_telegram_v46.tl']]; - $MadelineProto = new \danog\MadelineProto\API($settings); - - $checkedPhone = $MadelineProto->auth->checkPhone(// auth.checkPhone becomes auth->checkPhone - [ - 'phone_number' => $number, - ] - ); - \danog\MadelineProto\Logger::log($checkedPhone); - $sentCode = $MadelineProto->phone_login($number); - \danog\MadelineProto\Logger::log($sentCode); - echo 'Enter the code you received: '; - $code = fgets(STDIN, (isset($sentCode['type']['length']) ? $sentCode['type']['length'] : 5) + 1); - - $authorization = $MadelineProto->complete_phone_login($code); - \danog\MadelineProto\Logger::log($authorization); - echo 'Serializing MadelineProto to session_old.madeline...'.PHP_EOL; - echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('session_old.madeline', $MadelineProto).' bytes'.PHP_EOL; -} -echo 'Deserializing MadelineProto from session_old.madeline...'.PHP_EOL; -$MadelineProto = \danog\MadelineProto\Serialization::deserialize('session_old.madeline');