summaryrefslogtreecommitdiff
path: root/src/modules/updatenotify/updatenotify.cpp
blob: b29753c21612722132fab5d57947d8899095570c (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
/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2009 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

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 "..\..\core\commonheaders.h"

#define UN_MOD               "UpdateNotify"
#define UN_ENABLE            "UpdateNotifyEnable"
#define UN_ENABLE_DEF        1
#define UN_LASTCHECK         "UpdateNotifyLastCheck"
#define UN_SERVERPERIOD      "UpdateNotifyPingDelayPeriod"
#define UN_CURRENTVERSION    "UpdateNotifyCurrentVersion"
#define UN_CURRENTVERSIONFND "UpdateNotifyCurrentVersionFound"
#define UN_NOTIFYTYPE        "UpdateNotifyType"
#define UN_NOTIFYTYPE_STABLE 1
#define UN_NOTIFYTYPE_BETA   2
#define UN_NOTIFYTYPE_ALPHA  3
#define UN_NOTIFYTYPE_DEF    UN_NOTIFYTYPE_STABLE
#define UN_CUSTOMXMLURL      "UpdateNotifyCustomXMLURL"
#define UN_URLXML            "http://update.miranda-im.org/update.xml"
#define UN_MINCHECKTIME      60*60 /* Check no more than once an hour */
#define UN_DEFAULTCHECKTIME  60*48*60 /* Default to check once every 48 hours */
#define UN_FIRSTCHECK        15 /* First check 15 seconds after startup */
#define UN_REPEATNOTIFYDLY   24*60*60 /* 24 hours before showing release notification again */

typedef struct {
	int isNew;
	int isManual;
    char version[64];
    char versionReal[16];
    char notesUrl[256];
	char downloadUrl[256];
	DWORD reqTime;
} UpdateNotifyData;

typedef struct {
    DWORD dwVersion;
    char *szVersionPublic;
    char *szVersion;
    char *szDownload;
    char *szNotes;
} UpdateNotifyReleaseData;

static BOOL bModuleInitialized = FALSE;
static HANDLE hNetlibUser = 0, hHookModules, hHookPreShutdown;
static UINT_PTR updateTimerId;
static HANDLE dwUpdateThreadID = 0;
static HWND hwndUpdateDlg = 0, hwndManualUpdateDlg = 0;
static XML_API xun;

static int UpdateNotifyOptInit(WPARAM wParam, LPARAM lParam);
static INT_PTR UpdateNotifyMenuCommand(WPARAM wParam, LPARAM lParam);
static VOID CALLBACK UpdateNotifyTimerCheck(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
static int UpdateNotifyMakeRequest(UpdateNotifyData *und);
static void UpdateNotifyPerform(void *m);
static INT_PTR CALLBACK UpdateNotifyProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
static INT_PTR CALLBACK UpdateNotifyOptsProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);

static int UpdateNotifyModulesLoaded(WPARAM, LPARAM) {
	NETLIBUSER nlu;

	ZeroMemory(&nlu, sizeof(nlu));
	nlu.cbSize = sizeof(nlu);
	nlu.flags =  NUF_OUTGOING|NUF_HTTPCONNS;
	nlu.szSettingsModule = UN_MOD;
	nlu.szDescriptiveName = Translate("Update notification");
	hNetlibUser = (HANDLE)CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM)&nlu);
	return 0;
}

static int UpdateNotifyPreShutdown(WPARAM, LPARAM) {
	if (IsWindow(hwndUpdateDlg)) {
		SendMessage(hwndUpdateDlg, WM_COMMAND, MAKELONG(IDOK, 0), 0);
	}
	if (IsWindow(hwndManualUpdateDlg)) {
		SendMessage(hwndManualUpdateDlg, WM_COMMAND, MAKELONG(IDOK, 0), 0);
	}
	return 0;
}

