public Task SignMessages(BubbleGroup group, bool sign, Action <bool> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         using (var client = new FullClientDisposable(this))
         {
             try
             {
                 var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsToggleSignaturesAsync(new ChannelsToggleSignaturesArgs
                 {
                     Channel = new InputChannel
                     {
                         ChannelId = uint.Parse(group.Address),
                         AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                     },
                     Enabled = sign
                 }));
                 SendToResponseDispatcher(response, client.Client);
                 result(true);
             }
             catch (Exception e)
             {
                 DebugPrint("## exception while migrating the chat " + e);
                 result(false);
             }
         }
     }));
 }
Пример #2
0
        private Contact CreateTelegramContact(User user)
        {
            Contact globalContact;

            if (user.Bot != null)
            {
                globalContact = new TelegramBotContact
                {
                    User = user
                };
            }
            else
            {
                globalContact = new TelegramContact
                {
                    User = user
                };
            }

            globalContact.Available = TelegramUtils.GetAvailable(user);
            globalContact.LastSeen  = TelegramUtils.GetLastSeenTime(user);
            globalContact.FirstName = user.FirstName;
            globalContact.LastName  = user.LastName;
            globalContact.Ids       = new List <Contact.ID>
            {
                new Contact.ID
                {
                    Service = this,
                    Id      = user.Id.ToString(CultureInfo.InvariantCulture)
                }
            };

            return(globalContact);
        }
