/// <summary> /// Initializes a new instance of the <see cref="T:XmppEventMessage"/> class. /// </summary> /// <param name="message">The event.</param> internal XmppEventMessage(Message message) { this.identifier = message.ID; this.from = message.From; this.to = message.To; this.eventMessage = (PubSubEvent)message.Items[0]; }
/// <summary> /// Initializes a new instance of the <see cref="T:XmppChatRoom"/> class. /// </summary> /// <param name="session">The session.</param> /// <param name="conferenceService">The conference service.</param> /// <param name="chatRoomId">The chat room id.</param> internal XmppChatRoom(XmppSession session, XmppService conferenceService, XmppJid chatRoomId) : base(session, chatRoomId) { this.conferenceService = conferenceService; this.seekEnterChatRoomEvent = new AutoResetEvent(false); this.createChatRoomEvent = new AutoResetEvent(false); }
/// <summary> /// Initializes a new instance of the <see cref="XmppContactResource"/> class. /// </summary> internal XmppContactResource(XmppSession session, XmppContact contact, XmppJid resourceId) { this.session = session; this.contact = contact; this.resourceId = resourceId; this.presence = new XmppContactPresence(this.session); this.capabilities = new XmppClientCapabilities(); this.pendingMessages = new List<string>(); this.Subscribe(); }
/// <summary> /// Gets the presence of the given user. /// </summary> /// <param name="targetJid">User JID</param> public void GetPresence(XmppJid targetJid) { Presence presence = new Presence { Id = XmppIdentifierGenerator.Generate(), Type = PresenceType.Probe, From = this.session.UserId, To = targetJid }; this.session.Send(presence); }
/// <summary> /// Creates the chat room. /// </summary> /// <param name="chatRoomName">Name of the chat room.</param> /// <returns></returns> public XmppChatRoom EnterChatRoom(string chatRoomName) { this.CheckSessionState(); XmppService service = this.ServiceDiscovery.GetService(XmppServiceCategory.Conference); XmppChatRoom chatRoom = null; XmppJid chatRoomId = new XmppJid ( chatRoomName, service.Identifier, this.UserId.UserName ); if (service != null) { chatRoom = new XmppChatRoom(this, service, chatRoomId); chatRoom.Enter(); } return chatRoom; }
/// <summary> /// Checks if a given user has an open chat session /// </summary> /// <param name="contactId"></param> /// <returns></returns> public bool HasOpenChat(XmppJid contactId) { return this.HasOpenChat(contactId.BareIdentifier); }
private void Initialize(Message message) { this.identifier = message.ID; this.from = message.From; this.to = message.To; this.language = message.Lang; this.type = message.Type; this.thread = String.Empty; this.chatStateNotification = XmppChatStateNotification.None; foreach (object item in message.Items) { if (item is MessageBody) { this.body = ((MessageBody)item).Value; } else if (item is MessageSubject) { this.subject = ((MessageSubject)item).Value; } else if (item is NotificationActive) { this.chatStateNotification = XmppChatStateNotification.Active; } else if (item is NotificationComposing) { this.chatStateNotification = XmppChatStateNotification.Composing; } else if (item is NotificationGone) { this.chatStateNotification = XmppChatStateNotification.Gone; } else if (item is NotificationInactive) { this.chatStateNotification = XmppChatStateNotification.Inactive; } else if (item is NotificationPaused) { this.chatStateNotification = XmppChatStateNotification.Paused; } } }
/// <summary> /// Creates the chat. /// </summary> /// <param name="contactId">The contact id.</param> /// <returns></returns> public XmppChat CreateChat(XmppJid contactId) { this.CheckSessionState(); XmppChat chat = null; lock (this.syncObject) { if (!this.chats.ContainsKey(contactId.BareIdentifier)) { chat = new XmppChat(this, this.Roster[contactId.BareIdentifier]); this.chats.Add(contactId.BareIdentifier, chat); chat.ChatClosed += new EventHandler(OnChatClosed); } else { chat = this.chats[contactId.BareIdentifier]; } } return chat; }
/// <summary> /// Opens the connection /// </summary> /// <param name="connectionString">The connection string used for authentication.</param> public void Open(string connectionString) { if (this.state == XmppConnectionState.Open) { throw new XmppException("Connection must be closed first."); } try { // Initialization this.Initialize(); // Build the connection string this.connectionString = new XmppConnectionString(connectionString); this.userId = this.connectionString.UserId; // Connect to the server if (this.connectionString.UseHttpBinding) { this.transport = new HttpTransport(); } else { this.transport = new TcpTransport(); } this.transportMessageSubscription = this.transport .OnMessageReceived .Subscribe(message => this.OnTransportMessageReceived(message)); this.transportStreamInitializedSubscription = this.transport .OnXmppStreamInitialized .Subscribe(message => this.OnTransportXmppStreamInitialized(message)); this.transportStreamClosedSubscription = this.transport .OnXmppStreamClosed .Subscribe(message => this.OnTransportXmppStreamClosed(message)); this.transport.Open(this.connectionString); // Initialize XMPP Stream this.InitializeXmppStream(); // Wait until we receive the Stream features this.WaitForStreamFeatures(); if (this.transport is ISecureTransport) { if (this.connectionString.PortNumber != 443 && this.connectionString.PortNumber != 5223) { if (this.SupportsFeature(XmppStreamFeatures.SecureConnection)) { ((ISecureTransport)this.transport).OpenSecureConnection(); // Wait until we receive the Stream features this.WaitForStreamFeatures(); } } } // Perform authentication bool authenticationDone = this.Authenticate(); if (authenticationDone) { // Resource Binding. this.BindResource(); // Open the session this.OpenSession(); // Update state this.state = XmppConnectionState.Open; } } catch (Exception ex) { if (this.AuthenticationFailiure != null) { this.AuthenticationFailiure(this, new XmppAuthenticationFailiureEventArgs(ex.ToString())); } this.Close(); } }
/// <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) { this.userId = ((Bind)item).Jid; this.bindResourceEvent.Set(); } else if (item is RosterQuery) { this.onRosterMessage.OnNext(item as RosterQuery); } else if (item is VCardData) { this.onVCardMessage.OnNext(iq); } else if (item is Ping) { if (iq.Type == IQType.Get) { // Send the "pong" response this.Send ( new IQ { ID = iq.ID, To = iq.From, From = this.UserId.ToString(), Type = IQType.Result } ); } } } if (item is ServiceQuery || item is ServiceItemQuery) { this.onServiceDiscoveryMessage.OnNext(iq); } } } }
/// <summary> /// Request subscription to the given user /// </summary> /// <param name="contactId"></param> public void RequestSubscription(XmppJid jid) { Presence presence = new Presence { Type = PresenceType.Subscribe, To = jid }; this.session.Send(presence); }
/// <summary> /// Closes the connection /// </summary> public void Close() { if (this.state != XmppConnectionState.Closed) { if (this.ConnectionClosing != null) { this.ConnectionClosing(this, new EventArgs()); } try { this.state = XmppConnectionState.Closing; if (this.transport != null) { this.transport.Close(); } } catch { } finally { if (this.initializedStreamEvent != null) { this.initializedStreamEvent.Set(); this.initializedStreamEvent = null; } if (this.streamFeaturesEvent != null) { this.streamFeaturesEvent.Set(); this.streamFeaturesEvent = null; } if (this.bindResourceEvent != null) { this.bindResourceEvent.Set(); this.bindResourceEvent = null; } if (this.initializedStreamEvent != null) { this.initializedStreamEvent.Close(); this.initializedStreamEvent = null; } if (this.streamFeaturesEvent != null) { this.streamFeaturesEvent.Close(); this.streamFeaturesEvent = null; } if (this.bindResourceEvent != null) { this.bindResourceEvent.Close(); this.bindResourceEvent = null; } if (this.transportMessageSubscription != null) { this.transportMessageSubscription.Dispose(); this.transportMessageSubscription = null; } if (this.transportStreamInitializedSubscription != null) { this.transportStreamInitializedSubscription.Dispose(); this.transportStreamInitializedSubscription = null; } if (this.transportStreamClosedSubscription != null) { this.transportStreamClosedSubscription.Dispose(); this.transportStreamClosedSubscription = null; } if (this.transport != null) { this.transport = null; } this.streamFeatures = this.streamFeatures & (~this.streamFeatures); this.state = XmppConnectionState.Closed; this.connectionString = null; this.userId = null; } if (this.ConnectionClosed != null) { this.ConnectionClosed(this, new EventArgs()); } } }
internal void AddDefaultResource() { Presence defaultPresence = new Presence(); XmppContactResource contactResource = new XmppContactResource(this.session, this, this.ContactId); XmppJid resourceJid = new XmppJid(this.contactId.UserName, this.ContactId.DomainName, Guid.NewGuid().ToString()); // Add a default resource defaultPresence.TypeSpecified = true; defaultPresence.From = resourceJid; defaultPresence.Type = PresenceType.Unavailable; defaultPresence.Items.Add(XmppContactResource.DefaultPresencePriorityValue); contactResource.Update(defaultPresence); this.resources.Add(contactResource); }
/// <summary> /// Subscribes to presence updates of the current user /// </summary> /// <param name="jid"></param> public void Unsuscribed(XmppJid jid) { Presence presence = new Presence { Type = PresenceType.Unsubscribed, To = jid }; this.session.Send(presence); }
/// <summary> /// Sets the initial presence against the given user. /// </summary> /// <param name="target">JID of the target user.</param> public void SetInitialPresence(XmppJid target) { Presence presence = new Presence(); if (target != null && target.ToString().Length > 0) { presence.To = target.ToString(); } this.session.Send(presence); }
/// <summary> /// Opens a new chat view for the given contact jid /// </summary> /// <param name="jid">The contact jid</param> public void OpenChatView(XmppJid jid) { this.OpenChatView(jid.BareIdentifier); }
internal void UpdatePresence(XmppJid jid, Presence presence) { lock (this.syncObject) { XmppContactResource resource = this.resources .Where(contactResource => contactResource.ResourceId.Equals(jid)) .SingleOrDefault(); if (resource == null) { resource = new XmppContactResource(this.session, this, jid); this.resources.Add(resource); } resource.Update(presence); // Remove the resource information if the contact has gone offline if (!resource.IsDefaultResource && resource.Presence.PresenceStatus == XmppPresenceState.Offline) { this.resources.Remove(resource); } this.NotifyPropertyChanged(() => Presence); this.NotifyPropertyChanged(() => Resource); } }