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
= NULL
;
101 if (t
== NULL
|| args
== NULL
)
104 if (runtime_settings
[0] == '\0')
107 snprintf(file
, sizeof file
, "%s/%s", work_dir
, runtime_settings
);
108 if ((f
= fopen(file
, "r+")) == NULL
)
112 case XT_WL_JAVASCRIPT
:
113 lst_str
= "JavaScript";
114 lt
= g_strdup_printf("js_wl=%s", dom
);
118 lt
= g_strdup_printf("cookie_wl=%s", dom
);
122 lt
= g_strdup_printf("pl_wl=%s", dom
);
125 show_oops(t
, "Invalid list id: %d", list
);
130 dom
= find_domain(uri
, args
->i
& XT_WL_TOPLEVEL
);
131 if (uri
== NULL
|| dom
== NULL
||
132 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
133 show_oops(t
, "Can't add domain to %s white list", lst_str
);
138 line
= fparseln(f
, &linelen
, NULL
, NULL
, 0);
141 if (!strcmp(line
, lt
))
147 fprintf(f
, "%s\n", lt
);
152 case XT_WL_JAVASCRIPT
:
153 d
= wl_find(dom
, &js_wl
);
155 settings_add("js_wl", dom
);
156 d
= wl_find(dom
, &js_wl
);
162 d
= wl_find(dom
, &c_wl
);
164 settings_add("cookie_wl", dom
);
165 d
= wl_find(dom
, &c_wl
);
169 /* find and add to persistent jar */
170 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
171 for (;cf
; cf
= cf
->next
) {
173 if (!strcmp(dom
, ci
->domain
) ||
174 !strcmp(&dom
[1], ci
->domain
)) /* deal with leading . */ {
175 c
= soup_cookie_copy(ci
);
176 _soup_cookie_jar_add_cookie(p_cookiejar
, c
);
179 soup_cookies_free(cf
);
183 d
= wl_find(dom
, &pl_wl
);
185 settings_add("pl_wl", dom
);
186 d
= wl_find(dom
, &pl_wl
);
191 abort(); /* can't happen */
209 wl_show(struct tab
*t
, struct karg
*args
, char *title
, struct domain_list
*wl
)
217 if (args
->i
& XT_WL_PERSISTENT
) {
219 body
= g_strdup_printf("%s<h2>Persistent</h2>", body
);
221 RB_FOREACH(d
, domain_list
, wl
) {
225 body
= g_strdup_printf("%s%s<br/>", body
, d
->d
);
231 if (args
->i
& XT_WL_SESSION
) {
233 body
= g_strdup_printf("%s<h2>Session</h2>", body
);
235 RB_FOREACH(d
, domain_list
, wl
) {
239 body
= g_strdup_printf("%s%s<br/>", body
, d
->d
);
244 tmp
= get_html_page(title
, body
, "", 0);
247 load_webkit_string(t
, tmp
, XT_URI_ABOUT_JSWL
);
248 else if (wl
== &c_wl
)
249 load_webkit_string(t
, tmp
, XT_URI_ABOUT_COOKIEWL
);
251 load_webkit_string(t
, tmp
, XT_URI_ABOUT_PLUGINWL
);
257 wl_add(char *str
, struct domain_list
*wl
, int handy
)
263 if (str
== NULL
|| wl
== NULL
|| strlen(str
) < 2)
266 DNPRINTF(XT_D_COOKIE
, "wl_add in: %s\n", str
);
268 /* treat *.moo.com the same as .moo.com */
269 if (str
[0] == '*' && str
[1] == '.')
271 else if (str
[0] == '.')
276 /* slice off port number */
277 p
= g_strrstr(str
, ":");
281 d
= g_malloc(sizeof *d
);
283 d
->d
= g_strdup_printf(".%s", str
);
285 d
->d
= g_strdup(str
);
288 if (RB_INSERT(domain_list
, wl
, d
))
291 DNPRINTF(XT_D_COOKIE
, "wl_add: %s\n", d
->d
);
302 add_cookie_wl(struct settings
*s
, char *entry
)
304 wl_add(entry
, &c_wl
, 1);
309 add_js_wl(struct settings
*s
, char *entry
)
311 wl_add(entry
, &js_wl
, 1 /* persistent */);
316 add_pl_wl(struct settings
*s
, char *entry
)
318 wl_add(entry
, &pl_wl
, 1 /* persistent */);
323 toggle_cwl(struct tab
*t
, struct karg
*args
)
334 dom
= find_domain(uri
, args
->i
& XT_WL_TOPLEVEL
);
336 if (uri
== NULL
|| dom
== NULL
||
337 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
338 show_oops(t
, "Can't toggle domain in cookie white list");
341 d
= wl_find(dom
, &c_wl
);
348 if (args
->i
& XT_WL_TOGGLE
)
350 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
352 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
356 /* enable cookies for domain */
357 wl_add(dom
, &c_wl
, 0);
359 /* disable cookies for domain */
361 RB_REMOVE(domain_list
, &c_wl
, d
);
364 if (args
->i
& XT_WL_RELOAD
)
365 webkit_web_view_reload(t
->wv
);
373 toggle_js(struct tab
*t
, struct karg
*args
)
383 g_object_get(G_OBJECT(t
->settings
),
384 "enable-scripts", &es
, (char *)NULL
);
385 if (args
->i
& XT_WL_TOGGLE
)
387 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
389 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
395 dom
= find_domain(uri
, args
->i
& XT_WL_TOPLEVEL
);
397 if (uri
== NULL
|| dom
== NULL
||
398 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
399 show_oops(t
, "Can't toggle domain in JavaScript white list");
404 button_set_stockid(t
->js_toggle
, GTK_STOCK_MEDIA_PLAY
);
405 wl_add(dom
, &js_wl
, 0 /* session */);
407 d
= wl_find(dom
, &js_wl
);
409 RB_REMOVE(domain_list
, &js_wl
, d
);
410 button_set_stockid(t
->js_toggle
, GTK_STOCK_MEDIA_PAUSE
);
412 g_object_set(G_OBJECT(t
->settings
),
413 "enable-scripts", es
, (char *)NULL
);
414 g_object_set(G_OBJECT(t
->settings
),
415 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
416 webkit_web_view_set_settings(t
->wv
, t
->settings
);
418 if (args
->i
& XT_WL_RELOAD
)
419 webkit_web_view_reload(t
->wv
);
427 toggle_pl(struct tab
*t
, struct karg
*args
)
437 g_object_get(G_OBJECT(t
->settings
),
438 "enable-plugins", &es
, (char *)NULL
);
439 if (args
->i
& XT_WL_TOGGLE
)
441 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
443 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
449 dom
= find_domain(uri
, args
->i
& XT_WL_TOPLEVEL
);
451 if (uri
== NULL
|| dom
== NULL
||
452 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
453 show_oops(t
, "Can't toggle domain in plugins white list");
458 wl_add(dom
, &pl_wl
, 0 /* session */);
460 d
= wl_find(dom
, &pl_wl
);
462 RB_REMOVE(domain_list
, &pl_wl
, d
);
464 g_object_set(G_OBJECT(t
->settings
),
465 "enable-plugins", es
, (char *)NULL
);
466 webkit_web_view_set_settings(t
->wv
, t
->settings
);
468 if (args
->i
& XT_WL_RELOAD
)
469 webkit_web_view_reload(t
->wv
);