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

Jabber Protocol Plugin for Miranda IM
Copyright (C) 2002-04  Santithorn Bunchua
Copyright (C) 2005-12  George Hazan
Copyright (C) 2007     Maxim Mluhov
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 <io.h>
#include "version.h"
#include "jabber_iq.h"
#include "jabber_rc.h"

#ifndef VER_SUITE_WH_SERVER
	#define VER_SUITE_WH_SERVER                     0x00008000
#endif

#ifndef PRODUCT_ULTIMATE
	#define PRODUCT_UNDEFINED                         0x00000000
	#define PRODUCT_ULTIMATE                          0x00000001
	#define PRODUCT_HOME_BASIC                        0x00000002
	#define PRODUCT_HOME_PREMIUM                      0x00000003
	#define PRODUCT_ENTERPRISE                        0x00000004
	#define PRODUCT_HOME_BASIC_N                      0x00000005
	#define PRODUCT_BUSINESS                          0x00000006
	#define PRODUCT_STANDARD_SERVER                   0x00000007
	#define PRODUCT_DATACENTER_SERVER                 0x00000008
	#define PRODUCT_SMALLBUSINESS_SERVER              0x00000009
	#define PRODUCT_ENTERPRISE_SERVER                 0x0000000A
	#define PRODUCT_STARTER                           0x0000000B
	#define PRODUCT_DATACENTER_SERVER_CORE            0x0000000C
	#define PRODUCT_STANDARD_SERVER_CORE              0x0000000D
	#define PRODUCT_ENTERPRISE_SERVER_CORE            0x0000000E
	#define PRODUCT_ENTERPRISE_SERVER_IA64            0x0000000F
	#define PRODUCT_BUSINESS_N                        0x00000010
	#define PRODUCT_WEB_SERVER                        0x00000011
	#define PRODUCT_CLUSTER_SERVER                    0x00000012
	#define PRODUCT_HOME_SERVER                       0x00000013
	#define PRODUCT_STORAGE_EXPRESS_SERVER            0x00000014
	#define PRODUCT_STORAGE_STANDARD_SERVER           0x00000015
	#define PRODUCT_STORAGE_WORKGROUP_SERVER          0x00000016
	#define PRODUCT_STORAGE_ENTERPRISE_SERVER         0x00000017
	#define PRODUCT_SERVER_FOR_SMALLBUSINESS          0x00000018
	#define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM      0x00000019
	#define PRODUCT_HOME_PREMIUM_N                    0x0000001a
	#define PRODUCT_ENTERPRISE_N                      0x0000001b
	#define PRODUCT_ULTIMATE_N                        0x0000001c
	#define PRODUCT_WEB_SERVER_CORE                   0x0000001d
	#define PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT  0x0000001e
	#define PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY    0x0000001f
	#define PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING   0x00000020
	#define PRODUCT_SERVER_FOUNDATION                 0x00000021
	#define PRODUCT_HOME_PREMIUM_SERVER               0x00000022
	#define PRODUCT_SERVER_FOR_SMALLBUSINESS_V        0x00000023
	#define PRODUCT_STANDARD_SERVER_V                 0x00000024
	#define PRODUCT_DATACENTER_SERVER_V               0x00000025
	#define PRODUCT_ENTERPRISE_SERVER_V               0x00000026
	#define PRODUCT_DATACENTER_SERVER_CORE_V          0x00000027
	#define PRODUCT_STANDARD_SERVER_CORE_V            0x00000028
	#define PRODUCT_ENTERPRISE_SERVER_CORE_V          0x00000029
	#define PRODUCT_HYPERV                            0x0000002a
	#define PRODUCT_STORAGE_EXPRESS_SERVER_CORE       0x0000002b
	#define PRODUCT_STORAGE_STANDARD_SERVER_CORE      0x0000002c
	#define PRODUCT_STORAGE_WORKGROUP_SERVER_CORE     0x0000002d
	#define PRODUCT_STORAGE_ENTERPRISE_SERVER_CORE    0x0000002e
	#define PRODUCT_STARTER_N                         0x0000002f
	#define PRODUCT_PROFESSIONAL                      0x00000030
	#define PRODUCT_PROFESSIONAL_N                    0x00000031
	#define PRODUCT_SB_SOLUTION_SERVER                0x00000032
	#define PRODUCT_SERVER_FOR_SB_SOLUTIONS           0x00000033
	#define PRODUCT_STANDARD_SERVER_SOLUTIONS         0x00000034
	#define PRODUCT_STANDARD_SERVER_SOLUTIONS_CORE    0x00000035
	#define PRODUCT_SB_SOLUTION_SERVER_EM             0x00000036
	#define PRODUCT_SERVER_FOR_SB_SOLUTIONS_EM        0x00000037
	#define PRODUCT_SOLUTION_EMBEDDEDSERVER           0x00000038
	#define PRODUCT_SOLUTION_EMBEDDEDSERVER_CORE      0x00000039
	#define PRODUCT_ESSENTIALBUSINESS_SERVER_MGMT     0x0000003B
	#define PRODUCT_ESSENTIALBUSINESS_SERVER_ADDL     0x0000003C
	#define PRODUCT_ESSENTIALBUSINESS_SERVER_MGMTSVC  0x0000003D
	#define PRODUCT_ESSENTIALBUSINESS_SERVER_ADDLSVC  0x0000003E
	#define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM_CORE 0x0000003f
	#define PRODUCT_CLUSTER_SERVER_V                  0x00000040
	#define PRODUCT_EMBEDDED                          0x00000041
	#define PRODUCT_STARTER_E                         0x00000042
	#define PRODUCT_HOME_BASIC_E                      0x00000043
	#define PRODUCT_HOME_PREMIUM_E                    0x00000044
	#define PRODUCT_PROFESSIONAL_E                    0x00000045
	#define PRODUCT_ENTERPRISE_E                      0x00000046
	#define PRODUCT_ULTIMATE_E                        0x00000047
