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
);
38 if (!SOUP_URI_VALID_FOR_HTTP(uri
)) {
43 if (flags
& XT_WL_TOPLEVEL
&&
44 !isdigit((unsigned char)uri
->host
[strlen(uri
->host
) - 1]))
45 p
= tld_get_suffix(uri
->host
);
49 if (flags
& XT_WL_TOPLEVEL
)
50 ret
= g_strdup_printf(".%s", p
);
51 else /* assume FQDN */
60 wl_find(const gchar
*s
, struct wl_list
*wl
)
64 if (s
== NULL
|| strlen(s
) == 0 || wl
== NULL
)
67 TAILQ_FOREACH(w
, wl
, entry
) {
70 if (!regexec(w
->re
, s
, 0, 0, 0))
78 wl_save(struct tab
*t
, struct karg
*args
, int list
)
80 char file
[PATH_MAX
], *lst_str
= NULL
;
82 char *line
= NULL
, *lt
= NULL
, *dom
;
90 if (t
== NULL
|| args
== NULL
)
93 if (runtime_settings
[0] == '\0')
97 case XT_WL_JAVASCRIPT
:
98 lst_str
= "JavaScript";
110 show_oops(t
, "Invalid list id: %d", list
);
115 dom
= find_domain(uri
, args
->i
);
116 if (uri
== NULL
|| dom
== NULL
||
117 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
118 show_oops(t
, "Can't add domain to %s white list", lst_str
);
123 case XT_WL_JAVASCRIPT
:
124 lt
= g_strdup_printf("js_wl=%s", dom
);
127 lt
= g_strdup_printf("cookie_wl=%s", dom
);
130 lt
= g_strdup_printf("pl_wl=%s", dom
);
133 lt
= g_strdup_printf("force_https=%s", dom
);
137 show_oops(t
, "Invalid list id: %d", list
);
141 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, runtime_settings
);
142 if ((f
= fopen(file
, "r+")) == NULL
) {
143 show_oops(t
, "can't open file %s");
148 line
= fparseln(f
, &linelen
, NULL
, NULL
, 0);
151 if (!strcmp(line
, lt
))
157 fprintf(f
, "%s\n", lt
);
162 case XT_WL_JAVASCRIPT
:
163 w
= wl_find(dom
, &js_wl
);
165 settings_add("js_wl", dom
);
166 w
= wl_find(dom
, &js_wl
);
172 w
= wl_find(dom
, &c_wl
);
174 settings_add("cookie_wl", dom
);
175 w
= wl_find(dom
, &c_wl
);
179 /* find and add to persistent jar */
180 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
181 for (;cf
; cf
= cf
->next
) {
183 if (!strcmp(dom
, ci
->domain
) ||
184 !strcmp(&dom
[1], ci
->domain
)) /* deal with leading . */ {
185 c
= soup_cookie_copy(ci
);
186 _soup_cookie_jar_add_cookie(p_cookiejar
, c
);
189 soup_cookies_free(cf
);
193 w
= wl_find(dom
, &pl_wl
);
195 settings_add("pl_wl", dom
);
196 w
= wl_find(dom
, &pl_wl
);
201 w
= wl_find(dom
, &force_https
);
203 settings_add("force_https", dom
);
204 w
= wl_find(dom
, &force_https
);
206 toggle_force_https(t
, &a
);
209 abort(); /* can't happen */
228 wl_show(struct tab
*t
, struct karg
*args
, char *title
, struct wl_list
*wl
)
236 if (args
->i
& XT_WL_PERSISTENT
) {
238 body
= g_strdup_printf("%s<h2>Persistent</h2>", body
);
240 TAILQ_FOREACH(w
, wl
, entry
) {
244 body
= g_strdup_printf("%s%s<br/>", body
, w
->pat
);
250 if (args
->i
& XT_WL_SESSION
) {
252 body
= g_strdup_printf("%s<h2>Session</h2>", body
);
254 TAILQ_FOREACH(w
, wl
, entry
) {
258 body
= g_strdup_printf("%s%s<br/>", body
, w
->pat
);
263 tmp
= get_html_page(title
, body
, "", 0);
266 load_webkit_string(t
, tmp
, XT_URI_ABOUT_JSWL
, 0);
267 else if (wl
== &c_wl
)
268 load_webkit_string(t
, tmp
, XT_URI_ABOUT_COOKIEWL
, 0);
269 else if (wl
== &pl_wl
)
270 load_webkit_string(t
, tmp
, XT_URI_ABOUT_PLUGINWL
, 0);
271 else if (wl
== &force_https
)
272 load_webkit_string(t
, tmp
, XT_URI_ABOUT_HTTPS
, 0);
278 wl_add(const char *str
, struct wl_list
*wl
, int flags
)
281 int add_dot
= 0, chopped
= 0;
283 char *escstr
, *p
, *pat
;
286 if (str
== NULL
|| wl
== NULL
|| strlen(str
) < 2)
289 DNPRINTF(XT_D_COOKIE
, "wl_add in: %s\n", str
);
291 /* slice off port number */
292 p
= g_strrstr(str
, ":");
296 w
= g_malloc(sizeof *w
);
297 w
->re
= g_malloc(sizeof *w
->re
);
298 if (flags
& XT_WL_REGEX
) {
299 w
->pat
= g_strdup_printf("re:%s", str
);
300 regcomp(w
->re
, str
, REG_EXTENDED
| REG_NOSUB
);
301 DNPRINTF(XT_D_COOKIE
, "wl_add: %s\n", str
);
303 /* treat *.moo.com the same as .moo.com */
304 if (s
[0] == '*' && s
[1] == '.')
306 else if (s
[0] != '.' && (flags
& XT_WL_TOPLEVEL
))
313 sv
= g_strsplit(s
, ".", 0);
314 escstr
= g_strjoinv("\\.", sv
);
318 w
->pat
= g_strdup_printf(".%s", str
);
319 pat
= g_strdup_printf("^(.*\\.)*%s$", escstr
);
320 regcomp(w
->re
, pat
, REG_EXTENDED
| REG_NOSUB
);
322 w
->pat
= g_strdup(str
);
324 pat
= g_strdup_printf("^(.*\\.)*%s$", escstr
);
326 pat
= g_strdup_printf("^%s$", escstr
);
327 regcomp(w
->re
, pat
, REG_EXTENDED
| REG_NOSUB
);
329 DNPRINTF(XT_D_COOKIE
, "wl_add: %s\n", pat
);
334 w
->handy
= (flags
& XT_WL_PERSISTENT
) ? 1 : 0;
336 TAILQ_INSERT_HEAD(wl
, w
, entry
);
342 add_cookie_wl(struct settings
*s
, char *entry
)
344 if (g_str_has_prefix(entry
, "re:")) {
346 wl_add(entry
, &c_wl
, XT_WL_PERSISTENT
| XT_WL_REGEX
);
348 wl_add(entry
, &c_wl
, XT_WL_PERSISTENT
);
353 add_js_wl(struct settings
*s
, char *entry
)
355 if (g_str_has_prefix(entry
, "re:")) {
357 wl_add(entry
, &js_wl
, XT_WL_PERSISTENT
| XT_WL_REGEX
);
359 wl_add(entry
, &js_wl
, XT_WL_PERSISTENT
);
364 add_pl_wl(struct settings
*s
, char *entry
)
366 if (g_str_has_prefix(entry
, "re:")) {
368 wl_add(entry
, &pl_wl
, XT_WL_PERSISTENT
| XT_WL_REGEX
);
370 wl_add(entry
, &pl_wl
, XT_WL_PERSISTENT
);
375 toggle_cwl(struct tab
*t
, struct karg
*args
)
386 dom
= find_domain(uri
, args
->i
);
388 if (uri
== NULL
|| dom
== NULL
||
389 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
390 show_oops(t
, "Can't toggle domain in cookie white list");
393 w
= wl_find(dom
, &c_wl
);
400 if (args
->i
& XT_WL_TOGGLE
)
402 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
404 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
408 /* enable cookies for domain */
409 args
->i
|= !XT_WL_PERSISTENT
;
410 wl_add(dom
, &c_wl
, args
->i
);
412 /* disable cookies for domain */
414 TAILQ_REMOVE(&c_wl
, w
, entry
);
420 if (args
->i
& XT_WL_RELOAD
)
421 webkit_web_view_reload(t
->wv
);
429 toggle_js(struct tab
*t
, struct karg
*args
)
439 g_object_get(G_OBJECT(t
->settings
),
440 "enable-scripts", &es
, (char *)NULL
);
441 if (args
->i
& XT_WL_TOGGLE
)
443 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
445 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
451 dom
= find_domain(uri
, args
->i
);
453 if (uri
== NULL
|| dom
== NULL
||
454 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
455 show_oops(t
, "Can't toggle domain in JavaScript white list");
460 button_set_icon_name(t
->js_toggle
, "media-playback-start");
461 args
->i
|= !XT_WL_PERSISTENT
;
462 wl_add(dom
, &js_wl
, args
->i
);
464 w
= wl_find(dom
, &js_wl
);
466 TAILQ_REMOVE(&js_wl
, w
, entry
);
470 button_set_icon_name(t
->js_toggle
, "media-playback-pause");
472 g_object_set(G_OBJECT(t
->settings
),
473 "enable-scripts", es
, (char *)NULL
);
474 g_object_set(G_OBJECT(t
->settings
),
475 "javascript-can-open-windows-automatically", js_auto_open_windows
? es
: 0, (char *)NULL
);
476 webkit_web_view_set_settings(t
->wv
, t
->settings
);
478 if (args
->i
& XT_WL_RELOAD
)
479 webkit_web_view_reload(t
->wv
);
487 toggle_pl(struct tab
*t
, struct karg
*args
)
497 g_object_get(G_OBJECT(t
->settings
),
498 "enable-plugins", &es
, (char *)NULL
);
499 if (args
->i
& XT_WL_TOGGLE
)
501 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
503 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
509 dom
= find_domain(uri
, args
->i
);
511 if (uri
== NULL
|| dom
== NULL
||
512 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
513 show_oops(t
, "Can't toggle domain in plugins white list");
518 args
->i
|= !XT_WL_PERSISTENT
;
519 wl_add(dom
, &pl_wl
, args
->i
);
521 w
= wl_find(dom
, &pl_wl
);
523 TAILQ_REMOVE(&pl_wl
, w
, entry
);
528 g_object_set(G_OBJECT(t
->settings
),
529 "enable-plugins", es
, (char *)NULL
);
530 webkit_web_view_set_settings(t
->wv
, t
->settings
);
532 if (args
->i
& XT_WL_RELOAD
)
533 webkit_web_view_reload(t
->wv
);
541 toggle_force_https(struct tab
*t
, struct karg
*args
)
552 dom
= find_domain(uri
, args
->i
);
554 if (uri
== NULL
|| dom
== NULL
||
555 webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
) {
556 show_oops(t
, "Can't toggle domain in https force list");
559 w
= wl_find(dom
, &force_https
);
566 if (args
->i
& XT_WL_TOGGLE
)
568 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
570 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
574 args
->i
|= !XT_WL_PERSISTENT
;
575 wl_add(dom
, &force_https
, args
->i
);
576 } else if (w
!= NULL
) {
577 TAILQ_REMOVE(&force_https
, w
, entry
);
582 if (args
->i
& XT_WL_RELOAD
)
583 webkit_web_view_reload(t
->wv
);