wbowling / adium

fork of adium

Fork of Adium for patches/improvements

Clone this repository (size: 220.3 MB): HTTPS / SSH
$ hg clone http://hg.wbowling.info/adium
commit 2814: bfdbbb6fd385
parent 2813: f7650158eeb5
branch: default
Display the unsent message that is passed from libpurple instead of ignoring it. Fixes #13190.
William Bowling / wbowling
10 months ago
adium / Plugins / Purple Service / adiumPurpleConversation.m
r2814:bfdbbb6fd385 604 loc 22.2 KB embed / history / annotate / raw /
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
 * Adium is the legal property of its developers, whose names are listed in the copyright file included
 * with this source distribution.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import "adiumPurpleConversation.h"
#import <AIUtilities/AIObjectAdditions.h>
#import <AIUtilities/AIAttributedStringAdditions.h>
#import <Adium/AIChat.h>
#import <Adium/AIContentTyping.h>
#import <Adium/AIHTMLDecoder.h>
#import <Adium/AIListContact.h>
#import <Adium/AIContentControllerProtocol.h>
#import "AINudgeBuzzHandlerPlugin.h"

#pragma mark Purple Images

#pragma mark Conversations
static void adiumPurpleConvCreate(PurpleConversation *conv)
{
	//Pass chats along to the account
	if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) {
		
		AIChat *chat = groupChatLookupFromConv(conv);
		
		[accountLookup(purple_conversation_get_account(conv)) addChat:chat];
	}
}

static void adiumPurpleConvDestroy(PurpleConversation *conv)
{
	/* Purple is telling us a conv was destroyed.  We've probably already cleaned up, but be sure in case purple calls this
	 * when we don't ask it to (for example if we are summarily kicked from a chat room and purple closes the 'window').
	 */
	AIChat *chat = (AIChat *)conv->ui_data;

	AILogWithSignature(@"%p: %@", conv, chat);

	//Chat will be nil if we've already cleaned up, at which point no further action is needed.
	if (chat) {
		[accountLookup(purple_conversation_get_account(conv)) chatWasDestroyed:chat];

		[chat setIdentifier:nil];
		[chat release];
		conv->ui_data = nil;
	}
}

static void adiumPurpleConvWriteChat(PurpleConversation *conv, const char *who,
								   const char *message, PurpleMessageFlags flags,
								   time_t mtime)
{
	/* We only care about this if:
	 *	1) It does not have the PURPLE_MESSAGE_SEND flag, which is set if Purple is sending a sent message back to us -or-
	 *  2) It is a delayed (history) message from a chat
	 */
	if (!(flags & PURPLE_MESSAGE_SEND) || (flags & PURPLE_MESSAGE_DELAYED)) {
		NSDictionary	*messageDict;
		NSString		*messageString;

		messageString = [NSString stringWithUTF8String:message];
		AILog(@"Source: %s \t Name: %s \t MyNick: %s : Message %@", 
			  who,
			  purple_conversation_get_name(conv),
			  purple_conv_chat_get_nick(PURPLE_CONV_CHAT(conv)),
			  messageString);

		NSDate				*date = [NSDate dateWithTimeIntervalSince1970:mtime];
		PurpleAccount		*purpleAccount = purple_conversation_get_account(conv);
		
		if ((flags & PURPLE_MESSAGE_SYSTEM) == PURPLE_MESSAGE_SYSTEM || !who) {
			CBPurpleAccount *account = accountLookup(purpleAccount);
			
			[account receivedEventForChat:groupChatLookupFromConv(conv)
								  message:messageString
									 date:date
									flags:flags];
		} else {
			NSAttributedString	*attributedMessage = [AIHTMLDecoder decodeHTML:messageString];
			NSNumber			*purpleMessageFlags = [NSNumber numberWithInteger:flags];
			NSString			*normalizedUID = get_real_name_for_account_conv_buddy(purpleAccount, conv, (char *)who);
			
			if (normalizedUID.length) {
				messageDict = [NSDictionary dictionaryWithObjectsAndKeys:attributedMessage, @"AttributedMessage",
							   normalizedUID, @"Source",
							   purpleMessageFlags, @"PurpleMessageFlags",
							   date, @"Date",nil];
				
			} else {
				messageDict = [NSDictionary dictionaryWithObjectsAndKeys:attributedMessage, @"AttributedMessage",
							   purpleMessageFlags, @"PurpleMessageFlags",
							   date, @"Date",nil];
			}

			[accountLookup(purple_conversation_get_account(conv)) receivedMultiChatMessage:messageDict inChat:groupChatLookupFromConv(conv)];
		}
	}
}

