summaryrefslogtreecommitdiff
path: root/protocols/JabberG/src/jabber_groupchat.cpp
blob: bb5921e1c30c326a84f32f078a72517675da0f1a (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
/*

Jabber Protocol Plugin for Miranda IM
Copyright (C) 2002-04  Santithorn Bunchua
Copyright (C) 2005-12  George Hazan
Copyright (C) 2012-13  Miranda NG Project

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.

*/

#include "jabber.h"
#include "jabber_iq.h"
#include "jabber_caps.h"

#define GC_SERVER_LIST_SIZE 5

int JabberGcGetStatus(JABBER_GC_AFFILIATION a, JABBER_GC_ROLE r);
int JabberGcGetStatus(JABBER_RESOURCE_STATUS *r);

struct JabberGcRecentInfo
{
	TCHAR *room, *server, *nick, *password;
	CJabberProto *ppro;

	JabberGcRecentInfo(CJabberProto* proto)
	{
		ppro = proto;
		room = server = nick = password = NULL;
	}
	JabberGcRecentInfo(CJabberProto* proto, const TCHAR *_room, const TCHAR *_server, const TCHAR *_nick = NULL, const TCHAR *_password = NULL)
	{
		ppro = proto;
		room = server = nick = password = NULL;
		fillData(_room, _server, _nick, _password);
	}
	JabberGcRecentInfo(CJabberProto* proto, const TCHAR *jid)
	{
		ppro = proto;
		room = server = nick = password = NULL;
		fillData(jid);
	}
	JabberGcRecentInfo(CJabberProto* proto, int iRecent)
	{
		ppro = proto;
		room = server = nick = password = NULL;
		loadRecent(iRecent);
	}

	~JabberGcRecentInfo()
	{
		cleanup();
	}

	void cleanup()
	{
		mir_free(room);
		mir_free(server);
		mir_free(nick);
		mir_free(password);
		room = server = nick = password = NULL;
	}

	BOOL equals(const TCHAR *room, const TCHAR *server, const TCHAR *nick = NULL, const TCHAR *password = NULL)
	{
		return
			null_strequals(this->room, room) &&
			null_strequals(this->server, server) &&
			null_strequals(this->nick, nick) &&
			null_strequals(this->password, password);
	}

	BOOL equalsnp(const TCHAR *room, const TCHAR *server, const TCHAR *nick = NULL)
	{
		return
			null_strequals(this->room, room) &&
			null_strequals(this->server, server) &&
			null_strequals(this->nick, nick);
	}

	void fillForm(HWND hwndDlg)
	{
		SetDlgItemText(hwndDlg, IDC_SERVER, server ? server : _T(""));
		SetDlgItemText(hwndDlg, IDC_ROOM, room ? room : _T(""));
		SetDlgItemText(hwndDlg, IDC_NICK, nick ? nick : _T(""));
		SetDlgItemText(hwndDlg, IDC_PASSWORD, password ? password : _T(""));
	}

	void fillData(const TCHAR *room, const TCHAR *server, const TCHAR *nick = NULL, const TCHAR *password = NULL)
	{
		cleanup();
		this->room		= room		? mir_tstrdup(room)		: NULL;
		this->server	= server	? mir_tstrdup(server)	: NULL;
		this->nick		= nick		? mir_tstrdup(nick)		: NULL;
		this->password	= password	? mir_tstrdup(password)	: NULL;
	}

	void fillData(const TCHAR *jid)
	{
		TCHAR *room, *server, *nick=NULL;
		room = NEWTSTR_ALLOCA(jid);
		server = _tcschr(room, _T('@'));
		if (server)
		{
			*server++ = 0;
			nick = _tcschr(server, _T('/'));
			if (nick) *nick++ = 0;
		} else
		{
			server = room;
			room = NULL;
		}

		fillData(room, server, nick);
	}

	BOOL loadRecent(int iRecent)
	{
		DBVARIANT dbv;
		char setting[MAXMODULELABELLENGTH];

		cleanup();

		mir_snprintf(setting, sizeof(setting), "rcMuc_%d_server", iRecent);
		if ( !ppro->getTString(setting, &dbv)) {
			server = mir_tstrdup(dbv.ptszVal);
			db_free(&dbv);
		}

		mir_snprintf(setting, sizeof(setting), "rcMuc_%d_room", iRecent);
		if ( !ppro->getTString(setting, &dbv)) {
			room = mir_tstrdup(dbv.ptszVal);
			db_free(&dbv);
		}

		mir_snprintf(setting, sizeof(setting), "rcMuc_%d_nick", iRecent);
		if ( !ppro->getTString(setting, &dbv)) {
			nick = mir_tstrdup(dbv.ptszVal);
			db_free(&dbv);
		}

		mir_snprintf(setting, sizeof(setting), "rcMuc_%d_passwordW", iRecent);
		password = ppro->JGetStringCrypt(NULL, setting);

		return room || server || nick || password;
	}

	void saveRecent(int iRecent)
	{
		char setting[MAXMODULELABELLENGTH];

		mir_snprintf(setting, sizeof(setting), "rcMuc_%d_server", iRecent);
		if (server)
			ppro->setTString(setting, server);
		else
			ppro->delSetting(setting);

		mir_snprintf(setting, sizeof(setting), "rcMuc_%d_room", iRecent);
		if (room)
			ppro->setTString(setting, room);
		else
			ppro->delSetting(setting);

		mir_snprintf(setting, sizeof(setting), "rcMuc_%d_nick", iRecent);
		if (nick)
			ppro->setTString(setting, nick);
		else
			ppro->delSetting(setting);

		mir_snprintf(setting, sizeof(setting), "rcMuc_%d_passwordW", iRecent);
		if (password)
			ppro->JSetStringCrypt(NULL, setting, password);
		else
			ppro->delSetting(setting);
	}

private:
	BOOL null_strequals(const TCHAR *str1, const TCHAR *str2)
	{
		if ( !str1 && !str2) return TRUE;
		if ( !str1 && str2 && !*str2) return TRUE;
		if ( !str2 && str1 && !*str1) return TRUE;

		if ( !str1 && str2) return FALSE;
		if ( !str2 && str1) return FALSE;

		return !lstrcmp(str1, str2);
	}
};

INT_PTR __cdecl CJabberProto::OnMenuHandleJoinGroupchat(WPARAM, LPARAM)
{
	if (jabberChatDllPresent)
		GroupchatJoinRoomByJid(NULL, NULL);
	else
		JabberChatDllError();
	return 0;
}

INT_PTR __cdecl CJabberProto::OnJoinChat(WPARAM wParam, LPARAM)
{
	DBVARIANT nick, jid;
	HANDLE hContact = (HANDLE)wParam;
	if (getTString(hContact, "ChatRoomID", &jid))
		return 0;

	if (getTString(hContact, "MyNick", &nick))
		if (getTString("Nick", &nick)) {
			db_free(&jid);
			return 0;
		}

	TCHAR *password = JGetStringCrypt(hContact, "LoginPassword");

	if (getWord(hContact, "Status", 0) != ID_STATUS_ONLINE) {
		if ( !jabberChatDllPresent)
			JabberChatDllError();
		else {
			TCHAR *p = _tcschr(jid.ptszVal, '@');
			if (p != NULL) {
				*p++ = 0;
				GroupchatJoinRoom(p, jid.ptszVal, nick.ptszVal, password);
	}	}	}

	mir_free(password);
	db_free(&nick);
	db_free(&jid);
	return 0;
}

INT_PTR __cdecl CJabberProto::OnLeaveChat(WPARAM wParam, LPARAM)
{
	DBVARIANT jid;
	HANDLE hContact = (HANDLE)wParam;
	if (getTString(hContact, "ChatRoomID", &jid))
		return 0;

	if (getWord(hContact, "Status", 0) != ID_STATUS_OFFLINE) {
		JABBER_LIST_ITEM *item = ListGetItemPtr(LIST_CHATROOM, jid.ptszVal);
		if (item != NULL)
			GcQuit(item, 0, NULL);
	}

	db_free(&jid);
	return 0;
}

void CJabberProto::GroupchatJoinRoom(const TCHAR *server, const TCHAR *room, const TCHAR *nick, const TCHAR *password, bool autojoin)
{
	JabberGcRecentInfo info(this);

	bool found = false;
	for (int i=0 ; i < 5; i++) {
		if ( !info.loadRecent(i))
			continue;

		if (info.equals(room, server, nick, password)) {
			found = true;
			break;
		}
	}

	if (!found) {
		for (int i = 4; i--;) {
			if (info.loadRecent(i))
				info.saveRecent(i + 1);
		}

		info.fillData(room, server, nick, password);
		info.saveRecent(0);
	}

	TCHAR text[JABBER_MAX_JID_LEN + 1];
	mir_sntprintf(text, SIZEOF(text), _T("%s@%s/%s"), room, server, nick);

	JABBER_LIST_ITEM *item = ListAdd(LIST_CHATROOM, text);
	item->bAutoJoin = autojoin;
	replaceStrT(item->nick, nick);
	replaceStrT(item->password, info.password);

	int status = (m_iStatus == ID_STATUS_INVISIBLE) ? ID_STATUS_ONLINE : m_iStatus;
	XmlNode x(_T("x")); x << XATTR(_T("xmlns"), JABBER_FEAT_MUC);
	if (info.password && info.password[0])
		x << XCHILD(_T("password"), info.password);

	SendPresenceTo(status, text, x);
}

////////////////////////////////////////////////////////////////////////////////
// Join Dialog

static int sttTextLineHeight = 16;

struct RoomInfo
{
	enum Overlay { ROOM_WAIT, ROOM_FAIL, ROOM_BOOKMARK, ROOM_DEFAULT };
	Overlay	overlay;
	TCHAR	*line1, *line2;
};

static int sttRoomListAppend(HWND hwndList, RoomInfo::Overlay overlay, const TCHAR *line1, const TCHAR *line2, const TCHAR *name)
{
	RoomInfo *info = (RoomInfo *)mir_alloc(sizeof(RoomInfo));
	info->overlay = overlay;
	info->line1 = line1 ? mir_tstrdup(line1) : 0;
	info->line2 = line2 ? mir_tstrdup(line2) : 0;

	int id = SendMessage(hwndList, CB_ADDSTRING, 0, (LPARAM)name);
	SendMessage(hwndList, CB_SETITEMDATA, id, (LPARAM)info);
	SendMessage(hwndList, CB_SETITEMHEIGHT, id, sttTextLineHeight * 2);
	return id;
}

void CJabberProto::OnIqResultDiscovery(HXML iqNode, CJabberIqInfo *pInfo)
{
	if ( !iqNode || !pInfo)
		return;

	HWND hwndList = (HWND)pInfo->GetUserData();
	SendMessage(hwndList, CB_SHOWDROPDOWN, FALSE, 0);
	SendMessage(hwndList, CB_RESETCONTENT, 0, 0);

	if (pInfo->GetIqType() == JABBER_IQ_TYPE_RESULT)
	{
		HXML query = xmlGetChild(iqNode , "query");
		if ( !query)
		{
			sttRoomListAppend(hwndList, RoomInfo::ROOM_FAIL,
				TranslateT("Jabber Error"),
				TranslateT("Failed to retrieve room list from server."),
				_T(""));
		} else
		{
			bool found = false;
			HXML item;
			for (int i = 1; item = xmlGetNthChild(query, _T("item"), i); i++)
			{
				const TCHAR *jid = xmlGetAttrValue(item, _T("jid"));
				TCHAR *name = NEWTSTR_ALLOCA(jid);
				if (name)
				{
					if (TCHAR *p = _tcschr(name, _T('@')))
						*p = 0;
				} else
				{
					name = _T("");
				}

				sttRoomListAppend(hwndList,
					ListGetItemPtr(LIST_BOOKMARK, jid) ? RoomInfo::ROOM_BOOKMARK : RoomInfo::ROOM_DEFAULT,
					xmlGetAttrValue(item, _T("name")),
					jid, name);

				found = true;
			}

			if ( !found)
			{
				sttRoomListAppend(hwndList, RoomInfo::ROOM_FAIL,
					TranslateT("Jabber Error"),
					TranslateT("No rooms available on server."),
					_T(""));
			}
		}
	} else
	if (pInfo->GetIqType() == JABBER_IQ_TYPE_ERROR)
	{
		HXML errorNode = xmlGetChild(iqNode , "error");
		TCHAR *str = JabberErrorMsg(errorNode);
		sttRoomListAppend(hwndList, RoomInfo::ROOM_FAIL,
			TranslateT("Jabber Error"),
			str,
			_T(""));
		mir_free(str);
	} else
	{
		sttRoomListAppend(hwndList, RoomInfo::ROOM_FAIL,
			TranslateT("Jabber Error"),
			TranslateT("Room list request timed out."),
			_T(""));
	}

	SendMessage(hwndList, CB_SHOWDROPDOWN, TRUE, 0);
}

static void sttJoinDlgShowRecentItems(HWND hwndDlg, int newCount)
{
	RECT rcTitle, rcLastItem;
	GetWindowRect(GetDlgItem(hwndDlg, IDC_TXT_RECENT), &rcTitle);
	GetWindowRect(GetDlgItem(hwndDlg, IDC_RECENT5), &rcLastItem);

	ShowWindow(GetDlgItem(hwndDlg, IDC_TXT_RECENT), newCount ? SW_SHOW : SW_HIDE);

	int oldCount = 5;
	for (int idc = IDC_RECENT1; idc <= IDC_RECENT5; ++idc)
		ShowWindow(GetDlgItem(hwndDlg, idc), (idc - IDC_RECENT1 < newCount) ? SW_SHOW : SW_HIDE);

	int curRecentHeight = rcLastItem.bottom - rcTitle.top - (5 - oldCount) * (rcLastItem.bottom - rcLastItem.top);
	int newRecentHeight = rcLastItem.bottom - rcTitle.top - (5 - newCount) * (rcLastItem.bottom - rcLastItem.top);
	if ( !newCount) newRecentHeight = 0;
	int offset = newRecentHeight - curRecentHeight;

	RECT rc;
	int ctrls[] = { IDC_BOOKMARKS, IDOK, IDCANCEL };
	for (int i=0; i < SIZEOF(ctrls); i++)
	{
		GetWindowRect(GetDlgItem(hwndDlg, ctrls[i]), &rc);
		MapWindowPoints(NULL, hwndDlg, (LPPOINT)&rc, 2);
		SetWindowPos(GetDlgItem(hwndDlg, ctrls[i]), NULL, rc.left, rc.top + offset, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
	}

	GetWindowRect(hwndDlg, &rc);
	SetWindowPos(hwndDlg, NULL, 0, 0, rc.right-rc.left, rc.bottom-rc.top+offset, SWP_NOMOVE|SWP_NOZORDER);
}

class CJabberDlgGcJoin: public CJabberDlgBase
{
	typedef CJabberDlgBase CSuper;

public:
	CJabberDlgGcJoin(CJabberProto *proto, TCHAR *jid);

protected:
	TCHAR *m_jid;

	void OnInitDialog();
	void OnClose();
	void OnDestroy();
	INT_PTR DlgProc(UINT msg, WPARAM wParam, LPARAM lParam);
};

CJabberDlgGcJoin::CJabberDlgGcJoin(CJabberProto *proto, TCHAR *jid) :
	CSuper(proto, IDD_GROUPCHAT_JOIN, NULL),
	m_jid(mir_tstrdup(jid))
{
	m_autoClose = 0;
}

void CJabberDlgGcJoin::OnInitDialog()
{
	CSuper::OnInitDialog();

	WindowSetIcon(m_hwnd, m_proto, "group");

	JabberGcRecentInfo *info = NULL;
	if (m_jid)
		info = new JabberGcRecentInfo(m_proto, m_jid);
	else
	{
		OpenClipboard(m_hwnd);
		HANDLE hData = GetClipboardData(CF_UNICODETEXT);

		if (hData)
		{
			TCHAR *buf = (TCHAR *)GlobalLock(hData);
			if (buf && _tcschr(buf, _T('@')) && !_tcschr(buf, _T(' ')))
				info = new JabberGcRecentInfo(m_proto, buf);
			GlobalUnlock(hData);
		}
		CloseClipboard();
	}

	if (info)
	{
		info->fillForm(m_hwnd);
		delete info;
	}

	DBVARIANT dbv;
	if ( !m_proto->getTString("Nick", &dbv)) {
		SetDlgItemText(m_hwnd, IDC_NICK, dbv.ptszVal);
		db_free(&dbv);
	}
	else {
		TCHAR *nick = JabberNickFromJID(m_proto->m_szJabberJID);
		SetDlgItemText(m_hwnd, IDC_NICK, nick);
		mir_free(nick);
	}

	{
		TEXTMETRIC tm = {0};
		HDC hdc = GetDC(m_hwnd);
		GetTextMetrics(hdc, &tm);
		ReleaseDC(m_hwnd, hdc);
		sttTextLineHeight = tm.tmHeight;
		SendDlgItemMessage(m_hwnd, IDC_ROOM, CB_SETITEMHEIGHT, -1, sttTextLineHeight-1);
	}

	{
		LOGFONT lf = {0};
		HFONT hfnt = (HFONT)SendDlgItemMessage(m_hwnd, IDC_TXT_RECENT, WM_GETFONT, 0, 0);
		GetObject(hfnt, sizeof(lf), &lf);
		lf.lfWeight = FW_BOLD;
		SendDlgItemMessage(m_hwnd, IDC_TXT_RECENT, WM_SETFONT, (WPARAM)CreateFontIndirect(&lf), TRUE);
	}

	SendDlgItemMessage(m_hwnd, IDC_BOOKMARKS, BM_SETIMAGE, IMAGE_ICON, (LPARAM)m_proto->LoadIconEx("bookmarks"));
	SendDlgItemMessage(m_hwnd, IDC_BOOKMARKS, BUTTONSETASFLATBTN, TRUE, 0);
	SendDlgItemMessage(m_hwnd, IDC_BOOKMARKS, BUTTONADDTOOLTIP, (WPARAM)"Bookmarks", 0);
	SendDlgItemMessage(m_hwnd, IDC_BOOKMARKS, BUTTONSETASPUSHBTN, TRUE, 0);

	m_proto->ComboLoadRecentStrings(m_hwnd, IDC_SERVER, "joinWnd_rcSvr");

	int i;
	for (i=0; i < 5; i++) {
		TCHAR jid[JABBER_MAX_JID_LEN];
		JabberGcRecentInfo info(m_proto);
		if ( !info.loadRecent(i))
			break;

		mir_sntprintf(jid, SIZEOF(jid), _T("%s@%s (%s)"),
			info.room, info.server,
			info.nick ? info.nick : TranslateT("<no nick>"));
		SetDlgItemText(m_hwnd, IDC_RECENT1+i, jid);
	}
	sttJoinDlgShowRecentItems(m_hwnd, i);
}

void CJabberDlgGcJoin::OnClose()
{
	CSuper::OnClose();
}

void CJabberDlgGcJoin::OnDestroy()
{
	g_ReleaseIcon((HICON)SendDlgItemMessage(m_hwnd, IDC_BOOKMARKS, BM_SETIMAGE, IMAGE_ICON, 0));
	m_proto->m_pDlgJabberJoinGroupchat = NULL;
	DeleteObject((HFONT)SendDlgItemMessage(m_hwnd, IDC_TXT_RECENT, WM_GETFONT, 0, 0));

	CSuper::OnDestroy();

	mir_free(m_jid); m_jid = NULL;
}

INT_PTR CJabberDlgGcJoin::DlgProc(UINT msg, WPARAM wParam, LPARAM lParam)
{
	TCHAR text[128];

	switch (msg) {
	case WM_DELETEITEM:
	{
		LPDELETEITEMSTRUCT lpdis = (LPDELETEITEMSTRUCT)lParam;
		if (lpdis->CtlID != IDC_ROOM)
			break;

		RoomInfo *info = (RoomInfo *)lpdis->itemData;
		if (info->line1) mir_free(info->line1);
		if (info->line2) mir_free(info->line2);
		mir_free(info);

		break;
	}

	case WM_MEASUREITEM:
	{
		LPMEASUREITEMSTRUCT lpmis = (LPMEASUREITEMSTRUCT)lParam;
		if (lpmis->CtlID != IDC_ROOM)
			break;

		lpmis->itemHeight = 2*sttTextLineHeight;
		if (lpmis->itemID == -1)
			lpmis->itemHeight = sttTextLineHeight-1;

		break;
	}

	case WM_DRAWITEM:
	{
		LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
		if (lpdis->CtlID != IDC_ROOM)
			break;

		if (lpdis->itemID < 0)
			break;

		RoomInfo *info = (RoomInfo *)SendDlgItemMessage(m_hwnd, IDC_ROOM, CB_GETITEMDATA, lpdis->itemID, 0);
		COLORREF clLine1, clLine2, clBack;

		if (lpdis->itemState & ODS_SELECTED)
		{
			FillRect(lpdis->hDC, &lpdis->rcItem, GetSysColorBrush(COLOR_HIGHLIGHT));
			clBack = GetSysColor(COLOR_HIGHLIGHT);
			clLine1 = GetSysColor(COLOR_HIGHLIGHTTEXT);
		} else
		{
			FillRect(lpdis->hDC, &lpdis->rcItem, GetSysColorBrush(COLOR_WINDOW));
			clBack = GetSysColor(COLOR_WINDOW);
			clLine1 = GetSysColor(COLOR_WINDOWTEXT);
		}
		clLine2 = RGB(
				GetRValue(clLine1) * 0.66 + GetRValue(clBack) * 0.34,
				GetGValue(clLine1) * 0.66 + GetGValue(clBack) * 0.34,
				GetBValue(clLine1) * 0.66 + GetBValue(clBack) * 0.34
			);

		SetBkMode(lpdis->hDC, TRANSPARENT);

		RECT rc;

		rc = lpdis->rcItem;
		rc.bottom -= (rc.bottom - rc.top) / 2;
		rc.left += 20;
		SetTextColor(lpdis->hDC, clLine1);
		DrawText(lpdis->hDC, info->line1, lstrlen(info->line1), &rc, DT_LEFT|DT_NOPREFIX|DT_SINGLELINE|DT_VCENTER|DT_WORD_ELLIPSIS);

		rc = lpdis->rcItem;
		rc.top += (rc.bottom - rc.top) / 2;
		rc.left += 20;
		SetTextColor(lpdis->hDC, clLine2);
		DrawText(lpdis->hDC, info->line2, lstrlen(info->line2), &rc, DT_LEFT|DT_NOPREFIX|DT_SINGLELINE|DT_VCENTER|DT_WORD_ELLIPSIS);

		DrawIconEx(lpdis->hDC, lpdis->rcItem.left+1, lpdis->rcItem.top+1, m_proto->LoadIconEx("group"), 16, 16, 0, NULL, DI_NORMAL);
		switch (info->overlay) {
		case RoomInfo::ROOM_WAIT:
			DrawIconEx(lpdis->hDC, lpdis->rcItem.left+1, lpdis->rcItem.top+1, m_proto->LoadIconEx("disco_progress"), 16, 16, 0, NULL, DI_NORMAL);
			break;
		case RoomInfo::ROOM_FAIL:
			DrawIconEx(lpdis->hDC, lpdis->rcItem.left+1, lpdis->rcItem.top+1, m_proto->LoadIconEx("disco_fail"), 16, 16, 0, NULL, DI_NORMAL);
			break;
		case RoomInfo::ROOM_BOOKMARK:
			DrawIconEx(lpdis->hDC, lpdis->rcItem.left+1, lpdis->rcItem.top+1, m_proto->LoadIconEx("disco_ok"), 16, 16, 0, NULL, DI_NORMAL);
			break;
		}
	}

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDC_SERVER:
			switch (HIWORD(wParam)) {
			case CBN_EDITCHANGE:
			case CBN_SELCHANGE:
				{
					int iqid = GetWindowLongPtr(GetDlgItem(m_hwnd, IDC_ROOM), GWLP_USERDATA);
					if (iqid)
					{
						m_proto->m_iqManager.ExpireIq(iqid);
						SetWindowLongPtr(GetDlgItem(m_hwnd, IDC_ROOM), GWLP_USERDATA, 0);
					}
					SendDlgItemMessage(m_hwnd, IDC_ROOM, CB_RESETCONTENT, 0, 0);
				}
				break;
			}
			break;

		case IDC_ROOM:
			switch (HIWORD(wParam)) {
			case CBN_DROPDOWN:
				if ( !SendDlgItemMessage(m_hwnd, IDC_ROOM, CB_GETCOUNT, 0, 0))
				{
					int iqid = GetWindowLongPtr(GetDlgItem(m_hwnd, IDC_ROOM), GWLP_USERDATA);
					if (iqid)
					{
						m_proto->m_iqManager.ExpireIq(iqid);
						SetWindowLongPtr(GetDlgItem(m_hwnd, IDC_ROOM), GWLP_USERDATA, 0);
					}

					SendDlgItemMessage(m_hwnd, IDC_ROOM, CB_RESETCONTENT, 0, 0);

					int len = GetWindowTextLength(GetDlgItem(m_hwnd, IDC_SERVER)) + 1;
					TCHAR *server = (TCHAR *)_alloca(len * sizeof(TCHAR));
					GetWindowText(GetDlgItem(m_hwnd, IDC_SERVER), server, len);

					if (*server)
					{
						sttRoomListAppend(GetDlgItem(m_hwnd, IDC_ROOM), RoomInfo::ROOM_WAIT, TranslateT("Loading..."), TranslateT("Please wait for room list to download."), _T(""));

						CJabberIqInfo *pInfo = m_proto->m_iqManager.AddHandler(&CJabberProto::OnIqResultDiscovery, JABBER_IQ_TYPE_GET, server, 0, -1, (void*)GetDlgItem(m_hwnd, IDC_ROOM));
						pInfo->SetTimeout(30000);
						XmlNodeIq iq(pInfo);
						iq << XQUERY(JABBER_FEAT_DISCO_ITEMS);
						m_proto->m_ThreadInfo->send(iq);

						SetWindowLongPtr(GetDlgItem(m_hwnd, IDC_ROOM), GWLP_USERDATA, pInfo->GetIqId());
					} else
					{
						sttRoomListAppend(GetDlgItem(m_hwnd, IDC_ROOM), RoomInfo::ROOM_FAIL,
							TranslateT("Jabber Error"),
							TranslateT("Please specify groupchat directory first."),
							_T(""));
					}
				}
				break;
			}
			break;

		case IDC_BOOKMARKS:
			{
				HMENU hMenu = CreatePopupMenu();

				LISTFOREACH(i, m_proto, LIST_BOOKMARK)
				{
					JABBER_LIST_ITEM *item = 0;
					if (item = m_proto->ListGetItemPtrFromIndex(i))
						if ( !lstrcmp(item->type, _T("conference")))
							AppendMenu(hMenu, MF_STRING, (UINT_PTR)item, item->name);
				}
				AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
				AppendMenu(hMenu, MF_STRING, (UINT_PTR)-1, TranslateT("Bookmarks..."));
				AppendMenu(hMenu, MF_STRING, (UINT_PTR)0, TranslateT("Cancel"));

				RECT rc; GetWindowRect(GetDlgItem(m_hwnd, IDC_BOOKMARKS), &rc);
				CheckDlgButton(m_hwnd, IDC_BOOKMARKS, TRUE);
				int res = TrackPopupMenu(hMenu, TPM_RETURNCMD, rc.left, rc.bottom, 0, m_hwnd, NULL);
				CheckDlgButton(m_hwnd, IDC_BOOKMARKS, FALSE);
				DestroyMenu(hMenu);

				if (res == -1)
					m_proto->OnMenuHandleBookmarks(0, 0);
				else if (res) {
					JABBER_LIST_ITEM *item = (JABBER_LIST_ITEM *)res;
					TCHAR *room = NEWTSTR_ALLOCA(item->jid);
					if (room) {
						TCHAR *server = _tcschr(room, _T('@'));
						if (server) {
							*server++ = 0;

							SendMessage(m_hwnd, WM_COMMAND, MAKEWPARAM(IDC_SERVER, CBN_EDITCHANGE), (LPARAM)GetDlgItem(m_hwnd, IDC_SERVER));

							SetDlgItemText(m_hwnd, IDC_SERVER, server);
							SetDlgItemText(m_hwnd, IDC_ROOM, room);
							SetDlgItemText(m_hwnd, IDC_NICK, item->nick);
							SetDlgItemText(m_hwnd, IDC_PASSWORD, item->password);
			}	}	}	}
			break;

		case IDC_RECENT1:
		case IDC_RECENT2:
		case IDC_RECENT3:
		case IDC_RECENT4:
		case IDC_RECENT5:
			{
				JabberGcRecentInfo info(m_proto, LOWORD(wParam) - IDC_RECENT1);
				info.fillForm(m_hwnd);
				if (GetAsyncKeyState(VK_CONTROL))
					break;
			}
			// fall through

		case IDOK:
			{
				GetDlgItemText(m_hwnd, IDC_SERVER, text, SIZEOF(text));
				TCHAR *server = NEWTSTR_ALLOCA(text), *room;

				m_proto->ComboAddRecentString(m_hwnd, IDC_SERVER, "joinWnd_rcSvr", server);

				GetDlgItemText(m_hwnd, IDC_ROOM, text, SIZEOF(text));
				room = NEWTSTR_ALLOCA(text);

				GetDlgItemText(m_hwnd, IDC_NICK, text, SIZEOF(text));
				TCHAR *nick = NEWTSTR_ALLOCA(text);

				GetDlgItemText(m_hwnd, IDC_PASSWORD, text, SIZEOF(text));
				TCHAR *password = NEWTSTR_ALLOCA(text);
				m_proto->GroupchatJoinRoom(server, room, nick, password);
			}
			// fall through
		case IDCANCEL:
			Close();
			break;
		}
		break;
	case WM_JABBER_CHECK_ONLINE:
		if ( !m_proto->m_bJabberOnline)
			EndDialog(m_hwnd, 0);
		break;
	}

	return CSuper::DlgProc(msg, wParam, lParam);
}

void CJabberProto::GroupchatJoinRoomByJid(HWND, TCHAR *jid)
{
	if (m_pDlgJabberJoinGroupchat)
		SetForegroundWindow(m_pDlgJabberJoinGroupchat->GetHwnd());
	else {
		m_pDlgJabberJoinGroupchat = new CJabberDlgGcJoin(this, jid);
		m_pDlgJabberJoinGroupchat->Show();
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
// JabberGroupchatProcessPresence - handles the group chat presence packet

struct JabberGroupchatChangeNicknameParam
{
	JabberGroupchatChangeNicknameParam(CJabberProto* ppro_, const TCHAR *jid_) :
		ppro(ppro_),
		jid(mir_tstrdup(jid_))
		{}

	~JabberGroupchatChangeNicknameParam()
	{	mir_free(jid);
	}

	CJabberProto *ppro;
	TCHAR *jid;
};

static VOID CALLBACK JabberGroupchatChangeNickname(void* arg)
{
	JabberGroupchatChangeNicknameParam* param = (JabberGroupchatChangeNicknameParam*)arg;
	if (param == NULL)
		return;

	JABBER_LIST_ITEM *item = param->ppro->ListGetItemPtr(LIST_CHATROOM, param->jid);
	if (item != NULL) {
		TCHAR szBuffer[ 1024 ];
		TCHAR szCaption[ 1024 ];
		szBuffer[ 0 ] = _T('\0');

		TCHAR *roomName = item->name ? item->name : item->jid;
		mir_sntprintf(szCaption, SIZEOF(szCaption), TranslateT("Change nickname in <%s>"), roomName);
		if (item->nick)
			mir_sntprintf(szBuffer, SIZEOF(szBuffer), _T("%s"), item->nick);

		if (param->ppro->EnterString(szBuffer, SIZEOF(szBuffer), szCaption, JES_COMBO, "gcNick_")) {
			TCHAR text[ 1024 ];
			replaceStrT(item->nick, szBuffer);
			mir_sntprintf(text, SIZEOF(text), _T("%s/%s"), item->jid, szBuffer);
			param->ppro->SendPresenceTo(param->ppro->m_iStatus, text, NULL);
	}	}

	delete param;
}

static int sttGetStatusCode(HXML node)
{
	HXML statusNode = xmlGetChild(node , "status");
	if (statusNode == NULL)
		return -1;

	const TCHAR *statusCode = xmlGetAttrValue(statusNode, _T("code"));
	if (statusCode == NULL)
		return -1;

	return _ttol(statusCode);
}

void CJabberProto::RenameParticipantNick(JABBER_LIST_ITEM *item, const TCHAR *oldNick, HXML itemNode)
{
	const TCHAR *jid = xmlGetAttrValue(itemNode, _T("jid"));
	const TCHAR *newNick = xmlGetAttrValue(itemNode, _T("nick"));
	if (newNick == NULL)
		return;

	pResourceStatus r( item->findResource(oldNick));
	if (r == NULL)
		return;

	r->m_tszResourceName = mir_tstrdup(newNick);

	if ( !lstrcmp(item->nick, oldNick)) {
		replaceStrT(item->nick, newNick);

		HANDLE hContact = HContactFromJID(item->jid);
		if (hContact != NULL)
			setTString(hContact, "MyNick", newNick);
	}

	GCDEST gcd = { m_szModuleName, NULL, GC_EVENT_CHUID };
	gcd.ptszID = item->jid;

	GCEVENT gce = {0};
	gce.cbSize = sizeof(GCEVENT);
	gce.pDest = &gcd;
	gce.ptszNick = oldNick;
	gce.ptszText = newNick;
	if (jid != NULL)
		gce.ptszUserInfo = jid;
	gce.time = time(0);
	gce.dwFlags = GC_TCHAR;
	CallServiceSync(MS_GC_EVENT, NULL, (LPARAM)&gce);

	gcd.iType = GC_EVENT_NICK;
	gce.ptszNick = oldNick;
	gce.ptszUID = newNick;
	gce.ptszText = newNick;
	CallServiceSync(MS_GC_EVENT, NULL, (LPARAM)&gce);
}

void CJabberProto::GroupchatProcessPresence(HXML node)
{
	const TCHAR *from;

	if ( !node || !xmlGetName(node) || lstrcmp(xmlGetName(node), _T("presence"))) return;
	if ((from = xmlGetAttrValue(node, _T("from"))) == NULL) return;

	const TCHAR *resource = _tcschr(from, '/');
	if (resource == NULL || *++resource == '\0')
		return;

	JABBER_LIST_ITEM *item = ListGetItemPtr(LIST_CHATROOM, from);
	if (item == NULL)
		return;

	pResourceStatus r( item->findResource(resource));

	HXML nNode = xmlGetChildByTag(node, "nick", "xmlns", JABBER_FEAT_NICK);
	const TCHAR *cnick = xmlGetText(nNode);
	const TCHAR *nick = cnick ? cnick : (r && r->m_tszNick ? r->m_tszNick : resource);

	// process custom nick change
	if (cnick && r && r->m_tszNick && _tcscmp(cnick, r->m_tszNick))
		r->m_tszNick = mir_tstrdup(cnick);

	HXML xNode = xmlGetChildByTag(node, "x", "xmlns", JABBER_FEAT_MUC_USER);
	HXML itemNode = xmlGetChild(xNode , "item");

	const TCHAR *type = xmlGetAttrValue(node, _T("type"));

	// entering room or a usual room presence
	if (type == NULL || !_tcscmp(type, _T("available"))) {
		TCHAR *room = JabberNickFromJID(from);
		if (room == NULL)
			return;

		GcLogCreate(item);
		item->iChatState = 0;

		// Update status of room participant
		int status = ID_STATUS_ONLINE;
		LPCTSTR ptszShow = xmlGetText( xmlGetChild(node , "show"));
		if (ptszShow) {
			     if ( !_tcscmp(ptszShow, _T("away"))) status = ID_STATUS_AWAY;
			else if ( !_tcscmp(ptszShow, _T("xa"))) status = ID_STATUS_NA;
			else if ( !_tcscmp(ptszShow, _T("dnd"))) status = ID_STATUS_DND;
			else if ( !_tcscmp(ptszShow, _T("chat"))) status = ID_STATUS_FREECHAT;
		}

		LPCTSTR str = xmlGetText( xmlGetChild(node , "status"));

		char priority = 0;
		if (LPCTSTR ptszPriority = xmlGetText( xmlGetChild(node , "priority")))
			priority = (char)_ttoi(ptszPriority);

		bool bStatusChanged = false, bRoomCreated = false, bAffiliationChanged = false, bRoleChanged = false;
		int  newRes = ListAddResource(LIST_CHATROOM, from, status, str, priority, cnick) ? GC_EVENT_JOIN : 0;

		if (pResourceStatus oldRes = ListFindResource(LIST_CHATROOM, from))
			if ((oldRes->m_iStatus != status) || lstrcmp_null(oldRes->m_tszStatusMessage, str))
				bStatusChanged = true;

		// Check additional MUC info for this user
		if (itemNode != NULL) {
			if (r == NULL)
				r = item->findResource(resource);
			if (r != NULL) {
				JABBER_GC_AFFILIATION affiliation = r->m_affiliation;
				JABBER_GC_ROLE role = r->m_role;

				if ((str = xmlGetAttrValue(itemNode, _T("affiliation"))) != NULL) {
					     if ( !_tcscmp(str, _T("owner")))       affiliation = AFFILIATION_OWNER;
					else if ( !_tcscmp(str, _T("admin")))       affiliation = AFFILIATION_ADMIN;
					else if ( !_tcscmp(str, _T("member")))      affiliation = AFFILIATION_MEMBER;
					else if ( !_tcscmp(str, _T("none")))	     affiliation = AFFILIATION_NONE;
					else if ( !_tcscmp(str, _T("outcast")))     affiliation = AFFILIATION_OUTCAST;
				}
				if ((str = xmlGetAttrValue(itemNode, _T("role"))) != NULL) {
					     if ( !_tcscmp(str, _T("moderator")))   role = ROLE_MODERATOR;
					else if ( !_tcscmp(str, _T("participant"))) role = ROLE_PARTICIPANT;
					else if ( !_tcscmp(str, _T("visitor")))     role = ROLE_VISITOR;
					else                                        role = ROLE_NONE;
				}

				if ((role != ROLE_NONE) && (JabberGcGetStatus(r) != JabberGcGetStatus(affiliation, role))) {
					GcLogUpdateMemberStatus(item, resource, nick, NULL, GC_EVENT_REMOVESTATUS, NULL);
					if ( !newRes) newRes = GC_EVENT_ADDSTATUS;
				}

				if (affiliation != r->m_affiliation) {
					r->m_affiliation = affiliation;
					bAffiliationChanged = true;
				}

				if (role != r->m_role) {
					r->m_role = role;
					if (r->m_role != ROLE_NONE)
						bRoleChanged = true;
				}

				if (str = xmlGetAttrValue(itemNode, _T("jid")))
					r->m_tszRealJid = mir_tstrdup(str);
			}
		}

		if (sttGetStatusCode(xNode) == 201)
			bRoomCreated = true;

		// show status change if needed
		if (bStatusChanged)
			if (pResourceStatus res = ListFindResource(LIST_CHATROOM, from))
				GcLogShowInformation(item, res, INFO_STATUS);

		// Update groupchat log window
		GcLogUpdateMemberStatus(item, resource, nick, str, newRes, NULL);
		if (r && bAffiliationChanged) GcLogShowInformation(item, r, INFO_AFFILIATION);
		if (r && bRoleChanged) GcLogShowInformation(item, r, INFO_ROLE);

		// update clist status
		HANDLE hContact = HContactFromJID(from);
		if (hContact != NULL)
			setWord(hContact, "Status", status);

		// Update room status
		//if (item->status != ID_STATUS_ONLINE) {
		//	item->status = ID_STATUS_ONLINE;
		//	setWord(hContact, "Status", (WORD)ID_STATUS_ONLINE);
		//	JabberLog("Room %s online", from);
		//}

		// Check <created/>
		if (bRoomCreated) {
			HXML n = xmlGetChild(node , "created");
			if (n != NULL && (str = xmlGetAttrValue(n, _T("xmlns"))) != NULL && !_tcscmp(str, JABBER_FEAT_MUC_OWNER)) {
				// A new room just created by me
				// Request room config
				int iqId = SerialNext();
				IqAdd(iqId, IQ_PROC_NONE, &CJabberProto::OnIqResultGetMuc);
				m_ThreadInfo->send( XmlNodeIq(_T("get"), iqId, item->jid) << XQUERY(JABBER_FEAT_MUC_OWNER));
			}
		}

		mir_free(room);
	}

	// leaving room
	else if ( !_tcscmp(type, _T("unavailable"))) {
		const TCHAR *str = 0;
		if (xNode != NULL && item->nick != NULL) {
			HXML reasonNode = xmlGetChild(itemNode , "reason");
			str = xmlGetAttrValue(itemNode, _T("jid"));

			int iStatus = sttGetStatusCode(xNode);
			if (iStatus == 301 && r != NULL)
				GcLogShowInformation(item, r, INFO_BAN);

			if ( !lstrcmp(resource, item->nick)) {
				switch(iStatus) {
				case 301:
				case 307:
					GcQuit(item, iStatus, reasonNode);
					return;

				case 303:
					RenameParticipantNick(item, resource, itemNode);
					return;
			}	}
			else {
				switch(iStatus) {
				case 303:
					RenameParticipantNick(item, resource, itemNode);
					return;

				case 301:
				case 307:
				case 322:
					ListRemoveResource(LIST_CHATROOM, from);
					GcLogUpdateMemberStatus(item, resource, nick, str, GC_EVENT_KICK, reasonNode, iStatus);
					return;
		}	}	}

		HXML statusNode = xmlGetChild(node , "status");
		GcLogUpdateMemberStatus(item, resource, nick, str, GC_EVENT_PART, statusNode);
		ListRemoveResource(LIST_CHATROOM, from);

		HANDLE hContact = HContactFromJID(from);
		if (hContact != NULL)
			setWord(hContact, "Status", ID_STATUS_OFFLINE);
	}

	// processing room errors
	else if ( !_tcscmp(type, _T("error"))) {
		int errorCode = 0;
		HXML errorNode = xmlGetChild(node , "error");
		ptrT str( JabberErrorMsg(errorNode, &errorCode));

		if (errorCode == JABBER_ERROR_CONFLICT) {
			ptrT newNick( getTStringA("GcAltNick"));
			if (++item->iChatState == 1 && newNick != NULL && newNick[0] != 0) {
				replaceStrT(item->nick, newNick);
				TCHAR text[1024] = { 0 };
				mir_sntprintf(text, SIZEOF(text), _T("%s/%s"), item->jid, newNick);
				SendPresenceTo(m_iStatus, text, NULL);
			}
			else {
				CallFunctionAsync(JabberGroupchatChangeNickname, new JabberGroupchatChangeNicknameParam(this, from));
				item->iChatState = 0;
			}
			return;
		}

		MsgPopup(NULL, str, TranslateT("Jabber Error"));

		if (item != NULL && !item->bChatActive)
			ListRemove(LIST_CHATROOM, from);
	}
}

void CJabberProto::GroupchatProcessMessage(HXML node)
{
	HXML n, m;
	const TCHAR *from, *type, *p, *nick, *resource;
	JABBER_LIST_ITEM *item;

	if ( !xmlGetName(node) || lstrcmp(xmlGetName(node), _T("message"))) return;
	if ((from = xmlGetAttrValue(node, _T("from"))) == NULL) return;
	if ((item = ListGetItemPtr(LIST_CHATROOM, from)) == NULL) return;

	if ((type = xmlGetAttrValue(node, _T("type"))) == NULL) return;
	if ( !lstrcmp(type, _T("error")))
		return;

	GCDEST gcd = { m_szModuleName, NULL, 0 };
	gcd.ptszID = item->jid;

	const TCHAR *msgText = NULL;

	resource = _tcschr(from, '/');
	if (resource != NULL && *++resource == '\0')
		resource = NULL;

	if ((n = xmlGetChild(node , "subject")) != NULL) {
		msgText = xmlGetText(n);
		if (msgText == NULL || msgText[0] == '\0')
			return;

		gcd.iType = GC_EVENT_TOPIC;

		if (resource == NULL && (m = xmlGetChild(node, "body")) != NULL) {
			const TCHAR *tmpnick = xmlGetText(m);
			if (tmpnick == NULL || *tmpnick == 0)
				return;

			const TCHAR *tmptr = _tcsstr(tmpnick, _T("has set the subject to:")); //ejabberd
			if (tmptr == NULL)
				tmptr = _tcsstr(tmpnick, TranslateT("has set the subject to:")); //ejabberd
			if (tmptr != NULL && *tmptr != 0) {
				*(TCHAR*)(--tmptr) = 0;
				resource = tmpnick;
			}
		}
		item->getTemp()->m_tszStatusMessage = mir_tstrdup(msgText);
	}
	else {
		if ((n = xmlGetChildByTag(node , "body", "xml:lang", m_tszSelectedLang)) == NULL)
			if ((n = xmlGetChild(node , "body")) == NULL)
				return;

		msgText = xmlGetText(n);
		if (msgText == NULL)
			return;

		if (resource == NULL)
			gcd.iType = GC_EVENT_INFORMATION;
		else if (_tcsncmp(msgText, _T("/me "), 4) == 0 && _tcslen(msgText) > 4) {
			msgText += 4;
			gcd.iType = GC_EVENT_ACTION;
		}
		else gcd.iType = GC_EVENT_MESSAGE;
	}

	GcLogCreate(item);

	time_t msgTime = 0;
	if ( !JabberReadXep203delay(node, msgTime)) {
		HXML xDelay = xmlGetChildByTag(node, "x", "xmlns", _T("jabber:x:delay"));
		if (xDelay && (p = xmlGetAttrValue(xDelay, _T("stamp"))) != NULL)
			msgTime = JabberIsoToUnixTime(p);
	}

	bool isHistory = msgTime != 0;
	time_t now = time(NULL);
	if (!msgTime || msgTime > now)
		msgTime = now;

	if (resource != NULL) {
		pResourceStatus r( item->findResource(resource));
		nick = (r && r->m_tszNick) ? r->m_tszNick : resource;
	}
	else nick = NULL;

	GCEVENT gce = { sizeof(GCEVENT) };
	gce.pDest = &gcd;
	gce.ptszUID = resource;
	gce.ptszNick = nick;
	gce.time = msgTime;
	gce.ptszText = EscapeChatTags((TCHAR*)msgText);
	gce.bIsMe = nick == NULL ? FALSE : (lstrcmp(resource, item->nick) == 0);
	gce.dwFlags = GC_TCHAR;

	if (!isHistory)
		gce.dwFlags |= GCEF_ADDTOLOG;

	if (m_options.GcLogChatHistory && isHistory)
		gce.dwFlags |= GCEF_NOTNOTIFY;

	CallServiceSync(MS_GC_EVENT, NULL, (LPARAM)&gce);

	item->bChatActive = 2;

	if (gcd.iType == GC_EVENT_TOPIC) {
		gce.dwFlags &= ~GCEF_ADDTOLOG;
		gcd.iType = GC_EVENT_SETSBTEXT;
		CallServiceSync(MS_GC_EVENT, NULL, (LPARAM)&gce);
	}

	mir_free((void*)gce.pszText); // Since we processed msgText and created a new string
}

/////////////////////////////////////////////////////////////////////////////////////////
// Accepting groupchat invitations

class CGroupchatInviteAcceptDlg : public CJabberDlgBase
{
	typedef CJabberDlgBase CSuper;
	CCtrlButton m_accept;
	JABBER_GROUPCHAT_INVITE_INFO* m_info;

public:
	CGroupchatInviteAcceptDlg(CJabberProto *ppro, JABBER_GROUPCHAT_INVITE_INFO* pInfo) :
		CSuper(ppro, IDD_GROUPCHAT_INVITE_ACCEPT, NULL),
		m_info(pInfo),
		m_accept(this, IDC_ACCEPT)
	{
		m_accept.OnClick = Callback(this, &CGroupchatInviteAcceptDlg::OnCommand_Accept);
	}

	void OnInitDialog()
	{
		CSuper::OnInitDialog();

		TCHAR buf[256];
		mir_sntprintf(buf, SIZEOF(buf), TranslateT("%s\nIncoming groupchat invitation."), m_info->roomJid);
		SetDlgItemText(m_hwnd, IDC_HEADERBAR, buf);

		SetDlgItemText(m_hwnd, IDC_FROM, m_info->from);

		if (m_info->reason != NULL)
			SetDlgItemText(m_hwnd, IDC_REASON, m_info->reason);

		TCHAR *myNick = JabberNickFromJID(m_proto->m_szJabberJID);
		SetDlgItemText(m_hwnd, IDC_NICK, myNick);
		mir_free(myNick);

		WindowSetIcon(m_hwnd, m_proto, "group");

		SetFocus(GetDlgItem(m_hwnd, IDC_NICK));
	}

	void OnCommand_Accept(CCtrlButton*)
	{
		TCHAR text[128];
		GetDlgItemText(m_hwnd, IDC_NICK, text, SIZEOF(text));
		m_proto->AcceptGroupchatInvite(m_info->roomJid, text, m_info->password);
		EndDialog(m_hwnd, 0);
	}
};

void __cdecl CJabberProto::GroupchatInviteAcceptThread(JABBER_GROUPCHAT_INVITE_INFO *inviteInfo)
{
	CGroupchatInviteAcceptDlg(this, inviteInfo).DoModal();

	mir_free(inviteInfo->roomJid);
	mir_free(inviteInfo->from);
	mir_free(inviteInfo->reason);
	mir_free(inviteInfo->password);
	mir_free(inviteInfo);
}

void CJabberProto::GroupchatProcessInvite(const TCHAR *roomJid, const TCHAR *from, const TCHAR *reason, const TCHAR *password)
{
	if (roomJid == NULL)
		return;

	if (ListGetItemPtr(LIST_CHATROOM, roomJid))
		return;

	if (m_options.AutoAcceptMUC == FALSE) {
		JABBER_GROUPCHAT_INVITE_INFO* inviteInfo = (JABBER_GROUPCHAT_INVITE_INFO *) mir_alloc(sizeof(JABBER_GROUPCHAT_INVITE_INFO));
		inviteInfo->roomJid  = mir_tstrdup(roomJid);
		inviteInfo->from     = mir_tstrdup(from);
		inviteInfo->reason   = mir_tstrdup(reason);
		inviteInfo->password = mir_tstrdup(password);
		ForkThread((MyThreadFunc)&CJabberProto::GroupchatInviteAcceptThread, inviteInfo);
	}
	else {
		TCHAR *myNick = JabberNickFromJID(m_szJabberJID);
		AcceptGroupchatInvite(roomJid, myNick, password);
		mir_free(myNick);
}	}

void CJabberProto::AcceptGroupchatInvite(const TCHAR *roomJid, const TCHAR *reason, const TCHAR *password)
{
	TCHAR room[256], *server, *p;
	_tcsncpy(room, roomJid, SIZEOF(room));
	p = _tcstok(room, _T("@"));
	server = _tcstok(NULL, _T("@"));
	GroupchatJoinRoom(server, p, reason, password);
}