summaryrefslogtreecommitdiff
path: root/plugins/WebView/src/webview_alerts.cpp
blob: 525d9e8ba6d2fecebdbc52718944b280c50d8a61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/*
* A plugin for Miranda IM which displays web page text in a window Copyright
* (C) 2005 Vincent Joyce.
*
* Miranda IM: the free icq client for MS Windows  Copyright (C) 2000-2
* Richard Hughes, Roland Rabien & Tristan Van de Vreede
*
* 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 "webview.h"

/*****************************************************************************/
int CALLBACK PopupDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) {
	case WM_COMMAND:
	case WM_CONTEXTMENU:
		MCONTACT hContact = PUGetContact(hWnd);
		ptrW url( db_get_wsa(hContact, MODULENAME, URL_KEY));

		if (message == WM_COMMAND) { // left click
			if(hContact != NULL) { 
				// open data window
				if ( db_get_b(NULL, MODULENAME, LCLK_WINDOW_KEY, 0)) {
					NotifyEventHooks(hHookDisplayDataAlert, (int) hContact, 0);
					mir_forkthread(GetData, (void*)hContact);
					PUDeletePopup(hWnd);
				}
				// open url
				if (db_get_b(NULL, MODULENAME, LCLK_WEB_PGE_KEY, 0)) {
					Utils_OpenUrlT(url);
					PUDeletePopup(hWnd);
					db_set_w(wParam, MODULENAME, "Status", ID_STATUS_ONLINE); 
				}
				// dismiss
				if (db_get_b(NULL, MODULENAME, LCLK_DISMISS_KEY, 1))
					PUDeletePopup(hWnd);
			}	
			else if (hContact == NULL)
				PUDeletePopup(hWnd);  
		} 
		else if (message == WM_CONTEXTMENU) { // right click
			if (hContact != NULL) {   
				// open datA window
				if (db_get_b(NULL, MODULENAME, RCLK_WINDOW_KEY, 0)) {
					NotifyEventHooks(hHookDisplayDataAlert, (int) hContact, 0);
					mir_forkthread(GetData, (void*)hContact);
					PUDeletePopup(hWnd);
				}
				// open url
				if (db_get_b(NULL, MODULENAME, RCLK_WEB_PGE_KEY, 1)) {
					Utils_OpenUrlT(url);
					PUDeletePopup(hWnd);
					db_set_w(wParam, MODULENAME, "Status", ID_STATUS_ONLINE); 
				}
				// dismiss
				if ( db_get_b(NULL, MODULENAME, RCLK_DISMISS_KEY, 0))
					PUDeletePopup(hWnd);
			}
			else if(hContact == NULL)
				PUDeletePopup(hWnd);   
		}
		break;
	}

	return DefWindowProc(hWnd, message, wParam, lParam);
}

/*****************************************************************************/
int WDisplayDataAlert(MCONTACT hContact)
{
	NotifyEventHooks(hHookDisplayDataAlert, hContact, 0);
	return 0;
}

/*****************************************************************************/
int WAlertPopup(MCONTACT hContact, wchar_t *displaytext)
{
	NotifyEventHooks(hHookAlertPopup, hContact, (LPARAM)displaytext);
	return 0;
}

/*****************************************************************************/
int WErrorPopup(MCONTACT hContact, wchar_t *textdisplay)
{
	NotifyEventHooks(hHookErrorPopup, hContact, (LPARAM) textdisplay);
	return 0;
}

/*****************************************************************************/
int WAlertOSD(MCONTACT hContact, wchar_t *displaytext)
{
	NotifyEventHooks(hHookAlertOSD, hContact, (LPARAM) displaytext);
	return 0;
}

