From 8026bd79dca8ac089fd41a8423da9382feea83fb Mon Sep 17 00:00:00 2001 From: David Woakes Date: Tue, 19 Jul 2016 14:47:08 +0100 Subject: [PATCH 1/3] Fix issues with GetDialogs Default value of max_id = 0 returns all dialogs Should return a class with lists of dialog, messages, chats and users. Add UserForeignConstructor to list of constructors Add missing detail to DialogConstructor (PeerNotifySettings) Unpack datastream completely to avoid issue with Ionic exception causing problems in userForeign parsing Use more recent code for Dialog construction --- TLSharp.Core/MTProto/TL.cs | 23 ++++++++++++++++++---- TLSharp.Core/Network/MtProtoSender.cs | 15 ++++++++++---- TLSharp.Core/Requests/GetDialogsRequest.cs | 9 +++++---- TLSharp.Core/TelegramClient.cs | 10 ++++++++-- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/TLSharp.Core/MTProto/TL.cs b/TLSharp.Core/MTProto/TL.cs index 2a0e5a7..cee1be6 100644 --- a/TLSharp.Core/MTProto/TL.cs +++ b/TLSharp.Core/MTProto/TL.cs @@ -537,6 +537,9 @@ namespace TLSharp.Core.MTProto {0x427425e7, typeof (AudioConstructor)}, {0x36f8c871, typeof (DocumentEmptyConstructor)}, {0xf9a39f4f, typeof (DocumentConstructor)}, + {0xab3a99ac, typeof (DialogConstructor)}, + {0xd9ccc4ef, typeof (UserRequestConstructor)}, + {0x075cf7a8, typeof (UserForeignConstructor)}, }; public static TLObject Parse(BinaryReader reader, uint code) @@ -1035,9 +1038,9 @@ namespace TLSharp.Core.MTProto return new MessageActionChatDeleteUserConstructor(user_id); } - public static Dialog dialog(Peer peer, int top_message, int unread_count) + public static Dialog dialog(Peer peer, int top_message, int unread_count, PeerNotifySettings peerNotifySettings) { - return new DialogConstructor(peer, top_message, unread_count); + return new DialogConstructor(peer, top_message, unread_count, peerNotifySettings); } public static Photo photoEmpty(long id) @@ -5957,23 +5960,33 @@ namespace TLSharp.Core.MTProto } } + public class MessageDialogs + { + public int? Count { get; set; } + public List Dialogs { get; set; } + public List Messages { get; set; } + public List Chats { get; set; } + public List Users { get; set; } + } public class DialogConstructor : Dialog { public Peer peer; public int top_message; public int unread_count; + public PeerNotifySettings peerNotifySettings; public DialogConstructor() { } - public DialogConstructor(Peer peer, int top_message, int unread_count) + public DialogConstructor(Peer peer, int top_message, int unread_count, PeerNotifySettings peerNotifySettings) { this.peer = peer; this.top_message = top_message; this.unread_count = unread_count; + this.peerNotifySettings = peerNotifySettings; } @@ -5984,10 +5997,11 @@ namespace TLSharp.Core.MTProto public override void Write(BinaryWriter writer) { - writer.Write(0x214a8cdf); + writer.Write(0xab3a99ac); this.peer.Write(writer); writer.Write(this.top_message); writer.Write(this.unread_count); + this.peerNotifySettings.Write(writer); } public override void Read(BinaryReader reader) @@ -5995,6 +6009,7 @@ namespace TLSharp.Core.MTProto this.peer = TL.Parse(reader); this.top_message = reader.ReadInt32(); this.unread_count = reader.ReadInt32(); + this.peerNotifySettings = TL.Parse(reader); } public override string ToString() diff --git a/TLSharp.Core/Network/MtProtoSender.cs b/TLSharp.Core/Network/MtProtoSender.cs index 1dfa02e..3f9ff94 100644 --- a/TLSharp.Core/Network/MtProtoSender.cs +++ b/TLSharp.Core/Network/MtProtoSender.cs @@ -295,11 +295,18 @@ namespace TLSharp.Core.Network { // gzip_packed byte[] packedData = Serializers.Bytes.read(messageReader); - using (var packedStream = new MemoryStream(packedData, false)) - using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress)) - using (var compressedReader = new BinaryReader(zipStream)) + using (var ms = new MemoryStream()) { - request.OnResponse(compressedReader); + using (var packedStream = new MemoryStream(packedData, false)) + using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress)) + { + zipStream.CopyTo(ms); + ms.Position = 0; + } + using (var compressedReader = new BinaryReader(ms)) + { + request.OnResponse(compressedReader); + } } } catch (ZlibException ex) diff --git a/TLSharp.Core/Requests/GetDialogsRequest.cs b/TLSharp.Core/Requests/GetDialogsRequest.cs index afa55a3..7ab35ed 100644 --- a/TLSharp.Core/Requests/GetDialogsRequest.cs +++ b/TLSharp.Core/Requests/GetDialogsRequest.cs @@ -11,6 +11,7 @@ namespace TLSharp.Core.Requests int _max_id; int _limit; + public int count; public List dialogs; public List messages; public List chats; @@ -35,7 +36,7 @@ namespace TLSharp.Core.Requests { bool dialogsSlice = reader.ReadUInt32() == 0x71e094f3; // else dialogs#15ba6c40 - if (dialogsSlice) reader.ReadInt32(); // count + if (dialogsSlice) count = reader.ReadInt32(); // count // dialogs var result = reader.ReadUInt32(); // vector#1cb5c415 @@ -48,7 +49,7 @@ namespace TLSharp.Core.Requests dialogs.Add(dialog_element); } // messages - var count = reader.ReadInt32(); + result = reader.ReadUInt32(); // vector#1cb5c415 int messages_len = reader.ReadInt32(); messages = new List(messages_len); for (int message_index = 0; message_index < messages_len; message_index++) @@ -58,7 +59,7 @@ namespace TLSharp.Core.Requests messages.Add(messages_element); } // chats - count = reader.ReadInt32(); + result = reader.ReadUInt32(); // vector#1cb5c415 int chats_len = reader.ReadInt32(); chats = new List(chats_len); for (int chat_index = 0; chat_index < chats_len; chat_index++) @@ -68,7 +69,7 @@ namespace TLSharp.Core.Requests chats.Add(chats_element); } // users - count = reader.ReadInt32(); + result = reader.ReadUInt32(); // vector#1cb5c415 int users_len = reader.ReadInt32(); users = new List(users_len); for (int users_index = 0; users_index < users_len; users_index++) diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 4364e7f..71dc520 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -250,13 +250,19 @@ namespace TLSharp.Core return Tuple.Create(request.type, request.bytes); } - public async Task> GetDialogs(int offset, int limit, int max_id = -1) + public async Task GetDialogs(int offset, int limit, int max_id = 0) { var request = new GetDialogsRequest(offset, max_id, limit); await _sender.Send(request); await _sender.Recieve(request); - return request.dialogs; + return new MessageDialogs + { + Dialogs = request.dialogs, + Messages = request.messages, + Chats = request.chats, + Users = request.users, + }; } public async Task GetUserFull(int user_id) From bfbc125bbe3c5e66b5d3c090a01228af717ddb5c Mon Sep 17 00:00:00 2001 From: David Woakes Date: Tue, 19 Jul 2016 15:18:29 +0100 Subject: [PATCH 2/3] Allow token to be sent via SMS or telegram --- TLSharp.Core/TelegramClient.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 71dc520..dea9264 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -23,6 +23,8 @@ namespace TLSharp.Core private Session _session; private List dcOptions; + public enum sms_type { numeric_code_via_sms = 0, numeric_code_via_telegram = 5 } + public TelegramClient(ISessionStore store, string sessionUserId, int apiId, string apiHash) { _apiHash = apiHash; @@ -92,7 +94,7 @@ namespace TLSharp.Core return authCheckPhoneRequest._phoneRegistered; } - public async Task SendCodeRequest(string phoneNumber) + public async Task SendCodeRequest(string phoneNumber, sms_type tokenDestination = sms_type.numeric_code_via_telegram) { var completed = false; @@ -100,7 +102,7 @@ namespace TLSharp.Core while (!completed) { - request = new AuthSendCodeRequest(phoneNumber, 5, _apiId, _apiHash, "en"); + request = new AuthSendCodeRequest(phoneNumber, (int)tokenDestination, _apiId, _apiHash, "en"); try { From 7c8d5e089e8a8d41d633a9a7f4141d3d47e237ed Mon Sep 17 00:00:00 2001 From: David Woakes Date: Wed, 20 Jul 2016 07:26:55 +0100 Subject: [PATCH 3/3] Make line endings consistent (LF) --- TLSharp.Core/TelegramClient.cs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 71dc520..774e80e 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -23,6 +23,8 @@ namespace TLSharp.Core private Session _session; private List dcOptions; + public enum sms_type { numeric_code_via_sms = 0, numeric_code_via_telegram = 5 } + public TelegramClient(ISessionStore store, string sessionUserId, int apiId, string apiHash) { _apiHash = apiHash; @@ -92,7 +94,7 @@ namespace TLSharp.Core return authCheckPhoneRequest._phoneRegistered; } - public async Task SendCodeRequest(string phoneNumber) + public async Task SendCodeRequest(string phoneNumber, sms_type tokenDestination = sms_type.numeric_code_via_telegram) { var completed = false; @@ -100,7 +102,7 @@ namespace TLSharp.Core while (!completed) { - request = new AuthSendCodeRequest(phoneNumber, 5, _apiId, _apiHash, "en"); + request = new AuthSendCodeRequest(phoneNumber, (int)tokenDestination, _apiId, _apiHash, "en"); try { @@ -239,8 +241,8 @@ namespace TLSharp.Core await _sender.Recieve(request); return request.messages; - } - + } + public async Task> GetFile(long volume_id, int local_id, long secret, int offset, int limit) { var request = new GetFileRequest(new InputFileLocationConstructor(volume_id, local_id, secret), offset, limit); @@ -248,20 +250,20 @@ namespace TLSharp.Core await _sender.Recieve(request); return Tuple.Create(request.type, request.bytes); - } - + } + public async Task GetDialogs(int offset, int limit, int max_id = 0) { var request = new GetDialogsRequest(offset, max_id, limit); await _sender.Send(request); await _sender.Recieve(request); - return new MessageDialogs - { - Dialogs = request.dialogs, - Messages = request.messages, - Chats = request.chats, - Users = request.users, + return new MessageDialogs + { + Dialogs = request.dialogs, + Messages = request.messages, + Chats = request.chats, + Users = request.users, }; }