Пример #3
0
 private bool IsPartOfParty(Participants partyParticipants)
 {
     if (partyParticipants.Type == ParticipantsType.Chat)
     {
         foreach (var partyParticipant in partyParticipants.ChatParticipants)
         {
             string participantAddress = TelegramUtils.GetUserIdFromParticipant(partyParticipant);
             if (participantAddress == Settings.AccountId.ToString(CultureInfo.InvariantCulture))
             {
                 return(true);
             }
         }
     }
     else
     {
         foreach (var partyParticipant in partyParticipants.ChannelParticipants)
         {
             string participantAddress = TelegramUtils.GetUserIdFromChannelParticipant(partyParticipant);
             if (participantAddress == Settings.AccountId.ToString(CultureInfo.InvariantCulture))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Пример #4
0
 public Task GetContacts(string query, bool searchForParties, Action <List <Contact> > result)
 {
     return(Task.Factory.StartNew(async() =>
     {
         //TODO: search for parties
         var response = (ContactsContacts)await _fullClient.Methods.ContactsGetContactsAsync(
             new ContactsGetContactsArgs
         {
             Hash = string.Empty
         });
         var users = response.Users.OfType <UserContact>();
         var contacts = users.Select(x =>
                                     new TelegramContact
         {
             Available = TelegramUtils.GetAvailable(x),
             LastSeen = TelegramUtils.GetLastSeenTime(x),
             FirstName = x.FirstName,
             LastName = x.LastName,
             Ids = new List <Contact.ID>
             {
                 new Contact.ID
                 {
                     Service = this,
                     Id = x.Id.ToString(CultureInfo.InvariantCulture)
                 }
             },
         }).OfType <Contact>().OrderBy(x => x.FirstName).ToList();
         result(contacts);
     }));
 }
Пример #5
0
        private bool IsCreator(string address, bool superGroup)
        {
            var fullChat          = FetchFullChat(address, superGroup);
            var partyParticipants = GetPartyParticipants(fullChat);

            if (!superGroup)
            {
                foreach (var partyParticipant in partyParticipants.ChatParticipants)
                {
                    var id = TelegramUtils.GetUserIdFromParticipant(partyParticipant);
                    if (id == Settings.AccountId.ToString(CultureInfo.InvariantCulture))
                    {
                        //TODO:check how the protocol responds
                        if (partyParticipant is ChatParticipantCreator)
                        {
                            return(true);
                        }
                    }
                }
            }
            else
            {
                var channel = _dialogs.GetChat(uint.Parse(address)) as Channel;
                if (channel != null)
                {
                    return(channel.Creator != null);
                }
            }
            return(false);
        }
Пример #6
0
 public Task LeaveParty(BubbleGroup group)
 {
     return(Task.Factory.StartNew(() =>
     {
         var inputUser = new InputUser {
             UserId = Settings.AccountId
         };
         using (var client = new FullClientDisposable(this))
         {
             if (!group.IsExtendedParty)
             {
                 var update = TelegramUtils.RunSynchronously(client.Client.Methods.MessagesDeleteChatUserAsync(new MessagesDeleteChatUserArgs
                 {
                     ChatId = uint.Parse(group.Address),
                     UserId = inputUser,
                 }));
                 SendToResponseDispatcher(update, client.Client);
             }
             else
             {
                 var update = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsLeaveChannelAsync(new ChannelsLeaveChannelArgs
                 {
                     Channel = new InputChannel
                     {
                         ChannelId = uint.Parse(group.Address),
                         AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                     }
                 }));
                 SendToResponseDispatcher(update, client.Client);
             }
         }
     }));
 }
Пример #7
0
 public Task SetPartyName(BubbleGroup group, string name)
 {
     return(Task.Factory.StartNew(() =>
     {
         using (var client = new FullClientDisposable(this))
         {
             if (!group.IsExtendedParty)
             {
                 var update = TelegramUtils.RunSynchronously(
                     client.Client.Methods.MessagesEditChatTitleAsync(new MessagesEditChatTitleArgs
                 {
                     ChatId = uint.Parse(group.Address),
                     Title = name,
                 }));
                 SendToResponseDispatcher(update, client.Client);
             }
             else
             {
                 var update = TelegramUtils.RunSynchronously(
                     client.Client.Methods.ChannelsEditTitleAsync(new ChannelsEditTitleArgs
                 {
                     Channel = new InputChannel
                     {
                         ChannelId = uint.Parse(group.Address),
                         AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                     },
                     Title = name
                 }));
                 SendToResponseDispatcher(update, client.Client);
             }
         }
     }));
 }
Пример #8
0
 public Task GetPartyParticipants(BubbleGroup group, Action <DisaParticipant[]> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         var fullChat = FetchFullChat(group.Address, group.IsExtendedParty);
         var partyParticipants = GetPartyParticipants(fullChat);
         var resultList = new List <DisaParticipant>();
         if (!group.IsExtendedParty)
         {
             foreach (var partyParticipant in partyParticipants.ChatParticipants)
             {
                 var id = TelegramUtils.GetUserIdFromParticipant(partyParticipant);
                 if (id != null)
                 {
                     var name = TelegramUtils.GetUserName(_dialogs.GetUser(uint.Parse(id)));
                     resultList.Add(new DisaParticipant(name, id));
                 }
             }
         }
         else
         {
             foreach (var partyParticipant in partyParticipants.ChannelParticipants)
             {
                 var id = TelegramUtils.GetUserIdFromChannelParticipant(partyParticipant);
                 if (id != null)
                 {
                     var name = TelegramUtils.GetUserName(_dialogs.GetUser(uint.Parse(id)));
                     resultList.Add(new DisaParticipant(name, id));
                 }
             }
         }
         result(resultList.ToArray());
     }));
 }
Пример #9
0
            public bool Equals(IChannelParticipant x, IChannelParticipant y)
            {
                var userIdX = TelegramUtils.GetUserIdFromChannelParticipant(x);
                var userIdY = TelegramUtils.GetUserIdFromChannelParticipant(y);

                return(userIdX == userIdY);
            }
Пример #10
0
 public Task GetPartyName(BubbleGroup group, Action <string> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         result(TelegramUtils.GetChatTitle(_dialogs.GetChat(uint.Parse(group.Address))));
     }));
 }
Пример #11
0
        private List <IChannelParticipant> GetChannelParticipants(ChannelFull channelFull, IChannelParticipantsFilter filter)
        {
            var participantsList = new List <IChannelParticipant>();

            using (var client = new FullClientDisposable(this))
            {
                uint count  = 100;
                uint offset = 0;
                var  result = (ChannelsChannelParticipants)TelegramUtils.RunSynchronously(
                    client.Client.Methods.ChannelsGetParticipantsAsync(new ChannelsGetParticipantsArgs
                {
                    Channel = new InputChannel
                    {
                        ChannelId  = channelFull.Id,
                        AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(channelFull.Id))
                    },
                    Filter = filter,
                    Limit  = 100,
                    Offset = offset
                }));
                participantsList.AddRange(result.Participants);
                _dialogs.AddUsers(result.Users);
            }
            return(participantsList);
        }
