1 const char w32log_rcs[] = "$Id: w32log.c,v 1.11 2001/06/07 23:08:12 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.11 2001/06/07 23:08:12 jongfoster
36 * Forward and ACL edit options removed.
38 * Revision 1.10 2001/05/31 21:37:11 jongfoster
39 * GUI changes to rename "permissions file" to "actions file".
41 * Revision 1.9 2001/05/31 17:33:13 oes
45 * Revision 1.8 2001/05/29 09:50:24 jongfoster
46 * Unified blocklist/imagelist/permissionslist.
47 * File format is still under discussion, but the internal changes
50 * Also modified interceptor behaviour:
51 * - We now intercept all URLs beginning with one of the following
52 * prefixes (and *only* these prefixes):
54 * * http://ijbswa.sf.net/config/
55 * * http://ijbswa.sourceforge.net/config/
56 * - New interceptors "home page" - go to http://i.j.b/ to see it.
57 * - Internal changes so that intercepted and fast redirect pages
58 * are not replaced with an image.
59 * - Interceptors now have the option to send a binary page direct
60 * to the client. (i.e. ijb-send-banner uses this)
61 * - Implemented show-url-info interceptor. (Which is why I needed
62 * the above interceptors changes - a typical URL is
63 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
64 * The previous mechanism would not have intercepted that, and
65 * if it had been intercepted then it then it would have replaced
68 * Revision 1.7 2001/05/26 01:26:34 jongfoster
69 * New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
70 * This #define cannot be set from ./configure - there's no point, it
71 * doesn't work yet. See feature request # 425722
73 * Revision 1.6 2001/05/26 00:31:30 jongfoster
74 * Fixing compiler warning about comparing signed/unsigned.
76 * Revision 1.5 2001/05/26 00:28:36 jongfoster
77 * Automatic reloading of config file.
78 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
79 * Most of the global variables have been moved to a new
80 * struct configuration_spec, accessed through csp->config->globalname
81 * Most of the globals remaining are used by the Win32 GUI.
83 * Revision 1.4 2001/05/22 18:56:28 oes
86 * Revision 1.3 2001/05/20 15:07:54 jongfoster
87 * File is now ignored if _WIN_CONSOLE is defined.
89 * Revision 1.2 2001/05/20 01:21:20 jongfoster
90 * Version 2.9.4 checkin.
91 * - Merged popupfile and cookiefile, and added control over PCRS
92 * filtering, in new "permissionsfile".
93 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
94 * file error you now get a message box (in the Win32 GUI) rather
95 * than the program exiting with no explanation.
96 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
98 * - Removed tabs from "config"
99 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
100 * - Bumped up version number.
102 * Revision 1.1.1.1 2001/05/15 13:59:07 oes
103 * Initial import of version 2.9.3 source tree
106 *********************************************************************/
115 #include <richedit.h>
119 #include "w32taskbar.h"
120 #include "w32rulesdlg.h"
124 #include "miscutil.h"
127 const char w32res_h_rcs[] = W32RES_H_VERSION;
131 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
134 const char w32log_h_rcs[] = W32LOG_H_VERSION;
136 #ifndef _WIN_CONSOLE /* entire file */
139 * Timers and the various durations
141 #define TIMER_ANIM_ID 1
142 #define TIMER_ANIM_TIME 100
143 #define TIMER_ANIMSTOP_ID 2
144 #define TIMER_ANIMSTOP_TIME 1000
145 #define TIMER_CLIPBUFFER_ID 3
146 #define TIMER_CLIPBUFFER_TIME 1000
147 #define TIMER_CLIPBUFFER_FORCE_ID 4
148 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
151 * Styles of text that can be output
154 #define STYLE_HIGHLIGHT 1
156 #define STYLE_HEADER 3
159 * Number of frames of animation in tray activity sequence
161 #define ANIM_FRAMES 8
163 #define DEFAULT_MAX_BUFFER_LINES 200
164 #define DEFAULT_LOG_FONT_NAME "MS Sans Serif"
165 #define DEFAULT_LOG_FONT_SIZE 8
168 * These values affect the way the log window behaves, they should be read
169 * from a file but for the moment, they are hardcoded here. Some options are
170 * configurable through the UI.
173 /* Indicates whether task bar shows activity animation */
174 BOOL g_bShowActivityAnimation = 1;
176 /* Indicates if the log window appears on the task bar */
177 BOOL g_bShowOnTaskBar = 0;
179 /* Indicates whether closing the log window really just hides it */
180 BOOL g_bCloseHidesWindow = 1;
182 /* Indicates if messages are logged at all */
183 BOOL g_bLogMessages = 1;
185 /* Indicates whether log messages are highlighted */
186 BOOL g_bHighlightMessages = 1;
188 /* Indicates if buffer is limited in size */
189 BOOL g_bLimitBufferSize = 1;
191 /* Maximum number of lines allowed in buffer when limited */
192 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
195 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
197 /* Size of font to use */
198 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
201 /* FIXME: this is a kludge */
203 const char * g_actions_file = NULL;
204 const char * g_re_filterfile = NULL;
206 const char * g_trustfile = NULL;
207 #endif /* def TRUST_FILES */
209 /* FIXME: end kludge */
213 /* Regular expression for detected URLs */
214 #define RE_URL "http:[^ \n\r]*"
217 * Regular expressions that are used to perform highlight in the log window
219 static struct _Pattern
224 } patterns_to_highlight[] =
227 { RE_URL, STYLE_LINK },
228 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
229 /* interesting text to highlight */
230 { "crunch!", STYLE_HIGHLIGHT },
232 { "referer:", STYLE_HEADER },
233 { "proxy-connection:", STYLE_HEADER },
234 { "proxy-agent:", STYLE_HEADER },
235 { "user-agent:", STYLE_HEADER },
236 { "host:", STYLE_HEADER },
237 { "accept:", STYLE_HEADER },
238 { "accept-encoding:", STYLE_HEADER },
239 { "accept-language:", STYLE_HEADER },
240 { "accept-charset:", STYLE_HEADER },
241 { "accept-ranges:", STYLE_HEADER },
242 { "date:", STYLE_HEADER },
243 { "cache-control:", STYLE_HEADER },
244 { "cache-last-checked:", STYLE_HEADER },
245 { "connection:", STYLE_HEADER },
246 { "content-type", STYLE_HEADER },
247 { "content-length", STYLE_HEADER },
248 { "cookie", STYLE_HEADER },
249 { "last-modified:", STYLE_HEADER },
250 { "pragma:", STYLE_HEADER },
251 { "server:", STYLE_HEADER },
252 { "etag:", STYLE_HEADER },
253 { "expires:", STYLE_HEADER },
254 { "warning:", STYLE_HEADER },
255 /* this is the terminator statement - do not delete! */
258 #endif /* def REGEX */
269 static CRITICAL_SECTION g_criticalsection;
270 static HWND g_hwndTray;
271 static HWND g_hwndLogBox;
272 static WNDPROC g_fnLogBox;
273 static HICON g_hiconAnim[ANIM_FRAMES];
274 static HICON g_hiconIdle;
275 static HICON g_hiconApp;
276 static int g_nAnimFrame;
277 static BOOL g_bClipPending = FALSE;
278 static int g_nRichEditVersion = 0;
283 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
284 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
285 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
286 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
287 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
288 static BOOL InitRichEdit(void);
289 static void LogClipBuffer(void);
290 static void LogCreatePatternMatchingBuffers(void);
291 static void LogDestroyPatternMatchingBuffers(void);
292 static int LogPutStringNoMatch(const char *pszText, int style);
295 /*********************************************************************
297 * Function : InitLogWindow
299 * Description : Initialise the log window.
303 * Returns : Always TRUE (there should be error checking on the resources).
305 *********************************************************************/
306 BOOL InitLogWindow(void)
311 g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
312 for (i = 0; i < ANIM_FRAMES; i++)
314 g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER1 + i));
316 g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_JUNKBUSTER));
318 /* Create the user interface */
319 g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
320 g_hwndTray = CreateTrayWindow(g_hInstance);
321 TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Junkbuster");
323 /* Create pattern matching buffers (for highlighting */
324 LogCreatePatternMatchingBuffers();
326 /* Create a critical section to protect multi-threaded access to certain things */
327 InitializeCriticalSection(&g_criticalsection);
334 /*********************************************************************
336 * Function : TermLogWindow
338 * Description : Cleanup the logwindow.
344 *********************************************************************/
345 void TermLogWindow(void)
349 LogDestroyPatternMatchingBuffers();
351 TrayDeleteIcon(g_hwndTray, 1);
352 DeleteObject(g_hiconApp);
353 DeleteObject(g_hiconIdle);
354 for (i = 0; i < ANIM_FRAMES; i++)
356 DeleteObject(g_hiconAnim[i]);
362 /*********************************************************************
364 * Function : LogCreatePatternMatchingBuffers
366 * Description : Compile the pattern matching buffers.
372 *********************************************************************/
373 void LogCreatePatternMatchingBuffers(void)
377 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
379 regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
386 /*********************************************************************
388 * Function : LogDestroyPatternMatchingBuffers
390 * Description : Free up the pattern matching buffers.
396 *********************************************************************/
397 void LogDestroyPatternMatchingBuffers(void)
401 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
403 regfree(&patterns_to_highlight[i].buffer);
410 /*********************************************************************
412 * Function : LogGetURLUnderCursor
414 * Description : Returns the URL from under the cursor (remember to free it!).
418 * Returns : NULL or a pointer to an URL string.
420 *********************************************************************/
421 char *LogGetURLUnderCursor(void)
423 char *szResult = NULL;
429 DWORD nWordStart = 0;
432 regcomp(&re, RE_URL, REG_ICASE);
434 /* Get the position of the cursor over the text window */
435 GetCursorPos(&ptCursor);
436 ScreenToClient(g_hwndLogBox, &ptCursor);
440 /* Search backwards and fowards to obtain the word that is highlighted */
441 nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
442 nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
443 nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
445 /* Compare the string to the pattern */
446 if (nWordEnd > nWordStart)
451 range.chrg.cpMin = nWordStart;
452 range.chrg.cpMax = nWordEnd;
453 range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
454 SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
456 if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
458 szResult = range.lpstrText;
462 free(range.lpstrText);
473 /*********************************************************************
475 * Function : LogPutString
477 * Description : Inserts text into the logging window. This is really
478 * a REGEXP aware wrapper function to `LogPutStringNoMatch'.
481 * 1 : pszText = pointer to string going to the log window
483 * Returns : 1 => success, else the return code from `LogPutStringNoMatch'.
484 * FIXME: this is backwards to the rest of IJB and to common
485 * programming practice. Please use 0 => success instead.
487 *********************************************************************/
488 int LogPutString(const char *pszText)
495 if (pszText == NULL || strlen(pszText) == 0)
505 /* Critical section stops multiple threads doing nasty interactions that
506 * foul up the highlighting and output.
508 EnterCriticalSection(&g_criticalsection);
511 if (g_bHighlightMessages)
515 /* First things first, regexp scan for various things that we would like highlighted */
516 for (i = 0; patterns_to_highlight[i].str != NULL; i++)
518 if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
520 char *pszBefore = NULL;
521 char *pszMatch = NULL;
522 char *pszAfter = NULL;
525 /* Split the string up into pieces representing the strings, before
526 at and after the matching pattern
530 pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
531 memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
532 strncpy(pszBefore, pszText, match.rm_so);
534 if (match.rm_eo < (regoff_t)strlen(pszText))
536 pszAfter = strdup(&pszText[match.rm_eo]);
538 nMatchSize = match.rm_eo - match.rm_so;
539 pszMatch = (char *)malloc(nMatchSize + 1);
540 strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
541 pszMatch[nMatchSize] = '\0';
543 /* Recursively call LogPutString */
546 LogPutString(pszBefore);
551 LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
556 LogPutString(pszAfter);
567 result = LogPutStringNoMatch(pszText, STYLE_NONE);
572 LeaveCriticalSection(&g_criticalsection);
579 /*********************************************************************
581 * Function : LogPutStringNoMatch
583 * Description : Puts a string into the logging window.
586 * 1 : pszText = pointer to string going to the log window
587 * 2 : style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
589 * Returns : Always 1 => success.
590 * FIXME: this is backwards to the rest of IJB and to common
591 * programming practice. Please use 0 => success instead.
593 *********************************************************************/
594 int LogPutStringNoMatch(const char *pszText, int style)
600 assert(g_hwndLogBox);
601 if (g_hwndLogBox == NULL)
606 /* TODO preserve existing selection */
608 /* Go to the end of the text */
609 nTextLength = GetWindowTextLength(g_hwndLogBox);
610 range.cpMin = nTextLength;
611 range.cpMax = nTextLength;
612 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
614 /* Apply a formatting style */
615 memset(&format, 0, sizeof(format));
616 format.cbSize = sizeof(format);
617 format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
618 format.yHeight = (g_nFontSize * 1440) / 72;
619 strcpy(format.szFaceName, g_szFontFaceName);
620 if (style == STYLE_NONE)
623 format.dwEffects |= CFE_AUTOCOLOR;
625 else if (style == STYLE_HEADER)
627 format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
629 else if (style == STYLE_HIGHLIGHT)
631 format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
633 else if (style == STYLE_LINK)
635 format.dwEffects |= CFE_UNDERLINE;
636 format.crTextColor = RGB(0, 0, 255);
638 SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
640 /* Append text to the end */
641 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
643 /* TODO Restore the old selection */
646 if (strchr(pszText, '\n') != NULL)
648 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
651 /* Set the force clip timer going. This timer ensures clipping is done
652 intermittently even when there is a sustained burst of logging
654 SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
656 g_bClipPending = TRUE;
664 /*********************************************************************
666 * Function : LogShowActivity
668 * Description : Start the spinner.
674 *********************************************************************/
675 void LogShowActivity(void)
677 /* Start some activity timers */
678 if (g_bShowActivityAnimation)
680 SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
681 SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
687 /*********************************************************************
689 * Function : LogClipBuffer
691 * Description : Prunes old lines from the log.
697 *********************************************************************/
698 void LogClipBuffer(void)
700 int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
701 if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
703 /* Compute the range representing the lines to be deleted */
704 LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
705 LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
708 range.cpMax = nLastChar;
710 /* TODO get current selection */
712 /* TODO adjust and clip old selection against range to be deleted */
714 /* Select range and erase it (turning off autoscroll to prevent
716 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
717 SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
718 SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
719 SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
721 /* Restore old selection */
727 /*********************************************************************
729 * Function : CreateHiddenLogOwnerWindow
731 * Description : Creates a hidden owner window that stops the log
732 * window appearing in the task bar.
735 * 1 : hInstance = application's instance handle
737 * Returns : Handle to newly created window.
739 *********************************************************************/
740 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
742 static const char *szWndName = "JunkbusterLogLogOwner";
747 wc.lpfnWndProc = LogOwnerWindowProc;
750 wc.hInstance = hInstance;
753 wc.hbrBackground = 0;
755 wc.lpszClassName = szWndName;
759 hwnd = CreateWindow(szWndName, szWndName,
760 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
761 CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
768 /*********************************************************************
770 * Function : LogOwnerWindowProc
772 * Description : Dummy procedure that does nothing special.
775 * 1 : hwnd = window handle
776 * 2 : uMsg = message number
777 * 3 : wParam = first param for this message
778 * 4 : lParam = next param for this message
780 * Returns : Same as `DefWindowProc'.
782 *********************************************************************/
783 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
785 return DefWindowProc(hwnd, uMsg, wParam, lParam);
790 /*********************************************************************
792 * Function : CreateLogWindow
794 * Description : Create the logging window.
797 * 1 : hInstance = application's instance handle
798 * 2 : nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
800 * Returns : Handle to newly created window.
802 *********************************************************************/
803 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
805 static const char *szWndName = "JunkbusterLogWindow";
806 static const char *szWndTitle = "Junkbuster";
809 HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
810 HWND hwndChild = NULL;
814 memset(&wc, 0, sizeof(wc));
815 wc.cbSize = sizeof(wc);
816 wc.style = CS_DBLCLKS;
817 wc.lpfnWndProc = LogWindowProc;
820 wc.hInstance = hInstance;
821 wc.hIcon = g_hiconApp;
823 wc.hbrBackground = 0;
824 wc.lpszMenuName = MAKEINTRESOURCE(IDR_LOGVIEW);
825 wc.lpszClassName = szWndName;
826 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
827 RegisterClassEx(&wc);
829 hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
830 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
831 CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
833 /* Now create a child list box */
834 GetClientRect(hwnd, &rcClient);
836 /* Create a rich edit control */
838 g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
839 ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
840 rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
841 hwnd, NULL, hInstance, NULL);
842 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
844 /* Subclass the control to catch certain messages */
845 g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
846 SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
848 /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
849 if (!g_bShowOnTaskBar &&
850 (nCmdShow == SW_SHOWMINIMIZED ||
851 nCmdShow == SW_MINIMIZE ||
852 nCmdShow == SW_SHOWMINNOACTIVE))
857 ShowWindow(hwnd, nCmdShow);
860 GetClientRect(g_hwndLogFrame, &rcClient);
861 SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
868 /*********************************************************************
870 * Function : InitRichEdit
872 * Description : Initialise the rich edit control library.
876 * Returns : TRUE => success, FALSE => failure.
877 * FIXME: this is backwards to the rest of IJB and to common
878 * programming practice. Please use 0 => success instead.
880 *********************************************************************/
881 BOOL InitRichEdit(void)
883 static HINSTANCE hInstRichEdit;
884 if (hInstRichEdit == NULL)
886 g_nRichEditVersion = 0;
887 hInstRichEdit = LoadLibraryA("RICHED20.DLL");
890 g_nRichEditVersion = _RICHEDIT_VER;
894 hInstRichEdit = LoadLibraryA("RICHED32.DLL");
897 g_nRichEditVersion = 0x0100;
901 return (hInstRichEdit != NULL) ? TRUE : FALSE;
906 /*********************************************************************
908 * Function : ShowLogWindow
910 * Description : Shows or hides the log window. We will also raise the
911 * window on a show command in case it is buried.
914 * 1 : bShow = TRUE to show, FALSE to mimize/hide
918 *********************************************************************/
919 void ShowLogWindow(BOOL bShow)
923 SetForegroundWindow(g_hwndLogFrame);
924 SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
926 else if (g_bShowOnTaskBar)
928 ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
932 ShowWindow(g_hwndLogFrame, SW_HIDE);
938 /*********************************************************************
940 * Function : EditFile
942 * Description : Opens the specified setting file for editing.
945 * 1 : filename = filename from the config (aka junkbstr.txt) file.
949 *********************************************************************/
950 void EditFile(const char *filename)
954 ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
960 /*--------------------------------------------------------------------------*/
961 /* Windows message handlers */
962 /*--------------------------------------------------------------------------*/
965 /*********************************************************************
967 * Function : OnLogRButtonUp
969 * Description : Handler for WM_RBUTTONUP messages.
972 * 1 : nModifier = wParam from mouse message (unused)
973 * 2 : x = x coordinate of the mouse event
974 * 3 : y = y coordinate of the mouse event
978 *********************************************************************/
979 void OnLogRButtonUp(int nModifier, int x, int y)
981 HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
984 HMENU hMenuPopup = GetSubMenu(hMenu, 0);
987 #endif /* def WIN_GUI_EDIT */
989 /* Check if there is a selection */
991 SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
992 if (range.cpMin == range.cpMax)
994 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
998 EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1002 /* Check if cursor is over a link */
1003 szURL = LogGetURLUnderCursor();
1007 TCHAR szMenuItemTemplate[1000];
1010 memset(&item, 0, sizeof(item));
1011 item.cbSize = sizeof(item);
1012 item.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
1013 item.fType = MFT_STRING;
1014 item.fState = MFS_ENABLED;
1015 item.wID = ID_NEW_BLOCKER;
1017 /* Put the item into the menu */
1018 memset(szMenuItemTemplate, 0, sizeof(szMenuItemTemplate));
1019 LoadString(g_hInstance, IDS_NEW_BLOCKER, szMenuItemTemplate, sizeof(szMenuItemTemplate) / sizeof(szMenuItemTemplate[0]));
1021 szMenuItem = (char *)malloc(strlen(szMenuItemTemplate) + strlen(szURL) + 1);
1022 sprintf(szMenuItem, szMenuItemTemplate, szURL);
1024 item.dwTypeData = szMenuItem;
1025 item.cch = strlen(szMenuItem);
1027 InsertMenuItem(hMenuPopup, 1, TRUE, &item);
1029 SetDefaultRule(szURL);
1033 #endif /* def WIN_GUI_EDIT */
1035 /* Display the popup */
1036 TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1043 /*********************************************************************
1045 * Function : OnLogCommand
1047 * Description : Handler for WM_COMMAND messages.
1050 * 1 : nCommand = the command portion of the menu selection event
1054 *********************************************************************/
1055 void OnLogCommand(int nCommand)
1060 ShowLogWindow(TRUE);
1064 PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1068 SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1071 case ID_VIEW_CLEARLOG:
1072 SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1075 case ID_VIEW_LOGMESSAGES:
1076 g_bLogMessages = !g_bLogMessages;
1077 /* SaveLogSettings(); */
1080 case ID_VIEW_MESSAGEHIGHLIGHTING:
1081 g_bHighlightMessages = !g_bHighlightMessages;
1082 /* SaveLogSettings(); */
1085 case ID_VIEW_LIMITBUFFERSIZE:
1086 g_bLimitBufferSize = !g_bLimitBufferSize;
1087 /* SaveLogSettings(); */
1090 case ID_VIEW_ACTIVITYANIMATION:
1091 g_bShowActivityAnimation = !g_bShowActivityAnimation;
1092 /* SaveLogSettings(); */
1096 /* by haroon - change toggle to its opposite value */
1098 g_bToggleIJB = !g_bToggleIJB;
1101 log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1105 log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1110 case ID_TOOLS_EDITJUNKBUSTER:
1111 EditFile(configfile);
1114 case ID_TOOLS_EDITACTIONS:
1115 EditFile(g_actions_file);
1118 case ID_TOOLS_EDITPERLRE:
1119 EditFile(g_re_filterfile);
1123 case ID_TOOLS_EDITTRUST:
1124 EditFile(g_trustfile);
1126 #endif /* def TRUST_FILES */
1129 case ID_NEW_BLOCKER:
1130 ShowRulesDialog(g_hwndLogFrame);
1132 #endif /* def WIN_GUI_EDIT */
1135 ShellExecute(g_hwndLogFrame, "open", "gpl.html", NULL, NULL, SW_SHOWNORMAL);
1139 ShellExecute(g_hwndLogFrame, "open", "ijbfaq.html", NULL, NULL, SW_SHOWNORMAL);
1142 case ID_HELP_MANUAL:
1143 ShellExecute(g_hwndLogFrame, "open", "ijbman.html", NULL, NULL, SW_SHOWNORMAL);
1146 case ID_HELP_STATUS:
1147 ShellExecute(g_hwndLogFrame, "open", "Junkbuster Status.URL", NULL, NULL, SW_SHOWNORMAL);
1150 case ID_HELP_ABOUTJUNKBUSTER:
1151 MessageBox(g_hwndLogFrame, win32_blurb, "Junkbuster Information", MB_OK);
1162 /*********************************************************************
1164 * Function : OnLogInitMenu
1166 * Description : Handler for WM_INITMENU messages. Enable, disable,
1167 * check, and/or uncheck menu options as apropos.
1170 * 1 : hmenu = handle to menu to "make current"
1174 *********************************************************************/
1175 void OnLogInitMenu(HMENU hmenu)
1177 /* Only enable editors if there is a file to edit */
1178 EnableMenuItem(hmenu, ID_TOOLS_EDITACTIONS, MF_BYCOMMAND | (g_actions_file ? MF_ENABLED : MF_GRAYED));
1179 EnableMenuItem(hmenu, ID_TOOLS_EDITPERLRE, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1181 EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1182 #endif /* def TRUST_FILES */
1184 /* Check/uncheck options */
1185 CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1186 CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1187 CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1188 CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1190 /* by haroon - menu item for Enable toggle on/off */
1191 CheckMenuItem(hmenu, ID_TOGGLE_IJB, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1197 /*********************************************************************
1199 * Function : OnLogTimer
1201 * Description : Handler for WM_TIMER messages.
1204 * 1 : nTimer = timer id (animation start/stop or clip buffer)
1208 *********************************************************************/
1209 void OnLogTimer(int nTimer)
1214 TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1217 case TIMER_ANIMSTOP_ID:
1219 TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1220 KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1221 KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1224 case TIMER_CLIPBUFFER_ID:
1225 case TIMER_CLIPBUFFER_FORCE_ID:
1227 g_bClipPending = FALSE;
1228 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1229 KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1240 /*********************************************************************
1242 * Function : LogRichEditProc
1244 * Description : Window subclass routine handles some events for the rich edit control.
1247 * 1 : hwnd = window handle of the rich edit control
1248 * 2 : uMsg = message number
1249 * 3 : wParam = first param for this message
1250 * 4 : lParam = next param for this message
1252 * Returns : Appropriate M$ window message handler codes.
1254 *********************************************************************/
1255 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1262 pt.x = LOWORD(lParam);
1263 pt.y = HIWORD(lParam);
1264 ClientToScreen(hwnd, &pt);
1265 OnLogRButtonUp(wParam, pt.x, pt.y);
1269 return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1274 /*********************************************************************
1276 * Function : LogWindowProc
1278 * Description : Windows call back routine handles events on the log window.
1281 * 1 : hwnd = handle of the logging window
1282 * 2 : uMsg = message number
1283 * 3 : wParam = first param for this message
1284 * 4 : lParam = next param for this message
1286 * Returns : Appropriate M$ window message handler codes.
1288 *********************************************************************/
1289 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1297 /* This is the end - beautiful friend - the end */
1298 DestroyWindow(g_hwndLogBox);
1299 DestroyWindow(g_hwndLogFrame);
1308 /* Resize the logging window to fit the new frame */
1312 GetClientRect(g_hwndLogFrame, &rc);
1313 SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1318 OnLogInitMenu((HMENU) wParam);
1326 OnLogCommand(LOWORD(wParam));
1333 if (g_bCloseHidesWindow)
1335 ShowLogWindow(FALSE);
1340 ShowLogWindow(FALSE);
1346 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1350 #endif /* ndef _WIN_CONSOLE - entire file */