Ignoring *.txt (probably config files), w32.res and w32.aps (resources)
[privoxy.git] / killpopup.c
1 const char killpopup_rcs[] = "$Id: killpopup.c,v 1.7 2001/07/20 19:29:25 haroon 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.7  2001/07/20 19:29:25  haroon
36  *    - In v1.5 forgot to add that I implemented LOG_LEVEL_POPUPS in errlog.c,
37  *      errlog.h and killpopup.c. In that case, it is superfluous to have define for
38  *      POPUP_VERBOSE, so I removed the defines and logging is now done
39  *      via log_error(LOG_LEVEL_POPUPS, ....)
40  *
41  *    Revision 1.6  2001/07/19 19:11:35  haroon
42  *    - Implemented Guy's idea of replacing window.open( with 1;''.concat(
43  *    - Implemented Guy's idea of replacing .resizeTo( with .scrollTo(
44  *
45  *    Revision 1.5  2001/07/18 15:02:52  haroon
46  *    improved nuking of window.open
47  *
48  *    Revision 1.4  2001/06/29 13:29:55  oes
49  *    Added FIXMEs (and didn't repair, hehe)
50  *
51  *    Revision 1.3  2001/05/22 18:56:28  oes
52  *    CRLF -> LF
53  *
54  *    Revision 1.2  2001/05/20 01:21:20  jongfoster
55  *    Version 2.9.4 checkin.
56  *    - Merged popupfile and cookiefile, and added control over PCRS
57  *      filtering, in new "permissionsfile".
58  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
59  *      file error you now get a message box (in the Win32 GUI) rather
60  *      than the program exiting with no explanation.
61  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
62  *      skipping.
63  *    - Removed tabs from "config"
64  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
65  *    - Bumped up version number.
66  *
67  *    Revision 1.1.1.1  2001/05/15 13:58:58  oes
68  *    Initial import of version 2.9.3 source tree
69  *
70  *
71  *********************************************************************/
72 \f
73
74 #include "config.h"
75
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <sys/types.h>
79 #include <string.h>
80 #include <malloc.h>
81 #include <errno.h>
82 #include <sys/stat.h>
83 #include <ctype.h>
84
85 #ifndef _WIN32
86 #include <unistd.h>
87 #endif
88
89 #include "project.h"
90 #include "killpopup.h"
91 #include "jcc.h"
92 #include "errlog.h"
93
94 const char killpopup_h_rcs[] = KILLPOPUP_H_VERSION;
95
96 #ifdef FEATURE_KILL_POPUPS
97
98 /*********************************************************************
99  *
100  * Function    :  filter_popups
101  *
102  * Description :  Filter the block of data that's been read from the server.
103  *                Caller is responsible for checking permissons list
104  *                to determine if this function should be called.
105  *                Remember not to change the content length (substitute char by char)
106  *
107  * Parameters  :
108  *          1  :  buff = Buffer to scan and modify.  Null terminated.
109  *          2  :  size = Buffer size, excluding null terminator.
110  *
111  * Returns     :  void
112  *
113  *********************************************************************/
114 void filter_popups(char *buff, int size)
115 {
116    char *popup = NULL;
117    char *close = NULL;
118    char *p     = NULL;
119
120    while ((popup = strstr( buff, "window.open(" )) != NULL)
121    {
122       if ( popup )
123       {
124          /*
125           * replace the window.open( with a harmless JavaScript replacement (notice the two single quotes)
126           * Guy's idea (thanks)
127           */
128          strncpy(popup, "1;''.concat(", 12);
129          log_error(LOG_LEVEL_POPUPS, "Blocked popup window open");
130       }
131    }
132    
133    while ((popup = strstr( buff, ".resizeTo(" )) != NULL)
134    {
135       if ( popup )
136       {
137          /*
138           * replace the .resizeTo( with a harmless JavaScript replacement
139           * Guy's idea (thanks)
140           */
141          strncpy(popup, ".scrollTo(", 10);
142          log_error(LOG_LEVEL_POPUPS, "Blocked popup window resize");
143       }
144    }
145
146    /* Filter all other crap like onUnload onExit etc.  (by BREITENB) NEW!*/
147    popup=strstr( buff, "<body");
148    if (!popup) popup=strstr( buff, "<BODY");
149    if (!popup) popup=strstr( buff, "<Body");
150    if (!popup) popup=strstr( buff, "<BOdy");
151    if (popup)
152    {
153       close=strchr(popup,'>');
154       if (close)
155       {
156          /* we are now between <body and the ending > FIXME: No, we're anywhere! --oes*/
157          p=strstr(popup, "onUnload");
158          if (p)
159          {
160             strncpy(p,"_nU_",4);
161          }
162          p=strstr(popup, "onExit");
163          if (p)
164          {
165             strncpy(p,"_nE_",4);
166          }
167       }
168    }
169
170 }
171
172 #endif /* def FEATURE_KILL_POPUPS */
173
174 /*
175   Local Variables:
176   tab-width: 3
177   end:
178 */