Exemplo n.º 1
0
 /// <summary>
 ///   Initializes a new instance of the <see cref = "T:XmppEventMessage" /> class.
 /// </summary>
 /// <param name = "message">The event.</param>
 internal XmppEventMessage(Message message)
 {
     identifier   = message.ID;
     @from        = message.From;
     to           = message.To;
     eventMessage = (PubSubEvent)message.Items[0];
 }
Exemplo n.º 2
0
 /// <summary>
 ///   Initializes a new instance of the <see cref = "T:XmppEventMessage" /> class.
 /// </summary>
 /// <param name = "message">The event.</param>
 internal XmppEventMessage(Message message)
 {
     identifier = message.ID;
     @from = message.From;
     to = message.To;
     eventMessage = (PubSubEvent) message.Items[0];
 }
Exemplo n.º 3
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.º 4
0
        private void Initialize(Message message)
        {
            identifier            = message.ID;
            @from                 = message.From;
            to                    = message.To;
            language              = message.Lang;
            type                  = message.Type;
            thread                = String.Empty;
            chatStateNotification = XmppChatStateNotification.None;

            foreach (object item in message.Items)
            {
                if (item is MessageBody)
                {
                    body = ((MessageBody)item).Value;
                }
                else if (item is MessageSubject)
                {
                    subject = ((MessageSubject)item).Value;
                }
                else if (item is NotificationActive)
                {
                    chatStateNotification = XmppChatStateNotification.Active;
                }
                else if (item is NotificationComposing)
                {
                    chatStateNotification = XmppChatStateNotification.Composing;
                }
                else if (item is NotificationGone)
                {
                    chatStateNotification = XmppChatStateNotification.Gone;
                }
                else if (item is NotificationInactive)
                {
                    chatStateNotification = XmppChatStateNotification.Inactive;
                }
                else if (item is NotificationPaused)
                {
                    chatStateNotification = XmppChatStateNotification.Paused;
                }
            }
        }
Exemplo n.º 5
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.º 6
0
        /// <summary>
        ///   Opens the connection
        /// </summary>
        /// <param name = "connectionString">The connection string used for authentication.</param>
        public void Open(string connectionString)
        {
            if (state == XmppConnectionState.Open)
            {
                throw new XmppException("Connection must be closed first.");
            }

            try
            {
                // Initialization
                Initialize();

                // Build the connection string
                this.connectionString = new XmppConnectionString(connectionString);
                userId = this.connectionString.UserId;

                // Connect to the server
                if (this.connectionString.UseHttpBinding)
                {
                    transport = new HttpTransport();
                }
                else
                {
                    transport = new TcpTransport();
                }

                transportMessageSubscription = transport
                    .OnMessageReceived
                    .Subscribe(message => OnTransportMessageReceived(message));

                transportStreamInitializedSubscription = transport
                    .OnXmppStreamInitialized
                    .Subscribe(message => OnTransportXmppStreamInitialized(message));

                transportStreamClosedSubscription = transport
                    .OnXmppStreamClosed
                    .Subscribe(message => OnTransportXmppStreamClosed(message));

                transport.Open(this.connectionString);

                // Initialize XMPP Stream
                InitializeXmppStream();

                // Wait until we receive the Stream features
                WaitForStreamFeatures();

                if (transport is ISecureTransport)
                {
                    if (this.connectionString.PortNumber != 443 &&
                        this.connectionString.PortNumber != 5223)
                    {
                        if (SupportsFeature(XmppStreamFeatures.SecureConnection))
                        {
                            ((ISecureTransport) transport).OpenSecureConnection();

                            // Wait until we receive the Stream features
                            WaitForStreamFeatures();
                        }
                    }
                }

                // Perform authentication
                bool authenticationDone = Authenticate();

                if (authenticationDone)
                {
                    // Resource Binding.
                    BindResource();

                    // Open the session
                    OpenSession();

                    // Update state
                    state = XmppConnectionState.Open;
                }
            }
            catch (Exception ex)
            {
                if (AuthenticationFailiure != null)
                {
                    AuthenticationFailiure(this, new XmppAuthenticationFailiureEventArgs(ex.ToString()));
                }

                Close();
            }
        }
