Exemplo n.º 1
0
        /// <summary>
        ///   Sends the message.
        /// </summary>
        /// <param name = "message">The message.</param>
        public string SendMessage(string message)
        {
            if (session == null)
            {
                throw new InvalidOperationException("Chat session is closed.");
            }

            var body = new MessageBody
            {
                Value = message
            };

            var chatMessage = new Message
            {
                ID   = XmppIdentifierGenerator.Generate(),
                Type = MessageType.Chat,
                From = session.UserId.ToString(),
                To   = Contact.ContactId.ToString(),
            };

            if (Contact.SupportsChatStateNotifications)
            {
                chatMessage.Items.Add(CreateChatStateNotification(XmppChatStateNotification.Active));
            }

            chatMessage.Items.Add(body);

            session.Send(chatMessage);

            return(chatMessage.ID);
        }
Exemplo n.º 2
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.º 3
0
        /// <summary>
        /// Performs the authentication using the Jabber mechanism.
        /// </summary>
        public override void Authenticate()
        {
            this.RequestAuthInformation();

            // Do authentication
            JabberAuthQuery auth = new JabberAuthQuery();

            if (authInfo.UserName != null)
            {
                auth.UserName = this.Connection.UserId.UserName;
            }
            if (authInfo.Resource != null)
            {
                auth.Resource = this.Connection.UserId.ResourceName;
            }
            if (authInfo.Digest != null)
            {
                auth.Digest = this.GenerateDigest(this.Connection.UserPassword);
            }
            else if (authInfo.Password != null)
            {
                auth.Password = this.Connection.UserPassword;
            }

            IQ iq = new IQ();

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

            iq.Items.Add(auth);

            this.PendingMessages.Add(iq.ID);

            this.Connection.Send(iq);
        }
Exemplo n.º 4
0
        /// <summary>
        ///   Adds the given contact to the roster
        /// </summary>
        /// <param name = "jid">Contact JID</param>
        /// <param name = "name">Contact name</param>
        public XmppRoster AddContact(string jid, string name)
        {
            var query = new RosterQuery();
            var iq    = new IQ();

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

            var item = new RosterItem();

            item.Subscription = RosterSubscriptionType.None;
            item.Jid          = jid;
            item.Name         = name;

            query.Items.Add(item);

            iq.Items.Add(query);

            pendingMessages.Add(iq.ID);

            connection.Send(iq);

            return(this);
        }
Exemplo n.º 5
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.º 6
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.º 7
0
        /// <summary>
        ///   Gets the presence of the given user.
        /// </summary>
        /// <param name = "targetJid">User JID</param>
        public void GetPresence(XmppJid targetJid)
        {
            var presence = new Presence
            {
                Id   = XmppIdentifierGenerator.Generate(),
                Type = PresenceType.Probe,
                From = session.UserId,
                To   = targetJid
            };

            session.Send(presence);
        }
Exemplo n.º 8
0
        /// <summary>
        ///   Sets the presence state with the given state, status message and priority
        /// </summary>
        /// <param name = "showAs"></param>
        /// <param name = "statusMessage"></param>
        /// <param name = "priority"></param>
        public void SetPresence(XmppPresenceState showAs, string statusMessage, int priority)
        {
            var presence = new Presence();
            var status   = new Status();

            status.Value = statusMessage;
            presence.Id  = XmppIdentifierGenerator.Generate();

            presence.Items.Add((ShowType)showAs);
            presence.Items.Add(status);

            session.Send(presence);
        }
Exemplo n.º 9
0
        /// <summary>
        ///   Closes the chatroom
        /// </summary>
        public void Close()
        {
            var presence = new Presence
            {
                Id   = XmppIdentifierGenerator.Generate(),
                To   = Identifier,
                Type = PresenceType.Unavailable
            };

            PendingMessages.Add(presence.Id);

            Session.Send(presence);
        }
Exemplo n.º 10
0
        /// <summary>
        ///   Request Roster list to the XMPP Server
        /// </summary>
        public XmppRoster RequestRosterList()
        {
            var query = new RosterQuery();
            var iq    = new IQ();

            iq.ID   = XmppIdentifierGenerator.Generate();
            iq.Type = IQType.Get;
            iq.From = connection.UserId;

            iq.Items.Add(query);

            connection.Send(iq);

            return(this);
        }