int LoadUpdateNotifyModule(void) {
	CLISTMENUITEM mi = { 0 };
	
	bModuleInitialized = TRUE;
	
    // Upgrade Routine
    if (DBGetContactSettingByte(NULL, UN_MOD, "UpdateNotifyNotifyAlpha", 0)) {
        DBDeleteContactSetting(NULL, UN_MOD, "UpdateNotifyNotifyAlpha");
        DBWriteContactSettingByte(NULL, UN_MOD, UN_NOTIFYTYPE, UN_NOTIFYTYPE_ALPHA);
    }
    // Ene Upgrade Routine
    
	CreateServiceFunction("UpdateNotify/UpdateCommand", UpdateNotifyMenuCommand);
	mi.cbSize = sizeof(mi);
	mi.flags = CMIF_ICONFROMICOLIB;
	mi.icolibItem = GetSkinIconHandle(SKINICON_OTHER_EMPTYBLOB);
	mi.pszPopupName = LPGEN("&Help");
	mi.position = 2000030000;
	mi.pszName = LPGEN("Check for Update");
	mi.pszService = "UpdateNotify/UpdateCommand";
	Menu_AddMainMenuItem(&mi);
	
	hHookModules = HookEvent(ME_SYSTEM_MODULESLOADED, UpdateNotifyModulesLoaded);
	hHookPreShutdown = HookEvent(ME_SYSTEM_PRESHUTDOWN, UpdateNotifyPreShutdown);
	HookEvent(ME_OPT_INITIALISE, UpdateNotifyOptInit);
	updateTimerId = SetTimer(NULL, 0, 1000*UN_FIRSTCHECK, UpdateNotifyTimerCheck);
    mir_getXI(&xun);
	return 0;
}

void UnloadUpdateNotifyModule()
{
	if (!bModuleInitialized) return;
	UnhookEvent(hHookModules);
	UnhookEvent(hHookPreShutdown);
}

static int UpdateNotifyOptInit(WPARAM wParam, LPARAM) {
	OPTIONSDIALOGPAGE odp;

	ZeroMemory(&odp, sizeof(odp));
	odp.cbSize = sizeof(odp);
	odp.position = 100000000;
	odp.hInstance = hMirandaInst;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_UPDATENOTIFY);
	odp.pszGroup = LPGEN("Events");
	odp.pszTitle = LPGEN("Update Notify");
	odp.pfnDlgProc = UpdateNotifyOptsProc;
	odp.flags = ODPF_BOLDGROUPS;
	CallService(MS_OPT_ADDPAGE, wParam, (LPARAM)&odp);
	return 0;
}

static INT_PTR UpdateNotifyMenuCommand(WPARAM, LPARAM) {
	UpdateNotifyData und;
	
	if (IsWindow(hwndManualUpdateDlg)) {
		SetForegroundWindow(hwndManualUpdateDlg);
		return 0;
	}
	ZeroMemory(&und, sizeof(und));
	UpdateNotifyMakeRequest(&und);
	if (und.isNew) {
		DBWriteContactSettingString(NULL, UN_MOD, UN_CURRENTVERSION, und.versionReal);
		DBWriteContactSettingDword(NULL, UN_MOD, UN_CURRENTVERSIONFND, und.reqTime);
	}
	und.isManual = 1;
	DialogBoxParam(hMirandaInst, MAKEINTRESOURCE(IDD_UPDATE_NOTIFY), 0, UpdateNotifyProc, (LPARAM)&und);
	hwndManualUpdateDlg = 0;
	return 0;	
}

