public override void SendBubble(Bubble b)
 {
     var textBubble = b as TextBubble;
     if (textBubble != null)
     {
         Utils.Delay(2000).Wait();
         Platform.ScheduleAction(1, new WakeLockBalancer.ActionObject(() =>
         {
             EventBubble(new TextBubble(Time.GetNowUnixTimestamp(), Bubble.BubbleDirection.Incoming,
                 textBubble.Address, null, false, this, Reverse(textBubble.Message)));
         }, WakeLockBalancer.ActionObject.ExecuteType.TaskWithWakeLock));
     }
 }
 public static bool UpdateStatus(Service service, string bubbleGroupAddress,
     string bubbleId, Bubble.BubbleStatus status)
 {
     var group = BubbleGroupManager.FindWithAddress(service, bubbleGroupAddress);
     if (group == null)
     {
         return false;
     }
     BubbleGroupFactory.LoadFullyIfNeeded(group);
     foreach (var bubble in @group.Bubbles)
     {
         if (bubble.ID == bubbleId)
         {
             UpdateStatus(bubble, status, @group);
             return true;
         }
     }
     return false;
 }
 public static void UpdateStatus(VisualBubble b, Bubble.BubbleStatus status, BubbleGroup group, 
     bool updateBubbleGroupBubbles = true)
 {
     b.Status = status;
     BubbleGroupDatabase.UpdateBubble(@group, b);
     if (updateBubbleGroupBubbles)
         BubbleGroupEvents.RaiseBubblesUpdated(@group);
     BubbleGroupEvents.RaiseRefreshed(@group);
 }
        private static Task<bool> Send(Bubble b, BubbleGroup group, bool resend)
        {
            return Task<bool>.Factory.StartNew(() =>
            {                   
                var vb = b as VisualBubble;
                if (vb != null)
                {
                    if (vb.Status == Bubble.BubbleStatus.Sent)
                    {
                        Utils.DebugPrint("Trying to send a bubble that is already sent! On " + vb.Service.Information.ServiceName);
                        return true;
                    }

                    Func<bool> restartServiceIfNeeded = () =>
                    {
                        if (!ServiceManager.IsRegistered(b.Service) ||
                            ServiceManager.IsRunning(b.Service) ||
                            ServiceManager.IsAborted(b.Service) )
                            return false;

                        Utils.DebugPrint(
                            "For f***s sakes. The scheduler isn't doing it's job properly, or " +
                            "you're sending a message to it at a weird time. Starting it up bra (" +
                            b.Service.Information.ServiceName + ").");
                        ServiceManager.AbortAndRestart(b.Service);
                        return true;
                    };

                    var visualBubbleServiceId = vb.Service as IVisualBubbleServiceId;
                    if (vb.IdService == null && vb.IdService2 == null && visualBubbleServiceId != null)
                    {
                        visualBubbleServiceId.AddVisualBubbleIdServices(vb);
                    }

                    try
                    {
                        @group = Group(vb, resend, true);
                    }
                    catch (Exception ex)
                    {
                        Utils.DebugPrint("Problem in Send:GroupBubble from service " + 
                                                 vb.Service.Information.ServiceName + ": " + ex.Message);
                        return false;
                    }

                    if (@group == null)
                    {
                        Utils.DebugPrint("Could not find a suitable group for bubble " + vb.ID 
                                                 + " on " + vb.Service.Information.ServiceName); 
                        return false;
                    }

                    var shouldQueue = vb.Service.QueuedBubblesParameters == null || 
                                      !vb.Service.QueuedBubblesParameters.BubblesNotToQueue.Contains(vb.GetType());

                    try
                    {
                        if (shouldQueue && !resend && 
                            BubbleQueueManager.HasQueuedBubbles(vb.Service.Information.ServiceName, 
                                true, false))
                        {
                            BubbleQueueManager.JustQueue(group, vb);
                            restartServiceIfNeeded();
                            return false;
                        }

                        if (shouldQueue)
                        {
                            Monitor.Enter(vb.Service.SendBubbleLock);
                        }

                        using (var queued = new BubbleQueueManager.InsertBubble(group, vb, shouldQueue))
                        {
                            Action checkForQueued = () =>
                            {
                                if (!resend 
                                    && BubbleQueueManager.HasQueuedBubbles(vb.Service.Information.ServiceName, true, true))
                                {
                                    BubbleQueueManager.Send(new [] { vb.Service.Information.ServiceName });
                                }
                            };

                            try
                            {
                                FailBubbleIfPathDoesntExist(vb);
                                SendBubbleInternal(b);
                            }
                            catch (ServiceQueueBubbleException ex)
                            {
                                Utils.DebugPrint("Queuing visual bubble on service " + 
                                                         vb.Service.Information.ServiceName + ": " + ex.Message);

                                UpdateStatus(vb, Bubble.BubbleStatus.Waiting, @group);

                                if (!restartServiceIfNeeded())
                                {
                                    checkForQueued();
                                }

                                return false;
                            }
                            catch (Exception ex)
                            {
                                queued.CancelQueue();

                                Utils.DebugPrint("Visual bubble on " + 
                                                         vb.Service.Information.ServiceName + " failed to be sent: " +
                                                         ex);

                                UpdateStatus(vb, Bubble.BubbleStatus.Failed, @group);
                                BubbleGroupEvents.RaiseBubbleFailed(vb, @group);

                                if (!restartServiceIfNeeded())
                                {
                                    checkForQueued();
                                }
                                    
                                //FIXME: if the bubble fails to send, allow the queue manager to continue.
                                if (resend)
                                    return true;

                                return false;
                            }
                                
                            queued.CancelQueue();

                            lock (BubbleGroupManager.LastBubbleSentTimestamps)
                            {
                                BubbleGroupManager.LastBubbleSentTimestamps[group.ID] = Time.GetNowUnixTimestamp();
                            }

                            if (vb.Status == Bubble.BubbleStatus.Delivered)
                            {
                                Utils.DebugPrint(
                                    "************ Race condition. The server set the status to delivered/read before we could send it to sent. :')");
                                checkForQueued();
                                return true;
                            }

                            UpdateStatus(vb, Bubble.BubbleStatus.Sent, @group);

                            checkForQueued();
                            return true;
                        }
                    }
                    finally
                    {
                        if (shouldQueue)
                        {
                            Monitor.Exit(vb.Service.SendBubbleLock);
                        }
                    }
                }
                else
                {
                    var composeBubble = b as ComposeBubble;

                    if (composeBubble != null)
                    {
                        var bubbleToSend = composeBubble.BubbleToSend;
                        var visualBubbleServiceId = bubbleToSend.Service as IVisualBubbleServiceId;
                        if (bubbleToSend.IdService == null && bubbleToSend.IdService2 == null && 
                            visualBubbleServiceId != null)
                        {
                            visualBubbleServiceId.AddVisualBubbleIdServices(bubbleToSend);
                        }
                        @group.InsertByTime(bubbleToSend);
                        try
                        {
                            BubbleGroupEvents.RaiseBubbleInserted(bubbleToSend, @group);
                        }
                        catch
                        {
                            // do nothing
                        }
                    }

                    try
                    {
                        SendBubbleInternal(b);
                    }
                    catch (ServiceBubbleGroupAddressException ex)
                    {
                        if (!String.IsNullOrWhiteSpace(ex.Address))
                        {
                            if (composeBubble != null)
                            {
                                composeBubble.BubbleToSend.Address = ex.Address;
                                composeBubble.BubbleToSend.Status = Bubble.BubbleStatus.Sent;

                                var actualGroup = Group(composeBubble.BubbleToSend, resend, true);

                                ServiceEvents.RaiseComposeFinished(
                                    @group as ComposeBubbleGroup, actualGroup);

                                return true;
                            }
                        }

                        composeBubble.BubbleToSend.Status = Bubble.BubbleStatus.Failed;
                        return false;
                    }
                    catch (Exception ex)
                    {
                        Utils.DebugPrint("Failed to send bubble on service " + b.Service.Information.ServiceName);

                        if (composeBubble != null)
                        {
                            composeBubble.BubbleToSend.Status = Bubble.BubbleStatus.Failed;
                        }

                        return false;
                    }

                    if (composeBubble != null)
                    {
                        composeBubble.BubbleToSend.Status = Bubble.BubbleStatus.Failed;
                        return false;
                    }

                    return true;
                }
            });
        }
        private static void SendBubbleInternal(Bubble b)
        {
            if (b.Service == null)
            {
                throw new ServiceBubbleSendFailedException("This function cannot be called with a null service.");
            }

            if (!b.Service.Information.DoesSupport(b.GetType()))
            {
                throw new ServiceBubbleSendFailedException("The service " + b.Service.Information.ServiceName +
                                                           " does not support " + b.GetType());
            }

            Utils.DebugPrint("Sending " + b.GetType().Name + " on service " + b.Service.Information.ServiceName);
            b.Service.SendBubble(b);
        }
 public static Task<bool> Send(Bubble b, bool resend = false)
 {
     return Send(b, null, resend);
 }
 public static bool UpdateStatus(Service service, string bubbleGroupAddress,
     string bubbleId, Bubble.BubbleStatus status)
 {
     return UpdateStatus(service, bubbleGroupAddress, bubbleId, status, null);
 }
 public static void UpdateStatus(VisualBubble[] bubbles, Bubble.BubbleStatus status, BubbleGroup group, 
     bool updateBubbleGroupBubbles = true)
 {
     foreach (var bubble in bubbles)
     {
         bubble.Status = status;
     }
     BubbleGroupDatabase.UpdateBubble(@group, bubbles);
     if (updateBubbleGroupBubbles)
         BubbleGroupEvents.RaiseBubblesUpdated(@group);
     BubbleGroupEvents.RaiseRefreshed(@group);
 }
