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 domain_list
*wl
)
58 struct domain
*d
= NULL
, dfind
;
61 if (s
== NULL
|| wl
== NULL
)
66 for (i
= strlen(s
) - 1; i
>= 0; --i
) {
67 if (i
== 0 || (s
[i
] == '.')) {
68 dfind
.d
= (gchar
*)&s
[i
];
69 d
= RB_FIND(domain_list
, wl
, &dfind
);
72 if (i
== 0 && s
[i
] != '.') {
73 dfind
.d
= g_strdup_printf(".%s", s
);
74 d
= RB_FIND(domain_list
, wl
, &dfind
);
87 wl_save(struct tab
*t
, struct karg
*args
, int list
)
89 char file
[PATH_MAX
], *lst_str
= NULL
;
91 char *line
= NULL
, *lt
= NULL
, *dom
;
99 if (t
== NULL
|| args
== NULL
)
102 if (runtime_settings
[0] == '\0')
106 case XT_WL_JAVASCRIPT
:
107 lst_str
= "JavaScript";
119 show_oops(t
, "Invalid list id: %d", list
);
124 dom
= find_domain(uri
, args
->i
);
125 if (uri
== NULL
|| dom
== NULL
||
126 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
127 show_oops(t
, "Can't add domain to %s white list", lst_str
);
132 case XT_WL_JAVASCRIPT
:
133 lt
= g_strdup_printf("js_wl=%s", dom
);
136 lt
= g_strdup_printf("cookie_wl=%s", dom
);
139 lt
= g_strdup_printf("pl_wl=%s", dom
);
142 lt
= g_strdup_printf("force_https=%s", dom
);
146 show_oops(t
, "Invalid list id: %d", list
);
150 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, runtime_settings
);
151 if ((f
= fopen(file
, "r+")) == NULL
) {
152 show_oops(t
, "can't open file %s");
157 line
= fparseln(f
, &linelen
, NULL
, NULL
, 0);
160 if (!strcmp(line
, lt
))
166 fprintf(f
, "%s\n", lt
);
171 case XT_WL_JAVASCRIPT
:
172 d
= wl_find(dom
, &js_wl
);
174 settings_add("js_wl", dom
);
175 d
= wl_find(dom
, &js_wl
);
181 d
= wl_find(dom
, &c_wl
);
183 settings_add("cookie_wl", dom
);
184 d
= wl_find(dom
, &c_wl
);
188 /* find and add to persistent jar */
189 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
190 for (;cf
; cf
= cf
->next
) {
192 if (!strcmp(dom
, ci
->domain
) ||
193 !strcmp(&dom
[1], ci
->domain
)) /* deal with leading . */ {
194 c
= soup_cookie_copy(ci
);
195 _soup_cookie_jar_add_cookie(p_cookiejar
, c
);
198 soup_cookies_free(cf
);
202 d
= wl_find(dom
, &pl_wl
);
204 settings_add("pl_wl", dom
);
205 d
= wl_find(dom
, &pl_wl
);
210 d
= wl_find(dom
, &force_https
);
212 settings_add("force_https", dom
);
213 d
= wl_find(dom
, &force_https
);
215 toggle_force_https(t
, &a
);
218 abort(); /* can't happen */
237 wl_show(struct tab
*t
, struct karg
*args
, char *title
, struct domain_list
*wl
)
245 if (args
->i
& XT_WL_PERSISTENT
) {
247 body
= g_strdup_printf("%s<h2>Persistent</h2>", body
);
249 RB_FOREACH(d
, domain_list
, wl
) {
253 body
= g_strdup_printf("%s%s<br/>", body
, d
->d
);
259 if (args
->i
& XT_WL_SESSION
) {
261 body
= g_strdup_printf("%s<h2>Session</h2>", body
);
263 RB_FOREACH(d
, domain_list
, wl
) {
267 body
= g_strdup_printf("%s%s<br/>", body
, d
->d
);
272 tmp
= get_html_page(title
, body
, "", 0);
275 load_webkit_string(t
, tmp
, XT_URI_ABOUT_JSWL
);
276 else if (wl
== &c_wl
)
277 load_webkit_string(t
, tmp
, XT_URI_ABOUT_COOKIEWL
);
278 else if (wl
== &pl_wl
)
279 load_webkit_string(t
, tmp
, XT_URI_ABOUT_PLUGINWL
);
280 else if (wl
== &force_https
)
281 load_webkit_string(t
, tmp
, XT_URI_ABOUT_HTTPS
);
287 wl_add(const char *str
, struct domain_list
*wl
, int flags
)
293 if (str
== NULL
|| wl
== NULL
|| strlen(str
) < 2)
296 DNPRINTF(XT_D_COOKIE
, "wl_add in: %s\n", str
);
298 /* treat *.moo.com the same as .moo.com */
299 if (str
[0] == '*' && str
[1] == '.')
301 else if (str
[0] != '.' && (flags
& XT_WL_TOPLEVEL
))
304 /* slice off port number */
305 p
= g_strrstr(str
, ":");
309 d
= g_malloc(sizeof *d
);
311 d
->d
= g_strdup_printf(".%s", str
);
313 d
->d
= g_strdup(str
);
314 d
->handy
= (flags
& XT_WL_PERSISTENT
) ? 1 : 0;
316 if (RB_INSERT(domain_list
, wl
, d
))
319 DNPRINTF(XT_D_COOKIE
, "wl_add: %s\n", d
->d
);
330 add_cookie_wl(struct settings
*s
, char *entry
)
332 wl_add(entry
, &c_wl
, XT_WL_PERSISTENT
);
337 add_js_wl(struct settings
*s
, char *entry
)
339 wl_add(entry
, &js_wl
, XT_WL_PERSISTENT
);
344 add_pl_wl(struct settings
*s
, char *entry
)
346 wl_add(entry
, &pl_wl
, XT_WL_PERSISTENT
);
351 toggle_cwl(struct tab
*t
, struct karg
*args
)
362 dom
= find_domain(uri
, args
->i
);
364 if (uri
== NULL
|| dom
== NULL
||
365 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
366 show_oops(t
, "Can't toggle domain in cookie white list");
369 d
= wl_find(dom
, &c_wl
);
376 if (args
->i
& XT_WL_TOGGLE
)
378 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
380 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
384 /* enable cookies for domain */
385 args
->i
|= !XT_WL_PERSISTENT
;
386 wl_add(dom
, &c_wl
, args
->i
);
388 /* disable cookies for domain */
390 RB_REMOVE(domain_list
, &c_wl
, d
);
393 if (args
->i
& XT_WL_RELOAD
)
394 webkit_web_view_reload(t
->wv
);
402 toggle_js(struct tab
*t
, struct karg
*args
)
412 g_object_get(G_OBJECT(t
->settings
),
413 "enable-scripts", &es
, (char *)NULL
);
414 if (args
->i
& XT_WL_TOGGLE
)
416 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
418 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
424 dom
= find_domain(uri
, args
->i
);
426 if (uri
== NULL
|| dom
== NULL
||
427 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
428 show_oops(t
, "Can't toggle domain in JavaScript white list");
433 button_set_stockid(t
->js_toggle
, GTK_STOCK_MEDIA_PLAY
);
434 args
->i
|= !XT_WL_PERSISTENT
;
435 wl_add(dom
, &js_wl
, args
->i
);
437 d
= wl_find(dom
, &js_wl
);
439 RB_REMOVE(domain_list
, &js_wl
, d
);
440 button_set_stockid(t
->js_toggle
, GTK_STOCK_MEDIA_PAUSE
);
442 g_object_set(G_OBJECT(t
->settings
),
443 "enable-scripts", es
, (char *)NULL
);
444 g_object_set(G_OBJECT(t
->settings
),
445 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
446 webkit_web_view_set_settings(t
->wv
, t
->settings
);
448 if (args
->i
& XT_WL_RELOAD
)
449 webkit_web_view_reload(t
->wv
);
457 toggle_pl(struct tab
*t
, struct karg
*args
)
467 g_object_get(G_OBJECT(t
->settings
),
468 "enable-plugins", &es
, (char *)NULL
);
469 if (args
->i
& XT_WL_TOGGLE
)
471 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
473 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
479 dom
= find_domain(uri
, args
->i
);
481 if (uri
== NULL
|| dom
== NULL
||
482 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
483 show_oops(t
, "Can't toggle domain in plugins white list");
488 args
->i
|= !XT_WL_PERSISTENT
;
489 wl_add(dom
, &pl_wl
, args
->i
);
491 d
= wl_find(dom
, &pl_wl
);
493 RB_REMOVE(domain_list
, &pl_wl
, d
);
495 g_object_set(G_OBJECT(t
->settings
),
496 "enable-plugins", es
, (char *)NULL
);
497 webkit_web_view_set_settings(t
->wv
, t
->settings
);
499 if (args
->i
& XT_WL_RELOAD
)
500 webkit_web_view_reload(t
->wv
);
508 toggle_force_https(struct tab
*t
, struct karg
*args
)
519 dom
= find_domain(uri
, args
->i
);
521 if (uri
== NULL
|| dom
== NULL
||
522 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
523 show_oops(t
, "Can't toggle domain in https force list");
526 d
= wl_find(dom
, &force_https
);
533 if (args
->i
& XT_WL_TOGGLE
)
535 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
537 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
541 dom
= find_domain(uri
, args
->i
);
544 args
->i
|= !XT_WL_PERSISTENT
;
545 wl_add(dom
, &force_https
, args
->i
);
547 d
= wl_find(dom
, &force_https
);
549 RB_REMOVE(domain_list
, &force_https
, d
);
552 if (args
->i
& XT_WL_RELOAD
)
553 webkit_web_view_reload(t
->wv
);