static VOID CALLBACK UpdateNotifyTimerCheck(HWND, UINT, UINT_PTR, DWORD)
{
	KillTimer(NULL, updateTimerId);
	if (!DBGetContactSettingByte(NULL, UN_MOD, UN_ENABLE, UN_ENABLE_DEF))
		return;
	if (dwUpdateThreadID != 0) {
		Netlib_Logf(hNetlibUser, "Update notification already running, ignoring attempt");
		return;
	}
	{
		DWORD lastCheck = 0;

		if (!hNetlibUser)
			return;
		lastCheck = DBGetContactSettingDword(NULL, UN_MOD, UN_LASTCHECK, 0);
		if (!lastCheck) { // never checked for update before
			Netlib_Logf(hNetlibUser, "Running update notify check for the first time.");
			dwUpdateThreadID = mir_forkthread(UpdateNotifyPerform, 0);
		}
		else {
			DWORD dwNow = time(NULL), dwTimeDiff;
			DWORD dwServerPing = DBGetContactSettingDword(NULL, UN_MOD, UN_SERVERPERIOD, UN_DEFAULTCHECKTIME);

			if (lastCheck>dwNow) {
				// time for last check is after the current date so reset lastcheck and quit
				DBWriteContactSettingDword(NULL, UN_MOD, UN_LASTCHECK, dwNow);
				return;
			}
			dwTimeDiff = dwNow - lastCheck;
			if (dwServerPing<UN_MINCHECKTIME)
				dwServerPing = UN_MINCHECKTIME;
			if (dwTimeDiff>dwServerPing)
				dwUpdateThreadID = mir_forkthread(UpdateNotifyPerform, 0);
		}
		updateTimerId = SetTimer(NULL, 0, 1000*UN_MINCHECKTIME, UpdateNotifyTimerCheck);
	}
}

static DWORD UpdateNotifyMakeVersion(char *str) {
    DWORD a1, a2, a3, a4;
    if (!str)
        return 0;
    sscanf(str, "%u.%u.%u.%u", &a1, &a2, &a3, &a4);
    return PLUGIN_MAKE_VERSION(a1, a2, a3, a4);
}

inline bool UpdateNotifyIsNewer(DWORD dwCurrent, DWORD dwTest) 
{ return dwTest > dwCurrent; }

inline bool UpdateNotifyReleaseDataValid(UpdateNotifyReleaseData *d) 
{ return d && d->szVersionPublic && d->szVersion && d->szDownload && d->szNotes; }

static void UpdateNotifyFreeReleaseData(UpdateNotifyReleaseData *d) {
    if (!d) 
        return;
    if (d->szVersionPublic) mir_free(d->szVersionPublic);
    if (d->szVersion) mir_free(d->szVersion);
    if (d->szDownload) mir_free(d->szDownload);
    if (d->szNotes) mir_free(d->szNotes);
}

static void UpdateNotifyReleaseLogUpdate(UpdateNotifyReleaseData *d) {
    if (!UpdateNotifyReleaseDataValid(d)) 
        return;
    #ifdef _WIN64
    Netlib_Logf(hNetlibUser, "Update server version: %s [%s] [64-bit]", d->szVersionPublic, d->szVersion);
    #else defined(_UNICODE)
    Netlib_Logf(hNetlibUser, "Update server version: %s [%s] [Unicode]", d->szVersionPublic, d->szVersion);
    #endif
}

static void UpdateNotifyReleaseCopyData(UpdateNotifyReleaseData *d, UpdateNotifyData *und) {
    if (!UpdateNotifyReleaseDataValid(d)||!und) 
        return;
    mir_snprintf(und->version, sizeof(und->version), "%s", d->szVersionPublic);
    mir_snprintf(und->versionReal, sizeof(und->versionReal), "%s", d->szVersion);
    mir_snprintf(und->notesUrl, sizeof(und->notesUrl), "%s", d->szNotes);
    mir_snprintf(und->downloadUrl, sizeof(und->downloadUrl), "%s", d->szDownload);
}

