SweetaAhuja

Untitled

Apr 18th, 2016
60
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. * e.g. uniqueRoomJID@conference.yourserverdomain
  180. */
  181.  
  182. XMPPJID *roomJID = [XMPPJID jidWithString:objGroup.jId];
  183. XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomStorage
  184. jid:roomJID
  185. dispatchQueue:dispatch_get_main_queue()];
  186. [xmppRoom changeNickname:objGroup.groupName];
  187. [xmppRoom activate:self.xmppStream];
  188. [xmppRoom addDelegate:self
  189. delegateQueue:dispatch_get_main_queue()];
  190.  
  191.  
  192. [xmppRoom joinRoomUsingNickname:objGroup.groupName
  193. history:nil
  194. password:nil];
  195. }
  196.  
  197. - (void)xmppRoomDidCreate:(XMPPRoom *)sender
  198. {
  199. NSLog(@"Group Created");
  200. }
  201.  
  202. - (void)xmppRoomDidJoin:(XMPPRoom *)sender
  203. {
  204. NSLog(@"xmppRoomDidJoin");
  205. [sender fetchConfigurationForm];
  206.  
  207. NSString *strRoomJId = [[sender myRoomJID] user];
  208.  
  209. NSMutableDictionary *dic = [ShareObj.sharedDB getRecordFromQuery:[NSString stringWithFormat:
  210. @"Select * from Groups Where jId Like '%@'",kOFGroup(strRoomJId)]];
  211.  
  212. Groups *objGroup = [[Groups alloc] initWithObject:dic];
  213.  
  214. if(objGroup.groupMembers.length>0)
  215. {
  216. NSArray *arrUsers = [objGroup.groupMembers componentsSeparatedByString:@","];
  217.  
  218. for(NSString *sreMobileNumber in arrUsers)
  219. {
  220. if(![sreMobileNumber isEqualToString:ShareObj.loginUser.mobileNumber])
  221. {
  222. XMPPJID *jId = [XMPPJID jidWithString:kOFUser([sreMobileNumber sha1])];
  223.  
  224. [sender inviteUser:jId
  225. inGroup:objGroup
  226. withMessage:[NSString stringWithFormat:@"You are added in %@ group.",objGroup.groupName]];
  227. }
  228.  
  229. if(![sender isJoined] )
  230. {
  231.  
  232. }
  233. }
  234. }
  235. }
  236.  
  237. - (void)xmppRoom:(XMPPRoom *)sender didFetchModeratorsList:(NSArray *)items
  238. {
  239. //DDLogInfo(@"%@: %@ --- %@", THIS_FILE, THIS_METHOD, sender.roomJID.bare);
  240.  
  241. NSLog(@"%@: %@ --- %@", THIS_FILE, THIS_METHOD, sender.roomJID.bare);
  242.  
  243. // if (check the flag for room and invite) // This has to be done only when we intended
  244. // {
  245. // NSArray* users = list of users we need to invite.
  246. //
  247. // if (users.count > 0)
  248. // {
  249. // for (int i=0; i< users.count; i++)
  250. // {
  251. // NSString *jid = [NSString stringWithFormat:@"%@@xyz.biz", [users objectAtIndex:i]];
  252. // XMPPJID *xmppJID=[XMPPJID jidWithString:jid];
  253. // [sender inviteUser:xmppJID withMessage:@"Join Group."];
  254. // }
  255. // [sender sendMessageWithBody:@"Hi All"];
  256. // }
  257. // }
  258. }
  259.  
  260. - (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm
  261. {
  262. NSXMLElement *newConfig = [configForm copy];
  263. NSArray *fields = [newConfig elementsForName:@"field"];
  264.  
  265. for (NSXMLElement *field in fields)
  266. {
  267. NSString *var = [field attributeStringValueForName:@"var"];
  268.  
  269. // Make Room Persistent
  270. if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
  271. [field removeChildAtIndex:0];
  272. [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
  273. }
  274. }
  275.  
  276. [sender configureRoomUsingOptions:newConfig];
  277. }
  278.  
  279. - (void)xmppMUC:(XMPPMUC *)sender roomJID:(XMPPJID *) roomJID didReceiveInvitation:(XMPPMessage *)message;
  280. {
  281. NSLog(@"%@",roomJID);
  282.  
  283. NSString *messageBody = [message body];
  284.  
  285. NSError *jsonError;
  286. NSData *objectData = [messageBody dataUsingEncoding:NSUTF8StringEncoding];
  287. NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
  288. options:NSJSONReadingMutableContainers
  289. error:&jsonError];
  290.  
  291. Groups *objNewGroup = [[Groups alloc] initWithObject:json];
  292. [objNewGroup updateInDB];
  293.  
  294. NSString *messageFromUser = [[message from] user];
  295.  
  296. Conversation *objConversation = [Conversation getConversationWithGroup:objNewGroup];
  297.  
  298. Message *objMessage = [[Message alloc] init];
  299. objMessage.messageId = [XMPPStream generateUUID];
  300. objMessage.msgType = MsgTypeAlert;
  301. objMessage.jId = messageFromUser;
  302. objMessage.msgContent = [NSString stringWithFormat:@"You are added in %@ Group.",objNewGroup.groupName];
  303. objMessage.conversationId = objConversation.conversationId;
  304. [objMessage setIsMsgRead:0];
  305. [objMessage setMsgStatus:Delivered];
  306. [objMessage updateInDB];
  307.  
  308. [objConversation updateKey:kMsgId value:@(objMessage.msgId)];
  309.  
  310. [xmppRoster addUser:roomJID withNickname:objNewGroup.groupName];
  311.  
  312. [[NSNotificationCenter defaultCenter] postNotificationName:kReceivedMessageInConversationNotification
  313. object:objConversation];
  314.  
  315. [self joinMultiUserChatRoom:roomJID];
  316. }
  317.  
  318. - (void) joinMultiUserChatRoom:(XMPPJID *) roomJID
  319. {
  320. XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];
  321. XMPPRoom *xmppRoom = [[XMPPRoom alloc]
  322. initWithRoomStorage:roomMemoryStorage
  323. jid:roomJID
  324. dispatchQueue:dispatch_get_main_queue()];
  325.  
  326. [xmppRoom activate:[self xmppStream]];
  327. [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
  328. [xmppRoom joinRoomUsingNickname:[NSString stringWithFormat:@"%@ %@",ShareObj.loginUser.firstName,ShareObj.loginUser.firstName]
  329. history:nil];
  330. }
  331.  
  332.  
  333. - (void)handlePresence:(XMPPPresence *)presence room:(XMPPRoom *)room;
  334. {
  335. NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
  336. }
  337.  
  338. - (void)handleIncomingMessage:(XMPPMessage *)message room:(XMPPRoom *)room
  339. {
  340. NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
  341. }
  342.  
  343. - (void)handleOutgoingMessage:(XMPPMessage *)message room:(XMPPRoom *)room
  344. {
  345. NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
  346. }
  347.  
  348. - (void)handleDidLeaveRoom:(XMPPRoom *)room
  349. {
  350. NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
  351. }
Add Comment
Please, Sign In to add comment