2
* Copyright © 2004 Adam Hooper
3
* Copyright © 2005, 2006 Jean-François Rameau
4
* Copyright © 2009 Xan Lopez <xan@gnome.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2, or (at your option)
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
#include "adblock-pattern.h"
29
#include "ephy-file-helpers.h"
30
#include "ephy-debug.h"
32
#define DEFAULT_BLACKLIST_FILENAME "adblock-patterns"
33
#define BLACKLIST_FILENAME "blacklist"
34
#define WHITELIST_FILENAME "whitelist"
42
void adblock_pattern_save (GSList *patterns, AdblockPatternType type);
45
adblock_pattern_load_from_file (GHashTable *patterns,
55
if (!g_file_get_contents (filename, &contents, NULL, NULL))
57
g_warning ("Could not read from file '%s'", filename);
61
t = lines = g_strsplit (contents, "\n", 0);
66
if (line == NULL) break;
68
if (*line == '#') continue; /* comment */
72
if (*line == '\0') continue; /* empty line */
74
regex = g_regex_new (line, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0, &error);
78
g_warning ("Could not compile expression \"%s\"\n"
80
line, error->message);
86
g_hash_table_insert (patterns, g_strdup (line), regex);
94
adblock_pattern_filename (AdblockPatternType type, OpType op)
96
char *filename = NULL;
100
case PATTERN_BLACKLIST:
101
filename = g_build_filename (ephy_dot_dir (), "extensions", "data",
102
"adblock", BLACKLIST_FILENAME,
105
case PATTERN_WHITELIST:
106
filename = g_build_filename (ephy_dot_dir (), "extensions", "data",
107
"adblock", WHITELIST_FILENAME,
110
case PATTERN_DEFAULT_BLACKLIST:
111
/* We first try the user's one */
112
filename = g_build_filename (ephy_dot_dir (), "extensions", "data",
113
"adblock", DEFAULT_BLACKLIST_FILENAME,
115
if (op == OP_LOAD && !g_file_test (filename, G_FILE_TEST_IS_REGULAR))
118
/* Then, try the global one */
119
filename = g_build_filename (SHARE_DIR,
120
DEFAULT_BLACKLIST_FILENAME,
130
adblock_pattern_foreach_save (const char *pattern,
136
status = g_io_channel_write_chars (channel, pattern, -1, &bytes_written, NULL);
137
status = g_io_channel_write_chars (channel, "\n", -1, &bytes_written, NULL);
143
adblock_pattern_replace_dot (const char *line)
148
g_return_val_if_fail (line != NULL, NULL);
150
sub = g_strsplit (line, ".", 0);
151
res = g_strjoinv ("\\.", sub);
159
adblock_pattern_rewrite_patterns (const char *contents)
163
GRegex *regex1, *regex2;
164
GError *error = NULL;
166
GSList *patterns = NULL;
168
/* We don't care about some specific rules */
169
regex1 = g_regex_new ("^\\[Adblock\\]", 0, 0, &error);
172
g_warning ("Could not compile expression ^\\[Adblock]\n" "Error: %s",
174
g_error_free (error);
177
regex2 = g_regex_new ("^\\!Filterset", 0, 0, &error);
180
g_warning ("Could not compile expression ^\\!Filterset\n" "Error: %s",
182
g_error_free (error);
186
t = lines = g_strsplit (contents, "\n", 0);
191
if (line == NULL) break;
193
if (*line == '#') continue; /* comment */
197
if (*line == '\0') continue; /* empty line */
199
match = g_regex_match (regex1, line, 0, NULL);
203
match = g_regex_match (regex2, line, 0, NULL);
209
*(line + strlen (line) -1) = '\0';
211
patterns = g_slist_prepend (patterns, g_strdup (line));
214
line = adblock_pattern_replace_dot (line);
216
patterns = g_slist_prepend (patterns, g_strdup (line));
220
g_regex_unref (regex1);
221
g_regex_unref (regex2);
223
adblock_pattern_save (patterns, PATTERN_DEFAULT_BLACKLIST);
225
g_slist_foreach (patterns, (GFunc)g_free, NULL);
229
adblock_pattern_get_filterg_patterns (const char *date)
232
char *contents = NULL, *url = NULL;
235
GFileInputStream *input_stream;
238
url = g_strdup_printf ("http://www.pierceive.com/filtersetg/%s", date);
239
file = g_file_new_for_uri (url);
240
info = g_file_query_info (file,
241
G_FILE_ATTRIBUTE_STANDARD_SIZE,
245
g_warning ("Could not get rules file from filterg site");
250
size = (gsize) g_file_info_get_size (info);
251
g_object_unref (info);
254
/* First, get the changelog so we can build the url pointing to the last rules */
255
input_stream = g_file_read (file, NULL, NULL);
256
if (input_stream != NULL)
260
contents = g_malloc (size);
261
res = g_input_stream_read_all (G_INPUT_STREAM (input_stream),
268
g_warning ("Could not get rules file from filterg site");
270
g_object_unref (input_stream);
274
g_warning ("Could not get rules file from filterg site");
279
g_object_unref (file);
285
adblock_pattern_get_filterg_date (void)
288
char *contents = NULL;
293
GFileInputStream *input_stream;
296
file = g_file_new_for_uri ("http://www.pierceive.com/filtersetg/latest.txt");
297
info = g_file_query_info (file,
298
G_FILE_ATTRIBUTE_STANDARD_SIZE,
302
g_warning ("Could not get latest.txt file from filterg site");
307
size = (gsize) g_file_info_get_size (info);
308
g_object_unref (info);
311
/* First, get the changelog so we can build the url pointing to the last rules */
312
input_stream = g_file_read (file, NULL, NULL);
313
if (input_stream != NULL)
317
contents = g_malloc (size);
318
res = g_input_stream_read_all (G_INPUT_STREAM (input_stream),
325
g_warning ("Could not get latest.txt file from filterg site");
328
g_object_unref (input_stream);
329
lines = g_strsplit (contents, "\n", 0);
330
date = g_strdup (lines [0]);
337
g_warning ("Could not get latest.txt file from filterg site");
342
g_object_unref (file);
350
adblock_pattern_load (GHashTable *patterns,
351
AdblockPatternType type)
353
char *filename = NULL;
355
filename = adblock_pattern_filename (type, OP_LOAD);
357
if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
359
LOG ("Loading patterns from %s", filename);
361
adblock_pattern_load_from_file (patterns, filename);
368
adblock_pattern_save (GSList *patterns, AdblockPatternType type)
370
GError *error = NULL;
371
char *filename = NULL;
373
filename = adblock_pattern_filename (type, OP_SAVE);
375
GIOChannel *channel = g_io_channel_new_file (filename, "w", NULL);
377
g_slist_foreach (patterns,
378
(GFunc)adblock_pattern_foreach_save,
381
g_io_channel_shutdown (channel, TRUE, &error);
385
adblock_pattern_get_filtersetg_patterns (void)
387
char *date, *patterns;
389
date = adblock_pattern_get_filterg_date ();
392
g_warning ("Could not get the last update");
396
patterns = adblock_pattern_get_filterg_patterns (date);
397
if (patterns == NULL)
399
g_warning ("Could not get content from last update");
403
adblock_pattern_rewrite_patterns (patterns);