telethon 0.1.0 → 0.2.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 +4 -4
- data/CHANGELOG.md +96 -0
- data/README.md +103 -8
- data/lib/telethon/cli/commands.rb +25 -20
- data/lib/telethon/client/auth.rb +22 -1
- data/lib/telethon/client/chats.rb +577 -0
- data/lib/telethon/client/dialogs.rb +211 -0
- data/lib/telethon/client/downloads.rb +659 -0
- data/lib/telethon/client/entities.rb +50 -2
- data/lib/telethon/client/files.rb +341 -192
- data/lib/telethon/client/messages.rb +264 -40
- data/lib/telethon/client/updates.rb +407 -0
- data/lib/telethon/client.rb +119 -31
- data/lib/telethon/crypto/rsa.rb +81 -5
- data/lib/telethon/custom/admin_log_event.rb +133 -0
- data/lib/telethon/custom/conversation.rb +358 -0
- data/lib/telethon/custom/dialog.rb +144 -0
- data/lib/telethon/custom/file.rb +130 -0
- data/lib/telethon/custom/forward.rb +63 -0
- data/lib/telethon/custom/getters.rb +152 -0
- data/lib/telethon/custom/participant_permissions.rb +100 -0
- data/lib/telethon/errors.rb +12 -0
- data/lib/telethon/events/album.rb +108 -0
- data/lib/telethon/events/chat_action.rb +212 -0
- data/lib/telethon/events/common.rb +173 -0
- data/lib/telethon/events/message_deleted.rb +119 -0
- data/lib/telethon/events/new_message.rb +176 -0
- data/lib/telethon/events/raw.rb +35 -0
- data/lib/telethon/events/user_update.rb +129 -0
- data/lib/telethon/extensions/html.rb +373 -0
- data/lib/telethon/extensions/markdown.rb +116 -0
- data/lib/telethon/extensions/text.rb +171 -0
- data/lib/telethon/message.rb +423 -0
- data/lib/telethon/mime.rb +1930 -0
- data/lib/telethon/mime_guess.rb +54 -0
- data/lib/telethon/network/authenticator.rb +5 -4
- data/lib/telethon/network/sender.rb +7 -0
- data/lib/telethon/private_files.rb +25 -0
- data/lib/telethon/session.rb +18 -0
- data/lib/telethon/tl/object.rb +4 -0
- data/lib/telethon/updates/message_box.rb +481 -0
- data/lib/telethon/version.rb +1 -1
- data/lib/telethon.rb +25 -0
- metadata +26 -1
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Telethon
|
|
4
|
+
# Python's mimetypes.guess_type and guess_extension over the tables in mime.rb, which is how Telethon
|
|
5
|
+
# names and types files.
|
|
6
|
+
module Mime
|
|
7
|
+
# Python's posixpath.splitext: [root, ext]; leading dots of the name are not an extension.
|
|
8
|
+
def self.splitext(path)
|
|
9
|
+
sep = path.rindex("/") || -1
|
|
10
|
+
dot = path.rindex(".")
|
|
11
|
+
return [path, ""] if dot.nil? || dot < sep
|
|
12
|
+
|
|
13
|
+
i = sep + 1
|
|
14
|
+
while i < dot
|
|
15
|
+
return [path[0, dot], path[dot..]] if path[i] != "."
|
|
16
|
+
|
|
17
|
+
i += 1
|
|
18
|
+
end
|
|
19
|
+
[path, ""]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# [mime type or nil, encoding or nil] for a file name or URL, as mimetypes.guess_type (strict).
|
|
23
|
+
def self.guess_type(name)
|
|
24
|
+
path = name.to_s
|
|
25
|
+
scheme = path[/\A[A-Za-z][A-Za-z0-9+\-.]*(?=:)/]
|
|
26
|
+
if scheme && scheme.size > 1
|
|
27
|
+
path = path[(scheme.size + 1)..]
|
|
28
|
+
if scheme.downcase == "data"
|
|
29
|
+
comma = path.index(",")
|
|
30
|
+
return [nil, nil] unless comma
|
|
31
|
+
|
|
32
|
+
semi = path.index(";")
|
|
33
|
+
type = semi && semi < comma ? path[0, semi] : path[0, comma]
|
|
34
|
+
type = "text/plain" if type.include?("=") || !type.include?("/")
|
|
35
|
+
return [type, nil]
|
|
36
|
+
end
|
|
37
|
+
path = path.sub(%r{\A//[^/?#]*}, "")
|
|
38
|
+
path = path.split(/[?#]/, 2)[0] || ""
|
|
39
|
+
end
|
|
40
|
+
base, ext = splitext(path)
|
|
41
|
+
while (stands_for = SUFFIXES[ext.downcase])
|
|
42
|
+
base, ext = splitext(base + stands_for)
|
|
43
|
+
end
|
|
44
|
+
encoding = ENCODINGS[ext]
|
|
45
|
+
_base, ext = splitext(base) if encoding
|
|
46
|
+
[TYPES[ext.downcase], encoding]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The extension (with its dot) for a mime type, or nil: mimetypes.guess_extension.
|
|
50
|
+
def self.guess_extension(mime)
|
|
51
|
+
EXTENSIONS[mime.to_s.downcase]
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -10,7 +10,8 @@ module Telethon
|
|
|
10
10
|
|
|
11
11
|
module_function
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
# keys are the RSA keys the data center may ask for: Telegram's own unless given (a CDN's).
|
|
14
|
+
def run(transport, keys = Crypto::RSA::KEYS)
|
|
14
15
|
plain = PlainSender.new(transport)
|
|
15
16
|
|
|
16
17
|
nonce = Crypto.random_bytes(16)
|
|
@@ -26,7 +27,7 @@ module Telethon
|
|
|
26
27
|
inner = TL.encode(TL.make("p_q_inner_data", "pq" => pq_bytes, "p" => p_bytes, "q" => q_bytes,
|
|
27
28
|
"nonce" => nonce, "server_nonce" => res_pq["server_nonce"],
|
|
28
29
|
"new_nonce" => new_nonce))
|
|
29
|
-
fingerprint, cipher = encrypt_inner(res_pq["server_public_key_fingerprints"], inner)
|
|
30
|
+
fingerprint, cipher = encrypt_inner(res_pq["server_public_key_fingerprints"], inner, keys)
|
|
30
31
|
|
|
31
32
|
params = plain.call(TL.make("req_DH_params", "nonce" => nonce, "server_nonce" => res_pq["server_nonce"],
|
|
32
33
|
"p" => p_bytes, "q" => q_bytes,
|
|
@@ -75,10 +76,10 @@ module Telethon
|
|
|
75
76
|
[auth_key, time_offset]
|
|
76
77
|
end
|
|
77
78
|
|
|
78
|
-
def encrypt_inner(fingerprints, inner)
|
|
79
|
+
def encrypt_inner(fingerprints, inner, keys = Crypto::RSA::KEYS)
|
|
79
80
|
[false, true].each do |use_old|
|
|
80
81
|
fingerprints.each do |fingerprint|
|
|
81
|
-
cipher = Crypto::RSA.encrypt(fingerprint, inner, use_old: use_old)
|
|
82
|
+
cipher = Crypto::RSA.encrypt(fingerprint, inner, use_old: use_old, keys: keys)
|
|
82
83
|
return [fingerprint, cipher] if cipher
|
|
83
84
|
end
|
|
84
85
|
end
|
|
@@ -21,5 +21,30 @@ module Telethon
|
|
|
21
21
|
File.rename(temporary, path)
|
|
22
22
|
path
|
|
23
23
|
end
|
|
24
|
+
|
|
25
|
+
# Writes what the block gives the file through a temporary file, renamed into place at the end and
|
|
26
|
+
# removed if the block fails. Missing directories above it are made owner-only.
|
|
27
|
+
def stream(path)
|
|
28
|
+
parents(path)
|
|
29
|
+
temporary = "#{path}.tmp"
|
|
30
|
+
begin
|
|
31
|
+
File.open(temporary, File::WRONLY | File::CREAT | File::TRUNC, 0o600) do |file|
|
|
32
|
+
file.chmod(0o600)
|
|
33
|
+
yield file
|
|
34
|
+
end
|
|
35
|
+
File.rename(temporary, path)
|
|
36
|
+
ensure
|
|
37
|
+
File.delete(temporary) if File.exist?(temporary)
|
|
38
|
+
end
|
|
39
|
+
path
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def parents(path)
|
|
43
|
+
dir = File.dirname(path)
|
|
44
|
+
return if dir == path || Dir.exist?(dir)
|
|
45
|
+
|
|
46
|
+
parents(dir)
|
|
47
|
+
Dir.mkdir(dir, 0o700)
|
|
48
|
+
end
|
|
24
49
|
end
|
|
25
50
|
end
|
data/lib/telethon/session.rb
CHANGED
|
@@ -16,6 +16,11 @@ module Telethon
|
|
|
16
16
|
TEST = { 1 => "149.154.175.10", 2 => "149.154.167.40", 3 => "149.154.175.117" }.freeze
|
|
17
17
|
|
|
18
18
|
attr_accessor :dc_id, :address, :port, :auth_key, :user_id, :test
|
|
19
|
+
# Whether the account is a bot: true, false, or nil until get_me has said (Telethon's self_bot).
|
|
20
|
+
attr_accessor :bot
|
|
21
|
+
# The update state, to catch up on what was missed: {"pts", "qts", "date", "seq"} or nil, and
|
|
22
|
+
# {channel_id => pts}.
|
|
23
|
+
attr_accessor :update_state, :channel_pts
|
|
19
24
|
attr_reader :directory, :entities
|
|
20
25
|
|
|
21
26
|
def initialize(directory)
|
|
@@ -25,7 +30,10 @@ module Telethon
|
|
|
25
30
|
@port = 443
|
|
26
31
|
@auth_key = nil
|
|
27
32
|
@user_id = nil
|
|
33
|
+
@bot = nil
|
|
28
34
|
@test = false
|
|
35
|
+
@update_state = nil
|
|
36
|
+
@channel_pts = {}
|
|
29
37
|
@entities = {}
|
|
30
38
|
@entities_changed = false
|
|
31
39
|
load
|
|
@@ -79,6 +87,13 @@ module Telethon
|
|
|
79
87
|
PrivateFiles.directory(@directory)
|
|
80
88
|
data = { "dc_id" => @dc_id, "address" => @address, "port" => @port,
|
|
81
89
|
"auth_key" => @auth_key ? @auth_key.unpack1("H*") : nil, "user_id" => @user_id, "test" => @test }
|
|
90
|
+
data["bot"] = @bot unless @bot.nil?
|
|
91
|
+
if @update_state
|
|
92
|
+
data["update_state"] = @update_state
|
|
93
|
+
channels = {}
|
|
94
|
+
@channel_pts.each { |id, pts| channels[id.to_s] = pts }
|
|
95
|
+
data["channel_pts"] = channels
|
|
96
|
+
end
|
|
82
97
|
PrivateFiles.write(session_path, "#{JSON.pretty_generate(data)}\n")
|
|
83
98
|
save_entities if @entities_changed
|
|
84
99
|
end
|
|
@@ -104,7 +119,10 @@ module Telethon
|
|
|
104
119
|
key = data["auth_key"]
|
|
105
120
|
@auth_key = key ? [key].pack("H*") : nil
|
|
106
121
|
@user_id = data["user_id"]
|
|
122
|
+
@bot = data["bot"]
|
|
107
123
|
@test = data["test"] == true
|
|
124
|
+
@update_state = data["update_state"]
|
|
125
|
+
(data["channel_pts"] || {}).each { |id, pts| @channel_pts[id.to_i] = pts }
|
|
108
126
|
end
|
|
109
127
|
@entities = JSON.parse(File.read(entities_path, encoding: "UTF-8")) if File.exist?(entities_path)
|
|
110
128
|
end
|
data/lib/telethon/tl/object.rb
CHANGED
|
@@ -8,10 +8,14 @@ module Telethon
|
|
|
8
8
|
# keeps them as data, which is what lets a Spinel build compile the whole schema (DEVELOPING.md).
|
|
9
9
|
class Object
|
|
10
10
|
attr_reader :name, :fields
|
|
11
|
+
# What get_participants puts on each user it answers, as Telethon sets user.participant: the
|
|
12
|
+
# channelParticipant* or chatParticipant* entry, or nil. Not part of the TL value.
|
|
13
|
+
attr_accessor :participant
|
|
11
14
|
|
|
12
15
|
def initialize(name, fields = {})
|
|
13
16
|
@name = name
|
|
14
17
|
@fields = fields
|
|
18
|
+
@participant = nil
|
|
15
19
|
end
|
|
16
20
|
|
|
17
21
|
def [](key)
|
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Telethon
|
|
4
|
+
# Telethon's _updates package: knowing which updates were seen, finding gaps in them, and asking for
|
|
5
|
+
# the difference when one is found (https://core.telegram.org/api/updates).
|
|
6
|
+
module Updates
|
|
7
|
+
# Telegram sends seq 0 when "it doesn't matter", so 0 means unknown here too.
|
|
8
|
+
NO_SEQ = 0
|
|
9
|
+
BOT_CHANNEL_DIFF_LIMIT = 100_000
|
|
10
|
+
USER_CHANNEL_DIFF_LIMIT = 100
|
|
11
|
+
# > It may be useful to wait up to 0.5 seconds
|
|
12
|
+
POSSIBLE_GAP_TIMEOUT = 0.5
|
|
13
|
+
# Without any update for this long, an entry fetches its difference (the docs say 15 minutes).
|
|
14
|
+
NO_UPDATES_TIMEOUT = 15 * 60
|
|
15
|
+
|
|
16
|
+
# Entries of the message box. Telethon keys them with two sentinel objects and channel ids; these
|
|
17
|
+
# are negative so every key is an Integer (channel ids are positive).
|
|
18
|
+
ACCOUNT = -1 # the account-wide pts: private chats and basic groups
|
|
19
|
+
SECRET = -2 # the account-wide qts: secret chats
|
|
20
|
+
|
|
21
|
+
# Raised by process_updates when the updates cannot be applied: the difference must be fetched.
|
|
22
|
+
class GapError < StandardError
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Seconds on a clock that only moves forward; tests replace it.
|
|
26
|
+
class Clock
|
|
27
|
+
def now
|
|
28
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Telethon's PtsInfo.from_update: [pts, pts_count, entry] of an update, or nil for one that may be
|
|
33
|
+
# applied in any order.
|
|
34
|
+
def self.pts_info(update)
|
|
35
|
+
pts = update["pts"]
|
|
36
|
+
if pts && pts != 0
|
|
37
|
+
count = update["pts_count"] || 0
|
|
38
|
+
message = update["message"]
|
|
39
|
+
peer = message.is_a?(TL::Object) ? message["peer_id"] : nil
|
|
40
|
+
entry = if peer.is_a?(TL::Object) && peer["channel_id"]
|
|
41
|
+
peer["channel_id"]
|
|
42
|
+
elsif update["channel_id"] && update["channel_id"] != 0
|
|
43
|
+
update["channel_id"]
|
|
44
|
+
else
|
|
45
|
+
ACCOUNT
|
|
46
|
+
end
|
|
47
|
+
return [pts, count, entry]
|
|
48
|
+
end
|
|
49
|
+
qts = update["qts"]
|
|
50
|
+
return [qts, 1, SECRET] if qts && qts != 0
|
|
51
|
+
|
|
52
|
+
nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Where an update would start in its sequence, to sort a batch (Telethon's _sort_gaps).
|
|
56
|
+
def self.gap_key(update)
|
|
57
|
+
info = pts_info(update)
|
|
58
|
+
info ? info[0] - info[1] : 0
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# A stable sort of [update, self_outgoing] pairs by gap_key, as Python's sorted() is stable.
|
|
62
|
+
def self.sort_pairs(pairs)
|
|
63
|
+
keyed = []
|
|
64
|
+
pairs.each_with_index { |pair, i| keyed << [gap_key(pair[0]), i, pair] }
|
|
65
|
+
keyed.sort! { |a, b| a[0] == b[0] ? a[1] <=> b[1] : a[0] <=> b[0] }
|
|
66
|
+
keyed.map { |k| k[2] }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# The local pts of an entry, and when to fetch its difference if no update comes before then.
|
|
70
|
+
class State
|
|
71
|
+
attr_accessor :pts, :deadline
|
|
72
|
+
|
|
73
|
+
def initialize(pts, deadline)
|
|
74
|
+
@pts = pts
|
|
75
|
+
@deadline = deadline
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Updates past a gap, held for a moment in case the missing ones arrive (pairs [update, self_outgoing]).
|
|
80
|
+
class PossibleGap
|
|
81
|
+
attr_accessor :deadline, :updates
|
|
82
|
+
|
|
83
|
+
def initialize(deadline, updates)
|
|
84
|
+
@deadline = deadline
|
|
85
|
+
@updates = updates
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Telethon's MessageBox. An update is either applied, skipped as already seen, or held as a possible
|
|
90
|
+
# gap; a gap that does not close within half a second, or an entry silent for 15 minutes, gets its
|
|
91
|
+
# difference fetched (get_difference, get_channel_difference), and the entry's socket updates are
|
|
92
|
+
# ignored meanwhile, since the difference carries them.
|
|
93
|
+
class MessageBox
|
|
94
|
+
attr_reader :map, :possible_gaps, :getting_diff_for, :next_deadline
|
|
95
|
+
attr_accessor :date, :seq
|
|
96
|
+
attr_writer :clock
|
|
97
|
+
|
|
98
|
+
def initialize(clock = Clock.new)
|
|
99
|
+
@clock = clock
|
|
100
|
+
@map = {}
|
|
101
|
+
@date = 1 # Telethon: the epoch plus a second
|
|
102
|
+
@seq = NO_SEQ
|
|
103
|
+
@next_deadline = nil
|
|
104
|
+
@possible_gaps = {}
|
|
105
|
+
@getting_diff_for = {} # a set: entry => true, in insertion order
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def now
|
|
109
|
+
@clock.now
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def next_updates_deadline
|
|
113
|
+
now + NO_UPDATES_TIMEOUT
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# A previously saved state: account pts/qts/date/seq and {channel_id => pts}.
|
|
117
|
+
def load(pts, qts, date, seq, channels)
|
|
118
|
+
deadline = next_updates_deadline
|
|
119
|
+
@map = {}
|
|
120
|
+
@map[ACCOUNT] = State.new(pts, deadline) if pts != NO_SEQ
|
|
121
|
+
@map[SECRET] = State.new(qts, deadline) if qts != NO_SEQ
|
|
122
|
+
channels.each { |id, channel_pts| @map[id] = State.new(channel_pts, deadline) }
|
|
123
|
+
@date = date
|
|
124
|
+
@seq = seq
|
|
125
|
+
@next_deadline = ACCOUNT
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# [{"pts", "qts", "date", "seq"}, {channel_id => pts}], to save.
|
|
129
|
+
def session_state
|
|
130
|
+
account = { "pts" => @map[ACCOUNT] ? @map[ACCOUNT].pts : NO_SEQ,
|
|
131
|
+
"qts" => @map[SECRET] ? @map[SECRET].pts : NO_SEQ, "date" => @date, "seq" => @seq }
|
|
132
|
+
channels = {}
|
|
133
|
+
@map.each { |id, state| channels[id] = state.pts if id.positive? }
|
|
134
|
+
[account, channels]
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def empty?
|
|
138
|
+
!@map.key?(ACCOUNT)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# The next instant receiving updates should time out at. Entries past their deadline are marked
|
|
142
|
+
# as needing their difference; while any is, this answers now.
|
|
143
|
+
def check_deadlines
|
|
144
|
+
current = now
|
|
145
|
+
return current unless @getting_diff_for.empty?
|
|
146
|
+
|
|
147
|
+
deadline = next_updates_deadline
|
|
148
|
+
if !@possible_gaps.empty?
|
|
149
|
+
@possible_gaps.each_value { |gap| deadline = gap.deadline if gap.deadline < deadline }
|
|
150
|
+
elsif @next_deadline && @map.key?(@next_deadline)
|
|
151
|
+
entry_deadline = @map[@next_deadline].deadline
|
|
152
|
+
deadline = entry_deadline if entry_deadline < deadline
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
if current >= deadline
|
|
156
|
+
@possible_gaps.each { |entry, gap| @getting_diff_for[entry] = true if current >= gap.deadline }
|
|
157
|
+
@map.each { |entry, state| @getting_diff_for[entry] = true if current >= state.deadline }
|
|
158
|
+
@getting_diff_for.each_key { |entry| @possible_gaps.delete(entry) }
|
|
159
|
+
end
|
|
160
|
+
deadline
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Resets the no-updates deadline of entries (an array), keeping next_deadline the closest one.
|
|
164
|
+
def reset_deadlines(entries, deadline)
|
|
165
|
+
return if entries.empty?
|
|
166
|
+
|
|
167
|
+
last = nil
|
|
168
|
+
entries.each do |entry|
|
|
169
|
+
raise Error, "reset_deadlines on entry #{entry}, which has no state" unless @map.key?(entry)
|
|
170
|
+
|
|
171
|
+
@map[entry].deadline = deadline
|
|
172
|
+
last = entry
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
if entries.include?(@next_deadline)
|
|
176
|
+
closest = nil
|
|
177
|
+
@map.each { |entry, state| closest = entry if closest.nil? || state.deadline < @map[closest].deadline }
|
|
178
|
+
@next_deadline = closest
|
|
179
|
+
elsif @next_deadline && @map.key?(@next_deadline) && deadline < @map[@next_deadline].deadline
|
|
180
|
+
@next_deadline = last
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def reset_channel_deadline(channel_id, timeout)
|
|
185
|
+
timeout = NO_UPDATES_TIMEOUT if timeout.nil? || timeout.zero?
|
|
186
|
+
reset_deadlines([channel_id], now + timeout)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# The state from updates.getState or a difference (an updates.state). Right after login, so that
|
|
190
|
+
# older updates are not fetched.
|
|
191
|
+
def set_state(state, reset: true)
|
|
192
|
+
deadline = next_updates_deadline
|
|
193
|
+
if state["pts"] != NO_SEQ || !reset
|
|
194
|
+
@map[ACCOUNT] = State.new(state["pts"], deadline)
|
|
195
|
+
else
|
|
196
|
+
@map.delete(ACCOUNT)
|
|
197
|
+
end
|
|
198
|
+
# Telegram uses the qts for bots, but may reset it back to 0 while applying a difference
|
|
199
|
+
# (Telethon issue #3873): at login 0 means unknown, while applying a difference it means 0.
|
|
200
|
+
if state["qts"] != NO_SEQ || !reset
|
|
201
|
+
@map[SECRET] = State.new(state["qts"], deadline)
|
|
202
|
+
else
|
|
203
|
+
@map.delete(SECRET)
|
|
204
|
+
end
|
|
205
|
+
@date = state["date"]
|
|
206
|
+
@seq = state["seq"]
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# A channel's pts, as getting dialogs learns it, only if none is known yet.
|
|
210
|
+
def try_set_channel_state(id, pts)
|
|
211
|
+
@map[id] = State.new(pts, next_updates_deadline) unless @map.key?(id)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# Starts getting the difference for an entry, if it has a known state to start from.
|
|
215
|
+
def try_begin_get_diff(entry)
|
|
216
|
+
unless @map.key?(entry)
|
|
217
|
+
raise Error, "a possible gap for entry #{entry}, which has no state" if @possible_gaps.key?(entry)
|
|
218
|
+
|
|
219
|
+
return
|
|
220
|
+
end
|
|
221
|
+
@getting_diff_for[entry] = true
|
|
222
|
+
@possible_gaps.delete(entry)
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def end_get_diff(entry)
|
|
226
|
+
raise Error, "end_get_diff on entry #{entry}, which was not getting its difference" unless @getting_diff_for.key?(entry)
|
|
227
|
+
|
|
228
|
+
@getting_diff_for.delete(entry)
|
|
229
|
+
reset_deadlines([entry], next_updates_deadline)
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# Applies an Updates value the socket or a request gave. Appends to result the updates to
|
|
233
|
+
# dispatch (not those of a request this client made: self_outgoing) and answers [users, chats].
|
|
234
|
+
# Raises GapError when the difference must be fetched first.
|
|
235
|
+
def process_updates(updates, self_outgoing, result)
|
|
236
|
+
date = updates["date"]
|
|
237
|
+
seq = updates["seq"]
|
|
238
|
+
seq_start = updates["seq_start"]
|
|
239
|
+
users = updates["users"] || []
|
|
240
|
+
chats = updates["chats"] || []
|
|
241
|
+
|
|
242
|
+
if date.nil?
|
|
243
|
+
# updatesTooLong is the only one without a date; it is a gap.
|
|
244
|
+
try_begin_get_diff(ACCOUNT)
|
|
245
|
+
raise GapError, "updatesTooLong"
|
|
246
|
+
end
|
|
247
|
+
seq ||= NO_SEQ
|
|
248
|
+
seq_start ||= seq
|
|
249
|
+
|
|
250
|
+
list = updates["updates"]
|
|
251
|
+
if list.nil? || list.empty?
|
|
252
|
+
# updateShort is the only one that cannot be dispatched itself and has no updates field.
|
|
253
|
+
list = [updates.is?("updateShort") ? updates["update"] : updates]
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# > For all the other [not updates or updatesCombined] Updates type constructors there is no
|
|
257
|
+
# > need to check seq or change a local state.
|
|
258
|
+
if seq_start != NO_SEQ
|
|
259
|
+
return [users, chats] if @seq + 1 > seq_start # already handled
|
|
260
|
+
|
|
261
|
+
if @seq + 1 < seq_start
|
|
262
|
+
try_begin_get_diff(ACCOUNT)
|
|
263
|
+
raise GapError, "seq gap"
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
applied = []
|
|
268
|
+
reset = {}
|
|
269
|
+
# Telegram can send updates out of order (readChannelInbox before newChannelMessage with the same
|
|
270
|
+
# pts), so they are sorted first.
|
|
271
|
+
Updates.sort_pairs(list.map { |u| [u, self_outgoing] }).each do |pair|
|
|
272
|
+
update = apply_pts_info(pair[0], pair[1], reset)
|
|
273
|
+
applied << [update, pair[1]] if update
|
|
274
|
+
end
|
|
275
|
+
reset_deadlines(reset.keys, next_updates_deadline)
|
|
276
|
+
|
|
277
|
+
unless @possible_gaps.empty?
|
|
278
|
+
# See whether the updates held in gaps can be applied now. One that cannot goes back to the
|
|
279
|
+
# end of its gap, so a full pass keeps the order.
|
|
280
|
+
@possible_gaps.keys.each do |key|
|
|
281
|
+
gap = @possible_gaps[key]
|
|
282
|
+
gap.updates = Updates.sort_pairs(gap.updates)
|
|
283
|
+
gap.updates.size.times do
|
|
284
|
+
pair = gap.updates.shift
|
|
285
|
+
update = apply_pts_info(pair[0], pair[1], nil)
|
|
286
|
+
applied << [update, pair[1]] if update
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
@possible_gaps.delete_if { |_, gap| gap.updates.empty? }
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
applied.each { |pair| result << pair[0] unless pair[1] }
|
|
293
|
+
|
|
294
|
+
if !applied.empty? && @possible_gaps.empty?
|
|
295
|
+
# > If the updates were applied, local Updates state must be updated with seq (unless it's 0)
|
|
296
|
+
# > and date from the constructor.
|
|
297
|
+
@date = date if date != 0
|
|
298
|
+
@seq = seq if seq != NO_SEQ
|
|
299
|
+
end
|
|
300
|
+
[users, chats]
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
# Answers the update if its pts follows the local one. Else holds it as a possible gap (unless it
|
|
304
|
+
# was seen already, or its difference is being fetched) and answers nil. reset collects the entries
|
|
305
|
+
# whose no-updates deadline restarts. (Telethon passes it an empty set and tests its truthiness, so
|
|
306
|
+
# its deadlines never reset; this does what it means to.)
|
|
307
|
+
def apply_pts_info(update, self_outgoing, reset)
|
|
308
|
+
if update.is?("updateChannelTooLong")
|
|
309
|
+
try_begin_get_diff(update["channel_id"])
|
|
310
|
+
return nil
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
info = Updates.pts_info(update)
|
|
314
|
+
return update unless info # no pts: applies in any order
|
|
315
|
+
|
|
316
|
+
pts, count, entry = info
|
|
317
|
+
reset[entry] = true if reset
|
|
318
|
+
return nil if @getting_diff_for.key?(entry)
|
|
319
|
+
|
|
320
|
+
if @map.key?(entry)
|
|
321
|
+
local = @map[entry].pts
|
|
322
|
+
return nil if local + count > pts # already handled
|
|
323
|
+
|
|
324
|
+
if local + count < pts
|
|
325
|
+
@possible_gaps[entry] ||= PossibleGap.new(now + POSSIBLE_GAP_TIMEOUT, [])
|
|
326
|
+
@possible_gaps[entry].updates << [update, self_outgoing]
|
|
327
|
+
return nil
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
if @map.key?(entry)
|
|
332
|
+
@map[entry].pts = pts
|
|
333
|
+
else
|
|
334
|
+
# A channel may start with readChannelInbox (pts 1, count 0) and then newChannelMessage
|
|
335
|
+
# (pts 2, count 1): a count of 0 starts one lower, so the message is not taken as seen.
|
|
336
|
+
start = pts - (count.zero? ? 1 : 0)
|
|
337
|
+
@map[entry] = State.new(start.zero? ? 1 : start, next_updates_deadline)
|
|
338
|
+
end
|
|
339
|
+
update
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
# The updates.getDifference to make for the account, if any.
|
|
343
|
+
def get_difference
|
|
344
|
+
[ACCOUNT, SECRET].each do |entry|
|
|
345
|
+
next unless @getting_diff_for.key?(entry)
|
|
346
|
+
raise Error, "getting the difference of entry #{entry}, which has no state" unless @map.key?(entry)
|
|
347
|
+
|
|
348
|
+
return TL.make("updates.getDifference", "pts" => @map[ACCOUNT].pts, "date" => @date,
|
|
349
|
+
"qts" => @map[SECRET] ? @map[SECRET].pts : NO_SEQ)
|
|
350
|
+
end
|
|
351
|
+
nil
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
# Applies an updates.Difference. Answers [updates, users, chats].
|
|
355
|
+
def apply_difference(diff)
|
|
356
|
+
finish = false
|
|
357
|
+
result = [[], [], []]
|
|
358
|
+
case diff.name
|
|
359
|
+
when "updates.differenceEmpty"
|
|
360
|
+
finish = true
|
|
361
|
+
@date = diff["date"]
|
|
362
|
+
@seq = diff["seq"]
|
|
363
|
+
when "updates.difference"
|
|
364
|
+
finish = true
|
|
365
|
+
result = apply_difference_type(diff)
|
|
366
|
+
when "updates.differenceSlice"
|
|
367
|
+
result = apply_difference_type(diff)
|
|
368
|
+
when "updates.differenceTooLong"
|
|
369
|
+
finish = true
|
|
370
|
+
@map[ACCOUNT].pts = diff["pts"] # its deadline resets as the difference ends
|
|
371
|
+
end
|
|
372
|
+
end_difference if finish
|
|
373
|
+
result
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
def apply_difference_type(diff)
|
|
377
|
+
set_state(diff["intermediate_state"] || diff["state"], reset: false)
|
|
378
|
+
updates = difference_updates(diff["other_updates"], [ACCOUNT, SECRET])
|
|
379
|
+
diff["new_messages"].each do |m|
|
|
380
|
+
updates << TL.make("updateNewMessage", "message" => m, "pts" => NO_SEQ, "pts_count" => NO_SEQ)
|
|
381
|
+
end
|
|
382
|
+
diff["new_encrypted_messages"].each do |m|
|
|
383
|
+
updates << TL.make("updateNewEncryptedMessage", "message" => m, "qts" => NO_SEQ)
|
|
384
|
+
end
|
|
385
|
+
[updates, diff["users"], diff["chats"]]
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
# The other_updates of a difference. Telethon runs them through process_updates after moving to the
|
|
389
|
+
# difference's state, while the entry is still getting its difference, so every one with a pts reads
|
|
390
|
+
# as handled or as being fetched and is dropped: an edit, deletion or read that only a difference
|
|
391
|
+
# brings (every catch_up) never reaches a handler. Those of the entries the difference is for are
|
|
392
|
+
# past the old local pts by definition, so they are given back; any other (a channel's, in the
|
|
393
|
+
# account's difference) goes through as if from the socket.
|
|
394
|
+
def difference_updates(list, entries)
|
|
395
|
+
result = []
|
|
396
|
+
Updates.sort_pairs(list.map { |u| [u, false] }).each do |pair|
|
|
397
|
+
update = pair[0]
|
|
398
|
+
if update.is?("updateChannelTooLong")
|
|
399
|
+
try_begin_get_diff(update["channel_id"])
|
|
400
|
+
next
|
|
401
|
+
end
|
|
402
|
+
info = Updates.pts_info(update)
|
|
403
|
+
if info.nil? || entries.include?(info[2])
|
|
404
|
+
result << update
|
|
405
|
+
else
|
|
406
|
+
applied = apply_pts_info(update, false, nil)
|
|
407
|
+
result << applied if applied
|
|
408
|
+
end
|
|
409
|
+
end
|
|
410
|
+
result
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
def end_difference
|
|
414
|
+
account = @getting_diff_for.key?(ACCOUNT)
|
|
415
|
+
secret = @getting_diff_for.key?(SECRET)
|
|
416
|
+
raise Error, "ending a difference while neither account nor secret is getting one" unless account || secret
|
|
417
|
+
|
|
418
|
+
end_get_diff(ACCOUNT) if account
|
|
419
|
+
end_get_diff(SECRET) if secret
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
# The channel to get the difference of next, or nil.
|
|
423
|
+
def channel_needing_difference
|
|
424
|
+
@getting_diff_for.each_key { |entry| return entry if entry.positive? }
|
|
425
|
+
nil
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
# The updates.getChannelDifference for a channel, given its access hash.
|
|
429
|
+
def get_channel_difference(channel_id, access_hash, bot)
|
|
430
|
+
state = @map[channel_id]
|
|
431
|
+
raise Error, "getting the difference of channel #{channel_id}, which has no state" unless state
|
|
432
|
+
|
|
433
|
+
TL.make("updates.getChannelDifference",
|
|
434
|
+
"force" => false, "channel" => TL.make("inputChannel", "channel_id" => channel_id, "access_hash" => access_hash),
|
|
435
|
+
"filter" => TL.make("channelMessagesFilterEmpty"), "pts" => state.pts,
|
|
436
|
+
"limit" => bot ? BOT_CHANNEL_DIFF_LIMIT : USER_CHANNEL_DIFF_LIMIT)
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
# A channel whose access hash is unknown cannot get its difference: forget its pts, so the next
|
|
440
|
+
# update sets it again instead of asking for the hash over and over.
|
|
441
|
+
def forget_channel(channel_id)
|
|
442
|
+
end_get_diff(channel_id)
|
|
443
|
+
@map.delete(channel_id)
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
# Applies an updates.ChannelDifference. Answers [updates, users, chats].
|
|
447
|
+
def apply_channel_difference(channel_id, diff)
|
|
448
|
+
@possible_gaps.delete(channel_id)
|
|
449
|
+
case diff.name
|
|
450
|
+
when "updates.channelDifferenceEmpty"
|
|
451
|
+
end_get_diff(channel_id)
|
|
452
|
+
@map[channel_id].pts = diff["pts"]
|
|
453
|
+
[[], [], []]
|
|
454
|
+
when "updates.channelDifferenceTooLong"
|
|
455
|
+
pts = diff["dialog"]["pts"]
|
|
456
|
+
@map[channel_id].pts = pts if pts
|
|
457
|
+
reset_channel_deadline(channel_id, diff["timeout"])
|
|
458
|
+
# Its "latest messages" would be a partial answer to what was asked; nothing is given instead.
|
|
459
|
+
[[], [], []]
|
|
460
|
+
else
|
|
461
|
+
end_get_diff(channel_id) if diff["final"]
|
|
462
|
+
@map[channel_id].pts = diff["pts"]
|
|
463
|
+
updates = difference_updates(diff["other_updates"], [channel_id])
|
|
464
|
+
diff["new_messages"].each do |m|
|
|
465
|
+
updates << TL.make("updateNewChannelMessage", "message" => m, "pts" => NO_SEQ, "pts_count" => NO_SEQ)
|
|
466
|
+
end
|
|
467
|
+
reset_channel_deadline(channel_id, nil)
|
|
468
|
+
[updates, diff["users"], diff["chats"]]
|
|
469
|
+
end
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
# Ends a channel's difference early: :temporary (server trouble: retry later, pts unchanged) or
|
|
473
|
+
# :banned (the account can no longer read it: forget it).
|
|
474
|
+
def end_channel_difference(channel_id, reason)
|
|
475
|
+
@possible_gaps.delete(channel_id)
|
|
476
|
+
end_get_diff(channel_id)
|
|
477
|
+
@map.delete(channel_id) if reason == :banned
|
|
478
|
+
end
|
|
479
|
+
end
|
|
480
|
+
end
|
|
481
|
+
end
|