1 const char w32log_rcs[] = "$Id: w32log.c,v 1.8 2001/05/29 09:50:24 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.8 2001/05/29 09:50:24 jongfoster
36 * Unified blocklist/imagelist/permissionslist.
37 * File format is still under discussion, but the internal changes
40 * Also modified interceptor behaviour:
41 * - We now intercept all URLs beginning with one of the following
42 * prefixes (and *only* these prefixes):
44 * * http://ijbswa.sf.net/config/
45 * * http://ijbswa.sourceforge.net/config/
46 * - New interceptors "home page" - go to http://i.j.b/ to see it.
47 * - Internal changes so that intercepted and fast redirect pages
48 * are not replaced with an image.
49 * - Interceptors now have the option to send a binary page direct
50 * to the client. (i.e. ijb-send-banner uses this)
51 * - Implemented show-url-info interceptor. (Which is why I needed
52 * the above interceptors changes - a typical URL is
53 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
54 * The previous mechanism would not have intercepted that, and
55 * if it had been intercepted then it then it would have replaced
58 * Revision 1.7 2001/05/26 01:26:34 jongfoster
59 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
60 * This #define cannot be set from ./configure - there's no point, it
61 * doesn't work yet. See feature request # 425722
63 * Revision 1.6 2001/05/26 00:31:30 jongfoster
64 * Fixing compiler warning about comparing signed/unsigned.
66 * Revision 1.5 2001/05/26 00:28:36 jongfoster
67 * Automatic reloading of config file.
68 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
69 * Most of the global variables have been moved to a new
70 * struct configuration_spec, accessed through csp->config->globalname
71 * Most of the globals remaining are used by the Win32 GUI.
73 * Revision 1.4 2001/05/22 18:56:28 oes
76 * Revision 1.3 2001/05/20 15:07:54 jongfoster
77 * File is now ignored if _WIN_CONSOLE is defined.
79 * Revision 1.2 2001/05/20 01:21:20 jongfoster
80 * Version 2.9.4 checkin.
81 * - Merged popupfile and cookiefile, and added control over PCRS
82 * filtering, in new "permissionsfile".
83 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
84 * file error you now get a message box (in the Win32 GUI) rather
85 * than the program exiting with no explanation.
86 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
88 * - Removed tabs from "config"
89 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
90 * - Bumped up version number.
92 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
93 * Initial import of version 2.9.3 source tree
96 *********************************************************************/
105 #include <richedit.h>
109 #include "w32taskbar.h"
110 #include "w32rulesdlg.h"
114 #include "miscutil.h"
117 const char w32res_h_rcs[] = W32RES_H_VERSION;
121 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
124 const char w32log_h_rcs[] = W32LOG_H_VERSION;
126 #ifndef _WIN_CONSOLE /* entire file */
129 * Timers and the various durations
131 #define TIMER_ANIM_ID 1
132 #define TIMER_ANIM_TIME 100
133 #define TIMER_ANIMSTOP_ID 2
134 #define TIMER_ANIMSTOP_TIME 1000
135 #define TIMER_CLIPBUFFER_ID 3
136 #define TIMER_CLIPBUFFER_TIME 1000
137 #define TIMER_CLIPBUFFER_FORCE_ID 4
138 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
141 * Styles of text that can be output
144 #define STYLE_HIGHLIGHT 1
146 #define STYLE_HEADER 3
149 * Number of frames of animation in tray activity sequence
151 #define ANIM_FRAMES 8
153 #define DEFAULT_MAX_BUFFER_LINES 200
154 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
155 #define DEFAULT_LOG_FONT_SIZE 8
158 * These values affect the way the log window behaves, they should be read
159 * from a file but for the moment, they are hardcoded here. Some options are
160 * configurable through the UI.
163 /* Indicates whether task bar shows activity animation */
164 BOOL g_bShowActivityAnimation = 1;
166 /* Indicates if the log window appears on the task bar */
167 BOOL g_bShowOnTaskBar = 0;
169 /* Indicates whether closing the log window really just hides it */
170 BOOL g_bCloseHidesWindow = 1;
172 /* Indicates if messages are logged at all */
173 BOOL g_bLogMessages = 1;
175 /* Indicates whether log messages are highlighted */
176 BOOL g_bHighlightMessages = 1;
178 /* Indicates if buffer is limited in size */
179 BOOL g_bLimitBufferSize = 1;
181 /* Maximum number of lines allowed in buffer when limited */
182 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
185 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
187 /* Size of font to use */
188 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
191 /* FIXME: this is a kludge */
193 const char * g_permissions_file = NULL;
194 const char * g_forwardfile = NULL;
196 const char * g_aclfile = NULL;
197 #endif /* def ACL_FILES */
199 const char * g_re_filterfile = NULL;
202 const char * g_trustfile = NULL;
203 #endif /* def TRUST_FILES */
205 /* FIXME: end kludge */
209 /* Regular expression for detected URLs */
210 #define RE_URL "http:[^ \n\r]*"
213 * Regular expressions that are used to perform highlight in the log window
215 static struct _Pattern
220 } patterns_to_highlight[] =
223 { RE_URL, STYLE_LINK },
224 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
225 /* interesting text to highlight */
226 { "crunch!", STYLE_HIGHLIGHT },
228 { "referer:", STYLE_HEADER },
229 { "proxy-connection:", STYLE_HEADER },
230 { "proxy-agent:", STYLE_HEADER },
231 { "user-agent:", STYLE_HEADER },
232 { "host:", STYLE_HEADER },
233 { "accept:", STYLE_HEADER },
234 { "accept-encoding:", STYLE_HEADER },
235 { "accept-language:", STYLE_HEADER },
236 { "accept-charset:", STYLE_HEADER },
237 { "accept-ranges:", STYLE_HEADER },
238 { "date:", STYLE_HEADER },
239 { "cache-control:", STYLE_HEADER },
240 { "cache-last-checked:", STYLE_HEADER },
241 { "connection:", STYLE_HEADER },
242 { "content-type", STYLE_HEADER },
243 { "content-length", STYLE_HEADER },
244 { "cookie", STYLE_HEADER },
245 { "last-modified:", STYLE_HEADER },
246 { "pragma:", STYLE_HEADER },
247 { "server:", STYLE_HEADER },
248 { "etag:", STYLE_HEADER },
249 { "expires:", STYLE_HEADER },
250 { "warning:", STYLE_HEADER },
251 /* this is the terminator statement - do not delete! */
254 #endif /* def REGEX */
265 static CRITICAL_SECTION g_criticalsection;
266 static HWND g_hwndTray;
267 static HWND g_hwndLogBox;
268 static WNDPROC g_fnLogBox;
269 static HICON g_hiconAnim[ANIM_FRAMES];
270 static HICON g_hiconIdle;
271 static HICON g_hiconApp;
272 static int g_nAnimFrame;
273 static BOOL g_bClipPending = FALSE;
274 static int g_nRichEditVersion = 0;
279 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
280 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
281 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
282 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
283 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
284 static BOOL InitRichEdit(void);
285 static void LogClipBuffer(void);
286 static void LogCreatePatternMatchingBuffers(void);
287 static void LogDestroyPatternMatchingBuffers(void);
288 static int LogPutStringNoMatch(const char *pszText, int style);
291 /*********************************************************************
293 * Function : InitLogWindow
295 * Description : Initialise the log window.
299 * Returns : Always TRUE (there should be error checking on the resources).
301 *********************************************************************/
302 BOOL InitLogWindow(void)
307 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
308 for (i = 0; i < ANIM_FRAMES; i++)
310 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER1 + i));
312 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER));
314 /* Create the user interface */
315 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
316 g_hwndTray = CreateTrayWindow(g_hInstance);
317 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Junkbuster");
319 /* Create pattern matching buffers (for highlighting */
320 LogCreatePatternMatchingBuffers();
322 /* Create a critical section to protect multi-threaded access to certain things */
323 InitializeCriticalSection(&g_criticalsection);
330 /*********************************************************************
332 * Function : TermLogWindow
334 * Description : Cleanup the logwindow.
340 *********************************************************************/
341 void TermLogWindow(void)
345 LogDestroyPatternMatchingBuffers();
347 TrayDeleteIcon(g_hwndTray, 1);
348 DeleteObject(g_hiconApp);
349 DeleteObject(g_hiconIdle);
350 for (i = 0; i < ANIM_FRAMES; i++)
352 DeleteObject(g_hiconAnim[i]);
358 /*********************************************************************
360 * Function : LogCreatePatternMatchingBuffers
362 * Description : Compile the pattern matching buffers.
368 *********************************************************************/
369 void LogCreatePatternMatchingBuffers(void)
373 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
375 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
382 /*********************************************************************
384 * Function : LogDestroyPatternMatchingBuffers
386 * Description : Free up the pattern matching buffers.
392 *********************************************************************/
393 void LogDestroyPatternMatchingBuffers(void)
397 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
399 regfree(&patterns_to_highlight[i].buffer);
406 /*********************************************************************
408 * Function : LogGetURLUnderCursor
410 * Description : Returns the URL from under the cursor (remember to free it!).
414 * Returns : NULL or a pointer to an URL string.
416 *********************************************************************/
417 char *LogGetURLUnderCursor(void)
419 char *szResult = NULL;
425 DWORD nWordStart = 0;
428 regcomp(&re, RE_URL, REG_ICASE);
430 /* Get the position of the cursor over the text window */
431 GetCursorPos(&ptCursor);
432 ScreenToClient(g_hwndLogBox, &ptCursor);
436 /* Search backwards and fowards to obtain the word that is highlighted */
437 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
438 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
439 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
441 /* Compare the string to the pattern */
442 if (nWordEnd > nWordStart)
447 range.chrg.cpMin = nWordStart;
448 range.chrg.cpMax = nWordEnd;
449 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
450 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
452 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
454 szResult = range.lpstrText;
458 free(range.lpstrText);
469 /*********************************************************************
471 * Function : LogPutString
473 * Description : Inserts text into the logging window. This is really
474 * a REGEXP aware wrapper function to `LogPutStringNoMatch'.
477 * 1 : pszText = pointer to string going to the log window
479 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
480 * FIXME: this is backwards to the rest of IJB and to common
481 * programming practice. Please use 0 => success instead.
483 *********************************************************************/
484 int LogPutString(const char *pszText)
491 if (pszText == NULL || strlen(pszText) == 0)
501 /* Critical section stops multiple threads doing nasty interactions that
502 * foul up the highlighting and output.
504 EnterCriticalSection(&g_criticalsection);
507 if (g_bHighlightMessages)
511 /* First things first, regexp scan for various things that we would like highlighted */
512 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
514 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
516 char *pszBefore = NULL;
517 char *pszMatch = NULL;
518 char *pszAfter = NULL;
521 /* Split the string up into pieces representing the strings, before
522 at and after the matching pattern
526 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
527 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
528 strncpy(pszBefore, pszText, match.rm_so);
530 if (match.rm_eo < (regoff_t)strlen(pszText))
532 pszAfter = strdup(&pszText[match.rm_eo]);
534 nMatchSize = match.rm_eo - match.rm_so;
535 pszMatch = (char *)malloc(nMatchSize + 1);
536 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
537 pszMatch[nMatchSize] = '\0';
539 /* Recursively call LogPutString */
542 LogPutString(pszBefore);
547 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
552 LogPutString(pszAfter);
563 result = LogPutStringNoMatch(pszText, STYLE_NONE);
568 LeaveCriticalSection(&g_criticalsection);
575 /*********************************************************************
577 * Function : LogPutStringNoMatch
579 * Description : Puts a string into the logging window.
582 * 1 : pszText = pointer to string going to the log window
583 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
585 * Returns : Always 1 => success.
586 * FIXME: this is backwards to the rest of IJB and to common
587 * programming practice. Please use 0 => success instead.
589 *********************************************************************/
590 int LogPutStringNoMatch(const char *pszText, int style)
596 assert(g_hwndLogBox);
597 if (g_hwndLogBox == NULL)
602 /* TODO preserve existing selection */
604 /* Go to the end of the text */
605 nTextLength = GetWindowTextLength(g_hwndLogBox);
606 range.cpMin = nTextLength;
607 range.cpMax = nTextLength;
608 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
610 /* Apply a formatting style */
611 memset(&format, 0, sizeof(format));
612 format.cbSize = sizeof(format);
613 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
614 format.yHeight = (g_nFontSize * 1440) / 72;
615 strcpy(format.szFaceName, g_szFontFaceName);
616 if (style == STYLE_NONE)
619 format.dwEffects |= CFE_AUTOCOLOR;
621 else if (style == STYLE_HEADER)
623 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
625 else if (style == STYLE_HIGHLIGHT)
627 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
629 else if (style == STYLE_LINK)
631 format.dwEffects |= CFE_UNDERLINE;
632 format.crTextColor = RGB(0, 0, 255);
634 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
636 /* Append text to the end */
637 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
639 /* TODO Restore the old selection */
642 if (strchr(pszText, '\n') != NULL)
644 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
647 /* Set the force clip timer going. This timer ensures clipping is done
648 intermittently even when there is a sustained burst of logging
650 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
652 g_bClipPending = TRUE;
660 /*********************************************************************
662 * Function : LogShowActivity
664 * Description : Start the spinner.
670 *********************************************************************/
671 void LogShowActivity(void)
673 /* Start some activity timers */
674 if (g_bShowActivityAnimation)
676 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
677 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
683 /*********************************************************************
685 * Function : LogClipBuffer
687 * Description : Prunes old lines from the log.
693 *********************************************************************/
694 void LogClipBuffer(void)
696 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
697 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
699 /* Compute the range representing the lines to be deleted */
700 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
701 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
704 range.cpMax = nLastChar;
706 /* TODO get current selection */
708 /* TODO adjust and clip old selection against range to be deleted */
710 /* Select range and erase it (turning off autoscroll to prevent
712 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
713 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
714 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
715 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
717 /* Restore old selection */
723 /*********************************************************************
725 * Function : CreateHiddenLogOwnerWindow
727 * Description : Creates a hidden owner window that stops the log
728 * window appearing in the task bar.
731 * 1 : hInstance = application's instance handle
733 * Returns : Handle to newly created window.
735 *********************************************************************/
736 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
738 static const char *szWndName = "JunkbusterLogLogOwner";
743 wc.lpfnWndProc = LogOwnerWindowProc;
746 wc.hInstance = hInstance;
749 wc.hbrBackground = 0;
751 wc.lpszClassName = szWndName;
755 hwnd = CreateWindow(szWndName, szWndName,
756 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
757 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
764 /*********************************************************************
766 * Function : LogOwnerWindowProc
768 * Description : Dummy procedure that does nothing special.
771 * 1 : hwnd = window handle
772 * 2 : uMsg = message number
773 * 3 : wParam = first param for this message
774 * 4 : lParam = next param for this message
776 * Returns : Same as `DefWindowProc'.
778 *********************************************************************/
779 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
781 return DefWindowProc(hwnd, uMsg, wParam, lParam);
786 /*********************************************************************
788 * Function : CreateLogWindow
790 * Description : Create the logging window.
793 * 1 : hInstance = application's instance handle
794 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
796 * Returns : Handle to newly created window.
798 *********************************************************************/
799 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
801 static const char *szWndName = "JunkbusterLogWindow";
802 static const char *szWndTitle = "Junkbuster";
805 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
806 HWND hwndChild = NULL;
810 memset(&wc, 0, sizeof(wc));
811 wc.cbSize = sizeof(wc);
812 wc.style = CS_DBLCLKS;
813 wc.lpfnWndProc = LogWindowProc;
816 wc.hInstance = hInstance;
817 wc.hIcon = g_hiconApp;
819 wc.hbrBackground = 0;
820 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
821 wc.lpszClassName = szWndName;
822 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
823 RegisterClassEx(&wc);
825 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
826 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
827 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
829 /* Now create a child list box */
830 GetClientRect(hwnd, &rcClient);
832 /* Create a rich edit control */
834 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
835 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
836 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
837 hwnd, NULL, hInstance, NULL);
838 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
840 /* Subclass the control to catch certain messages */
841 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
842 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
844 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
845 if (!g_bShowOnTaskBar &&
846 (nCmdShow == SW_SHOWMINIMIZED ||
847 nCmdShow == SW_MINIMIZE ||
848 nCmdShow == SW_SHOWMINNOACTIVE))
853 ShowWindow(hwnd, nCmdShow);
856 GetClientRect(g_hwndLogFrame, &rcClient);
857 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
864 /*********************************************************************
866 * Function : InitRichEdit
868 * Description : Initialise the rich edit control library.
872 * Returns : TRUE => success, FALSE => failure.
873 * FIXME: this is backwards to the rest of IJB and to common
874 * programming practice. Please use 0 => success instead.
876 *********************************************************************/
877 BOOL InitRichEdit(void)
879 static HINSTANCE hInstRichEdit;
880 if (hInstRichEdit == NULL)
882 g_nRichEditVersion = 0;
883 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
886 g_nRichEditVersion = _RICHEDIT_VER;
890 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
893 g_nRichEditVersion = 0x0100;
897 return (hInstRichEdit != NULL) ? TRUE : FALSE;
902 /*********************************************************************
904 * Function : ShowLogWindow
906 * Description : Shows or hides the log window. We will also raise the
907 * window on a show command in case it is buried.
910 * 1 : bShow = TRUE to show, FALSE to mimize/hide
914 *********************************************************************/
915 void ShowLogWindow(BOOL bShow)
919 SetForegroundWindow(g_hwndLogFrame);
920 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
922 else if (g_bShowOnTaskBar)
924 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
928 ShowWindow(g_hwndLogFrame, SW_HIDE);
934 /*********************************************************************
936 * Function : EditFile
938 * Description : Opens the specified setting file for editing.
941 * 1 : filename = filename from the config (aka junkbstr.txt) file.
945 *********************************************************************/
946 void EditFile(const char *filename)
950 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
956 /*--------------------------------------------------------------------------*/
957 /* Windows message handlers */
958 /*--------------------------------------------------------------------------*/
961 /*********************************************************************
963 * Function : OnLogRButtonUp
965 * Description : Handler for WM_RBUTTONUP messages.
968 * 1 : nModifier = wParam from mouse message (unused)
969 * 2 : x = x coordinate of the mouse event
970 * 3 : y = y coordinate of the mouse event
974 *********************************************************************/
975 void OnLogRButtonUp(int nModifier, int x, int y)
977 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
980 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
983 #endif /* def WIN_GUI_EDIT */
985 /* Check if there is a selection */
987 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
988 if (range.cpMin == range.cpMax)
990 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
994 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
998 /* Check if cursor is over a link */
999 szURL = LogGetURLUnderCursor();
1003 TCHAR szMenuItemTemplate[1000];
1006 memset(&item, 0, sizeof(item));
1007 item.cbSize = sizeof(item);
1008 item.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
1009 item.fType = MFT_STRING;
1010 item.fState = MFS_ENABLED;
1011 item.wID = ID_NEW_BLOCKER;
1013 /* Put the item into the menu */
1014 memset(szMenuItemTemplate, 0, sizeof(szMenuItemTemplate));
1015 LoadString(g_hInstance, IDS_NEW_BLOCKER, szMenuItemTemplate, sizeof(szMenuItemTemplate) / sizeof(szMenuItemTemplate[0]));
1017 szMenuItem = (char *)malloc(strlen(szMenuItemTemplate) + strlen(szURL) + 1);
1018 sprintf(szMenuItem, szMenuItemTemplate, szURL);
1020 item.dwTypeData = szMenuItem;
1021 item.cch = strlen(szMenuItem);
1023 InsertMenuItem(hMenuPopup, 1, TRUE, &item);
1025 SetDefaultRule(szURL);
1029 #endif /* def WIN_GUI_EDIT */
1031 /* Display the popup */
1032 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1039 /*********************************************************************
1041 * Function : OnLogCommand
1043 * Description : Handler for WM_COMMAND messages.
1046 * 1 : nCommand = the command portion of the menu selection event
1050 *********************************************************************/
1051 void OnLogCommand(int nCommand)
1056 ShowLogWindow(TRUE);
1060 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1064 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1067 case ID_VIEW_CLEARLOG:
1068 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1071 case ID_VIEW_LOGMESSAGES:
1072 g_bLogMessages = !g_bLogMessages;
1073 /* SaveLogSettings(); */
1076 case ID_VIEW_MESSAGEHIGHLIGHTING:
1077 g_bHighlightMessages = !g_bHighlightMessages;
1078 /* SaveLogSettings(); */
1081 case ID_VIEW_LIMITBUFFERSIZE:
1082 g_bLimitBufferSize = !g_bLimitBufferSize;
1083 /* SaveLogSettings(); */
1086 case ID_VIEW_ACTIVITYANIMATION:
1087 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1088 /* SaveLogSettings(); */
1092 /* by haroon - change toggle to its opposite value */
1094 g_bToggleIJB = !g_bToggleIJB;
1097 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1101 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1106 case ID_TOOLS_EDITJUNKBUSTER:
1107 EditFile(configfile);
1110 case ID_TOOLS_EDITPERMISSIONS:
1111 EditFile(g_permissions_file);
1114 case ID_TOOLS_EDITFORWARD:
1115 EditFile(g_forwardfile);
1119 case ID_TOOLS_EDITACLS:
1120 EditFile(g_aclfile);
1122 #endif /* def ACL_FILES */
1125 case ID_TOOLS_EDITPERLRE:
1126 EditFile(g_re_filterfile);
1131 case ID_TOOLS_EDITTRUST:
1132 EditFile(g_trustfile);
1134 #endif /* def TRUST_FILES */
1137 case ID_NEW_BLOCKER:
1138 ShowRulesDialog(g_hwndLogFrame);
1140 #endif /* def WIN_GUI_EDIT */
1143 ShellExecute(g_hwndLogFrame, "open", "gpl.html", NULL, NULL, SW_SHOWNORMAL);
1147 ShellExecute(g_hwndLogFrame, "open", "ijbfaq.html", NULL, NULL, SW_SHOWNORMAL);
1150 case ID_HELP_MANUAL:
1151 ShellExecute(g_hwndLogFrame, "open", "ijbman.html", NULL, NULL, SW_SHOWNORMAL);
1154 case ID_HELP_STATUS:
1155 ShellExecute(g_hwndLogFrame, "open", "Junkbuster Status.URL", NULL, NULL, SW_SHOWNORMAL);
1158 case ID_HELP_ABOUTJUNKBUSTER:
1159 MessageBox(g_hwndLogFrame, win32_blurb, "Junkbuster Information", MB_OK);
1170 /*********************************************************************
1172 * Function : OnLogInitMenu
1174 * Description : Handler for WM_INITMENU messages. Enable, disable,
1175 * check, and/or uncheck menu options as apropos.
1178 * 1 : hmenu = handle to menu to "make current"
1182 *********************************************************************/
1183 void OnLogInitMenu(HMENU hmenu)
1185 /* Only enable editors if there is a file to edit */
1186 EnableMenuItem(hmenu, ID_TOOLS_EDITPERMISSIONS, MF_BYCOMMAND | (g_permissions_file ? MF_ENABLED : MF_GRAYED));
1187 EnableMenuItem(hmenu, ID_TOOLS_EDITFORWARD, MF_BYCOMMAND | (g_forwardfile ? MF_ENABLED : MF_GRAYED));
1189 EnableMenuItem(hmenu, ID_TOOLS_EDITACLS, MF_BYCOMMAND | (g_aclfile ? MF_ENABLED : MF_GRAYED));
1190 #endif /* def ACL_FILES */
1192 EnableMenuItem(hmenu, ID_TOOLS_EDITPERLRE, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1195 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1196 #endif /* def TRUST_FILES */
1198 /* Check/uncheck options */
1199 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1200 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1201 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1202 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1204 /* by haroon - menu item for Enable toggle on/off */
1205 CheckMenuItem(hmenu, ID_TOGGLE_IJB, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1211 /*********************************************************************
1213 * Function : OnLogTimer
1215 * Description : Handler for WM_TIMER messages.
1218 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1222 *********************************************************************/
1223 void OnLogTimer(int nTimer)
1228 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1231 case TIMER_ANIMSTOP_ID:
1233 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1234 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1235 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1238 case TIMER_CLIPBUFFER_ID:
1239 case TIMER_CLIPBUFFER_FORCE_ID:
1241 g_bClipPending = FALSE;
1242 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1243 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1254 /*********************************************************************
1256 * Function : LogRichEditProc
1258 * Description : Window subclass routine handles some events for the rich edit control.
1261 * 1 : hwnd = window handle of the rich edit control
1262 * 2 : uMsg = message number
1263 * 3 : wParam = first param for this message
1264 * 4 : lParam = next param for this message
1266 * Returns : Appropriate M$ window message handler codes.
1268 *********************************************************************/
1269 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1276 pt.x = LOWORD(lParam);
1277 pt.y = HIWORD(lParam);
1278 ClientToScreen(hwnd, &pt);
1279 OnLogRButtonUp(wParam, pt.x, pt.y);
1283 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1288 /*********************************************************************
1290 * Function : LogWindowProc
1292 * Description : Windows call back routine handles events on the log window.
1295 * 1 : hwnd = handle of the logging window
1296 * 2 : uMsg = message number
1297 * 3 : wParam = first param for this message
1298 * 4 : lParam = next param for this message
1300 * Returns : Appropriate M$ window message handler codes.
1302 *********************************************************************/
1303 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1311 /* This is the end - beautiful friend - the end */
1312 DestroyWindow(g_hwndLogBox);
1313 DestroyWindow(g_hwndLogFrame);
1322 /* Resize the logging window to fit the new frame */
1326 GetClientRect(g_hwndLogFrame, &rc);
1327 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1332 OnLogInitMenu((HMENU) wParam);
1340 OnLogCommand(LOWORD(wParam));
1347 if (g_bCloseHidesWindow)
1349 ShowLogWindow(FALSE);
1354 ShowLogWindow(FALSE);
1360 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1364 #endif /* ndef _WIN_CONSOLE - entire file */