static int UpdateNotifyMakeRequest(UpdateNotifyData *und) {
	NETLIBHTTPREQUEST req;
	NETLIBHTTPREQUEST *resp;
	NETLIBHTTPHEADER headers[1];
	DWORD dwVersion;
	char szVersion[32], szUrl[256], szVersionText[128], szUserAgent[64];
	int isUnicode, isAlphaCheck, isBetaCheck;
	DBVARIANT dbv;
	
	if (!und) 
		return 0;
	und->version[0] = 0;
	und->versionReal[0] = 0;
	und->notesUrl[0] = 0;
	und->downloadUrl[0] = 0;
	und->reqTime = time(NULL);
	
	DBWriteContactSettingDword(NULL, UN_MOD, UN_LASTCHECK, und->reqTime);
	CallService(MS_SYSTEM_GETVERSIONTEXT, sizeof(szVersionText), (LPARAM)szVersionText);
	isUnicode = strstr(szVersionText, "Unicode") != NULL ? 1 : 0;
	isBetaCheck = DBGetContactSettingByte(NULL, UN_MOD, UN_NOTIFYTYPE, UN_NOTIFYTYPE_DEF) == UN_NOTIFYTYPE_BETA?1:0;
	isAlphaCheck = DBGetContactSettingByte(NULL, UN_MOD, UN_NOTIFYTYPE, UN_NOTIFYTYPE_DEF) == UN_NOTIFYTYPE_ALPHA?1:0;
	dwVersion = CallService(MS_SYSTEM_GETVERSION, 0, 0);
	mir_snprintf(szVersion, sizeof(szVersion), "%d.%d.%d.%d", 
		HIBYTE(HIWORD(dwVersion)), LOBYTE(HIWORD(dwVersion)), 
		HIBYTE(LOWORD(dwVersion)), LOBYTE(LOWORD(dwVersion)));
	if (!DBGetContactSettingString(NULL, UN_MOD, UN_CUSTOMXMLURL, &dbv)) {
		mir_snprintf(szUrl, sizeof(szUrl), "%s", dbv.pszVal?dbv.pszVal:UN_URLXML);
		DBFreeVariant(&dbv);
	}
	else mir_snprintf(szUrl, sizeof(szUrl), "%s", UN_URLXML);
	ZeroMemory(&req, sizeof(req));
	req.cbSize = sizeof(req);
	req.requestType = REQUEST_GET;
	req.szUrl = szUrl;
	req.flags = 0;
	headers[0].szName = "User-Agent";
	headers[0].szValue = szUserAgent;
    #ifdef _WIN64
    mir_snprintf(szUserAgent, sizeof(szUserAgent), "Miranda/%s (x64)", szVersion);
    #else defined(_UNICODE)
    mir_snprintf(szUserAgent, sizeof(szUserAgent), "Miranda/%s (Unicode)", szVersion);
    #endif
	req.headersCount = 1;
	req.headers = headers;
	resp = (NETLIBHTTPREQUEST *)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)hNetlibUser, (LPARAM)&req);
	if (resp) {
		if (resp->resultCode == 200 && resp->dataLength > 0) {
			//int i;
			int resUpdate = 0;
            TCHAR *tXml;
            char *tmp;
            HXML nodeDoc, n;
            
            tXml = mir_a2t(resp->pData);
            nodeDoc = xun.parseString(tXml, 0, _T("miranda"));
            if (nodeDoc) {
                int rdStableValid = 0, rdBetaValid = 0, rdAlphaValid = 0;
                // stable release
                UpdateNotifyReleaseData rdStable;
                ZeroMemory(&rdStable, sizeof(rdStable));
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasestable/versionpublic"), 0)) != NULL && xun.getText(n)) {
                    rdStable.szVersionPublic = mir_t2a(xun.getText(n));
                }
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasestable/versionreal"), 0)) != NULL && xun.getText(n)) {
                    rdStable.szVersion = mir_t2a(xun.getText(n));
                    if (rdStable.szVersion)
                        rdStable.dwVersion = UpdateNotifyMakeVersion(rdStable.szVersion);
                }
                #ifdef _WIN64
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasestable/downloadx64exe"), 0)) != NULL && xun.getText(n)) {
                    rdStable.szDownload = mir_t2a(xun.getText(n));
                }
                #else defined(_UNICODE)
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasestable/downloadunicodeexe"), 0)) != NULL && xun.getText(n)) {
                    rdStable.szDownload = mir_t2a(xun.getText(n));
                }
                #endif
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasestable/notesurl"), 0)) != NULL && xun.getText(n)) {
                    rdStable.szNotes = mir_t2a(xun.getText(n));
                }
                rdStableValid = UpdateNotifyReleaseDataValid(&rdStable);
                
                // beta release
                UpdateNotifyReleaseData rdBeta;
                ZeroMemory(&rdBeta, sizeof(rdBeta));
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasebeta/versionpublic"), 0)) != NULL && xun.getText(n)) {
                    rdBeta.szVersionPublic = mir_t2a(xun.getText(n));
                }
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasebeta/versionreal"), 0)) != NULL && xun.getText(n)) {
                    rdBeta.szVersion = mir_t2a(xun.getText(n));
                    if (rdBeta.szVersion)
                        rdBeta.dwVersion = UpdateNotifyMakeVersion(rdBeta.szVersion);
                }
                #ifdef _WIN64
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasebeta/downloadx64zip"), 0)) != NULL && xun.getText(n)) {
                    rdBeta.szDownload = mir_t2a(xun.getText(n));
                }
                #else defined(_UNICODE)
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasebeta/downloadunicodeexe"), 0)) != NULL && xun.getText(n)) {
                    rdBeta.szDownload = mir_t2a(xun.getText(n));
                }
                #endif
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasebeta/notesurl"), 0)) != NULL && xun.getText(n)) {
                    rdBeta.szNotes = mir_t2a(xun.getText(n));
                }
                rdBetaValid = UpdateNotifyReleaseDataValid(&rdBeta);
                
                // alpha release
                UpdateNotifyReleaseData rdAlpha;
                ZeroMemory(&rdAlpha, sizeof(rdAlpha));
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasealpha/versionpublic"), 0)) != NULL && xun.getText(n)) {
                    rdAlpha.szVersionPublic = mir_t2a(xun.getText(n));
                }
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasealpha/versionreal"), 0)) != NULL && xun.getText(n)) {
                    rdAlpha.szVersion = mir_t2a(xun.getText(n));
                    if (rdAlpha.szVersion)
                        rdAlpha.dwVersion = UpdateNotifyMakeVersion(rdAlpha.szVersion);
                }
                #ifdef _WIN64
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasealpha/downloadx64zip"), 0)) != NULL && xun.getText(n)) {
                    rdAlpha.szDownload = mir_t2a(xun.getText(n));
                }
                #else defined(_UNICODE)
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasealpha/downloadunicodezip"), 0)) != NULL && xun.getText(n)) {
                    rdAlpha.szDownload = mir_t2a(xun.getText(n));
                }
                #endif
                if ((n = xun.getChildByPath(nodeDoc, _T("releases/releasealpha/notesurl"), 0)) != NULL && xun.getText(n)) {
                    rdAlpha.szNotes = mir_t2a(xun.getText(n));
                }
                rdAlphaValid = UpdateNotifyReleaseDataValid(&rdAlpha);
                
                if (isBetaCheck) {
                    if (!rdBetaValid&&rdStableValid) {
                        UpdateNotifyReleaseLogUpdate(&rdStable);
                        if (UpdateNotifyIsNewer(dwVersion, rdStable.dwVersion)) 
                            resUpdate = 1;
                        UpdateNotifyReleaseCopyData(&rdStable, und);
                    }
                    else if (rdBetaValid&&rdStableValid&&UpdateNotifyIsNewer(rdBeta.dwVersion, rdStable.dwVersion)) {
                        UpdateNotifyReleaseLogUpdate(&rdStable);
                        if (UpdateNotifyIsNewer(dwVersion, UpdateNotifyMakeVersion(rdStable.szVersion))) 
                            resUpdate = 1;
                        UpdateNotifyReleaseCopyData(&rdStable, und);
                    }
                    else if (rdBetaValid) {
                        UpdateNotifyReleaseLogUpdate(&rdBeta);
                        if (UpdateNotifyIsNewer(dwVersion, rdBeta.dwVersion)) 
                            resUpdate = 1;
                        UpdateNotifyReleaseCopyData(&rdBeta, und);
                    }
                }
                else if (isAlphaCheck) {                 
                    if (!rdAlphaValid&&rdStableValid) {
                        if (UpdateNotifyIsNewer(rdStable.dwVersion, rdAlpha.dwVersion)) {
                            UpdateNotifyReleaseLogUpdate(&rdAlpha);
                            if (UpdateNotifyIsNewer(dwVersion, rdAlpha.dwVersion)) 
                                resUpdate = 1;
                            UpdateNotifyReleaseCopyData(&rdAlpha, und);
                        }
                        else {
                            UpdateNotifyReleaseLogUpdate(&rdStable);
                            if (UpdateNotifyIsNewer(dwVersion, rdStable.dwVersion)) 
                                resUpdate = 1;
                            UpdateNotifyReleaseCopyData(&rdStable, und);
                        } 
                    }
                    else if (rdAlphaValid&&rdStableValid&&UpdateNotifyIsNewer(rdAlpha.dwVersion, rdStable.dwVersion)) {
                        UpdateNotifyReleaseLogUpdate(&rdStable);
                        if (UpdateNotifyIsNewer(dwVersion, rdStable.dwVersion)) 
                            resUpdate = 1;
                        UpdateNotifyReleaseCopyData(&rdStable, und);
                    }
                    else if (!rdAlphaValid&&rdBetaValid&&rdStableValid) {
                        if (UpdateNotifyIsNewer(rdStable.dwVersion, rdBeta.dwVersion)) {
                            UpdateNotifyReleaseLogUpdate(&rdBeta);
                            if (UpdateNotifyIsNewer(dwVersion, rdBeta.dwVersion)) 
                                resUpdate = 1;
                            UpdateNotifyReleaseCopyData(&rdBeta, und);
                        }
                        else {
                            UpdateNotifyReleaseLogUpdate(&rdStable);
                            if (UpdateNotifyIsNewer(dwVersion, rdStable.dwVersion)) 
                                resUpdate = 1;
                            UpdateNotifyReleaseCopyData(&rdStable, und);
                        } 
                    }
                    else if (rdAlphaValid) {
                        UpdateNotifyReleaseLogUpdate(&rdAlpha);
                        if (UpdateNotifyIsNewer(dwVersion, rdAlpha.dwVersion)) 
                            resUpdate = 1;
                        UpdateNotifyReleaseCopyData(&rdAlpha, und);
                    }
                }
                else {
                    if (rdStableValid) {
                        UpdateNotifyReleaseLogUpdate(&rdStable);
                        if (UpdateNotifyIsNewer(dwVersion, rdStable.dwVersion)) 
                            resUpdate = 1;
                        UpdateNotifyReleaseCopyData(&rdStable, und);
                    }
                }
              
                UpdateNotifyFreeReleaseData(&rdStable);
                UpdateNotifyFreeReleaseData(&rdBeta);
                UpdateNotifyFreeReleaseData(&rdAlpha);
                // settings
                if ((n = xun.getChildByPath(nodeDoc, _T("settings/ping"), 0)) != NULL && xun.getText(n)) {
                    tmp = mir_t2a(xun.getText(n));
                    if (tmp) {
                        int pingval = atoi(tmp);
                        if ((pingval*60*60)>UN_MINCHECKTIME) {
                            Netlib_Logf(hNetlibUser, "Next update check in %d hours", pingval);
                            DBWriteContactSettingDword(NULL, UN_MOD, UN_SERVERPERIOD, pingval*60*60);
                        }
                        mir_free(tmp);
                    }
                }
                if ((n = xun.getChildByPath(nodeDoc, _T("settings/updateurl"), 0)) != NULL && xun.getText(n)) {
                    tmp = mir_t2a(xun.getText(n));
                    if (tmp) {
                        Netlib_Logf(hNetlibUser, "Update URL has changed (%s)", tmp);
                        DBWriteContactSettingString(NULL, UN_MOD, UN_CUSTOMXMLURL, tmp);
                        mir_free(tmp);
                    }
                }
                if (resUpdate&&und->version&&und->versionReal&&und->notesUrl&&und->downloadUrl) {
                    Netlib_Logf(hNetlibUser, "A new version of Miranda IM is available: %s", und->version);
                    und->isNew = 1;
                }
				xun.destroyNode(nodeDoc);
            }
            mir_free(tXml);
		}
		else Netlib_Logf(hNetlibUser, "Invalid response code from HTTP request");
		CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
	}
	else Netlib_Logf(hNetlibUser, "No response from HTTP request");
	return und->isNew;
}