#endif
#ifndef PRODUCT_CORE_N // Win8
	#define PRODUCT_CORE_N                            0x00000062
	#define PRODUCT_CORE_COUNTRYSPECIFIC              0x00000063
	#define PRODUCT_CORE_SINGLELANGUAGE               0x00000064
	#define PRODUCT_CORE                              0x00000065
	#define PRODUCT_PROFESSIONAL_WMC                  0x00000067
	#define PRODUCT_UNLICENSED                        0xABCDABCD
#endif

typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);

#define StringCchCopy(x,y,z)      lstrcpyn((x),(z),(y))
#define StringCchCat(x,y,z)       lstrcat((x),(z))
#define StringCchPrintf           mir_sntprintf

// slightly modified sample from MSDN
BOOL GetOSDisplayString(LPTSTR pszOS, int BUFSIZE)
{
	OSVERSIONINFOEX osvi;
	SYSTEM_INFO si;
	PGNSI pGNSI;
	PGPI pGPI;

	DWORD dwType;

	ZeroMemory(&si, sizeof(SYSTEM_INFO));
	ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));

	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

	BOOL bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *) &osvi);
	if ( !bOsVersionInfoEx)
	{
		osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
		if ( !GetVersionEx((OSVERSIONINFO*)&osvi))
			return FALSE;
	}

	// Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
	HMODULE hKernel = GetModuleHandle(TEXT("kernel32.dll"));
	pGNSI = (PGNSI) GetProcAddress(hKernel, "GetNativeSystemInfo");
	if (pGNSI != NULL)
		pGNSI(&si);
	else
		GetSystemInfo(&si);

	// Some code from Crash Dumper Plugin :-)
	if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4)
	{
		StringCchCopy(pszOS, BUFSIZE, TEXT("Microsoft "));

		// Test for the specific product.
		if (osvi.dwMajorVersion == 6)
		{
			switch (osvi.dwMinorVersion)
			{
			case 0:
				if (osvi.wProductType == VER_NT_WORKSTATION)
					StringCchCat(pszOS, BUFSIZE, TEXT("Windows Vista "));
				else
					StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 "));
				break;

			case 1:
				if (osvi.wProductType == VER_NT_WORKSTATION)
					StringCchCat(pszOS, BUFSIZE, TEXT("Windows 7 "));
				else
					StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 R2 "));
				break;

			case 2:
				if (osvi.wProductType == VER_NT_WORKSTATION)
					StringCchCat(pszOS, BUFSIZE, TEXT("Windows 8 "));
				else
					StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2012 "));
				break;
			}

			pGPI = (PGPI) GetProcAddress(hKernel, "GetProductInfo");
			if (pGPI != NULL)
				pGPI(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);

			switch(dwType)
			{
			case PRODUCT_ULTIMATE:
				StringCchCat(pszOS, BUFSIZE, TEXT("Ultimate Edition"));
				break;
			case PRODUCT_PROFESSIONAL:
				StringCchCat(pszOS, BUFSIZE, TEXT("Professional Edition"));
				break;
			case PRODUCT_PROFESSIONAL_WMC:
				StringCchCat(pszOS, BUFSIZE, TEXT("Professional Edition with Media Center"));
				break;
			case PRODUCT_HOME_PREMIUM:
				StringCchCat(pszOS, BUFSIZE, TEXT("Home Premium Edition"));
				break;
			case PRODUCT_HOME_BASIC:
				StringCchCat(pszOS, BUFSIZE, TEXT("Home Basic Edition"));
				break;
			case PRODUCT_ENTERPRISE:
				StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition"));
				break;
			case PRODUCT_BUSINESS:
				StringCchCat(pszOS, BUFSIZE, TEXT("Business Edition"));
				break;
			case PRODUCT_STARTER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Starter Edition"));
				break;
			case PRODUCT_CLUSTER_SERVER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Cluster Server Edition"));
				break;
			case PRODUCT_DATACENTER_SERVER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition"));
				break;
			case PRODUCT_DATACENTER_SERVER_CORE:
				StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition (core installation)"));
				break;
			case PRODUCT_ENTERPRISE_SERVER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition"));
				break;
			case PRODUCT_ENTERPRISE_SERVER_CORE:
				StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition (core installation)"));
				break;
			case PRODUCT_ENTERPRISE_SERVER_IA64:
				StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems"));
				break;
			case PRODUCT_SMALLBUSINESS_SERVER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server"));
				break;
			case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
				StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server Premium Edition"));
				break;
			case PRODUCT_STANDARD_SERVER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition"));
				break;
			case PRODUCT_STANDARD_SERVER_CORE:
				StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition (core installation)"));
				break;
			case PRODUCT_WEB_SERVER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Web Server Edition"));
				break;
			}
			if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
				StringCchCat(pszOS, BUFSIZE, TEXT(", 64-bit"));
			else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL)
				StringCchCat(pszOS, BUFSIZE, TEXT(", 32-bit"));
		}

		if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
		{
			if (GetSystemMetrics(SM_SERVERR2))
				StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2003 R2, "));
			else if (osvi.wSuiteMask==VER_SUITE_STORAGE_SERVER)
				StringCchCat(pszOS, BUFSIZE, TEXT("Windows Storage Server 2003"));
			else if (osvi.wSuiteMask==VER_SUITE_WH_SERVER)
				StringCchCat(pszOS, BUFSIZE, TEXT("Windows Home Server"));
			else if (osvi.wProductType == VER_NT_WORKSTATION &&
				si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
			{
				StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP Professional x64 Edition"));
			}
			else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2003, "));

			// Test for the server type.
			if (osvi.wProductType != VER_NT_WORKSTATION)
			{
				if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64)
				{
					if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
						StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition for Itanium-based Systems"));
					else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
						StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems"));
				}

				else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
				{
					if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
						StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter x64 Edition"));
					else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
						StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise x64 Edition"));
					else StringCchCat(pszOS, BUFSIZE, TEXT("Standard x64 Edition"));
				}

				else
				{
					if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
						StringCchCat(pszOS, BUFSIZE, TEXT("Compute Cluster Edition"));
					else if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
						StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition"));
					else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
						StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition"));
					else if (osvi.wSuiteMask & VER_SUITE_BLADE)
						StringCchCat(pszOS, BUFSIZE, TEXT("Web Edition"));
					else StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition"));
				}
			}
		}

		if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
		{
			StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP "));
			if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
				StringCchCat(pszOS, BUFSIZE, TEXT("Home Edition"));
			else StringCchCat(pszOS, BUFSIZE, TEXT("Professional"));
		}

		if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
		{
			StringCchCat(pszOS, BUFSIZE, TEXT("Windows 2000 "));

			if (osvi.wProductType == VER_NT_WORKSTATION)
			{
				StringCchCat(pszOS, BUFSIZE, TEXT("Professional"));
			}
			else
			{
				if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
					StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Server"));
				else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
					StringCchCat(pszOS, BUFSIZE, TEXT("Advanced Server"));
				else StringCchCat(pszOS, BUFSIZE, TEXT("Server"));
			}
		}

		// Include service pack (if any) and build number.

		if (_tcslen(osvi.szCSDVersion) > 0)
		{
			StringCchCat(pszOS, BUFSIZE, TEXT(" "));
			StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion);
		}

		TCHAR buf[80];

		StringCchPrintf(buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber);
		StringCchCat(pszOS, BUFSIZE, buf);

		return TRUE;
	}
	else
	{
		return FALSE;
	}
}