/*****************************************************************************/
int PopupAlert(WPARAM wParam, LPARAM lParam)
{
	POPUPDATAT ppd = { 0 };

	if( ((HANDLE)wParam) != NULL) {
		DBVARIANT dbv;
		db_get_ws(wParam, MODULENAME, PRESERVE_NAME_KEY, &dbv);
		mir_wstrncpy(ppd.lptzContactName, dbv.ptszVal, _countof(ppd.lptzContactName));
		db_free(&dbv);
	}
	else mir_wstrcpy(ppd.lptzContactName, _A2W(MODULENAME));

	ppd.lchContact = wParam;
	ppd.lchIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_SITE));

	wchar_t *displaytext = (wchar_t*)lParam;
	if ((mir_wstrlen(displaytext) == MAX_SECONDLINE) ||  (mir_wstrlen(displaytext) > MAX_SECONDLINE))
		mir_snwprintf(ppd.lptzText, displaytext);
	else if (mir_wstrlen(displaytext) < MAX_SECONDLINE)
		mir_snwprintf(ppd.lptzText, displaytext);

	if ( db_get_b(NULL, MODULENAME, POP_USECUSTCLRS_KEY, 0)) {
		ppd.colorBack = db_get_dw(NULL, MODULENAME, POP_BG_CLR_KEY, Def_color_bg);
		ppd.colorText = db_get_dw(NULL, MODULENAME, POP_TXT_CLR_KEY, Def_color_txt);
	}
	else if ( db_get_b(NULL, MODULENAME, POP_USEWINCLRS_KEY, 0)) {
		ppd.colorBack = GetSysColor(COLOR_BTNFACE);
		ppd.colorText = GetSysColor(COLOR_WINDOWTEXT);
	}
	else if ( db_get_b(NULL, MODULENAME, POP_USESAMECLRS_KEY, 1)) {
		ppd.colorBack = BackgoundClr;
		ppd.colorText = TextClr;
	}

	ppd.PluginWindowProc = NULL;
	ppd.iSeconds = db_get_dw(NULL, MODULENAME, POP_DELAY_KEY, 0);

	if (ServiceExists(MS_POPUP_ADDPOPUPT))
		CallService(MS_POPUP_ADDPOPUPT, (WPARAM)&ppd, 0);

	return 0;
}

/*****************************************************************************/
int OSDAlert(WPARAM hContact, LPARAM lParam)
{
	char contactname[255], newdisplaytext[2000];
	contactname[0] = 0;

	if (hContact != NULL) {
		DBVARIANT dbv;
		if (!db_get_s(hContact, MODULENAME, PRESERVE_NAME_KEY, &dbv)) {
			strncpy_s(contactname, _countof(contactname), dbv.pszVal, _TRUNCATE);
			db_free(&dbv);
		}
	}
	if (contactname[0] == 0)
		strncpy_s(contactname, _countof(contactname), MODULENAME, _TRUNCATE);

	char *displaytext = (char*)lParam;
	mir_snprintf(newdisplaytext, "%s: %s", contactname, Translate(displaytext));

	if (ServiceExists("OSD/Announce"))
		CallService("OSD/Announce", (WPARAM)newdisplaytext, 0);

	return 0;
}

/*****************************************************************************/
int ErrorMsgs(WPARAM wParam, LPARAM lParam)
{
	MCONTACT hContact = wParam;
	wchar_t newdisplaytext[2000], *displaytext = (wchar_t*)lParam;

	if (db_get_b(NULL, MODULENAME, SUPPRESS_ERR_KEY, 0))
		return 0;

	wchar_t *ptszContactName = pcli->pfnGetContactDisplayName(hContact, 0);
	if (ServiceExists(MS_POPUP_ADDPOPUPT) && db_get_b(NULL, MODULENAME, ERROR_POPUP_KEY, 0)) {
		mir_snwprintf(newdisplaytext, L"%s\n%s", ptszContactName, displaytext);
		PUShowMessageT(newdisplaytext, SM_WARNING);
	}
	else if ( ServiceExists("OSD/Announce") && db_get_b(NULL, MODULENAME, ERROR_POPUP_KEY, 0)) {
		mir_snwprintf(newdisplaytext, L"%s: %s", ptszContactName, TranslateW(displaytext));
		CallService("OSD/Announce", (WPARAM)newdisplaytext, 0);
	}

	Clist_TrayNotifyW(NULL, ptszContactName, TranslateW(displaytext), NIIF_ERROR, 15000);
	return 0;
}

