Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bbef60
commit 349232e
Showing
4 changed files
with
253 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#import "XMPPModule.h" | ||
|
||
#define _XMPP_MESSAGE_DELIVERY_RECEIPTS_H | ||
|
||
/** | ||
* XMPPMessageDeliveryReceipts can be configured to automatically send delivery receipts and requests in accordance to XEP-0184 | ||
**/ | ||
|
||
@interface XMPPMessageDeliveryReceipts : XMPPModule | ||
|
||
/** | ||
* Automatically add message delivery requests to outgoing messages, in all situations that are permitted in XEP-0184 | ||
* | ||
* - Message MUST NOT be of type 'error' or 'groupchat' | ||
* - Message MUST have an id | ||
* - Message MUST NOT have a delivery receipt or request | ||
* - To must either be a bare JID or a full JID that advertises the urn:xmpp:receipts capability | ||
* | ||
* Default NO | ||
**/ | ||
|
||
@property (assign) BOOL autoSendMessageDeliveryRequests; | ||
|
||
/** | ||
* Automatically send message delivery receipts when a message with a delivery request is recieved | ||
* | ||
* Default NO | ||
**/ | ||
|
||
@property (assign) BOOL autoSendMessageDeliveryReceipts; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
#import "XMPPMessageDeliveryReceipts.h" | ||
#import "XMPPMessage+XEP_0184.h" | ||
#import "XMPPFramework.h" | ||
|
||
#if ! __has_feature(objc_arc) | ||
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). | ||
#endif | ||
|
||
#define XMLNS_URN_XMPP_RECEIPTS @"urn:xmpp:receipts" | ||
|
||
@implementation XMPPMessageDeliveryReceipts | ||
|
||
@synthesize autoSendMessageDeliveryRequests; | ||
@synthesize autoSendMessageDeliveryReceipts; | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
#pragma mark Init/Dealloc | ||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
- (id)initWithDispatchQueue:(dispatch_queue_t)queue | ||
{ | ||
if((self = [super initWithDispatchQueue:queue])) | ||
{ | ||
autoSendMessageDeliveryRequests = NO; | ||
autoSendMessageDeliveryReceipts = NO; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
|
||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
#pragma mark XMPPModule | ||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
- (BOOL)activate:(XMPPStream *)aXmppStream | ||
{ | ||
if ([super activate:aXmppStream]) | ||
{ | ||
#ifdef _XMPP_CAPABILITIES_H | ||
[xmppStream autoAddDelegate:self delegateQueue:moduleQueue toModulesOfClass:[XMPPCapabilities class]]; | ||
#endif | ||
return YES; | ||
} | ||
|
||
return NO; | ||
} | ||
|
||
- (void)deactivate | ||
{ | ||
#ifdef _XMPP_CAPABILITIES_H | ||
[xmppStream removeAutoDelegate:self delegateQueue:moduleQueue fromModulesOfClass:[XMPPCapabilities class]]; | ||
#endif | ||
|
||
[super deactivate]; | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
#pragma mark Properties | ||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
- (BOOL)autoSendMessageDeliveryRequests | ||
{ | ||
__block BOOL result = NO; | ||
|
||
dispatch_block_t block = ^{ | ||
result = autoSendMessageDeliveryRequests; | ||
}; | ||
|
||
if (dispatch_get_specific(moduleQueueTag)) | ||
block(); | ||
else | ||
dispatch_sync(moduleQueue, block); | ||
|
||
return result; | ||
} | ||
|
||
- (void)setAutoSendMessageDeliveryRequests:(BOOL)flag | ||
{ | ||
dispatch_block_t block = ^{ | ||
autoSendMessageDeliveryRequests = flag; | ||
}; | ||
|
||
if (dispatch_get_specific(moduleQueueTag)) | ||
block(); | ||
else | ||
dispatch_async(moduleQueue, block); | ||
} | ||
|
||
- (BOOL)autoSendMessageDeliveryReceipts | ||
{ | ||
__block BOOL result = NO; | ||
|
||
dispatch_block_t block = ^{ | ||
result = autoSendMessageDeliveryReceipts; | ||
}; | ||
|
||
if (dispatch_get_specific(moduleQueueTag)) | ||
block(); | ||
else | ||
dispatch_sync(moduleQueue, block); | ||
|
||
return result; | ||
} | ||
|
||
- (void)setAutoSendMessageDeliveryReceipts:(BOOL)flag | ||
{ | ||
dispatch_block_t block = ^{ | ||
autoSendMessageDeliveryReceipts = flag; | ||
}; | ||
|
||
if (dispatch_get_specific(moduleQueueTag)) | ||
block(); | ||
else | ||
dispatch_async(moduleQueue, block); | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
#pragma mark XMPPStream Delegate | ||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message | ||
{ | ||
if([message hasReceiptRequest]) | ||
{ | ||
if(self.autoSendMessageDeliveryReceipts) | ||
{ | ||
XMPPMessage *generatedReceiptResponse = [message generateReceiptResponse]; | ||
[sender sendElement:generatedReceiptResponse]; | ||
} | ||
} | ||
} | ||
|
||
- (XMPPMessage *)xmppStream:(XMPPStream *)sender willSendMessage:(XMPPMessage *)message | ||
{ | ||
if(self.autoSendMessageDeliveryRequests | ||
&& [message to] | ||
&& ![message isErrorMessage] && ![[[message attributeForName:@"type"] stringValue] isEqualToString:@"groupchat"] | ||
&& [[message elementID] length] | ||
&& ![message hasReceiptRequest] && ![message hasReceiptResponse]) | ||
{ | ||
|
||
#ifdef _XMPP_CAPABILITIES_H | ||
BOOL addReadReceipt = NO; | ||
|
||
__block XMPPCapabilities *xmppCapabilities = nil; | ||
|
||
[xmppStream enumerateModulesOfClass:[XMPPCapabilities class] withBlock:^(XMPPModule *module, NSUInteger idx, BOOL *stop) { | ||
xmppCapabilities = (XMPPCapabilities *)module; | ||
}]; | ||
|
||
if([[message to] isFull] && [xmppCapabilities.xmppCapabilitiesStorage areCapabilitiesKnownForJID:[message to] xmppStream:sender]) | ||
{ | ||
NSXMLElement *capabilities = [xmppCapabilities.xmppCapabilitiesStorage capabilitiesForJID:[message to] xmppStream:xmppStream]; | ||
|
||
for(NSXMLElement *feature in [capabilities children]) | ||
{ | ||
if([[feature name] isEqualToString:@"feature"] | ||
&& [[feature attributeStringValueForName:@"var"] isEqualToString:XMLNS_URN_XMPP_RECEIPTS]) | ||
{ | ||
addReadReceipt = YES; | ||
break; | ||
} | ||
|
||
} | ||
|
||
} | ||
else | ||
{ | ||
addReadReceipt = YES; | ||
} | ||
#else | ||
BOOL addReadReceipt = YES; | ||
#endif | ||
|
||
if(addReadReceipt) | ||
{ | ||
[message addReceiptRequest]; | ||
} | ||
} | ||
|
||
return message; | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
#pragma mark XMPPCapabilities delegate | ||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
#ifdef _XMPP_CAPABILITIES_H | ||
/** | ||
* If an XMPPCapabilites instance is used we want to advertise our support for XEP-0184. | ||
**/ | ||
- (void)xmppCapabilities:(XMPPCapabilities *)sender collectingMyCapabilities:(NSXMLElement *)query | ||
{ | ||
// This method is invoked on the moduleQueue. | ||
|
||
// <query xmlns="http://jabber.org/protocol/disco#info"> | ||
// ... | ||
// <feature var='urn:xmpp:receipts'/> | ||
// ... | ||
// </query> | ||
|
||
NSXMLElement *messageDeliveryReceiptsFeatureElement = [NSXMLElement elementWithName:@"feature"]; | ||
[messageDeliveryReceiptsFeatureElement addAttributeWithName:@"var" stringValue:XMLNS_URN_XMPP_RECEIPTS]; | ||
|
||
[query addChild:messageDeliveryReceiptsFeatureElement]; | ||
} | ||
#endif | ||
|
||
@end |