sfeed_mbox.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <unistd.h>
  6. #include "util.h"
  7. static char *line;
  8. static size_t linesize;
  9. static char host[256], *user, dtimebuf[32], mtimebuf[32];
  10. static int usecontent = 0; /* env variable: $SFEED_MBOX_CONTENT */
  11. static unsigned long long
  12. djb2(unsigned char *s, unsigned long long hash)
  13. {
  14. int c;
  15. while ((c = *s++))
  16. hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  17. return hash;
  18. }
  19. /* Unescape / decode fields printed by string_print_encoded()
  20. * "\\" to "\", "\t", to TAB, "\n" to newline. Other escape sequences are
  21. * ignored: "\z" etc. Mangle "From " in mboxrd style (always prefix >). */
  22. static void
  23. printcontent(const char *s, FILE *fp)
  24. {
  25. escapefrom:
  26. for (; *s == '>'; s++)
  27. putc('>', fp);
  28. /* escape "From ", mboxrd-style. */
  29. if (!strncmp(s, "From ", 5))
  30. putc('>', fp);
  31. for (; *s; s++) {
  32. switch (*s) {
  33. case '\\':
  34. s++;
  35. switch (*s) {
  36. case 'n':
  37. putc('\n', fp);
  38. s++;
  39. goto escapefrom;
  40. case '\\': putc('\\', fp); break;
  41. case 't': putc('\t', fp); break;
  42. }
  43. break;
  44. default:
  45. putc(*s, fp); break;
  46. }
  47. }
  48. }
  49. static void
  50. printfeed(FILE *fp, const char *feedname)
  51. {
  52. char *fields[FieldLast], timebuf[32];
  53. struct tm parsedtm, *tm;
  54. time_t parsedtime;
  55. unsigned long long hash;
  56. ssize_t linelen;
  57. int ishtml;
  58. while ((linelen = getline(&line, &linesize, fp)) > 0 &&
  59. !ferror(stdout)) {
  60. if (line[linelen - 1] == '\n')
  61. line[--linelen] = '\0';
  62. hash = djb2((unsigned char *)line, 5381ULL);
  63. parseline(line, fields);
  64. /* mbox + mail header */
  65. printf("From MAILER-DAEMON %s\n", mtimebuf);
  66. parsedtime = 0;
  67. if (!strtotime(fields[FieldUnixTimestamp], &parsedtime) &&
  68. (tm = gmtime_r(&parsedtime, &parsedtm)) &&
  69. strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S +0000", tm)) {
  70. printf("Date: %s\n", timebuf);
  71. } else {
  72. printf("Date: %s\n", dtimebuf); /* invalid/missing: use current time */
  73. }
  74. printf("From: %s <sfeed@>\n", fields[FieldAuthor][0] ? fields[FieldAuthor] : feedname);
  75. printf("To: %s <%s@%s>\n", user, user, host);
  76. printf("Subject: %s\n", fields[FieldTitle]);
  77. printf("Message-ID: <%s%s%llu@%s>\n",
  78. fields[FieldUnixTimestamp],
  79. fields[FieldUnixTimestamp][0] ? "." : "",
  80. hash, feedname);
  81. ishtml = usecontent && !strcmp(fields[FieldContentType], "html");
  82. if (ishtml)
  83. fputs("Content-Type: text/html; charset=\"utf-8\"\n", stdout);
  84. else
  85. fputs("Content-Type: text/plain; charset=\"utf-8\"\n", stdout);
  86. fputs("Content-Transfer-Encoding: binary\n", stdout);
  87. printf("X-Feedname: %s\n", feedname);
  88. fputs("\n", stdout);
  89. if (ishtml) {
  90. fputs("<p>\n", stdout);
  91. if (fields[FieldLink][0]) {
  92. fputs("Link: <a href=\"", stdout);
  93. xmlencode(fields[FieldLink], stdout);
  94. fputs("\">", stdout);
  95. xmlencode(fields[FieldLink], stdout);
  96. fputs("</a><br/>\n", stdout);
  97. }
  98. if (fields[FieldEnclosure][0]) {
  99. fputs("Enclosure: <a href=\"", stdout);
  100. xmlencode(fields[FieldEnclosure], stdout);
  101. fputs("\">", stdout);
  102. xmlencode(fields[FieldEnclosure], stdout);
  103. fputs("</a><br/>\n", stdout);
  104. }
  105. fputs("</p>\n", stdout);
  106. } else {
  107. if (fields[FieldLink][0])
  108. printf("Link: %s\n", fields[FieldLink]);
  109. if (fields[FieldEnclosure][0])
  110. printf("Enclosure: %s\n", fields[FieldEnclosure]);
  111. }
  112. if (usecontent) {
  113. fputs("\n", stdout);
  114. if (ishtml && fields[FieldLink][0]) {
  115. fputs("<base href=\"", stdout);
  116. xmlencode(fields[FieldLink], stdout);
  117. fputs("\"/>\n", stdout);
  118. }
  119. printcontent(fields[FieldContent], stdout);
  120. }
  121. fputs("\n\n", stdout);
  122. }
  123. }
  124. int
  125. main(int argc, char *argv[])
  126. {
  127. struct tm tmnow;
  128. time_t now;
  129. FILE *fp;
  130. char *name, *tmp;
  131. int i;
  132. if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1)
  133. err(1, "pledge");
  134. if ((tmp = getenv("SFEED_MBOX_CONTENT")))
  135. usecontent = !strcmp(tmp, "1");
  136. if (!(user = getenv("USER")))
  137. user = "you";
  138. if (gethostname(host, sizeof(host)) == -1)
  139. err(1, "gethostname");
  140. if ((now = time(NULL)) == (time_t)-1)
  141. errx(1, "time");
  142. if (!gmtime_r(&now, &tmnow))
  143. err(1, "gmtime_r: can't get current time");
  144. if (!strftime(mtimebuf, sizeof(mtimebuf), "%a %b %d %H:%M:%S %Y", &tmnow))
  145. errx(1, "strftime: can't format current time");
  146. if (!strftime(dtimebuf, sizeof(dtimebuf), "%a, %d %b %Y %H:%M:%S +0000", &tmnow))
  147. errx(1, "strftime: can't format current time");
  148. if (argc == 1) {
  149. printfeed(stdin, "");
  150. checkfileerror(stdin, "<stdin>", 'r');
  151. } else {
  152. for (i = 1; i < argc; i++) {
  153. if (!(fp = fopen(argv[i], "r")))
  154. err(1, "fopen: %s", argv[i]);
  155. name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
  156. printfeed(fp, name);
  157. checkfileerror(fp, argv[i], 'r');
  158. checkfileerror(stdout, "<stdout>", 'w');
  159. fclose(fp);
  160. }
  161. }
  162. checkfileerror(stdout, "<stdout>", 'w');
  163. return 0;
  164. }