summaryrefslogtreecommitdiff
path: root/libpurple/protocols/jabber/message.c
blob: f42588170ce39b0d242d4ce63a2fc3486a8d3a71 (plain)
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
/*
 * purple - Jabber Protocol Plugin
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed 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., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 *
 */
#include <glib/gi18n-lib.h>

#include <purple.h>

#include "adhoccommands.h"
#include "buddy.h"
#include "chat.h"
#include "data.h"
#include "message.h"
#include "pep.h"
#include "iq.h"

#include <string.h>

typedef struct {
	PurpleConversation *conv;
	gchar *shortcut;
} JabberMessageRemoteSmileyAddData;

static GString *jm_body_with_oob(JabberMessage *jm) {
	GList *etc;
	GString *body = g_string_new("");

	if(jm->xhtml)
		g_string_append(body, jm->xhtml);
	else if(jm->body)
		g_string_append(body, jm->body);

	for(etc = jm->etc; etc; etc = etc->next) {
		PurpleXmlNode *x = etc->data;
		const char *xmlns = purple_xmlnode_get_namespace(x);
		if(purple_strequal(xmlns, NS_OOB_X_DATA)) {
			PurpleXmlNode *url, *desc;
			char *urltxt, *desctxt;

			url = purple_xmlnode_get_child(x, "url");
			desc = purple_xmlnode_get_child(x, "desc");

			if(!url)
				continue;

			urltxt = purple_xmlnode_get_data(url);
			desctxt = desc ? purple_xmlnode_get_data(desc) : urltxt;

			if(body->len && !purple_strequal(body->str, urltxt))
				g_string_append_printf(body, "<br/><a href='%s'>%s</a>",
						urltxt, desctxt);
			else
				g_string_printf(body, "<a href='%s'>%s</a>",
						urltxt, desctxt);

			g_free(urltxt);

			if(desctxt != urltxt)
				g_free(desctxt);
		}
	}

	return body;
}

void jabber_message_free(JabberMessage *jm)
{
	g_free(jm->from);
	g_free(jm->to);
	g_free(jm->id);
	g_free(jm->subject);
	g_free(jm->body);
	g_free(jm->xhtml);
	g_free(jm->password);
	g_free(jm->error);
	g_free(jm->thread_id);
	g_list_free(jm->etc);
	g_list_free(jm->eventitems);

	g_free(jm);
}

static void handle_chat(JabberMessage *jm)
{
	const gchar *contact = jm->outgoing ? jm->to : jm->from;
	JabberID *jid = jabber_id_new(contact);

	PurpleConnection *gc;
	PurpleAccount *account;
	PurpleMessageFlags flags = 0;
	JabberBuddy *jb;
	JabberBuddyResource *jbr;
	GString *body;

	if(!jid)
		return;

	gc = jm->js->gc;
	account = purple_connection_get_account(gc);

	jb = jabber_buddy_find(jm->js, contact, TRUE);
	jbr = jabber_buddy_find_resource(jb, jid->resource);

	if (jbr && jm->chat_state != JM_STATE_NONE)
		jbr->chat_states = JABBER_CHAT_STATES_SUPPORTED;

	switch(jm->chat_state) {
		case JM_STATE_COMPOSING:
			purple_serv_got_typing(gc, contact, 0, PURPLE_IM_TYPING);
			break;
		case JM_STATE_PAUSED:
			purple_serv_got_typing(gc, contact, 0, PURPLE_IM_TYPED);
			break;
		case JM_STATE_GONE: {
			PurpleIMConversation *im = purple_conversations_find_im_with_account(
					contact, account);
			if (im && jid->node && jid->domain) {
				char buf[256];
				PurpleBuddy *buddy;

				g_snprintf(buf, sizeof(buf), "%s@%s", jid->node, jid->domain);

				if ((buddy = purple_blist_find_buddy(account, buf))) {
					const char *who;
					char *escaped;

					who = purple_buddy_get_alias(buddy);
					escaped = g_markup_escape_text(who, -1);

					g_snprintf(buf, sizeof(buf),
					           _("%s has left the conversation."), escaped);
					g_free(escaped);

					/* At some point when we restructure PurpleConversation,
					 * this should be able to be implemented by removing the
					 * user from the conversation like we do with chats now. */
					purple_conversation_write_system_message(
						PURPLE_CONVERSATION(im), buf, 0);
				}
			}
			purple_serv_got_typing_stopped(gc, contact);
			break;
		}
		default:
			purple_serv_got_typing_stopped(gc, contact);
	}

	body = jm_body_with_oob(jm);

	if(body && body->len) {
		if (jid->resource) {
			/*
			 * We received a message from a specific resource, so
			 * we probably want a reply to go to this specific
			 * resource (i.e. bind/lock the conversation to this
			 * resource).
			 *
			 * This works because purple_im_conversation_send gets the name
			 * from purple_conversation_get_name()
			 */
			PurpleIMConversation *im;

			im = purple_conversations_find_im_with_account(contact, account);
			if (im && !purple_strequal(contact,
					purple_conversation_get_name(PURPLE_CONVERSATION(im)))) {
				purple_debug_info("jabber", "Binding conversation to %s\n",
				                  contact);
				purple_conversation_set_name(PURPLE_CONVERSATION(im), contact);
			}
		}

		if(jbr) {
			/* Treat SUPPORTED as a terminal with no escape :) */
			if (jbr->chat_states != JABBER_CHAT_STATES_SUPPORTED) {
				if (jm->chat_state != JM_STATE_NONE)
					jbr->chat_states = JABBER_CHAT_STATES_SUPPORTED;
				else
					jbr->chat_states = JABBER_CHAT_STATES_UNSUPPORTED;
			}

			g_free(jbr->thread_id);
			jbr->thread_id = g_strdup(jm->thread_id);
		}

		if(jm->forwarded) {
			flags |= PURPLE_MESSAGE_FORWARDED;
		}
		flags |= jm->outgoing ? PURPLE_MESSAGE_SEND : PURPLE_MESSAGE_RECV;

		purple_serv_got_im(gc, contact, body->str, flags, jm->sent);
	}

	jabber_id_free(jid);

	if(body)
		g_string_free(body, TRUE);
}

