From 4c9d9d84f289c40c3c9b44491db47d2c88b00204 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 1 Sep 2018 01:27:22 +0200 Subject: [PATCH 1/5] Change the way int to bytes conversion is used Maybe at some point I should switch to struct --- pyrogram/api/core/primitives/bool.py | 2 +- pyrogram/api/core/primitives/bytes.py | 2 +- pyrogram/api/core/primitives/int.py | 2 +- pyrogram/api/core/primitives/null.py | 2 +- pyrogram/crypto/rsa.py | 14 +++++--------- pyrogram/session/auth.py | 20 ++++++++++---------- 6 files changed, 19 insertions(+), 23 deletions(-) diff --git a/pyrogram/api/core/primitives/bool.py b/pyrogram/api/core/primitives/bool.py index cc8655d9..9641e865 100644 --- a/pyrogram/api/core/primitives/bool.py +++ b/pyrogram/api/core/primitives/bool.py @@ -30,7 +30,7 @@ class BoolFalse(Object): return cls.value def __new__(cls) -> bytes: - return int.to_bytes(cls.ID, 4, "little") + return cls.ID.to_bytes(4, "little") class BoolTrue(BoolFalse): diff --git a/pyrogram/api/core/primitives/bytes.py b/pyrogram/api/core/primitives/bytes.py index da438ef1..d161cc9c 100644 --- a/pyrogram/api/core/primitives/bytes.py +++ b/pyrogram/api/core/primitives/bytes.py @@ -48,7 +48,7 @@ class Bytes(Object): else: return ( bytes([254]) - + int.to_bytes(length, 3, "little") + + length.to_bytes(3, "little") + value + bytes(-length % 4) ) diff --git a/pyrogram/api/core/primitives/int.py b/pyrogram/api/core/primitives/int.py index 0985367f..3de37151 100644 --- a/pyrogram/api/core/primitives/int.py +++ b/pyrogram/api/core/primitives/int.py @@ -29,7 +29,7 @@ class Int(Object): return int.from_bytes(b.read(cls.SIZE), "little", signed=signed) def __new__(cls, value: int, signed: bool = True) -> bytes: - return int.to_bytes(value, cls.SIZE, "little", signed=signed) + return value.to_bytes(cls.SIZE, "little", signed=signed) class Long(Int): diff --git a/pyrogram/api/core/primitives/null.py b/pyrogram/api/core/primitives/null.py index 3d73c06e..7a26b112 100644 --- a/pyrogram/api/core/primitives/null.py +++ b/pyrogram/api/core/primitives/null.py @@ -29,4 +29,4 @@ class Null(Object): return None def __new__(cls) -> bytes: - return int.to_bytes(cls.ID, 4, "little") + return cls.ID.to_bytes(4, "little") diff --git a/pyrogram/crypto/rsa.py b/pyrogram/crypto/rsa.py index 9f02e2cc..10302dab 100644 --- a/pyrogram/crypto/rsa.py +++ b/pyrogram/crypto/rsa.py @@ -206,12 +206,8 @@ class RSA: @classmethod def encrypt(cls, data: bytes, fingerprint: int) -> bytes: - return int.to_bytes( - pow( - int.from_bytes(data, "big"), - cls.server_public_keys[fingerprint].e, - cls.server_public_keys[fingerprint].m - ), - 256, - "big" - ) + return pow( + int.from_bytes(data, "big"), + cls.server_public_keys[fingerprint].e, + cls.server_public_keys[fingerprint].m + ).to_bytes(256, "big") diff --git a/pyrogram/session/auth.py b/pyrogram/session/auth.py index a545a642..87817da1 100644 --- a/pyrogram/session/auth.py +++ b/pyrogram/session/auth.py @@ -111,8 +111,8 @@ class Auth: data = types.PQInnerData( res_pq.pq, - int.to_bytes(p, 4, "big"), - int.to_bytes(q, 4, "big"), + p.to_bytes(4, "big"), + q.to_bytes(4, "big"), nonce, server_nonce, new_nonce, @@ -131,8 +131,8 @@ class Auth: functions.ReqDHParams( nonce, server_nonce, - int.to_bytes(p, 4, "big"), - int.to_bytes(q, 4, "big"), + p.to_bytes(4, "big"), + q.to_bytes(4, "big"), public_key_fingerprint, encrypted_data ) @@ -140,8 +140,8 @@ class Auth: encrypted_answer = server_dh_params.encrypted_answer - server_nonce = int.to_bytes(server_nonce, 16, "little", signed=True) - new_nonce = int.to_bytes(new_nonce, 32, "little", signed=True) + server_nonce = server_nonce.to_bytes(16, "little", signed=True) + new_nonce = new_nonce.to_bytes(32, "little", signed=True) tmp_aes_key = ( sha1(new_nonce + server_nonce).digest() @@ -170,7 +170,7 @@ class Auth: # Step 6 g = server_dh_inner_data.g b = int.from_bytes(urandom(256), "big") - g_b = int.to_bytes(pow(g, b, dh_prime), 256, "big") + g_b = pow(g, b, dh_prime).to_bytes(256, "big") retry_id = 0 @@ -199,8 +199,8 @@ class Auth: # Step 7; Step 8 g_a = int.from_bytes(server_dh_inner_data.g_a, "big") - auth_key = int.to_bytes(pow(g_a, b, dh_prime), 256, "big") - server_nonce = int.to_bytes(server_nonce, 16, "little", signed=True) + auth_key = pow(g_a, b, dh_prime).to_bytes(256, "big") + server_nonce = server_nonce.to_bytes(16, "little", signed=True) # TODO: Handle errors @@ -235,7 +235,7 @@ class Auth: # 3rd message assert nonce == set_client_dh_params_answer.nonce assert server_nonce == set_client_dh_params_answer.server_nonce - server_nonce = int.to_bytes(server_nonce, 16, "little", signed=True) + server_nonce = server_nonce.to_bytes(16, "little", signed=True) log.debug("Nonce fields check: OK") # Step 9 From db6042e91b1b8dfafa2ae51e9e2b8c4d07b6a329 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 2 Sep 2018 13:04:29 +0200 Subject: [PATCH 2/5] Fetch ChatForbidden and ChannelForbidden peers This fixes unwanted PEER_ID_INVALID errors in cases where a user or a bot was kicked/banned from a group, supergroup or channel --- pyrogram/client/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index e9be7a4e..a3cbd93b 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -615,7 +615,7 @@ class Client(Methods, BaseClient): if phone is not None: self.peers_by_phone[phone] = input_peer - if isinstance(entity, types.Chat): + if isinstance(entity, (types.Chat, types.ChatForbidden)): chat_id = entity.id peer_id = -chat_id @@ -625,7 +625,7 @@ class Client(Methods, BaseClient): self.peers_by_id[peer_id] = input_peer - if isinstance(entity, types.Channel): + if isinstance(entity, (types.Channel, types.ChannelForbidden)): channel_id = entity.id peer_id = int("-100" + str(channel_id)) @@ -634,7 +634,7 @@ class Client(Methods, BaseClient): if access_hash is None: continue - username = entity.username + username = getattr(entity, "username", None) input_peer = types.InputPeerChannel( channel_id=channel_id, From b64d66130542b9d9aeb913c5c3b5f8280a306930 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 5 Sep 2018 16:41:46 +0200 Subject: [PATCH 3/5] Restore original Makefile --- docs/Makefile | 2 +- docs/Makefile_ | 20 -------------------- 2 files changed, 1 insertion(+), 21 deletions(-) delete mode 100644 docs/Makefile_ diff --git a/docs/Makefile b/docs/Makefile index c01e3d3d..c647eb13 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -3,7 +3,7 @@ # You can set these variables from the command line. SPHINXOPTS = -SPHINXBUILD = ~/PycharmProjects/pyrogram/venv3.6/bin/sphinx-build +SPHINXBUILD = sphinx-build SPHINXPROJ = Pyrogram SOURCEDIR = source BUILDDIR = build diff --git a/docs/Makefile_ b/docs/Makefile_ deleted file mode 100644 index c647eb13..00000000 --- a/docs/Makefile_ +++ /dev/null @@ -1,20 +0,0 @@ -# Minimal makefile for Sphinx documentation -# - -# You can set these variables from the command line. -SPHINXOPTS = -SPHINXBUILD = sphinx-build -SPHINXPROJ = Pyrogram -SOURCEDIR = source -BUILDDIR = build - -# Put it first so that "make" without argument is like "make help". -help: - @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) - -.PHONY: help Makefile - -# Catch-all target: route all unknown targets to Sphinx using the new -# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). -%: Makefile - @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) \ No newline at end of file From 024e3280c1c623ff92ed70e810033f5c04e0079d Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 5 Sep 2018 16:44:07 +0200 Subject: [PATCH 4/5] Remove unneeded file --- compiler/api/compiler.py | 5 ++--- compiler/api/source/pyrogram.tl | 22 ---------------------- 2 files changed, 2 insertions(+), 25 deletions(-) delete mode 100644 compiler/api/source/pyrogram.tl diff --git a/compiler/api/compiler.py b/compiler/api/compiler.py index 555d1824..bcb96ea4 100644 --- a/compiler/api/compiler.py +++ b/compiler/api/compiler.py @@ -172,9 +172,8 @@ def start(): with open("{}/source/auth_key.tl".format(HOME), encoding="utf-8") as auth, \ open("{}/source/sys_msgs.tl".format(HOME), encoding="utf-8") as system, \ - open("{}/source/main_api.tl".format(HOME), encoding="utf-8") as api, \ - open("{}/source/pyrogram.tl".format(HOME), encoding="utf-8") as pyrogram: - schema = (auth.read() + system.read() + api.read() + pyrogram.read()).splitlines() + open("{}/source/main_api.tl".format(HOME), encoding="utf-8") as api: + schema = (auth.read() + system.read() + api.read()).splitlines() with open("{}/template/mtproto.txt".format(HOME), encoding="utf-8") as f: mtproto_template = f.read() diff --git a/compiler/api/source/pyrogram.tl b/compiler/api/source/pyrogram.tl deleted file mode 100644 index a8232bd6..00000000 --- a/compiler/api/source/pyrogram.tl +++ /dev/null @@ -1,22 +0,0 @@ -// Pyrogram - ----types--- - -//pyrogram.update#b0700000 flags:# update_id:int message:flags.0?Message edited_message:flags.1?Message channel_post:flags.2?Message edited_channel_post:flags.3?Message inline_query:flags.4?InlineQuery chosen_inline_result:flags.5?ChosenInlineResult callback_query:flags.6?CallbackQuery shipping_query:flags.7?ShippingQuery pre_checkout_query:flags.8?PreCheckoutQuery = pyrogram.Update; -//pyrogram.user#b0700001 flags:# id:int is_bot:Bool first_name:string last_name:flags.0?string username:flags.1?string language_code:flags.2?string phone_number:flags.3?string photo:flags.4?ChatPhoto = pyrogram.User; -//pyrogram.chat#b0700002 flags:# id:int type:string title:flags.0?string username:flags.1?string first_name:flags.2?string last_name:flags.3?string all_members_are_administrators:flags.4?Bool photo:flags.5?ChatPhoto description:flags.6?string invite_link:flags.7?string pinned_message:flags.8?Message sticker_set_name:flags.9?string can_set_sticker_set:flags.10?Bool = pyrogram.Chat; -//pyrogram.message#b0700003 flags:# message_id:int from_user:flags.0?User date:int chat:Chat forward_from:flags.1?User forward_from_chat:flags.2?Chat forward_from_message_id:flags.3?int forward_signature:flags.4?string forward_date:flags.5?int reply_to_message:flags.6?Message edit_date:flags.7?int media_group_id:flags.8?string author_signature:flags.9?string text:flags.10?string entities:flags.11?Vector caption_entities:flags.12?Vector audio:flags.13?Audio document:flags.14?Document game:flags.15?Game photo:flags.16?Vector sticker:flags.17?Sticker video:flags.18?Video voice:flags.19?Voice video_note:flags.20?VideoNote caption:flags.21?string contact:flags.22?Contact location:flags.23?Location venue:flags.24?Venue new_chat_members:flags.25?Vector left_chat_member:flags.26?User new_chat_title:flags.27?string new_chat_photo:flags.28?Vector delete_chat_photo:flags.29?true group_chat_created:flags.30?true supergroup_chat_created:flags.31?true channel_chat_created:flags.32?true migrate_to_chat_id:flags.33?int migrate_from_chat_id:flags.34?int pinned_message:flags.35?Message invoice:flags.36?Invoice successful_payment:flags.37?SuccessfulPayment connected_website:flags.38?string views:flags.39?int via_bot:flags.40?User = pyrogram.Message; -//pyrogram.messageEntity#b0700004 flags:# type:string offset:int length:int url:flags.0?string user:flags.1?User = pyrogram.MessageEntity; -//pyrogram.photoSize#b0700005 flags:# file_id:string file_size:flags.0?int date:flags.1?int width:int height:int = pyrogram.PhotoSize; -//pyrogram.audio#b0700006 flags:# file_id:string thumb:flags.0?PhotoSize file_name:flags.1?string mime_type:flags.2?string file_size:flags.3?int date:flags.4?int duration:int performer:flags.5?string title:flags.6?string = pyrogram.Audio; -//pyrogram.document#b0700007 flags:# file_id:string thumb:flags.0?PhotoSize file_name:flags.1?string mime_type:flags.2?string file_size:flags.3?int date:flags.4?int = pyrogram.Document; -//pyrogram.video#b0700008 flags:# file_id:string thumb:flags.0?PhotoSize file_name:flags.1?string mime_type:flags.2?string file_size:flags.3?int date:flags.4?int width:int height:int duration:int = pyrogram.Video; -//pyrogram.voice#b0700009 flags:# file_id:string thumb:flags.0?PhotoSize file_name:flags.1?string mime_type:flags.2?string file_size:flags.3?int date:flags.4?int duration:int = pyrogram.Voice; -//pyrogram.videoNote#b0700010 flags:# file_id:string thumb:flags.0?PhotoSize file_name:flags.1?string mime_type:flags.2?string file_size:flags.3?int date:flags.4?int length:int duration:int = pyrogram.VideoNote; -//pyrogram.contact#b0700011 flags:# phone_number:string first_name:string last_name:flags.0?string user_id:flags.1?int = pyrogram.Contact; -//pyrogram.location#b0700012 longitude:double latitude:double = pyrogram.Location; -//pyrogram.venue#b0700013 flags:# location:Location title:string address:string foursquare_id:flags.0?string = pyrogram.Venue; -//pyrogram.userProfilePhotos#b0700014 total_count:int photos:Vector> = pyrogram.UserProfilePhotos; -//pyrogram.chatPhoto#b0700015 small_file_id:string big_file_id:string = pyrogram.ChatPhoto; -//pyrogram.chatMember#b0700016 flags:# user:User status:string until_date:flags.0?int can_be_edited:flags.1?Bool can_change_info:flags.2?Bool can_post_messages:flags.3?Bool can_edit_messages:flags.4?Bool can_delete_messages:flags.5?Bool can_invite_users:flags.6?Bool can_restrict_members:flags.7?Bool can_pin_messages:flags.8?Bool can_promote_members:flags.9?Bool can_send_messages:flags.10?Bool can_send_media_messages:flags.11?Bool can_send_other_messages:flags.12?Bool can_add_web_page_previews:flags.13?Bool = pyrogram.ChatMember; -//pyrogram.sticker#b0700017 flags:# file_id:string thumb:flags.0?PhotoSize file_name:flags.1?string mime_type:flags.2?string file_size:flags.3?int date:flags.4?int width:int height:int emoji:flags.5?string set_name:flags.6?string mask_position:flags.7?MaskPosition = pyrogram.Sticker; From b07c13a5131c3c5fddf8b3940e7c44b8a7b264bc Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 5 Sep 2018 16:50:38 +0200 Subject: [PATCH 5/5] Remove useless definition PyCharm will complain about Long not being a bytes, let's not care. --- pyrogram/api/core/primitives/int.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pyrogram/api/core/primitives/int.py b/pyrogram/api/core/primitives/int.py index 3de37151..4b9aded8 100644 --- a/pyrogram/api/core/primitives/int.py +++ b/pyrogram/api/core/primitives/int.py @@ -35,9 +35,6 @@ class Int(Object): class Long(Int): SIZE = 8 - def __new__(cls, *args): - return super().__new__(cls, *args) - class Int128(Int): SIZE = 16