diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index b4f59459..2b8e715b 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -676,33 +676,34 @@ class MTProto { $this->initing_authorization = true; $this->updates_state['sync_loading'] = true; + try { - foreach ($this->datacenter->sockets as $id => $socket) { - if (strpos($id, 'media')) { - continue; - } - $cdn = strpos($id, 'cdn'); - if ($socket->session_id === null) { - $socket->session_id = $this->random(8); - $socket->session_in_seq_no = 0; - $socket->session_out_seq_no = 0; - } - if ($socket->temp_auth_key === null || $socket->auth_key === null) { - if ($socket->auth_key === null && !$cdn) { - \danog\MadelineProto\Logger::log([sprintf(\danog\MadelineProto\Lang::$current_lang['gen_perm_auth_key'], $id)], Logger::NOTICE); - $socket->auth_key = $this->create_auth_key(-1, $id); + foreach ($this->datacenter->sockets as $id => $socket) { + if (strpos($id, 'media')) { + continue; } - \danog\MadelineProto\Logger::log([sprintf(\danog\MadelineProto\Lang::$current_lang['gen_temp_auth_key'], $id)], Logger::NOTICE); - $socket->temp_auth_key = $this->create_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in'], $id); - if (!$cdn) { - $this->bind_temp_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in'], $id); - $this->get_config($this->write_client_info('help.getConfig', [], ['datacenter' => $id])); + $cdn = strpos($id, 'cdn'); + if ($socket->session_id === null) { + $socket->session_id = $this->random(8); + $socket->session_in_seq_no = 0; + $socket->session_out_seq_no = 0; } - if (in_array($socket->protocol, ['http', 'https'])) { - $this->method_call('http_wait', ['max_wait' => 0, 'wait_after' => 0, 'max_delay' => 0], ['datacenter' => $id]); + if ($socket->temp_auth_key === null || $socket->auth_key === null) { + if ($socket->auth_key === null && !$cdn) { + \danog\MadelineProto\Logger::log([sprintf(\danog\MadelineProto\Lang::$current_lang['gen_perm_auth_key'], $id)], Logger::NOTICE); + $socket->auth_key = $this->create_auth_key(-1, $id); + } + \danog\MadelineProto\Logger::log([sprintf(\danog\MadelineProto\Lang::$current_lang['gen_temp_auth_key'], $id)], Logger::NOTICE); + $socket->temp_auth_key = $this->create_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in'], $id); + if (!$cdn) { + $this->bind_temp_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in'], $id); + $this->get_config($this->write_client_info('help.getConfig', [], ['datacenter' => $id])); + } + if (in_array($socket->protocol, ['http', 'https'])) { + $this->method_call('http_wait', ['max_wait' => 0, 'wait_after' => 0, 'max_delay' => 0], ['datacenter' => $id]); + } } } - } } finally { $this->initing_authorization = false; $this->updates_state['sync_loading'] = false; @@ -712,26 +713,28 @@ class MTProto public function sync_authorization($authorized_dc) { $this->updates_state['sync_loading'] = true; + try { - foreach ($this->datacenter->sockets as $new_dc => $socket) { - if (($int_dc = preg_replace('|/D+|', '', $new_dc)) == $authorized_dc) { - continue; + foreach ($this->datacenter->sockets as $new_dc => $socket) { + if (($int_dc = preg_replace('|/D+|', '', $new_dc)) == $authorized_dc) { + continue; + } + if ($int_dc != $new_dc) { + continue; + } + \danog\MadelineProto\Logger::log([$int_dc, $new_dc]); + if (strpos($new_dc, '_') !== false) { + continue; + } + \danog\MadelineProto\Logger::log([sprintf(\danog\MadelineProto\Lang::$current_lang['copy_auth_dcs'], $authorized_dc, $new_dc)], Logger::VERBOSE); + $exported_authorization = $this->method_call('auth.exportAuthorization', ['dc_id' => $new_dc], ['datacenter' => $authorized_dc]); + $this->method_call('auth.logOut', [], ['datacenter' => $new_dc]); + $authorization = $this->method_call('auth.importAuthorization', $exported_authorization, ['datacenter' => $new_dc]); } - if ($int_dc != $new_dc) { - continue; - } - \danog\MadelineProto\Logger::log([$int_dc, $new_dc]); - if (strpos($new_dc, '_') !== false) { - continue; - } - \danog\MadelineProto\Logger::log([sprintf(\danog\MadelineProto\Lang::$current_lang['copy_auth_dcs'], $authorized_dc, $new_dc)], Logger::VERBOSE); - $exported_authorization = $this->method_call('auth.exportAuthorization', ['dc_id' => $new_dc], ['datacenter' => $authorized_dc]); - $this->method_call('auth.logOut', [], ['datacenter' => $new_dc]); - $authorization = $this->method_call('auth.importAuthorization', $exported_authorization, ['datacenter' => $new_dc]); - } } finally { - $this->updates_state['sync_loading'] = false; + $this->updates_state['sync_loading'] = false; } + return $authorization; } diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index 54ad3b28..cd4b33bd 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -501,6 +501,7 @@ trait AuthKeyHandler public function get_dh_config() { $this->updates_state['sync_loading'] = true; + try { $dh_config = $this->method_call('messages.getDhConfig', ['version' => $this->dh_config['version'], 'random_length' => 0], ['datacenter' => $this->datacenter->curdc]); } finally { diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index cc9f5e87..3d474294 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -83,6 +83,7 @@ trait CallHandler if ($canunset = !$this->updates_state['sync_loading'] && !$this->threads && !$this->run_workers) { $this->updates_state['sync_loading'] = true; } + try { \danog\MadelineProto\Logger::log(['Calling method (try number '.$count.' for '.$method.')...'], \danog\MadelineProto\Logger::ULTRA_VERBOSE); @@ -210,18 +211,18 @@ trait CallHandler $last_error = $e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine(); \danog\MadelineProto\Logger::log(['An error occurred while calling method '.$method.': '.$last_error.'. Recreating connection and retrying to call method...'], \danog\MadelineProto\Logger::WARNING); //if (in_array($this->datacenter->sockets[$aargs['datacenter']]->protocol, ['http', 'https']) && $method !== 'http_wait') { - //$this->method_call('http_wait', ['max_wait' => $this->datacenter->sockets[$aargs['datacenter']]->timeout, 'wait_after' => 0, 'max_delay' => 0], ['datacenter' => $aargs['datacenter']]); + //$this->method_call('http_wait', ['max_wait' => $this->datacenter->sockets[$aargs['datacenter']]->timeout, 'wait_after' => 0, 'max_delay' => 0], ['datacenter' => $aargs['datacenter']]); //} else { - $this->datacenter->sockets[$aargs['datacenter']]->close_and_reopen(); + $this->datacenter->sockets[$aargs['datacenter']]->close_and_reopen(); //} continue; } catch (\RuntimeException $e) { $last_error = $e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine(); \danog\MadelineProto\Logger::log(['An error occurred while calling method '.$method.': '.$last_error.'. Recreating connection and retrying to call method...'], \danog\MadelineProto\Logger::WARNING); //if (in_array($this->datacenter->sockets[$aargs['datacenter']]->protocol, ['http', 'https']) && $method !== 'http_wait') { - //$this->method_call('http_wait', ['max_wait' => $this->datacenter->sockets[$aargs['datacenter']]->timeout, 'wait_after' => 0, 'max_delay' => 0], ['datacenter' => $aargs['datacenter']]); + //$this->method_call('http_wait', ['max_wait' => $this->datacenter->sockets[$aargs['datacenter']]->timeout, 'wait_after' => 0, 'max_delay' => 0], ['datacenter' => $aargs['datacenter']]); //} else { - $this->datacenter->sockets[$aargs['datacenter']]->close_and_reopen(); + $this->datacenter->sockets[$aargs['datacenter']]->close_and_reopen(); //} continue; } finally { diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index 782da27e..71c9176b 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -193,11 +193,14 @@ trait UpdateHandler break; case 'updates.channelDifference': $this->load_channel_state($channel)['sync_loading'] = true; + try { - $this->set_channel_state($channel, $difference); - $this->handle_update_messages($difference['new_messages'], $channel); - $this->handle_multiple_update($difference['other_updates'], [], $channel); - } finally { $this->load_channel_state($channel)['sync_loading'] = false; } + $this->set_channel_state($channel, $difference); + $this->handle_update_messages($difference['new_messages'], $channel); + $this->handle_multiple_update($difference['other_updates'], [], $channel); + } finally { + $this->load_channel_state($channel)['sync_loading'] = false; + } if (!$difference['final']) { unset($difference); $this->get_channel_difference($channel); @@ -206,11 +209,14 @@ trait UpdateHandler case 'updates.channelDifferenceTooLong': \danog\MadelineProto\Logger::log(['Got '.$difference['_']], \danog\MadelineProto\Logger::VERBOSE); $this->load_channel_state($channel)['sync_loading'] = true; + try { - $this->set_channel_state($channel, $difference); - $this->handle_update_messages($difference['messages'], $channel); - unset($difference); - } finally { $this->load_channel_state($channel)['sync_loading'] = false; } + $this->set_channel_state($channel, $difference); + $this->handle_update_messages($difference['messages'], $channel); + unset($difference); + } finally { + $this->load_channel_state($channel)['sync_loading'] = false; + } $this->get_channel_difference($channel); break; default: @@ -274,8 +280,9 @@ trait UpdateHandler } \danog\MadelineProto\Logger::log(['Got '.$difference['_']], \danog\MadelineProto\Logger::ULTRA_VERBOSE); + try { - switch ($difference['_']) { + switch ($difference['_']) { case 'updates.differenceEmpty': $this->set_update_state($difference); break; @@ -302,17 +309,22 @@ trait UpdateHandler throw new \danog\MadelineProto\Exception('Unrecognized update difference received: '.var_export($difference, true)); break; } - } finally { $this->updates_state['sync_loading'] = false; } + } finally { + $this->updates_state['sync_loading'] = false; + } } public function get_updates_state() { $last = $this->updates_state['sync_loading']; $this->updates_state['sync_loading'] = true; + try { - $data = $this->method_call('updates.getState', [], ['datacenter' => $this->datacenter->curdc]); - $this->get_cdn_config($this->datacenter->curdc); - } finally { $this->updates_state['sync_loading'] = $last; } + $data = $this->method_call('updates.getState', [], ['datacenter' => $this->datacenter->curdc]); + $this->get_cdn_config($this->datacenter->curdc); + } finally { + $this->updates_state['sync_loading'] = $last; + } return $data; } diff --git a/src/danog/MadelineProto/Wrappers/DialogHandler.php b/src/danog/MadelineProto/Wrappers/DialogHandler.php index 9e96337d..6fbd687a 100644 --- a/src/danog/MadelineProto/Wrappers/DialogHandler.php +++ b/src/danog/MadelineProto/Wrappers/DialogHandler.php @@ -23,26 +23,28 @@ trait DialogHandler $res = ['dialogs' => [0], 'count' => 1]; $datacenter = $this->datacenter->curdc; $peers = []; + try { - while ($this->dialog_params['count'] < $res['count']) { - \danog\MadelineProto\Logger::log([\danog\MadelineProto\Lang::$current_lang['getting_dialogs']]); - $res = $this->method_call('messages.getDialogs', $this->dialog_params, ['datacenter' => $datacenter, 'FloodWaitLimit' => 100]); - foreach ($res['dialogs'] as $dialog) { - if (!in_array($dialog['peer'], $peers)) { - $peers[] = $dialog['peer']; + while ($this->dialog_params['count'] < $res['count']) { + \danog\MadelineProto\Logger::log([\danog\MadelineProto\Lang::$current_lang['getting_dialogs']]); + $res = $this->method_call('messages.getDialogs', $this->dialog_params, ['datacenter' => $datacenter, 'FloodWaitLimit' => 100]); + foreach ($res['dialogs'] as $dialog) { + if (!in_array($dialog['peer'], $peers)) { + $peers[] = $dialog['peer']; + } + } + $this->dialog_params['count'] += count($res['dialogs']); + $this->dialog_params['offset_date'] = end($res['messages'])['date']; + $this->dialog_params['offset_peer'] = end($res['dialogs'])['peer']; + $this->dialog_params['offset_id'] = end($res['messages'])['id'] & 4294967296; + if (!isset($res['count'])) { + break; } } - $this->dialog_params['count'] += count($res['dialogs']); - $this->dialog_params['offset_date'] = end($res['messages'])['date']; - $this->dialog_params['offset_peer'] = end($res['dialogs'])['peer']; - $this->dialog_params['offset_id'] = end($res['messages'])['id'] & 4294967296; - if (!isset($res['count'])) { - break; - } - } } finally { - $this->updates_state['sync_loading'] = false; + $this->updates_state['sync_loading'] = false; } + return $peers; } }