diff --git a/TLSharp.Core/MTProto/TL.cs b/TLSharp.Core/MTProto/TL.cs index cee1be6..6e98dd7 100644 --- a/TLSharp.Core/MTProto/TL.cs +++ b/TLSharp.Core/MTProto/TL.cs @@ -5960,6 +5960,12 @@ namespace TLSharp.Core.MTProto } } + public class ContactsContacts + { + public IList Contacts { get; set; } + public IList Users { get; set; } + } + public class MessageDialogs { public int? Count { get; set; } diff --git a/TLSharp.Core/Requests/GetContactRequest b/TLSharp.Core/Requests/GetContactRequest deleted file mode 100644 index ba57235..0000000 --- a/TLSharp.Core/Requests/GetContactRequest +++ /dev/null @@ -1,72 +0,0 @@ -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 GetContactRequest : MTProtoRequest - { - private List currentContacts { get; set; } - - public List contacts; - public List users; - - public GetContactRequest(List currentContactsID = null) - { - currentContacts = currentContactsID; - } - - public override void OnSend(BinaryWriter writer) - { - writer.Write(0x22c6aa08); - if (currentContacts == null) - Serializers.String.write(writer, ""); - else - { - string hash = ""; - foreach (var currentUserID in currentContacts) - { - var md5 = Utils.Helpers.md5(currentUserID.ToString()); - hash += md5 + ","; - } - hash = hash.Length > 0 ? hash.Remove(hash.LastIndexOf(','), 1) : Utils.Helpers.md5( hash); - Serializers.String.write(writer, hash); - } - } - - public override void OnResponse(BinaryReader reader) - { - var code = reader.ReadUInt32(); - var result = reader.ReadInt32(); // vector code - int contact_len = reader.ReadInt32(); - this.contacts = new List(contact_len); - for (int imported_index = 0; imported_index < contact_len; imported_index++) - { - Contact imported_element; - imported_element = TL.Parse(reader); - this.contacts.Add(imported_element); - } - reader.ReadInt32(); // vector code - int users_len = reader.ReadInt32(); - this.users = new List(users_len); - for (int users_index = 0; users_index < users_len; users_index++) - { - UserContactConstructor users_element; - users_element = TL.Parse(reader); - this.users.Add(users_element); - } - } - - public override void OnException(Exception exception) - { - throw new NotImplementedException(); - } - public override bool Confirmed { get { return true; } } - private readonly bool responded; - public override bool Responded { get { return responded; } } - } -} diff --git a/TLSharp.Core/Requests/GetContactsRequest.cs b/TLSharp.Core/Requests/GetContactsRequest.cs new file mode 100644 index 0000000..dd7aa5d --- /dev/null +++ b/TLSharp.Core/Requests/GetContactsRequest.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TLSharp.Core.MTProto; +using System.Security.Cryptography; + +namespace TLSharp.Core.Requests +{ + public class GetContactsRequest : MTProtoRequest + { + private List CurrentContacts { get; set; } + + public List Contacts; + public List Users; + + public GetContactsRequest(IList currentContacts = null) + { + if (currentContacts != null) + { + CurrentContacts = currentContacts.ToList(); + CurrentContacts.Sort(); + } + } + + public override void OnSend(BinaryWriter writer) + { + writer.Write(0x22c6aa08); + if (CurrentContacts == null) + Serializers.String.write(writer, ""); + else + { + // create CSV of contactids and calculate md5 hash + string hash; + var list = string.Join(",", CurrentContacts); + using (var md5 = MD5.Create()) + { + var retVal = md5.ComputeHash(Encoding.UTF8.GetBytes(list)); + var sb = new StringBuilder(); + foreach (var t in retVal) + { + sb.Append(t.ToString("x2")); + } + hash = sb.ToString(); + } + + Serializers.String.write(writer, hash); + } + } + + public override void OnResponse(BinaryReader reader) + { + var code = reader.ReadUInt32(); + // if contactsNotModified then exit + if (code == 0xb74ba9d2) return; + + reader.ReadInt32(); // vector code + var contactLen = reader.ReadInt32(); + Contacts = new List(contactLen); + for (var importedIndex = 0; importedIndex < contactLen; importedIndex++) + { + var importedElement = TL.Parse(reader); + this.Contacts.Add(importedElement); + } + reader.ReadInt32(); // vector code + var usersLen = reader.ReadInt32(); + Users = new List(usersLen); + for (var usersIndex = 0; usersIndex < usersLen; usersIndex++) + { + var usersElement = TL.Parse(reader); + this.Users.Add(usersElement); + } + } + + public override void OnException(Exception exception) + { + throw new NotImplementedException(); + } + public override bool Confirmed { get { return true; } } + private readonly bool _responded; + public override bool Responded { get { return _responded; } } + } +} diff --git a/TLSharp.Core/TLSharp.Core.csproj b/TLSharp.Core/TLSharp.Core.csproj index 67821e5..66a2d8f 100644 --- a/TLSharp.Core/TLSharp.Core.csproj +++ b/TLSharp.Core/TLSharp.Core.csproj @@ -69,6 +69,7 @@ + @@ -100,4 +101,4 @@ --> - + \ No newline at end of file diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 7c4fd90..8c71f32 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -105,8 +105,6 @@ namespace TLSharp.Core request = new AuthSendCodeRequest(phoneNumber, (int)tokenDestination, _apiId, _apiHash, "en"); try { - - await _sender.Send(request); await _sender.Receive(request); @@ -282,5 +280,19 @@ namespace TLSharp.Core return regex.IsMatch(number); } + + public async Task GetContacts(IList contactIds = null) + { + var request = new GetContactsRequest(contactIds); + await _sender.Send(request); + await _sender.Receive(request); + + return new ContactsContacts + { + Contacts = request.Contacts, + Users = request.Users, + }; + } + } }