diff --git a/README.md b/README.md index 7014cbcd..7ffa6bde 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Created by [Daniil Gentili](https://daniil.it), licensed under AGPLv3. MadelineProto logo -Logo created by [Matthew Hesketh](https://matthewhesketh.com) (thanks again!). +Logo created by [Matthew Hesketh](http://matthewhesketh.com) (thanks again!). PHP implementation of MTProto, based on [telepy](https://github.com/griganton/telepy_old). diff --git a/build_docs.php b/build_docs.php index 065e7632..1ec76959 100755 --- a/build_docs.php +++ b/build_docs.php @@ -317,7 +317,7 @@ foreach ($constructors as $method => &$value) { $last_namespace = $new_namespace; } file_put_contents('constructors/index.md', '--- -title: Types +title: Constructors description: List of constructors --- # Constructors diff --git a/docs/API_docs/constructors/index.md b/docs/API_docs/constructors/index.md index c192d7bf..4a94a709 100644 --- a/docs/API_docs/constructors/index.md +++ b/docs/API_docs/constructors/index.md @@ -1,5 +1,5 @@ --- -title: Types +title: Constructors description: List of constructors --- # Constructors diff --git a/docs/index.md b/docs/index.md index c2f3a6e9..7ad2ccc5 100644 --- a/docs/index.md +++ b/docs/index.md @@ -10,7 +10,7 @@ Created by [Daniil Gentili](https://daniil.it), licensed under AGPLv3. MadelineProto logo -Logo created by [Matthew Hesketh](https://matthewhesketh.com) (thanks again!). +Logo created by [Matthew Hesketh](http://matthewhesketh.com) (thanks again!). PHP implementation of MTProto, based on [telepy](https://github.com/griganton/telepy_old). diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index 64bc84c7..1ddc8dac 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -74,6 +74,7 @@ class API extends APIFactory ] ); $this->API->datacenter->authorized = true; + $this->API->get_updates_state(); \danog\MadelineProto\Logger::log('Logged in successfully!'); restore_error_handler(); @@ -123,6 +124,7 @@ class API extends APIFactory ); $this->API->datacenter->waiting_code = false; $this->API->datacenter->authorized = true; + $this->API->get_updates_state(); \danog\MadelineProto\Logger::log('Logged in successfully!'); restore_error_handler(); diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 70856d1d..a7bea56a 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -23,9 +23,11 @@ class MTProto extends PrimeModule use \danog\MadelineProto\MTProtoTools\Crypt; use \danog\MadelineProto\MTProtoTools\MessageHandler; use \danog\MadelineProto\MTProtoTools\MsgIdHandler; + use \danog\MadelineProto\MTProtoTools\PeerHandler; use \danog\MadelineProto\MTProtoTools\ResponseHandler; use \danog\MadelineProto\MTProtoTools\SaltHandler; use \danog\MadelineProto\MTProtoTools\SeqNoHandler; + use \danog\MadelineProto\MTProtoTools\UpdateHandler; public $settings = []; public $config = ['expires' => -1]; @@ -207,6 +209,9 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB $this->setup_logger(); $this->datacenter->__construct($this->settings['connection'], $this->settings['connection_settings']); $this->reset_session(); + if ($this->datacenter->authorized) { + $this->get_updates_state(); + } } public function setup_logger() diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php new file mode 100644 index 00000000..87a1f820 --- /dev/null +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -0,0 +1,31 @@ +. +*/ + +namespace danog\MadelineProto\MTProtoTools; + +/** + * Manages peers + */ +trait PeerHandler +{ + public $chats = []; + public $users = []; + + public function add_users($users) { + var_dump($users); + } + public function add_chats($chats) { + var_dump($chats); + } +} + +?> diff --git a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php index 8f8b1a46..4e681400 100644 --- a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php @@ -207,20 +207,28 @@ trait ResponseHandler default: $this->ack_incoming_message_id($current_msg_id); // Acknowledge that I received the server's response $response_type = $this->tl->constructors->find_by_predicate($response['_'])['type']; - \danog\MadelineProto\Logger::log('Trying to assign a response of type '.$response_type.' to its request...'); - foreach ($this->datacenter->new_outgoing as $key => $expecting) { - \danog\MadelineProto\Logger::log('Does the request of return type '.$expecting['type'].' and msg_id '.$expecting['msg_id'].' match?'); - if ($response_type == $expecting['type']) { - \danog\MadelineProto\Logger::log('Yes'); - $this->datacenter->outgoing_messages[$expecting['msg_id']]['response'] = $current_msg_id; - unset($this->datacenter->new_outgoing[$key]); + switch ($response_type) { + case 'Updates': unset($this->datacenter->new_incoming[$current_msg_id]); + $this->handle_updates($response); + break; + default: + \danog\MadelineProto\Logger::log('Trying to assign a response of type '.$response_type.' to its request...'); + foreach ($this->datacenter->new_outgoing as $key => $expecting) { + \danog\MadelineProto\Logger::log('Does the request of return type '.$expecting['type'].' and msg_id '.$expecting['msg_id'].' match?'); + if ($response_type == $expecting['type']) { + \danog\MadelineProto\Logger::log('Yes'); + $this->datacenter->outgoing_messages[$expecting['msg_id']]['response'] = $current_msg_id; + unset($this->datacenter->new_outgoing[$key]); + unset($this->datacenter->new_incoming[$current_msg_id]); - return; - } - \danog\MadelineProto\Logger::log('No'); + return; + } + \danog\MadelineProto\Logger::log('No'); + } + throw new \danog\MadelineProto\ResponseException('Dunno how to handle '.PHP_EOL.var_export($response, true)); + break; } - throw new \danog\MadelineProto\ResponseException('Dunno how to handle '.PHP_EOL.var_export($response, true)); break; } } diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php new file mode 100644 index 00000000..c69d71b2 --- /dev/null +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -0,0 +1,96 @@ +. +*/ + +namespace danog\MadelineProto\MTProtoTools; + +/** + * Manages updates. + */ +trait UpdateHandler +{ + public $updates_state = []; + + public function update_state($data, $chat_id = 0) + { + if (!isset($this->updates_state[$chat_id])) { + $this->updates_state[$chat_id] = ['date' => 0, 'pts' => 0, 'seq' => 0]; + } + + $this->updates_state[$chat_id]['pts'] = (!isset($data['pts']) || $data['pts'] == 0) ? $this->updates_state[$chat_id]['pts'] : $data['pts']; + $this->updates_state[$chat_id]['seq'] = (!isset($data['seq']) || $data['seq'] == 0) ? $this->updates_state[$chat_id]['seq'] : $data['seq']; + $this->updates_state[$chat_id]['date'] = (!isset($data['date']) || $data['date'] < $this->updates_state[$chat_id]['date']) ? $this->updates_state[$chat_id]['date'] : $data['date']; + + } + public function get_updates_state() { + if (empty($this->updates_state)) { + return $this->update_state($this->method_call('updates.getState')); + } + $difference = $this->method_call('updates.getDifference', ['pts' => $this->updates_state['pts'], 'date' => $this->updates_state['date'], 'qts' => -1]); + switch ($difference['_']) { + case 'updates.differenceEmpty': + $this->update_state($difference); + break; + case 'updates.difference': + $this->update_state($difference['state']); + $this->add_users($difference['users']); + $this->add_chats($difference['chats']); + $this->handle_update_messages($difference['new_messages']); + $this->handle_other_updates($difference['other_updates']); + break; + case 'updates.differenceSlice': + $this->update_state($difference['state']); + $this->add_users($difference['users']); + $this->add_chats($difference['chats']); + $this->handle_update_messages($difference['new_messages']); + $this->handle_other_updates($difference['other_updates']); + $this->get_updates_state(); + break; + default: + throw new \danog\MadelineProto\Exception('Unrecognized update difference received: '.var_export($difference)); + break; + } + } + public function handle_updates($updates) { + switch ($updates['_']) { + case 'updatesTooLong': + $this->get_updates_state(); + break; + case 'updateShortMessage': + case 'updateShortChatMessage': + case 'updateShortSentMessage': + $this->handle_update_messages([$updates]); + break; + case 'updateShort': + $this->handle_other_updates([$updates['update']]); + break; + case 'updatesCombined': + $this->add_users($updates['users']); + $this->add_chats($updates['chats']); + $this->handle_other_updates($updates['updates']); + break; + case 'updates': + $this->add_users($updates['users']); + $this->add_chats($updates['chats']); + $this->handle_other_updates($updates['updates']); + break; + default: + throw new \danog\MadelineProto\Exception('Unrecognized update received: '.var_export($updates)); + break; + } + } + public function handle_other_updates($updates) { + var_dump($updates); + } + public function handle_update_messages($messages) { + var_dump($messages); + } +}