Module: MTProto::TL::Reader
- Defined in:
- lib/mtproto/tl/reader.rb
Overview
Shared read-only TL primitives — peer/string/bytes/media/document reads and
the schema handle — reused by the message parser (TL::Message) and the other
update parsers. Each method only READS; advancing past a whole object is the
caller's job via Schema#skip, so any unmodelled tail is sized by the schema.
Constant Summary collapse
- SCHEMA_PATH =
File.('../../../data/tl-schema.json', __dir__)
- MESSAGE_REPLY_HEADER =
0x1b97dd66- MEDIA_PHOTO =
0xe216eb63- MEDIA_DOCUMENT =
0x52d8ccd9- DOCUMENT =
0x8fd4c4d8- PHOTO =
0xfb197a65- PHOTO_SIZE =
0x75c78e60- PHOTO_CACHED_SIZE =
0x021e1ad6- PHOTO_SIZE_PROGRESSIVE =
0xfa3efb95- DOCUMENT_ATTRIBUTE_AUDIO =
0x9852f9c6- DOCUMENT_ATTRIBUTE_FILENAME =
0x15590068- SIZES_WITH_WIDTH =
Every size Telegram holds this photo in, by the name upload.getFile asks for it.
A photo, unlike a document, is not fetched whole: the location names which size is wanted, and naming none is answered as if the reference had gone stale — which is what makes a missing size look like an expired one.
Only the sizes with a width can be fetched: the other kinds carry their own bytes inline (a blur, an outline) and have nothing to ask for.
[PHOTO_SIZE, PHOTO_CACHED_SIZE, PHOTO_SIZE_PROGRESSIVE].freeze
Class Method Summary collapse
-
.document_kind(flags) ⇒ Object
messageMediaDocument flags: video flags.6 / round flags.7 / voice flags.8.
-
.parse_document(data, offset) ⇒ Object
When the media is messageMediaDocument, pull the document's downloadable fields (file location + audio metadata); nil for any other media.
-
.parse_document_attributes(data, offset) ⇒ Object
Vector
: pull voice/duration from documentAttributeAudio and the name from documentAttributeFilename; advance over the rest via schema so an unknown attribute never throws off the read. -
.parse_media(data, offset) ⇒ Object
Classify message media into :photo / :video / :audio / :document, or nil.
- .parse_peer(data, offset) ⇒ Object
-
.parse_photo(data, offset) ⇒ Object
When the media is messageMediaPhoto, pull the photo's input reference (id/access_hash/file_reference) so it can be re-sent via inputMediaPhoto — e.g.
-
.parse_reply_to(data, offset) ⇒ Object
messageReplyHeader#1b97dd66: flags, then (in wire order) reply_to_msg_id flags.4?int, reply_to_peer_id flags.0?Peer, reply_from flags.5?MessageFwdHeader, reply_media flags.8?MessageMedia, reply_to_top_id flags.1?int, quote_text flags.6?string, quote_entities flags.7?Vector
, quote_offset flags.10?int. - .photo_sizes(data, offset) ⇒ Object
- .read_tl_bytes(data, offset) ⇒ Object
- .read_tl_string(data, offset) ⇒ Object
- .schema ⇒ Object
Class Method Details
.document_kind(flags) ⇒ Object
messageMediaDocument flags: video flags.6 / round flags.7 / voice flags.8.
104 105 106 107 108 109 |
# File 'lib/mtproto/tl/reader.rb', line 104 def document_kind(flags) return :video if flags.anybits?(1 << 6) || flags.anybits?(1 << 7) return :audio if flags.anybits?(1 << 8) :document end |
.parse_document(data, offset) ⇒ Object
When the media is messageMediaDocument, pull the document's downloadable fields (file location + audio metadata); nil for any other media. The thumb vectors before dc_id are sized by the schema, so this reaches dc_id and the attribute list correctly regardless of thumbnails.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mtproto/tl/reader.rb', line 115 def parse_document(data, offset) return unless data[offset, 4].unpack1('L<') == MEDIA_DOCUMENT media_flags = data[offset + 4, 4].unpack1('L<') return unless media_flags.anybits?(1 << 0) # document present offset += 8 return unless data[offset, 4].unpack1('L<') == DOCUMENT offset += 4 flags = data[offset, 4].unpack1('L<') offset += 4 id = data[offset, 8].unpack1('q<') access_hash = data[offset + 8, 8].unpack1('q<') offset += 16 file_reference, offset = read_tl_bytes(data, offset) offset += 4 # date mime_type, offset = read_tl_string(data, offset) size = data[offset, 8].unpack1('q<') offset += 8 offset = schema.skip_vector(data, offset) if flags.anybits?(1 << 0) # thumbs offset = schema.skip_vector(data, offset) if flags.anybits?(1 << 1) # video_thumbs dc_id = data[offset, 4].unpack1('l<') offset += 4 voice, duration, file_name = parse_document_attributes(data, offset) { id: id, access_hash: access_hash, file_reference: file_reference, dc_id: dc_id, mime_type: mime_type, size: size, voice: voice, duration: duration, file_name: file_name } rescue StandardError nil end |
.parse_document_attributes(data, offset) ⇒ Object
Vector
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/mtproto/tl/reader.rb', line 198 def parse_document_attributes(data, offset) offset += 4 # vector constructor count = data[offset, 4].unpack1('L<') offset += 4 voice = false duration = nil file_name = nil count.times do ctor = data[offset, 4].unpack1('L<') case ctor when DOCUMENT_ATTRIBUTE_AUDIO aflags = data[offset + 4, 4].unpack1('L<') voice ||= aflags.anybits?(1 << 10) duration = data[offset + 8, 4].unpack1('l<') when DOCUMENT_ATTRIBUTE_FILENAME file_name, = read_tl_string(data, offset + 4) end offset = schema.skip(data, offset) end [voice, duration, file_name] end |
.parse_media(data, offset) ⇒ Object
Classify message media into :photo / :video / :audio / :document, or nil.
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mtproto/tl/reader.rb', line 92 def parse_media(data, offset) case data[offset, 4].unpack1('L<') when MEDIA_PHOTO :photo when MEDIA_DOCUMENT document_kind(data[offset + 4, 4].unpack1('L<')) end rescue StandardError nil end |
.parse_peer(data, offset) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mtproto/tl/reader.rb', line 31 def parse_peer(data, offset) constructor = data[offset, 4].unpack1('L<') offset += 4 case constructor when Constructors::PEER_USER [:user, data[offset, 8].unpack1('Q<'), offset + 8] when Constructors::PEER_CHAT [:chat, data[offset, 8].unpack1('Q<'), offset + 8] when Constructors::PEER_CHANNEL [:channel, data[offset, 8].unpack1('Q<'), offset + 8] else raise "Unknown peer constructor: 0x#{constructor.to_s(16)}" end end |
.parse_photo(data, offset) ⇒ Object
When the media is messageMediaPhoto, pull the photo's input reference (id/access_hash/file_reference) so it can be re-sent via inputMediaPhoto — e.g. re-posting a guest-mention's photo, where forwarding is not allowed. has_stickers is a true-flag (no body), so id starts right after photo's flags.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/mtproto/tl/reader.rb', line 151 def parse_photo(data, offset) return unless data[offset, 4].unpack1('L<') == MEDIA_PHOTO media_flags = data[offset + 4, 4].unpack1('L<') return unless media_flags.anybits?(1 << 0) # photo present offset += 8 # messageMediaPhoto ctor + flags return unless data[offset, 4].unpack1('L<') == PHOTO offset += 8 # photo ctor + flags id = data[offset, 8].unpack1('q<') access_hash = data[offset + 8, 8].unpack1('q<') offset += 16 file_reference, offset = read_tl_bytes(data, offset) { id: id, access_hash: access_hash, file_reference: file_reference, sizes: photo_sizes(data, offset + 4) } # +4: date, which precedes the sizes rescue StandardError nil end |
.parse_reply_to(data, offset) ⇒ Object
messageReplyHeader#1b97dd66: flags, then (in wire order) reply_to_msg_id
flags.4?int, reply_to_peer_id flags.0?Peer, reply_from flags.5?MessageFwdHeader,
reply_media flags.8?MessageMedia, reply_to_top_id flags.1?int, quote_text
flags.6?string, quote_entities flags.7?Vector
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/mtproto/tl/reader.rb', line 58 def parse_reply_to(data, offset) return unless data[offset, 4].unpack1('L<') == MESSAGE_REPLY_HEADER io = offset + 4 flags = data[io, 4].unpack1('L<') io += 4 forum_topic = flags.anybits?(1 << 3) msg_id = nil if flags.anybits?(1 << 4) # reply_to_msg_id msg_id = data[io, 4].unpack1('l<') io += 4 end peer_type = peer_id = nil peer_type, peer_id, io = parse_peer(data, io) if flags.anybits?(1 << 0) # reply_to_peer_id io = schema.skip(data, io) if flags.anybits?(1 << 5) # reply_from io = schema.skip(data, io) if flags.anybits?(1 << 8) # reply_media io += 4 if flags.anybits?(1 << 1) # reply_to_top_id quote_text = nil quote_text, io = read_tl_string(data, io) if flags.anybits?(1 << 6) io = schema.skip_vector(data, io) if flags.anybits?(1 << 7) # quote_entities quote_offset = flags.anybits?(1 << 10) ? data[io, 4].unpack1('l<') : nil { msg_id: msg_id, is_reply: forum_topic ? flags.anybits?(1 << 1) : true, is_quote: flags.anybits?(1 << 9), quote_text: quote_text, quote_offset: quote_offset, peer_type: peer_type, peer_id: peer_id } rescue StandardError nil end |
.photo_sizes(data, offset) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/mtproto/tl/reader.rb', line 182 def photo_sizes(data, offset) found = [] schema.skip_vector(data, offset) do |bytes, at| constructor = bytes[at, 4].unpack1('L<') name, after = read_tl_string(bytes, at + 4) found << { type: name, width: bytes[after, 4].unpack1('l<') } if SIZES_WITH_WIDTH.include?(constructor) schema.skip(bytes, at) end found rescue StandardError [] end |
.read_tl_bytes(data, offset) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/mtproto/tl/reader.rb', line 226 def read_tl_bytes(data, offset) first_byte = data.getbyte(offset) if first_byte == 254 len = data.getbyte(offset + 1) | (data.getbyte(offset + 2) << 8) | (data.getbyte(offset + 3) << 16) total = 4 + len else len = first_byte total = 1 + len end padding = (4 - (total % 4)) % 4 [data[offset + (total - len), len], offset + total + padding] end |
.read_tl_string(data, offset) ⇒ Object
221 222 223 224 |
# File 'lib/mtproto/tl/reader.rb', line 221 def read_tl_string(data, offset) bytes, offset = read_tl_bytes(data, offset) [bytes.force_encoding('UTF-8'), offset] end |
.schema ⇒ Object
27 28 29 |
# File 'lib/mtproto/tl/reader.rb', line 27 def schema @schema ||= Schema.new(SCHEMA_PATH) end |