MadelineProto/src/danog/MadelineProto/VoIP/AuthKeyHandler.php

261 lines
15 KiB
PHP
Raw Normal View History

<?php
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
/**
* AuthKeyHandler module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
2019-05-31 12:18:10 +02:00
* @copyright 2016-2019 Daniil Gentili <daniil@daniil.it>
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
2018-02-24 17:54:39 +01:00
namespace danog\MadelineProto\VoIP;
/**
* Manages the creation of the authorization key.
*
* https://core.telegram.org/mtproto/auth_key
* https://core.telegram.org/mtproto/samples-auth_key
*/
trait AuthKeyHandler
{
2017-05-27 23:21:14 +02:00
private $calls = [];
2019-06-04 14:55:58 +02:00
2019-10-29 21:33:23 +01:00
public function requestCall($user)
2019-05-15 22:08:49 +02:00
{
2019-10-29 21:33:23 +01:00
return $this->wait($this->requestCallAsync($user));
2019-05-15 22:08:49 +02:00
}
2019-06-04 14:55:58 +02:00
2019-10-29 21:33:23 +01:00
public function acceptCall($user)
2019-05-15 22:08:49 +02:00
{
2019-10-29 21:33:23 +01:00
return $this->wait($this->acceptCallAsync($user));
2019-05-15 22:08:49 +02:00
}
2019-06-04 14:55:58 +02:00
2019-10-29 21:33:23 +01:00
public function discardCall($call, $reason, $rating = [], $need_debug = true)
2019-05-15 22:08:49 +02:00
{
2019-10-29 21:33:23 +01:00
return $this->wait($this->discardCallAsync($call, $reason, $rating, $need_debug));
2019-05-15 22:08:49 +02:00
}
2019-06-04 14:55:58 +02:00
2019-10-29 21:33:23 +01:00
public function requestCallAsync($user)
{
2019-09-02 17:08:36 +02:00
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
2019-06-26 17:16:06 +02:00
throw \danog\MadelineProto\Exception::extension('libtgvoip');
2017-07-19 12:41:18 +02:00
}
2019-10-29 21:33:23 +01:00
$user = yield $this->getInfo($user);
2017-07-20 16:20:19 +02:00
if (!isset($user['InputUser']) || $user['InputUser']['_'] === 'inputUserSelf') {
throw new \danog\MadelineProto\Exception(\danog\MadelineProto\Lang::$current_lang['peer_not_in_db']);
2017-05-02 12:13:27 +02:00
}
2017-05-02 12:13:01 +02:00
$user = $user['InputUser'];
2019-09-02 17:08:36 +02:00
$this->logger->logger(\sprintf(\danog\MadelineProto\Lang::$current_lang['calling_user'], $user['user_id']), \danog\MadelineProto\Logger::VERBOSE);
2019-10-29 21:33:23 +01:00
$dh_config = yield $this->getDhConfig();
2018-04-08 19:53:30 +02:00
$this->logger->logger(\danog\MadelineProto\Lang::$current_lang['generating_a'], \danog\MadelineProto\Logger::VERBOSE);
2018-04-17 11:17:44 +02:00
$a = \phpseclib\Math\BigInteger::randomRange(\danog\MadelineProto\Magic::$two, $dh_config['p']->subtract(\danog\MadelineProto\Magic::$two));
2018-04-08 19:53:30 +02:00
$this->logger->logger(\danog\MadelineProto\Lang::$current_lang['generating_g_a'], \danog\MadelineProto\Logger::VERBOSE);
$g_a = $dh_config['g']->powMod($a, $dh_config['p']);
2019-10-29 21:33:23 +01:00
$this->checkG($g_a, $dh_config['p']);
2019-03-29 20:25:42 +01:00
$controller = new \danog\MadelineProto\VoIP(true, $user['user_id'], $this, \danog\MadelineProto\VoIP::CALL_STATE_REQUESTED);
2019-09-02 17:08:36 +02:00
$controller->storage = ['a' => $a, 'g_a' => \str_pad($g_a->toBytes(), 256, \chr(0), \STR_PAD_LEFT)];
2019-10-29 21:33:23 +01:00
$res = yield $this->methodCallAsyncRead('phone.requestCall', ['user_id' => $user, 'g_a_hash' => \hash('sha256', $g_a->toBytes(), true), 'protocol' => ['_' => 'phoneCallProtocol', 'udp_p2p' => true, 'udp_reflector' => true, 'min_layer' => 65, 'max_layer' => \danog\MadelineProto\VoIP::getConnectionMaxLayer()]], ['datacenter' => $this->datacenter->curdc]);
2019-03-29 20:25:42 +01:00
$controller->setCall($res['phone_call']);
$this->calls[$res['phone_call']['id']] = $controller;
2019-05-29 17:19:42 +02:00
yield $this->updaters[false]->resume();
2018-02-24 17:54:39 +01:00
2017-07-19 12:41:18 +02:00
return $controller;
}
2018-02-24 17:54:39 +01:00
2019-10-29 21:33:23 +01:00
public function acceptCallAsync($call)
{
2019-09-02 17:08:36 +02:00
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
throw new \danog\MadelineProto\Exception();
2017-04-02 16:43:47 +02:00
}
2019-10-29 21:33:23 +01:00
if ($this->callStatus($call['id']) !== \danog\MadelineProto\VoIP::CALL_STATE_ACCEPTED) {
2019-09-02 17:08:36 +02:00
$this->logger->logger(\sprintf(\danog\MadelineProto\Lang::$current_lang['call_error_1'], $call['id']));
2018-02-24 17:54:39 +01:00
2017-07-31 16:42:24 +02:00
return false;
}
2019-09-02 17:08:36 +02:00
$this->logger->logger(\sprintf(\danog\MadelineProto\Lang::$current_lang['accepting_call'], $this->calls[$call['id']]->getOtherID()), \danog\MadelineProto\Logger::VERBOSE);
2019-10-29 21:33:23 +01:00
$dh_config = yield $this->getDhConfig();
2018-04-08 19:53:30 +02:00
$this->logger->logger(\danog\MadelineProto\Lang::$current_lang['generating_b'], \danog\MadelineProto\Logger::VERBOSE);
2018-04-17 11:17:44 +02:00
$b = \phpseclib\Math\BigInteger::randomRange(\danog\MadelineProto\Magic::$two, $dh_config['p']->subtract(\danog\MadelineProto\Magic::$two));
2017-04-02 16:42:17 +02:00
$g_b = $dh_config['g']->powMod($b, $dh_config['p']);
2019-10-29 21:33:23 +01:00
$this->checkG($g_b, $dh_config['p']);
2018-02-24 17:54:39 +01:00
try {
2019-10-29 21:33:23 +01:00
$res = yield $this->methodCallAsyncRead('phone.acceptCall', ['peer' => $call, 'g_b' => $g_b->toBytes(), 'protocol' => ['_' => 'phoneCallProtocol', 'udp_reflector' => true, 'udp_p2p' => true, 'min_layer' => 65, 'max_layer' => \danog\MadelineProto\VoIP::getConnectionMaxLayer()]], ['datacenter' => $this->datacenter->curdc]);
} catch (\danog\MadelineProto\RPCErrorException $e) {
2017-07-28 22:38:13 +02:00
if ($e->rpc === 'CALL_ALREADY_ACCEPTED') {
2019-09-02 17:08:36 +02:00
$this->logger->logger(\sprintf(\danog\MadelineProto\Lang::$current_lang['call_already_accepted'], $call['id']));
2018-02-24 17:54:39 +01:00
return true;
}
2017-07-31 16:42:24 +02:00
if ($e->rpc === 'CALL_ALREADY_DECLINED') {
2018-04-08 19:53:30 +02:00
$this->logger->logger(\danog\MadelineProto\Lang::$current_lang['call_already_declined']);
2019-10-29 21:33:23 +01:00
yield $this->discardCallAsync($call['id'], 'phoneCallDiscardReasonHangup');
2018-02-24 17:54:39 +01:00
2017-07-31 16:42:24 +02:00
return false;
}
2018-02-24 17:54:39 +01:00
throw $e;
}
2017-07-20 16:20:19 +02:00
$this->calls[$res['phone_call']['id']]->storage['b'] = $b;
2019-05-29 17:19:42 +02:00
yield $this->updaters[false]->resume();
2018-02-24 17:54:39 +01:00
2017-07-31 16:42:24 +02:00
return true;
2017-04-02 16:42:17 +02:00
}
2018-02-24 17:54:39 +01:00
2019-10-29 21:33:23 +01:00
public function confirmCall($params)
2017-04-02 16:42:17 +02:00
{
2019-09-02 17:08:36 +02:00
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
2019-06-26 17:16:06 +02:00
throw \danog\MadelineProto\Exception::extension('libtgvoip');
2017-07-19 12:41:18 +02:00
}
2019-10-29 21:33:23 +01:00
if ($this->callStatus($params['id']) !== \danog\MadelineProto\VoIP::CALL_STATE_REQUESTED) {
2019-09-02 17:08:36 +02:00
$this->logger->logger(\sprintf(\danog\MadelineProto\Lang::$current_lang['call_error_2'], $params['id']));
2018-02-24 17:54:39 +01:00
2017-04-02 16:42:17 +02:00
return false;
}
2019-09-02 17:08:36 +02:00
$this->logger->logger(\sprintf(\danog\MadelineProto\Lang::$current_lang['call_confirming'], $this->calls[$params['id']]->getOtherID()), \danog\MadelineProto\Logger::VERBOSE);
2019-10-29 21:33:23 +01:00
$dh_config = yield $this->getDhConfig();
2019-07-18 23:59:02 +02:00
$params['g_b'] = new \phpseclib\Math\BigInteger((string) $params['g_b'], 256);
2019-10-29 21:33:23 +01:00
$this->checkG($params['g_b'], $dh_config['p']);
2019-09-02 17:08:36 +02:00
$key = \str_pad($params['g_b']->powMod($this->calls[$params['id']]->storage['a'], $dh_config['p'])->toBytes(), 256, \chr(0), \STR_PAD_LEFT);
2019-07-21 16:50:48 +02:00
try {
2019-10-29 21:33:23 +01:00
$res = (yield $this->methodCallAsyncRead('phone.confirmCall', ['key_fingerprint' => \substr(\sha1($key, true), -8), 'peer' => ['id' => $params['id'], 'access_hash' => $params['access_hash'], '_' => 'inputPhoneCall'], 'g_a' => $this->calls[$params['id']]->storage['g_a'], 'protocol' => ['_' => 'phoneCallProtocol', 'udp_reflector' => true, 'min_layer' => 65, 'max_layer' => \danog\MadelineProto\VoIP::getConnectionMaxLayer()]], ['datacenter' => $this->datacenter->curdc]))['phone_call'];
2019-07-21 16:50:48 +02:00
} catch (\danog\MadelineProto\RPCErrorException $e) {
if ($e->rpc === 'CALL_ALREADY_ACCEPTED') {
2019-09-02 17:08:36 +02:00
$this->logger->logger(\sprintf(\danog\MadelineProto\Lang::$current_lang['call_already_accepted'], $call['id']));
2019-07-21 16:50:48 +02:00
return true;
}
if ($e->rpc === 'CALL_ALREADY_DECLINED') {
$this->logger->logger(\danog\MadelineProto\Lang::$current_lang['call_already_declined']);
2019-10-29 21:33:23 +01:00
yield $this->discardCallAsync($call['id'], 'phoneCallDiscardReasonHangup');
2019-07-21 16:50:48 +02:00
return false;
}
throw $e;
}
2017-07-20 16:20:19 +02:00
$visualization = [];
2019-09-02 17:08:36 +02:00
$length = new \phpseclib\Math\BigInteger(\count(\danog\MadelineProto\Magic::$emojis));
foreach (\str_split(\hash('sha256', $key.\str_pad($this->calls[$params['id']]->storage['g_a'], 256, \chr(0), \STR_PAD_LEFT), true), 8) as $number) {
$number[0] = \chr(\ord($number[0]) & 0x7f);
2018-04-17 11:17:44 +02:00
$visualization[] = \danog\MadelineProto\Magic::$emojis[(int) (new \phpseclib\Math\BigInteger($number, 256))->divide($length)[1]->toString()];
2017-07-19 12:41:18 +02:00
}
2017-07-20 16:20:19 +02:00
$this->calls[$params['id']]->setVisualization($visualization);
2019-05-10 13:34:44 +02:00
2019-09-02 17:08:36 +02:00
$this->calls[$params['id']]->configuration['endpoints'] = \array_merge($res['connections'], $this->calls[$params['id']]->configuration['endpoints']);
$this->calls[$params['id']]->configuration = \array_merge(['recv_timeout' => $this->config['call_receive_timeout_ms'] / 1000, 'init_timeout' => $this->config['call_connect_timeout_ms'] / 1000, 'data_saving' => \danog\MadelineProto\VoIP::DATA_SAVING_NEVER, 'enable_NS' => true, 'enable_AEC' => true, 'enable_AGC' => true, 'auth_key' => $key, 'auth_key_id' => \substr(\sha1($key, true), -8), 'call_id' => \substr(\hash('sha256', $key, true), -16), 'network_type' => \danog\MadelineProto\VoIP::NET_TYPE_ETHERNET], $this->calls[$params['id']]->configuration);
2017-07-19 12:41:18 +02:00
$this->calls[$params['id']]->parseConfig();
2017-07-31 20:57:13 +02:00
$res = $this->calls[$params['id']]->startTheMagic();
2018-02-24 17:54:39 +01:00
2017-07-31 20:57:13 +02:00
return $res;
2017-04-02 16:42:17 +02:00
}
2018-02-24 17:54:39 +01:00
2019-10-29 21:33:23 +01:00
public function completeCall($params)
2017-04-02 16:43:47 +02:00
{
2019-09-02 17:08:36 +02:00
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
2019-06-26 17:16:06 +02:00
throw \danog\MadelineProto\Exception::extension('libtgvoip');
2017-07-19 12:41:18 +02:00
}
2019-10-29 21:33:23 +01:00
if ($this->callStatus($params['id']) !== \danog\MadelineProto\VoIP::CALL_STATE_ACCEPTED || !isset($this->calls[$params['id']]->storage['b'])) {
2019-09-02 17:08:36 +02:00
$this->logger->logger(\sprintf(\danog\MadelineProto\Lang::$current_lang['call_error_3'], $params['id']));
2018-02-24 17:54:39 +01:00
return false;
}
2019-09-02 17:08:36 +02:00
$this->logger->logger(\sprintf(\danog\MadelineProto\Lang::$current_lang['call_completing'], $this->calls[$params['id']]->getOtherID()), \danog\MadelineProto\Logger::VERBOSE);
2019-10-29 21:33:23 +01:00
$dh_config = yield $this->getDhConfig();
2019-09-02 17:08:36 +02:00
if (\hash('sha256', $params['g_a_or_b'], true) != $this->calls[$params['id']]->storage['g_a_hash']) {
throw new \danog\MadelineProto\SecurityException(\danog\MadelineProto\Lang::$current_lang['invalid_g_a']);
2017-04-02 16:42:17 +02:00
}
2019-07-18 23:59:02 +02:00
$params['g_a_or_b'] = new \phpseclib\Math\BigInteger((string) $params['g_a_or_b'], 256);
2019-10-29 21:33:23 +01:00
$this->checkG($params['g_a_or_b'], $dh_config['p']);
2019-09-02 17:08:36 +02:00
$key = \str_pad($params['g_a_or_b']->powMod($this->calls[$params['id']]->storage['b'], $dh_config['p'])->toBytes(), 256, \chr(0), \STR_PAD_LEFT);
if (\substr(\sha1($key, true), -8) != $params['key_fingerprint']) {
throw new \danog\MadelineProto\SecurityException(\danog\MadelineProto\Lang::$current_lang['fingerprint_invalid']);
}
2017-07-20 16:20:19 +02:00
$visualization = [];
2019-09-02 17:08:36 +02:00
$length = new \phpseclib\Math\BigInteger(\count(\danog\MadelineProto\Magic::$emojis));
foreach (\str_split(\hash('sha256', $key.\str_pad($params['g_a_or_b']->toBytes(), 256, \chr(0), \STR_PAD_LEFT), true), 8) as $number) {
$number[0] = \chr(\ord($number[0]) & 0x7f);
2018-04-17 11:17:44 +02:00
$visualization[] = \danog\MadelineProto\Magic::$emojis[(int) (new \phpseclib\Math\BigInteger($number, 256))->divide($length)[1]->toString()];
2017-07-20 16:20:19 +02:00
}
$this->calls[$params['id']]->setVisualization($visualization);
2019-09-02 17:08:36 +02:00
$this->calls[$params['id']]->configuration['endpoints'] = \array_merge($params['connections'], $this->calls[$params['id']]->configuration['endpoints']);
$this->calls[$params['id']]->configuration = \array_merge(['recv_timeout' => $this->config['call_receive_timeout_ms'] / 1000, 'init_timeout' => $this->config['call_connect_timeout_ms'] / 1000, 'data_saving' => \danog\MadelineProto\VoIP::DATA_SAVING_NEVER, 'enable_NS' => true, 'enable_AEC' => true, 'enable_AGC' => true, 'auth_key' => $key, 'auth_key_id' => \substr(\sha1($key, true), -8), 'call_id' => \substr(\hash('sha256', $key, true), -16), 'network_type' => \danog\MadelineProto\VoIP::NET_TYPE_ETHERNET], $this->calls[$params['id']]->configuration);
2017-07-20 16:20:19 +02:00
$this->calls[$params['id']]->parseConfig();
2018-02-24 17:54:39 +01:00
2017-07-31 20:57:13 +02:00
return $this->calls[$params['id']]->startTheMagic();
}
2018-02-24 17:54:39 +01:00
2019-10-29 21:33:23 +01:00
public function callStatus($id)
{
2019-09-02 17:08:36 +02:00
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
2019-06-26 17:16:06 +02:00
throw \danog\MadelineProto\Exception::extension('libtgvoip');
2017-07-19 12:41:18 +02:00
}
if (isset($this->calls[$id])) {
2017-07-19 12:41:18 +02:00
return $this->calls[$id]->getCallState();
}
2018-02-24 17:54:39 +01:00
2017-07-20 19:31:09 +02:00
return \danog\MadelineProto\VoIP::CALL_STATE_NONE;
}
2018-02-24 17:54:39 +01:00
2019-10-29 21:33:23 +01:00
public function getCall($call)
{
2019-09-02 17:08:36 +02:00
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
2019-06-26 17:16:06 +02:00
throw \danog\MadelineProto\Exception::extension('libtgvoip');
2017-07-19 12:41:18 +02:00
}
2018-02-24 17:54:39 +01:00
return $this->calls[$call];
}
2018-02-24 17:54:39 +01:00
2019-10-29 21:33:23 +01:00
public function discardCallAsync($call, $reason, $rating = [], $need_debug = true)
{
2019-09-02 17:08:36 +02:00
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
2019-06-26 17:16:06 +02:00
throw \danog\MadelineProto\Exception::extension('libtgvoip');
2017-07-20 16:20:19 +02:00
}
2017-07-22 01:02:48 +02:00
if (!isset($this->calls[$call['id']])) {
return;
}
2019-09-02 17:08:36 +02:00
$this->logger->logger(\sprintf(\danog\MadelineProto\Lang::$current_lang['call_discarding'], $call['id']), \danog\MadelineProto\Logger::VERBOSE);
2018-02-24 17:54:39 +01:00
2017-07-20 16:20:19 +02:00
try {
2019-10-29 21:33:23 +01:00
$res = yield $this->methodCallAsyncRead('phone.discardCall', ['peer' => $call, 'duration' => \time() - $this->calls[$call['id']]->whenCreated(), 'connection_id' => $this->calls[$call['id']]->getPreferredRelayID(), 'reason' => $reason], ['datacenter' => $this->datacenter->curdc]);
2017-07-20 16:20:19 +02:00
} catch (\danog\MadelineProto\RPCErrorException $e) {
2019-09-02 17:08:36 +02:00
if (!\in_array($e->rpc, ['CALL_ALREADY_DECLINED', 'CALL_ALREADY_ACCEPTED'])) {
2017-07-20 16:20:19 +02:00
throw $e;
}
}
2017-07-20 16:20:19 +02:00
if (!empty($rating)) {
2019-09-02 17:08:36 +02:00
$this->logger->logger(\sprintf(\danog\MadelineProto\Lang::$current_lang['call_set_rating'], $call['id']), \danog\MadelineProto\Logger::VERBOSE);
2019-10-29 21:33:23 +01:00
yield $this->methodCallAsyncRead('phone.setCallRating', ['peer' => $call, 'rating' => $rating['rating'], 'comment' => $rating['comment']], ['datacenter' => $this->datacenter->curdc]);
2017-07-20 16:20:19 +02:00
}
2019-05-10 13:34:44 +02:00
if ($need_debug && isset($this->calls[$call['id']])) {
2019-09-02 17:08:36 +02:00
$this->logger->logger(\sprintf(\danog\MadelineProto\Lang::$current_lang['call_debug_saving'], $call['id']), \danog\MadelineProto\Logger::VERBOSE);
2019-10-29 21:33:23 +01:00
yield $this->methodCallAsyncRead('phone.saveCallDebug', ['peer' => $call, 'debug' => $this->calls[$call['id']]->getDebugLog()], ['datacenter' => $this->datacenter->curdc]);
2017-07-20 16:20:19 +02:00
}
2017-07-21 12:23:09 +02:00
$update = ['_' => 'updatePhoneCall', 'phone_call' => $this->calls[$call['id']]];
if (isset($this->settings['pwr']['strict']) && $this->settings['pwr']['strict']) {
2019-10-29 21:33:23 +01:00
$this->pwrUpdateHandler($update);
2017-07-21 12:23:09 +02:00
} else {
\in_array($this->settings['updates']['callback'], [['danog\\MadelineProto\\API', 'getUpdatesUpdateHandler'], 'getUpdatesUpdateHandler']) ? $this->getUpdatesUpdateHandler($update) : $this->settings['updates']['callback']($update);
2017-07-21 12:23:09 +02:00
}
unset($this->calls[$call['id']]);
}
public function checkCalls()
{
2019-09-02 17:08:36 +02:00
\array_walk($this->calls, function ($controller, $id) {
2017-07-20 16:21:07 +02:00
if ($controller->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) {
$controller->discard();
}
});
}
2018-02-24 17:54:39 +01:00
}