forked from conformal/xombrero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
about.c
2225 lines (1925 loc) · 53.3 KB
/
about.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010, 2011 Marco Peereboom <[email protected]>
* Copyright (c) 2011 Stevan Andjelkovic <[email protected]>
* Copyright (c) 2010, 2011, 2012 Edd Barrett <[email protected]>
* Copyright (c) 2011 Todd T. Fries <[email protected]>
* Copyright (c) 2011 Raphael Graf <[email protected]>
* Copyright (c) 2011 Michal Mazurek <[email protected]>
* Copyright (c) 2012 Josh Rickmar <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <xombrero.h>
/*
* xombrero "protocol" (xtp)
* We use this for managing stuff like downloads and favorites. They
* make magical HTML pages in memory which have xxxt:// links in order
* to communicate with xombrero's internals. These links take the format:
* xxxt://class/session_key/action/arg
*
* Don't begin xtp class/actions as 0. atoi returns that on error.
*
* Typically we have not put addition of items in this framework, as
* adding items is either done via an ex-command or via a keybinding instead.
*/
#define XT_HTML_TAG "<html xmlns='http://www.w3.org/1999/xhtml'>\n"
#define XT_DOCTYPE "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>\n"
#define XT_PAGE_STYLE "<style type='text/css'>\n" \
"td{overflow: hidden;" \
" padding: 2px 2px 2px 2px;" \
" border: 1px solid black;" \
" vertical-align:top;" \
" word-wrap: break-word}\n" \
"tr:hover{background: #ffff99}\n" \
"th{background-color: #cccccc;" \
" border: 1px solid black}\n" \
"table{width: 100%%;" \
" border: 1px black solid;" \
" border-collapse:collapse}\n" \
".progress-outer{" \
"border: 1px solid black;" \
" height: 8px;" \
" width: 90%%}\n" \
".progress-inner{float: left;" \
" height: 8px;" \
" background: green}\n" \
".dlstatus{font-size: small;" \
" text-align: center}\n" \
"table#settings{background-color: #eee;"\
" border: 0px;" \
" margin: 15px;}\n" \
"table#settings td{border: 0px;}\n" \
"table#settings th{border: 0px;}\n" \
"table#settings tr{" \
" background: #f6f6f6;}\n" \
"table#settings tr:nth-child(even){" \
" background: #eeeeee;}\n" \
"table#settings tr#modified{" \
" background: #FFFFBA;}\n" \
"table#settings tr#modified:nth-child(even){" \
" background: #ffffA0;}\n" \
"</style>\n"
int js_show_wl(struct tab *, struct karg *);
int pl_show_wl(struct tab *, struct karg *);
int https_show_wl(struct tab *, struct karg *);
int xtp_page_set(struct tab *, struct karg *);
int xtp_page_rt(struct tab *, struct karg *);
int marco(struct tab *, struct karg *);
int startpage(struct tab *, struct karg *);
const char * marco_message(int *);
void update_cookie_tabs(struct tab *apart_from);
int about_webkit(struct tab *, struct karg *);
int allthethings(struct tab *, struct karg *);
/*
* If you change the index of any of these, correct the
* XT_XTP_TAB_MEANING_* macros in xombrero.h!
*/
struct about_type about_list[] = {
{ XT_URI_ABOUT_ABOUT, xtp_page_ab },
{ XT_URI_ABOUT_ALLTHETHINGS, allthethings },
{ XT_URI_ABOUT_BLANK, blank },
{ XT_URI_ABOUT_CERTS, ca_cmd },
{ XT_URI_ABOUT_COOKIEWL, cookie_show_wl },
{ XT_URI_ABOUT_COOKIEJAR, xtp_page_cl },
{ XT_URI_ABOUT_DOWNLOADS, xtp_page_dl },
{ XT_URI_ABOUT_FAVORITES, xtp_page_fl },
{ XT_URI_ABOUT_HELP, help },
{ XT_URI_ABOUT_HISTORY, xtp_page_hl },
{ XT_URI_ABOUT_JSWL, js_show_wl },
{ XT_URI_ABOUT_SET, xtp_page_set },
{ XT_URI_ABOUT_STATS, stats },
{ XT_URI_ABOUT_MARCO, marco },
{ XT_URI_ABOUT_STARTPAGE, startpage },
{ XT_URI_ABOUT_PLUGINWL, pl_show_wl },
{ XT_URI_ABOUT_HTTPS, https_show_wl },
{ XT_URI_ABOUT_WEBKIT, about_webkit },
{ XT_URI_ABOUT_SEARCH, xtp_page_sl },
{ XT_URI_ABOUT_RUNTIME, xtp_page_rt },
{ XT_URI_ABOUT_SECVIOLATION, NULL },
};
struct search_type {
const char *name;
const char *url;
} search_list[] = {
{ "Google (SSL)", "https://encrypted.google.com/search?q=%s" },
{ "Bing", "http://www.bing.com/search?q=%s" },
{ "Yahoo", "http://search.yahoo.com/search?p=%s" },
{ "DuckDuckGo", "https://duckduckgo.com/?q=%s" },
{ "DuckDuckGo (HTML)", "https://duckduckgo.com/html?q=%s" },
{ "DuckDuckGo (Lite)", "https://duckduckgo.com/lite?q=%s" },
{ "Ixquick", "https://ixquick.com/do/search?q=%s" },
{ "Startpage", "https://startpage.com/do/search?q=%s" },
};
/*
* Session IDs.
* We use these to prevent people putting xxxt:// URLs on
* websites in the wild. We generate 8 bytes and represent in hex (16 chars)
*/
#define XT_XTP_SES_KEY_SZ 8
#define XT_XTP_SES_KEY_HEX_FMT \
"%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8
int updating_fl_tabs = 0;
int updating_dl_tabs = 0;
int updating_hl_tabs = 0;
int updating_cl_tabs = 0;
int updating_sl_tabs = 0;
int updating_sv_tabs = 0;
int updating_set_tabs = 0;
struct download_list downloads;
size_t
about_list_size(void)
{
return (LENGTH(about_list));
}
gchar *
get_html_page(gchar *title, gchar *body, gchar *head, bool addstyles)
{
gchar *r;
r = g_strdup_printf(XT_DOCTYPE XT_HTML_TAG
"<head>\n"
"<title>%s</title>\n"
"%s"
"%s"
"</head>\n"
"<body>\n"
"<h1>%s</h1>\n"
"%s\n</body>\n"
"</html>",
title,
addstyles ? XT_PAGE_STYLE : "",
head,
title,
body);
return (r);
}
/*
* Display a web page from a HTML string in memory, rather than from a URL
*/
void
load_webkit_string(struct tab *t, const char *str, gchar *title, int nohist)
{
char file[PATH_MAX];
int i, xtp_meaning;
if (g_signal_handler_is_connected(t->wv, t->progress_handle))
g_signal_handler_disconnect(t->wv, t->progress_handle);
/* we set this to indicate we want to manually do navaction */
if (t->bfl && !nohist) {
t->item = webkit_web_back_forward_list_get_current_item(t->bfl);
if (t->item)
g_object_ref(t->item);
}
xtp_meaning = XT_XTP_TAB_MEANING_NORMAL;
if (title) {
/* set xtp_meaning */
for (i = 0; i < LENGTH(about_list); i++)
if (!strcmp(title, about_list[i].name)) {
xtp_meaning = i;
break;
}
webkit_web_view_load_string(t->wv, str, NULL, encoding,
XT_XTP_STR);
#if GTK_CHECK_VERSION(2, 20, 0)
gtk_spinner_stop(GTK_SPINNER(t->spinner));
gtk_widget_hide(t->spinner);
#endif
snprintf(file, sizeof file, "%s" PS "%s", resource_dir, icons[0]);
xt_icon_from_file(t, file);
}
set_xtp_meaning(t, xtp_meaning);
t->progress_handle = g_signal_connect(t->wv,
"notify::progress", G_CALLBACK(webview_progress_changed_cb), t);
}
int
blank(struct tab *t, struct karg *args)
{
if (t == NULL)
show_oops(NULL, "blank invalid parameters");
load_webkit_string(t, "", XT_URI_ABOUT_BLANK, 0);
return (0);
}
int
help(struct tab *t, struct karg *args)
{
char *page, *head, *body;
if (t == NULL)
show_oops(NULL, "help invalid parameters");
head = "<meta http-equiv=\"REFRESH\" content=\"0;"
"url=https://opensource.conformal.com/cgi-bin/man-cgi?xombrero\">"
"</head>\n";
body = "xombrero man page <a href=\"https://opensource.conformal.com/"
"cgi-bin/man-cgi?xombrero\">https://opensource.conformal.com/"
"cgi-bin/man-cgi?xombrero</a>";
page = get_html_page(XT_NAME, body, head, FALSE);
load_webkit_string(t, page, XT_URI_ABOUT_HELP, 0);
g_free(page);
return (0);
}
int
stats(struct tab *t, struct karg *args)
{
char *page, *body, *s, line[64 * 1024];
uint64_t line_count = 0;
FILE *r_cookie_f;
if (t == NULL)
show_oops(NULL, "stats invalid parameters");
line[0] = '\0';
if (save_rejected_cookies) {
if ((r_cookie_f = fopen(rc_fname, "r"))) {
for (;;) {
s = fgets(line, sizeof line, r_cookie_f);
if (s == NULL || feof(r_cookie_f) ||
ferror(r_cookie_f))
break;
line_count++;
}
fclose(r_cookie_f);
snprintf(line, sizeof line,
"<br/>Cookies blocked(*) total: %" PRIu64,
line_count);
} else
show_oops(t, "Can't open blocked cookies file: %s",
strerror(errno));
}
body = g_strdup_printf(
"Cookies blocked(*) this session: %" PRIu64
"%s"
"<p><small><b>*</b> results vary based on settings</small></p>",
blocked_cookies,
line);
page = get_html_page("Statistics", body, "", 0);
g_free(body);
load_webkit_string(t, page, XT_URI_ABOUT_STATS, 0);
g_free(page);
return (0);
}
void
show_certs(struct tab *t, gnutls_x509_crt_t *certs,
size_t cert_count, char *title)
{
gnutls_datum_t *cinfo;
char *tmp, *body;
int i;
body = g_strdup("");
for (i = 0; i < cert_count; i++) {
cinfo = gnutls_malloc(sizeof *cinfo);
if (gnutls_x509_crt_print(certs[i], GNUTLS_CRT_PRINT_FULL,
cinfo)) {
gnutls_free(cinfo);
g_free(body);
return;
}
tmp = body;
body = g_strdup_printf("%s<h2>Cert #%d</h2><pre>%s</pre>",
body, i, cinfo->data);
gnutls_free(cinfo);
g_free(tmp);
}
tmp = get_html_page(title, body, "", 0);
g_free(body);
load_webkit_string(t, tmp, XT_URI_ABOUT_CERTS, 0);
g_free(tmp);
}
int
ca_cmd(struct tab *t, struct karg *args)
{
FILE *f = NULL;
int rv = 1, certs_read;
unsigned int certs = 0;
struct stat sb;
gnutls_datum_t dt;
gnutls_x509_crt_t *c = NULL;
char *certs_buf = NULL, *s;
if ((f = fopen(ssl_ca_file, "r")) == NULL) {
show_oops(t, "Can't open CA file: %s", ssl_ca_file);
return (1);
}
if (fstat(fileno(f), &sb) == -1) {
show_oops(t, "Can't stat CA file: %s", ssl_ca_file);
goto done;
}
certs_buf = g_malloc(sb.st_size + 1);
if (fread(certs_buf, 1, sb.st_size, f) != sb.st_size) {
show_oops(t, "Can't read CA file: %s", strerror(errno));
goto done;
}
certs_buf[sb.st_size] = '\0';
s = certs_buf;
while ((s = strstr(s, "BEGIN CERTIFICATE"))) {
certs++;
s += strlen("BEGIN CERTIFICATE");
}
bzero(&dt, sizeof dt);
dt.data = (unsigned char *)certs_buf;
dt.size = sb.st_size;
c = gnutls_malloc(sizeof(*c) * certs);
certs_read = gnutls_x509_crt_list_import(c, &certs, &dt,
GNUTLS_X509_FMT_PEM, 0);
if (certs_read <= 0) {
show_oops(t, "No cert(s) available");
goto done;
}
show_certs(t, c, certs_read, "Certificate Authority Certificates");
done:
if (c)
gnutls_free(c);
if (certs_buf)
g_free(certs_buf);
if (f)
fclose(f);
return (rv);
}
int
cookie_show_wl(struct tab *t, struct karg *args)
{
args->i = XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION;
wl_show(t, args, "Cookie White List", &c_wl);
return (0);
}
int
js_show_wl(struct tab *t, struct karg *args)
{
args->i = XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION;
wl_show(t, args, "JavaScript White List", &js_wl);
return (0);
}
int
cookie_cmd(struct tab *t, struct karg *args)
{
if (args->i & XT_SHOW)
wl_show(t, args, "Cookie White List", &c_wl);
else if (args->i & XT_WL_TOGGLE) {
args->i |= XT_WL_RELOAD;
toggle_cwl(t, args);
} else if (args->i & XT_SAVE) {
args->i |= XT_WL_RELOAD;
wl_save(t, args, XT_WL_COOKIE);
} else if (args->i & XT_DELETE) {
remove_cookie_all();
update_cookie_tabs(NULL);
}
return (0);
}
int
js_cmd(struct tab *t, struct karg *args)
{
if (args->i & XT_SHOW)
wl_show(t, args, "JavaScript White List", &js_wl);
else if (args->i & XT_SAVE) {
args->i |= XT_WL_RELOAD;
wl_save(t, args, XT_WL_JAVASCRIPT);
} else if (args->i & XT_WL_TOGGLE) {
args->i |= XT_WL_RELOAD;
toggle_js(t, args);
} else if (args->i & XT_DELETE)
show_oops(t, "'js delete' currently unimplemented");
return (0);
}
int
pl_show_wl(struct tab *t, struct karg *args)
{
args->i = XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION;
wl_show(t, args, "Plugin White List", &pl_wl);
return (0);
}
int
pl_cmd(struct tab *t, struct karg *args)
{
if (args->i & XT_SHOW)
wl_show(t, args, "Plugin White List", &pl_wl);
else if (args->i & XT_SAVE) {
args->i |= XT_WL_RELOAD;
wl_save(t, args, XT_WL_PLUGIN);
} else if (args->i & XT_WL_TOGGLE) {
args->i |= XT_WL_RELOAD;
toggle_pl(t, args);
} else if (args->i & XT_DELETE)
show_oops(t, "'plugin delete' currently unimplemented");
return (0);
}
int
https_show_wl(struct tab *t, struct karg *args)
{
args->i = XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION;
wl_show(t, args, "HTTPS Force List", &force_https);
return (0);
}
int
https_cmd(struct tab *t, struct karg *args)
{
if (args->i & XT_SHOW)
wl_show(t, args, "HTTPS Force List", &force_https);
else if (args->i & XT_SAVE) {
args->i |= XT_WL_RELOAD;
wl_save(t, args, XT_WL_HTTPS);
} else if (args->i & XT_WL_TOGGLE) {
args->i |= XT_WL_RELOAD;
toggle_force_https(t, args);
} else if (args->i & XT_DELETE)
show_oops(t, "https delete' currently unimplemented");
return (0);
}
/*
* cancel, remove, etc. downloads
*/
void
xtp_handle_dl(struct tab *t, uint8_t cmd, int id, const char *query)
{
struct download find, *d = NULL;
#ifndef __MINGW32__
char *file = NULL;
const char *uri = NULL;
#endif
DNPRINTF(XT_D_DOWNLOAD, "download control: cmd %d, id %d\n", cmd, id);
/* some commands require a valid download id */
if (cmd != XT_XTP_DL_LIST) {
/* lookup download in question */
find.id = id;
d = RB_FIND(download_list, &downloads, &find);
if (d == NULL) {
show_oops(t, "%s: no such download", __func__);
return;
}
}
/* decide what to do */
switch (cmd) {
case XT_XTP_DL_START:
/* our downloads always needs to be
* restarted if called from here
*/
download_start(t, d, XT_DL_RESTART);
break;
case XT_XTP_DL_CANCEL:
webkit_download_cancel(d->download);
g_object_unref(d->download);
RB_REMOVE(download_list, &downloads, d);
break;
case XT_XTP_DL_UNLINK:
#ifdef __MINGW32__
/* XXX uri's aren't handled properly on windows? */
unlink(webkit_download_get_destination_uri(d->download));
#else
uri = webkit_download_get_destination_uri(d->download);
if ((file = g_filename_from_uri(uri, NULL, NULL)) != NULL) {
unlink(file);
g_free(file);
}
#endif
/* FALLTHROUGH */
case XT_XTP_DL_REMOVE:
webkit_download_cancel(d->download); /* just incase */
g_object_unref(d->download);
RB_REMOVE(download_list, &downloads, d);
break;
case XT_XTP_DL_LIST:
/* Nothing */
break;
default:
show_oops(t, "%s: unknown command", __func__);
break;
};
xtp_page_dl(t, NULL);
}
void
xtp_handle_hl(struct tab *t, uint8_t cmd, int id, const char *query)
{
struct history *h, *next, *ht;
int i = 1;
switch (cmd) {
case XT_XTP_HL_REMOVE:
/* walk backwards, as listed in reverse */
for (h = RB_MAX(history_list, &hl); h != NULL; h = next) {
next = RB_PREV(history_list, &hl, h);
if (id == i) {
RB_REMOVE(history_list, &hl, h);
g_free((gpointer) h->title);
g_free((gpointer) h->uri);
g_free(h);
break;
}
i++;
}
break;
case XT_XTP_HL_REMOVE_ALL:
RB_FOREACH_SAFE(h, history_list, &hl, ht)
RB_REMOVE(history_list, &hl, h);
break;
case XT_XTP_HL_LIST:
/* Nothing - just xtp_page_hl() below */
break;
default:
show_oops(t, "%s: unknown command", __func__);
break;
};
xtp_page_hl(t, NULL);
}
/* remove a favorite */
void
remove_favorite(struct tab *t, int index)
{
char file[PATH_MAX], *title, *uri = NULL;
char *new_favs, *tmp;
FILE *f;
int i;
size_t len, lineno;
/* open favorites */
snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_FAVS_FILE);
if ((f = fopen(file, "r")) == NULL) {
show_oops(t, "%s: can't open favorites: %s",
__func__, strerror(errno));
return;
}
/* build a string which will become the new favorites file */
new_favs = g_strdup("");
for (i = 1;;) {
if ((title = fparseln(f, &len, &lineno, NULL, 0)) == NULL)
if (feof(f) || ferror(f))
break;
/* XXX THIS IS NOT THE RIGHT HEURISTIC */
if (len == 0) {
free(title);
title = NULL;
continue;
}
if ((uri = fparseln(f, &len, &lineno, NULL, 0)) == NULL) {
if (feof(f) || ferror(f)) {
show_oops(t, "%s: can't parse favorites %s",
__func__, strerror(errno));
goto clean;
}
}
/* as long as this isn't the one we are deleting add to file */
if (i != index) {
tmp = new_favs;
new_favs = g_strdup_printf("%s%s\n%s\n",
new_favs, title, uri);
g_free(tmp);
}
free(uri);
uri = NULL;
free(title);
title = NULL;
i++;
}
fclose(f);
/* write back new favorites file */
if ((f = fopen(file, "w")) == NULL) {
show_oops(t, "%s: can't open favorites: %s",
__func__, strerror(errno));
goto clean;
}
if (fwrite(new_favs, strlen(new_favs), 1, f) != 1)
show_oops(t, "%s: can't fwrite", __func__);
fclose(f);
clean:
if (uri)
free(uri);
if (title)
free(title);
g_free(new_favs);
}
int
add_favorite(struct tab *t, struct karg *args)
{
char file[PATH_MAX];
FILE *f;
char *line = NULL;
size_t urilen, linelen;
gchar *argtitle = NULL;
const gchar *uri, *title;
if (t == NULL)
return (1);
/* don't allow adding of xtp pages to favorites */
if (t->xtp_meaning != XT_XTP_TAB_MEANING_NORMAL) {
show_oops(t, "%s: can't add xtp pages to favorites", __func__);
return (1);
}
snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_FAVS_FILE);
if ((f = fopen(file, "r+")) == NULL) {
show_oops(t, "Can't open favorites file: %s", strerror(errno));
return (1);
}
if (args->s && strlen(g_strstrip(args->s)))
argtitle = html_escape(g_strstrip(args->s));
title = argtitle ? argtitle : get_title(t, FALSE);
uri = get_uri(t);
if (title == NULL || uri == NULL) {
show_oops(t, "can't add page to favorites");
goto done;
}
urilen = strlen(uri);
for (;;) {
if ((line = fparseln(f, &linelen, NULL, NULL, 0)) == NULL) {
if (feof(f))
break;
else {
show_oops(t, "Error reading favorites file: %s",
strerror(errno));
goto done;
}
}
if (linelen == urilen && !strcmp(line, uri))
goto done;
free(line);
line = NULL;
}
fprintf(f, "\n%s\n%s", title, uri);
done:
if (argtitle)
g_free(argtitle);
if (line)
free(line);
fclose(f);
update_favorite_tabs(NULL);
return (0);
}
char *
search_engine_add(char *body, const char *name, const char *url,
const char *key, int select)
{
char *b = body;
body = g_strdup_printf("%s<tr>"
"<td>%s</td>"
"<td>%s</td>"
"<td style='text-align: center'>"
"<a href='%s%d/%s/%d/%d'>[ Select ]</a></td>"
"</tr>\n",
body,
name,
url,
XT_XTP_STR, XT_XTP_SL, key, XT_XTP_SL_SET, select);
g_free(b);
return (body);
}
void
xtp_handle_ab(struct tab *t, uint8_t cmd, int arg, const char *query)
{
char config[PATH_MAX];
char *cmdstr;
char **sv;
switch (cmd) {
case XT_XTP_AB_EDIT_CONF:
if (external_editor == NULL || strlen(external_editor) == 0) {
show_oops(t, "external_editor is unset");
break;
}
snprintf(config, sizeof config, "%s" PS ".%s", pwd->pw_dir,
XT_CONF_FILE);
sv = g_strsplit(external_editor, "<file>", -1);
cmdstr = g_strjoinv(config, sv);
g_strfreev(sv);
sv = g_strsplit_set(cmdstr, " \t", -1);
if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
NULL, NULL))
show_oops(t, "%s: could not spawn process", __func__);
g_strfreev(sv);
g_free(cmdstr);
break;
default:
show_oops(t, "%s, invalid about command", __func__);
break;
};
xtp_page_ab(t, NULL);
}
void
xtp_handle_fl(struct tab *t, uint8_t cmd, int arg, const char *query)
{
struct karg args = {0};
switch (cmd) {
case XT_XTP_FL_LIST:
/* nothing, just the below call to xtp_page_fl() */
break;
case XT_XTP_FL_REMOVE:
remove_favorite(t, arg);
args.i = XT_DELETE;
break;
default:
show_oops(t, "%s: invalid favorites command", __func__);
break;
};
xtp_page_fl(t, &args);
}
void
xtp_handle_cl(struct tab *t, uint8_t cmd, int arg, const char *query)
{
switch (cmd) {
case XT_XTP_CL_LIST:
/* nothing, just xtp_page_cl() */
break;
case XT_XTP_CL_REMOVE:
remove_cookie(arg);
break;
case XT_XTP_CL_REMOVE_DOMAIN:
remove_cookie_domain(arg);
break;
case XT_XTP_CL_REMOVE_ALL:
remove_cookie_all();
break;
default:
show_oops(t, "%s: unknown cookie xtp command", __func__);
break;
};
xtp_page_cl(t, NULL);
}
void
xtp_handle_sl(struct tab *t, uint8_t cmd, int arg, const char *query)
{
const char *search;
char *enc_search, *uri;
char **sv;
switch (cmd) {
case XT_XTP_SL_SET:
set_search_string((char *)search_list[arg].url);
if (save_runtime_setting("search_string", search_list[arg].url))
show_oops(t, "could not set search_string in runtime");
break;
default:
show_oops(t, "%s: unknown search xtp command", __func__);
break;
};
search = gtk_entry_get_text(GTK_ENTRY(t->search_entry)); /* static */
enc_search = soup_uri_encode(search, XT_RESERVED_CHARS);
sv = g_strsplit(search_string, "%s", 2);
uri = g_strjoinv(enc_search, sv);
load_uri(t, uri);
g_free(enc_search);
g_strfreev(sv);
g_free(uri);
}
void
xtp_handle_sv(struct tab *t, uint8_t cmd, int id, const char *query)
{
SoupURI *soupuri = NULL;
struct karg args = {0};
struct secviolation find, *sv;
find.xtp_arg = id;
if ((sv = RB_FIND(secviolation_list, &svl, &find)) == NULL)
return;
args.ptr = (void *)sv->t;
args.s = sv->uri;
switch (cmd) {
case XT_XTP_SV_SHOW_NEW_CERT:
args.i = XT_SHOW;
if (cert_cmd(t, &args)) {
xtp_page_sv(t, &args);
return;
}
break;
case XT_XTP_SV_SHOW_CACHED_CERT:
args.i = XT_CACHE | XT_SHOW;
if (cert_cmd(t, &args)) {
xtp_page_sv(t, &args);
return;
}
break;
case XT_XTP_SV_ALLOW_SESSION:
soupuri = soup_uri_new(sv->uri);
wl_add(soupuri->host, &svil, 0);
load_uri(t, sv->uri);
focus_webview(t);
break;
case XT_XTP_SV_CACHE:
args.i = XT_CACHE;
if (cert_cmd(t, &args)) {
xtp_page_sv(t, &args);
return;
}
load_uri(t, sv->uri);
focus_webview(t);
break;
default:
show_oops(t, "%s: invalid secviolation command", __func__);
break;
};
g_free(sv->uri);
if (soupuri)
soup_uri_free(soupuri);
RB_REMOVE(secviolation_list, &svl, sv);
}
void
xtp_handle_rt(struct tab *t, uint8_t cmd, int id, const char *query)
{
struct set_reject *sr;
GHashTable *new_settings = NULL;
int modify;
char *val, *curval, *s;
int i = 0;
switch (cmd) {
case XT_XTP_RT_SAVE:
if (query == NULL)
break;
new_settings = soup_form_decode(query);
for (i = 0; i < get_settings_size(); ++i) {
if (!rs[i].activate)
continue;
val = (char *)g_hash_table_lookup(new_settings,
rs[i].name);
modify = 0;
switch (rs[i].type) {
case XT_S_INT: /* FALLTHROUGH */
case XT_S_BOOL:
if (atoi(val) != *rs[i].ival)
modify = 1;
break;
case XT_S_DOUBLE:
if (atof(val) < (*rs[i].dval - 0.0001) ||
atof(val) > (*rs[i].dval + 0.0001))
modify = 1;
break;
case XT_S_STR:
s = (rs[i].sval == NULL || *rs[i].sval == NULL)
? "" : *rs[i].sval;
if (rs[i].sval && g_strcmp0(val, s))
modify = 1;
else if (rs[i].s && rs[i].s->get) {
curval = rs[i].s->get(NULL);
if (g_strcmp0(val, curval))
modify = 1;
g_free(curval);
}
break;
case XT_S_INVALID: /* FALLTHROUGH */
default:
break;
}
if (rs[i].activate(val)) {
sr = g_malloc(sizeof *sr);
sr->name = g_strdup(rs[i].name);
sr->value = g_strdup(val);
TAILQ_INSERT_TAIL(&srl, sr, entry);
continue;
}
if (modify)
if (save_runtime_setting(rs[i].name, val))
show_oops(t, "error");
}
break;
default:
show_oops(t, "%s: invalid set command", __func__);
break;
}
if (new_settings)
g_hash_table_destroy(new_settings);
xtp_page_rt(t, NULL);
}
/* link an XTP class to it's session key and handler function */
struct xtp_despatch {
uint8_t xtp_class;
void (*handle_func)(struct tab *, uint8_t, int,
const char *query);