sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

surf-tip-url-filtering.diff (4176B)


      1 diff --git a/Makefile b/Makefile
      2 index a9d4d1d..75fb004 100644
      3 --- a/Makefile
      4 +++ b/Makefile
      5 @@ -18,7 +18,14 @@ options:
      6  	@echo CC $<
      7  	@${CC} -c ${CFLAGS} $<
      8  
      9 -${OBJ}: config.h config.mk
     10 +${OBJ}: config.h config.mk filters_compiled
     11 +
     12 +filters_compiled: filters
     13 +	sed -e '/^$$/d' -e 's|\\|\\\\|g' -e 's|$$|",|' -e 's|^|"|' < filters > $@
     14 +
     15 +filters:
     16 +	@echo creating $@ from filters.def
     17 +	@cp filters.def $@
     18  
     19  config.h:
     20  	@echo creating $@ from config.def.h
     21 @@ -30,7 +37,7 @@ surf: ${OBJ}
     22  
     23  clean:
     24  	@echo cleaning
     25 -	@rm -f surf ${OBJ} surf-${VERSION}.tar.gz
     26 +	@rm -f surf ${OBJ} surf-${VERSION}.tar.gz filters_compiled
     27  
     28  dist: clean
     29  	@echo creating dist tarball
     30 diff --git a/config.def.h b/config.def.h
     31 index 80a0feb..3cc5131 100644
     32 --- a/config.def.h
     33 +++ b/config.def.h
     34 @@ -21,6 +21,13 @@ static char *cafile         = "/etc/ssl/certs/ca-certificates.crt";
     35  static char *strictssl      = FALSE; /* Refuse untrusted SSL connections */
     36  static time_t sessiontime   = 3600;
     37  
     38 +/* Regular expressions to match URLs that should not be loaded */
     39 +char *filter_patterns[] = {
     40 +#include "filters_compiled"
     41 +};
     42 +/* Define this for verbose filtering */
     43 +// #define FILTER_VERBOSE
     44 +
     45  /* Webkit default features */
     46  static Bool enablescrollbars = TRUE;
     47  static Bool enablespatialbrowsing = TRUE;
     48 diff --git a/filters.def b/filters.def
     49 new file mode 100644
     50 index 0000000..77158de
     51 --- /dev/null
     52 +++ b/filters.def
     53 @@ -0,0 +1,2 @@
     54 +^eviladvertisments\.com$
     55 +/favicon\.ico$
     56 diff --git a/surf.c b/surf.c
     57 index 7c78b4a..8d24197 100644
     58 --- a/surf.c
     59 +++ b/surf.c
     60 @@ -20,6 +20,7 @@
     61  #include <webkit/webkit.h>
     62  #include <glib/gstdio.h>
     63  #include <JavaScriptCore/JavaScript.h>
     64 +#include <regex.h>
     65  #include <sys/file.h>
     66  #include <libgen.h>
     67  #include <stdarg.h>
     68 @@ -27,6 +28,7 @@
     69  #include "arg.h"
     70  
     71  char *argv0;
     72 +regex_t *filter_expressions;
     73  
     74  #define LENGTH(x)               (sizeof x / sizeof x[0])
     75  #define CLEANMASK(mask)         (mask & (MODKEY|GDK_SHIFT_MASK))
     76 @@ -119,6 +121,8 @@ static void destroyclient(Client *c);
     77  static void destroywin(GtkWidget* w, Client *c);
     78  static void die(const char *errstr, ...);
     79  static void eval(Client *c, const Arg *arg);
     80 +static bool filter_init(void);
     81 +static bool filter_request(const gchar *uri);
     82  static void find(Client *c, const Arg *arg);
     83  static void fullscreen(Client *c, const Arg *arg);
     84  static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
     85 @@ -200,7 +204,7 @@ beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
     86  		gpointer d) {
     87  	const gchar *uri = webkit_network_request_get_uri(req);
     88  
     89 -	if(g_str_has_suffix(uri, "/favicon.ico"))
     90 +	if(filter_request(uri))
     91  		webkit_network_request_set_uri(req, "about:blank");
     92  }
     93  
     94 @@ -473,6 +477,49 @@ die(const char *errstr, ...) {
     95  	exit(EXIT_FAILURE);
     96  }
     97  
     98 +static bool
     99 +filter_init(void) {
    100 +	bool errors = false;
    101 +	char *errorbuf;
    102 +
    103 +	errorbuf = malloc(sizeof(char) * BUFSIZ);
    104 +	filter_expressions = malloc(sizeof(regex_t) * LENGTH(filter_patterns));
    105 +
    106 +	for (off_t idx = 0; idx < LENGTH(filter_patterns); idx++) {
    107 +		char *pat = filter_patterns[idx];
    108 +		int err = regcomp(&filter_expressions[idx], pat,
    109 +				            REG_EXTENDED | REG_ICASE | REG_NOSUB);
    110 +		if (err != 0) {
    111 +			/* regerror always ends messages with 0x00 */
    112 +			(void) regerror(err, &filter_expressions[idx], errorbuf, BUFSIZ);
    113 +			fprintf(stderr, "Failed to compile \"%s\": %s\n", pat, errorbuf);
    114 +			errors = true;
    115 +		}
    116 +	}
    117 +
    118 +	free(errorbuf);
    119 +	return !errors;
    120 +}
    121 +
    122 +static bool
    123 +filter_request(const gchar *uri) {
    124 +	if (!strcmp(uri, "about:blank"))
    125 +		return false;
    126 +	for (off_t idx = 0; idx < LENGTH(filter_patterns); idx++) {
    127 +		if (regexec(&filter_expressions[idx], uri, 0, NULL, 0) == REG_NOMATCH) {
    128 +			continue;
    129 +		}
    130 +#ifdef FILTER_VERBOSE
    131 +		fprintf(stderr, "filtering \"%s\"\n", uri);
    132 +#endif
    133 +		return true;
    134 +	}
    135 +#ifdef FILTER_VERBOSE
    136 +	fprintf(stderr, "not filtering \"%s\"\n", uri);
    137 +#endif
    138 +	return false;
    139 +}
    140 +
    141  static void
    142  find(Client *c, const Arg *arg) {
    143  	const char *s;
    144 @@ -1152,6 +1199,10 @@ setup(void) {
    145  		g_free(new_proxy);
    146  		usingproxy = 1;
    147  	}
    148 +
    149 +	if (!filter_init()) {
    150 +		die("Failed to compile one or more filter expressions\n");
    151 +	}
    152  }
    153  
    154  static void