mtproto 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/mtproto/auth_key_generator.rb +16 -6
- data/lib/mtproto/client/rpc.rb +141 -0
- data/lib/mtproto/client.rb +36 -181
- data/lib/mtproto/crypto/aes_ige.rb +1 -1
- data/lib/mtproto/crypto/factorization.rb +1 -1
- data/lib/mtproto/message_id.rb +13 -0
- data/lib/mtproto/rpc/get_config.rb +37 -0
- data/lib/mtproto/rpc/get_contacts.rb +22 -0
- data/lib/mtproto/rpc/get_updates_difference.rb +33 -0
- data/lib/mtproto/rpc/get_updates_state.rb +22 -0
- data/lib/mtproto/rpc/get_users.rb +22 -0
- data/lib/mtproto/rpc/ping.rb +26 -0
- data/lib/mtproto/rpc/send_code.rb +44 -0
- data/lib/mtproto/rpc/send_message.rb +31 -0
- data/lib/mtproto/rpc/sign_in.rb +52 -0
- data/lib/mtproto/tl/auth_key/dh_gen_response.rb +37 -0
- data/lib/mtproto/tl/auth_key/req_dh_params.rb +31 -0
- data/lib/mtproto/tl/auth_key/req_pq_multi.rb +18 -0
- data/lib/mtproto/tl/auth_key/res_pq.rb +62 -0
- data/lib/mtproto/tl/auth_key/server_dh_params.rb +43 -0
- data/lib/mtproto/tl/auth_key/set_client_dh_params.rb +25 -0
- data/lib/mtproto/tl/message.rb +2 -216
- data/lib/mtproto/tl/method_builder.rb +29 -0
- data/lib/mtproto/tl/rpc/auth/authorization.rb +107 -0
- data/lib/mtproto/tl/rpc/auth/send_code.rb +28 -0
- data/lib/mtproto/tl/rpc/auth/sent_code.rb +36 -0
- data/lib/mtproto/tl/rpc/auth/sign_in.rb +32 -0
- data/lib/mtproto/tl/rpc/contacts/contacts.rb +155 -0
- data/lib/mtproto/tl/rpc/contacts/get_contacts.rb +18 -0
- data/lib/mtproto/tl/rpc/help/config.rb +35 -0
- data/lib/mtproto/tl/rpc/help/get_config.rb +17 -0
- data/lib/mtproto/tl/rpc/messages/send_message.rb +43 -0
- data/lib/mtproto/tl/rpc/messages/updates.rb +87 -0
- data/lib/mtproto/tl/rpc/ping.rb +18 -0
- data/lib/mtproto/tl/rpc/pong.rb +46 -0
- data/lib/mtproto/tl/rpc/updates/difference.rb +332 -0
- data/lib/mtproto/tl/rpc/updates/get_difference.rb +42 -0
- data/lib/mtproto/tl/rpc/updates/get_state.rb +17 -0
- data/lib/mtproto/tl/rpc/updates/state.rb +59 -0
- data/lib/mtproto/tl/rpc/users/get_users.rb +25 -0
- data/lib/mtproto/tl/rpc/users/users.rb +99 -0
- data/lib/mtproto/updates_poller.rb +111 -0
- data/lib/mtproto/version.rb +1 -1
- data/lib/mtproto.rb +10 -1
- metadata +53 -5
- data/ext/aes_ige/Makefile +0 -273
- data/ext/factorization/Makefile +0 -273
- data/lib/mtproto/connection.rb +0 -103
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../../gzip_packed'
|
|
4
|
+
require_relative '../../rpc_error'
|
|
5
|
+
|
|
6
|
+
module MTProto
|
|
7
|
+
module TL
|
|
8
|
+
module RPC
|
|
9
|
+
module Auth
|
|
10
|
+
class Authorization
|
|
11
|
+
CONSTRUCTOR = 0x2ea2c0d4
|
|
12
|
+
CONSTRUCTOR_SIGN_UP_REQUIRED = 0x44747e9a
|
|
13
|
+
|
|
14
|
+
def self.parse(response)
|
|
15
|
+
constructor = response[0, 4].unpack1('L<')
|
|
16
|
+
|
|
17
|
+
if constructor == TL::GzipPacked::CONSTRUCTOR
|
|
18
|
+
response = TL::GzipPacked.unpack(response)
|
|
19
|
+
constructor = response[0, 4].unpack1('L<')
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
if constructor == TL::RpcError::CONSTRUCTOR
|
|
23
|
+
error = TL::RpcError.deserialize(response)
|
|
24
|
+
raise MTProto::RpcError.new(error.error_code, error.error_message)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
if constructor == CONSTRUCTOR_SIGN_UP_REQUIRED
|
|
28
|
+
return { authorization: nil, sign_up_required: true }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
if constructor == CONSTRUCTOR
|
|
32
|
+
offset = 4
|
|
33
|
+
|
|
34
|
+
# Parse flags
|
|
35
|
+
flags = response[offset, 4].unpack1('L<')
|
|
36
|
+
offset += 4
|
|
37
|
+
|
|
38
|
+
# tmp_sessions:flags.0?int
|
|
39
|
+
if (flags & (1 << 0)) != 0
|
|
40
|
+
offset += 4
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# otherwise_relogin_days:flags.1?int
|
|
44
|
+
if (flags & (1 << 1)) != 0
|
|
45
|
+
offset += 4
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# future_auth_token:flags.2?bytes
|
|
49
|
+
if (flags & (1 << 2)) != 0
|
|
50
|
+
first_byte = response[offset].unpack1('C')
|
|
51
|
+
|
|
52
|
+
if first_byte < 254
|
|
53
|
+
# Short format: 1 byte length + data + padding
|
|
54
|
+
token_len = first_byte
|
|
55
|
+
offset += 1 + token_len
|
|
56
|
+
padding = (4 - ((1 + token_len) % 4)) % 4
|
|
57
|
+
offset += padding
|
|
58
|
+
else
|
|
59
|
+
# Long format: 0xfe + 3 bytes length + data + padding
|
|
60
|
+
offset += 1
|
|
61
|
+
token_len = response[offset, 3].unpack('CCC').inject(0) {|sum, b| (sum << 8) + b}
|
|
62
|
+
offset += 3 + token_len
|
|
63
|
+
padding = (4 - ((4 + token_len) % 4)) % 4
|
|
64
|
+
offset += padding
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Parse User object
|
|
69
|
+
# user#20b1422 flags:# flags2:# id:long access_hash:flags.0?long ...
|
|
70
|
+
user_constructor = response[offset, 4].unpack1('L<')
|
|
71
|
+
offset += 4
|
|
72
|
+
|
|
73
|
+
user_flags = response[offset, 4].unpack1('L<')
|
|
74
|
+
offset += 4
|
|
75
|
+
|
|
76
|
+
user_flags2 = response[offset, 4].unpack1('L<')
|
|
77
|
+
offset += 4
|
|
78
|
+
|
|
79
|
+
# id:long (always present)
|
|
80
|
+
user_id = response[offset, 8].unpack1('Q<')
|
|
81
|
+
offset += 8
|
|
82
|
+
|
|
83
|
+
# access_hash:flags.0?long (conditional on flags.0)
|
|
84
|
+
access_hash = nil
|
|
85
|
+
if (user_flags & (1 << 0)) != 0
|
|
86
|
+
access_hash = response[offset, 8].unpack1('Q<')
|
|
87
|
+
offset += 8
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# We have what we need, skip the rest of User fields
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
authorization: true,
|
|
94
|
+
flags: flags,
|
|
95
|
+
sign_up_required: false,
|
|
96
|
+
user_id: user_id,
|
|
97
|
+
access_hash: access_hash
|
|
98
|
+
}
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
raise UnexpectedConstructorError.new(constructor)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../../serializer'
|
|
4
|
+
require_relative '../../code_settings'
|
|
5
|
+
|
|
6
|
+
module MTProto
|
|
7
|
+
module TL
|
|
8
|
+
module RPC
|
|
9
|
+
module Auth
|
|
10
|
+
class SendCode
|
|
11
|
+
CONSTRUCTOR = 0xa677244f
|
|
12
|
+
|
|
13
|
+
def self.build(phone_number:, api_id:, api_hash:, code_settings: {})
|
|
14
|
+
raise ArgumentError, 'phone_number is required' if phone_number.nil? || phone_number.empty?
|
|
15
|
+
|
|
16
|
+
query = [CONSTRUCTOR].pack('L<')
|
|
17
|
+
query += Serializer.serialize_string(phone_number)
|
|
18
|
+
query += Serializer.serialize_int(api_id)
|
|
19
|
+
query += Serializer.serialize_string(api_hash)
|
|
20
|
+
query += CodeSettings.serialize(code_settings)
|
|
21
|
+
|
|
22
|
+
query
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../../gzip_packed'
|
|
4
|
+
require_relative '../../rpc_error'
|
|
5
|
+
require_relative '../../sent_code'
|
|
6
|
+
|
|
7
|
+
module MTProto
|
|
8
|
+
module TL
|
|
9
|
+
module RPC
|
|
10
|
+
module Auth
|
|
11
|
+
class SentCode
|
|
12
|
+
def self.parse(response)
|
|
13
|
+
constructor = response[0, 4].unpack1('L<')
|
|
14
|
+
|
|
15
|
+
if constructor == TL::GzipPacked::CONSTRUCTOR
|
|
16
|
+
response = TL::GzipPacked.unpack(response)
|
|
17
|
+
constructor = response[0, 4].unpack1('L<')
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
if constructor == TL::RpcError::CONSTRUCTOR
|
|
21
|
+
error = TL::RpcError.deserialize(response)
|
|
22
|
+
raise MTProto::RpcError.new(error.error_code, error.error_message)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
if constructor == TL::SentCode::CONSTRUCTOR
|
|
26
|
+
sent_code = TL::SentCode.deserialize(response)
|
|
27
|
+
sent_code.to_h
|
|
28
|
+
else
|
|
29
|
+
raise UnexpectedConstructorError.new(constructor)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../../serializer'
|
|
4
|
+
|
|
5
|
+
module MTProto
|
|
6
|
+
module TL
|
|
7
|
+
module RPC
|
|
8
|
+
module Auth
|
|
9
|
+
class SignIn
|
|
10
|
+
CONSTRUCTOR = 0x8d52a951
|
|
11
|
+
|
|
12
|
+
def self.build(phone_number:, phone_code_hash:, phone_code:)
|
|
13
|
+
raise ArgumentError, 'phone_number is required' if phone_number.nil? || phone_number.empty?
|
|
14
|
+
raise ArgumentError, 'phone_code_hash is required' if phone_code_hash.nil? || phone_code_hash.empty?
|
|
15
|
+
raise ArgumentError, 'phone_code is required' if phone_code.nil? || phone_code.empty?
|
|
16
|
+
|
|
17
|
+
flags = 0
|
|
18
|
+
flags |= (1 << 0) # phone_code present
|
|
19
|
+
|
|
20
|
+
query = [CONSTRUCTOR].pack('L<')
|
|
21
|
+
query += Serializer.serialize_int(flags)
|
|
22
|
+
query += Serializer.serialize_string(phone_number)
|
|
23
|
+
query += Serializer.serialize_string(phone_code_hash)
|
|
24
|
+
query += Serializer.serialize_string(phone_code)
|
|
25
|
+
|
|
26
|
+
query
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../../gzip_packed'
|
|
4
|
+
require_relative '../../rpc_error'
|
|
5
|
+
|
|
6
|
+
module MTProto
|
|
7
|
+
module TL
|
|
8
|
+
module RPC
|
|
9
|
+
module Contacts
|
|
10
|
+
class Contacts
|
|
11
|
+
CONSTRUCTOR_CONTACTS = 0xeae87e42
|
|
12
|
+
CONSTRUCTOR_CONTACTS_NOT_MODIFIED = 0xb74ba9d2
|
|
13
|
+
VECTOR_CONSTRUCTOR = 0x1cb5c415
|
|
14
|
+
|
|
15
|
+
def self.parse(response)
|
|
16
|
+
offset = 0
|
|
17
|
+
|
|
18
|
+
constructor = response[offset, 4].unpack1('L<')
|
|
19
|
+
offset += 4
|
|
20
|
+
|
|
21
|
+
if constructor == TL::GzipPacked::CONSTRUCTOR
|
|
22
|
+
response = TL::GzipPacked.unpack(response)
|
|
23
|
+
constructor = response[0, 4].unpack1('L<')
|
|
24
|
+
offset = 4
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
if constructor == TL::RpcError::CONSTRUCTOR
|
|
28
|
+
error = TL::RpcError.deserialize(response)
|
|
29
|
+
raise MTProto::RpcError.new(error.error_code, error.error_message)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
case constructor
|
|
33
|
+
when CONSTRUCTOR_CONTACTS_NOT_MODIFIED
|
|
34
|
+
# contacts.contactsNotModified#b74ba9d2 = contacts.Contacts;
|
|
35
|
+
{
|
|
36
|
+
contacts: [],
|
|
37
|
+
users: []
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
when CONSTRUCTOR_CONTACTS
|
|
41
|
+
# contacts.contacts#eae87e42 contacts:Vector<Contact> saved_count:int users:Vector<User> = contacts.Contacts;
|
|
42
|
+
|
|
43
|
+
# Parse contacts vector
|
|
44
|
+
contacts, offset = parse_contacts_vector(response, offset)
|
|
45
|
+
|
|
46
|
+
# Parse saved_count
|
|
47
|
+
saved_count = response[offset, 4].unpack1('L<')
|
|
48
|
+
offset += 4
|
|
49
|
+
|
|
50
|
+
# Parse users vector
|
|
51
|
+
users, offset = parse_users_vector(response, offset)
|
|
52
|
+
|
|
53
|
+
{
|
|
54
|
+
contacts: contacts,
|
|
55
|
+
saved_count: saved_count,
|
|
56
|
+
users: users
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
else
|
|
60
|
+
raise "Unknown Contacts constructor: 0x#{constructor.to_s(16)}"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def self.parse_contacts_vector(response, offset)
|
|
65
|
+
vector_constructor = response[offset, 4].unpack1('L<')
|
|
66
|
+
offset += 4
|
|
67
|
+
|
|
68
|
+
return [[], offset] unless vector_constructor == VECTOR_CONSTRUCTOR
|
|
69
|
+
|
|
70
|
+
count = response[offset, 4].unpack1('L<')
|
|
71
|
+
offset += 4
|
|
72
|
+
|
|
73
|
+
contacts = []
|
|
74
|
+
count.times do
|
|
75
|
+
contact, offset = parse_contact(response, offset)
|
|
76
|
+
contacts << contact if contact
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
[contacts, offset]
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def self.parse_contact(response, offset)
|
|
83
|
+
# contact#145ade0b user_id:long mutual:Bool
|
|
84
|
+
constructor = response[offset, 4].unpack1('L<')
|
|
85
|
+
offset += 4
|
|
86
|
+
|
|
87
|
+
return [nil, offset] unless constructor == 0x145ade0b
|
|
88
|
+
|
|
89
|
+
user_id = response[offset, 8].unpack1('Q<')
|
|
90
|
+
offset += 8
|
|
91
|
+
|
|
92
|
+
mutual_constructor = response[offset, 4].unpack1('L<')
|
|
93
|
+
offset += 4
|
|
94
|
+
mutual = mutual_constructor == 0x997275b5 # boolTrue
|
|
95
|
+
|
|
96
|
+
[{
|
|
97
|
+
user_id: user_id,
|
|
98
|
+
mutual: mutual
|
|
99
|
+
}, offset]
|
|
100
|
+
rescue StandardError
|
|
101
|
+
[nil, offset]
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def self.parse_users_vector(response, offset)
|
|
105
|
+
vector_constructor = response[offset, 4].unpack1('L<')
|
|
106
|
+
offset += 4
|
|
107
|
+
|
|
108
|
+
return [[], offset] unless vector_constructor == VECTOR_CONSTRUCTOR
|
|
109
|
+
|
|
110
|
+
count = response[offset, 4].unpack1('L<')
|
|
111
|
+
offset += 4
|
|
112
|
+
|
|
113
|
+
users = []
|
|
114
|
+
count.times do
|
|
115
|
+
user, offset = parse_user(response, offset)
|
|
116
|
+
users << user if user
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
[users, offset]
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def self.parse_user(response, offset)
|
|
123
|
+
user_constructor = response[offset, 4].unpack1('L<')
|
|
124
|
+
offset += 4
|
|
125
|
+
|
|
126
|
+
# user#20b1422 flags:# flags2:# id:long access_hash:flags.0?long ...
|
|
127
|
+
return [nil, offset] unless user_constructor == 0x20b1422
|
|
128
|
+
|
|
129
|
+
flags = response[offset, 4].unpack1('L<')
|
|
130
|
+
offset += 4
|
|
131
|
+
|
|
132
|
+
flags2 = response[offset, 4].unpack1('L<')
|
|
133
|
+
offset += 4
|
|
134
|
+
|
|
135
|
+
user_id = response[offset, 8].unpack1('Q<')
|
|
136
|
+
offset += 8
|
|
137
|
+
|
|
138
|
+
access_hash = nil
|
|
139
|
+
if (flags & (1 << 0)) != 0
|
|
140
|
+
access_hash = response[offset, 8].unpack1('Q<')
|
|
141
|
+
offset += 8
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
[{
|
|
145
|
+
id: user_id,
|
|
146
|
+
access_hash: access_hash
|
|
147
|
+
}, offset]
|
|
148
|
+
rescue StandardError
|
|
149
|
+
[nil, offset]
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MTProto
|
|
4
|
+
module TL
|
|
5
|
+
module RPC
|
|
6
|
+
module Contacts
|
|
7
|
+
class GetContacts
|
|
8
|
+
def self.build(hash: 0)
|
|
9
|
+
# contacts.getContacts#22c6aa08 hash:int = contacts.Contacts;
|
|
10
|
+
data = [0x22c6aa08].pack('L<')
|
|
11
|
+
data += [hash].pack('l<')
|
|
12
|
+
data
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../../gzip_packed'
|
|
4
|
+
require_relative '../../rpc_error'
|
|
5
|
+
require_relative '../../config'
|
|
6
|
+
|
|
7
|
+
module MTProto
|
|
8
|
+
module TL
|
|
9
|
+
module RPC
|
|
10
|
+
module Help
|
|
11
|
+
class Config
|
|
12
|
+
def self.parse(response)
|
|
13
|
+
constructor = response[0, 4].unpack1('L<')
|
|
14
|
+
|
|
15
|
+
if constructor == TL::GzipPacked::CONSTRUCTOR
|
|
16
|
+
response = TL::GzipPacked.unpack(response)
|
|
17
|
+
constructor = response[0, 4].unpack1('L<')
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
if constructor == TL::RpcError::CONSTRUCTOR
|
|
21
|
+
error = TL::RpcError.deserialize(response)
|
|
22
|
+
raise MTProto::RpcError.new(error.error_code, error.error_message)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
if constructor == TL::Config::CONSTRUCTOR || constructor == TL::Config::CONSTRUCTOR_ALT
|
|
26
|
+
TL::Config.deserialize(response)
|
|
27
|
+
else
|
|
28
|
+
raise UnexpectedConstructorError.new(constructor)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../../serializer'
|
|
4
|
+
|
|
5
|
+
module MTProto
|
|
6
|
+
module TL
|
|
7
|
+
module RPC
|
|
8
|
+
module Messages
|
|
9
|
+
class SendMessage
|
|
10
|
+
CONSTRUCTOR = 0xfe05dc9a
|
|
11
|
+
INPUT_PEER_USER = 0xdde8a54c
|
|
12
|
+
|
|
13
|
+
def self.build(user_id:, access_hash:, message:, random_id: nil)
|
|
14
|
+
raise ArgumentError, 'user_id is required' if user_id.nil?
|
|
15
|
+
raise ArgumentError, 'access_hash is required' if access_hash.nil?
|
|
16
|
+
raise ArgumentError, 'message is required and cannot be empty' if message.nil? || message.empty?
|
|
17
|
+
|
|
18
|
+
random_id ||= SecureRandom.random_number(2**63)
|
|
19
|
+
|
|
20
|
+
query = [CONSTRUCTOR].pack('L<')
|
|
21
|
+
|
|
22
|
+
# flags:# (no optional fields for now)
|
|
23
|
+
flags = 0
|
|
24
|
+
query += Serializer.serialize_int(flags)
|
|
25
|
+
|
|
26
|
+
# peer:InputPeer (inputPeerUser)
|
|
27
|
+
query += [INPUT_PEER_USER].pack('L<')
|
|
28
|
+
query += Serializer.serialize_long(user_id)
|
|
29
|
+
query += Serializer.serialize_long(access_hash)
|
|
30
|
+
|
|
31
|
+
# message:string
|
|
32
|
+
query += Serializer.serialize_string(message)
|
|
33
|
+
|
|
34
|
+
# random_id:long
|
|
35
|
+
query += Serializer.serialize_long(random_id)
|
|
36
|
+
|
|
37
|
+
query
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../../gzip_packed'
|
|
4
|
+
require_relative '../../rpc_error'
|
|
5
|
+
|
|
6
|
+
module MTProto
|
|
7
|
+
module TL
|
|
8
|
+
module RPC
|
|
9
|
+
module Messages
|
|
10
|
+
class Updates
|
|
11
|
+
CONSTRUCTOR_UPDATES = 0x74ae4240
|
|
12
|
+
CONSTRUCTOR_UPDATE_SHORT = 0x78d4dec1
|
|
13
|
+
CONSTRUCTOR_UPDATE_SHORT_MESSAGE = 0x313bc7f8
|
|
14
|
+
CONSTRUCTOR_UPDATE_SHORT_CHAT_MESSAGE = 0x4d6deea5
|
|
15
|
+
CONSTRUCTOR_UPDATE_SHORT_SENT_MESSAGE = 0x9015e101
|
|
16
|
+
|
|
17
|
+
def self.parse(response)
|
|
18
|
+
offset = 0
|
|
19
|
+
|
|
20
|
+
constructor = response[offset, 4].unpack1('L<')
|
|
21
|
+
offset += 4
|
|
22
|
+
|
|
23
|
+
if constructor == TL::GzipPacked::CONSTRUCTOR
|
|
24
|
+
response = TL::GzipPacked.unpack(response)
|
|
25
|
+
constructor = response[0, 4].unpack1('L<')
|
|
26
|
+
offset = 4
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
if constructor == TL::RpcError::CONSTRUCTOR
|
|
30
|
+
error = TL::RpcError.deserialize(response)
|
|
31
|
+
raise MTProto::RpcError.new(error.error_code, error.error_message)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
case constructor
|
|
35
|
+
when CONSTRUCTOR_UPDATE_SHORT_SENT_MESSAGE
|
|
36
|
+
# updateShortSentMessage#11f1331c flags:# out:flags.1?true id:int pts:int pts_count:int date:int media:flags.9?MessageMedia entities:flags.7?Vector<MessageEntity> ttl_period:flags.25?int = Updates;
|
|
37
|
+
flags = response[offset, 4].unpack1('L<')
|
|
38
|
+
offset += 4
|
|
39
|
+
|
|
40
|
+
message_id = response[offset, 4].unpack1('L<')
|
|
41
|
+
offset += 4
|
|
42
|
+
|
|
43
|
+
pts = response[offset, 4].unpack1('L<')
|
|
44
|
+
offset += 4
|
|
45
|
+
|
|
46
|
+
pts_count = response[offset, 4].unpack1('L<')
|
|
47
|
+
offset += 4
|
|
48
|
+
|
|
49
|
+
date = response[offset, 4].unpack1('L<')
|
|
50
|
+
offset += 4
|
|
51
|
+
|
|
52
|
+
{
|
|
53
|
+
sent: true,
|
|
54
|
+
message_id: message_id,
|
|
55
|
+
date: date,
|
|
56
|
+
pts: pts,
|
|
57
|
+
pts_count: pts_count
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
when CONSTRUCTOR_UPDATE_SHORT_MESSAGE, CONSTRUCTOR_UPDATE_SHORT_CHAT_MESSAGE
|
|
61
|
+
# updateShortMessage#2296d2c8 or updateShortChatMessage#402d5dbb
|
|
62
|
+
# Both have similar structure with flags, id, etc.
|
|
63
|
+
flags = response[offset, 4].unpack1('L<')
|
|
64
|
+
offset += 4
|
|
65
|
+
|
|
66
|
+
message_id = response[offset, 4].unpack1('L<')
|
|
67
|
+
|
|
68
|
+
{
|
|
69
|
+
sent: true,
|
|
70
|
+
message_id: message_id
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
when CONSTRUCTOR_UPDATES, CONSTRUCTOR_UPDATE_SHORT
|
|
74
|
+
# For full updates or updateShort, just confirm success
|
|
75
|
+
{
|
|
76
|
+
sent: true
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
else
|
|
80
|
+
raise "Unknown Updates constructor: 0x#{constructor.to_s(16)}"
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../serializer'
|
|
4
|
+
|
|
5
|
+
module MTProto
|
|
6
|
+
module TL
|
|
7
|
+
module RPC
|
|
8
|
+
class Ping
|
|
9
|
+
CONSTRUCTOR = 0x7abe77ec
|
|
10
|
+
|
|
11
|
+
def self.build(ping_id)
|
|
12
|
+
body = Serializer.serialize_int(CONSTRUCTOR)
|
|
13
|
+
body + [ping_id].pack('Q<')
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MTProto
|
|
4
|
+
module TL
|
|
5
|
+
module RPC
|
|
6
|
+
class Pong
|
|
7
|
+
CONSTRUCTOR = 0x347773c5
|
|
8
|
+
CONSTRUCTOR_BAD_MSG_NOTIFICATION = 0xa7eff811
|
|
9
|
+
|
|
10
|
+
def self.parse(body)
|
|
11
|
+
constructor = body[0, 4].unpack1('L<')
|
|
12
|
+
|
|
13
|
+
if constructor == CONSTRUCTOR_BAD_MSG_NOTIFICATION
|
|
14
|
+
bad_msg = BadMsgNotification.deserialize(body)
|
|
15
|
+
raise "Bad message notification: #{bad_msg.error_message} (code: #{bad_msg.error_code}, msg_id: #{bad_msg.bad_msg_id}, seqno: #{bad_msg.bad_msg_seqno})"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
if constructor == MsgContainer::CONSTRUCTOR
|
|
19
|
+
container = MsgContainer.deserialize(body)
|
|
20
|
+
pong_message = container.messages.find do |msg|
|
|
21
|
+
msg[:body][0, 4].unpack1('L<') == CONSTRUCTOR
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
raise 'No pong message found in container' unless pong_message
|
|
25
|
+
|
|
26
|
+
offset = 4
|
|
27
|
+
msg_id = pong_message[:body][offset, 8].unpack1('Q<')
|
|
28
|
+
offset += 8
|
|
29
|
+
ping_id = pong_message[:body][offset, 8].unpack1('Q<')
|
|
30
|
+
|
|
31
|
+
return { msg_id: msg_id, ping_id: ping_id }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
raise "Unexpected constructor: 0x#{constructor.to_s(16)}" unless constructor == CONSTRUCTOR
|
|
35
|
+
|
|
36
|
+
offset = 4
|
|
37
|
+
msg_id = body[offset, 8].unpack1('Q<')
|
|
38
|
+
offset += 8
|
|
39
|
+
ping_id = body[offset, 8].unpack1('Q<')
|
|
40
|
+
|
|
41
|
+
{ msg_id: msg_id, ping_id: ping_id }
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|