├── .gitignore ├── PKGBUILDs ├── surf │ └── PKGBUILD └── wyebab │ └── PKGBUILD ├── PKGBUILD ├── ephy-uri-tester.h ├── makefile ├── wyebrun.h ├── README.md ├── ab.c ├── wyebrun.c ├── ephy-uri-tester.c └── COPYING /.gitignore: -------------------------------------------------------------------------------- 1 | adblock.so 2 | librun.o 3 | wyebab 4 | !wyebab/ 5 | testrun 6 | user.css 7 | -------------------------------------------------------------------------------- /PKGBUILDs/surf/PKGBUILD: -------------------------------------------------------------------------------- 1 | # Maintainer: jun7 2 | pkgname=wyebadblock-surf 3 | pkgver=1 4 | pkgrel=1 5 | pkgdesc="adblock link to wyebadblock for surf." 6 | arch=('x86_64') 7 | url="https://github.com/jun7/wyebadblock" 8 | license=('GPL3') 9 | depends=('wyebadblock-git' 'surf') 10 | makedepends=() 11 | source=() 12 | 13 | 14 | package() { 15 | mkdir -p "$pkgdir/usr/local/lib/surf" 16 | ln -s /usr/lib/wyebrowser/adblock.so "$pkgdir/usr/local/lib/surf" 17 | } 18 | -------------------------------------------------------------------------------- /PKGBUILDs/wyebab/PKGBUILD: -------------------------------------------------------------------------------- 1 | # Maintainer: jun7 2 | pkgname=wyebab-git 3 | pkgver=1.1 4 | pkgrel=1 5 | pkgdesc="An adblock command using the easylist.txt." 6 | arch=('x86_64') 7 | url="https://github.com/jun7/wyebadblock" 8 | license=('GPL3') 9 | depends=('glib2') 10 | conflicts=('wyebadblock-git') 11 | makedepends=('git') 12 | _branch=master 13 | source=("git://github.com/jun7/wyebadblock.git#branch=$_branch") 14 | md5sums=('SKIP') 15 | 16 | pkgver(){ 17 | cd "$srcdir/wyebadblock" 18 | printf "%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)" 19 | } 20 | 21 | prepare() { 22 | cd "$srcdir/wyebadblock" 23 | git pull --rebase origin $_branch 24 | make clean 25 | } 26 | 27 | build() { 28 | cd "$srcdir/wyebadblock" 29 | DEBUG=0 30 | make wyebab 31 | } 32 | 33 | package() { 34 | cd "$srcdir/wyebadblock" 35 | install -Dm755 wyebab "$pkgdir/usr/bin/wyebab" 36 | } 37 | -------------------------------------------------------------------------------- /PKGBUILD: -------------------------------------------------------------------------------- 1 | # Maintainer: jun7 2 | pkgname=wyebadblock-git 3 | pkgver=1.1 4 | pkgrel=7 5 | pkgdesc="An adblock extension for webkit2gtk browsers." 6 | arch=('x86_64') 7 | provides=('wyebab-git') 8 | url="https://github.com/jun7/wyebadblock" 9 | license=('GPL3') 10 | depends=('webkit2gtk') 11 | makedepends=('git') 12 | _branch=master 13 | source=("git+https://github.com/jun7/wyebadblock.git#branch=$_branch") 14 | md5sums=('SKIP') 15 | 16 | pkgver(){ 17 | cd "$srcdir/wyebadblock" 18 | printf "%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)" 19 | } 20 | 21 | prepare() { 22 | cd "$srcdir/wyebadblock" 23 | git pull --rebase origin $_branch 24 | make clean 25 | } 26 | 27 | build() { 28 | cd "$srcdir/wyebadblock" 29 | DEBUG=0 30 | make 31 | } 32 | 33 | package() { 34 | cd "$srcdir/wyebadblock" 35 | install -Dm755 wyebab "$pkgdir/usr/bin/wyebab" 36 | install -Dm755 adblock.so "$pkgdir/usr/lib/wyebrowser/adblock.so" 37 | } 38 | -------------------------------------------------------------------------------- /ephy-uri-tester.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 | /* 3 | * Copyright © 2011 Igalia S.L. 4 | * 5 | * This file is part of Epiphany. 6 | * 7 | * Epiphany is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * Epiphany is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with Epiphany. If not, see . 19 | */ 20 | 21 | #pragma once 22 | 23 | #include 24 | 25 | G_BEGIN_DECLS 26 | 27 | #define EPHY_TYPE_URI_TESTER (ephy_uri_tester_get_type ()) 28 | 29 | G_DECLARE_FINAL_TYPE (EphyUriTester, ephy_uri_tester, EPHY, URI_TESTER, GObject) 30 | 31 | EphyUriTester *ephy_uri_tester_new (const char *adblock_data_dir); 32 | void ephy_uri_tester_load (EphyUriTester *tester); 33 | char *ephy_uri_tester_rewrite_uri (EphyUriTester *tester, 34 | const char *request_uri, 35 | const char *page_uri); 36 | 37 | 38 | G_END_DECLS 39 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | LISTNAME=easylist.txt 2 | PREFIX ?= /usr 3 | WEBKITVER ?= 4.0 4 | WEBKIT ?= webkit2gtk-$(WEBKITVER) 5 | EXTENSION_DIR ?= $(PREFIX)/lib/wyebrowser 6 | 7 | ifneq ($(WEBKITVER), 4.0) 8 | VERDIR=/$(WEBKITVER) 9 | endif 10 | ifeq ($(DEBUG), 1) 11 | CFLAGS += -Wall 12 | else 13 | DEBUG = 0 14 | CFLAGS += -Wno-deprecated-declarations 15 | endif 16 | DDEBUG=-DDEBUG=${DEBUG} 17 | 18 | all: adblock.so wyebab librun.o testrun 19 | 20 | adblock.so: ab.c ephy-uri-tester.c ephy-uri-tester.h librun.o makefile 21 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< librun.o -shared -fPIC \ 22 | `pkg-config --cflags --libs gtk+-3.0 glib-2.0 $(WEBKIT)` \ 23 | $(DDEBUG) -DISEXT -DEXENAME=\"wyebab\" 24 | 25 | wyebab: ab.c ephy-uri-tester.c ephy-uri-tester.h librun.o makefile 26 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< librun.o \ 27 | `pkg-config --cflags --libs glib-2.0 gio-2.0` \ 28 | $(DDEBUG) -DDIRNAME=\"wyebadblock\" -DLISTNAME=\"$(LISTNAME)\" 29 | 30 | librun.o: wyebrun.c wyebrun.h makefile 31 | $(CC) $(CFLAGS) $(LDFLAGS) -c -o $@ $< -fPIC\ 32 | `pkg-config --cflags --libs glib-2.0` \ 33 | $(DDEBUG) 34 | 35 | testrun: wyebrun.c wyebrun.h makefile 36 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< \ 37 | `pkg-config --cflags --libs glib-2.0 gio-2.0` \ 38 | $(DDEBUG) -DTESTER=1 39 | 40 | clean: 41 | rm -f adblock.so 42 | rm -f wyebab 43 | rm -f librun.o 44 | rm -f testrun 45 | 46 | install: 47 | install -Dm755 wyebab $(DESTDIR)$(PREFIX)/bin/wyebab 48 | install -Dm755 adblock.so $(DESTDIR)$(EXTENSION_DIR)$(VERDIR)/adblock.so 49 | 50 | uninstall: 51 | rm -f $(PREFIX)/bin/wyebab 52 | rm -f $(EXTENSION_DIR)$(VERDIR)/adblock.so 53 | -rmdir $(EXTENSION_DIR)$(VERDIR) 54 | -rmdir $(EXTENSION_DIR) 55 | 56 | 57 | re: clean all 58 | # $(MAKE) clean 59 | # $(MAKE) all 60 | 61 | full: re install 62 | -------------------------------------------------------------------------------- /wyebrun.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2018 jun7@hush.com 3 | 4 | This file is part of wyebrun. 5 | 6 | wyebrun is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | wyebrun is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with wyebrun. If not, see . 18 | */ 19 | 20 | #ifndef _WYEBRUN_H 21 | #define _WYEBRUN_H 22 | 23 | #include 24 | #include 25 | 26 | #define WYEBPREFIX "--wyebrun" 27 | #define WYEBKEEPSEC 3 28 | 29 | //client 30 | //wyebrun spawns the exe automatically 31 | char *wyebget( char *exe, char *data); //don't free the ret val 32 | void wyebsend(char *exe, char *data); 33 | void wyebkeep(char *exe, int sec); //keep alive. default is 3s 34 | //loop the wyebkeep. to stop, use g_source_remove 35 | guint wyebloop(char *exe, int sec); 36 | //send stdin to svr and ret data to stdout 37 | //blank and enter exits 38 | void wyebclient(char *exe); 39 | 40 | //server 41 | typedef char *(*wyebdataf)(char *data); 42 | //server is spawned with an arg the caller 43 | bool wyebsvr(int argc, char **argv, wyebdataf func); 44 | //or if there is own GMainLoop 45 | void wyebwatch(char *exe, char *caller, wyebdataf func); 46 | //the caller is sent as 'exe --wyebrun 9999' and 47 | //used to send the res meaning we are ready. 48 | //3 sec left or client will die 49 | 50 | 51 | #endif //_WYEBRUN_H 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wyebadblock 2 | An adblock extension using the easylist.txt for [wyeb](https://github.com/jun7/wyeb), also webkit2gtk browsers. 3 | 4 | most of code of this are from https://github.com/GNOME/epiphany/tree/master/embed 5 | 6 | wyebab is shared by clients, So even nowadays, browsers spawn procs for each windows, 7 | wyebab keeps single server proc that makes less RAM and less CPU times. 8 | 9 | For example, while an epiphay's webproc uses 240M RAM for 10 | a page, wyeb uses 160M for the same page. 11 | Of course wyebab uses 80M but not gain. 12 | 13 | Don't worry, wyebab wills quit automatically when there is no client and 30 secs past. 14 | 15 | ## usage: 16 | 17 | depends: gtk+-3.0 glib-2.0 webkit2gtk-4.0 18 | 19 | make 20 | sudo make install 21 | 22 | then 23 | copy the **easylist.txt** to ~/.config/wyebadblock/ 24 | 25 | wyebadblock only checks 'easylist.txt' 26 | 27 | You can check whether wyebab has found easylist.txt by `wyebab --css` 28 | 29 | ## for webkit2gtk-4.1 30 | WEBKITVER=4.1 make 31 | sudo WEBKITVER=4.1 make install 32 | Make sure this adds a dir 4.1/ before the adblock.so 33 | 34 | ## Addition for other webkit2gtk browsers 35 | webkit2gtk loads extensions in a dir designated by each apps. 36 | So we have to make a link to wyebab in the dir. 37 | 38 | Following paths are for Arch Linux. Make sure on other distros, the paths are may different. 39 | 40 | ### surfer 41 | 42 | See the Surfer's README 43 | 44 | https://github.com/nihilowy/surfer 45 | 46 | ### epiphany 47 | 48 | sudo ln -s /usr/lib/wyebrowser/adblock.so /usr/lib64/epiphany/web-extensions 49 | 50 | This lacks epiphany's functionalities about adblock (e.g. can't disable), but saves RAM and CPU. 51 | Make sure while you keep enable epiphany's one, it is working too. 52 | 53 | ### surf 54 | sudo mkdir usr/local/lib/surf 55 | sudo ln -s /usr/lib/wyebrowser/adblock.so /usr/local/lib/surf 56 | 57 | There is a PKGBUILD file. see the 'PKGBULDs' dir. 58 | 59 | ### vimb 60 | 61 | sudo ln -s /usr/lib/wyebrowser/4.1/adblock.so /usr/lib/vimb 62 | 63 | If the path doesn't work, check https://fanglingsu.github.io/vimb/howto.html#block 64 | 65 | ### luakit 66 | 67 | sudo ln -s /usr/lib/wyebrowser/adblock.so /lib/luakit 68 | 69 | ### lariza 70 | 71 | mkdir -p ~/.config/lariza/web_extensions 72 | ln -s /usr/lib/wyebrowser/adblock.so ~/.config/lariza/web_extensions 73 | 74 | Since lariza enables sandbox, wyebadblock does not work without changing 'webkit_web_context_set_sandbox_enabled'. 75 | 76 | ### badwolf 77 | 78 | mkdir -p ${XDG_DATA_HOME:-$HOME/.local/share}/badwolf/webkit-web-extensions/ 79 | ln -s /usr/lib/wyebrowser/adblock.so ${XDG_DATA_HOME:-$HOME/.local/share}/badwolf/webkit-web-extensions/ 80 | 81 | ### Others 82 | 83 | search 'webkit_web_context_set_web_extensions_directory' in its source code 84 | and make the link from the dir to the wyebadblock just like above browsers. 85 | 86 | Also make sure 'webkit_web_context_set_sandbox_enabled' is not set. 87 | 88 | --- 89 | 90 | 91 | ## Element Hiding 92 | Per domain CSS hider rule is not supported 93 | 94 | wyebab --css > user.css 95 | 96 | And add the user.css to your browser as a user css. 97 | For wyeb, just copy the user.css to the conf dir. 98 | 99 | Make sure that huge css takes RAM a lot. 100 | Also it is often used to detect adblock. 101 | 102 | 103 | ## Disabling 104 | 105 | Setting chars(whatever) to the env value $DISABLE_ADBLOCK disables adblock. 106 | 107 | or 108 | 109 | Move the easylist.txt. 110 | 111 | ### For source code: 112 | set string ";adblock:false;" as the user data of the 113 | webkit_web_context_set_web_extensions_initialization_user_data; 114 | 115 | 116 | #### Runtime 117 | 118 | g_object_set_data(G_OBJECT(webkitwebpage), "adblock", GINT_TO_POINTER('n')); 119 | 120 | in any extension in the same process. 121 | 122 | #### Full Control 123 | 124 | If you want full control of wyebab as wyeb does, wyebab has api mode. 125 | Set string ";wyebabapi;" as the extensions_init's data. 126 | In api mode wyebab doesn't block URIs but keeps alive server proc and 127 | set the check function to the page object. 128 | So you can call the check func where ever as below. 129 | 130 | bool (*checkf)(const char *, const char *) = 131 | g_object_get_data(G_OBJECT(webkitwebpage), "wyebcheck"); 132 | if (checkf) 133 | passed = checkf(requesturi, pageuri); 134 | 135 | 136 | or 137 | 138 | Use the shell interface below. 139 | 140 | or 141 | 142 | Use wyebrun like the client code of wyebab in the ISEXT part of the ab.c. 143 | 144 | ## Shell 145 | 146 | wyebab 147 | 148 | Reads stdin outputs to stdout and keeps server. 149 | blank + enter quits. 150 | 151 | wyebab "requst_uri page_uri" 152 | 153 | Outputs result and 154 | Keeps server 30 secs. 155 | -------------------------------------------------------------------------------- /ab.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2018 jun7@hush.com 3 | 4 | This file is part of wyebadblock. 5 | 6 | wyebadblock is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | wyebadblock is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with wyebadblock. If not, see . 18 | */ 19 | 20 | #include "wyebrun.h" 21 | 22 | #if DEBUG 23 | # define D(f, ...) g_print("#"#f"\n", __VA_ARGS__); 24 | # define DD(a) g_print("#"#a"\n"); 25 | #else 26 | # define D(f, ...) ; 27 | # define DD(a) ; 28 | #endif 29 | 30 | 31 | #if ISEXT 32 | 33 | #include 34 | static bool check(const char *requri, const char *pageuri) 35 | { 36 | char *uris = g_strconcat(requri, " ", pageuri, NULL); 37 | char *ruri = wyebget(EXENAME, uris); 38 | g_free(uris); 39 | 40 | if (ruri && !*ruri) return false; 41 | return true; 42 | } 43 | static gboolean reqcb(WebKitWebPage *kp, WebKitURIRequest *req, 44 | WebKitURIResponse *r, gpointer p) 45 | { 46 | if (g_object_get_data(G_OBJECT(kp), "adblock") == (gpointer)'n') 47 | return false; 48 | 49 | static bool first = true; 50 | if (first) 51 | { 52 | if (webkit_uri_request_get_http_headers(req)) 53 | first = false; 54 | else //no head is local data. so haven't to block 55 | return false; 56 | } 57 | 58 | if (check(webkit_uri_request_get_uri(req), 59 | webkit_web_page_get_uri(kp))) return false; 60 | return true; 61 | } 62 | 63 | #if WEBKIT_MAJOR_VERSION > 2 || WEBKIT_MINOR_VERSION > 26 64 | static gboolean msgcb(WebKitWebPage *kp, WebKitUserMessage *msg, gpointer p) 65 | { 66 | if (strcmp("wyebadblock", webkit_user_message_get_name(msg))) return false; 67 | g_object_set_data(G_OBJECT(kp), "adblock", 68 | g_variant_get_boolean(webkit_user_message_get_parameters(msg)) ? NULL : 69 | GINT_TO_POINTER('n')); 70 | return true; 71 | } 72 | #endif 73 | 74 | static gboolean keepcb(WebKitWebPage *kp) 75 | { 76 | if (g_object_get_data(G_OBJECT(kp), "adblock") != (gpointer)'n') 77 | wyebkeep(EXENAME, 30); 78 | return true; 79 | } 80 | 81 | static bool apimode; 82 | static void pageinit(WebKitWebExtension *ex, WebKitWebPage *kp) 83 | { 84 | DD(pageinit) 85 | 86 | if (!apimode) 87 | g_signal_connect(kp, "send-request", G_CALLBACK(reqcb), NULL); 88 | #if WEBKIT_MAJOR_VERSION > 2 || WEBKIT_MINOR_VERSION > 26 89 | g_signal_connect(kp, "user-message-received", G_CALLBACK(msgcb), NULL); 90 | #endif 91 | 92 | g_object_set_data(G_OBJECT(kp), "wyebcheck", check); 93 | 94 | keepcb(kp); 95 | g_object_weak_ref(G_OBJECT(kp), (GWeakNotify)g_source_remove, 96 | GUINT_TO_POINTER(g_timeout_add(11 * 1000, (GSourceFunc)keepcb, kp))); 97 | } 98 | 99 | G_MODULE_EXPORT void webkit_web_extension_initialize_with_user_data( 100 | WebKitWebExtension *ex, const GVariant *v) 101 | { 102 | bool hasarg = false; 103 | const char *str; 104 | if (v && g_variant_is_of_type((GVariant *)v, G_VARIANT_TYPE_STRING) && 105 | (str = g_variant_get_string((GVariant *)v, NULL))) 106 | { 107 | bool enable = true; 108 | char **args = g_strsplit(str, ";", -1); 109 | for (char **arg = args; *arg; arg++) 110 | { 111 | if (g_str_has_prefix(*arg, "adblock:")) 112 | { 113 | enable = !strcmp(*arg + 8, "true"); 114 | hasarg = true; 115 | } 116 | if (!strcmp(*arg, "wyebabapi")) 117 | apimode = true; 118 | } 119 | g_strfreev(args); 120 | if (!enable) return; 121 | } 122 | 123 | if (!hasarg && *(g_getenv("DISABLE_ADBLOCK") ?: "") != '\0') 124 | return; 125 | 126 | g_signal_connect(ex, "page-created", G_CALLBACK(pageinit), NULL); 127 | } 128 | 129 | 130 | 131 | #else 132 | 133 | #include "ephy-uri-tester.c" 134 | 135 | static EphyUriTester *tester; 136 | static GThread *initt; 137 | 138 | static gpointer inittcb(gpointer p) 139 | { 140 | ephy_uri_tester_load(tester); 141 | return NULL; 142 | } 143 | 144 | static void monitorcb( 145 | GFileMonitor *m, GFile *f, GFile *o, GFileMonitorEvent e, gpointer p) 146 | { 147 | if (e == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT || 148 | e == G_FILE_MONITOR_EVENT_DELETED) 149 | exit(0); 150 | } 151 | static bool tryload(const char *parent) 152 | { 153 | char *path = g_build_filename(parent, DIRNAME, LISTNAME, NULL); 154 | 155 | GFile *gf = g_file_new_for_path(path); 156 | GFileMonitor *gm = g_file_monitor_file(gf, 157 | G_FILE_MONITOR_NONE, NULL, NULL); 158 | g_signal_connect(gm, "changed", G_CALLBACK(monitorcb), NULL); 159 | g_object_unref(gf); 160 | 161 | if (g_file_test(path, G_FILE_TEST_EXISTS)) 162 | { 163 | filter_file = g_file_new_for_path(path); 164 | tester = ephy_uri_tester_new("/foo/bar"); 165 | initt = g_thread_new("init", inittcb, NULL); 166 | } 167 | g_free(path); 168 | return tester; 169 | } 170 | static void init() 171 | { 172 | DD(wyebad init) 173 | if (tryload(g_get_user_config_dir())) return; 174 | 175 | for (const gchar * const *d = g_get_system_data_dirs(); *d; d++) 176 | if (tryload(*d)) return; 177 | 178 | char *dir = g_build_filename(g_get_user_config_dir(), DIRNAME, NULL); 179 | if (!g_file_test(dir, G_FILE_TEST_EXISTS)) 180 | g_mkdir_with_parents(dir, 0700); 181 | g_free(dir); 182 | } 183 | 184 | static char *datafunc(char *req) 185 | { 186 | static GMutex datam; 187 | g_mutex_lock(&datam); 188 | 189 | if (initt) 190 | { 191 | g_thread_join(initt); 192 | initt = NULL; 193 | } 194 | 195 | //req uri + ' ' + page uri 196 | char **args = g_strsplit(req, " ", 2); 197 | 198 | char *ret = !tester ? g_strdup(args[0]) : 199 | ephy_uri_tester_rewrite_uri(tester, args[0], args[1] ?: args[0]); 200 | 201 | g_strfreev(args); 202 | 203 | #if DEBUG 204 | if (ret) 205 | D(ret %s, ret) 206 | else 207 | D(BLOCKED %s, req) 208 | #endif 209 | 210 | g_mutex_unlock(&datam); 211 | return ret; 212 | } 213 | 214 | 215 | int main(int argc, char **argv) 216 | { 217 | DD(This bin is compiled with DEBUG=1) 218 | 219 | if (argc == 1) 220 | { 221 | wyebclient(argv[0]); 222 | } 223 | else if (!strcmp(argv[1], WYEBPREFIX)) 224 | { 225 | init(); 226 | wyebsvr(argc, argv, datafunc); 227 | } 228 | else if (!strcmp(argv[1], "-css") || !strcmp(argv[1], "--css")) 229 | { 230 | init(); 231 | if (!initt) 232 | { 233 | g_print("#There is no "LISTNAME"\n"); 234 | exit(0); 235 | } 236 | g_thread_join(initt); 237 | 238 | g_print("%s", tester->blockcss->str); 239 | g_print("\n\n\n\n" 240 | "{display:none !important} /*\n" 241 | "{opacity:0 !important; position:absolute !important;}\n" 242 | "/**/\n" 243 | ); 244 | //g_print(tester->blockcssprivate->str); 245 | } 246 | else 247 | { 248 | wyebkeep(argv[0], 30); 249 | g_print("%s", wyebget(argv[0], argv[1])); 250 | } 251 | 252 | exit(0); 253 | } 254 | 255 | #endif 256 | -------------------------------------------------------------------------------- /wyebrun.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2018 jun7@hush.com 3 | 4 | This file is part of wyebrun. 5 | 6 | wyebrun is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | wyebrun is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with wyebrun. If not, see . 18 | */ 19 | 20 | //getpid 21 | #include 22 | #include 23 | 24 | //flock 25 | #include 26 | // for musl-libc havent fcntl in file.h 27 | #include 28 | 29 | //monitor 30 | #include 31 | 32 | 33 | #include "wyebrun.h" 34 | 35 | #define ROOTNAME "wyebrun" 36 | #define CLIDIR "clients" 37 | #define PREFIX WYEBPREFIX 38 | #define KEEPS WYEBKEEPSEC 39 | #define INPUT "wyebinput" 40 | #define PING "wyebping" 41 | #define DPINGTIME 1000 42 | 43 | #define P(f, ...) g_print(#f"\n", __VA_ARGS__); 44 | 45 | #if DEBUG 46 | # define D(f, ...) g_print("#"#f"\n", __VA_ARGS__); 47 | # define DD(a) g_print("#"#a"\n"); 48 | #else 49 | # define D(...) ; 50 | # define DD(a) ; 51 | #endif 52 | 53 | typedef enum { 54 | //to svr 55 | CSuntil = 'u', 56 | CSdata = 'd', 57 | CSping = 'p', 58 | 59 | //to client 60 | CCwoke = 'w', 61 | CCret = 'r', //retrun data 62 | CClost = 'l', //we lost the req 63 | } Com; 64 | 65 | 66 | 67 | //shared 68 | static void fatal(int i) 69 | { 70 | P(\n!!! fatal %d !!!\n, i) 71 | exit(1); 72 | } 73 | static void mkdirif(char *path) 74 | { 75 | char *dir = g_path_get_dirname(path); 76 | if (!g_file_test(dir, G_FILE_TEST_EXISTS)) 77 | g_mkdir_with_parents(dir, 0700); 78 | 79 | g_free(dir); 80 | } 81 | static char *ipcpath(char *exe, char *name) 82 | { 83 | return g_build_filename(g_get_user_runtime_dir(), 84 | ROOTNAME, exe, name, NULL); 85 | } 86 | static bool ipcsend(char *exe, char *name, 87 | Com type, char *caller, char *data) 88 | { 89 | char *path = ipcpath(exe, name); 90 | 91 | char *esc = g_strescape(data ?: "", ""); 92 | D(ipcsend exe:%s name:%s type:%c sizs:%lu, exe, name, type, strlen(esc ?: "")) 93 | char *line = g_strdup_printf("%c%s:%s\n", type, caller ?: "", esc); 94 | int len = strlen(line); 95 | g_free(esc); 96 | 97 | 98 | bool ret = false; 99 | int pp = open(path, O_WRONLY | O_NONBLOCK); 100 | if (pp > -1) 101 | { 102 | if (len > PIPE_BUF) //4096 => len is atomic 103 | { 104 | flock(pp, LOCK_EX); 105 | fcntl(pp, F_SETFL, 0); //clear O_NONBLOCK to write len > 65536; 106 | } 107 | else 108 | flock(pp, LOCK_SH); 109 | 110 | ret = write(pp, line, len) == len; 111 | close(pp); 112 | } 113 | 114 | g_free(line); 115 | g_free(path); 116 | 117 | //D(ipcsend ret %d, ret) 118 | return ret; 119 | } 120 | 121 | static gboolean ipccb(GIOChannel *ch, GIOCondition cnd, gpointer p); 122 | 123 | static GSource *ipcwatch(char *exe, char *name, GMainContext *ctx, gpointer p) 124 | { 125 | char *path = ipcpath(exe, name); 126 | if (!g_file_test(path, G_FILE_TEST_EXISTS)) 127 | { 128 | mkdirif(path); 129 | mkfifo(path, 0600); 130 | } 131 | 132 | GIOChannel *io = g_io_channel_new_file(path, "r+", NULL); 133 | GSource *watch = g_io_create_watch(io, G_IO_IN); 134 | g_io_channel_unref(io); 135 | g_source_set_callback(watch, (GSourceFunc)ipccb, p, NULL); 136 | g_source_attach(watch, ctx); 137 | 138 | g_free(path); 139 | return watch; 140 | } 141 | 142 | 143 | 144 | //@server 145 | static char *svrexe; 146 | static GMainLoop *sloop; 147 | static wyebdataf dataf; 148 | static GHashTable *orders; 149 | static GMutex ordersm; 150 | 151 | static gboolean quitif(gpointer p) 152 | { 153 | g_mutex_lock(&ordersm); 154 | if (!g_hash_table_size(orders)) 155 | { 156 | DD(SVR QUITS\n) 157 | g_main_loop_quit(sloop); 158 | } 159 | g_mutex_unlock(&ordersm); 160 | return true; 161 | } 162 | static void until(int sec) 163 | { 164 | if (!sloop) return; 165 | 166 | static guint last; 167 | static GMutex m; 168 | g_mutex_lock(&m); 169 | if (last) 170 | g_source_remove(last); 171 | last = g_timeout_add_full(G_PRIORITY_LOW * 2, sec * 1000, quitif, NULL, NULL); 172 | g_mutex_unlock(&m); 173 | } 174 | static gpointer pingt(gpointer p) 175 | { 176 | GMainContext *ctx = g_main_context_new(); 177 | ipcwatch(svrexe, PING, ctx, NULL); 178 | g_main_loop_run(g_main_loop_new(ctx, true)); 179 | return NULL; 180 | } 181 | void wyebwatch(char *exe, char *caller, wyebdataf func) 182 | { 183 | svrexe = exe; 184 | dataf = func; 185 | orders = g_hash_table_new(g_str_hash, g_str_equal); 186 | 187 | until(KEEPS); 188 | 189 | g_thread_unref(g_thread_new("ping", pingt, NULL)); 190 | ipcwatch(exe, INPUT, g_main_context_default(), NULL); 191 | 192 | if (!ipcsend(CLIDIR, caller, CCwoke, "", NULL)) 193 | fatal(1); 194 | } 195 | 196 | static void monitorcb(GFileMonitor *m, GFile *f, GFile *o, GFileMonitorEvent e, 197 | gpointer p) 198 | { 199 | if (e == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT) 200 | g_timeout_add(100, quitif, NULL); 201 | } 202 | static gboolean svrinit(char *caller) 203 | { 204 | wyebwatch(svrexe, caller, dataf); 205 | 206 | char path[PATH_MAX]; 207 | (void)readlink("/proc/self/exe", path, PATH_MAX); 208 | D(exepath %s, path) 209 | GFile *gf = g_file_new_for_path(path); 210 | GFileMonitor *gm = g_file_monitor_file( 211 | gf, G_FILE_MONITOR_NONE, NULL, NULL); 212 | g_signal_connect(gm, "changed", G_CALLBACK(monitorcb), NULL); 213 | g_object_unref(gf); 214 | 215 | return false; 216 | } 217 | 218 | bool wyebsvr(int argc, char **argv, wyebdataf func) 219 | { 220 | if (argc < 3 || strcmp(argv[1], PREFIX)) return false; 221 | 222 | svrexe = argv[0]; 223 | dataf = func; 224 | sloop = g_main_loop_new(NULL, false); 225 | g_idle_add((GSourceFunc)svrinit, argv[2]); 226 | g_main_loop_run(sloop); 227 | 228 | return true; 229 | } 230 | 231 | 232 | typedef struct { 233 | char *caller; 234 | char *data; 235 | } Dataargs; 236 | static void getdata(gpointer p, gpointer ap) 237 | { 238 | Dataargs *args = p; 239 | 240 | if (*args->caller) 241 | { 242 | g_mutex_lock(&ordersm); 243 | g_hash_table_add(orders, args->caller); 244 | g_mutex_unlock(&ordersm); 245 | } 246 | 247 | char *data = dataf(args->data); 248 | if (*args->caller && !ipcsend(CLIDIR, args->caller, CCret, "", data)) fatal(2); 249 | g_free(data); 250 | 251 | if (*args->caller) 252 | { 253 | g_mutex_lock(&ordersm); 254 | g_hash_table_remove(orders, args->caller); 255 | g_mutex_unlock(&ordersm); 256 | } 257 | 258 | g_free(args->caller); 259 | g_free(args->data); 260 | g_free(args); 261 | } 262 | 263 | 264 | 265 | 266 | //@client 267 | typedef struct { 268 | GMutex retm; 269 | GMainContext *wctx; 270 | GMainLoop *loop; 271 | GSource *watch; 272 | char *pid; 273 | char *retdata; 274 | char *pppath; 275 | char *exe; //do not free. this is tmp 276 | } Client; 277 | 278 | static GSList *rmpath; 279 | static GMutex rmm; 280 | static void __attribute__((destructor)) removepp() 281 | { 282 | for (; rmpath; rmpath = rmpath->next) 283 | remove(rmpath->data); 284 | } 285 | static Client *makecl() 286 | { 287 | Client *c = g_new0(Client, 1); 288 | // g_mutex_init(&c->retm); 289 | static int tid; 290 | c->pid = g_strdup_printf("%d-%u", tid++, getpid()); 291 | 292 | c->wctx = g_main_context_new(); 293 | c->loop = g_main_loop_new(c->wctx, true); 294 | c->watch = ipcwatch(CLIDIR, c->pid, c->wctx, c); 295 | c->pppath = ipcpath( CLIDIR, c->pid); 296 | g_mutex_lock(&rmm); 297 | rmpath = g_slist_prepend(rmpath, c->pppath); 298 | g_mutex_unlock(&rmm); 299 | 300 | g_thread_unref(g_thread_new("wait", (GThreadFunc)g_main_loop_run, c->loop)); 301 | 302 | return c; 303 | } 304 | static void freecl(Client *c) 305 | { 306 | g_main_loop_quit(c->loop); 307 | g_source_unref(c->watch); 308 | g_main_loop_unref(c->loop); 309 | g_main_context_unref(c->wctx); 310 | g_mutex_clear(&c->retm); 311 | g_free(c->pid); 312 | g_free(c->retdata); 313 | remove(c->pppath); 314 | g_mutex_lock(&rmm); 315 | rmpath = g_slist_remove(rmpath, c->pppath); 316 | g_mutex_unlock(&rmm); 317 | g_free(c->pppath); 318 | g_free(c); 319 | } 320 | static Client *getcl() 321 | { 322 | static GMutex m; 323 | g_mutex_lock(&m); 324 | 325 | static GPrivate pc = G_PRIVATE_INIT((GDestroyNotify)freecl); 326 | Client *c = g_private_get(&pc); 327 | 328 | if (!c) 329 | { 330 | c = makecl(); 331 | g_private_set(&pc, c); 332 | } 333 | g_mutex_unlock(&m); 334 | return c; 335 | } 336 | 337 | static GHashTable *lastsec; 338 | static GMutex lastm; 339 | static void setsec(char *exe, int sec) 340 | { 341 | g_mutex_lock(&lastm); 342 | if (!lastsec) lastsec = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); 343 | g_hash_table_replace(lastsec, g_strdup(exe), g_strdup_printf("%d", sec)); 344 | g_mutex_unlock(&lastm); 345 | } 346 | static char *keepstr(char *exe) 347 | { 348 | g_mutex_lock(&lastm); 349 | char *ret = g_hash_table_lookup(lastsec, exe); 350 | g_mutex_unlock(&lastm); 351 | 352 | #define Z(v) #v 353 | return ret ?: Z(KEEPS); 354 | #undef Z 355 | } 356 | 357 | static gboolean pingloop(Client *c) 358 | { 359 | if (!ipcsend(c->exe, PING, CSping, c->pid, keepstr(c->exe))) 360 | g_mutex_unlock(&c->retm); 361 | 362 | return true; 363 | } 364 | static gboolean timeoutcb(Client *c) 365 | { 366 | g_mutex_unlock(&c->retm); 367 | return false; 368 | } 369 | 370 | //don't free 371 | static char *request(char *exe, Com type, bool caller, char *data) 372 | { 373 | Client *c = getcl(); 374 | 375 | if (caller) 376 | { 377 | g_mutex_lock(&c->retm); 378 | g_free(c->retdata); 379 | c->retdata = NULL; 380 | } 381 | 382 | if (!ipcsend(exe, INPUT, type, caller ? c->pid : NULL, data)) 383 | { //svr is not running 384 | char *path = ipcpath(exe, "lock"); 385 | if (!g_file_test(path, G_FILE_TEST_EXISTS)) 386 | mkdirif(path); 387 | 388 | int lock = open(path, O_RDONLY | O_CREAT, S_IRUSR); 389 | g_free(path); 390 | flock(lock, LOCK_EX); 391 | 392 | //retry in single proc 393 | if (!ipcsend(exe, INPUT, type, caller ? c->pid : NULL, data)) 394 | { 395 | if (!caller) 396 | g_mutex_lock(&c->retm); 397 | 398 | GSource *tout = g_timeout_source_new(KEEPS * 1000); 399 | g_source_set_callback(tout, (GSourceFunc)timeoutcb, c, NULL); 400 | g_source_attach(tout, c->wctx); 401 | 402 | 403 | char **argv = g_new0(char*, 4); 404 | argv[0] = exe; 405 | argv[1] = PREFIX; 406 | argv[2] = c->pid; 407 | GError *err = NULL; 408 | if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, 409 | NULL, NULL, NULL, &err)) 410 | { 411 | g_print("err %s", err->message); 412 | g_error_free(err); 413 | } 414 | g_free(argv); 415 | 416 | 417 | g_mutex_lock(&c->retm); 418 | g_mutex_unlock(&c->retm); 419 | 420 | g_source_destroy(tout); 421 | g_source_unref(tout); 422 | 423 | //wyebloop doesn't know svr quits 424 | wyebkeep(exe, 0); 425 | 426 | if (caller) 427 | g_mutex_lock(&c->retm); 428 | if (!ipcsend(exe, INPUT, type, caller ? c->pid : NULL, data)) 429 | P(Spawning %s failed !!, exe) 430 | } 431 | close(lock); 432 | } 433 | 434 | if (caller) 435 | { 436 | GSource *ping = g_timeout_source_new(DPINGTIME); 437 | c->exe = exe; 438 | g_source_set_callback(ping, (GSourceFunc)pingloop, c, NULL); 439 | g_source_attach(ping, c->wctx); //attach to ping thread 440 | 441 | g_mutex_lock(&c->retm); 442 | g_mutex_unlock(&c->retm); 443 | 444 | g_source_destroy(ping); 445 | g_source_unref(ping); 446 | } 447 | 448 | return c->retdata; 449 | } 450 | 451 | char *wyebget(char *exe, char *data) 452 | { 453 | return request(exe, CSdata, true, data); 454 | } 455 | void wyebsend(char *exe, char *data) 456 | { 457 | request(exe, CSdata, false, data); 458 | } 459 | static gboolean keepcb(char *exe) 460 | { 461 | request(exe, CSuntil, false, keepstr(exe)); 462 | return false; 463 | } 464 | void wyebkeep(char *exe, int sec) 465 | { 466 | if (sec) setsec(exe, sec); 467 | g_idle_add_full(G_PRIORITY_DEFAULT, (GSourceFunc)keepcb, 468 | g_strdup(exe), g_free); 469 | } 470 | 471 | static gboolean loopcb(char *exe) 472 | { 473 | keepcb(exe); 474 | return true; 475 | } 476 | guint wyebloop(char *exe, int sec) 477 | { 478 | if (sec) setsec(exe, sec); 479 | loopcb(exe); 480 | return g_timeout_add_full(G_PRIORITY_DEFAULT, sec * 300, 481 | (GSourceFunc)loopcb, g_strdup(exe), g_free); 482 | } 483 | 484 | 485 | #if TESTER 486 | static void testget(gpointer p, gpointer ap) 487 | { 488 | // P(ret %s - %s, wyebget(ap, p), (char *)p) 489 | wyebget(ap, p); 490 | g_free(p); 491 | } 492 | #endif 493 | static gboolean tcinputcb(GIOChannel *ch, GIOCondition cnd, char *exe) 494 | { 495 | char *line; 496 | if (cnd == G_IO_HUP || G_IO_STATUS_EOF == 497 | g_io_channel_read_line(ch, &line, NULL, NULL, NULL)) 498 | exit(0); 499 | 500 | if (!line) return true; 501 | 502 | g_strstrip(line); 503 | if (!strlen(line)) 504 | exit(0); 505 | 506 | #if TESTER 507 | if (g_str_has_prefix(line, "l")) 508 | { 509 | GThreadPool *pool = g_thread_pool_new(testget, exe, 32, false, NULL); 510 | 511 | gint64 start = g_get_monotonic_time(); 512 | for (int i = 0; i < 100000; i++) 513 | { 514 | char *is = g_strdup_printf("l%d", i); 515 | //g_print("loop %d ret %s\n", i, wyebget(exe, is)); 516 | if (*(line + 1) == 's') 517 | wyebsend(exe, is); 518 | else if (*(line + 1) == 'p') 519 | D(pint %s, is) 520 | else if (*(line + 1) == 'c') 521 | { 522 | gchar *data; 523 | g_spawn_command_line_sync("echo 'ret'", &data, NULL, NULL, NULL); 524 | g_strchomp(data); 525 | D(cmd %s %s, is, data) 526 | g_free(data); 527 | } 528 | else 529 | { 530 | // wyebget(exe, is); 531 | g_thread_pool_push(pool, is, NULL); 532 | is = NULL; 533 | } 534 | 535 | g_free(is); 536 | } 537 | g_thread_pool_free(pool, false, true); 538 | 539 | gint64 now = g_get_monotonic_time(); 540 | P(time %f, (now - start) / 1000000.0) 541 | } 542 | else 543 | #endif 544 | P(%s, wyebget(exe, line)) 545 | 546 | g_free(line); 547 | return true; 548 | } 549 | static gboolean tcinit(char *exe) 550 | { 551 | #if DEBUG 552 | wyebloop(exe, 300); 553 | #else 554 | wyebloop(exe, 2); 555 | #endif 556 | 557 | GIOChannel *io = g_io_channel_unix_new(fileno(stdin)); 558 | g_io_add_watch(io, G_IO_IN | G_IO_HUP, (GIOFunc)tcinputcb, exe); 559 | 560 | return false; 561 | } 562 | void wyebclient(char *exe) 563 | { 564 | GMainLoop *loop = g_main_loop_new(NULL, false); 565 | g_idle_add((GSourceFunc)tcinit, exe); 566 | g_main_loop_run(loop); 567 | } 568 | 569 | 570 | 571 | //@ipccb 572 | gboolean ipccb(GIOChannel *ch, GIOCondition cnd, gpointer p) 573 | { 574 | //D(ipccb %c, svrexe ? 'S':'C') 575 | char *line; 576 | g_io_channel_read_line(ch, &line, NULL, NULL, NULL); 577 | if (!line) return true; 578 | 579 | Com type = *line; 580 | char *id = line + 1; 581 | char *data = strchr(line, ':'); 582 | *data++ = '\0'; 583 | g_strchomp(data); 584 | 585 | #if DEBUG 586 | static int i; 587 | D(%c ipccb%d %c/%s/%lu;, svrexe ? 'S':'C', i++, type ,id, strlen(data)) 588 | #endif 589 | 590 | switch (type) { 591 | //server 592 | case CSdata: 593 | { 594 | Dataargs *args = g_new(Dataargs, 1); 595 | args->caller = g_strdup(id); 596 | args->data = g_strcompress(data); 597 | 598 | static GThreadPool *pool; 599 | if (!pool) pool = g_thread_pool_new(getdata, NULL, -1, false, NULL); 600 | g_thread_pool_push(pool, args, NULL); 601 | break; 602 | } 603 | case CSping: 604 | g_mutex_lock(&ordersm); 605 | if (!g_hash_table_lookup(orders, id)) 606 | ipcsend(CLIDIR, id, CClost, NULL, NULL); 607 | g_mutex_unlock(&ordersm); 608 | case CSuntil: 609 | until(atoi(data)); 610 | break; 611 | 612 | //client 613 | case CCret: 614 | case CClost: 615 | case CCwoke: 616 | { 617 | Client *c = p; 618 | if (type == CCret) 619 | c->retdata = g_strcompress(data); 620 | 621 | //for the case pinging at same time of ret 622 | g_mutex_trylock(&c->retm); 623 | g_mutex_unlock(&c->retm); 624 | g_thread_yield(); 625 | break; 626 | } 627 | } 628 | 629 | g_free(line); 630 | return true; 631 | } 632 | 633 | 634 | 635 | //test 636 | #if TESTER 637 | static char *testdata(char *data) 638 | { 639 | //sleep(9); 640 | //g_free(data); //makes crash 641 | 642 | static int i; 643 | return g_strdup_printf("%d th test data. req is %s", ++i, data); 644 | } 645 | 646 | int main(int argc, char **argv) 647 | { 648 | // start = g_get_monotonic_time(); 649 | // gint64 now = g_get_monotonic_time(); 650 | // D(time %ld %ld, now - start, now) 651 | 652 | if (!wyebsvr(argc, argv, testdata)) 653 | wyebclient(argv[0]); 654 | 655 | exit(0); 656 | } 657 | #endif 658 | -------------------------------------------------------------------------------- /ephy-uri-tester.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2011 Igalia S.L. 3 | * 4 | * This file is part of Epiphany. 5 | * 6 | * Epiphany is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * Epiphany is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with Epiphany. If not, see . 18 | * 19 | * Some parts of this file based on the Midori's 'adblock' extension, 20 | * licensed with the GNU Lesser General Public License 2.1, Copyright 21 | * (C) 2009-2010 Christian Dywan and 2009 22 | * Alexander Butenko . Check Midori's web site 23 | * at http://www.twotoasts.de 24 | */ 25 | 26 | #define LOG(...) 27 | /* 28 | #include "config.h" 29 | */ 30 | #include "ephy-uri-tester.h" 31 | 32 | /* 33 | #include "ephy-debug.h" 34 | #include "ephy-prefs.h" 35 | #include "ephy-settings.h" 36 | #include "ephy-uri-tester-shared.h" 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | */ 43 | 44 | #define SIGNATURE_SIZE 8 45 | 46 | struct _EphyUriTester { 47 | GObject parent_instance; 48 | 49 | char *adblock_data_dir; 50 | 51 | GHashTable *pattern; 52 | GHashTable *keys; 53 | GHashTable *optslist; 54 | GHashTable *urlcache; 55 | 56 | GHashTable *whitelisted_pattern; 57 | GHashTable *whitelisted_keys; 58 | GHashTable *whitelisted_optslist; 59 | GHashTable *whitelisted_urlcache; 60 | 61 | GString *blockcss; 62 | GString *blockcssprivate; 63 | 64 | GRegex *regex_third_party; 65 | GRegex *regex_pattern; 66 | GRegex *regex_subdocument; 67 | GRegex *regex_frame_add; 68 | 69 | GMainLoop *load_loop; 70 | int adblock_filters_to_load; 71 | gboolean adblock_loaded; 72 | }; 73 | 74 | enum { 75 | PROP_0, 76 | PROP_ADBLOCK_DATA_DIR, 77 | LAST_PROP 78 | }; 79 | 80 | static GParamSpec *obj_properties[LAST_PROP]; 81 | 82 | G_DEFINE_TYPE (EphyUriTester, ephy_uri_tester, G_TYPE_OBJECT) 83 | 84 | static GString * 85 | ephy_uri_tester_fixup_regexp (const char *prefix, char *src); 86 | 87 | static inline int 88 | ephy_uri_tester_check_rule (EphyUriTester *tester, 89 | GRegex *regex, 90 | const char *patt, 91 | const char *req_uri, 92 | const char *page_uri, 93 | gboolean whitelist) 94 | { 95 | char *opts; 96 | GHashTable *optslist = tester->optslist; 97 | if (whitelist) 98 | optslist = tester->whitelisted_optslist; 99 | 100 | if (!g_regex_match_full (regex, req_uri, -1, 0, 0, NULL, NULL)) 101 | return FALSE; 102 | 103 | opts = g_hash_table_lookup (optslist, patt); 104 | if (opts && g_regex_match (tester->regex_third_party, opts, 0, NULL)) { 105 | if (page_uri && g_regex_match_full (regex, page_uri, -1, 0, 0, NULL, NULL)) 106 | return FALSE; 107 | } 108 | /* TODO: Domain and document opt check */ 109 | if (whitelist) 110 | LOG ("whitelisted by pattern regexp=%s -- %s", g_regex_get_pattern (regex), req_uri); 111 | else 112 | LOG ("blocked by pattern regexp=%s -- %s", g_regex_get_pattern (regex), req_uri); 113 | return TRUE; 114 | } 115 | 116 | static inline gboolean 117 | ephy_uri_tester_is_matched_by_pattern (EphyUriTester *tester, 118 | const char *req_uri, 119 | const char *page_uri, 120 | gboolean whitelist) 121 | { 122 | GHashTableIter iter; 123 | gpointer patt, regex; 124 | GHashTable *pattern = tester->pattern; 125 | if (whitelist) 126 | pattern = tester->whitelisted_pattern; 127 | 128 | g_hash_table_iter_init (&iter, pattern); 129 | while (g_hash_table_iter_next (&iter, &patt, ®ex)) { 130 | if (ephy_uri_tester_check_rule (tester, regex, patt, req_uri, page_uri, whitelist)) 131 | return TRUE; 132 | } 133 | return FALSE; 134 | } 135 | 136 | static inline gboolean 137 | ephy_uri_tester_is_matched_by_key (EphyUriTester *tester, 138 | const char *opts, 139 | const char *req_uri, 140 | const char *page_uri, 141 | gboolean whitelist) 142 | { 143 | char *uri; 144 | int len; 145 | int pos = 0; 146 | GList *regex_bl = NULL; 147 | GString *guri; 148 | gboolean ret = FALSE; 149 | char sig[SIGNATURE_SIZE + 1]; 150 | GHashTable *keys = tester->keys; 151 | if (whitelist) 152 | keys = tester->whitelisted_keys; 153 | 154 | memset (&sig[0], 0, sizeof (sig)); 155 | /* Signatures are made on pattern, so we need to convert url to a pattern as well */ 156 | guri = ephy_uri_tester_fixup_regexp ("", (char *)req_uri); 157 | uri = guri->str; 158 | len = guri->len; 159 | 160 | for (pos = len - SIGNATURE_SIZE; pos >= 0; pos--) { 161 | GRegex *regex; 162 | strncpy (sig, uri + pos, SIGNATURE_SIZE); 163 | regex = g_hash_table_lookup (keys, sig); 164 | 165 | /* Dont check if regex is already blacklisted */ 166 | if (!regex || g_list_find (regex_bl, regex)) 167 | continue; 168 | ret = ephy_uri_tester_check_rule (tester, regex, sig, req_uri, page_uri, whitelist); 169 | if (ret) 170 | break; 171 | regex_bl = g_list_prepend (regex_bl, regex); 172 | } 173 | g_string_free (guri, TRUE); 174 | g_list_free (regex_bl); 175 | return ret; 176 | } 177 | 178 | static gboolean 179 | ephy_uri_tester_is_matched (EphyUriTester *tester, 180 | const char *opts, 181 | const char *req_uri, 182 | const char *page_uri, 183 | gboolean whitelist) 184 | { 185 | gpointer is_matched; 186 | GHashTable *urlcache = tester->urlcache; 187 | if (whitelist) 188 | urlcache = tester->whitelisted_urlcache; 189 | 190 | /* Check cached URLs first. */ 191 | if (g_hash_table_lookup_extended (urlcache, req_uri, NULL, &is_matched)) 192 | return GPOINTER_TO_INT (is_matched); 193 | 194 | /* Look for a match either by key or by pattern. */ 195 | if (ephy_uri_tester_is_matched_by_key (tester, opts, req_uri, page_uri, whitelist)) { 196 | g_hash_table_insert (urlcache, g_strdup (req_uri), GINT_TO_POINTER (TRUE)); 197 | return TRUE; 198 | } 199 | 200 | /* Matching by pattern is pretty expensive, so do it if needed only. */ 201 | if (ephy_uri_tester_is_matched_by_pattern (tester, req_uri, page_uri, whitelist)) { 202 | g_hash_table_insert (urlcache, g_strdup (req_uri), GINT_TO_POINTER (TRUE)); 203 | return TRUE; 204 | } 205 | 206 | /* No match. */ 207 | g_hash_table_insert (urlcache, g_strdup (req_uri), GINT_TO_POINTER (FALSE)); 208 | return FALSE; 209 | } 210 | 211 | static GString * 212 | ephy_uri_tester_fixup_regexp (const char *prefix, char *src) 213 | { 214 | GString *str; 215 | 216 | if (!src) 217 | return NULL; 218 | 219 | str = g_string_new (prefix); 220 | 221 | /* lets strip first .* */ 222 | if (src[0] == '*') { 223 | (void)*src++; 224 | } 225 | 226 | /* NOTE: The '$' is used as separator for the rule options, so rule patterns 227 | cannot ever contain them. If a rule needs to match it, it uses "%24". 228 | Splitting the option is done in ephy_uri_tester_add_url_pattern(). 229 | 230 | The loop below always escapes square brackets. This way there is no chance 231 | that they get interpreted as a character class, and it is NOT needed to 232 | escape '-' because it's only special inside a character class. */ 233 | do { 234 | switch (*src) { 235 | case '*': 236 | g_string_append (str, ".*"); 237 | break; 238 | case '^': 239 | /* Matches a separator character, defined as: 240 | * "anything but a letter, a digit, or one of the following: _ - . %" */ 241 | g_string_append (str, "([^a-zA-Z\\d]|[_\\-\\.%])"); 242 | break; 243 | case '|': 244 | /* If at the end of the pattern, the match is anchored at the end. In 245 | * the middle of a pattern it matches a literal vertical bar and the 246 | * character must be escaped. */ 247 | if (src[1] == '\0') 248 | g_string_append (str, "$"); 249 | else 250 | g_string_append (str, "\\|"); 251 | break; 252 | /* The following characters are escaped as they have a meaning in 253 | * regular expressions: 254 | * - '.' matches any character. 255 | * - '+' matches the preceding pattern one or more times. 256 | * - '?' matches the preceding pattern zero or one times. 257 | * - '[' ']' are used to define a character class. 258 | * - '{' '}' are used to define a min/max quantifier. 259 | * - '(' ')' are used to defin a submatch expression. 260 | * - '\' has several uses in regexps (shortcut character classes. 261 | * matching non-printing characters, using octal/hex, octal 262 | * constants, backreferences... they must to be escaped to 263 | * match a literal backslash and prevent wrecking havoc!). */ 264 | case '.': 265 | case '+': 266 | case '?': 267 | case '[': 268 | case ']': 269 | case '{': 270 | case '}': 271 | case '(': 272 | case ')': 273 | case '\\': 274 | g_string_append_printf (str, "\\%c", *src); 275 | break; 276 | default: 277 | g_string_append_printf (str, "%c", *src); 278 | break; 279 | } 280 | src++; 281 | } while (*src); 282 | 283 | return str; 284 | } 285 | 286 | static void 287 | ephy_uri_tester_compile_regexp (EphyUriTester *tester, 288 | GString *gpatt, 289 | const char *opts, 290 | gboolean whitelist) 291 | { 292 | GHashTable *pattern; 293 | GHashTable *keys; 294 | GHashTable *optslist; 295 | GRegex *regex; 296 | GError *error = NULL; 297 | char *patt; 298 | int len; 299 | 300 | if (!gpatt) 301 | return; 302 | 303 | patt = gpatt->str; 304 | len = gpatt->len; 305 | 306 | /* TODO: Play with optimization flags */ 307 | regex = g_regex_new (patt, G_REGEX_OPTIMIZE, 308 | G_REGEX_MATCH_NOTEMPTY, &error); 309 | if (error) { 310 | g_warning ("%s: %s", G_STRFUNC, error->message); 311 | g_error_free (error); 312 | return; 313 | } 314 | 315 | pattern = tester->pattern; 316 | keys = tester->keys; 317 | optslist = tester->optslist; 318 | if (whitelist) { 319 | pattern = tester->whitelisted_pattern; 320 | keys = tester->whitelisted_keys; 321 | optslist = tester->whitelisted_optslist; 322 | } 323 | 324 | if (!g_regex_match (tester->regex_pattern, patt, 0, NULL)) { 325 | int signature_count = 0; 326 | int pos = 0; 327 | char *sig; 328 | 329 | for (pos = len - SIGNATURE_SIZE; pos >= 0; pos--) { 330 | sig = g_strndup (patt + pos, SIGNATURE_SIZE); 331 | if (!strchr (sig, '*') && 332 | !g_hash_table_lookup (keys, sig)) { 333 | LOG ("sig: %s %s", sig, patt); 334 | g_hash_table_insert (keys, g_strdup (sig), g_regex_ref (regex)); 335 | g_hash_table_insert (optslist, g_strdup (sig), g_strdup (opts)); 336 | signature_count++; 337 | } else { 338 | if (sig[0] == '*' && 339 | !g_hash_table_lookup (pattern, patt)) { 340 | LOG ("patt2: %s %s", sig, patt); 341 | g_hash_table_insert (pattern, g_strdup (patt), g_regex_ref (regex)); 342 | g_hash_table_insert (optslist, g_strdup (patt), g_strdup (opts)); 343 | } 344 | } 345 | g_free (sig); 346 | } 347 | g_regex_unref (regex); 348 | 349 | if (signature_count > 1 && g_hash_table_lookup (pattern, patt)) 350 | g_hash_table_remove (pattern, patt); 351 | } else { 352 | LOG ("patt: %s%s", patt, ""); 353 | /* Pattern is a regexp chars */ 354 | g_hash_table_insert (pattern, g_strdup (patt), regex); 355 | g_hash_table_insert (optslist, g_strdup (patt), g_strdup (opts)); 356 | } 357 | } 358 | 359 | static void 360 | ephy_uri_tester_add_url_pattern (EphyUriTester *tester, 361 | const char *prefix, 362 | const char *type, 363 | char *line, 364 | gboolean whitelist) 365 | { 366 | char **data; 367 | char *patt; 368 | GString *format_patt; 369 | const char *opts; 370 | 371 | data = g_strsplit (line, "$", -1); 372 | if (!data || !data[0]) { 373 | g_strfreev (data); 374 | return; 375 | } 376 | 377 | if (data[1] && data[2]) { 378 | patt = g_strconcat (data[0], data[1], NULL); 379 | opts = g_strconcat (type, ",", data[2], NULL); 380 | } else if (data[1]) { 381 | patt = data[0]; 382 | opts = g_strconcat (type, ",", data[1], NULL); 383 | } else { 384 | patt = data[0]; 385 | opts = type; 386 | } 387 | 388 | if (g_regex_match (tester->regex_subdocument, opts, 0, NULL)) { 389 | if (data[1] && data[2]) 390 | g_free (patt); 391 | if (data[1]) 392 | g_free ((char *)opts); 393 | g_strfreev (data); 394 | return; 395 | } 396 | 397 | format_patt = ephy_uri_tester_fixup_regexp (prefix, patt); 398 | 399 | if (whitelist) 400 | LOG ("whitelist: %s opts %s", format_patt->str, opts); 401 | else 402 | LOG ("blacklist: %s opts %s", format_patt->str, opts); 403 | 404 | ephy_uri_tester_compile_regexp (tester, format_patt, opts, whitelist); 405 | 406 | if (data[1] && data[2]) 407 | g_free (patt); 408 | if (data[1]) 409 | g_free ((char *)opts); 410 | g_strfreev (data); 411 | 412 | g_string_free (format_patt, TRUE); 413 | } 414 | 415 | static inline void 416 | ephy_uri_tester_frame_add (EphyUriTester *tester, char *line) 417 | { 418 | /* 419 | const char *separator = " , "; 420 | */ 421 | //for wyeb 422 | const char *separator = ",\n"; 423 | 424 | (void)*line++; 425 | (void)*line++; 426 | if (strchr (line, '\'') 427 | || (strchr (line, ':') 428 | && !g_regex_match (tester->regex_frame_add, line, 0, NULL))) { 429 | return; 430 | } 431 | g_string_append (tester->blockcss, separator); 432 | g_string_append (tester->blockcss, line); 433 | } 434 | 435 | static inline void 436 | ephy_uri_tester_frame_add_private (EphyUriTester *tester, 437 | const char *line, 438 | const char *sep) 439 | { 440 | char **data; 441 | data = g_strsplit (line, sep, 2); 442 | 443 | if (!(data[1] && *data[1]) 444 | || strchr (data[1], '\'') 445 | || (strchr (data[1], ':') 446 | && !g_regex_match (tester->regex_frame_add, data[1], 0, NULL))) { 447 | g_strfreev (data); 448 | return; 449 | } 450 | 451 | if (strchr (data[0], ',')) { 452 | char **domains; 453 | int i; 454 | 455 | domains = g_strsplit (data[0], ",", -1); 456 | for (i = 0; domains[i]; i++) { 457 | g_string_append_printf (tester->blockcssprivate, ";sites['%s']+=',%s'", 458 | g_strstrip (domains[i]), data[1]); 459 | } 460 | g_strfreev (domains); 461 | } else { 462 | g_string_append_printf (tester->blockcssprivate, ";sites['%s']+=',%s'", 463 | data[0], data[1]); 464 | } 465 | g_strfreev (data); 466 | } 467 | 468 | static void 469 | ephy_uri_tester_parse_line (EphyUriTester *tester, 470 | char *line, 471 | gboolean whitelist) 472 | { 473 | if (!line) 474 | return; 475 | 476 | g_strchomp (line); 477 | /* Ignore comments and new lines */ 478 | if (line[0] == '!') 479 | return; 480 | /* FIXME: No support for [include] and [exclude] tags */ 481 | if (line[0] == '[') 482 | return; 483 | 484 | /* Whitelisted exception rules */ 485 | if (g_str_has_prefix (line, "@@")) { 486 | ephy_uri_tester_parse_line (tester, line + 2, TRUE); 487 | return; 488 | } 489 | 490 | /* FIXME: No support for domain= */ 491 | if (strstr (line, "domain=")) 492 | return; 493 | 494 | /* Skip garbage */ 495 | if (line[0] == ' ' || !line[0]) 496 | return; 497 | 498 | /* Got CSS block hider */ 499 | if (line[0] == '#' && line[1] == '#') { 500 | ephy_uri_tester_frame_add (tester, line); 501 | return; 502 | } 503 | /* Got CSS block hider. Workaround */ 504 | if (line[0] == '#') 505 | return; 506 | 507 | /* Got per domain CSS hider rule */ 508 | if (strstr (line, "##")) { 509 | ephy_uri_tester_frame_add_private (tester, line, "##"); 510 | return; 511 | } 512 | 513 | /* Got per domain CSS hider rule. Workaround */ 514 | if (strchr (line, '#')) { 515 | ephy_uri_tester_frame_add_private (tester, line, "#"); 516 | return; 517 | } 518 | /* Got URL blocker rule */ 519 | if (line[0] == '|' && line[1] == '|') { 520 | (void)*line++; 521 | (void)*line++; 522 | /* set a regex prefix to ensure that '||' patterns are anchored at the 523 | * start and that any characters (if any) preceding the domain specified 524 | * by the rule is separated from it by a dot '.' */ 525 | ephy_uri_tester_add_url_pattern (tester, "^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?", "fulluri", line, whitelist); 526 | return; 527 | } 528 | if (line[0] == '|') { 529 | (void)*line++; 530 | ephy_uri_tester_add_url_pattern (tester, "^", "fulluri", line, whitelist); 531 | return; 532 | } 533 | ephy_uri_tester_add_url_pattern (tester, "", "uri", line, whitelist); 534 | } 535 | 536 | static void 537 | ephy_uri_tester_adblock_loaded (EphyUriTester *tester) 538 | { 539 | if (g_atomic_int_dec_and_test (&tester->adblock_filters_to_load)) { 540 | tester->adblock_loaded = TRUE; 541 | g_main_loop_quit (tester->load_loop); 542 | } 543 | } 544 | 545 | static void 546 | file_parse_cb (GDataInputStream *stream, GAsyncResult *result, EphyUriTester *tester) 547 | { 548 | char *line; 549 | GError *error = NULL; 550 | 551 | line = g_data_input_stream_read_line_finish (stream, result, NULL, &error); 552 | if (!line) { 553 | if (error) { 554 | g_warning ("Error parsing file: %s\n", error->message); 555 | g_error_free (error); 556 | } 557 | 558 | ephy_uri_tester_adblock_loaded (tester); 559 | return; 560 | } 561 | 562 | ephy_uri_tester_parse_line (tester, line, FALSE); 563 | g_free (line); 564 | 565 | g_data_input_stream_read_line_async (stream, G_PRIORITY_DEFAULT_IDLE, NULL, 566 | (GAsyncReadyCallback)file_parse_cb, tester); 567 | } 568 | 569 | static void 570 | file_read_cb (GFile *file, GAsyncResult *result, EphyUriTester *tester) 571 | { 572 | GFileInputStream *stream; 573 | GDataInputStream *data_stream; 574 | GError *error = NULL; 575 | 576 | stream = g_file_read_finish (file, result, &error); 577 | if (!stream) { 578 | char *path; 579 | 580 | path = g_file_get_path (file); 581 | g_warning ("Error opening file %s for parsing: %s\n", path, error->message); 582 | g_free (path); 583 | g_error_free (error); 584 | 585 | ephy_uri_tester_adblock_loaded (tester); 586 | return; 587 | } 588 | 589 | data_stream = g_data_input_stream_new (G_INPUT_STREAM (stream)); 590 | g_object_unref (stream); 591 | 592 | g_data_input_stream_read_line_async (data_stream, G_PRIORITY_DEFAULT_IDLE, NULL, 593 | (GAsyncReadyCallback)file_parse_cb, tester); 594 | g_object_unref (data_stream); 595 | } 596 | 597 | static gboolean 598 | ephy_uri_tester_block_uri (EphyUriTester *tester, 599 | const char *req_uri, 600 | const char *page_uri) 601 | { 602 | /* check whitelisting rules before the normal ones */ 603 | if (ephy_uri_tester_is_matched (tester, NULL, req_uri, page_uri, TRUE)) 604 | return FALSE; 605 | return ephy_uri_tester_is_matched (tester, NULL, req_uri, page_uri, FALSE); 606 | } 607 | 608 | char * 609 | ephy_uri_tester_rewrite_uri (EphyUriTester *tester, 610 | const char *request_uri, 611 | const char *page_uri) 612 | { 613 | /* Should we block the URL outright? */ 614 | if (ephy_uri_tester_block_uri (tester, request_uri, page_uri)) { 615 | g_debug ("Request '%s' blocked (page: '%s')", request_uri, page_uri); 616 | 617 | return NULL; 618 | } 619 | 620 | return g_strdup (request_uri); 621 | } 622 | 623 | static void 624 | adblock_file_monitor_changed (GFileMonitor *monitor, 625 | GFile *file, 626 | GFile *other_file, 627 | GFileMonitorEvent event_type, 628 | EphyUriTester *tester) 629 | { 630 | if (event_type != G_FILE_MONITOR_EVENT_RENAMED) 631 | return; 632 | 633 | g_signal_handlers_disconnect_by_func (monitor, adblock_file_monitor_changed, tester); 634 | g_file_read_async (other_file, G_PRIORITY_DEFAULT_IDLE, NULL, 635 | (GAsyncReadyCallback)file_read_cb, 636 | tester); 637 | } 638 | 639 | 640 | //for wyeb 641 | static GFile *filter_file; 642 | 643 | 644 | static void 645 | ephy_uri_tester_begin_loading_adblock_filters (EphyUriTester *tester, 646 | GList **monitors) 647 | { 648 | //for wyeb 649 | tester->adblock_filters_to_load = 1; 650 | /* 651 | char **filters; 652 | 653 | filters = g_settings_get_strv (EPHY_SETTINGS_MAIN, EPHY_PREFS_ADBLOCK_FILTERS); 654 | tester->adblock_filters_to_load = g_strv_length (filters); 655 | for (guint i = 0; filters[i]; i++) { 656 | GFile *filter_file; 657 | 658 | filter_file = ephy_uri_tester_get_adblock_filter_file (tester->adblock_data_dir, filters[i]); 659 | */ 660 | if (!g_file_query_exists (filter_file, NULL)) { 661 | GFileMonitor *monitor; 662 | GError *error = NULL; 663 | 664 | monitor = g_file_monitor_file (filter_file, G_FILE_MONITOR_WATCH_MOVES, NULL, &error); 665 | if (monitor) { 666 | *monitors = g_list_prepend (*monitors, monitor); 667 | g_signal_connect (monitor, "changed", G_CALLBACK (adblock_file_monitor_changed), tester); 668 | } else { 669 | g_warning ("Failed to monitor adblock file: %s\n", error->message); 670 | g_error_free (error); 671 | ephy_uri_tester_adblock_loaded (tester); 672 | } 673 | } else { 674 | g_file_read_async (filter_file, G_PRIORITY_DEFAULT_IDLE, NULL, 675 | (GAsyncReadyCallback)file_read_cb, 676 | tester); 677 | } 678 | g_object_unref (filter_file); 679 | /* 680 | } 681 | g_strfreev (filters); 682 | */ 683 | } 684 | 685 | static void 686 | ephy_uri_tester_load_sync (GTask *task, 687 | EphyUriTester *tester) 688 | { 689 | GMainContext *context; 690 | GList *monitors = NULL; 691 | 692 | context = g_main_context_new (); 693 | g_main_context_push_thread_default (context); 694 | tester->load_loop = g_main_loop_new (context, FALSE); 695 | 696 | if (!tester->adblock_loaded) 697 | ephy_uri_tester_begin_loading_adblock_filters (tester, &monitors); 698 | 699 | g_main_loop_run (tester->load_loop); 700 | 701 | g_list_free_full (monitors, g_object_unref); 702 | g_main_context_pop_thread_default (context); 703 | g_main_context_unref (context); 704 | g_main_loop_unref (tester->load_loop); 705 | 706 | g_task_return_boolean (task, TRUE); 707 | } 708 | 709 | static void 710 | ephy_uri_tester_init (EphyUriTester *tester) 711 | { 712 | LOG ("EphyUriTester initializing %p", tester); 713 | 714 | tester->pattern = g_hash_table_new_full (g_str_hash, g_str_equal, 715 | (GDestroyNotify)g_free, 716 | (GDestroyNotify)g_regex_unref); 717 | tester->keys = g_hash_table_new_full (g_str_hash, g_str_equal, 718 | (GDestroyNotify)g_free, 719 | (GDestroyNotify)g_regex_unref); 720 | tester->optslist = g_hash_table_new_full (g_str_hash, g_str_equal, 721 | (GDestroyNotify)g_free, 722 | (GDestroyNotify)g_free); 723 | tester->urlcache = g_hash_table_new_full (g_str_hash, g_str_equal, 724 | (GDestroyNotify)g_free, 725 | NULL); 726 | 727 | tester->whitelisted_pattern = g_hash_table_new_full (g_str_hash, g_str_equal, 728 | (GDestroyNotify)g_free, 729 | (GDestroyNotify)g_regex_unref); 730 | tester->whitelisted_keys = g_hash_table_new_full (g_str_hash, g_str_equal, 731 | (GDestroyNotify)g_free, 732 | (GDestroyNotify)g_regex_unref); 733 | tester->whitelisted_optslist = g_hash_table_new_full (g_str_hash, g_str_equal, 734 | (GDestroyNotify)g_free, 735 | (GDestroyNotify)g_free); 736 | tester->whitelisted_urlcache = g_hash_table_new_full (g_str_hash, g_str_equal, 737 | (GDestroyNotify)g_free, 738 | NULL); 739 | 740 | tester->blockcss = g_string_new ("z-non-exist"); 741 | tester->blockcssprivate = g_string_new (""); 742 | 743 | tester->regex_third_party = g_regex_new (",third-party", 744 | G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 745 | G_REGEX_MATCH_NOTEMPTY, 746 | NULL); 747 | tester->regex_pattern = g_regex_new ("^/.*[\\^\\$\\*].*/$", 748 | G_REGEX_UNGREEDY | G_REGEX_OPTIMIZE, 749 | G_REGEX_MATCH_NOTEMPTY, 750 | NULL); 751 | tester->regex_subdocument = g_regex_new ("subdocument", 752 | G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 753 | G_REGEX_MATCH_NOTEMPTY, 754 | NULL); 755 | tester->regex_frame_add = g_regex_new (".*\\[.*:.*\\].*", 756 | G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 757 | G_REGEX_MATCH_NOTEMPTY, 758 | NULL); 759 | } 760 | 761 | static void 762 | ephy_uri_tester_set_property (GObject *object, 763 | guint prop_id, 764 | const GValue *value, 765 | GParamSpec *pspec) 766 | { 767 | EphyUriTester *tester = EPHY_URI_TESTER (object); 768 | 769 | switch (prop_id) { 770 | case PROP_ADBLOCK_DATA_DIR: 771 | tester->adblock_data_dir = g_value_dup_string (value); 772 | break; 773 | default: 774 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); 775 | break; 776 | } 777 | } 778 | 779 | static void 780 | ephy_uri_tester_finalize (GObject *object) 781 | { 782 | EphyUriTester *tester = EPHY_URI_TESTER (object); 783 | 784 | LOG ("EphyUriTester finalizing %p", object); 785 | 786 | g_free (tester->adblock_data_dir); 787 | 788 | g_hash_table_destroy (tester->pattern); 789 | g_hash_table_destroy (tester->keys); 790 | g_hash_table_destroy (tester->optslist); 791 | g_hash_table_destroy (tester->urlcache); 792 | 793 | g_hash_table_destroy (tester->whitelisted_pattern); 794 | g_hash_table_destroy (tester->whitelisted_keys); 795 | g_hash_table_destroy (tester->whitelisted_optslist); 796 | g_hash_table_destroy (tester->whitelisted_urlcache); 797 | 798 | g_string_free (tester->blockcss, TRUE); 799 | g_string_free (tester->blockcssprivate, TRUE); 800 | 801 | g_regex_unref (tester->regex_third_party); 802 | g_regex_unref (tester->regex_pattern); 803 | g_regex_unref (tester->regex_subdocument); 804 | g_regex_unref (tester->regex_frame_add); 805 | 806 | G_OBJECT_CLASS (ephy_uri_tester_parent_class)->finalize (object); 807 | } 808 | 809 | static void 810 | ephy_uri_tester_class_init (EphyUriTesterClass *klass) 811 | { 812 | GObjectClass *object_class = G_OBJECT_CLASS (klass); 813 | 814 | object_class->set_property = ephy_uri_tester_set_property; 815 | object_class->finalize = ephy_uri_tester_finalize; 816 | 817 | obj_properties[PROP_ADBLOCK_DATA_DIR] = 818 | g_param_spec_string ("adblock-data-dir", 819 | "Adblock data dir", 820 | "The adblock data dir", 821 | NULL, 822 | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); 823 | 824 | g_object_class_install_properties (object_class, LAST_PROP, obj_properties); 825 | } 826 | 827 | EphyUriTester * 828 | ephy_uri_tester_new (const char *adblock_data_dir) 829 | { 830 | return EPHY_URI_TESTER (g_object_new (EPHY_TYPE_URI_TESTER, "adblock-data-dir", adblock_data_dir, NULL)); 831 | } 832 | 833 | /* 834 | static void 835 | ephy_uri_tester_reload_adblock_filters (EphyUriTester *tester) 836 | { 837 | g_hash_table_remove_all (tester->pattern); 838 | g_hash_table_remove_all (tester->keys); 839 | g_hash_table_remove_all (tester->optslist); 840 | g_hash_table_remove_all (tester->urlcache); 841 | 842 | g_hash_table_remove_all (tester->whitelisted_pattern); 843 | g_hash_table_remove_all (tester->whitelisted_keys); 844 | g_hash_table_remove_all (tester->whitelisted_optslist); 845 | g_hash_table_remove_all (tester->whitelisted_urlcache); 846 | 847 | tester->adblock_loaded = FALSE; 848 | ephy_uri_tester_load (tester); 849 | } 850 | 851 | static void 852 | ephy_uri_tester_adblock_filters_changed_cb (GSettings *settings, 853 | char *key, 854 | EphyUriTester *tester) 855 | { 856 | ephy_uri_tester_reload_adblock_filters (tester); 857 | } 858 | 859 | static void 860 | ephy_uri_tester_enable_adblock_changed_cb (GSettings *settings, 861 | char *key, 862 | EphyUriTester *tester) 863 | { 864 | ephy_uri_tester_reload_adblock_filters (tester); 865 | } 866 | */ 867 | 868 | void 869 | ephy_uri_tester_load (EphyUriTester *tester) 870 | { 871 | GTask *task; 872 | /* 873 | char **trash; 874 | 875 | g_assert (EPHY_IS_URI_TESTER (tester)); 876 | 877 | if (!g_settings_get_boolean (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_ENABLE_ADBLOCK)) 878 | tester->adblock_loaded = TRUE; 879 | */ 880 | 881 | if (tester->adblock_loaded) 882 | return; 883 | 884 | /* 885 | g_signal_handlers_disconnect_by_func (EPHY_SETTINGS_WEB, ephy_uri_tester_adblock_filters_changed_cb, tester); 886 | g_signal_handlers_disconnect_by_func (EPHY_SETTINGS_WEB, ephy_uri_tester_enable_adblock_changed_cb, tester); 887 | */ 888 | 889 | task = g_task_new (tester, NULL, NULL, NULL); 890 | g_task_run_in_thread_sync (task, (GTaskThreadFunc)ephy_uri_tester_load_sync); 891 | g_object_unref (task); 892 | 893 | /* 894 | g_signal_connect (EPHY_SETTINGS_MAIN, "changed::" EPHY_PREFS_ADBLOCK_FILTERS, 895 | G_CALLBACK (ephy_uri_tester_adblock_filters_changed_cb), tester); 896 | g_signal_connect (EPHY_SETTINGS_WEB, "changed::" EPHY_PREFS_WEB_ENABLE_ADBLOCK, 897 | G_CALLBACK (ephy_uri_tester_enable_adblock_changed_cb), tester); 898 | */ 899 | /* GSettings never emits the changed signal until after we read the setting 900 | * the first time after connecting the handler... work around this.*/ 901 | /* 902 | trash = g_settings_get_strv (EPHY_SETTINGS_MAIN, EPHY_PREFS_ADBLOCK_FILTERS); 903 | g_strfreev (trash); 904 | */ 905 | } 906 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------