Пример #12
0
 public Task SetPartyAddNewMembersRestriction(BubbleGroup group, PartyOptionsSettingsAddNewMembersRestriction restriction, Action <bool> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         using (var client = new FullClientDisposable(this))
         {
             try
             {
                 var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsToggleInvitesAsync(new ChannelsToggleInvitesArgs
                 {
                     Channel = new InputChannel
                     {
                         ChannelId = uint.Parse(group.Address),
                         AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                     },
                     Enabled = restriction == PartyOptionsSettingsAddNewMembersRestriction.Everyone
                 }));
                 SendToResponseDispatcher(response, client.Client);
                 result(true);
             }
             catch (Exception e)
             {
                 DebugPrint("##### Exeption while setting all members are admins type " + e);
                 result(false);
             }
         }
     }));
 }
Пример #13
0
 public Task GetAddressTitle(string address, Action <string> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         result(TelegramUtils.GetUserName(_dialogs.GetUser(uint.Parse(address))));
     }));
 }
Пример #14
0
 public Task SetPartyDescription(BubbleGroup group, string description, Action <bool> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         if (description != null)
         {
             using (var client = new FullClientDisposable(this))
             {
                 var edited = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsEditAboutAsync(new ChannelsEditAboutArgs
                 {
                     Channel = new InputChannel
                     {
                         ChannelId = uint.Parse(group.Address),
                         AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                     },
                     About = description
                 }));
                 if (edited)
                 {
                     result(true);
                 }
                 else
                 {
                     result(false);
                 }
             }
         }
     }));
 }
Пример #15
0
 public Task UnblockPartyParticipant(BubbleGroup group, DisaParticipant participant, Action <bool> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         using (var client = new FullClientDisposable(this))
         {
             try
             {
                 var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsKickFromChannelAsync(new ChannelsKickFromChannelArgs
                 {
                     Channel = new InputChannel
                     {
                         ChannelId = uint.Parse(group.Address),
                         AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                     },
                     Kicked = false,
                     UserId = new InputUser
                     {
                         UserId = uint.Parse(participant.Address),
                         AccessHash = GetUserAccessHashIfForeign(participant.Address)
                     }
                 }));
                 result(true);
             }
             catch (Exception e)
             {
                 DebugPrint("#### Exception " + e);
                 result(false);
             }
         }
     }));
 }
Пример #16
0
        private List <Contact> GetGlobalContacts(ContactsFound contactsFound)
        {
            var globalContacts = new List <Contact>();

            _dialogs.AddUsers(contactsFound.Users);
            foreach (var iUser in contactsFound.Users)
            {
                var user = iUser as User;
                if (user != null && user.Bot == null)
                {
                    globalContacts.Add(new TelegramContact
                    {
                        Available = TelegramUtils.GetAvailable(user),
                        LastSeen  = TelegramUtils.GetLastSeenTime(user),
                        FirstName = user.FirstName,
                        LastName  = user.LastName,
                        User      = user,
                        Ids       = new List <Contact.ID>
                        {
                            new Contact.ID
                            {
                                Service = this,
                                Id      = user.Id.ToString(CultureInfo.InvariantCulture)
                            }
                        },
                    });
                }
            }
            return(globalContacts);
        }
Пример #17
0
 public Task GetPartyBlockedParticipants(BubbleGroup group, Action <DisaParticipant[]> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         var returnList = new List <DisaParticipant>();
         var fullChat = FetchFullChat(group.Address, group.IsExtendedParty);
         var iChatFull = fullChat.FullChat;
         var channelFull = iChatFull as ChannelFull;
         if (channelFull != null)
         {
             var kickedParticipants = GetChannelParticipants(channelFull, new ChannelParticipantsKicked());
             foreach (var participant in kickedParticipants)
             {
                 if (participant is ChannelParticipantKicked)
                 {
                     var id = TelegramUtils.GetUserIdFromChannelParticipant(participant);
                     if (id != null)
                     {
                         var user = _dialogs.GetUser(uint.Parse(id));
                         var name = TelegramUtils.GetUserName(user);
                         returnList.Add(new DisaParticipant(name, id));
                     }
                 }
             }
         }
         result(returnList.ToArray());
     }));
 }
