diff --git a/src/Client.cs b/src/Client.cs index 446be46..350649e 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -39,6 +39,9 @@ namespace WTelegram private readonly SemaphoreSlim _sendSemaphore = new(1); private CancellationTokenSource _cts; + /// Welcome to WTelegramClient! 😀 + /// Config callback, is queried for: api_id, api_hash, session_pathname + /// Handler for Telegram updates messages that are not replies to RPC API calls public Client(Func configProvider = null, Func updateHandler = null) { _config = configProvider ?? DefaultConfigOrAsk; @@ -98,6 +101,8 @@ namespace WTelegram _session.Reset(); } + /// Establish connection to Telegram servers. Config callback is queried for: server_address + /// Most methods of this class are async Task, so please use public async Task ConnectAsync() { if (_reactorTask != null) @@ -234,7 +239,7 @@ namespace WTelegram var buffer = memStream.GetBuffer(); int frameLength = (int)memStream.Length; BinaryPrimitives.WriteInt32LittleEndian(buffer, frameLength + 4); // patch frame_len with correct value - uint crc = Helpers.UpdateCrc32(0, buffer, 0, frameLength); + uint crc = Compat.UpdateCrc32(0, buffer, 0, frameLength); writer.Write(crc); // int32 frame_crc //TODO: support Transport obfuscation? @@ -277,8 +282,8 @@ namespace WTelegram var payload = new byte[length]; if (await FullReadAsync(_networkStream, payload, length, ct) != length) throw new ApplicationException("Could not read frame data : Connection shut down"); - uint crc32 = Helpers.UpdateCrc32(0, frame, 0, 8); - crc32 = Helpers.UpdateCrc32(crc32, payload, 0, payload.Length); + uint crc32 = Compat.UpdateCrc32(0, frame, 0, 8); + crc32 = Compat.UpdateCrc32(crc32, payload, 0, payload.Length); if (await FullReadAsync(_networkStream, frame, 4, ct) != 4) throw new ApplicationException("Could not read frame CRC : Connection shut down"); if (crc32 != BinaryPrimitives.ReadUInt32LittleEndian(frame)) @@ -621,6 +626,11 @@ namespace WTelegram } } + /// + /// Login as bot (if not already done). + /// Config callback is queried for: bot_token + /// + /// Detail about the logged bot public async Task BotAuthIfNeeded() { if (_session.User != null) @@ -633,6 +643,13 @@ namespace WTelegram return user; } + /// + /// Login as a user (if not already done). + /// Config callback is queried for: phone_number, verification_code + ///
and eventually first_name, last_name (signup required), password (2FA auth) + ///
+ /// + /// Detail about the logged user public async Task UserAuthIfNeeded(CodeSettings settings = null) { if (_session.User != null) diff --git a/src/Compat.cs b/src/Compat.cs index 1f6f200..f249169 100644 --- a/src/Compat.cs +++ b/src/Compat.cs @@ -9,6 +9,12 @@ namespace WTelegram { static class Compat { + // see also https://github.com/dotnet/runtime/issues/2036 and https://github.com/dotnet/runtime/pull/53623 + internal static readonly Func UpdateCrc32 = (Func) + typeof(System.IO.Compression.ZipArchive).Assembly.GetType("System.IO.Compression.Crc32Helper") + .GetMethod("UpdateCrc32", new[] { typeof(uint), typeof(byte[]), typeof(int), typeof(int) }) + .CreateDelegate(typeof(Func)); + #if NETCOREAPP2_1_OR_GREATER internal static IPEndPoint IPEndPoint_Parse(string addr) => IPEndPoint.Parse(addr); internal static BigInteger BigEndianInteger(byte[] value) => new(value, true, true); @@ -23,7 +29,7 @@ namespace WTelegram internal static byte[] ToByteArray(this BigInteger bigInteger, bool isUnsigned = false, bool isBigEndian = false) { - if (!isBigEndian || !isUnsigned) throw new ArgumentException("Unexpected parameters to BigInteger"); + if (!isBigEndian || !isUnsigned) throw new ArgumentException("Unexpected parameters to ToByteArray"); var result = bigInteger.ToByteArray(); if (result[^1] == 0) result = result[0..^1]; Array.Reverse(result); diff --git a/src/Helpers.TL.cs b/src/Helpers.TL.cs index c952b4f..fd59324 100644 --- a/src/Helpers.TL.cs +++ b/src/Helpers.TL.cs @@ -7,6 +7,7 @@ namespace TL partial class InputChannel { public static InputPeerChannel Empty => new(); } partial class InputDocument { public static InputDocumentEmpty Empty => new(); } partial class InputPeer { public static InputPeerEmpty Empty => new(); } + partial class InputPeer { public static InputPeerSelf Self => new(); } partial class InputPhoto { public static InputPhotoEmpty Empty => new(); } partial class InputEncryptedFile { public static InputEncryptedFileEmpty Empty => new(); } partial class InputStickerSet { public static InputStickerSetEmpty Empty => new(); } diff --git a/src/Helpers.cs b/src/Helpers.cs index 2be1fbd..31b5f53 100644 --- a/src/Helpers.cs +++ b/src/Helpers.cs @@ -189,11 +189,5 @@ namespace WTelegram } return true; } - - // see also https://github.com/dotnet/runtime/issues/2036 and https://github.com/dotnet/runtime/pull/53623 - internal static readonly Func UpdateCrc32 = (Func) - typeof(System.IO.Compression.ZipArchive).Assembly.GetType("System.IO.Compression.Crc32Helper") - .GetMethod("UpdateCrc32", new[] { typeof(uint), typeof(byte[]), typeof(int), typeof(int) }) - .CreateDelegate(typeof(Func)); } }