XMPPContactManager: Support for handling subscription requests

This commit is contained in:
Florian Zeitz 2013-06-09 20:26:24 +02:00
parent 702a18636d
commit bd0440b3ee
2 changed files with 49 additions and 4 deletions

View file

@ -57,6 +57,15 @@
- (void)contactManager: (XMPPContactManager*)manager - (void)contactManager: (XMPPContactManager*)manager
didRemoveContact: (XMPPContact*)contact; 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 * \brief This callback is called whenever a contact is about to change its
* roster item * roster item
@ -118,6 +127,9 @@
- initWithConnection: (XMPPConnection*)connection - initWithConnection: (XMPPConnection*)connection
roster: (XMPPRoster*)roster; roster: (XMPPRoster*)roster;
- (void)sendSubscribedToJID: (XMPPJID*)subscriber;
- (void)sendUnsubscribedToJID: (XMPPJID*)subscriber;
/** /**
* \brief Adds the specified delegate. * \brief Adds the specified delegate.
* *

View file

@ -58,6 +58,22 @@
[super dealloc]; [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 <XMPPContactManagerDelegate>)delegate - (void)addDelegate: (id <XMPPContactManagerDelegate>)delegate
{ {
[_delegates addDelegate: delegate]; [_delegates addDelegate: delegate];
@ -145,26 +161,43 @@
- (void)connection: (XMPPConnection*)connection - (void)connection: (XMPPConnection*)connection
didReceivePresence: (XMPPPresence*)presence didReceivePresence: (XMPPPresence*)presence
{ {
XMPPContact *contact;
XMPPJID *JID = [presence from]; 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) if (contact == nil)
return; return;
// We only care for available and unavailable here, not subscriptions /* Available presence */
if ([[presence type] isEqual: @"available"]) { if ([type isEqual: @"available"]) {
[contact XMPP_setPresence: presence [contact XMPP_setPresence: presence
resource: [JID resource]]; resource: [JID resource]];
[_delegates broadcastSelector: @selector(contact: [_delegates broadcastSelector: @selector(contact:
didSendPresence:) didSendPresence:)
withObject: contact withObject: contact
withObject: presence]; withObject: presence];
} else if ([[presence type] isEqual: @"unavailable"]) { return;
}
/* Unavailable presence */
if ([type isEqual: @"unavailable"]) {
[contact XMPP_removePresenceForResource: [JID resource]]; [contact XMPP_removePresenceForResource: [JID resource]];
[_delegates broadcastSelector: @selector(contact: [_delegates broadcastSelector: @selector(contact:
didSendPresence:) didSendPresence:)
withObject: contact withObject: contact
withObject: presence]; withObject: presence];
return;
} }
} }