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

Jabber Protocol Plugin for Miranda NG

Copyright (c) 2002-04  Santithorn Bunchua
Copyright (c) 2005-12  George Hazan
Copyright (c) 2007     Maxim Mluhov
Copyright (ñ) 2012-16 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 "stdafx.h"
#include "jabber_iq.h"
#include "jabber_rc.h"
#include "version.h"

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

	if (!m_options.AllowVersionRequests)
		return FALSE;

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

	if (m_options.ShowOSVersion) {
		wchar_t os[256] = { 0 };
		if (!GetOSDisplayString(os, _countof(os)))
			mir_wstrncpy(os, L"Microsoft Windows", _countof(os));
		query << XCHILD(L"os", os);
	}

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

// last activity (XEP-0012) support
BOOL CJabberProto::OnIqRequestLastActivity(HXML, CJabberIqInfo *pInfo)
{
	m_ThreadInfo->send(
		XmlNodeIq(L"result", pInfo) << XQUERY(JABBER_FEAT_LAST_ACTIVITY)
		<< XATTRI(L"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(L"result", pInfo) << XATTR(L"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)
{
	wchar_t stime[100];
	wchar_t szTZ[10];

	TimeZone_PrintDateTime(UTC_TIME_HANDLE, L"I", stime, _countof(stime), 0);

	int nGmtOffset = GetGMTOffset();
	mir_snwprintf(szTZ, L"%+03d:%02d", nGmtOffset / 60, nGmtOffset % 60);

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

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

	_tzset();
	time(&ltime);
	gmt = gmtime(&ltime);
	mir_snwprintf(stime, L"%.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 = _wctime(&ltime);
	dtime[24] = 0;

	XmlNodeIq iq(L"result", pInfo);
	HXML queryNode = iq << XQUERY(JABBER_FEAT_ENTITY_TIME_OLD);
	queryNode << XCHILD(L"utc", stime);
	LPCTSTR szTZName = TimeZone_GetName(NULL);
	if (szTZName)
		queryNode << XCHILD(L"tz", szTZName);
	queryNode << XCHILD(L"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;

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

	wchar_t szFileName[MAX_PATH];
	GetAvatarFileName(NULL, szFileName, _countof(szFileName));

	FILE* in = _wfopen(szFileName, L"rb");
	if (in == NULL)
		return TRUE;

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

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

	ptrA str(mir_base64_encode((PBYTE)(char*)buffer, bytes));
	m_ThreadInfo->send(XmlNodeIq(L"result", pInfo) << XQUERY(JABBER_FEAT_AVATAR) << XCHILD(L"query", _A2T(str)) << XATTR(L"mimetype", szMimeType));
	return TRUE;
}

BOOL CJabberProto::OnSiRequest(HXML node, CJabberIqInfo *pInfo)
{
	const wchar_t *szProfile = XmlGetAttrValue(pInfo->GetChildNode(), L"profile");

	if (szProfile && !mir_wstrcmp(szProfile, JABBER_FEAT_SI_FT))
		FtHandleSiRequest(node);
	else {
		XmlNodeIq iq(L"error", pInfo);
		HXML error = iq << XCHILD(L"error") << XATTRI(L"code", 400) << XATTR(L"type", L"cancel");
		error << XCHILDNS(L"bad-request", L"urn:ietf:params:xml:ns:xmpp-stanzas");
		error << XCHILD(L"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()) {
		wchar_t *szFrom = JabberPrepareJid(pInfo->GetFrom());
		if (!szFrom)
			return TRUE;

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

		wchar_t *pDelimiter = wcschr(szFrom, '/');
		if (pDelimiter) *pDelimiter = 0;

		pDelimiter = wcschr(szTo, '/');
		if (pDelimiter) *pDelimiter = 0;

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

		mir_free(szFrom);
		mir_free(szTo);

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

	const wchar_t *jid, *str;

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

		if (mir_wstrcmp(XmlGetName(itemNode), L"item") != 0)
			continue;
		if ((jid = XmlGetAttrValue(itemNode, L"jid")) == NULL)
			continue;
		if ((str = XmlGetAttrValue(itemNode, L"subscription")) == NULL)
			continue;

		// we will not add new account when subscription=remove
		if (!mir_wstrcmp(str, L"to") || !mir_wstrcmp(str, L"both") || !mir_wstrcmp(str, L"from") || !mir_wstrcmp(str, L"none")) {
			const wchar_t *name = XmlGetAttrValue(itemNode, L"name");
			ptrW nick((name != NULL) ? mir_wstrdup(name) : JabberNickFromJID(jid));
			if (nick != NULL) {
				MCONTACT hContact = HContactFromJID(jid, false);
				if (hContact == NULL)
					hContact = DBCreateContact(jid, nick, false, false);
				else
					setWString(hContact, "jid", jid);

				JABBER_LIST_ITEM *item = ListAdd(LIST_ROSTER, jid, hContact);
				replaceStrW(item->nick, nick);
				item->bRealContact = true;

				HXML groupNode = XmlGetChild(itemNode, "group");
				replaceStrW(item->group, XmlGetText(groupNode));

				if (name != NULL) {
					ptrW tszNick(getWStringA(hContact, "Nick"));
					if (tszNick != NULL) {
						if (mir_wstrcmp(nick, tszNick) != 0)
							db_set_ws(hContact, "CList", "MyHandle", nick);
						else
							db_unset(hContact, "CList", "MyHandle");
					}
					else db_set_ws(hContact, "CList", "MyHandle", nick);
				}
				else db_unset(hContact, "CList", "MyHandle");

				if (!m_options.IgnoreRosterGroups) {
					if (item->group != NULL) {
						Clist_GroupCreate(0, item->group);
						db_set_ws(hContact, "CList", "Group", item->group);
					}
					else db_unset(hContact, "CList", "Group");
				}
			}
		}

		if (JABBER_LIST_ITEM *item = ListGetItemPtr(LIST_ROSTER, jid)) {
			if (!mir_wstrcmp(str, L"both")) item->subscription = SUB_BOTH;
			else if (!mir_wstrcmp(str, L"to")) item->subscription = SUB_TO;
			else if (!mir_wstrcmp(str, L"from")) item->subscription = SUB_FROM;
			else item->subscription = SUB_NONE;
			debugLogW(L"Roster push for jid=%s, set subscription to %s", jid, str);

			MCONTACT hContact = HContactFromJID(jid);

			// 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 (!mir_wstrcmp(str, L"remove")) {
				if (hContact) {
					SetContactOfflineStatus(hContact);
					ListRemove(LIST_ROSTER, jid);
				}
			}
			else if (isChatRoom(hContact))
				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(L"error", pInfo);
		HXML e = XmlAddChild(iq, L"error", L"File transfer refused"); XmlAddAttr(e, L"code", 406);
		m_ThreadInfo->send(iq);
		return TRUE;
	}

	filetransfer *ft = new filetransfer(this);
	ft->std.totalFiles = 1;
	ft->jid = mir_wstrdup(pInfo->GetFrom());
	ft->std.hContact = pInfo->GetHContact();
	ft->type = FT_OOB;
	ft->httpHostName = NULL;
	ft->httpPort = 80;
	ft->httpPath = NULL;

	// Parse the URL
	wchar_t *str = (wchar_t*)XmlGetText(n);	// URL of the file to get
	if (!wcsnicmp(str, L"http://", 7)) {
		wchar_t *p = str + 7, *q;
		if ((q = wcschr(p, '/')) != NULL) {
			wchar_t text[1024];
			if (q - p < _countof(text)) {
				wcsncpy_s(text, p, q - p);
				text[q - p] = '\0';
				if ((p = wcschr(text, ':')) != NULL) {
					ft->httpPort = (WORD)_wtoi(p + 1);
					*p = '\0';
				}
				ft->httpHostName = mir_u2a(text);
			}
		}
	}

	if (pInfo->GetIdStr())
		ft->szId = JabberId2string(pInfo->GetIqId());

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

		debugLogA("Host=%s Port=%d Path=%s", ft->httpHostName, ft->httpPort, ft->httpPath);
		if ((n = XmlGetChild(pInfo->GetChildNode(), "desc")) != NULL)
			desc = (wchar_t*)XmlGetText(n);

		wchar_t *str2;
		debugLogW(L"description = %s", desc);
		if ((str2 = wcsrchr(ft->httpPath, '/')) != NULL)
			str2++;
		else
			str2 = ft->httpPath;
		str2 = mir_wstrdup(str2);
		JabberHttpUrlDecode(str2);

		PROTORECVFILET pre;
		pre.dwFlags = PRFF_UNICODE;
		pre.timestamp = time(NULL);
		pre.descr.w = desc;
		pre.files.w = &str2;
		pre.fileCount = 1;
		pre.lParam = (LPARAM)ft;
		ProtoChainRecvFile(ft->std.hContact, &pre);
		mir_free(str2);
	}
	else {
		// reject
		XmlNodeIq iq(L"error", pInfo);
		HXML e = XmlAddChild(iq, L"error", L"File transfer refused"); XmlAddAttr(e, L"code", 406);
		m_ThreadInfo->send(iq);
		delete ft;
	}
	return TRUE;
}

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

	const wchar_t *szNode = XmlGetAttrValue(pInfo->GetChildNode(), L"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(L"error", pInfo)
		<< XCHILD(L"error") << XATTRI(L"code", 404) << XATTR(L"type", L"cancel")
		<< XCHILDNS(L"item-not-found", L"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 wchar_t *szNode = XmlGetAttrValue(pInfo->GetChildNode(), L"node");
	if (szNode && m_adhocManager.HandleItemsRequest(iqNode, pInfo, szNode))
		return TRUE;

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

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

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

BOOL CJabberProto::AddClistHttpAuthEvent(CJabberHttpAuthParams *pParams)
{
	char szService[256];
	mir_snprintf(szService, "%s%s", m_szModuleName, JS_HTTP_AUTH);

	CLISTEVENT cle = {};
	cle.hIcon = (HICON)LoadIconEx("openid");
	cle.flags = CLEF_PROTOCOLGLOBAL | CLEF_TCHAR;
	cle.hDbEvent = -99;
	cle.lParam = (LPARAM)pParams;
	cle.pszService = szService;
	cle.ptszTooltip = TranslateT("Http authentication request received");
	pcli->pfnAddEvent(&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 wchar_t *szId = XmlGetAttrValue(pConfirm, L"id");
	const wchar_t *szMethod = XmlGetAttrValue(pConfirm, L"method");
	const wchar_t *szUrl = XmlGetAttrValue(pConfirm, L"url");
	if (!szId || !szMethod || !szUrl)
		return TRUE;

	CJabberHttpAuthParams *pParams = (CJabberHttpAuthParams*)mir_calloc(sizeof(CJabberHttpAuthParams));
	if (pParams) {
		pParams->m_nType = CJabberHttpAuthParams::IQ;
		pParams->m_szFrom = mir_wstrdup(pInfo->GetFrom());
		pParams->m_szId = mir_wstrdup(szId);
		pParams->m_szMethod = mir_wstrdup(szMethod);
		pParams->m_szUrl = mir_wstrdup(szUrl);
		AddClistHttpAuthEvent(pParams);
	}
	return TRUE;
}