Fixed copy command to copy the file to te right place.
[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  *                FIXME: Should use the replacements proposed by Guy
94  *
95  * Parameters  :
96  *          1  :  buff = Buffer to scan and modify.  Null terminated.
97  *          2  :  size = Buffer size, excluding null terminator.
98  *
99  * Returns     :  void
100  *
101  *********************************************************************/
102 void filter_popups(char *buff, int size)
103 {
104    char *popup = NULL;
105    char *close = NULL;
106    char *p     = NULL;
107    char *q     = NULL; /* by BREITENB NEW! */
108
109    while ((popup = strstr( buff, "window.open(" )) != NULL)
110    {
111 #ifdef POPUP_VERBOSE
112       fprintf(logfp, "Found start of window open" );
113 #endif
114       close = strstr( popup+1, ");" );
115       if ( close )
116       {
117 #ifdef POPUP_VERBOSE
118          fprintf(logfp, "Found end of window open" );
119 #endif
120          for ( p = popup; 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 = '/';
134          popup++;
135          *popup = '/';
136       }
137
138
139       q=popup; /* by BREITENB NEW! */
140       while (q>=buff)
141       {
142          if (*q==' ' || *q=='\t')
143             q--;
144          else break;
145       }
146       if (q>=buff)
147       {
148          if (*q=='=') *++q='1';
149          /* result of popup is assigned to a variable! ensure success. hehehe. */
150       }
151    }
152
153    /* Filter all other crap like onUnload onExit etc.  (by BREITENB) NEW!*/
154    popup=strstr( buff, "<body");
155    if (!popup) popup=strstr( buff, "<BODY");
156    if (!popup) popup=strstr( buff, "<Body");
157    if (!popup) popup=strstr( buff, "<BOdy");
158    if (popup)
159    {
160       close=strchr(popup,'>');
161       if (close)
162       {
163          /* we are now between <body and the ending > FIXME: No, we're anywhere! --oes*/
164          p=strstr(popup, "onUnload");
165          if (p)
166          {
167             strncpy(p,"_nU_",4);
168          }
169          p=strstr(popup, "onExit");
170          if (p)
171          {
172             strncpy(p,"_nE_",4);
173          }
174       }
175    }
176
177 }
178
179 #endif /* def KILLPOPUPS */
180
181 /*
182   Local Variables:
183   tab-width: 3
184   end:
185 */