/*****************************************************************************/
void SaveToFile(MCONTACT hContact, char *truncated)
{
	char *mode;
	if (!db_get_b(hContact, MODULENAME, APPEND_KEY, 0))
		mode = "w";
	else
		mode = "a";

	char url[300]; url[0] = '\0';
	DBVARIANT dbv;
	if (!db_get_s(hContact, MODULENAME, URL_KEY, &dbv)) {
		strncpy_s(url, _countof(url), dbv.pszVal, _TRUNCATE);
		db_free(&dbv);
	}

	if ( db_get_s(hContact, MODULENAME, FILE_KEY, &dbv))
		return;

	FILE *pfile = fopen(dbv.pszVal, mode);
	if (pfile == NULL)
		WErrorPopup(hContact, TranslateT("Cannot write to file"));
	else {
		char timestring[128], timeprefix[32];
		char temptime1[32], temptime2[32];

		time_t ftime = time(NULL);
		struct tm *nTime = localtime(&ftime);

		mir_snprintf(timeprefix, " %s ", Translate("Last updated on"));
		strftime(temptime1, 32, " %a, %b %d, %Y ", nTime);
		strftime(temptime2, 32, " %I:%M %p.", nTime);
		mir_snprintf(timestring, "(%s)%s\n%s,%s\n", MODULENAME, url, temptime1, temptime2);

		fputs(timestring, pfile);
		fwrite(truncated, mir_strlen(truncated), 1, pfile);
		fputs("\n\n", pfile);
		fclose(pfile);
	}

	db_free(&dbv);
}