Exemplo n.º 7
0
        /// <summary>
        ///   Closes the connection
        /// </summary>
        public void Close()
        {
            if (state != XmppConnectionState.Closed)
            {
                if (ConnectionClosing != null)
                {
                    ConnectionClosing(this, new EventArgs());
                }

                try
                {
                    state = XmppConnectionState.Closing;

                    if (transport != null)
                    {
                        transport.Close();
                    }
                }
                catch
                {
                }
                finally
                {
                    if (initializedStreamEvent != null)
                    {
                        initializedStreamEvent.Set();
                        initializedStreamEvent = null;
                    }

                    if (streamFeaturesEvent != null)
                    {
                        streamFeaturesEvent.Set();
                        streamFeaturesEvent = null;
                    }

                    if (bindResourceEvent != null)
                    {
                        bindResourceEvent.Set();
                        bindResourceEvent = null;
                    }

                    if (initializedStreamEvent != null)
                    {
                        initializedStreamEvent.Close();
                        initializedStreamEvent = null;
                    }

                    if (streamFeaturesEvent != null)
                    {
                        streamFeaturesEvent.Close();
                        streamFeaturesEvent = null;
                    }

                    if (bindResourceEvent != null)
                    {
                        bindResourceEvent.Close();
                        bindResourceEvent = null;
                    }

                    if (transportMessageSubscription != null)
                    {
                        transportMessageSubscription.Dispose();
                        transportMessageSubscription = null;
                    }

                    if (transportStreamInitializedSubscription != null)
                    {
                        transportStreamInitializedSubscription.Dispose();
                        transportStreamInitializedSubscription = null;
                    }

                    if (transportStreamClosedSubscription != null)
                    {
                        transportStreamClosedSubscription.Dispose();
                        transportStreamClosedSubscription = null;
                    }

                    if (transport != null)
                    {
                        transport = null;
                    }

                    streamFeatures = streamFeatures & (~streamFeatures);
                    state = XmppConnectionState.Closed;
                    connectionString = null;
                    userId = null;
                }

                if (ConnectionClosed != null)
                {
                    ConnectionClosed(this, new EventArgs());
                }
            }
        }