static void adiumPurpleConvWriteIm(PurpleConversation *conv, const char *who,
								 const char *message, PurpleMessageFlags flags,
								 time_t mtime)
{
	//We only care about this if it does not have the PURPLE_MESSAGE_SEND flag, which is set if Purple is sending a sent message back to us
	if ((flags & PURPLE_MESSAGE_SEND) == 0) {
		if (flags & PURPLE_MESSAGE_NOTIFY) {
			// We received a notification (nudge or buzz). Send a notification of such.
			NSString *type, *messageString = [NSString stringWithUTF8String:message];

			// Determine what we're actually notifying about.
			if ([messageString rangeOfString:@"nudge" options:(NSCaseInsensitiveSearch | NSLiteralSearch)].location != NSNotFound) {
				type = @"Nudge";
			} else if ([messageString rangeOfString:@"buzz" options:(NSCaseInsensitiveSearch | NSLiteralSearch)].location != NSNotFound) {
				type = @"Buzz";
			} else {
				// Just call an unknown type a "notification"
				type = @"notification";
			}

			[[NSNotificationCenter defaultCenter] postNotificationName:Chat_NudgeBuzzOccured
																			   object:chatLookupFromConv(conv)
																			 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
																					   type, @"Type",
																					   nil]];
		} else {
			NSDictionary		*messageDict;
			CBPurpleAccount		*adiumAccount = accountLookup(purple_conversation_get_account(conv));
			NSString			*messageString;
			AIChat				*chat;
			
			messageString = [NSString stringWithUTF8String:message];
			chat = chatLookupFromConv(conv);
			
			AILog(@"adiumPurpleConvWriteIm: Received %@ from %@", messageString, chat.listObject.UID);
			
			//Process any purple imgstore references into real HTML tags pointing to real images
			messageString = processPurpleImages(messageString, adiumAccount);
			
			messageDict = [NSDictionary dictionaryWithObjectsAndKeys:messageString,@"Message",
						   [NSNumber numberWithInteger:flags],@"PurpleMessageFlags",
						   [NSDate dateWithTimeIntervalSince1970:mtime],@"Date",nil];
			
			[adiumAccount receivedIMChatMessage:messageDict
										 inChat:chat];
		}
	}
}

