summaryrefslogtreecommitdiff
path: root/plugins/NewsAggregator/Src/CheckFeed.cpp
blob: 7daad6dc2fc3bd5f3debb5ddf2d144135fd25516 (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
/*
Copyright (C) 2012 Mataes

This is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this file; see the file license.txt.  If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/

#include "common.h"

TCHAR* CheckFeed(TCHAR *tszURL, HWND hwndDlg)
{
	Netlib_LogfT(hNetlibUser, _T("Started validating feed %s."), tszURL);
	char *szData = NULL;
	GetNewsData(tszURL, &szData, NULL, hwndDlg);
	if (szData) {
		TCHAR *tszData = mir_utf8decodeT(szData);
		if (!tszData)
			tszData = mir_a2t(szData);
		int bytesParsed = 0;
		HXML hXml = xi.parseString(tszData, &bytesParsed, NULL);
		mir_free(tszData);
		mir_free(szData);
		if (hXml != NULL) {
			int childcount = 0;
			HXML node;
			if (!mir_tstrcmpi(xi.getName(hXml), _T("xml")))
				node = xi.getChild(hXml, childcount);
			else
				node = hXml;
			while (node) {
				LPCTSTR szNodeName = xi.getName(node);
				if (!mir_tstrcmpi(szNodeName, _T("rss")) || !mir_tstrcmpi(szNodeName, _T("rdf"))) {
					HXML chan = xi.getChild(node, 0);
					for (int j = 0; j < xi.getChildCount(chan); j++) {
						HXML child = xi.getChild(chan, j);
						if (!mir_tstrcmpi(xi.getName(child), _T("title"))) {
							TCHAR mes[MAX_PATH];
							mir_sntprintf(mes, SIZEOF(mes), TranslateT("%s\nis a valid feed's address."), tszURL);
							MessageBox(hwndDlg, mes, TranslateT("News Aggregator"), MB_OK | MB_ICONINFORMATION);
							TCHAR *tszTitle = (TCHAR *)xi.getText(child);
							return tszTitle;
						}
					}
				}
				else if (!mir_tstrcmpi(szNodeName, _T("feed"))) {
					for (int j = 0; j < xi.getChildCount(node); j++) {
						HXML child = xi.getChild(node, j);
						if (!mir_tstrcmpi(xi.getName(child), _T("title"))) {
							TCHAR mes[MAX_PATH];
							mir_sntprintf(mes, SIZEOF(mes), TranslateT("%s\nis a valid feed's address."), tszURL);
							MessageBox(hwndDlg, mes, TranslateT("News Aggregator"), MB_OK | MB_ICONINFORMATION);
							TCHAR *tszTitle = (TCHAR *)xi.getText(child);
							return tszTitle;
						}
					}
				}
				node = xi.getChild(hXml, ++childcount);
			}
		}
		xi.destroyNode(hXml);
	}
	Netlib_LogfT(hNetlibUser, _T("%s is not a valid feed's address."), tszURL);
	TCHAR mes[MAX_PATH];
	mir_sntprintf(mes, SIZEOF(mes), TranslateT("%s\nis not a valid feed's address."), tszURL);
	MessageBox(hwndDlg, mes, TranslateT("News Aggregator"), MB_OK | MB_ICONERROR);
	return NULL;
}

static void XmlToMsg(MCONTACT hContact, CMString &title, CMString &link, CMString &descr, CMString &author, CMString &comments, CMString &guid, CMString &category, time_t stamp)
{
	CMString message = db_get_tsa(hContact, MODULE, "MsgFormat");
	if (!message)
		message = mir_tstrdup(TAGSDEFAULT);

	if (title.IsEmpty())
		message.Replace(_T("#<title>#"), TranslateT("empty"));
	else
		message.Replace(_T("#<title>#"), title);

	if (link.IsEmpty())
		message.Replace(_T("#<link>#"), TranslateT("empty"));
	else
		message.Replace(_T("#<link>#"), link);

	if (descr.IsEmpty())
		message.Replace(_T("#<description>#"), TranslateT("empty"));
	else
		message.Replace(_T("#<description>#"), descr);

	if (author.IsEmpty())
		message.Replace(_T("#<author>#"), TranslateT("empty"));
	else
		message.Replace(_T("#<author>#"), author);

	if (comments.IsEmpty())
		message.Replace(_T("#<comments>#"), TranslateT("empty"));
	else
		message.Replace(_T("#<comments>#"), comments);

	if (guid.IsEmpty())
		message.Replace(_T("#<guid>#"), TranslateT("empty"));
	else
		message.Replace(_T("#<guid>#"), guid);

	if (category.IsEmpty())
		message.Replace(_T("#<category>#"), TranslateT("empty"));
	else
		message.Replace(_T("#<category>#"), category);

	DBEVENTINFO olddbei = { 0 };
	olddbei.cbSize = sizeof(olddbei);

	bool  MesExist = false;
	ptrA  pszTemp(mir_utf8encodeT(message));
	DWORD cbMemoLen = 10000, cbOrigLen = (DWORD)strlen(pszTemp);
	BYTE *pbBuffer = (BYTE*)mir_alloc(cbMemoLen);
	for (MEVENT hDbEvent = db_event_last(hContact); hDbEvent; hDbEvent = db_event_prev(hContact, hDbEvent)) {
		olddbei.cbBlob = db_event_getBlobSize(hDbEvent);
		if (olddbei.cbBlob > cbMemoLen)
			pbBuffer = (PBYTE)mir_realloc(pbBuffer, (size_t)(cbMemoLen = olddbei.cbBlob));
		olddbei.pBlob = pbBuffer;
		db_event_get(hDbEvent, &olddbei);

		// there's no need to look for the elder events
		if (stamp > 0 && olddbei.timestamp < (DWORD)stamp)
			break;

		if ((DWORD)strlen((char*)olddbei.pBlob) == cbOrigLen && !mir_strcmp((char*)olddbei.pBlob, pszTemp)) {
			MesExist = true;
			break;
		}
	}
	mir_free(pbBuffer);

	if (!MesExist) {
		if (stamp == 0)
			stamp = time(NULL);

		PROTORECVEVENT recv = { 0 };
		recv.flags = PREF_TCHAR;
		recv.timestamp = (DWORD)stamp;
		recv.tszMessage = (TCHAR*)message.c_str();
		ProtoChainRecvMsg(hContact, &recv);
	}
}

void CheckCurrentFeed(MCONTACT hContact)
{
	// Check is disabled by the user?
	if (!db_get_b(hContact, MODULE, "CheckState", 1) != 0)
		return;

	TCHAR *szURL = db_get_tsa(hContact, MODULE, "URL");
	if (szURL == NULL)
		return;

	Netlib_LogfT(hNetlibUser, _T("Started checking feed %s."), szURL);

	char *szData = NULL;
	GetNewsData(szURL, &szData, hContact, NULL);
	mir_free(szURL);

	if (szData) {
		TCHAR *tszData = mir_utf8decodeT(szData);
		if (!tszData)
			tszData = mir_a2t(szData);
		int bytesParsed = 0;
		HXML hXml = xi.parseString(tszData, &bytesParsed, NULL);
		mir_free(tszData);
		mir_free(szData);

		CMString szValue;
		if (hXml != NULL) {
			int childcount = 0;
			HXML node;
			if (!mir_tstrcmpi(xi.getName(hXml), _T("xml")))
				node = xi.getChild(hXml, childcount);
			else
				node = hXml;
			while (node) {
				LPCTSTR szNodeName = xi.getName(node);
				bool isRSS = !mir_tstrcmpi(szNodeName, _T("rss")), isAtom = !mir_tstrcmpi(szNodeName, _T("rdf"));
				if (isRSS || isAtom) {
					if (isRSS) {
						for (int i = 0; i < xi.getAttrCount(node); i++) {
							LPCTSTR szAttrName = xi.getAttrName(node, i);
							if (!mir_tstrcmpi(szAttrName, _T("version"))) {
								TCHAR ver[MAX_PATH];
								mir_sntprintf(ver, SIZEOF(ver), _T("RSS %s"), xi.getAttrValue(node, szAttrName));
								db_set_ts(hContact, MODULE, "MirVer", ver);
								break;
							}
						}
					}
					else if (isAtom)
						db_set_ts(hContact, MODULE, "MirVer", _T("RSS 1.0"));

					HXML chan = xi.getChild(node, 0);
					for (int j = 0; j < xi.getChildCount(chan); j++) {
						HXML child = xi.getChild(chan, j);
						LPCTSTR childName = xi.getName(child);
						if (!mir_tstrcmpi(childName, _T("title"))) {
							LPCTSTR szChildText = xi.getText(child);
							if (szChildText)
								db_set_ts(hContact, MODULE, "FirstName", ClearText(szValue, szChildText));
						}
						else if (!mir_tstrcmpi(childName, _T("link"))) {
							LPCTSTR szChildText = xi.getText(child);
							if (szChildText)
								db_set_ts(hContact, MODULE, "Homepage", ClearText(szValue, szChildText));
						}
						else if (!mir_tstrcmpi(childName, _T("description"))) {
							LPCTSTR szChildText = xi.getText(child);
							if (szChildText) {
								ClearText(szValue, szChildText);
								db_set_ts(hContact, MODULE, "About", szValue);
								db_set_ts(hContact, "CList", "StatusMsg", szValue);
							}
						}
						else if (!mir_tstrcmpi(childName, _T("language"))) {
							LPCTSTR szChildText = xi.getText(child);
							if (szChildText)
								db_set_ts(hContact, MODULE, "Language1", ClearText(szValue, szChildText));
						}
						else if (!mir_tstrcmpi(childName, _T("managingEditor"))) {
							LPCTSTR szChildText = xi.getText(child);
							if (szChildText)
								db_set_ts(hContact, MODULE, "e-mail", ClearText(szValue, szChildText));
						}
						else if (!mir_tstrcmpi(childName, _T("category"))) {
							LPCTSTR szChildText = xi.getText(child);
							if (szChildText)
								db_set_ts(hContact, MODULE, "Interest0Text", ClearText(szValue, szChildText));
						}
						else if (!mir_tstrcmpi(childName, _T("copyright"))) {
							LPCTSTR szChildText = xi.getText(child);
							if (szChildText)
								db_set_s(hContact, "UserInfo", "MyNotes", _T2A(ClearText(szValue, szChildText)));
						}
						else if (!mir_tstrcmpi(childName, _T("image"))) {
							for (int x = 0; x < xi.getChildCount(child); x++) {
								HXML imageval = xi.getChild(child, x);
								if (!mir_tstrcmpi(xi.getName(imageval), _T("url"))) {
									LPCTSTR url = xi.getText(imageval);
									db_set_ts(hContact, MODULE, "ImageURL", url);

									PROTO_AVATAR_INFORMATIONT pai = { NULL };
									pai.cbSize = sizeof(pai);
									pai.hContact = hContact;

									TCHAR *szNick = db_get_tsa(hContact, MODULE, "Nick");
									if (szNick) {
										TCHAR *ext = _tcsrchr((TCHAR *)url, _T('.')) + 1;
										pai.format = ProtoGetAvatarFormat(url);

										CMString filename = szNick;
										filename.Replace(_T("/"), _T("_"));
										mir_sntprintf(pai.filename, SIZEOF(pai.filename), _T("%s\\%s.%s"), tszRoot, filename.c_str(), ext);
										CreateDirectoryTreeT(tszRoot);
										if (DownloadFile(url, pai.filename)) {
											db_set_ts(hContact, MODULE, "ImagePath", pai.filename);
											ProtoBroadcastAck(MODULE, hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, (HANDLE)&pai, NULL);
										}
										else ProtoBroadcastAck(MODULE, hContact, ACKTYPE_AVATAR, ACKRESULT_FAILED, (HANDLE)&pai, NULL);
										mir_free(szNick);
										break;
									}
								}
							}
						}
						else if (!mir_tstrcmpi(childName, _T("lastBuildDate"))) {
							LPCTSTR szChildText = xi.getText(child);
							if (szChildText) {
								TCHAR *lastupdtime = (TCHAR *)xi.getText(child);
								time_t stamp = DateToUnixTime(lastupdtime, 0);
								double deltaupd = difftime(time(NULL), stamp);
								double deltacheck = difftime(time(NULL), (time_t)db_get_dw(hContact, MODULE, "LastCheck", 0));
								if (deltaupd - deltacheck >= 0) {
									db_set_dw(hContact, MODULE, "LastCheck", (DWORD)time(NULL));
									xi.destroyNode(hXml);
									return;
								}
							}
						}
						else if (!mir_tstrcmpi(childName, _T("item"))) {
							CMString title, link, descr, author, comments, guid, category;
							time_t stamp = 0;
							for (int z = 0; z < xi.getChildCount(child); z++) {
								HXML itemval = xi.getChild(child, z);
								LPCTSTR itemName = xi.getName(itemval);
								// We only use the first tag for now and ignore the rest.
								if (!mir_tstrcmpi(itemName, _T("title")))
									ClearText(title, xi.getText(itemval));

								else if (!mir_tstrcmpi(itemName, _T("link")))
									ClearText(link, xi.getText(itemval));

								else if (!mir_tstrcmpi(itemName, _T("pubDate")) || !mir_tstrcmpi(itemName, _T("date"))) {
									if (stamp == 0)
										stamp = DateToUnixTime(xi.getText(itemval), 0);
								}
								else if (!mir_tstrcmpi(itemName, _T("description")) || !mir_tstrcmpi(itemName, _T("encoded")))
									ClearText(descr, xi.getText(itemval));

								else if (!mir_tstrcmpi(itemName, _T("author")) || !mir_tstrcmpi(itemName, _T("creator")))
									ClearText(author, xi.getText(itemval));

								else if (!mir_tstrcmpi(itemName, _T("comments")))
									ClearText(comments, xi.getText(itemval));

								else if (!mir_tstrcmpi(itemName, _T("guid")))
									ClearText(guid, xi.getText(itemval));

								else if (!mir_tstrcmpi(itemName, _T("category")))
									ClearText(category, xi.getText(itemval));
							}

							XmlToMsg(hContact, title, link, descr, author, comments, guid, category, stamp);
						}
					}
				}
				else if (!mir_tstrcmpi(szNodeName, _T("feed"))) {
					db_set_ts(hContact, MODULE, "MirVer", _T("Atom 3"));
					for (int j = 0; j < xi.getChildCount(node); j++) {
						HXML child = xi.getChild(node, j);
						LPCTSTR szChildName = xi.getName(child);
						if (!mir_tstrcmpi(szChildName, _T("title"))) {
							LPCTSTR szChildText = xi.getText(child);
							if (szChildText)
								db_set_ts(hContact, MODULE, "FirstName", ClearText(szValue, szChildText));
						}
						else if (!mir_tstrcmpi(szChildName, _T("link"))) {
							for (int x = 0; x < xi.getAttrCount(child); x++) {
								if (!mir_tstrcmpi(xi.getAttrName(child, x), _T("rel")))
									if (!mir_tstrcmpi(xi.getAttrValue(child, xi.getAttrName(child, x)), _T("self")))
										break;

								if (!mir_tstrcmpi(xi.getAttrName(child, x), _T("href")))
									db_set_ts(hContact, MODULE, "Homepage", xi.getAttrValue(child, xi.getAttrName(child, x)));
							}
						}
						else if (!mir_tstrcmpi(szChildName, _T("subtitle"))) {
							LPCTSTR szChildText = xi.getText(child);
							if (szChildText) {
								ClearText(szValue, szChildText);
								db_set_ts(hContact, MODULE, "About", szValue);
								db_set_ts(hContact, "CList", "StatusMsg", szValue);
							}
						}
						else if (!mir_tstrcmpi(szChildName, _T("language"))) {
							LPCTSTR szChildText = xi.getText(child);
							if (szChildText)
								db_set_ts(hContact, MODULE, "Language1", ClearText(szValue, szChildText));
						}
						else if (!mir_tstrcmpi(szChildName, _T("author"))) {
							for (int x = 0; x < xi.getChildCount(child); x++) {
								HXML authorval = xi.getChild(child, x);
								if (!mir_tstrcmpi(xi.getName(authorval), _T("email"))) {
									db_set_ts(hContact, MODULE, "e-mail", xi.getText(authorval));
									break;
								}
							}
						}
						else if (!mir_tstrcmpi(szChildName, _T("category"))) {
							LPCTSTR szChildText = xi.getText(child);
							if (szChildText)
								db_set_ts(hContact, MODULE, "Interest0Text", ClearText(szValue, szChildText));
						}
						else if (!mir_tstrcmpi(szChildName, _T("icon"))) {
							for (int x = 0; x < xi.getChildCount(child); x++) {
								HXML imageval = xi.getChild(child, x);
								if (!mir_tstrcmpi(xi.getName(imageval), _T("url"))) {
									LPCTSTR url = xi.getText(imageval);
									db_set_ts(hContact, MODULE, "ImageURL", url);

									TCHAR *szNick = db_get_tsa(hContact, MODULE, "Nick");
									if (szNick) {
										PROTO_AVATAR_INFORMATIONT pai = { NULL };
										pai.cbSize = sizeof(pai);
										pai.hContact = hContact;
										TCHAR *ext = _tcsrchr((TCHAR *)url, _T('.')) + 1;
										pai.format = ProtoGetAvatarFormat(ext);

										TCHAR *filename = szNick;
										mir_sntprintf(pai.filename, SIZEOF(pai.filename), _T("%s\\%s.%s"), tszRoot, filename, ext);
										if (DownloadFile(url, pai.filename)) {
											db_set_ts(hContact, MODULE, "ImagePath", pai.filename);
											ProtoBroadcastAck(MODULE, hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, (HANDLE)&pai, NULL);
										}
										else ProtoBroadcastAck(MODULE, hContact, ACKTYPE_AVATAR, ACKRESULT_FAILED, (HANDLE)&pai, NULL);
										mir_free(szNick);
										break;
									}
								}
							}
						}
						else if (!mir_tstrcmpi(szChildName, _T("updated"))) {
							LPCTSTR szChildText = xi.getText(child);
							if (szChildText) {
								TCHAR *lastupdtime = (TCHAR *)szChildText;
								time_t stamp = DateToUnixTime(lastupdtime, 1);
								double deltaupd = difftime(time(NULL), stamp);
								double deltacheck = difftime(time(NULL), (time_t)db_get_dw(hContact, MODULE, "LastCheck", 0));
								if (deltaupd - deltacheck >= 0) {
									db_set_dw(hContact, MODULE, "LastCheck", (DWORD)time(NULL));
									xi.destroyNode(hXml);
									return;
								}
							}
						}
						else if (!mir_tstrcmpi(szChildName, _T("entry"))) {
							CMString title, link, descr, author, comments, guid, category;
							time_t stamp = 0;
							for (int z = 0; z < xi.getChildCount(child); z++) {
								HXML itemval = xi.getChild(child, z);
								LPCTSTR szItemName = xi.getName(itemval);
								if (!mir_tstrcmpi(szItemName, _T("title"))) {
									LPCTSTR szItemText = xi.getText(itemval);
									if (szItemText)
										ClearText(title, szItemText);
								}
								else if (!mir_tstrcmpi(szItemName, _T("link"))) {
									for (int x = 0; x < xi.getAttrCount(itemval); x++) {
										if (!mir_tstrcmpi(xi.getAttrName(itemval, x), _T("href"))) {
											ClearText(link, xi.getAttrValue(itemval, xi.getAttrName(itemval, x)));
											break;
										}
									}
								}
								else if (!mir_tstrcmpi(szItemName, _T("updated"))) {
									if (stamp == 0)
										stamp = DateToUnixTime(xi.getText(itemval), 0);
								}
								else if (!mir_tstrcmpi(szItemName, _T("summary")) || !mir_tstrcmpi(szItemName, _T("content"))) {
									LPCTSTR szItemText = xi.getText(itemval);
									if (szItemText)
										ClearText(descr, szItemText);
								}
								else if (!mir_tstrcmpi(szItemName, _T("author"))) {
									for (int x = 0; x < xi.getChildCount(itemval); x++) {
										HXML authorval = xi.getChild(itemval, x);
										if (!mir_tstrcmpi(xi.getName(authorval), _T("name")) && xi.getText(authorval)) {
											ClearText(author, xi.getText(authorval));
											break;
										}
									}
								}
								else if (!mir_tstrcmpi(szItemName, _T("comments"))) {
									LPCTSTR szItemText = xi.getText(itemval);
									if (szItemText)
										ClearText(comments, szItemText);
								}
								else if (!mir_tstrcmpi(szItemName, _T("id"))) {
									LPCTSTR szItemText = xi.getText(itemval);
									if (szItemText)
										ClearText(guid, xi.getText(itemval));
								}
								else if (!mir_tstrcmpi(szItemName, _T("category"))) {
									for (int x = 0; x < xi.getAttrCount(itemval); x++) {
										LPCTSTR szAttrName = xi.getAttrName(itemval, x);
										if (!mir_tstrcmpi(szAttrName, _T("term")) && xi.getText(itemval)) {
											ClearText(category, xi.getAttrValue(itemval, szAttrName));
											break;
										}
									}
								}
							}

							XmlToMsg(hContact, title, link, descr, author, comments, guid, category, stamp);
						}
					}
				}
				node = xi.getChild(hXml, ++childcount);
			}
			xi.destroyNode(hXml);
		}
	}
	db_set_dw(hContact, MODULE, "LastCheck", (DWORD)time(NULL));
}

void CheckCurrentFeedAvatar(MCONTACT hContact)
{
	if (!db_get_b(hContact, MODULE, "CheckState", 1))
		return;

	TCHAR *szURL = db_get_tsa(hContact, MODULE, "URL");
	if (szURL == NULL)
		return;

	char *szData = NULL;
	GetNewsData(szURL, &szData, hContact, NULL);
	mir_free(szURL);

	if (szData == NULL)
		return;

	TCHAR *tszData = mir_utf8decodeT(szData);
	if (!tszData)
		tszData = mir_a2t(szData);
	int bytesParsed = 0;
	HXML hXml = xi.parseString(tszData, &bytesParsed, NULL);
	mir_free(tszData);
	mir_free(szData);
	if (hXml == NULL)
		return;

	int childcount = 0;
	HXML node = xi.getChild(hXml, childcount);
	while (node) {
		LPCTSTR szNodeName = xi.getName(node);
		if (!mir_tstrcmpi(szNodeName, _T("rss")) || !mir_tstrcmpi(szNodeName, _T("rdf"))) {
			HXML chan = xi.getChild(node, 0);
			for (int j = 0; j < xi.getChildCount(chan); j++) {
				HXML child = xi.getChild(chan, j);
				if (!mir_tstrcmpi(xi.getName(child), _T("image"))) {
					for (int x = 0; x < xi.getChildCount(child); x++) {
						HXML imageval = xi.getChild(child, x);
						if (!mir_tstrcmpi(xi.getName(imageval), _T("url"))) {
							LPCTSTR url = xi.getText(imageval);
							db_set_ts(hContact, MODULE, "ImageURL", url);

							PROTO_AVATAR_INFORMATIONT pai = { NULL };
							pai.cbSize = sizeof(pai);
							pai.hContact = hContact;

							TCHAR *szNick = db_get_tsa(hContact, MODULE, "Nick");
							if (szNick) {
								TCHAR *ext = _tcsrchr((TCHAR *)url, _T('.')) + 1;
								pai.format = ProtoGetAvatarFormat(ext);

								TCHAR *filename = szNick;
								mir_sntprintf(pai.filename, SIZEOF(pai.filename), _T("%s\\%s.%s"), tszRoot, filename, ext);
								if (DownloadFile(url, pai.filename)) {
									db_set_ts(hContact, MODULE, "ImagePath", pai.filename);
									ProtoBroadcastAck(MODULE, hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, (HANDLE)&pai, NULL);
								}
								else ProtoBroadcastAck(MODULE, hContact, ACKTYPE_AVATAR, ACKRESULT_FAILED, (HANDLE)&pai, NULL);
								mir_free(szNick);
								break;
							}
						}
					}
				}
			}
		}
		else if (!mir_tstrcmpi(szNodeName, _T("feed"))) {
			for (int j = 0; j < xi.getChildCount(node); j++) {
				HXML child = xi.getChild(node, j);
				if (!mir_tstrcmpi(xi.getName(child), _T("icon"))) {
					for (int x = 0; x < xi.getChildCount(child); x++) {
						HXML imageval = xi.getChild(child, x);
						if (!mir_tstrcmpi(xi.getName(imageval), _T("url"))) {
							LPCTSTR url = xi.getText(imageval);
							db_set_ts(hContact, MODULE, "ImageURL", url);

							TCHAR *szNick = db_get_tsa(hContact, MODULE, "Nick");
							if (szNick) {
								PROTO_AVATAR_INFORMATIONT pai = { NULL };
								pai.cbSize = sizeof(pai);
								pai.hContact = hContact;

								TCHAR *ext = _tcsrchr((TCHAR *)url, _T('.')) + 1;
								pai.format = ProtoGetAvatarFormat(ext);

								TCHAR *filename = szNick;
								mir_sntprintf(pai.filename, SIZEOF(pai.filename), _T("%s\\%s.%s"), tszRoot, filename, ext);
								if (DownloadFile(url, pai.filename)) {
									db_set_ts(hContact, MODULE, "ImagePath", pai.filename);
									ProtoBroadcastAck(MODULE, hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, (HANDLE)&pai, NULL);
								}
								else ProtoBroadcastAck(MODULE, hContact, ACKTYPE_AVATAR, ACKRESULT_FAILED, (HANDLE)&pai, NULL);
								mir_free(szNick);
								break;
							}
						}
					}
				}
			}
		}
		node = xi.getChild(hXml, ++childcount);
	}
	xi.destroyNode(hXml);
}