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 if ((p
= strrchr(uri
->host
, '.')) != NULL
) {
41 while(--p
>= uri
->host
&& *p
!= '.');
48 ret
= g_strdup_printf(".%s", p
);
56 wl_find(const gchar
*search
, struct domain_list
*wl
)
59 struct domain
*d
= NULL
, dfind
;
62 if (search
== NULL
|| wl
== NULL
)
64 if (strlen(search
) < 2)
68 s
= g_strdup_printf(".%s", search
);
72 for (i
= strlen(s
) - 1; i
>= 0; i
--) {
75 d
= RB_FIND(domain_list
, wl
, &dfind
);
89 wl_save(struct tab
*t
, struct karg
*args
, int list
)
91 char file
[PATH_MAX
], *lst_str
= NULL
;
93 char *line
= NULL
, *lt
= NULL
, *dom
;
101 if (t
== NULL
|| args
== NULL
)
104 if (runtime_settings
[0] == '\0')
108 case XT_WL_JAVASCRIPT
:
109 lst_str
= "JavaScript";
118 show_oops(t
, "Invalid list id: %d", list
);
123 dom
= find_domain(uri
, args
->i
& XT_WL_TOPLEVEL
);
124 if (uri
== NULL
|| dom
== NULL
||
125 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
126 show_oops(t
, "Can't add domain to %s white list", lst_str
);
131 case XT_WL_JAVASCRIPT
:
132 lt
= g_strdup_printf("js_wl=%s", dom
);
135 lt
= g_strdup_printf("cookie_wl=%s", dom
);
138 lt
= g_strdup_printf("pl_wl=%s", dom
);
142 show_oops(t
, "Invalid list id: %d", list
);
146 snprintf(file
, sizeof file
, "%s/%s", work_dir
, runtime_settings
);
147 if ((f
= fopen(file
, "r+")) == NULL
) {
148 show_oops(t
, "can't open file %s");
153 line
= fparseln(f
, &linelen
, NULL
, NULL
, 0);
156 if (!strcmp(line
, lt
))
162 fprintf(f
, "%s\n", lt
);
167 case XT_WL_JAVASCRIPT
:
168 d
= wl_find(dom
, &js_wl
);
170 settings_add("js_wl", dom
);
171 d
= wl_find(dom
, &js_wl
);
177 d
= wl_find(dom
, &c_wl
);
179 settings_add("cookie_wl", dom
);
180 d
= wl_find(dom
, &c_wl
);
184 /* find and add to persistent jar */
185 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
186 for (;cf
; cf
= cf
->next
) {
188 if (!strcmp(dom
, ci
->domain
) ||
189 !strcmp(&dom
[1], ci
->domain
)) /* deal with leading . */ {
190 c
= soup_cookie_copy(ci
);
191 _soup_cookie_jar_add_cookie(p_cookiejar
, c
);
194 soup_cookies_free(cf
);
198 d
= wl_find(dom
, &pl_wl
);
200 settings_add("pl_wl", dom
);
201 d
= wl_find(dom
, &pl_wl
);
206 abort(); /* can't happen */
225 wl_show(struct tab
*t
, struct karg
*args
, char *title
, struct domain_list
*wl
)
233 if (args
->i
& XT_WL_PERSISTENT
) {
235 body
= g_strdup_printf("%s<h2>Persistent</h2>", body
);
237 RB_FOREACH(d
, domain_list
, wl
) {
241 body
= g_strdup_printf("%s%s<br/>", body
, d
->d
);
247 if (args
->i
& XT_WL_SESSION
) {
249 body
= g_strdup_printf("%s<h2>Session</h2>", body
);
251 RB_FOREACH(d
, domain_list
, wl
) {
255 body
= g_strdup_printf("%s%s<br/>", body
, d
->d
);
260 tmp
= get_html_page(title
, body
, "", 0);
263 load_webkit_string(t
, tmp
, XT_URI_ABOUT_JSWL
);
264 else if (wl
== &c_wl
)
265 load_webkit_string(t
, tmp
, XT_URI_ABOUT_COOKIEWL
);
267 load_webkit_string(t
, tmp
, XT_URI_ABOUT_PLUGINWL
);
273 wl_add(char *str
, struct domain_list
*wl
, int handy
)
279 if (str
== NULL
|| wl
== NULL
|| strlen(str
) < 2)
282 DNPRINTF(XT_D_COOKIE
, "wl_add in: %s\n", str
);
284 /* treat *.moo.com the same as .moo.com */
285 if (str
[0] == '*' && str
[1] == '.')
287 else if (str
[0] == '.')
292 /* slice off port number */
293 p
= g_strrstr(str
, ":");
297 d
= g_malloc(sizeof *d
);
299 d
->d
= g_strdup_printf(".%s", str
);
301 d
->d
= g_strdup(str
);
304 if (RB_INSERT(domain_list
, wl
, d
))
307 DNPRINTF(XT_D_COOKIE
, "wl_add: %s\n", d
->d
);
318 add_cookie_wl(struct settings
*s
, char *entry
)
320 wl_add(entry
, &c_wl
, 1);
325 add_js_wl(struct settings
*s
, char *entry
)
327 wl_add(entry
, &js_wl
, 1 /* persistent */);
332 add_pl_wl(struct settings
*s
, char *entry
)
334 wl_add(entry
, &pl_wl
, 1 /* persistent */);
339 toggle_cwl(struct tab
*t
, struct karg
*args
)
350 dom
= find_domain(uri
, args
->i
& XT_WL_TOPLEVEL
);
352 if (uri
== NULL
|| dom
== NULL
||
353 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
354 show_oops(t
, "Can't toggle domain in cookie white list");
357 d
= wl_find(dom
, &c_wl
);
364 if (args
->i
& XT_WL_TOGGLE
)
366 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
368 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
372 /* enable cookies for domain */
373 wl_add(dom
, &c_wl
, 0);
375 /* disable cookies for domain */
377 RB_REMOVE(domain_list
, &c_wl
, d
);
380 if (args
->i
& XT_WL_RELOAD
)
381 webkit_web_view_reload(t
->wv
);
389 toggle_js(struct tab
*t
, struct karg
*args
)
399 g_object_get(G_OBJECT(t
->settings
),
400 "enable-scripts", &es
, (char *)NULL
);
401 if (args
->i
& XT_WL_TOGGLE
)
403 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
405 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
411 dom
= find_domain(uri
, args
->i
& XT_WL_TOPLEVEL
);
413 if (uri
== NULL
|| dom
== NULL
||
414 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
415 show_oops(t
, "Can't toggle domain in JavaScript white list");
420 button_set_stockid(t
->js_toggle
, GTK_STOCK_MEDIA_PLAY
);
421 wl_add(dom
, &js_wl
, 0 /* session */);
423 d
= wl_find(dom
, &js_wl
);
425 RB_REMOVE(domain_list
, &js_wl
, d
);
426 button_set_stockid(t
->js_toggle
, GTK_STOCK_MEDIA_PAUSE
);
428 g_object_set(G_OBJECT(t
->settings
),
429 "enable-scripts", es
, (char *)NULL
);
430 g_object_set(G_OBJECT(t
->settings
),
431 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
432 webkit_web_view_set_settings(t
->wv
, t
->settings
);
434 if (args
->i
& XT_WL_RELOAD
)
435 webkit_web_view_reload(t
->wv
);
443 toggle_pl(struct tab
*t
, struct karg
*args
)
453 g_object_get(G_OBJECT(t
->settings
),
454 "enable-plugins", &es
, (char *)NULL
);
455 if (args
->i
& XT_WL_TOGGLE
)
457 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
459 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
465 dom
= find_domain(uri
, args
->i
& XT_WL_TOPLEVEL
);
467 if (uri
== NULL
|| dom
== NULL
||
468 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
469 show_oops(t
, "Can't toggle domain in plugins white list");
474 wl_add(dom
, &pl_wl
, 0 /* session */);
476 d
= wl_find(dom
, &pl_wl
);
478 RB_REMOVE(domain_list
, &pl_wl
, d
);
480 g_object_set(G_OBJECT(t
->settings
),
481 "enable-plugins", es
, (char *)NULL
);
482 webkit_web_view_set_settings(t
->wv
, t
->settings
);
484 if (args
->i
& XT_WL_RELOAD
)
485 webkit_web_view_reload(t
->wv
);