Пример #18
0
        public Task GetUserInformationSecondaryActionThumbnail(string address, Action <byte[]> result)
        {
            return(Task.Factory.StartNew(() =>
            {
                if (address == null)
                {
                    result(null);
                }

                var user = _dialogs.GetUser(uint.Parse(address));

                var phoneNumber = TelegramUtils.GetUserPhoneNumber(user);
                if (!string.IsNullOrWhiteSpace(phoneNumber))
                {
                    if (PhoneBookHasNumber(phoneNumber))
                    {
                        result(Platform.GetIcon(IconType.People));
                    }
                    else
                    {
                        result(Platform.GetIcon(IconType.AddContact));
                    }
                }
                else
                {
                    result(null);
                }
            }));
        }
Пример #19
0
        private List <Contact> GetGlobalPartyContacts(ContactsFound contactsFound)
        {
            var globalContacts = new List <Contact>();

            _dialogs.AddChats(contactsFound.Chats);
            Utils.DebugPrint("Chats found " + ObjectDumper.Dump(contactsFound.Chats));
            foreach (var chat in contactsFound.Chats)
            {
                var name   = TelegramUtils.GetChatTitle(chat);
                var kicked = TelegramUtils.GetChatKicked(chat);
                if (kicked)
                {
                    continue;
                }
                globalContacts.Add(new TelegramPartyContact
                {
                    FirstName = name,
                    Ids       = new List <Contact.ID>
                    {
                        new Contact.PartyID
                        {
                            Service       = this,
                            Id            = TelegramUtils.GetChatId(chat),
                            ExtendedParty = chat is Channel
                        }
                    },
                });
            }
            return(globalContacts);
        }
Пример #20
0
        private uint GetLastPtsForChannel(TelegramClient client, string address)
        {
            uint offset = 0;

            uint pts = 0;

Again:

            var iDialogs = TelegramUtils.RunSynchronously(client.Methods.MessagesGetDialogsAsync(new MessagesGetDialogsArgs
            {
                Limit      = 100,
                OffsetPeer = new InputPeerEmpty()
            }));
            var dialogs      = iDialogs as MessagesDialogs;
            var dialogsSlice = iDialogs as MessagesDialogsSlice;

            if (dialogs != null)
            {
                pts = FindPtsFromDialogs(dialogs.Dialogs, address);
            }
            if (dialogsSlice != null)
            {
                pts = FindPtsFromDialogs(dialogsSlice.Dialogs, address);
                //pts can never be zero, so we use this
                if (pts == 0)
                {
                    offset += dialogsSlice.Count;
                    goto Again;
                }
            }

            return(pts);
        }
Пример #21
0
        private IMessagesMessages GetChatMessagesForChannel(BubbleGroup group, long fromTime, int max, FullClientDisposable client)
        {
            var fullChat    = FetchFullChat(group.Address, true);
            var fullChannel = fullChat.FullChat as ChannelFull;

            if (fullChannel != null)
            {
                if (fullChannel.MigratedFromChatId != 0)
                {
                    var peerChat = GetInputPeer(fullChannel.MigratedFromChatId.ToString(), true, false);
                    DisposeFullChat();
                    return(TelegramUtils.RunSynchronously(
                               client.Client.Methods.MessagesGetHistoryAsync(new MessagesGetHistoryArgs
                    {
                        Peer = peerChat,
                        OffsetDate = (uint)fromTime,
                        Limit = (uint)max
                    })));
                }
                DisposeFullChat();
                return(MakeMessagesMessages(new List <IChat>(), new List <IUser>(), new List <IMessage>()));
            }
            DisposeFullChat();
            return(MakeMessagesMessages(new List <IChat>(), new List <IUser>(), new List <IMessage>()));
        }
Пример #22
0
 public static bool RequestAccountReset(Service service, TelegramSettings settings)
 {
     try
     {
         var transportConfig =
             new TcpClientTransportConfig(settings.NearestDcIp, settings.NearestDcPort);
         using (var client = new TelegramClient(transportConfig,
                                                new ConnectionConfig(settings.AuthKey, settings.Salt), AppInfo))
         {
             TelegramUtils.RunSynchronously(client.Connect());
             try
             {
                 var result = TelegramUtils.RunSynchronously(client.Methods.AccountDeleteAccountAsync(new AccountDeleteAccountArgs
                 {
                     Reason = "Forgot Password"
                 }));
                 return(result);
             }
             catch (RpcErrorException ex)
             {
                 var error = (RpcError)ex.Error;
                 return(false);
             }
         }
     }
     catch (Exception ex)
     {
         service.DebugPrint("Error in VerifyPassword: " + ex);
         return(false);
     }
 }
