Exemplo n.º 1
0
        /// <summary>
        ///   Performs the gateway unregistration process
        /// </summary>
        public void Unregister()
        {
            var query = new RegisterQuery();
            var iq = new IQ();

            iq.ID = XmppIdentifierGenerator.Generate();
            iq.Type = IQType.Set;
            iq.From = Session.UserId;
            iq.To = Identifier;

            iq.Items.Add(query);

            query.Remove = "";

            PendingMessages.Add(iq.ID);

            Session.Send(iq);
        }
Exemplo n.º 2
0
        /// <summary>
        ///   Publishes the avatar.
        /// </summary>
        /// <param name = "mimetype">The mimetype.</param>
        /// <param name = "hash">The hash.</param>
        /// <param name = "avatarImage">The avatar image.</param>
        public IXmppSession PublishAvatar(string mimetype, string hash, Image avatarImage)
        {
            var avatarData = new MemoryStream();

            try
            {
                avatarImage.Save(avatarData, ImageFormat.Png);

                // Publish the avatar
                var iq = new IQ();
                var vcard = new VCardData();

                iq.ID = XmppIdentifierGenerator.Generate();
                iq.Type = IQType.Set;
                iq.From = UserId.ToString();

                vcard.Photo.Type = mimetype;
                vcard.Photo.Photo = avatarData.ToArray();

                iq.Items.Add(vcard);

                Send(iq);

                // Save the avatar
                avatarStorage.SaveAvatar(UserId.BareIdentifier, hash, avatarData);

                // Update session configuration
                avatarStorage.Save();
            }
            catch
            {
                throw;
            }
            finally
            {
                if (avatarData != null)
                {
                    avatarData.Close();
                    avatarData.Dispose();
                    avatarData = null;
                }
            }

            return this;
        }
Exemplo n.º 3
0
        private void OnServiceDiscoveryMessage(IQ message)
        {
            pendingMessages.Remove(message.ID);

            foreach (ServiceItemQuery item in message.Items)
            {
                foreach (ServiceItem sitem in item.Items)
                {
                    features.Add(sitem.Node);
                }

                //if (SupportsUserTune)
                //{

                //}
            }
        }
Exemplo n.º 4
0
        private void OnQueryErrorMessage(IQ message)
        {
            if (PendingMessages.Contains(message.ID))
            {
                PendingMessages.Remove(message.ID);

                waitEvent.Set();
            }
        }
