/
callbacks.c
1825 lines (1451 loc) · 48.9 KB
/
callbacks.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 2001 by Sean Conner. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Comments, questions and criticisms can be sent to: sean@conman.org
*
************************************************/
#ifndef __GNUC__
# define __attribute__(x)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <cgilib6/conf.h>
#include <cgilib6/htmltok.h>
#include <cgilib6/chunk.h>
#include <cgilib6/util.h>
#include "conf.h"
#include "blog.h"
#include "timeutil.h"
#include "frontend.h"
#include "fix.h"
#include "blogutil.h"
#include "conversion.h"
#include "globals.h"
#ifdef EMAIL_NOTIFY
# include <gdbm.h>
#endif
/*****************************************************************/
static void cb_ad (FILE *,void *);
static void cb_ad_content (FILE *,void *);
static void cb_atom_categories (FILE *,void *);
static void cb_atom_category (FILE *,void *);
static void cb_atom_entry (FILE *,void *);
static void cb_begin_year (FILE *,void *);
static void cb_blog_adtag (FILE *,void *);
static void cb_blog_adtag_entity (FILE *,void *);
static void cb_blog_author (FILE *,void *);
static void cb_blog_author_email (FILE *,void *);
static void cb_blog_class (FILE *,void *);
static void cb_blog_description (FILE *,void *);
static void cb_blog_name (FILE *,void *);
static void cb_blog_script (FILE *,void *);
static void cb_blog_title (FILE *,void *);
static void cb_blog_url (FILE *,void *);
static void cb_blog_url_home (FILE *,void *);
static void cb_comments (FILE *,void *);
static void cb_comments_body (FILE *,void *);
static void cb_comments_check (FILE *,void *);
static void cb_comments_filename (FILE *,void *);
static void cb_cond_hr (FILE *,void *);
static void cb_date_day (FILE *,void *);
static void cb_date_day_normal (FILE *,void *);
static void cb_date_day_url (FILE *,void *);
static void cb_edit (FILE *,void *);
static void cb_edit_adtag (FILE *,void *);
static void cb_edit_author (FILE *,void *);
static void cb_edit_body (FILE *,void *);
static void cb_edit_class (FILE *,void *);
static void cb_edit_date (FILE *,void *);
static void cb_edit_email (FILE *,void *);
static void cb_edit_filter (FILE *,void *);
static void cb_edit_status (FILE *,void *);
static void cb_edit_title (FILE *,void *);
static void cb_entry (FILE *,void *);
static void cb_entry_adtag (FILE *,void *);
static void cb_entry_author (FILE *,void *);
static void cb_entry_body (FILE *,void *);
static void cb_entry_body_entified (FILE *,void *);
static void cb_entry_body_jsonified (FILE *,void *);
static void cb_entry_class (FILE *,void *);
static void cb_entry_class_jsonified (FILE *,void *);
static void cb_entry_cond_author (FILE *,void *);
static void cb_entry_cond_date (FILE *,void *);
static void cb_entry_date (FILE *,void *);
static void cb_entry_description (FILE *,void *);
static void cb_entry_id (FILE *,void *);
static void cb_entry_name (FILE *,void *);
static void cb_entry_path (FILE *,void *);
static void cb_entry_pubdate (FILE *,void *);
static void cb_entry_status (FILE *,void *);
static void cb_entry_title (FILE *,void *);
static void cb_entry_url (FILE *,void *);
static void cb_generator (FILE *,void *);
static void cb_json_item (FILE *,void *);
static void cb_navigation_bar (FILE *,void *);
static void cb_navigation_bar_next (FILE *,void *);
static void cb_navigation_bar_prev (FILE *,void *);
static void cb_navigation_current (FILE *,void *);
static void cb_navigation_current_url (FILE *,void *);
static void cb_navigation_first_title (FILE *,void *);
static void cb_navigation_first_url (FILE *,void *);
static void cb_navigation_last_title (FILE *,void *);
static void cb_navigation_last_url (FILE *,void *);
static void cb_navigation_link (FILE *,void *);
static void cb_navigation_link_next (FILE *,void *);
static void cb_navigation_link_prev (FILE *,void *);
static void cb_navigation_next_title (FILE *,void *);
static void cb_navigation_next_url (FILE *,void *);
static void cb_navigation_prev_title (FILE *,void *);
static void cb_navigation_prev_url (FILE *,void *);
static void cb_now_year (FILE *,void *);
static void cb_overview (FILE *,void *);
static void cb_overview_date (FILE *,void *);
static void cb_overview_list (FILE *,void *);
static void cb_request_url (FILE *,void *);
static void cb_robots_index (FILE *,void *);
static void cb_rss_item (FILE *,void *);
static void cb_rss_item_url (FILE *,void *);
static void cb_rss_pubdate (FILE *,void *);
static void cb_rss_url (FILE *,void *);
static void cb_update_time (FILE *,void *);
static void cb_update_type (FILE *,void *);
static void cb_xyzzy (FILE *,void *);
static void print_nav_url (FILE *,struct btm const *,int);
static void print_nav_title (FILE *,struct btm const *,int);
static void print_nav_name (FILE *,struct btm const *,int,char);
static void fixup_uri (BlogEntry *,HtmlToken,char const *);
static void handle_aflinks (HtmlToken,char const *);
/************************************************************************/
/*--------------------------------
; the following table needs to
; be in alphabetical order
;--------------------------------*/
static struct chunk_callback const m_callbacks[] =
{
{ "ad" , cb_ad } ,
{ "ad.content" , cb_ad_content } ,
{ "atom.categories" , cb_atom_categories } ,
{ "atom.category" , cb_atom_category } ,
{ "atom.entry" , cb_atom_entry } ,
{ "begin.year" , cb_begin_year } ,
{ "blog.adtag" , cb_blog_adtag } ,
{ "blog.adtag.entity" , cb_blog_adtag_entity } ,
{ "blog.author" , cb_blog_author } ,
{ "blog.author.email" , cb_blog_author_email } ,
{ "blog.class" , cb_blog_class } ,
{ "blog.description" , cb_blog_description } ,
{ "blog.name" , cb_blog_name } ,
{ "blog.script" , cb_blog_script } ,
{ "blog.title" , cb_blog_title } ,
{ "blog.url" , cb_blog_url } ,
{ "blog.url.home" , cb_blog_url_home } ,
{ "comments" , cb_comments } ,
{ "comments.body" , cb_comments_body } ,
{ "comments.check" , cb_comments_check } ,
{ "comments.filename" , cb_comments_filename } ,
{ "cond.hr" , cb_cond_hr } ,
{ "date.day" , cb_date_day } ,
{ "date.day.normal" , cb_date_day_normal } ,
{ "date.day.url" , cb_date_day_url } ,
{ "edit" , cb_edit } ,
{ "edit.adtag" , cb_edit_adtag } ,
{ "edit.author" , cb_edit_author } ,
{ "edit.body" , cb_edit_body } ,
{ "edit.class" , cb_edit_class } ,
{ "edit.date" , cb_edit_date } ,
{ "edit.email" , cb_edit_email } ,
{ "edit.filter" , cb_edit_filter } ,
{ "edit.status" , cb_edit_status } ,
{ "edit.title" , cb_edit_title } ,
{ "entry" , cb_entry } ,
{ "entry.adtag" , cb_entry_adtag } ,
{ "entry.author" , cb_entry_author } ,
{ "entry.body" , cb_entry_body } ,
{ "entry.body.entified" , cb_entry_body_entified } ,
{ "entry.body.jsonified" , cb_entry_body_jsonified } ,
{ "entry.class" , cb_entry_class } ,
{ "entry.class.jsonified" , cb_entry_class_jsonified } ,
{ "entry.cond.author" , cb_entry_cond_author } ,
{ "entry.cond.date" , cb_entry_cond_date } ,
{ "entry.date" , cb_entry_date } ,
{ "entry.description" , cb_entry_description } ,
{ "entry.id" , cb_entry_id } ,
{ "entry.name" , cb_entry_name } ,
{ "entry.path" , cb_entry_path } ,
{ "entry.pubdate" , cb_entry_pubdate } ,
{ "entry.status" , cb_entry_status } ,
{ "entry.title" , cb_entry_title } ,
{ "entry.url" , cb_entry_url } ,
{ "generator" , cb_generator } ,
{ "json.item" , cb_json_item } ,
{ "navigation.bar" , cb_navigation_bar } ,
{ "navigation.bar.next" , cb_navigation_bar_next } ,
{ "navigation.bar.prev" , cb_navigation_bar_prev } ,
{ "navigation.current" , cb_navigation_current } ,
{ "navigation.current.url" , cb_navigation_current_url } ,
{ "navigation.first.title" , cb_navigation_first_title } ,
{ "navigation.first.url" , cb_navigation_first_url } ,
{ "navigation.last.title" , cb_navigation_last_title } ,
{ "navigation.last.url" , cb_navigation_last_url } ,
{ "navigation.link" , cb_navigation_link } ,
{ "navigation.link.next" , cb_navigation_link_next } ,
{ "navigation.link.prev" , cb_navigation_link_prev } ,
{ "navigation.next.title" , cb_navigation_next_title } ,
{ "navigation.next.url" , cb_navigation_next_url } ,
{ "navigation.prev.title" , cb_navigation_prev_title } ,
{ "navigation.prev.url" , cb_navigation_prev_url } ,
{ "now.year" , cb_now_year } ,
{ "overview" , cb_overview } ,
{ "overview.date" , cb_overview_date } ,
{ "overview.list" , cb_overview_list } ,
{ "request.url" , cb_request_url } ,
{ "robots.index" , cb_robots_index } ,
{ "rss.item" , cb_rss_item } ,
{ "rss.item.url" , cb_rss_item_url } ,
{ "rss.pubdate" , cb_rss_pubdate } ,
{ "rss.url" , cb_rss_url } ,
{ "update.time" , cb_update_time } ,
{ "update.type" , cb_update_type } ,
{ "xyzzy" , cb_xyzzy } ,
};
static size_t const m_cbnum = sizeof(m_callbacks) / sizeof(struct chunk_callback);
/*************************************************************************/
void generic_cb(char const *which,FILE *out,void *data)
{
Chunk templates;
assert(which != NULL);
assert(out != NULL);
templates = ChunkNew(g_templates,m_callbacks,m_cbnum);
ChunkProcess(templates,which,out,data);
ChunkFree(templates);
fflush(out);
}
/*********************************************************************/
static void cb_ad(FILE *out,void *data)
{
struct callback_data *cbd = data;
char fname[FILENAME_MAX];
assert(out != NULL);
assert(data != NULL);
assert(cbd->entry->valid);
sprintf(
fname,
"%04d/%02d/%02d/%d.ad",
cbd->entry->when.year,
cbd->entry->when.month,
cbd->entry->when.day,
cbd->entry->when.part
);
cbd->ad = fopen(fname,"r");
if (cbd->ad == NULL) return;
generic_cb("ad",out,data);
fclose(cbd->ad);
cbd->ad = NULL;
}
/**********************************************************************/
static void cb_ad_content(FILE *out,void *data)
{
struct callback_data *cbd = data;
assert(out != NULL);
assert(data != NULL);
/*------------------------------------------
; we might also do a generic_cb() here, but
; I would need one that takes a FILE *
; object ... just an idea ...
;-------------------------------------------*/
fcopy(out,cbd->ad);
}
/*********************************************************************/
static void cb_blog_script(FILE *out,void *data __attribute__((unused)))
{
char *script;
assert(out != NULL);
script = getenv("SCRIPT_NAME");
if (script)
fputs(script,out);
}
/**********************************************************************/
static void cb_blog_url(FILE *out,void *data)
{
struct callback_data *cbd = data;
assert(out != NULL);
assert(data != NULL);
print_nav_url(out,&cbd->entry->when,UNIT_DAY);
}
/*************************************************************************/
static void cb_blog_url_home(FILE *out,void *data __attribute__((unused)))
{
assert(out != NULL);
fputs(c_fullbaseurl,out);
}
/**********************************************************************/
static void cb_blog_name(FILE *out,void *data __attribute__((unused)))
{
assert(out != NULL);
fputs(c_name,out);
}
/*********************************************************************/
static void cb_blog_title(FILE *out,void *data)
{
if (gd.navunit == UNIT_PART)
{
assert(data != NULL);
struct callback_data *cbd = data;
BlogEntry *entry = (BlogEntry *)ListGetHead(&cbd->list);
if (NodeValid(&entry->node) && entry->valid)
fprintf(out,"%s - ",entry->title);
}
fputs(c_name,out);
}
/*********************************************************************/
static void cb_blog_author(FILE *out,void *data __attribute__((unused)))
{
assert(out != NULL);
fputs(c_author,out);
}
/********************************************************************/
static void cb_blog_author_email(FILE *out,void *data __attribute__((unused)))
{
assert(out != NULL);
fputs(c_email,out);
}
/**********************************************************************/
static void cb_blog_class(FILE *out,void *data __attribute__((unused)))
{
assert(out != NULL);
fputs(c_class,out);
}
/**********************************************************************/
static void cb_blog_description(FILE *out,void *data __attribute__((unused)))
{
assert(out != NULL);
fputs(c_description,out);
}
/*********************************************************************/
static void cb_blog_adtag(FILE *out,void *data)
{
struct callback_data *cbd;
char *tag;
assert(out != NULL);
if (data == NULL)
tag = UrlEncodeString(c_adtag);
else
{
cbd = data;
tag = UrlEncodeString(cbd->adtag);
}
fputs(tag,out);
free(tag);
}
/*********************************************************************/
static void cb_blog_adtag_entity(FILE *out,void *data)
{
struct callback_data *cbd;
FILE *entityout;
char const *tag;
assert(out != NULL);
if (data == NULL)
tag = c_adtag;
else
{
cbd = data;
tag = cbd->adtag;
}
entityout = fentity_encode_onwrite(out);
if (entityout == NULL) return;
fputs(tag,entityout);
fclose(entityout);
}
/********************************************************************/
static void cb_entry(FILE *out,void *data)
{
struct callback_data *cbd = data;
BlogEntry *entry;
assert(out != NULL);
if (data == NULL)
{
if (gd.htmldump)
fcopy(out,gd.htmldump);
return;
}
if (gd.f.overview)
{
generic_cb("overview",out,data);
return;
}
for
(
entry = (BlogEntry *)ListRemHead(&cbd->list);
NodeValid(&entry->node);
entry = (BlogEntry *)ListRemHead(&cbd->list)
)
{
assert(entry->valid);
cbd->entry = entry;
generic_cb("entry",out,data);
cbd->last = entry->when;
BlogEntryFree(entry);
}
cbd->entry = NULL;
}
/**********************************************************************/
static void cb_entry_url(FILE *out,void *data)
{
struct callback_data *cbd = data;
BlogEntry *entry;
assert(out != NULL);
assert(data != NULL);
entry = cbd->entry;
assert(entry->valid);
print_nav_url(out,&entry->when,UNIT_PART);
}
/**********************************************************************/
static void cb_entry_id(FILE *out,void *data)
{
struct callback_data *cbd = data;
BlogEntry *entry;
assert(out != NULL);
assert(data != NULL);
entry = cbd->entry;
assert(entry->valid);
print_nav_name(out,&entry->when,UNIT_DAY,'-');
}
/**********************************************************************/
static void cb_entry_date(FILE *out,void *data)
{
struct callback_data *cbd = data;
assert(out != NULL);
assert(data != NULL);
print_nav_url(out,&cbd->entry->when,UNIT_DAY);
}
/*********************************************************************/
static void cb_entry_description(FILE *out,void *data)
{
char const *msg = c_description;
assert(out != NULL);
if (gd.navunit == UNIT_PART)
{
assert(data != NULL);
struct callback_data *cbd = data;
BlogEntry *entry = (BlogEntry *)ListGetHead(&cbd->list);
if (NodeValid(&entry->node) && entry->valid)
{
if (!empty_string(entry->status))
msg = entry->status;
else if (!empty_string(entry->title))
msg = entry->title;
}
}
fputs(msg,out);
}
/*********************************************************************/
static void cb_entry_pubdate(FILE *out,void *data)
{
struct callback_data *cbd = data;
BlogEntry *entry;
time_t ts;
struct tm *ptm;
char buffer[BUFSIZ];
assert(out != NULL);
assert(data != NULL);
entry = cbd->entry;
assert(entry->valid);
ts = entry->timestamp;
ptm = localtime(&ts);
sprintf(
buffer,
"%04d-%02d-%02dT%02d:%02d:%02d%+03d:%02d",
ptm->tm_year + 1900,
ptm->tm_mon + 1,
ptm->tm_mday,
ptm->tm_hour,
ptm->tm_min,
ptm->tm_sec,
c_tzhour,
c_tzmin
);
fputs(buffer,out);
}
/********************************************************************/
static void cb_entry_title(FILE *out,void *data)
{
struct callback_data *cbd = data;
assert(out != NULL);
assert(data != NULL);
assert(cbd->entry->valid);
fputs(cbd->entry->title,out);
}
/***********************************************************************/
static void cb_entry_class(FILE *out,void *data)
{
char const *msg = c_class;
assert(out != NULL);
if (data != NULL)
{
struct callback_data *cbd = data;
if (cbd->entry != NULL)
msg = cbd->entry->class;
else
{
if (gd.navunit == UNIT_PART)
{
BlogEntry *entry = (BlogEntry *)ListGetHead(&cbd->list);
if (NodeValid(&entry->node) && entry->valid)
if (!empty_string(entry->class))
msg = entry->class;
}
}
}
fputs(msg,out);
}
/***********************************************************************/
static void cb_entry_class_jsonified(FILE *out,void *data)
{
struct callback_data *cbd = data;
String *cats;
size_t num;
assert(out != NULL);
assert(data != NULL);
cats = tag_split(&num,cbd->entry->class);
for (size_t i = 0 ; i < num ; i++)
{
fputc('"',out);
FILE *jout = fjson_encode_onwrite(out);
if (jout)
{
fwrite(cats[i].d,cats[i].s,1,jout);
fclose(jout);
}
fputc('"',out);
if (i < num - 1)
fputc(',',out);
}
free(cats);
}
/***********************************************************************/
static void cb_entry_status(FILE *out,void *data)
{
struct callback_data *cbd = data;
assert(out != NULL);
assert(data != NULL);
fputs(cbd->entry->status,out);
}
/************************************************************************/
static void cb_entry_adtag(FILE *out,void *data __attribute__((unused)))
{
assert(out != NULL);
fputs("Soon young Skywalker. Soon.",out);
}
/**********************************************************************/
static void cb_entry_author(FILE *out,void *data)
{
struct callback_data *cbd = data;
assert(out != NULL);
assert(data != NULL);
assert(cbd->entry->valid);
fputs(cbd->entry->author,out);
}
/*************************************************************************/
static void handle_aflinks(HtmlToken token,char const *attrib)
{
struct pair *src;
assert(token != NULL);
assert(attrib != NULL);
src = HtmlParseGetPair(token,attrib);
if (src != NULL)
{
for (size_t i = 0 ; i < c_numaflinks ; i++)
{
if (strncmp(src->value,c_aflinks[i].proto,c_aflinks[i].psize) == 0)
{
char buffer[BUFSIZ];
struct pair *np;
snprintf(
buffer,
sizeof(buffer),
c_aflinks[i].format,
&src->value[c_aflinks[i].psize + 1]
);
np = PairCreate(attrib,buffer);
NodeInsert(&src->node,&np->node);
NodeRemove(&src->node);
PairFree(src);
return;
}
}
}
}
/*************************************************************************/
static void fixup_uri(BlogEntry *entry,HtmlToken token,char const *attrib)
{
struct pair *src;
struct pair *np;
assert(entry != NULL);
assert(entry->valid);
assert(token != NULL);
assert(attrib != NULL);
handle_aflinks(token,attrib);
src = HtmlParseGetPair(token,attrib);
if (
(src != NULL)
&& (src->value[0] != '/')
&& (src->value[0] != '#')
&& (strncmp(src->value,"http:",5) != 0)
&& (strncmp(src->value,"https:",6) != 0)
&& (strncmp(src->value,"gopher:",7) != 0)
&& (strncmp(src->value,"mailto:",7) != 0)
&& (strncmp(src->value,"ftp:",4) != 0)
&& (strncmp(src->value,"javascript:",11) != 0)
&& (strncmp(src->value,"nntp:",5) != 0)
&& (strncmp(src->value,"news:",5) != 0)
&& (strncmp(src->value,"file:",5) != 0)
)
{
char buffer[BUFSIZ];
char const *baseurl;
/*-----------------------------------------------------
; Which URL to use? Full or partial?
;-----------------------------------------------------*/
if (gd.f.fullurl)
baseurl = c_fullbaseurl;
else
baseurl = c_baseurl;
/*---------------------------------------------------------
; all this to reassign the value, without ``knowing'' how the pair stuff
; actually works. Perhaps add a PairReValue() call or something?
;
; Anyway, check to see if baseurl ends in a '/', and if not, we then add
; one, otherwise, don't.
;
; If baseurl ends in a '/', and src->value does not start with a '/', we
; end up with a double '//'. We need to make sure we canonicalize all
; partial urls to either end in a '/', or not end in a '/'.
;----------------------------------------------------------*/
if (src->value[0] == '/')
sprintf(buffer,"%s%s",baseurl,src->value);
else if (isdigit(src->value[0]))
sprintf(buffer,"%s/%s",baseurl,src->value);
else
sprintf(
buffer,
"%s/%04d/%02d/%02d/%s",
baseurl,
entry->when.year,
entry->when.month,
entry->when.day,
src->value
);
np = PairCreate(attrib,buffer);
NodeInsert(&src->node,&np->node);
NodeRemove(&src->node);
PairFree(src);
}
}
/***************************************************************/
static void cb_entry_body(FILE *out,void *data)
{
struct callback_data *cbd = data;
BlogEntry *entry;
FILE *in;
HtmlToken token;
int t;
assert(out != NULL);
assert(data != NULL);
entry = cbd->entry;
assert(entry->valid);
in = fmemopen(entry->body,strlen(entry->body),"r");
if (in == NULL) return;
token = HtmlParseNew(in);
if (token == NULL)
{
fclose(in);
return;
}
while((t = HtmlParseNext(token)) != T_EOF)
{
if (t == T_TAG)
{
/*---------------------------------------------
; checked in order of *my* usage.
;----------------------------------------------*/
if (strcmp(HtmlParseValue(token),"A") == 0)
fixup_uri(entry,token,"HREF");
else if (strcmp(HtmlParseValue(token),"BLOCKQUOTE") == 0)
fixup_uri(entry,token,"CITE");
else if (strcmp(HtmlParseValue(token),"IMG") == 0)
{
fixup_uri(entry,token,"SRC");
fixup_uri(entry,token,"LONGDESC");
fixup_uri(entry,token,"USEMAP");
}
else if (strcmp(HtmlParseValue(token),"Q") == 0)
fixup_uri(entry,token,"CITE");
else if (strcmp(HtmlParseValue(token),"INS") == 0)
fixup_uri(entry,token,"CITE");
else if (strcmp(HtmlParseValue(token),"DEL") == 0)
fixup_uri(entry,token,"CITE");
else if (strcmp(HtmlParseValue(token),"FORM") == 0)
fixup_uri(entry,token,"ACTION");
else if (strcmp(HtmlParseValue(token),"INPUT") == 0)
{
fixup_uri(entry,token,"SRC");
fixup_uri(entry,token,"USEMAP");
}
else if (strcmp(HtmlParseValue(token),"AREA") == 0)
fixup_uri(entry,token,"HREF");
else if (strcmp(HtmlParseValue(token),"OBJECT") == 0)
{
fixup_uri(entry,token,"CLASSID");
fixup_uri(entry,token,"CODEBASE");
fixup_uri(entry,token,"DATA");
fixup_uri(entry,token,"ARCHIVE");
fixup_uri(entry,token,"USEMAP");
}
/*-----------------------------------------------------
; elements that can only appear in the <HEAD> section
; so commented out for now but here for reference
;------------------------------------------------------*/
#if 0
else if (strcmp(HtmlParseValue(token),"LINK") == 0)
fixup_uri(entry,token,"HREF");
else if (strcmp(HtmlParseValue(token),"BASE") == 0)
fixup_uri(entry,token,"HREF");
else if (strcmp(HtmlParseValue(token),"HEAD") == 0)
fixup_uri(entry,token,"PROFILE");
else if (strcmp(HtmlParseValue(token),"SCRIPT") == 0)
{
fixup_uri(entry,token,"SRC");
fixup_uri(entry,token,"FOR");
}
#endif
HtmlParsePrintTag(token,out);
}
else if (t == T_STRING)
fputs(HtmlParseValue(token),out);
else if (t == T_COMMENT)
fprintf(out,"<!%s>",HtmlParseValue(token));
}
HtmlParseFree(token);
fclose(in);
}
/**********************************************************************/
static void cb_entry_body_entified(FILE *out,void *data)
{
FILE *eout;
assert(out != NULL);
eout = fentity_encode_onwrite(out);
if (eout == NULL) return;
cb_entry_body(eout,data);
fclose(eout);
}
/*********************************************************************/
static void cb_entry_body_jsonified(FILE *out,void *data)
{
FILE *jout;
assert(out != NULL);
jout = fjson_encode_onwrite(out);
if (jout == NULL) return;
cb_entry_body(jout,data);
fclose(jout);
}
/**********************************************************************/
static void cb_cond_hr(FILE *out,void *data)
{
struct callback_data *cbd = data;
BlogEntry *entry;
assert(out != NULL);
if (gd.f.navigation && (gd.navunit == UNIT_PART))
return;
assert(data != NULL);
entry = cbd->entry;
assert(entry->valid);
if (btm_cmp_date(&entry->when,&cbd->last) == 0)
{
if (entry->when.part != cbd->last.part)
fputs("<hr class=\"next\">",out);
}
}
/*********************************************************************/
static void cb_rss_pubdate(FILE *out,void *data __attribute__((unused)))
{
struct tm *ptm;
char buffer[BUFSIZ];
assert(out != NULL);
ptm = gmtime(&g_blog->tnow);
strftime(buffer,sizeof(buffer),"%a, %d %b %Y %H:%M:%S GMT",ptm);
fputs(buffer,out);
}
/********************************************************************/
static void cb_rss_url(FILE *out,void *data __attribute__((unused)))
{
assert(out != NULL);
fprintf(out,"%s/",c_fullbaseurl);
}
/*******************************************************************/
static void cb_rss_item(FILE *out,void *data)
{
struct callback_data *cbd = data;
BlogEntry *entry;
assert(out != NULL);
assert(data != NULL);
for
(
entry = (BlogEntry *)ListRemHead(&cbd->list);
NodeValid(&entry->node);
entry = (BlogEntry *)ListRemHead(&cbd->list)
)
{
assert(entry->valid);
cbd->entry = entry;
generic_cb("item",out,data);
cbd->last = entry->when;
BlogEntryFree(entry);
}
cbd->entry = NULL;
}
/******************************************************************/
static void cb_rss_item_url(FILE *out,void *data)
{