Пример #23
0
 public static AuthPasswordRecovery RequestPasswordRecovery(Service service, TelegramSettings settings)
 {
     try
     {
         var transportConfig =
             new TcpClientTransportConfig(settings.NearestDcIp, settings.NearestDcPort);
         using (var client = new TelegramClient(transportConfig,
                                                new ConnectionConfig(settings.AuthKey, settings.Salt), AppInfo))
         {
             TelegramUtils.RunSynchronously(client.Connect());
             try
             {
                 var result = (AuthPasswordRecovery)TelegramUtils.RunSynchronously(client.Methods.AuthRequestPasswordRecoveryAsync(new AuthRequestPasswordRecoveryArgs
                 {
                 }));
                 Thread.Sleep(100);
                 return(result);
             }
             catch (RpcErrorException ex)
             {
                 var error = (RpcError)ex.Error;
                 return(null);
             }
         }
     }
     catch (Exception ex)
     {
         service.DebugPrint("Error in VerifyPassword: " + ex);
         return(null);
     }
 }
Пример #24
0
 public static TelegramSettings GenerateAuthentication(Service service)
 {
     try
     {
         service.DebugPrint("Fetching nearest DC...");
         var settings = new TelegramSettings();
         var authInfo = TelegramUtils.RunSynchronously(FetchNewAuthentication(DefaultTransportConfig));
         using (var client = new TelegramClient(DefaultTransportConfig,
                                                new ConnectionConfig(authInfo.AuthKey, authInfo.Salt), AppInfo))
         {
             TelegramUtils.RunSynchronously(client.Connect());
             var nearestDcId = (NearestDc)TelegramUtils.RunSynchronously(client.Methods.HelpGetNearestDcAsync(new HelpGetNearestDcArgs {
             }));
             var config      = (Config)TelegramUtils.RunSynchronously(client.Methods.HelpGetConfigAsync(new HelpGetConfigArgs {
             }));
             var dcOption    = config.DcOptions.OfType <DcOption>().FirstOrDefault(x => x.Id == nearestDcId.NearestDcProperty);
             settings.NearestDcId   = nearestDcId.NearestDcProperty;
             settings.NearestDcIp   = dcOption.IpAddress;
             settings.NearestDcPort = (int)dcOption.Port;
         }
         service.DebugPrint("Generating authentication on nearest DC...");
         var authInfo2 = TelegramUtils.RunSynchronously(FetchNewAuthentication(
                                                            new TcpClientTransportConfig(settings.NearestDcIp, settings.NearestDcPort)));
         settings.AuthKey = authInfo2.AuthKey;
         settings.Salt    = authInfo2.Salt;
         service.DebugPrint("Great! Ready for the service to start.");
         return(settings);
     }
     catch (Exception ex)
     {
         service.DebugPrint("Error in GenerateAuthentication: " + ex);
     }
     return(null);
 }
Пример #25
0
        public Task GetBotCallbackAnswer(BubbleGroup group, VisualBubble bubble, Bots.KeyboardButton button, Action <Bots.MessagesBotCallbackAnswer> answer)
        {
            return(Task.Factory.StartNew(() =>
            {
                using (var client = new FullClientDisposable(this))
                {
                    // Note: Telegram also has KeyboardButtonGame which functions as a callback also
                    if (button is Bots.KeyboardButtonCallback)
                    {
                        var keyboardButtonCallback = button as Bots.KeyboardButtonCallback;

                        var args = new MessagesGetBotCallbackAnswerArgs
                        {
                            MsgId = uint.Parse(bubble.IdService),
                            Data = keyboardButtonCallback.Data,
                            Peer = GetInputPeer(group.Address, group.IsParty, group.IsExtendedParty)
                        };

                        SharpTelegram.Schema.MessagesBotCallbackAnswer telegramBotCallbackAnswer =
                            (SharpTelegram.Schema.MessagesBotCallbackAnswer)
                            TelegramUtils.RunSynchronously(
                                client.Client.Methods.MessagesGetBotCallbackAnswerAsync(args));

                        var disaBotCallbackAnswer = new Bots.MessagesBotCallbackAnswer
                        {
                            Alert = telegramBotCallbackAnswer.Alert != null ? true : false,
                            Message = telegramBotCallbackAnswer.Message
                        };

                        answer(disaBotCallbackAnswer);
                    }
                }
            }));
        }
