6187b7e05a202416420af0b415681c6e0bedca3b
[privoxy.git] / killpopup.c
1 const char killpopup_rcs[] = "$Id: killpopup.c,v 1.3 2001/05/22 18:56:28 oes Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/killpopup.c,v $
5  *
6  * Purpose     :  Handles the filtering of popups.
7  *
8  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
9  *                IJBSWA team.  http://ijbswa.sourceforge.net
10  *
11  *                Based on the Internet Junkbuster originally written
12  *                by and Copyright (C) 1997 Anonymous Coders and 
13  *                Junkbusters Corporation.  http://www.junkbusters.com
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: killpopup.c,v $
35  *    Revision 1.3  2001/05/22 18:56:28  oes
36  *    CRLF -> LF
37  *
38  *    Revision 1.2  2001/05/20 01:21:20  jongfoster
39  *    Version 2.9.4 checkin.
40  *    - Merged popupfile and cookiefile, and added control over PCRS
41  *      filtering, in new "permissionsfile".
42  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
43  *      file error you now get a message box (in the Win32 GUI) rather
44  *      than the program exiting with no explanation.
45  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
46  *      skipping.
47  *    - Removed tabs from "config"
48  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
49  *    - Bumped up version number.
50  *
51  *    Revision 1.1.1.1  2001/05/15 13:58:58  oes
52  *    Initial import of version 2.9.3 source tree
53  *
54  *
55  *********************************************************************/
56 \f
57
58 #include "config.h"
59
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <sys/types.h>
63 #include <string.h>
64 #include <malloc.h>
65 #include <errno.h>
66 #include <sys/stat.h>
67 #include <ctype.h>
68
69 #ifndef _WIN32
70 #include <unistd.h>
71 #endif
72
73 #include "project.h"
74 #include "killpopup.h"
75 #include "jcc.h"
76
77 const char killpopup_h_rcs[] = KILLPOPUP_H_VERSION;
78
79 #ifdef KILLPOPUPS
80
81 /* Change these for debug output.  *lots*. */
82 /*#define POPUP_VERBOSE 1*/
83 #undef POPUP_VERBOSE
84
85
86 /*********************************************************************
87  *
88  * Function    :  filter_popups
89  *
90  * Description :  Filter the block of data that's been read from the server.
91  *                Caller is responsible for checking permissons list
92  *                to determine if this function should be called.
93  *
94  * Parameters  :
95  *          1  :  buff = Buffer to scan and modify.  Null terminated.
96  *          2  :  size = Buffer size, excluding null terminator.
97  *
98  * Returns     :  void
99  *
100  *********************************************************************/
101 void filter_popups(char *buff, int size)
102 {
103    char *popup = NULL;
104    char *close = NULL;
105    char *p     = NULL;
106
107    while ((popup = strstr( buff, "window.open(" )) != NULL)
108    {
109 #ifdef POPUP_VERBOSE
110       fprintf(logfp, "Found start of window open" );
111 #endif
112       close = strstr( popup+1, ");" );
113       if ( close )
114       {
115 #ifdef POPUP_VERBOSE
116          fprintf(logfp, "Found end of window open" );
117 #endif
118          p = popup;
119          *p++ = '1';
120          for ( ; p != (close+1); p++ )
121          {
122             *p = ' ';
123          }
124 #ifdef POPUP_VERBOSE
125          fprintf(logfp, "Blocked %s\n", host_name );
126 #endif
127       }
128       else
129       {
130 #ifdef POPUP_VERBOSE
131          fprintf(logfp, "Couldn't find end, turned into comment.  Read boundary?\n" );
132 #endif
133          *popup++ = '1';
134          *popup++ = ';';
135          *popup++ = '/';
136          *popup = '/';
137          /*
138           * result of popup is assigned to variable and the rest commented out
139           * window.open(blah
140           *             will be translated to
141           * 1;//ow.open(blah
142           *             and
143           * myWindow = window.open(blah
144           *             will be translated to
145           * myWindow = 1;//ow.open(blah
146           */
147       }
148    }
149
150    /* Filter all other crap like onUnload onExit etc.  (by BREITENB) NEW!*/
151    popup=strstr( buff, "<body");
152    if (!popup) popup=strstr( buff, "<BODY");
153    if (!popup) popup=strstr( buff, "<Body");
154    if (!popup) popup=strstr( buff, "<BOdy");
155    if (popup)
156    {
157       close=strchr(popup,'>');
158       if (close)
159       {
160          /* we are now between <body and the ending > FIXME: No, we're anywhere! --oes*/
161          p=strstr(popup, "onUnload");
162          if (p)
163          {
164             strncpy(p,"_nU_",4);
165          }
166          p=strstr(popup, "onExit");
167          if (p)
168          {
169             strncpy(p,"_nE_",4);
170          }
171       }
172    }
173
174 }
175
176 #endif /* def KILLPOPUPS */
177
178 /*
179   Local Variables:
180   tab-width: 3
181   end:
182 */