BOOL CJabberProto::OnIqRequestVersion(HXML, CJabberIqInfo* pInfo)
{
	if ( !pInfo->GetFrom())
		return TRUE;

	if ( !m_options.AllowVersionRequests)
		return FALSE;

	XmlNodeIq iq(_T("result"), pInfo);
	HXML query = iq << XQUERY(_T(JABBER_FEAT_VERSION));
	query << XCHILD(_T("name"), _T("Miranda NG Jabber"));
	query << XCHILD(_T("version"), szCoreVersion);

	if (m_options.ShowOSVersion)
	{
		TCHAR os[256] = {0};
		if ( !GetOSDisplayString(os, SIZEOF(os)))
			lstrcpyn(os, _T("Microsoft Windows"), SIZEOF(os));
		query << XCHILD(_T("os"), os);
	}

	m_ThreadInfo->send(iq);
	return TRUE;
}

// last activity (XEP-0012) support
BOOL CJabberProto::OnIqRequestLastActivity(HXML, CJabberIqInfo *pInfo)
{
	m_ThreadInfo->send(
		XmlNodeIq(_T("result"), pInfo) << XQUERY(_T(JABBER_FEAT_LAST_ACTIVITY))
			<< XATTRI(_T("seconds"), m_tmJabberIdleStartTime ? time(0) - m_tmJabberIdleStartTime : 0));
	return TRUE;
}

