PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lilypad/Sources/LPGroupChat.m

https://github.com/sapo/sapo-messenger-for-mac
Objective C | 689 lines | 535 code | 125 blank | 29 comment | 71 complexity | 92378cc7cecc83b3b7864332432d34f0 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause
  1. //
  2. // LPGroupChat.m
  3. // Lilypad
  4. //
  5. // Copyright (C) 2006-2008 PT.COM, All rights reserved.
  6. // Author: Joao Pavao <jpavao@co.sapo.pt>
  7. //
  8. // For more information on licensing, read the README file.
  9. // Para mais informações sobre o licenciamento, leia o ficheiro README.
  10. //
  11. #import "LPGroupChat.h"
  12. #import "LPAccount.h"
  13. #import "LPGroupChatContact.h"
  14. #warning Estes devem dar para apagar depois de tratar dos outros warnings
  15. #import "LPAccountsController.h"
  16. #import "LPAccount.h"
  17. #import "LPChatsManager.h"
  18. #define NSStringWithFormatIfNotEmpty(formatStr, argStr) \
  19. ([argStr length] > 0 ? [NSString stringWithFormat:formatStr, argStr] : @"")
  20. @interface LPGroupChat () // Private Methods
  21. - (void)p_setActive:(BOOL)flag;
  22. - (void)p_setNickname:(NSString *)nickname;
  23. - (void)p_setLastSetNickname:(NSString *)nickname;
  24. - (void)p_setLastUsedPassword:(NSString *)password;
  25. - (void)p_addParticipant:(LPGroupChatContact *)contact;
  26. - (void)p_removeParticipant:(LPGroupChatContact *)contact;
  27. - (LPGroupChatContact *)p_participantWithNickname:(NSString *)nickname;
  28. - (void)p_updateParticipantNicknameFrom:(NSString *)oldNickname to:(NSString *)newNickname;
  29. - (void)p_doEmitUserSystemMessages;
  30. @end
  31. @implementation LPGroupChat
  32. + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key
  33. {
  34. if ([key isEqualToString:@"nickname"] || [key isEqualToString:@"topic"]) {
  35. // Avoid triggering change notifications on calls to -[LPGroupChat setNickname:] or -[LPGroupChat setTopic:]
  36. return NO;
  37. } else {
  38. return YES;
  39. }
  40. }
  41. + groupChatForRoomWithJID:(NSString *)roomJID onAccount:(LPAccount *)account groupChatID:(int)ID nickname:(NSString *)nickname password:(NSString *)password
  42. {
  43. return [[[[self class] alloc] initForRoomWithJID:roomJID onAccount:account groupChatID:ID nickname:nickname password:password] autorelease];
  44. }
  45. - initForRoomWithJID:(NSString *)roomJID onAccount:(LPAccount *)account groupChatID:(int)ID nickname:(NSString *)nickname password:(NSString *)password
  46. {
  47. if (self = [super init]) {
  48. m_ID = ID;
  49. m_account = [account retain];
  50. m_roomJID = [roomJID copy];
  51. m_nickname = [nickname copy];
  52. m_lastSetNickname = [nickname copy];
  53. m_lastUsedPassword = [password copy];
  54. m_participants = [[NSMutableSet alloc] init];
  55. m_participantsByNickname = [[NSMutableDictionary alloc] init];
  56. [m_account addObserver:self
  57. forKeyPath:@"online"
  58. options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
  59. context:NULL];
  60. }
  61. return self;
  62. }
  63. - (void)dealloc
  64. {
  65. [m_account removeObserver:self forKeyPath:@"online"];
  66. [m_account release];
  67. [m_roomJID release];
  68. [m_nickname release];
  69. [m_lastSetNickname release];
  70. [m_lastUsedPassword release];
  71. [m_topic release];
  72. [m_participants release];
  73. [m_participantsByNickname release];
  74. [m_pendingInvites release];
  75. [super dealloc];
  76. }
  77. - (id)delegate
  78. {
  79. return m_delegate;
  80. }
  81. - (void)setDelegate:(id)delegate
  82. {
  83. m_delegate = delegate;
  84. }
  85. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  86. {
  87. if ([keyPath isEqualToString:@"online"]) {
  88. BOOL wasOnline = [[change objectForKey:NSKeyValueChangeOldKey] boolValue];
  89. BOOL isOnline = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
  90. if (wasOnline && !isOnline) {
  91. // Account went down
  92. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  93. NSString *sysMsg = [NSString stringWithFormat:
  94. NSLocalizedString(@"The account \"%@\", which is used by this group-chat, has been"
  95. @" disconnected.",
  96. @"Chat room system message"),
  97. [object description]];
  98. [m_delegate groupChat:self didReceiveSystemMessage:sysMsg];
  99. }
  100. [self handleDidLeaveGroupChat];
  101. }
  102. else if (!wasOnline && isOnline) {
  103. // Account went up
  104. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  105. NSString *sysMsg = [NSString stringWithFormat:
  106. NSLocalizedString(@"The account \"%@\", which is used by this group-chat, has been"
  107. @" reconnected and is now back online.",
  108. @"Chat room system message"),
  109. [object description]];
  110. [m_delegate groupChat:self didReceiveSystemMessage:sysMsg];
  111. }
  112. [self retryJoinWithNickname:[self lastSetNickname] password:[self lastUsedPassword]];
  113. }
  114. }
  115. else {
  116. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  117. }
  118. }
  119. - (void)retryJoinWithNickname:(NSString *)nickname password:(NSString *)password
  120. {
  121. NSAssert( ![self isActive] , @"retryJoinWithPassword: shouldn't be invoked because we have already successfully joined the room!");
  122. if (![[self nickname] isEqualToString:nickname]) {
  123. [self p_setNickname:nickname];
  124. }
  125. if (![[self lastSetNickname] isEqualToString:nickname]) {
  126. [self p_setLastSetNickname:nickname];
  127. }
  128. if (![[self lastUsedPassword] isEqualToString:password]) {
  129. [self p_setLastUsedPassword:password];
  130. }
  131. [LFAppController groupChatRetryJoin:[self ID] nickname:nickname password:password];
  132. }
  133. - (int)ID
  134. {
  135. return m_ID;
  136. }
  137. - (LPAccount *)account
  138. {
  139. return [[m_account retain] autorelease];
  140. }
  141. - (NSString *)roomJID
  142. {
  143. return [[m_roomJID copy] autorelease];
  144. }
  145. - (NSString *)roomName
  146. {
  147. return [m_roomJID JIDUsernameComponent];
  148. }
  149. - (NSString *)nickname
  150. {
  151. return [[m_nickname copy] autorelease];
  152. }
  153. - (void)setNickname:(NSString *)newNick
  154. {
  155. if (![m_lastSetNickname isEqualToString:newNick]) {
  156. [self p_setLastSetNickname:newNick];
  157. }
  158. [LFAppController groupChatSetNicknameOnRoom:[self ID] to:newNick];
  159. }
  160. - (NSString *)lastSetNickname
  161. {
  162. return [[m_lastSetNickname copy] autorelease];
  163. }
  164. - (NSString *)lastUsedPassword
  165. {
  166. return [[m_lastUsedPassword copy] autorelease];
  167. }
  168. - (BOOL)isActive
  169. {
  170. return m_isActive;
  171. }
  172. - (NSString *)topic
  173. {
  174. return [[m_topic copy] autorelease];
  175. }
  176. - (void)setTopic:(NSString *)newTopic
  177. {
  178. [LFAppController groupChatSetTopicOnRoom:[self ID] to:newTopic];
  179. }
  180. - (void)inviteJID:(NSString *)jid withReason:(NSString *)reason
  181. {
  182. if ([self isActive]) {
  183. [LFAppController groupChatInvite:jid
  184. room:[self roomJID]
  185. accountUUID:[[self account] UUID]
  186. reason:reason];
  187. if ([m_delegate respondsToSelector:@selector(groupChat:didInviteJID:withReason:)]) {
  188. [m_delegate groupChat:self didInviteJID:jid withReason:reason];
  189. }
  190. }
  191. else {
  192. if (m_pendingInvites == nil)
  193. m_pendingInvites = [[NSMutableArray alloc] init];
  194. // Store it in the list of pending invitations
  195. [m_pendingInvites addObject:[NSDictionary dictionaryWithObjectsAndKeys:
  196. jid, @"JID", reason, @"Reason", nil]];
  197. }
  198. }
  199. - (LPGroupChatContact *)myGroupChatContact
  200. {
  201. return [[m_myGroupChatContact retain] autorelease];
  202. }
  203. - (NSSet *)participants
  204. {
  205. return [[m_participants retain] autorelease];
  206. }
  207. - (void)reloadRoomConfigurationForm
  208. {
  209. [LFAppController fetchGroupChatConfigurationForm:[self ID]];
  210. }
  211. - (void)submitRoomConfigurationForm:(NSString *)configurationXMLForm
  212. {
  213. [LFAppController submitGroupChatConfigurationForm:[self ID] :configurationXMLForm];
  214. }
  215. - (void)sendPlainTextMessage:(NSString *)message
  216. {
  217. [LFAppController groupChatMessageSend:[self ID] plain:message];
  218. }
  219. - (void)endGroupChat
  220. {
  221. [[LPChatsManager chatsManager] endGroupChat:self];
  222. }
  223. #pragma mark -
  224. - (void)p_setActive:(BOOL)flag
  225. {
  226. if (flag != m_isActive) {
  227. [self willChangeValueForKey:@"active"];
  228. m_isActive = flag;
  229. [self didChangeValueForKey:@"active"];
  230. }
  231. }
  232. - (void)p_setNickname:(NSString *)nickname
  233. {
  234. if (m_nickname != nickname) {
  235. [self willChangeValueForKey:@"nickname"];
  236. [m_nickname release];
  237. m_nickname = [nickname copy];
  238. [self didChangeValueForKey:@"nickname"];
  239. }
  240. }
  241. - (void)p_setLastSetNickname:(NSString *)nickname
  242. {
  243. if (m_lastSetNickname != nickname) {
  244. [self willChangeValueForKey:@"lastSetNickname"];
  245. [m_lastSetNickname release];
  246. m_lastSetNickname = [nickname copy];
  247. [self didChangeValueForKey:@"lastSetNickname"];
  248. }
  249. }
  250. - (void)p_setLastUsedPassword:(NSString *)password
  251. {
  252. if (m_lastUsedPassword != password) {
  253. [self willChangeValueForKey:@"lastUsedPassword"];
  254. [m_lastUsedPassword release];
  255. m_lastUsedPassword = [password copy];
  256. [self didChangeValueForKey:@"lastUsedPassword"];
  257. }
  258. }
  259. - (void)p_addParticipant:(LPGroupChatContact *)contact
  260. {
  261. NSSet *changeSet = [NSSet setWithObject:contact];
  262. [self willChangeValueForKey:@"participants" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changeSet];
  263. [m_participants addObject:contact];
  264. [self didChangeValueForKey:@"participants" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changeSet];
  265. [m_participantsByNickname setObject:contact forKey:[contact nickname]];
  266. }
  267. - (void)p_removeParticipant:(LPGroupChatContact *)contact
  268. {
  269. NSSet *changeSet = [NSSet setWithObject:contact];
  270. [self willChangeValueForKey:@"participants" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changeSet];
  271. [m_participants removeObject:contact];
  272. [self didChangeValueForKey:@"participants" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changeSet];
  273. [m_participantsByNickname removeObjectForKey:[contact nickname]];
  274. if (contact == m_myGroupChatContact) {
  275. [self willChangeValueForKey:@"myGroupChatContact"];
  276. m_myGroupChatContact = nil;
  277. [self didChangeValueForKey:@"myGroupChatContact"];
  278. }
  279. }
  280. - (LPGroupChatContact *)p_participantWithNickname:(NSString *)nickname
  281. {
  282. return [m_participantsByNickname objectForKey:nickname];
  283. }
  284. - (void)p_updateParticipantNicknameFrom:(NSString *)oldNickname to:(NSString *)newNickname
  285. {
  286. LPGroupChatContact *contact = [m_participantsByNickname objectForKey:oldNickname];
  287. [contact retain];
  288. [m_participantsByNickname removeObjectForKey:oldNickname];
  289. [contact handleChangedNickname:newNickname];
  290. [m_participantsByNickname setObject:contact forKey:newNickname];
  291. [contact release];
  292. }
  293. - (void)p_doEmitUserSystemMessages
  294. {
  295. m_emitUserSystemMessages = YES;
  296. }
  297. #pragma mark -
  298. - (void)handleDidJoinGroupChatWithJID:(NSString *)roomJID nickname:(NSString *)nickname
  299. {
  300. [self p_setActive:YES];
  301. if (![[self nickname] isEqualToString:nickname]) {
  302. [self p_setNickname:nickname];
  303. }
  304. [self performSelector:@selector(p_doEmitUserSystemMessages) withObject:nil afterDelay:5.0];
  305. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  306. [m_delegate groupChat:self didReceiveSystemMessage:NSLocalizedString(@"Chat-room was joined successfully.",
  307. @"Chat room system message")];
  308. }
  309. // Are there any pending invitations waiting to be sent?
  310. NSEnumerator *inviteEnum = [m_pendingInvites objectEnumerator];
  311. NSDictionary *inviteDict;
  312. while (inviteDict = [inviteEnum nextObject]) {
  313. [self inviteJID:[inviteDict objectForKey:@"JID"] withReason:[inviteDict objectForKey:@"Reason"]];
  314. }
  315. [m_pendingInvites release]; m_pendingInvites = nil;
  316. }
  317. - (void)handleDidLeaveGroupChat
  318. {
  319. [self p_setActive:NO];
  320. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  321. [m_delegate groupChat:self didReceiveSystemMessage:NSLocalizedString(@"You have left the chat-room.",
  322. @"Chat room system message")];
  323. }
  324. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(p_doEmitUserSystemMessages) object:nil];
  325. }
  326. - (void)handleDidCreateGroupChat
  327. {
  328. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  329. [m_delegate groupChat:self didReceiveSystemMessage:NSLocalizedString(@"Chat-room was created.",
  330. @"Chat room system message")];
  331. }
  332. }
  333. - (void)handleDidDestroyGroupChatWithReason:(NSString *)reason alternateRoomJID:(NSString *)alternateRoomJID
  334. {
  335. #warning This notification could be handled in a more user-friendly way. Simply showing a chat-room JID to the user is lame!
  336. NSString *sysMsg = ( ([alternateRoomJID length] > 0) ?
  337. [NSString stringWithFormat:
  338. NSLocalizedString(@"Chat-room was destroyed. Please join the alternative chat-room at \"%@\".",
  339. @"Chat room system message"), alternateRoomJID] :
  340. NSLocalizedString(@"Chat-room was destroyed.", @"Chat room system message") );
  341. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  342. [m_delegate groupChat:self didReceiveSystemMessage:sysMsg];
  343. }
  344. }
  345. - (void)handleContactDidJoinGroupChatWithNickname:(NSString *)nickname JID:(NSString *)jid role:(NSString *)role affiliation:(NSString *)affiliation
  346. {
  347. LPGroupChatContact *newContact = [LPGroupChatContact groupChatContactWithNickame:nickname realJID:jid
  348. role:role affiliation:affiliation
  349. groupChat:self];
  350. [self p_addParticipant:newContact];
  351. if ([m_nickname isEqualToString:nickname]) {
  352. [self willChangeValueForKey:@"myGroupChatContact"];
  353. m_myGroupChatContact = newContact;
  354. [self didChangeValueForKey:@"myGroupChatContact"];
  355. }
  356. if (m_emitUserSystemMessages) {
  357. // Send a system message to our delegate
  358. NSString *sysMsg = [NSString stringWithFormat:
  359. NSLocalizedString(@"\"%@\"%@ has joined the chat. <%@, %@>", @"Chat room system message"),
  360. [newContact userPresentableNickname],
  361. NSStringWithFormatIfNotEmpty(@" (%@)", jid),
  362. role, affiliation];
  363. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  364. [m_delegate groupChat:self didReceiveSystemMessage:sysMsg];
  365. }
  366. }
  367. }
  368. - (void)handleContactWithNickname:(NSString *)nickname didChangeRoleTo:(NSString *)role affiliationTo:(NSString *)affiliation
  369. {
  370. LPGroupChatContact *contact = [self p_participantWithNickname:nickname];
  371. NSString *jid = [contact realJID];
  372. [contact handleChangedRole:role orAffiliation:affiliation];
  373. if (m_emitUserSystemMessages) {
  374. // Send a system message to our delegate
  375. NSString *sysMsg = [NSString stringWithFormat:
  376. NSLocalizedString(@"\"%@\"%@ is now <%@, %@>.", @"Chat room system message"),
  377. [contact userPresentableNickname],
  378. NSStringWithFormatIfNotEmpty(@" (%@)", jid),
  379. role, affiliation];
  380. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  381. [m_delegate groupChat:self didReceiveSystemMessage:sysMsg];
  382. }
  383. }
  384. }
  385. - (void)handleContactWithNickname:(NSString *)nickname didChangeStatusTo:(LPStatus)status statusMessageTo:(NSString *)statusMsg
  386. {
  387. [[self p_participantWithNickname:nickname] handleChangedStatus:status statusMessage:statusMsg];
  388. }
  389. - (void)handleContactWithNickname:(NSString *)nickname didChangeNicknameFrom:(NSString *)old_nickname to:(NSString *)new_nickname
  390. {
  391. NSString *oldPresentableNickname = [[self p_participantWithNickname:old_nickname] userPresentableNickname];
  392. [self p_updateParticipantNicknameFrom:old_nickname to:new_nickname];
  393. NSString *newPresentableNickname = [[self p_participantWithNickname:new_nickname] userPresentableNickname];
  394. if ([m_nickname isEqualToString:nickname]) {
  395. [self p_setNickname:new_nickname];
  396. }
  397. // Send a system message to our delegate
  398. NSString *sysMsg = [NSString stringWithFormat:
  399. NSLocalizedString(@"\"%@\" is now known as \"%@\".", @"Chat room system message"),
  400. oldPresentableNickname, newPresentableNickname];
  401. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  402. [m_delegate groupChat:self didReceiveSystemMessage:sysMsg];
  403. }
  404. }
  405. - (void)handleContactWithNickname:(NSString *)nickname wasKickedBy:(NSString *)actor reason:(NSString *)reason
  406. {
  407. LPGroupChatContact *contact = [self p_participantWithNickname:nickname];
  408. NSString *userPresentableNickname = [contact userPresentableNickname];
  409. NSString *jid = [contact realJID];
  410. [self p_removeParticipant:contact];
  411. // Send a system message to our delegate
  412. NSString *sysMsg = [NSString stringWithFormat:
  413. NSLocalizedString(@"\"%@\"%@ was kicked%@%@.", @"Chat room system message"),
  414. userPresentableNickname,
  415. NSStringWithFormatIfNotEmpty(@" (%@)", jid),
  416. NSStringWithFormatIfNotEmpty(@" by \"%@\"", actor),
  417. NSStringWithFormatIfNotEmpty(@" (reason: %@)", reason)];
  418. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  419. [m_delegate groupChat:self didReceiveSystemMessage:sysMsg];
  420. }
  421. // Are we the one being kicked?
  422. if ([m_nickname isEqualToString:nickname]) {
  423. if ([m_delegate respondsToSelector:@selector(groupChat:didGetKickedBy:reason:)]) {
  424. [m_delegate groupChat:self didGetKickedBy:[self p_participantWithNickname:actor] reason:reason];
  425. }
  426. }
  427. }
  428. - (void)handleContactWithNickname:(NSString *)nickname wasBannedBy:(NSString *)actor reason:(NSString *)reason
  429. {
  430. LPGroupChatContact *contact = [self p_participantWithNickname:nickname];
  431. NSString *userPresentableNickname = [contact userPresentableNickname];
  432. NSString *jid = [contact realJID];
  433. [self p_removeParticipant:contact];
  434. // Send a system message to our delegate
  435. NSString *sysMsg = [NSString stringWithFormat:
  436. NSLocalizedString(@"\"%@\"%@ was banned%@%@.", @"Chat room system message"),
  437. userPresentableNickname,
  438. NSStringWithFormatIfNotEmpty(@" (%@)", jid),
  439. NSStringWithFormatIfNotEmpty(@" by \"%@\"", actor),
  440. NSStringWithFormatIfNotEmpty(@" (reason: %@)", reason)];
  441. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  442. [m_delegate groupChat:self didReceiveSystemMessage:sysMsg];
  443. }
  444. // Are we the one being banned?
  445. if ([m_nickname isEqualToString:nickname]) {
  446. if ([m_delegate respondsToSelector:@selector(groupChat:didGetBannedBy:reason:)]) {
  447. [m_delegate groupChat:self didGetBannedBy:[self p_participantWithNickname:actor] reason:reason];
  448. }
  449. }
  450. }
  451. - (void)handleContactWithNickname:(NSString *)nickname wasRemovedFromChatBy:(NSString *)actor reason:(NSString *)reason dueTo:(NSString *)dueTo
  452. {
  453. LPGroupChatContact *contact = [self p_participantWithNickname:nickname];
  454. NSString *userPresentableNickname = [contact userPresentableNickname];
  455. LPGroupChatContact *actorContact = [self p_participantWithNickname:actor];
  456. NSString *actorPresentableNickname = [actorContact userPresentableNickname];
  457. NSString *jid = [contact realJID];
  458. [self p_removeParticipant:contact];
  459. // Send a system message to our delegate
  460. NSString *sysMsg = [NSString stringWithFormat:
  461. NSLocalizedString(@"\"%@\"%@ was removed from the room%@ (due to: \"%@\"%@).", @"Chat room system message"),
  462. userPresentableNickname,
  463. NSStringWithFormatIfNotEmpty(@" (%@)", jid),
  464. NSStringWithFormatIfNotEmpty(@" by \"%@\"", actorPresentableNickname),
  465. dueTo,
  466. NSStringWithFormatIfNotEmpty(@", reason: %@", reason)];
  467. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  468. [m_delegate groupChat:self didReceiveSystemMessage:sysMsg];
  469. }
  470. if ([m_nickname isEqualToString:nickname])
  471. ; // Do something different if we're the one being removed?
  472. }
  473. - (void)handleContactWithNickname:(NSString *)nickname didLeaveWithStatusMessage:(NSString *)status
  474. {
  475. LPGroupChatContact *contact = [self p_participantWithNickname:nickname];
  476. NSString *userPresentableNickname = [contact userPresentableNickname];
  477. NSString *jid = [contact realJID];
  478. [self p_removeParticipant:contact];
  479. if (m_emitUserSystemMessages) {
  480. // Send a system message to our delegate
  481. NSString *sysMsg = [NSString stringWithFormat:
  482. NSLocalizedString(@"\"%@\"%@ has left the room%@.", @"Chat room system message"),
  483. userPresentableNickname,
  484. NSStringWithFormatIfNotEmpty(@" (%@)", jid),
  485. NSStringWithFormatIfNotEmpty(@" (%@)", status)];
  486. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  487. [m_delegate groupChat:self didReceiveSystemMessage:sysMsg];
  488. }
  489. }
  490. if ([m_nickname isEqualToString:nickname])
  491. ; // Do something different if we're the one leaving?
  492. }
  493. - (void)handleGroupChatErrorWithCode:(int)code message:(NSString *)msg
  494. {
  495. // Send a system message to our delegate
  496. NSString *sysMsg = [NSString stringWithFormat:
  497. NSLocalizedString(@"Chat room error: %@ (%d)", @"Chat room system message"),
  498. msg, code];
  499. if (code == 401) {
  500. // Password required or wrong password was provided
  501. if ([m_delegate respondsToSelector:@selector(groupChat:unableToProceedDueToWrongPasswordWithErrorMessage:)]) {
  502. [m_delegate groupChat:self unableToProceedDueToWrongPasswordWithErrorMessage:sysMsg];
  503. }
  504. }
  505. else if (code == 409) {
  506. // Nickname is already taken
  507. if ([m_delegate respondsToSelector:@selector(groupChat:unableToProceedDueToNicknameAlreadyInUseWithErrorMessage:)]) {
  508. [m_delegate groupChat:self unableToProceedDueToNicknameAlreadyInUseWithErrorMessage:sysMsg];
  509. }
  510. }
  511. else {
  512. // Some other error
  513. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  514. [m_delegate groupChat:self didReceiveSystemMessage:sysMsg];
  515. }
  516. }
  517. }
  518. - (void)handleTopicChangedTo:(NSString *)newTopic by:(NSString *)actor
  519. {
  520. [self willChangeValueForKey:@"topic"];
  521. [m_topic release];
  522. m_topic = [newTopic copy];
  523. [self didChangeValueForKey:@"topic"];
  524. // Send a system message to our delegate
  525. LPGroupChatContact *contact = [self p_participantWithNickname:actor];
  526. NSString *userPresentableNickname = [contact userPresentableNickname];
  527. NSString *jid = [contact realJID];
  528. NSString *sysMsg;
  529. if ([actor length] > 0) {
  530. sysMsg = [NSString stringWithFormat:
  531. NSLocalizedString(@"\"%@\"%@ has changed the topic to: \"%@\"", @"Chat room system message"),
  532. userPresentableNickname,
  533. NSStringWithFormatIfNotEmpty(@" (%@)", jid),
  534. newTopic];
  535. }
  536. else {
  537. sysMsg = [NSString stringWithFormat:
  538. NSLocalizedString(@"The topic has been set to: \"%@\"", @"Chat room system message"),
  539. newTopic];
  540. }
  541. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveSystemMessage:)]) {
  542. [m_delegate groupChat:self didReceiveSystemMessage:sysMsg];
  543. }
  544. }
  545. - (void)handleReceivedMessageFromNickname:(NSString *)nickname plainBody:(NSString *)plainBody
  546. {
  547. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveMessage:fromContact:)]) {
  548. LPGroupChatContact *contact = [m_participantsByNickname objectForKey:nickname];
  549. [m_delegate groupChat:self didReceiveMessage:plainBody fromContact:contact];
  550. }
  551. }
  552. - (void)handleReceivedConfigurationForm:(NSString *)configForm errorMessage:(NSString *)errorMsg
  553. {
  554. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveRoomConfigurationForm:errorMessage:)]) {
  555. [m_delegate groupChat:self didReceiveRoomConfigurationForm:configForm errorMessage:errorMsg];
  556. }
  557. }
  558. - (void)handleResultOfConfigurationModification:(BOOL)succeeded errorMessage:(NSString *)errorMsg
  559. {
  560. if ([m_delegate respondsToSelector:@selector(groupChat:didReceiveResultOfRoomConfigurationModification:errorMessage:)]) {
  561. [m_delegate groupChat:self didReceiveResultOfRoomConfigurationModification:succeeded errorMessage:errorMsg];
  562. }
  563. }
  564. @end