-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathw32log.c
1525 lines (1324 loc) · 44.5 KB
/
w32log.c
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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/w32log.c,v $
*
* Purpose : Functions for creating and destroying the log window,
* outputting strings, processing messages and so on.
*
* Copyright : Written by and Copyright (C) 2001-2009 members of
* the Privoxy team. https://www.privoxy.org/
*
* Written by and Copyright (C) 1999 Adam Lock
*
* 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.
*
* The GNU General Public License should be included with
* this file. If not, you can view it at
* http://www.gnu.org/copyleft/gpl.html
* or write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*********************************************************************/
#include "config.h"
#include <assert.h>
#include <stdio.h>
#ifndef STRICT
#define STRICT
#endif
#include <winsock2.h>
#include <windows.h>
#include <richedit.h>
#include "project.h"
#include "w32log.h"
#include "w32taskbar.h"
#include "win32.h"
#include "w32res.h"
#include "jcc.h"
#include "miscutil.h"
#include "errlog.h"
#include "loadcfg.h"
#ifdef FEATURE_MANUAL_TAGGER
#include "list.h"
#endif
#ifndef _WIN_CONSOLE /* entire file */
/*
* Timers and the various durations
*/
#define TIMER_ANIM_ID 1
#define TIMER_ANIM_TIME 100
#define TIMER_ANIMSTOP_ID 2
#define TIMER_ANIMSTOP_TIME 1000
#define TIMER_CLIPBUFFER_ID 3
#define TIMER_CLIPBUFFER_TIME 1000
#define TIMER_CLIPBUFFER_FORCE_ID 4
#define TIMER_CLIPBUFFER_FORCE_TIME 5000
/*
* Styles of text that can be output
*/
#define STYLE_NONE 0
#define STYLE_HIGHLIGHT 1
#define STYLE_LINK 2
#define STYLE_HEADER 3
/*
* Number of frames of animation in tray activity sequence
*/
#define ANIM_FRAMES 8
#define DEFAULT_MAX_BUFFER_LINES 200
#define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
#define DEFAULT_LOG_FONT_SIZE 8
/*
* These values affect the way the log window behaves, they should be read
* from a file but for the moment, they are hardcoded here. Some options are
* configurable through the UI.
*/
/* Indicates whether task bar shows activity animation */
BOOL g_bShowActivityAnimation = 1;
/* Indicates whether the log window is shown */
BOOL g_bShowLogWindow = 1;
/* Indicates if the log window appears on the task bar */
BOOL g_bShowOnTaskBar = 0;
/* Indicates whether closing the log window really just hides it */
BOOL g_bCloseHidesWindow = 1;
/* Indicates if messages are logged at all */
BOOL g_bLogMessages = 1;
/* Indicates whether log messages are highlighted */
BOOL g_bHighlightMessages = 1;
/* Indicates if buffer is limited in size */
BOOL g_bLimitBufferSize = 1;
/* Maximum number of lines allowed in buffer when limited */
int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
/* Font to use */
char g_szFontFaceName[32] = DEFAULT_LOG_FONT_NAME;
/* Size of font to use */
int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
/* FIXME: this is a kludge */
const char * g_default_actions_file = NULL;
const char * g_user_actions_file = NULL;
const char * g_default_filterfile = NULL;
const char * g_user_filterfile = NULL;
#ifdef FEATURE_TRUST
const char * g_trustfile = NULL;
#endif /* def FEATURE_TRUST */
#ifdef FEATURE_FORWARD_CLASS
struct forward_class_spec* g_forward_class = NULL;
#endif /* def FEATURE_FORWARD_CLASS */
#ifdef FEATURE_MANUAL_TAGGER
struct manual_tagger_spec* g_manual_tagger = NULL;
#endif /* def FEATURE_MANUAL_TAGGER */
/* FIXME: end kludge */
/* Regular expression for detected URLs */
#define RE_URL "http:[^ \n\r]*"
/*
* Regular expressions that are used to perform highlight in the log window
*/
static struct _Pattern
{
const char *str;
int style;
regex_t buffer;
} patterns_to_highlight[] =
{
/* url headers */
{ RE_URL, STYLE_LINK },
/* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
/* interesting text to highlight */
/* see jcc.c crunch_reason for the full list */
{ "Crunch: Blocked:", STYLE_HIGHLIGHT },
{ "Crunch: Untrusted", STYLE_HIGHLIGHT },
{ "Crunch: Redirected:", STYLE_HIGHLIGHT },
{ "Crunch: DNS failure", STYLE_HIGHLIGHT },
{ "Crunch: Forwarding failed", STYLE_HIGHLIGHT },
{ "Crunch: Connection failure", STYLE_HIGHLIGHT },
{ "Crunch: Out of memory", STYLE_HIGHLIGHT },
{ "Connect: Found reusable socket", STYLE_HIGHLIGHT },
{ "Connect: Reusing server socket", STYLE_HIGHLIGHT },
{ "Connect: Created new connection to", STYLE_HIGHLIGHT },
{ "hung up on us", STYLE_HIGHLIGHT },
{ "Info: Loading actions file:", STYLE_HIGHLIGHT },
{ "Info: Loading filter file:", STYLE_HIGHLIGHT },
{ "Info: Now toggled ", STYLE_HIGHLIGHT },
{ "Crunching Referer:", STYLE_HIGHLIGHT },
/* what are all the possible error strings?? */
{ "Error:", STYLE_HIGHLIGHT },
/* http headers */
{ "referer:", STYLE_HEADER },
{ "proxy-connection:", STYLE_HEADER },
{ "proxy-agent:", STYLE_HEADER },
{ "user-agent:", STYLE_HEADER },
{ "host:", STYLE_HEADER },
{ "accept:", STYLE_HEADER },
{ "accept-encoding:", STYLE_HEADER },
{ "accept-language:", STYLE_HEADER },
{ "accept-charset:", STYLE_HEADER },
{ "accept-ranges:", STYLE_HEADER },
{ "date:", STYLE_HEADER },
{ "cache-control:", STYLE_HEADER },
{ "cache-last-checked:", STYLE_HEADER },
{ "connection:", STYLE_HEADER },
{ "content-type", STYLE_HEADER },
{ "content-length", STYLE_HEADER },
{ "cookie", STYLE_HEADER },
{ "last-modified:", STYLE_HEADER },
{ "pragma:", STYLE_HEADER },
{ "server:", STYLE_HEADER },
{ "etag:", STYLE_HEADER },
{ "expires:", STYLE_HEADER },
{ "warning:", STYLE_HEADER },
/* this is the terminator statement - do not delete! */
{ NULL, STYLE_NONE }
};
/*
* Public variables
*/
HWND g_hwndLogFrame;
HICON g_hiconApp;
/*
* Private variables
*/
static CRITICAL_SECTION g_criticalsection;
static HWND g_hwndTray;
static HWND g_hwndLogBox;
static WNDPROC g_fnLogBox;
static HICON g_hiconAnim[ANIM_FRAMES];
static HICON g_hiconIdle;
static HICON g_hiconOff;
static int g_nAnimFrame;
static BOOL g_bClipPending = FALSE;
static int g_nRichEditVersion = 0;
#if defined(FEATURE_FORWARD_CLASS) || defined(FEATURE_MANUAL_TAGGER)
static UINT g_uQueryMessage;
#endif
/*
* Private functions
*/
static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static BOOL InitRichEdit(void);
static void LogClipBuffer(void);
static void LogCreatePatternMatchingBuffers(void);
static void LogDestroyPatternMatchingBuffers(void);
static int LogPutStringNoMatch(const char *pszText, int style);
#ifdef FEATURE_MANUAL_TAGGER
static HMENU GetTagSubMenu(HMENU hmenu);
static void SetTagSubMenu(HMENU hmenu);
#endif /* def FEATURE_MANUAL_TAGGER */
#ifdef FEATURE_FORWARD_CLASS
static HMENU GetForwardSubMenu(HMENU hmenu);
static void SetForwardSubMenu(HMENU hmenu);
#endif /* def FEATURE_FORWARD_CLASS */
static void SetIdleIcon(void);
/*********************************************************************
*
* Function : InitLogWindow
*
* Description : Initialise the log window.
*
* Parameters : None
*
* Returns : Always TRUE (there should be error checking on the resources).
*
*********************************************************************/
BOOL InitLogWindow(void)
{
int i;
/* Load the icons */
g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
g_hiconOff = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_OFF));
for (i = 0; i < ANIM_FRAMES; i++)
{
g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
}
g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
/* Create the user interface */
g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
g_hwndTray = CreateTrayWindow(g_hInstance);
TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
/* Create pattern matching buffers (for highlighting */
LogCreatePatternMatchingBuffers();
/* Create a critical section to protect multi-threaded access to certain things */
InitializeCriticalSection(&g_criticalsection);
#if defined(FEATURE_FORWARD_CLASS) || defined(FEATURE_MANUAL_TAGGER)
g_uQueryMessage = RegisterWindowMessage("PrivoxyEnhancedQueryMessage");
#endif
return TRUE;
}
/*********************************************************************
*
* Function : TermLogWindow
*
* Description : Cleanup the logwindow.
*
* Parameters : None
*
* Returns : N/A
*
*********************************************************************/
void TermLogWindow(void)
{
int i;
LogDestroyPatternMatchingBuffers();
TrayDeleteIcon(g_hwndTray, 1);
DeleteObject(g_hiconApp);
DeleteObject(g_hiconIdle);
DeleteObject(g_hiconOff);
for (i = 0; i < ANIM_FRAMES; i++)
{
DeleteObject(g_hiconAnim[i]);
}
}
/*********************************************************************
*
* Function : LogCreatePatternMatchingBuffers
*
* Description : Compile the pattern matching buffers.
*
* Parameters : None
*
* Returns : N/A
*
*********************************************************************/
void LogCreatePatternMatchingBuffers(void)
{
int i;
for (i = 0; patterns_to_highlight[i].str != NULL; i++)
{
regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
}
}
/*********************************************************************
*
* Function : LogDestroyPatternMatchingBuffers
*
* Description : Free up the pattern matching buffers.
*
* Parameters : None
*
* Returns : N/A
*
*********************************************************************/
void LogDestroyPatternMatchingBuffers(void)
{
int i;
for (i = 0; patterns_to_highlight[i].str != NULL; i++)
{
regfree(&patterns_to_highlight[i].buffer);
}
}
/*********************************************************************
*
* Function : LogPutString
*
* Description : Inserts text into the logging window. This is really
* a regexp aware wrapper function to `LogPutStringNoMatch'.
*
* Parameters :
* 1 : pszText = pointer to string going to the log window
*
* Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
* FIXME: this is backwards to the rest of IJB and to common
* programming practice. Please use 0 => success instead.
*
*********************************************************************/
int LogPutString(const char *pszText)
{
int i;
int result = 0;
if (!g_bLogMessages)
{
return 1;
}
if (pszText == NULL || strlen(pszText) == 0)
{
return 1;
}
/* Critical section stops multiple threads doing nasty interactions that
* foul up the highlighting and output.
*/
EnterCriticalSection(&g_criticalsection);
if (g_bHighlightMessages)
{
regmatch_t match;
/* First things first, regexp scan for various things that we would like highlighted */
for (i = 0; patterns_to_highlight[i].str != NULL; i++)
{
if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
{
char *pszBefore = NULL;
char *pszMatch = NULL;
char *pszAfter = NULL;
int nMatchSize;
/* Split the string up into pieces representing the strings, before
at and after the matching pattern
*/
if (match.rm_so > 0)
{
pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
strncpy(pszBefore, pszText, match.rm_so);
}
if (match.rm_eo < (regoff_t)strlen(pszText))
{
pszAfter = strdup(&pszText[match.rm_eo]);
}
nMatchSize = match.rm_eo - match.rm_so;
pszMatch = (char *)malloc(nMatchSize + 1);
strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
pszMatch[nMatchSize] = '\0';
/* Recursively call LogPutString */
if (pszBefore)
{
LogPutString(pszBefore);
free(pszBefore);
}
if (pszMatch)
{
LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
free(pszMatch);
}
if (pszAfter)
{
LogPutString(pszAfter);
free(pszAfter);
}
result = 1;
goto end;
}
}
}
result = LogPutStringNoMatch(pszText, STYLE_NONE);
end:
LeaveCriticalSection(&g_criticalsection);
return result;
}
/*********************************************************************
*
* Function : LogPutStringNoMatch
*
* Description : Puts a string into the logging window.
*
* Parameters :
* 1 : pszText = pointer to string going to the log window
* 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
*
* Returns : Always 1 => success.
* FIXME: this is backwards to the rest of IJB and to common
* programming practice. Please use 0 => success instead.
*
*********************************************************************/
int LogPutStringNoMatch(const char *pszText, int style)
{
CHARRANGE range;
CHARFORMAT format;
int nTextLength;
assert(g_hwndLogBox);
if (g_hwndLogBox == NULL)
{
return 1;
}
/* TODO preserve existing selection */
/* Go to the end of the text */
nTextLength = GetWindowTextLength(g_hwndLogBox);
range.cpMin = nTextLength;
range.cpMax = nTextLength;
SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
/* Apply a formatting style */
memset(&format, 0, sizeof(format));
format.cbSize = sizeof(format);
format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT |
CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE | CFM_CHARSET;
format.bCharSet = DEFAULT_CHARSET;
format.yHeight = (g_nFontSize * 1440) / 72;
strlcpy(format.szFaceName, g_szFontFaceName, sizeof(format.szFaceName));
if (style == STYLE_NONE)
{
/* DO NOTHING */
format.dwEffects |= CFE_AUTOCOLOR;
}
else if (style == STYLE_HEADER)
{
format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
}
else if (style == STYLE_HIGHLIGHT)
{
format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
}
else if (style == STYLE_LINK)
{
format.dwEffects |= CFE_UNDERLINE;
format.crTextColor = RGB(0, 0, 255);
}
SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
/* Append text to the end */
SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
/* TODO Restore the old selection */
/* Purge buffer */
if (strchr(pszText, '\n') != NULL)
{
SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
if (!g_bClipPending)
{
/* Set the force clip timer going. This timer ensures clipping is done
intermittently even when there is a sustained burst of logging
*/
SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
}
g_bClipPending = TRUE;
}
return 1;
}
/*********************************************************************
*
* Function : LogShowActivity
*
* Description : Start the spinner.
*
* Parameters : None
*
* Returns : N/A
*
*********************************************************************/
void LogShowActivity(void)
{
/* Start some activity timers */
if (g_bShowActivityAnimation)
{
SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
}
}
/*********************************************************************
*
* Function : LogClipBuffer
*
* Description : Prunes old lines from the log.
*
* Parameters : None
*
* Returns : N/A
*
*********************************************************************/
void LogClipBuffer(void)
{
int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
{
/* Compute the range representing the lines to be deleted */
LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
CHARRANGE range;
range.cpMin = 0;
range.cpMax = nLastChar;
/* TODO get current selection */
/* TODO adjust and clip old selection against range to be deleted */
/* Select range and erase it (turning off autoscroll to prevent
nasty scrolling) */
SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
/* reposition (back to) the end of the log content */
range.cpMin = SendMessage (g_hwndLogBox, WM_GETTEXTLENGTH, 0, 0);
range.cpMax = -1;
SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
/* restore vertical ScrollBar stuff (messed up by AUTOVSCROLL) */
SendMessage (g_hwndLogBox, EM_SCROLL, SB_LINEDOWN, 0);
}
}
/*********************************************************************
*
* Function : CreateHiddenLogOwnerWindow
*
* Description : Creates a hidden owner window that stops the log
* window appearing in the task bar.
*
* Parameters :
* 1 : hInstance = application's instance handle
*
* Returns : Handle to newly created window.
*
*********************************************************************/
HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
{
static const char *szWndName = "PrivoxyLogOwner";
WNDCLASS wc;
HWND hwnd;
wc.style = 0;
wc.lpfnWndProc = LogOwnerWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = g_hiconApp;
wc.hCursor = 0;
wc.hbrBackground = 0;
wc.lpszMenuName = 0;
wc.lpszClassName = szWndName;
RegisterClass(&wc);
hwnd = CreateWindow(szWndName, szWndName,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
return hwnd;
}
/*********************************************************************
*
* Function : LogOwnerWindowProc
*
* Description : Dummy procedure that does nothing special.
*
* Parameters :
* 1 : hwnd = window handle
* 2 : uMsg = message number
* 3 : wParam = first param for this message
* 4 : lParam = next param for this message
*
* Returns : Same as `DefWindowProc'.
*
*********************************************************************/
LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
/*********************************************************************
*
* Function : CreateLogWindow
*
* Description : Create the logging window.
*
* Parameters :
* 1 : hInstance = application's instance handle
* 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
*
* Returns : Handle to newly created window.
*
*********************************************************************/
HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
{
static const char *szWndName = "PrivoxyLogWindow";
static const char *szWndTitle = "Privoxy";
HWND hwnd = NULL;
HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
RECT rcClient;
WNDCLASSEX wc;
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(wc);
wc.style = CS_DBLCLKS;
wc.lpfnWndProc = LogWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = g_hiconApp;
wc.hCursor = 0;
wc.hbrBackground = 0;
wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
wc.lpszClassName = szWndName;
wc.hbrBackground = GetStockObject(WHITE_BRUSH);
RegisterClassEx(&wc);
hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
/* Now create a child list box */
GetClientRect(hwnd, &rcClient);
/* Create a rich edit control */
InitRichEdit();
g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
hwnd, NULL, hInstance, NULL);
/* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
/* Subclass the control to catch certain messages */
g_fnLogBox = (WNDPROC) GetWindowLongPtr(g_hwndLogBox, GWLP_WNDPROC);
SetWindowLongPtr(g_hwndLogBox, GWLP_WNDPROC, (LONG_PTR) LogRichEditProc);
/* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
if (!g_bShowOnTaskBar &&
(nCmdShow == SW_SHOWMINIMIZED ||
nCmdShow == SW_MINIMIZE ||
nCmdShow == SW_SHOWMINNOACTIVE))
{
g_bShowLogWindow = FALSE;
nCmdShow = SW_HIDE;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
GetClientRect(g_hwndLogFrame, &rcClient);
SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
return hwnd;
}
/*********************************************************************
*
* Function : InitRichEdit
*
* Description : Initialise the rich edit control library.
*
* Parameters : None
*
* Returns : TRUE => success, FALSE => failure.
* FIXME: this is backwards to the rest of IJB and to common
* programming practice. Please use 0 => success instead.
*
*********************************************************************/
BOOL InitRichEdit(void)
{
static HINSTANCE hInstRichEdit;
if (hInstRichEdit == NULL)
{
g_nRichEditVersion = 0;
hInstRichEdit = LoadLibraryA("RICHED20.DLL");
if (hInstRichEdit)
{
g_nRichEditVersion = _RICHEDIT_VER;
}
else
{
hInstRichEdit = LoadLibraryA("RICHED32.DLL");
if (hInstRichEdit)
{
g_nRichEditVersion = 0x0100;
}
}
}
return (hInstRichEdit != NULL) ? TRUE : FALSE;
}
/*********************************************************************
*
* Function : ShowLogWindow
*
* Description : Shows or hides the log window. We will also raise the
* window on a show command in case it is buried.
*
* Parameters :
* 1 : bShow = TRUE to show, FALSE to mimize/hide
*
* Returns : N/A
*
*********************************************************************/
void ShowLogWindow(BOOL bShow)
{
if (bShow)
{
SetForegroundWindow(g_hwndLogFrame);
SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
}
else if (g_bShowOnTaskBar)
{
ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
}
else
{
ShowWindow(g_hwndLogFrame, SW_HIDE);
}
}
/*********************************************************************
*
* Function : EditFile
*
* Description : Opens the specified setting file for editing.
* FIXME: What if the file has no associated application. Check for return values
* from ShellExecute??
*
* Parameters :
* 1 : filename = filename from the config (aka config.txt) file.
*
* Returns : N/A
*
*********************************************************************/
void EditFile(const char *filename)
{
if (filename)
{
ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
}
}
/*--------------------------------------------------------------------------*/
/* Windows message handlers */
/*--------------------------------------------------------------------------*/
/*********************************************************************
*
* Function : OnLogRButtonUp
*
* Description : Handler for WM_RBUTTONUP messages.
*
* Parameters :
* 1 : nModifier = wParam from mouse message (unused)
* 2 : x = x coordinate of the mouse event
* 3 : y = y coordinate of the mouse event
*
* Returns : N/A
*
*********************************************************************/
void OnLogRButtonUp(int nModifier, int x, int y)
{
HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
if (hMenu != NULL)
{
HMENU hMenuPopup = GetSubMenu(hMenu, 0);
/* Check if there is a selection */
CHARRANGE range;
SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
if (range.cpMin == range.cpMax)
{
EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
}
else
{
EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
}
/* Display the popup */
TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
DestroyMenu(hMenu);
}
}
/*********************************************************************
*
* Function : OnLogCommand
*
* Description : Handler for WM_COMMAND messages.
*
* Parameters :
* 1 : nCommand = the command portion of the menu selection event
*
* Returns : N/A
*
*********************************************************************/
void OnLogCommand(int nCommand)
{
switch (nCommand)
{
case ID_TOGGLE_SHOWWINDOW:
g_bShowLogWindow = !g_bShowLogWindow;
ShowLogWindow(g_bShowLogWindow);
break;
case ID_FILE_EXIT:
PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
break;
case ID_EDIT_COPY:
SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
break;
case ID_VIEW_CLEARLOG:
SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
break;
case ID_VIEW_LOGMESSAGES:
g_bLogMessages = !g_bLogMessages;
/* SaveLogSettings(); */
break;
case ID_VIEW_MESSAGEHIGHLIGHTING:
g_bHighlightMessages = !g_bHighlightMessages;
/* SaveLogSettings(); */
break;
case ID_VIEW_LIMITBUFFERSIZE:
g_bLimitBufferSize = !g_bLimitBufferSize;
/* SaveLogSettings(); */
break;
case ID_VIEW_ACTIVITYANIMATION:
g_bShowActivityAnimation = !g_bShowActivityAnimation;
/* SaveLogSettings(); */
break;
#ifdef FEATURE_TOGGLE
case ID_TOGGLE_ENABLED:
global_toggle_state = !global_toggle_state;
log_error(LOG_LEVEL_INFO,
"Now toggled %s", global_toggle_state ? "ON" : "OFF");
/*
* Leverage TIMER_ANIMSTOP_ID to set the idle icon through the
* "application queue". According to MSDN, 10 milliseconds are
* the lowest value possible and seem to be close enough to
* "instantly".
*/
SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, 10, NULL);
break;
#endif /* def FEATURE_TOGGLE */
case ID_TOOLS_EDITCONFIG:
EditFile(configfile);
break;
case ID_TOOLS_EDITDEFAULTACTIONS:
EditFile(g_default_actions_file);
break;
case ID_TOOLS_EDITUSERACTIONS:
EditFile(g_user_actions_file);
break;
case ID_TOOLS_EDITDEFAULTFILTERS:
EditFile(g_default_filterfile);
break;
case ID_TOOLS_EDITUSERFILTERS:
EditFile(g_user_filterfile);
break;
#ifdef FEATURE_TRUST
case ID_TOOLS_EDITTRUST: