diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index d66c1198..7827fa89 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -61,12 +61,12 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB 'max_tries' => [ 'query' => 5, 'authorization' => 5, - 'response' => 5 + 'response' => 5, ], 'msg_array_limit' => [ 'incoming' => 30, 'outgoing' => 30, - ] + ], ]; foreach ($default_settings as $key => $param) { if (!isset($settings[$key])) { diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index b8b6f052..f1ff8f77 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -17,7 +17,8 @@ namespace danog\MadelineProto\MTProtoTools; */ class CallHandler extends AuthKeyHandler { - public function wait_for_response($res_id) { + public function wait_for_response($res_id) + { $response = null; $count = 0; while ($response == null && $count++ < $this->settings['max_tries']['response']) { @@ -25,14 +26,14 @@ class CallHandler extends AuthKeyHandler $deserialized = $this->tl->deserialize(\danog\MadelineProto\Tools::fopen_and_write('php://memory', 'rw+b', $server_answer)); $tempres = $this->handle_message($deserialized); if ($tempres == null) { - if (isset($this->outgoing_messages[$res_id]["response"])) { - $response = $this->outgoing_messages[$res_id]["response"]; + if (isset($this->outgoing_messages[$res_id]['response'])) { + $response = $this->outgoing_messages[$res_id]['response']; } } else { $response = $tempres; } } - switch ($response["_"]) { + switch ($response['_']) { case 'rpc_error': throw new Exception('Got rpc error '.$response['error_code'].': '.$response['error_message']); break; @@ -41,13 +42,14 @@ class CallHandler extends AuthKeyHandler break; } } + public function method_call($method, $args) { foreach (range(1, $this->settings['max_tries']['query']) as $i) { try { $int_message_id = $this->send_message($this->tl->serialize_method($method, $args), $this->tl->content_related($method)); - $this->outgoing_messages[$int_message_id]["method"] = $method; - $this->outgoing_messages[$int_message_id]["args"] = $args; + $this->outgoing_messages[$int_message_id]['method'] = $method; + $this->outgoing_messages[$int_message_id]['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().'. Recreating connection and retrying to call method...'); @@ -58,6 +60,7 @@ class CallHandler extends AuthKeyHandler if ($server_answer == null) { throw new Exception('An error occurred while calling method '.$method.'.'); } + return $server_answer; } throw new Exception('An error occurred while calling method '.$method.'.'); @@ -69,8 +72,8 @@ class CallHandler extends AuthKeyHandler try { $int_message_id = $this->send_message($this->tl->serialize_obj($object, $args), $this->tl->content_related($object)); // $server_answer = $this->recv_message(); - $this->outgoing_messages[$int_message_id]["method"] = $object; - $this->outgoing_messages[$int_message_id]["args"] = $args; + $this->outgoing_messages[$int_message_id]['method'] = $object; + $this->outgoing_messages[$int_message_id]['args'] = $args; } catch (Exception $e) { $this->log->log('An error occurred while calling object '.$object.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().'. Recreating connection and retrying to call object...'); unset($this->sock); diff --git a/src/danog/MadelineProto/MTProtoTools/Crypt.php b/src/danog/MadelineProto/MTProtoTools/Crypt.php index 39a251ff..3fac8589 100644 --- a/src/danog/MadelineProto/MTProtoTools/Crypt.php +++ b/src/danog/MadelineProto/MTProtoTools/Crypt.php @@ -87,5 +87,4 @@ class Crypt extends CallHandler return $ciphered; } - } diff --git a/src/danog/MadelineProto/MTProtoTools/MessageHandler.php b/src/danog/MadelineProto/MTProtoTools/MessageHandler.php index cc620e76..39120aab 100644 --- a/src/danog/MadelineProto/MTProtoTools/MessageHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/MessageHandler.php @@ -38,6 +38,7 @@ class MessageHandler extends Crypt $this->outgoing_messages[$int_message_id]['seq_no'] = $seq_no; } $this->sock->send_message($message); + return $int_message_id; } diff --git a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php index a4525dbc..6bf40d99 100644 --- a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php @@ -25,7 +25,7 @@ class ResponseHandler extends MsgIdHandler $this->ack_outgoing_message_id($msg_id); // Acknowledge that the server received my message } break; - + case 'rpc_result': end($this->incoming_messages); end($this->outgoing_messages); @@ -34,9 +34,10 @@ class ResponseHandler extends MsgIdHandler if ($response['req_msg_id'] != key($this->outgoing_messages)) { throw new Exception('Message id mismatch; req_msg_id ('.$response['req_msg_id'].') != last sent msg id ('.key($this->outgoing_messages).').'); } + return $this->handle_response($response['result'], $response['req_msg_id']); break; - + case 'future_salts': end($this->outgoing_messages); $this->ack_outgoing_message_id($response['req_msg_id']); // Acknowledge that the server received my request @@ -44,13 +45,14 @@ class ResponseHandler extends MsgIdHandler throw new Exception('Message id mismatch; req_msg_id ('.$response['req_msg_id'].') != last sent msg id ('.key($this->outgoing_messages).').'); } $this->log->log('Received future salts.'); + return $this->handle_response($response, $response['req_msg_id']); break; case 'pong': foreach ($this->outgoing_messages as $omessage) { - if ($omessage["args"]["ping_id"] == $response["ping_id"]) { - $this->outgoing_messages[$response["msg_id"]]["response"] = $response; + if ($omessage['args']['ping_id'] == $response['ping_id']) { + $this->outgoing_messages[$response['msg_id']]['response'] = $response; } } break; @@ -65,7 +67,7 @@ class ResponseHandler extends MsgIdHandler $this->log->log('Received container.'); $this->log->log($response['messages']); foreach ($response['messages'] as $message) { - $this->incoming_messages[$message['msg_id']] = [ 'seq_no' => $message['seqno']]; + $this->incoming_messages[$message['msg_id']] = ['seq_no' => $message['seqno']]; $responses[] = $this->handle_message($message['body']); } foreach ($responses as $response) { @@ -91,7 +93,9 @@ class ResponseHandler extends MsgIdHandler break; } } - public function handle_response($response, $res_id = null) { + + public function handle_response($response, $res_id = null) + { if ($res_id == null) { return $response; } @@ -100,7 +104,7 @@ class ResponseHandler extends MsgIdHandler case 'rpc_answer_dropped': $this->ack_outgoing_message_id($response['req_msg_id']); // Acknowledge that the server received the original query (the same one, the response to which we wish to forget) default: - $this->outgoing_messages[$res_id]["response"] = $response; + $this->outgoing_messages[$res_id]['response'] = $response; break; } }