From 0459498eb80c561ba2f2d6f2269e35b888f39321 Mon Sep 17 00:00:00 2001 From: danogentili Date: Thu, 13 Oct 2016 23:22:11 +0200 Subject: [PATCH] Fixed handling of certain responses when named params aren't used and beautified logging --- src/danog/MadelineProto/API.php | 2 +- src/danog/MadelineProto/Logging.php | 2 +- .../MTProtoTools/CallHandler.php | 3 ++- .../MTProtoTools/MsgIdHandler.php | 3 ++- .../MTProtoTools/ResponseHandler.php | 14 +++++----- src/danog/MadelineProto/TL/TL.php | 27 ++++++++++++------- 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index cfef6471..bc337c1f 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -21,7 +21,7 @@ class API extends Tools set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']); $this->session = new MTProto($params); var_dump($future_salts = $this->ping(3)); - $future_salts = $this->get_future_salts(3); + var_dump($this->get_future_salts(3)); } public function __destruct() diff --git a/src/danog/MadelineProto/Logging.php b/src/danog/MadelineProto/Logging.php index ec021483..faa0adc3 100644 --- a/src/danog/MadelineProto/Logging.php +++ b/src/danog/MadelineProto/Logging.php @@ -64,7 +64,7 @@ class Logging if (!is_string($param)) { $param = var_export($param, true); } - $param = basename(debug_backtrace()[0]['file'], '.php').': '.$param; + $param = str_pad(basename(debug_backtrace()[0]['file'], '.php').': ', 16).(($mode == 3) ? "\t" : "").$param; switch ($mode) { case '1': error_log($param); diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index b8757658..878de996 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -46,11 +46,12 @@ class CallHandler extends AuthKeyHandler { foreach (range(1, $this->settings['max_tries']['query']) as $i) { try { + $args = $this->tl->get_named_method_args($method, $args); $int_message_id = $this->send_message($this->tl->serialize_method($method, $args), $this->tl->content_related($method)); $this->outgoing_messages[$int_message_id]['content'] = ['method' => $method, 'args' => $args]; $server_answer = $this->wait_for_response($int_message_id); } catch (Exception $e) { - $this->log->log('An error occurred while calling method '.$method.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().$e->getTraceAsString().'. Recreating connection and retrying to call method...'); + $this->log->log('An error occurred while calling method '.$method.': '.$e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine().'. Recreating connection and retrying to call method...'); unset($this->connection); $this->connection = new \danog\MadelineProto\DataCenter($this->settings['connection'], $this->settings['connection_settings']); continue; diff --git a/src/danog/MadelineProto/MTProtoTools/MsgIdHandler.php b/src/danog/MadelineProto/MTProtoTools/MsgIdHandler.php index 64c0b949..d828c152 100644 --- a/src/danog/MadelineProto/MTProtoTools/MsgIdHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/MsgIdHandler.php @@ -74,7 +74,8 @@ class MsgIdHandler extends MessageHandler ); $keys = array_keys($this->outgoing_messages); asort($keys); - if ($int_message_id <= end($keys)) { + $keys = end($keys); + while ($int_message_id <= $keys) { $int_message_id += 4; } $this->check_message_id($int_message_id, true); diff --git a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php index 6670056b..19294044 100644 --- a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php @@ -41,6 +41,7 @@ class ResponseHandler extends MsgIdHandler break; case 'bad_msg_notification': + break; case 'bad_server_salt': $this->ack_outgoing_message_id($response['bad_msg_id']); // Acknowledge that the server received my request $this->outgoing_messages[$response['bad_msg_id']]['response'] = $last_received; @@ -48,14 +49,13 @@ class ResponseHandler extends MsgIdHandler break; case 'pong': -var_dump($this->outgoing_messages); - foreach ($this->outgoing_messages as $msg_id => &$omessage) { - if (isset($omessage['content']['args']['ping_id']) && $omessage['content']['args']['ping_id'] == $response['ping_id']) { - $omessage['response'] = $response['msg_id']; - $this->incoming_messages[$response['msg_id']]['content'] = $response; - $this->ack_outgoing_message_id($msg_id); - } + foreach ($this->outgoing_messages as $msg_id => &$omessage) { + if (isset($omessage['content']['args']['ping_id']) && $omessage['content']['args']['ping_id'] == $response['ping_id']) { + $omessage['response'] = $response['msg_id']; + $this->incoming_messages[$response['msg_id']]['content'] = $response; + $this->ack_outgoing_message_id($msg_id); } + } break; case 'new_session_created': // $this->settings['authorization']['temp_auth_key']['server_salt'] = $response['server_salt']; diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index 50439ad1..c857fb99 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -58,7 +58,23 @@ class TL return $bytes_io; } + public function get_named_method_args($type_, $kwargs) { + if (isset($this->method_name[$type_])) { + $tl_method = $this->method_name[$type_]; + } else { + throw new Exception('Could not extract type: '.$type_); + } + if (count(array_filter(array_keys($kwargs), 'is_string')) == 0) { + $argcount = 0; + $newargs = []; + foreach ($tl_method->params as $arg) { + $newargs[$arg['name']] = $kwargs[$argcount++]; + } + $kwargs = $newargs; + } + return $kwargs; + } public function serialize_method($type_, $kwargs) { $bytes_io = ''; @@ -68,15 +84,8 @@ class TL throw new Exception('Could not extract type: '.$type_); } $bytes_io .= \danog\PHP\Struct::pack('id); - if (count(array_filter(array_keys($kwargs), 'is_string')) > 0) { - foreach ($tl_method->params as $arg) { - $bytes_io .= $this->serialize_param($arg['type'], $kwargs[$arg['name']]); - } - } else { - $argcount = 0; - foreach ($tl_method->params as $arg) { - $bytes_io .= $this->serialize_param($arg['type'], $kwargs[$argcount++]); - } + foreach ($tl_method->params as $arg) { + $bytes_io .= $this->serialize_param($arg['type'], $kwargs[$arg['name']]); } return $bytes_io;