From 57afc246eeaf4a99d66f9325e8d9b77373ab0be8 Mon Sep 17 00:00:00 2001 From: Anton Grigoryev Date: Wed, 25 Feb 2015 18:30:03 +0300 Subject: [PATCH] Send and receive messages successfully. TL schema parsing TODO: dynamic methods and constructors generator --- mtproto.py | 137 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 122 insertions(+), 15 deletions(-) diff --git a/mtproto.py b/mtproto.py index 69d825cc..ac847d01 100644 --- a/mtproto.py +++ b/mtproto.py @@ -6,19 +6,126 @@ Created on Tue Sep 2 19:26:15 2014 """ import binascii import struct +import socket +import re +from datetime import datetime +import sys -def sendpacket(socket, data, number): - step1 = (len(data)+12).to_bytes(4,'little') + number.to_bytes(4,'little') + data - step2 = step1 + binascii.crc32(step1).to_bytes(4,'little') - socket.send(step2) - -def recvpacket(socket): - packet_length_data = socket.recv(4) - if len(packet_length_data)>0: - packet_length = struct.unpack(")?") + res = [] + for s in arg_re.findall(arg_string): + d = {'name': s[0], 'type': s[1]} + if s[2]: + d['subtype'] = s[3] + res.append(d) + return res + + +class TL: + def __init__(self): + self.func_dict_id = {} + self.func_dict_name = {} + self.obj_dict_id = {} + self.obj_dict_name = {} + + # Read constructors + + f = open("TL_schema", 'r') + for line in f: + if line.startswith("---functions---"): + break + try: + z = TlElement(line) + self.obj_dict_id[z.id] = z + self.obj_dict_name[z.name] = z + except SyntaxError: + pass + + # Read methods + + for line in f: + if line.startswith("---functions---"): + break + try: + z = TlElement(line) + self.func_dict_id[z.id] = z + self.func_dict_name[z.name] = z + except SyntaxError: + pass + + def tl_serialize(self, elem, kwargs): + assert isinstance(elem, TlElement) + for arg in elem.args: + pass + return struct.pack(" 0: # if we have smth. in the socket + packet_length = struct.unpack("