Exemplo n.º 1
0
        public async Task SendAsnwerTo(string messageId, XmppAddress to)
        {
            var transport = XmppTransportManager.GetTransport();
            var iq        = new InfoQuery
            {
                Id            = messageId
                , Type        = InfoQueryType.Result
                , To          = to
                , ServiceInfo = new ServiceInfo
                {
                    Node = this.node
                }
            };

            foreach (var identity in this.Identities)
            {
                iq.ServiceInfo.Identities.Add(identity);
            }

            foreach (var feature in this.Features)
            {
                iq.ServiceInfo.Features.Add(feature);
            }

            await transport.SendAsync(iq).ConfigureAwait(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ChatConversation"/> class.
        /// </summary>
        /// <param name="contact">The conversation contact.</param>
        internal ChatConversation(Contact contact)
        {
            var transport = XmppTransportManager.GetTransport();

            this.remoteParticipantComposingChangedStream = new Subject <RemoteParticipantComposingChangedEventData>();
            this.incomingChatMessageStream = new Subject <ChatMessage>();

            this.ThreadingInfo = new ChatConversationThreadingInfo
            {
                Id               = IdentifierGenerator.Generate()
                , ContactId      = contact.Address
                , ConversationId = this.Id
                , Custom         = null
                , Kind           = ChatConversationThreadingKind.ContactId
            };

            this.ThreadingInfo.Participants.Add(transport.UserAddress);
            this.ThreadingInfo.Participants.Add(contact.Address);

            this.store = XmppTransportManager.RequestStore(contact.Address.BareAddress);
            this.store.ChangeTracker.Enable();

            transport.MessageStream
            .Where(message => message.IsChat && message.FromAddress.BareAddress == contact.Address)
            .Subscribe(async message => await OnMessageReceived(message).ConfigureAwait(false));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds the given contact to the roster
        /// </summary>
        /// <param name="address">Contact address</param>
        /// <param name="name">Contact name</param>
        public async Task AddContactAsync(XmppAddress address, string name)
        {
            var contact   = this[address];
            var transport = XmppTransportManager.GetTransport();

            if (contact != null)
            {
                throw new ArgumentException("The given address is already in the contact list");
            }

            var iq = new InfoQuery()
            {
                Type     = InfoQueryType.Set
                , From   = transport.UserAddress.BareAddress
                , To     = transport.UserAddress.BareAddress
                , Roster = new Roster
                {
                    Items =
                    {
                        new RosterItem
                        {
                            Subscription = RosterSubscriptionType.None
                            , Jid        = address.BareAddress
                            , Name       = name
                        }
                    }
                }
            };

            await transport.SendAsync(iq, r => this.OnAddContactResponse(r), e => this.OnAddContactError(e))
            .ConfigureAwait(false);
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="chatMessage">The chat message to validate.</param>
        /// <returns>The validation result.</returns>
        public ChatMessageValidationResult ValidateMessage(ChatMessage chatMessage)
        {
            var transport = XmppTransportManager.GetTransport();
            var status    = ChatMessageValidationStatus.Valid;

            if (transport == null)
            {
                status = ChatMessageValidationStatus.TransportNotFound;
            }
            else if (transport.State != XmppTransportState.Open)
            {
                status = ChatMessageValidationStatus.TransportInactive;
            }
            else if (chatMessage == null || chatMessage.ThreadingInfo == null)
            {
                status = ChatMessageValidationStatus.InvalidData;
            }
            else if (String.IsNullOrEmpty(chatMessage.Body))
            {
                status = ChatMessageValidationStatus.InvalidBody;
            }
            else if (String.IsNullOrEmpty(chatMessage.ThreadingInfo.ContactId))
            {
                status = ChatMessageValidationStatus.NoRecipients;
            }
            else if (transport.Contacts[chatMessage.ThreadingInfo.ContactId] == null)
            {
                status = ChatMessageValidationStatus.InvalidRecipients;
            }

            return(new ChatMessageValidationResult(status));
        }
Exemplo n.º 5
0
        private async Task OnAdvertiseCapabilities(Presence response)
        {
            var transport = XmppTransportManager.GetTransport();

            if (response.From != transport.UserAddress)
            {
                await this.disco.SendAsnwerTo(response.Id, response.From);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ChatMessageStore"/> class.
        /// </summary>
        internal ChatMessageStore()
        {
            var transport = XmppTransportManager.GetTransport();

            this.messageChangedStream = new Subject <ChatMessage>();
            this.storeChangedStream   = new Subject <ChatMessageStoreChangedEventData>();
            this.ChangeTracker        = new ChatMessageChangeTracker();
            this.messageReader        = new ChatMessageReader();
        }
Exemplo n.º 7
0
        private void OnConnected()
        {
            var transport = XmppTransportManager.GetTransport();

            transport.MessageStream
            .Where(m => m.Type == MessageType.Headline || m.Type == MessageType.Normal)
            .Subscribe(message => { this.OnMessageReceived(message); });

            this.activities.CollectionChanged += new NotifyCollectionChangedEventHandler(OnCollectionChanged);
        }
Exemplo n.º 8
0
        private void SubscribeToRosterPush()
        {
            var transport = XmppTransportManager.GetTransport();

            transport.InfoQueryStream
            .Where(message => message.To == transport.UserAddress &&
                   message.Roster != null &&
                   message.IsUpdate)
            .Subscribe(async message => await this.OnRosterPush(message)
                       .ConfigureAwait(false));
        }
Exemplo n.º 9
0
        public async Task OpenConnectionTest()
        {
            var transport = XmppTransportManager.GetTransport();

            transport.ConnectionString = ConnectionStringHelper.GetDefaultConnectionString();
            transport.StateChanged.Subscribe(state => Debug.WriteLine("TEST -> Connection state " + state.ToString()));

            await transport.OpenAsync();

            System.Threading.SpinWait.SpinUntil(() => { return(transport.State == XmppTransportState.Open); });
        }
Exemplo n.º 10
0
        private async Task AdvertiseCapabilitiesAsync()
        {
            var transport = XmppTransportManager.GetTransport();
            var presence  = new Presence
            {
                Capabilities = this.caps
            };

            await transport.SendAsync(presence, async r => await this.OnAdvertiseCapabilities(r), e => this.OnError(e))
                           .ConfigureAwait(false);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Attempts to send a chat message. The message is saved to the message store as part of the send operation.
        /// </summary>
        /// <param name="chatMessage">The chat message to be sent.</param>
        public async Task SendMessageAsync(ChatMessage chatMessage)
        {
            var status = this.ValidateMessage(chatMessage);

            if (!chatMessage.IsIncoming)
            {
                if (status.Status == ChatMessageValidationStatus.Valid ||
                    status.Status == ChatMessageValidationStatus.ValidWithLargeMessage)
                {
                    var xmppMessage = chatMessage.ToXmpp();
                    var transport   = XmppTransportManager.GetTransport();

                    chatMessage.RemoteId = xmppMessage.Id;

                    if (chatMessage.Status == ChatMessageStatus.Draft)
                    {
                        chatMessage.Status = ChatMessageStatus.Sending;
                    }
                    else
                    {
                        chatMessage.Status = ChatMessageStatus.SendRetryNeeded;
                    }

                    foreach (var deliveryInfo in chatMessage.RecipientsDeliveryInfos)
                    {
                        deliveryInfo.Status = chatMessage.Status;
                    }

                    await this.SaveMessageAsync(chatMessage).ConfigureAwait(false);

                    await transport.SendAsync(xmppMessage
                                              , null
                                              , async message => await OnMessageError(message).ConfigureAwait(false))
                    .ConfigureAwait(false);

                    chatMessage.Status = ChatMessageStatus.Sent;

                    foreach (var deliveryInfo in chatMessage.RecipientsDeliveryInfos)
                    {
                        deliveryInfo.Status = chatMessage.Status;
                    }

                    await this.SaveMessageAsync(chatMessage).ConfigureAwait(false);
                }
                else
                {
                    chatMessage.Status = ChatMessageStatus.SendRetryNeeded;
                }
            }
            else
            {
                await this.SaveMessageAsync(chatMessage).ConfigureAwait(false);
            }
        }
Exemplo n.º 12
0
        private void SubscribeToPresenceChanges()
        {
            var transport = XmppTransportManager.GetTransport();

            transport.PresenceStream
            .Where(message => message.FromAddress.BareAddress == this.Address && !message.IsError)
            .Subscribe(async message => await this.OnPresenceChangedAsync(message).ConfigureAwait(false));

            transport.StateChanged
            .Where(state => state == XmppTransportState.Closed)
            .Subscribe(state => OnDisconnected());
        }
Exemplo n.º 13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ContactResourcePresence"/> class using
        /// the given session.
        /// </summary>
        /// <param name="session"></param>
        internal ContactResourcePresence(ContactResource resource)
        {
            var transport = XmppTransportManager.GetTransport();

            this.resource       = resource;
            this.presenceStream = new Subject <ContactResource>();
            this.ShowAs         = ShowType.Offline;

            transport.StateChanged
            .Where(state => state == XmppTransportState.Closing)
            .Subscribe(state => OnDisconnecting());
        }
Exemplo n.º 14
0
        private async Task UnsuscribedAsync()
        {
            var transport = XmppTransportManager.GetTransport();
            var presence  = new Presence
            {
                To              = this.Address
                , Type          = PresenceType.Unsubscribed
                , TypeSpecified = true
            };

            await transport.SendAsync(presence).ConfigureAwait(false);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Subscribes to presence updates of the current user
        /// </summary>
        public async Task AcceptSubscriptionAsync()
        {
            var transport = XmppTransportManager.GetTransport();
            var presence  = new Presence
            {
                To              = this.Address
                , Type          = PresenceType.Subscribed
                , TypeSpecified = true
            };

            await transport.SendAsync(presence).ConfigureAwait(false);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Gets the presence information for the current contact resource.
        /// </summary>
        /// <param name="address">User address</param>
        public async Task GetPresenceAsync()
        {
            var transport = XmppTransportManager.GetTransport();
            var presence  = new Presence
            {
                From            = transport.UserAddress
                , To            = this.resource.Address
                , Type          = PresenceType.Probe
                , TypeSpecified = true
            };

            await transport.SendAsync(presence).ConfigureAwait(false);
        }
Exemplo n.º 17
0
        private async Task OnPresenceChangedAsync(Presence message)
        {
            var transport = XmppTransportManager.GetTransport();
            var resource  = this.resources.SingleOrDefault(contactResource => contactResource.Address == message.From);

            if (resource == null)
            {
                resource = new ContactResource(message.From, message);

                this.resources.Add(resource);
                this.newResourceStream.OnNext(resource);

                if (transport.ServerCapabilities != null && resource.SupportsEntityCapabilities)
                {
                    await resource.DiscoverCapabilitiesAsync().ConfigureAwait(false);
                }
            }
            else
            {
                if (message.TypeSpecified)
                {
                    switch (message.Type)
                    {
                    case PresenceType.Probe:
                        break;

                    case PresenceType.Subscribe:
                        // auto-accept subscription requests
                        await this.AcceptSubscriptionAsync().ConfigureAwait(false);

                        break;

                    case PresenceType.Unavailable:
                        this.UpdateResource(resource, message);
                        break;

                    case PresenceType.Unsubscribe:
                        await this.UnsuscribedAsync().ConfigureAwait(false);

                        break;
                    }
                }
                else
                {
                    this.UpdateResource(resource, message);
                }
            }

            this.RaisePropertyChanged(() => Resources);
            this.RaisePropertyChanged(() => HighPriorityResource);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Discover features.
        /// </summary>
        public async Task DiscoverFeaturesAsync()
        {
            var transport = XmppTransportManager.GetTransport();
            var iq        = new InfoQuery
            {
                Type          = InfoQueryType.Get
                , From        = transport.UserAddress
                , To          = this.Node
                , ServiceInfo = new ServiceInfo()
            };

            await transport.SendAsync(iq, r => this.OnDiscoverFeatures(r), e => this.OnError(e))
            .ConfigureAwait(false);
        }
Exemplo n.º 19
0
        private async Task RequestAvatarAsync()
        {
            var transport = XmppTransportManager.GetTransport();
            var iq        = new InfoQuery
            {
                From        = transport.UserAddress
                , To        = this.Address
                , Type      = InfoQueryType.Get
                , VCardData = new VCardData()
            };

            await transport.SendAsync(iq, r => this.OnAvatarResponse(r), e => this.OnError(e))
            .ConfigureAwait(false);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Initializes a new instance of the <see cref="XmppSession"/> class
        /// </summary>
        internal Activity()
        {
            var transport = XmppTransportManager.GetTransport();

            this.activities = new ObservableCollection <Event>();

            transport.StateChanged
            .Where(state => state == XmppTransportState.Open)
            .Subscribe(state => OnConnected());

            transport.StateChanged
            .Where(state => state == XmppTransportState.Closing)
            .Subscribe(state => OnDisconnecting());
        }
Exemplo n.º 21
0
        /// <summary>
        /// Request Roster list to the XMPP Server
        /// </summary>
        public async Task RequestRosterAsync()
        {
            var transport = XmppTransportManager.GetTransport();
            var iq        = new InfoQuery
            {
                Type     = InfoQueryType.Get
                , From   = transport.UserAddress
                , To     = transport.UserAddress.BareAddress
                , Roster = new Roster()
            };

            await transport.SendAsync(iq, r => this.OnUpdateRoster(r), e => this.OnRosterError(e))
            .ConfigureAwait(false);
        }
Exemplo n.º 22
0
        /// <summary>
        /// Discover if we have personal eventing support enabled
        /// </summary>
        /// <returns></returns>
        internal async Task DiscoverSupportAsync()
        {
            var transport = XmppTransportManager.GetTransport();
            var iq        = new InfoQuery
            {
                Type          = InfoQueryType.Get
                , From        = transport.UserAddress
                , To          = transport.UserAddress.BareAddress
                , ServiceItem = new ServiceItem()
            };

            await transport.SendAsync(iq, r => this.OnDiscoverSupport(r), e => this.OnDiscoverError(e))
            .ConfigureAwait(false);
        }
Exemplo n.º 23
0
        private void InitializeSubscriptions()
        {
            var transport = XmppTransportManager.GetTransport();

            transport.StateChanged
                     .Where(state => state == XmppTransportState.Open)
                     .Subscribe(async state => await AdvertiseCapabilitiesAsync().ConfigureAwait(false));

            transport.InfoQueryStream
                     .Where(message => message.To == transport.UserAddress
                         && message.IsRequest
                         && message.ServiceInfo != null
                         && message.ServiceInfo.Node == this.disco.Node)
                     .Subscribe(message => this.OnAdvertiseCapabilities(message));
        }
Exemplo n.º 24
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ContactList"/> class
        /// </summary>
        internal ContactList()
        {
            var transport = XmppTransportManager.GetTransport();

            this.contacts           = new ConcurrentBag <Contact>();
            this.contactListChanged = new Subject <CollectionChangedEventData <Contact> >();

            transport.StateChanged
            .Where(state => state == XmppTransportState.Open)
            .Subscribe(async state => await OnConnectedAsync().ConfigureAwait(false));

            transport.StateChanged
            .Where(state => state == XmppTransportState.Closing)
            .Subscribe(state => OnDisconnected());
        }
Exemplo n.º 25
0
        /// <summary>
        /// Performs the gateway registration process
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public async Task RegisterAsync(string username, string password)
        {
            var transport = XmppTransportManager.GetTransport();
            var iq        = new InfoQuery
            {
                Type       = InfoQueryType.Set
                , From     = transport.UserAddress
                , To       = this.Address
                , Register = new Register {
                    UserName = username, Password = password
                }
            };

            await transport.SendAsync(iq).ConfigureAwait(false);
        }
Exemplo n.º 26
0
        /// <summary>
        /// Performs the gateway unregistration process
        /// </summary>
        public async Task UnregisterAsync()
        {
            var transport = XmppTransportManager.GetTransport();
            var iq        = new InfoQuery
            {
                Type       = InfoQueryType.Set
                , From     = transport.UserAddress
                , To       = this.Address
                , Register = new Register {
                    Remove = String.Empty
                }
            };

            await transport.SendAsync(iq).ConfigureAwait(false);
        }
Exemplo n.º 27
0
        public async Task DiscoverAsync()
        {
            var transport = XmppTransportManager.GetTransport();

#warning TODO: Grab Capabilities from storage or send the discovery request
            var iq = new InfoQuery
            {
                From          = transport.UserAddress
                , To          = this.Address
                , Type        = InfoQueryType.Get
                , ServiceInfo = new ServiceInfo {
                    Node = this.info.Node
                }
            };

            await transport.SendAsync(iq, r => this.OnDiscoverResponse(r), r => this.OnDiscoverError(r))
            .ConfigureAwait(false);
        }
Exemplo n.º 28
0
        /// <summary>
        /// Refreshes the blocked contacts list
        /// </summary>
        /// <returns></returns>
        public async Task RefreshBlockedContactsAsync()
        {
#warning TODO: Change to use entity caps
            //if (!this.Client.ServiceDiscovery.SupportsBlocking)
            //{
            //    return;
            //}

            var transport = XmppTransportManager.GetTransport();
            var iq        = new InfoQuery
            {
                Type        = InfoQueryType.Get
                , BlockList = new BlockList()
            };

            await transport.SendAsync(iq, r => this.OnBlockedContactsResponse(r), e => this.OnBlockedContactsError(e))
            .ConfigureAwait(false);
        }
Exemplo n.º 29
0
        /// <summary>
        /// Unblocks all blocked contacts
        /// </summary>
        public async Task UnBlockAllAsync()
        {
#warning TODO: Change to use entity caps
            //if (!this.Client.ServiceDiscovery.SupportsBlocking)
            //{
            //    return;
            //}

            var transport = XmppTransportManager.GetTransport();
            var iq        = new InfoQuery
            {
                Type      = InfoQueryType.Set
                , From    = transport.UserAddress
                , Unblock = new Unblock()
            };

            await transport.SendAsync(iq, r => this.OnUnBlockAllResponse(r), e => this.OnUnBlockAllError(e))
            .ConfigureAwait(false);
        }
Exemplo n.º 30
0
        /// <summary>
        /// Unblock contact.
        /// </summary>
        public async Task UnBlockAsync()
        {
            var transport = XmppTransportManager.GetTransport();

            if (!transport.ServerCapabilities.SupportsBlocking)
            {
                return;
            }

            var iq = new InfoQuery
            {
                From      = transport.UserAddress
                , Type    = InfoQueryType.Set
                , Unblock = new Unblock(this.Address.BareAddress)
            };

            await transport.SendAsync(iq, r => this.OnContactUnBlocked(r), e => this.OnBlockingError(e))
            .ConfigureAwait(false);
        }