/*****************************************************************************/
int ProcessAlerts(MCONTACT hContact, char *truncated, char *tstr, char *contactname, int notpresent)
{
	char alertstring[255];
	wchar_t displaystring[300];
	FILE  *pcachefile;
	DBVARIANT tdbv;
	int wasAlert = 0;

	int statalertpos = 0, disalertpos = 0, statalertposend = 0;
	char*alertpos;
	char Alerttempstring[300], Alerttempstring2[300];
	static char cachecompare[MAXSIZE1];
	static char raw[MAXSIZE1];

	int alertIndex = 0, eventIndex = 0;

	char tempraw[MAXSIZE1];
	memset(&tempraw, 0, sizeof(tempraw));
	memset(&raw, 0, sizeof(raw));

	strncpy(tempraw, truncated, _countof(tempraw));

	memset(&alertstring, 0, sizeof(alertstring));
	memset(&Alerttempstring, 0, sizeof(Alerttempstring));
	memset(&Alerttempstring2, 0, sizeof(Alerttempstring2));
	memset(&cachecompare, 0, sizeof(cachecompare));

	// alerts
	if (db_get_b(hContact, MODULENAME, ENABLE_ALERTS_KEY, 0)) { // ALERTS
		alertIndex = db_get_b(hContact, MODULENAME, ALRT_INDEX_KEY, 0);
		eventIndex = db_get_b(hContact, MODULENAME, EVNT_INDEX_KEY, 0);
		if (notpresent) {
			if (alertIndex == 0) { // Popup
				Sleep(1000);
				WAlertPopup(hContact, TranslateT("Start/end strings not found or strings not set."));
				// contactlist name//
				if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
					db_set_s(hContact, "CList", "MyHandle", tstr);
			}
			else if (alertIndex == 1) { // log to file
				if (!db_get_s(hContact, MODULENAME, FILE_KEY, &tdbv)) {
					int AmountWspcRem = 0;

					if (!db_get_b(hContact, MODULENAME, SAVE_AS_RAW_KEY, 0)) {
						CodetoSymbol(tempraw);
						Sleep(100); // avoid 100% CPU

						EraseBlock(tempraw);
						Sleep(100); // avoid 100% CPU

						FastTagFilter(tempraw);
						Sleep(100); // avoid 100% CPU

						NumSymbols(tempraw);
						Sleep(100); // avoid 100% CPU

						EraseSymbols(tempraw);
						Sleep(100); // avoid 100% CPU

						AmountWspcRem = db_get_b(hContact, MODULENAME, RWSPACE_KEY, 0);
						RemoveInvis(tempraw, AmountWspcRem);
						Sleep(100); // avoid 100% CPU

						Removewhitespace(tempraw);
					}

					SaveToFile(hContact, tempraw);
					db_free(&tdbv);

					if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
						db_set_s(hContact, "CList", "MyHandle", tstr);
				}
			} 
			else if (alertIndex == 3) {
				WAlertOSD(hContact, TranslateT("Alert start/end strings not found or strings not set."));
				if (db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
					db_set_s(hContact, "CList", "MyHandle", tstr);
			}
			else if (eventIndex == 2) {
				WDisplayDataAlert(hContact);

				if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
					db_set_s(hContact, "CList", "MyHandle", tstr);

				HWND hwndDlg = (WindowList_Find(hWindowList, hContact));

				SetDlgItemText(hwndDlg, IDC_STATUSBAR, TranslateT("Start/end strings not found or strings not set."));
			}
			else MessageBox(NULL, TranslateT("Start/end strings not found or strings not set."), _A2W(MODULENAME), MB_OK);
		}

		if (eventIndex == 0) { // string present
			if (!db_get_s(hContact, MODULENAME, ALERT_STRING_KEY, &tdbv)) {
				strncpy_s(alertstring, _countof(alertstring), tdbv.pszVal, _TRUNCATE);
				db_free(&tdbv);

				if ((strstr(tempraw, alertstring)) != 0) { // // ENDALERT EVENT:CHECK FOR STRING
					// there was an alert
					wasAlert = 1;

					// play sound?
					SkinPlaySound("webviewalert");
					//
					if ((!notpresent)) {
						if (alertIndex == 0) { // popup
							mir_snwprintf(displaystring, L"%s \"%S\" %s.", Translate("The string"), alertstring, Translate("has been found on the web page"));
							WAlertPopup(hContact, displaystring);

							// contactlist name//
							if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
								db_set_s(hContact, "CList", "MyHandle", tstr);
						} //
						else if (alertIndex == 1) {
							if (!db_get_s(hContact, MODULENAME, FILE_KEY, &tdbv)) {
								int AmountWspcRem = 0;
								if (!db_get_b(hContact, MODULENAME, SAVE_AS_RAW_KEY, 0)) {
									CodetoSymbol(tempraw);
									Sleep(100); // avoid 100% CPU

									EraseBlock(tempraw);
									Sleep(100); // avoid 100% CPU

									FastTagFilter(tempraw);
									Sleep(100); // avoid 100% CPU

									NumSymbols(tempraw);
									Sleep(100); // avoid 100% CPU

									EraseSymbols(tempraw);
									Sleep(100); // avoid 100% CPU

									AmountWspcRem = db_get_b(hContact, MODULENAME, RWSPACE_KEY, 0);
									RemoveInvis(tempraw, AmountWspcRem);
									Sleep(100); // avoid 100% CPU

									Removewhitespace(tempraw);
								}
								SaveToFile(hContact, tempraw);
								db_free(&tdbv);

								if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
									db_set_s(hContact, "CList", "MyHandle", tstr);
							}
						}
						else if (alertIndex == 3) {
							mir_snwprintf(displaystring, L"%s \"%s\" %s.", TranslateT("The string"), alertstring, TranslateT("has been found on the web page"));
							WAlertOSD(hContact, displaystring);

							// contactlist name//
							if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
								db_set_s(hContact, "CList", "MyHandle", tstr);
						}
						else if (alertIndex == 2) {
							WDisplayDataAlert(hContact);
							// contactlist name//
							if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
								db_set_s(hContact, "CList", "MyHandle", tstr);

							HWND hwndDlg = WindowList_Find(hWindowList, hContact);
							SetDlgItemText(hwndDlg, IDC_STATUSBAR, TranslateT("Download successful; about to process data..."));
						}
						else MessageBox(NULL, TranslateT("Unknown alert type."), _A2W(MODULENAME), MB_OK);
					}
				}
			}
		}
		else if (eventIndex == 1) { // webpage changed
			// TEST GET NAME FOR CACHE
			wchar_t cachepath[MAX_PATH], cachedirectorypath[MAX_PATH], newcachepath[MAX_PATH + 50];
			GetModuleFileName(hInst, cachepath, _countof(cachepath));
			wchar_t *cacheend = wcsrchr(cachepath, '\\');
			cacheend++;
			*cacheend = '\0';

			mir_snwprintf(cachedirectorypath, L"%s%S%S", cachepath, MODULENAME, "cache\\");
			CreateDirectory(cachedirectorypath, NULL);
			mir_snwprintf(newcachepath, L"%s%S%S%S%S", cachepath, MODULENAME, "cache\\", contactname, ".txt");
			// file exists?
			if ( _waccess(newcachepath, 0) != -1) {
				if ((pcachefile = _wfopen(newcachepath, L"r")) == NULL)
					WErrorPopup((UINT_PTR)contactname, TranslateT("Cannot read from file"));
				else {
					memset(&cachecompare, 0, sizeof(cachecompare));
					fread(cachecompare, sizeof(cachecompare), 1, pcachefile);
					fclose(pcachefile);
				}
			}
			// write to cache
			if ((pcachefile = _wfopen(newcachepath, L"w")) == NULL)
				WErrorPopup((UINT_PTR)contactname, TranslateT("Cannot write to file 1"));
			else {
				fwrite(tempraw, mir_strlen(tempraw), 1, pcachefile); //smaller cache
				fclose(pcachefile);
				db_set_ws(hContact, MODULENAME, CACHE_FILE_KEY, newcachepath);
			}
			// end write to cache

			if (strncmp(tempraw, cachecompare, mir_strlen(tempraw)) != 0) { //lets try this instead
				// play sound?
				SkinPlaySound("webviewalert");
				// there was an alert
				wasAlert = 1;

				if (!notpresent) {
					if (alertIndex == 0) { // popup
						WAlertPopup(hContact, TranslateT("The web page has changed."));
						// contactlist name//
						if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
							db_set_s(hContact, "CList", "MyHandle", tstr);
					}
					else if (alertIndex == 3) { // osd
						WAlertOSD(hContact, TranslateT("The web page has changed."));
						// contactlist name//
						if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
							db_set_s(hContact, "CList", "MyHandle", tstr);
					}
					else if (alertIndex == 1) { // log
						if (!db_get_s(hContact, MODULENAME, FILE_KEY, &tdbv)) {
							int AmountWspcRem = 0;

							if (!db_get_b(hContact, MODULENAME, SAVE_AS_RAW_KEY, 0)) {
								CodetoSymbol(tempraw);
								Sleep(100); // avoid 100% CPU

								EraseBlock(tempraw);
								Sleep(100); // avoid 100% CPU

								FastTagFilter(tempraw);
								Sleep(100); // avoid 100% CPU

								NumSymbols(tempraw);
								Sleep(100); // avoid 100% CPU

								EraseSymbols(tempraw);
								Sleep(100); // avoid 100% CPU

								AmountWspcRem = db_get_b(hContact, MODULENAME, RWSPACE_KEY, 0);
								RemoveInvis(tempraw, AmountWspcRem);
								Sleep(100); // avoid 100% CPU

								Removewhitespace(tempraw);
							}

							SaveToFile(hContact, tempraw);
							db_free(&tdbv);
							// contactlist name//
							if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
								db_set_s(hContact, "CList", "MyHandle", tstr);
						}
					}
					else if (alertIndex == 2) { // window
						WDisplayDataAlert(hContact);
						// contactlist name//
						if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
							db_set_s(hContact, "CList", "MyHandle", tstr);
					}
					else MessageBox(NULL, TranslateT("Unknown alert type."), _A2W(MODULENAME), MB_OK);
				}
			}
		}

		if (eventIndex == 2) { // part of webpage changed
			Alerttempstring[0] = Alerttempstring2[0] = 0;
			if (!db_get_s(hContact, MODULENAME, ALRT_S_STRING_KEY, &tdbv)) {
				strncpy_s(Alerttempstring, _countof(Alerttempstring), tdbv.pszVal, _TRUNCATE);
				db_free(&tdbv);
			}
			if (!db_get_s(hContact, MODULENAME, ALRT_E_STRING_KEY, &tdbv)) {
				strncpy_s(Alerttempstring2, _countof(Alerttempstring2), tdbv.pszVal, _TRUNCATE);
				db_free(&tdbv);
			}

			// putting data into string
			if (((strstr(tempraw, Alerttempstring)) != 0) && ((strstr(tempraw, Alerttempstring2)) != 0)) {
				//start string
				alertpos = strstr(tempraw, Alerttempstring);
				statalertpos = alertpos - tempraw;

				memset(&alertpos, 0, sizeof(alertpos));
				//end string
				alertpos = strstr(tempraw, Alerttempstring2);
				statalertposend = alertpos - tempraw + (int)mir_strlen(Alerttempstring2);

				if (statalertpos > statalertposend) {
					memset(&tempraw, ' ', statalertpos);
					memset(&alertpos, 0, sizeof(alertpos));
					alertpos = strstr(tempraw, Alerttempstring2);
					statalertposend = alertpos - tempraw + (int)mir_strlen(Alerttempstring2);
				}

				if (statalertpos < statalertposend) {
					memset(&raw, 0, sizeof(raw));

					//start string
					alertpos = strstr(tempraw, Alerttempstring);
					statalertpos = alertpos - tempraw;

					//end string
					alertpos = strstr(tempraw, Alerttempstring2);
					statalertposend = alertpos - tempraw + (int)mir_strlen(Alerttempstring2);

					if (statalertpos > statalertposend) {
						memset(&tempraw, ' ', statalertpos);
						memset(&alertpos, 0, sizeof(alertpos));
						alertpos = strstr(tempraw, Alerttempstring2);
						statalertposend = alertpos - tempraw + (int)mir_strlen(Alerttempstring2);
					}
					disalertpos = 0;

					//write selected data to string
					strncpy(raw, &tempraw[statalertpos], (statalertposend - statalertpos));
					raw[(statalertposend - statalertpos)] = '\0';
				}
			} // end putting data into string
			else { // start and/or end string not present
				if (alertIndex == 0) { // popup
					Sleep(1000);
					WAlertPopup(hContact, TranslateT("Alert start/end strings not found or strings not set."));
					// contactlist name//
					if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
						db_set_s(hContact, "CList", "MyHandle", tstr);
				}
				else if (alertIndex == 1) { // LOG
					if (!notpresent) { // dont log to file twice if both types of start/end strings not present
						if (!db_get_s(hContact, MODULENAME, FILE_KEY, &tdbv)) {
							int AmountWspcRem = 0;
							if (!db_get_b(hContact, MODULENAME, SAVE_AS_RAW_KEY, 0)) {
								CodetoSymbol(tempraw);
								Sleep(100); // avoid 100% CPU

								EraseBlock(tempraw);
								Sleep(100); // avoid 100% CPU

								FastTagFilter(tempraw);
								Sleep(100); // avoid 100% CPU

								NumSymbols(tempraw);
								Sleep(100); // avoid 100% CPU

								EraseSymbols(tempraw);
								Sleep(100); // avoid 100% CPU

								AmountWspcRem = db_get_b(hContact, MODULENAME, RWSPACE_KEY, 0);
								RemoveInvis(tempraw, AmountWspcRem);
								Sleep(100); // avoid 100% CPU

								Removewhitespace(tempraw);
							}

							SaveToFile(hContact, tempraw);
							db_free(&tdbv);
							// contactlist name
							if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
								db_set_s(hContact, "CList", "MyHandle", tstr);
						}
					}
				} 
				else if (alertIndex == 3) { // osd
					WAlertOSD(hContact, TranslateT("Alert start/end strings not found or strings not set."));
					// contactlist name//
					if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
						db_set_s(hContact, "CList", "MyHandle", tstr);
				}
				else if (alertIndex == 2) { // window
					WDisplayDataAlert(hContact);
					// contactlist name//
					if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
						db_set_s(hContact, "CList", "MyHandle", tstr);

					HWND hwndDlg = (WindowList_Find(hWindowList, hContact));
					SetDlgItemText(hwndDlg, IDC_STATUSBAR, TranslateT("Alert start/end strings not found or strings not set."));
				}
				else MessageBox(NULL, TranslateT("Alert start/end strings not found or strings not set."), _A2W(MODULENAME), MB_OK);

				db_set_w(hContact, MODULENAME, "Status", ID_STATUS_AWAY);
			}

			///////////////
			if (((strstr(tempraw, Alerttempstring)) != 0) && ((strstr(tempraw, Alerttempstring2)) != 0)) {
				// TEST GET NAME FOR CACHE
				wchar_t cachepath[MAX_PATH], cachedirectorypath[MAX_PATH], newcachepath[MAX_PATH + 50];
				GetModuleFileName(hInst, cachepath, _countof(cachepath));
				wchar_t *cacheend = wcsrchr(cachepath, '\\');
				cacheend++;
				*cacheend = '\0';

				mir_snwprintf(cachedirectorypath, L"%s%S%S", cachepath, MODULENAME, "cache\\");
				CreateDirectory(cachedirectorypath, NULL);
				mir_snwprintf(newcachepath, L"%s%S%S%S%S", cachepath, MODULENAME, "cache\\", contactname, ".txt");
				// file exists?
				if ( _waccess(newcachepath, 0) != -1) {
					if ((pcachefile = _wfopen(newcachepath, L"r")) == NULL)
						WErrorPopup((UINT_PTR)contactname, TranslateT("Cannot read from file"));
					else {
						memset(&cachecompare, 0, sizeof(cachecompare));
						fread(cachecompare, sizeof(cachecompare), 1, pcachefile);
						fclose(pcachefile);
					}
				}
				// write to cache
				if ((pcachefile = _wfopen(newcachepath, L"w")) == NULL)
					WErrorPopup((UINT_PTR)contactname, TranslateT("Cannot write to file 2"));
				else {
					fwrite(raw, mir_strlen(raw), 1, pcachefile); //smaller cache
					db_set_ws(hContact, MODULENAME, CACHE_FILE_KEY, newcachepath);
					fclose(pcachefile);
				}
				// end write to cache
				if (strncmp(raw, cachecompare, (mir_strlen(raw))) != 0) { //lets try this instead
					// play sound?
					SkinPlaySound("webviewalert");
					// there was an alert
					wasAlert = 1;

					if (!notpresent) {
						if (alertIndex == 0) { // popup
							WAlertPopup(hContact, TranslateT("Specific part of the web page has changed."));
							// contactlist name//
							if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
								db_set_s(hContact, "CList", "MyHandle", tstr);
						}
						else if (alertIndex == 3) { // osd
							WAlertOSD(hContact, TranslateT("Specific part of the web page has changed."));
							// contactlist name//
							if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
								db_set_s(hContact, "CList", "MyHandle", tstr);
						}
						else if (alertIndex == 1) { // log to file
							if (!db_get_s(hContact, MODULENAME, FILE_KEY, &tdbv)) {
								int AmountWspcRem = 0;
								if (!db_get_b(hContact, MODULENAME, SAVE_AS_RAW_KEY, 0)) {
									CodetoSymbol(tempraw);
									Sleep(100); // avoid 100% CPU

									EraseBlock(tempraw);
									Sleep(100); // avoid 100% CPU

									FastTagFilter(tempraw);
									Sleep(100); // avoid 100% CPU

									NumSymbols(tempraw);
									Sleep(100); // avoid 100% CPU

									EraseSymbols(tempraw);
									Sleep(100); // avoid 100% CPU

									AmountWspcRem = db_get_b(hContact, MODULENAME, RWSPACE_KEY, 0);
									RemoveInvis(tempraw, AmountWspcRem);
									Sleep(100); // avoid 100% CPU

									Removewhitespace(tempraw);
								}

								SaveToFile(hContact, tempraw);
								db_free(&tdbv);
								// contactlist name
								if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
									db_set_s(hContact, "CList", "MyHandle", tstr);
							}
						}
						else if (alertIndex == 2) { // window
							WDisplayDataAlert(hContact);
							// contactlist name//
							if ( db_get_b(hContact, MODULENAME, APND_DATE_NAME_KEY, 0))
								db_set_s(hContact, "CList", "MyHandle", tstr);
						}
						else MessageBox(NULL, TranslateT("Unknown alert type."), _A2W(MODULENAME), MB_OK);
					}
				}
			}
		} // alert type
	}
	// end alerts

	//if always log to file option is enabled do this
	if (wasAlert && alertIndex != 1) { // dont do for log to file alert
		if ( db_get_b(hContact, MODULENAME, ALWAYS_LOG_KEY, 0)) {
			if (!db_get_s(hContact, MODULENAME, FILE_KEY, &tdbv)) {
				int AmountWspcRem = 0;

				if (!db_get_b(hContact, MODULENAME, SAVE_AS_RAW_KEY, 0)) {
					CodetoSymbol(tempraw);
					Sleep(100); // avoid 100% CPU

					EraseBlock(tempraw);
					Sleep(100); // avoid 100% CPU

					FastTagFilter(tempraw);
					Sleep(100); // avoid 100% CPU

					NumSymbols(tempraw);
					Sleep(100); // avoid 100% CPU

					EraseSymbols(tempraw);
					Sleep(100); // avoid 100% CPU

					AmountWspcRem = db_get_b(hContact, MODULENAME, RWSPACE_KEY, 0);
					RemoveInvis(tempraw, AmountWspcRem);
					Sleep(100); // avoid 100% CPU

					Removewhitespace(tempraw);
				}

				SaveToFile(hContact, tempraw);
				db_free(&tdbv);
			}
		}
	}
	strncpy(truncated, tempraw, mir_strlen(truncated));
	return wasAlert;
}

/*****************************************************************************/
int DataWndAlertCommand(WPARAM wParam, LPARAM)
{
	MCONTACT hContact = wParam;
	if ( WindowList_Find(hWindowList, hContact))
		return 0;

	HWND hwndDlg = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_DISPLAY_DATA), NULL, DlgProcDisplayData, hContact);
	HWND hTopmost = db_get_b(hContact, MODULENAME, ON_TOP_KEY, 0) ? HWND_TOPMOST : HWND_NOTOPMOST;
	SendDlgItemMessage(hwndDlg, IDC_STICK_BUTTON, BM_SETIMAGE, IMAGE_ICON, (LPARAM) ((HICON) LoadImage(hInst, MAKEINTRESOURCE(IDI_STICK), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 0)));
	if ( db_get_b(NULL, MODULENAME, SAVE_INDIVID_POS_KEY, 0))
		SetWindowPos(hwndDlg, hTopmost,
			db_get_dw(hContact, MODULENAME, "WVx", 100), // Xposition,
			db_get_dw(hContact, MODULENAME, "WVy", 100), // Yposition,
			db_get_dw(hContact, MODULENAME, "WVwidth", 100), // WindowWidth,
			db_get_dw(hContact, MODULENAME, "WVheight", 100), 0); // WindowHeight,
	else
		SetWindowPos(hwndDlg, HWND_TOPMOST, Xposition, Yposition, WindowWidth, WindowHeight, 0);

	ShowWindow(hwndDlg, SW_SHOW);
	SetActiveWindow(hwndDlg);
	return 0;
}

