mtproto 0.0.7 → 0.0.9

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.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.env.example +4 -0
  3. data/ext/aes_ige/extconf.rb +3 -9
  4. data/ext/factorization/extconf.rb +2 -0
  5. data/lib/mtproto/auth_key_generator.rb +66 -103
  6. data/lib/mtproto/binary.rb +21 -0
  7. data/lib/mtproto/client/api/check_password.rb +41 -0
  8. data/lib/mtproto/client/api/export_login_token.rb +27 -0
  9. data/lib/mtproto/client/api/get_updates_difference.rb +21 -0
  10. data/lib/mtproto/client/api/get_updates_state.rb +14 -0
  11. data/lib/mtproto/client/api/get_users.rb +14 -0
  12. data/lib/mtproto/client/api/import_login_token.rb +23 -0
  13. data/lib/mtproto/client/api/send_code.rb +21 -0
  14. data/lib/mtproto/client/api/sign_in.rb +27 -0
  15. data/lib/mtproto/client/api.rb +28 -0
  16. data/lib/mtproto/client/rpc/response.rb +63 -0
  17. data/lib/mtproto/client/rpc.rb +67 -107
  18. data/lib/mtproto/client.rb +176 -31
  19. data/lib/mtproto/crypto/dh_key_exchange.rb +1 -2
  20. data/lib/mtproto/crypto/dh_validator.rb +17 -19
  21. data/lib/mtproto/crypto/factorization.rb +1 -1
  22. data/lib/mtproto/crypto/rsa_key.rb +2 -2
  23. data/lib/mtproto/crypto/srp.rb +117 -0
  24. data/lib/mtproto/delegate_methods.rb +11 -0
  25. data/lib/mtproto/message/message.rb +85 -0
  26. data/lib/mtproto/session.rb +1 -1
  27. data/lib/mtproto/tl/constructors.rb +2269 -0
  28. data/lib/mtproto/tl/object.rb +25 -0
  29. data/lib/mtproto/tl/objects/account_password.rb +72 -0
  30. data/lib/mtproto/tl/objects/authorization.rb +73 -0
  31. data/lib/mtproto/tl/objects/check_password.rb +46 -0
  32. data/lib/mtproto/tl/objects/client_dh_inner_data.rb +47 -0
  33. data/lib/mtproto/tl/objects/dh_gen_response.rb +50 -0
  34. data/lib/mtproto/tl/objects/export_login_token.rb +51 -0
  35. data/lib/mtproto/tl/objects/get_config.rb +15 -0
  36. data/lib/mtproto/tl/objects/get_difference.rb +36 -0
  37. data/lib/mtproto/tl/objects/get_password.rb +15 -0
  38. data/lib/mtproto/tl/objects/get_state.rb +15 -0
  39. data/lib/mtproto/tl/objects/get_users.rb +20 -0
  40. data/lib/mtproto/tl/objects/help_config.rb +77 -0
  41. data/lib/mtproto/tl/objects/import_login_token.rb +39 -0
  42. data/lib/mtproto/tl/objects/init_connection.rb +59 -0
  43. data/lib/mtproto/tl/objects/invoke_with_layer.rb +22 -0
  44. data/lib/mtproto/tl/objects/login_token.rb +82 -0
  45. data/lib/mtproto/tl/objects/pq_inner_data.rb +69 -0
  46. data/lib/mtproto/tl/objects/req_dh_params.rb +65 -0
  47. data/lib/mtproto/tl/objects/req_pq_multi.rb +23 -0
  48. data/lib/mtproto/tl/objects/res_pq.rb +75 -0
  49. data/lib/mtproto/tl/objects/send_code.rb +50 -0
  50. data/lib/mtproto/tl/objects/sent_code.rb +79 -0
  51. data/lib/mtproto/tl/objects/server_dh_inner_data.rb +74 -0
  52. data/lib/mtproto/tl/objects/server_dh_params.rb +53 -0
  53. data/lib/mtproto/tl/objects/set_client_dh_params.rb +48 -0
  54. data/lib/mtproto/tl/objects/sign_in.rb +47 -0
  55. data/lib/mtproto/tl/objects/update.rb +80 -0
  56. data/lib/mtproto/tl/objects/update_short.rb +22 -0
  57. data/lib/mtproto/tl/objects/update_short_message.rb +67 -0
  58. data/lib/mtproto/tl/objects/updates_difference.rb +157 -0
  59. data/lib/mtproto/tl/objects/updates_state.rb +37 -0
  60. data/lib/mtproto/tl/objects/users.rb +86 -0
  61. data/lib/mtproto/transport/abridged_packet_codec.rb +35 -12
  62. data/lib/mtproto/transport/connection.rb +23 -0
  63. data/lib/mtproto/transport/errors.rb +11 -0
  64. data/lib/mtproto/transport/packet.rb +19 -0
  65. data/lib/mtproto/transport/tcp_connection.rb +57 -46
  66. data/lib/mtproto/{tl → type}/bad_msg_notification.rb +11 -11
  67. data/lib/mtproto/{tl → type}/client_dh_inner_data.rb +1 -1
  68. data/lib/mtproto/{tl → type}/gzip_packed.rb +6 -4
  69. data/lib/mtproto/{tl → type}/message.rb +3 -3
  70. data/lib/mtproto/{tl → type}/msg_container.rb +1 -1
  71. data/lib/mtproto/{tl → type}/new_session_created.rb +1 -1
  72. data/lib/mtproto/{tl/p_q_inner_data.rb → type/pq_inner_data.rb} +1 -1
  73. data/lib/mtproto/{tl → type}/rpc_error.rb +1 -2
  74. data/lib/mtproto/{tl → type}/serializer.rb +1 -1
  75. data/lib/mtproto/{tl → type}/server_dh_inner_data.rb +1 -1
  76. data/lib/mtproto/updates_poller.rb +37 -33
  77. data/lib/mtproto/version.rb +1 -1
  78. data/lib/mtproto.rb +21 -22
  79. data/scripts/generate_constructors.rb +65 -0
  80. metadata +80 -49
  81. data/lib/mtproto/rpc/get_config.rb +0 -37
  82. data/lib/mtproto/rpc/get_contacts.rb +0 -22
  83. data/lib/mtproto/rpc/get_updates_difference.rb +0 -33
  84. data/lib/mtproto/rpc/get_updates_state.rb +0 -22
  85. data/lib/mtproto/rpc/get_users.rb +0 -22
  86. data/lib/mtproto/rpc/ping.rb +0 -26
  87. data/lib/mtproto/rpc/send_code.rb +0 -44
  88. data/lib/mtproto/rpc/send_message.rb +0 -31
  89. data/lib/mtproto/rpc/sign_in.rb +0 -52
  90. data/lib/mtproto/tl/auth_key/dh_gen_response.rb +0 -37
  91. data/lib/mtproto/tl/auth_key/req_dh_params.rb +0 -31
  92. data/lib/mtproto/tl/auth_key/req_pq_multi.rb +0 -18
  93. data/lib/mtproto/tl/auth_key/res_pq.rb +0 -62
  94. data/lib/mtproto/tl/auth_key/server_dh_params.rb +0 -43
  95. data/lib/mtproto/tl/auth_key/set_client_dh_params.rb +0 -25
  96. data/lib/mtproto/tl/code_settings.rb +0 -25
  97. data/lib/mtproto/tl/config.rb +0 -124
  98. data/lib/mtproto/tl/method_builder.rb +0 -29
  99. data/lib/mtproto/tl/rpc/auth/authorization.rb +0 -107
  100. data/lib/mtproto/tl/rpc/auth/send_code.rb +0 -28
  101. data/lib/mtproto/tl/rpc/auth/sent_code.rb +0 -36
  102. data/lib/mtproto/tl/rpc/auth/sign_in.rb +0 -32
  103. data/lib/mtproto/tl/rpc/contacts/contacts.rb +0 -155
  104. data/lib/mtproto/tl/rpc/contacts/get_contacts.rb +0 -18
  105. data/lib/mtproto/tl/rpc/help/config.rb +0 -35
  106. data/lib/mtproto/tl/rpc/help/get_config.rb +0 -17
  107. data/lib/mtproto/tl/rpc/messages/send_message.rb +0 -43
  108. data/lib/mtproto/tl/rpc/messages/updates.rb +0 -87
  109. data/lib/mtproto/tl/rpc/ping.rb +0 -18
  110. data/lib/mtproto/tl/rpc/pong.rb +0 -46
  111. data/lib/mtproto/tl/rpc/updates/difference.rb +0 -332
  112. data/lib/mtproto/tl/rpc/updates/get_difference.rb +0 -42
  113. data/lib/mtproto/tl/rpc/updates/get_state.rb +0 -17
  114. data/lib/mtproto/tl/rpc/updates/state.rb +0 -59
  115. data/lib/mtproto/tl/rpc/users/get_users.rb +0 -25
  116. data/lib/mtproto/tl/rpc/users/users.rb +0 -99
  117. data/lib/mtproto/tl/sent_code.rb +0 -128
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../serializer'
4
-
5
- module MTProto
6
- module TL
7
- module AuthKey
8
- class SetClientDHParams
9
- CONSTRUCTOR = 0xf5045f1f
10
-
11
- def self.build(nonce:, server_nonce:, encrypted_data:)
12
- raise ArgumentError, 'Nonce must be 16 bytes' unless nonce.bytesize == 16
13
- raise ArgumentError, 'Server nonce must be 16 bytes' unless server_nonce.bytesize == 16
14
-
15
- body = Serializer.serialize_int(CONSTRUCTOR)
16
- body += nonce
17
- body += server_nonce
18
- body += Serializer.serialize_bytes(encrypted_data)
19
-
20
- body
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MTProto
4
- module TL
5
- class CodeSettings
6
- CONSTRUCTOR = 0xad253d78
7
-
8
- def self.serialize(settings = {})
9
- body = Serializer.serialize_int(CONSTRUCTOR)
10
-
11
- flags = 0
12
- flags |= (1 << 0) if settings[:allow_flashcall]
13
- flags |= (1 << 1) if settings[:current_number]
14
- flags |= (1 << 4) if settings[:allow_app_hash]
15
- flags |= (1 << 5) if settings[:allow_missed_call]
16
- flags |= (1 << 7) if settings[:allow_firebase]
17
- flags |= (1 << 9) if settings[:unknown_number]
18
-
19
- body += Serializer.serialize_int(flags)
20
-
21
- body
22
- end
23
- end
24
- end
25
- end
@@ -1,124 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MTProto
4
- module TL
5
- class Config
6
- CONSTRUCTOR = 0xcc1a241e
7
- CONSTRUCTOR_ALT = 0x3072cfa1
8
-
9
- attr_reader :flags, :date, :expires, :test_mode, :this_dc, :dc_options
10
-
11
- def self.deserialize(data)
12
- offset = 4
13
-
14
- flags = data[offset, 4].unpack1('L<')
15
- offset += 4
16
-
17
- date = data[offset, 4].unpack1('L<')
18
- offset += 4
19
-
20
- expires = data[offset, 4].unpack1('L<')
21
- offset += 4
22
-
23
- test_mode = (data[offset, 4].unpack1('L<') == 0x997275b5)
24
- offset += 4
25
-
26
- this_dc = data[offset, 4].unpack1('L<')
27
- offset += 4
28
-
29
- dc_options_constructor = data[offset, 4].unpack1('L<')
30
- offset += 4
31
-
32
- raise "Expected vector constructor 0x1cb5c415, got 0x#{dc_options_constructor.to_s(16)}" unless dc_options_constructor == 0x1cb5c415
33
-
34
- dc_options_count = data[offset, 4].unpack1('L<')
35
- offset += 4
36
-
37
- dc_options = []
38
- dc_options_count.times do
39
- dc_option, bytes_read = DcOption.deserialize_from(data[offset..])
40
- dc_options << dc_option
41
- offset += bytes_read
42
- end
43
-
44
- new(
45
- flags: flags,
46
- date: date,
47
- expires: expires,
48
- test_mode: test_mode,
49
- this_dc: this_dc,
50
- dc_options: dc_options
51
- )
52
- end
53
-
54
- def initialize(flags:, date:, expires:, test_mode:, this_dc:, dc_options:)
55
- @flags = flags
56
- @date = date
57
- @expires = expires
58
- @test_mode = test_mode
59
- @this_dc = this_dc
60
- @dc_options = dc_options
61
- end
62
- end
63
-
64
- class DcOption
65
- attr_reader :id, :ip_address, :port, :flags
66
-
67
- def self.deserialize_from(data)
68
- offset = 0
69
-
70
- constructor = data[offset, 4].unpack1('L<')
71
- offset += 4
72
-
73
- raise "Expected dcOption constructor 0x18b7a10d, got 0x#{constructor.to_s(16)}" unless constructor == 0x18b7a10d
74
-
75
- flags = data[offset, 4].unpack1('L<')
76
- offset += 4
77
-
78
- id = data[offset, 4].unpack1('L<')
79
- offset += 4
80
-
81
- ip_length = data[offset].ord
82
- offset += 1
83
-
84
- ip_address = data[offset, ip_length]
85
- offset += ip_length
86
-
87
- padding = (4 - ((ip_length + 1) % 4)) % 4
88
- offset += padding
89
-
90
- port = data[offset, 4].unpack1('L<')
91
- offset += 4
92
-
93
- [new(id: id, ip_address: ip_address, port: port, flags: flags), offset]
94
- end
95
-
96
- def initialize(id:, ip_address:, port:, flags:)
97
- @id = id
98
- @ip_address = ip_address
99
- @port = port
100
- @flags = flags
101
- end
102
-
103
- def ipv6?
104
- (@flags & 1) != 0
105
- end
106
-
107
- def media_only?
108
- (@flags & 2) != 0
109
- end
110
-
111
- def tcpo_only?
112
- (@flags & 4) != 0
113
- end
114
-
115
- def cdn?
116
- (@flags & 8) != 0
117
- end
118
-
119
- def static?
120
- (@flags & 16) != 0
121
- end
122
- end
123
- end
124
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'serializer'
4
-
5
- module MTProto
6
- module TL
7
- module MethodBuilder
8
- def invoke_with_layer(layer, query)
9
- body = Serializer.serialize_int(0xda9b0d0d)
10
- body += Serializer.serialize_int(layer)
11
- body + query
12
- end
13
-
14
- def init_connection(api_id:, device_model:, system_version:, app_version:, system_lang_code:, lang_pack:, lang_code:, query:)
15
- body = Serializer.serialize_int(0xc1cd5ea9)
16
- flags = 0
17
- body += Serializer.serialize_int(flags)
18
- body += Serializer.serialize_int(api_id)
19
- body += Serializer.serialize_string(device_model)
20
- body += Serializer.serialize_string(system_version)
21
- body += Serializer.serialize_string(app_version)
22
- body += Serializer.serialize_string(system_lang_code)
23
- body += Serializer.serialize_string(lang_pack)
24
- body += Serializer.serialize_string(lang_code)
25
- body + query
26
- end
27
- end
28
- end
29
- end
@@ -1,107 +0,0 @@
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
@@ -1,28 +0,0 @@
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
@@ -1,36 +0,0 @@
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
@@ -1,32 +0,0 @@
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
@@ -1,155 +0,0 @@
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
@@ -1,18 +0,0 @@
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
@@ -1,35 +0,0 @@
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
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MTProto
4
- module TL
5
- module RPC
6
- module Help
7
- class GetConfig
8
- CONSTRUCTOR = 0xc4f9186b
9
-
10
- def self.build
11
- [CONSTRUCTOR].pack('L<')
12
- end
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,43 +0,0 @@
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