From bd0440b3eec249ea01c4c2b33c8988719458c8bc Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Sun, 9 Jun 2013 20:26:24 +0200 Subject: [PATCH] XMPPContactManager: Support for handling subscription requests --- src/XMPPContactManager.h | 12 ++++++++++++ src/XMPPContactManager.m | 41 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/XMPPContactManager.h b/src/XMPPContactManager.h index d18aed7..f54a5eb 100644 --- a/src/XMPPContactManager.h +++ b/src/XMPPContactManager.h @@ -57,6 +57,15 @@ - (void)contactManager: (XMPPContactManager*)manager didRemoveContact: (XMPPContact*)contact; +/** + * \brief This callback is called when a subscription request is received + * + * \param manager The contact manager that received the request + * \param presence The type=subscribe presence + */ +- (void)contactManager: (XMPPContactManager*)manager + didReceiveSubscriptionRequest: (XMPPPresence*)presence; + /** * \brief This callback is called whenever a contact is about to change its * roster item @@ -118,6 +127,9 @@ - initWithConnection: (XMPPConnection*)connection roster: (XMPPRoster*)roster; +- (void)sendSubscribedToJID: (XMPPJID*)subscriber; +- (void)sendUnsubscribedToJID: (XMPPJID*)subscriber; + /** * \brief Adds the specified delegate. * diff --git a/src/XMPPContactManager.m b/src/XMPPContactManager.m index b87b084..86b8f03 100644 --- a/src/XMPPContactManager.m +++ b/src/XMPPContactManager.m @@ -58,6 +58,22 @@ [super dealloc]; } + +- (void)sendSubscribedToJID: (XMPPJID*)subscriber +{ + XMPPPresence *presence = [XMPPPresence presenceWithType: @"subscribed"]; + [presence setTo: subscriber]; + [_connection sendStanza: presence]; +} + +- (void)sendUnsubscribedToJID: (XMPPJID*)subscriber +{ + XMPPPresence *presence = + [XMPPPresence presenceWithType: @"unsubscribed"]; + [presence setTo: subscriber]; + [_connection sendStanza: presence]; +} + - (void)addDelegate: (id )delegate { [_delegates addDelegate: delegate]; @@ -145,26 +161,43 @@ - (void)connection: (XMPPConnection*)connection didReceivePresence: (XMPPPresence*)presence { + XMPPContact *contact; XMPPJID *JID = [presence from]; - XMPPContact *contact = [_contacts objectForKey: [JID bareJID]]; + OFString *type = [presence type]; + /* Subscription request */ + if ([type isEqual: @"subscribe"]) { + of_log(@"ObjXMPP: received subscription request"); + [_delegates broadcastSelector: @selector(contactManager: + didReceiveSubscriptionRequest:) + withObject: self + withObject: presence]; + return; + } + + contact = [_contacts objectForKey: [JID bareJID]]; if (contact == nil) return; - // We only care for available and unavailable here, not subscriptions - if ([[presence type] isEqual: @"available"]) { + /* Available presence */ + if ([type isEqual: @"available"]) { [contact XMPP_setPresence: presence resource: [JID resource]]; [_delegates broadcastSelector: @selector(contact: didSendPresence:) withObject: contact withObject: presence]; - } else if ([[presence type] isEqual: @"unavailable"]) { + return; + } + + /* Unavailable presence */ + if ([type isEqual: @"unavailable"]) { [contact XMPP_removePresenceForResource: [JID resource]]; [_delegates broadcastSelector: @selector(contact: didSendPresence:) withObject: contact withObject: presence]; + return; } }