static void handle_headline(JabberMessage *jm)
{
	char *title;
	GString *body;

	if(!jm->xhtml && !jm->body)
		return; /* ignore headlines without any content */

	body = jm_body_with_oob(jm);
	title = g_strdup_printf(_("Message from %s"), jm->from);

	purple_notify_formatted(jm->js->gc, title, jm->subject ? jm->subject : title,
			NULL, body->str, NULL, NULL);

	g_free(title);
	g_string_free(body, TRUE);
}

static void handle_groupchat(JabberMessage *jm)
{
	JabberID *jid = jabber_id_new(jm->from);
	JabberChat *chat;
	PurpleMessageFlags messageFlags = 0;

	if(!jid)
		return;

	chat = jabber_chat_find(jm->js, jid->node, jid->domain);

	if(!chat)
		return;

	if(jm->subject) {
		purple_chat_conversation_set_topic(chat->conv, jid->resource,
				jm->subject);
		messageFlags |= PURPLE_MESSAGE_NO_LOG;
		if(!jm->xhtml && !jm->body) {
			char *msg, *tmp, *tmp2;
			tmp = g_markup_escape_text(jm->subject, -1);
			tmp2 = purple_markup_linkify(tmp);
			if(jid->resource)
				msg = g_strdup_printf(_("%s has set the topic to: %s"), jid->resource, tmp2);
			else
				msg = g_strdup_printf(_("The topic is: %s"), tmp2);
			purple_conversation_write_system_message(PURPLE_CONVERSATION(chat->conv),
				msg, messageFlags);
			g_free(tmp);
			g_free(tmp2);
			g_free(msg);
		}
	}

	if(jm->xhtml || jm->body) {
		if(jid->resource)
			purple_serv_got_chat_in(jm->js->gc, chat->id, jid->resource,
							messageFlags | (jm->delayed ? PURPLE_MESSAGE_DELAYED : 0),
							jm->xhtml ? jm->xhtml : jm->body, jm->sent);
		else if(chat->muc)
			purple_conversation_write_system_message(
				PURPLE_CONVERSATION(chat->conv),
				jm->xhtml ? jm->xhtml : jm->body, messageFlags);
	}

	jabber_id_free(jid);
}

static void handle_groupchat_invite(JabberMessage *jm)
{
	GHashTable *components;
	JabberID *jid = jabber_id_new(jm->to);

	if(!jid)
		return;

	components = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);

	g_hash_table_replace(components, "room", g_strdup(jid->node));
	g_hash_table_replace(components, "server", g_strdup(jid->domain));
	g_hash_table_replace(components, "handle", g_strdup(jm->js->user->node));
	g_hash_table_replace(components, "password", g_strdup(jm->password));

	jabber_id_free(jid);
	purple_serv_got_chat_invite(jm->js->gc, jm->to, jm->from, jm->body, components);
}

static void handle_error(JabberMessage *jm)
{
	char *buf;

	if(!jm->body)
		return;

	buf = g_strdup_printf(_("Message delivery to %s failed: %s"),
			jm->from, jm->error ? jm->error : "");

	purple_notify_formatted(jm->js->gc, _("XMPP Message Error"), _("XMPP Message Error"), buf,
			jm->xhtml ? jm->xhtml : jm->body, NULL, NULL);

	g_free(buf);
}

static void handle_buzz(JabberMessage *jm) {
	PurpleAccount *account;

	/* Delayed buzz MUST NOT be accepted */
	if(jm->delayed)
		return;

	/* Reject buzz when it's not enabled */
	if(!jm->js->allowBuzz)
		return;

	account = purple_connection_get_account(jm->js->gc);

	if (purple_blist_find_buddy(account, jm->from) == NULL)
		return; /* Do not accept buzzes from unknown people */

	/* xmpp only has 1 attention type, so index is 0 */
	purple_protocol_got_attention(jm->js->gc, jm->from, 0);
}

static void
jabber_message_get_refs_from_xmlnode_internal(const PurpleXmlNode *message,
	GHashTable *table)
{
	PurpleXmlNode *child;

	for (child = purple_xmlnode_get_child(message, "img") ; child ;
		 child = purple_xmlnode_get_next_twin(child)) {
		const gchar *src = purple_xmlnode_get_attrib(child, "src");

		if (g_str_has_prefix(src, "cid:")) {
			const gchar *cid = src + 4;

			/* if we haven't "fetched" this yet... */
			if (!g_hash_table_lookup(table, cid)) {
				gchar *value;
				const gchar *alt = purple_xmlnode_get_attrib(child, "alt");
				/* if there is no "alt" string, use the cid...
				 include the entire src, eg. "cid:.." to avoid linkification */
				if (alt && alt[0] != '\0') {
					/* workaround for when "alt" is set to the value of the
					 CID (which Jabbim seems to do), to avoid it showing up
						 as an mailto: link */
					if (purple_email_is_valid(alt)) {
						value = g_strdup_printf("smiley:%s", alt);
					} else {
						value = g_strdup(alt);
					}
				} else {
					value = g_strdup(src);
				}
				g_hash_table_insert(table, g_strdup(cid), value);
			}
		}
	}

	for (child = message->child ; child ; child = child->next) {
		jabber_message_get_refs_from_xmlnode_internal(child, table);
	}
}

static gboolean
jabber_message_get_refs(gpointer key, gpointer value, gpointer user_data)
{
	GList **refs = (GList **) user_data;
	PurpleKeyValuePair *ref = purple_key_value_pair_new_full(key, value, g_free);

	*refs = g_list_append(*refs, ref);

	return TRUE;
}