static void adiumPurpleConvWriteConv(PurpleConversation *conv, const char *who, const char *alias,
								   const char *message, PurpleMessageFlags flags,
								   time_t mtime)
{
	AILog(@"adiumPurpleConvWriteConv: Received %s from %s [%i]",message,who,flags);
	AIChat	*chat = chatLookupFromConv(conv);

	if (!chat) {
		return;
	}
	
	NSString			*messageString = [NSString stringWithUTF8String:message];
	
	if (!messageString) {
		AILogWithSignature(@"Received write without message: %@ %d", chat, flags);
		return;
	}
	
	if (flags & PURPLE_MESSAGE_ERROR) {	
		if ([messageString rangeOfString:@"User information not available"].location != NSNotFound) {
			//Ignore user information errors; they are irrelevent
			//XXX The user info check only works in English; libpurple should be modified to be better about this useless information spamming
			return;
		}
		
		AIChatErrorType	errorType = AIChatUnknownError;
		
		if (([messageString rangeOfString:[NSString stringWithUTF8String:_("Not logged in")]].location != NSNotFound) || 
			([messageString rangeOfString:[NSString stringWithUTF8String:_("User temporarily unavailable")]].location != NSNotFound)) {
			errorType = AIChatMessageSendingUserNotAvailable;
		} else if ([messageString rangeOfString:[NSString stringWithUTF8String:_("In local permit/deny")]].location != NSNotFound) {
			errorType = AIChatMessageSendingUserIsBlocked;
		} else if (([messageString rangeOfString:[NSString stringWithUTF8String:_("Reply too big")]].location != NSNotFound) ||
				   ([messageString rangeOfString:@"message is too large"].location != NSNotFound)) {
			//XXX - there may be other conditions, but this seems the most common so that's how we'll classify it
			errorType = AIChatMessageSendingTooLarge;
		} else if ([messageString rangeOfString:[NSString stringWithUTF8String:_("Command failed")]].location != NSNotFound) {
			errorType = AIChatCommandFailed;
		} else if ([messageString rangeOfString:[NSString stringWithUTF8String:_("Wrong number of arguments")]].location != NSNotFound) {
			errorType = AIChatInvalidNumberOfArguments;
		} else if ([messageString rangeOfString:[NSString stringWithUTF8String:_("Rate")]].location != NSNotFound) {
			//XXX Is 'Rate' really a standalone translated string?
			errorType = AIChatMessageSendingMissedRateLimitExceeded;
		} else if ([messageString rangeOfString:[NSString stringWithUTF8String:_("Too evil")]].location != NSNotFound) {
			errorType = AIChatMessageReceivingMissedRemoteIsTooEvil;
		}
		/* Another is 'refused by client', which is definitely seen when sending an offline message to an invalid screenname...
		 * but I don't know when else it is sent. -evands
		 */
		
		/* We will wait until the next run loop, in case this error message was generated by
		 * the sending of a message. This allows the results of sending the message to be displayed
		 * first.
		 */
		if (errorType != AIChatUnknownError) {
			[accountLookup(purple_conversation_get_account(conv)) performSelector:@selector(errorForChat:type:)
			 withObject:chat
			 withObject:[NSNumber numberWithInteger:errorType]
			 afterDelay:0];
		} else {
			[adium.contentController performSelector:@selector(displayEvent:ofType:inChat:)
			 withObject:messageString
			 withObject:@"libpurpleMessage"
			 withObject:chat
			 afterDelay:0];
		}
		
		AILog(@"*** Conversation error %@: %@", chat, messageString);
		
	} else if (flags & PURPLE_MESSAGE_RAW)
	{
		[adium.contentController performSelector:@selector(displayEvent:ofType:inChat:)
									  withObject:messageString
									  withObject:@"libpurpleMessage"
									  withObject:chat
									  afterDelay:0];
		
	} else {
		BOOL				shouldDisplayMessage = TRUE;
		if (strcmp(message, _("Direct IM established")) == 0) {
			[accountLookup(purple_conversation_get_account(conv)) updateContact:chat.listObject
											   forEvent:[NSNumber numberWithInteger:PURPLE_BUDDY_DIRECTIM_CONNECTED]];
			shouldDisplayMessage = FALSE;
			
		} else {
			BOOL isClosingDirectIM = FALSE;
			if ((strcmp(message, _("The remote user has closed the connection.")) == 0) ||
				(strcmp(message, _("The remote user has declined your request.")) == 0) ||
				(strcmp(message, _("Received invalid data on connection with remote user.")) == 0) ||
				(strcmp(message, _("Could not establish a connection with the remote user.")) == 0)) {
				isClosingDirectIM = TRUE;
			}
			
			if (!isClosingDirectIM) {
				//Only works in English - XXX fix me!
				if ([messageString rangeOfString:@"Lost connection with the remote user:"].location != NSNotFound) {
					isClosingDirectIM = TRUE;
				}
			}
			
			if (isClosingDirectIM) {
				if (strcmp(message, _("The remote user has closed the connection.")) != 0) {
					//Display the message if it's not just the one for the other guy closing it...
					[adium.contentController displayEvent:messageString
												   ofType:@"directIMDisconnected"
												   inChat:chat];
				}
				
				[accountLookup(purple_conversation_get_account(conv)) updateContact:chat.listObject forEvent:[NSNumber numberWithInteger:PURPLE_BUDDY_DIRECTIM_DISCONNECTED]];
				shouldDisplayMessage = FALSE;
			}
		}

		if (shouldDisplayMessage) {
			CBPurpleAccount *account = accountLookup(purple_conversation_get_account(conv));
			
			[account receivedEventForChat:chat
								  message:messageString
									 date:[NSDate dateWithTimeIntervalSince1970:mtime]
									flags:flags];
		}
	}
}

