2 * Copyright (c) 2010, 2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2011 Stevan Andjelkovic <stevan@student.chalmers.se>
4 * Copyright (c) 2010, 2011 Edd Barrett <vext01@gmail.com>
5 * Copyright (c) 2011 Todd T. Fries <todd@fries.net>
6 * Copyright (c) 2011 Raphael Graf <r@undefined.ch>
7 * Copyright (c) 2011 Michal Mazurek <akfaew@jasminek.net>
8 * Copyright (c) 2012 Josh Rickmar <jrick@devio.us>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 find_domain(const gchar
*s
, int flags
)
34 uri
= soup_uri_new(s
);
36 if (uri
== NULL
|| !SOUP_URI_VALID_FOR_HTTP(uri
))
39 if (flags
& XT_WL_TOPLEVEL
&&
40 !isdigit(uri
->host
[strlen(uri
->host
) - 1]))
41 p
= tld_get_suffix(uri
->host
);
45 if (flags
& XT_WL_TOPLEVEL
)
46 ret
= g_strdup_printf(".%s", p
);
47 else /* assume FQDN */
56 wl_find(const gchar
*s
, struct wl_list
*wl
)
60 if (s
== NULL
|| strlen(s
) == 0 || wl
== NULL
)
63 TAILQ_FOREACH(w
, wl
, entry
) {
66 if (!regexec(w
->re
, s
, 0, 0, 0))
74 wl_save(struct tab
*t
, struct karg
*args
, int list
)
76 char file
[PATH_MAX
], *lst_str
= NULL
;
78 char *line
= NULL
, *lt
= NULL
, *dom
;
86 if (t
== NULL
|| args
== NULL
)
89 if (runtime_settings
[0] == '\0')
93 case XT_WL_JAVASCRIPT
:
94 lst_str
= "JavaScript";
106 show_oops(t
, "Invalid list id: %d", list
);
111 dom
= find_domain(uri
, args
->i
);
112 if (uri
== NULL
|| dom
== NULL
||
113 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
114 show_oops(t
, "Can't add domain to %s white list", lst_str
);
119 case XT_WL_JAVASCRIPT
:
120 lt
= g_strdup_printf("js_wl=%s", dom
);
123 lt
= g_strdup_printf("cookie_wl=%s", dom
);
126 lt
= g_strdup_printf("pl_wl=%s", dom
);
129 lt
= g_strdup_printf("force_https=%s", dom
);
133 show_oops(t
, "Invalid list id: %d", list
);
137 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, runtime_settings
);
138 if ((f
= fopen(file
, "r+")) == NULL
) {
139 show_oops(t
, "can't open file %s");
144 line
= fparseln(f
, &linelen
, NULL
, NULL
, 0);
147 if (!strcmp(line
, lt
))
153 fprintf(f
, "%s\n", lt
);
158 case XT_WL_JAVASCRIPT
:
159 w
= wl_find(dom
, &js_wl
);
161 settings_add("js_wl", dom
);
162 w
= wl_find(dom
, &js_wl
);
168 w
= wl_find(dom
, &c_wl
);
170 settings_add("cookie_wl", dom
);
171 w
= wl_find(dom
, &c_wl
);
175 /* find and add to persistent jar */
176 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
177 for (;cf
; cf
= cf
->next
) {
179 if (!strcmp(dom
, ci
->domain
) ||
180 !strcmp(&dom
[1], ci
->domain
)) /* deal with leading . */ {
181 c
= soup_cookie_copy(ci
);
182 _soup_cookie_jar_add_cookie(p_cookiejar
, c
);
185 soup_cookies_free(cf
);
189 w
= wl_find(dom
, &pl_wl
);
191 settings_add("pl_wl", dom
);
192 w
= wl_find(dom
, &pl_wl
);
197 w
= wl_find(dom
, &force_https
);
199 settings_add("force_https", dom
);
200 w
= wl_find(dom
, &force_https
);
202 toggle_force_https(t
, &a
);
205 abort(); /* can't happen */
224 wl_show(struct tab
*t
, struct karg
*args
, char *title
, struct wl_list
*wl
)
232 if (args
->i
& XT_WL_PERSISTENT
) {
234 body
= g_strdup_printf("%s<h2>Persistent</h2>", body
);
236 TAILQ_FOREACH(w
, wl
, entry
) {
240 body
= g_strdup_printf("%s%s<br/>", body
, w
->pat
);
246 if (args
->i
& XT_WL_SESSION
) {
248 body
= g_strdup_printf("%s<h2>Session</h2>", body
);
250 TAILQ_FOREACH(w
, wl
, entry
) {
254 body
= g_strdup_printf("%s%s<br/>", body
, w
->pat
);
259 tmp
= get_html_page(title
, body
, "", 0);
262 load_webkit_string(t
, tmp
, XT_URI_ABOUT_JSWL
, 0);
263 else if (wl
== &c_wl
)
264 load_webkit_string(t
, tmp
, XT_URI_ABOUT_COOKIEWL
, 0);
265 else if (wl
== &pl_wl
)
266 load_webkit_string(t
, tmp
, XT_URI_ABOUT_PLUGINWL
, 0);
267 else if (wl
== &force_https
)
268 load_webkit_string(t
, tmp
, XT_URI_ABOUT_HTTPS
, 0);
274 wl_add(const char *str
, struct wl_list
*wl
, int flags
)
277 int add_dot
= 0, chopped
= 0;
279 char *escstr
, *p
, *pat
;
282 if (str
== NULL
|| wl
== NULL
|| strlen(str
) < 2)
285 DNPRINTF(XT_D_COOKIE
, "wl_add in: %s\n", str
);
287 /* slice off port number */
288 p
= g_strrstr(str
, ":");
292 w
= g_malloc(sizeof *w
);
293 w
->re
= g_malloc(sizeof *w
->re
);
294 if (flags
& XT_WL_REGEX
) {
295 w
->pat
= g_strdup_printf("re:%s", str
);
296 regcomp(w
->re
, str
, REG_EXTENDED
| REG_NOSUB
);
297 DNPRINTF(XT_D_COOKIE
, "wl_add: %s\n", str
);
299 /* treat *.moo.com the same as .moo.com */
300 if (s
[0] == '*' && s
[1] == '.')
302 else if (s
[0] != '.' && (flags
& XT_WL_TOPLEVEL
))
309 sv
= g_strsplit(s
, ".", 0);
310 escstr
= g_strjoinv("\\.", sv
);
314 w
->pat
= g_strdup_printf(".%s", str
);
315 pat
= g_strdup_printf("^(.*\\.)*%s$", escstr
);
316 regcomp(w
->re
, pat
, REG_EXTENDED
| REG_NOSUB
);
318 w
->pat
= g_strdup(str
);
320 pat
= g_strdup_printf("^(.*\\.)*%s$", escstr
);
322 pat
= g_strdup_printf("^%s$", escstr
);
323 regcomp(w
->re
, pat
, REG_EXTENDED
| REG_NOSUB
);
325 DNPRINTF(XT_D_COOKIE
, "wl_add: %s\n", pat
);
330 w
->handy
= (flags
& XT_WL_PERSISTENT
) ? 1 : 0;
332 TAILQ_INSERT_HEAD(wl
, w
, entry
);
338 add_cookie_wl(struct settings
*s
, char *entry
)
340 if (g_str_has_prefix(entry
, "re:")) {
342 wl_add(entry
, &c_wl
, XT_WL_PERSISTENT
| XT_WL_REGEX
);
344 wl_add(entry
, &c_wl
, XT_WL_PERSISTENT
);
349 add_js_wl(struct settings
*s
, char *entry
)
351 if (g_str_has_prefix(entry
, "re:")) {
353 wl_add(entry
, &js_wl
, XT_WL_PERSISTENT
| XT_WL_REGEX
);
355 wl_add(entry
, &js_wl
, XT_WL_PERSISTENT
);
360 add_pl_wl(struct settings
*s
, char *entry
)
362 if (g_str_has_prefix(entry
, "re:")) {
364 wl_add(entry
, &pl_wl
, XT_WL_PERSISTENT
| XT_WL_REGEX
);
366 wl_add(entry
, &pl_wl
, XT_WL_PERSISTENT
);
371 toggle_cwl(struct tab
*t
, struct karg
*args
)
382 dom
= find_domain(uri
, args
->i
);
384 if (uri
== NULL
|| dom
== NULL
||
385 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
386 show_oops(t
, "Can't toggle domain in cookie white list");
389 w
= wl_find(dom
, &c_wl
);
396 if (args
->i
& XT_WL_TOGGLE
)
398 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
400 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
404 /* enable cookies for domain */
405 args
->i
|= !XT_WL_PERSISTENT
;
406 wl_add(dom
, &c_wl
, args
->i
);
408 /* disable cookies for domain */
410 TAILQ_REMOVE(&c_wl
, w
, entry
);
416 if (args
->i
& XT_WL_RELOAD
)
417 webkit_web_view_reload(t
->wv
);
425 toggle_js(struct tab
*t
, struct karg
*args
)
435 g_object_get(G_OBJECT(t
->settings
),
436 "enable-scripts", &es
, (char *)NULL
);
437 if (args
->i
& XT_WL_TOGGLE
)
439 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
441 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
447 dom
= find_domain(uri
, args
->i
);
449 if (uri
== NULL
|| dom
== NULL
||
450 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
451 show_oops(t
, "Can't toggle domain in JavaScript white list");
456 button_set_stockid(t
->js_toggle
, GTK_STOCK_MEDIA_PLAY
);
457 args
->i
|= !XT_WL_PERSISTENT
;
458 wl_add(dom
, &js_wl
, args
->i
);
460 w
= wl_find(dom
, &js_wl
);
462 TAILQ_REMOVE(&js_wl
, w
, entry
);
466 button_set_stockid(t
->js_toggle
, GTK_STOCK_MEDIA_PAUSE
);
468 g_object_set(G_OBJECT(t
->settings
),
469 "enable-scripts", es
, (char *)NULL
);
470 g_object_set(G_OBJECT(t
->settings
),
471 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
472 webkit_web_view_set_settings(t
->wv
, t
->settings
);
474 if (args
->i
& XT_WL_RELOAD
)
475 webkit_web_view_reload(t
->wv
);
483 toggle_pl(struct tab
*t
, struct karg
*args
)
493 g_object_get(G_OBJECT(t
->settings
),
494 "enable-plugins", &es
, (char *)NULL
);
495 if (args
->i
& XT_WL_TOGGLE
)
497 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
499 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
505 dom
= find_domain(uri
, args
->i
);
507 if (uri
== NULL
|| dom
== NULL
||
508 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
509 show_oops(t
, "Can't toggle domain in plugins white list");
514 args
->i
|= !XT_WL_PERSISTENT
;
515 wl_add(dom
, &pl_wl
, args
->i
);
517 w
= wl_find(dom
, &pl_wl
);
519 TAILQ_REMOVE(&pl_wl
, w
, entry
);
524 g_object_set(G_OBJECT(t
->settings
),
525 "enable-plugins", es
, (char *)NULL
);
526 webkit_web_view_set_settings(t
->wv
, t
->settings
);
528 if (args
->i
& XT_WL_RELOAD
)
529 webkit_web_view_reload(t
->wv
);
537 toggle_force_https(struct tab
*t
, struct karg
*args
)
548 dom
= find_domain(uri
, args
->i
);
550 if (uri
== NULL
|| dom
== NULL
||
551 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
552 show_oops(t
, "Can't toggle domain in https force list");
555 w
= wl_find(dom
, &force_https
);
562 if (args
->i
& XT_WL_TOGGLE
)
564 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
566 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
570 dom
= find_domain(uri
, args
->i
);
573 args
->i
|= !XT_WL_PERSISTENT
;
574 wl_add(dom
, &force_https
, args
->i
);
576 w
= wl_find(dom
, &force_https
);
578 TAILQ_REMOVE(&force_https
, w
, entry
);
584 if (args
->i
& XT_WL_RELOAD
)
585 webkit_web_view_reload(t
->wv
);