Status: | Merged |
---|---|
Approved by: | Paweł Forysiuk |
Approved revision: | 6325 |
Merged at revision: | 6558 |
Proposed branch: | lp:~midori/midori/wk2ab |
Merge into: | lp:midori |
Diff against target: |
3520 lines (+1443/-1934) 18 files modified
data/adblock.list (+54/-0) extensions/CMakeLists.txt (+18/-2) extensions/adblock.c (+0/-1930) extensions/adblock/config.vala (+107/-0) extensions/adblock/element.vala (+36/-0) extensions/adblock/extension.vala (+545/-0) extensions/adblock/filter.vala (+52/-0) extensions/adblock/keys.vala (+47/-0) extensions/adblock/options.vala (+32/-0) extensions/adblock/pattern.vala (+26/-0) extensions/adblock/subscriptions.vala (+328/-0) extensions/adblock/updater.vala (+139/-0) extensions/adblock/whitelist.vala (+30/-0) katze/midori-paths.vala (+1/-0) midori/midori-app.c (+1/-1) midori/midori.vapi (+1/-0) midori/webkit2gtk-3.0.vapi (+16/-0) po/POTFILES.in (+10/-1) |
To merge this branch: | bzr merge lp:~midori/midori/wk2ab |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Paweł Forysiuk | Approve | ||
Review via email: mp+178349@code.launchpad.net |
Commit message
Rewrite Adblock more modularly, add Whitelist support
Description of the change
TODO:
- Expose filter metadata to user (name, last updated, time to next update)
- Check checksum hash of the subscription after downloading
- Remove subscriptions
- Open local files in editor
- Ability to disable adblock for site/for all sites via icon
- Save pre-parsed pre filtered ruleset into file as .preparsed
- Save matched/ cached uris from previous runs
[https:/
enabled=1
expires=
retries=1
homepage=https:/
title=EasyList
Test cases https:/
To post a comment you must log in.
lp:~midori/midori/wk2ab
updated
- 6325. By Cris Dywan
-
Rename adblock2 to adblock
Revision history for this message
Paweł Forysiuk (tuxator) : | # |
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === added file 'data/adblock.list' | |||
2 | --- data/adblock.list 1970-01-01 00:00:00 +0000 | |||
3 | +++ data/adblock.list 2014-02-20 20:31:17 +0000 | |||
4 | @@ -0,0 +1,54 @@ | |||
5 | 1 | [Adblock Plus 2.0] | ||
6 | 2 | ! Version: 201402142200 | ||
7 | 3 | ! Title: Exercise | ||
8 | 4 | ! Last modified: 11 Feb 2014 22:00 UTC | ||
9 | 5 | ! Expires: 3 days (update frequency) | ||
10 | 6 | ! Homepage: http://www.midori-browser.org | ||
11 | 7 | ! Licence: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt | ||
12 | 8 | ! Copyright (C) 2014 Christian Dywan <christian@twotoasts.de> | ||
13 | 9 | ! | ||
14 | 10 | ! Yadayada http://example.com/ e-mail (somebody@example.com). | ||
15 | 11 | ! | ||
16 | 12 | !-----Spam eggs--------! | ||
17 | 13 | ! *** la:le/lu_foo_bar.txt *** | ||
18 | 14 | |/http://ads.blub.boing/*$domain=xxx.com | ||
19 | 15 | |/http://ads.blub.boing/*$domain=xxx.com,foo.fr,coco.at | ||
20 | 16 | /market.php?$domain=adf.ly|foo.com | ||
21 | 17 | /?placement=$script,domain=putlocker.com|sockshare.com | ||
22 | 18 | |||
23 | 19 | ! Some basic filters | ||
24 | 20 | *ads.foo.bar* | ||
25 | 21 | *ads.bogus.name* | ||
26 | 22 | ||^http://ads.bla.blub/* | ||
27 | 23 | engine.adct.ru/*? | ||
28 | 24 | /addyn|*|adtech; | ||
29 | 25 | doubleclick.net/pfadx/*.mtvi | ||
30 | 26 | objects.tremormedia.com/embed/xml/*.xml?r= | ||
31 | 27 | videostrip.com^*/admatcherclient. | ||
32 | 28 | test.dom/test?var | ||
33 | 29 | /adpage. | ||
34 | 30 | br.gcl.ru/cgi-bin/br/ | ||
35 | 31 | _300x600. | ||
36 | 32 | _rectangle_ads. | ||
37 | 33 | +adverts/ | ||
38 | 34 | -2/ads/ | ||
39 | 35 | |||
40 | 36 | ! CSS elements | ||
41 | 37 | old.tv,delicio.us,longc.at###box | ||
42 | 38 | ##.zRightAdNote | ||
43 | 39 | ###advertisingModule160x600 | ||
44 | 40 | ##a[href$="/vghd.shtml"] | ||
45 | 41 | imagetwist.com###left[align="center"] > center > a[target="_blank"] | ||
46 | 42 | |||
47 | 43 | ! Options | ||
48 | 44 | ||videobox.com/?tid=$popup | ||
49 | 45 | ||sexsearchcom.com^$popup,third-party | ||
50 | 46 | ||206.217.206.137^$third-party | ||
51 | 47 | /spopunder^$popup | ||
52 | 48 | ||putlocker.com^*.php?*title$subdocument | ||
53 | 49 | ://ads.$popup | ||
54 | 50 | |||
55 | 51 | ! Whitelist | ||
56 | 52 | @@||hortifor.com/images/*120x60$~third-party | ||
57 | 53 | @@||stickam.com/wb/www/category/300x250/$image | ||
58 | 54 | @@||adultadworld.com/adhandler/$subdocument | ||
59 | 0 | 55 | ||
60 | === modified file 'extensions/CMakeLists.txt' | |||
61 | --- extensions/CMakeLists.txt 2013-09-08 19:56:46 +0000 | |||
62 | +++ extensions/CMakeLists.txt 2014-02-20 20:31:17 +0000 | |||
63 | @@ -15,7 +15,6 @@ | |||
64 | 15 | file(GLOB EXTENSIONS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *) | 15 | file(GLOB EXTENSIONS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *) |
65 | 16 | if (HALF_BRO_INCOM_WEBKIT2) | 16 | if (HALF_BRO_INCOM_WEBKIT2) |
66 | 17 | list(REMOVE_ITEM EXTENSIONS | 17 | list(REMOVE_ITEM EXTENSIONS |
67 | 18 | "adblock.c" | ||
68 | 19 | "cookie-permissions" | 18 | "cookie-permissions" |
69 | 20 | "addons.c" | 19 | "addons.c" |
70 | 21 | "formhistory" | 20 | "formhistory" |
71 | @@ -46,13 +45,30 @@ | |||
72 | 46 | string(FIND ${UNIT_SRC} "." UNIT_EXTENSION) | 45 | string(FIND ${UNIT_SRC} "." UNIT_EXTENSION) |
73 | 47 | if (UNIT_EXTENSION EQUAL -1) | 46 | if (UNIT_EXTENSION EQUAL -1) |
74 | 48 | file(GLOB UNIT_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${UNIT_SRC}/*.c") | 47 | file(GLOB UNIT_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${UNIT_SRC}/*.c") |
75 | 48 | file(GLOB UNIT_FILES_VALA RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${UNIT_SRC}/*.vala") | ||
76 | 49 | if (UNIT_FILES_VALA) | ||
77 | 50 | include(ValaPrecompile) | ||
78 | 51 | vala_precompile(UNIT_SRC_C ${UNIT_SRC} | ||
79 | 52 | ${UNIT_FILES_VALA} | ||
80 | 53 | PACKAGES | ||
81 | 54 | ${PKGS} | ||
82 | 55 | OPTIONS | ||
83 | 56 | ${VALAFLAGS} | ||
84 | 57 | CUSTOM_VAPIS | ||
85 | 58 | ${EXTRA_VAPIS} | ||
86 | 59 | "${CMAKE_SOURCE_DIR}/midori/midori.vapi" | ||
87 | 60 | "${CMAKE_SOURCE_DIR}/katze/katze.vapi" | ||
88 | 61 | "${CMAKE_BINARY_DIR}/midori/${LIBMIDORI}.vapi" | ||
89 | 62 | ) | ||
90 | 63 | set(UNIT_FILES ${UNIT_FILES} ${UNIT_SRC_C}) | ||
91 | 64 | endif () | ||
92 | 49 | if (UNIT_FILES) | 65 | if (UNIT_FILES) |
93 | 50 | add_library(${UNIT_SRC} MODULE ${UNIT_FILES}) | 66 | add_library(${UNIT_SRC} MODULE ${UNIT_FILES}) |
94 | 51 | target_link_libraries(${UNIT_SRC} | 67 | target_link_libraries(${UNIT_SRC} |
95 | 52 | ${LIBMIDORI} | 68 | ${LIBMIDORI} |
96 | 53 | ) | 69 | ) |
97 | 54 | set_target_properties(${UNIT_SRC} PROPERTIES | 70 | set_target_properties(${UNIT_SRC} PROPERTIES |
99 | 55 | COMPILE_FLAGS ${CFLAGS} | 71 | COMPILE_FLAGS ${VALA_CFLAGS} |
100 | 56 | ) | 72 | ) |
101 | 57 | install(TARGETS ${UNIT_SRC} | 73 | install(TARGETS ${UNIT_SRC} |
102 | 58 | LIBRARY DESTINATION ${EXTENSIONDIR} | 74 | LIBRARY DESTINATION ${EXTENSIONDIR} |
103 | 59 | 75 | ||
104 | === added directory 'extensions/adblock' | |||
105 | === removed file 'extensions/adblock.c' | |||
106 | --- extensions/adblock.c 2013-11-09 17:56:58 +0000 | |||
107 | +++ extensions/adblock.c 1970-01-01 00:00:00 +0000 | |||
108 | @@ -1,1930 +0,0 @@ | |||
109 | 1 | /* | ||
110 | 2 | Copyright (C) 2009-2012 Christian Dywan <christian@twotoasts.de> | ||
111 | 3 | Copyright (C) 2009-2012 Alexander Butenko <a.butenka@gmail.com> | ||
112 | 4 | |||
113 | 5 | This library is free software; you can redistribute it and/or | ||
114 | 6 | modify it under the terms of the GNU Lesser General Public | ||
115 | 7 | License as published by the Free Software Foundation; either | ||
116 | 8 | version 2.1 of the License, or (at your option) any later version. | ||
117 | 9 | |||
118 | 10 | See the file COPYING for the full license text. | ||
119 | 11 | */ | ||
120 | 12 | #include <midori/midori.h> | ||
121 | 13 | #include "midori-core.h" | ||
122 | 14 | #include <glib/gstdio.h> | ||
123 | 15 | |||
124 | 16 | #include "config.h" | ||
125 | 17 | #if HAVE_UNISTD_H | ||
126 | 18 | #include <unistd.h> | ||
127 | 19 | #endif | ||
128 | 20 | |||
129 | 21 | #define SIGNATURE_SIZE 8 | ||
130 | 22 | #define USE_PATTERN_MATCHING 1 | ||
131 | 23 | #define CUSTOM_LIST_NAME "custom.list" | ||
132 | 24 | #define ADBLOCK_FILTER_VALID(__filter) \ | ||
133 | 25 | (__filter && (g_str_has_prefix (__filter, "http") \ | ||
134 | 26 | || g_str_has_prefix (__filter, "file"))) | ||
135 | 27 | #define ADBLOCK_FILTER_SET(__filter,__active) \ | ||
136 | 28 | __filter[4] = __active ? (__filter[5] == ':' ? 's' : ':') : '-' | ||
137 | 29 | #define ADBLOCK_FILTER_IS_SET(__filter) \ | ||
138 | 30 | (__filter[4] != '-' && __filter[5] != '-') | ||
139 | 31 | #define adblock_debug(dmsg, darg1, darg2) \ | ||
140 | 32 | do { if (midori_debug ("adblock:match")) g_debug (dmsg, darg1, darg2); } while (0) | ||
141 | 33 | |||
142 | 34 | static GHashTable* pattern = NULL; | ||
143 | 35 | static GHashTable* keys = NULL; | ||
144 | 36 | static GHashTable* optslist = NULL; | ||
145 | 37 | static GHashTable* urlcache = NULL; | ||
146 | 38 | static GHashTable* blockcssprivate = NULL; | ||
147 | 39 | static GHashTable* navigationwhitelist = NULL; | ||
148 | 40 | static GString* blockcss = NULL; | ||
149 | 41 | static GList* update_list = NULL; | ||
150 | 42 | static gboolean update_done = FALSE; | ||
151 | 43 | |||
152 | 44 | static void | ||
153 | 45 | adblock_parse_file (gchar* path); | ||
154 | 46 | |||
155 | 47 | static gboolean | ||
156 | 48 | adblock_file_is_up_to_date (gchar* path); | ||
157 | 49 | |||
158 | 50 | static void | ||
159 | 51 | adblock_reload_rules (MidoriExtension* extension, | ||
160 | 52 | gboolean custom_only); | ||
161 | 53 | |||
162 | 54 | static gchar* | ||
163 | 55 | adblock_build_js (const gchar* uri) | ||
164 | 56 | { | ||
165 | 57 | gchar* domain; | ||
166 | 58 | const gchar* style; | ||
167 | 59 | GString* subdomain; | ||
168 | 60 | GString* code; | ||
169 | 61 | int cnt = 0, blockscnt = 0; | ||
170 | 62 | gchar** subdomains; | ||
171 | 63 | |||
172 | 64 | domain = midori_uri_parse_hostname (uri, NULL); | ||
173 | 65 | subdomains = g_strsplit (domain, ".", -1); | ||
174 | 66 | g_free (domain); | ||
175 | 67 | if (!subdomains) | ||
176 | 68 | return NULL; | ||
177 | 69 | |||
178 | 70 | code = g_string_new ( | ||
179 | 71 | "window.addEventListener ('DOMContentLoaded'," | ||
180 | 72 | "function () {" | ||
181 | 73 | " if (document.getElementById('madblock'))" | ||
182 | 74 | " return;" | ||
183 | 75 | " public = '"); | ||
184 | 76 | |||
185 | 77 | cnt = g_strv_length (subdomains) - 1; | ||
186 | 78 | subdomain = g_string_new (subdomains [cnt]); | ||
187 | 79 | g_string_prepend_c (subdomain, '.'); | ||
188 | 80 | cnt--; | ||
189 | 81 | while (cnt >= 0) | ||
190 | 82 | { | ||
191 | 83 | g_string_prepend (subdomain, subdomains[cnt]); | ||
192 | 84 | if ((style = g_hash_table_lookup (blockcssprivate, subdomain->str))) | ||
193 | 85 | { | ||
194 | 86 | g_string_append (code, style); | ||
195 | 87 | g_string_append_c (code, ','); | ||
196 | 88 | blockscnt++; | ||
197 | 89 | } | ||
198 | 90 | g_string_prepend_c (subdomain, '.'); | ||
199 | 91 | cnt--; | ||
200 | 92 | } | ||
201 | 93 | g_string_free (subdomain, TRUE); | ||
202 | 94 | g_strfreev (subdomains); | ||
203 | 95 | |||
204 | 96 | if (blockscnt == 0) | ||
205 | 97 | return g_string_free (code, TRUE); | ||
206 | 98 | |||
207 | 99 | g_string_append (code, | ||
208 | 100 | " zz-non-existent {display: none !important}';" | ||
209 | 101 | " var mystyle = document.createElement('style');" | ||
210 | 102 | " mystyle.setAttribute('type', 'text/css');" | ||
211 | 103 | " mystyle.setAttribute('id', 'madblock');" | ||
212 | 104 | " mystyle.appendChild(document.createTextNode(public));" | ||
213 | 105 | " var head = document.getElementsByTagName('head')[0];" | ||
214 | 106 | " if (head) head.appendChild(mystyle);" | ||
215 | 107 | "}, true);"); | ||
216 | 108 | return g_string_free (code, FALSE); | ||
217 | 109 | } | ||
218 | 110 | |||
219 | 111 | static GString* | ||
220 | 112 | adblock_fixup_regexp (const gchar* prefix, | ||
221 | 113 | gchar* src); | ||
222 | 114 | |||
223 | 115 | static void | ||
224 | 116 | adblock_destroy_db () | ||
225 | 117 | { | ||
226 | 118 | g_string_free (blockcss, TRUE); | ||
227 | 119 | blockcss = NULL; | ||
228 | 120 | |||
229 | 121 | g_hash_table_destroy (pattern); | ||
230 | 122 | pattern = NULL; | ||
231 | 123 | g_hash_table_destroy (optslist); | ||
232 | 124 | optslist = NULL; | ||
233 | 125 | g_hash_table_destroy (urlcache); | ||
234 | 126 | urlcache = NULL; | ||
235 | 127 | g_hash_table_destroy (blockcssprivate); | ||
236 | 128 | blockcssprivate = NULL; | ||
237 | 129 | g_hash_table_destroy (navigationwhitelist); | ||
238 | 130 | navigationwhitelist = NULL; | ||
239 | 131 | } | ||
240 | 132 | |||
241 | 133 | static void | ||
242 | 134 | adblock_init_db () | ||
243 | 135 | { | ||
244 | 136 | pattern = g_hash_table_new_full (g_str_hash, g_str_equal, | ||
245 | 137 | (GDestroyNotify)g_free, | ||
246 | 138 | (GDestroyNotify)g_regex_unref); | ||
247 | 139 | keys = g_hash_table_new_full (g_str_hash, g_str_equal, | ||
248 | 140 | (GDestroyNotify)g_free, | ||
249 | 141 | (GDestroyNotify)g_regex_unref); | ||
250 | 142 | optslist = g_hash_table_new_full (g_str_hash, g_str_equal, | ||
251 | 143 | NULL, | ||
252 | 144 | (GDestroyNotify)g_free); | ||
253 | 145 | urlcache = g_hash_table_new_full (g_str_hash, g_str_equal, | ||
254 | 146 | (GDestroyNotify)g_free, | ||
255 | 147 | (GDestroyNotify)g_free); | ||
256 | 148 | blockcssprivate = g_hash_table_new_full (g_str_hash, g_str_equal, | ||
257 | 149 | (GDestroyNotify)g_free, | ||
258 | 150 | (GDestroyNotify)g_free); | ||
259 | 151 | navigationwhitelist = g_hash_table_new_full (g_direct_hash, g_str_equal, | ||
260 | 152 | NULL, | ||
261 | 153 | (GDestroyNotify)g_free); | ||
262 | 154 | |||
263 | 155 | blockcss = g_string_new ("z-non-exist"); | ||
264 | 156 | } | ||
265 | 157 | |||
266 | 158 | static void | ||
267 | 159 | adblock_download_notify_status_cb (WebKitDownload* download, | ||
268 | 160 | GParamSpec* pspec, | ||
269 | 161 | MidoriExtension* extension) | ||
270 | 162 | { | ||
271 | 163 | if (update_done) | ||
272 | 164 | return; | ||
273 | 165 | |||
274 | 166 | if (webkit_download_get_status (download) == WEBKIT_DOWNLOAD_STATUS_FINISHED) | ||
275 | 167 | { | ||
276 | 168 | GList* li = NULL; | ||
277 | 169 | for (li = update_list; li != NULL; li = g_list_next (li)) | ||
278 | 170 | { | ||
279 | 171 | gchar* uri = g_strdup (webkit_download_get_destination_uri (download) + 7); | ||
280 | 172 | if (g_strcmp0 (li->data, uri)) | ||
281 | 173 | update_list = g_list_remove (update_list, li->data); | ||
282 | 174 | g_free (uri); | ||
283 | 175 | } | ||
284 | 176 | } | ||
285 | 177 | |||
286 | 178 | if (g_list_length (update_list) == 0) | ||
287 | 179 | { | ||
288 | 180 | adblock_reload_rules (extension, FALSE); | ||
289 | 181 | update_done = TRUE; | ||
290 | 182 | } | ||
291 | 183 | } | ||
292 | 184 | |||
293 | 185 | static gchar* | ||
294 | 186 | adblock_get_filename_for_uri (const gchar* uri) | ||
295 | 187 | { | ||
296 | 188 | gchar* filename; | ||
297 | 189 | gchar* folder; | ||
298 | 190 | gchar* path; | ||
299 | 191 | |||
300 | 192 | if (!ADBLOCK_FILTER_IS_SET (uri)) | ||
301 | 193 | return NULL; | ||
302 | 194 | |||
303 | 195 | if (!strncmp (uri, "file", 4)) | ||
304 | 196 | return g_strndup (uri + 7, strlen (uri) - 7); | ||
305 | 197 | |||
306 | 198 | folder = g_build_filename (midori_paths_get_cache_dir (), "adblock", NULL); | ||
307 | 199 | katze_mkdir_with_parents (folder, 0700); | ||
308 | 200 | |||
309 | 201 | filename = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1); | ||
310 | 202 | path = g_build_filename (folder, filename, NULL); | ||
311 | 203 | |||
312 | 204 | g_free (filename); | ||
313 | 205 | g_free (folder); | ||
314 | 206 | return path; | ||
315 | 207 | } | ||
316 | 208 | |||
317 | 209 | static void | ||
318 | 210 | adblock_reload_rules (MidoriExtension* extension, | ||
319 | 211 | gboolean custom_only) | ||
320 | 212 | { | ||
321 | 213 | gchar* path; | ||
322 | 214 | gchar* custom_list; | ||
323 | 215 | gchar** filters; | ||
324 | 216 | guint i = 0; | ||
325 | 217 | MidoriApp* app = midori_extension_get_app (extension); | ||
326 | 218 | MidoriWebSettings* settings = katze_object_get_object (app, "settings"); | ||
327 | 219 | |||
328 | 220 | if (pattern) | ||
329 | 221 | adblock_destroy_db (); | ||
330 | 222 | adblock_init_db (); | ||
331 | 223 | |||
332 | 224 | custom_list = g_build_filename (midori_extension_get_config_dir (extension), | ||
333 | 225 | CUSTOM_LIST_NAME, NULL); | ||
334 | 226 | adblock_parse_file (custom_list); | ||
335 | 227 | g_free (custom_list); | ||
336 | 228 | |||
337 | 229 | filters = midori_extension_get_string_list (extension, "filters", NULL); | ||
338 | 230 | if (!custom_only && filters && *filters) | ||
339 | 231 | { | ||
340 | 232 | while (filters[i] != NULL) | ||
341 | 233 | { | ||
342 | 234 | path = adblock_get_filename_for_uri (filters[i]); | ||
343 | 235 | if (!path) | ||
344 | 236 | { | ||
345 | 237 | i++; | ||
346 | 238 | continue; | ||
347 | 239 | } | ||
348 | 240 | |||
349 | 241 | if (!adblock_file_is_up_to_date (path)) | ||
350 | 242 | { | ||
351 | 243 | WebKitNetworkRequest* request; | ||
352 | 244 | WebKitDownload* download; | ||
353 | 245 | gchar* destination = g_filename_to_uri (path, NULL, NULL); | ||
354 | 246 | |||
355 | 247 | request = webkit_network_request_new (filters[i]); | ||
356 | 248 | download = webkit_download_new (request); | ||
357 | 249 | g_object_unref (request); | ||
358 | 250 | webkit_download_set_destination_uri (download, destination); | ||
359 | 251 | update_list = g_list_prepend (update_list, path); | ||
360 | 252 | g_free (destination); | ||
361 | 253 | g_signal_connect (download, "notify::status", | ||
362 | 254 | G_CALLBACK (adblock_download_notify_status_cb), extension); | ||
363 | 255 | webkit_download_start (download); | ||
364 | 256 | } | ||
365 | 257 | else | ||
366 | 258 | adblock_parse_file (path); | ||
367 | 259 | g_free (path); | ||
368 | 260 | i++; | ||
369 | 261 | } | ||
370 | 262 | } | ||
371 | 263 | g_strfreev (filters); | ||
372 | 264 | g_string_append (blockcss, " {display: none !important}\n"); | ||
373 | 265 | |||
374 | 266 | midori_web_settings_add_style (settings, "adblock-blockcss", blockcss->str); | ||
375 | 267 | g_object_unref (settings); | ||
376 | 268 | } | ||
377 | 269 | |||
378 | 270 | static void | ||
379 | 271 | adblock_preferences_render_tick_cb (GtkTreeViewColumn* column, | ||
380 | 272 | GtkCellRenderer* renderer, | ||
381 | 273 | GtkTreeModel* model, | ||
382 | 274 | GtkTreeIter* iter, | ||
383 | 275 | MidoriExtension* extension) | ||
384 | 276 | { | ||
385 | 277 | gchar* filter; | ||
386 | 278 | |||
387 | 279 | gtk_tree_model_get (model, iter, 0, &filter, -1); | ||
388 | 280 | |||
389 | 281 | g_object_set (renderer, | ||
390 | 282 | "activatable", ADBLOCK_FILTER_VALID (filter), | ||
391 | 283 | "active", ADBLOCK_FILTER_VALID (filter) && ADBLOCK_FILTER_IS_SET (filter), | ||
392 | 284 | NULL); | ||
393 | 285 | |||
394 | 286 | g_free (filter); | ||
395 | 287 | } | ||
396 | 288 | |||
397 | 289 | static void | ||
398 | 290 | adblock_preferences_renderer_text_edited_cb (GtkCellRenderer* renderer, | ||
399 | 291 | const gchar* tree_path, | ||
400 | 292 | const gchar* new_text, | ||
401 | 293 | GtkTreeModel* model) | ||
402 | 294 | { | ||
403 | 295 | GtkTreeIter iter; | ||
404 | 296 | |||
405 | 297 | if (gtk_tree_model_get_iter_from_string (model, &iter, tree_path)) | ||
406 | 298 | gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, new_text, -1); | ||
407 | 299 | } | ||
408 | 300 | |||
409 | 301 | static void | ||
410 | 302 | adblock_preferences_renderer_toggle_toggled_cb (GtkCellRendererToggle* renderer, | ||
411 | 303 | const gchar* path, | ||
412 | 304 | GtkTreeModel* model) | ||
413 | 305 | { | ||
414 | 306 | GtkTreeIter iter; | ||
415 | 307 | |||
416 | 308 | if (gtk_tree_model_get_iter_from_string (model, &iter, path)) | ||
417 | 309 | { | ||
418 | 310 | gchar* filter; | ||
419 | 311 | |||
420 | 312 | gtk_tree_model_get (model, &iter, 0, &filter, -1); | ||
421 | 313 | |||
422 | 314 | if (ADBLOCK_FILTER_VALID (filter)) | ||
423 | 315 | { | ||
424 | 316 | ADBLOCK_FILTER_SET (filter, TRUE); | ||
425 | 317 | if (gtk_cell_renderer_toggle_get_active (renderer)) | ||
426 | 318 | { | ||
427 | 319 | if (midori_uri_is_http (filter)) | ||
428 | 320 | { | ||
429 | 321 | gchar* filename = adblock_get_filename_for_uri (filter); | ||
430 | 322 | g_unlink (filename); | ||
431 | 323 | g_free (filename); | ||
432 | 324 | } | ||
433 | 325 | ADBLOCK_FILTER_SET (filter, FALSE); | ||
434 | 326 | } | ||
435 | 327 | |||
436 | 328 | gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, filter, -1); | ||
437 | 329 | |||
438 | 330 | g_free (filter); | ||
439 | 331 | } | ||
440 | 332 | } | ||
441 | 333 | } | ||
442 | 334 | |||
443 | 335 | static void | ||
444 | 336 | adblock_preferences_render_text_cb (GtkTreeViewColumn* column, | ||
445 | 337 | GtkCellRenderer* renderer, | ||
446 | 338 | GtkTreeModel* model, | ||
447 | 339 | GtkTreeIter* iter, | ||
448 | 340 | MidoriExtension* extension) | ||
449 | 341 | { | ||
450 | 342 | gchar* filter; | ||
451 | 343 | |||
452 | 344 | gtk_tree_model_get (model, iter, 0, &filter, -1); | ||
453 | 345 | |||
454 | 346 | if (ADBLOCK_FILTER_VALID (filter)) | ||
455 | 347 | ADBLOCK_FILTER_SET (filter, TRUE); | ||
456 | 348 | |||
457 | 349 | g_object_set (renderer, | ||
458 | 350 | "text", filter, | ||
459 | 351 | NULL); | ||
460 | 352 | |||
461 | 353 | g_free (filter); | ||
462 | 354 | } | ||
463 | 355 | |||
464 | 356 | static void | ||
465 | 357 | adblock_preferences_model_row_changed_cb (GtkTreeModel* model, | ||
466 | 358 | GtkTreePath* path, | ||
467 | 359 | GtkTreeIter* iter, | ||
468 | 360 | MidoriExtension* extension) | ||
469 | 361 | { | ||
470 | 362 | gsize length = gtk_tree_model_iter_n_children (model, NULL); | ||
471 | 363 | gchar** filters = g_new (gchar*, length + 1); | ||
472 | 364 | guint i = 0; | ||
473 | 365 | gboolean need_reload = FALSE; | ||
474 | 366 | |||
475 | 367 | if (gtk_tree_model_iter_children (model, iter, NULL)) | ||
476 | 368 | do | ||
477 | 369 | { | ||
478 | 370 | gchar* filter; | ||
479 | 371 | gtk_tree_model_get (model, iter, 0, &filter, -1); | ||
480 | 372 | if (filter && *filter) | ||
481 | 373 | { | ||
482 | 374 | filters[i++] = filter; | ||
483 | 375 | need_reload = TRUE; | ||
484 | 376 | } | ||
485 | 377 | else | ||
486 | 378 | g_free (filter); | ||
487 | 379 | } | ||
488 | 380 | while (gtk_tree_model_iter_next (model, iter)); | ||
489 | 381 | filters[i] = NULL; | ||
490 | 382 | midori_extension_set_string_list (extension, "filters", filters, i); | ||
491 | 383 | g_free (filters); | ||
492 | 384 | if (need_reload) | ||
493 | 385 | adblock_reload_rules (extension, FALSE); | ||
494 | 386 | } | ||
495 | 387 | |||
496 | 388 | static void | ||
497 | 389 | adblock_preferences_model_row_deleted_cb (GtkTreeModel* model, | ||
498 | 390 | GtkTreePath* path, | ||
499 | 391 | MidoriExtension* extension) | ||
500 | 392 | { | ||
501 | 393 | GtkTreeIter iter; | ||
502 | 394 | adblock_preferences_model_row_changed_cb (model, path, &iter, extension); | ||
503 | 395 | } | ||
504 | 396 | |||
505 | 397 | static void | ||
506 | 398 | adblock_preferences_add_clicked_cb (GtkWidget* button, | ||
507 | 399 | GtkTreeModel* model) | ||
508 | 400 | { | ||
509 | 401 | GtkEntry* entry = GTK_IS_ENTRY (button) | ||
510 | 402 | ? button : g_object_get_data (G_OBJECT (button), "entry"); | ||
511 | 403 | gtk_list_store_insert_with_values (GTK_LIST_STORE (model), | ||
512 | 404 | NULL, 0, 0, gtk_entry_get_text (entry), -1); | ||
513 | 405 | gtk_entry_set_text (entry, ""); | ||
514 | 406 | } | ||
515 | 407 | |||
516 | 408 | static void | ||
517 | 409 | adblock_preferences_edit_clicked_cb (GtkWidget* button, | ||
518 | 410 | GtkTreeViewColumn* column) | ||
519 | 411 | { | ||
520 | 412 | GdkEvent* event = gtk_get_current_event (); | ||
521 | 413 | GtkTreeView* treeview = g_object_get_data (G_OBJECT (button), "treeview"); | ||
522 | 414 | GtkTreeModel* model; | ||
523 | 415 | GtkTreeIter iter; | ||
524 | 416 | if (katze_tree_view_get_selected_iter (treeview, &model, &iter)) | ||
525 | 417 | { | ||
526 | 418 | gchar* path = gtk_tree_model_get_string_from_iter (model, &iter); | ||
527 | 419 | GtkTreePath* tree_path = gtk_tree_path_new_from_string (path); | ||
528 | 420 | /* gtk_cell_renderer_start_editing */ | ||
529 | 421 | gtk_tree_view_set_cursor (treeview, tree_path, column, TRUE); | ||
530 | 422 | gtk_tree_path_free (tree_path); | ||
531 | 423 | g_free (path); | ||
532 | 424 | } | ||
533 | 425 | gdk_event_free (event); | ||
534 | 426 | } | ||
535 | 427 | |||
536 | 428 | static void | ||
537 | 429 | adblock_preferences_remove_clicked_cb (GtkWidget* button, | ||
538 | 430 | GtkTreeView* treeview) | ||
539 | 431 | { | ||
540 | 432 | GtkTreeModel* model; | ||
541 | 433 | GtkTreeIter iter; | ||
542 | 434 | if (katze_tree_view_get_selected_iter (treeview, &model, &iter)) | ||
543 | 435 | gtk_list_store_remove (GTK_LIST_STORE (model), &iter); | ||
544 | 436 | } | ||
545 | 437 | |||
546 | 438 | static gboolean | ||
547 | 439 | adblock_activate_link_cb (GtkWidget* label, | ||
548 | 440 | const gchar* uri) | ||
549 | 441 | { | ||
550 | 442 | MidoriBrowser* browser = midori_browser_get_for_widget (label); | ||
551 | 443 | GtkWidget* view = midori_browser_add_uri (browser, uri); | ||
552 | 444 | midori_browser_set_current_tab (browser, view); | ||
553 | 445 | return TRUE; | ||
554 | 446 | } | ||
555 | 447 | |||
556 | 448 | static GtkWidget* | ||
557 | 449 | adblock_get_preferences_dialog (MidoriExtension* extension) | ||
558 | 450 | { | ||
559 | 451 | MidoriApp* app; | ||
560 | 452 | GtkWidget* browser; | ||
561 | 453 | const gchar* dialog_title; | ||
562 | 454 | GtkWidget* dialog; | ||
563 | 455 | GtkWidget* content_area; | ||
564 | 456 | gint width, height; | ||
565 | 457 | GtkWidget* xfce_heading; | ||
566 | 458 | GtkWidget* hbox; | ||
567 | 459 | GtkListStore* liststore; | ||
568 | 460 | GtkWidget* treeview; | ||
569 | 461 | GtkTreeViewColumn* column; | ||
570 | 462 | GtkCellRenderer* renderer_text; | ||
571 | 463 | GtkCellRenderer* renderer_toggle; | ||
572 | 464 | GtkWidget* scrolled; | ||
573 | 465 | gchar** filters; | ||
574 | 466 | GtkWidget* vbox; | ||
575 | 467 | GtkWidget* button; | ||
576 | 468 | gchar* description; | ||
577 | 469 | GtkWidget* entry; | ||
578 | 470 | #if HAVE_OSX | ||
579 | 471 | GtkWidget* icon; | ||
580 | 472 | #endif | ||
581 | 473 | |||
582 | 474 | app = midori_extension_get_app (extension); | ||
583 | 475 | browser = katze_object_get_object (app, "browser"); | ||
584 | 476 | |||
585 | 477 | dialog_title = _("Configure Advertisement filters"); | ||
586 | 478 | dialog = gtk_dialog_new_with_buttons (dialog_title, GTK_WINDOW (browser), | ||
587 | 479 | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR, | ||
588 | 480 | #if !HAVE_OSX | ||
589 | 481 | GTK_STOCK_HELP, GTK_RESPONSE_HELP, | ||
590 | 482 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, | ||
591 | 483 | #endif | ||
592 | 484 | NULL); | ||
593 | 485 | katze_widget_add_class (gtk_dialog_get_widget_for_response ( | ||
594 | 486 | GTK_DIALOG (dialog), GTK_RESPONSE_HELP), "help_button"); | ||
595 | 487 | content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); | ||
596 | 488 | gtk_window_set_icon_name (GTK_WINDOW (dialog), GTK_STOCK_PROPERTIES); | ||
597 | 489 | /* TODO: Implement some kind of help function */ | ||
598 | 490 | gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), | ||
599 | 491 | GTK_RESPONSE_HELP, FALSE); | ||
600 | 492 | sokoke_widget_get_text_size (dialog, "M", &width, &height); | ||
601 | 493 | gtk_window_set_default_size (GTK_WINDOW (dialog), width * 52, -1); | ||
602 | 494 | g_signal_connect_swapped (dialog, "response", | ||
603 | 495 | G_CALLBACK (gtk_widget_destroy), dialog); | ||
604 | 496 | /* TODO: We need mnemonics */ | ||
605 | 497 | if ((xfce_heading = sokoke_xfce_header_new ( | ||
606 | 498 | gtk_window_get_icon_name (GTK_WINDOW (dialog)), dialog_title))) | ||
607 | 499 | gtk_box_pack_start (GTK_BOX (content_area), xfce_heading, FALSE, FALSE, 0); | ||
608 | 500 | hbox = gtk_hbox_new (FALSE, 0); | ||
609 | 501 | gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 12); | ||
610 | 502 | vbox = gtk_vbox_new (FALSE, 0); | ||
611 | 503 | gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 4); | ||
612 | 504 | button = gtk_label_new (NULL); | ||
613 | 505 | description = g_strdup_printf (_( | ||
614 | 506 | "Type the address of a preconfigured filter list in the text entry " | ||
615 | 507 | "and click \"Add\" to add it to the list. " | ||
616 | 508 | "You can find more lists at %s."), | ||
617 | 509 | "<a href=\"http://adblockplus.org/en/subscriptions\">adblockplus.org/en/subscriptions</a> " | ||
618 | 510 | "<a href=\"http://easylist.adblockplus.org/\">easylist.adblockplus.org</a>"); | ||
619 | 511 | g_signal_connect (button, "activate-link", | ||
620 | 512 | G_CALLBACK (adblock_activate_link_cb), NULL); | ||
621 | 513 | gtk_label_set_markup (GTK_LABEL (button), description); | ||
622 | 514 | g_free (description); | ||
623 | 515 | gtk_label_set_line_wrap (GTK_LABEL (button), TRUE); | ||
624 | 516 | gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 4); | ||
625 | 517 | entry = katze_uri_entry_new (NULL); | ||
626 | 518 | gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 4); | ||
627 | 519 | liststore = gtk_list_store_new (1, G_TYPE_STRING); | ||
628 | 520 | treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (liststore)); | ||
629 | 521 | gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE); | ||
630 | 522 | column = gtk_tree_view_column_new (); | ||
631 | 523 | renderer_toggle = gtk_cell_renderer_toggle_new (); | ||
632 | 524 | gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (column), renderer_toggle, FALSE); | ||
633 | 525 | gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (column), renderer_toggle, | ||
634 | 526 | (GtkCellLayoutDataFunc)adblock_preferences_render_tick_cb, | ||
635 | 527 | extension, NULL); | ||
636 | 528 | g_signal_connect (renderer_toggle, "toggled", | ||
637 | 529 | G_CALLBACK (adblock_preferences_renderer_toggle_toggled_cb), liststore); | ||
638 | 530 | gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column); | ||
639 | 531 | column = gtk_tree_view_column_new (); | ||
640 | 532 | renderer_text = gtk_cell_renderer_text_new (); | ||
641 | 533 | gtk_tree_view_column_pack_start (column, renderer_text, TRUE); | ||
642 | 534 | g_object_set (renderer_text, "editable", TRUE, NULL); | ||
643 | 535 | g_signal_connect (renderer_text, "edited", | ||
644 | 536 | G_CALLBACK (adblock_preferences_renderer_text_edited_cb), liststore); | ||
645 | 537 | gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (column), renderer_text, | ||
646 | 538 | (GtkCellLayoutDataFunc)adblock_preferences_render_text_cb, | ||
647 | 539 | extension, NULL); | ||
648 | 540 | gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column); | ||
649 | 541 | scrolled = gtk_scrolled_window_new (NULL, NULL); | ||
650 | 542 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled), | ||
651 | 543 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); | ||
652 | 544 | gtk_container_add (GTK_CONTAINER (scrolled), treeview); | ||
653 | 545 | gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled), | ||
654 | 546 | GTK_SHADOW_IN); | ||
655 | 547 | gtk_box_pack_start (GTK_BOX (vbox), scrolled, TRUE, TRUE, 5); | ||
656 | 548 | |||
657 | 549 | filters = midori_extension_get_string_list (extension, "filters", NULL); | ||
658 | 550 | if (filters != NULL) | ||
659 | 551 | { | ||
660 | 552 | gsize i = 0; | ||
661 | 553 | while (filters[i++] != NULL) | ||
662 | 554 | gtk_list_store_insert_with_values (GTK_LIST_STORE (liststore), | ||
663 | 555 | NULL, i - 1, 0, filters[i -1], -1); | ||
664 | 556 | } | ||
665 | 557 | g_strfreev (filters); | ||
666 | 558 | g_object_connect (liststore, | ||
667 | 559 | "signal::row-inserted", | ||
668 | 560 | adblock_preferences_model_row_changed_cb, extension, | ||
669 | 561 | "signal::row-changed", | ||
670 | 562 | adblock_preferences_model_row_changed_cb, extension, | ||
671 | 563 | "signal::row-deleted", | ||
672 | 564 | adblock_preferences_model_row_deleted_cb, extension, | ||
673 | 565 | NULL); | ||
674 | 566 | |||
675 | 567 | g_object_unref (liststore); | ||
676 | 568 | vbox = gtk_vbox_new (FALSE, 4); | ||
677 | 569 | gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 4); | ||
678 | 570 | button = gtk_button_new_from_stock (GTK_STOCK_ADD); | ||
679 | 571 | g_object_set_data (G_OBJECT (dialog), "entry", entry); | ||
680 | 572 | g_object_set_data (G_OBJECT (button), "entry", entry); | ||
681 | 573 | g_signal_connect (button, "clicked", | ||
682 | 574 | G_CALLBACK (adblock_preferences_add_clicked_cb), liststore); | ||
683 | 575 | g_signal_connect (entry, "activate", | ||
684 | 576 | G_CALLBACK (adblock_preferences_add_clicked_cb), liststore); | ||
685 | 577 | gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0); | ||
686 | 578 | button = gtk_button_new_from_stock (GTK_STOCK_EDIT); | ||
687 | 579 | g_object_set_data (G_OBJECT (button), "treeview", treeview); | ||
688 | 580 | g_signal_connect (button, "clicked", | ||
689 | 581 | G_CALLBACK (adblock_preferences_edit_clicked_cb), column); | ||
690 | 582 | gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0); | ||
691 | 583 | button = gtk_button_new_from_stock (GTK_STOCK_REMOVE); | ||
692 | 584 | g_signal_connect (button, "clicked", | ||
693 | 585 | G_CALLBACK (adblock_preferences_remove_clicked_cb), treeview); | ||
694 | 586 | gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0); | ||
695 | 587 | button = gtk_label_new (""); /* This is an invisible separator */ | ||
696 | 588 | gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 8); | ||
697 | 589 | gtk_widget_set_sensitive (button, FALSE); | ||
698 | 590 | button = gtk_label_new (""); /* This is an invisible separator */ | ||
699 | 591 | gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 12); | ||
700 | 592 | button = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN); | ||
701 | 593 | gtk_widget_set_sensitive (button, FALSE); | ||
702 | 594 | gtk_box_pack_end (GTK_BOX (vbox), button, FALSE, FALSE, 0); | ||
703 | 595 | button = gtk_button_new_from_stock (GTK_STOCK_GO_UP); | ||
704 | 596 | gtk_widget_set_sensitive (button, FALSE); | ||
705 | 597 | gtk_box_pack_end (GTK_BOX (vbox), button, FALSE, FALSE, 0); | ||
706 | 598 | |||
707 | 599 | #if HAVE_OSX | ||
708 | 600 | hbox = gtk_hbox_new (FALSE, 0); | ||
709 | 601 | button = gtk_button_new (); | ||
710 | 602 | icon = gtk_image_new_from_stock (GTK_STOCK_HELP, GTK_ICON_SIZE_BUTTON); | ||
711 | 603 | gtk_button_set_image (GTK_BUTTON (button), icon); | ||
712 | 604 | /* TODO: Implement some kind of help function */ | ||
713 | 605 | gtk_widget_set_sensitive (button, FALSE); | ||
714 | 606 | /* g_signal_connect (button, "clicked", | ||
715 | 607 | G_CALLBACK (adblock_preferences_help_clicked_cb), dialog); */ | ||
716 | 608 | gtk_box_pack_end (GTK_BOX (hbox), | ||
717 | 609 | button, FALSE, FALSE, 4); | ||
718 | 610 | gtk_box_pack_end (GTK_BOX (content_area), | ||
719 | 611 | hbox, FALSE, FALSE, 0); | ||
720 | 612 | #endif | ||
721 | 613 | gtk_widget_show_all (content_area); | ||
722 | 614 | |||
723 | 615 | g_object_unref (browser); | ||
724 | 616 | |||
725 | 617 | return dialog; | ||
726 | 618 | } | ||
727 | 619 | |||
728 | 620 | static GtkWidget* | ||
729 | 621 | adblock_show_preferences_dialog (MidoriExtension* extension, | ||
730 | 622 | const gchar* uri) | ||
731 | 623 | { | ||
732 | 624 | static GtkWidget* dialog = NULL; | ||
733 | 625 | |||
734 | 626 | if (!dialog) | ||
735 | 627 | { | ||
736 | 628 | dialog = adblock_get_preferences_dialog (extension); | ||
737 | 629 | g_signal_connect (dialog, "destroy", | ||
738 | 630 | G_CALLBACK (gtk_widget_destroyed), &dialog); | ||
739 | 631 | gtk_widget_show (dialog); | ||
740 | 632 | } | ||
741 | 633 | else | ||
742 | 634 | gtk_window_present (GTK_WINDOW (dialog)); | ||
743 | 635 | |||
744 | 636 | if (uri != NULL) | ||
745 | 637 | { | ||
746 | 638 | GtkWidget* entry = g_object_get_data (G_OBJECT (dialog), "entry"); | ||
747 | 639 | gtk_entry_set_text (GTK_ENTRY (entry), uri); | ||
748 | 640 | } | ||
749 | 641 | return dialog; | ||
750 | 642 | } | ||
751 | 643 | |||
752 | 644 | static void | ||
753 | 645 | adblock_open_preferences_cb (MidoriExtension* extension) | ||
754 | 646 | { | ||
755 | 647 | adblock_show_preferences_dialog (extension, NULL); | ||
756 | 648 | } | ||
757 | 649 | |||
758 | 650 | static gint | ||
759 | 651 | adblock_check_rule (GRegex* regex, | ||
760 | 652 | const gchar* patt, | ||
761 | 653 | const gchar* req_uri, | ||
762 | 654 | const gchar* page_uri) | ||
763 | 655 | { | ||
764 | 656 | gchar* opts; | ||
765 | 657 | |||
766 | 658 | if (!g_regex_match_full (regex, req_uri, -1, 0, 0, NULL, NULL)) | ||
767 | 659 | return FALSE; | ||
768 | 660 | |||
769 | 661 | opts = g_hash_table_lookup (optslist, patt); | ||
770 | 662 | if (opts && g_regex_match_simple (",third-party", opts, | ||
771 | 663 | G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY)) | ||
772 | 664 | { | ||
773 | 665 | if (page_uri && g_regex_match_full (regex, page_uri, -1, 0, 0, NULL, NULL)) | ||
774 | 666 | return FALSE; | ||
775 | 667 | } | ||
776 | 668 | /* TODO: Domain opt check */ | ||
777 | 669 | adblock_debug ("blocked by pattern regexp=%s -- %s", g_regex_get_pattern (regex), req_uri); | ||
778 | 670 | return TRUE; | ||
779 | 671 | } | ||
780 | 672 | |||
781 | 673 | static gboolean | ||
782 | 674 | adblock_is_matched_by_pattern (const gchar* req_uri, | ||
783 | 675 | const gchar* page_uri) | ||
784 | 676 | { | ||
785 | 677 | GHashTableIter iter; | ||
786 | 678 | gpointer patt, regex; | ||
787 | 679 | |||
788 | 680 | if (USE_PATTERN_MATCHING == 0) | ||
789 | 681 | return FALSE; | ||
790 | 682 | |||
791 | 683 | g_hash_table_iter_init (&iter, pattern); | ||
792 | 684 | while (g_hash_table_iter_next (&iter, &patt, ®ex)) | ||
793 | 685 | { | ||
794 | 686 | if (adblock_check_rule (regex, patt, req_uri, page_uri)) | ||
795 | 687 | return TRUE; | ||
796 | 688 | } | ||
797 | 689 | return FALSE; | ||
798 | 690 | } | ||
799 | 691 | |||
800 | 692 | static gboolean | ||
801 | 693 | adblock_is_matched_by_key (const gchar* req_uri, | ||
802 | 694 | const gchar* page_uri) | ||
803 | 695 | { | ||
804 | 696 | gchar* uri; | ||
805 | 697 | gint len; | ||
806 | 698 | int pos = 0; | ||
807 | 699 | GList* regex_bl = NULL; | ||
808 | 700 | GString* guri; | ||
809 | 701 | gboolean ret = FALSE; | ||
810 | 702 | gchar sig[SIGNATURE_SIZE + 1]; | ||
811 | 703 | |||
812 | 704 | memset (&sig[0], 0, sizeof (sig)); | ||
813 | 705 | /* Signatures are made on pattern, so we need to convert url to a pattern as well */ | ||
814 | 706 | guri = adblock_fixup_regexp ("", (gchar*)req_uri); | ||
815 | 707 | uri = guri->str; | ||
816 | 708 | len = guri->len; | ||
817 | 709 | |||
818 | 710 | for (pos = len - SIGNATURE_SIZE; pos >= 0; pos--) | ||
819 | 711 | { | ||
820 | 712 | GRegex* regex; | ||
821 | 713 | strncpy (sig, uri + pos, SIGNATURE_SIZE); | ||
822 | 714 | regex = g_hash_table_lookup (keys, sig); | ||
823 | 715 | |||
824 | 716 | /* Dont check if regex is already blacklisted */ | ||
825 | 717 | if (!regex || g_list_find (regex_bl, regex)) | ||
826 | 718 | continue; | ||
827 | 719 | ret = adblock_check_rule (regex, sig, req_uri, page_uri); | ||
828 | 720 | if (ret) | ||
829 | 721 | break; | ||
830 | 722 | regex_bl = g_list_prepend (regex_bl, regex); | ||
831 | 723 | } | ||
832 | 724 | g_string_free (guri, TRUE); | ||
833 | 725 | g_list_free (regex_bl); | ||
834 | 726 | return ret; | ||
835 | 727 | } | ||
836 | 728 | |||
837 | 729 | static gboolean | ||
838 | 730 | adblock_is_matched (const gchar* req_uri, | ||
839 | 731 | const gchar* page_uri) | ||
840 | 732 | { | ||
841 | 733 | gchar* value; | ||
842 | 734 | |||
843 | 735 | if ((value = g_hash_table_lookup (urlcache, req_uri))) | ||
844 | 736 | { | ||
845 | 737 | if (value[0] == '0') | ||
846 | 738 | return FALSE; | ||
847 | 739 | else | ||
848 | 740 | return TRUE; | ||
849 | 741 | } | ||
850 | 742 | |||
851 | 743 | if (adblock_is_matched_by_key (req_uri, page_uri)) | ||
852 | 744 | { | ||
853 | 745 | g_hash_table_insert (urlcache, g_strdup (req_uri), g_strdup("1")); | ||
854 | 746 | return TRUE; | ||
855 | 747 | } | ||
856 | 748 | |||
857 | 749 | if (adblock_is_matched_by_pattern (req_uri, page_uri)) | ||
858 | 750 | { | ||
859 | 751 | g_hash_table_insert (urlcache, g_strdup (req_uri), g_strdup("1")); | ||
860 | 752 | return TRUE; | ||
861 | 753 | } | ||
862 | 754 | g_hash_table_insert (urlcache, g_strdup (req_uri), g_strdup("0")); | ||
863 | 755 | return FALSE; | ||
864 | 756 | } | ||
865 | 757 | |||
866 | 758 | static gchar* | ||
867 | 759 | adblock_prepare_urihider_js (GList* uris) | ||
868 | 760 | { | ||
869 | 761 | GList* li = NULL; | ||
870 | 762 | GString* js = g_string_new ( | ||
871 | 763 | "(function() {" | ||
872 | 764 | "function getElementsByAttribute (strTagName, strAttributeName, arrAttributeValue) {" | ||
873 | 765 | " var arrElements = document.getElementsByTagName (strTagName);" | ||
874 | 766 | " var arrReturnElements = new Array();" | ||
875 | 767 | " for (var j=0; j<arrAttributeValue.length; j++) {" | ||
876 | 768 | " var strAttributeValue = arrAttributeValue[j];" | ||
877 | 769 | " for (var i=0; i<arrElements.length; i++) {" | ||
878 | 770 | " var oCurrent = arrElements[i];" | ||
879 | 771 | " var oAttribute = oCurrent.getAttribute && oCurrent.getAttribute (strAttributeName);" | ||
880 | 772 | " if (oAttribute && oAttribute.length > 0 && strAttributeValue.indexOf (oAttribute) != -1)" | ||
881 | 773 | " arrReturnElements.push (oCurrent);" | ||
882 | 774 | " }" | ||
883 | 775 | " }" | ||
884 | 776 | " return arrReturnElements;" | ||
885 | 777 | "};" | ||
886 | 778 | "function hideElementBySrc (uris) {" | ||
887 | 779 | " var oElements = getElementsByAttribute('img', 'src', uris);" | ||
888 | 780 | " if (oElements.length == 0)" | ||
889 | 781 | " oElements = getElementsByAttribute ('iframe', 'src', uris);" | ||
890 | 782 | " for (var i=0; i<oElements.length; i++) {" | ||
891 | 783 | " oElements[i].style.visibility = 'hidden !important';" | ||
892 | 784 | " oElements[i].style.width = '0';" | ||
893 | 785 | " oElements[i].style.height = '0';" | ||
894 | 786 | " }" | ||
895 | 787 | "};" | ||
896 | 788 | "var uris=new Array ();"); | ||
897 | 789 | |||
898 | 790 | for (li = uris; li != NULL; li = g_list_next (li)) | ||
899 | 791 | g_string_append_printf (js, "uris.push ('%s');", (gchar*)li->data); | ||
900 | 792 | |||
901 | 793 | g_string_append (js, "hideElementBySrc (uris);})();"); | ||
902 | 794 | |||
903 | 795 | return g_string_free (js, FALSE); | ||
904 | 796 | } | ||
905 | 797 | |||
906 | 798 | static gboolean | ||
907 | 799 | adblock_navigation_policy_decision_requested_cb (WebKitWebView* web_view, | ||
908 | 800 | WebKitWebFrame* web_frame, | ||
909 | 801 | WebKitNetworkRequest* request, | ||
910 | 802 | WebKitWebNavigationAction* action, | ||
911 | 803 | WebKitWebPolicyDecision* decision, | ||
912 | 804 | MidoriExtension* extension) | ||
913 | 805 | { | ||
914 | 806 | const gchar* uri = webkit_network_request_get_uri (request); | ||
915 | 807 | if (g_str_has_prefix (uri, "abp:")) | ||
916 | 808 | { | ||
917 | 809 | gchar** parts; | ||
918 | 810 | gchar* filter; | ||
919 | 811 | if (g_str_has_prefix (uri, "abp:subscribe?location=")) | ||
920 | 812 | uri = &uri[23]; | ||
921 | 813 | else if (g_str_has_prefix (uri, "abp://subscribe?location=")) | ||
922 | 814 | uri = &uri[25]; | ||
923 | 815 | else | ||
924 | 816 | return FALSE; | ||
925 | 817 | |||
926 | 818 | parts = g_strsplit (uri, "&", 2); | ||
927 | 819 | filter = soup_uri_decode (parts[0]); | ||
928 | 820 | webkit_web_policy_decision_ignore (decision); | ||
929 | 821 | adblock_show_preferences_dialog (extension, filter); | ||
930 | 822 | g_free (filter); | ||
931 | 823 | g_strfreev (parts); | ||
932 | 824 | return TRUE; | ||
933 | 825 | } | ||
934 | 826 | |||
935 | 827 | if (web_frame == webkit_web_view_get_main_frame (web_view)) | ||
936 | 828 | { | ||
937 | 829 | const gchar* req_uri = webkit_network_request_get_uri (request); | ||
938 | 830 | g_hash_table_replace (navigationwhitelist, web_view, g_strdup (req_uri)); | ||
939 | 831 | } | ||
940 | 832 | return false; | ||
941 | 833 | } | ||
942 | 834 | |||
943 | 835 | |||
944 | 836 | static void | ||
945 | 837 | adblock_resource_request_starting_cb (WebKitWebView* web_view, | ||
946 | 838 | WebKitWebFrame* web_frame, | ||
947 | 839 | WebKitWebResource* web_resource, | ||
948 | 840 | WebKitNetworkRequest* request, | ||
949 | 841 | WebKitNetworkResponse* response, | ||
950 | 842 | MidoriView* view) | ||
951 | 843 | { | ||
952 | 844 | SoupMessage* msg; | ||
953 | 845 | GList* blocked_uris; | ||
954 | 846 | const gchar* req_uri; | ||
955 | 847 | const char *page_uri; | ||
956 | 848 | |||
957 | 849 | page_uri = webkit_web_view_get_uri (web_view); | ||
958 | 850 | /* Skip checks on about: pages */ | ||
959 | 851 | if (midori_uri_is_blank (page_uri)) | ||
960 | 852 | return; | ||
961 | 853 | |||
962 | 854 | req_uri = webkit_network_request_get_uri (request); | ||
963 | 855 | |||
964 | 856 | if (!g_strcmp0 (req_uri, g_hash_table_lookup (navigationwhitelist, web_view))) | ||
965 | 857 | return; | ||
966 | 858 | |||
967 | 859 | if (!midori_uri_is_http (req_uri) | ||
968 | 860 | || g_str_has_suffix (req_uri, "favicon.ico")) | ||
969 | 861 | return; | ||
970 | 862 | |||
971 | 863 | msg = webkit_network_request_get_message (request); | ||
972 | 864 | if (!(msg && !g_strcmp0 (msg->method, "GET"))) | ||
973 | 865 | return; | ||
974 | 866 | |||
975 | 867 | if (response != NULL) /* request is caused by redirect */ | ||
976 | 868 | { | ||
977 | 869 | if (web_frame == webkit_web_view_get_main_frame (web_view)) | ||
978 | 870 | { | ||
979 | 871 | g_hash_table_replace (navigationwhitelist, web_view, g_strdup (req_uri)); | ||
980 | 872 | return; | ||
981 | 873 | } | ||
982 | 874 | } | ||
983 | 875 | |||
984 | 876 | if (midori_debug ("adblock:time")) | ||
985 | 877 | g_test_timer_start (); | ||
986 | 878 | if (adblock_is_matched (req_uri, page_uri)) | ||
987 | 879 | { | ||
988 | 880 | blocked_uris = g_object_get_data (G_OBJECT (web_view), "blocked-uris"); | ||
989 | 881 | blocked_uris = g_list_prepend (blocked_uris, g_strdup (req_uri)); | ||
990 | 882 | webkit_network_request_set_uri (request, "about:blank"); | ||
991 | 883 | g_object_set_data (G_OBJECT (web_view), "blocked-uris", blocked_uris); | ||
992 | 884 | } | ||
993 | 885 | if (midori_debug ("adblock:time")) | ||
994 | 886 | g_debug ("match: %f%s", g_test_timer_elapsed (), "seconds"); | ||
995 | 887 | } | ||
996 | 888 | |||
997 | 889 | static void | ||
998 | 890 | adblock_custom_block_image_cb (GtkWidget* widget, | ||
999 | 891 | MidoriExtension* extension) | ||
1000 | 892 | { | ||
1001 | 893 | gchar* custom_list; | ||
1002 | 894 | FILE* list; | ||
1003 | 895 | MidoriApp* app; | ||
1004 | 896 | GtkWidget* browser; | ||
1005 | 897 | GtkWidget* dialog; | ||
1006 | 898 | GtkWidget* content_area; | ||
1007 | 899 | GtkSizeGroup* sizegroup; | ||
1008 | 900 | GtkWidget* hbox; | ||
1009 | 901 | GtkWidget* label; | ||
1010 | 902 | GtkWidget* entry; | ||
1011 | 903 | gchar* title; | ||
1012 | 904 | |||
1013 | 905 | app = midori_extension_get_app (extension); | ||
1014 | 906 | browser = katze_object_get_object (app, "browser"); | ||
1015 | 907 | |||
1016 | 908 | title = _("Edit rule"); | ||
1017 | 909 | dialog = gtk_dialog_new_with_buttons (title, GTK_WINDOW (browser), | ||
1018 | 910 | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR, | ||
1019 | 911 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, | ||
1020 | 912 | GTK_STOCK_ADD, GTK_RESPONSE_ACCEPT, | ||
1021 | 913 | NULL); | ||
1022 | 914 | content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); | ||
1023 | 915 | gtk_window_set_icon_name (GTK_WINDOW (dialog), GTK_STOCK_ADD); | ||
1024 | 916 | gtk_container_set_border_width (GTK_CONTAINER (dialog), 5); | ||
1025 | 917 | gtk_container_set_border_width (GTK_CONTAINER (content_area), 5); | ||
1026 | 918 | sizegroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL); | ||
1027 | 919 | |||
1028 | 920 | hbox = gtk_hbox_new (FALSE, 8); | ||
1029 | 921 | gtk_container_set_border_width (GTK_CONTAINER (hbox), 5); | ||
1030 | 922 | label = gtk_label_new_with_mnemonic (_("_Rule:")); | ||
1031 | 923 | gtk_size_group_add_widget (sizegroup, label); | ||
1032 | 924 | gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); | ||
1033 | 925 | entry = gtk_entry_new (); | ||
1034 | 926 | gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE); | ||
1035 | 927 | gtk_entry_set_text (GTK_ENTRY (entry), | ||
1036 | 928 | g_object_get_data (G_OBJECT (widget), "uri")); | ||
1037 | 929 | gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0); | ||
1038 | 930 | gtk_box_pack_start (GTK_BOX (content_area), hbox, FALSE, TRUE, 0); | ||
1039 | 931 | gtk_widget_show_all (hbox); | ||
1040 | 932 | |||
1041 | 933 | gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT); | ||
1042 | 934 | if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_ACCEPT) | ||
1043 | 935 | { | ||
1044 | 936 | gtk_widget_destroy (dialog); | ||
1045 | 937 | return; | ||
1046 | 938 | } | ||
1047 | 939 | |||
1048 | 940 | custom_list = g_build_filename (midori_extension_get_config_dir (extension), | ||
1049 | 941 | CUSTOM_LIST_NAME, NULL); | ||
1050 | 942 | katze_mkdir_with_parents (midori_extension_get_config_dir (extension), 0700); | ||
1051 | 943 | if ((list = g_fopen (custom_list, "a+"))) | ||
1052 | 944 | { | ||
1053 | 945 | g_fprintf (list, "%s\n", gtk_entry_get_text (GTK_ENTRY (entry))); | ||
1054 | 946 | fclose (list); | ||
1055 | 947 | adblock_reload_rules (extension, TRUE); | ||
1056 | 948 | g_debug ("%s: Updated custom list\n", G_STRFUNC); | ||
1057 | 949 | } | ||
1058 | 950 | else | ||
1059 | 951 | g_debug ("%s: Failed to open custom list %s\n", G_STRFUNC, custom_list); | ||
1060 | 952 | g_free (custom_list); | ||
1061 | 953 | gtk_widget_destroy (dialog); | ||
1062 | 954 | } | ||
1063 | 955 | |||
1064 | 956 | static void | ||
1065 | 957 | adblock_populate_popup_cb (WebKitWebView* web_view, | ||
1066 | 958 | GtkWidget* menu, | ||
1067 | 959 | MidoriExtension* extension) | ||
1068 | 960 | { | ||
1069 | 961 | GtkWidget* menuitem; | ||
1070 | 962 | gchar *uri; | ||
1071 | 963 | gint x, y; | ||
1072 | 964 | GdkEventButton event; | ||
1073 | 965 | WebKitHitTestResultContext context; | ||
1074 | 966 | WebKitHitTestResult* hit_test; | ||
1075 | 967 | |||
1076 | 968 | gdk_window_get_pointer (gtk_widget_get_window(GTK_WIDGET (web_view)), &x, &y, NULL); | ||
1077 | 969 | event.x = x; | ||
1078 | 970 | event.y = y; | ||
1079 | 971 | hit_test = webkit_web_view_get_hit_test_result (web_view, &event); | ||
1080 | 972 | context = katze_object_get_int (hit_test, "context"); | ||
1081 | 973 | if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE) | ||
1082 | 974 | { | ||
1083 | 975 | uri = katze_object_get_string (hit_test, "image-uri"); | ||
1084 | 976 | menuitem = gtk_menu_item_new_with_mnemonic (_("Bl_ock image")); | ||
1085 | 977 | } | ||
1086 | 978 | else if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) | ||
1087 | 979 | { | ||
1088 | 980 | uri = katze_object_get_string (hit_test, "link-uri"); | ||
1089 | 981 | menuitem = gtk_menu_item_new_with_mnemonic (_("Bl_ock link")); | ||
1090 | 982 | } | ||
1091 | 983 | else | ||
1092 | 984 | return; | ||
1093 | 985 | gtk_widget_show (menuitem); | ||
1094 | 986 | gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem); | ||
1095 | 987 | g_object_set_data_full (G_OBJECT (menuitem), "uri", uri, (GDestroyNotify)g_free); | ||
1096 | 988 | g_signal_connect (menuitem, "activate", | ||
1097 | 989 | G_CALLBACK (adblock_custom_block_image_cb), extension); | ||
1098 | 990 | } | ||
1099 | 991 | |||
1100 | 992 | static void | ||
1101 | 993 | adblock_load_finished_cb (WebKitWebView *web_view, | ||
1102 | 994 | WebKitWebFrame *web_frame, | ||
1103 | 995 | gpointer user_data) | ||
1104 | 996 | { | ||
1105 | 997 | GList* uris = g_object_get_data (G_OBJECT (web_view), "blocked-uris"); | ||
1106 | 998 | gchar* script; | ||
1107 | 999 | GList* li; | ||
1108 | 1000 | |||
1109 | 1001 | if (g_list_nth_data (uris, 0) == NULL) | ||
1110 | 1002 | return; | ||
1111 | 1003 | |||
1112 | 1004 | script = adblock_prepare_urihider_js (uris); | ||
1113 | 1005 | webkit_web_view_execute_script (web_view, script); | ||
1114 | 1006 | li = NULL; | ||
1115 | 1007 | for (li = uris; li != NULL; li = g_list_next (li)) | ||
1116 | 1008 | uris = g_list_remove (uris, li->data); | ||
1117 | 1009 | g_free (script); | ||
1118 | 1010 | g_object_set_data (G_OBJECT (web_view), "blocked-uris", uris); | ||
1119 | 1011 | } | ||
1120 | 1012 | |||
1121 | 1013 | static void | ||
1122 | 1014 | adblock_window_object_cleared_cb (WebKitWebView* web_view, | ||
1123 | 1015 | WebKitWebFrame* web_frame, | ||
1124 | 1016 | JSContextRef js_context, | ||
1125 | 1017 | JSObjectRef js_window) | ||
1126 | 1018 | { | ||
1127 | 1019 | const char *page_uri; | ||
1128 | 1020 | gchar* script; | ||
1129 | 1021 | |||
1130 | 1022 | page_uri = webkit_web_frame_get_uri (web_frame); | ||
1131 | 1023 | /* Don't add adblock css into speeddial and about: pages */ | ||
1132 | 1024 | if (!midori_uri_is_http (page_uri)) | ||
1133 | 1025 | return; | ||
1134 | 1026 | |||
1135 | 1027 | script = adblock_build_js (page_uri); | ||
1136 | 1028 | if (!script) | ||
1137 | 1029 | return; | ||
1138 | 1030 | |||
1139 | 1031 | g_free (sokoke_js_script_eval (js_context, script, NULL)); | ||
1140 | 1032 | g_free (script); | ||
1141 | 1033 | } | ||
1142 | 1034 | |||
1143 | 1035 | static void | ||
1144 | 1036 | adblock_add_tab_cb (MidoriBrowser* browser, | ||
1145 | 1037 | MidoriView* view, | ||
1146 | 1038 | MidoriExtension* extension) | ||
1147 | 1039 | { | ||
1148 | 1040 | GtkWidget* web_view = midori_view_get_web_view (view); | ||
1149 | 1041 | |||
1150 | 1042 | g_signal_connect (web_view, "window-object-cleared", | ||
1151 | 1043 | G_CALLBACK (adblock_window_object_cleared_cb), 0); | ||
1152 | 1044 | |||
1153 | 1045 | g_signal_connect_after (web_view, "populate-popup", | ||
1154 | 1046 | G_CALLBACK (adblock_populate_popup_cb), extension); | ||
1155 | 1047 | g_signal_connect (web_view, "navigation-policy-decision-requested", | ||
1156 | 1048 | G_CALLBACK (adblock_navigation_policy_decision_requested_cb), extension); | ||
1157 | 1049 | g_signal_connect (web_view, "resource-request-starting", | ||
1158 | 1050 | G_CALLBACK (adblock_resource_request_starting_cb), view); | ||
1159 | 1051 | g_signal_connect (web_view, "load-finished", | ||
1160 | 1052 | G_CALLBACK (adblock_load_finished_cb), view); | ||
1161 | 1053 | } | ||
1162 | 1054 | |||
1163 | 1055 | static void | ||
1164 | 1056 | adblock_remove_tab_cb (MidoriBrowser* browser, | ||
1165 | 1057 | MidoriView* view, | ||
1166 | 1058 | MidoriExtension* extension) | ||
1167 | 1059 | { | ||
1168 | 1060 | GtkWidget* web_view = midori_view_get_web_view (view); | ||
1169 | 1061 | g_hash_table_remove (navigationwhitelist, web_view); | ||
1170 | 1062 | } | ||
1171 | 1063 | |||
1172 | 1064 | static void | ||
1173 | 1065 | adblock_deactivate_cb (MidoriExtension* extension, | ||
1174 | 1066 | MidoriBrowser* browser); | ||
1175 | 1067 | |||
1176 | 1068 | static void | ||
1177 | 1069 | adblock_app_add_browser_cb (MidoriApp* app, | ||
1178 | 1070 | MidoriBrowser* browser, | ||
1179 | 1071 | MidoriExtension* extension) | ||
1180 | 1072 | { | ||
1181 | 1073 | GtkWidget* statusbar; | ||
1182 | 1074 | GtkWidget* image; | ||
1183 | 1075 | GList* children; | ||
1184 | 1076 | |||
1185 | 1077 | statusbar = katze_object_get_object (browser, "statusbar"); | ||
1186 | 1078 | image = NULL; | ||
1187 | 1079 | /* image = gtk_image_new_from_stock (STOCK_IMAGE, GTK_ICON_SIZE_MENU); | ||
1188 | 1080 | gtk_widget_show (image); | ||
1189 | 1081 | gtk_box_pack_start (GTK_BOX (statusbar), image, FALSE, FALSE, 3); */ | ||
1190 | 1082 | g_object_set_data_full (G_OBJECT (browser), "status-image", image, | ||
1191 | 1083 | (GDestroyNotify)gtk_widget_destroy); | ||
1192 | 1084 | |||
1193 | 1085 | children = midori_browser_get_tabs (MIDORI_BROWSER (browser)); | ||
1194 | 1086 | for (; children; children = g_list_next (children)) | ||
1195 | 1087 | adblock_add_tab_cb (browser, children->data, extension); | ||
1196 | 1088 | g_list_free (children); | ||
1197 | 1089 | |||
1198 | 1090 | g_signal_connect (browser, "add-tab", | ||
1199 | 1091 | G_CALLBACK (adblock_add_tab_cb), extension); | ||
1200 | 1092 | g_signal_connect (browser, "remove-tab", | ||
1201 | 1093 | G_CALLBACK (adblock_remove_tab_cb), extension); | ||
1202 | 1094 | g_signal_connect (extension, "open-preferences", | ||
1203 | 1095 | G_CALLBACK (adblock_open_preferences_cb), extension); | ||
1204 | 1096 | g_signal_connect (extension, "deactivate", | ||
1205 | 1097 | G_CALLBACK (adblock_deactivate_cb), browser); | ||
1206 | 1098 | g_object_unref (statusbar); | ||
1207 | 1099 | } | ||
1208 | 1100 | |||
1209 | 1101 | static GString* | ||
1210 | 1102 | adblock_fixup_regexp (const gchar* prefix, | ||
1211 | 1103 | gchar* src) | ||
1212 | 1104 | { | ||
1213 | 1105 | GString* str; | ||
1214 | 1106 | int len = 0; | ||
1215 | 1107 | |||
1216 | 1108 | if (!src) | ||
1217 | 1109 | return NULL; | ||
1218 | 1110 | |||
1219 | 1111 | str = g_string_new (prefix); | ||
1220 | 1112 | |||
1221 | 1113 | /* lets strip first .* */ | ||
1222 | 1114 | if (src[0] == '*') | ||
1223 | 1115 | { | ||
1224 | 1116 | (void)*src++; | ||
1225 | 1117 | } | ||
1226 | 1118 | |||
1227 | 1119 | do | ||
1228 | 1120 | { | ||
1229 | 1121 | switch (*src) | ||
1230 | 1122 | { | ||
1231 | 1123 | case '*': | ||
1232 | 1124 | g_string_append (str, ".*"); | ||
1233 | 1125 | break; | ||
1234 | 1126 | /*case '.': | ||
1235 | 1127 | g_string_append (str, "\\."); | ||
1236 | 1128 | break;*/ | ||
1237 | 1129 | case '?': | ||
1238 | 1130 | g_string_append (str, "\\?"); | ||
1239 | 1131 | break; | ||
1240 | 1132 | case '|': | ||
1241 | 1133 | /* FIXME: We actually need to match :[0-9]+ or '/'. Sign means | ||
1242 | 1134 | "here could be port number or nothing". So bla.com^ will match | ||
1243 | 1135 | bla.com/ or bla.com:8080/ but not bla.com.au/ */ | ||
1244 | 1136 | case '^': | ||
1245 | 1137 | case '+': | ||
1246 | 1138 | break; | ||
1247 | 1139 | case '[': | ||
1248 | 1140 | g_string_append (str, "\\["); | ||
1249 | 1141 | break; | ||
1250 | 1142 | case ']': | ||
1251 | 1143 | g_string_append (str, "\\]"); | ||
1252 | 1144 | break; | ||
1253 | 1145 | case '(': | ||
1254 | 1146 | g_string_append (str, "\\("); | ||
1255 | 1147 | break; | ||
1256 | 1148 | case ')': | ||
1257 | 1149 | g_string_append (str, "\\)"); | ||
1258 | 1150 | break; | ||
1259 | 1151 | default: | ||
1260 | 1152 | g_string_append_printf (str,"%c", *src); | ||
1261 | 1153 | break; | ||
1262 | 1154 | } | ||
1263 | 1155 | src++; | ||
1264 | 1156 | } | ||
1265 | 1157 | while (*src); | ||
1266 | 1158 | |||
1267 | 1159 | len = str->len; | ||
1268 | 1160 | /* We dont need .* in the end of url. Thats stupid */ | ||
1269 | 1161 | if (str->str && str->str[len-1] == '*' && str->str[len-2] == '.') | ||
1270 | 1162 | g_string_erase (str, len-2, 2); | ||
1271 | 1163 | |||
1272 | 1164 | return str; | ||
1273 | 1165 | } | ||
1274 | 1166 | |||
1275 | 1167 | static gboolean | ||
1276 | 1168 | adblock_compile_regexp (GString* gpatt, | ||
1277 | 1169 | gchar* opts) | ||
1278 | 1170 | { | ||
1279 | 1171 | GRegex* regex; | ||
1280 | 1172 | GError* error = NULL; | ||
1281 | 1173 | int pos = 0; | ||
1282 | 1174 | gchar *sig; | ||
1283 | 1175 | gchar *patt; | ||
1284 | 1176 | int len; | ||
1285 | 1177 | |||
1286 | 1178 | if (!gpatt) | ||
1287 | 1179 | return FALSE; | ||
1288 | 1180 | |||
1289 | 1181 | patt = gpatt->str; | ||
1290 | 1182 | len = gpatt->len; | ||
1291 | 1183 | |||
1292 | 1184 | /* TODO: Play with optimization flags */ | ||
1293 | 1185 | regex = g_regex_new (patt, G_REGEX_OPTIMIZE, | ||
1294 | 1186 | G_REGEX_MATCH_NOTEMPTY, &error); | ||
1295 | 1187 | if (error) | ||
1296 | 1188 | { | ||
1297 | 1189 | g_warning ("%s: %s", G_STRFUNC, error->message); | ||
1298 | 1190 | g_error_free (error); | ||
1299 | 1191 | return TRUE; | ||
1300 | 1192 | } | ||
1301 | 1193 | |||
1302 | 1194 | if (!g_regex_match_simple ("^/.*[\\^\\$\\*].*/$", patt, G_REGEX_UNGREEDY, G_REGEX_MATCH_NOTEMPTY)) | ||
1303 | 1195 | { | ||
1304 | 1196 | int signature_count = 0; | ||
1305 | 1197 | |||
1306 | 1198 | for (pos = len - SIGNATURE_SIZE; pos >= 0; pos--) { | ||
1307 | 1199 | sig = g_strndup (patt + pos, SIGNATURE_SIZE); | ||
1308 | 1200 | if (!g_regex_match_simple ("[\\*]", sig, G_REGEX_UNGREEDY, G_REGEX_MATCH_NOTEMPTY) && | ||
1309 | 1201 | !g_hash_table_lookup (keys, sig)) | ||
1310 | 1202 | { | ||
1311 | 1203 | adblock_debug ("sig: %s %s", sig, patt); | ||
1312 | 1204 | g_hash_table_insert (keys, sig, regex); | ||
1313 | 1205 | g_hash_table_insert (optslist, sig, g_strdup (opts)); | ||
1314 | 1206 | signature_count++; | ||
1315 | 1207 | } | ||
1316 | 1208 | else | ||
1317 | 1209 | { | ||
1318 | 1210 | if (g_regex_match_simple ("^\\*", sig, G_REGEX_UNGREEDY, G_REGEX_MATCH_NOTEMPTY) && | ||
1319 | 1211 | !g_hash_table_lookup (pattern, patt)) | ||
1320 | 1212 | { | ||
1321 | 1213 | adblock_debug ("patt2: %s %s", sig, patt); | ||
1322 | 1214 | g_hash_table_insert (pattern, patt, regex); | ||
1323 | 1215 | g_hash_table_insert (optslist, patt, g_strdup (opts)); | ||
1324 | 1216 | } | ||
1325 | 1217 | g_free (sig); | ||
1326 | 1218 | } | ||
1327 | 1219 | } | ||
1328 | 1220 | if (signature_count > 1 && g_hash_table_lookup (pattern, patt)) | ||
1329 | 1221 | { | ||
1330 | 1222 | g_hash_table_steal (pattern, patt); | ||
1331 | 1223 | return TRUE; | ||
1332 | 1224 | } | ||
1333 | 1225 | return FALSE; | ||
1334 | 1226 | } | ||
1335 | 1227 | else | ||
1336 | 1228 | { | ||
1337 | 1229 | adblock_debug ("patt: %s%s", patt, ""); | ||
1338 | 1230 | /* Pattern is a regexp chars */ | ||
1339 | 1231 | g_hash_table_insert (pattern, patt, regex); | ||
1340 | 1232 | g_hash_table_insert (optslist, patt, g_strdup (opts)); | ||
1341 | 1233 | return FALSE; | ||
1342 | 1234 | } | ||
1343 | 1235 | } | ||
1344 | 1236 | |||
1345 | 1237 | static gchar* | ||
1346 | 1238 | adblock_add_url_pattern (gchar* prefix, | ||
1347 | 1239 | gchar* type, | ||
1348 | 1240 | gchar* line) | ||
1349 | 1241 | { | ||
1350 | 1242 | gchar** data; | ||
1351 | 1243 | gchar* patt; | ||
1352 | 1244 | GString* format_patt; | ||
1353 | 1245 | gchar* opts; | ||
1354 | 1246 | gboolean should_free; | ||
1355 | 1247 | |||
1356 | 1248 | data = g_strsplit (line, "$", -1); | ||
1357 | 1249 | if (!data || !data[0]) | ||
1358 | 1250 | { | ||
1359 | 1251 | g_strfreev (data); | ||
1360 | 1252 | return NULL; | ||
1361 | 1253 | } | ||
1362 | 1254 | |||
1363 | 1255 | if (data[1] && data[2]) | ||
1364 | 1256 | { | ||
1365 | 1257 | patt = g_strconcat (data[0], data[1], NULL); | ||
1366 | 1258 | opts = g_strconcat (type, ",", data[2], NULL); | ||
1367 | 1259 | } | ||
1368 | 1260 | else if (data[1]) | ||
1369 | 1261 | { | ||
1370 | 1262 | patt = data[0]; | ||
1371 | 1263 | opts = g_strconcat (type, ",", data[1], NULL); | ||
1372 | 1264 | } | ||
1373 | 1265 | else | ||
1374 | 1266 | { | ||
1375 | 1267 | patt = data[0]; | ||
1376 | 1268 | opts = type; | ||
1377 | 1269 | } | ||
1378 | 1270 | |||
1379 | 1271 | if (g_regex_match_simple ("subdocument", opts, | ||
1380 | 1272 | G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY)) | ||
1381 | 1273 | { | ||
1382 | 1274 | if (data[1] && data[2]) | ||
1383 | 1275 | g_free (patt); | ||
1384 | 1276 | if (data[1]) | ||
1385 | 1277 | g_free (opts); | ||
1386 | 1278 | g_strfreev (data); | ||
1387 | 1279 | return NULL; | ||
1388 | 1280 | } | ||
1389 | 1281 | |||
1390 | 1282 | format_patt = adblock_fixup_regexp (prefix, patt); | ||
1391 | 1283 | |||
1392 | 1284 | adblock_debug ("got: %s opts %s", format_patt->str, opts); | ||
1393 | 1285 | should_free = adblock_compile_regexp (format_patt, opts); | ||
1394 | 1286 | |||
1395 | 1287 | if (data[1] && data[2]) | ||
1396 | 1288 | g_free (patt); | ||
1397 | 1289 | if (data[1]) | ||
1398 | 1290 | g_free (opts); | ||
1399 | 1291 | g_strfreev (data); | ||
1400 | 1292 | |||
1401 | 1293 | return g_string_free (format_patt, should_free); | ||
1402 | 1294 | } | ||
1403 | 1295 | |||
1404 | 1296 | static void | ||
1405 | 1297 | adblock_frame_add (gchar* line) | ||
1406 | 1298 | { | ||
1407 | 1299 | const gchar* separator = " , "; | ||
1408 | 1300 | |||
1409 | 1301 | (void)*line++; | ||
1410 | 1302 | (void)*line++; | ||
1411 | 1303 | if (strchr (line, '\'') | ||
1412 | 1304 | || (strchr (line, ':') | ||
1413 | 1305 | && !g_regex_match_simple (".*\\[.*:.*\\].*", line, | ||
1414 | 1306 | G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY))) | ||
1415 | 1307 | { | ||
1416 | 1308 | return; | ||
1417 | 1309 | } | ||
1418 | 1310 | g_string_append (blockcss, separator); | ||
1419 | 1311 | g_string_append (blockcss, line); | ||
1420 | 1312 | } | ||
1421 | 1313 | |||
1422 | 1314 | static void | ||
1423 | 1315 | adblock_update_css_hash (gchar* domain, | ||
1424 | 1316 | gchar* value) | ||
1425 | 1317 | { | ||
1426 | 1318 | const gchar* olddata; | ||
1427 | 1319 | gchar* newdata; | ||
1428 | 1320 | |||
1429 | 1321 | if ((olddata = g_hash_table_lookup (blockcssprivate, domain))) | ||
1430 | 1322 | { | ||
1431 | 1323 | newdata = g_strconcat (olddata, " , ", value, NULL); | ||
1432 | 1324 | g_hash_table_replace (blockcssprivate, g_strdup (domain), newdata); | ||
1433 | 1325 | } | ||
1434 | 1326 | else | ||
1435 | 1327 | g_hash_table_insert (blockcssprivate, g_strdup (domain), g_strdup (value)); | ||
1436 | 1328 | } | ||
1437 | 1329 | |||
1438 | 1330 | static void | ||
1439 | 1331 | adblock_frame_add_private (const gchar* line, | ||
1440 | 1332 | const gchar* sep) | ||
1441 | 1333 | { | ||
1442 | 1334 | gchar** data; | ||
1443 | 1335 | data = g_strsplit (line, sep, 2); | ||
1444 | 1336 | |||
1445 | 1337 | if (!(data[1] && *data[1]) | ||
1446 | 1338 | || strchr (data[1], '\'') | ||
1447 | 1339 | || (strchr (data[1], ':') | ||
1448 | 1340 | && !g_regex_match_simple (".*\\[.*:.*\\].*", data[1], | ||
1449 | 1341 | G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY))) | ||
1450 | 1342 | { | ||
1451 | 1343 | g_strfreev (data); | ||
1452 | 1344 | return; | ||
1453 | 1345 | } | ||
1454 | 1346 | |||
1455 | 1347 | if (strchr (data[0], ',')) | ||
1456 | 1348 | { | ||
1457 | 1349 | gchar** domains; | ||
1458 | 1350 | gint i; | ||
1459 | 1351 | |||
1460 | 1352 | domains = g_strsplit (data[0], ",", -1); | ||
1461 | 1353 | for (i = 0; domains[i]; i++) | ||
1462 | 1354 | { | ||
1463 | 1355 | gchar* domain; | ||
1464 | 1356 | |||
1465 | 1357 | domain = domains[i]; | ||
1466 | 1358 | /* Ignore Firefox-specific option */ | ||
1467 | 1359 | if (!g_strcmp0 (domain, "~pregecko2")) | ||
1468 | 1360 | continue; | ||
1469 | 1361 | /* FIXME: ~ should negate match */ | ||
1470 | 1362 | if (domain[0] == '~') | ||
1471 | 1363 | domain++; | ||
1472 | 1364 | adblock_update_css_hash (g_strstrip (domain), data[1]); | ||
1473 | 1365 | } | ||
1474 | 1366 | g_strfreev (domains); | ||
1475 | 1367 | } | ||
1476 | 1368 | else | ||
1477 | 1369 | { | ||
1478 | 1370 | adblock_update_css_hash (data[0], data[1]); | ||
1479 | 1371 | } | ||
1480 | 1372 | g_strfreev (data); | ||
1481 | 1373 | } | ||
1482 | 1374 | |||
1483 | 1375 | static gchar* | ||
1484 | 1376 | adblock_parse_line (gchar* line) | ||
1485 | 1377 | { | ||
1486 | 1378 | /* | ||
1487 | 1379 | * AdblockPlus rule reference based on http://adblockplus.org/en/filters | ||
1488 | 1380 | * Block URL: | ||
1489 | 1381 | * http://example.com/ads/banner123.gif | ||
1490 | 1382 | * http://example.com/ads/banner*.gif | ||
1491 | 1383 | * Partial match for "ad": | ||
1492 | 1384 | * *ad* | ||
1493 | 1385 | * ad | ||
1494 | 1386 | * Block example.com/annoyingflash.swf but not example.com/swf/: | ||
1495 | 1387 | * swf| | ||
1496 | 1388 | * Block bad.example/banner.gif but not good.example/analyze?http://bad.example: | ||
1497 | 1389 | * |http://baddomain.example/ | ||
1498 | 1390 | * Block http(s) example.com but not badexample.com or good.example/analyze?http://bad.example: | ||
1499 | 1391 | * ||example.com/banner.gif | ||
1500 | 1392 | * Block example.com/ and example.com:8000/ but not example.com.ar/: | ||
1501 | 1393 | * http://example.com^ | ||
1502 | 1394 | * A ^ matches anything that isn't A-Za-z0-0_-.% | ||
1503 | 1395 | * Block example.com:8000/foo.bar?a=12&b=%D1%82%D0%B5: | ||
1504 | 1396 | * ^example.com^ | ||
1505 | 1397 | * ^%D1%82%D0%B5^ | ||
1506 | 1398 | * ^foo.bar^ | ||
1507 | 1399 | * TODO: ^ is partially supported by Midori | ||
1508 | 1400 | * Block banner123 and banner321 with a regex: | ||
1509 | 1401 | * /banner\d+/ | ||
1510 | 1402 | * Never block URIs with "advice": | ||
1511 | 1403 | * @@advice | ||
1512 | 1404 | * No blocking at all: | ||
1513 | 1405 | * @@http://example.com | ||
1514 | 1406 | * @@|http://example.com | ||
1515 | 1407 | * TODO: @@ is currently ignored by Midori. | ||
1516 | 1408 | * Element hiding by class: | ||
1517 | 1409 | * ##textad | ||
1518 | 1410 | * ##div.textad | ||
1519 | 1411 | * Element hiding by id: | ||
1520 | 1412 | * ##div#sponsorad | ||
1521 | 1413 | * ##*#sponsorad | ||
1522 | 1414 | * Match example.com/ and something.example.com/ but not example.org/ | ||
1523 | 1415 | * example.com##*.sponsor | ||
1524 | 1416 | * Match multiple domains: | ||
1525 | 1417 | * domain1.example,domain2.example,domain3.example##*.sponsor | ||
1526 | 1418 | * Match on any domain but "example.com": | ||
1527 | 1419 | * ~example.com##*.sponsor | ||
1528 | 1420 | * Match on "example.com" except "foo.example.com": | ||
1529 | 1421 | * example.com,~foo.example.com##*.sponsor | ||
1530 | 1422 | * By design rules only apply to full domain names: | ||
1531 | 1423 | * "domain" is NOT equal to "domain.example,domain.test." | ||
1532 | 1424 | * In Firefox rules can apply to browser UI: | ||
1533 | 1425 | * browser##menuitem#javascriptConsole will hide the Console menuitem | ||
1534 | 1426 | * Hide tables with width attribute 80%: | ||
1535 | 1427 | * ##table[width="80%"] | ||
1536 | 1428 | * Hide all div with title attribute containing "adv": | ||
1537 | 1429 | * ##div[title*="adv"] | ||
1538 | 1430 | * Hide div with title starting with "adv" and ending with "ert": | ||
1539 | 1431 | * ##div[title^="adv"][title$="ert"] | ||
1540 | 1432 | * Match tables with width attribute 80% and bgcolor attribute white: | ||
1541 | 1433 | * table[width="80%"][bgcolor="white"] | ||
1542 | 1434 | * TODO: [] is currently ignored by Midori | ||
1543 | 1435 | * Hide anything following div with class "adheader": | ||
1544 | 1436 | * ##div.adheader + * | ||
1545 | 1437 | * Old CSS element hiding syntax, officially deprecated: | ||
1546 | 1438 | * #div(id=foo) | ||
1547 | 1439 | * Match anything but "example.com" | ||
1548 | 1440 | * ~example.com##*.sponsor | ||
1549 | 1441 | * TODO: ~ is currently ignored by Midori | ||
1550 | 1442 | * Match "example.com" domain except "foo.example.com": | ||
1551 | 1443 | * example.com,~foo.example.com##*.sponsor | ||
1552 | 1444 | * ! Comment | ||
1553 | 1445 | * Supported options after a trailing $: | ||
1554 | 1446 | * domain,third-party,~pregecko2 | ||
1555 | 1447 | * Official options (not all supported by Midori): | ||
1556 | 1448 | * script,image,stylesheet,object,xmlhttprequest,object-subrequest, | ||
1557 | 1449 | * subdocument,document,elemhide,popup,third-party,sitekey,match-case | ||
1558 | 1450 | * collapse,donottrack,pregecko2 | ||
1559 | 1451 | * Deprecated: | ||
1560 | 1452 | * background,xbl,ping,dtd | ||
1561 | 1453 | * Inverse options: | ||
1562 | 1454 | * ~script,~image,~stylesheet,~object,~xmlhttprequest,~collapse, | ||
1563 | 1455 | * ~object-subrequest,~subdocument,~document,~elemhide,~third-party, | ||
1564 | 1456 | * ~pregecko2 | ||
1565 | 1457 | **/ | ||
1566 | 1458 | |||
1567 | 1459 | /* Skip invalid, empty and comment lines */ | ||
1568 | 1460 | if (!(line && line[0] != ' ' && line[0] != '!' && line[0])) | ||
1569 | 1461 | return NULL; | ||
1570 | 1462 | |||
1571 | 1463 | /* FIXME: No support for whitelisting */ | ||
1572 | 1464 | if (line[0] == '@' && line[1] == '@') | ||
1573 | 1465 | return NULL; | ||
1574 | 1466 | /* FIXME: No support for [include] and [exclude] tags */ | ||
1575 | 1467 | if (line[0] == '[') | ||
1576 | 1468 | return NULL; | ||
1577 | 1469 | |||
1578 | 1470 | g_strchomp (line); | ||
1579 | 1471 | |||
1580 | 1472 | /* Got CSS block hider */ | ||
1581 | 1473 | if (line[0] == '#' && line[1] == '#' ) | ||
1582 | 1474 | { | ||
1583 | 1475 | adblock_frame_add (line); | ||
1584 | 1476 | return NULL; | ||
1585 | 1477 | } | ||
1586 | 1478 | /* Got CSS block hider. Workaround */ | ||
1587 | 1479 | if (line[0] == '#') | ||
1588 | 1480 | return NULL; | ||
1589 | 1481 | |||
1590 | 1482 | /* Got per domain CSS hider rule */ | ||
1591 | 1483 | if (strstr (line, "##")) | ||
1592 | 1484 | { | ||
1593 | 1485 | adblock_frame_add_private (line, "##"); | ||
1594 | 1486 | return NULL; | ||
1595 | 1487 | } | ||
1596 | 1488 | /* Got per domain CSS hider rule. Workaround */ | ||
1597 | 1489 | if (strchr (line, '#')) | ||
1598 | 1490 | { | ||
1599 | 1491 | adblock_frame_add_private (line, "#"); | ||
1600 | 1492 | return NULL; | ||
1601 | 1493 | } | ||
1602 | 1494 | |||
1603 | 1495 | /* Got URL blocker rule */ | ||
1604 | 1496 | if (line[0] == '|' && line[1] == '|' ) | ||
1605 | 1497 | { | ||
1606 | 1498 | (void)*line++; | ||
1607 | 1499 | (void)*line++; | ||
1608 | 1500 | return adblock_add_url_pattern ("", "fulluri", line); | ||
1609 | 1501 | } | ||
1610 | 1502 | if (line[0] == '|') | ||
1611 | 1503 | { | ||
1612 | 1504 | (void)*line++; | ||
1613 | 1505 | return adblock_add_url_pattern ("^", "fulluri", line); | ||
1614 | 1506 | } | ||
1615 | 1507 | return adblock_add_url_pattern ("", "uri", line); | ||
1616 | 1508 | } | ||
1617 | 1509 | |||
1618 | 1510 | static GDateMonth | ||
1619 | 1511 | str_month_name_to_gdate (const gchar* month) | ||
1620 | 1512 | { | ||
1621 | 1513 | guint i; | ||
1622 | 1514 | const gchar* months[] = { | ||
1623 | 1515 | "", "January", "February", "March", "April", "May", "June", | ||
1624 | 1516 | "July", "August", "September", "October", "November", "December" | ||
1625 | 1517 | }; | ||
1626 | 1518 | |||
1627 | 1519 | for (i = 0; i < G_N_ELEMENTS (months); i++) | ||
1628 | 1520 | { | ||
1629 | 1521 | if (strncmp (month, months[i], 3) == 0) | ||
1630 | 1522 | return i; | ||
1631 | 1523 | } | ||
1632 | 1524 | return 0; | ||
1633 | 1525 | } | ||
1634 | 1526 | |||
1635 | 1527 | static gboolean | ||
1636 | 1528 | adblock_file_is_up_to_date (gchar* path) | ||
1637 | 1529 | { | ||
1638 | 1530 | FILE* file; | ||
1639 | 1531 | gchar line[2000]; | ||
1640 | 1532 | |||
1641 | 1533 | /* Check a chunk of header for update info */ | ||
1642 | 1534 | if ((file = g_fopen (path, "r"))) | ||
1643 | 1535 | { | ||
1644 | 1536 | gint days_to_expire = 0; | ||
1645 | 1537 | gchar* timestamp = NULL; | ||
1646 | 1538 | guint i; | ||
1647 | 1539 | gboolean found_meta = FALSE; | ||
1648 | 1540 | gint fs_days_elapsed, days_elapsed = 0, least_days; | ||
1649 | 1541 | |||
1650 | 1542 | for (i = 0; i <= 15; i++) | ||
1651 | 1543 | { | ||
1652 | 1544 | if (!fgets (line, 2000, file)) | ||
1653 | 1545 | break; | ||
1654 | 1546 | |||
1655 | 1547 | if (strncmp ("! Expires", line, 9) == 0) | ||
1656 | 1548 | { | ||
1657 | 1549 | gchar** parts = g_strsplit (line, " ", 4); | ||
1658 | 1550 | days_to_expire = atoi (parts[2]); | ||
1659 | 1551 | g_strfreev (parts); | ||
1660 | 1552 | found_meta = TRUE; | ||
1661 | 1553 | } | ||
1662 | 1554 | if (strncmp ("! This list expires after", line, 25) == 0) | ||
1663 | 1555 | { | ||
1664 | 1556 | gchar** parts = g_strsplit (line, " ", 7); | ||
1665 | 1557 | |||
1666 | 1558 | if (strncmp (parts[6], "days", 4) == 0) | ||
1667 | 1559 | days_to_expire = atoi (parts[5]); | ||
1668 | 1560 | if (strncmp (parts[6], "hours", 5) == 0) | ||
1669 | 1561 | days_to_expire = (atoi (parts[5])) / 24; | ||
1670 | 1562 | |||
1671 | 1563 | g_strfreev (parts); | ||
1672 | 1564 | found_meta = TRUE; | ||
1673 | 1565 | } | ||
1674 | 1566 | |||
1675 | 1567 | if (strncmp ("! Last mod", line, 10) == 0 | ||
1676 | 1568 | || strncmp ("! Updated", line, 9) == 0) | ||
1677 | 1569 | { | ||
1678 | 1570 | gchar** parts = g_strsplit (line, ":", 2); | ||
1679 | 1571 | timestamp = g_strdup (parts[1] + 1); | ||
1680 | 1572 | g_strchomp (timestamp); | ||
1681 | 1573 | g_strfreev (parts); | ||
1682 | 1574 | found_meta = TRUE; | ||
1683 | 1575 | } | ||
1684 | 1576 | } | ||
1685 | 1577 | |||
1686 | 1578 | if (!found_meta) | ||
1687 | 1579 | { | ||
1688 | 1580 | g_print ("Adblock: no metadata found in %s (broken download?)\n", path); | ||
1689 | 1581 | return FALSE; | ||
1690 | 1582 | } | ||
1691 | 1583 | |||
1692 | 1584 | /* query filesystem about file change, maybe there is no update yet | ||
1693 | 1585 | * or there is no "modified" metadata to check, otherwise we will repeatedly | ||
1694 | 1586 | * download files that have no new updates */ | ||
1695 | 1587 | { | ||
1696 | 1588 | GDate* current = g_date_new (); | ||
1697 | 1589 | GDate* fs_mod_date = g_date_new (); | ||
1698 | 1590 | GTimeVal mod_time; | ||
1699 | 1591 | GFile* filter_file = g_file_new_for_path (path); | ||
1700 | 1592 | GFileInfo* info = g_file_query_info (filter_file, "time:modified", 0, NULL, NULL); | ||
1701 | 1593 | |||
1702 | 1594 | g_file_info_get_modification_time (info, &mod_time); | ||
1703 | 1595 | g_date_set_time_t (current, time (NULL)); | ||
1704 | 1596 | g_date_set_time_val (fs_mod_date, &mod_time); | ||
1705 | 1597 | |||
1706 | 1598 | fs_days_elapsed = g_date_days_between (fs_mod_date, current); | ||
1707 | 1599 | |||
1708 | 1600 | g_date_free (current); | ||
1709 | 1601 | g_date_free (fs_mod_date); | ||
1710 | 1602 | } | ||
1711 | 1603 | |||
1712 | 1604 | /* If there is no update metadata but file is valid, assume one week */ | ||
1713 | 1605 | if ((!days_to_expire && !timestamp) && fs_days_elapsed < 7) | ||
1714 | 1606 | return TRUE; | ||
1715 | 1607 | |||
1716 | 1608 | if (days_to_expire && timestamp != NULL) | ||
1717 | 1609 | { | ||
1718 | 1610 | GDate* current = g_date_new (); | ||
1719 | 1611 | GDate* mod_date = g_date_new (); | ||
1720 | 1612 | gchar** parts; | ||
1721 | 1613 | gboolean use_dots = FALSE; | ||
1722 | 1614 | |||
1723 | 1615 | /* Common dates are 20 Mar 2012, 20.08.2012 */ | ||
1724 | 1616 | if (strrchr (timestamp, '.')) | ||
1725 | 1617 | { | ||
1726 | 1618 | use_dots = TRUE; | ||
1727 | 1619 | /* In case of date like '20.08.2012 12:34' | ||
1728 | 1620 | * we should also nuke the time part */ | ||
1729 | 1621 | if (strrchr (timestamp, ' ')) | ||
1730 | 1622 | { | ||
1731 | 1623 | gchar** part = g_strsplit (timestamp, " ", 2); | ||
1732 | 1624 | parts = g_strsplit (part[0], ".", 4); | ||
1733 | 1625 | g_strfreev (part); | ||
1734 | 1626 | } | ||
1735 | 1627 | else | ||
1736 | 1628 | parts = g_strsplit (timestamp, ".", 4); | ||
1737 | 1629 | } | ||
1738 | 1630 | else | ||
1739 | 1631 | parts = g_strsplit (timestamp, " ", 4); | ||
1740 | 1632 | |||
1741 | 1633 | if (use_dots) | ||
1742 | 1634 | g_date_set_month (mod_date, atoi (parts[1])); | ||
1743 | 1635 | else | ||
1744 | 1636 | g_date_set_month (mod_date, str_month_name_to_gdate (parts[1])); | ||
1745 | 1637 | |||
1746 | 1638 | /* check if first part is year 201(2) or day */ | ||
1747 | 1639 | if (strncmp (parts[0], "201", 3) == 0) | ||
1748 | 1640 | { | ||
1749 | 1641 | g_date_set_day (mod_date, atoi (parts[2])); | ||
1750 | 1642 | g_date_set_year (mod_date, atoi (parts[0])); | ||
1751 | 1643 | } | ||
1752 | 1644 | else | ||
1753 | 1645 | { | ||
1754 | 1646 | g_date_set_day (mod_date, atoi (parts[0])); | ||
1755 | 1647 | g_date_set_year (mod_date, atoi (parts[2])); | ||
1756 | 1648 | } | ||
1757 | 1649 | g_strfreev (parts); | ||
1758 | 1650 | |||
1759 | 1651 | g_date_set_time_t (current, time (NULL)); | ||
1760 | 1652 | days_elapsed = g_date_days_between (mod_date, current); | ||
1761 | 1653 | |||
1762 | 1654 | g_date_free (current); | ||
1763 | 1655 | g_date_free (mod_date); | ||
1764 | 1656 | g_free (timestamp); | ||
1765 | 1657 | } | ||
1766 | 1658 | |||
1767 | 1659 | /* File from the future? Assume up to date */ | ||
1768 | 1660 | if (days_elapsed < 0) | ||
1769 | 1661 | { | ||
1770 | 1662 | g_print ("Adblock: file %s appears to be from the future," | ||
1771 | 1663 | "check your system clock!\n", path); | ||
1772 | 1664 | return TRUE; | ||
1773 | 1665 | } | ||
1774 | 1666 | |||
1775 | 1667 | least_days = days_elapsed < fs_days_elapsed ? days_elapsed : fs_days_elapsed; | ||
1776 | 1668 | if (least_days < days_to_expire) | ||
1777 | 1669 | return TRUE; | ||
1778 | 1670 | else | ||
1779 | 1671 | return FALSE; | ||
1780 | 1672 | |||
1781 | 1673 | return TRUE; | ||
1782 | 1674 | } | ||
1783 | 1675 | return FALSE; | ||
1784 | 1676 | } | ||
1785 | 1677 | |||
1786 | 1678 | static void | ||
1787 | 1679 | adblock_parse_file (gchar* path) | ||
1788 | 1680 | { | ||
1789 | 1681 | FILE* file; | ||
1790 | 1682 | gchar line[2000]; | ||
1791 | 1683 | |||
1792 | 1684 | if ((file = g_fopen (path, "r"))) | ||
1793 | 1685 | { | ||
1794 | 1686 | while (fgets (line, 2000, file)) | ||
1795 | 1687 | adblock_parse_line (line); | ||
1796 | 1688 | fclose (file); | ||
1797 | 1689 | } | ||
1798 | 1690 | } | ||
1799 | 1691 | |||
1800 | 1692 | static void | ||
1801 | 1693 | adblock_deactivate_tabs (MidoriView* view, | ||
1802 | 1694 | MidoriBrowser* browser, | ||
1803 | 1695 | MidoriExtension* extension) | ||
1804 | 1696 | { | ||
1805 | 1697 | GtkWidget* web_view = midori_view_get_web_view (view); | ||
1806 | 1698 | |||
1807 | 1699 | g_signal_handlers_disconnect_by_func ( | ||
1808 | 1700 | web_view, adblock_window_object_cleared_cb, 0); | ||
1809 | 1701 | g_signal_handlers_disconnect_by_func ( | ||
1810 | 1702 | web_view, adblock_populate_popup_cb, extension); | ||
1811 | 1703 | g_signal_handlers_disconnect_by_func ( | ||
1812 | 1704 | web_view, adblock_resource_request_starting_cb, view); | ||
1813 | 1705 | g_signal_handlers_disconnect_by_func ( | ||
1814 | 1706 | web_view, adblock_load_finished_cb, view); | ||
1815 | 1707 | g_signal_handlers_disconnect_by_func ( | ||
1816 | 1708 | web_view, adblock_navigation_policy_decision_requested_cb, extension); | ||
1817 | 1709 | } | ||
1818 | 1710 | |||
1819 | 1711 | static void | ||
1820 | 1712 | adblock_deactivate_cb (MidoriExtension* extension, | ||
1821 | 1713 | MidoriBrowser* browser) | ||
1822 | 1714 | { | ||
1823 | 1715 | GList* children; | ||
1824 | 1716 | MidoriApp* app = midori_extension_get_app (extension); | ||
1825 | 1717 | MidoriWebSettings* settings = katze_object_get_object (app, "settings"); | ||
1826 | 1718 | |||
1827 | 1719 | g_signal_handlers_disconnect_by_func ( | ||
1828 | 1720 | browser, adblock_open_preferences_cb, extension); | ||
1829 | 1721 | g_signal_handlers_disconnect_by_func ( | ||
1830 | 1722 | extension, adblock_deactivate_cb, browser); | ||
1831 | 1723 | g_signal_handlers_disconnect_by_func ( | ||
1832 | 1724 | app, adblock_app_add_browser_cb, extension); | ||
1833 | 1725 | g_signal_handlers_disconnect_by_func ( | ||
1834 | 1726 | browser, adblock_add_tab_cb, extension); | ||
1835 | 1727 | g_signal_handlers_disconnect_by_func ( | ||
1836 | 1728 | browser, adblock_remove_tab_cb, extension); | ||
1837 | 1729 | |||
1838 | 1730 | children = midori_browser_get_tabs (MIDORI_BROWSER (browser)); | ||
1839 | 1731 | for (; children; children = g_list_next (children)) | ||
1840 | 1732 | adblock_deactivate_tabs (children->data, browser, extension); | ||
1841 | 1733 | g_list_free (children); | ||
1842 | 1734 | |||
1843 | 1735 | adblock_destroy_db (); | ||
1844 | 1736 | midori_web_settings_remove_style (settings, "adblock-blockcss"); | ||
1845 | 1737 | g_object_unref (settings); | ||
1846 | 1738 | } | ||
1847 | 1739 | |||
1848 | 1740 | static void | ||
1849 | 1741 | adblock_activate_cb (MidoriExtension* extension, | ||
1850 | 1742 | MidoriApp* app) | ||
1851 | 1743 | { | ||
1852 | 1744 | KatzeArray* browsers; | ||
1853 | 1745 | MidoriBrowser* browser; | ||
1854 | 1746 | |||
1855 | 1747 | adblock_reload_rules (extension, FALSE); | ||
1856 | 1748 | |||
1857 | 1749 | browsers = katze_object_get_object (app, "browsers"); | ||
1858 | 1750 | KATZE_ARRAY_FOREACH_ITEM (browser, browsers) | ||
1859 | 1751 | adblock_app_add_browser_cb (app, browser, extension); | ||
1860 | 1752 | g_signal_connect (app, "add-browser", | ||
1861 | 1753 | G_CALLBACK (adblock_app_add_browser_cb), extension); | ||
1862 | 1754 | |||
1863 | 1755 | g_object_unref (browsers); | ||
1864 | 1756 | } | ||
1865 | 1757 | |||
1866 | 1758 | static void | ||
1867 | 1759 | test_adblock_parse (void) | ||
1868 | 1760 | { | ||
1869 | 1761 | adblock_init_db (); | ||
1870 | 1762 | g_assert (!adblock_parse_line (NULL)); | ||
1871 | 1763 | g_assert (!adblock_parse_line ("!")); | ||
1872 | 1764 | g_assert (!adblock_parse_line ("@@")); | ||
1873 | 1765 | g_assert (!adblock_parse_line ("##")); | ||
1874 | 1766 | g_assert (!adblock_parse_line ("[")); | ||
1875 | 1767 | |||
1876 | 1768 | g_assert_cmpstr (adblock_parse_line ("+advert/"), ==, "advert/"); | ||
1877 | 1769 | g_assert_cmpstr (adblock_parse_line ("*foo"), ==, "foo"); | ||
1878 | 1770 | g_assert_cmpstr (adblock_parse_line ("f*oo"), ==, "f.*oo"); | ||
1879 | 1771 | g_assert_cmpstr (adblock_parse_line ("?foo"), ==, "\\?foo"); | ||
1880 | 1772 | g_assert_cmpstr (adblock_parse_line ("foo?"), ==, "foo\\?"); | ||
1881 | 1773 | |||
1882 | 1774 | g_assert_cmpstr (adblock_parse_line (".*foo/bar"), ==, "..*foo/bar"); | ||
1883 | 1775 | g_assert_cmpstr (adblock_parse_line ("http://bla.blub/*"), ==, "http://bla.blub/"); | ||
1884 | 1776 | g_assert_cmpstr (adblock_parse_line ("bag?r[]=*cpa"), ==, "bag\\?r\\[\\]=.*cpa"); | ||
1885 | 1777 | g_assert_cmpstr (adblock_parse_line ("(facebookLike,"), ==, "\\(facebookLike,"); | ||
1886 | 1778 | adblock_destroy_db (); | ||
1887 | 1779 | } | ||
1888 | 1780 | |||
1889 | 1781 | static void | ||
1890 | 1782 | test_subscription_update (void) | ||
1891 | 1783 | { | ||
1892 | 1784 | gint temp; | ||
1893 | 1785 | gchar* filename; | ||
1894 | 1786 | |||
1895 | 1787 | adblock_init_db (); | ||
1896 | 1788 | temp = g_file_open_tmp ("midori_adblock_update_test_XXXXXX", &filename, NULL); | ||
1897 | 1789 | close (temp); | ||
1898 | 1790 | |||
1899 | 1791 | g_file_set_contents (filename, "", -1, NULL); | ||
1900 | 1792 | g_assert (!adblock_file_is_up_to_date (filename)); | ||
1901 | 1793 | |||
1902 | 1794 | g_file_set_contents (filename, | ||
1903 | 1795 | "[Adblock Plus 1.1]\n" | ||
1904 | 1796 | "! Checksum: 48f6Qdo4PsNogsurLvQ71w\n" | ||
1905 | 1797 | "! Title: EasyList\n" | ||
1906 | 1798 | "! Last modified: 05 Sep 2010 11:00 UTC\n" | ||
1907 | 1799 | "! This list expires after 48 hours\n", | ||
1908 | 1800 | -1, NULL); | ||
1909 | 1801 | g_assert (!adblock_file_is_up_to_date (filename)); | ||
1910 | 1802 | |||
1911 | 1803 | g_file_set_contents (filename, | ||
1912 | 1804 | "[Adblock Plus 1.1]\n" | ||
1913 | 1805 | "! Checksum: 48f6Qdo4PsNogsurLvQ71w\n" | ||
1914 | 1806 | "! Title: EasyList\n" | ||
1915 | 1807 | "! Last modified: 05.09.2010 11:00 UTC\n" | ||
1916 | 1808 | "! Expires: 2 days (update frequency)\n", | ||
1917 | 1809 | -1, NULL); | ||
1918 | 1810 | g_assert (!adblock_file_is_up_to_date (filename)); | ||
1919 | 1811 | |||
1920 | 1812 | g_file_set_contents (filename, | ||
1921 | 1813 | "[Adblock Plus 1.1]\n" | ||
1922 | 1814 | "! Checksum: 48f6Qdo4PsNogsurLvQ71w\n" | ||
1923 | 1815 | "! Title: EasyList\n" | ||
1924 | 1816 | "! Updated: 05 Nov 2014 11:00 UTC\n" | ||
1925 | 1817 | "! Expires: 5 days (update frequency)\n", | ||
1926 | 1818 | -1, NULL); | ||
1927 | 1819 | g_assert (adblock_file_is_up_to_date (filename)); | ||
1928 | 1820 | |||
1929 | 1821 | g_file_set_contents (filename, | ||
1930 | 1822 | "[Adblock]\n" | ||
1931 | 1823 | "! dutchblock v3\n" | ||
1932 | 1824 | "! This list expires after 14 days\n" | ||
1933 | 1825 | "|http://b*.mookie1.com/\n", | ||
1934 | 1826 | -1, NULL); | ||
1935 | 1827 | g_assert (adblock_file_is_up_to_date (filename)); | ||
1936 | 1828 | |||
1937 | 1829 | g_file_set_contents (filename, | ||
1938 | 1830 | "[Adblock Plus 2.0]\n" | ||
1939 | 1831 | "! Checksum: S4reE8XaYTtAFxe2RjgBPg\n" | ||
1940 | 1832 | "! Last modification time (GMT): 2012.11.05 13:33\n" | ||
1941 | 1833 | "! Expires: 5 days (update frequency)\n", | ||
1942 | 1834 | -1, NULL); | ||
1943 | 1835 | g_assert (!adblock_file_is_up_to_date (filename)); | ||
1944 | 1836 | |||
1945 | 1837 | g_file_set_contents (filename, | ||
1946 | 1838 | "[Adblock Plus 2.0]\n" | ||
1947 | 1839 | "! Checksum: S4reE8XaYTtAFxe2RjgBPg\n" | ||
1948 | 1840 | "! Last modification time (GMT): 2012.11.05 13:33\n", | ||
1949 | 1841 | -1, NULL); | ||
1950 | 1842 | g_assert (!adblock_file_is_up_to_date (filename)); | ||
1951 | 1843 | |||
1952 | 1844 | g_unlink (filename); | ||
1953 | 1845 | g_free (filename); | ||
1954 | 1846 | adblock_destroy_db (); | ||
1955 | 1847 | } | ||
1956 | 1848 | |||
1957 | 1849 | static void | ||
1958 | 1850 | test_adblock_pattern (void) | ||
1959 | 1851 | { | ||
1960 | 1852 | gint temp; | ||
1961 | 1853 | gchar* filename; | ||
1962 | 1854 | |||
1963 | 1855 | adblock_init_db (); | ||
1964 | 1856 | temp = g_file_open_tmp ("midori_adblock_match_test_XXXXXX", &filename, NULL); | ||
1965 | 1857 | |||
1966 | 1858 | /* TODO: Update some tests and add new ones. */ | ||
1967 | 1859 | g_file_set_contents (filename, | ||
1968 | 1860 | "*ads.foo.bar*\n" | ||
1969 | 1861 | "*ads.bogus.name*\n" | ||
1970 | 1862 | "||^http://ads.bla.blub/*\n" | ||
1971 | 1863 | "|http://ads.blub.boing/*$domain=xxx.com\n" | ||
1972 | 1864 | "engine.adct.ru/*?\n" | ||
1973 | 1865 | "/addyn|*|adtech;\n" | ||
1974 | 1866 | "doubleclick.net/pfadx/*.mtvi\n" | ||
1975 | 1867 | "objects.tremormedia.com/embed/xml/*.xml?r=\n" | ||
1976 | 1868 | "videostrip.com^*/admatcherclient.\n" | ||
1977 | 1869 | "test.dom/test?var\n" | ||
1978 | 1870 | "/adpage.\n" | ||
1979 | 1871 | "br.gcl.ru/cgi-bin/br/", | ||
1980 | 1872 | -1, NULL); | ||
1981 | 1873 | |||
1982 | 1874 | adblock_parse_file (filename); | ||
1983 | 1875 | |||
1984 | 1876 | g_test_timer_start (); | ||
1985 | 1877 | g_assert (adblock_is_matched ("http://www.engadget.com/_uac/adpage.html", "")); | ||
1986 | 1878 | g_assert (adblock_is_matched ("http://test.dom/test?var=1", "")); | ||
1987 | 1879 | g_assert (adblock_is_matched ("http://ads.foo.bar/teddy", "")); | ||
1988 | 1880 | g_assert (!adblock_is_matched ("http://ads.fuu.bar/teddy", "")); | ||
1989 | 1881 | g_assert (adblock_is_matched ("https://ads.bogus.name/blub", "")); | ||
1990 | 1882 | g_assert (adblock_is_matched ("http://ads.bla.blub/kitty", "")); | ||
1991 | 1883 | g_assert (adblock_is_matched ("http://ads.blub.boing/soda", "")); | ||
1992 | 1884 | g_assert (!adblock_is_matched ("http://ads.foo.boing/beer", "")); | ||
1993 | 1885 | g_assert (adblock_is_matched ("https://testsub.engine.adct.ru/test?id=1", "")); | ||
1994 | 1886 | if (USE_PATTERN_MATCHING) | ||
1995 | 1887 | g_assert (adblock_is_matched ("http://test.ltd/addyn/test/test?var=adtech;&var2=1", "")); | ||
1996 | 1888 | g_assert (adblock_is_matched ("http://add.doubleclick.net/pfadx/aaaa.mtvi", "")); | ||
1997 | 1889 | g_assert (!adblock_is_matched ("http://add.doubleclick.net/pfadx/aaaa.mtv", "")); | ||
1998 | 1890 | g_assert (adblock_is_matched ("http://objects.tremormedia.com/embed/xml/list.xml?r=", "")); | ||
1999 | 1891 | g_assert (!adblock_is_matched ("http://qq.videostrip.c/sub/admatcherclient.php", "")); | ||
2000 | 1892 | g_assert (adblock_is_matched ("http://qq.videostrip.com/sub/admatcherclient.php", "")); | ||
2001 | 1893 | g_assert (adblock_is_matched ("http://qq.videostrip.com/sub/admatcherclient.php", "")); | ||
2002 | 1894 | g_assert (adblock_is_matched ("http://br.gcl.ru/cgi-bin/br/test", "")); | ||
2003 | 1895 | g_assert (!adblock_is_matched ("https://bugs.webkit.org/buglist.cgi?query_format=advanced&short_desc_type=allwordssubstr&short_desc=&long_desc_type=substring&long_desc=&bug_file_loc_type=allwordssubstr&bug_file_loc=&keywords_type=allwords&keywords=&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&emailassigned_to1=1&emailtype1=substring&email1=&emailassigned_to2=1&emailreporter2=1&emailcc2=1&emailtype2=substring&email2=&bugidtype=include&bug_id=&votes=&chfieldfrom=&chfieldto=Now&chfieldvalue=&query_based_on=gtkport&field0-0-0=keywords&type0-0-0=anywordssubstr&value0-0-0=Gtk%20Cairo%20soup&field0-0-1=short_desc&type0-0-1=anywordssubstr&value0-0-1=Gtk%20Cairo%20soup%20autoconf%20automake%20autotool&field0-0-2=component&type0-0-2=equals&value0-0-2=WebKit%20Gtk", "")); | ||
2004 | 1896 | g_assert (!adblock_is_matched ("http://www.engadget.com/2009/09/24/google-hits-android-rom-modder-with-a-cease-and-desist-letter/", "")); | ||
2005 | 1897 | g_assert (!adblock_is_matched ("http://karibik-invest.com/es/bienes_raices/search.php?sqT=19&sqN=&sqMp=&sqL=0&qR=1&sqMb=&searchMode=1&action=B%FAsqueda", "")); | ||
2006 | 1898 | g_assert (!adblock_is_matched ("http://google.com", "")); | ||
2007 | 1899 | |||
2008 | 1900 | g_print ("Search took %f seconds\n", g_test_timer_elapsed ()); | ||
2009 | 1901 | |||
2010 | 1902 | close (temp); | ||
2011 | 1903 | g_unlink (filename); | ||
2012 | 1904 | adblock_destroy_db (); | ||
2013 | 1905 | } | ||
2014 | 1906 | |||
2015 | 1907 | void | ||
2016 | 1908 | extension_test (void) | ||
2017 | 1909 | { | ||
2018 | 1910 | g_test_add_func ("/extensions/adblock/parse", test_adblock_parse); | ||
2019 | 1911 | g_test_add_func ("/extensions/adblock/pattern", test_adblock_pattern); | ||
2020 | 1912 | g_test_add_func ("/extensions/adblock/update", test_subscription_update); | ||
2021 | 1913 | } | ||
2022 | 1914 | |||
2023 | 1915 | MidoriExtension* | ||
2024 | 1916 | extension_init (void) | ||
2025 | 1917 | { | ||
2026 | 1918 | MidoriExtension* extension = g_object_new (MIDORI_TYPE_EXTENSION, | ||
2027 | 1919 | "name", _("Advertisement blocker"), | ||
2028 | 1920 | "description", _("Block advertisements according to a filter list"), | ||
2029 | 1921 | "version", "0.6" MIDORI_VERSION_SUFFIX, | ||
2030 | 1922 | "authors", "Christian Dywan <christian@twotoasts.de>", | ||
2031 | 1923 | NULL); | ||
2032 | 1924 | midori_extension_install_string_list (extension, "filters", NULL, G_MAXSIZE); | ||
2033 | 1925 | |||
2034 | 1926 | g_signal_connect (extension, "activate", | ||
2035 | 1927 | G_CALLBACK (adblock_activate_cb), NULL); | ||
2036 | 1928 | |||
2037 | 1929 | return extension; | ||
2038 | 1930 | } | ||
2039 | 1931 | 0 | ||
2040 | === added file 'extensions/adblock/config.vala' | |||
2041 | --- extensions/adblock/config.vala 1970-01-01 00:00:00 +0000 | |||
2042 | +++ extensions/adblock/config.vala 2014-02-20 20:31:17 +0000 | |||
2043 | @@ -0,0 +1,107 @@ | |||
2044 | 1 | /* | ||
2045 | 2 | Copyright (C) 2014 Christian Dywan <christian@twotoasts.de> | ||
2046 | 3 | |||
2047 | 4 | This library is free software; you can redistribute it and/or | ||
2048 | 5 | modify it under the terms of the GNU Lesser General Public | ||
2049 | 6 | License as published by the Free Software Foundation; either | ||
2050 | 7 | version 2.1 of the License, or (at your option) any later version. | ||
2051 | 8 | |||
2052 | 9 | See the file COPYING for the full license text. | ||
2053 | 10 | */ | ||
2054 | 11 | |||
2055 | 12 | namespace Adblock { | ||
2056 | 13 | public class Config : GLib.Object { | ||
2057 | 14 | List<Subscription> subscriptions; | ||
2058 | 15 | string? path; | ||
2059 | 16 | KeyFile keyfile; | ||
2060 | 17 | Subscription? custom; | ||
2061 | 18 | |||
2062 | 19 | public Config (string? path) { | ||
2063 | 20 | subscriptions = new GLib.List<Subscription> (); | ||
2064 | 21 | |||
2065 | 22 | this.path = path; | ||
2066 | 23 | if (path == null) | ||
2067 | 24 | return; | ||
2068 | 25 | |||
2069 | 26 | string custom_list = GLib.Path.build_filename (path, "custom.list"); | ||
2070 | 27 | try { | ||
2071 | 28 | custom = new Subscription (Filename.to_uri (custom_list, null)); | ||
2072 | 29 | subscriptions.append (custom); | ||
2073 | 30 | } catch (Error error) { | ||
2074 | 31 | custom = null; | ||
2075 | 32 | warning ("Failed to add custom list %s: %s", custom_list, error.message); | ||
2076 | 33 | } | ||
2077 | 34 | |||
2078 | 35 | string filename = GLib.Path.build_filename (path, "config"); | ||
2079 | 36 | keyfile = new GLib.KeyFile (); | ||
2080 | 37 | try { | ||
2081 | 38 | keyfile.load_from_file (filename, GLib.KeyFileFlags.NONE); | ||
2082 | 39 | string[] filters = keyfile.get_string_list ("settings", "filters"); | ||
2083 | 40 | foreach (string filter in filters) { | ||
2084 | 41 | bool active = false; | ||
2085 | 42 | string uri = filter; | ||
2086 | 43 | if (filter.has_prefix ("http-")) | ||
2087 | 44 | uri = "http:" + filter.substring (6); | ||
2088 | 45 | else if (filter.has_prefix ("file-")) | ||
2089 | 46 | uri = "file:" + filter.substring (6); | ||
2090 | 47 | else if (filter.has_prefix ("https-")) | ||
2091 | 48 | uri = "https:" + filter.substring (7); | ||
2092 | 49 | else | ||
2093 | 50 | active = true; | ||
2094 | 51 | Subscription sub = new Subscription (uri); | ||
2095 | 52 | sub.active = active; | ||
2096 | 53 | sub.add_feature (new Updater ()); | ||
2097 | 54 | sub.notify["active"].connect (active_changed); | ||
2098 | 55 | subscriptions.append (sub); | ||
2099 | 56 | } | ||
2100 | 57 | } catch (FileError.NOENT exist_error) { | ||
2101 | 58 | /* It's no error if no config file exists */ | ||
2102 | 59 | } catch (GLib.Error settings_error) { | ||
2103 | 60 | warning ("Error reading settings from %s: %s\n", filename, settings_error.message); | ||
2104 | 61 | } | ||
2105 | 62 | |||
2106 | 63 | size = subscriptions.length (); | ||
2107 | 64 | } | ||
2108 | 65 | |||
2109 | 66 | void active_changed (Object subscription, ParamSpec pspec) { | ||
2110 | 67 | var filters = new StringBuilder (); | ||
2111 | 68 | foreach (var sub in subscriptions) { | ||
2112 | 69 | if (sub == custom) | ||
2113 | 70 | continue; | ||
2114 | 71 | if (sub.uri.has_prefix ("http:") && !sub.active) | ||
2115 | 72 | filters.append ("http-" + sub.uri.substring (4)); | ||
2116 | 73 | else if (sub.uri.has_prefix ("file:") && !sub.active) | ||
2117 | 74 | filters.append ("file-" + sub.uri.substring (4)); | ||
2118 | 75 | else if (sub.uri.has_prefix ("https:") && !sub.active) | ||
2119 | 76 | filters.append ("https-" + sub.uri.substring (5)); | ||
2120 | 77 | else | ||
2121 | 78 | filters.append (sub.uri); | ||
2122 | 79 | filters.append_c (';'); | ||
2123 | 80 | } | ||
2124 | 81 | |||
2125 | 82 | string[] list = (filters.str.slice (0, -1)).split (";"); | ||
2126 | 83 | keyfile.set_string_list ("settings", "filters", list); | ||
2127 | 84 | try { | ||
2128 | 85 | string filename = GLib.Path.build_filename (path, "config"); | ||
2129 | 86 | FileUtils.set_contents (filename, keyfile.to_data ()); | ||
2130 | 87 | } catch (Error error) { | ||
2131 | 88 | warning ("Failed to save settings: %s", error.message); | ||
2132 | 89 | } | ||
2133 | 90 | } | ||
2134 | 91 | |||
2135 | 92 | public void add_custom_rule (string rule) { | ||
2136 | 93 | try { | ||
2137 | 94 | var file = File.new_for_uri (custom.uri); | ||
2138 | 95 | file.append_to (FileCreateFlags.NONE).write (("%s\n".printf (rule)).data); | ||
2139 | 96 | } catch (Error error) { | ||
2140 | 97 | warning ("Failed to add custom rule: %s", error.message); | ||
2141 | 98 | } | ||
2142 | 99 | } | ||
2143 | 100 | |||
2144 | 101 | /* foreach support */ | ||
2145 | 102 | public new Subscription? get (uint index) { | ||
2146 | 103 | return subscriptions.nth_data (index); | ||
2147 | 104 | } | ||
2148 | 105 | public uint size { get; private set; } | ||
2149 | 106 | } | ||
2150 | 107 | } | ||
2151 | 0 | 108 | ||
2152 | === added file 'extensions/adblock/element.vala' | |||
2153 | --- extensions/adblock/element.vala 1970-01-01 00:00:00 +0000 | |||
2154 | +++ extensions/adblock/element.vala 2014-02-20 20:31:17 +0000 | |||
2155 | @@ -0,0 +1,36 @@ | |||
2156 | 1 | /* | ||
2157 | 2 | Copyright (C) 2014 Christian Dywan <christian@twotoasts.de> | ||
2158 | 3 | |||
2159 | 4 | This library is free software; you can redistribute it and/or | ||
2160 | 5 | modify it under the terms of the GNU Lesser General Public | ||
2161 | 6 | License as published by the Free Software Foundation; either | ||
2162 | 7 | version 2.1 of the License, or (at your option) any later version. | ||
2163 | 8 | |||
2164 | 9 | See the file COPYING for the full license text. | ||
2165 | 10 | */ | ||
2166 | 11 | |||
2167 | 12 | namespace Adblock { | ||
2168 | 13 | public class Element : Feature { | ||
2169 | 14 | public HashTable<string, string> blockcssprivate; | ||
2170 | 15 | bool debug_element; | ||
2171 | 16 | |||
2172 | 17 | public Element () { | ||
2173 | 18 | base (); | ||
2174 | 19 | debug_element = "adblock:element" in (Environment.get_variable ("MIDORI_DEBUG") ?? ""); | ||
2175 | 20 | } | ||
2176 | 21 | |||
2177 | 22 | public override void clear () { | ||
2178 | 23 | blockcssprivate = new HashTable<string, string> (str_hash, str_equal); | ||
2179 | 24 | } | ||
2180 | 25 | |||
2181 | 26 | public string? lookup (string domain) { | ||
2182 | 27 | return blockcssprivate.lookup (domain); | ||
2183 | 28 | } | ||
2184 | 29 | |||
2185 | 30 | public void insert (string domain, string value) { | ||
2186 | 31 | if (debug_element) | ||
2187 | 32 | stdout.printf ("Element to be blocked %s => %s\n", domain, value); | ||
2188 | 33 | blockcssprivate.insert (domain, value); | ||
2189 | 34 | } | ||
2190 | 35 | } | ||
2191 | 36 | } | ||
2192 | 0 | 37 | ||
2193 | === added file 'extensions/adblock/extension.vala' | |||
2194 | --- extensions/adblock/extension.vala 1970-01-01 00:00:00 +0000 | |||
2195 | +++ extensions/adblock/extension.vala 2014-02-20 20:31:17 +0000 | |||
2196 | @@ -0,0 +1,545 @@ | |||
2197 | 1 | /* | ||
2198 | 2 | Copyright (C) 2009-2014 Christian Dywan <christian@twotoasts.de> | ||
2199 | 3 | Copyright (C) 2009-2012 Alexander Butenko <a.butenka@gmail.com> | ||
2200 | 4 | |||
2201 | 5 | This library is free software; you can redistribute it and/or | ||
2202 | 6 | modify it under the terms of the GNU Lesser General Public | ||
2203 | 7 | License as published by the Free Software Foundation; either | ||
2204 | 8 | version 2.1 of the License, or (at your option) any later version. | ||
2205 | 9 | |||
2206 | 10 | See the file COPYING for the full license text. | ||
2207 | 11 | */ | ||
2208 | 12 | |||
2209 | 13 | namespace Adblock { | ||
2210 | 14 | public enum Directive { | ||
2211 | 15 | ALLOW, | ||
2212 | 16 | BLOCK | ||
2213 | 17 | } | ||
2214 | 18 | |||
2215 | 19 | public class Extension : Midori.Extension { | ||
2216 | 20 | Config config; | ||
2217 | 21 | HashTable<string, Directive?> cache; | ||
2218 | 22 | |||
2219 | 23 | #if HAVE_WEBKIT2 | ||
2220 | 24 | public Extension (WebKit.WebExtension web_extension) { | ||
2221 | 25 | init (); | ||
2222 | 26 | web_extension.page_created.connect (page_created); | ||
2223 | 27 | } | ||
2224 | 28 | |||
2225 | 29 | void page_created (WebKit.WebPage web_page) { | ||
2226 | 30 | web_page.send_request.connect (send_request); | ||
2227 | 31 | } | ||
2228 | 32 | |||
2229 | 33 | bool send_request (WebKit.WebPage web_page, WebKit.URIRequest request, WebKit.URIResponse? redirected_response) { | ||
2230 | 34 | return request_handled (web_page.uri, request.uri); | ||
2231 | 35 | } | ||
2232 | 36 | #else | ||
2233 | 37 | public Extension () { | ||
2234 | 38 | GLib.Object (name: _("Advertisement blocker"), | ||
2235 | 39 | description: _("Block advertisements according to a filter list"), | ||
2236 | 40 | version: "2.0", | ||
2237 | 41 | authors: "Christian Dywan <christian@twotoasts.de>"); | ||
2238 | 42 | activate.connect (extension_activated); | ||
2239 | 43 | open_preferences.connect (extension_preferences); | ||
2240 | 44 | } | ||
2241 | 45 | |||
2242 | 46 | void extension_preferences () { | ||
2243 | 47 | open_dialog (null); | ||
2244 | 48 | } | ||
2245 | 49 | |||
2246 | 50 | void open_dialog (string? uri) { | ||
2247 | 51 | var dialog = new Gtk.Dialog.with_buttons (_("Configure Advertisement filters"), | ||
2248 | 52 | null, | ||
2249 | 53 | #if !HAVE_GTK3 | ||
2250 | 54 | Gtk.DialogFlags.NO_SEPARATOR | | ||
2251 | 55 | #endif | ||
2252 | 56 | Gtk.DialogFlags.DESTROY_WITH_PARENT, | ||
2253 | 57 | Gtk.STOCK_HELP, Gtk.ResponseType.HELP, | ||
2254 | 58 | Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE); | ||
2255 | 59 | #if HAVE_GTK3 | ||
2256 | 60 | dialog.get_widget_for_response (Gtk.ResponseType.HELP).get_style_context ().add_class ("help_button"); | ||
2257 | 61 | #endif | ||
2258 | 62 | dialog.set_icon_name (Gtk.STOCK_PROPERTIES); | ||
2259 | 63 | dialog.set_response_sensitive (Gtk.ResponseType.HELP, false); | ||
2260 | 64 | |||
2261 | 65 | var hbox = new Gtk.HBox (false, 0); | ||
2262 | 66 | (dialog.get_content_area () as Gtk.Box).pack_start (hbox, true, true, 12); | ||
2263 | 67 | var vbox = new Gtk.VBox (false, 0); | ||
2264 | 68 | hbox.pack_start (vbox, true, true, 4); | ||
2265 | 69 | var button = new Gtk.Label (null); | ||
2266 | 70 | string description = """ | ||
2267 | 71 | Type the address of a preconfigured filter list in the text entry | ||
2268 | 72 | and click "Add" to add it to the list. | ||
2269 | 73 | You can find more lists at %s %s. | ||
2270 | 74 | """.printf ( | ||
2271 | 75 | "<a href=\"http://adblockplus.org/en/subscriptions\">adblockplus.org/en/subscriptions</a>", | ||
2272 | 76 | "<a href=\"http://easylist.adblockplus.org/\">easylist.adblockplus.org</a>"); | ||
2273 | 77 | button.activate_link.connect ((uri)=>{ | ||
2274 | 78 | var browser = Midori.Browser.get_for_widget (button); | ||
2275 | 79 | var view = browser.add_uri (uri); | ||
2276 | 80 | browser.tab = view; | ||
2277 | 81 | return true; | ||
2278 | 82 | }); | ||
2279 | 83 | button.set_markup (description); | ||
2280 | 84 | button.set_line_wrap (true); | ||
2281 | 85 | vbox.pack_start (button, false, false, 4); | ||
2282 | 86 | |||
2283 | 87 | var entry = new Gtk.Entry (); | ||
2284 | 88 | if (uri != null) | ||
2285 | 89 | entry.set_text (uri); | ||
2286 | 90 | vbox.pack_start (entry, false, false, 4); | ||
2287 | 91 | |||
2288 | 92 | var liststore = new Gtk.ListStore (1, typeof (Subscription)); | ||
2289 | 93 | var treeview = new Gtk.TreeView.with_model (liststore); | ||
2290 | 94 | treeview.set_headers_visible (false); | ||
2291 | 95 | var column = new Gtk.TreeViewColumn (); | ||
2292 | 96 | var renderer_toggle = new Gtk.CellRendererToggle (); | ||
2293 | 97 | column.pack_start (renderer_toggle, false); | ||
2294 | 98 | column.set_cell_data_func (renderer_toggle, (column, renderer, model, iter) => { | ||
2295 | 99 | Subscription sub; | ||
2296 | 100 | liststore.get (iter, 0, out sub); | ||
2297 | 101 | renderer.set ("active", sub.active, | ||
2298 | 102 | "sensitive", !sub.uri.has_suffix ("custom.list")); | ||
2299 | 103 | }); | ||
2300 | 104 | renderer_toggle.toggled.connect ((path) => { | ||
2301 | 105 | Gtk.TreeIter iter; | ||
2302 | 106 | if (liststore.get_iter_from_string (out iter, path)) { | ||
2303 | 107 | Subscription sub; | ||
2304 | 108 | liststore.get (iter, 0, out sub); | ||
2305 | 109 | sub.active = !sub.active; | ||
2306 | 110 | } | ||
2307 | 111 | }); | ||
2308 | 112 | treeview.append_column (column); | ||
2309 | 113 | |||
2310 | 114 | column = new Gtk.TreeViewColumn (); | ||
2311 | 115 | var renderer_text = new Gtk.CellRendererText (); | ||
2312 | 116 | column.pack_start (renderer_text, false); | ||
2313 | 117 | renderer_text.set ("editable", true); | ||
2314 | 118 | // TODO: renderer_text.edited.connect | ||
2315 | 119 | column.set_cell_data_func (renderer_text, (column, renderer, model, iter) => { | ||
2316 | 120 | Subscription sub; | ||
2317 | 121 | liststore.get (iter, 0, out sub); | ||
2318 | 122 | renderer.set ("text", sub.uri); | ||
2319 | 123 | }); | ||
2320 | 124 | treeview.append_column (column); | ||
2321 | 125 | |||
2322 | 126 | var scrolled = new Gtk.ScrolledWindow (null, null); | ||
2323 | 127 | scrolled.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC); | ||
2324 | 128 | scrolled.add (treeview); | ||
2325 | 129 | vbox.pack_start (scrolled); | ||
2326 | 130 | |||
2327 | 131 | foreach (Subscription sub in config) | ||
2328 | 132 | liststore.insert_with_values (null, 0, 0, sub); | ||
2329 | 133 | // TODO: row-inserted row-changed row-deleted | ||
2330 | 134 | // TODO vbox with add/ edit/ remove/ down/ up | ||
2331 | 135 | |||
2332 | 136 | dialog.get_content_area ().show_all (); | ||
2333 | 137 | |||
2334 | 138 | dialog.response.connect ((response)=>{ dialog.destroy (); }); | ||
2335 | 139 | dialog.show (); | ||
2336 | 140 | } | ||
2337 | 141 | |||
2338 | 142 | void extension_activated (Midori.App app) { | ||
2339 | 143 | init (); | ||
2340 | 144 | foreach (var browser in app.get_browsers ()) | ||
2341 | 145 | browser_added (browser); | ||
2342 | 146 | app.add_browser.connect (browser_added); | ||
2343 | 147 | } | ||
2344 | 148 | |||
2345 | 149 | void browser_added (Midori.Browser browser) { | ||
2346 | 150 | foreach (var tab in browser.get_tabs ()) | ||
2347 | 151 | tab_added (tab); | ||
2348 | 152 | browser.add_tab.connect (tab_added); | ||
2349 | 153 | } | ||
2350 | 154 | |||
2351 | 155 | void tab_added (Midori.View view) { | ||
2352 | 156 | view.web_view.resource_request_starting.connect (resource_requested); | ||
2353 | 157 | view.web_view.navigation_policy_decision_requested.connect (navigation_requested); | ||
2354 | 158 | view.notify["load-status"].connect ((pspec) => { | ||
2355 | 159 | if (view.load_status == Midori.LoadStatus.FINISHED) | ||
2356 | 160 | inject_css (view, view.uri); | ||
2357 | 161 | }); | ||
2358 | 162 | view.context_menu.connect (context_menu); | ||
2359 | 163 | } | ||
2360 | 164 | |||
2361 | 165 | void context_menu (WebKit.HitTestResult hit_test_result, Midori.ContextAction menu) { | ||
2362 | 166 | string label, uri; | ||
2363 | 167 | if ((hit_test_result.context & WebKit.HitTestResultContext.IMAGE) != 0) { | ||
2364 | 168 | label = _("Bl_ock image"); | ||
2365 | 169 | uri = hit_test_result.image_uri; | ||
2366 | 170 | } else if ((hit_test_result.context & WebKit.HitTestResultContext.LINK) != 0) { | ||
2367 | 171 | label = _("Bl_ock link"); | ||
2368 | 172 | uri = hit_test_result.link_uri; | ||
2369 | 173 | } else | ||
2370 | 174 | return; | ||
2371 | 175 | var action = new Gtk.Action ("BlockElement", label, null, null); | ||
2372 | 176 | action.activate.connect ((action) => { | ||
2373 | 177 | edit_rule_dialog (uri); | ||
2374 | 178 | }); | ||
2375 | 179 | menu.add (action); | ||
2376 | 180 | } | ||
2377 | 181 | |||
2378 | 182 | void edit_rule_dialog (string uri) { | ||
2379 | 183 | var dialog = new Gtk.Dialog.with_buttons (_("Edit rule"), | ||
2380 | 184 | null, | ||
2381 | 185 | #if !HAVE_GTK3 | ||
2382 | 186 | Gtk.DialogFlags.NO_SEPARATOR | | ||
2383 | 187 | #endif | ||
2384 | 188 | Gtk.DialogFlags.DESTROY_WITH_PARENT, | ||
2385 | 189 | Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, | ||
2386 | 190 | Gtk.STOCK_ADD, Gtk.ResponseType.ACCEPT); | ||
2387 | 191 | dialog.set_icon_name (Gtk.STOCK_ADD); | ||
2388 | 192 | dialog.resizable = false; | ||
2389 | 193 | |||
2390 | 194 | var hbox = new Gtk.HBox (false, 8); | ||
2391 | 195 | var sizegroup = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL); | ||
2392 | 196 | hbox.border_width = 5; | ||
2393 | 197 | var label = new Gtk.Label.with_mnemonic (_("_Rule:")); | ||
2394 | 198 | sizegroup.add_widget (label); | ||
2395 | 199 | hbox.pack_start (label, false, false, 0); | ||
2396 | 200 | (dialog.get_content_area () as Gtk.Box).pack_start (hbox, false, true, 0); | ||
2397 | 201 | |||
2398 | 202 | var entry = new Gtk.Entry (); | ||
2399 | 203 | sizegroup.add_widget (entry); | ||
2400 | 204 | entry.activates_default = true; | ||
2401 | 205 | entry.set_text (uri); | ||
2402 | 206 | hbox.pack_start (entry, true, true, 0); | ||
2403 | 207 | |||
2404 | 208 | dialog.get_content_area ().show_all (); | ||
2405 | 209 | |||
2406 | 210 | dialog.set_default_response (Gtk.ResponseType.ACCEPT); | ||
2407 | 211 | if (dialog.run () != Gtk.ResponseType.ACCEPT) | ||
2408 | 212 | return; | ||
2409 | 213 | |||
2410 | 214 | string new_rule = entry.get_text (); | ||
2411 | 215 | dialog.destroy (); | ||
2412 | 216 | config.add_custom_rule (new_rule); | ||
2413 | 217 | } | ||
2414 | 218 | |||
2415 | 219 | |||
2416 | 220 | void resource_requested (WebKit.WebView web_view, WebKit.WebFrame frame, | ||
2417 | 221 | WebKit.WebResource resource, WebKit.NetworkRequest request, WebKit.NetworkResponse? response) { | ||
2418 | 222 | |||
2419 | 223 | if (request_handled (web_view.uri, request.uri)) | ||
2420 | 224 | request.set_uri ("about:blank"); | ||
2421 | 225 | } | ||
2422 | 226 | |||
2423 | 227 | bool navigation_requested (WebKit.WebFrame frame, WebKit.NetworkRequest request, | ||
2424 | 228 | WebKit.WebNavigationAction action, WebKit.WebPolicyDecision decision) { | ||
2425 | 229 | |||
2426 | 230 | string uri = request.uri; | ||
2427 | 231 | if (uri.has_prefix ("abp:")) { | ||
2428 | 232 | uri = uri.replace ("abp://", "abp:"); | ||
2429 | 233 | if (uri.has_prefix ("abp:subscribe?location=")) { | ||
2430 | 234 | /* abp://subscripe?location=http://example.com&title=foo */ | ||
2431 | 235 | string[] parts = uri.substring (23, -1).split ("&", 2); | ||
2432 | 236 | decision.ignore (); | ||
2433 | 237 | open_dialog (parts[0]); | ||
2434 | 238 | return true; | ||
2435 | 239 | } | ||
2436 | 240 | } | ||
2437 | 241 | return false; | ||
2438 | 242 | } | ||
2439 | 243 | |||
2440 | 244 | void inject_css (Midori.View view, string page_uri) { | ||
2441 | 245 | /* Don't block ads on internal pages */ | ||
2442 | 246 | if (!Midori.URI.is_http (page_uri)) | ||
2443 | 247 | return; | ||
2444 | 248 | string domain = Midori.URI.parse_hostname (page_uri, null); | ||
2445 | 249 | string[] subdomains = domain.split ("."); | ||
2446 | 250 | if (subdomains == null) | ||
2447 | 251 | return; | ||
2448 | 252 | int cnt = subdomains.length - 1; | ||
2449 | 253 | var subdomain = new StringBuilder (subdomains[cnt]); | ||
2450 | 254 | subdomain.prepend_c ('.'); | ||
2451 | 255 | cnt--; | ||
2452 | 256 | var code = new StringBuilder (); | ||
2453 | 257 | bool debug_element = "adblock:element" in (Environment.get_variable ("MIDORI_DEBUG") ?? ""); | ||
2454 | 258 | string hider_css; | ||
2455 | 259 | |||
2456 | 260 | /* Hide elements that were blocked, otherwise we will get "broken image" icon */ | ||
2457 | 261 | cache.foreach ((key, val) => { | ||
2458 | 262 | if (val == Adblock.Directive.BLOCK) | ||
2459 | 263 | code.append ("img[src*=\"%s\"] , iframe[src*=\"%s\"] , ".printf (key, key)); | ||
2460 | 264 | }); | ||
2461 | 265 | if (debug_element) | ||
2462 | 266 | hider_css = " { background-color: red; border: 4px solid green; }"; | ||
2463 | 267 | else | ||
2464 | 268 | hider_css = " { visiblility: hidden; width: 0; height: 0; }"; | ||
2465 | 269 | |||
2466 | 270 | code.truncate (code.len -3); | ||
2467 | 271 | code.append (hider_css); | ||
2468 | 272 | if (debug_element) | ||
2469 | 273 | stdout.printf ("hider css: %s\n", code.str); | ||
2470 | 274 | view.inject_stylesheet (code.str); | ||
2471 | 275 | |||
2472 | 276 | code.erase (); | ||
2473 | 277 | int blockscnt = 0; | ||
2474 | 278 | while (cnt >= 0) { | ||
2475 | 279 | subdomain.prepend (subdomains[cnt]); | ||
2476 | 280 | string? style = null; | ||
2477 | 281 | foreach (Subscription sub in config) { | ||
2478 | 282 | foreach (var feature in sub) { | ||
2479 | 283 | if (feature is Adblock.Element) { | ||
2480 | 284 | style = (feature as Adblock.Element).lookup (subdomain.str); | ||
2481 | 285 | break; | ||
2482 | 286 | } | ||
2483 | 287 | } | ||
2484 | 288 | } | ||
2485 | 289 | if (style != null) { | ||
2486 | 290 | code.append (style); | ||
2487 | 291 | code.append_c (','); | ||
2488 | 292 | blockscnt++; | ||
2489 | 293 | } | ||
2490 | 294 | subdomain.prepend_c ('.'); | ||
2491 | 295 | cnt--; | ||
2492 | 296 | } | ||
2493 | 297 | if (blockscnt == 0) | ||
2494 | 298 | return; | ||
2495 | 299 | code.truncate (code.len - 1); | ||
2496 | 300 | |||
2497 | 301 | if (debug_element) | ||
2498 | 302 | hider_css = " { background-color: red !important; border: 4px solid green !important; }"; | ||
2499 | 303 | else | ||
2500 | 304 | hider_css = " { display: none !important }"; | ||
2501 | 305 | |||
2502 | 306 | code.append (hider_css); | ||
2503 | 307 | view.inject_stylesheet (code.str); | ||
2504 | 308 | if (debug_element) | ||
2505 | 309 | stdout.printf ("css: %s\n", code.str); | ||
2506 | 310 | } | ||
2507 | 311 | #endif | ||
2508 | 312 | |||
2509 | 313 | internal void init () { | ||
2510 | 314 | debug ("Adblock2"); | ||
2511 | 315 | |||
2512 | 316 | string config_dir = Midori.Paths.get_extension_config_dir ("libadblock.so"); | ||
2513 | 317 | config = new Config (config_dir); | ||
2514 | 318 | reload_rules (); | ||
2515 | 319 | } | ||
2516 | 320 | |||
2517 | 321 | void reload_rules () { | ||
2518 | 322 | cache = new HashTable<string, Directive?> (str_hash, str_equal); | ||
2519 | 323 | foreach (Subscription sub in config) { | ||
2520 | 324 | try { | ||
2521 | 325 | sub.parse (); | ||
2522 | 326 | } catch (GLib.Error error) { | ||
2523 | 327 | warning ("Error parsing %s: %s", sub.uri, error.message); | ||
2524 | 328 | } | ||
2525 | 329 | } | ||
2526 | 330 | } | ||
2527 | 331 | |||
2528 | 332 | bool request_handled (string page_uri, string request_uri) { | ||
2529 | 333 | /* Always allow the main page */ | ||
2530 | 334 | if (request_uri == page_uri) | ||
2531 | 335 | return false; | ||
2532 | 336 | |||
2533 | 337 | Directive? directive = cache.lookup (request_uri); | ||
2534 | 338 | if (directive == null) { | ||
2535 | 339 | foreach (Subscription sub in config) { | ||
2536 | 340 | directive = sub.get_directive (request_uri, page_uri); | ||
2537 | 341 | if (directive != null) | ||
2538 | 342 | break; | ||
2539 | 343 | } | ||
2540 | 344 | if (directive == null) | ||
2541 | 345 | directive = Directive.ALLOW; | ||
2542 | 346 | cache.insert (request_uri, directive); | ||
2543 | 347 | } | ||
2544 | 348 | return directive == Directive.BLOCK; | ||
2545 | 349 | } | ||
2546 | 350 | } | ||
2547 | 351 | |||
2548 | 352 | static void debug (string format, ...) { | ||
2549 | 353 | bool debug_match = "adblock:match" in (Environment.get_variable ("MIDORI_DEBUG") ?? ""); | ||
2550 | 354 | if (!debug_match) | ||
2551 | 355 | return; | ||
2552 | 356 | |||
2553 | 357 | var args = va_list (); | ||
2554 | 358 | stdout.vprintf (format + "\n", args); | ||
2555 | 359 | } | ||
2556 | 360 | |||
2557 | 361 | internal static string? fixup_regex (string prefix, string? src) { | ||
2558 | 362 | if (src == null) | ||
2559 | 363 | return null; | ||
2560 | 364 | |||
2561 | 365 | var fixed = new StringBuilder (); | ||
2562 | 366 | fixed.append(prefix); | ||
2563 | 367 | |||
2564 | 368 | uint i = 0; | ||
2565 | 369 | if (src[0] == '*') | ||
2566 | 370 | i++; | ||
2567 | 371 | uint l = src.length; | ||
2568 | 372 | while (i < l) { | ||
2569 | 373 | char c = src[i]; | ||
2570 | 374 | switch (c) { | ||
2571 | 375 | case '*': | ||
2572 | 376 | fixed.append (".*"); break; | ||
2573 | 377 | case '|': | ||
2574 | 378 | case '^': | ||
2575 | 379 | case '+': | ||
2576 | 380 | break; | ||
2577 | 381 | case '?': | ||
2578 | 382 | case '[': | ||
2579 | 383 | case ']': | ||
2580 | 384 | fixed.append_printf ("\\%c", c); break; | ||
2581 | 385 | default: | ||
2582 | 386 | fixed.append_c (c); break; | ||
2583 | 387 | } | ||
2584 | 388 | i++; | ||
2585 | 389 | } | ||
2586 | 390 | return fixed.str; | ||
2587 | 391 | } | ||
2588 | 392 | } | ||
2589 | 393 | |||
2590 | 394 | #if HAVE_WEBKIT2 | ||
2591 | 395 | Adblock.Extension? filter; | ||
2592 | 396 | public static void webkit_web_extension_initialize (WebKit.WebExtension web_extension) { | ||
2593 | 397 | filter = new Adblock.Extension (web_extension); | ||
2594 | 398 | } | ||
2595 | 399 | #else | ||
2596 | 400 | public Midori.Extension extension_init () { | ||
2597 | 401 | return new Adblock.Extension (); | ||
2598 | 402 | } | ||
2599 | 403 | #endif | ||
2600 | 404 | |||
2601 | 405 | #if !HAVE_WEBKIT2 | ||
2602 | 406 | struct TestCaseLine { | ||
2603 | 407 | public string line; | ||
2604 | 408 | public string fixed; | ||
2605 | 409 | } | ||
2606 | 410 | |||
2607 | 411 | const TestCaseLine[] lines = { | ||
2608 | 412 | { null, null }, | ||
2609 | 413 | { "!", "!" }, | ||
2610 | 414 | { "@@", "@@" }, | ||
2611 | 415 | { "##", "##" }, | ||
2612 | 416 | { "[", "\\[" }, | ||
2613 | 417 | { "+advert/", "advert/" }, | ||
2614 | 418 | { "*foo", "foo" }, | ||
2615 | 419 | { "f*oo", "f.*oo" }, | ||
2616 | 420 | { "?foo", "\\?foo" }, | ||
2617 | 421 | { "foo?", "foo\\?" }, | ||
2618 | 422 | { ".*foo/bar", "..*foo/bar" }, | ||
2619 | 423 | { "http://bla.blub/*", "http://bla.blub/.*" }, | ||
2620 | 424 | { "bag?r[]=*cpa", "bag\\?r\\[\\]=.*cpa" }, | ||
2621 | 425 | { "(facebookLike,", "(facebookLike," }, | ||
2622 | 426 | }; | ||
2623 | 427 | |||
2624 | 428 | void test_adblock_fixup_regexp () { | ||
2625 | 429 | foreach (var line in lines) { | ||
2626 | 430 | Katze.assert_str_equal (line.line, Adblock.fixup_regex ("", line.line), line.fixed); | ||
2627 | 431 | } | ||
2628 | 432 | } | ||
2629 | 433 | |||
2630 | 434 | struct TestCasePattern { | ||
2631 | 435 | public string uri; | ||
2632 | 436 | public Adblock.Directive directive; | ||
2633 | 437 | } | ||
2634 | 438 | |||
2635 | 439 | const TestCasePattern[] patterns = { | ||
2636 | 440 | { "http://www.engadget.com/_uac/adpage.html", Adblock.Directive.BLOCK }, | ||
2637 | 441 | { "http://test.dom/test?var=1", Adblock.Directive.BLOCK }, | ||
2638 | 442 | { "http://ads.foo.bar/teddy", Adblock.Directive.BLOCK }, | ||
2639 | 443 | { "http://ads.fuu.bar/teddy", Adblock.Directive.ALLOW }, | ||
2640 | 444 | { "https://ads.bogus.name/blub", Adblock.Directive.BLOCK }, | ||
2641 | 445 | // FIXME { "http://ads.bla.blub/kitty", Adblock.Directive.BLOCK }, | ||
2642 | 446 | // FIXME { "http://ads.blub.boing/soda", Adblock.Directive.BLOCK }, | ||
2643 | 447 | { "http://ads.foo.boing/beer", Adblock.Directive.ALLOW }, | ||
2644 | 448 | { "https://testsub.engine.adct.ru/test?id=1", Adblock.Directive.BLOCK }, | ||
2645 | 449 | { "http://test.ltd/addyn/test/test?var=adtech;&var2=1", Adblock.Directive.BLOCK }, | ||
2646 | 450 | { "http://add.doubleclick.net/pfadx/aaaa.mtvi", Adblock.Directive.BLOCK }, | ||
2647 | 451 | { "http://add.doubleclick.net/pfadx/aaaa.mtv", Adblock.Directive.ALLOW }, | ||
2648 | 452 | { "http://objects.tremormedia.com/embed/xml/list.xml?r=", Adblock.Directive.BLOCK }, | ||
2649 | 453 | { "http://qq.videostrip.c/sub/admatcherclient.php", Adblock.Directive.ALLOW }, | ||
2650 | 454 | { "http://qq.videostrip.com/sub/admatcherclient.php", Adblock.Directive.BLOCK }, | ||
2651 | 455 | { "http://qq.videostrip.com/sub/admatcherclient.php", Adblock.Directive.BLOCK }, | ||
2652 | 456 | { "http://br.gcl.ru/cgi-bin/br/test", Adblock.Directive.BLOCK }, | ||
2653 | 457 | { "https://bugs.webkit.org/buglist.cgi?query_format=advanced&short_desc_type=allwordssubstr&short_desc=&long_desc_type=substring&long_desc=&bug_file_loc_type=allwordssubstr&bug_file_loc=&keywords_type=allwords&keywords=&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&emailassigned_to1=1&emailtype1=substring&email1=&emailassigned_to2=1&emailreporter2=1&emailcc2=1&emailtype2=substring&email2=&bugidtype=include&bug_id=&votes=&chfieldfrom=&chfieldto=Now&chfieldvalue=&query_based_on=gtkport&field0-0-0=keywords&type0-0-0=anywordssubstr&value0-0-0=Gtk%20Cairo%20soup&field0-0-1=short_desc&type0-0-1=anywordssubstr&value0-0-1=Gtk%20Cairo%20soup%20autoconf%20automake%20autotool&field0-0-2=component&type0-0-2=equals&value0-0-2=WebKit%20Gtk", Adblock.Directive.ALLOW }, | ||
2654 | 458 | { "http://www.engadget.com/2009/09/24/google-hits-android-rom-modder-with-a-cease-and-desist-letter/", Adblock.Directive.ALLOW }, | ||
2655 | 459 | { "http://karibik-invest.com/es/bienes_raices/search.php?sqT=19&sqN=&sqMp=&sqL=0&qR=1&sqMb=&searchMode=1&action=B%FAsqueda", Adblock.Directive.ALLOW }, | ||
2656 | 460 | { "http://google.com", Adblock.Directive.ALLOW }, | ||
2657 | 461 | }; | ||
2658 | 462 | |||
2659 | 463 | string pretty_directive (Adblock.Directive? directive) { | ||
2660 | 464 | if (directive == null) | ||
2661 | 465 | return "none"; | ||
2662 | 466 | return directive.to_string (); | ||
2663 | 467 | } | ||
2664 | 468 | |||
2665 | 469 | void test_adblock_pattern () { | ||
2666 | 470 | string path = Midori.Paths.get_res_filename ("adblock.list"); | ||
2667 | 471 | string uri; | ||
2668 | 472 | try { | ||
2669 | 473 | uri = Filename.to_uri (path, null); | ||
2670 | 474 | } catch (Error error) { | ||
2671 | 475 | GLib.error (error.message); | ||
2672 | 476 | } | ||
2673 | 477 | var sub = new Adblock.Subscription (uri); | ||
2674 | 478 | try { | ||
2675 | 479 | sub.parse (); | ||
2676 | 480 | } catch (Error error) { | ||
2677 | 481 | GLib.error (error.message); | ||
2678 | 482 | } | ||
2679 | 483 | foreach (var pattern in patterns) { | ||
2680 | 484 | Adblock.Directive? directive = sub.get_directive (pattern.uri, ""); | ||
2681 | 485 | if (directive == null) | ||
2682 | 486 | directive = Adblock.Directive.ALLOW; | ||
2683 | 487 | if (directive != pattern.directive) { | ||
2684 | 488 | error ("%s expected for %s but got %s", | ||
2685 | 489 | pretty_directive (pattern.directive), pattern.uri, pretty_directive (directive)); | ||
2686 | 490 | } | ||
2687 | 491 | } | ||
2688 | 492 | } | ||
2689 | 493 | |||
2690 | 494 | struct TestUpdateExample { | ||
2691 | 495 | public string content; | ||
2692 | 496 | public bool result; | ||
2693 | 497 | } | ||
2694 | 498 | |||
2695 | 499 | const TestUpdateExample[] examples = { | ||
2696 | 500 | { "[Adblock Plus 1.1]\n! Last modified: 05 Sep 2010 11:00 UTC\n! This list expires after 48 hours\n", true }, | ||
2697 | 501 | { "[Adblock Plus 1.1]\n! Last modified: 05.09.2010 11:00 UTC\n! Expires: 2 days (update frequency)\n", true }, | ||
2698 | 502 | { "[Adblock Plus 1.1]\n! Updated: 05 Nov 2024 11:00 UTC\n! Expires: 5 days (update frequency)\n", false }, | ||
2699 | 503 | { "[Adblock]\n! dutchblock v3\n! This list expires after 14 days\n|http://b*.mookie1.com/\n", false }, | ||
2700 | 504 | { "[Adblock Plus 2.0]\n! Last modification time (GMT): 2012.11.05 13:33\n! Expires: 5 days (update frequency)\n", true }, | ||
2701 | 505 | { "[Adblock Plus 2.0]\n! Last modification time (GMT): 2012.11.05 13:33\n", true }, | ||
2702 | 506 | { "[Adblock]\n ! dummy, i dont have any dates\n", false }, | ||
2703 | 507 | }; | ||
2704 | 508 | |||
2705 | 509 | void test_subscription_update () { | ||
2706 | 510 | string uri; | ||
2707 | 511 | FileIOStream iostream; | ||
2708 | 512 | File file; | ||
2709 | 513 | try { | ||
2710 | 514 | file = File.new_tmp ("midori_adblock_update_test_XXXXXX", out iostream); | ||
2711 | 515 | uri = file.get_uri (); | ||
2712 | 516 | } catch (Error error) { | ||
2713 | 517 | GLib.error (error.message); | ||
2714 | 518 | } | ||
2715 | 519 | var sub = new Adblock.Subscription (uri); | ||
2716 | 520 | var updater = new Adblock.Updater (); | ||
2717 | 521 | sub.add_feature (updater); | ||
2718 | 522 | |||
2719 | 523 | foreach (var example in examples) { | ||
2720 | 524 | try { | ||
2721 | 525 | file.replace_contents (example.content.data, null, false, FileCreateFlags.NONE, null); | ||
2722 | 526 | updater.last_mod_meta = null; | ||
2723 | 527 | updater.expires_meta = null; | ||
2724 | 528 | sub.parse (); | ||
2725 | 529 | } catch (Error error) { | ||
2726 | 530 | GLib.error (error.message); | ||
2727 | 531 | } | ||
2728 | 532 | if (example.result == true) | ||
2729 | 533 | assert (updater.needs_updating()); | ||
2730 | 534 | else | ||
2731 | 535 | assert (!updater.needs_updating()); | ||
2732 | 536 | } | ||
2733 | 537 | } | ||
2734 | 538 | |||
2735 | 539 | public void extension_test () { | ||
2736 | 540 | Test.add_func ("/extensions/adblock2/parse", test_adblock_fixup_regexp); | ||
2737 | 541 | Test.add_func ("/extensions/adblock2/pattern", test_adblock_pattern); | ||
2738 | 542 | Test.add_func ("/extensions/adblock2/update", test_subscription_update); | ||
2739 | 543 | } | ||
2740 | 544 | #endif | ||
2741 | 545 | |||
2742 | 0 | 546 | ||
2743 | === added file 'extensions/adblock/filter.vala' | |||
2744 | --- extensions/adblock/filter.vala 1970-01-01 00:00:00 +0000 | |||
2745 | +++ extensions/adblock/filter.vala 2014-02-20 20:31:17 +0000 | |||
2746 | @@ -0,0 +1,52 @@ | |||
2747 | 1 | /* | ||
2748 | 2 | Copyright (C) 2009-2014 Christian Dywan <christian@twotoasts.de> | ||
2749 | 3 | Copyright (C) 2009-2012 Alexander Butenko <a.butenka@gmail.com> | ||
2750 | 4 | |||
2751 | 5 | This library is free software; you can redistribute it and/or | ||
2752 | 6 | modify it under the terms of the GNU Lesser General Public | ||
2753 | 7 | License as published by the Free Software Foundation; either | ||
2754 | 8 | version 2.1 of the License, or (at your option) any later version. | ||
2755 | 9 | |||
2756 | 10 | See the file COPYING for the full license text. | ||
2757 | 11 | */ | ||
2758 | 12 | |||
2759 | 13 | namespace Adblock { | ||
2760 | 14 | public abstract class Filter : Feature { | ||
2761 | 15 | Options optslist; | ||
2762 | 16 | protected HashTable<string, Regex?> rules; | ||
2763 | 17 | |||
2764 | 18 | public virtual void insert (string sig, Regex regex) { | ||
2765 | 19 | rules.insert (sig, regex); | ||
2766 | 20 | } | ||
2767 | 21 | |||
2768 | 22 | public virtual Regex? lookup (string sig) { | ||
2769 | 23 | return rules.lookup (sig); | ||
2770 | 24 | } | ||
2771 | 25 | |||
2772 | 26 | public virtual uint size () { | ||
2773 | 27 | return rules.size (); | ||
2774 | 28 | } | ||
2775 | 29 | |||
2776 | 30 | protected Filter (Options options) { | ||
2777 | 31 | optslist = options; | ||
2778 | 32 | clear (); | ||
2779 | 33 | } | ||
2780 | 34 | |||
2781 | 35 | public override void clear () { | ||
2782 | 36 | rules = new HashTable<string, Regex> (str_hash, str_equal); | ||
2783 | 37 | } | ||
2784 | 38 | |||
2785 | 39 | protected bool check_rule (Regex regex, string pattern, string request_uri, string page_uri) throws Error { | ||
2786 | 40 | if (!regex.match_full (request_uri)) | ||
2787 | 41 | return false; | ||
2788 | 42 | |||
2789 | 43 | var opts = optslist.lookup (pattern); | ||
2790 | 44 | if (opts != null && Regex.match_simple (",third-party", opts, | ||
2791 | 45 | RegexCompileFlags.CASELESS, RegexMatchFlags.NOTEMPTY)) | ||
2792 | 46 | if (page_uri != null && regex.match_full (page_uri)) | ||
2793 | 47 | return false; | ||
2794 | 48 | debug ("blocked by pattern regexp=%s -- %s", regex.get_pattern (), request_uri); | ||
2795 | 49 | return true; | ||
2796 | 50 | } | ||
2797 | 51 | } | ||
2798 | 52 | } | ||
2799 | 0 | 53 | ||
2800 | === added file 'extensions/adblock/keys.vala' | |||
2801 | --- extensions/adblock/keys.vala 1970-01-01 00:00:00 +0000 | |||
2802 | +++ extensions/adblock/keys.vala 2014-02-20 20:31:17 +0000 | |||
2803 | @@ -0,0 +1,47 @@ | |||
2804 | 1 | /* | ||
2805 | 2 | Copyright (C) 2009-2014 Christian Dywan <christian@twotoasts.de> | ||
2806 | 3 | Copyright (C) 2009-2012 Alexander Butenko <a.butenka@gmail.com> | ||
2807 | 4 | |||
2808 | 5 | This library is free software; you can redistribute it and/or | ||
2809 | 6 | modify it under the terms of the GNU Lesser General Public | ||
2810 | 7 | License as published by the Free Software Foundation; either | ||
2811 | 8 | version 2.1 of the License, or (at your option) any later version. | ||
2812 | 9 | |||
2813 | 10 | See the file COPYING for the full license text. | ||
2814 | 11 | */ | ||
2815 | 12 | |||
2816 | 13 | namespace Adblock { | ||
2817 | 14 | public class Keys : Filter { | ||
2818 | 15 | List<Regex> blacklist; | ||
2819 | 16 | |||
2820 | 17 | public Keys (Options options) { | ||
2821 | 18 | base (options); | ||
2822 | 19 | } | ||
2823 | 20 | |||
2824 | 21 | public override void clear () { | ||
2825 | 22 | base.clear (); | ||
2826 | 23 | blacklist = new List<Regex> (); | ||
2827 | 24 | } | ||
2828 | 25 | |||
2829 | 26 | public override Directive? match (string request_uri, string page_uri) throws Error { | ||
2830 | 27 | string? uri = fixup_regex ("", request_uri); | ||
2831 | 28 | if (uri == null) | ||
2832 | 29 | return null; | ||
2833 | 30 | |||
2834 | 31 | int signature_size = 8; | ||
2835 | 32 | int pos, l = uri.length; | ||
2836 | 33 | for (pos = l - signature_size; pos >= 0; pos--) { | ||
2837 | 34 | string signature = uri.offset (pos).ndup (signature_size); | ||
2838 | 35 | var regex = rules.lookup (signature); | ||
2839 | 36 | if (regex == null || blacklist.find (regex) != null) | ||
2840 | 37 | continue; | ||
2841 | 38 | |||
2842 | 39 | if (check_rule (regex, uri, request_uri, page_uri)) | ||
2843 | 40 | return Directive.BLOCK; | ||
2844 | 41 | blacklist.prepend (regex); | ||
2845 | 42 | } | ||
2846 | 43 | |||
2847 | 44 | return null; | ||
2848 | 45 | } | ||
2849 | 46 | } | ||
2850 | 47 | } | ||
2851 | 0 | 48 | ||
2852 | === added file 'extensions/adblock/options.vala' | |||
2853 | --- extensions/adblock/options.vala 1970-01-01 00:00:00 +0000 | |||
2854 | +++ extensions/adblock/options.vala 2014-02-20 20:31:17 +0000 | |||
2855 | @@ -0,0 +1,32 @@ | |||
2856 | 1 | /* | ||
2857 | 2 | Copyright (C) 2014 Christian Dywan <christian@twotoasts.de> | ||
2858 | 3 | |||
2859 | 4 | This library is free software; you can redistribute it and/or | ||
2860 | 5 | modify it under the terms of the GNU Lesser General Public | ||
2861 | 6 | License as published by the Free Software Foundation; either | ||
2862 | 7 | version 2.1 of the License, or (at your option) any later version. | ||
2863 | 8 | |||
2864 | 9 | See the file COPYING for the full license text. | ||
2865 | 10 | */ | ||
2866 | 11 | |||
2867 | 12 | namespace Adblock { | ||
2868 | 13 | public class Options : GLib.Object { | ||
2869 | 14 | HashTable<string, string?> optslist; | ||
2870 | 15 | |||
2871 | 16 | public Options () { | ||
2872 | 17 | clear (); | ||
2873 | 18 | } | ||
2874 | 19 | |||
2875 | 20 | public void insert (string sig, string? opts) { | ||
2876 | 21 | optslist.insert (sig, opts); | ||
2877 | 22 | } | ||
2878 | 23 | |||
2879 | 24 | public string? lookup (string sig) { | ||
2880 | 25 | return optslist.lookup (sig); | ||
2881 | 26 | } | ||
2882 | 27 | |||
2883 | 28 | public void clear () { | ||
2884 | 29 | optslist = new HashTable<string, string?> (str_hash, str_equal); | ||
2885 | 30 | } | ||
2886 | 31 | } | ||
2887 | 32 | } | ||
2888 | 0 | 33 | ||
2889 | === added file 'extensions/adblock/pattern.vala' | |||
2890 | --- extensions/adblock/pattern.vala 1970-01-01 00:00:00 +0000 | |||
2891 | +++ extensions/adblock/pattern.vala 2014-02-20 20:31:17 +0000 | |||
2892 | @@ -0,0 +1,26 @@ | |||
2893 | 1 | /* | ||
2894 | 2 | Copyright (C) 2009-2014 Christian Dywan <christian@twotoasts.de> | ||
2895 | 3 | Copyright (C) 2009-2012 Alexander Butenko <a.butenka@gmail.com> | ||
2896 | 4 | |||
2897 | 5 | This library is free software; you can redistribute it and/or | ||
2898 | 6 | modify it under the terms of the GNU Lesser General Public | ||
2899 | 7 | License as published by the Free Software Foundation; either | ||
2900 | 8 | version 2.1 of the License, or (at your option) any later version. | ||
2901 | 9 | |||
2902 | 10 | See the file COPYING for the full license text. | ||
2903 | 11 | */ | ||
2904 | 12 | |||
2905 | 13 | namespace Adblock { | ||
2906 | 14 | public class Pattern : Filter { | ||
2907 | 15 | public Pattern (Options options) { | ||
2908 | 16 | base (options); | ||
2909 | 17 | } | ||
2910 | 18 | |||
2911 | 19 | public override Directive? match (string request_uri, string page_uri) throws Error { | ||
2912 | 20 | foreach (var patt in rules.get_keys ()) | ||
2913 | 21 | if (check_rule (rules.lookup (patt), patt, request_uri, page_uri)) | ||
2914 | 22 | return Directive.BLOCK; | ||
2915 | 23 | return null; | ||
2916 | 24 | } | ||
2917 | 25 | } | ||
2918 | 26 | } | ||
2919 | 0 | 27 | ||
2920 | === added file 'extensions/adblock/subscriptions.vala' | |||
2921 | --- extensions/adblock/subscriptions.vala 1970-01-01 00:00:00 +0000 | |||
2922 | +++ extensions/adblock/subscriptions.vala 2014-02-20 20:31:17 +0000 | |||
2923 | @@ -0,0 +1,328 @@ | |||
2924 | 1 | /* | ||
2925 | 2 | Copyright (C) 2009-2014 Christian Dywan <christian@twotoasts.de> | ||
2926 | 3 | Copyright (C) 2009-2012 Alexander Butenko <a.butenka@gmail.com> | ||
2927 | 4 | |||
2928 | 5 | This library is free software; you can redistribute it and/or | ||
2929 | 6 | modify it under the terms of the GNU Lesser General Public | ||
2930 | 7 | License as published by the Free Software Foundation; either | ||
2931 | 8 | version 2.1 of the License, or (at your option) any later version. | ||
2932 | 9 | |||
2933 | 10 | See the file COPYING for the full license text. | ||
2934 | 11 | */ | ||
2935 | 12 | |||
2936 | 13 | namespace Adblock { | ||
2937 | 14 | public abstract class Feature : GLib.Object { | ||
2938 | 15 | public virtual bool header (string key, string value) { | ||
2939 | 16 | return false; | ||
2940 | 17 | } | ||
2941 | 18 | public virtual Directive? match (string request_uri, string page_uri) throws Error { | ||
2942 | 19 | return null; | ||
2943 | 20 | } | ||
2944 | 21 | public virtual void clear () { | ||
2945 | 22 | } | ||
2946 | 23 | } | ||
2947 | 24 | |||
2948 | 25 | public class Subscription : GLib.Object { | ||
2949 | 26 | public string? path; | ||
2950 | 27 | public string uri { get; set; default = null; } | ||
2951 | 28 | public bool active { get; set; default = true; } | ||
2952 | 29 | List<Feature> features; | ||
2953 | 30 | public Pattern pattern; | ||
2954 | 31 | public Keys keys; | ||
2955 | 32 | public Options optslist; | ||
2956 | 33 | public Whitelist whitelist; | ||
2957 | 34 | public Element element; | ||
2958 | 35 | WebKit.Download? download; | ||
2959 | 36 | |||
2960 | 37 | public Subscription (string uri) { | ||
2961 | 38 | this.uri = uri; | ||
2962 | 39 | |||
2963 | 40 | this.optslist = new Options (); | ||
2964 | 41 | this.whitelist = new Whitelist (optslist); | ||
2965 | 42 | add_feature (this.whitelist); | ||
2966 | 43 | this.keys = new Keys (optslist); | ||
2967 | 44 | add_feature (this.keys); | ||
2968 | 45 | this.pattern = new Pattern (optslist); | ||
2969 | 46 | add_feature (this.pattern); | ||
2970 | 47 | this.element = new Element (); | ||
2971 | 48 | add_feature (this.element); | ||
2972 | 49 | clear (); | ||
2973 | 50 | } | ||
2974 | 51 | |||
2975 | 52 | public void add_feature (Feature feature) { | ||
2976 | 53 | features.append (feature); | ||
2977 | 54 | size++; | ||
2978 | 55 | } | ||
2979 | 56 | |||
2980 | 57 | /* foreach support */ | ||
2981 | 58 | public new Feature? get (uint index) { | ||
2982 | 59 | return features.nth_data (index); | ||
2983 | 60 | } | ||
2984 | 61 | public uint size { get; private set; } | ||
2985 | 62 | |||
2986 | 63 | public void clear () { | ||
2987 | 64 | foreach (var feature in features) | ||
2988 | 65 | feature.clear (); | ||
2989 | 66 | optslist.clear (); | ||
2990 | 67 | } | ||
2991 | 68 | |||
2992 | 69 | internal void parse_line (string? line) throws Error { | ||
2993 | 70 | if (line.has_prefix ("@@")) { | ||
2994 | 71 | if (line.contains("$") && line.contains ("domain")) | ||
2995 | 72 | return; | ||
2996 | 73 | if (line.has_prefix ("@@||")) | ||
2997 | 74 | add_url_pattern ("^", "whitelist", line.offset (4)); | ||
2998 | 75 | else if (line.has_prefix ("@@|")) | ||
2999 | 76 | add_url_pattern ("^", "whitelist", line.offset (3)); | ||
3000 | 77 | else | ||
3001 | 78 | add_url_pattern ("", "whitelist", line.offset (2)); | ||
3002 | 79 | return; | ||
3003 | 80 | } | ||
3004 | 81 | /* TODO: [include] [exclude] */ | ||
3005 | 82 | if (line[0] == '[') | ||
3006 | 83 | return; | ||
3007 | 84 | |||
3008 | 85 | /* CSS block hider */ | ||
3009 | 86 | if (line.has_prefix ("##")) { | ||
3010 | 87 | /* TODO */ | ||
3011 | 88 | return; | ||
3012 | 89 | } | ||
3013 | 90 | if (line[0] == '#') | ||
3014 | 91 | return; | ||
3015 | 92 | |||
3016 | 93 | /* Per domain CSS hider rule */ | ||
3017 | 94 | if ("##" in line) { | ||
3018 | 95 | frame_add_private (line, "##"); | ||
3019 | 96 | return; | ||
3020 | 97 | } | ||
3021 | 98 | if ("#" in line) { | ||
3022 | 99 | frame_add_private (line, "#"); | ||
3023 | 100 | return; | ||
3024 | 101 | } | ||
3025 | 102 | |||
3026 | 103 | /* URL blocker rule */ | ||
3027 | 104 | if (line.has_prefix ("|")) { | ||
3028 | 105 | /* TODO: handle options and domains excludes */ | ||
3029 | 106 | if (line.contains("$")) | ||
3030 | 107 | return; | ||
3031 | 108 | |||
3032 | 109 | if (line.has_prefix ("||")) | ||
3033 | 110 | add_url_pattern ("", "fulluri", line.offset (2)); | ||
3034 | 111 | else | ||
3035 | 112 | add_url_pattern ("^", "fulluri", line.offset (1)); | ||
3036 | 113 | return /* add_url_pattern */; | ||
3037 | 114 | } | ||
3038 | 115 | |||
3039 | 116 | add_url_pattern ("", "uri", line); | ||
3040 | 117 | return /* add_url_pattern */; | ||
3041 | 118 | } | ||
3042 | 119 | |||
3043 | 120 | void frame_add_private (string line, string sep) { | ||
3044 | 121 | string[] data = line.split (sep, 2); | ||
3045 | 122 | if (!(data[1] != null && data[1] != "") | ||
3046 | 123 | || data[1].chr (-1, '\'') != null | ||
3047 | 124 | || (data[1].chr (-1, ':') != null | ||
3048 | 125 | && !Regex.match_simple (".*\\[.*:.*\\].*", data[1], | ||
3049 | 126 | RegexCompileFlags.CASELESS, RegexMatchFlags.NOTEMPTY))) { | ||
3050 | 127 | return; | ||
3051 | 128 | } | ||
3052 | 129 | |||
3053 | 130 | if (data[0].chr (-1, ',') != null) { | ||
3054 | 131 | string[] domains = data[0].split (",", -1); | ||
3055 | 132 | |||
3056 | 133 | foreach (string domain in domains) { | ||
3057 | 134 | /* Ignore Firefox-specific option */ | ||
3058 | 135 | if (domain == "~pregecko2") | ||
3059 | 136 | continue; | ||
3060 | 137 | /* FIXME: ~ should negate match */ | ||
3061 | 138 | if (domain[0] == '~') | ||
3062 | 139 | domain = domain.substring (1, -1); | ||
3063 | 140 | update_css_hash (domain.strip (), data[1]); | ||
3064 | 141 | } | ||
3065 | 142 | } | ||
3066 | 143 | else { | ||
3067 | 144 | update_css_hash (data[0], data[1]); | ||
3068 | 145 | } | ||
3069 | 146 | } | ||
3070 | 147 | |||
3071 | 148 | void update_css_hash (string domain, string value) { | ||
3072 | 149 | string? olddata = element.lookup (domain); | ||
3073 | 150 | if (olddata != null) { | ||
3074 | 151 | string newdata = olddata + " , " + value; | ||
3075 | 152 | element.insert (domain, newdata); | ||
3076 | 153 | } else { | ||
3077 | 154 | element.insert (domain, value); | ||
3078 | 155 | } | ||
3079 | 156 | } | ||
3080 | 157 | |||
3081 | 158 | void add_url_pattern (string prefix, string type, string line) throws Error { | ||
3082 | 159 | string[]? data = line.split ("$", 2); | ||
3083 | 160 | if (data == null || data[0] == null) | ||
3084 | 161 | return; | ||
3085 | 162 | |||
3086 | 163 | string patt, opts; | ||
3087 | 164 | patt = data[0]; | ||
3088 | 165 | opts = type; | ||
3089 | 166 | |||
3090 | 167 | if (data[1] != null) | ||
3091 | 168 | opts = type + "," + data[1]; | ||
3092 | 169 | |||
3093 | 170 | if (Regex.match_simple ("subdocument", opts, | ||
3094 | 171 | RegexCompileFlags.CASELESS, RegexMatchFlags.NOTEMPTY)) | ||
3095 | 172 | return; | ||
3096 | 173 | |||
3097 | 174 | string format_patt = fixup_regex (prefix, patt); | ||
3098 | 175 | debug ("got: %s opts %s", format_patt, opts); | ||
3099 | 176 | compile_regexp (format_patt, opts); | ||
3100 | 177 | /* return format_patt */ | ||
3101 | 178 | } | ||
3102 | 179 | |||
3103 | 180 | bool compile_regexp (string? patt, string opts) throws Error { | ||
3104 | 181 | if (patt == null) | ||
3105 | 182 | return false; | ||
3106 | 183 | try { | ||
3107 | 184 | var regex = new Regex (patt, RegexCompileFlags.OPTIMIZE, RegexMatchFlags.NOTEMPTY); | ||
3108 | 185 | /* is pattern is already a regex? */ | ||
3109 | 186 | if (Regex.match_simple ("^/.*[\\^\\$\\*].*/$", patt, | ||
3110 | 187 | RegexCompileFlags.UNGREEDY, RegexMatchFlags.NOTEMPTY) | ||
3111 | 188 | || opts != null && opts.contains ("whitelist")) { | ||
3112 | 189 | debug ("patt: %s", patt); | ||
3113 | 190 | if (opts.contains ("whitelist")) | ||
3114 | 191 | this.whitelist.insert (patt, regex); | ||
3115 | 192 | else | ||
3116 | 193 | this.pattern.insert (patt, regex); | ||
3117 | 194 | this.optslist.insert (patt, opts); | ||
3118 | 195 | return false; | ||
3119 | 196 | } else { /* nope, no regex */ | ||
3120 | 197 | int pos = 0, len; | ||
3121 | 198 | int signature_size = 8; | ||
3122 | 199 | string sig; | ||
3123 | 200 | len = patt.length; | ||
3124 | 201 | |||
3125 | 202 | /* chop up pattern into substrings for faster matching */ | ||
3126 | 203 | for (pos = len - signature_size; pos>=0; pos--) | ||
3127 | 204 | { | ||
3128 | 205 | sig = patt.offset (pos).ndup (signature_size); | ||
3129 | 206 | /* we don't have a * nor \\, does not look like regex, save chunk as "key" */ | ||
3130 | 207 | if (!Regex.match_simple ("[\\*]", sig, RegexCompileFlags.UNGREEDY, RegexMatchFlags.NOTEMPTY) && keys.lookup (sig) == null) { | ||
3131 | 208 | this.keys.insert (sig, regex); | ||
3132 | 209 | this.optslist.insert (sig, opts); | ||
3133 | 210 | } else { | ||
3134 | 211 | /* starts with * or \\ - save as regex */ | ||
3135 | 212 | if ((sig.has_prefix ("*") || sig.has_prefix("\\")) && this.pattern.lookup (sig) == null) { | ||
3136 | 213 | this.pattern.insert (sig, regex); | ||
3137 | 214 | this.optslist.insert (sig, opts); | ||
3138 | 215 | } | ||
3139 | 216 | } | ||
3140 | 217 | } | ||
3141 | 218 | } | ||
3142 | 219 | return false; | ||
3143 | 220 | } | ||
3144 | 221 | catch (Error error) { | ||
3145 | 222 | warning ("Adblock compile regexp: %s", error.message); | ||
3146 | 223 | return true; | ||
3147 | 224 | } | ||
3148 | 225 | } | ||
3149 | 226 | |||
3150 | 227 | public void parse_header (string header) throws Error { | ||
3151 | 228 | /* Headers come in two forms | ||
3152 | 229 | ! Foo: Bar | ||
3153 | 230 | ! Some freeform text | ||
3154 | 231 | */ | ||
3155 | 232 | string key = header; | ||
3156 | 233 | string value = ""; | ||
3157 | 234 | if (header.contains (":")) { | ||
3158 | 235 | string[] parts = header.split (":", 2); | ||
3159 | 236 | if (parts[0] != null) { | ||
3160 | 237 | key = parts[0].substring (2, -1); | ||
3161 | 238 | value = parts[1].substring (1, -1); | ||
3162 | 239 | } | ||
3163 | 240 | } | ||
3164 | 241 | debug ("Header '%s' says '%s'", key, value); | ||
3165 | 242 | foreach (var feature in features) { | ||
3166 | 243 | if (feature.header (key, value)) | ||
3167 | 244 | break; | ||
3168 | 245 | } | ||
3169 | 246 | } | ||
3170 | 247 | |||
3171 | 248 | void download_status (ParamSpec pspec) { | ||
3172 | 249 | if (download.get_status () != WebKit.DownloadStatus.FINISHED) | ||
3173 | 250 | return; | ||
3174 | 251 | |||
3175 | 252 | download = null; | ||
3176 | 253 | try { | ||
3177 | 254 | parse (); | ||
3178 | 255 | } catch (Error error) { | ||
3179 | 256 | warning ("Error parsing %s: %s", uri, error.message); | ||
3180 | 257 | } | ||
3181 | 258 | } | ||
3182 | 259 | |||
3183 | 260 | public void parse () throws Error | ||
3184 | 261 | { | ||
3185 | 262 | if (!active) | ||
3186 | 263 | return; | ||
3187 | 264 | |||
3188 | 265 | debug ("Parsing %s (%s)", uri, path); | ||
3189 | 266 | |||
3190 | 267 | clear (); | ||
3191 | 268 | |||
3192 | 269 | if (uri.has_prefix ("file://")) | ||
3193 | 270 | path = Filename.from_uri (uri); | ||
3194 | 271 | else { | ||
3195 | 272 | string cache_dir = GLib.Path.build_filename (GLib.Environment.get_home_dir (), ".cache", "midori", "adblock"); | ||
3196 | 273 | Midori.Paths.mkdir_with_parents (cache_dir); | ||
3197 | 274 | string filename = Checksum.compute_for_string (ChecksumType.MD5, this.uri, -1); | ||
3198 | 275 | path = GLib.Path.build_filename (cache_dir, filename); | ||
3199 | 276 | } | ||
3200 | 277 | |||
3201 | 278 | File filter_file = File.new_for_path (path); | ||
3202 | 279 | DataInputStream stream; | ||
3203 | 280 | try { | ||
3204 | 281 | stream = new DataInputStream (filter_file.read ()); | ||
3205 | 282 | } catch (IOError.NOT_FOUND exist_error) { | ||
3206 | 283 | #if HAVE_WEBKIT2 | ||
3207 | 284 | /* TODO */ | ||
3208 | 285 | #else | ||
3209 | 286 | if (download != null) | ||
3210 | 287 | return; | ||
3211 | 288 | |||
3212 | 289 | download = new WebKit.Download (new WebKit.NetworkRequest (uri)); | ||
3213 | 290 | download.destination_uri = Filename.to_uri (path, null); | ||
3214 | 291 | download.notify["status"].connect (download_status); | ||
3215 | 292 | debug ("Fetching %s to %s now", uri, download.destination_uri); | ||
3216 | 293 | download.start (); | ||
3217 | 294 | #endif | ||
3218 | 295 | return; | ||
3219 | 296 | } | ||
3220 | 297 | |||
3221 | 298 | string? line; | ||
3222 | 299 | while ((line = stream.read_line (null)) != null) { | ||
3223 | 300 | if (line == null) | ||
3224 | 301 | continue; | ||
3225 | 302 | string chomped = line.chomp (); | ||
3226 | 303 | if (chomped == "") | ||
3227 | 304 | continue; | ||
3228 | 305 | if (line[0] == '!') | ||
3229 | 306 | parse_header (chomped); | ||
3230 | 307 | else | ||
3231 | 308 | parse_line (chomped); | ||
3232 | 309 | } | ||
3233 | 310 | } | ||
3234 | 311 | |||
3235 | 312 | public Directive? get_directive (string request_uri, string page_uri) { | ||
3236 | 313 | try { | ||
3237 | 314 | foreach (var feature in features) { | ||
3238 | 315 | Directive? directive = feature.match (request_uri, page_uri); | ||
3239 | 316 | if (directive != null) { | ||
3240 | 317 | debug ("%s gave %s for %s (%s)\n", | ||
3241 | 318 | feature.get_type ().name (), directive.to_string (), request_uri, page_uri); | ||
3242 | 319 | return directive; | ||
3243 | 320 | } | ||
3244 | 321 | } | ||
3245 | 322 | } catch (Error error) { | ||
3246 | 323 | warning ("Adblock match error: %s\n", error.message); | ||
3247 | 324 | } | ||
3248 | 325 | return null; | ||
3249 | 326 | } | ||
3250 | 327 | } | ||
3251 | 328 | } | ||
3252 | 0 | 329 | ||
3253 | === added file 'extensions/adblock/updater.vala' | |||
3254 | --- extensions/adblock/updater.vala 1970-01-01 00:00:00 +0000 | |||
3255 | +++ extensions/adblock/updater.vala 2014-02-20 20:31:17 +0000 | |||
3256 | @@ -0,0 +1,139 @@ | |||
3257 | 1 | /* | ||
3258 | 2 | Copyright (C) 2014 Paweł Forysiuk <tuxator@o2.pl> | ||
3259 | 3 | Copyright (C) 2014 Christian Dywan <christian@twotoasts.de> | ||
3260 | 4 | |||
3261 | 5 | This library is free software; you can redistribute it and/or | ||
3262 | 6 | modify it under the terms of the GNU Lesser General Public | ||
3263 | 7 | License as published by the Free Software Foundation; either | ||
3264 | 8 | version 2.1 of the License, or (at your option) any later version. | ||
3265 | 9 | |||
3266 | 10 | See the file COPYING for the full license text. | ||
3267 | 11 | */ | ||
3268 | 12 | |||
3269 | 13 | namespace Adblock { | ||
3270 | 14 | public class Updater : Feature { | ||
3271 | 15 | public string expires_meta { get; set; default = null; } | ||
3272 | 16 | public string last_mod_meta { get; set; default = null; } | ||
3273 | 17 | public int64 update_tstamp { get; set; default = 0; } | ||
3274 | 18 | public int64 last_mod_tstamp { get; set; default = 0; } | ||
3275 | 19 | public int64 last_check_tstamp { get; set; default = 0; } | ||
3276 | 20 | |||
3277 | 21 | public Updater () { | ||
3278 | 22 | } | ||
3279 | 23 | |||
3280 | 24 | public override bool header (string key, string value) { | ||
3281 | 25 | if (key.has_prefix ("Last mod") || key == "Updated") { | ||
3282 | 26 | last_mod_meta = value; | ||
3283 | 27 | return true; | ||
3284 | 28 | } else if (key == "Expires") { | ||
3285 | 29 | /* ! Expires: 5 days (update frequency) */ | ||
3286 | 30 | expires_meta = value; | ||
3287 | 31 | return true; | ||
3288 | 32 | } else if (key.has_prefix ("! This list expires after")) { | ||
3289 | 33 | /* ! This list expires after 14 days */ | ||
3290 | 34 | expires_meta = key.substring (26, -1); | ||
3291 | 35 | return true; | ||
3292 | 36 | } | ||
3293 | 37 | return false; | ||
3294 | 38 | } | ||
3295 | 39 | |||
3296 | 40 | int get_month_from_string (string? month) { | ||
3297 | 41 | if (month == null) | ||
3298 | 42 | return 0; | ||
3299 | 43 | |||
3300 | 44 | string[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; | ||
3301 | 45 | for (int i = 0; i<= months.length; i++) | ||
3302 | 46 | { | ||
3303 | 47 | if (month.has_prefix (months[i])) | ||
3304 | 48 | return i+1; | ||
3305 | 49 | } | ||
3306 | 50 | return 0; | ||
3307 | 51 | } | ||
3308 | 52 | |||
3309 | 53 | public bool needs_updating () { | ||
3310 | 54 | DateTime now = new DateTime.now_local (); | ||
3311 | 55 | DateTime expire_date = null; | ||
3312 | 56 | DateTime last_mod_date = null; | ||
3313 | 57 | string? last_mod = last_mod_meta; | ||
3314 | 58 | string? expires = expires_meta; | ||
3315 | 59 | |||
3316 | 60 | /* We have "last modification" metadata */ | ||
3317 | 61 | if (last_mod != null) { | ||
3318 | 62 | int h = 0, min = 0, d, m, y; | ||
3319 | 63 | /* Date in a form of: 20.08.2012 12:34 */ | ||
3320 | 64 | if (last_mod.contains (".") || last_mod.contains("-")) { | ||
3321 | 65 | string[] parts = last_mod.split (" ", 2); | ||
3322 | 66 | string[] date_parts; | ||
3323 | 67 | string split_char = " "; | ||
3324 | 68 | |||
3325 | 69 | /* contains time part ? */ | ||
3326 | 70 | if (parts[1] != "" && parts[1].contains (":")) { | ||
3327 | 71 | string[] time_parts = parts[1].split (":", 2); | ||
3328 | 72 | h = int.parse(time_parts[0]); | ||
3329 | 73 | min = int.parse(time_parts[1]); | ||
3330 | 74 | } | ||
3331 | 75 | |||
3332 | 76 | /* check if dot or dash was used as a delimiter */ | ||
3333 | 77 | if (parts[0].contains (".")) | ||
3334 | 78 | split_char = "."; | ||
3335 | 79 | else if (parts[0].contains ("-")) | ||
3336 | 80 | split_char = "-"; | ||
3337 | 81 | |||
3338 | 82 | date_parts = parts[0].split (split_char, 3); | ||
3339 | 83 | m = int.parse(date_parts[1]); | ||
3340 | 84 | if (date_parts[2].length == 4) { | ||
3341 | 85 | y = int.parse(date_parts[2]); | ||
3342 | 86 | d = int.parse(date_parts[0]); | ||
3343 | 87 | } else { | ||
3344 | 88 | y = int.parse(date_parts[0]); | ||
3345 | 89 | d = int.parse(date_parts[2]); | ||
3346 | 90 | } | ||
3347 | 91 | } else { /* Date in a form of: 20 Mar 2012 12:34 */ | ||
3348 | 92 | string[] parts = last_mod.split (" ", 4); | ||
3349 | 93 | /* contains time part ? */ | ||
3350 | 94 | if (parts[3] != null && parts[3].contains (":")) { | ||
3351 | 95 | string[] time_parts = parts[3].split (":", 2); | ||
3352 | 96 | h = int.parse(time_parts[0]); | ||
3353 | 97 | min = int.parse(time_parts[1]); | ||
3354 | 98 | } | ||
3355 | 99 | |||
3356 | 100 | m = get_month_from_string (parts[1]); | ||
3357 | 101 | if (parts[2].length == 4) { | ||
3358 | 102 | y = int.parse(parts[2]); | ||
3359 | 103 | d = int.parse(parts[0]); | ||
3360 | 104 | } else { | ||
3361 | 105 | y = int.parse(parts[0]); | ||
3362 | 106 | d = int.parse(parts[2]); | ||
3363 | 107 | } | ||
3364 | 108 | } | ||
3365 | 109 | |||
3366 | 110 | last_mod_date = new DateTime.local (y, m, d, h, min, 0.0); | ||
3367 | 111 | } | ||
3368 | 112 | |||
3369 | 113 | if (last_mod_date == null) | ||
3370 | 114 | last_mod_date = now; | ||
3371 | 115 | |||
3372 | 116 | /* We have "expires" metadata */ | ||
3373 | 117 | if (expires != null) { | ||
3374 | 118 | if (expires.contains ("days")) { | ||
3375 | 119 | string[] parts = expires.split (" "); | ||
3376 | 120 | expire_date = last_mod_date.add_days (int.parse (parts[0])); | ||
3377 | 121 | } else if (expires.contains ("hours")) { | ||
3378 | 122 | string[] parts = expires.split (" "); | ||
3379 | 123 | expire_date = last_mod_date.add_hours (int.parse (parts[0])); | ||
3380 | 124 | } | ||
3381 | 125 | } else { | ||
3382 | 126 | /* No expire metadata found, assume x days */ | ||
3383 | 127 | int days_to_expire = 7; | ||
3384 | 128 | expire_date = last_mod_date.add_days (days_to_expire); | ||
3385 | 129 | } | ||
3386 | 130 | |||
3387 | 131 | last_mod_tstamp = last_mod_date.to_unix (); | ||
3388 | 132 | last_check_tstamp = now.to_unix (); | ||
3389 | 133 | update_tstamp = expire_date.to_unix (); | ||
3390 | 134 | |||
3391 | 135 | /* Check if we are past expire date */ | ||
3392 | 136 | return now.compare (expire_date) == 1; | ||
3393 | 137 | } | ||
3394 | 138 | } | ||
3395 | 139 | } | ||
3396 | 0 | 140 | ||
3397 | === added file 'extensions/adblock/whitelist.vala' | |||
3398 | --- extensions/adblock/whitelist.vala 1970-01-01 00:00:00 +0000 | |||
3399 | +++ extensions/adblock/whitelist.vala 2014-02-20 20:31:17 +0000 | |||
3400 | @@ -0,0 +1,30 @@ | |||
3401 | 1 | /* | ||
3402 | 2 | Copyright (C) 2014 Christian Dywan <christian@twotoasts.de> | ||
3403 | 3 | Copyright (C) 2014 Paweł Forysiuk <tuxator@o2.pl> | ||
3404 | 4 | |||
3405 | 5 | This library is free software; you can redistribute it and/or | ||
3406 | 6 | modify it under the terms of the GNU Lesser General Public | ||
3407 | 7 | License as published by the Free Software Foundation; either | ||
3408 | 8 | version 2.1 of the License, or (at your option) any later version. | ||
3409 | 9 | |||
3410 | 10 | See the file COPYING for the full license text. | ||
3411 | 11 | */ | ||
3412 | 12 | |||
3413 | 13 | namespace Adblock { | ||
3414 | 14 | public class Whitelist : Filter { | ||
3415 | 15 | public Whitelist (Options options) { | ||
3416 | 16 | base (options); | ||
3417 | 17 | } | ||
3418 | 18 | |||
3419 | 19 | public override Directive? match (string request_uri, string page_uri) throws Error { | ||
3420 | 20 | foreach (var white in rules.get_keys ()) { | ||
3421 | 21 | var regex = rules.lookup (white); | ||
3422 | 22 | if (!regex.match_full (request_uri)) | ||
3423 | 23 | return null; | ||
3424 | 24 | if (Regex.match_simple (regex.get_pattern (), request_uri, RegexCompileFlags.UNGREEDY, RegexMatchFlags.NOTEMPTY)) | ||
3425 | 25 | return Directive.ALLOW; | ||
3426 | 26 | } | ||
3427 | 27 | return null; | ||
3428 | 28 | } | ||
3429 | 29 | } | ||
3430 | 30 | } | ||
3431 | 0 | 31 | ||
3432 | === modified file 'katze/midori-paths.vala' | |||
3433 | --- katze/midori-paths.vala 2013-11-22 18:14:18 +0000 | |||
3434 | +++ katze/midori-paths.vala 2014-02-20 20:31:17 +0000 | |||
3435 | @@ -143,6 +143,7 @@ | |||
3436 | 143 | #if HAVE_WEBKIT2 | 143 | #if HAVE_WEBKIT2 |
3437 | 144 | WebKit.WebContext.get_default ().set_disk_cache_directory ( | 144 | WebKit.WebContext.get_default ().set_disk_cache_directory ( |
3438 | 145 | Path.build_path (Path.DIR_SEPARATOR_S, cache_dir, "web")); | 145 | Path.build_path (Path.DIR_SEPARATOR_S, cache_dir, "web")); |
3439 | 146 | |||
3440 | 146 | var cookie_manager = WebKit.WebContext.get_default ().get_cookie_manager (); | 147 | var cookie_manager = WebKit.WebContext.get_default ().get_cookie_manager (); |
3441 | 147 | cookie_manager.set_persistent_storage (Path.build_filename (config, "cookies.db"), | 148 | cookie_manager.set_persistent_storage (Path.build_filename (config, "cookies.db"), |
3442 | 148 | WebKit.CookiePersistentStorage.SQLITE); | 149 | WebKit.CookiePersistentStorage.SQLITE); |
3443 | 149 | 150 | ||
3444 | === modified file 'midori/midori-app.c' | |||
3445 | --- midori/midori-app.c 2014-01-12 18:14:15 +0000 | |||
3446 | +++ midori/midori-app.c 2014-02-20 20:31:17 +0000 | |||
3447 | @@ -1256,7 +1256,7 @@ | |||
3448 | 1256 | midori_debug (const gchar* token) | 1256 | midori_debug (const gchar* token) |
3449 | 1257 | { | 1257 | { |
3450 | 1258 | static const gchar* debug_token = NULL; | 1258 | static const gchar* debug_token = NULL; |
3452 | 1259 | const gchar* debug_tokens = "adblock:match adblock:time startup headers body referer cookies paths hsts unarmed bookmarks mouse app "; | 1259 | const gchar* debug_tokens = "adblock:match adblock:time adblock:element startup headers body referer cookies paths hsts unarmed bookmarks mouse app "; |
3453 | 1260 | if (debug_token == NULL) | 1260 | if (debug_token == NULL) |
3454 | 1261 | { | 1261 | { |
3455 | 1262 | gchar* found_token; | 1262 | gchar* found_token; |
3456 | 1263 | 1263 | ||
3457 | === modified file 'midori/midori.vapi' | |||
3458 | --- midori/midori.vapi 2014-01-26 11:33:02 +0000 | |||
3459 | +++ midori/midori.vapi 2014-02-20 20:31:17 +0000 | |||
3460 | @@ -135,6 +135,7 @@ | |||
3461 | 135 | public void install_boolean (string name, bool default_value); | 135 | public void install_boolean (string name, bool default_value); |
3462 | 136 | public void install_integer (string name, int default_value); | 136 | public void install_integer (string name, int default_value); |
3463 | 137 | public void install_string (string name, string default_value); | 137 | public void install_string (string name, string default_value); |
3464 | 138 | public void install_string_list (string name, string[]? default_value); | ||
3465 | 138 | 139 | ||
3466 | 139 | public bool get_boolean (string name); | 140 | public bool get_boolean (string name); |
3467 | 140 | public int get_integer (string name); | 141 | public int get_integer (string name); |
3468 | 141 | 142 | ||
3469 | === modified file 'midori/webkit2gtk-3.0.vapi' | |||
3470 | --- midori/webkit2gtk-3.0.vapi 2013-04-08 18:35:37 +0000 | |||
3471 | +++ midori/webkit2gtk-3.0.vapi 2014-02-20 20:31:17 +0000 | |||
3472 | @@ -552,6 +552,22 @@ | |||
3473 | 552 | [CCode (has_construct_function = false)] | 552 | [CCode (has_construct_function = false)] |
3474 | 553 | protected WebViewBase (); | 553 | protected WebViewBase (); |
3475 | 554 | } | 554 | } |
3476 | 555 | [CCode (cheader_filename = "webkit2/webkit-web-extension.h", type_id = "webkit_web_extension_get_type ()")] | ||
3477 | 556 | public class WebExtension : GLib.Object { | ||
3478 | 557 | [CCode (has_construct_function = false)] | ||
3479 | 558 | protected WebExtension (); | ||
3480 | 559 | public WebKit.WebPage get_page (uint64 page_id); | ||
3481 | 560 | public signal void page_created (WebKit.WebPage web_page); | ||
3482 | 561 | } | ||
3483 | 562 | [CCode (cheader_filename = "webkit2/webkit-web-extension.h", type_id = "webkit_web_extension_get_type ()")] | ||
3484 | 563 | public class WebPage : GLib.Object { | ||
3485 | 564 | [CCode (has_construct_function = false)] | ||
3486 | 565 | protected WebPage (); | ||
3487 | 566 | public uint64 get_id (); | ||
3488 | 567 | public string uri { get; } | ||
3489 | 568 | public signal bool send_request (WebKit.URIRequest request, WebKit.URIResponse? redirected_response); | ||
3490 | 569 | public signal void document_loaded (); | ||
3491 | 570 | } | ||
3492 | 555 | [CCode (cheader_filename = "webkit2/webkit2.h", type_id = "webkit_window_properties_get_type ()")] | 571 | [CCode (cheader_filename = "webkit2/webkit2.h", type_id = "webkit_window_properties_get_type ()")] |
3493 | 556 | public class WindowProperties : GLib.Object { | 572 | public class WindowProperties : GLib.Object { |
3494 | 557 | [CCode (has_construct_function = false)] | 573 | [CCode (has_construct_function = false)] |
3495 | 558 | 574 | ||
3496 | === modified file 'po/POTFILES.in' | |||
3497 | --- po/POTFILES.in 2013-12-13 17:34:40 +0000 | |||
3498 | +++ po/POTFILES.in 2014-02-20 20:31:17 +0000 | |||
3499 | @@ -36,7 +36,6 @@ | |||
3500 | 36 | katze/katze-preferences.c | 36 | katze/katze-preferences.c |
3501 | 37 | katze/midori-uri.vala | 37 | katze/midori-uri.vala |
3502 | 38 | katze/midori-paths.vala | 38 | katze/midori-paths.vala |
3503 | 39 | extensions/adblock.c | ||
3504 | 40 | extensions/addons.c | 39 | extensions/addons.c |
3505 | 41 | extensions/colorful-tabs.c | 40 | extensions/colorful-tabs.c |
3506 | 42 | extensions/cookie-manager/cookie-manager.c | 41 | extensions/cookie-manager/cookie-manager.c |
3507 | @@ -92,3 +91,13 @@ | |||
3508 | 92 | extensions/tabby.vala | 91 | extensions/tabby.vala |
3509 | 93 | extensions/flummi.vala | 92 | extensions/flummi.vala |
3510 | 94 | extensions/notes.vala | 93 | extensions/notes.vala |
3511 | 94 | extensions/adblock/extension.vala | ||
3512 | 95 | extensions/adblock/filter.vala | ||
3513 | 96 | extensions/adblock/keys.vala | ||
3514 | 97 | extensions/adblock/pattern.vala | ||
3515 | 98 | extensions/adblock/options.vala | ||
3516 | 99 | extensions/adblock/whitelist.vala | ||
3517 | 100 | extensions/adblock/subscriptions.vala | ||
3518 | 101 | extensions/adblock/config.vala | ||
3519 | 102 | extensions/adblock/updater.vala | ||
3520 | 103 | extensions/adblock/element.vala |