static GList *
jabber_message_get_refs_from_xmlnode(const PurpleXmlNode *message)
{
	GList *refs = NULL;
	GHashTable *unique_refs = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);

	jabber_message_get_refs_from_xmlnode_internal(message, unique_refs);
	(void)g_hash_table_foreach_remove(unique_refs, jabber_message_get_refs, &refs);
	g_hash_table_destroy(unique_refs);
	return refs;
}

static gchar *
jabber_message_xml_to_string_strip_img_smileys(PurpleXmlNode *xhtml)
{
	gchar *markup = purple_xmlnode_to_str(xhtml, NULL);
	int len = strlen(markup);
	int pos = 0;
	GString *out = g_string_new(NULL);

	while (pos < len) {
		/* this is a bit cludgy, maybe there is a better way to do this...
		  we need to find all <img> tags within the XHTML and replace those
			tags with the value of their "alt" attributes */
		if (g_str_has_prefix(&(markup[pos]), "<img")) {
			PurpleXmlNode *img = NULL;
			int pos2 = pos;
			const gchar *src;

			for (; pos2 < len ; pos2++) {
				if (g_str_has_prefix(&(markup[pos2]), "/>")) {
					pos2 += 2;
					break;
				} else if (g_str_has_prefix(&(markup[pos2]), "</img>")) {
					pos2 += 5;
					break;
				}
			}

			/* note, if the above loop didn't find the end of the <img> tag,
			  it the parsed string will be until the end of the input string,
			  in which case purple_xmlnode_from_str will bail out and return NULL,
			  in this case the "if" statement below doesn't trigger and the
			  text is copied unchanged */
			img = purple_xmlnode_from_str(&(markup[pos]), pos2 - pos);
			src = purple_xmlnode_get_attrib(img, "src");

			if (g_str_has_prefix(src, "cid:")) {
				const gchar *alt = purple_xmlnode_get_attrib(img, "alt");
				/* if the "alt" attribute is empty, put the cid as smiley string */
				if (alt && alt[0] != '\0') {
					/* if the "alt" is the same as the CID, as Jabbim does,
					 this prevents linkification... */
					if (purple_email_is_valid(alt)) {
						gchar *safe_alt = g_strdup_printf("smiley:%s", alt);
						out = g_string_append(out, safe_alt);
						g_free(safe_alt);
					} else {
						gchar *alt_escaped = g_markup_escape_text(alt, -1);
						out = g_string_append(out, alt_escaped);
						g_free(alt_escaped);
					}
				} else {
					out = g_string_append(out, src);
				}
				pos += pos2 - pos;
			} else {
				out = g_string_append_c(out, markup[pos]);
				pos++;
			}

			purple_xmlnode_free(img);

		} else {
			out = g_string_append_c(out, markup[pos]);
			pos++;
		}
	}

	g_free(markup);
	return g_string_free(out, FALSE);
}

static void
jabber_message_add_remote_smileys(JabberStream *js, const gchar *who,
	const PurpleXmlNode *message)
{
	PurpleXmlNode *data_tag;
	for (data_tag = purple_xmlnode_get_child_with_namespace(message, "data", NS_BOB) ;
		 data_tag ;
		 data_tag = purple_xmlnode_get_next_twin(data_tag)) {
		const gchar *cid = purple_xmlnode_get_attrib(data_tag, "cid");
		const JabberData *data = jabber_data_find_remote_by_cid(js, who, cid);

		if (!data && cid != NULL) {
			/* we haven't cached this already, let's add it */
			JabberData *new_data = jabber_data_create_from_xml(data_tag);

			if (new_data) {
				jabber_data_associate_remote(js, who, new_data);
			}
		}
	}
}

static void
jabber_message_remote_smiley_got(JabberData *jdata, gchar *alt, gpointer d) {
	JabberMessageRemoteSmileyAddData *data = (JabberMessageRemoteSmileyAddData *)d;

	if (jdata) {
		PurpleSmiley *smiley = NULL;

		purple_debug_info("jabber",
			"smiley data retrieved successfully");

		smiley = purple_smiley_new_from_data(
			data->shortcut,
			jabber_data_get_data(jdata),
			jabber_data_get_size(jdata)
		);

		purple_conversation_add_smiley(data->conv, smiley);

		g_object_unref(G_OBJECT(smiley));
	} else {
		purple_debug_error("jabber", "failed retrieving smiley data");
	}

	g_object_unref(G_OBJECT(data->conv));
	g_free(data->shortcut);
	g_slice_free(JabberMessageRemoteSmileyAddData, data);
}

static void
jabber_message_remote_smiley_add(JabberStream *js, PurpleConversation *conv,
	const gchar *from, const gchar *shortcut, const gchar *cid)
{
	PurpleSmiley *smiley = NULL;
	const JabberData *jdata = NULL;

	purple_debug_misc("jabber", "about to add remote smiley %s to the conv",
		shortcut);

	smiley = purple_conversation_get_smiley(conv, shortcut);
	if(PURPLE_IS_SMILEY(smiley)) {
		purple_debug_misc("jabber", "smiley was already present");
		return;
	}

	/* TODO: cache lookup by "cid" */
	jdata = jabber_data_find_remote_by_cid(js, from, cid);
	if (jdata) {
		purple_debug_info("jabber", "smiley data is already known");

		smiley = purple_smiley_new_from_data(
			shortcut,
			jabber_data_get_data(jdata),
			jabber_data_get_size(jdata)
		);

		purple_conversation_add_smiley(conv, smiley);

		g_object_unref(G_OBJECT(smiley));
	} else {
		JabberMessageRemoteSmileyAddData *data = NULL;

		data = g_slice_new(JabberMessageRemoteSmileyAddData);
		data->conv = g_object_ref(conv);
		data->shortcut = g_strdup(shortcut);

		purple_debug_info("jabber", "smiley data is unknown, "
			"need to request it");

		jabber_data_request(js, cid, from, NULL, FALSE,
			jabber_message_remote_smiley_got, data);
	}
}

