sfeed_frames.c (5058B) - raw
1 #include <sys/types.h> 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <time.h> 7 8 #include "util.h" 9 10 static struct feed *feeds; 11 static char *line; 12 static size_t linesize; 13 static time_t comparetime; 14 static unsigned long totalnew, total; 15 16 static void 17 printfeed(FILE *fpitems, FILE *fpin, struct feed *f) 18 { 19 char *fields[FieldLast]; 20 ssize_t linelen; 21 unsigned int isnew; 22 struct tm rtm, *tm; 23 time_t parsedtime; 24 25 /* menu if not unnamed */ 26 if (f->name[0]) { 27 fputs("<h2 id=\"", fpitems); 28 xmlencode(f->name, fpitems); 29 fputs("\"><a href=\"#", fpitems); 30 xmlencode(f->name, fpitems); 31 fputs("\">", fpitems); 32 xmlencode(f->name, fpitems); 33 fputs("</a></h2>\n", fpitems); 34 } 35 fputs("<pre>\n", fpitems); 36 37 while ((linelen = getline(&line, &linesize, fpin)) > 0) { 38 if (line[linelen - 1] == '\n') 39 line[--linelen] = '\0'; 40 parseline(line, fields); 41 42 parsedtime = 0; 43 if (!strtotime(fields[FieldUnixTimestamp], &parsedtime) && 44 (tm = localtime_r(&parsedtime, &rtm))) { 45 isnew = (parsedtime >= comparetime) ? 1 : 0; 46 totalnew += isnew; 47 f->totalnew += isnew; 48 fprintf(fpitems, "%04d-%02d-%02d %02d:%02d ", 49 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 50 tm->tm_hour, tm->tm_min); 51 } else { 52 isnew = 0; 53 fputs(" ", fpitems); 54 } 55 f->total++; 56 total++; 57 58 if (fields[FieldLink][0]) { 59 fputs("<a href=\"", fpitems); 60 xmlencode(fields[FieldLink], fpitems); 61 fputs("\">", fpitems); 62 } 63 if (isnew) 64 fputs("<b><u>", fpitems); 65 xmlencode(fields[FieldTitle], fpitems); 66 if (isnew) 67 fputs("</u></b>", fpitems); 68 if (fields[FieldLink][0]) 69 fputs("</a>", fpitems); 70 fputs("\n", fpitems); 71 } 72 fputs("</pre>\n", fpitems); 73 } 74 75 int 76 main(int argc, char *argv[]) 77 { 78 FILE *fpindex, *fpitems, *fpmenu = NULL, *fp; 79 char *name; 80 int i, showsidebar = (argc > 1); 81 struct feed *f; 82 83 if (pledge("stdio rpath wpath cpath", NULL) == -1) 84 err(1, "pledge"); 85 86 if (!(feeds = calloc(argc, sizeof(struct feed)))) 87 err(1, "calloc"); 88 89 if ((comparetime = time(NULL)) == -1) 90 err(1, "time"); 91 /* 1 day is old news */ 92 comparetime -= 86400; 93 94 /* write main index page */ 95 if (!(fpindex = fopen("index.html", "wb"))) 96 err(1, "fopen: index.html"); 97 if (!(fpitems = fopen("items.html", "wb"))) 98 err(1, "fopen: items.html"); 99 if (showsidebar && !(fpmenu = fopen("menu.html", "wb"))) 100 err(1, "fopen: menu.html"); 101 102 if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1) 103 err(1, "pledge"); 104 105 fputs("<!DOCTYPE HTML>\n" 106 "<html>\n" 107 "\t<head>\n" 108 "\t<meta name=\"referrer\" content=\"no-referrer\" />\n" 109 "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" 110 "\t<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n" 111 "</head>\n" 112 "<body class=\"frame\"><div id=\"items\">", fpitems); 113 114 if (argc == 1) { 115 feeds[0].name = ""; 116 printfeed(fpitems, stdin, &feeds[0]); 117 } else { 118 for (i = 1; i < argc; i++) { 119 name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i]; 120 feeds[i - 1].name = name; 121 122 if (!(fp = fopen(argv[i], "r"))) 123 err(1, "fopen: %s", argv[i]); 124 printfeed(fpitems, fp, &feeds[i - 1]); 125 if (ferror(fp)) 126 err(1, "ferror: %s", argv[i]); 127 fclose(fp); 128 } 129 } 130 fputs("</div></body>\n</html>\n", fpitems); /* div items */ 131 132 if (showsidebar) { 133 fputs("<!DOCTYPE HTML>\n" 134 "<html>\n" 135 "<head>\n" 136 "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" 137 "\t<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n" 138 "</head>\n" 139 "<body class=\"frame\">\n<div id=\"sidebar\">\n", fpmenu); 140 141 for (i = 1; i < argc; i++) { 142 f = &feeds[i - 1]; 143 if (f->totalnew) 144 fputs("<a class=\"n\" href=\"items.html#", fpmenu); 145 else 146 fputs("<a href=\"items.html#", fpmenu); 147 xmlencode(f->name, fpmenu); 148 fputs("\" target=\"items\">", fpmenu); 149 if (f->totalnew > 0) 150 fputs("<b><u>", fpmenu); 151 xmlencode(f->name, fpmenu); 152 fprintf(fpmenu, " (%lu)", f->totalnew); 153 if (f->totalnew > 0) 154 fputs("</u></b>", fpmenu); 155 fputs("</a><br/>\n", fpmenu); 156 } 157 fputs("</div></body></html>\n", fpmenu); 158 } 159 fputs("<!DOCTYPE html>\n<html>\n<head>\n" 160 "\t<meta name=\"referrer\" content=\"no-referrer\" />\n" 161 "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" 162 "\t<title>(", fpindex); 163 fprintf(fpindex, "%lu/%lu", totalnew, total); 164 fputs(") - Newsfeed</title>\n\t<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n" 165 "</head>\n", fpindex); 166 if (showsidebar) { 167 fputs("<frameset framespacing=\"0\" cols=\"250,*\" frameborder=\"1\">\n" 168 "\t<frame name=\"menu\" src=\"menu.html\" target=\"menu\">\n", fpindex); 169 } else { 170 fputs("<frameset framespacing=\"0\" cols=\"*\" frameborder=\"1\">\n", fpindex); 171 } 172 fputs( 173 "\t<frame name=\"items\" src=\"items.html\" target=\"items\">\n" 174 "</frameset>\n" 175 "</html>\n", fpindex); 176 177 fclose(fpindex); 178 fclose(fpitems); 179 if (fpmenu) 180 fclose(fpmenu); 181 182 return 0; 183 }