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