// XEP-0199: XMPP Ping support
BOOL CJabberProto::OnIqRequestPing(HXML, CJabberIqInfo *pInfo)
{
	m_ThreadInfo->send( XmlNodeIq(_T("result"), pInfo) << XATTR(_T("from"), m_ThreadInfo->fullJID));
	return TRUE;
}

// Returns the current GMT offset in seconds
int GetGMTOffset(void)
{
	TIME_ZONE_INFORMATION tzinfo;
	int nOffset = 0;

	DWORD dwResult= GetTimeZoneInformation(&tzinfo);

	switch(dwResult) {
	case TIME_ZONE_ID_STANDARD:
		nOffset = tzinfo.Bias + tzinfo.StandardBias;
		break;
	case TIME_ZONE_ID_DAYLIGHT:
		nOffset = tzinfo.Bias + tzinfo.DaylightBias;
		break;
	case TIME_ZONE_ID_UNKNOWN:
		nOffset = tzinfo.Bias;
		break;
	case TIME_ZONE_ID_INVALID:
	default:
		nOffset = 0;
		break;
	}

	return -nOffset;
}

// entity time (XEP-0202) support
BOOL CJabberProto::OnIqRequestTime(HXML, CJabberIqInfo *pInfo)
{
	TCHAR stime[100];
	TCHAR szTZ[10];

	tmi.printDateTime(UTC_TIME_HANDLE, _T("I"), stime, SIZEOF(stime), 0);

	int nGmtOffset = GetGMTOffset();
	mir_sntprintf(szTZ, SIZEOF(szTZ), _T("%+03d:%02d"), nGmtOffset / 60, nGmtOffset % 60);

	XmlNodeIq iq(_T("result"), pInfo);
	HXML timeNode = iq << XCHILDNS(_T("time"), _T(JABBER_FEAT_ENTITY_TIME));
	timeNode << XCHILD(_T("utc"), stime); timeNode << XCHILD(_T("tzo"), szTZ);
	LPCTSTR szTZName = tmi.getTzName(NULL);
	if (szTZName)
		timeNode << XCHILD(_T("tz"), szTZName);
	m_ThreadInfo->send(iq);
	return TRUE;
}

