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>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 find_domain(const gchar
*s
, int toplevel
)
33 uri
= soup_uri_new(s
);
35 if (uri
== NULL
|| !SOUP_URI_VALID_FOR_HTTP(uri
)) {
39 if (toplevel
&& !isdigit(uri
->host
[strlen(uri
->host
) - 1])) {
40 p
= tld_get_suffix(uri
->host
);
44 ret
= g_strdup_printf(".%s", p
);
52 wl_find(const gchar
*search
, struct domain_list
*wl
)
55 struct domain
*d
= NULL
, dfind
;
58 if (search
== NULL
|| wl
== NULL
)
60 if (strlen(search
) < 2)
64 s
= g_strdup_printf(".%s", search
);
68 for (i
= strlen(s
) - 1; i
>= 0; i
--) {
71 d
= RB_FIND(domain_list
, wl
, &dfind
);
85 wl_save(struct tab
*t
, struct karg
*args
, int list
)
87 char file
[PATH_MAX
], *lst_str
= NULL
;
89 char *line
= NULL
, *lt
= NULL
, *dom
;
97 if (t
== NULL
|| args
== NULL
)
100 if (runtime_settings
[0] == '\0')
104 case XT_WL_JAVASCRIPT
:
105 lst_str
= "JavaScript";
114 show_oops(t
, "Invalid list id: %d", list
);
119 dom
= find_domain(uri
, args
->i
& XT_WL_TOPLEVEL
);
120 if (uri
== NULL
|| dom
== NULL
||
121 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
122 show_oops(t
, "Can't add domain to %s white list", lst_str
);
127 case XT_WL_JAVASCRIPT
:
128 lt
= g_strdup_printf("js_wl=%s", dom
);
131 lt
= g_strdup_printf("cookie_wl=%s", dom
);
134 lt
= g_strdup_printf("pl_wl=%s", dom
);
138 show_oops(t
, "Invalid list id: %d", list
);
142 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, runtime_settings
);
143 if ((f
= fopen(file
, "r+")) == NULL
) {
144 show_oops(t
, "can't open file %s");
149 line
= fparseln(f
, &linelen
, NULL
, NULL
, 0);
152 if (!strcmp(line
, lt
))
158 fprintf(f
, "%s\n", lt
);
163 case XT_WL_JAVASCRIPT
:
164 d
= wl_find(dom
, &js_wl
);
166 settings_add("js_wl", dom
);
167 d
= wl_find(dom
, &js_wl
);
173 d
= wl_find(dom
, &c_wl
);
175 settings_add("cookie_wl", dom
);
176 d
= wl_find(dom
, &c_wl
);
180 /* find and add to persistent jar */
181 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
182 for (;cf
; cf
= cf
->next
) {
184 if (!strcmp(dom
, ci
->domain
) ||
185 !strcmp(&dom
[1], ci
->domain
)) /* deal with leading . */ {
186 c
= soup_cookie_copy(ci
);
187 _soup_cookie_jar_add_cookie(p_cookiejar
, c
);
190 soup_cookies_free(cf
);
194 d
= wl_find(dom
, &pl_wl
);
196 settings_add("pl_wl", dom
);
197 d
= wl_find(dom
, &pl_wl
);
202 abort(); /* can't happen */
221 wl_show(struct tab
*t
, struct karg
*args
, char *title
, struct domain_list
*wl
)
229 if (args
->i
& XT_WL_PERSISTENT
) {
231 body
= g_strdup_printf("%s<h2>Persistent</h2>", body
);
233 RB_FOREACH(d
, domain_list
, wl
) {
237 body
= g_strdup_printf("%s%s<br/>", body
, d
->d
);
243 if (args
->i
& XT_WL_SESSION
) {
245 body
= g_strdup_printf("%s<h2>Session</h2>", body
);
247 RB_FOREACH(d
, domain_list
, wl
) {
251 body
= g_strdup_printf("%s%s<br/>", body
, d
->d
);
256 tmp
= get_html_page(title
, body
, "", 0);
259 load_webkit_string(t
, tmp
, XT_URI_ABOUT_JSWL
);
260 else if (wl
== &c_wl
)
261 load_webkit_string(t
, tmp
, XT_URI_ABOUT_COOKIEWL
);
263 load_webkit_string(t
, tmp
, XT_URI_ABOUT_PLUGINWL
);
269 wl_add(char *str
, struct domain_list
*wl
, int handy
)
275 if (str
== NULL
|| wl
== NULL
|| strlen(str
) < 2)
278 DNPRINTF(XT_D_COOKIE
, "wl_add in: %s\n", str
);
280 /* treat *.moo.com the same as .moo.com */
281 if (str
[0] == '*' && str
[1] == '.')
283 else if (str
[0] == '.')
288 /* slice off port number */
289 p
= g_strrstr(str
, ":");
293 d
= g_malloc(sizeof *d
);
295 d
->d
= g_strdup_printf(".%s", str
);
297 d
->d
= g_strdup(str
);
300 if (RB_INSERT(domain_list
, wl
, d
))
303 DNPRINTF(XT_D_COOKIE
, "wl_add: %s\n", d
->d
);
314 add_cookie_wl(struct settings
*s
, char *entry
)
316 wl_add(entry
, &c_wl
, 1);
321 add_js_wl(struct settings
*s
, char *entry
)
323 wl_add(entry
, &js_wl
, 1 /* persistent */);
328 add_pl_wl(struct settings
*s
, char *entry
)
330 wl_add(entry
, &pl_wl
, 1 /* persistent */);
335 toggle_cwl(struct tab
*t
, struct karg
*args
)
346 dom
= find_domain(uri
, args
->i
& XT_WL_TOPLEVEL
);
348 if (uri
== NULL
|| dom
== NULL
||
349 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
350 show_oops(t
, "Can't toggle domain in cookie white list");
353 d
= wl_find(dom
, &c_wl
);
360 if (args
->i
& XT_WL_TOGGLE
)
362 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
364 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
368 /* enable cookies for domain */
369 wl_add(dom
, &c_wl
, 0);
371 /* disable cookies for domain */
373 RB_REMOVE(domain_list
, &c_wl
, d
);
376 if (args
->i
& XT_WL_RELOAD
)
377 webkit_web_view_reload(t
->wv
);
385 toggle_js(struct tab
*t
, struct karg
*args
)
395 g_object_get(G_OBJECT(t
->settings
),
396 "enable-scripts", &es
, (char *)NULL
);
397 if (args
->i
& XT_WL_TOGGLE
)
399 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
401 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
407 dom
= find_domain(uri
, args
->i
& XT_WL_TOPLEVEL
);
409 if (uri
== NULL
|| dom
== NULL
||
410 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
411 show_oops(t
, "Can't toggle domain in JavaScript white list");
416 button_set_stockid(t
->js_toggle
, GTK_STOCK_MEDIA_PLAY
);
417 wl_add(dom
, &js_wl
, 0 /* session */);
419 d
= wl_find(dom
, &js_wl
);
421 RB_REMOVE(domain_list
, &js_wl
, d
);
422 button_set_stockid(t
->js_toggle
, GTK_STOCK_MEDIA_PAUSE
);
424 g_object_set(G_OBJECT(t
->settings
),
425 "enable-scripts", es
, (char *)NULL
);
426 g_object_set(G_OBJECT(t
->settings
),
427 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
428 webkit_web_view_set_settings(t
->wv
, t
->settings
);
430 if (args
->i
& XT_WL_RELOAD
)
431 webkit_web_view_reload(t
->wv
);
439 toggle_pl(struct tab
*t
, struct karg
*args
)
449 g_object_get(G_OBJECT(t
->settings
),
450 "enable-plugins", &es
, (char *)NULL
);
451 if (args
->i
& XT_WL_TOGGLE
)
453 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
455 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
461 dom
= find_domain(uri
, args
->i
& XT_WL_TOPLEVEL
);
463 if (uri
== NULL
|| dom
== NULL
||
464 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
465 show_oops(t
, "Can't toggle domain in plugins white list");
470 wl_add(dom
, &pl_wl
, 0 /* session */);
472 d
= wl_find(dom
, &pl_wl
);
474 RB_REMOVE(domain_list
, &pl_wl
, d
);
476 g_object_set(G_OBJECT(t
->settings
),
477 "enable-plugins", es
, (char *)NULL
);
478 webkit_web_view_set_settings(t
->wv
, t
->settings
);
480 if (args
->i
& XT_WL_RELOAD
)
481 webkit_web_view_reload(t
->wv
);