diff --git a/client/src/telethon/__init__.py b/client/src/telethon/__init__.py index 58f3ace6..2e8c599c 100644 --- a/client/src/telethon/__init__.py +++ b/client/src/telethon/__init__.py @@ -1 +1,6 @@ +from ._impl import tl as _tl +from ._impl.client import Client, Config +from ._impl.session import Session from .version import __version__ + +__all__ = ["_tl", "Client", "Config", "Session"] diff --git a/client/src/telethon/_impl/__init__.py b/client/src/telethon/_impl/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/client/src/telethon/_impl/client/__init__.py b/client/src/telethon/_impl/client/__init__.py index e69de29b..c79d3d1e 100644 --- a/client/src/telethon/_impl/client/__init__.py +++ b/client/src/telethon/_impl/client/__init__.py @@ -0,0 +1,3 @@ +from .client import Client, Config + +__all__ = ["Client", "Config"] diff --git a/client/src/telethon/_impl/client/client/__init__.py b/client/src/telethon/_impl/client/client/__init__.py index e69de29b..cdf630dc 100644 --- a/client/src/telethon/_impl/client/client/__init__.py +++ b/client/src/telethon/_impl/client/client/__init__.py @@ -0,0 +1,15 @@ +from .bots import InlineResult, InlineResults +from .client import Client +from .files import File, InFileLike, MediaLike, OutFileLike +from .net import Config + +__all__ = [ + "InlineResult", + "InlineResults", + "Client", + "File", + "InFileLike", + "MediaLike", + "OutFileLike", + "Config", +] diff --git a/client/src/telethon/_impl/client/client/auth.py b/client/src/telethon/_impl/client/client/auth.py index 8abe287d..dc16c142 100644 --- a/client/src/telethon/_impl/client/client/auth.py +++ b/client/src/telethon/_impl/client/client/auth.py @@ -2,13 +2,11 @@ from __future__ import annotations from typing import TYPE_CHECKING, Optional, Union -from ...mtproto.mtp.types import RpcError -from ...session.message_box.defs import Session -from ...session.message_box.defs import User as SessionUser +from ...mtproto import RpcError +from ...session import Session +from ...session import User as SessionUser from ...tl import abcs, functions, types -from ..types.chat.user import User -from ..types.login_token import LoginToken -from ..types.password_token import PasswordToken +from ..types import LoginToken, PasswordToken, User from .net import connect_sender if TYPE_CHECKING: diff --git a/client/src/telethon/_impl/client/client/bots.py b/client/src/telethon/_impl/client/client/bots.py index 5bc11465..3cec194d 100644 --- a/client/src/telethon/_impl/client/client/bots.py +++ b/client/src/telethon/_impl/client/client/bots.py @@ -2,10 +2,8 @@ from __future__ import annotations from typing import TYPE_CHECKING, AsyncIterator, List, Optional, Self, Union -from ...._impl.tl import abcs, functions, types -from ..types.chat import ChatLike -from ..types.message import Message -from ..types.meta import NoPublicConstructor +from ...tl import abcs, functions, types +from ..types import ChatLike, Message, NoPublicConstructor from ..utils import generate_random_id if TYPE_CHECKING: diff --git a/client/src/telethon/_impl/client/client/client.py b/client/src/telethon/_impl/client/client/client.py index da493503..bfd1fb97 100644 --- a/client/src/telethon/_impl/client/client/client.py +++ b/client/src/telethon/_impl/client/client/client.py @@ -15,19 +15,10 @@ from typing import ( Union, ) -from ...mtsender.sender import Sender -from ...session.chat.hash_cache import ChatHashCache -from ...session.chat.packed import PackedChat -from ...session.message_box.defs import Session -from ...session.message_box.messagebox import MessageBox -from ...tl import abcs -from ...tl.core.request import Request -from ..types.async_list import AsyncList -from ..types.chat import ChatLike -from ..types.chat.user import User -from ..types.login_token import LoginToken -from ..types.message import Message -from ..types.password_token import PasswordToken +from ...mtsender import Sender +from ...session import ChatHashCache, MessageBox, PackedChat, Session +from ...tl import Request, abcs +from ..types import AsyncList, ChatLike, LoginToken, Message, PasswordToken, User from .account import edit_2fa, end_takeout, takeout from .auth import ( bot_sign_in, diff --git a/client/src/telethon/_impl/client/client/files.py b/client/src/telethon/_impl/client/client/files.py index 27184c98..8ae9dfd0 100644 --- a/client/src/telethon/_impl/client/client/files.py +++ b/client/src/telethon/_impl/client/client/files.py @@ -7,9 +7,7 @@ from pathlib import Path from typing import TYPE_CHECKING, Any, Coroutine, List, Optional, Protocol, Self, Union from ...tl import abcs, functions, types -from ..types.chat import ChatLike -from ..types.message import Message -from ..types.meta import NoPublicConstructor +from ..types import ChatLike, Message, NoPublicConstructor from ..utils import generate_random_id from .messages import parse_message diff --git a/client/src/telethon/_impl/client/client/messages.py b/client/src/telethon/_impl/client/client/messages.py index 3dd3c309..90ae735e 100644 --- a/client/src/telethon/_impl/client/client/messages.py +++ b/client/src/telethon/_impl/client/client/messages.py @@ -2,25 +2,12 @@ from __future__ import annotations import datetime import sys -from typing import ( - TYPE_CHECKING, - Any, - Coroutine, - Dict, - List, - Literal, - Optional, - Tuple, - Union, -) - -from telethon._impl.client.types.async_list import AsyncList -from telethon._impl.session.chat.packed import PackedChat +from typing import TYPE_CHECKING, Dict, List, Literal, Optional, Tuple, Union +from ...session import PackedChat from ...tl import abcs, functions, types from ..parsers import parse_html_message, parse_markdown_message -from ..types.chat import ChatLike -from ..types.message import Message +from ..types import AsyncList, ChatLike, Message from ..utils import generate_random_id if TYPE_CHECKING: diff --git a/client/src/telethon/_impl/client/client/net.py b/client/src/telethon/_impl/client/client/net.py index 652b18c3..705e7269 100644 --- a/client/src/telethon/_impl/client/client/net.py +++ b/client/src/telethon/_impl/client/client/net.py @@ -7,14 +7,12 @@ from dataclasses import dataclass, field from typing import TYPE_CHECKING, Optional, TypeVar from ....version import __version__ -from ...mtproto.mtp.types import RpcError -from ...mtproto.transport.full import Full -from ...mtsender.sender import Sender -from ...mtsender.sender import connect as connect_without_auth -from ...mtsender.sender import connect_with_auth -from ...session.message_box.defs import DataCenter, Session -from ...tl import LAYER, functions -from ...tl.core.request import Request +from ...mtproto import Full, RpcError +from ...mtsender import Sender +from ...mtsender import connect as connect_without_auth +from ...mtsender import connect_with_auth +from ...session import DataCenter, Session +from ...tl import LAYER, Request, functions if TYPE_CHECKING: from .client import Client diff --git a/client/src/telethon/_impl/client/client/users.py b/client/src/telethon/_impl/client/client/users.py index a1694531..2fe9012b 100644 --- a/client/src/telethon/_impl/client/client/users.py +++ b/client/src/telethon/_impl/client/client/users.py @@ -2,9 +2,9 @@ from __future__ import annotations from typing import TYPE_CHECKING, Optional -from ...session.chat.packed import PackedChat, PackedType +from ...session import PackedChat, PackedType from ...tl import abcs, types -from ..types.chat import Channel, ChatLike, Group, User +from ..types import Channel, ChatLike, Group, User if TYPE_CHECKING: from .client import Client diff --git a/client/src/telethon/_impl/client/types/__init__.py b/client/src/telethon/_impl/client/types/__init__.py index e69de29b..e6a3dd3d 100644 --- a/client/src/telethon/_impl/client/types/__init__.py +++ b/client/src/telethon/_impl/client/types/__init__.py @@ -0,0 +1,20 @@ +from .async_list import AsyncList +from .chat import Channel, Chat, ChatLike, Group, RestrictionReason, User +from .login_token import LoginToken +from .message import Message +from .meta import NoPublicConstructor +from .password_token import PasswordToken + +__all__ = [ + "AsyncList", + "Channel", + "Chat", + "ChatLike", + "Group", + "RestrictionReason", + "User", + "LoginToken", + "Message", + "NoPublicConstructor", + "PasswordToken", +] diff --git a/client/src/telethon/_impl/client/types/chat/__init__.py b/client/src/telethon/_impl/client/types/chat/__init__.py index 4e6f89fc..4be77fe0 100644 --- a/client/src/telethon/_impl/client/types/chat/__init__.py +++ b/client/src/telethon/_impl/client/types/chat/__init__.py @@ -1,6 +1,6 @@ from typing import Union -from ....session.chat.packed import PackedChat +from ....session import PackedChat from ....tl import abcs from .channel import Channel from .group import Group @@ -9,4 +9,4 @@ from .user import RestrictionReason, User Chat = Union[Channel, Group, User] ChatLike = Union[Chat, PackedChat, int, str, abcs.InputPeer] -__all__ = ["Chat", "Channel", "Group", "RestrictionReason", "User"] +__all__ = ["Chat", "ChatLike", "Channel", "Group", "RestrictionReason", "User"] diff --git a/client/src/telethon/_impl/client/types/chat/channel.py b/client/src/telethon/_impl/client/types/chat/channel.py index c258cb5e..56bd00fe 100644 --- a/client/src/telethon/_impl/client/types/chat/channel.py +++ b/client/src/telethon/_impl/client/types/chat/channel.py @@ -1,6 +1,6 @@ from typing import Optional, Self, Union -from ....session.chat.packed import PackedChat, PackedType +from ....session import PackedChat, PackedType from ....tl import abcs, types from ..meta import NoPublicConstructor diff --git a/client/src/telethon/_impl/client/types/chat/group.py b/client/src/telethon/_impl/client/types/chat/group.py index a16e8081..f1c2a818 100644 --- a/client/src/telethon/_impl/client/types/chat/group.py +++ b/client/src/telethon/_impl/client/types/chat/group.py @@ -1,6 +1,6 @@ from typing import Optional, Self, Union -from ....session.chat.packed import PackedChat, PackedType +from ....session import PackedChat, PackedType from ....tl import abcs, types from ..meta import NoPublicConstructor diff --git a/client/src/telethon/_impl/client/types/chat/user.py b/client/src/telethon/_impl/client/types/chat/user.py index b39068b7..a4ede133 100644 --- a/client/src/telethon/_impl/client/types/chat/user.py +++ b/client/src/telethon/_impl/client/types/chat/user.py @@ -1,6 +1,6 @@ from typing import List, Optional, Self -from ....session.chat.packed import PackedChat, PackedType +from ....session import PackedChat, PackedType from ....tl import abcs, types from ..meta import NoPublicConstructor diff --git a/client/src/telethon/_impl/client/types/login_token.py b/client/src/telethon/_impl/client/types/login_token.py index 24a0be0c..c00bdd2a 100644 --- a/client/src/telethon/_impl/client/types/login_token.py +++ b/client/src/telethon/_impl/client/types/login_token.py @@ -1,7 +1,6 @@ from typing import Self -from telethon._impl.tl import types - +from ...tl import types from .meta import NoPublicConstructor diff --git a/client/src/telethon/_impl/client/types/message.py b/client/src/telethon/_impl/client/types/message.py index 06a88eac..b4a57f3e 100644 --- a/client/src/telethon/_impl/client/types/message.py +++ b/client/src/telethon/_impl/client/types/message.py @@ -1,8 +1,8 @@ import datetime from typing import Optional, Self -from ...client.types.chat import Chat from ...tl import abcs, types +from .chat import Chat from .meta import NoPublicConstructor diff --git a/client/src/telethon/_impl/client/types/password_token.py b/client/src/telethon/_impl/client/types/password_token.py index 5cc57bbf..a601ad4d 100644 --- a/client/src/telethon/_impl/client/types/password_token.py +++ b/client/src/telethon/_impl/client/types/password_token.py @@ -1,7 +1,6 @@ from typing import Self -from telethon._impl.tl import types - +from ...tl import types from .meta import NoPublicConstructor diff --git a/client/src/telethon/_impl/crypto/__init__.py b/client/src/telethon/_impl/crypto/__init__.py index 420e44ba..af3e9cf5 100644 --- a/client/src/telethon/_impl/crypto/__init__.py +++ b/client/src/telethon/_impl/crypto/__init__.py @@ -1,115 +1,26 @@ -import os -from collections import namedtuple -from enum import IntEnum -from hashlib import sha1, sha256 - -from .aes import ige_decrypt, ige_encrypt from .auth_key import AuthKey +from .crypto import ( + Side, + calc_key, + decrypt_data_v2, + decrypt_ige, + encrypt_data_v2, + encrypt_ige, + generate_key_data_from_nonce, +) +from .factorize import factorize +from .rsa import RSA_KEYS, encrypt_hashed - -# "where x = 0 for messages from client to server and x = 8 for those from server to client" -class Side(IntEnum): - CLIENT = 0 - SERVER = 8 - - -CalcKey = namedtuple("CalcKey", ("key", "iv")) - - -# https://core.telegram.org/mtproto/description#defining-aes-key-and-initialization-vector -def calc_key(auth_key: AuthKey, msg_key: bytes, side: Side) -> CalcKey: - x = int(side) - - # sha256_a = SHA256 (msg_key + substr (auth_key, x, 36)) - sha256_a = sha256(msg_key + auth_key.data[x : x + 36]).digest() - - # sha256_b = SHA256 (substr (auth_key, 40+x, 36) + msg_key) - sha256_b = sha256(auth_key.data[x + 40 : x + 76] + msg_key).digest() - - # aes_key = substr (sha256_a, 0, 8) + substr (sha256_b, 8, 16) + substr (sha256_a, 24, 8) - aes_key = sha256_a[:8] + sha256_b[8:24] + sha256_a[24:32] - - # aes_iv = substr (sha256_b, 0, 8) + substr (sha256_a, 8, 16) + substr (sha256_b, 24, 8) - aes_iv = sha256_b[:8] + sha256_a[8:24] + sha256_b[24:32] - - return CalcKey(aes_key, aes_iv) - - -def determine_padding_v2_length(length: int) -> int: - return 16 + (16 - (length % 16)) - - -def _do_encrypt_data_v2( - plaintext: bytes, auth_key: AuthKey, random_padding: bytes -) -> bytes: - padded_plaintext = ( - plaintext + random_padding[: determine_padding_v2_length(len(plaintext))] - ) - - side = Side.CLIENT - x = int(side) - - # msg_key_large = SHA256 (substr (auth_key, 88+x, 32) + plaintext + random_padding) - msg_key_large = sha256(auth_key.data[x + 88 : x + 120] + padded_plaintext).digest() - - # msg_key = substr (msg_key_large, 8, 16) - msg_key = msg_key_large[8:24] - - key, iv = calc_key(auth_key, msg_key, side) - ciphertext = ige_encrypt(padded_plaintext, key, iv) - - return auth_key.key_id + msg_key + ciphertext - - -def encrypt_data_v2(plaintext: bytes, auth_key: AuthKey) -> bytes: - random_padding = os.urandom(32) - return _do_encrypt_data_v2(plaintext, auth_key, random_padding) - - -def decrypt_data_v2(ciphertext: bytes, auth_key: AuthKey) -> bytes: - side = Side.SERVER - x = int(side) - - if len(ciphertext) < 24 or (len(ciphertext) - 24) % 16 != 0: - raise ValueError("invalid ciphertext buffer length") - - # TODO Check salt, session_id and sequence_number - key_id = ciphertext[:8] - if auth_key.key_id != key_id: - raise ValueError("server authkey mismatches with ours") - - msg_key = ciphertext[8:24] - key, iv = calc_key(auth_key, msg_key, side) - plaintext = ige_decrypt(ciphertext[24:], key, iv) - - # https://core.telegram.org/mtproto/security_guidelines#mtproto-encrypted-messages - our_key = sha256(auth_key.data[x + 88 : x + 120] + plaintext).digest() - if msg_key != our_key[8:24]: - raise ValueError("server msgkey mismatches with ours") - - return plaintext - - -def generate_key_data_from_nonce(server_nonce: int, new_nonce: int) -> CalcKey: - server_bytes = server_nonce.to_bytes(16) - new_bytes = new_nonce.to_bytes(32) - hash1 = sha1(new_bytes + server_bytes).digest() - hash2 = sha1(server_bytes + new_bytes).digest() - hash3 = sha1(new_bytes + new_bytes).digest() - - key = hash1 + hash2[:12] - iv = hash2[12:20] + hash3 + new_bytes[:4] - return CalcKey(key, iv) - - -def encrypt_ige(plaintext: bytes, key: bytes, iv: bytes) -> bytes: - if len(plaintext) % 16 != 0: - plaintext += os.urandom((16 - (len(plaintext) % 16)) % 16) - return ige_encrypt(plaintext, key, iv) - - -def decrypt_ige(padded_ciphertext: bytes, key: bytes, iv: bytes) -> bytes: - return ige_decrypt(padded_ciphertext, key, iv) - - -__all__ = ["AuthKey", "encrypt_data_v2", "decrypt_data_v2"] +__all__ = [ + "AuthKey", + "Side", + "calc_key", + "decrypt_data_v2", + "decrypt_ige", + "encrypt_data_v2", + "encrypt_ige", + "generate_key_data_from_nonce", + "factorize", + "RSA_KEYS", + "encrypt_hashed", +] diff --git a/client/src/telethon/_impl/crypto/crypto.py b/client/src/telethon/_impl/crypto/crypto.py new file mode 100644 index 00000000..e7213515 --- /dev/null +++ b/client/src/telethon/_impl/crypto/crypto.py @@ -0,0 +1,112 @@ +import os +from collections import namedtuple +from enum import IntEnum +from hashlib import sha1, sha256 + +from .aes import ige_decrypt, ige_encrypt +from .auth_key import AuthKey + + +# "where x = 0 for messages from client to server and x = 8 for those from server to client" +class Side(IntEnum): + CLIENT = 0 + SERVER = 8 + + +CalcKey = namedtuple("CalcKey", ("key", "iv")) + + +# https://core.telegram.org/mtproto/description#defining-aes-key-and-initialization-vector +def calc_key(auth_key: AuthKey, msg_key: bytes, side: Side) -> CalcKey: + x = int(side) + + # sha256_a = SHA256 (msg_key + substr (auth_key, x, 36)) + sha256_a = sha256(msg_key + auth_key.data[x : x + 36]).digest() + + # sha256_b = SHA256 (substr (auth_key, 40+x, 36) + msg_key) + sha256_b = sha256(auth_key.data[x + 40 : x + 76] + msg_key).digest() + + # aes_key = substr (sha256_a, 0, 8) + substr (sha256_b, 8, 16) + substr (sha256_a, 24, 8) + aes_key = sha256_a[:8] + sha256_b[8:24] + sha256_a[24:32] + + # aes_iv = substr (sha256_b, 0, 8) + substr (sha256_a, 8, 16) + substr (sha256_b, 24, 8) + aes_iv = sha256_b[:8] + sha256_a[8:24] + sha256_b[24:32] + + return CalcKey(aes_key, aes_iv) + + +def determine_padding_v2_length(length: int) -> int: + return 16 + (16 - (length % 16)) + + +def _do_encrypt_data_v2( + plaintext: bytes, auth_key: AuthKey, random_padding: bytes +) -> bytes: + padded_plaintext = ( + plaintext + random_padding[: determine_padding_v2_length(len(plaintext))] + ) + + side = Side.CLIENT + x = int(side) + + # msg_key_large = SHA256 (substr (auth_key, 88+x, 32) + plaintext + random_padding) + msg_key_large = sha256(auth_key.data[x + 88 : x + 120] + padded_plaintext).digest() + + # msg_key = substr (msg_key_large, 8, 16) + msg_key = msg_key_large[8:24] + + key, iv = calc_key(auth_key, msg_key, side) + ciphertext = ige_encrypt(padded_plaintext, key, iv) + + return auth_key.key_id + msg_key + ciphertext + + +def encrypt_data_v2(plaintext: bytes, auth_key: AuthKey) -> bytes: + random_padding = os.urandom(32) + return _do_encrypt_data_v2(plaintext, auth_key, random_padding) + + +def decrypt_data_v2(ciphertext: bytes, auth_key: AuthKey) -> bytes: + side = Side.SERVER + x = int(side) + + if len(ciphertext) < 24 or (len(ciphertext) - 24) % 16 != 0: + raise ValueError("invalid ciphertext buffer length") + + # TODO Check salt, session_id and sequence_number + key_id = ciphertext[:8] + if auth_key.key_id != key_id: + raise ValueError("server authkey mismatches with ours") + + msg_key = ciphertext[8:24] + key, iv = calc_key(auth_key, msg_key, side) + plaintext = ige_decrypt(ciphertext[24:], key, iv) + + # https://core.telegram.org/mtproto/security_guidelines#mtproto-encrypted-messages + our_key = sha256(auth_key.data[x + 88 : x + 120] + plaintext).digest() + if msg_key != our_key[8:24]: + raise ValueError("server msgkey mismatches with ours") + + return plaintext + + +def generate_key_data_from_nonce(server_nonce: int, new_nonce: int) -> CalcKey: + server_bytes = server_nonce.to_bytes(16) + new_bytes = new_nonce.to_bytes(32) + hash1 = sha1(new_bytes + server_bytes).digest() + hash2 = sha1(server_bytes + new_bytes).digest() + hash3 = sha1(new_bytes + new_bytes).digest() + + key = hash1 + hash2[:12] + iv = hash2[12:20] + hash3 + new_bytes[:4] + return CalcKey(key, iv) + + +def encrypt_ige(plaintext: bytes, key: bytes, iv: bytes) -> bytes: + if len(plaintext) % 16 != 0: + plaintext += os.urandom((16 - (len(plaintext) % 16)) % 16) + return ige_encrypt(plaintext, key, iv) + + +def decrypt_ige(padded_ciphertext: bytes, key: bytes, iv: bytes) -> bytes: + return ige_decrypt(padded_ciphertext, key, iv) diff --git a/client/src/telethon/_impl/mtproto/__init__.py b/client/src/telethon/_impl/mtproto/__init__.py index e69de29b..94a723ce 100644 --- a/client/src/telethon/_impl/mtproto/__init__.py +++ b/client/src/telethon/_impl/mtproto/__init__.py @@ -0,0 +1,31 @@ +from .authentication import CreatedKey, Step1, Step2, Step3, create_key +from .authentication import step1 as auth_step1 +from .authentication import step2 as auth_step2 +from .authentication import step3 as auth_step3 +from .mtp import BadMessage, Deserialization, Encrypted, MsgId, Mtp, Plain, RpcError +from .transport import Abridged, Full, Intermediate, MissingBytes, Transport +from .utils import DEFAULT_COMPRESSION_THRESHOLD + +__all__ = [ + "CreatedKey", + "Step1", + "Step2", + "Step3", + "create_key", + "auth_step1", + "auth_step2", + "auth_step3", + "BadMessage", + "Deserialization", + "Encrypted", + "MsgId", + "Mtp", + "Plain", + "RpcError", + "Abridged", + "Full", + "Intermediate", + "MissingBytes", + "Transport", + "DEFAULT_COMPRESSION_THRESHOLD", +] diff --git a/client/src/telethon/_impl/mtproto/authentication.py b/client/src/telethon/_impl/mtproto/authentication.py index 15896c31..cdc1af31 100644 --- a/client/src/telethon/_impl/mtproto/authentication.py +++ b/client/src/telethon/_impl/mtproto/authentication.py @@ -5,12 +5,16 @@ from dataclasses import dataclass from hashlib import sha1 from typing import Tuple -from telethon._impl.crypto import decrypt_ige, encrypt_ige, generate_key_data_from_nonce -from telethon._impl.crypto.auth_key import AuthKey -from telethon._impl.crypto.factorize import factorize -from telethon._impl.crypto.rsa import RSA_KEYS, encrypt_hashed -from telethon._impl.tl.core.reader import Reader - +from ..crypto import ( + RSA_KEYS, + AuthKey, + decrypt_ige, + encrypt_hashed, + encrypt_ige, + factorize, + generate_key_data_from_nonce, +) +from ..tl.core import Reader from ..tl.mtproto.abcs import ServerDhInnerData as AbcServerDhInnerData from ..tl.mtproto.abcs import ServerDhParams, SetClientDhParamsAnswer from ..tl.mtproto.functions import req_dh_params, req_pq_multi, set_client_dh_params diff --git a/client/src/telethon/_impl/mtproto/mtp/__init__.py b/client/src/telethon/_impl/mtproto/mtp/__init__.py index 7084a00b..64c6424f 100644 --- a/client/src/telethon/_impl/mtproto/mtp/__init__.py +++ b/client/src/telethon/_impl/mtproto/mtp/__init__.py @@ -1,10 +1,11 @@ from .encrypted import Encrypted from .plain import Plain -from .types import Deserialization, MsgId, Mtp, RpcError +from .types import BadMessage, Deserialization, MsgId, Mtp, RpcError __all__ = [ "Encrypted", "Plain", + "BadMessage", "Deserialization", "MsgId", "Mtp", diff --git a/client/src/telethon/_impl/mtproto/mtp/encrypted.py b/client/src/telethon/_impl/mtproto/mtp/encrypted.py index ec23a277..2f39ce28 100644 --- a/client/src/telethon/_impl/mtproto/mtp/encrypted.py +++ b/client/src/telethon/_impl/mtproto/mtp/encrypted.py @@ -1,10 +1,10 @@ import os import struct import time -from typing import List, Optional, Tuple, Union +from typing import List, Optional, Tuple from ...crypto import AuthKey, decrypt_data_v2, encrypt_data_v2 -from ...tl.core.reader import Reader +from ...tl.core import Reader from ...tl.mtproto.abcs import BadMsgNotification as AbcBadMsgNotification from ...tl.mtproto.abcs import DestroySessionRes from ...tl.mtproto.abcs import MsgDetailedInfo as AbcMsgDetailedInfo diff --git a/client/src/telethon/_impl/mtproto/mtp/types.py b/client/src/telethon/_impl/mtproto/mtp/types.py index 6c76e8f5..8c2f6a2e 100644 --- a/client/src/telethon/_impl/mtproto/mtp/types.py +++ b/client/src/telethon/_impl/mtproto/mtp/types.py @@ -1,7 +1,7 @@ import re from abc import ABC, abstractmethod from dataclasses import dataclass -from typing import List, NewType, Optional, Self, Tuple, Union +from typing import List, NewType, Optional, Self, Tuple from ...tl.mtproto.types import RpcError as GeneratedRpcError diff --git a/client/src/telethon/_impl/mtproto/transport/__init__.py b/client/src/telethon/_impl/mtproto/transport/__init__.py index 31ab0028..e3120625 100644 --- a/client/src/telethon/_impl/mtproto/transport/__init__.py +++ b/client/src/telethon/_impl/mtproto/transport/__init__.py @@ -1,3 +1,6 @@ -from .abcs import Transport +from .abcs import MissingBytes, Transport +from .abridged import Abridged +from .full import Full +from .intermediate import Intermediate -__all__ = ["Transport"] +__all__ = ["MissingBytes", "Transport", "Abridged", "Full", "Intermediate"] diff --git a/client/src/telethon/_impl/mtsender/__init__.py b/client/src/telethon/_impl/mtsender/__init__.py index e69de29b..5b2c5ed6 100644 --- a/client/src/telethon/_impl/mtsender/__init__.py +++ b/client/src/telethon/_impl/mtsender/__init__.py @@ -0,0 +1,17 @@ +from .sender import ( + MAXIMUM_DATA, + NO_PING_DISCONNECT, + PING_DELAY, + Sender, + connect, + connect_with_auth, +) + +__all__ = [ + "MAXIMUM_DATA", + "NO_PING_DISCONNECT", + "PING_DELAY", + "Sender", + "connect", + "connect_with_auth", +] diff --git a/client/src/telethon/_impl/mtsender/sender.py b/client/src/telethon/_impl/mtsender/sender.py index 7a327a60..d3ade99a 100644 --- a/client/src/telethon/_impl/mtsender/sender.py +++ b/client/src/telethon/_impl/mtsender/sender.py @@ -6,14 +6,20 @@ from asyncio import FIRST_COMPLETED, Event, Future, StreamReader, StreamWriter from dataclasses import dataclass from typing import Generic, List, Optional, Self, TypeVar -from ..crypto.auth_key import AuthKey -from ..mtproto import authentication -from ..mtproto.mtp.encrypted import Encrypted -from ..mtproto.mtp.plain import Plain -from ..mtproto.mtp.types import BadMessage, MsgId, Mtp, RpcError -from ..mtproto.transport.abcs import MissingBytes, Transport +from ..crypto import AuthKey +from ..mtproto import ( + BadMessage, + Encrypted, + MissingBytes, + MsgId, + Mtp, + Plain, + RpcError, + Transport, + authentication, +) +from ..tl import Request as RemoteCall from ..tl.abcs import Updates -from ..tl.core.request import Request as RemoteCall from ..tl.mtproto.functions import ping_delay_disconnect MAXIMUM_DATA = (1024 * 1024) + (8 * 1024) diff --git a/client/src/telethon/_impl/session/__init__.py b/client/src/telethon/_impl/session/__init__.py index e69de29b..9bf52f07 100644 --- a/client/src/telethon/_impl/session/__init__.py +++ b/client/src/telethon/_impl/session/__init__.py @@ -0,0 +1,37 @@ +from .chat import ChatHashCache, PackedChat, PackedType +from .message_box import ( + BOT_CHANNEL_DIFF_LIMIT, + NO_UPDATES_TIMEOUT, + USER_CHANNEL_DIFF_LIMIT, + ChannelState, + DataCenter, + Gap, + MessageBox, + PossibleGap, + PrematureEndReason, + PtsInfo, + Session, + State, + UpdateState, + User, +) + +__all__ = [ + "ChatHashCache", + "PackedChat", + "PackedType", + "BOT_CHANNEL_DIFF_LIMIT", + "NO_UPDATES_TIMEOUT", + "USER_CHANNEL_DIFF_LIMIT", + "ChannelState", + "DataCenter", + "Gap", + "PossibleGap", + "PrematureEndReason", + "PtsInfo", + "Session", + "State", + "UpdateState", + "User", + "MessageBox", +] diff --git a/client/src/telethon/_impl/session/chat/packed.py b/client/src/telethon/_impl/session/chat/packed.py index b6c806a6..2097ec0e 100644 --- a/client/src/telethon/_impl/session/chat/packed.py +++ b/client/src/telethon/_impl/session/chat/packed.py @@ -2,7 +2,7 @@ import struct from enum import Enum from typing import Optional, Self -from telethon._impl.tl import abcs, types +from ...tl import abcs, types class PackedType(Enum): diff --git a/client/src/telethon/_impl/session/message_box/__init__.py b/client/src/telethon/_impl/session/message_box/__init__.py index e69de29b..49cc5722 100644 --- a/client/src/telethon/_impl/session/message_box/__init__.py +++ b/client/src/telethon/_impl/session/message_box/__init__.py @@ -0,0 +1,33 @@ +from .defs import ( + BOT_CHANNEL_DIFF_LIMIT, + NO_UPDATES_TIMEOUT, + USER_CHANNEL_DIFF_LIMIT, + ChannelState, + DataCenter, + Gap, + PossibleGap, + PrematureEndReason, + PtsInfo, + Session, + State, + UpdateState, + User, +) +from .messagebox import MessageBox + +__all__ = [ + "BOT_CHANNEL_DIFF_LIMIT", + "NO_UPDATES_TIMEOUT", + "USER_CHANNEL_DIFF_LIMIT", + "ChannelState", + "DataCenter", + "Gap", + "PossibleGap", + "PrematureEndReason", + "PtsInfo", + "Session", + "State", + "UpdateState", + "User", + "MessageBox", +] diff --git a/client/src/telethon/_impl/session/message_box/adaptor.py b/client/src/telethon/_impl/session/message_box/adaptor.py index 6bd585f4..e4c21865 100644 --- a/client/src/telethon/_impl/session/message_box/adaptor.py +++ b/client/src/telethon/_impl/session/message_box/adaptor.py @@ -1,7 +1,7 @@ from typing import Optional from ...tl import abcs, types -from ..chat.hash_cache import ChatHashCache +from ..chat import ChatHashCache from .defs import ENTRY_ACCOUNT, ENTRY_SECRET, NO_SEQ, Gap, PtsInfo diff --git a/client/src/telethon/_impl/session/message_box/messagebox.py b/client/src/telethon/_impl/session/message_box/messagebox.py index a5f6ed58..c1a4adcb 100644 --- a/client/src/telethon/_impl/session/message_box/messagebox.py +++ b/client/src/telethon/_impl/session/message_box/messagebox.py @@ -4,9 +4,8 @@ import logging import time from typing import Dict, List, Optional, Set, Tuple -from ...tl import abcs, functions, types -from ...tl.core.request import Request -from ..chat.hash_cache import ChatHashCache +from ...tl import Request, abcs, functions, types +from ..chat import ChatHashCache from .adaptor import adapt, pts_info_from_update from .defs import ( BOT_CHANNEL_DIFF_LIMIT, diff --git a/client/src/telethon/_impl/tl/__init__.py b/client/src/telethon/_impl/tl/__init__.py index ce48616f..89c247e4 100644 --- a/client/src/telethon/_impl/tl/__init__.py +++ b/client/src/telethon/_impl/tl/__init__.py @@ -1,4 +1,14 @@ -from . import abcs, core, functions, mtproto, types +from . import abcs, functions, mtproto, types +from .core import Request from .layer import LAYER, TYPE_MAPPING -__all__ = ["abcs", "core", "functions", "mtproto", "types", "LAYER", "TYPE_MAPPING"] +__all__ = [ + "abcs", + "core", + "functions", + "mtproto", + "types", + "Request", + "LAYER", + "TYPE_MAPPING", +] diff --git a/client/tests/auth_key_test.py b/client/tests/auth_key_test.py index 381ff64c..978b9554 100644 --- a/client/tests/auth_key_test.py +++ b/client/tests/auth_key_test.py @@ -1,4 +1,4 @@ -from telethon._impl.crypto.auth_key import AuthKey +from telethon._impl.crypto import AuthKey def get_auth_key() -> AuthKey: diff --git a/client/tests/authentication_test.py b/client/tests/authentication_test.py index c59f4fc5..c36a09f5 100644 --- a/client/tests/authentication_test.py +++ b/client/tests/authentication_test.py @@ -1,4 +1,4 @@ -from telethon._impl.crypto.auth_key import AuthKey +from telethon._impl.crypto import AuthKey from telethon._impl.mtproto.authentication import ( CreatedKey, _do_step1, diff --git a/client/tests/client_test.py b/client/tests/client_test.py index 10099eb4..46337006 100644 --- a/client/tests/client_test.py +++ b/client/tests/client_test.py @@ -2,10 +2,8 @@ import os import random from pytest import mark -from telethon._impl.client.client.client import Client -from telethon._impl.client.client.net import Config -from telethon._impl.session.message_box.defs import Session -from telethon._impl.tl.mtproto import functions, types +from telethon import Client, Config, Session +from telethon import _tl as tl @mark.api @@ -27,6 +25,6 @@ async def test_ping_pong() -> None: assert client.connected ping_id = random.randrange(-(2**63), 2**63) - pong = await client(functions.ping(ping_id=ping_id)) - assert isinstance(pong, types.Pong) + pong = await client(tl.mtproto.functions.ping(ping_id=ping_id)) + assert isinstance(pong, tl.mtproto.types.Pong) assert pong.ping_id == ping_id diff --git a/client/tests/crypto_test.py b/client/tests/crypto_test.py index 34b7cdb9..c181cb65 100644 --- a/client/tests/crypto_test.py +++ b/client/tests/crypto_test.py @@ -1,13 +1,13 @@ from telethon._impl.crypto import ( + AuthKey, Side, - _do_encrypt_data_v2, calc_key, decrypt_data_v2, decrypt_ige, encrypt_ige, generate_key_data_from_nonce, ) -from telethon._impl.crypto.auth_key import AuthKey +from telethon._impl.crypto.crypto import _do_encrypt_data_v2 def get_test_auth_key() -> AuthKey: diff --git a/client/tests/factorize_test.py b/client/tests/factorize_test.py index 025bec2a..c80eaafa 100644 --- a/client/tests/factorize_test.py +++ b/client/tests/factorize_test.py @@ -1,4 +1,4 @@ -from telethon._impl.crypto.factorize import factorize +from telethon._impl.crypto import factorize def test_factorization_1() -> None: diff --git a/client/tests/mtproto_test.py b/client/tests/mtproto_test.py index 4c256878..d3fbdaf9 100644 --- a/client/tests/mtproto_test.py +++ b/client/tests/mtproto_test.py @@ -1,8 +1,8 @@ import struct from pytest import raises -from telethon._impl.crypto.auth_key import AuthKey -from telethon._impl.mtproto.mtp import Encrypted, Plain, RpcError +from telethon._impl.crypto import AuthKey +from telethon._impl.mtproto import Encrypted, Plain, RpcError from telethon._impl.tl.mtproto.types import RpcError as GeneratedRpcError diff --git a/client/tests/mtsender_test.py b/client/tests/mtsender_test.py index 99519f8b..78279935 100644 --- a/client/tests/mtsender_test.py +++ b/client/tests/mtsender_test.py @@ -2,8 +2,8 @@ import asyncio import logging from pytest import LogCaptureFixture, mark -from telethon._impl.mtproto.transport.full import Full -from telethon._impl.mtsender.sender import connect +from telethon._impl.mtproto import Full +from telethon._impl.mtsender import connect from telethon._impl.tl import LAYER, abcs, functions, types TELEGRAM_TEST_DC_2 = "149.154.167.40:443" diff --git a/client/tests/packed_chat_test.py b/client/tests/packed_chat_test.py index d6548ada..8a4a98c1 100644 --- a/client/tests/packed_chat_test.py +++ b/client/tests/packed_chat_test.py @@ -1,4 +1,4 @@ -from telethon._impl.session.chat.packed import PackedChat, PackedType +from telethon._impl.session import PackedChat, PackedType def test_hash_optional() -> None: diff --git a/client/tests/reader_test.py b/client/tests/reader_test.py index aed74e97..26037bfc 100644 --- a/client/tests/reader_test.py +++ b/client/tests/reader_test.py @@ -1,8 +1,7 @@ import struct from pytest import mark -from telethon._impl.tl.core import Reader -from telethon._impl.tl.core.serializable import Serializable +from telethon._impl.tl.core import Reader, Serializable from telethon._impl.tl.mtproto.types import BadServerSalt from telethon._impl.tl.types import GeoPoint diff --git a/client/tests/transport/abridged_test.py b/client/tests/transport/abridged_test.py index 00e74bf7..1cb3af1c 100644 --- a/client/tests/transport/abridged_test.py +++ b/client/tests/transport/abridged_test.py @@ -1,7 +1,7 @@ from typing import Tuple from pytest import raises -from telethon._impl.mtproto.transport.abridged import Abridged +from telethon._impl.mtproto import Abridged class Output(bytearray): diff --git a/client/tests/transport/full_test.py b/client/tests/transport/full_test.py index d2311e98..4fc37cb1 100644 --- a/client/tests/transport/full_test.py +++ b/client/tests/transport/full_test.py @@ -1,7 +1,7 @@ from typing import Tuple from pytest import raises -from telethon._impl.mtproto.transport.full import Full +from telethon._impl.mtproto import Full class Output(bytearray): diff --git a/client/tests/transport/intermediate_test.py b/client/tests/transport/intermediate_test.py index 3c5f4a08..e2761553 100644 --- a/client/tests/transport/intermediate_test.py +++ b/client/tests/transport/intermediate_test.py @@ -1,7 +1,7 @@ from typing import Tuple from pytest import raises -from telethon._impl.mtproto.transport.intermediate import Intermediate +from telethon._impl.mtproto import Intermediate class Output(bytearray): diff --git a/generator/src/telethon_generator/_impl/codegen/generator.py b/generator/src/telethon_generator/_impl/codegen/generator.py index 4a2fc9ae..051083f0 100644 --- a/generator/src/telethon_generator/_impl/codegen/generator.py +++ b/generator/src/telethon_generator/_impl/codegen/generator.py @@ -1,7 +1,7 @@ from pathlib import Path from typing import Set -from ..tl_parser.tl.parameter_type import NormalParameter +from ..tl_parser import NormalParameter from .fakefs import FakeFs, SourceWriter from .loader import ParsedTl from .serde.common import ( diff --git a/generator/src/telethon_generator/_impl/codegen/loader.py b/generator/src/telethon_generator/_impl/codegen/loader.py index 269a9ee1..7355a200 100644 --- a/generator/src/telethon_generator/_impl/codegen/loader.py +++ b/generator/src/telethon_generator/_impl/codegen/loader.py @@ -2,7 +2,7 @@ import re from dataclasses import dataclass from typing import List, Optional -from ...tl_parser import Definition, FunctionDef, TypeDef, parse_tl_file +from ..tl_parser import Definition, FunctionDef, TypeDef, parse_tl_file @dataclass diff --git a/generator/src/telethon_generator/_impl/tl_parser/__init__.py b/generator/src/telethon_generator/_impl/tl_parser/__init__.py index e69de29b..a43149de 100644 --- a/generator/src/telethon_generator/_impl/tl_parser/__init__.py +++ b/generator/src/telethon_generator/_impl/tl_parser/__init__.py @@ -0,0 +1,26 @@ +from .tl import ( + BaseParameter, + Definition, + Flag, + FlagsParameter, + NormalParameter, + Parameter, + Type, + TypeDefNotImplemented, +) +from .tl_iterator import FunctionDef, TypeDef +from .tl_iterator import iterate as parse_tl_file + +__all__ = [ + "FunctionDef", + "TypeDef", + "parse_tl_file", + "Definition", + "Flag", + "Parameter", + "TypeDefNotImplemented", + "BaseParameter", + "FlagsParameter", + "NormalParameter", + "Type", +] diff --git a/generator/src/telethon_generator/_impl/tl_parser/tl/__init__.py b/generator/src/telethon_generator/_impl/tl_parser/tl/__init__.py index e69de29b..0352a18f 100644 --- a/generator/src/telethon_generator/_impl/tl_parser/tl/__init__.py +++ b/generator/src/telethon_generator/_impl/tl_parser/tl/__init__.py @@ -0,0 +1,16 @@ +from .definition import Definition +from .flag import Flag +from .parameter import Parameter, TypeDefNotImplemented +from .parameter_type import BaseParameter, FlagsParameter, NormalParameter +from .ty import Type + +__all__ = [ + "Definition", + "Flag", + "Parameter", + "TypeDefNotImplemented", + "BaseParameter", + "FlagsParameter", + "NormalParameter", + "Type", +] diff --git a/generator/src/telethon_generator/tl_parser/__init__.py b/generator/src/telethon_generator/tl_parser/__init__.py index 7349e192..369d096c 100644 --- a/generator/src/telethon_generator/tl_parser/__init__.py +++ b/generator/src/telethon_generator/tl_parser/__init__.py @@ -1,14 +1,16 @@ -from .._impl.tl_parser.tl.definition import Definition -from .._impl.tl_parser.tl.flag import Flag -from .._impl.tl_parser.tl.parameter import Parameter, TypeDefNotImplemented -from .._impl.tl_parser.tl.parameter_type import ( +from .._impl.tl_parser import ( BaseParameter, + Definition, + Flag, FlagsParameter, + FunctionDef, NormalParameter, + Parameter, + Type, + TypeDef, + TypeDefNotImplemented, + parse_tl_file, ) -from .._impl.tl_parser.tl.ty import Type -from .._impl.tl_parser.tl_iterator import FunctionDef, TypeDef -from .._impl.tl_parser.tl_iterator import iterate as parse_tl_file __all__ = [ "Definition",