void jabber_message_parse(JabberStream *js, PurpleXmlNode *packet)
{
	JabberMessage *jm;
	const char *id, *from, *to, *type;
	PurpleXmlNode *child = NULL, *received = NULL;
	gboolean signal_return;
	gboolean delayed = FALSE, is_outgoing = FALSE, is_forwarded = FALSE;
	time_t timestamp = time(NULL);

	/* Check if we have a carbons received element from our own account. */
	from = purple_xmlnode_get_attrib(packet, "from");
	if(from != NULL && jabber_is_own_account(js, from)) {
		PurpleXmlNode *forwarded = NULL;

		/* We check if this is a received carbon first. */
		received = purple_xmlnode_get_child_with_namespace(packet, "received",
		                                                   NS_MESSAGE_CARBONS);
		if(received != NULL) {
			forwarded = purple_xmlnode_get_child_with_namespace(received,
			                                                    "forwarded",
			                                                    NS_FORWARD);
		} else {
			PurpleXmlNode *sent = NULL;

			sent = purple_xmlnode_get_child_with_namespace(packet, "sent",
			                                               NS_MESSAGE_CARBONS);
			if(sent != NULL) {
				forwarded = purple_xmlnode_get_child_with_namespace(sent,
				                                                    "forwarded",
				                                                    NS_FORWARD);
				is_outgoing = TRUE;
			}
		}

		if(forwarded != NULL) {
			PurpleXmlNode *fwd_msg = NULL;

			fwd_msg = purple_xmlnode_get_child_with_namespace(forwarded,
			                                                  "message",
			                                                  NS_XMPP_CLIENT);
			if(fwd_msg != NULL) {
				PurpleXmlNode *delay = NULL;

				/* We have a forwarded message, so update the packet to point
				 * to it directly.
				 */
				packet = fwd_msg;
				is_forwarded = TRUE;

				/* Now check if it was a delayed message and if so, grab the
				 * timestamp that the server sent.
				 */
				delay = purple_xmlnode_get_child_with_namespace(forwarded,
				                                                "delay",
				                                                NS_DELAYED_DELIVERY);
				if(delay != NULL) {
					const gchar *ts = purple_xmlnode_get_attrib(delay,
					                                            "stamp");

					timestamp = purple_str_to_time(ts, TRUE, NULL, NULL, NULL);
					delayed = TRUE;
				}
			}
		}
	}

	/* If the message was forwarded, packet is now pointing to the forwarded
	 * message.
	 */
	from = purple_xmlnode_get_attrib(packet, "from");
	id = purple_xmlnode_get_attrib(packet, "id");
	to = purple_xmlnode_get_attrib(packet, "to");
	type = purple_xmlnode_get_attrib(packet, "type");

	signal_return = GPOINTER_TO_INT(purple_signal_emit_return_1(purple_connection_get_protocol(js->gc),
			"jabber-receiving-message", js->gc, type, id, from, to, packet));
	if (signal_return)
		return;

	jm = g_new0(JabberMessage, 1);
	jm->js = js;
	jm->sent = timestamp;
	jm->delayed = delayed;
	jm->chat_state = JM_STATE_NONE;
	jm->forwarded = is_forwarded;
	jm->outgoing = is_outgoing;

	if(type) {
		if(purple_strequal(type, "normal"))
			jm->type = JABBER_MESSAGE_NORMAL;
		else if(purple_strequal(type, "chat"))
			jm->type = JABBER_MESSAGE_CHAT;
		else if(purple_strequal(type, "groupchat"))
			jm->type = JABBER_MESSAGE_GROUPCHAT;
		else if(purple_strequal(type, "headline"))
			jm->type = JABBER_MESSAGE_HEADLINE;
		else if(purple_strequal(type, "error"))
			jm->type = JABBER_MESSAGE_ERROR;
		else
			jm->type = JABBER_MESSAGE_OTHER;
	} else {
		jm->type = JABBER_MESSAGE_NORMAL;
	}

	jm->from = g_strdup(from);
	jm->to   = g_strdup(to);
	jm->id   = g_strdup(id);

	for(child = packet->child; child; child = child->next) {
		const char *xmlns = purple_xmlnode_get_namespace(child);
		if(child->type != PURPLE_XMLNODE_TYPE_TAG)
			continue;

		if(purple_strequal(child->name, "error")) {
			const char *code = purple_xmlnode_get_attrib(child, "code");
			char *code_txt = NULL;
			char *text = purple_xmlnode_get_data(child);
			if (!text) {
				PurpleXmlNode *enclosed_text_node;

				if ((enclosed_text_node = purple_xmlnode_get_child(child, "text")))
					text = purple_xmlnode_get_data(enclosed_text_node);
			}

			if(code)
				code_txt = g_strdup_printf(_("(Code %s)"), code);

			if(!jm->error)
				jm->error = g_strdup_printf("%s%s%s",
						text ? text : "",
						text && code_txt ? " " : "",
						code_txt ? code_txt : "");

			g_free(code_txt);
			g_free(text);
		} else if (xmlns == NULL) {
			/* QuLogic: Not certain this is correct, but it would have happened
			   with the previous code. */
			if(purple_strequal(child->name, "x"))
				jm->etc = g_list_append(jm->etc, child);
			/* The following tests expect xmlns != NULL */
			continue;
		} else if(purple_strequal(child->name, "subject") && purple_strequal(xmlns, NS_XMPP_CLIENT)) {
			if(!jm->subject) {
				jm->subject = purple_xmlnode_get_data(child);
				if(!jm->subject)
					jm->subject = g_strdup("");
			}
		} else if(purple_strequal(child->name, "thread") && purple_strequal(xmlns, NS_XMPP_CLIENT)) {
			if(!jm->thread_id)
				jm->thread_id = purple_xmlnode_get_data(child);
		} else if(purple_strequal(child->name, "body") && purple_strequal(xmlns, NS_XMPP_CLIENT)) {
			if(!jm->body) {
				char *msg = purple_xmlnode_get_data(child);
				char *escaped = purple_markup_escape_text(msg, -1);
				jm->body = purple_strdup_withhtml(escaped);
				g_free(escaped);
				g_free(msg);
			}
		} else if(purple_strequal(child->name, "html") && purple_strequal(xmlns, NS_XHTML_IM)) {
			if(!jm->xhtml && purple_xmlnode_get_child(child, "body")) {
				char *c;

				PurpleConnection *gc = js->gc;
				PurpleAccount *account = purple_connection_get_account(gc);
				PurpleConversation *conv = NULL;
				GList *smiley_refs = NULL;
				gchar *reformatted_xhtml;

				if (purple_account_get_bool(account, "custom_smileys", TRUE)) {
					/* find a list of smileys ("cid" and "alt" text pairs)
					  occuring in the message */
					smiley_refs = jabber_message_get_refs_from_xmlnode(child);
					purple_debug_info("jabber", "found %d smileys\n",
						g_list_length(smiley_refs));

					if (smiley_refs) {
						if (jm->type == JABBER_MESSAGE_GROUPCHAT) {
							JabberID *jid = jabber_id_new(jm->from);
							JabberChat *chat = NULL;

							if (jid) {
								chat = jabber_chat_find(js, jid->node, jid->domain);
								if (chat)
									conv = PURPLE_CONVERSATION(chat->conv);
								jabber_id_free(jid);
							}
						} else if (jm->type == JABBER_MESSAGE_NORMAL ||
						           jm->type == JABBER_MESSAGE_CHAT) {
							conv =
								purple_conversations_find_with_account(from, account);
							if (!conv) {
								/* we need to create the conversation here */
								conv = PURPLE_CONVERSATION(
									purple_im_conversation_new(account, from));
							}
						}
					}

					/* process any newly provided smileys */
					jabber_message_add_remote_smileys(js, to, packet);
				}

				purple_xmlnode_strip_prefixes(child);

				/* reformat xhtml so that img tags with a "cid:" src gets
				  translated to the bare text of the emoticon (the "alt" attrib) */
				/* this is done also when custom smiley retrieval is turned off,
				  this way the receiver always sees the shortcut instead */
				reformatted_xhtml =
					jabber_message_xml_to_string_strip_img_smileys(child);

				jm->xhtml = reformatted_xhtml;

				/* add known custom emoticons to the conversation */
				/* note: if there were no smileys in the incoming message, or
				  	if receiving custom smileys is turned off, smiley_refs will
					be NULL */
				if (conv) {
					for (GList *it = smiley_refs; it; it = g_list_next(it)) {
						PurpleKeyValuePair *ref = it->data;

						jabber_message_remote_smiley_add(js,
						        conv, from, ref->value, ref->key);
					}
				}
				g_list_free_full(smiley_refs, (GDestroyNotify)purple_key_value_pair_free);

			    /* Convert all newlines to whitespace. Technically, even regular, non-XML HTML is supposed to ignore newlines, but Pidgin has, as convention
				 * treated \n as a newline for compatibility with other protocols
				 */
				for (c = jm->xhtml; *c != '\0'; c++) {
					if (*c == '\n')
						*c = ' ';
				}
			}
		} else if(purple_strequal(child->name, "active") && purple_strequal(xmlns,"http://jabber.org/protocol/chatstates")) {
			jm->chat_state = JM_STATE_ACTIVE;
		} else if(purple_strequal(child->name, "composing") && purple_strequal(xmlns,"http://jabber.org/protocol/chatstates")) {
			jm->chat_state = JM_STATE_COMPOSING;
		} else if(purple_strequal(child->name, "paused") && purple_strequal(xmlns,"http://jabber.org/protocol/chatstates")) {
			jm->chat_state = JM_STATE_PAUSED;
		} else if(purple_strequal(child->name, "inactive") && purple_strequal(xmlns,"http://jabber.org/protocol/chatstates")) {
			jm->chat_state = JM_STATE_INACTIVE;
		} else if(purple_strequal(child->name, "gone") && purple_strequal(xmlns,"http://jabber.org/protocol/chatstates")) {
			jm->chat_state = JM_STATE_GONE;
		} else if(purple_strequal(child->name, "event") && purple_strequal(xmlns,"http://jabber.org/protocol/pubsub#event")) {
			PurpleXmlNode *items;
			jm->type = JABBER_MESSAGE_EVENT;
			for(items = purple_xmlnode_get_child(child,"items"); items; items = items->next)
				jm->eventitems = g_list_append(jm->eventitems, items);
		} else if(purple_strequal(child->name, "attention") && purple_strequal(xmlns, NS_ATTENTION)) {
			jm->hasBuzz = TRUE;
		} else if(purple_strequal(child->name, "delay") && purple_strequal(xmlns, NS_DELAYED_DELIVERY)) {
			const char *timestamp = purple_xmlnode_get_attrib(child, "stamp");
			jm->delayed = TRUE;
			if(timestamp)
				jm->sent = purple_str_to_time(timestamp, TRUE, NULL, NULL, NULL);
		} else if(purple_strequal(child->name, "x")) {
			if(purple_strequal(xmlns, NS_DELAYED_DELIVERY_LEGACY)) {
				const char *timestamp = purple_xmlnode_get_attrib(child, "stamp");
				jm->delayed = TRUE;
				if(timestamp)
					jm->sent = purple_str_to_time(timestamp, TRUE, NULL, NULL, NULL);
			} else if(purple_strequal(xmlns, "jabber:x:conference") &&
					jm->type != JABBER_MESSAGE_GROUPCHAT_INVITE &&
					jm->type != JABBER_MESSAGE_ERROR) {
				const char *jid = purple_xmlnode_get_attrib(child, "jid");
				if(jid) {
					const char *reason = purple_xmlnode_get_attrib(child, "reason");
					const char *password = purple_xmlnode_get_attrib(child, "password");

					jm->type = JABBER_MESSAGE_GROUPCHAT_INVITE;
					g_free(jm->to);
					jm->to = g_strdup(jid);

					if (reason) {
						g_free(jm->body);
						jm->body = g_strdup(reason);
					}

					if (password) {
						g_free(jm->password);
						jm->password = g_strdup(password);
					}
				}
			} else if(purple_strequal(xmlns, "http://jabber.org/protocol/muc#user") &&
					jm->type != JABBER_MESSAGE_ERROR) {
				PurpleXmlNode *invite = purple_xmlnode_get_child(child, "invite");
				if(invite) {
					PurpleXmlNode *reason, *password;
					const char *jid = purple_xmlnode_get_attrib(invite, "from");
					g_free(jm->to);
					jm->to = jm->from;
					jm->from = g_strdup(jid);
					if((reason = purple_xmlnode_get_child(invite, "reason"))) {
						g_free(jm->body);
						jm->body = purple_xmlnode_get_data(reason);
					}
					if((password = purple_xmlnode_get_child(child, "password"))) {
						g_free(jm->password);
						jm->password = purple_xmlnode_get_data(password);
					}

					jm->type = JABBER_MESSAGE_GROUPCHAT_INVITE;
				}
			} else {
				jm->etc = g_list_append(jm->etc, child);
			}
		} else if (purple_strequal(child->name, "query")) {
			const char *node = purple_xmlnode_get_attrib(child, "node");
			if (purple_strequal(xmlns, NS_DISCO_ITEMS)
					&& purple_strequal(node, "http://jabber.org/protocol/commands")) {
				jabber_adhoc_got_list(js, jm->from, child);
			}
		}
	}

	if(jm->hasBuzz)
		handle_buzz(jm);

	switch(jm->type) {
		case JABBER_MESSAGE_OTHER:
			purple_debug_info("jabber",
					"Received message of unknown type: %s\n", type);
			/* Fall-through is intentional */
		case JABBER_MESSAGE_NORMAL:
		case JABBER_MESSAGE_CHAT:
			handle_chat(jm);
			break;
		case JABBER_MESSAGE_HEADLINE:
			handle_headline(jm);
			break;
		case JABBER_MESSAGE_GROUPCHAT:
			handle_groupchat(jm);
			break;
		case JABBER_MESSAGE_GROUPCHAT_INVITE:
			handle_groupchat_invite(jm);
			break;
		case JABBER_MESSAGE_EVENT:
			jabber_handle_event(jm);
			break;
		case JABBER_MESSAGE_ERROR:
			handle_error(jm);
			break;
	}
	jabber_message_free(jm);
}