Exemplo n.º 5
0
        private void OpenSession()
        {
            if (SupportsFeature(XmppStreamFeatures.Sessions))
            {
                var iq = new IQ();

                iq.Type = IQType.Set;
                iq.To = connectionString.HostName;
                iq.ID = XmppIdentifierGenerator.Generate();

                iq.Items.Add(new Session());

                Send(iq);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        ///   Discover if we have personal eventing support enabled
        /// </summary>
        /// <returns></returns>
        internal void DiscoverSupport()
        {
            var query = new ServiceItemQuery();
            var iq = new IQ();

            iq.ID = XmppIdentifierGenerator.Generate();
            iq.Type = IQType.Get;
            iq.From = session.UserId;
            iq.To = session.UserId.BareIdentifier;

            iq.Items.Add(query);

            pendingMessages.Add(iq.ID);
            features.Clear();

            session.Send(iq);
        }
Exemplo n.º 7
0
        private void OnQueryErrorReceived(IQ message)
        {
            pendingMessages.Remove(message.ID);

            if (message.Error.FeatureNotImplemented != null)
            {
                SupportsServiceDiscovery = false;
            }
        }
Exemplo n.º 8
0
        /// <summary>
        ///   Updates the contact data.
        /// </summary>
        public void Update()
        {
            var iq = new IQ();
            var query = new RosterQuery();
            var item = new RosterItem();

            iq.Type = IQType.Set;

            item.Jid = ContactId.BareIdentifier;
            item.Name = DisplayName;
            item.Subscription = (RosterSubscriptionType)Subscription;

            item.Groups.AddRange(Groups);

            query.Items.Add(item);
            iq.Items.Add(query);

            session.Send(iq);
        }
Exemplo n.º 9
0
        /// <summary>
        ///   Stops user tune publications
        /// </summary>
        public IXmppSession StopTunePublication()
        {
            var iq = new IQ();
            var pubsub = new PubSub();
            var publish = new PubSubPublish();
            var item = new PubSubItem();
            var tune = new Tune();

            iq.Items.Add(pubsub);
            pubsub.Items.Add(publish);
            publish.Items.Add(item);

            iq.From = UserId.ToString();
            iq.ID = XmppIdentifierGenerator.Generate();
            iq.Type = IQType.Set;
            publish.Node = XmppFeatures.UserMood;
            item.Item = tune;

            Send(iq);

            return this;
        }
Exemplo n.º 10
0
        /// <summary>
        ///   Discover existing services provided by the XMPP Server
        /// </summary>
        /// <returns></returns>
        public void DiscoverServices()
        {
            Clear();

            string domain = ((String.IsNullOrEmpty(domainName)) ? session.UserId.DomainName : domainName);
            string messageId = XmppIdentifierGenerator.Generate();
            var iq = new IQ
                         {
                             ID = messageId,
                             Type = IQType.Get,
                             From = session.UserId,
                             To = domain
                         };

            iq.Items.Add(new ServiceItemQuery());

            pendingMessages.Add(messageId);

            session.Send(iq);
        }
Exemplo n.º 11
0
        /// <summary>
        ///   Publishes user tune information
        /// </summary>
        public IXmppSession PublishTune(XmppUserTuneEvent tuneEvent)
        {
            var iq = new IQ();
            var pubsub = new PubSub();
            var publish = new PubSubPublish();
            var item = new PubSubItem();
            var tune = new Tune();

            iq.Items.Add(pubsub);
            pubsub.Items.Add(publish);
            publish.Items.Add(item);

            iq.From = UserId.ToString();
            iq.ID = XmppIdentifierGenerator.Generate();
            iq.Type = IQType.Set;
            publish.Node = XmppFeatures.UserMood;
            item.Item = tune;
            tune.Artist = tuneEvent.Artist;
            tune.Length = tuneEvent.Length;
            tune.Rating = tuneEvent.Rating;
            tune.Source = tuneEvent.Source;
            tune.Title = tuneEvent.Title;
            tune.Track = tuneEvent.Track;
            tune.Uri = tuneEvent.Uri;

            Send(iq);

            return this;
        }
Exemplo n.º 12
0
        /// <summary>
        ///   Publishes user mood information
        /// </summary>
        public IXmppSession PublishMood(XmppUserMoodEvent moodEvent)
        {
            var iq = new IQ();
            var pubsub = new PubSub();
            var publish = new PubSubPublish();
            var item = new PubSubItem();
            var mood = new Mood();

            iq.Items.Add(pubsub);
            pubsub.Items.Add(publish);
            publish.Items.Add(item);

            iq.From = UserId.ToString();
            iq.ID = XmppIdentifierGenerator.Generate();
            iq.Type = IQType.Set;
            publish.Node = XmppFeatures.UserMood;
            item.Item = mood;
            mood.MoodType = (MoodType) Enum.Parse(typeof (MoodType), moodEvent.Mood);
            mood.Text = moodEvent.Text;

            Send(iq);

            return this;
        }
Exemplo n.º 13
0
        /// <summary>
        ///   Publishes the display name.
        /// </summary>
        /// <param name = "displayName">The display name.</param>
        public IXmppSession PublishDisplayName(string displayName)
        {
            // Publish the display name ( nickname )
            var iq = new IQ();
            var vcard = new VCardData();

            iq.ID = XmppIdentifierGenerator.Generate();
            iq.Type = IQType.Set;
            iq.From = UserId.ToString();

            vcard.NickName = displayName;

            iq.Items.Add(vcard);

            Send(iq);

            return this;
        }
Exemplo n.º 14
0
        /// <summary>
        ///   Adds to group.
        /// </summary>
        /// <param name = "groupName">Name of the group.</param>
        public void AddToGroup(string groupName)
        {
            var iq = new IQ();
            var query = new RosterQuery();
            var item = new RosterItem();

            if (!Groups.Contains(groupName))
            {
                Groups.Add(groupName);
            }

            iq.Type = IQType.Set;

            item.Jid = ContactId.BareIdentifier;
            item.Name = Name;
            item.Subscription = (RosterSubscriptionType)Subscription;

            item.Groups.Add(groupName);

            query.Items.Add(item);
            iq.Items.Add(query);

            session.Send(iq);
        }
Exemplo n.º 15
0
        private void OnServiceDiscoveryMessageReceived(IQ message)
        {
            pendingMessages.Remove(message.ID);
            SupportsServiceDiscovery = true;

            foreach (object item in message.Items)
            {
                if (item is ServiceItemQuery)
                {
                    // List of availabl services
                    foreach (ServiceItem serviceItem in ((ServiceItemQuery) item).Items)
                    {
                        services.Add(new XmppService(session, serviceItem.Jid));
                    }
                }
            }

            //NotifyPropertyChanged(() => Services);
        }
Exemplo n.º 16
0
        /// <summary>
        ///   Unblock contact.
        /// </summary>
        public void UnBlock()
        {
            if (session.ServiceDiscovery.SupportsBlocking)
            {
                var iq = new IQ
                             {
                                 ID = XmppIdentifierGenerator.Generate(),
                                 From = session.UserId,
                                 Type = IQType.Set
                             };

                var unBlock = new UnBlock();

                unBlock.Items.Add
                    (
                        new BlockItem
                            {
                                Jid = ContactId.BareIdentifier
                            }
                    );

                iq.Items.Add(unBlock);

                session.Send(iq);
            }
        }
Exemplo n.º 17
0
        private void DiscoverCapabilities()
        {
            var requestIq = new IQ();
            var request = new ServiceQuery();

            request.Node = Capabilities.DiscoveryInfoNode;
            requestIq.From = session.UserId;
            requestIq.ID = XmppIdentifierGenerator.Generate();
            requestIq.To = ResourceId;
            requestIq.Type = IQType.Get;

            requestIq.Items.Add(request);

            pendingMessages.Add(requestIq.ID);

            session.Send(requestIq);
        }
Exemplo n.º 18
0
        private void BindResource()
        {
            if (SupportsFeature(XmppStreamFeatures.ResourceBinding))
            {
                var bind = new Bind();
                bind.Resource = UserId.ResourceName;

                var iq = new IQ();
                iq.Type = IQType.Set;
                iq.ID = XmppIdentifierGenerator.Generate();

                iq.Items.Add(bind);

                Send(iq);

                bindResourceEvent.WaitOne();
            }
        }
Exemplo n.º 19
0
        private void OnServiceDiscoveryMessage(IQ message)
        {
            pendingMessages.Remove(message.ID);

            Capabilities.Identities.Clear();
            Capabilities.Features.Clear();

            // Reponse to our capabilities query
            foreach (object item in message.Items)
            {
                if (item is ServiceQuery)
                {
                    var query = (ServiceQuery) item;

                    foreach (ServiceIdentity identity in query.Identities)
                    {
                        Capabilities.Identities.Add
                            (
                                new XmppServiceIdentity(identity.Name, identity.Category, identity.Type)
                            );
                    }

                    foreach (ServiceFeature supportedFeature in query.Features)
                    {
                        Capabilities.Features.Add(new XmppServiceFeature(supportedFeature.Name));
                    }
                }
            }

            if (!session.ClientCapabilitiesStorage.Exists(capabilities.Node, capabilities.VerificationString))
            {
                session.ClientCapabilitiesStorage.ClientCapabilities.Add(Capabilities);
                session.ClientCapabilitiesStorage.Save();
            }

            // NotifyPropertyChanged(() => Capabilities);
        }
Exemplo n.º 20
0
        /// <summary>
        ///   Process an XMPP IQ message
        /// </summary>
        /// <param name = "iq"></param>
        private void ProcessQueryMessage(IQ iq)
        {
            if (iq.Items != null && iq.Items.Count > 0)
            {
                foreach (object item in iq.Items)
                {
                    if (iq.Type != IQType.Error)
                    {
                        if (item is Bind)
                        {
                            userId = ((Bind) item).Jid;

                            bindResourceEvent.Set();
                        }
                        else if (item is RosterQuery)
                        {
                            onRosterMessage.OnNext(item as RosterQuery);
                        }
                        else if (item is VCardData)
                        {
                            onVCardMessage.OnNext(iq);
                        }
                        else if (item is Ping)
                        {
                            if (iq.Type == IQType.Get)
                            {
                                // Send the "pong" response
                                Send
                                    (
                                        new IQ
                                            {
                                                ID = iq.ID,
                                                To = iq.From,
                                                From = UserId.ToString(),
                                                Type = IQType.Result
                                            }
                                    );
                            }
                        }
                    }

                    if (item is ServiceQuery || item is ServiceItemQuery)
                    {
                        onServiceDiscoveryMessage.OnNext(iq);
                    }
                }
            }
        }
Exemplo n.º 21
0
        private void OnVCardMessage(IQ message)
        {
            // Update the Avatar image
            var vCard = (VCardData) message.Items[0];

            if (vCard.Photo.Photo != null && vCard.Photo.Photo.Length > 0)
            {
                Image avatarImage = null;

                try
                {
                    DisposeAvatarStream();

                    using (var avatarStream = new MemoryStream(vCard.Photo.Photo))
                    {
                        // En sure it's a valid image
                        avatarImage = Image.FromStream(avatarStream);

                        // Save avatar
                        if (avatarStream != null && avatarStream.Length > 0)
                        {
                            session.AvatarStorage.SaveAvatar(ResourceId.BareIdentifier, avatarHash, avatarStream);
                        }
                    }

                    Avatar = session.AvatarStorage.ReadAvatar(ResourceId.BareIdentifier);
                }
                catch
                {
            #warning TODO: Handle the exception
                }
                finally
                {
                    if (avatarImage != null)
                    {
                        avatarImage.Dispose();
                        avatarImage = null;
                    }
                }
            }
            else
            {
                session.AvatarStorage.RemoveAvatar(ResourceId.BareIdentifier);
            }

            session.AvatarStorage.Save();
        }
Exemplo n.º 22
0
 private void OnQueryErrorMessage(IQ message)
 {
     if (pendingMessages.Contains(message.ID))
     {
         pendingMessages.Remove(message.ID);
     }
 }
Exemplo n.º 23
0
        private void RequestAvatar()
        {
            if (contact.Subscription == XmppContactSubscriptionType.Both ||
                contact.Subscription == XmppContactSubscriptionType.To)
            {
                var iq = new IQ();

                iq.ID = XmppIdentifierGenerator.Generate();
                iq.Type = IQType.Get;
                iq.To = ResourceId;
                iq.From = session.UserId;

                iq.Items.Add(new VCardData());

                session.Send(iq);
            }
        }
Exemplo n.º 24
0
        /// <summary>
        ///   Discover item items.
        /// </summary>
        public virtual void DiscoverItems()
        {
            if (!featuresRequested)
            {
                DiscoverFeatures();
            }

            if (items == null)
            {
                items = new List<XmppServiceItem>();
            }
            else
            {
                items.Clear();
            }

            // Get Service Details
            var iq = new IQ
                         {
                             ID = XmppIdentifierGenerator.Generate(),
                             Type = IQType.Get,
                             From = session.UserId,
                             To = Identifier
                         };

            iq.Items.Add(new ServiceItemQuery());

            itemsRequested = true;
            PendingMessages.Add(iq.ID);
            session.Send(iq);

            waitEvent.WaitOne();
        }
Exemplo n.º 25
0
 protected virtual void OnQueryErrorMessage(IQ message)
 {
 }
Exemplo n.º 26
0
        private void OnServiceDiscoveryMessage(IQ message)
        {
            if (PendingMessages.Contains(message.ID))
            {
                PendingMessages.Remove(message.ID);

                foreach (object item in message.Items)
                {
                    if (item is ServiceItemQuery)
                    {
                        foreach (ServiceItem serviceItem in ((ServiceItemQuery) item).Items)
                        {
                            items.Add(new XmppServiceItem(session, serviceItem.Jid));
                        }

                        //NotifyPropertyChanged(() => Items);
                    }
                    else if (item is ServiceQuery)
                    {
                        // Details of available services
                        var query = (ServiceQuery) item;

                        if (identities == null)
                        {
                            identities = new List<XmppServiceIdentity>();
                        }

                        identities.Clear();

                        foreach (ServiceIdentity identity in query.Identities)
                        {
                            identities.Add(new XmppServiceIdentity(identity.Name, identity.Category, identity.Type));
                        }

                        foreach (ServiceFeature feature in query.Features)
                        {
                            features.Add(new XmppServiceFeature(feature.Name));
                        }

                        //NotifyPropertyChanged(() => Identities);
                        //NotifyPropertyChanged(() => Features);
                    }
                }

                waitEvent.Set();
            }
        }
Exemplo n.º 27
0
 protected abstract void OnServiceDiscoveryMessage(IQ message);
Exemplo n.º 28
0
        /// <summary>
        ///   Performs the gateway registration process
        /// </summary>
        /// <param name = "username"></param>
        /// <param name = "password"></param>
        public void Register(string username, string password)
        {
            var query = new RegisterQuery();
            var iq = new IQ();

            iq.ID = XmppIdentifierGenerator.Generate();
            iq.Type = IQType.Set;
            iq.From = Session.UserId;
            iq.To = Identifier;

            iq.Items.Add(query);

            query.UserName = username;
            query.Password = password;

            PendingMessages.Add(iq.ID);

            Session.Send(iq);
        }
Exemplo n.º 29
0
        protected override void OnServiceDiscoveryMessage(IQ message)
        {
            // Answers to Entity Capabilities and service discovery info requests
            if (message.Type == IQType.Get)
            {
                if (message.Items[0] is ServiceQuery)
                {
                    var query = (ServiceQuery) message.Items[0];
                    var response = new ServiceQuery
                                       {
                                           Node = ((!String.IsNullOrEmpty(query.Node)) ? DiscoveryInfoNode : null)
                                       };

                    var responseIq = new IQ
                                         {
                                             From = session.UserId,
                                             ID = message.ID,
                                             To = message.From,
                                             Type = IQType.Result
                                         };

                    foreach (XmppServiceIdentity identity in Identities)
                    {
                        var supportedIdentity = new ServiceIdentity
                                                    {
                                                        Name = identity.Name,
                                                        Category = identity.Category.ToString().ToLower(),
                                                        Type = identity.Type
                                                    };

                        response.Identities.Add(supportedIdentity);
                    }

                    foreach (XmppServiceFeature supportedFeature in Features)
                    {
                        var feature = new ServiceFeature
                                          {
                                              Name = supportedFeature.Name
                                          };

                        response.Features.Add(feature);
                    }

                    responseIq.Items.Add(response);

                    session.Send(responseIq);

                    //// Error response
                    //IQ errorIq = new IQ();
                    //ServiceQuery response = new ServiceQuery();
                    //Error error = new Error();

                    //errorIq.From = this.session.UserId.ToString();
                    //errorIq.ID = e.QueryResult.ID;
                    //errorIq.To = e.QueryResult.From;
                    //errorIq.Type = IQType.Error;
                    //errorIq.Error = error;
                    //response.Node = query.Node;
                    //error.Type = ErrorType.Cancel;
                    //error.ItemNotFound = "";

                    //errorIq.Items.Add(response);

                    //this.session.Send(errorIq);
                }
            }
        }