more additions.
[privoxy.git] / w32log.c
1 const char w32log_rcs[] = "$Id: w32log.c,v 1.20 2002/03/24 12:03:47 jongfoster Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/w32log.c,v $
5  *
6  * Purpose     :  Functions for creating and destroying the log window,
7  *                ouputting strings, processing messages and so on.
8  *
9  * Copyright   :  Written by and Copyright (C) 2001-2002 members of
10  *                the Privoxy team.  http://privoxy.org/
11  *
12  *                Written by and Copyright (C) 1999 Adam Lock
13  *                <locka@iol.ie>
14  *
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.
20  *
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.
26  *
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.
32  *
33  * Revisions   :
34  *    $Log: w32log.c,v $
35  *    Revision 1.20  2002/03/24 12:03:47  jongfoster
36  *    Name change
37  *
38  *    Revision 1.19  2002/01/17 21:04:17  jongfoster
39  *    Replacing hard references to the URL of the config interface
40  *    with #defines from project.h
41  *
42  *    Revision 1.18  2001/11/30 23:37:24  jongfoster
43  *    Renaming the Win32 config file to config.txt - this is almost the
44  *    same as the corresponding UNIX name "config"
45  *
46  *    Revision 1.17  2001/11/16 00:46:31  jongfoster
47  *    Fixing compiler warnings
48  *
49  *    Revision 1.16  2001/08/01 19:58:12  jongfoster
50  *    Fixing documentation filenames in help menu, and making status
51  *    option work without needing the "Junkbuster Status.URL" file.
52  *
53  *    Revision 1.15  2001/07/30 22:08:36  jongfoster
54  *    Tidying up #defines:
55  *    - All feature #defines are now of the form FEATURE_xxx
56  *    - Permanently turned off WIN_GUI_EDIT
57  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
58  *
59  *    Revision 1.14  2001/07/29 18:47:05  jongfoster
60  *    Adding missing #include "loadcfg.h"
61  *
62  *    Revision 1.13  2001/07/19 19:15:14  haroon
63  *    - Added a FIXME for EditFile but didn't fix :-)
64  *
65  *    Revision 1.12  2001/07/13 14:04:59  oes
66  *    Removed all #ifdef PCRS
67  *
68  *    Revision 1.11  2001/06/07 23:08:12  jongfoster
69  *    Forward and ACL edit options removed.
70  *
71  *    Revision 1.10  2001/05/31 21:37:11  jongfoster
72  *    GUI changes to rename "permissions file" to "actions file".
73  *
74  *    Revision 1.9  2001/05/31 17:33:13  oes
75  *
76  *    CRLF -> LF
77  *
78  *    Revision 1.8  2001/05/29 09:50:24  jongfoster
79  *    Unified blocklist/imagelist/permissionslist.
80  *    File format is still under discussion, but the internal changes
81  *    are (mostly) done.
82  *
83  *    Also modified interceptor behaviour:
84  *    - We now intercept all URLs beginning with one of the following
85  *      prefixes (and *only* these prefixes):
86  *        * http://i.j.b/
87  *        * http://ijbswa.sf.net/config/
88  *        * http://ijbswa.sourceforge.net/config/
89  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
90  *    - Internal changes so that intercepted and fast redirect pages
91  *      are not replaced with an image.
92  *    - Interceptors now have the option to send a binary page direct
93  *      to the client. (i.e. ijb-send-banner uses this)
94  *    - Implemented show-url-info interceptor.  (Which is why I needed
95  *      the above interceptors changes - a typical URL is
96  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
97  *      The previous mechanism would not have intercepted that, and
98  *      if it had been intercepted then it then it would have replaced
99  *      it with an image.)
100  *
101  *    Revision 1.7  2001/05/26 01:26:34  jongfoster
102  *    New #define, WIN_GUI_EDIT, enables the (embryonic) Win32 GUI editor.
103  *    This #define cannot be set from ./configure - there's no point, it
104  *    doesn't work yet.  See feature request # 425722
105  *
106  *    Revision 1.6  2001/05/26 00:31:30  jongfoster
107  *    Fixing compiler warning about comparing signed/unsigned.
108  *
109  *    Revision 1.5  2001/05/26 00:28:36  jongfoster
110  *    Automatic reloading of config file.
111  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
112  *    Most of the global variables have been moved to a new
113  *    struct configuration_spec, accessed through csp->config->globalname
114  *    Most of the globals remaining are used by the Win32 GUI.
115  *
116  *    Revision 1.4  2001/05/22 18:56:28  oes
117  *    CRLF -> LF
118  *
119  *    Revision 1.3  2001/05/20 15:07:54  jongfoster
120  *    File is now ignored if _WIN_CONSOLE is defined.
121  *
122  *    Revision 1.2  2001/05/20 01:21:20  jongfoster
123  *    Version 2.9.4 checkin.
124  *    - Merged popupfile and cookiefile, and added control over PCRS
125  *      filtering, in new "permissionsfile".
126  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
127  *      file error you now get a message box (in the Win32 GUI) rather
128  *      than the program exiting with no explanation.
129  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
130  *      skipping.
131  *    - Removed tabs from "config"
132  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
133  *    - Bumped up version number.
134  *
135  *    Revision 1.1.1.1  2001/05/15 13:59:07  oes
136  *    Initial import of version 2.9.3 source tree
137  *
138  *
139  *********************************************************************/
140 \f
141
142 #include "config.h"
143
144 #include <assert.h>
145 #include <stdio.h>
146
147 #include <windows.h>
148 #include <richedit.h>
149
150 #include "project.h"
151 #include "w32log.h"
152 #include "w32taskbar.h"
153 #include "win32.h"
154 #include "w32res.h"
155 #include "jcc.h"
156 #include "miscutil.h"
157 #include "errlog.h"
158 #include "loadcfg.h"
159
160 const char w32res_h_rcs[] = W32RES_H_VERSION;
161
162 #ifdef __MINGW32__
163 #include "cygwin.h"
164 const char cygwin_h_rcs[] = CYGWIN_H_VERSION;
165 #endif
166
167 const char w32log_h_rcs[] = W32LOG_H_VERSION;
168
169 #ifndef _WIN_CONSOLE /* entire file */
170
171 /*
172  * Timers and the various durations
173  */
174 #define TIMER_ANIM_ID               1
175 #define TIMER_ANIM_TIME             100
176 #define TIMER_ANIMSTOP_ID           2
177 #define TIMER_ANIMSTOP_TIME         1000
178 #define TIMER_CLIPBUFFER_ID         3
179 #define TIMER_CLIPBUFFER_TIME       1000
180 #define TIMER_CLIPBUFFER_FORCE_ID   4
181 #define TIMER_CLIPBUFFER_FORCE_TIME 5000
182
183 /*
184  * Styles of text that can be output
185  */
186 #define STYLE_NONE      0
187 #define STYLE_HIGHLIGHT 1
188 #define STYLE_LINK      2
189 #define STYLE_HEADER    3
190
191 /*
192  * Number of frames of animation in tray activity sequence
193  */
194 #define ANIM_FRAMES 8
195
196 #define DEFAULT_MAX_BUFFER_LINES    200
197 #define DEFAULT_LOG_FONT_NAME       "MS Sans Serif"
198 #define DEFAULT_LOG_FONT_SIZE       8
199
200 /*
201  * These values affect the way the log window behaves, they should be read
202  * from a file but for the moment, they are hardcoded here. Some options are
203  * configurable through the UI.
204  */
205
206 /* Indicates whether task bar shows activity animation */
207 BOOL g_bShowActivityAnimation = 1;
208
209 /* Indicates if the log window appears on the task bar */
210 BOOL g_bShowOnTaskBar = 0;
211
212 /* Indicates whether closing the log window really just hides it */
213 BOOL g_bCloseHidesWindow = 1;
214
215 /* Indicates if messages are logged at all */
216 BOOL g_bLogMessages = 1;
217
218 /* Indicates whether log messages are highlighted */
219 BOOL g_bHighlightMessages = 1;
220
221 /* Indicates if buffer is limited in size */
222 BOOL g_bLimitBufferSize = 1;
223
224 /* Maximum number of lines allowed in buffer when limited */
225 int g_nMaxBufferLines = DEFAULT_MAX_BUFFER_LINES;
226
227 /* Font to use */
228 char g_szFontFaceName[255] = DEFAULT_LOG_FONT_NAME;
229
230 /* Size of font to use */
231 int g_nFontSize = DEFAULT_LOG_FONT_SIZE;
232
233
234 /* FIXME: this is a kludge */
235
236 const char * g_actions_file = NULL;
237 const char * g_re_filterfile = NULL;
238 #ifdef FEATURE_TRUST
239 const char * g_trustfile = NULL;
240 #endif /* def FEATURE_TRUST */
241
242 /* FIXME: end kludge */
243
244
245 #ifdef REGEX
246 /* Regular expression for detected URLs */
247 #define RE_URL "http:[^ \n\r]*"
248
249 /*
250  * Regular expressions that are used to perform highlight in the log window
251  */
252 static struct _Pattern
253 {
254    const char *str;
255    int style;
256    regex_t buffer;
257 } patterns_to_highlight[] =
258 {
259    /* url headers */
260    { RE_URL,                STYLE_LINK },
261 /* { "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[^ \n\r]*", STYLE_LINK }, */
262    /* interesting text to highlight */
263    { "crunch!",           STYLE_HIGHLIGHT },
264    /* http headers */
265    { "referer:",            STYLE_HEADER },
266    { "proxy-connection:",   STYLE_HEADER },
267    { "proxy-agent:",        STYLE_HEADER },
268    { "user-agent:",         STYLE_HEADER },
269    { "host:",               STYLE_HEADER },
270    { "accept:",             STYLE_HEADER },
271    { "accept-encoding:",    STYLE_HEADER },
272    { "accept-language:",    STYLE_HEADER },
273    { "accept-charset:",     STYLE_HEADER },
274    { "accept-ranges:",      STYLE_HEADER },
275    { "date:",               STYLE_HEADER },
276    { "cache-control:",      STYLE_HEADER },
277    { "cache-last-checked:", STYLE_HEADER },
278    { "connection:",         STYLE_HEADER },
279    { "content-type",        STYLE_HEADER },
280    { "content-length",      STYLE_HEADER },
281    { "cookie",              STYLE_HEADER },
282    { "last-modified:",      STYLE_HEADER },
283    { "pragma:",             STYLE_HEADER },
284    { "server:",             STYLE_HEADER },
285    { "etag:",               STYLE_HEADER },
286    { "expires:",            STYLE_HEADER },
287    { "warning:",            STYLE_HEADER },
288    /* this is the terminator statement - do not delete! */
289    { NULL,                  STYLE_NONE }
290 };
291 #endif /* def REGEX */
292
293
294 /*
295  * Public variables
296  */
297 HWND g_hwndLogFrame;
298
299 /*
300  * Private variables
301  */
302 static CRITICAL_SECTION g_criticalsection;
303 static HWND g_hwndTray;
304 static HWND g_hwndLogBox;
305 static WNDPROC g_fnLogBox;
306 static HICON g_hiconAnim[ANIM_FRAMES];
307 static HICON g_hiconIdle;
308 static HICON g_hiconApp;
309 static int g_nAnimFrame;
310 static BOOL g_bClipPending = FALSE;
311 static int g_nRichEditVersion = 0;
312
313 /*
314  * Private functions
315  */
316 static HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow);
317 static HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance);
318 static LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
319 static LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
320 static LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
321 static BOOL InitRichEdit(void);
322 static void LogClipBuffer(void);
323 static void LogCreatePatternMatchingBuffers(void);
324 static void LogDestroyPatternMatchingBuffers(void);
325 static int LogPutStringNoMatch(const char *pszText, int style);
326
327
328 /*********************************************************************
329  *
330  * Function    :  InitLogWindow
331  *
332  * Description :  Initialise the log window.
333  *
334  * Parameters  :  None
335  *
336  * Returns     :  Always TRUE (there should be error checking on the resources).
337  *
338  *********************************************************************/
339 BOOL InitLogWindow(void)
340 {
341    int i;
342
343    /* Load the icons */
344    g_hiconIdle = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IDLE));
345    for (i = 0; i < ANIM_FRAMES; i++)
346    {
347       g_hiconAnim[i] = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ANIMATED1 + i));
348    }
349    g_hiconApp = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON));
350
351    /* Create the user interface */
352    g_hwndLogFrame = CreateLogWindow(g_hInstance, g_nCmdShow);
353    g_hwndTray = CreateTrayWindow(g_hInstance);
354    TrayAddIcon(g_hwndTray, 1, g_hiconApp, "Privoxy");
355
356    /* Create pattern matching buffers (for highlighting */
357    LogCreatePatternMatchingBuffers();
358
359    /* Create a critical section to protect multi-threaded access to certain things */
360    InitializeCriticalSection(&g_criticalsection);
361
362    return TRUE;
363
364 }
365
366
367 /*********************************************************************
368  *
369  * Function    :  TermLogWindow
370  *
371  * Description :  Cleanup the logwindow.
372  *
373  * Parameters  :  None
374  *
375  * Returns     :  N/A
376  *
377  *********************************************************************/
378 void TermLogWindow(void)
379 {
380    int i;
381
382    LogDestroyPatternMatchingBuffers();
383
384    TrayDeleteIcon(g_hwndTray, 1);
385    DeleteObject(g_hiconApp);
386    DeleteObject(g_hiconIdle);
387    for (i = 0; i < ANIM_FRAMES; i++)
388    {
389       DeleteObject(g_hiconAnim[i]);
390    }
391
392 }
393
394
395 /*********************************************************************
396  *
397  * Function    :  LogCreatePatternMatchingBuffers
398  *
399  * Description :  Compile the pattern matching buffers.
400  *
401  * Parameters  :  None
402  *
403  * Returns     :  N/A
404  *
405  *********************************************************************/
406 void LogCreatePatternMatchingBuffers(void)
407 {
408 #ifdef REGEX
409    int i;
410    for (i = 0; patterns_to_highlight[i].str != NULL; i++)
411    {
412       regcomp(&patterns_to_highlight[i].buffer, patterns_to_highlight[i].str, REG_ICASE);
413    }
414 #endif
415
416 }
417
418
419 /*********************************************************************
420  *
421  * Function    :  LogDestroyPatternMatchingBuffers
422  *
423  * Description :  Free up the pattern matching buffers.
424  *
425  * Parameters  :  None
426  *
427  * Returns     :  N/A
428  *
429  *********************************************************************/
430 void LogDestroyPatternMatchingBuffers(void)
431 {
432 #ifdef REGEX
433    int i;
434    for (i = 0; patterns_to_highlight[i].str != NULL; i++)
435    {
436       regfree(&patterns_to_highlight[i].buffer);
437    }
438 #endif
439
440 }
441
442
443 /*********************************************************************
444  *
445  * Function    :  LogGetURLUnderCursor
446  *
447  * Description :  Returns the URL from under the cursor (remember to free it!).
448  *
449  * Parameters  :  None
450  *
451  * Returns     :  NULL or a pointer to an URL string.
452  *
453  *********************************************************************/
454 char *LogGetURLUnderCursor(void)
455 {
456    char *szResult = NULL;
457 #ifdef REGEX
458    regex_t re;
459    POINT ptCursor;
460    POINTL ptl;
461    DWORD nPos;
462    DWORD nWordStart = 0;
463    DWORD nWordEnd = 0;
464
465    regcomp(&re, RE_URL, REG_ICASE);
466
467    /* Get the position of the cursor over the text window */
468    GetCursorPos(&ptCursor);
469    ScreenToClient(g_hwndLogBox, &ptCursor);
470    ptl.x = ptCursor.x;
471    ptl.y = ptCursor.y;
472
473    /* Search backwards and fowards to obtain the word that is highlighted */
474    nPos = LOWORD(SendMessage(g_hwndLogBox, EM_CHARFROMPOS, 0, (LPARAM) &ptl));
475    nWordStart = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_LEFT, nPos);
476    nWordEnd = SendMessage(g_hwndLogBox, EM_FINDWORDBREAK, WB_RIGHTBREAK, nPos);
477
478    /* Compare the string to the pattern */
479    if (nWordEnd > nWordStart)
480    {
481       TEXTRANGE range;
482       regmatch_t match;
483
484       range.chrg.cpMin = nWordStart;
485       range.chrg.cpMax = nWordEnd;
486       range.lpstrText = (LPSTR)zalloc(nWordEnd - nWordStart + 1);
487       SendMessage(g_hwndLogBox, EM_GETTEXTRANGE, 0, (LPARAM) &range);
488
489       if (regexec(&re, range.lpstrText, 1, &match, 0) == 0)
490       {
491          szResult = range.lpstrText;
492       }
493       else
494       {
495          free(range.lpstrText);
496       }
497
498       regfree(&re);
499    }
500 #endif
501    return szResult;
502
503 }
504
505
506 /*********************************************************************
507  *
508  * Function    :  LogPutString
509  *
510  * Description :  Inserts text into the logging window.  This is really
511  *                a REGEXP aware wrapper function to `LogPutStringNoMatch'.
512  *
513  * Parameters  :
514  *          1  :  pszText = pointer to string going to the log window
515  *
516  * Returns     :  1 => success, else the return code from `LogPutStringNoMatch'.
517  *                FIXME: this is backwards to the rest of IJB and to common
518  *                programming practice.  Please use 0 => success instead.
519  *
520  *********************************************************************/
521 int LogPutString(const char *pszText)
522 {
523 #ifdef REGEX
524    int i;
525 #endif
526    int result = 0;
527
528    if (pszText == NULL || strlen(pszText) == 0)
529    {
530       return 1;
531    }
532
533    if (!g_bLogMessages)
534    {
535       return 1;
536    }
537
538    /* Critical section stops multiple threads doing nasty interactions that
539     * foul up the highlighting and output.
540     */
541    EnterCriticalSection(&g_criticalsection);
542
543 #ifdef REGEX
544    if (g_bHighlightMessages)
545    {
546       regmatch_t match;
547
548       /* First things first, regexp scan for various things that we would like highlighted */
549       for (i = 0; patterns_to_highlight[i].str != NULL; i++)
550       {
551          if (regexec(&patterns_to_highlight[i].buffer, pszText, 1, &match, 0) == 0)
552          {
553             char *pszBefore = NULL;
554             char *pszMatch = NULL;
555             char *pszAfter = NULL;
556             int nMatchSize;
557
558             /* Split the string up into pieces representing the strings, before
559                at and after the matching pattern
560              */
561             if (match.rm_so > 0)
562             {
563                pszBefore = (char *)malloc((match.rm_so + 1) * sizeof(char));
564                memset(pszBefore, 0, (match.rm_so + 1) * sizeof(char));
565                strncpy(pszBefore, pszText, match.rm_so);
566             }
567             if (match.rm_eo < (regoff_t)strlen(pszText))
568             {
569                pszAfter = strdup(&pszText[match.rm_eo]);
570             }
571             nMatchSize = match.rm_eo - match.rm_so;
572             pszMatch = (char *)malloc(nMatchSize + 1);
573             strncpy(pszMatch, &pszText[match.rm_so], nMatchSize);
574             pszMatch[nMatchSize] = '\0';
575
576             /* Recursively call LogPutString */
577             if (pszBefore)
578             {
579                LogPutString(pszBefore);
580                free(pszBefore);
581             }
582             if (pszMatch)
583             {
584                LogPutStringNoMatch(pszMatch, patterns_to_highlight[i].style);
585                free(pszMatch);
586             }
587             if (pszAfter)
588             {
589                LogPutString(pszAfter);
590                free(pszAfter);
591             }
592
593             result = 1;
594             goto end;
595          }
596       }
597    }
598 #endif
599
600    result = LogPutStringNoMatch(pszText, STYLE_NONE);
601
602 #ifdef REGEX
603 end:
604 #endif
605    LeaveCriticalSection(&g_criticalsection);
606
607    return result;
608
609 }
610
611
612 /*********************************************************************
613  *
614  * Function    :  LogPutStringNoMatch
615  *
616  * Description :  Puts a string into the logging window.
617  *
618  * Parameters  :
619  *          1  :  pszText = pointer to string going to the log window
620  *          2  :  style = STYLE_NONE, STYLE_HEADER, STYLE_HIGHLIGHT, or STYLE_LINK
621  *
622  * Returns     :  Always 1 => success.
623  *                FIXME: this is backwards to the rest of IJB and to common
624  *                programming practice.  Please use 0 => success instead.
625  *
626  *********************************************************************/
627 int LogPutStringNoMatch(const char *pszText, int style)
628 {
629    CHARRANGE range;
630    CHARFORMAT format;
631    int nTextLength;
632
633    assert(g_hwndLogBox);
634    if (g_hwndLogBox == NULL)
635    {
636       return 1;
637    }
638
639    /* TODO preserve existing selection */
640
641    /* Go to the end of the text */
642    nTextLength = GetWindowTextLength(g_hwndLogBox);
643    range.cpMin = nTextLength;
644    range.cpMax = nTextLength;
645    SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
646
647    /* Apply a formatting style */
648    memset(&format, 0, sizeof(format));
649    format.cbSize = sizeof(format);
650    format.dwMask = CFM_BOLD | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_ITALIC | CFM_COLOR | CFM_FACE | CFM_SIZE;
651    format.yHeight = (g_nFontSize * 1440) / 72;
652    strcpy(format.szFaceName, g_szFontFaceName);
653    if (style == STYLE_NONE)
654    {
655       /* DO NOTHING */
656       format.dwEffects |= CFE_AUTOCOLOR;
657    }
658    else if (style == STYLE_HEADER)
659    {
660       format.dwEffects |= CFE_AUTOCOLOR | CFE_ITALIC;
661    }
662    else if (style == STYLE_HIGHLIGHT)
663    {
664       format.dwEffects |= CFE_AUTOCOLOR | CFE_BOLD;
665    }
666    else if (style == STYLE_LINK)
667    {
668       format.dwEffects |= CFE_UNDERLINE;
669       format.crTextColor = RGB(0, 0, 255);
670    }
671    SendMessage(g_hwndLogBox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
672
673    /* Append text to the end */
674    SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) pszText);
675
676    /* TODO Restore the old selection */
677
678    /* Purge buffer */
679    if (strchr(pszText, '\n') != NULL)
680    {
681       SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID, TIMER_CLIPBUFFER_TIME, NULL);
682       if (!g_bClipPending)
683       {
684          /* Set the force clip timer going. This timer ensures clipping is done
685             intermittently even when there is a sustained burst of logging
686          */
687          SetTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID, TIMER_CLIPBUFFER_FORCE_TIME, NULL);
688       }
689       g_bClipPending = TRUE;
690    }
691
692    return 1;
693
694 }
695
696
697 /*********************************************************************
698  *
699  * Function    :  LogShowActivity
700  *
701  * Description :  Start the spinner.
702  *
703  * Parameters  :  None
704  *
705  * Returns     :  N/A
706  *
707  *********************************************************************/
708 void LogShowActivity(void)
709 {
710    /* Start some activity timers */
711    if (g_bShowActivityAnimation)
712    {
713       SetTimer(g_hwndLogFrame, TIMER_ANIM_ID, TIMER_ANIM_TIME, NULL);
714       SetTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID, TIMER_ANIMSTOP_TIME, NULL);
715    }
716
717 }
718
719
720 /*********************************************************************
721  *
722  * Function    :  LogClipBuffer
723  *
724  * Description :  Prunes old lines from the log.
725  *
726  * Parameters  :  None
727  *
728  * Returns     :  N/A
729  *
730  *********************************************************************/
731 void LogClipBuffer(void)
732 {
733    int nLines = SendMessage(g_hwndLogBox, EM_GETLINECOUNT, 0, 0);
734    if (g_bLimitBufferSize && nLines > g_nMaxBufferLines)
735    {
736       /* Compute the range representing the lines to be deleted */
737       LONG nLastLineToDelete = nLines - g_nMaxBufferLines;
738       LONG nLastChar = SendMessage(g_hwndLogBox, EM_LINEINDEX, nLastLineToDelete, 0);
739       CHARRANGE range;
740       range.cpMin = 0;
741       range.cpMax = nLastChar;
742
743       /* TODO get current selection */
744
745       /* TODO adjust and clip old selection against range to be deleted */
746
747       /* Select range and erase it (turning off autoscroll to prevent
748          nasty scrolling) */
749       SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
750       SendMessage(g_hwndLogBox, EM_EXSETSEL, 0, (LPARAM) &range);
751       SendMessage(g_hwndLogBox, EM_REPLACESEL, FALSE, (LPARAM) "");
752       SendMessage(g_hwndLogBox, EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL);
753
754       /* Restore old selection */
755    }
756
757 }
758
759
760 /*********************************************************************
761  *
762  * Function    :  CreateHiddenLogOwnerWindow
763  *
764  * Description :  Creates a hidden owner window that stops the log
765  *                window appearing in the task bar.
766  *
767  * Parameters  :
768  *          1  :  hInstance = application's instance handle
769  *
770  * Returns     :  Handle to newly created window.
771  *
772  *********************************************************************/
773 HWND CreateHiddenLogOwnerWindow(HINSTANCE hInstance)
774 {
775    static const char *szWndName = "PrivoxyLogOwner";
776    WNDCLASS wc;
777    HWND hwnd;
778
779    wc.style          = 0;
780    wc.lpfnWndProc    = LogOwnerWindowProc;
781    wc.cbClsExtra     = 0;
782    wc.cbWndExtra     = 0;
783    wc.hInstance      = hInstance;
784    wc.hIcon          = 0;
785    wc.hCursor        = 0;
786    wc.hbrBackground  = 0;
787    wc.lpszMenuName   = 0;
788    wc.lpszClassName  = szWndName;
789
790    RegisterClass(&wc);
791
792    hwnd = CreateWindow(szWndName, szWndName,
793       WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
794       CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
795
796    return hwnd;
797
798 }
799
800
801 /*********************************************************************
802  *
803  * Function    :  LogOwnerWindowProc
804  *
805  * Description :  Dummy procedure that does nothing special.
806  *
807  * Parameters  :
808  *          1  :  hwnd = window handle
809  *          2  :  uMsg = message number
810  *          3  :  wParam = first param for this message
811  *          4  :  lParam = next param for this message
812  *
813  * Returns     :  Same as `DefWindowProc'.
814  *
815  *********************************************************************/
816 LRESULT CALLBACK LogOwnerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
817 {
818    return DefWindowProc(hwnd, uMsg, wParam, lParam);
819
820 }
821
822
823 /*********************************************************************
824  *
825  * Function    :  CreateLogWindow
826  *
827  * Description :  Create the logging window.
828  *
829  * Parameters  :
830  *          1  :  hInstance = application's instance handle
831  *          2  :  nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
832  *
833  * Returns     :  Handle to newly created window.
834  *
835  *********************************************************************/
836 HWND CreateLogWindow(HINSTANCE hInstance, int nCmdShow)
837 {
838    static const char *szWndName = "PrivoxyLogWindow";
839    static const char *szWndTitle = "Privoxy";
840
841    HWND hwnd = NULL;
842    HWND hwndOwner = (g_bShowOnTaskBar) ? NULL : CreateHiddenLogOwnerWindow(hInstance);
843    RECT rcClient;
844    WNDCLASSEX wc;
845
846    memset(&wc, 0, sizeof(wc));
847    wc.cbSize         = sizeof(wc);
848    wc.style          = CS_DBLCLKS;
849    wc.lpfnWndProc    = LogWindowProc;
850    wc.cbClsExtra     = 0;
851    wc.cbWndExtra     = 0;
852    wc.hInstance      = hInstance;
853    wc.hIcon          = g_hiconApp;
854    wc.hCursor        = 0;
855    wc.hbrBackground  = 0;
856    wc.lpszMenuName   = MAKEINTRESOURCE(IDR_LOGVIEW);
857    wc.lpszClassName  = szWndName;
858    wc.hbrBackground  = GetStockObject(WHITE_BRUSH);
859    RegisterClassEx(&wc);
860
861    hwnd = CreateWindowEx(WS_EX_APPWINDOW, szWndName, szWndTitle,
862       WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
863       CW_USEDEFAULT, hwndOwner, NULL, hInstance, NULL);
864
865    /* Now create a child list box */
866    GetClientRect(hwnd, &rcClient);
867
868    /* Create a rich edit control */
869    InitRichEdit();
870    g_hwndLogBox = CreateWindowEx(0, (g_nRichEditVersion == 0x0100) ? "RichEdit" : RICHEDIT_CLASS, "",
871       ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
872       rcClient.left, rcClient.top, rcClient.right, rcClient.bottom,
873       hwnd, NULL, hInstance, NULL);
874 /* SendMessage(g_hwndLogBox, EM_SETWORDWRAPMODE, 0, 0); */
875
876    /* Subclass the control to catch certain messages */
877    g_fnLogBox = (WNDPROC) GetWindowLong(g_hwndLogBox, GWL_WNDPROC);
878    SetWindowLong(g_hwndLogBox, GWL_WNDPROC, (LONG) LogRichEditProc);
879
880    /* Minimizing looks stupid when the log window is not on the task bar, so hide instead */
881    if (!g_bShowOnTaskBar &&
882          (nCmdShow == SW_SHOWMINIMIZED ||
883           nCmdShow == SW_MINIMIZE ||
884           nCmdShow == SW_SHOWMINNOACTIVE))
885    {
886       nCmdShow = SW_HIDE;
887    }
888
889    ShowWindow(hwnd, nCmdShow);
890    UpdateWindow(hwnd);
891
892    GetClientRect(g_hwndLogFrame, &rcClient);
893    SetWindowPos(g_hwndLogBox, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_NOZORDER);
894
895    return hwnd;
896
897 }
898
899
900 /*********************************************************************
901  *
902  * Function    :  InitRichEdit
903  *
904  * Description :  Initialise the rich edit control library.
905  *
906  * Parameters  :  None
907  *
908  * Returns     :  TRUE => success, FALSE => failure.
909  *                FIXME: this is backwards to the rest of IJB and to common
910  *                programming practice.  Please use 0 => success instead.
911  *
912  *********************************************************************/
913 BOOL InitRichEdit(void)
914 {
915    static HINSTANCE hInstRichEdit;
916    if (hInstRichEdit == NULL)
917    {
918       g_nRichEditVersion = 0;
919       hInstRichEdit = LoadLibraryA("RICHED20.DLL");
920       if (hInstRichEdit)
921       {
922          g_nRichEditVersion = _RICHEDIT_VER;
923       }
924       else
925       {
926          hInstRichEdit = LoadLibraryA("RICHED32.DLL");
927          if (hInstRichEdit)
928          {
929             g_nRichEditVersion = 0x0100;
930          }
931       }
932    }
933    return (hInstRichEdit != NULL) ? TRUE : FALSE;
934
935 }
936
937
938 /*********************************************************************
939  *
940  * Function    :  ShowLogWindow
941  *
942  * Description :  Shows or hides the log window.  We will also raise the
943  *                window on a show command in case it is buried.
944  *
945  * Parameters  :
946  *          1  :  bShow = TRUE to show, FALSE to mimize/hide
947  *
948  * Returns     :  N/A
949  *
950  *********************************************************************/
951 void ShowLogWindow(BOOL bShow)
952 {
953    if (bShow)
954    {
955       SetForegroundWindow(g_hwndLogFrame);
956       SetWindowPos(g_hwndLogFrame, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
957    }
958    else if (g_bShowOnTaskBar)
959    {
960       ShowWindow(g_hwndLogFrame, SW_MINIMIZE);
961    }
962    else
963    {
964       ShowWindow(g_hwndLogFrame, SW_HIDE);
965    }
966
967 }
968
969
970 /*********************************************************************
971  *
972  * Function    :  EditFile
973  *
974  * Description :  Opens the specified setting file for editing.
975  * FIXME: What if the file has no associated application. Check for return values
976 *        from ShellExecute??
977  *
978  * Parameters  :
979  *          1  :  filename = filename from the config (aka config.txt) file.
980  *
981  * Returns     :  N/A
982  *
983  *********************************************************************/
984 void EditFile(const char *filename)
985 {
986    if (filename)
987    {
988       ShellExecute(g_hwndLogFrame, "open", filename, NULL, NULL, SW_SHOWNORMAL);
989    }
990
991 }
992
993
994 /*--------------------------------------------------------------------------*/
995 /* Windows message handlers                                                 */
996 /*--------------------------------------------------------------------------*/
997
998
999 /*********************************************************************
1000  *
1001  * Function    :  OnLogRButtonUp
1002  *
1003  * Description :  Handler for WM_RBUTTONUP messages.
1004  *
1005  * Parameters  :
1006  *          1  :  nModifier = wParam from mouse message (unused)
1007  *          2  :  x = x coordinate of the mouse event
1008  *          3  :  y = y coordinate of the mouse event
1009  *
1010  * Returns     :  N/A
1011  *
1012  *********************************************************************/
1013 void OnLogRButtonUp(int nModifier, int x, int y)
1014 {
1015    HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_POPUP_SELECTION));
1016    if (hMenu != NULL)
1017    {
1018       HMENU hMenuPopup = GetSubMenu(hMenu, 0);
1019
1020       /* Check if there is a selection */
1021       CHARRANGE range;
1022       SendMessage(g_hwndLogBox, EM_EXGETSEL, 0, (LPARAM) &range);
1023       if (range.cpMin == range.cpMax)
1024       {
1025          EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
1026       }
1027       else
1028       {
1029          EnableMenuItem(hMenuPopup, ID_EDIT_COPY, MF_BYCOMMAND | MF_ENABLED);
1030       }
1031
1032       /* Display the popup */
1033       TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x, y, 0, g_hwndLogFrame, NULL);
1034       DestroyMenu(hMenu);
1035    }
1036
1037 }
1038
1039
1040 /*********************************************************************
1041  *
1042  * Function    :  OnLogCommand
1043  *
1044  * Description :  Handler for WM_COMMAND messages.
1045  *
1046  * Parameters  :
1047  *          1  :  nCommand = the command portion of the menu selection event
1048  *
1049  * Returns     :  N/A
1050  *
1051  *********************************************************************/
1052 void OnLogCommand(int nCommand)
1053 {
1054    switch (nCommand)
1055    {
1056       case ID_SHOWWINDOW:
1057          ShowLogWindow(TRUE);
1058          break;
1059
1060       case ID_FILE_EXIT:
1061          PostMessage(g_hwndLogFrame, WM_CLOSE, 0, 0);
1062          break;
1063
1064       case ID_EDIT_COPY:
1065          SendMessage(g_hwndLogBox, WM_COPY, 0, 0);
1066          break;
1067
1068       case ID_VIEW_CLEARLOG:
1069          SendMessage(g_hwndLogBox, WM_SETTEXT, 0, (LPARAM) "");
1070          break;
1071
1072       case ID_VIEW_LOGMESSAGES:
1073          g_bLogMessages = !g_bLogMessages;
1074          /* SaveLogSettings(); */
1075          break;
1076
1077       case ID_VIEW_MESSAGEHIGHLIGHTING:
1078          g_bHighlightMessages = !g_bHighlightMessages;
1079          /* SaveLogSettings(); */
1080          break;
1081
1082       case ID_VIEW_LIMITBUFFERSIZE:
1083          g_bLimitBufferSize = !g_bLimitBufferSize;
1084          /* SaveLogSettings(); */
1085          break;
1086
1087       case ID_VIEW_ACTIVITYANIMATION:
1088          g_bShowActivityAnimation = !g_bShowActivityAnimation;
1089          /* SaveLogSettings(); */
1090          break;
1091
1092 #ifdef FEATURE_TOGGLE
1093       /* by haroon - change toggle to its opposite value */
1094       case ID_TOGGLE_ENABLED:
1095          g_bToggleIJB = !g_bToggleIJB;
1096          if (g_bToggleIJB)
1097          {
1098             log_error(LOG_LEVEL_INFO, "Now toggled ON.");
1099          }
1100          else
1101          {
1102             log_error(LOG_LEVEL_INFO, "Now toggled OFF.");
1103          }
1104          break;
1105 #endif /* def FEATURE_TOGGLE */
1106
1107       case ID_TOOLS_EDITCONFIG:
1108          EditFile(configfile);
1109          break;
1110
1111       case ID_TOOLS_EDITACTIONS:
1112          EditFile(g_actions_file);
1113          break;
1114
1115       case ID_TOOLS_EDITFILTERS:
1116          EditFile(g_re_filterfile);
1117          break;
1118
1119 #ifdef FEATURE_TRUST
1120       case ID_TOOLS_EDITTRUST:
1121          EditFile(g_trustfile);
1122          break;
1123 #endif /* def FEATURE_TRUST */
1124
1125       case ID_HELP_GPL:
1126          ShellExecute(g_hwndLogFrame, "open", "doc/gpl.html", NULL, NULL, SW_SHOWNORMAL);
1127          break;
1128
1129       case ID_HELP_FAQ:
1130          ShellExecute(g_hwndLogFrame, "open", "doc/ijbfaq.html", NULL, NULL, SW_SHOWNORMAL);
1131          break;
1132
1133       case ID_HELP_MANUAL:
1134          ShellExecute(g_hwndLogFrame, "open", "doc/ijbman.html", NULL, NULL, SW_SHOWNORMAL);
1135          break;
1136
1137       case ID_HELP_STATUS:
1138          ShellExecute(g_hwndLogFrame, "open", CGI_PREFIX "show-status", NULL, NULL, SW_SHOWNORMAL);
1139          break;
1140
1141       case ID_HELP_ABOUT:
1142          MessageBox(g_hwndLogFrame, win32_blurb, "About Privoxy", MB_OK);
1143          break;
1144
1145       default:
1146          /* DO NOTHING */
1147          break;
1148    }
1149
1150 }
1151
1152
1153 /*********************************************************************
1154  *
1155  * Function    :  OnLogInitMenu
1156  *
1157  * Description :  Handler for WM_INITMENU messages.  Enable, disable,
1158  *                check, and/or uncheck menu options as apropos.
1159  *
1160  * Parameters  :
1161  *          1  :  hmenu = handle to menu to "make current"
1162  *
1163  * Returns     :  N/A
1164  *
1165  *********************************************************************/
1166 void OnLogInitMenu(HMENU hmenu)
1167 {
1168    /* Only enable editors if there is a file to edit */
1169    EnableMenuItem(hmenu, ID_TOOLS_EDITACTIONS, MF_BYCOMMAND | (g_actions_file ? MF_ENABLED : MF_GRAYED));
1170    EnableMenuItem(hmenu, ID_TOOLS_EDITFILTERS, MF_BYCOMMAND | (g_re_filterfile ? MF_ENABLED : MF_GRAYED));
1171 #ifdef FEATURE_TRUST
1172    EnableMenuItem(hmenu, ID_TOOLS_EDITTRUST, MF_BYCOMMAND | (g_trustfile ? MF_ENABLED : MF_GRAYED));
1173 #endif /* def FEATURE_TRUST */
1174
1175    /* Check/uncheck options */
1176    CheckMenuItem(hmenu, ID_VIEW_LOGMESSAGES, MF_BYCOMMAND | (g_bLogMessages ? MF_CHECKED : MF_UNCHECKED));
1177    CheckMenuItem(hmenu, ID_VIEW_MESSAGEHIGHLIGHTING, MF_BYCOMMAND | (g_bHighlightMessages ? MF_CHECKED : MF_UNCHECKED));
1178    CheckMenuItem(hmenu, ID_VIEW_LIMITBUFFERSIZE, MF_BYCOMMAND | (g_bLimitBufferSize ? MF_CHECKED : MF_UNCHECKED));
1179    CheckMenuItem(hmenu, ID_VIEW_ACTIVITYANIMATION, MF_BYCOMMAND | (g_bShowActivityAnimation ? MF_CHECKED : MF_UNCHECKED));
1180 #ifdef FEATURE_TOGGLE
1181    /* by haroon - menu item for Enable toggle on/off */
1182    CheckMenuItem(hmenu, ID_TOGGLE_ENABLED, MF_BYCOMMAND | (g_bToggleIJB ? MF_CHECKED : MF_UNCHECKED));
1183 #endif /* def FEATURE_TOGGLE */
1184
1185 }
1186
1187
1188 /*********************************************************************
1189  *
1190  * Function    :  OnLogTimer
1191  *
1192  * Description :  Handler for WM_TIMER messages.
1193  *
1194  * Parameters  :
1195  *          1  :  nTimer = timer id (animation start/stop or clip buffer)
1196  *
1197  * Returns     :  N/A
1198  *
1199  *********************************************************************/
1200 void OnLogTimer(int nTimer)
1201 {
1202    switch (nTimer)
1203    {
1204       case TIMER_ANIM_ID:
1205          TraySetIcon(g_hwndTray, 1, g_hiconAnim[g_nAnimFrame++ % ANIM_FRAMES]);
1206          break;
1207
1208       case TIMER_ANIMSTOP_ID:
1209          g_nAnimFrame = 0;
1210          TraySetIcon(g_hwndTray, 1, g_hiconIdle);
1211          KillTimer(g_hwndLogFrame, TIMER_ANIM_ID);
1212          KillTimer(g_hwndLogFrame, TIMER_ANIMSTOP_ID);
1213          break;
1214
1215       case TIMER_CLIPBUFFER_ID:
1216       case TIMER_CLIPBUFFER_FORCE_ID:
1217          LogClipBuffer();
1218          g_bClipPending = FALSE;
1219          KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_ID);
1220          KillTimer(g_hwndLogFrame, TIMER_CLIPBUFFER_FORCE_ID);
1221          break;
1222
1223       default:
1224          /* DO NOTHING */
1225          break;
1226    }
1227
1228 }
1229
1230
1231 /*********************************************************************
1232  *
1233  * Function    :  LogRichEditProc
1234  *
1235  * Description :  Window subclass routine handles some events for the rich edit control.
1236  *
1237  * Parameters  :
1238  *          1  :  hwnd = window handle of the rich edit control
1239  *          2  :  uMsg = message number
1240  *          3  :  wParam = first param for this message
1241  *          4  :  lParam = next param for this message
1242  *
1243  * Returns     :  Appropriate M$ window message handler codes.
1244  *
1245  *********************************************************************/
1246 LRESULT CALLBACK LogRichEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1247 {
1248    switch (uMsg)
1249    {
1250       case WM_RBUTTONUP:
1251       {
1252          POINT pt;
1253          pt.x = LOWORD(lParam);
1254          pt.y = HIWORD(lParam);
1255          ClientToScreen(hwnd, &pt);
1256          OnLogRButtonUp(wParam, pt.x, pt.y);
1257       }
1258       return 0;
1259    }
1260    return CallWindowProc(g_fnLogBox, hwnd, uMsg, wParam, lParam);
1261
1262 }
1263
1264
1265 /*********************************************************************
1266  *
1267  * Function    :  LogWindowProc
1268  *
1269  * Description :  Windows call back routine handles events on the log window.
1270  *
1271  * Parameters  :
1272  *          1  :  hwnd = handle of the logging window
1273  *          2  :  uMsg = message number
1274  *          3  :  wParam = first param for this message
1275  *          4  :  lParam = next param for this message
1276  *
1277  * Returns     :  Appropriate M$ window message handler codes.
1278  *
1279  *********************************************************************/
1280 LRESULT CALLBACK LogWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1281 {
1282    switch (uMsg)
1283    {
1284       case WM_CREATE:
1285          return 0;
1286
1287       case WM_CLOSE:
1288          /* This is the end - beautiful friend - the end */
1289          DestroyWindow(g_hwndLogBox);
1290          DestroyWindow(g_hwndLogFrame);
1291          return 0;
1292
1293       case WM_DESTROY:
1294          PostQuitMessage(0);
1295          return 0;
1296
1297       case WM_SHOWWINDOW:
1298       case WM_SIZE:
1299          /* Resize the logging window to fit the new frame */
1300          if (g_hwndLogBox)
1301          {
1302             RECT rc;
1303             GetClientRect(g_hwndLogFrame, &rc);
1304             SetWindowPos(g_hwndLogBox, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER);
1305          }
1306          return 0;
1307
1308       case WM_INITMENU:
1309          OnLogInitMenu((HMENU) wParam);
1310          return 0;
1311
1312       case WM_TIMER:
1313          OnLogTimer(wParam);
1314          return 0;
1315
1316       case WM_COMMAND:
1317          OnLogCommand(LOWORD(wParam));
1318          return 0;
1319
1320       case WM_SYSCOMMAND:
1321          switch (wParam)
1322          {
1323             case SC_CLOSE:
1324                if (g_bCloseHidesWindow)
1325                {
1326                   ShowLogWindow(FALSE);
1327                   return 0;
1328                }
1329                break;
1330             case SC_MINIMIZE:
1331                ShowLogWindow(FALSE);
1332                return 0;
1333          }
1334          break;
1335    }
1336
1337    return DefWindowProc(hwnd, uMsg, wParam, lParam);
1338
1339 }
1340
1341 #endif /* ndef _WIN_CONSOLE - entire file */
1342
1343 /*
1344   Local Variables:
1345   tab-width: 3
1346   end:
1347 */