diff --git a/mtproto.py b/mtproto.py index a9f1b136..d077fbbe 100644 --- a/mtproto.py +++ b/mtproto.py @@ -5,57 +5,33 @@ Created on Tue Sep 2 19:26:15 2014 @author: agrigoryev """ from binascii import crc32 -import struct -import socket -import re from datetime import datetime -import sys import io -import configparser +import json +import socket +import struct -current_module = sys.modules[__name__] def vis(bs): - l = len(bs) - n = len(bs) // 8 + """ + Function to visualize byte streams. Split into bytes, print to console. + :param bs: BYTE STRING + """ + symbols_in_one_line = 8 + n = len(bs) // symbols_in_one_line for i in range(n): - print(" ".join(["%02X" % b for b in bs[i*8:i*8+8]])) - print(" ".join(["%02X" % b for b in bs[i*8+8:]])+"\n") - - -class TlElement: - def __init__(self, type_string): - tl_re = re.compile("""([a-z]\w*) #name - \#?([0-9a-f]{8})?\s+ #id - (\{.*\}\s+)? #subtype - (.*)\s+ #arguments list - =\s+([A-Z]\w*) #result - (\s+(\w+))?; #subresult""", re.X) - assert isinstance(type_string, str) - x = tl_re.match(type_string) - if x is not None: - self.name = x.groups()[0] - if x.groups()[1] is not None: - self.id = int(x.groups()[1], 16) - else: - self.id = crc32(re.sub("(#[0-9a-f]{8})|([;\n{}])", "", type_string).encode()) - self.args = self.get_arg_list(x.groups()[3]) - self.result = x.groups()[4] - else: - raise SyntaxError - - @staticmethod - def get_arg_list(arg_string): - arg_re = re.compile("([\w0-9]+)?:?([\w0-9]+)(<([\w0-0]+)>)?") - res = [] - for s in arg_re.findall(arg_string): - d = {'name': s[0], 'type': s[1], 'subtype': s[3] if s[2] is not None else None} - res.append(d) - return res + print(" ".join(["%02X" % b for b in bs[i*symbols_in_one_line:(i+1)*symbols_in_one_line]])) # for every 8 symbols line + print(" ".join(["%02X" % b for b in bs[(i+1)*symbols_in_one_line:]])+"\n") # for last line class TL: def __init__(self): + with open("TL_schema.JSON", 'r') as f: + TL_dict = json.load(f) + + self.methods = TL_dict['methods'] + self.constructors = TL_dict['constructors'] + self.func_dict_id = {} self.func_dict_name = {} self.obj_dict_id = {} @@ -63,83 +39,52 @@ class TL: # 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.result] = z - except SyntaxError: - pass + for elem in self.constructors: + z = TlElement(elem) + self.func_dict_id = {} - # Read methods + def serialize(self, type_, data): + # 1. Type constructor ID + # 2. type costructor params - 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 + # Bare types + if type_ == 'string': + assert isinstance(data, str) + struct.pack(">') - vis(message) - #self.number += 1 - data = self.header(message) + message + vis(message) # Sending message visualisation to console + data = self.header_unencrypted(message) + message step1 = struct.pack(' 0: # if we have smth. in the socket packet_length = struct.unpack("