static void UpdateNotifyPerform(void *)
{
	UpdateNotifyData und;
	DBVARIANT dbv;
	
	ZeroMemory(&und, sizeof(und));
	UpdateNotifyMakeRequest(&und);
	if (und.isNew) {
		int notify = 1;
		
		if (!DBGetContactSettingString(NULL, UN_MOD, UN_CURRENTVERSION, &dbv)) {
			if (!strcmp(dbv.pszVal, und.versionReal)) { // already notified of this version
			
				DWORD dwNotifyLast = DBGetContactSettingDword(NULL, UN_MOD, UN_CURRENTVERSIONFND, 0);

				if (dwNotifyLast>und.reqTime) { // fix last check date if time has changed
					DBWriteContactSettingDword(NULL, UN_MOD, UN_CURRENTVERSIONFND, und.reqTime);
					notify = 0;
				}
				else if (und.reqTime-dwNotifyLast<UN_REPEATNOTIFYDLY) {
					notify = 0;
				}
				DBFreeVariant(&dbv);
			}
		}
		if (notify) {
			DBWriteContactSettingString(NULL, UN_MOD, UN_CURRENTVERSION, und.versionReal);
			DBWriteContactSettingDword(NULL, UN_MOD, UN_CURRENTVERSIONFND, und.reqTime);
			DialogBoxParam(hMirandaInst, MAKEINTRESOURCE(IDD_UPDATE_NOTIFY), 0, UpdateNotifyProc, (LPARAM)&und);
			hwndUpdateDlg = 0;
		}
	}
	dwUpdateThreadID = 0;
}

