|
| 1 | +#import "XMPPAttentionModule.h" |
| 2 | + |
| 3 | +#if ! __has_feature(objc_arc) |
| 4 | +#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). |
| 5 | +#endif |
| 6 | + |
| 7 | +@implementation XMPPAttentionModule |
| 8 | + |
| 9 | +- (id)init |
| 10 | +{ |
| 11 | + return [self initWithDispatchQueue:NULL]; |
| 12 | +} |
| 13 | + |
| 14 | +- (id)initWithDispatchQueue:(dispatch_queue_t)queue |
| 15 | +{ |
| 16 | + if ((self = [super initWithDispatchQueue:queue])) |
| 17 | + { |
| 18 | + respondsToQueries = YES; |
| 19 | + } |
| 20 | + return self; |
| 21 | +} |
| 22 | + |
| 23 | +- (BOOL)activate:(XMPPStream *)aXmppStream |
| 24 | +{ |
| 25 | + if ([super activate:aXmppStream]) |
| 26 | + { |
| 27 | +#ifdef _XMPP_CAPABILITIES_H |
| 28 | + [xmppStream autoAddDelegate:self delegateQueue:moduleQueue toModulesOfClass:[XMPPCapabilities class]]; |
| 29 | +#endif |
| 30 | + return YES; |
| 31 | + } |
| 32 | + |
| 33 | + return NO; |
| 34 | +} |
| 35 | + |
| 36 | +- (void)deactivate |
| 37 | +{ |
| 38 | +#ifdef _XMPP_CAPABILITIES_H |
| 39 | + [xmppStream removeAutoDelegate:self delegateQueue:moduleQueue fromModulesOfClass:[XMPPCapabilities class]]; |
| 40 | +#endif |
| 41 | + |
| 42 | + [super deactivate]; |
| 43 | +} |
| 44 | + |
| 45 | +- (BOOL)respondsToQueries |
| 46 | +{ |
| 47 | + if (dispatch_get_current_queue() == moduleQueue) |
| 48 | + { |
| 49 | + return respondsToQueries; |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + __block BOOL result; |
| 54 | + |
| 55 | + dispatch_sync(moduleQueue, ^{ |
| 56 | + result = respondsToQueries; |
| 57 | + }); |
| 58 | + return result; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +- (void)setRespondsToQueries:(BOOL)flag |
| 63 | +{ |
| 64 | + dispatch_block_t block = ^{ |
| 65 | + |
| 66 | + if (respondsToQueries != flag) |
| 67 | + { |
| 68 | + respondsToQueries = flag; |
| 69 | + |
| 70 | +#ifdef _XMPP_CAPABILITIES_H |
| 71 | + @autoreleasepool { |
| 72 | + // Capabilities may have changed, need to notify others. |
| 73 | + [xmppStream resendMyPresence]; |
| 74 | + } |
| 75 | +#endif |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + if (dispatch_get_current_queue() == moduleQueue) |
| 80 | + block(); |
| 81 | + else |
| 82 | + dispatch_async(moduleQueue, block); |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +- (BOOL)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message |
| 87 | +{ |
| 88 | + // This method is invoked on the moduleQueue. |
| 89 | + |
| 90 | + // Format of an attention message. Body is optional and not used by clients like Pidgin |
| 91 | + // <message from='[email protected]/lab' to='[email protected]/home' type='headline'> |
| 92 | + // <attention xmlns='urn:xmpp:attention:0'/> |
| 93 | + // <body>Why don't you answer, Herbie?</body> |
| 94 | + // </message> |
| 95 | + |
| 96 | + if ([message isAttentionMessage]) |
| 97 | + { |
| 98 | + [multicastDelegate xmppAttention:self didReceiveAttentionHeadlineMessage:message]; |
| 99 | + } |
| 100 | + return YES; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +#ifdef _XMPP_CAPABILITIES_H |
| 105 | +/** |
| 106 | + * If an XMPPCapabilites instance is used we want to advertise our support for attention requests. |
| 107 | + **/ |
| 108 | +- (void)xmppCapabilities:(XMPPCapabilities *)sender collectingMyCapabilities:(NSXMLElement *)query |
| 109 | +{ |
| 110 | + // This method is invoked on the moduleQueue. |
| 111 | + |
| 112 | + if (respondsToQueries) |
| 113 | + { |
| 114 | + // <query xmlns="http://jabber.org/protocol/disco#info"> |
| 115 | + // ... |
| 116 | + // <feature var="urn:xmpp:attention:0"/> |
| 117 | + // ... |
| 118 | + // </query> |
| 119 | + |
| 120 | + NSXMLElement *feature = [NSXMLElement elementWithName:@"feature"]; |
| 121 | + [feature addAttributeWithName:@"var" stringValue:XMLNS_ATTENTION]; |
| 122 | + |
| 123 | + [query addChild:feature]; |
| 124 | + } |
| 125 | +} |
| 126 | +#endif |
| 127 | + |
| 128 | +@end |
0 commit comments