Пример #26
0
 private bool CheckLink(string linkUsefulPart, out IChat alreadyJoinedChat)
 {
     using (var client = new FullClientDisposable(this))
     {
         try
         {
             var result = TelegramUtils.RunSynchronously(client.Client.Methods.MessagesCheckChatInviteAsync(new MessagesCheckChatInviteArgs
             {
                 Hash = linkUsefulPart
             }));
             var chatInvite        = result as ChatInvite;
             var chatInviteAlready = result as ChatInviteAlready;
             if (chatInvite != null)
             {
                 alreadyJoinedChat = null;
                 return(true);
             }
             if (chatInviteAlready != null)
             {
                 alreadyJoinedChat = chatInviteAlready.Chat;
                 return(true);
             }
             alreadyJoinedChat = null;
             return(false);
         }
         catch (Exception ex)
         {
             DebugPrint("Exception while checking invite" + ex);
             alreadyJoinedChat = null;
             return(false);
         }
     }
 }
Пример #27
0
 public Task FetchBubbleGroupAddress(Tuple <Contact, Contact.ID>[] contacts, Action <bool, string> result)
 {
     return(Task.Factory.StartNew(async() =>
     {
         if (contacts.Length > 1)
         {
             var userContacts = await FetchContacts();
             var inputUsers = new List <IInputUser>();
             var names = new List <string>();
             foreach (var contact in contacts)
             {
                 names.Add(contact.Item1.FullName);
                 var id = uint.Parse(contact.Item2.Id);
                 foreach (var userContact in userContacts)
                 {
                     if (userContact.Id == id)
                     {
                         var inputUser = TelegramUtils.CastUserToInputUser(userContact);
                         if (inputUser != null)
                         {
                             inputUsers.Add(inputUser);
                             break;
                         }
                     }
                 }
             }
             if (inputUsers.Any())
             {
                 var subject = BubbleGroupUtils.GeneratePartyTitle(names.ToArray());
                 if (subject.Length > 25)
                 {
                     subject = subject.Substring(0, 22);
                     subject += "...";
                 }
                 using (var client = new FullClientDisposable(this))
                 {
                     var response = await client.Client.Methods.MessagesCreateChatAsync(
                         new MessagesCreateChatArgs
                     {
                         Users = inputUsers,
                         Title = subject,
                     });
                     ProcessIncomingPayload(response, true);
                     SaveState(response);
                     var chat = TelegramUtils.GetChatFromStatedMessage(response);
                     result(true, TelegramUtils.GetChatId(chat));
                 }
             }
             else
             {
                 result(false, null);
             }
         }
         else
         {
             result(true, contacts[0].Item2.Id);
         }
     }));
 }
Пример #28
0
        public Task DemotePartyParticipantsFromLeader(BubbleGroup group, DisaParticipant participant, Action <DemotePartyParticipantsResult> result)
        {
            return(Task.Factory.StartNew(() =>
            {
                var inputUser = new InputUser
                {
                    UserId = uint.Parse(participant.Address),
                    AccessHash = GetUserAccessHashIfForeign(participant.Address)
                };

                using (var client = new FullClientDisposable(this))
                {
                    if (!group.IsExtendedParty)
                    {
                        if (!ChatAdminsEnabled(group.Address))
                        {
                            result(DemotePartyParticipantsResult.AllMembersAreAdministratorsEnabled);
                            return;
                        }
                        try
                        {
                            TelegramUtils.RunSynchronously(client.Client.Methods.MessagesEditChatAdminAsync(new MessagesEditChatAdminArgs
                            {
                                ChatId = uint.Parse(group.Address),
                                IsAdmin = false,
                                UserId = inputUser,
                            }));
                            result(DemotePartyParticipantsResult.Success);
                        }
                        catch (Exception e)
                        {
                            result(DemotePartyParticipantsResult.Failure);
                        }
                    }
                    else
                    {
                        try
                        {
                            var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsEditAdminAsync(new ChannelsEditAdminArgs
                            {
                                Channel = new InputChannel
                                {
                                    ChannelId = uint.Parse(group.Address),
                                    AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                                },
                                Role = new ChannelRoleEmpty(),
                                UserId = inputUser
                            }));
                            SendToResponseDispatcher(response, client.Client);
                            result(DemotePartyParticipantsResult.Success);
                        }
                        catch (Exception e)
                        {
                            result(DemotePartyParticipantsResult.Failure);
                        }
                    }
                }
            }));
        }