static INT_PTR CALLBACK UpdateNotifyProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch ( msg ) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hwndDlg);
		Window_SetIcon_IcoLib(hwndDlg, SKINICON_OTHER_MIRANDA);
		{
			UpdateNotifyData *und = (UpdateNotifyData*)lParam;
			char szVersion[128], szVersionTmp[128], *p;
			TCHAR szTmp[128];
			
			if (und->isManual)
				hwndManualUpdateDlg = hwndDlg;
			else hwndUpdateDlg = hwndDlg;
			if (und->isNew) {
				TCHAR* ptszVer = mir_a2t( und->version );
				mir_sntprintf(szTmp, SIZEOF(szTmp), TranslateT("Miranda IM %s Now Available"), ptszVer);
				mir_free(ptszVer);
				ShowWindow(GetDlgItem(hwndDlg, IDC_UPDATE), SW_HIDE);
			}
			else {
				mir_sntprintf(szTmp, SIZEOF(szTmp), TranslateT("No Update Available"));
				SetDlgItemText(hwndDlg, IDC_MESSAGE, TranslateT("You are running the latest version of Miranda IM.  No update is available at this time."));
				EnableWindow(GetDlgItem(hwndDlg, IDC_DOWNLOAD), FALSE);
				ShowWindow(GetDlgItem(hwndDlg, IDC_VERSION), SW_HIDE);
			}
			SetWindowText(hwndDlg, szTmp);
			CallService(MS_SYSTEM_GETVERSIONTEXT, sizeof(szVersion), (LPARAM)szVersion);
			p = strstr(szVersion, "x64 Unicode");
			if (p)
				*p = '\0';
			p = strstr(szVersion, " Unicode");
			if (p)
				*p = '\0';
			SetDlgItemTextA(hwndDlg, IDC_CURRENTVERSION, szVersion);
			mir_snprintf(szVersionTmp, SIZEOF(szVersionTmp), "%s", und->version?und->version:szVersion);
			SetDlgItemTextA(hwndDlg, und->isNew?IDC_VERSION:IDC_UPDATE, szVersionTmp);
			if (und->isNew) {
				HFONT hFont;
				LOGFONT lf;

				hFont = (HFONT)SendDlgItemMessage(hwndDlg, IDC_VERSION, WM_GETFONT, 0, 0);
				GetObject(hFont, sizeof(lf), &lf);
				lf.lfWeight = FW_BOLD;
				hFont = CreateFontIndirect(&lf);
				SendDlgItemMessage(hwndDlg, IDC_VERSION, WM_SETFONT, (WPARAM)hFont, 0);
				hFont = (HFONT)SendDlgItemMessage(hwndDlg, IDC_NEWVERSIONLABEL, WM_GETFONT, 0, 0);
				GetObject(hFont, sizeof(lf), &lf);
				lf.lfWeight = FW_BOLD;
				hFont = CreateFontIndirect(&lf);
				SendDlgItemMessage(hwndDlg, IDC_NEWVERSIONLABEL, WM_SETFONT, (WPARAM)hFont, 0);
			}
			SetFocus(GetDlgItem(hwndDlg, IDOK));
			SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);
		}
		break;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDC_VERSION:
			{
				UpdateNotifyData *und = (UpdateNotifyData*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
				if (und&&und->notesUrl)
					CallService(MS_UTILS_OPENURL, 1, (LPARAM)und->notesUrl);
				break;
			}
		case IDC_DOWNLOAD:
			{
				UpdateNotifyData *und = (UpdateNotifyData*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
				if (und&&und->downloadUrl) {
					CallService(MS_UTILS_OPENURL, 1, (LPARAM)und->downloadUrl);
					DestroyWindow(hwndDlg);
				}
				break;
			}
		case IDOK:
		case IDCANCEL:
			DestroyWindow(hwndDlg);
			return TRUE;
		}
		break;

	case WM_DESTROY:
		Window_FreeIcon_IcoLib( hwndDlg );
		break;
	}
	return FALSE;
}

