From 0ed77ef98480322b7ca8c5cee440a641d9e2f4a5 Mon Sep 17 00:00:00 2001
From: Wizou <11647984+wiz0u@users.noreply.github.com>
Date: Mon, 17 Jan 2022 15:06:29 +0100
Subject: [PATCH] Support GZipped array in RpcResult
---
FAQ.md | 32 +++++++++++++++++---------------
src/Client.cs | 16 ++++++++++++----
src/TL.Table.cs | 1 +
src/TL.cs | 12 ++++--------
4 files changed, 34 insertions(+), 27 deletions(-)
diff --git a/FAQ.md b/FAQ.md
index 509a7e3..5f9b53d 100644
--- a/FAQ.md
+++ b/FAQ.md
@@ -36,7 +36,8 @@ An easy solution is to call `Interaction.InputBox("Enter verification code")` in
This might require adding a reference *(and `using`)* to the Microsoft.VisualBasic assembly.
A more complex solution requires the use of a `ManualResetEventSlim` that you will wait for in Config callback,
-and when the user has provided the verification_code through your GUI, you "set" the event to release your Config callback so it can return the code. ([see example](https://stackoverflow.com/a/70379582/3365403))
+and when the user has provided the verification_code through your GUI, you "set" the event to release your Config callback so it can return the code.
+([download a full example](https://github.com/wiz0u/WTelegramClient/raw/master/Examples/WinForms_app.zip))
#### 4. Where to get the access_hash? Why the error `CHANNEL_INVALID` or `USER_ID_INVALID`?
@@ -84,8 +85,22 @@ If you use the Github source project in an old .NET Framework 4.x or .NET Core x
To fix this, you should also switch to using the [WTelegramClient Nuget package](https://www.nuget.org/packages/WTelegramClient) as it will install the required dependencies for it to work.
+
+#### 7. I get error FLOOD_WAIT_8xxxx or PEER_FLOOD, PHONE_NUMBER_BANNED. I can't import phone numbers.
+
+You can get these kind of problems if you abuse Telegram [Terms of Service](https://telegram.org/tos), or the [API Terms of Service](https://core.telegram.org/api/terms), or make excessive requests.
+
+You can try to wait more between the requests, wait for a day or two to see if the requests become possible again.
+>ℹ️ For FLOOD_WAIT_X with X < 60 seconds (see `client.FloodRetryThreshold`), WTelegramClient will automatically wait the specified delay and retry the request for you.
+
+An account that was limited due to reported spam might receive PEER_FLOOD errors. Read [Telegram Spam FAQ](https://telegram.org/faq_spam) to learn more.
+
+If you think your phone number was banned from Telegram for a wrong reason, you may try to contact [recover@telegram.org](mailto:recover@telegram.org), explaining what you were doing.
+
+In any case, WTelegramClient is not responsible for the bad usage of the library and we are not affiliated to Telegram teams, so there is nothing we can do.
+
-#### 7. How to not get banned from Telegram?
+#### 8. How to not get banned from Telegram?
**Do not share publicly your app's ID and hash!** They cannot be regenerated and are bound to your Telegram account.
@@ -112,19 +127,6 @@ Here are some key points:
If your client displays Telegram channels to the user, you have to support and display [official sponsored messages](https://core.telegram.org/api/sponsored-messages).
-
-#### 8. I can't import phone numbers. I get error PHONE_NUMBER_BANNED, FLOOD_WAIT_8xxxx or PEER_FLOOD
-
-You can get these kind of problems if you abuse Telegram [Terms of Service](https://telegram.org/tos), or the [API Terms of Service](https://core.telegram.org/api/terms), or make excessive requests.
-
-You can try to wait more between the requests, wait for a day or two to see if the requests become possible again.
-
-An account that was limited due to reported spam might receive PEER_FLOOD errors. Read [Telegram Spam FAQ](https://telegram.org/faq_spam) to learn more.
-
-If you think your phone number was banned from Telegram for a wrong reason, you may try to contact [recover@telegram.org](mailto:recover@telegram.org), explaining what you were doing.
-
-In any case, WTelegramClient is not responsible for the bad usage of the library and we are not affiliated to Telegram teams, so there is nothing we can do.
-
#### 9. Why the error `CHAT_ID_INVALID`?
diff --git a/src/Client.cs b/src/Client.cs
index 8acc628..419615e 100644
--- a/src/Client.cs
+++ b/src/Client.cs
@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
+using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Sockets;
@@ -777,12 +778,19 @@ namespace WTelegram
{
if (!type.IsArray)
result = reader.ReadTLValue(type);
- else if (reader.ReadUInt32() == Layer.RpcErrorCtor)
- result = reader.ReadTLObject(Layer.RpcErrorCtor);
else
{
- reader.BaseStream.Position -= 4;
- result = reader.ReadTLValue(type);
+ var peek = reader.ReadUInt32();
+ if (peek == Layer.RpcErrorCtor)
+ result = reader.ReadTLObject(Layer.RpcErrorCtor);
+ else if (peek == Layer.GZipedCtor)
+ using (var gzipReader = new TL.BinaryReader(new GZipStream(new MemoryStream(reader.ReadTLBytes()), CompressionMode.Decompress), reader.Client))
+ result = gzipReader.ReadTLValue(type);
+ else
+ {
+ reader.BaseStream.Position -= 4;
+ result = reader.ReadTLValue(type);
+ }
}
if (type.IsEnum) result = Enum.ToObject(type, result);
if (result is RpcError rpcError)
diff --git a/src/TL.Table.cs b/src/TL.Table.cs
index 6518461..246d69c 100644
--- a/src/TL.Table.cs
+++ b/src/TL.Table.cs
@@ -14,6 +14,7 @@ namespace TL
internal const uint RpcErrorCtor = 0x2144CA19;
internal const uint MsgContainerCtor = 0x73F1F8DC;
internal const uint BadMsgCtor = 0xA7EFF811;
+ internal const uint GZipedCtor = 0x3072CFA1;
[EditorBrowsable(EditorBrowsableState.Never)]
public readonly static Dictionary Table = new()
diff --git a/src/TL.cs b/src/TL.cs
index 97a670f..b276756 100644
--- a/src/TL.cs
+++ b/src/TL.cs
@@ -53,6 +53,9 @@ namespace TL
internal static IObject ReadTLObject(this BinaryReader reader, uint ctorNb = 0)
{
if (ctorNb == 0) ctorNb = reader.ReadUInt32();
+ if (ctorNb == Layer.GZipedCtor)
+ using (var gzipReader = new BinaryReader(new GZipStream(new MemoryStream(reader.ReadTLBytes()), CompressionMode.Decompress), reader.Client))
+ return ReadTLObject(gzipReader);
if (!Layer.Table.TryGetValue(ctorNb, out var type))
throw new ApplicationException($"Cannot find type for ctor #{ctorNb:x}");
if (type == null) return null; // nullable ctor (class meaning is associated with null)
@@ -70,7 +73,7 @@ namespace TL
if (field.Name == "flags") flags = (int)value;
else if (field.Name == "access_hash") reader.Client?.UpdateAccessHash(obj, type, value);
}
- return type == typeof(GzipPacked) ? UnzipPacket((GzipPacked)obj, reader.Client) : (IObject)obj;
+ return (IObject)obj;
}
internal static void WriteTLValue(this BinaryWriter writer, object value, Type valueType)
@@ -292,13 +295,6 @@ namespace TL
writer.Write(0); // null arrays/strings are serialized as empty
}
- internal static IObject UnzipPacket(GzipPacked obj, WTelegram.Client client)
- {
- using var reader = new BinaryReader(new GZipStream(new MemoryStream(obj.packed_data), CompressionMode.Decompress), client);
- var result = reader.ReadTLObject();
- return result;
- }
-
#if DEBUG
private static void ShouldntBeHere() => System.Diagnostics.Debugger.Break();
#else