static gboolean
jabber_conv_support_custom_smileys(JabberStream *js,
	PurpleConversation *conv, const gchar *who)
{
	JabberBuddy *jb;
	JabberChat *chat;

	if (PURPLE_IS_IM_CONVERSATION(conv)) {
		jb = jabber_buddy_find(js, who, FALSE);
		if (jb) {
			return jabber_buddy_has_capability(jb, NS_BOB);
		} else {
			return FALSE;
		}
	} else if (PURPLE_IS_CHAT_CONVERSATION(conv)) {
		chat = jabber_chat_find_by_conv(PURPLE_CHAT_CONVERSATION(conv));
		if (chat) {
			/* do not attempt to send custom smileys in a MUC with more than
			 10 people, to avoid getting too many BoB requests */
			return jabber_chat_get_num_participants(chat) <= 10 &&
				jabber_chat_all_participants_have_capability(chat,
					NS_BOB);
		} else {
			return FALSE;
		}
	} else {
		return FALSE;
	}
}

static gboolean
jabber_message_smileyify_cb(GString *out, PurpleSmiley *smiley,
	PurpleConversation *_empty, gpointer _unused)
{
	const gchar *shortcut;
	const JabberData *data;
	PurpleXmlNode *smiley_node;
	gchar *node_xml;

	shortcut = purple_smiley_get_shortcut(smiley);
	data = jabber_data_find_local_by_alt(shortcut);

	if (!data)
		return FALSE;

	smiley_node = jabber_data_get_xhtml_im(data, shortcut);
	node_xml = purple_xmlnode_to_str(smiley_node, NULL);

	g_string_append(out, node_xml);

	purple_xmlnode_free(smiley_node);
	g_free(node_xml);

	return TRUE;
}

