From b5ca3fcc0e5e34ac083169eeddbf1eb19dacce08 Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Fri, 8 Mar 2024 11:52:30 +0100 Subject: [PATCH] Fix SendAlbumAsync videoUrlAsFile. using [] syntax --- src/Client.Helpers.cs | 14 +++++++------- src/Client.cs | 8 ++++---- src/Compat.cs | 2 +- src/Encryption.cs | 6 +++--- src/Helpers.cs | 8 ++++---- src/SecretChats.cs | 4 ++-- src/Session.cs | 4 ++-- src/TL.Helpers.cs | 24 ++++++++++++------------ src/TL.Schema.cs | 6 +++--- src/TlsStream.cs | 22 +++++++++++----------- 10 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/Client.Helpers.cs b/src/Client.Helpers.cs index db7a17d..4775d48 100644 --- a/src/Client.Helpers.cs +++ b/src/Client.Helpers.cs @@ -218,10 +218,10 @@ namespace WTelegram case InputMediaDocumentExternal imde: string mimeType = null; var inputFile = await UploadFromUrl(imde.url); - if (mimeType?.StartsWith("video/") == true) - ism.media = new InputMediaUploadedDocument(inputFile, mimeType, new DocumentAttributeVideo { flags = DocumentAttributeVideo.Flags.supports_streaming }); - else + if (videoUrlAsFile || mimeType?.StartsWith("video/") != true) ism.media = new InputMediaUploadedDocument(inputFile, mimeType); + else + ism.media = new InputMediaUploadedDocument(inputFile, mimeType, new DocumentAttributeVideo { flags = DocumentAttributeVideo.Flags.supports_streaming }); goto retry; case InputMediaPhotoExternal impe: inputFile = await UploadFromUrl(impe.url); @@ -510,7 +510,7 @@ namespace WTelegram public async Task Channels_GetAllParticipants(InputChannelBase channel, bool includeKickBan = false, string alphabet1 = "АБCДЕЄЖФГHИІJКЛМНОПQРСТУВWХЦЧШЩЫЮЯЗ", string alphabet2 = "АCЕHИJЛМНОРСТУВWЫ", CancellationToken cancellationToken = default) { alphabet2 ??= alphabet1; - var result = new Channels_ChannelParticipants { chats = new(), users = new() }; + var result = new Channels_ChannelParticipants { chats = [], users = [] }; var user_ids = new HashSet(); var participants = new List(); @@ -616,7 +616,7 @@ namespace WTelegram { case InputPeerChat chat: await this.Messages_EditChatAdmin(chat.chat_id, user, is_admin); - return new Updates { date = DateTime.UtcNow, users = new(), updates = Array.Empty(), + return new Updates { date = DateTime.UtcNow, users = [], updates = [], chats = (await this.Messages_GetChats(chat.chat_id)).chats }; case InputPeerChannel channel: return await this.Channels_EditAdmin(channel, user, @@ -663,7 +663,7 @@ namespace WTelegram { case InputPeerChat chat: await this.Messages_DeleteChat(chat.chat_id); - return new Updates { date = DateTime.UtcNow, users = new(), updates = Array.Empty(), + return new Updates { date = DateTime.UtcNow, users = [], updates = [], chats = (await this.Messages_GetChats(chat.chat_id)).chats }; case InputPeerChannel channel: return await this.Channels_DeleteChannel(channel); @@ -695,7 +695,7 @@ namespace WTelegram public async Task ReadHistory(InputPeer peer, int max_id = default) => peer is InputPeerChannel channel ? await this.Channels_ReadHistory(channel, max_id) : (await this.Messages_ReadHistory(peer, max_id)) != null; - private static readonly char[] UrlSeparator = new[] { '?', '#', '/' }; + private static readonly char[] UrlSeparator = ['?', '#', '/']; /// Return information about a chat/channel based on Invite Link or Public Link /// Public link or Invite link, like https://t.me/+InviteHash, https://t.me/joinchat/InviteHash or https://t.me/channelname
Works also without https:// prefix diff --git a/src/Client.cs b/src/Client.cs index 2b3447c..c12482a 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -64,12 +64,12 @@ namespace WTelegram private Stream _networkStream; private IObject _lastSentMsg; private long _lastRecvMsgId; - private readonly List _msgsToAck = new(); + private readonly List _msgsToAck = []; private readonly Random _random = new(); private int _saltChangeCounter; private Task _reactorTask; private Rpc _bareRpc; - private readonly Dictionary _pendingRpcs = new(); + private readonly Dictionary _pendingRpcs = []; private SemaphoreSlim _sendSemaphore = new(0); private readonly SemaphoreSlim _semaphore = new(1); private Task _connecting; @@ -489,7 +489,7 @@ namespace WTelegram { lock (_session) { - _dcSession.Salts ??= new(); + _dcSession.Salts ??= []; if (_dcSession.Salts.Count != 0) { var keys = _dcSession.Salts.Keys; @@ -1285,7 +1285,7 @@ namespace WTelegram if (isContent && CheckMsgsToAck() is MsgsAck msgsAck) { var (ackId, ackSeqno) = NewMsgId(false); - var container = new MsgContainer { messages = new _Message[] { new(msgId, seqno, msg), new(ackId, ackSeqno, msgsAck) } }; + var container = new MsgContainer { messages = [new(msgId, seqno, msg), new(ackId, ackSeqno, msgsAck)] }; await SendAsync(container, false); return; } diff --git a/src/Compat.cs b/src/Compat.cs index 9364fde..b83e832 100644 --- a/src/Compat.cs +++ b/src/Compat.cs @@ -87,7 +87,7 @@ namespace System.Runtime.CompilerServices { if (array == null) throw new ArgumentNullException(); var (offset, length) = range.GetOffsetAndLength(array.Length); - if (length == 0) return Array.Empty(); + if (length == 0) return []; var dest = typeof(T).IsValueType || typeof(T[]) == array.GetType() ? new T[length] : (T[])Array.CreateInstance(array.GetType().GetElementType()!, length); Array.Copy(array, offset, dest, 0, length); diff --git a/src/Encryption.cs b/src/Encryption.cs index 23ab8fc..a7ede50 100644 --- a/src/Encryption.cs +++ b/src/Encryption.cs @@ -15,7 +15,7 @@ namespace WTelegram { internal static class Encryption { - private static readonly Dictionary PublicKeys = new(); + private static readonly Dictionary PublicKeys = []; internal static readonly RNGCryptoServiceProvider RNG = new(); internal static readonly Aes AesECB = Aes.Create(); @@ -209,7 +209,7 @@ namespace WTelegram SafePrimes.Add(p); } - private static readonly HashSet SafePrimes = new() { new(new byte[] // C71CAEB9C6B1C904... + private static readonly HashSet SafePrimes = [ new(new byte[] // C71CAEB9C6B1C904... { 0x5B, 0xCC, 0x2F, 0xB9, 0xE3, 0xD8, 0x9C, 0x11, 0x03, 0x04, 0xB1, 0x34, 0xF0, 0xAD, 0x4F, 0x6F, 0xBF, 0x54, 0x24, 0x4B, 0xD0, 0x15, 0x4E, 0x2E, 0xEE, 0x05, 0xB1, 0x35, 0xF6, 0x15, 0x81, 0x0D, @@ -227,7 +227,7 @@ namespace WTelegram 0xDB, 0xF4, 0x30, 0x25, 0xD2, 0x93, 0x94, 0x22, 0x58, 0x40, 0xC1, 0xA7, 0x0A, 0x8A, 0x19, 0x48, 0x0F, 0x93, 0x3D, 0x56, 0x37, 0xD0, 0x34, 0x49, 0xC1, 0x21, 0x3E, 0x8E, 0x23, 0x40, 0x0D, 0x98, 0x73, 0x3F, 0xF1, 0x70, 0x2F, 0x52, 0x6C, 0x8E, 0x04, 0xC9, 0xB1, 0xC6, 0xB9, 0xAE, 0x1C, 0xC7, 0x00 - })}; + })]; internal static void CheckGoodGaAndGb(BigInteger g, BigInteger dh_prime) { diff --git a/src/Helpers.cs b/src/Helpers.cs index 50b445e..decf66c 100644 --- a/src/Helpers.cs +++ b/src/Helpers.cs @@ -18,8 +18,8 @@ namespace WTelegram public static readonly JsonSerializerOptions JsonOptions = new() { IncludeFields = true, WriteIndented = true, IgnoreReadOnlyProperties = true, DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull }; - private static readonly ConsoleColor[] LogLevelToColor = new[] { ConsoleColor.DarkGray, ConsoleColor.DarkCyan, - ConsoleColor.Cyan, ConsoleColor.Yellow, ConsoleColor.Red, ConsoleColor.Magenta, ConsoleColor.DarkBlue }; + private static readonly ConsoleColor[] LogLevelToColor = [ ConsoleColor.DarkGray, ConsoleColor.DarkCyan, + ConsoleColor.Cyan, ConsoleColor.Yellow, ConsoleColor.Red, ConsoleColor.Magenta, ConsoleColor.DarkBlue ]; private static void DefaultLogger(int level, string message) { Console.ForegroundColor = LogLevelToColor[level]; @@ -186,7 +186,7 @@ namespace WTelegram } internal static readonly byte[] StrippedThumbJPG = // see https://core.telegram.org/api/files#stripped-thumbnails - { + [ 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x28, 0x1c, 0x1e, 0x23, 0x1e, 0x19, 0x28, 0x23, 0x21, 0x23, 0x2d, 0x2b, 0x28, 0x30, 0x3c, 0x64, 0x41, 0x3c, 0x37, 0x37, @@ -223,7 +223,7 @@ namespace WTelegram 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00 - }; + ]; internal static string GetSystemVersion() { diff --git a/src/SecretChats.cs b/src/SecretChats.cs index cc64f91..9cc89cd 100644 --- a/src/SecretChats.cs +++ b/src/SecretChats.cs @@ -28,7 +28,7 @@ namespace WTelegram private readonly Client client; private readonly FileStream storage; - private readonly Dictionary chats = new(); + private readonly Dictionary chats = []; private Messages_DhConfig dh; private BigInteger dh_prime; private readonly SHA256 sha256 = SHA256.Create(); @@ -57,7 +57,7 @@ namespace WTelegram public int RemoteLayer => remoteLayer; internal long key_fingerprint; - internal SortedList pendingMsgs = new(); + internal SortedList pendingMsgs = []; internal void Discarded() // clear out fields for more security { Array.Clear(authKey, 0, authKey.Length); diff --git a/src/Session.cs b/src/Session.cs index d5d0b82..950cad0 100644 --- a/src/Session.cs +++ b/src/Session.cs @@ -16,7 +16,7 @@ namespace WTelegram public int ApiId; public long UserId; public int MainDC; - public Dictionary DCSessions = new(); + public Dictionary DCSessions = []; public TL.DcOption[] DcOptions; public class DCSession @@ -195,7 +195,7 @@ namespace WTelegram internal class ActionStore : MemoryStream { private readonly Action _save; - public ActionStore(byte[] initial, Action save) : base(initial ?? Array.Empty()) => _save = save; + public ActionStore(byte[] initial, Action save) : base(initial ?? []) => _save = save; public override void Write(byte[] buffer, int offset, int count) => _save(buffer[offset..(offset + count)]); public override void SetLength(long value) { } } diff --git a/src/TL.Helpers.cs b/src/TL.Helpers.cs index 8f187a0..f74311b 100644 --- a/src/TL.Helpers.cs +++ b/src/TL.Helpers.cs @@ -285,7 +285,7 @@ namespace TL partial class ChatParticipantAdmin { public override bool IsAdmin => true; } partial class ChatParticipantsBase { public abstract ChatParticipantBase[] Participants { get; }} - partial class ChatParticipantsForbidden { public override ChatParticipantBase[] Participants => Array.Empty(); } + partial class ChatParticipantsForbidden { public override ChatParticipantBase[] Participants => []; } partial class ChatParticipants { public override ChatParticipantBase[] Participants => participants; } partial class MessageEmpty { public override string ToString() => "(no message)"; } @@ -429,8 +429,8 @@ namespace TL public abstract Update[] UpdateList { get; } public virtual Dictionary Users => NoUsers; public virtual Dictionary Chats => NoChats; - private static readonly Dictionary NoUsers = new(); - private static readonly Dictionary NoChats = new(); + private static readonly Dictionary NoUsers = []; + private static readonly Dictionary NoChats = []; public virtual (long mbox_id, int pts, int pts_count) GetMBox() => default; } partial class UpdatesCombined @@ -447,10 +447,10 @@ namespace TL public override Dictionary Chats => chats; public override (long mbox_id, int pts, int pts_count) GetMBox() => (-2, seq, 1); } - partial class UpdatesTooLong { public override Update[] UpdateList => Array.Empty(); } - partial class UpdateShort { public override Update[] UpdateList => new[] { update }; } - partial class UpdateShortSentMessage { public override Update[] UpdateList => Array.Empty(); } - partial class UpdateShortMessage { public override Update[] UpdateList => new[] { new UpdateNewMessage + partial class UpdatesTooLong { public override Update[] UpdateList => []; } + partial class UpdateShort { public override Update[] UpdateList => [update]; } + partial class UpdateShortSentMessage { public override Update[] UpdateList => []; } + partial class UpdateShortMessage { public override Update[] UpdateList => [ new UpdateNewMessage { message = new Message { @@ -460,8 +460,8 @@ namespace TL peer_id = new PeerUser { user_id = user_id }, fwd_from = fwd_from, via_bot_id = via_bot_id, ttl_period = ttl_period }, pts = pts, pts_count = pts_count - } }; } - partial class UpdateShortChatMessage { public override Update[] UpdateList => new[] { new UpdateNewMessage + } ]; } + partial class UpdateShortChatMessage { public override Update[] UpdateList => [ new UpdateNewMessage { message = new Message { @@ -471,7 +471,7 @@ namespace TL peer_id = new PeerChat { chat_id = chat_id }, fwd_from = fwd_from, via_bot_id = via_bot_id, ttl_period = ttl_period }, pts = pts, pts_count = pts_count - } }; } + } ]; } partial class InputEncryptedChat { public static implicit operator int(InputEncryptedChat chat) => chat.chat_id; public static implicit operator InputEncryptedChat(EncryptedChatBase chat) => new() { chat_id = chat.ID, access_hash = chat.AccessHash }; } @@ -580,8 +580,8 @@ namespace TL } partial class Updates_ChannelDifferenceEmpty { - public override MessageBase[] NewMessages => Array.Empty(); - public override Update[] OtherUpdates => Array.Empty(); + public override MessageBase[] NewMessages => []; + public override Update[] OtherUpdates => []; public override bool Final => flags.HasFlag(Flags.final); public override int Timeout => timeout; } diff --git a/src/TL.Schema.cs b/src/TL.Schema.cs index 7e7e52a..855998b 100644 --- a/src/TL.Schema.cs +++ b/src/TL.Schema.cs @@ -5183,9 +5183,9 @@ namespace TL /// Number of sent updates public int seq; - public override MessageBase[] NewMessages => Array.Empty(); - public override EncryptedMessageBase[] NewEncryptedMessages => Array.Empty(); - public override Update[] OtherUpdates => Array.Empty(); + public override MessageBase[] NewMessages => []; + public override EncryptedMessageBase[] NewEncryptedMessages => []; + public override Update[] OtherUpdates => []; /// returns a or for the given Peer public override IPeerInfo UserOrChat(Peer peer) => null; } diff --git a/src/TlsStream.cs b/src/TlsStream.cs index 97ed1e5..5b7b7ac 100644 --- a/src/TlsStream.cs +++ b/src/TlsStream.cs @@ -15,10 +15,10 @@ namespace WTelegram { public TlsStream(Stream innerStream) : base(innerStream) { } private int _tlsFrameleft; - private readonly byte[] _tlsSendHeader = new byte[] { 0x17, 0x03, 0x03, 0, 0 }; + private readonly byte[] _tlsSendHeader = [0x17, 0x03, 0x03, 0, 0]; private readonly byte[] _tlsReadHeader = new byte[5]; - static readonly byte[] TlsServerHello3 = new byte[] { 0x14, 0x03, 0x03, 0x00, 0x01, 0x01, 0x17, 0x03, 0x03 }; - static readonly byte[] TlsClientPrefix = new byte[] { 0x14, 0x03, 0x03, 0x00, 0x01, 0x01 }; + static readonly byte[] TlsServerHello3 = [0x14, 0x03, 0x03, 0x00, 0x01, 0x01, 0x17, 0x03, 0x03]; + static readonly byte[] TlsClientPrefix = [0x14, 0x03, 0x03, 0x00, 0x01, 0x01]; public override async Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken ct) { @@ -85,20 +85,20 @@ namespace WTelegram throw new WTException("TLS Handshake failed"); } - static readonly byte[] TlsClientHello1 = new byte[11] { // https://tls13.xargs.org/#client-hello/annotated - 0x16, 0x03, 0x01, 0x02, 0x00, 0x01, 0x00, 0x01, 0xfc, 0x03, 0x03 }; + static readonly byte[] TlsClientHello1 = [ // https://tls13.xargs.org/#client-hello/annotated + 0x16, 0x03, 0x01, 0x02, 0x00, 0x01, 0x00, 0x01, 0xfc, 0x03, 0x03 ]; // digest[32] // 0x20 // random[32] // 0x00, 0x20 // grease(0) GREASE are two identical bytes ending with nibble 'A' - static readonly byte[] TlsClientHello2 = new byte[34] { + static readonly byte[] TlsClientHello2 = [ 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30, 0xcc, 0xa9, 0xcc, 0xa8, 0xc0, 0x13, 0xc0, 0x14, 0x00, 0x9c, 0x00, 0x9d, 0x00, 0x2f, 0x00, 0x35, - 0x01, 0x00, 0x01, 0x93 }; + 0x01, 0x00, 0x01, 0x93 ]; // grease(2) // 0x00, 0x00 - static readonly byte[] TlsClientHello3 = new byte[134] { + static readonly byte[] TlsClientHello3 = [ // 0x00, 0x00, len { len { 0x00 len { domain } } } len is 16-bit big-endian length of the following block of data 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x08, 0x4A, 0x4A,/*=grease(4)*/ 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, @@ -114,10 +114,10 @@ namespace WTelegram 0x00, 0x33, 0x00, 0x2b, 0x00, 0x29, 0x4A, 0x4A,/*=grease(4) */ 0x00, 0x01, 0x00, 0x00, 0x1d, 0x00, 0x20, /* random[32] */ 0x44, 0x69, 0x00, 0x05, 0x00, 0x03, 0x02, 0x68, 0x32, 0xff, 0x01, 0x00, 0x01, 0x00, - }; + ]; // grease(3) - static readonly byte[] TlsClientHello4 = new byte[5] { - 0x00, 0x01, 0x00, 0x00, 0x15 }; + static readonly byte[] TlsClientHello4 = [ + 0x00, 0x01, 0x00, 0x00, 0x15 ]; // len { padding } padding with NUL bytes to reach 517 bytes static byte[] TlsClientHello(byte[] key, byte[] domain)