NSString *get_real_name_for_account_conv_buddy(PurpleAccount *account, PurpleConversation *conv, char *who)
{
	g_return_val_if_fail(who != NULL && strlen(who), nil);
	
	PurplePlugin *prpl = purple_find_prpl(purple_account_get_protocol_id(account));
	PurplePluginProtocolInfo  *prpl_info = (prpl ? PURPLE_PLUGIN_PROTOCOL_INFO(prpl) : NULL);
	PurpleConvChat *convChat = purple_conversation_get_chat_data(conv);
	
	char *uid = NULL;
	
	NSString *normalizedUID;
	
	if (prpl_info && prpl_info->get_cb_real_name) {
		// Get the real name of the buddy for use as a UID, if available.
		uid = prpl_info->get_cb_real_name(purple_account_get_connection(account),
										  purple_conv_chat_get_id(convChat),
										  who);
	}
	
	if (!uid) {
		// strdup it, mostly so the free below won't have to be cased out.
		uid = g_strdup(who);
	}
		
	normalizedUID = [NSString stringWithUTF8String:purple_normalize(account, uid)];
		
	// We have to free the result of get_cb_real_name.
	g_free(uid);

	return normalizedUID;
}

static void adiumPurpleConvChatAddUsers(PurpleConversation *conv, GList *cbuddies, gboolean new_arrivals)
{
	if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) {
		PurpleAccount *account = purple_conversation_get_account(conv);

		NSMutableArray *users = [NSMutableArray array];
		
		for (GList *l = cbuddies; l; l = l->next) {
			PurpleConvChatBuddy *cb = (PurpleConvChatBuddy *)l->data;
			
			NSMutableDictionary *user = [NSMutableDictionary dictionary];
			[user setObject:get_real_name_for_account_conv_buddy(account, conv, cb->name) forKey:@"UID"];
			[user setObject:[NSNumber numberWithInteger:cb->flags] forKey:@"Flags"];
			if (cb->alias) {
				[user setObject:[NSString stringWithUTF8String:cb->alias] forKey:@"Alias"];
			}
			
			[users addObject:user];
		}

		[accountLookup(account) updateUserListForChat:groupChatLookupFromConv(conv)
												users:users
										   newlyAdded:new_arrivals];
	} else
		AILog(@"adiumPurpleConvChatAddUsers: IM");
}

static void adiumPurpleConvChatRenameUser(PurpleConversation *conv, const char *oldName,
										const char *newName, const char *newAlias)
{
	AILog(@"adiumPurpleConvChatRenameUser: %s: oldName %s, newName %s, newAlias %s",
			   purple_conversation_get_name(conv),
			   oldName, newName, newAlias);
	
	if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) {
		PurpleConvChat *convChat = purple_conversation_get_chat_data(conv);
		PurpleConvChatBuddy *cb = purple_conv_chat_cb_find(convChat, oldName);
		
		PurpleAccount *account = purple_conversation_get_account(conv);
		
		[accountLookup(purple_conversation_get_account(conv)) renameParticipant:get_real_name_for_account_conv_buddy(account, conv, (char *)oldName)
																		newName:get_real_name_for_account_conv_buddy(account, conv, (char *)newName)
																	   newAlias:[NSString stringWithUTF8String:newAlias]
																		  flags:cb->flags
																		 inChat:groupChatLookupFromConv(conv)];
	}
}

static void adiumPurpleConvChatRemoveUsers(PurpleConversation *conv, GList *users)
{
	if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) {
		NSMutableArray	*usersArray = [NSMutableArray array];
		PurpleAccount	*account = purple_conversation_get_account(conv);

		GList *l;
		for (l = users; l != NULL; l = l->next) {
			NSString *normalizedUID = get_real_name_for_account_conv_buddy(account, conv, (char *)l->data);
			[usersArray addObject:normalizedUID];
		}

		[accountLookup(account) removeUsersArray:usersArray
										fromChat:groupChatLookupFromConv(conv)];

	} else {
		AILog(@"adiumPurpleConvChatRemoveUser: IM");
	}
}