BOOL CJabberProto::OnIqProcessIqOldTime(HXML, CJabberIqInfo *pInfo)
{
	struct tm *gmt;
	time_t ltime;
	TCHAR stime[ 100 ], *dtime;

	_tzset();
	time(&ltime);
	gmt = gmtime(&ltime);
	mir_sntprintf(stime, SIZEOF(stime), _T("%.4i%.2i%.2iT%.2i:%.2i:%.2i"),
		gmt->tm_year + 1900, gmt->tm_mon + 1,
		gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
	dtime = _tctime(&ltime);
	dtime[ 24 ] = 0;

	XmlNodeIq iq(_T("result"), pInfo);
	HXML queryNode = iq << XQUERY(_T(JABBER_FEAT_ENTITY_TIME_OLD));
	queryNode << XCHILD(_T("utc"), stime);
	LPCTSTR szTZName = tmi.getTzName(NULL);
	if (szTZName)
		queryNode << XCHILD(_T("tz"), szTZName);
	queryNode << XCHILD(_T("display"), dtime);
	m_ThreadInfo->send(iq);
	return TRUE;
}

BOOL CJabberProto::OnIqRequestAvatar(HXML, CJabberIqInfo *pInfo)
{
	if ( !m_options.EnableAvatars)
		return TRUE;

	int pictureType = m_options.AvatarType;
	if (pictureType == PA_FORMAT_UNKNOWN)
		return TRUE;

	TCHAR* szMimeType;
	switch(pictureType) {
		case PA_FORMAT_JPEG:	 szMimeType = _T("image/jpeg");   break;
		case PA_FORMAT_GIF:	 szMimeType = _T("image/gif");    break;
		case PA_FORMAT_PNG:	 szMimeType = _T("image/png");    break;
		case PA_FORMAT_BMP:	 szMimeType = _T("image/bmp");    break;
		default:	return TRUE;
	}

	TCHAR szFileName[ MAX_PATH ];
	GetAvatarFileName(NULL, szFileName, SIZEOF(szFileName));

	FILE* in = _tfopen(szFileName, _T("rb"));
	if (in == NULL)
		return TRUE;

	long bytes = _filelength(_fileno(in));
	char* buffer = (char*)mir_alloc(bytes*4/3 + bytes + 1000);
	if (buffer == NULL) {
		fclose(in);
		return TRUE;
	}

	fread(buffer, bytes, 1, in);
	fclose(in);

	char* str = JabberBase64Encode(buffer, bytes);
	m_ThreadInfo->send( XmlNodeIq(_T("result"), pInfo) << XQUERY(_T(JABBER_FEAT_AVATAR)) << XCHILD(_T("query"), _A2T(str)) << XATTR(_T("mimetype"), szMimeType));
	mir_free(str);
	mir_free(buffer);
	return TRUE;
}

BOOL CJabberProto::OnSiRequest(HXML node, CJabberIqInfo *pInfo)
{
	const TCHAR *szProfile = xmlGetAttrValue(pInfo->GetChildNode(), _T("profile"));

	if (szProfile && !_tcscmp(szProfile, _T(JABBER_FEAT_SI_FT)))
		FtHandleSiRequest(node);
	else {
		XmlNodeIq iq(_T("error"), pInfo);
		HXML error = iq << XCHILD(_T("error")) << XATTRI(_T("code"), 400) << XATTR(_T("type"), _T("cancel"));
		error << XCHILDNS(_T("bad-request"), _T("urn:ietf:params:xml:ns:xmpp-stanzas"));
		error << XCHILD(_T("bad-profile"));
		m_ThreadInfo->send(iq);
	}
	return TRUE;
}

BOOL CJabberProto::OnRosterPushRequest(HXML, CJabberIqInfo *pInfo)
{
	HXML queryNode = pInfo->GetChildNode();

	// RFC 3921 #7.2 Business Rules
	if (pInfo->GetFrom()) {
		TCHAR* szFrom = JabberPrepareJid(pInfo->GetFrom());
		if ( !szFrom)
			return TRUE;

		TCHAR* szTo = JabberPrepareJid(m_ThreadInfo->fullJID);
		if ( !szTo) {
			mir_free(szFrom);
			return TRUE;
		}

		TCHAR* pDelimiter = _tcschr(szFrom, _T('/'));
		if (pDelimiter) *pDelimiter = _T('\0');

		pDelimiter = _tcschr(szTo, _T('/'));
		if (pDelimiter) *pDelimiter = _T('\0');

		BOOL bRetVal = _tcscmp(szFrom, szTo) == 0;

		mir_free(szFrom);
		mir_free(szTo);

		// invalid JID
		if ( !bRetVal) {
			Log("<iq/> attempt to hack via roster push from " TCHAR_STR_PARAM, pInfo->GetFrom());
			return TRUE;
		}
	}

	JABBER_LIST_ITEM *item;
	HANDLE hContact = NULL;
	const TCHAR *jid, *str, *name;
	TCHAR* nick;

	Log("<iq/> Got roster push, query has %d children", xmlGetChildCount(queryNode));
	for (int i=0; ; i++) {
		HXML itemNode = xmlGetChild(queryNode ,i);
		if ( !itemNode)
			break;

		if (_tcscmp(xmlGetName(itemNode), _T("item")) != 0)
			continue;
		if ((jid = xmlGetAttrValue(itemNode, _T("jid"))) == NULL)
			continue;
		if ((str = xmlGetAttrValue(itemNode, _T("subscription"))) == NULL)
			continue;

		// we will not add new account when subscription=remove
		if ( !_tcscmp(str, _T("to")) || !_tcscmp(str, _T("both")) || !_tcscmp(str, _T("from")) || !_tcscmp(str, _T("none"))) {
			if ((name = xmlGetAttrValue(itemNode, _T("name"))) != NULL)
				nick = mir_tstrdup(name);
			else
				nick = JabberNickFromJID(jid);

			if (nick != NULL) {
				if ((item=ListAdd(LIST_ROSTER, jid)) != NULL) {
					replaceStrT(item->nick, nick);

					HXML groupNode = xmlGetChild(itemNode , "group");
					replaceStrT(item->group, (groupNode) ? xmlGetText(groupNode) : NULL);

					if ((hContact=HContactFromJID(jid, 0)) == NULL) {
						// Received roster has a new JID.
						// Add the jid (with empty resource) to Miranda contact list.
						hContact = DBCreateContact(jid, nick, FALSE, FALSE);
					}
					else JSetStringT(hContact, "jid", jid);

					if (name != NULL) {
						DBVARIANT dbnick;
						if ( !JGetStringT(hContact, "Nick", &dbnick)) {
							if (_tcscmp(nick, dbnick.ptszVal) != 0)
								db_set_ts(hContact, "CList", "MyHandle", nick);
							else
								db_unset(hContact, "CList", "MyHandle");

							db_free(&dbnick);
						}
						else db_set_ts(hContact, "CList", "MyHandle", nick);
					}
					else db_unset(hContact, "CList", "MyHandle");

					if ( !m_options.IgnoreRosterGroups)
					{
						if (item->group != NULL) {
							JabberContactListCreateGroup(item->group);
							db_set_ts(hContact, "CList", "Group", item->group);
						}
						else
							db_unset(hContact, "CList", "Group");
					}
				}
				mir_free(nick);
		}	}

		if ((item=ListGetItemPtr(LIST_ROSTER, jid)) != NULL) {
			if ( !_tcscmp(str, _T("both"))) item->subscription = SUB_BOTH;
			else if ( !_tcscmp(str, _T("to"))) item->subscription = SUB_TO;
			else if ( !_tcscmp(str, _T("from"))) item->subscription = SUB_FROM;
			else item->subscription = SUB_NONE;
			Log("Roster push for jid=" TCHAR_STR_PARAM ", set subscription to " TCHAR_STR_PARAM, jid, str);
			// subscription = remove is to remove from roster list
			// but we will just set the contact to offline and not actually
			// remove, so that history will be retained.
			if ( !_tcscmp(str, _T("remove"))) {
				if ((hContact=HContactFromJID(jid)) != NULL) {
					SetContactOfflineStatus(hContact);
					ListRemove(LIST_ROSTER, jid);
			}	}
			else if (JGetByte(hContact, "ChatRoom", 0))
				db_unset(hContact, "CList", "Hidden");
			else
				UpdateSubscriptionInfo(hContact, item);
	}	}

	UI_SAFE_NOTIFY(m_pDlgServiceDiscovery, WM_JABBER_TRANSPORT_REFRESH);
	RebuildInfoFrame();
	return TRUE;
}

BOOL CJabberProto::OnIqRequestOOB(HXML, CJabberIqInfo *pInfo)
{
	if ( !pInfo->GetFrom() || !pInfo->GetHContact())
		return TRUE;

	HXML n = xmlGetChild(pInfo->GetChildNode(), "url");
	if ( !n || !xmlGetText(n))
		return TRUE;

	if (m_options.BsOnlyIBB) {
		// reject
		XmlNodeIq iq(_T("error"), pInfo);
		HXML e = xmlAddChild(iq, _T("error"), _T("File transfer refused")); xmlAddAttr(e, _T("code"), 406);
		m_ThreadInfo->send(iq);
		return TRUE;
	}

	TCHAR text[ 1024 ];
	TCHAR *str, *p, *q;

	str = (TCHAR*)xmlGetText(n);	// URL of the file to get
	filetransfer *ft = new filetransfer(this);
	ft->std.totalFiles = 1;
	ft->jid = mir_tstrdup(pInfo->GetFrom());
	ft->std.hContact = pInfo->GetHContact();
	ft->type = FT_OOB;
	ft->httpHostName = NULL;
	ft->httpPort = 80;
	ft->httpPath = NULL;

	// Parse the URL
	if ( !_tcsnicmp(str, _T("http://"), 7)) {
		p = str + 7;
		if ((q = _tcschr(p, '/')) != NULL) {
			if (q-p < SIZEOF(text)) {
				_tcsncpy(text, p, q-p);
				text[q-p] = '\0';
				if ((p = _tcschr(text, ':')) != NULL) {
					ft->httpPort = (WORD)_ttoi(p+1);
					*p = '\0';
				}
				ft->httpHostName = mir_t2a(text);
	}	}	}

	if (pInfo->GetIdStr())
		ft->iqId = mir_tstrdup(pInfo->GetIdStr());

	if (ft->httpHostName && ft->httpPath) {
		TCHAR* desc = NULL;

		Log("Host=%s Port=%d Path=%s", ft->httpHostName, ft->httpPort, ft->httpPath);
		if ((n = xmlGetChild(pInfo->GetChildNode(), "desc")) != NULL)
			desc = (TCHAR*)xmlGetText(n);

		TCHAR* str2;
		Log("description = %s", desc);
		if ((str2 = _tcsrchr(ft->httpPath, '/')) != NULL)
			str2++;
		else
			str2 = ft->httpPath;
		str2 = mir_tstrdup(str2);
		JabberHttpUrlDecode(str2);

		PROTORECVFILET pre;
		pre.flags = PREF_TCHAR;
		pre.timestamp = time(NULL);
		pre.tszDescription = desc;
		pre.ptszFiles = &str2;
		pre.fileCount = 1;
		pre.lParam = (LPARAM)ft;
		ProtoChainRecvFile(ft->std.hContact, &pre);
		mir_free(str2);
	}
	else {
		// reject
		XmlNodeIq iq(_T("error"), pInfo);
		HXML e = xmlAddChild(iq, _T("error"), _T("File transfer refused")); xmlAddAttr(e, _T("code"), 406);
		m_ThreadInfo->send(iq);
		delete ft;
	}
	return TRUE;
}

BOOL CJabberProto::OnHandleDiscoInfoRequest(HXML iqNode, CJabberIqInfo* pInfo)
{
	if ( !pInfo->GetChildNode())
		return TRUE;

	const TCHAR *szNode = xmlGetAttrValue(pInfo->GetChildNode(), _T("node"));
	// caps hack
	if (m_clientCapsManager.HandleInfoRequest(iqNode, pInfo, szNode))
		return TRUE;

	// ad-hoc hack:
	if (szNode && m_adhocManager.HandleInfoRequest(iqNode, pInfo, szNode))
		return TRUE;

	// another request, send empty result
	m_ThreadInfo->send(
		XmlNodeIq(_T("error"), pInfo)
			<< XCHILD(_T("error")) << XATTRI(_T("code"), 404) << XATTR(_T("type"), _T("cancel"))
				<< XCHILDNS(_T("item-not-found"), _T("urn:ietf:params:xml:ns:xmpp-stanzas")));
	return TRUE;
}

BOOL CJabberProto::OnHandleDiscoItemsRequest(HXML iqNode, CJabberIqInfo* pInfo)
{
	if ( !pInfo->GetChildNode())
		return TRUE;

	// ad-hoc commands check:
	const TCHAR *szNode = xmlGetAttrValue(pInfo->GetChildNode(), _T("node"));
	if (szNode && m_adhocManager.HandleItemsRequest(iqNode, pInfo, szNode))
		return TRUE;

	// another request, send empty result
	XmlNodeIq iq(_T("result"), pInfo);
	HXML resultQuery = iq << XQUERY(_T(JABBER_FEAT_DISCO_ITEMS));
	if (szNode)
		xmlAddAttr(resultQuery, _T("node"), szNode);

	if ( !szNode && m_options.EnableRemoteControl)
		resultQuery << XCHILD(_T("item")) << XATTR(_T("jid"), m_ThreadInfo->fullJID)
			<< XATTR(_T("node"), _T(JABBER_FEAT_COMMANDS)) << XATTR(_T("name"), _T("Ad-hoc commands"));

	m_ThreadInfo->send(iq);
	return TRUE;
}

BOOL CJabberProto::AddClistHttpAuthEvent(CJabberHttpAuthParams *pParams)
{
	CLISTEVENT cle = {0};
	char szService[256];
	mir_snprintf(szService, sizeof(szService),"%s%s", m_szModuleName, JS_HTTP_AUTH);
	cle.cbSize = sizeof(CLISTEVENT);
	cle.hIcon = (HICON) LoadIconEx("openid");
	cle.flags = CLEF_PROTOCOLGLOBAL | CLEF_TCHAR;
	cle.hDbEvent = (HANDLE)("test");
	cle.lParam = (LPARAM) pParams;
	cle.pszService = szService;
	cle.ptszTooltip = TranslateT("Http authentication request received");
	CallService(MS_CLIST_ADDEVENT, 0, (LPARAM)&cle);

	return TRUE;
}

BOOL CJabberProto::OnIqHttpAuth(HXML node, CJabberIqInfo* pInfo)
{
	if ( !m_options.AcceptHttpAuth)
		return TRUE;

	if ( !node || !pInfo->GetChildNode() || !pInfo->GetFrom() || !pInfo->GetIdStr())
		return TRUE;

	HXML pConfirm = xmlGetChild(node , "confirm");
	if ( !pConfirm)
		return TRUE;

	const TCHAR *szId = xmlGetAttrValue(pConfirm, _T("id"));
	const TCHAR *szMethod = xmlGetAttrValue(pConfirm, _T("method"));
	const TCHAR *szUrl = xmlGetAttrValue(pConfirm, _T("url"));

	if ( !szId || !szMethod || !szUrl)
		return TRUE;

	CJabberHttpAuthParams *pParams = (CJabberHttpAuthParams *)mir_alloc(sizeof(CJabberHttpAuthParams));
	if ( !pParams)
		return TRUE;
	ZeroMemory(pParams, sizeof(CJabberHttpAuthParams));
	pParams->m_nType = CJabberHttpAuthParams::IQ;
	pParams->m_szFrom = mir_tstrdup(pInfo->GetFrom());
	pParams->m_szId = mir_tstrdup(szId);
	pParams->m_szMethod = mir_tstrdup(szMethod);
	pParams->m_szUrl = mir_tstrdup(szUrl);

	AddClistHttpAuthEvent(pParams);

	return TRUE;
}