static char *
jabber_message_smileyfy_xhtml(JabberMessage *jm, const char *xhtml)
{
	PurpleAccount *account = purple_connection_get_account(jm->js->gc);
	GList *found_smileys, *it, *it_next;
	PurpleConversation *conv;
	gboolean has_too_large_smiley = FALSE;
	gchar *smileyfied_xhtml = NULL;

	conv = purple_conversations_find_with_account(jm->to, account);

	if (!jabber_conv_support_custom_smileys(jm->js, conv, jm->to))
		return NULL;

	found_smileys = purple_smiley_parser_find(
		purple_smiley_custom_get_list(), xhtml, TRUE);
	if (!found_smileys)
		return NULL;

	for (it = found_smileys; it; it = it_next) {
		PurpleSmiley *smiley = it->data;
		gboolean valid = TRUE;

		it_next = g_list_next(it);

		if (purple_image_get_data_size(PURPLE_IMAGE(smiley)) > JABBER_DATA_MAX_SIZE) {
			has_too_large_smiley = TRUE;
			valid = FALSE;
			purple_debug_warning("jabber", "Refusing to send "
				"smiley %s (too large, max is %d)",
				purple_smiley_get_shortcut(smiley),
				JABBER_DATA_MAX_SIZE);
		}

		if (!valid)
			found_smileys = g_list_delete_link(found_smileys, it);
	}

	if (has_too_large_smiley) {
		purple_conversation_write_system_message(conv,
			_("A custom smiley in the message is too large to send."),
			PURPLE_MESSAGE_ERROR);
	}

	if (!found_smileys)
		return NULL;

	for (it = found_smileys; it; it = g_list_next(it)) {
		PurpleSmiley *smiley = it->data;
		const gchar *shortcut = purple_smiley_get_shortcut(smiley);
		const gchar *mimetype;
		JabberData *jdata;

		/* the object has been sent before */
		if (jabber_data_find_local_by_alt(shortcut))
			continue;

		mimetype = purple_image_get_mimetype(PURPLE_IMAGE(smiley));
		if (!mimetype) {
			purple_debug_error("jabber",
				"unknown mime type for image");
			continue;
		}

		jdata = jabber_data_create_from_data(
			purple_image_get_data(PURPLE_IMAGE(smiley)),
			purple_image_get_data_size(PURPLE_IMAGE(smiley)),
			mimetype, FALSE, jm->js);

		purple_debug_info("jabber", "cache local smiley alt=%s, cid=%s",
			shortcut, jabber_data_get_cid(jdata));
		jabber_data_associate_local(jdata, shortcut);
	}

	g_list_free(found_smileys);

	smileyfied_xhtml = purple_smiley_parser_replace(
		purple_smiley_custom_get_list(), xhtml,
		jabber_message_smileyify_cb, NULL);

	return smileyfied_xhtml;
}