Exemple #9
0
        public override void SendBubble(Bubble b)
        {
            var presenceBubble = b as PresenceBubble;
            if (presenceBubble != null)
            {
                _hasPresence = presenceBubble.Available;
                SetFullClientPingDelayDisconnect();
                using (var client = new FullClientDisposable(this))
                {
                    if (_hasPresence)
                    {
                        if (_mutableSettings.Date != 0)
                        {
                            // as thy come forth, thy shall get difference
                            FetchDifference(client.Client);
                        }
                        var updatedUsers = GetUpdatedUsersOfAllDialogs(client.Client);
                        if (updatedUsers != null)
                        {
                            foreach (var updatedUser in updatedUsers)
                            {
                                EventBubble(new PresenceBubble(Time.GetNowUnixTimestamp(), Bubble.BubbleDirection.Incoming,
                                    TelegramUtils.GetUserId(updatedUser), false, this, TelegramUtils.GetAvailable(updatedUser)));
                            }
                        }
                    }
                    SendPresence(client.Client);
                }
            }

            var typingBubble = b as TypingBubble;
            if (typingBubble != null)
            {
                var peer = GetInputPeer(typingBubble.Address, typingBubble.Party, typingBubble.ExtendedParty);
                using (var client = new FullClientDisposable(this))
                {
                    TelegramUtils.RunSynchronously(client.Client.Methods.MessagesSetTypingAsync(
                        new MessagesSetTypingArgs
                        {
                            Peer = peer,
                            Action = typingBubble.IsAudio ? (ISendMessageAction)new SendMessageRecordAudioAction() : (ISendMessageAction)new SendMessageTypingAction()
                        }));
                }
            }

            var textBubble = b as TextBubble;
            if (textBubble != null)
            {
                var peer = GetInputPeer(textBubble.Address, textBubble.Party, textBubble.ExtendedParty);

                using (var client = new FullClientDisposable(this))
                {
                    var iUpdates = TelegramUtils.RunSynchronously(
                        client.Client.Methods.MessagesSendMessageAsync(new MessagesSendMessageArgs
                        {
                            Flags = 0,
                            Peer = peer,
                            Message = textBubble.Message,
                            RandomId = ulong.Parse(textBubble.IdService2)
                        }));
                    DebugPrint(">>>> IUpdates message sent " + ObjectDumper.Dump(iUpdates));
                    var updateShortSentMessage = iUpdates as UpdateShortSentMessage;
                    if (updateShortSentMessage != null)
                    {
                        textBubble.IdService = updateShortSentMessage.Id.ToString(CultureInfo.InvariantCulture);
                    }
                    var updates = iUpdates as Updates;
                    if (updates != null)
                    {
                        foreach (var update in updates.UpdatesProperty)
                        {
                            var updateNewChannelMessage = update as UpdateNewChannelMessage;
                            if (updateNewChannelMessage == null) continue;
                            var message = updateNewChannelMessage.Message as Message;
                            if (message != null)
                            {
                                textBubble.IdService = message.Id.ToString(CultureInfo.InvariantCulture);
                            }
                        }
                    }
                    SendToResponseDispatcher(iUpdates, client.Client);
                }
            }

            var readBubble = b as ReadBubble;
            if (readBubble != null)
            {
                using (var client = new FullClientDisposable(this))
                {
                    var peer = GetInputPeer(readBubble.Address, readBubble.Party, readBubble.ExtendedParty);
                    if (peer is InputPeerChannel)
                    {
                        TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsReadHistoryAsync(new ChannelsReadHistoryArgs
                        {
                            Channel = new InputChannel
                            {
                                ChannelId = uint.Parse(readBubble.Address),
                                AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(readBubble.Address)))
                            },
                            MaxId = 0
                        }));
                    }
                    else
                    {
                        var messagesAffectedMessages =
                            TelegramUtils.RunSynchronously(client.Client.Methods.MessagesReadHistoryAsync(
                            new MessagesReadHistoryArgs
                            {
                                Peer = peer,
                                MaxId = 0,

                            })) as MessagesAffectedMessages;
                        if (messagesAffectedMessages != null)
                        {
                            SaveState(0, messagesAffectedMessages.Pts, 0, 0);
                        }
                    }
                }
            }

            var imageBubble = b as ImageBubble;
            if (imageBubble != null)
            {
                var fileId = GenerateRandomId();
                try
                {
                    var inputFile = UploadFile(imageBubble, fileId, 0);
                    SendFile(imageBubble, inputFile);
                }
                catch (Exception e)
                {
                    DebugPrint("File upload error " + e);
                    throw;
                }
            }

            var fileBubble = b as FileBubble;

            if (fileBubble != null)
            {
                var fileId = GenerateRandomId();
                try
                {
                    var fileInfo = new FileInfo(fileBubble.Path);
                    DebugPrint(">>>>>>> the size of the file is " + fileInfo.Length);
                    if (fileInfo.Length <= 10485760)
                    {
                        var inputFile = UploadFile(fileBubble, fileId, fileInfo.Length);
                        SendFile(fileBubble, inputFile);
                    }
                    else
                    {
                        var inputFile = UploadBigFile(fileBubble, fileId, fileInfo.Length);
                        SendFile(fileBubble, inputFile);
                    }
                }
                catch (Exception e)
                {
                    DebugPrint("File upload error " + e);
                    throw;
                }
            }

            var audioBubble = b as AudioBubble;

            if (audioBubble != null)
            {
                var fileId = GenerateRandomId();
                try
                {
                    var fileInfo = new FileInfo(audioBubble.AudioPath);
                    DebugPrint(">>>>>>> the size of the file is " + fileInfo.Length);
                    if (fileInfo.Length <= 10485760)
                    {
                        var inputFile = UploadFile(audioBubble, fileId, fileInfo.Length);
                        SendFile(audioBubble, inputFile);
                    }
                    else
                    {
                        var inputFile = UploadBigFile(audioBubble, fileId, fileInfo.Length);
                        SendFile(audioBubble, inputFile);
                    }
                }
                catch (Exception e)
                {
                    DebugPrint("File upload error " + e);
                    throw;
                }
            }

            var locationBubble = b as LocationBubble;

            if (locationBubble != null)
            {
                SendGeoLocation(locationBubble);
            }

            var contactBubble = b as ContactBubble;

            if (contactBubble != null)
            {
                SendContact(contactBubble);
            }

            DebugPrint(">>>> Sending visual bubble " + ObjectDumper.Dump(b));

        }
