1 /*********************************************************************
2 *
3 * File : $Source: /cvsroot/ijbswa/current/w32log.c,v $
4 *
5 * Purpose : Functions for creating and destroying the log window,
6 * outputting strings, processing messages and so on.
7 *
8 * Copyright : Written by and Copyright (C) 2001-2009 members of
9 * the Privoxy team. https://www.privoxy.org/
10 *
11 * Written by and Copyright (C) 1999 Adam Lock
12 * <locka@iol.ie>
13 *
14 * This program is free software; you can redistribute it
15 * and/or modify it under the terms of the GNU General
16 * Public License as published by the Free Software
17 * Foundation; either version 2 of the License, or (at
18 * your option) any later version.
19 *
20 * This program is distributed in the hope that it will
21 * be useful, but WITHOUT ANY WARRANTY; without even the
22 * implied warranty of MERCHANTABILITY or FITNESS FOR A
23 * PARTICULAR PURPOSE. See the GNU General Public
24 * License for more details.
25 *
26 * The GNU General Public License should be included with
27 * this file. If not, you can view it at
28 * http://www.gnu.org/copyleft/gpl.html
29 * or write to the Free Software Foundation, Inc., 59
30 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 *
32 *********************************************************************/
33
34
35 #include "config.h"
36
37 #include <assert.h>
38 #include <stdio.h>
39
40 #ifndef STRICT
41 #define STRICT
42 #endif
43 #include <winsock2.h>
44 #include <windows.h>
45 #include <richedit.h>
46
47 #include "project.h"
48 #include "w32log.h"
49 #include "w32taskbar.h"
50 #include "win32.h"
51 #include "w32res.h"
52 #include "jcc.h"
53 #include "miscutil.h"
54 #include "errlog.h"
55 #include "loadcfg.h"
56
57 #ifndef _WIN_CONSOLE /* entire file */
58
59 /*
60 * Timers and the various durations
61 */
62 #define TIMER_ANIM_ID 1
63 #define TIMER_ANIM_TIME 100
64 #define TIMER_ANIMSTOP_ID 2
65 #define TIMER_ANIMSTOP_TIME 1000
66 #define TIMER_CLIPBUFFER_ID 3
67 #define TIMER_CLIPBUFFER_TIME 1000
68 #define TIMER_CLIPBUFFER_FORCE_ID 4
69 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
70
71 /*
72 * Styles of text that can be output
73 */
74 #define STYLE_NONE 0
75 #define STYLE_HIGHLIGHT 1
76 #define STYLE_LINK 2
77 #define STYLE_HEADER 3
78
79 /*
80 * Number of frames of animation in tray activity sequence
81 */
82 #define ANIM_FRAMES 8
83
84 #define DEFAULT_MAX_BUFFER_LINES 200
85 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
86 #define DEFAULT_LOG_FONT_SIZE 8
87
88 /*
89 * These values affect the way the log window behaves, they should be read
90 * from a file but for the moment, they are hardcoded here. Some options are
91 * configurable through the UI.
92 */
93
94 /* Indicates whether task bar shows activity animation */
95 BOOL g_bShowActivityAnimation = 1;
96
97 /* Indicates whether the log window is shown */
98 BOOL g_bShowLogWindow = 1;
99
100 /* Indicates if the log window appears on the task bar */
101 BOOL g_bShowOnTaskBar = 0;
102
103 /* Indicates whether closing the log window really just hides it */
104 BOOL g_bCloseHidesWindow = 1;
105
106 /* Indicates if messages are logged at all */
107 BOOL g_bLogMessages = 1;
108
109 /* Indicates whether log messages are highlighted */
110 BOOL g_bHighlightMessages = 1;
111
112 /* Indicates if buffer is limited in size */
113 BOOL g_bLimitBufferSize = 1;
114
115 /* Maximum number of lines allowed in buffer when limited */
116 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
117
118 /* Font to use */
119 char g_szFontFaceName[32] = DEFAULT_LOG_FONT_NAME;
120
121 /* Size of font to use */
122 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
123
124
125 /* FIXME: this is a kludge */
126
127 const char * g_default_actions_file = NULL;
128 const char * g_user_actions_file = NULL;
129 const char * g_default_filterfile = NULL;
130 const char * g_user_filterfile = NULL;
131 #ifdef FEATURE_TRUST
132 const char * g_trustfile = NULL;
133 #endif /* def FEATURE_TRUST */
134
135 /* FIXME: end kludge */
136
137 /* Regular expression for detected URLs */
138 #define RE_URL "http:[^ \n\r]*"
139
140 /*
141 * Regular expressions that are used to perform highlight in the log window
142 */
143 static struct _Pattern
144 {
145 const char *str;
146 int style;
147 regex_t buffer;
148 } patterns_to_highlight[] =
149 {
150 /* url headers */
151 { RE_URL, STYLE_LINK },
152 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
153 /* interesting text to highlight */
154 /* see jcc.c crunch_reason for the full list */
155 { "Crunch: Blocked:", STYLE_HIGHLIGHT },
156 { "Crunch: Untrusted", STYLE_HIGHLIGHT },
157 { "Crunch: Redirected:", STYLE_HIGHLIGHT },
158 { "Crunch: DNS failure", STYLE_HIGHLIGHT },
159 { "Crunch: Forwarding failed", STYLE_HIGHLIGHT },
160 { "Crunch: Connection failure", STYLE_HIGHLIGHT },
161 { "Crunch: Out of memory", STYLE_HIGHLIGHT },
162 { "Connect: Found reusable socket", STYLE_HIGHLIGHT },
163 { "Connect: Reusing server socket", STYLE_HIGHLIGHT },
164 { "Connect: Created new connection to", STYLE_HIGHLIGHT },
165 { "hung up on us", STYLE_HIGHLIGHT },
166 { "Info: Loading actions file:", STYLE_HIGHLIGHT },
167 { "Info: Loading filter file:", STYLE_HIGHLIGHT },
168 { "Info: Now toggled ", STYLE_HIGHLIGHT },
169 { "Crunching Referer:", STYLE_HIGHLIGHT },
170 /* what are all the possible error strings?? */
171 { "Error:", STYLE_HIGHLIGHT },
172 /* http headers */
173 { "referer:", STYLE_HEADER },
174 { "proxy-connection:", STYLE_HEADER },
175 { "proxy-agent:", STYLE_HEADER },
176 { "user-agent:", STYLE_HEADER },
177 { "host:", STYLE_HEADER },
178 { "accept:", STYLE_HEADER },
179 { "accept-encoding:", STYLE_HEADER },
180 { "accept-language:", STYLE_HEADER },
181 { "accept-charset:", STYLE_HEADER },
182 { "accept-ranges:", STYLE_HEADER },
183 { "date:", STYLE_HEADER },
184 { "cache-control:", STYLE_HEADER },
185 { "cache-last-checked:", STYLE_HEADER },
186 { "connection:", STYLE_HEADER },
187 { "content-type", STYLE_HEADER },
188 { "content-length", STYLE_HEADER },
189 { "cookie", STYLE_HEADER },
190 { "last-modified:", STYLE_HEADER },
191 { "pragma:", STYLE_HEADER },
192 { "server:", STYLE_HEADER },
193 { "etag:", STYLE_HEADER },
194 { "expires:", STYLE_HEADER },
195 { "warning:", STYLE_HEADER },
196 /* this is the terminator statement - do not delete! */
197 { NULL, STYLE_NONE }
198 };
199
200 /*
201 * Public variables
202 */
203 HWND g_hwndLogFrame;
204 HICON g_hiconApp;
205
206 /*
207 * Private variables
208 */
209 static CRITICAL_SECTION g_criticalsection;
210 static HWND g_hwndTray;
211 static HWND g_hwndLogBox;
212 static WNDPROC g_fnLogBox;
213 static HICON g_hiconAnim[ANIM_FRAMES];
214 static HICON g_hiconIdle;
215 static HICON g_hiconOff;
216 static int g_nAnimFrame;
217 static BOOL g_bClipPending = FALSE;
218 static int g_nRichEditVersion = 0;
219
220 /*
221 * Private functions
222 */
223 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
224 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
225 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
226 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
227 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
228 static BOOL InitRichEdit(void);
229 static void LogClipBuffer(void);
230 static void LogCreatePatternMatchingBuffers(void);
231 static void LogDestroyPatternMatchingBuffers(void);
232 static int LogPutStringNoMatch(const char *pszText, int style);
233 static void SetIdleIcon(void);
234
235
236 /*********************************************************************
237 *
238 * Function : InitLogWindow
239 *
240 * Description : Initialise the log window.
241 *
242 * Parameters : None
243 *
244 * Returns : Always TRUE (there should be error checking on the resources).
245 *
246 *********************************************************************/
InitLogWindow(void)247 BOOL InitLogWindow(void)
248 {
249 int i;
250
251 /* Load the icons */
252 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
253 g_hiconOff = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_OFF));
254 for (i = 0; i < ANIM_FRAMES; i++)
255 {
256 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
257 }
258 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
259
260 /* Create the user interface */
261 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
262 g_hwndTray = CreateTrayWindow(g_hInstance);
263 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
264
265 /* Create pattern matching buffers (for highlighting */
266 LogCreatePatternMatchingBuffers();
267
268 /* Create a critical section to protect multi-threaded access to certain things */
269 InitializeCriticalSection(&g_criticalsection);
270
271 return TRUE;
272
273 }
274
275
276 /*********************************************************************
277 *
278 * Function : TermLogWindow
279 *
280 * Description : Cleanup the logwindow.
281 *
282 * Parameters : None
283 *
284 * Returns : N/A
285 *
286 *********************************************************************/
TermLogWindow(void)287 void TermLogWindow(void)
288 {
289 int i;
290
291 LogDestroyPatternMatchingBuffers();
292
293 TrayDeleteIcon(g_hwndTray, 1);
294 DeleteObject(g_hiconApp);
295 DeleteObject(g_hiconIdle);
296 DeleteObject(g_hiconOff);
297 for (i = 0; i < ANIM_FRAMES; i++)
298 {
299 DeleteObject(g_hiconAnim[i]);
300 }
301
302 }
303
304
305 /*********************************************************************
306 *
307 * Function : LogCreatePatternMatchingBuffers
308 *
309 * Description : Compile the pattern matching buffers.
310 *
311 * Parameters : None
312 *
313 * Returns : N/A
314 *
315 *********************************************************************/
LogCreatePatternMatchingBuffers(void)316 void LogCreatePatternMatchingBuffers(void)
317 {
318 int i;
319 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
320 {
321 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
322 }
323 }
324
325
326 /*********************************************************************
327 *
328 * Function : LogDestroyPatternMatchingBuffers
329 *
330 * Description : Free up the pattern matching buffers.
331 *
332 * Parameters : None
333 *
334 * Returns : N/A
335 *
336 *********************************************************************/
LogDestroyPatternMatchingBuffers(void)337 void LogDestroyPatternMatchingBuffers(void)
338 {
339 int i;
340 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
341 {
342 regfree(&patterns_to_highlight[i].buffer);
343 }
344 }
345
346
347 /*********************************************************************
348 *
349 * Function : LogPutString
350 *
351 * Description : Inserts text into the logging window. This is really
352 * a regexp aware wrapper function to `LogPutStringNoMatch'.
353 *
354 * Parameters :
355 * 1 : pszText = pointer to string going to the log window
356 *
357 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
358 * FIXME: this is backwards to the rest of IJB and to common
359 * programming practice. Please use 0 => success instead.
360 *
361 *********************************************************************/
LogPutString(const char * pszText)362 int LogPutString(const char *pszText)
363 {
364 int i;
365 int result = 0;
366
367 if (!g_bLogMessages)
368 {
369 return 1;
370 }
371
372 if (pszText == NULL || strlen(pszText) == 0)
373 {
374 return 1;
375 }
376
377 /* Critical section stops multiple threads doing nasty interactions that
378 * foul up the highlighting and output.
379 */
380 EnterCriticalSection(&g_criticalsection);
381
382 if (g_bHighlightMessages)
383 {
384 regmatch_t match;
385
386 /* First things first, regexp scan for various things that we would like highlighted */
387 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
388 {
389 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
390 {
391 char *pszBefore = NULL;
392 char *pszMatch = NULL;
393 char *pszAfter = NULL;
394 int nMatchSize;
395
396 /* Split the string up into pieces representing the strings, before
397 at and after the matching pattern
398 */
399 if (match.rm_so > 0)
400 {
401 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
402 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
403 strncpy(pszBefore, pszText, match.rm_so);
404 }
405 if (match.rm_eo < (regoff_t)strlen(pszText))
406 {
407 pszAfter = strdup(&pszText[match.rm_eo]);
408 }
409 nMatchSize = match.rm_eo - match.rm_so;
410 pszMatch = (char *)malloc(nMatchSize + 1);
411 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
412 pszMatch[nMatchSize] = '\0';
413
414 /* Recursively call LogPutString */
415 if (pszBefore)
416 {
417 LogPutString(pszBefore);
418 free(pszBefore);
419 }
420 if (pszMatch)
421 {
422 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
423 free(pszMatch);
424 }
425 if (pszAfter)
426 {
427 LogPutString(pszAfter);
428 free(pszAfter);
429 }
430
431 result = 1;
432 goto end;
433 }
434 }
435 }
436
437 result = LogPutStringNoMatch(pszText, STYLE_NONE);
438
439 end:
440 LeaveCriticalSection(&g_criticalsection);
441
442 return result;
443
444 }
445
446
447 /*********************************************************************
448 *
449 * Function : LogPutStringNoMatch
450 *
451 * Description : Puts a string into the logging window.
452 *
453 * Parameters :
454 * 1 : pszText = pointer to string going to the log window
455 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
456 *
457 * Returns : Always 1 => success.
458 * FIXME: this is backwards to the rest of IJB and to common
459 * programming practice. Please use 0 => success instead.
460 *
461 *********************************************************************/
LogPutStringNoMatch(const char * pszText,int style)462 int LogPutStringNoMatch(const char *pszText, int style)
463 {
464 CHARRANGE range;
465 CHARFORMAT format;
466 int nTextLength;
467
468 assert(g_hwndLogBox);
469 if (g_hwndLogBox == NULL)
470 {
471 return 1;
472 }
473
474 /* TODO preserve existing selection */
475
476 /* Go to the end of the text */
477 nTextLength = GetWindowTextLength(g_hwndLogBox);
478 range.cpMin = nTextLength;
479 range.cpMax = nTextLength;
480 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
481
482 /* Apply a formatting style */
483 memset(&format, 0, sizeof(format));
484 format.cbSize = sizeof(format);
485 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT |
486 CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE | CFM_CHARSET;
487 format.bCharSet = DEFAULT_CHARSET;
488 format.yHeight = (g_nFontSize * 1440) / 72;
489 strlcpy(format.szFaceName, g_szFontFaceName, sizeof(format.szFaceName));
490 if (style == STYLE_NONE)
491 {
492 /* DO NOTHING */
493 format.dwEffects |= CFE_AUTOCOLOR;
494 }
495 else if (style == STYLE_HEADER)
496 {
497 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
498 }
499 else if (style == STYLE_HIGHLIGHT)
500 {
501 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
502 }
503 else if (style == STYLE_LINK)
504 {
505 format.dwEffects |= CFE_UNDERLINE;
506 format.crTextColor = RGB(0, 0, 255);
507 }
508 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
509
510 /* Append text to the end */
511 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
512
513 /* TODO Restore the old selection */
514
515 /* Purge buffer */
516 if (strchr(pszText, '\n') != NULL)
517 {
518 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
519 if (!g_bClipPending)
520 {
521 /* Set the force clip timer going. This timer ensures clipping is done
522 intermittently even when there is a sustained burst of logging
523 */
524 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
525 }
526 g_bClipPending = TRUE;
527 }
528
529 return 1;
530
531 }
532
533
534 /*********************************************************************
535 *
536 * Function : LogShowActivity
537 *
538 * Description : Start the spinner.
539 *
540 * Parameters : None
541 *
542 * Returns : N/A
543 *
544 *********************************************************************/
LogShowActivity(void)545 void LogShowActivity(void)
546 {
547 /* Start some activity timers */
548 if (g_bShowActivityAnimation)
549 {
550 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
551 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
552 }
553
554 }
555
556
557 /*********************************************************************
558 *
559 * Function : LogClipBuffer
560 *
561 * Description : Prunes old lines from the log.
562 *
563 * Parameters : None
564 *
565 * Returns : N/A
566 *
567 *********************************************************************/
LogClipBuffer(void)568 void LogClipBuffer(void)
569 {
570 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
571 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
572 {
573 /* Compute the range representing the lines to be deleted */
574 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
575 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
576 CHARRANGE range;
577 range.cpMin = 0;
578 range.cpMax = nLastChar;
579
580 /* TODO get current selection */
581
582 /* TODO adjust and clip old selection against range to be deleted */
583
584 /* Select range and erase it (turning off autoscroll to prevent
585 nasty scrolling) */
586 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
587 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
588 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
589 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
590
591 /* reposition (back to) the end of the log content */
592 range.cpMin = SendMessage (g_hwndLogBox, WM_GETTEXTLENGTH, 0, 0);
593 range.cpMax = -1;
594 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
595
596 /* restore vertical ScrollBar stuff (messed up by AUTOVSCROLL) */
597 SendMessage (g_hwndLogBox, EM_SCROLL, SB_LINEDOWN, 0);
598
599 }
600
601 }
602
603
604 /*********************************************************************
605 *
606 * Function : CreateHiddenLogOwnerWindow
607 *
608 * Description : Creates a hidden owner window that stops the log
609 * window appearing in the task bar.
610 *
611 * Parameters :
612 * 1 : hInstance = application's instance handle
613 *
614 * Returns : Handle to newly created window.
615 *
616 *********************************************************************/
CreateHiddenLogOwnerWindow(HINSTANCE hInstance)617 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
618 {
619 static const char *szWndName = "PrivoxyLogOwner";
620 WNDCLASS wc;
621 HWND hwnd;
622
623 wc.style = 0;
624 wc.lpfnWndProc = LogOwnerWindowProc;
625 wc.cbClsExtra = 0;
626 wc.cbWndExtra = 0;
627 wc.hInstance = hInstance;
628 wc.hIcon = g_hiconApp;
629 wc.hCursor = 0;
630 wc.hbrBackground = 0;
631 wc.lpszMenuName = 0;
632 wc.lpszClassName = szWndName;
633
634 RegisterClass(&wc);
635
636 hwnd = CreateWindow(szWndName, szWndName,
637 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
638 CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
639
640 return hwnd;
641
642 }
643
644
645 /*********************************************************************
646 *
647 * Function : LogOwnerWindowProc
648 *
649 * Description : Dummy procedure that does nothing special.
650 *
651 * Parameters :
652 * 1 : hwnd = window handle
653 * 2 : uMsg = message number
654 * 3 : wParam = first param for this message
655 * 4 : lParam = next param for this message
656 *
657 * Returns : Same as `DefWindowProc'.
658 *
659 *********************************************************************/
LogOwnerWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)660 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
661 {
662 return DefWindowProc(hwnd, uMsg, wParam, lParam);
663
664 }
665
666
667 /*********************************************************************
668 *
669 * Function : CreateLogWindow
670 *
671 * Description : Create the logging window.
672 *
673 * Parameters :
674 * 1 : hInstance = application's instance handle
675 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
676 *
677 * Returns : Handle to newly created window.
678 *
679 *********************************************************************/
CreateLogWindow(HINSTANCE hInstance,int nCmdShow)680 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
681 {
682 static const char *szWndName = "PrivoxyLogWindow";
683 static const char *szWndTitle = "Privoxy";
684
685 HWND hwnd = NULL;
686 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
687 RECT rcClient;
688 WNDCLASSEX wc;
689
690 memset(&wc, 0, sizeof(wc));
691 wc.cbSize = sizeof(wc);
692 wc.style = CS_DBLCLKS;
693 wc.lpfnWndProc = LogWindowProc;
694 wc.cbClsExtra = 0;
695 wc.cbWndExtra = 0;
696 wc.hInstance = hInstance;
697 wc.hIcon = g_hiconApp;
698 wc.hCursor = 0;
699 wc.hbrBackground = 0;
700 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
701 wc.lpszClassName = szWndName;
702 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
703 RegisterClassEx(&wc);
704
705 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
706 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
707 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
708
709 /* Now create a child list box */
710 GetClientRect(hwnd, &rcClient);
711
712 /* Create a rich edit control */
713 InitRichEdit();
714 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
715 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
716 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
717 hwnd, NULL, hInstance, NULL);
718 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
719
720 /* Subclass the control to catch certain messages */
721 g_fnLogBox = (WNDPROC) GetWindowLongPtr(g_hwndLogBox, GWLP_WNDPROC);
722 SetWindowLongPtr(g_hwndLogBox, GWLP_WNDPROC, (LONG_PTR) LogRichEditProc);
723
724 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
725 if (!g_bShowOnTaskBar &&
726 (nCmdShow == SW_SHOWMINIMIZED ||
727 nCmdShow == SW_MINIMIZE ||
728 nCmdShow == SW_SHOWMINNOACTIVE))
729 {
730 g_bShowLogWindow = FALSE;
731 nCmdShow = SW_HIDE;
732 }
733
734 ShowWindow(hwnd, nCmdShow);
735 UpdateWindow(hwnd);
736
737
738 GetClientRect(g_hwndLogFrame, &rcClient);
739 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
740
741 return hwnd;
742
743 }
744
745
746 /*********************************************************************
747 *
748 * Function : InitRichEdit
749 *
750 * Description : Initialise the rich edit control library.
751 *
752 * Parameters : None
753 *
754 * Returns : TRUE => success, FALSE => failure.
755 * FIXME: this is backwards to the rest of IJB and to common
756 * programming practice. Please use 0 => success instead.
757 *
758 *********************************************************************/
InitRichEdit(void)759 BOOL InitRichEdit(void)
760 {
761 static HINSTANCE hInstRichEdit;
762 if (hInstRichEdit == NULL)
763 {
764 g_nRichEditVersion = 0;
765 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
766 if (hInstRichEdit)
767 {
768 g_nRichEditVersion = _RICHEDIT_VER;
769 }
770 else
771 {
772 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
773 if (hInstRichEdit)
774 {
775 g_nRichEditVersion = 0x0100;
776 }
777 }
778 }
779 return (hInstRichEdit != NULL) ? TRUE : FALSE;
780
781 }
782
783
784 /*********************************************************************
785 *
786 * Function : ShowLogWindow
787 *
788 * Description : Shows or hides the log window. We will also raise the
789 * window on a show command in case it is buried.
790 *
791 * Parameters :
792 * 1 : bShow = TRUE to show, FALSE to mimize/hide
793 *
794 * Returns : N/A
795 *
796 *********************************************************************/
ShowLogWindow(BOOL bShow)797 void ShowLogWindow(BOOL bShow)
798 {
799 if (bShow)
800 {
801 SetForegroundWindow(g_hwndLogFrame);
802 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
803
804 }
805 else if (g_bShowOnTaskBar)
806 {
807 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
808 }
809 else
810 {
811 ShowWindow(g_hwndLogFrame, SW_HIDE);
812 }
813 }
814
815
816 /*********************************************************************
817 *
818 * Function : EditFile
819 *
820 * Description : Opens the specified setting file for editing.
821 * FIXME: What if the file has no associated application. Check for return values
822 * from ShellExecute??
823 *
824 * Parameters :
825 * 1 : filename = filename from the config (aka config.txt) file.
826 *
827 * Returns : N/A
828 *
829 *********************************************************************/
EditFile(const char * filename)830 void EditFile(const char *filename)
831 {
832 if (filename)
833 {
834 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
835 }
836
837 }
838
839
840 /*--------------------------------------------------------------------------*/
841 /* Windows message handlers */
842 /*--------------------------------------------------------------------------*/
843
844
845 /*********************************************************************
846 *
847 * Function : OnLogRButtonUp
848 *
849 * Description : Handler for WM_RBUTTONUP messages.
850 *
851 * Parameters :
852 * 1 : nModifier = wParam from mouse message (unused)
853 * 2 : x = x coordinate of the mouse event
854 * 3 : y = y coordinate of the mouse event
855 *
856 * Returns : N/A
857 *
858 *********************************************************************/
OnLogRButtonUp(int nModifier,int x,int y)859 void OnLogRButtonUp(int nModifier, int x, int y)
860 {
861 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
862 if (hMenu != NULL)
863 {
864 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
865
866 /* Check if there is a selection */
867 CHARRANGE range;
868 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
869 if (range.cpMin == range.cpMax)
870 {
871 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
872 }
873 else
874 {
875 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
876 }
877
878 /* Display the popup */
879 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
880 DestroyMenu(hMenu);
881 }
882
883 }
884
885
886 /*********************************************************************
887 *
888 * Function : OnLogCommand
889 *
890 * Description : Handler for WM_COMMAND messages.
891 *
892 * Parameters :
893 * 1 : nCommand = the command portion of the menu selection event
894 *
895 * Returns : N/A
896 *
897 *********************************************************************/
OnLogCommand(int nCommand)898 void OnLogCommand(int nCommand)
899 {
900 switch (nCommand)
901 {
902 case ID_TOGGLE_SHOWWINDOW:
903 g_bShowLogWindow = !g_bShowLogWindow;
904
905 ShowLogWindow(g_bShowLogWindow);
906 break;
907
908 case ID_FILE_EXIT:
909 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
910 break;
911
912 case ID_EDIT_COPY:
913 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
914 break;
915
916 case ID_VIEW_CLEARLOG:
917 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
918 break;
919
920 case ID_VIEW_LOGMESSAGES:
921 g_bLogMessages = !g_bLogMessages;
922 /* SaveLogSettings(); */
923 break;
924
925 case ID_VIEW_MESSAGEHIGHLIGHTING:
926 g_bHighlightMessages = !g_bHighlightMessages;
927 /* SaveLogSettings(); */
928 break;
929
930 case ID_VIEW_LIMITBUFFERSIZE:
931 g_bLimitBufferSize = !g_bLimitBufferSize;
932 /* SaveLogSettings(); */
933 break;
934
935 case ID_VIEW_ACTIVITYANIMATION:
936 g_bShowActivityAnimation = !g_bShowActivityAnimation;
937 /* SaveLogSettings(); */
938 break;
939
940 #ifdef FEATURE_TOGGLE
941 case ID_TOGGLE_ENABLED:
942 global_toggle_state = !global_toggle_state;
943 log_error(LOG_LEVEL_INFO,
944 "Now toggled %s", global_toggle_state ? "ON" : "OFF");
945 /*
946 * Leverage TIMER_ANIMSTOP_ID to set the idle icon through the
947 * "application queue". According to MSDN, 10 milliseconds are
948 * the lowest value possible and seem to be close enough to
949 * "instantly".
950 */
951 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, 10, NULL);
952 break;
953 #endif /* def FEATURE_TOGGLE */
954
955 case ID_TOOLS_EDITCONFIG:
956 EditFile(configfile);
957 break;
958
959 case ID_TOOLS_EDITDEFAULTACTIONS:
960 EditFile(g_default_actions_file);
961 break;
962
963 case ID_TOOLS_EDITUSERACTIONS:
964 EditFile(g_user_actions_file);
965 break;
966
967 case ID_TOOLS_EDITDEFAULTFILTERS:
968 EditFile(g_default_filterfile);
969 break;
970
971 case ID_TOOLS_EDITUSERFILTERS:
972 EditFile(g_user_filterfile);
973 break;
974
975 #ifdef FEATURE_TRUST
976 case ID_TOOLS_EDITTRUST:
977 EditFile(g_trustfile);
978 break;
979 #endif /* def FEATURE_TRUST */
980
981 case ID_HELP_GPL:
982 ShellExecute(g_hwndLogFrame, "open", "LICENSE.txt", NULL, NULL, SW_SHOWNORMAL);
983 break;
984
985 case ID_HELP_FAQ:
986 ShellExecute(g_hwndLogFrame, "open", "doc\\faq\\index.html", NULL, NULL, SW_SHOWNORMAL);
987 break;
988
989 case ID_HELP_MANUAL:
990 ShellExecute(g_hwndLogFrame, "open", "doc\\user-manual\\index.html", NULL, NULL, SW_SHOWNORMAL);
991 break;
992
993 case ID_HELP_STATUS:
994 ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
995 break;
996
997 case ID_HELP_ABOUT:
998 MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
999 break;
1000
1001 default:
1002 /* DO NOTHING */
1003 break;
1004 }
1005
1006 }
1007
1008
1009 /*********************************************************************
1010 *
1011 * Function : OnLogInitMenu
1012 *
1013 * Description : Handler for WM_INITMENU messages. Enable, disable,
1014 * check, and/or uncheck menu options as apropos.
1015 *
1016 * Parameters :
1017 * 1 : hmenu = handle to menu to "make current"
1018 *
1019 * Returns : N/A
1020 *
1021 *********************************************************************/
OnLogInitMenu(HMENU hmenu)1022 void OnLogInitMenu(HMENU hmenu)
1023 {
1024 /* Only enable editors if there is a file to edit */
1025 EnableMenuItem(hmenu, ID_TOOLS_EDITDEFAULTACTIONS, MF_BYCOMMAND | (g_default_actions_file ? MF_ENABLED : MF_GRAYED));
1026 EnableMenuItem(hmenu, ID_TOOLS_EDITUSERACTIONS, MF_BYCOMMAND | (g_user_actions_file ? MF_ENABLED : MF_GRAYED));
1027 EnableMenuItem(hmenu, ID_TOOLS_EDITDEFAULTFILTERS, MF_BYCOMMAND | (g_default_filterfile ? MF_ENABLED : MF_GRAYED));
1028 EnableMenuItem(hmenu, ID_TOOLS_EDITUSERFILTERS, MF_BYCOMMAND | (g_user_filterfile ? MF_ENABLED : MF_GRAYED));
1029 #ifdef FEATURE_TRUST
1030 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1031 #endif /* def FEATURE_TRUST */
1032
1033 /* Check/uncheck options */
1034 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1035 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1036 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1037 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1038 #ifdef FEATURE_TOGGLE
1039 /* by haroon - menu item for Enable toggle on/off */
1040 CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (global_toggle_state ? MF_CHECKED : MF_UNCHECKED));
1041 #endif /* def FEATURE_TOGGLE */
1042 CheckMenuItem(hmenu, ID_TOGGLE_SHOWWINDOW, MF_BYCOMMAND | (g_bShowLogWindow ? MF_CHECKED : MF_UNCHECKED));
1043
1044 }
1045
1046
1047 /*********************************************************************
1048 *
1049 * Function : OnLogTimer
1050 *
1051 * Description : Handler for WM_TIMER messages.
1052 *
1053 * Parameters :
1054 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1055 *
1056 * Returns : N/A
1057 *
1058 *********************************************************************/
OnLogTimer(int nTimer)1059 void OnLogTimer(int nTimer)
1060 {
1061 switch (nTimer)
1062 {
1063 case TIMER_ANIM_ID:
1064 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1065 break;
1066
1067 case TIMER_ANIMSTOP_ID:
1068 g_nAnimFrame = 0;
1069 SetIdleIcon();
1070 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1071 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1072 break;
1073
1074 case TIMER_CLIPBUFFER_ID:
1075 case TIMER_CLIPBUFFER_FORCE_ID:
1076 LogClipBuffer();
1077 g_bClipPending = FALSE;
1078 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1079 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1080 break;
1081
1082 default:
1083 /* DO NOTHING */
1084 break;
1085 }
1086
1087 }
1088
1089
1090 /*********************************************************************
1091 *
1092 * Function : SetIdleIcon
1093 *
1094 * Description : Sets the tray icon to either idle or off
1095 *
1096 * Parameters : none
1097 *
1098 * Returns : N/A
1099 *
1100 *********************************************************************/
SetIdleIcon()1101 void SetIdleIcon()
1102 {
1103 #ifdef FEATURE_TOGGLE
1104 if (!global_toggle_state)
1105 {
1106 TraySetIcon(g_hwndTray, 1, g_hiconOff);
1107 }
1108 else
1109 #endif /* def FEATURE_TOGGLE */
1110 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1111 }
1112
1113
1114 /*********************************************************************
1115 *
1116 * Function : LogRichEditProc
1117 *
1118 * Description : Window subclass routine handles some events for the rich edit control.
1119 *
1120 * Parameters :
1121 * 1 : hwnd = window handle of the rich edit control
1122 * 2 : uMsg = message number
1123 * 3 : wParam = first param for this message
1124 * 4 : lParam = next param for this message
1125 *
1126 * Returns : Appropriate M$ window message handler codes.
1127 *
1128 *********************************************************************/
LogRichEditProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)1129 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1130 {
1131 switch (uMsg)
1132 {
1133 case WM_RBUTTONUP:
1134 {
1135 POINT pt;
1136 pt.x = LOWORD(lParam);
1137 pt.y = HIWORD(lParam);
1138 ClientToScreen(hwnd, &pt);
1139 OnLogRButtonUp(wParam, pt.x, pt.y);
1140 return 0;
1141 }
1142 case WM_CHAR:
1143 {
1144 if ((GetKeyState(VK_CONTROL) != 0) && (wParam == 4)) /* ctrl+d */
1145 {
1146 OnLogCommand(ID_VIEW_CLEARLOG);
1147 return 0;
1148 }
1149 }
1150 }
1151 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1152
1153 }
1154
1155
1156 /*********************************************************************
1157 *
1158 * Function : LogWindowProc
1159 *
1160 * Description : Windows call back routine handles events on the log window.
1161 *
1162 * Parameters :
1163 * 1 : hwnd = handle of the logging window
1164 * 2 : uMsg = message number
1165 * 3 : wParam = first param for this message
1166 * 4 : lParam = next param for this message
1167 *
1168 * Returns : Appropriate M$ window message handler codes.
1169 *
1170 *********************************************************************/
LogWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)1171 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1172 {
1173 switch (uMsg)
1174 {
1175 case WM_CREATE:
1176 return 0;
1177
1178 case WM_CLOSE:
1179 /* This is the end - my only friend - the end */
1180 DestroyWindow(g_hwndLogBox);
1181 DestroyWindow(g_hwndLogFrame);
1182 return 0;
1183
1184 case WM_DESTROY:
1185 PostQuitMessage(0);
1186 return 0;
1187
1188 case WM_SHOWWINDOW:
1189 g_bShowLogWindow = wParam;
1190 case WM_SIZE: /* note: implicit-fallthrough */
1191 /* Resize the logging window to fit the new frame */
1192 if (g_hwndLogBox)
1193 {
1194 RECT rc;
1195 GetClientRect(g_hwndLogFrame, &rc);
1196 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1197 }
1198 return 0;
1199
1200 case WM_INITMENU:
1201 OnLogInitMenu((HMENU) wParam);
1202 return 0;
1203
1204 case WM_TIMER:
1205 OnLogTimer(wParam);
1206 return 0;
1207
1208 case WM_COMMAND:
1209 OnLogCommand(LOWORD(wParam));
1210 return 0;
1211
1212 case WM_SYSCOMMAND:
1213 switch (wParam)
1214 {
1215 case SC_CLOSE:
1216 if (g_bCloseHidesWindow)
1217 {
1218 ShowLogWindow(FALSE);
1219 return 0;
1220 }
1221 break;
1222 case SC_MINIMIZE:
1223 ShowLogWindow(FALSE);
1224 return 0;
1225 }
1226 break;
1227
1228 case WM_CHAR:
1229 if ((GetKeyState(VK_CONTROL) != 0) && (wParam == 4)) /* ctrl+d */
1230 {
1231 OnLogCommand(ID_VIEW_CLEARLOG);
1232 return 0;
1233 }
1234 break;
1235 }
1236
1237 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1238
1239 }
1240
1241 #endif /* ndef _WIN_CONSOLE - entire file */
1242
1243 /*
1244 Local Variables:
1245 tab-width: 3
1246 end:
1247 */
1248