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
|
/*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "pages.h"
#include "config.h"
int
makedirectories(const char *basedir, const char *file)
{
/*
* make directory 'basedir'
* tokenise 'file' and make all directories leading to the final file inside 'basedir'
*/
// TODO: implement
char buffer[512];
struct stat sb = {0};
if(!(stat(output_dir, &sb) == 0) && !S_ISDIR(sb.st_mode))
{
fprintf(stderr, "directory '%s' does not exist, trying to make directory..\n", output_dir);
if (mkdir(output_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
{
fprintf(stderr, "unable to mkdir '%s', unrecoverable failure\n", output_dir);
return 0;
}
fprintf(stderr, "OK\n");
}
return 1;
}
int
createfile(const char *file, const char *template)
{
/*
* create file at location 'output_dir'/file' using the template 'template'
* overwrite if it exists
* assume the caller has put us in the base directory already
*/
/*
* FIXME: if trying to create a file with subdirectories it will not work
* one day tokenise the file path and make them
*/
if (!makedirectories(output_dir, file))
{
fprintf(stderr, "unable to create directories for '%s/%s', unrecoverable failure\n", output_dir, file);
return 0;
}
char filename[512] = {0};
snprintf(filename, 512, "%s%s", output_dir, file);
FILE *out = fopen(filename, "w");
if (!out)
{
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
fprintf(stderr, "unable to open file '%s', unrecoverable failure\n", filename);
return 0;
}
FILE *in = fopen(template, "r");
if (!in)
{
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
fprintf(stderr, "unable to open file '%s', unrecoverable failure\n", filename);
fclose(out);
return 0;
}
char c;
while ((c = fgetc(in)) != EOF)
{
fputc(c, out);
}
fclose(out);
fclose(in);
return 1;
}
long
findstring(const char *file, const char *str)
{
/*
* find first occurance of substring 'str' in file 'output_dir'/'file'
* return offset of string beginning
* -1 on error (including not found)
*
* return value is a long obtained by ftell(), set the position later using fseek()
* (use SEEK_SET as whence for beginning of file)
*/
long offset = -1;
char filename[512] = {0};
snprintf(filename, 512, "%s%s", output_dir, file);
FILE *in = fopen(filename, "r");
if (!in)
{
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
fprintf(stderr, "unable to open file '%s', unrecoverable failure\n", filename);
return -1;
}
/* hacky */
char buffer[1024] = {0};
char *strpos = NULL;
while (fgets(buffer, 1024, in) != NULL)
{
strpos = strstr(buffer, str);
if (strpos)
{
/* found our substring */
break;
}
memset(buffer, 0, 1024);
continue;
}
if (!strpos)
{
fprintf(stderr, "unable to find substring '%s' in '%s'\n", str, filename);
fclose(in);
return -1;
}
/* we have the line that holds our substring, find out where it is in the file */
long curpos = ftell(in);
if (!curpos)
{
fprintf(stderr, "ftell() failed\n");
fclose(in);
return -1;
}
// strpos-buffer = first byte of str in line
offset = (curpos - strlen(buffer)+1) + strpos-buffer;
fclose(in);
return offset;
}
int
deletebytes(const char *file, long offset, size_t bytes)
{
/*
* delete 'bytes' bytes from 'output_dir'/'file' starting at offset 'offset'
* return 1 on success. 0 on failure
*/
char filename[512] = {0};
snprintf(filename, 512, "%s%s", output_dir, file);
FILE *in = fopen(filename, "r");
if (!in)
{
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
fprintf(stderr, "unable to open file '%s', unrecoverable failure\n", filename);
return 0;
}
FILE *out = fopen("tmp.tmp", "w");
if (!out)
{
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
fprintf(stderr, "unable to open temporary file, unrecoverable failure\n");
fclose(in);
return 0;
}
long curpos = 1;
char c;
/* read bytes from 'in' and write them to 'out', skipping 'bytes' bytes starting at 'offset' */
while ((c = fgetc(in)) != EOF)
{
if (curpos >= offset && curpos <= ((offset-1)+bytes))
{
; // byte is one we want to skip
}
else
fputc(c, out);
curpos++;
}
/* delete 'filename' and move our temp file 'out' to take its place */
fclose(in);
if (remove(filename) == -1)
{
fprintf(stderr, "unable to delete file '%s', unrecoverable failure\n", filename);
fclose(out);
return 0;
}
if (rename("tmp.tmp", filename) == -1)
{
fprintf(stderr, "unable to rename tmp file to '%s', unrecoverable failure\n", filename);
fclose(out);
return 0;
}
fclose(out);
return 1;
}
int
writeatbyte(const char *dest, struct fileorstring *source, long offset)
{
/*
* write file 'source' into 'output_dir'/'file' starting at offset 'offset'
* return 1 on success. 0 on failure
*/
char filename[512] = {0};
snprintf(filename, 512, "%s%s", output_dir, dest);
FILE *in = fopen(filename, "r");
if (!in)
{
fprintf(stderr, "unable to open file '%s', unrecoverable failure\n", filename);
return 0;
}
FILE *tmp = fopen("tmp.tmp", "w");
if (!tmp)
{
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
fprintf(stderr, "unable to open temp file, unrecoverable failure\n");
fclose(in);
return 0;
}
FILE *src = NULL;
if (source->file != NULL)
{
src = fopen(source->file, "r");
if (!src)
{
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
fprintf(stderr, "unable to open file '%s', unrecoverable failure\n", source->file);
fclose(in);
fclose(tmp);
return 0;
}
}
/*
* read file 'in' until byte 'offset', writing into file 'tmp'
* if source->file isn't NULL, write file 'src' into 'tmp'
* otherwise write 'source->str' into file 'tmp'
* finish writing file 'in' into 'tmp'
* delete file 'filename'
* rename file 'tmp' 'filename'
*/
int c, t, a;
long curpos = 1;
int i = 0;
while ((c = fgetc(in)) != EOF)
{
if (curpos == offset)
{
/* if we have a file open to get bytes to write from */
if (src != NULL)
{
while ((t = fgetc(src)) != EOF)
{
/* prevent writing end of line '\n' if the next char is EOF */
a = fgetc(src);
ungetc(a, src);
if (a == EOF && t == '\n')
continue;
fputc(t, tmp);
}
}
/* otherwise we read from source->str */
else
{
while ((t = source->str[i]) != '\0')
{
fputc(source->str[i], tmp);
i++;
}
}
}
fputc(c, tmp);
curpos++;
}
fclose(tmp);
fclose(in);
if (src)
fclose(src);
if (remove(filename) == -1)
{
fprintf(stderr, "unable to delete file '%s', unrecoverable failure\n", filename);
return 0;
}
if (rename("tmp.tmp", filename) == -1)
{
fprintf(stderr, "unable to rename tmp file to '%s', unrecoverable failure\n", filename);
return 0;
}
return 1;
}
int
replaceinpage(const char *outfile, const char *toreplace, struct fileorstring *source)
{
/*
* replace 'toreplace' in file 'outfile' taking 'infile' as input
* return 1 on success, 0 on failure
*/
/* special case for ignoring a call so we can reuse the function */
if (source->file == NULL && source->str == NULL)
return 1;
long substrpos = -1;
substrpos = findstring(outfile, toreplace);
if (substrpos < 0)
{
return 0;
}
if (!deletebytes(outfile, substrpos, strlen(toreplace)))
{
return 0;
}
if (!writeatbyte(outfile, source, substrpos))
{
return 0;
}
return 1;
}
char
*gettime(char *buffer, size_t size)
{
/*
* put the current date into a buffer and return it
*/
time_t t = time(NULL);
struct tm tm = *localtime(&t);
snprintf(buffer, size, "%d/%d/%d", tm.tm_mday, tm.tm_mon+1, tm.tm_year + 1900);
return buffer;
}
int
createtmpfile(const char *name, const char *content, size_t size)
{
FILE *tmp = fopen(name, "w");
if (!tmp)
{
fprintf(stderr, "unable to create temp file '%s', unrecoverable failure\n", name);
return 0;
}
fwrite(content, 1, size, tmp);
fclose(tmp);
return 1;
}
int
genericpage(int flags, const char *output, const char *ind, const char *tit, const char *inf)
{
if (!createfile(output, template_file))
{
fprintf(stderr, "unable to generate genericpage\n");
return 0;
}
char date[255];
struct fileorstring index = {ind, NULL};
struct fileorstring title = {NULL, tit};
struct fileorstring info = {NULL, inf};
struct fileorstring time = {NULL, gettime(date, sizeof(date))};
if (!replaceinpage(output, content_string, &index) ||
!replaceinpage(output, title_string, &title) ||
!replaceinpage(output, info_string, &info) ||
!replaceinpage(output, time_string, &time))
{
fprintf(stderr, "unable to generate output\n");
return 0;
}
return 1;
}
int
frontpage(int flags)
{
return genericpage(flags, frontpage_index_output, frontpage_index, frontpage_title, frontpage_info);
}
int
likespage(int flags)
{
return genericpage(flags, likes_content_output, likes_content, likes_title, likes_info);
}
int
dislikespage(int flags)
{
return genericpage(flags, dislikes_content_output, dislikes_content, dislikes_title, dislikes_info);
}
int
interestingpage(int flags)
{
return genericpage(flags, interesting_content_output, interesting_content, interesting_title, interesting_info);
}
int
opinionspage(int flags)
{
return genericpage(flags, opinions_content_output, opinions_content, opinions_title, opinions_info);
}
int
opinions_animepage(int flags)
{
return genericpage(flags, opinions_anime_content_output, opinions_anime_content, opinions_anime_title, opinions_anime_info);
}
int
opinions_everythingpage(int flags)
{
return genericpage(flags, opinions_everything_content_output, opinions_everything_content, opinions_everything_title, opinions_everything_info);
}
int
portfoliopage(int flags)
{
return genericpage(flags, portfolio_content_output, portfolio_content, portfolio_title, portfolio_info);
}
/* posts functions */
int
postscompare(const void *a, const void *b)
{
return (*(int *)a - *(int *)b);
}
char
*gettitle(char *file, char *title, size_t size)
{
FILE *in = fopen(file, "r");
if (!in)
{
/* can't get title, use default */
fprintf(stderr, "Unable to get post title for %s\n", file);
strncpy(title, posts_title, size);
return title;
}
char buff[size];
memset(buff, 0, size);
fgets(buff, size, in);
striphtml(buff, size);
strncpy(title, buff, size);
size_t len = strlen(title)-1;
while ((title[len] == ' ') || (title[len] == '\t') || (title[len] == '\n'))
{
title[len] = '\0';
len = strlen(title)-1;
}
fclose(in);
return title;
}
int
createdirectpages(const int *posts, size_t totalposts)
{
/*
* create direct post files for each post
*/
char file[512];
char source[512];
char title[1024];
for (int x = 1; x < totalposts; x++)
{
memset(source, 0, 1);
memset(file, 0, 1);
/* output file */
snprintf(file, 512, "%s%d.html", direct_output_dir, posts[x]);
/* source file */
snprintf(source, 512, "%s%d.txt", posts_content, posts[x]);
if (!createfile(file, template_file))
{
fprintf(stderr, "unable to create file '%s', unrecoverable failure\n", file);
return 0;
}
/* get post title */
gettitle(source, title, 1024);
if (!genericpage(NONE, file, source, title, posts_info))
{
fprintf(stderr, "unable to generate direct post page '%s', unrecoverable failure\n", file);
return 0;
}
//printf("%d\n", posts[x]);
}
return 1;
}
char
*generatepagebar(char *bar, size_t size, const int *posts, size_t totalposts, int currentpage, int pagecount)
{
/*
* create a navigation bar for the posts pages highlighting the 'currentpage' link
* store it in bar and return it, NULL on error
*/
char buff[size];
int i;
size_t freespace = size;
/* gross hack, sxprintf returns the number of chars written, keep track of our freespace */
freespace -= snprintf(buff, freespace, "<div class='middle'> <a href='%d'>prev</a> ", (currentpage == 1) ? pagecount : currentpage-1);
if (freespace <= 0) {fprintf(stderr, "out of space in buffer for generatepagebar()\n"); return NULL;}
strncat(bar, buff, freespace);
for (int i = 1; i <= pagecount; i++)
{
// FIXME: this doesn't need to be an if/else..
if (currentpage == i)
{
freespace -= snprintf(buff, freespace, "<strong><i>%d</i></strong> ", i);
}
else
{
freespace -= snprintf(buff, freespace, "<a href='%d'>%d</a> ", i, i);
}
strncat(bar, buff, freespace);
}
freespace -= snprintf(buff, freespace, "<a href='%d'>next</a></div>", (currentpage == pagecount) ? 1 : currentpage+1);
if (freespace <= 0) {fprintf(stderr, "out of space in buffer for generatepagebar()\n"); return NULL;}
strncat(bar, buff, freespace);
return bar;
}
int
writeposts(const int *posts, size_t totalposts, const char *outfile, int currentpage, int pagecount, int flags)
{
/*
* write posts into 'output_dir'/'outfile'
* return 1 on success, 0 on failure
*/
char outfilename[512] = {0};
snprintf(outfilename, 512, "%s%s", output_dir, outfile);
char pagebar[512] = {0};
if (generatepagebar(pagebar, 512, posts, totalposts, currentpage, pagecount) == NULL)
{
fprintf(stderr, "unable to generate pagebar for %s, unrecoverable failure\n", outfilename);
return 0;
}
int start;
int stop;
start = 1 + (posts_per_page * currentpage) - posts_per_page;
stop = start + posts_per_page;
while (stop > (totalposts))
stop--;
int post;
/* write posts to a temp file */
char source[512];
FILE *postfile;
FILE *tmp = fopen("post.tmp", "a");
if (!tmp)
{
fprintf(stderr, "unable to open temp file, unrecoverable failure\n");
return 0;
}
fprintf(tmp, "%s\n", pagebar);
if (flags & PINNED && currentpage == 1)
{
char pinned[4096] = {0};
if (!generatepinned(pinned, 4096))
{
fprintf(stderr, "unable to generate pinned section\n");
}
else
{
fprintf(tmp, "%s\n", pinned);
}
}
for (int x = start; x < stop; x++)
{
/*
* post is the index into posts[] we actually want
* we do this because we want page 1 to contain the last blog posts not actually the first and so on
*/
post = totalposts - posts[x];
fprintf(tmp, "post #%d<br>\n<a href='direct/%d.html'>direct link</a><br>\n", post, post);
snprintf(source, 512, "%s%d.txt", posts_content, post);
postfile = fopen(source, "r");
if (!postfile)
{
fprintf(stderr, "unable to open temp file, unrecoverable failure\n");
fclose(tmp);
return 0;
}
char c;
while ((c = fgetc(postfile)) != EOF)
{
fputc(c, tmp);
}
fprintf(tmp, "<hr>\n");
fclose(postfile);
}
fprintf(tmp, "%s\n", pagebar);
fclose(tmp);
struct fileorstring replacement = {"post.tmp", NULL};
if (!replaceinpage(outfile, content_string, &replacement))
{
fprintf(stderr, "unable to replace content in %s, unrecoverable failure\n", outfilename);
return 0;
}
if (remove("post.tmp") == -1)
{
fprintf(stderr, "unable to remove temp file (post.tmp), unrecoverable failure\n");
return 0;
}
return 1;
}
int
generatepinned(char *buff, size_t size)
{
char title[64] = {0};
char file[512] = {0};
char outbuff[512] = {0};
strncat(buff, "<br><br>\n<div class=\"pinned\">\nPinned posts:\n", 60);
//TODO: check we fit into the buffer
// pray we fit
for (int i = 0; i < sizeof(pinned)/sizeof(pinned[0]); i++)
{
snprintf(file, 512, "%s/%d.txt", posts_content, pinned[i]);
gettitle(file, title, 64);
snprintf(outbuff, 512, "<br><a href=\"direct/%d.html\">%s</a>\n",pinned[i], title);
strncat(buff, outbuff, 512);
}
strncat(buff, "\n</div>\n<br><br><br>\n", 29);
return 1;
}
int
generatepostpages(const int *posts, size_t totalposts, int pagecount, int flags)
{
/*
* create each blog page, there will be 'pagecount' of them
* containing 'posts_per_page' posts each
* return 1 on success, 0 on failure
*/
char outfilename[512] = {0};
int postswritten = 0;
for (int i = 1; i <= pagecount; i++)
{
/* loop through and create every post page required */
snprintf(outfilename, 512, "%s%d.html", posts_output_dir, i);
if (!createfile(outfilename, template_file))
{
fprintf(stderr, "unable to create file '%s', unrecoverable failure\n", outfilename);
return 0;
}
if (!genericpage(NONE, outfilename, NULL, posts_title, posts_info))
{
fprintf(stderr, "unable to create generic page '%s', unrecoverable failure\n", outfilename);
return 0;
}
if (!writeposts(posts, totalposts, outfilename, i, pagecount, flags))
{
fprintf(stderr, "unable to create write posts to page %d', unrecoverable failure\n", i);
return 0;
}
}
return 1;
}
int
postspage(int flags)
{
/*
* TODO: document
*/
/* get all files in the directory that are .txt */
int posts[512]; // logical maximum number of posts
size_t totalposts = 0;
DIR *dir;
struct dirent *ent;
if ((dir = opendir(posts_content)) != NULL)
{
while ((ent = readdir(dir)) != NULL)
{
if (strstr(ent->d_name, ".txt") != NULL)
{
int ign = 0;
/* check if post should be ignored */
for (int i = 0; i < sizeof(ignore)/sizeof(ignore[0]); i++)
{
if (atoi(ent->d_name) == ignore[i])
{
printf("ignoring post %d\n", atoi(ent->d_name));
ign = 1;
}
}
if (ign)
continue;
posts[totalposts] = atoi(ent->d_name); // gross
totalposts++;
}
}
closedir(dir);
}
else
{
/* could not open directory */
fprintf(stderr, "Error opening directory: %s\n", strerror(errno));
fprintf(stderr, "unable to open directory '%s', unrecoverable failure\n", posts_content);
return 0;
}
/* sort posts */
qsort(posts, totalposts, sizeof(int), postscompare);
/* create direct link pages */
if (!createdirectpages(posts, totalposts))
{
fprintf(stderr, "unable to create direct post pages, unrecoverable failure\n");
return 0;
}
/* determine how many pages we need, if total posts divided by 'posts_per_page' isn't 0, we need 'posts_per_page'+1 pages */
int pagecount = (totalposts%posts_per_page == 0) ? totalposts/posts_per_page : totalposts/posts_per_page+1;
if (!generatepostpages(posts, totalposts, pagecount, flags))
{
fprintf(stderr, "unable to create post pages, unrecoverable failure\n");
return 0;
}
/* generate rss feed if required */
if (flags & RSS)
{
generaterss(posts, totalposts, flags);
}
return 1;
}
/* rss */
char
*striphtml(char *str, size_t size)
{
/*
* strip html tags from 'str'
* dont copy newline
* return pointer to 'str'
*/
int idx = 0;
int opened = 0; // false
for(size_t i = 0; i < size; i++)
{
if(str[i] == '<')
{
opened = 1; // true
}
else if (str[i] == '>')
{
opened = 0; // false
}
else if (!opened)
{
str[idx++] = str[i];
}
}
str[idx] = '\0';
if (str[idx-1] == '\n')
str[idx-1] = '\0';
return str;
}
char
*rfc822date(char *date, size_t size)
{
/*
* convert a dumb d/m/y date string into something rfc822 compliant
* absolutely not portable
*/
struct tm tm = {0};
char buff[size];
strptime(date, "%d/%m/%Y", &tm);
strftime(buff, size, "%d %b %Y", &tm);
strncpy(date, buff, size);
snprintf(date, size, "%s 00:00:00 GMT", buff);
return date;
}
char
*getimage(const char *line, char *buff, size_t size)
{
long pos = 0;
size_t linesize = strlen(line);
while (((line[pos] != '>') && (line[pos] != '\0'))
&& pos < linesize
&& pos < size)
{
buff[pos] = line[pos];
pos++;
}
buff[pos++] = '>';
buff[pos++] = '\0';
/* check src attribute for relative link and make it absolute */
char b1[8] = {0};
char newbuff[size-pos]; // ensure we dont exceed size, pos bytes already written
char finalbuff[size];
memset(finalbuff, 0, size);
char *src = strstr(buff, "src=\"");
pos = 5; // size of src="
if (src)
{
src += pos;
strncpy(b1, src, 4);
if (strcmp(b1, "http") != 0) // naively assume an http means a url
{
fprintf(stderr, "%s: relative path in rss feed, fixing..\n", src);
snprintf(newbuff, size-pos, "%s/%s", base_url, src);
strncpy(finalbuff, buff, src-buff);
strncat(finalbuff, newbuff, size-pos);
strncpy(buff, finalbuff, size);
}
}
return buff;
}
int
writerss(FILE *out, int post, int flags)
{
/*
* create rss item using 'post_content'/'posts'.txt into FILE 'out'
* return 1 on success, 0 on fail
*/
if (!out)
return 0;
char buff[512];
snprintf(buff, 512, "%s/%d.txt", posts_content, post);
FILE *in = fopen(buff, "r");
if (!in)
{
fprintf(stderr, "unable to open file %s, unrecoverable failure\n", buff);
return 0;
}
char line[4096];
char title[512] = {0};
char date[512] = {0};
char description[4096] = {0};
char item[8195] = {0};
char image[512] = {0};
int hasimg = 0;
int pos = 0;
while ((fgets(line, 4096, in)) != NULL)
{
if (pos == 0)
{
/* should be the title */
strncpy(title, line, 512);
}
else if (pos == 1)
{
/* should be the date */
strncpy(date, line, 512);
}
else if (pos >= 2 && (title[0] == '\n' || date[0] == '\n'))
{
fprintf(stderr, "post %s has a broken format, please fix it\n", buff);
fclose(in);
return 0;
}
else if (pos == 3)
{
strncpy(description, line, 100);
}
/* try to find images if flag set */
char *img;
if ((flags & RSSIMAGES) && (img = strstr(line, "<img")))
{
hasimg = 1;
getimage(img, image, 1024);
break;
}
pos++;
}
striphtml(title, strlen(title));
striphtml(date, strlen(date));
striphtml(description, strlen(description));
strcat(description, " ...");
rfc822date(date, 512);
//printf("title is: %s date is: %s: desc is: %s\n", title, date, description);
if (hasimg)
{
snprintf(item, 8195, "<item>\n\t<title>%s</title>\n\t<pubDate>%s</pubDate>\n\t<author>%s</author>\n\t<link>https://danieljon.es/posts/direct/%d</link>\n\t<guid>https://danieljon.es/posts/direct/%d</guid>\n\t<description>%s\n<![CDATA[<br>%s]]> </description>\n</item>\n", title, date, author_string, post, post, description, image);
}
else
{
snprintf(item, 8195, "<item>\n\t<title>%s</title>\n\t<pubDate>%s</pubDate>\n\t<author>%s</author>\n\t<link>https://danieljon.es/posts/direct/%d</link>\n\t<guid>https://danieljon.es/posts/direct/%d</guid>\n\t<description>%s</description>\n</item>\n", title, date, author_string, post, post, description);
}
fprintf(out, "%s", item);
fclose(in);
return 1;
}
int
generaterss(const int *posts, size_t totalposts, int flags)
{
/*
* generate an rss feed for our blog/posts
*/
if (!createfile(rss_output, rss_template))
{
fprintf(stderr, "unable to create %s, unrecoverable failure\n", rss_output);
return 0;
}
/* open a temporary file for writing items to */
FILE *tmp = fopen("rss.tmp", "w+");
if (!tmp)
{
fprintf(stderr, "unable to create %s, unrecoverable failure\n", "rss.tmp");
return 0;
}
/* loop through each post and write an rss file to our open file */
int i;
int towrite = post_count;
while (towrite > totalposts)
towrite--;
for (i = 1; i <= towrite; i++)
{
if (!writerss(tmp, (totalposts - posts[i]), flags))
return 0;
}
struct fileorstring src = {"rss.tmp", NULL};
/* we need to flush the file, so just close it */
fclose(tmp);
if (!replaceinpage(rss_output, rss_string, &src))
{
fprintf(stderr, "unable to replace %s in %s, unrecoverable failure\n", rss_string, rss_output);
return 0;
}
remove("rss.tmp");
return 1;
}
|