2018-06-09 14:48:27 +03:00
|
|
|
import logging
|
2017-09-27 22:06:01 +03:00
|
|
|
import struct
|
|
|
|
|
2018-06-09 14:11:49 +03:00
|
|
|
from .tlmessage import TLMessage
|
2018-06-09 14:48:27 +03:00
|
|
|
from ..tlobject import TLObject
|
|
|
|
|
|
|
|
__log__ = logging.getLogger(__name__)
|
2017-09-25 21:52:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
class MessageContainer(TLObject):
|
2017-09-29 14:11:33 +03:00
|
|
|
CONSTRUCTOR_ID = 0x73f1f8dc
|
2017-09-25 21:52:27 +03:00
|
|
|
|
2017-09-27 22:01:20 +03:00
|
|
|
def __init__(self, messages):
|
2017-09-25 21:52:27 +03:00
|
|
|
super().__init__()
|
|
|
|
self.content_related = False
|
2017-09-27 22:01:20 +03:00
|
|
|
self.messages = messages
|
2017-09-25 21:52:27 +03:00
|
|
|
|
2017-10-29 22:13:00 +03:00
|
|
|
def to_dict(self, recursive=True):
|
|
|
|
return {
|
|
|
|
'content_related': self.content_related,
|
|
|
|
'messages':
|
|
|
|
([] if self.messages is None else [
|
|
|
|
None if x is None else x.to_dict() for x in self.messages
|
|
|
|
]) if recursive else self.messages,
|
|
|
|
}
|
|
|
|
|
2017-10-17 20:54:24 +03:00
|
|
|
def __bytes__(self):
|
2017-09-27 22:06:01 +03:00
|
|
|
return struct.pack(
|
2017-09-29 14:11:33 +03:00
|
|
|
'<Ii', MessageContainer.CONSTRUCTOR_ID, len(self.messages)
|
2017-10-17 20:54:24 +03:00
|
|
|
) + b''.join(bytes(m) for m in self.messages)
|
2017-09-25 21:52:27 +03:00
|
|
|
|
2017-10-29 22:13:00 +03:00
|
|
|
def __str__(self):
|
|
|
|
return TLObject.pretty_format(self)
|
|
|
|
|
|
|
|
def stringify(self):
|
|
|
|
return TLObject.pretty_format(self, indent=0)
|
2018-06-09 14:11:49 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_reader(cls, reader):
|
|
|
|
# This assumes that .read_* calls are done in the order they appear
|
2018-06-09 14:48:27 +03:00
|
|
|
messages = []
|
|
|
|
for _ in range(reader.read_int()):
|
|
|
|
msg_id = reader.read_long()
|
|
|
|
seq_no = reader.read_int()
|
|
|
|
length = reader.read_int()
|
|
|
|
before = reader.tell_position()
|
|
|
|
obj = reader.tgread_object()
|
|
|
|
messages.append(TLMessage(msg_id, seq_no, obj))
|
|
|
|
if reader.tell_position() != before + length:
|
|
|
|
reader.set_position(before)
|
|
|
|
__log__.warning('Data left after TLObject {}: {!r}'
|
|
|
|
.format(obj, reader.read(length)))
|
|
|
|
return MessageContainer(messages)
|