Exemplo n.º 8
0
        private void Initialize(Message message)
        {
            identifier = message.ID;
            @from = message.From;
            to = message.To;
            language = message.Lang;
            type = message.Type;
            thread = String.Empty;
            chatStateNotification = XmppChatStateNotification.None;

            foreach (object item in message.Items)
            {
                if (item is MessageBody)
                {
                    body = ((MessageBody) item).Value;
                }
                else if (item is MessageSubject)
                {
                    subject = ((MessageSubject) item).Value;
                }
                else if (item is NotificationActive)
                {
                    chatStateNotification = XmppChatStateNotification.Active;
                }
                else if (item is NotificationComposing)
                {
                    chatStateNotification = XmppChatStateNotification.Composing;
                }
                else if (item is NotificationGone)
                {
                    chatStateNotification = XmppChatStateNotification.Gone;
                }
                else if (item is NotificationInactive)
                {
                    chatStateNotification = XmppChatStateNotification.Inactive;
                }
                else if (item is NotificationPaused)
                {
                    chatStateNotification = XmppChatStateNotification.Paused;
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        ///   Closes the connection
        /// </summary>
        public void Close()
        {
            if (state != XmppConnectionState.Closed)
            {
                if (ConnectionClosing != null)
                {
                    ConnectionClosing(this, new EventArgs());
                }

                try
                {
                    state = XmppConnectionState.Closing;

                    if (transport != null)
                    {
                        transport.Close();
                    }
                }
                catch
                {
                }
                finally
                {
                    if (initializedStreamEvent != null)
                    {
                        initializedStreamEvent.Set();
                        initializedStreamEvent = null;
                    }

                    if (streamFeaturesEvent != null)
                    {
                        streamFeaturesEvent.Set();
                        streamFeaturesEvent = null;
                    }

                    if (bindResourceEvent != null)
                    {
                        bindResourceEvent.Set();
                        bindResourceEvent = null;
                    }

                    if (initializedStreamEvent != null)
                    {
                        initializedStreamEvent.Close();
                        initializedStreamEvent = null;
                    }

                    if (streamFeaturesEvent != null)
                    {
                        streamFeaturesEvent.Close();
                        streamFeaturesEvent = null;
                    }

                    if (bindResourceEvent != null)
                    {
                        bindResourceEvent.Close();
                        bindResourceEvent = null;
                    }

                    if (transportMessageSubscription != null)
                    {
                        transportMessageSubscription.Dispose();
                        transportMessageSubscription = null;
                    }

                    if (transportStreamInitializedSubscription != null)
                    {
                        transportStreamInitializedSubscription.Dispose();
                        transportStreamInitializedSubscription = null;
                    }

                    if (transportStreamClosedSubscription != null)
                    {
                        transportStreamClosedSubscription.Dispose();
                        transportStreamClosedSubscription = null;
                    }

                    if (transport != null)
                    {
                        transport = null;
                    }

                    streamFeatures   = streamFeatures & (~streamFeatures);
                    state            = XmppConnectionState.Closed;
                    connectionString = null;
                    userId           = null;
                }

                if (ConnectionClosed != null)
                {
                    ConnectionClosed(this, new EventArgs());
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        ///   Opens the connection
        /// </summary>
        /// <param name = "connectionString">The connection string used for authentication.</param>
        public void Open(string connectionString)
        {
            if (state == XmppConnectionState.Open)
            {
                throw new XmppException("Connection must be closed first.");
            }

            try
            {
                // Initialization
                Initialize();

                // Build the connection string
                this.connectionString = new XmppConnectionString(connectionString);
                userId = this.connectionString.UserId;

                // Connect to the server
                if (this.connectionString.UseHttpBinding)
                {
                    transport = new HttpTransport();
                }
                else
                {
                    transport = new TcpTransport();
                }

                transportMessageSubscription = transport
                                               .OnMessageReceived
                                               .Subscribe(message => OnTransportMessageReceived(message));

                transportStreamInitializedSubscription = transport
                                                         .OnXmppStreamInitialized
                                                         .Subscribe(message => OnTransportXmppStreamInitialized(message));

                transportStreamClosedSubscription = transport
                                                    .OnXmppStreamClosed
                                                    .Subscribe(message => OnTransportXmppStreamClosed(message));

                transport.Open(this.connectionString);

                // Initialize XMPP Stream
                InitializeXmppStream();

                // Wait until we receive the Stream features
                WaitForStreamFeatures();

                if (transport is ISecureTransport)
                {
                    if (this.connectionString.PortNumber != 443 &&
                        this.connectionString.PortNumber != 5223)
                    {
                        if (SupportsFeature(XmppStreamFeatures.SecureConnection))
                        {
                            ((ISecureTransport)transport).OpenSecureConnection();

                            // Wait until we receive the Stream features
                            WaitForStreamFeatures();
                        }
                    }
                }

                // Perform authentication
                bool authenticationDone = Authenticate();

                if (authenticationDone)
                {
                    // Resource Binding.
                    BindResource();

                    // Open the session
                    OpenSession();

                    // Update state
                    state = XmppConnectionState.Open;
                }
            }
            catch (Exception ex)
            {
                if (AuthenticationFailiure != null)
                {
                    AuthenticationFailiure(this, new XmppAuthenticationFailiureEventArgs(ex.ToString()));
                }

                Close();
            }
        }