void jabber_message_send(JabberMessage *jm)
{
	PurpleXmlNode *message, *child;
	const char *type = NULL;

	message = purple_xmlnode_new("message");

	switch(jm->type) {
		case JABBER_MESSAGE_NORMAL:
			type = "normal";
			break;
		case JABBER_MESSAGE_CHAT:
		case JABBER_MESSAGE_GROUPCHAT_INVITE:
			type = "chat";
			break;
		case JABBER_MESSAGE_HEADLINE:
			type = "headline";
			break;
		case JABBER_MESSAGE_GROUPCHAT:
			type = "groupchat";
			break;
		case JABBER_MESSAGE_ERROR:
			type = "error";
			break;
		case JABBER_MESSAGE_OTHER:
		default:
			type = NULL;
			break;
	}

	if(type)
		purple_xmlnode_set_attrib(message, "type", type);

	if (jm->id)
		purple_xmlnode_set_attrib(message, "id", jm->id);

	purple_xmlnode_set_attrib(message, "to", jm->to);

	if(jm->thread_id) {
		child = purple_xmlnode_new_child(message, "thread");
		purple_xmlnode_insert_data(child, jm->thread_id, -1);
	}

	child = NULL;
	switch(jm->chat_state)
	{
		case JM_STATE_ACTIVE:
			child = purple_xmlnode_new_child(message, "active");
			break;
		case JM_STATE_COMPOSING:
			child = purple_xmlnode_new_child(message, "composing");
			break;
		case JM_STATE_PAUSED:
			child = purple_xmlnode_new_child(message, "paused");
			break;
		case JM_STATE_INACTIVE:
			child = purple_xmlnode_new_child(message, "inactive");
			break;
		case JM_STATE_GONE:
			child = purple_xmlnode_new_child(message, "gone");
			break;
		case JM_STATE_NONE:
			/* yep, nothing */
			break;
	}
	if(child)
		purple_xmlnode_set_namespace(child, "http://jabber.org/protocol/chatstates");

	if(jm->subject) {
		child = purple_xmlnode_new_child(message, "subject");
		purple_xmlnode_insert_data(child, jm->subject, -1);
	}

	if(jm->body) {
		child = purple_xmlnode_new_child(message, "body");
		purple_xmlnode_insert_data(child, jm->body, -1);
	}

	if(jm->xhtml) {
		if ((child = purple_xmlnode_from_str(jm->xhtml, -1))) {
			purple_xmlnode_insert_child(message, child);
		} else {
			purple_debug_error("jabber",
					"XHTML translation/validation failed, returning: %s\n",
					jm->xhtml);
		}
	}

	jabber_send(jm->js, message);

	purple_xmlnode_free(message);
}

/*
 * Compare the XHTML and plain strings passed in for "equality". Any HTML markup
 * other than <br/> (matches a newline) in the XHTML will cause this to return
 * FALSE.
 */
static gboolean
jabber_xhtml_plain_equal(const char *xhtml_escaped, const char *plain)
{
	int i = 0;
	int j = 0;
	gboolean ret;
	char *xhtml = purple_unescape_html(xhtml_escaped);

	while (xhtml[i] && plain[j]) {
		if (xhtml[i] == plain[j]) {
			i += 1;
			j += 1;
			continue;
		}

		if (plain[j] == '\n' && !strncmp(xhtml+i, "<br/>", 5)) {
			i += 5;
			j += 1;
			continue;
		}

		g_free(xhtml);
		return FALSE;
	}

	/* Are we at the end of both strings? */
	ret = (xhtml[i] == plain[j]) && (xhtml[i] == '\0');
	g_free(xhtml);
	return ret;
}

