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