/*****************************************************************************/
void ReadFromFile(void *param)
{
	MCONTACT hContact = (UINT_PTR)param;

	DBVARIANT dbv;
	char truncated[MAXSIZE1];
	int  AmountWspcRem = 0;
	int  fileexists = 0;         

	HWND hwndDlg = WindowList_Find(hWindowList, hContact);

	char contactname[100]; contactname[0] = 0;
	if (!db_get_s(hContact, "CList", "MyHandle", &dbv)) {
		strncpy_s(contactname, _countof(contactname), dbv.pszVal, _TRUNCATE);
		db_free(&dbv);
	}

	if ( db_get_s(hContact, MODULENAME, CACHE_FILE_KEY, &dbv))
		return;

	FILE *pfile;
	if ((pfile = fopen(dbv.pszVal, "r")) == NULL) {
		SendToRichEdit(hwndDlg, Translate("Cannot read from cache file"), TextClr, BackgoundClr);
		fileexists = 0;
	} 
	else {
		fread(truncated, sizeof(truncated), 1, pfile);
		fclose(pfile);
		fileexists =1;
	}

	db_free(&dbv);

	if(fileexists) {
		CodetoSymbol(truncated);
		Sleep(100); // avoid 100% CPU

		EraseBlock(truncated);
		Sleep(100); // avoid 100% CPU

		FastTagFilter(truncated);
		Sleep(100); // avoid 100% CPU

		NumSymbols(truncated);
		Sleep(100); // avoid 100% CPU

		EraseSymbols(truncated);
		Sleep(100); // avoid 100% CPU

		AmountWspcRem = db_get_b(hContact, MODULENAME, RWSPACE_KEY, 0);
		RemoveInvis(truncated, AmountWspcRem);
		Sleep(100); // avoid 100% CPU

		Removewhitespace(truncated);

		SendToRichEdit(hwndDlg, truncated, TextClr, BackgoundClr);
		SetDlgItemText(hwndDlg, IDC_STATUSBAR, TranslateT("Loaded from cache")); 
	}
}