int jabber_message_send_im(PurpleProtocolIM *pim, PurpleConnection *gc,
                           PurpleMessage *msg)
{
	JabberMessage *jm;
	JabberBuddy *jb;
	JabberBuddyResource *jbr;
	char *xhtml;
	char *tmp;
	char *resource;
	const gchar *rcpt = purple_message_get_recipient(msg);

	if (!rcpt || purple_message_is_empty(msg))
		return 0;

	resource = jabber_get_resource(rcpt);

	jb = jabber_buddy_find(purple_connection_get_protocol_data(gc), rcpt, TRUE);
	jbr = jabber_buddy_find_resource(jb, resource);

	g_free(resource);

	jm = g_new0(JabberMessage, 1);
	jm->js = purple_connection_get_protocol_data(gc);
	jm->type = JABBER_MESSAGE_CHAT;
	jm->chat_state = JM_STATE_ACTIVE;
	jm->to = g_strdup(rcpt);
	jm->id = jabber_get_next_id(jm->js);

	if(jbr) {
		if(jbr->thread_id)
			jm->thread_id = jbr->thread_id;

		if (jbr->chat_states == JABBER_CHAT_STATES_UNSUPPORTED)
			jm->chat_state = JM_STATE_NONE;
		else {
			/* if(JABBER_CHAT_STATES_UNKNOWN == jbr->chat_states)
			   jbr->chat_states = JABBER_CHAT_STATES_UNSUPPORTED; */
		}
	}

	tmp = purple_utf8_strip_unprintables(purple_message_get_contents(msg));
	purple_markup_html_to_xhtml(tmp, &xhtml, &jm->body);
	g_free(tmp);

	tmp = jabber_message_smileyfy_xhtml(jm, xhtml);
	if (tmp) {
		g_free(xhtml);
		xhtml = tmp;
	}

	/*
	 * For backward compatibility with user expectations or for those not on
	 * the user's roster, allow sending XHTML-IM markup.
	 */
	if (!jbr || !jbr->caps.info ||
			jabber_resource_has_capability(jbr, NS_XHTML_IM)) {
		if (!jabber_xhtml_plain_equal(xhtml, jm->body))
			/* Wrap the message in <p/> for great interoperability justice. */
			jm->xhtml = g_strdup_printf("<html xmlns='" NS_XHTML_IM "'><body xmlns='" NS_XHTML "'><p>%s</p></body></html>", xhtml);
	}

	g_free(xhtml);

	jabber_message_send(jm);
	jabber_message_free(jm);
	return 1;
}

gint
jabber_message_send_chat(PurpleProtocolChat *protocol_chat,
                         PurpleConnection *gc, gint id, PurpleMessage *msg)
{
	JabberChat *chat;
	JabberMessage *jm;
	JabberStream *js;
	char *xhtml;
	char *tmp;

	if (!gc || purple_message_is_empty(msg))
		return 0;

	js = purple_connection_get_protocol_data(gc);
	chat = jabber_chat_find_by_id(js, id);

	if(!chat)
		return 0;

	jm = g_new0(JabberMessage, 1);
	jm->js = purple_connection_get_protocol_data(gc);
	jm->type = JABBER_MESSAGE_GROUPCHAT;
	jm->to = g_strdup_printf("%s@%s", chat->room, chat->server);
	jm->id = jabber_get_next_id(jm->js);

	tmp = purple_utf8_strip_unprintables(purple_message_get_contents(msg));
	purple_markup_html_to_xhtml(tmp, &xhtml, &jm->body);
	g_free(tmp);
	tmp = jabber_message_smileyfy_xhtml(jm, xhtml);
	if (tmp) {
		g_free(xhtml);
		xhtml = tmp;
	}

	if (chat->xhtml && !jabber_xhtml_plain_equal(xhtml, jm->body))
		/* Wrap the message in <p/> for greater interoperability justice. */
		jm->xhtml = g_strdup_printf("<html xmlns='" NS_XHTML_IM "'><body xmlns='" NS_XHTML "'><p>%s</p></body></html>", xhtml);

	g_free(xhtml);

	jabber_message_send(jm);
	jabber_message_free(jm);

	return 1;
}

unsigned int jabber_send_typing(PurpleProtocolIM *pim, PurpleConnection *gc,
                                const char *who, PurpleIMTypingState state)
{
	JabberStream *js;
	JabberMessage *jm;
	JabberBuddy *jb;
	JabberBuddyResource *jbr;
	char *resource;

	js = purple_connection_get_protocol_data(gc);
	jb = jabber_buddy_find(js, who, TRUE);
	if (!jb)
		return 0;

	resource = jabber_get_resource(who);
	jbr = jabber_buddy_find_resource(jb, resource);
	g_free(resource);

	/* We know this entity doesn't support chat states */
	if (jbr && jbr->chat_states == JABBER_CHAT_STATES_UNSUPPORTED)
		return 0;

	/* *If* we don't have presence /and/ the buddy can't see our
	 * presence, don't send typing notifications.
	 */
	if (!jbr && !(jb->subscription & JABBER_SUB_FROM))
		return 0;

	/* TODO: figure out threading */
	jm = g_new0(JabberMessage, 1);
	jm->js = js;
	jm->type = JABBER_MESSAGE_CHAT;
	jm->to = g_strdup(who);
	jm->id = jabber_get_next_id(jm->js);

	if(PURPLE_IM_TYPING == state)
		jm->chat_state = JM_STATE_COMPOSING;
	else if(PURPLE_IM_TYPED == state)
		jm->chat_state = JM_STATE_PAUSED;
	else
		jm->chat_state = JM_STATE_ACTIVE;

	/* if(JABBER_CHAT_STATES_UNKNOWN == jbr->chat_states)
		jbr->chat_states = JABBER_CHAT_STATES_UNSUPPORTED; */

	jabber_message_send(jm);
	jabber_message_free(jm);

	return 0;
}

gboolean jabber_buzz_isenabled(JabberStream *js, const gchar *namespace) {
	return js->allowBuzz;
}

gboolean jabber_custom_smileys_isenabled(JabberStream *js, const gchar *namespace)
{
	PurpleAccount *account = purple_connection_get_account(js->gc);

	return purple_account_get_bool(account, "custom_smileys", TRUE);
}