Exemple #10
0
        public override async void SendBubble(Bubble b)
        {
            var presenceBubble = b as PresenceBubble;
            if (presenceBubble != null)
            {
                _hasPresence = presenceBubble.Available;
                SetFullClientPingDelayDisconnect();
                if (_hasPresence)
                {
                    var updatedUsers = GetUpdatedUsersOfAllDialogs();
                    if (updatedUsers != null)
                    {
                        foreach (var updatedUser in updatedUsers)
                        {
                            EventBubble(new PresenceBubble(Time.GetNowUnixTimestamp(), Bubble.BubbleDirection.Incoming, 
                                TelegramUtils.GetUserId(updatedUser), false, this, TelegramUtils.GetAvailable(updatedUser)));
                        }
                    }
                }
                TelegramUtils.RunSynchronously(_fullClient.Methods.AccountUpdateStatusAsync(
                    new AccountUpdateStatusArgs
                {
                    Offline = !presenceBubble.Available
                }));
            }

            var typingBubble = b as TypingBubble;
            if (typingBubble != null)
            {
                var peer = GetInputPeer(typingBubble.Address, typingBubble.Party);
                TelegramUtils.RunSynchronously(_fullClient.Methods.MessagesSetTypingAsync(
                    new MessagesSetTypingArgs
                {
                    Peer = peer,
                    Action = typingBubble.IsAudio ? 
                        (ISendMessageAction)new SendMessageRecordAudioAction() : (ISendMessageAction)new SendMessageTypingAction()
                }));
            }

            var textBubble = b as TextBubble;
            if (textBubble != null)
            {
                var peer = GetInputPeer(textBubble.Address, textBubble.Party);
                TelegramUtils.RunSynchronously(_fullClient.Methods.MessagesSendMessageAsync(new MessagesSendMessageArgs
                    {
                        Peer = peer,
                        Message = textBubble.Message,
                        RandomId = ulong.Parse(textBubble.IdService)
                    }));
            }
        }
 public static void UpdateStatus(Service service, string bubbleId, Bubble.BubbleStatus status)
 {
     var serviceGroups = BubbleGroupManager.FindAll(service);
     foreach (var group in serviceGroups)
     {
         foreach (var bubble in @group)
         {
             if (bubble.ID == bubbleId)
             {
                 UpdateStatus(bubble, status, @group);
                 return;
             }
         }
     }
 }