Started writing update handlers

This commit is contained in:
Daniil Gentili 2016-12-23 12:19:13 +01:00
parent c42fa19afd
commit cfc7cb4e04
9 changed files with 157 additions and 15 deletions

View File

@ -6,7 +6,7 @@ Created by [Daniil Gentili](https://daniil.it), licensed under AGPLv3.
<img src='https://daniil.it/MadelineProto/logo.png' alt='MadelineProto logo' onmouseover="this.src='https://daniil.it/MadelineProto/logo-hover.png';" onmouseout="this.src='https://daniil.it/MadelineProto/logo.png';" />
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).

View File

@ -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

View File

@ -1,5 +1,5 @@
---
title: Types
title: Constructors
description: List of constructors
---
# Constructors

View File

@ -10,7 +10,7 @@ Created by [Daniil Gentili](https://daniil.it), licensed under AGPLv3.
<img src='https://daniil.it/MadelineProto/logo.png' alt='MadelineProto logo' onmouseover="this.src='https://daniil.it/MadelineProto/logo-hover.png';" onmouseout="this.src='https://daniil.it/MadelineProto/logo.png';" />
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).

View File

@ -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();

View File

@ -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()

View File

@ -0,0 +1,31 @@
<?php
/*
Copyright 2016 Daniil Gentili
(https://daniil.it)
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/>.
*/
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);
}
}
?>

View File

@ -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;
}
}

View File

@ -0,0 +1,96 @@
<?php
/*
Copyright 2016 Daniil Gentili
(https://daniil.it)
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/>.
*/
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);
}
}