static void adiumPurpleConvUpdateUser(PurpleConversation *conv, const char *user)
{
	PurpleAccount *account = purple_conversation_get_account(conv);
	CBPurpleAccount *adiumAccount = accountLookup(account);
	
	PurpleConvChatBuddy *cb = purple_conv_chat_cb_find(PURPLE_CONV_CHAT(conv), user);
	
	NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
	
	GList *attribute = purple_conv_chat_cb_get_attribute_keys(cb);
	
	for (; attribute != NULL; attribute = g_list_next(attribute)) {
		[attributes setObject:[NSString stringWithUTF8String:purple_conv_chat_cb_get_attribute(cb, attribute->data)]
					   forKey:[NSString stringWithUTF8String:attribute->data]];
	}
	
	g_list_free(attribute);
	
	NSString *alias = cb->alias ? [NSString stringWithUTF8String:cb->alias] : nil;
	
	[adiumAccount updateUser:get_real_name_for_account_conv_buddy(account, conv, (char *)user)
					 forChat:groupChatLookupFromConv(conv)
					   flags:cb->flags
					   alias:alias
				  attributes:attributes];
}

static void adiumPurpleConvPresent(PurpleConversation *conv)
{
	
}

//This isn't a function we want Purple doing anything with, I don't think
static gboolean adiumPurpleConvHasFocus(PurpleConversation *conv)
{
	return NO;
}

static void adiumPurpleConvUpdated(PurpleConversation *conv, PurpleConvUpdateType type)
{
	if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) {
		PurpleConvChat  *chat = purple_conversation_get_chat_data(conv);
		
		switch(type) {
			case PURPLE_CONV_UPDATE_TOPIC:
			{
				NSString *who = nil;
				
				if (chat->who != NULL) {
					who = [NSString stringWithUTF8String:chat->who];
				}
				
				[accountLookup(purple_conversation_get_account(conv)) updateTopic:(purple_conv_chat_get_topic(chat) ?
																				   [NSString stringWithUTF8String:purple_conv_chat_get_topic(chat)] :
																				   nil)
																		  forChat:groupChatLookupFromConv(conv)
																	   withSource:who];
				break;
			}
			case PURPLE_CONV_UPDATE_TITLE:
				[accountLookup(purple_conversation_get_account(conv)) updateTitle:(purple_conversation_get_title(conv) ?
														   [NSString stringWithUTF8String:purple_conversation_get_title(conv)] :
														   nil)
												  forChat:groupChatLookupFromConv(conv)];
				
				AILog(@"Update to title: %s",purple_conversation_get_title(conv));
				break;
			case PURPLE_CONV_UPDATE_CHATLEFT:
				[accountLookup(purple_conversation_get_account(conv)) leftChat:groupChatLookupFromConv(conv)];
				break;
			case PURPLE_CONV_UPDATE_ADD:
			case PURPLE_CONV_UPDATE_REMOVE:
			case PURPLE_CONV_UPDATE_ACCOUNT:
			case PURPLE_CONV_UPDATE_TYPING:
			case PURPLE_CONV_UPDATE_UNSEEN:
			case PURPLE_CONV_UPDATE_LOGGING:
			case PURPLE_CONV_ACCOUNT_ONLINE:
			case PURPLE_CONV_ACCOUNT_OFFLINE:
			case PURPLE_CONV_UPDATE_AWAY:
			case PURPLE_CONV_UPDATE_ICON:
			case PURPLE_CONV_UPDATE_FEATURES:

/*				
				[accountLookup(purple_conversation_get_account(conv)) mainPerformSelector:@selector(convUpdateForChat:type:)
													   withObject:groupChatLookupFromConv(conv)
													   withObject:[NSNumber numberWithInt:type]];
*/				
			default:
				break;
		}

	} else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) {
		PurpleConvIm  *im = purple_conversation_get_im_data(conv);
		switch (type) {
			case PURPLE_CONV_UPDATE_TYPING: {

				AITypingState typingState;

				switch (purple_conv_im_get_typing_state(im)) {
					case PURPLE_TYPING:
						typingState = AITyping;
						break;
					case PURPLE_TYPED:
						typingState = AIEnteredText;
						break;
					case PURPLE_NOT_TYPING:
					default:
						typingState = AINotTyping;
						break;
				}

				NSNumber	*typingStateNumber = [NSNumber numberWithInteger:typingState];

				[accountLookup(purple_conversation_get_account(conv)) typingUpdateForIMChat:imChatLookupFromConv(conv)
															 typing:typingStateNumber];
				break;
			}
			case PURPLE_CONV_UPDATE_AWAY: {
				//If the conversation update is UPDATE_AWAY, it seems to suppress the typing state being updated
				//Reset purple's typing tracking, then update to receive a PURPLE_CONV_UPDATE_TYPING message
				purple_conv_im_set_typing_state(im, PURPLE_NOT_TYPING);
				purple_conv_im_update_typing(im);
				break;
			}
			default:
				break;
		}
	}
}