Exemplo n.º 11
0
        private void RequestAuthInformation()
        {
            JabberAuthQuery auth = new JabberAuthQuery();
            IQ request           = new IQ();

            auth.UserName = this.Connection.UserId.UserName;
            request.To    = this.Connection.Server;
            request.Type  = IQType.Get;
            request.ID    = XmppIdentifierGenerator.Generate();

            request.Items.Add(auth);

            this.Connection.Send(request);

            // Wait for Response. It will be received in async mode
            this.authInfoEvent.WaitOne();
        }
        public void DiscoverCapabilities()
        {
            IQ           requestIq = new IQ();
            ServiceQuery request   = new ServiceQuery();

            request.Node   = this.DiscoveryInfoNode;
            requestIq.From = this.Session.UserId.ToString();
            requestIq.ID   = XmppIdentifierGenerator.Generate();
            requestIq.To   = this.resource.ResourceId.ToString();
            requestIq.Type = IQType.Get;

            requestIq.Items.Add(request);

            this.pendingMessages.Add(requestIq.ID);

            this.Session.Send(requestIq);
        }
Exemplo n.º 13
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.º 14
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.º 15
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.º 16
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.º 17
0
        /// <summary>
        ///   Unblocks all blocked contacts
        /// </summary>
        public XmppRoster UnBlockAll()
        {
            if (session.ServiceDiscovery.SupportsBlocking)
            {
                var iq = new IQ
                {
                    ID   = XmppIdentifierGenerator.Generate(),
                    From = session.UserId,
                    Type = IQType.Set
                };

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

                session.Send(iq);
            }

            return(this);
        }
Exemplo n.º 18
0
        /// <summary>
        ///   Sends a chat state notification
        /// </summary>
        /// <param name = "notificationType"></param>
        public void SendChatStateNotification(XmppChatStateNotification notificationType)
        {
            // Generate the notification only if the target entity supports it
            if (Contact.SupportsChatStateNotifications)
            {
                var message = new Message
                {
                    ID   = XmppIdentifierGenerator.Generate(),
                    Type = MessageType.Chat,
                    From = session.UserId.ToString(),
                    To   = Contact.ContactId.ToString(),
                };

                message.Items.Add(CreateChatStateNotification(notificationType));

                session.Send(message);
            }
        }
Exemplo n.º 19
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.º 20
0
        /// <summary>
        ///   Refreshes the blocked contacts list
        /// </summary>
        /// <returns></returns>
        public XmppRoster RefreshBlockedContacts()
        {
#warning TODO: Check if contact list should be stored in a separated collection or the information should be injected into XmppContact class
            if (session.ServiceDiscovery.SupportsBlocking)
            {
                var iq = new IQ
                {
                    ID   = XmppIdentifierGenerator.Generate(),
                    Type = IQType.Get
                };

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

                session.Send(iq);
            }

            return(this);
        }
Exemplo n.º 21
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.º 22
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.º 23
0
        public void AdvertiseCapabilities()
        {
            if (!String.IsNullOrEmpty(ServiceDiscoveryName) &&
                !String.IsNullOrEmpty(Node) &&
                Identities.Count > 0)
            {
                var presence = new Presence
                {
                    Id = XmppIdentifierGenerator.Generate()
                };

                if (session.Capabilities != null)
                {
                    presence.Items.Add(GetEntityCapabilities());
                }

                session.Send(presence);
            }
        }
Exemplo n.º 24
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.º 25
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.º 26
0
        /// <summary>
        ///   Sends the message.
        /// </summary>
        /// <param name = "message">The message.</param>
        public XmppChatRoom SendMessage(string message)
        {
            var chatMessage = new Message
            {
                ID   = XmppIdentifierGenerator.Generate(),
                Type = MessageType.GroupChat,
                From = Session.UserId.ToString(),
                To   = Identifier
            };

            chatMessage.Items.Add
            (
                new MessageBody
            {
                Value = message
            }
            );

            Session.Send(chatMessage);

            return(this);
        }
Exemplo n.º 27
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.º 28
0
        /// <summary>
        ///   Deletes a user from the roster list
        /// </summary>
        public XmppRoster RemoveContact(XmppJid jid)
        {
            var query = new RosterQuery();
            var iq    = new IQ();

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

            var item = new RosterItem();

            item.Jid          = jid.BareIdentifier;
            item.Subscription = RosterSubscriptionType.Remove;

            query.Items.Add(item);

            iq.Items.Add(query);

            pendingMessages.Add(iq.ID);

            connection.Send(iq);

            return(this);
        }
Exemplo n.º 29
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="XmppSaslDigestAuthenticator" /> class.
 /// </summary>
 public XmppSaslDigestAuthenticator(XmppConnection connection)
     : base(connection)
 {
     cnonce       = Convert.ToBase64String(Encoding.UTF8.GetBytes(XmppIdentifierGenerator.Generate()));
     successEvent = new AutoResetEvent(false);
 }
Exemplo n.º 30
0
 /// <summary>
 ///   Creates the chat room.
 /// </summary>
 /// <param name = "chatRoomName">Name of the chat room.</param>
 /// <returns></returns>
 public XmppChatRoom EnterChatRoom()
 {
     return(EnterChatRoom(XmppIdentifierGenerator.Generate()));
 }