Change "Edit Packages" URL to use https.
[privoxy.git] / killpopup.c
1 const char killpopup_rcs[] = "$Id: killpopup.c,v 1.16 2002/03/26 22:29:55 swa Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/Attic/killpopup.c,v $
5  *
6  * Purpose     :  Handles the filtering of popups.
7  *
8  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
9  *                Privoxy team. http://www.privoxy.org/
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.16  2002/03/26 22:29:55  swa
36  *    we have a new homepage!
37  *
38  *    Revision 1.15  2002/03/24 13:25:43  swa
39  *    name change related issues
40  *
41  *    Revision 1.14  2002/03/07 03:46:53  oes
42  *    Fixed compiler warnings etc
43  *
44  *    Revision 1.13  2001/11/13 00:16:40  jongfoster
45  *    Replacing references to malloc.h with the standard stdlib.h
46  *    (See ANSI or K&R 2nd Ed)
47  *
48  *    Revision 1.12  2001/10/25 03:40:48  david__schmidt
49  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
50  *    threads to call select() simultaneously.  So, it's time to do a real, live,
51  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
52  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
53  *
54  *    Revision 1.11  2001/10/07 15:42:41  oes
55  *    filter_popups now gets a csp pointer so it can raise the new
56  *      CSP_FLAG_MODIFIED flag.
57  *
58  *    Revision 1.10  2001/09/22 16:34:44  jongfoster
59  *    Removing unneeded #includes
60  *
61  *    Revision 1.9  2001/07/31 14:44:22  oes
62  *    Deleted unused size parameter from filter_popups()
63  *
64  *    Revision 1.8  2001/07/30 22:08:36  jongfoster
65  *    Tidying up #defines:
66  *    - All feature #defines are now of the form FEATURE_xxx
67  *    - Permanently turned off WIN_GUI_EDIT
68  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
69  *
70  *    Revision 1.7  2001/07/20 19:29:25  haroon
71  *    - In v1.5 forgot to add that I implemented LOG_LEVEL_POPUPS in errlog.c,
72  *      errlog.h and killpopup.c. In that case, it is superfluous to have define for
73  *      POPUP_VERBOSE, so I removed the defines and logging is now done
74  *      via log_error(LOG_LEVEL_POPUPS, ....)
75  *
76  *    Revision 1.6  2001/07/19 19:11:35  haroon
77  *    - Implemented Guy's idea of replacing window.open( with 1;''.concat(
78  *    - Implemented Guy's idea of replacing .resizeTo( with .scrollTo(
79  *
80  *    Revision 1.5  2001/07/18 15:02:52  haroon
81  *    improved nuking of window.open
82  *
83  *    Revision 1.4  2001/06/29 13:29:55  oes
84  *    Added FIXMEs (and didn't repair, hehe)
85  *
86  *    Revision 1.3  2001/05/22 18:56:28  oes
87  *    CRLF -> LF
88  *
89  *    Revision 1.2  2001/05/20 01:21:20  jongfoster
90  *    Version 2.9.4 checkin.
91  *    - Merged popupfile and cookiefile, and added control over PCRS
92  *      filtering, in new "permissionsfile".
93  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
94  *      file error you now get a message box (in the Win32 GUI) rather
95  *      than the program exiting with no explanation.
96  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
97  *      skipping.
98  *    - Removed tabs from "config"
99  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
100  *    - Bumped up version number.
101  *
102  *    Revision 1.1.1.1  2001/05/15 13:58:58  oes
103  *    Initial import of version 2.9.3 source tree
104  *
105  *
106  *********************************************************************/
107 \f
108
109 #include "config.h"
110
111 #include <stdio.h>
112 #include <stdlib.h>
113 #include <sys/types.h>
114 #include <string.h>
115 #include <errno.h>
116 #include <sys/stat.h>
117 #include <ctype.h>
118
119 #if !defined(_WIN32) && !defined(__OS2__)
120 #include <unistd.h>
121 #endif
122
123 #include "project.h"
124 #include "killpopup.h"
125 #include "errlog.h"
126
127 const char killpopup_h_rcs[] = KILLPOPUP_H_VERSION;
128
129 #ifdef FEATURE_KILL_POPUPS
130
131 /*********************************************************************
132  *
133  * Function    :  filter_popups
134  *
135  * Description :  Filter the block of data that's been read from the server
136  *                for javascript popup code and replace by syntactically
137  *                neutral code of the same size.
138  *                Raise the CSP_FLAG_MODIFIED flag on success.
139  *
140  * Parameters  :
141  *          1  :  buff = Buffer to scan and modify.  Null terminated.
142  *          2  :  csp = Client state pointer
143  *
144  * Returns     :  void
145  *
146  *********************************************************************/
147 void filter_popups(char *buff, struct client_state *csp)
148 {
149    char *start_p = NULL;
150    char *close_p = NULL;
151    char *p     = NULL;
152
153    /*
154     * replace the window.open( with a harmless JavaScript replacement
155     * (notice the two single quotes)
156     */
157    while ((start_p = strstr(buff, "window.open(")) != NULL)
158    {
159       if (start_p)
160       {
161          strncpy(start_p, "1;''.concat(", 12);
162          log_error(LOG_LEVEL_POPUPS, "Blocked popup window open");
163          csp->flags |= CSP_FLAG_MODIFIED;
164       }
165    }
166
167    /*
168     * replace the .resizeTo( with a harmless JavaScript replacement
169     */
170    while ((start_p = strstr(buff, ".resizeTo(")) != NULL)
171    {
172       if (start_p)
173       {
174          strncpy(start_p, ".scrollTo(", 10);
175          log_error(LOG_LEVEL_POPUPS, "Blocked popup window resize");
176          csp->flags |= CSP_FLAG_MODIFIED;
177       }
178    }
179
180    /* 
181     * Filter onUnload and onExit
182     */
183    start_p = strstr(buff, "<body");
184    if (!start_p) start_p = strstr(buff, "<BODY");
185    if (!start_p) start_p = strstr(buff, "<Body");
186    if (!start_p) start_p = strstr(buff, "<BOdy");
187    if (start_p)
188    {
189       close_p = strchr(start_p, '>');
190       if (close_p)
191       {
192          /* we are now between <body and the ending > */
193          p = strstr(start_p, "onUnload");
194          if (p)
195          {
196             strncpy(p, "_nU_", 4);
197             csp->flags |= CSP_FLAG_MODIFIED;
198          }
199          p = strstr(start_p, "onExit");
200          if (p)
201          {
202             strncpy(p, "_nE_", 4);
203             csp->flags |= CSP_FLAG_MODIFIED;
204          }
205       }
206    }
207
208 }
209
210 #endif /* def FEATURE_KILL_POPUPS */
211
212 /*
213   Local Variables:
214   tab-width: 3
215   end:
216 */