From dcdb2405b59a0a08b77cbc11046c39065ccd0c3c Mon Sep 17 00:00:00 2001 From: Lonami Date: Wed, 14 Oct 2015 18:16:27 +0200 Subject: [PATCH] More requests added More requests added: - GetDialogsRequest - GetFileRequest - GetHistoryRequest - GetNearestDcRequest - GetUsersRequest --- TLSharp.Core/Requests/GetDialogsRequest.cs | 90 ++++++++++++++++++++ TLSharp.Core/Requests/GetFileRequest.cs | 49 +++++++++++ TLSharp.Core/Requests/GetHistoryRequest.cs | 72 ++++++++++++++++ TLSharp.Core/Requests/GetNearestDcRequest.cs | 45 ++++++++++ TLSharp.Core/Requests/GetUsersRequest.cs | 49 +++++++++++ TLSharp.Core/TLSharp.Core.csproj | 5 ++ TLSharp.Core/TelegramClient.cs | 31 ++++++- 7 files changed, 338 insertions(+), 3 deletions(-) create mode 100644 TLSharp.Core/Requests/GetDialogsRequest.cs create mode 100644 TLSharp.Core/Requests/GetFileRequest.cs create mode 100644 TLSharp.Core/Requests/GetHistoryRequest.cs create mode 100644 TLSharp.Core/Requests/GetNearestDcRequest.cs create mode 100644 TLSharp.Core/Requests/GetUsersRequest.cs diff --git a/TLSharp.Core/Requests/GetDialogsRequest.cs b/TLSharp.Core/Requests/GetDialogsRequest.cs new file mode 100644 index 0000000..e6c6f6f --- /dev/null +++ b/TLSharp.Core/Requests/GetDialogsRequest.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.IO; +using TLSharp.Core.MTProto; + +namespace TLSharp.Core.Requests +{ + public class GetDialogsRequest : MTProtoRequest + { + int _offset; + int _max_id; + int _limit; + + public List dialogs; + public List messages; + public List chats; + public List users; + + public GetDialogsRequest(int offset, int max_id, int limit) + { + _offset = offset; + _max_id = max_id; + _limit = limit; + } + + public override void OnSend(BinaryWriter writer) + { + writer.Write(0xeccf1df6); + writer.Write(_offset); + writer.Write(_max_id); + writer.Write(_limit); + } + + public override void OnResponse(BinaryReader reader) + { + bool dialogsSlice = reader.ReadUInt32() == 0x71e094f3; // else dialogs#15ba6c40 + + if (dialogsSlice) reader.ReadInt32(); // count + + // dialogs + var result = reader.ReadUInt32(); // vector#1cb5c415 + int dialogs_len = reader.ReadInt32(); + dialogs = new List(dialogs_len); + for (int dialogs_index = 0; dialogs_index < dialogs_len; dialogs_index++) + { + Dialog dialog_element; + dialog_element = TL.Parse(reader); + dialogs.Add(dialog_element); + } + // messages + var count = reader.ReadInt32(); + int messages_len = reader.ReadInt32(); + messages = new List(messages_len); + for (int message_index = 0; message_index < messages_len; message_index++) + { + Message messages_element; + messages_element = TL.Parse(reader); + messages.Add(messages_element); + } + // chats + count = reader.ReadInt32(); + int chats_len = reader.ReadInt32(); + chats = new List(chats_len); + for (int chat_index = 0; chat_index < chats_len; chat_index++) + { + Chat chats_element; + chats_element = TL.Parse(reader); + chats.Add(chats_element); + } + // users + count = reader.ReadInt32(); + int users_len = reader.ReadInt32(); + users = new List(users_len); + for (int users_index = 0; users_index < users_len; users_index++) + { + User users_element; + users_element = TL.Parse(reader); + users.Add(users_element); + } + } + + public override void OnException(Exception exception) + { + throw new NotImplementedException(); + } + + public override bool Confirmed => true; + public override bool Responded { get; } + } +} diff --git a/TLSharp.Core/Requests/GetFileRequest.cs b/TLSharp.Core/Requests/GetFileRequest.cs new file mode 100644 index 0000000..ef4741d --- /dev/null +++ b/TLSharp.Core/Requests/GetFileRequest.cs @@ -0,0 +1,49 @@ +using System; +using System.IO; +using TLSharp.Core.MTProto; + +namespace TLSharp.Core.Requests +{ + class GetFileRequest : MTProtoRequest + { + InputFileLocation _location; + int _offset; + int _limit; + + public storage_FileType type; + public int mtime; + public byte[] bytes; + + public GetFileRequest(InputFileLocation location, int offset, int limit) + { + _location = location; + _offset = offset; + _limit = limit; + } + + public override void OnSend(BinaryWriter writer) + { + writer.Write(0xe3a6cfb5); + _location.Write(writer); + writer.Write(_offset); + writer.Write(_limit); + } + + public override void OnResponse(BinaryReader reader) + { + var code = reader.ReadUInt32(); // upload.file#96a18d5 + + type = TL.Parse(reader); + mtime = reader.ReadInt32(); + bytes = reader.ReadBytes(_limit); + } + + public override void OnException(Exception exception) + { + throw new NotImplementedException(); + } + + public override bool Confirmed => true; + public override bool Responded { get; } + } +} diff --git a/TLSharp.Core/Requests/GetHistoryRequest.cs b/TLSharp.Core/Requests/GetHistoryRequest.cs new file mode 100644 index 0000000..98e85df --- /dev/null +++ b/TLSharp.Core/Requests/GetHistoryRequest.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.IO; +using TLSharp.Core.MTProto; + +namespace TLSharp.Core.Requests +{ + class GetHistoryRequest : MTProtoRequest + { + InputPeer _peer; + int _offset; + int _max_id; + int _limit; + + public List messages; + public List chats; + public List users; + + public GetHistoryRequest(InputPeer peer, int offset, int max_id, int limit) + { + _peer = peer; + _offset = offset; + _max_id = max_id; + _limit = limit; + } + + public override void OnSend(BinaryWriter writer) + { + writer.Write(0x92a1df2f); + _peer.Write(writer); + writer.Write(_offset); + writer.Write(_max_id); + writer.Write(_limit); + } + + public override void OnResponse(BinaryReader reader) + { + bool messagesSlice = reader.ReadUInt32() == 0xb446ae3; // else messages#8c718e87 + + if (messagesSlice) reader.ReadInt32(); // count + + // messages + var result = reader.ReadUInt32(); // vector#1cb5c415 + int messages_len = reader.ReadInt32(); + messages = new List(messages_len); + for (int i = 0; i < messages_len; i++) + messages.Add(TL.Parse(reader)); + + // chats + reader.ReadUInt32(); + int chats_len = reader.ReadInt32(); + chats = new List(chats_len); + for (int i = 0; i < chats_len; i++) + chats.Add(TL.Parse(reader)); + + // users + reader.ReadUInt32(); + int users_len = reader.ReadInt32(); + users = new List(users_len); + for (int i = 0; i < users_len; i++) + users.Add(TL.Parse(reader)); + } + + public override void OnException(Exception exception) + { + throw new NotImplementedException(); + } + + public override bool Confirmed => true; + public override bool Responded { get; } + } +} diff --git a/TLSharp.Core/Requests/GetNearestDcRequest.cs b/TLSharp.Core/Requests/GetNearestDcRequest.cs new file mode 100644 index 0000000..7dae67e --- /dev/null +++ b/TLSharp.Core/Requests/GetNearestDcRequest.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TLSharp.Core.MTProto; + +namespace TLSharp.Core.Requests +{ + public class GetNearestDcRequest : MTProtoRequest + { + public string country; + public int this_dc; + public int nearest_dc; + + public GetNearestDcRequest() { } + + public override void OnSend(BinaryWriter writer) + { + writer.Write(0x1fb33026); + } + + public override void OnResponse(BinaryReader reader) + { + var code = reader.ReadUInt32(); + + country = Serializers.String.read(reader); + this_dc = reader.ReadInt32(); + nearest_dc = reader.ReadInt32(); + + System.Diagnostics.Debug.WriteLine("country: " + country); + System.Diagnostics.Debug.WriteLine("this_dc: " + this_dc); + System.Diagnostics.Debug.WriteLine("nearest: " + nearest_dc); + } + + public override void OnException(Exception exception) + { + throw new NotImplementedException(); + } + + public override bool Confirmed => true; + public override bool Responded { get; } + } +} diff --git a/TLSharp.Core/Requests/GetUsersRequest.cs b/TLSharp.Core/Requests/GetUsersRequest.cs new file mode 100644 index 0000000..4cf4ad2 --- /dev/null +++ b/TLSharp.Core/Requests/GetUsersRequest.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.IO; +using TLSharp.Core.MTProto; + +namespace TLSharp.Core.Requests +{ + class GetUsersRequest : MTProtoRequest + { + List _id; + + public List users; + + public GetUsersRequest(List id) + { + _id = id; + } + + public override void OnSend(BinaryWriter writer) + { + writer.Write(0xd91a548); + writer.Write(0x1cb5c415); // vector#1cb5c415 + writer.Write(_id.Count); // vector length + foreach (var id in _id) + id.Write(writer); + } + + public override void OnResponse(BinaryReader reader) + { + var code = reader.ReadUInt32(); + var result = reader.ReadInt32(); // vector#1cb5c415 + if (result != 0) + { + int users_len = reader.ReadInt32(); // vector length + users = new List(users_len); + for (int i = 0; i < users_len; i++) + users.Add(TL.Parse(reader)); + } + } + + public override void OnException(Exception exception) + { + throw new NotImplementedException(); + } + + public override bool Confirmed => true; + public override bool Responded { get; } + } +} diff --git a/TLSharp.Core/TLSharp.Core.csproj b/TLSharp.Core/TLSharp.Core.csproj index 42240d8..fd4c3db 100644 --- a/TLSharp.Core/TLSharp.Core.csproj +++ b/TLSharp.Core/TLSharp.Core.csproj @@ -67,6 +67,11 @@ + + + + + diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 859494b..add4c2b 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using TLSharp.Core.Auth; @@ -18,7 +19,12 @@ namespace TLSharp.Core private int _apiId = 19474; private Session _session; - public TelegramClient(ISessionStore store, string sessionUserId) + public User loggedUser { get { return _session.User; } } + + public List chats; + public List users; + + public TelegramClient(ISessionStore store, string sessionUserId) { if (_apiId == 0) throw new InvalidOperationException("Your API_ID is invalid. Do a configuration first https://github.com/sochix/TLSharp#quick-configuration"); @@ -107,7 +113,26 @@ namespace TLSharp.Core await _sender.Send(request); await _sender.Recieve(request); - } - } + + public async Task LoadChatsAndUsers(int offset, int max_id, int limit) + { + // GetDialogs + var request = new GetDialogsRequest(offset, max_id, limit); + await _sender.Send(request); + await _sender.Recieve(request); + + chats = request.chats; + users = request.users; + } + + public async Task> GetHistory(int user_id, int offset, int max_id, int limit) + { + var request = new GetHistoryRequest(new InputPeerContactConstructor(user_id), offset, max_id, limit); + await _sender.Send(request); + await _sender.Recieve(request); + + return request.messages; + } + } }