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