1 const char w32log_rcs[] = "$Id: w32log.c,v 1.10 2001/05/31 21:37:11 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/w32log.c,v $
6 * Purpose : Functions for creating and destroying the log window,
7 * ouputting strings, processing messages and so on.
9 * Copyright : Written by and Copyright (C) 2001 the SourceForge
10 * IJBSWA team. http://ijbswa.sourceforge.net
12 * Written by and Copyright (C) 1999 Adam Lock
15 * This program is free software; you can redistribute it
16 * and/or modify it under the terms of the GNU General
17 * Public License as published by the Free Software
18 * Foundation; either version 2 of the License, or (at
19 * your option) any later version.
21 * This program is distributed in the hope that it will
22 * be useful, but WITHOUT ANY WARRANTY; without even the
23 * implied warranty of MERCHANTABILITY or FITNESS FOR A
24 * PARTICULAR PURPOSE. See the GNU General Public
25 * License for more details.
27 * The GNU General Public License should be included with
28 * this file. If not, you can view it at
29 * http://www.gnu.org/copyleft/gpl.html
30 * or write to the Free Software Foundation, Inc., 59
31 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 * Revision 1.10 2001/05/31 21:37:11 jongfoster
36 * GUI changes to rename "permissions file" to "actions file".
38 * Revision 1.9 2001/05/31 17:33:13 oes
42 * Revision 1.8 2001/05/29 09:50:24 jongfoster
43 * Unified blocklist/imagelist/permissionslist.
44 * File format is still under discussion, but the internal changes
47 * Also modified interceptor behaviour:
48 * - We now intercept all URLs beginning with one of the following
49 * prefixes (and *only* these prefixes):
51 * * http://ijbswa.sf.net/config/
52 * * http://ijbswa.sourceforge.net/config/
53 * - New interceptors "home page" - go to http://i.j.b/ to see it.
54 * - Internal changes so that intercepted and fast redirect pages
55 * are not replaced with an image.
56 * - Interceptors now have the option to send a binary page direct
57 * to the client. (i.e. ijb-send-banner uses this)
58 * - Implemented show-url-info interceptor. (Which is why I needed
59 * the above interceptors changes - a typical URL is
60 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
61 * The previous mechanism would not have intercepted that, and
62 * if it had been intercepted then it then it would have replaced
65 * Revision 1.7 2001/05/26 01:26:34 jongfoster
66 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
67 * This #define cannot be set from ./configure - there's no point, it
68 * doesn't work yet. See feature request # 425722
70 * Revision 1.6 2001/05/26 00:31:30 jongfoster
71 * Fixing compiler warning about comparing signed/unsigned.
73 * Revision 1.5 2001/05/26 00:28:36 jongfoster
74 * Automatic reloading of config file.
75 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
76 * Most of the global variables have been moved to a new
77 * struct configuration_spec, accessed through csp->config->globalname
78 * Most of the globals remaining are used by the Win32 GUI.
80 * Revision 1.4 2001/05/22 18:56:28 oes
83 * Revision 1.3 2001/05/20 15:07:54 jongfoster
84 * File is now ignored if _WIN_CONSOLE is defined.
86 * Revision 1.2 2001/05/20 01:21:20 jongfoster
87 * Version 2.9.4 checkin.
88 * - Merged popupfile and cookiefile, and added control over PCRS
89 * filtering, in new "permissionsfile".
90 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
91 * file error you now get a message box (in the Win32 GUI) rather
92 * than the program exiting with no explanation.
93 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
95 * - Removed tabs from "config"
96 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
97 * - Bumped up version number.
99 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
100 * Initial import of version 2.9.3 source tree
103 *********************************************************************/
112 #include <richedit.h>
116 #include "w32taskbar.h"
117 #include "w32rulesdlg.h"
121 #include "miscutil.h"
124 const char w32res_h_rcs[] = W32RES_H_VERSION;
128 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
131 const char w32log_h_rcs[] = W32LOG_H_VERSION;
133 #ifndef _WIN_CONSOLE /* entire file */
136 * Timers and the various durations
138 #define TIMER_ANIM_ID 1
139 #define TIMER_ANIM_TIME 100
140 #define TIMER_ANIMSTOP_ID 2
141 #define TIMER_ANIMSTOP_TIME 1000
142 #define TIMER_CLIPBUFFER_ID 3
143 #define TIMER_CLIPBUFFER_TIME 1000
144 #define TIMER_CLIPBUFFER_FORCE_ID 4
145 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
148 * Styles of text that can be output
151 #define STYLE_HIGHLIGHT 1
153 #define STYLE_HEADER 3
156 * Number of frames of animation in tray activity sequence
158 #define ANIM_FRAMES 8
160 #define DEFAULT_MAX_BUFFER_LINES 200
161 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
162 #define DEFAULT_LOG_FONT_SIZE 8
165 * These values affect the way the log window behaves, they should be read
166 * from a file but for the moment, they are hardcoded here. Some options are
167 * configurable through the UI.
170 /* Indicates whether task bar shows activity animation */
171 BOOL g_bShowActivityAnimation = 1;
173 /* Indicates if the log window appears on the task bar */
174 BOOL g_bShowOnTaskBar = 0;
176 /* Indicates whether closing the log window really just hides it */
177 BOOL g_bCloseHidesWindow = 1;
179 /* Indicates if messages are logged at all */
180 BOOL g_bLogMessages = 1;
182 /* Indicates whether log messages are highlighted */
183 BOOL g_bHighlightMessages = 1;
185 /* Indicates if buffer is limited in size */
186 BOOL g_bLimitBufferSize = 1;
188 /* Maximum number of lines allowed in buffer when limited */
189 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
192 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
194 /* Size of font to use */
195 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
198 /* FIXME: this is a kludge */
200 const char * g_actions_file = NULL;
202 const char * g_re_filterfile = NULL;
205 const char * g_trustfile = NULL;
206 #endif /* def TRUST_FILES */
208 /* FIXME: end kludge */
212 /* Regular expression for detected URLs */
213 #define RE_URL "http:[^ \n\r]*"
216 * Regular expressions that are used to perform highlight in the log window
218 static struct _Pattern
223 } patterns_to_highlight[] =
226 { RE_URL, STYLE_LINK },
227 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
228 /* interesting text to highlight */
229 { "crunch!", STYLE_HIGHLIGHT },
231 { "referer:", STYLE_HEADER },
232 { "proxy-connection:", STYLE_HEADER },
233 { "proxy-agent:", STYLE_HEADER },
234 { "user-agent:", STYLE_HEADER },
235 { "host:", STYLE_HEADER },
236 { "accept:", STYLE_HEADER },
237 { "accept-encoding:", STYLE_HEADER },
238 { "accept-language:", STYLE_HEADER },
239 { "accept-charset:", STYLE_HEADER },
240 { "accept-ranges:", STYLE_HEADER },
241 { "date:", STYLE_HEADER },
242 { "cache-control:", STYLE_HEADER },
243 { "cache-last-checked:", STYLE_HEADER },
244 { "connection:", STYLE_HEADER },
245 { "content-type", STYLE_HEADER },
246 { "content-length", STYLE_HEADER },
247 { "cookie", STYLE_HEADER },
248 { "last-modified:", STYLE_HEADER },
249 { "pragma:", STYLE_HEADER },
250 { "server:", STYLE_HEADER },
251 { "etag:", STYLE_HEADER },
252 { "expires:", STYLE_HEADER },
253 { "warning:", STYLE_HEADER },
254 /* this is the terminator statement - do not delete! */
257 #endif /* def REGEX */
268 static CRITICAL_SECTION g_criticalsection;
269 static HWND g_hwndTray;
270 static HWND g_hwndLogBox;
271 static WNDPROC g_fnLogBox;
272 static HICON g_hiconAnim[ANIM_FRAMES];
273 static HICON g_hiconIdle;
274 static HICON g_hiconApp;
275 static int g_nAnimFrame;
276 static BOOL g_bClipPending = FALSE;
277 static int g_nRichEditVersion = 0;
282 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
283 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
284 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
285 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
286 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
287 static BOOL InitRichEdit(void);
288 static void LogClipBuffer(void);
289 static void LogCreatePatternMatchingBuffers(void);
290 static void LogDestroyPatternMatchingBuffers(void);
291 static int LogPutStringNoMatch(const char *pszText, int style);
294 /*********************************************************************
296 * Function : InitLogWindow
298 * Description : Initialise the log window.
302 * Returns : Always TRUE (there should be error checking on the resources).
304 *********************************************************************/
305 BOOL InitLogWindow(void)
310 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
311 for (i = 0; i < ANIM_FRAMES; i++)
313 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER1 + i));
315 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER));
317 /* Create the user interface */
318 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
319 g_hwndTray = CreateTrayWindow(g_hInstance);
320 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Junkbuster");
322 /* Create pattern matching buffers (for highlighting */
323 LogCreatePatternMatchingBuffers();
325 /* Create a critical section to protect multi-threaded access to certain things */
326 InitializeCriticalSection(&g_criticalsection);
333 /*********************************************************************
335 * Function : TermLogWindow
337 * Description : Cleanup the logwindow.
343 *********************************************************************/
344 void TermLogWindow(void)
348 LogDestroyPatternMatchingBuffers();
350 TrayDeleteIcon(g_hwndTray, 1);
351 DeleteObject(g_hiconApp);
352 DeleteObject(g_hiconIdle);
353 for (i = 0; i < ANIM_FRAMES; i++)
355 DeleteObject(g_hiconAnim[i]);
361 /*********************************************************************
363 * Function : LogCreatePatternMatchingBuffers
365 * Description : Compile the pattern matching buffers.
371 *********************************************************************/
372 void LogCreatePatternMatchingBuffers(void)
376 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
378 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
385 /*********************************************************************
387 * Function : LogDestroyPatternMatchingBuffers
389 * Description : Free up the pattern matching buffers.
395 *********************************************************************/
396 void LogDestroyPatternMatchingBuffers(void)
400 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
402 regfree(&patterns_to_highlight[i].buffer);
409 /*********************************************************************
411 * Function : LogGetURLUnderCursor
413 * Description : Returns the URL from under the cursor (remember to free it!).
417 * Returns : NULL or a pointer to an URL string.
419 *********************************************************************/
420 char *LogGetURLUnderCursor(void)
422 char *szResult = NULL;
428 DWORD nWordStart = 0;
431 regcomp(&re, RE_URL, REG_ICASE);
433 /* Get the position of the cursor over the text window */
434 GetCursorPos(&ptCursor);
435 ScreenToClient(g_hwndLogBox, &ptCursor);
439 /* Search backwards and fowards to obtain the word that is highlighted */
440 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
441 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
442 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
444 /* Compare the string to the pattern */
445 if (nWordEnd > nWordStart)
450 range.chrg.cpMin = nWordStart;
451 range.chrg.cpMax = nWordEnd;
452 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
453 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
455 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
457 szResult = range.lpstrText;
461 free(range.lpstrText);
472 /*********************************************************************
474 * Function : LogPutString
476 * Description : Inserts text into the logging window. This is really
477 * a REGEXP aware wrapper function to `LogPutStringNoMatch'.
480 * 1 : pszText = pointer to string going to the log window
482 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
483 * FIXME: this is backwards to the rest of IJB and to common
484 * programming practice. Please use 0 => success instead.
486 *********************************************************************/
487 int LogPutString(const char *pszText)
494 if (pszText == NULL || strlen(pszText) == 0)
504 /* Critical section stops multiple threads doing nasty interactions that
505 * foul up the highlighting and output.
507 EnterCriticalSection(&g_criticalsection);
510 if (g_bHighlightMessages)
514 /* First things first, regexp scan for various things that we would like highlighted */
515 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
517 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
519 char *pszBefore = NULL;
520 char *pszMatch = NULL;
521 char *pszAfter = NULL;
524 /* Split the string up into pieces representing the strings, before
525 at and after the matching pattern
529 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
530 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
531 strncpy(pszBefore, pszText, match.rm_so);
533 if (match.rm_eo < (regoff_t)strlen(pszText))
535 pszAfter = strdup(&pszText[match.rm_eo]);
537 nMatchSize = match.rm_eo - match.rm_so;
538 pszMatch = (char *)malloc(nMatchSize + 1);
539 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
540 pszMatch[nMatchSize] = '\0';
542 /* Recursively call LogPutString */
545 LogPutString(pszBefore);
550 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
555 LogPutString(pszAfter);
566 result = LogPutStringNoMatch(pszText, STYLE_NONE);
571 LeaveCriticalSection(&g_criticalsection);
578 /*********************************************************************
580 * Function : LogPutStringNoMatch
582 * Description : Puts a string into the logging window.
585 * 1 : pszText = pointer to string going to the log window
586 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
588 * Returns : Always 1 => success.
589 * FIXME: this is backwards to the rest of IJB and to common
590 * programming practice. Please use 0 => success instead.
592 *********************************************************************/
593 int LogPutStringNoMatch(const char *pszText, int style)
599 assert(g_hwndLogBox);
600 if (g_hwndLogBox == NULL)
605 /* TODO preserve existing selection */
607 /* Go to the end of the text */
608 nTextLength = GetWindowTextLength(g_hwndLogBox);
609 range.cpMin = nTextLength;
610 range.cpMax = nTextLength;
611 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
613 /* Apply a formatting style */
614 memset(&format, 0, sizeof(format));
615 format.cbSize = sizeof(format);
616 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
617 format.yHeight = (g_nFontSize * 1440) / 72;
618 strcpy(format.szFaceName, g_szFontFaceName);
619 if (style == STYLE_NONE)
622 format.dwEffects |= CFE_AUTOCOLOR;
624 else if (style == STYLE_HEADER)
626 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
628 else if (style == STYLE_HIGHLIGHT)
630 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
632 else if (style == STYLE_LINK)
634 format.dwEffects |= CFE_UNDERLINE;
635 format.crTextColor = RGB(0, 0, 255);
637 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
639 /* Append text to the end */
640 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
642 /* TODO Restore the old selection */
645 if (strchr(pszText, '\n') != NULL)
647 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
650 /* Set the force clip timer going. This timer ensures clipping is done
651 intermittently even when there is a sustained burst of logging
653 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
655 g_bClipPending = TRUE;
663 /*********************************************************************
665 * Function : LogShowActivity
667 * Description : Start the spinner.
673 *********************************************************************/
674 void LogShowActivity(void)
676 /* Start some activity timers */
677 if (g_bShowActivityAnimation)
679 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
680 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
686 /*********************************************************************
688 * Function : LogClipBuffer
690 * Description : Prunes old lines from the log.
696 *********************************************************************/
697 void LogClipBuffer(void)
699 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
700 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
702 /* Compute the range representing the lines to be deleted */
703 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
704 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
707 range.cpMax = nLastChar;
709 /* TODO get current selection */
711 /* TODO adjust and clip old selection against range to be deleted */
713 /* Select range and erase it (turning off autoscroll to prevent
715 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
716 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
717 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
718 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
720 /* Restore old selection */
726 /*********************************************************************
728 * Function : CreateHiddenLogOwnerWindow
730 * Description : Creates a hidden owner window that stops the log
731 * window appearing in the task bar.
734 * 1 : hInstance = application's instance handle
736 * Returns : Handle to newly created window.
738 *********************************************************************/
739 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
741 static const char *szWndName = "JunkbusterLogLogOwner";
746 wc.lpfnWndProc = LogOwnerWindowProc;
749 wc.hInstance = hInstance;
752 wc.hbrBackground = 0;
754 wc.lpszClassName = szWndName;
758 hwnd = CreateWindow(szWndName, szWndName,
759 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
760 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
767 /*********************************************************************
769 * Function : LogOwnerWindowProc
771 * Description : Dummy procedure that does nothing special.
774 * 1 : hwnd = window handle
775 * 2 : uMsg = message number
776 * 3 : wParam = first param for this message
777 * 4 : lParam = next param for this message
779 * Returns : Same as `DefWindowProc'.
781 *********************************************************************/
782 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
784 return DefWindowProc(hwnd, uMsg, wParam, lParam);
789 /*********************************************************************
791 * Function : CreateLogWindow
793 * Description : Create the logging window.
796 * 1 : hInstance = application's instance handle
797 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
799 * Returns : Handle to newly created window.
801 *********************************************************************/
802 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
804 static const char *szWndName = "JunkbusterLogWindow";
805 static const char *szWndTitle = "Junkbuster";
808 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
809 HWND hwndChild = NULL;
813 memset(&wc, 0, sizeof(wc));
814 wc.cbSize = sizeof(wc);
815 wc.style = CS_DBLCLKS;
816 wc.lpfnWndProc = LogWindowProc;
819 wc.hInstance = hInstance;
820 wc.hIcon = g_hiconApp;
822 wc.hbrBackground = 0;
823 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
824 wc.lpszClassName = szWndName;
825 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
826 RegisterClassEx(&wc);
828 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
829 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
830 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
832 /* Now create a child list box */
833 GetClientRect(hwnd, &rcClient);
835 /* Create a rich edit control */
837 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
838 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
839 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
840 hwnd, NULL, hInstance, NULL);
841 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
843 /* Subclass the control to catch certain messages */
844 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
845 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
847 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
848 if (!g_bShowOnTaskBar &&
849 (nCmdShow == SW_SHOWMINIMIZED ||
850 nCmdShow == SW_MINIMIZE ||
851 nCmdShow == SW_SHOWMINNOACTIVE))
856 ShowWindow(hwnd, nCmdShow);
859 GetClientRect(g_hwndLogFrame, &rcClient);
860 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
867 /*********************************************************************
869 * Function : InitRichEdit
871 * Description : Initialise the rich edit control library.
875 * Returns : TRUE => success, FALSE => failure.
876 * FIXME: this is backwards to the rest of IJB and to common
877 * programming practice. Please use 0 => success instead.
879 *********************************************************************/
880 BOOL InitRichEdit(void)
882 static HINSTANCE hInstRichEdit;
883 if (hInstRichEdit == NULL)
885 g_nRichEditVersion = 0;
886 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
889 g_nRichEditVersion = _RICHEDIT_VER;
893 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
896 g_nRichEditVersion = 0x0100;
900 return (hInstRichEdit != NULL) ? TRUE : FALSE;
905 /*********************************************************************
907 * Function : ShowLogWindow
909 * Description : Shows or hides the log window. We will also raise the
910 * window on a show command in case it is buried.
913 * 1 : bShow = TRUE to show, FALSE to mimize/hide
917 *********************************************************************/
918 void ShowLogWindow(BOOL bShow)
922 SetForegroundWindow(g_hwndLogFrame);
923 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
925 else if (g_bShowOnTaskBar)
927 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
931 ShowWindow(g_hwndLogFrame, SW_HIDE);
937 /*********************************************************************
939 * Function : EditFile
941 * Description : Opens the specified setting file for editing.
944 * 1 : filename = filename from the config (aka junkbstr.txt) file.
948 *********************************************************************/
949 void EditFile(const char *filename)
953 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
959 /*--------------------------------------------------------------------------*/
960 /* Windows message handlers */
961 /*--------------------------------------------------------------------------*/
964 /*********************************************************************
966 * Function : OnLogRButtonUp
968 * Description : Handler for WM_RBUTTONUP messages.
971 * 1 : nModifier = wParam from mouse message (unused)
972 * 2 : x = x coordinate of the mouse event
973 * 3 : y = y coordinate of the mouse event
977 *********************************************************************/
978 void OnLogRButtonUp(int nModifier, int x, int y)
980 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
983 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
986 #endif /* def WIN_GUI_EDIT */
988 /* Check if there is a selection */
990 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
991 if (range.cpMin == range.cpMax)
993 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
997 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1001 /* Check if cursor is over a link */
1002 szURL = LogGetURLUnderCursor();
1006 TCHAR szMenuItemTemplate[1000];
1009 memset(&item, 0, sizeof(item));
1010 item.cbSize = sizeof(item);
1011 item.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
1012 item.fType = MFT_STRING;
1013 item.fState = MFS_ENABLED;
1014 item.wID = ID_NEW_BLOCKER;
1016 /* Put the item into the menu */
1017 memset(szMenuItemTemplate, 0, sizeof(szMenuItemTemplate));
1018 LoadString(g_hInstance, IDS_NEW_BLOCKER, szMenuItemTemplate, sizeof(szMenuItemTemplate) / sizeof(szMenuItemTemplate[0]));
1020 szMenuItem = (char *)malloc(strlen(szMenuItemTemplate) + strlen(szURL) + 1);
1021 sprintf(szMenuItem, szMenuItemTemplate, szURL);
1023 item.dwTypeData = szMenuItem;
1024 item.cch = strlen(szMenuItem);
1026 InsertMenuItem(hMenuPopup, 1, TRUE, &item);
1028 SetDefaultRule(szURL);
1032 #endif /* def WIN_GUI_EDIT */
1034 /* Display the popup */
1035 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1042 /*********************************************************************
1044 * Function : OnLogCommand
1046 * Description : Handler for WM_COMMAND messages.
1049 * 1 : nCommand = the command portion of the menu selection event
1053 *********************************************************************/
1054 void OnLogCommand(int nCommand)
1059 ShowLogWindow(TRUE);
1063 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1067 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1070 case ID_VIEW_CLEARLOG:
1071 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1074 case ID_VIEW_LOGMESSAGES:
1075 g_bLogMessages = !g_bLogMessages;
1076 /* SaveLogSettings(); */
1079 case ID_VIEW_MESSAGEHIGHLIGHTING:
1080 g_bHighlightMessages = !g_bHighlightMessages;
1081 /* SaveLogSettings(); */
1084 case ID_VIEW_LIMITBUFFERSIZE:
1085 g_bLimitBufferSize = !g_bLimitBufferSize;
1086 /* SaveLogSettings(); */
1089 case ID_VIEW_ACTIVITYANIMATION:
1090 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1091 /* SaveLogSettings(); */
1095 /* by haroon - change toggle to its opposite value */
1097 g_bToggleIJB = !g_bToggleIJB;
1100 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1104 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1109 case ID_TOOLS_EDITJUNKBUSTER:
1110 EditFile(configfile);
1113 case ID_TOOLS_EDITACTIONS:
1114 EditFile(g_actions_file);
1118 case ID_TOOLS_EDITPERLRE:
1119 EditFile(g_re_filterfile);
1124 case ID_TOOLS_EDITTRUST:
1125 EditFile(g_trustfile);
1127 #endif /* def TRUST_FILES */
1130 case ID_NEW_BLOCKER:
1131 ShowRulesDialog(g_hwndLogFrame);
1133 #endif /* def WIN_GUI_EDIT */
1136 ShellExecute(g_hwndLogFrame, "open", "gpl.html", NULL, NULL, SW_SHOWNORMAL);
1140 ShellExecute(g_hwndLogFrame, "open", "ijbfaq.html", NULL, NULL, SW_SHOWNORMAL);
1143 case ID_HELP_MANUAL:
1144 ShellExecute(g_hwndLogFrame, "open", "ijbman.html", NULL, NULL, SW_SHOWNORMAL);
1147 case ID_HELP_STATUS:
1148 ShellExecute(g_hwndLogFrame, "open", "Junkbuster Status.URL", NULL, NULL, SW_SHOWNORMAL);
1151 case ID_HELP_ABOUTJUNKBUSTER:
1152 MessageBox(g_hwndLogFrame, win32_blurb, "Junkbuster Information", MB_OK);
1163 /*********************************************************************
1165 * Function : OnLogInitMenu
1167 * Description : Handler for WM_INITMENU messages. Enable, disable,
1168 * check, and/or uncheck menu options as apropos.
1171 * 1 : hmenu = handle to menu to "make current"
1175 *********************************************************************/
1176 void OnLogInitMenu(HMENU hmenu)
1178 /* Only enable editors if there is a file to edit */
1179 EnableMenuItem(hmenu, ID_TOOLS_EDITACTIONS, MF_BYCOMMAND | (g_actions_file ? MF_ENABLED : MF_GRAYED));
1181 EnableMenuItem(hmenu, ID_TOOLS_EDITPERLRE, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1184 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1185 #endif /* def TRUST_FILES */
1187 /* Check/uncheck options */
1188 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1189 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1190 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1191 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1193 /* by haroon - menu item for Enable toggle on/off */
1194 CheckMenuItem(hmenu, ID_TOGGLE_IJB, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1200 /*********************************************************************
1202 * Function : OnLogTimer
1204 * Description : Handler for WM_TIMER messages.
1207 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1211 *********************************************************************/
1212 void OnLogTimer(int nTimer)
1217 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1220 case TIMER_ANIMSTOP_ID:
1222 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1223 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1224 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1227 case TIMER_CLIPBUFFER_ID:
1228 case TIMER_CLIPBUFFER_FORCE_ID:
1230 g_bClipPending = FALSE;
1231 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1232 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1243 /*********************************************************************
1245 * Function : LogRichEditProc
1247 * Description : Window subclass routine handles some events for the rich edit control.
1250 * 1 : hwnd = window handle of the rich edit control
1251 * 2 : uMsg = message number
1252 * 3 : wParam = first param for this message
1253 * 4 : lParam = next param for this message
1255 * Returns : Appropriate M$ window message handler codes.
1257 *********************************************************************/
1258 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1265 pt.x = LOWORD(lParam);
1266 pt.y = HIWORD(lParam);
1267 ClientToScreen(hwnd, &pt);
1268 OnLogRButtonUp(wParam, pt.x, pt.y);
1272 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1277 /*********************************************************************
1279 * Function : LogWindowProc
1281 * Description : Windows call back routine handles events on the log window.
1284 * 1 : hwnd = handle of the logging window
1285 * 2 : uMsg = message number
1286 * 3 : wParam = first param for this message
1287 * 4 : lParam = next param for this message
1289 * Returns : Appropriate M$ window message handler codes.
1291 *********************************************************************/
1292 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1300 /* This is the end - beautiful friend - the end */
1301 DestroyWindow(g_hwndLogBox);
1302 DestroyWindow(g_hwndLogFrame);
1311 /* Resize the logging window to fit the new frame */
1315 GetClientRect(g_hwndLogFrame, &rc);
1316 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1321 OnLogInitMenu((HMENU) wParam);
1329 OnLogCommand(LOWORD(wParam));
1336 if (g_bCloseHidesWindow)
1338 ShowLogWindow(FALSE);
1343 ShowLogWindow(FALSE);
1349 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1353 #endif /* ndef _WIN_CONSOLE - entire file */