static INT_PTR CALLBACK UpdateNotifyOptsProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hwndDlg);
		CheckDlgButton(hwndDlg, IDC_ENABLEUPDATES, DBGetContactSettingByte(NULL, UN_MOD, UN_ENABLE, UN_ENABLE_DEF) ? BST_CHECKED : BST_UNCHECKED);
		switch (DBGetContactSettingByte(NULL, UN_MOD, UN_NOTIFYTYPE, UN_NOTIFYTYPE_STABLE)) {
				case UN_NOTIFYTYPE_BETA: CheckDlgButton(hwndDlg, IDC_ENABLEBETA, BST_CHECKED); break;
				case UN_NOTIFYTYPE_ALPHA: CheckDlgButton(hwndDlg, IDC_ENABLEALPHA, BST_CHECKED); break;
				default: CheckDlgButton(hwndDlg, IDC_ENABLESTABLE, BST_CHECKED); break;
		}
		return TRUE;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
	case IDC_ENABLEUPDATES:
	case IDC_ENABLEALPHA:
    case IDC_ENABLEBETA:
    case IDC_ENABLESTABLE:
		SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
		break;
		}
		break;

	case WM_NOTIFY:
		{
			NMHDR *hdr = (NMHDR *)lParam;
			if (hdr&&hdr->code == PSN_APPLY) {
				DBWriteContactSettingByte(NULL, UN_MOD, UN_ENABLE, (BYTE)(IsDlgButtonChecked(hwndDlg, IDC_ENABLEUPDATES)));
                if (IsDlgButtonChecked(hwndDlg, IDC_ENABLESTABLE))
                    DBWriteContactSettingByte(NULL, UN_MOD, UN_NOTIFYTYPE, UN_NOTIFYTYPE_STABLE);
                if (IsDlgButtonChecked(hwndDlg, IDC_ENABLEBETA))
                    DBWriteContactSettingByte(NULL, UN_MOD, UN_NOTIFYTYPE, UN_NOTIFYTYPE_BETA);
                if (IsDlgButtonChecked(hwndDlg, IDC_ENABLEALPHA))
                    DBWriteContactSettingByte(NULL, UN_MOD, UN_NOTIFYTYPE, UN_NOTIFYTYPE_ALPHA);
			}
			break;
		}
	}
	return FALSE;
}