Пример #29
0
        private List <Mention> MentionsGetMentions(string token, string address, bool isChannel, TelegramClient optionalClient = null)
        {
            // Only handling usernames now
            if (token != "@")
            {
                return(new List <Mention>());
            }

            var fullChat          = MentionsFetchFullChat(address, isChannel, optionalClient);
            var partyParticipants = MentionsGetPartyParticipants(fullChat);

            var resultList = new List <Mention>();

            if (!isChannel)
            {
                foreach (var partyParticipant in partyParticipants.ChatParticipants)
                {
                    var id = TelegramUtils.GetUserIdFromParticipant(partyParticipant);
                    if (id != null)
                    {
                        var user     = _dialogs.GetUser(uint.Parse(id));
                        var username = TelegramUtils.GetUserHandle(user);
                        var name     = TelegramUtils.GetUserName(user);
                        var mention  = new Mention
                        {
                            Type    = MentionType.Username,
                            Value   = username,
                            Name    = name,
                            Address = id
                        };
                        resultList.Add(mention);
                    }
                }
            }
            else
            {
                foreach (var partyParticipant in partyParticipants.ChannelParticipants)
                {
                    var id = TelegramUtils.GetUserIdFromChannelParticipant(partyParticipant);
                    if (id != null)
                    {
                        var user     = _dialogs.GetUser(uint.Parse(id));
                        var username = TelegramUtils.GetUserHandle(user);
                        var name     = TelegramUtils.GetUserName(user);
                        var mention  = new Mention
                        {
                            Type    = MentionType.Username,
                            Value   = username,
                            Name    = name,
                            Address = id
                        };

                        resultList.Add(mention);
                    }
                }
            }

            return(resultList);
        }
Пример #30
0
        public Task GetPrivacyList(Action <string[]> addresses)
        {
            List <string> addressList;

            return(Task.Factory.StartNew(() =>
            {
                uint offset = 0;
                //TODO: check when it returns a chunk or a full list
                using (var client = new FullClientDisposable(this))
                {
                    Again:
                    var blocked =
                        TelegramUtils.RunSynchronously(
                            client.Client.Methods.ContactsGetBlockedAsync(new ContactsGetBlockedArgs
                    {
                        Limit = 100,
                        Offset = offset
                    }));
                    var contactsBlocked = blocked as ContactsBlocked;
                    var contactsBlockedSlice = blocked as ContactsBlockedSlice;

                    addressList = new List <string>();
                    if (contactsBlocked != null)
                    {
                        foreach (var blockedContact in contactsBlocked.Blocked)
                        {
                            var contactBlocked = blockedContact as ContactBlocked;
                            if (contactBlocked == null)
                            {
                                continue;
                            }
                            addressList.Add(contactBlocked.UserId.ToString(CultureInfo.InvariantCulture));
                        }
                        _dialogs.AddUsers(contactsBlocked.Users);
                    }
                    if (contactsBlockedSlice != null)
                    {
                        foreach (var blockedContact in contactsBlockedSlice.Blocked)
                        {
                            var contactBlocked = blockedContact as ContactBlocked;
                            if (contactBlocked == null)
                            {
                                continue;
                            }
                            addressList.Add(contactBlocked.UserId.ToString(CultureInfo.InvariantCulture));
                        }
                        _dialogs.AddUsers(contactsBlockedSlice.Users);
                        if (contactsBlockedSlice.Blocked.Any())
                        {
                            offset += contactsBlockedSlice.Count;
                            goto Again;
                        }
                    }
                    previousAddresses = addressList.ToArray();
                    addresses(addressList.ToArray());
                }
            }));
        }