private TelegramClient SpinUpClient(int dc) { object dcLock; lock (_spinUpLocks) { if (_spinUpLocks.ContainsKey(dc)) { dcLock = _spinUpLocks[dc]; } else { dcLock = new object(); _spinUpLocks[dc] = dcLock; } } lock (dcLock) { var client = GetClientInternal(dc); if (client != null) { return(client); } if (_config == null) { DebugPrint("Config is null. Unable to resolve DC information."); return(null); } var dcOption = _config.DcOptions.Cast <DcOption>().FirstOrDefault(x => x.Id == dc); if (dcOption == null) { DebugPrint("Unable to find DC for DC ID: " + dc); return(null); } var dcCached = DcDatabase.Get(dc); var transportConfig = new TcpClientTransportConfig(dcOption.IpAddress, (int)dcOption.Port); SharpMTProto.Authentication.AuthInfo authInfo; AuthExportedAuthorization exportedAuth = null; if (dcCached == null) { DebugPrint("Looks like we'll have to authenticate a new connection to: " + ObjectDumper.Dump(dcOption)); DebugPrint(">>>>>>>> Exporting auth..."); using (var clientDisposable = new TelegramClientDisposable(this)) { exportedAuth = (AuthExportedAuthorization)TelegramUtils.RunSynchronously(clientDisposable.Client.Methods.AuthExportAuthorizationAsync( new SharpTelegram.Schema.Layer18.AuthExportAuthorizationArgs { DcId = (uint)dc, })); } DebugPrint(">>>>>>> Got exported auth."); if (exportedAuth == null) { DebugPrint("Exported auth is null for some weird reason. DC ID: " + dc); return(null); } DebugPrint(">>>>>>> Fetching new authentication..."); authInfo = TelegramUtils.RunSynchronously(FetchNewAuthentication(transportConfig)); } else { authInfo = new SharpMTProto.Authentication.AuthInfo(dcCached.Key, BitConverter.ToUInt64(dcCached.Salt, 0)); } DebugPrint(">>>>>>>> Starting new client..."); var newClient = new TelegramClient(transportConfig, new ConnectionConfig(authInfo.AuthKey, authInfo.Salt), AppInfo); newClient.OnClosedInternally += (sender, e) => { DebugPrint("Removing connection to DC: " + dc); lock (_activeClients) { _activeClients.Remove(dc); } }; var result = TelegramUtils.RunSynchronously(newClient.Connect()); if (result != MTProtoConnectResult.Success) { DebugPrint("Failed to connect to DC: " + dc + ": " + result); return(null); } if (exportedAuth != null) { TelegramUtils.RunSynchronously(newClient.Methods.AuthImportAuthorizationAsync(new AuthImportAuthorizationArgs { Id = exportedAuth.Id, Bytes = exportedAuth.Bytes, })); } PingDelay(client, 60); lock (_activeClients) { _activeClients[dc] = newClient; } if (dcCached == null) { DcDatabase.Set(new TelegramDc { Dc = dc, Key = authInfo.AuthKey, Salt = BitConverter.GetBytes(authInfo.Salt), }); } return(newClient); } }
private byte[] FetchFileBytes(FileLocation fileLocation) { if (fileLocation.DcId == _settings.NearestDcId) { using (var clientDisposable = new TelegramClientDisposable(this)) { return FetchFileBytes(clientDisposable.Client, fileLocation); } } else { try { var client = GetClient((int)fileLocation.DcId); return FetchFileBytes(client, fileLocation); } catch (Exception ex) { DebugPrint("Failed to obtain client from DC manager: " + ex); return null; } } }
private void MarkMessageAsRecevied(uint messageId, TelegramClient client = null) { Action<TelegramClient> markAsReceived = theClient => { TelegramUtils.RunSynchronously(theClient.Methods.MessagesReceivedMessagesAsync( new MessagesReceivedMessagesArgs { MaxId = messageId })); }; if (client == null) { using (var disposableClient = new TelegramClientDisposable(this)) { markAsReceived(disposableClient.Client); } } else { markAsReceived(client); } }
private long GetUpdatedLastOnline(string id) { if (_dialogs == null) return 0; foreach (var user in _dialogs.Users) { var userId = TelegramUtils.GetUserId(user); if (userId == id) { var inputUser = TelegramUtils.CastUserToInputUser(user); if (inputUser != null) { using (var clientDisposable = new TelegramClientDisposable(this)) { var updatedUser = TelegramUtils.RunSynchronously(GetUser(inputUser, clientDisposable.Client)); return TelegramUtils.GetLastSeenTime(updatedUser); } } } } DebugPrint("Could not get last online for user: " + id); return 0; }
private List<IUser> GetUpdatedUsersOfAllDialogs() { if (_dialogs == null) return null; var peerUsers = _dialogs.Dialogs.Select(x => (x as Dialog).Peer).OfType<PeerUser>(); var users = new List<IUser>(); foreach (var peer in peerUsers) { var peerUserId = peer.UserId.ToString(CultureInfo.InvariantCulture); foreach (var user in _dialogs.Users) { var userId = TelegramUtils.GetUserId(user); if (peerUserId == userId) { users.Add(user); } } } using (var clientDisposable = new TelegramClientDisposable(this)) { var inputUsers = users.Select(x => TelegramUtils.CastUserToInputUser(x)).Where(d => d != null).ToList(); var updatedUsers = TelegramUtils.RunSynchronously(GetUsers(inputUsers, clientDisposable.Client)); return updatedUsers; } }
public override void RefreshPhoneBookContacts() { Utils.DebugPrint("Phone book contacts have been updated! Sending information to Telegram servers..."); var contacts = PhoneBook.PhoneBookContacts; var inputContacts = new List<IInputContact>(); foreach (var contact in contacts) { foreach (var phoneNumber in contact.PhoneNumbers) { inputContacts.Add(new InputPhoneContact { ClientId = ulong.Parse(contact.ContactId), Phone = phoneNumber.Number, FirstName = contact.FirstName, LastName = contact.LastName, }); } } if (!inputContacts.Any()) { Utils.DebugPrint("There are no input contacts!"); return; } try { using (var client = new TelegramClientDisposable(this)) { TelegramUtils.RunSynchronously(client.Client.Methods.ContactsImportContactsAsync( new ContactsImportContactsArgs { Contacts = inputContacts, Replace = false, })); Utils.DebugPrint("Fetching newest dialogs..."); GetDialogs(client.Client); } } catch (Exception ex) { Utils.DebugPrint("Failed to update contacts: " + ex); } }
public override Task GetBubbleGroupPartyParticipants(BubbleGroup group, Action<DisaParticipant[]> result) { return Task.Factory.StartNew(() => { using (var clientDisposable = new TelegramClientDisposable(this)) { var fullChat = (MessagesChatFull)TelegramUtils.RunSynchronously(clientDisposable.Client.Methods.MessagesGetFullChatAsync(new MessagesGetFullChatArgs { ChatId = uint.Parse(group.Address) })); var chatFull = (ChatFull)fullChat.FullChat; var participants = chatFull.Participants as ChatParticipants; if (participants == null) { result(null); } else { var users = participants.Participants.Select(x => GetUser(fullChat.Users, ((ChatParticipant)x).UserId.ToString(CultureInfo.InvariantCulture))); var disaParticipants = users.Select(x => new DisaParticipant( TelegramUtils.GetUserName(x), TelegramUtils.GetUserId(x))).ToArray(); result(disaParticipants); } } }); }
private TelegramClient SpinUpClient(int dc) { object dcLock; lock (_spinUpLocks) { if (_spinUpLocks.ContainsKey(dc)) { dcLock = _spinUpLocks[dc]; } else { dcLock = new object(); _spinUpLocks[dc] = dcLock; } } lock (dcLock) { var client = GetClientInternal(dc); if (client != null) return client; if (_config == null) { DebugPrint("Config is null. Unable to resolve DC information."); return null; } var dcOption = _config.DcOptions.Cast<DcOption>().FirstOrDefault(x => x.Id == dc); if (dcOption == null) { DebugPrint("Unable to find DC for DC ID: " + dc); return null; } var dcCached = DcDatabase.Get(dc); var transportConfig = new TcpClientTransportConfig(dcOption.IpAddress, (int)dcOption.Port); SharpMTProto.Authentication.AuthInfo authInfo; AuthExportedAuthorization exportedAuth = null; if (dcCached == null) { DebugPrint("Looks like we'll have to authenticate a new connection to: " + ObjectDumper.Dump(dcOption)); DebugPrint(">>>>>>>> Exporting auth..."); using (var clientDisposable = new TelegramClientDisposable(this)) { exportedAuth = (AuthExportedAuthorization)TelegramUtils.RunSynchronously(clientDisposable.Client.Methods.AuthExportAuthorizationAsync( new SharpTelegram.Schema.Layer18.AuthExportAuthorizationArgs { DcId = (uint)dc, })); } DebugPrint(">>>>>>> Got exported auth."); if (exportedAuth == null) { DebugPrint("Exported auth is null for some weird reason. DC ID: " + dc); return null; } DebugPrint(">>>>>>> Fetching new authentication..."); authInfo = TelegramUtils.RunSynchronously(FetchNewAuthentication(transportConfig)); } else { authInfo = new SharpMTProto.Authentication.AuthInfo(dcCached.Key, BitConverter.ToUInt64(dcCached.Salt, 0)); } DebugPrint(">>>>>>>> Starting new client..."); var newClient = new TelegramClient(transportConfig, new ConnectionConfig(authInfo.AuthKey, authInfo.Salt), AppInfo); newClient.OnClosedInternally += (sender, e) => { DebugPrint("Removing connection to DC: "+ dc); lock (_activeClients) { _activeClients.Remove(dc); } }; var result = TelegramUtils.RunSynchronously(newClient.Connect()); if (result != MTProtoConnectResult.Success) { DebugPrint("Failed to connect to DC: " + dc + ": " + result); return null; } if (exportedAuth != null) { TelegramUtils.RunSynchronously(newClient.Methods.AuthImportAuthorizationAsync(new AuthImportAuthorizationArgs { Id = exportedAuth.Id, Bytes = exportedAuth.Bytes, })); } PingDelay(client, 60); lock (_activeClients) { _activeClients[dc] = newClient; } if (dcCached == null) { DcDatabase.Set(new TelegramDc { Dc = dc, Key = authInfo.AuthKey, Salt = BitConverter.GetBytes(authInfo.Salt), }); } return newClient; } }