telethon 0.1.0
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 +7 -0
- data/CHANGELOG.md +30 -0
- data/LICENSE +48 -0
- data/README.md +84 -0
- data/data/api.tl +3336 -0
- data/data/mtproto.tl +116 -0
- data/exe/tg +6 -0
- data/lib/telethon/assertions.rb +30 -0
- data/lib/telethon/cli/args.rb +74 -0
- data/lib/telethon/cli/commands.rb +339 -0
- data/lib/telethon/cli.rb +114 -0
- data/lib/telethon/client/auth.rb +149 -0
- data/lib/telethon/client/entities.rb +153 -0
- data/lib/telethon/client/files.rb +236 -0
- data/lib/telethon/client/messages.rb +154 -0
- data/lib/telethon/client.rb +174 -0
- data/lib/telethon/crypto/aes.rb +105 -0
- data/lib/telethon/crypto/hashes.rb +45 -0
- data/lib/telethon/crypto/native.rb +44 -0
- data/lib/telethon/crypto/num.rb +189 -0
- data/lib/telethon/crypto/rsa.rb +39 -0
- data/lib/telethon/crypto/srp.rb +99 -0
- data/lib/telethon/errors.rb +52 -0
- data/lib/telethon/network/auth_key.rb +21 -0
- data/lib/telethon/network/authenticator.rb +133 -0
- data/lib/telethon/network/sender.rb +168 -0
- data/lib/telethon/network/state.rb +103 -0
- data/lib/telethon/network/transport.rb +94 -0
- data/lib/telethon/private_files.rb +25 -0
- data/lib/telethon/qr.rb +356 -0
- data/lib/telethon/session.rb +153 -0
- data/lib/telethon/tl/binary.rb +124 -0
- data/lib/telethon/tl/codec.rb +194 -0
- data/lib/telethon/tl/object.rb +47 -0
- data/lib/telethon/tl/schema.rb +128 -0
- data/lib/telethon/tl/schema_data.rb +2709 -0
- data/lib/telethon/version.rb +7 -0
- data/lib/telethon.rb +35 -0
- data/telethon.gemspec +33 -0
- metadata +102 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "zlib"
|
|
4
|
+
|
|
5
|
+
module Telethon
|
|
6
|
+
module TL
|
|
7
|
+
VECTOR_ID = 0x1cb5c415
|
|
8
|
+
BOOL_TRUE = 0x997275b5
|
|
9
|
+
BOOL_FALSE = 0xbc799737
|
|
10
|
+
GZIP_PACKED = 0x3072cfa1
|
|
11
|
+
RPC_RESULT = 0xf35c6d01
|
|
12
|
+
MSG_CONTAINER = 0x73f1f8dc
|
|
13
|
+
|
|
14
|
+
class EncodeError < StandardError
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
PRIMITIVES = %w[int long double int128 int256 string bytes Bool true #].freeze
|
|
18
|
+
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
def schema
|
|
22
|
+
@schema ||= Schema.new(SCHEMA_TEXT, LAYER)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# A new object of a constructor or function, checking the name against the schema.
|
|
26
|
+
def make(name, fields = {})
|
|
27
|
+
raise EncodeError, "no #{name} in layer #{LAYER}" unless schema.named(name)
|
|
28
|
+
|
|
29
|
+
Object.new(name, fields)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# The boxed bytes of a constructor or function call.
|
|
33
|
+
def encode(object)
|
|
34
|
+
writer = Writer.new
|
|
35
|
+
write_object(writer, object, true)
|
|
36
|
+
writer.to_s
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def decode(data)
|
|
40
|
+
read_boxed(Reader.new(data))
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def write_object(writer, object, boxed)
|
|
44
|
+
ctor = schema.named(object.name)
|
|
45
|
+
raise EncodeError, "no #{object.name} in layer #{LAYER}" unless ctor
|
|
46
|
+
|
|
47
|
+
writer.uint(ctor.id) if boxed
|
|
48
|
+
ctor.params.each do |param|
|
|
49
|
+
if param.flags?
|
|
50
|
+
writer.uint(flag_value(ctor, param.name, object))
|
|
51
|
+
next
|
|
52
|
+
end
|
|
53
|
+
value = object[param.name]
|
|
54
|
+
if param.optional?
|
|
55
|
+
next if value.nil? || (param.type == "true")
|
|
56
|
+
elsif value.nil? && param.type != "true"
|
|
57
|
+
raise EncodeError, "#{object.name}.#{param.name} is required"
|
|
58
|
+
end
|
|
59
|
+
next if param.type == "true"
|
|
60
|
+
|
|
61
|
+
write_value(writer, param.type, value, "#{object.name}.#{param.name}")
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def flag_value(ctor, field, object)
|
|
66
|
+
bits = 0
|
|
67
|
+
ctor.params.each do |param|
|
|
68
|
+
next unless param.flag_field == field
|
|
69
|
+
|
|
70
|
+
value = object[param.name]
|
|
71
|
+
present = param.type == "true" ? value == true : !value.nil?
|
|
72
|
+
bits |= (1 << param.flag_bit) if present
|
|
73
|
+
end
|
|
74
|
+
bits
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def write_value(writer, type, value, where)
|
|
78
|
+
case type
|
|
79
|
+
when "int" then writer.int(value)
|
|
80
|
+
when "long" then writer.long(value)
|
|
81
|
+
when "double" then writer.double(value)
|
|
82
|
+
when "int128", "int256"
|
|
83
|
+
size = type == "int128" ? 16 : 32
|
|
84
|
+
raise EncodeError, "#{where} must be #{size} bytes" unless value.bytesize == size
|
|
85
|
+
|
|
86
|
+
writer.raw(value)
|
|
87
|
+
when "string", "bytes" then writer.tl_bytes(value)
|
|
88
|
+
when "Bool" then writer.uint(value ? BOOL_TRUE : BOOL_FALSE)
|
|
89
|
+
else
|
|
90
|
+
if type.start_with?("Vector<") || type.start_with?("vector<")
|
|
91
|
+
writer.uint(VECTOR_ID) if type.start_with?("V")
|
|
92
|
+
inner = type[7, type.size - 8]
|
|
93
|
+
writer.int(value.size)
|
|
94
|
+
value.each { |item| write_value(writer, inner, item, where) }
|
|
95
|
+
elsif raw_bytes?(value)
|
|
96
|
+
writer.raw(value) # an already-encoded object, such as a query passed through invokeWithLayer
|
|
97
|
+
else
|
|
98
|
+
write_object(writer, value, !bare?(type))
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def raw_bytes?(value)
|
|
104
|
+
value.is_a?(String)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# A bare type is written without its constructor id: lowercase names other than the primitives.
|
|
108
|
+
def bare?(type)
|
|
109
|
+
dot = type.rindex(".")
|
|
110
|
+
first = dot ? type[dot + 1] : type[0]
|
|
111
|
+
first == first.downcase && first != "!" && !PRIMITIVES.include?(type) && type != "X"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def read_boxed(reader)
|
|
115
|
+
id = reader.uint
|
|
116
|
+
read_with_id(reader, id)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def read_with_id(reader, id)
|
|
120
|
+
return read_boxed(Reader.new(Zlib.gunzip(reader.tl_bytes))) if id == GZIP_PACKED
|
|
121
|
+
return true if id == BOOL_TRUE
|
|
122
|
+
return false if id == BOOL_FALSE
|
|
123
|
+
|
|
124
|
+
ctor = schema.with_id(id)
|
|
125
|
+
raise ReadError, format("unknown constructor 0x%08x at %d", id, reader.pos - 4) unless ctor
|
|
126
|
+
|
|
127
|
+
read_fields(reader, ctor)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def read_fields(reader, ctor)
|
|
131
|
+
fields = {}
|
|
132
|
+
flags = {}
|
|
133
|
+
ctor.params.each do |param|
|
|
134
|
+
if param.flags?
|
|
135
|
+
flags[param.name] = reader.uint
|
|
136
|
+
next
|
|
137
|
+
end
|
|
138
|
+
if param.optional?
|
|
139
|
+
set = (flags.fetch(param.flag_field, 0) >> param.flag_bit).anybits?(1)
|
|
140
|
+
if param.type == "true"
|
|
141
|
+
fields[param.name] = set
|
|
142
|
+
next
|
|
143
|
+
end
|
|
144
|
+
unless set
|
|
145
|
+
fields[param.name] = nil
|
|
146
|
+
next
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
fields[param.name] = read_value(reader, param.type)
|
|
150
|
+
end
|
|
151
|
+
Object.new(ctor.name, fields)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def read_value(reader, type)
|
|
155
|
+
case type
|
|
156
|
+
when "int" then reader.int
|
|
157
|
+
when "long" then reader.long
|
|
158
|
+
when "double" then reader.double
|
|
159
|
+
when "int128" then reader.bytes(16)
|
|
160
|
+
when "int256" then reader.bytes(32)
|
|
161
|
+
when "string" then reader.tl_string
|
|
162
|
+
when "bytes" then reader.tl_bytes
|
|
163
|
+
when "Bool" then reader.uint == BOOL_TRUE
|
|
164
|
+
when "true" then true
|
|
165
|
+
else
|
|
166
|
+
if type.start_with?("Vector<") || type.start_with?("vector<")
|
|
167
|
+
if type.start_with?("V")
|
|
168
|
+
id = reader.uint
|
|
169
|
+
raise ReadError, format("expected a vector, got 0x%08x", id) unless id == VECTOR_ID
|
|
170
|
+
end
|
|
171
|
+
inner = type[7, type.size - 8]
|
|
172
|
+
Array.new(reader.int) { read_value(reader, inner) }
|
|
173
|
+
elsif bare?(type)
|
|
174
|
+
ctor = schema.named(type)
|
|
175
|
+
raise ReadError, "unknown bare type #{type}" unless ctor
|
|
176
|
+
|
|
177
|
+
read_fields(reader, ctor)
|
|
178
|
+
else
|
|
179
|
+
read_boxed(reader)
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# The result of a function call, read by the function's declared result type, so that results
|
|
185
|
+
# like Vector<long> (which carry no constructor ids) decode too.
|
|
186
|
+
def read_result(reader, function_name)
|
|
187
|
+
ctor = schema.named(function_name)
|
|
188
|
+
type = ctor ? ctor.result : "Object"
|
|
189
|
+
return read_boxed(reader) if type == "X" || type == "Object"
|
|
190
|
+
|
|
191
|
+
read_value(reader, type)
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Telethon
|
|
4
|
+
module TL
|
|
5
|
+
# One TL value: a constructor or function name from the schema (e.g. "message",
|
|
6
|
+
# "messages.getHistory") and its fields by their schema names. Absent optional fields are nil; flag
|
|
7
|
+
# fields of type `true` are true or false. Telethon generates a class per constructor; this port
|
|
8
|
+
# keeps them as data, which is what lets a Spinel build compile the whole schema (DEVELOPING.md).
|
|
9
|
+
class Object
|
|
10
|
+
attr_reader :name, :fields
|
|
11
|
+
|
|
12
|
+
def initialize(name, fields = {})
|
|
13
|
+
@name = name
|
|
14
|
+
@fields = fields
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def [](key)
|
|
18
|
+
@fields[key]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def []=(key, value)
|
|
22
|
+
@fields[key] = value
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def key?(key)
|
|
26
|
+
@fields.key?(key)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def is?(name)
|
|
30
|
+
@name == name
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def ==(other)
|
|
34
|
+
other.is_a?(Object) && other.name == name && other.fields == fields
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def to_s
|
|
38
|
+
inspect
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def inspect
|
|
42
|
+
parts = @fields.map { |k, v| "#{k}=#{v.inspect}" }
|
|
43
|
+
"#<#{@name} #{parts.join(' ')}>"
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Telethon
|
|
4
|
+
module TL
|
|
5
|
+
# One parameter of a constructor, parsed from e.g. "reply_to:flags.3?MessageReplyHeader".
|
|
6
|
+
class Param
|
|
7
|
+
attr_reader :name, :type, :flag_field, :flag_bit
|
|
8
|
+
|
|
9
|
+
def initialize(name, type, flag_field, flag_bit)
|
|
10
|
+
@name = name
|
|
11
|
+
@type = type
|
|
12
|
+
@flag_field = flag_field
|
|
13
|
+
@flag_bit = flag_bit
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def flags?
|
|
17
|
+
@type == "#"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def optional?
|
|
21
|
+
@flag_bit >= 0
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# A constructor or a function of the schema.
|
|
26
|
+
class Constructor
|
|
27
|
+
attr_reader :id, :name, :params, :result, :function
|
|
28
|
+
|
|
29
|
+
def initialize(id, name, params, result, function)
|
|
30
|
+
@id = id
|
|
31
|
+
@name = name
|
|
32
|
+
@params = params
|
|
33
|
+
@result = result
|
|
34
|
+
@function = function
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The parsed schema: data/mtproto.tl and data/api.tl, embedded in schema_data.rb so that a native
|
|
39
|
+
# build needs no file at run time.
|
|
40
|
+
class Schema
|
|
41
|
+
attr_reader :layer
|
|
42
|
+
|
|
43
|
+
def initialize(text, layer)
|
|
44
|
+
@layer = layer
|
|
45
|
+
@by_name = {}
|
|
46
|
+
@by_id = {}
|
|
47
|
+
@functions_by_id = {}
|
|
48
|
+
function = false
|
|
49
|
+
text.each_line do |raw|
|
|
50
|
+
line = raw.strip
|
|
51
|
+
next if line.empty? || line.start_with?("//")
|
|
52
|
+
|
|
53
|
+
if line == "---functions---"
|
|
54
|
+
function = true
|
|
55
|
+
next
|
|
56
|
+
end
|
|
57
|
+
if line == "---types---"
|
|
58
|
+
function = false
|
|
59
|
+
next
|
|
60
|
+
end
|
|
61
|
+
ctor = parse(line, function)
|
|
62
|
+
next unless ctor
|
|
63
|
+
|
|
64
|
+
@by_name[ctor.name] = ctor
|
|
65
|
+
if function
|
|
66
|
+
@functions_by_id[ctor.id] = ctor
|
|
67
|
+
else
|
|
68
|
+
@by_id[ctor.id] = ctor
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def size
|
|
74
|
+
@by_name.size
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def named(name)
|
|
78
|
+
@by_name[name]
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def with_id(id)
|
|
82
|
+
@by_id[id]
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def function_with_id(id)
|
|
86
|
+
@functions_by_id[id]
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
def parse(line, function)
|
|
92
|
+
line = line.sub(/;\z/, "").strip
|
|
93
|
+
head, result = line.split(" = ", 2)
|
|
94
|
+
return nil unless result
|
|
95
|
+
|
|
96
|
+
words = head.split(" ")
|
|
97
|
+
first = words.shift
|
|
98
|
+
hash = first.index("#")
|
|
99
|
+
return nil unless hash
|
|
100
|
+
|
|
101
|
+
name = first[0, hash]
|
|
102
|
+
id = first[(hash + 1)..].to_i(16)
|
|
103
|
+
params = []
|
|
104
|
+
words.each do |word|
|
|
105
|
+
next if word.start_with?("{") # {X:Type}
|
|
106
|
+
|
|
107
|
+
colon = word.index(":")
|
|
108
|
+
next unless colon
|
|
109
|
+
|
|
110
|
+
pname = word[0, colon]
|
|
111
|
+
type = word[(colon + 1)..]
|
|
112
|
+
flag_field = ""
|
|
113
|
+
flag_bit = -1
|
|
114
|
+
q = type.index("?")
|
|
115
|
+
if q
|
|
116
|
+
cond = type[0, q]
|
|
117
|
+
type = type[(q + 1)..]
|
|
118
|
+
dot = cond.index(".")
|
|
119
|
+
flag_field = cond[0, dot]
|
|
120
|
+
flag_bit = cond[(dot + 1)..].to_i
|
|
121
|
+
end
|
|
122
|
+
params << Param.new(pname, type, flag_field, flag_bit)
|
|
123
|
+
end
|
|
124
|
+
Constructor.new(id, name, params, result.strip, function)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|