SweetaAhuja

Untitled

Apr 18th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.52 KB | None | 0 0
  1. //
  2. // AppDelegate.m
  3. // SoketDemo
  4. //
  5. // Created by Sweeta Ahuja on 15/04/16.
  6. // Copyright (c) 2016 Sweeta Ahuja. All rights reserved.
  7. //
  8.  
  9. #import "AppDelegate.h"
  10. #import "NSString+Hashes.h"
  11.  
  12. #import "GCDAsyncSocket.h"
  13. #import "XMPP.h"
  14. #import "XMPPReconnect.h"
  15. #import "XMPPCapabilitiesCoreDataStorage.h"
  16. #import "XMPPRosterCoreDataStorage.h"
  17. #import "XMPPRoomMemoryStorage.h"
  18.  
  19. #import "XMPPvCardAvatarModule.h"
  20. #import "XMPPvCardCoreDataStorage.h"
  21. #import "XMPPMessageDeliveryReceipts.h"
  22. #import "XMPPMessage+XEP_0184.h"
  23. #import "XMPPMessage+XEP_0085.h"
  24. #import "NSXMLElement+XEP_0203.h"
  25.  
  26. #import "DDLog.h"
  27. #import "DDTTYLogger.h"
  28.  
  29. #import "SharedManager.h"
  30.  
  31. #import <CFNetwork/CFNetwork.h>
  32.  
  33. @import GoogleMaps;
  34.  
  35. @interface AppDelegate ()
  36.  
  37. - (void)setupStream;
  38. - (void)teardownStream;
  39.  
  40. - (void)goOnline;
  41. - (void)goOffline;
  42.  
  43. @end
  44.  
  45. @implementation AppDelegate
  46. {
  47. id _services;
  48. }
  49.  
  50. @synthesize xmppStream;
  51. @synthesize xmppReconnect;
  52. @synthesize xmppRoster;
  53. @synthesize xmppRosterStorage;
  54. @synthesize xmppvCardTempModule;
  55. @synthesize xmppvCardAvatarModule;
  56. @synthesize xmppCapabilities;
  57. @synthesize xmppCapabilitiesStorage;
  58. @synthesize xmppLastActivity;
  59. @synthesize xmppMUC;
  60.  
  61. @synthesize hostReach;
  62.  
  63.  
  64. #pragma mark Private
  65. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66.  
  67. -(void)sendGroupMessage:(Message *)objMessage
  68. to:(NSString *)receiverJId
  69. {
  70.  
  71. XMPPJID* recipient = [XMPPJID jidWithString:receiverJId];
  72.  
  73. XMPPMessage* message = [[XMPPMessage alloc] initWithType:@"groupchat" to:recipient];
  74. [message addAttributeWithName:@"type" stringValue:@"groupchat"];
  75.  
  76. NSMutableDictionary *msgDic = [[NSMutableDictionary alloc] init];
  77.  
  78. NSMutableDictionary *senderProperty = [[NSMutableDictionary alloc] init];
  79. [senderProperty setObject:[NSString stringWithFormat:@"%@ %@",ShareObj.loginUser.firstName,ShareObj.loginUser.lastName] forKey:kuserName];
  80.  
  81. [senderProperty setObject:[NSString stringWithFormat:@"%ld",(long)ShareObj.loginUser.userId] forKey:kUserId];
  82. [senderProperty setObject:ShareObj.loginUser.mobileNumber forKey:kMobileNumber];
  83.  
  84. [msgDic setObject:senderProperty forKey:@"sender"];
  85. [msgDic setObject:[objMessage getDic] forKey:@"message"];
  86.  
  87. NSError *err = nil;
  88.  
  89. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:msgDic options:NSJSONWritingPrettyPrinted error:&err];
  90.  
  91. NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  92.  
  93. [message addBody:str];
  94.  
  95. // [message addAttributeWithName:@"id" stringValue:objMessage.messageId];
  96. //
  97. // // User this code to send markable seen message...
  98. // [message addMarkableChatMarker];
  99.  
  100. [[APP_DELEGATE xmppStream] sendElement:message];
  101.  
  102. // XMPPJID* recipient = [XMPPJID jidWithString:receiverJId];
  103. //
  104. // XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];
  105. // XMPPRoom *xmppRoom = [[XMPPRoom alloc]
  106. // initWithRoomStorage:roomMemoryStorage
  107. // jid:recipient
  108. // dispatchQueue:dispatch_get_main_queue()];
  109. //
  110. // NSMutableDictionary *msgDic = [[NSMutableDictionary alloc] init];
  111. //
  112. // NSMutableDictionary *senderProperty = [[NSMutableDictionary alloc] init];
  113. // [senderProperty setObject:[NSString stringWithFormat:@"%@ %@",ShareObj.loginUser.firstName,ShareObj.loginUser.lastName] forKey:kuserName];
  114. //
  115. // [senderProperty setObject:[NSString stringWithFormat:@"%ld",(long)ShareObj.loginUser.userId] forKey:kUserId];
  116. // [senderProperty setObject:ShareObj.loginUser.mobileNumber forKey:kMobileNumber];
  117. //
  118. // [msgDic setObject:senderProperty forKey:@"sender"];
  119. // [msgDic setObject:[objMessage getDic] forKey:@"message"];
  120. //
  121. // NSError *err = nil;
  122. //
  123. // NSData *jsonData = [NSJSONSerialization dataWithJSONObject:msgDic options:NSJSONWritingPrettyPrinted error:&err];
  124. //
  125. // NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  126. //
  127. // [xmppRoom sendMessageWithBody:str];
  128. //
  129. // [xmppRoom sendMessageWithBody:str];
  130. //
  131. // NSXMLElement *x = [NSXMLElement elementWithName:@"groupchat" xmlns:XMPPMUCNamespace];
  132. //
  133. // XMPPMessage* message = [[XMPPMessage alloc] initWithType:@"groupchat" to:recipient];
  134. // [message addChild:x];
  135. // [message addBody:str];
  136. //
  137. // [[APP_DELEGATE xmppStream] sendElement:message];
  138. }
  139.  
  140. -(void)sendMessage:(Message *)objMessage
  141. to:(NSString *)receiverJId
  142. isGroup:(BOOL)isGroupMessage
  143. {
  144. XMPPJID* recipient = [XMPPJID jidWithString:receiverJId];
  145.  
  146. XMPPMessage* message = [[XMPPMessage alloc] initWithType:@"chat" to:recipient];
  147. [message addAttributeWithName:@"type" stringValue:@"chat"];
  148.  
  149. NSMutableDictionary *msgDic = [[NSMutableDictionary alloc] init];
  150.  
  151. NSMutableDictionary *senderProperty = [[NSMutableDictionary alloc] init];
  152. [senderProperty setObject:[NSString stringWithFormat:@"%@ %@",ShareObj.loginUser.firstName,ShareObj.loginUser.lastName] forKey:kuserName];
  153.  
  154. [senderProperty setObject:[NSString stringWithFormat:@"%ld",(long)ShareObj.loginUser.userId] forKey:kUserId];
  155. [senderProperty setObject:ShareObj.loginUser.mobileNumber forKey:kMobileNumber];
  156.  
  157. [msgDic setObject:senderProperty forKey:@"sender"];
  158. [msgDic setObject:[objMessage getDic] forKey:@"message"];
  159.  
  160. NSError *err = nil;
  161.  
  162. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:msgDic options:NSJSONWritingPrettyPrinted error:&err];
  163.  
  164. NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  165.  
  166. [message addBody:str];
  167.  
  168. [[APP_DELEGATE xmppStream] sendElement:message];
  169. }
  170.  
  171. #pragma mark Groups...
  172.  
  173. - (void)createGroup:(Groups *)objGroup
  174. {
  175. XMPPRoomMemoryStorage *roomStorage = [[XMPPRoomMemoryStorage alloc] init];
  176.  
  177. /*
  178. * Remember to add 'conference' in your JID like this:
  179. */
  180.  
  181. XMPPJID *roomJID = [XMPPJID jidWithString:objGroup.jId];
  182. XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomStorage
  183. jid:roomJID
  184. dispatchQueue:dispatch_get_main_queue()];
  185. [xmppRoom changeNickname:objGroup.groupName];
  186. [xmppRoom activate:self.xmppStream];
  187. [xmppRoom addDelegate:self
  188. delegateQueue:dispatch_get_main_queue()];
  189.  
  190.  
  191. [xmppRoom joinRoomUsingNickname:objGroup.groupName
  192. history:nil
  193. password:nil];
  194. }
  195.  
  196. - (void)xmppRoomDidCreate:(XMPPRoom *)sender
  197. {
  198. NSLog(@"Group Created");
  199. }
  200.  
  201. - (void)xmppRoomDidJoin:(XMPPRoom *)sender
  202. {
  203. NSLog(@"xmppRoomDidJoin");
  204. [sender fetchConfigurationForm];
  205.  
  206. NSString *strRoomJId = [[sender myRoomJID] user];
  207.  
  208. NSMutableDictionary *dic = [ShareObj.sharedDB getRecordFromQuery:[NSString stringWithFormat:
  209. @"Select * from Groups Where jId Like '%@'",kOFGroup(strRoomJId)]];
  210.  
  211. Groups *objGroup = [[Groups alloc] initWithObject:dic];
  212.  
  213. if(objGroup.groupMembers.length>0)
  214. {
  215. NSArray *arrUsers = [objGroup.groupMembers componentsSeparatedByString:@","];
  216.  
  217. for(NSString *sreMobileNumber in arrUsers)
  218. {
  219. if(![sreMobileNumber isEqualToString:ShareObj.loginUser.mobileNumber])
  220. {
  221. XMPPJID *jId = [XMPPJID jidWithString:kOFUser([sreMobileNumber sha1])];
  222.  
  223. [sender inviteUser:jId
  224. inGroup:objGroup
  225. withMessage:[NSString stringWithFormat:@"You are added in %@ group.",objGroup.groupName]];
  226. }
  227.  
  228. if(![sender isJoined] )
  229. {
  230.  
  231. }
  232. }
  233. }
  234. }
  235.  
  236. - (void)xmppRoom:(XMPPRoom *)sender didFetchModeratorsList:(NSArray *)items
  237. {
  238. //DDLogInfo(@"%@: %@ --- %@", THIS_FILE, THIS_METHOD, sender.roomJID.bare);
  239.  
  240. NSLog(@"%@: %@ --- %@", THIS_FILE, THIS_METHOD, sender.roomJID.bare);
  241.  
  242. // if (check the flag for room and invite) // This has to be done only when we intended
  243. // {
  244. // NSArray* users = list of users we need to invite.
  245. //
  246. // if (users.count > 0)
  247. // {
  248. // for (int i=0; i< users.count; i++)
  249. // {
  250. // NSString *jid = [NSString stringWithFormat:@"%@@xyz.biz", [users objectAtIndex:i]];
  251. // XMPPJID *xmppJID=[XMPPJID jidWithString:jid];
  252. // [sender inviteUser:xmppJID withMessage:@"Join Group."];
  253. // }
  254. // [sender sendMessageWithBody:@"Hi All"];
  255. // }
  256. // }
  257. }
  258.  
  259. - (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm
  260. {
  261. NSXMLElement *newConfig = [configForm copy];
  262. NSArray *fields = [newConfig elementsForName:@"field"];
  263.  
  264. for (NSXMLElement *field in fields)
  265. {
  266. NSString *var = [field attributeStringValueForName:@"var"];
  267.  
  268. // Make Room Persistent
  269. if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
  270. [field removeChildAtIndex:0];
  271. [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
  272. }
  273. }
  274.  
  275. [sender configureRoomUsingOptions:newConfig];
  276. }
  277.  
  278. - (void)xmppMUC:(XMPPMUC *)sender roomJID:(XMPPJID *) roomJID didReceiveInvitation:(XMPPMessage *)message;
  279. {
  280. NSLog(@"%@",roomJID);
  281.  
  282. NSString *messageBody = [message body];
  283.  
  284. NSError *jsonError;
  285. NSData *objectData = [messageBody dataUsingEncoding:NSUTF8StringEncoding];
  286. NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
  287. options:NSJSONReadingMutableContainers
  288. error:&jsonError];
  289.  
  290. Groups *objNewGroup = [[Groups alloc] initWithObject:json];
  291. [objNewGroup updateInDB];
  292.  
  293. NSString *messageFromUser = [[message from] user];
  294.  
  295. Conversation *objConversation = [Conversation getConversationWithGroup:objNewGroup];
  296.  
  297. Message *objMessage = [[Message alloc] init];
  298. objMessage.messageId = [XMPPStream generateUUID];
  299. objMessage.msgType = MsgTypeAlert;
  300. objMessage.jId = messageFromUser;
  301. objMessage.msgContent = [NSString stringWithFormat:@"You are added in %@ Group.",objNewGroup.groupName];
  302. objMessage.conversationId = objConversation.conversationId;
  303. [objMessage setIsMsgRead:0];
  304. [objMessage setMsgStatus:Delivered];
  305. [objMessage updateInDB];
  306.  
  307. [objConversation updateKey:kMsgId value:@(objMessage.msgId)];
  308.  
  309. [xmppRoster addUser:roomJID withNickname:objNewGroup.groupName];
  310.  
  311. [[NSNotificationCenter defaultCenter] postNotificationName:kReceivedMessageInConversationNotification
  312. object:objConversation];
  313.  
  314. [self joinMultiUserChatRoom:roomJID];
  315. }
  316.  
  317. - (void) joinMultiUserChatRoom:(XMPPJID *) roomJID
  318. {
  319. XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];
  320. XMPPRoom *xmppRoom = [[XMPPRoom alloc]
  321. initWithRoomStorage:roomMemoryStorage
  322. jid:roomJID
  323. dispatchQueue:dispatch_get_main_queue()];
  324.  
  325. [xmppRoom activate:[self xmppStream]];
  326. [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
  327. [xmppRoom joinRoomUsingNickname:[NSString stringWithFormat:@"%@ %@",ShareObj.loginUser.firstName,ShareObj.loginUser.firstName]
  328. history:nil];
  329. }
  330.  
  331.  
  332. - (void)handlePresence:(XMPPPresence *)presence room:(XMPPRoom *)room;
  333. {
  334. NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
  335. }
  336.  
  337. - (void)handleIncomingMessage:(XMPPMessage *)message room:(XMPPRoom *)room
  338. {
  339. NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
  340. }
  341.  
  342. - (void)handleOutgoingMessage:(XMPPMessage *)message room:(XMPPRoom *)room
  343. {
  344. NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
  345. }
  346.  
  347. - (void)handleDidLeaveRoom:(XMPPRoom *)room
  348. {
  349. NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
  350. }
Add Comment
Please, Sign In to add comment