#pragma mark Custom smileys
gboolean adiumPurpleConvCustomSmileyAdd(PurpleConversation *conv, const char *smile, gboolean remote)
{
	AILog(@"%s: Added Custom Smiley %s",purple_conversation_get_name(conv),smile);
	[accountLookup(purple_conversation_get_account(conv)) chat:chatLookupFromConv(conv)
			 isWaitingOnCustomEmoticon:[NSString stringWithUTF8String:smile]];

	return TRUE;
}

void adiumPurpleConvCustomSmileyWrite(PurpleConversation *conv, const char *smile,
									const guchar *data, gsize size)
{
	AILog(@"%s: Write Custom Smiley %s (%x %i)",purple_conversation_get_name(conv),smile,data,size);

	[accountLookup(purple_conversation_get_account(conv)) chat:chatLookupFromConv(conv)
					 setCustomEmoticon:[NSString stringWithUTF8String:smile]
						 withImageData:[NSData dataWithBytes:data
													  length:size]];
}

void adiumPurpleConvCustomSmileyClose(PurpleConversation *conv, const char *smile)
{
	AILog(@"%s: Close Custom Smiley %s",purple_conversation_get_name(conv),smile);

	[accountLookup(purple_conversation_get_account(conv)) chat:chatLookupFromConv(conv)
				  closedCustomEmoticon:[NSString stringWithUTF8String:smile]];
}

static gboolean adiumPurpleConvJoin(PurpleConversation *conv, const char *name,
									PurpleConvChatBuddyFlags flags,
									GHashTable *users)
{
	AIChat *chat = groupChatLookupFromConv(conv);

	// We return TRUE if we want to hide it.
	return !chat.showJoinLeave;
}

static gboolean adiumPurpleConvLeave(PurpleConversation *conv, const char *name,
									 const char *reason, GHashTable *users)
{
	AIChat *chat = groupChatLookupFromConv(conv);
	
	// We return TRUE if we want to hide it.
	return !chat.showJoinLeave;	
}

static PurpleConversationUiOps adiumPurpleConversationOps = {
	adiumPurpleConvCreate,
    adiumPurpleConvDestroy,
    adiumPurpleConvWriteChat,
    adiumPurpleConvWriteIm,
    adiumPurpleConvWriteConv,
    adiumPurpleConvChatAddUsers,
    adiumPurpleConvChatRenameUser,
    adiumPurpleConvChatRemoveUsers,
	adiumPurpleConvUpdateUser,
	
	adiumPurpleConvPresent,
	adiumPurpleConvHasFocus,

	/* Custom Smileys */
	adiumPurpleConvCustomSmileyAdd,
	adiumPurpleConvCustomSmileyWrite,
	adiumPurpleConvCustomSmileyClose,

	/* send_confirm */
	NULL
};

PurpleConversationUiOps *adium_purple_conversation_get_ui_ops(void)
{
	return &adiumPurpleConversationOps;
}

void adiumPurpleConversation_init(void)
{	
	purple_conversations_set_ui_ops(adium_purple_conversation_get_ui_ops());

	purple_signal_connect_priority(purple_conversations_get_handle(), "conversation-updated", adium_purple_get_handle(),
								 PURPLE_CALLBACK(adiumPurpleConvUpdated), NULL,
								 PURPLE_SIGNAL_PRIORITY_LOWEST);
	
	purple_signal_connect(purple_conversations_get_handle(), "chat-buddy-joining", adium_purple_get_handle(),
						  PURPLE_CALLBACK(adiumPurpleConvJoin), NULL);
	
	purple_signal_connect(purple_conversations_get_handle(), "chat-buddy-leaving", adium_purple_get_handle(),
						  PURPLE_CALLBACK(adiumPurpleConvLeave), NULL);
	
}