sfeed

Simple RSS and Atom feed parser
git clone https://git.sinitax.com/codemadness/sfeed
Log | Files | Refs | README | LICENSE | Upstream | sfeed.txt

commit e88ade983fca58fc5b5592be4ad7734ae0bb121f
parent 6bb38b0df57c58c2f8088d990a28f2b2a8a87196
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Thu, 30 Jul 2015 16:14:04 +0200

sfeed_mbox: initial version, needs more work

Diffstat:
MMakefile | 6++++++
Asfeed_mbox.1 | 30++++++++++++++++++++++++++++++
Asfeed_mbox.c | 97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 133 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -6,6 +6,7 @@ SRC = \ sfeed.c\ sfeed_frames.c\ sfeed_html.c\ + sfeed_mbox.c\ sfeed_opml_import.c\ sfeed_plain.c\ sfeed_web.c\ @@ -19,6 +20,7 @@ BIN = \ sfeed\ sfeed_frames\ sfeed_html\ + sfeed_mbox\ sfeed_opml_import\ sfeed_plain\ sfeed_web\ @@ -30,6 +32,7 @@ MAN1 = \ sfeed.1\ sfeed_frames.1\ sfeed_html.1\ + sfeed_mbox.1\ sfeed_opml_export.1\ sfeed_opml_import.1\ sfeed_plain.1\ @@ -77,6 +80,9 @@ sfeed_frames: sfeed_frames.o util.o ${EXTRAOBJ} sfeed_html: sfeed_html.o util.o ${CC} -o $@ sfeed_html.o util.o ${LDFLAGS} +sfeed_mbox: sfeed_mbox.o util.o + ${CC} -o $@ sfeed_mbox.o util.o ${LDFLAGS} + sfeed_opml_import: sfeed_opml_import.o xml.o util.o ${EXTRAOBJ} ${CC} -o $@ sfeed_opml_import.o xml.o util.o ${EXTRAOBJ} ${LDFLAGS} diff --git a/sfeed_mbox.1 b/sfeed_mbox.1 @@ -0,0 +1,30 @@ +.Dd May 17, 2015 +.Dt SFEED_MBOX 1 +.Os +.Sh NAME +.Nm sfeed_mbox +.Nd formats a feeds file to mail +.Sh SYNOPSIS +.Nm +.Sh DESCRIPTION +.Nm +formats a feeds file (TSV) from +.Xr sfeed_update 1 +to mail. It reads TSV data from stdin and writes e-mails to stdout. These can +be further processed by tools like +.Xr procmail 1 . +.Sh FORMAT +Depending on the original content\-type the mail will be formatted as +plain-text (text/plain) or HTML (text/html). +.Sh CUSTOM HEADERS +To make filtering simpler some custom headers are set: +.Bl -tag -width Ds +.It X-Feedname +The feedname (set in sfeedrc). +.El +.Sh SEE ALSO +.Xr procmail 1 , +.Xr sfeed 1 , +.Xr sfeed_update 1 +.Sh AUTHORS +.An Hiltjo Posthuma Aq Mt hiltjo@codemadness.org diff --git a/sfeed_mbox.c b/sfeed_mbox.c @@ -0,0 +1,97 @@ +#include <sys/types.h> + +#include <err.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> + +#include "util.h" + +static char *line = NULL; +static size_t linesize = 0; + +static void +printfeed(FILE *fp, const char *feedname) +{ + struct tm tm; + char *fields[FieldLast], timebuf[32], mtimebuf[32]; + char host[HOST_NAME_MAX + 1], *user; + time_t parsedtime; + int r; + + if(!(user = getenv("USER"))) + user = "you"; + if (gethostname(host, sizeof(host)) == -1) + err(1, "gethostname"); + + if ((parsedtime = time(NULL)) == -1) + err(1, "time"); + if (!gmtime_r(&parsedtime, &tm)) + errx(1, "gmtime_r: can't get current time"); + if (!strftime(mtimebuf, sizeof(mtimebuf), "%a %b %d %H:%M:%S %Y", &tm)) + errx(1, "can't format current time"); + + while (parseline(&line, &linesize, fields, FieldLast, '\t', fp) > 0) { + /* mbox header */ + printf("From MAILER-DAEMON %s\n", mtimebuf); + printf("From: %s <sfeed@>\n", feedname); + printf("To: %s <%s@%s>\n", user, user, host); + printf("Subject: %s\n", fields[FieldTitle]); + + printf("Message-Id: <%s-%s-sfeed>\n", + fields[FieldUnixTimestamp], + fields[FieldId]); + + r = strtotime(fields[FieldUnixTimestamp], &parsedtime); + if (r != -1) { + if (gmtime_r(&parsedtime, &tm) && + strftime(timebuf, sizeof(timebuf), "%d %b %y %H:%M +0000", &tm)) + printf("Date: %s\n", timebuf); + } + printf("Content-Type: text/%s; charset=UTF-8\n", + fields[FieldContentType]); + printf("Content-Transfer-Encoding: binary\n"); + + if (*feedname != '\0') + printf("X-Feedname: %s\n", feedname); + printf("\nLink: %s", fields[FieldLink]); + printf("\n\n"); + + if (!strcmp(fields[FieldContentType], "html")) { + printf("<html><body>\n"); + printcontent(fields[FieldContent], stdout); + printf("</body></html>\n"); + } else { + printcontent(fields[FieldContent], stdout); + } + fputs("\n", stdout); + } +} + +int +main(int argc, char *argv[]) +{ + FILE *fp; + char *name; + int i; + + if (argc == 1) { + printfeed(stdin, ""); + } else { + for (i = 1; i < argc; i++) { + fp = fopen(argv[i], "r"); + if (!fp) + err(1, "fopen: %s", argv[i]); + name = xbasename(argv[i]); + printfeed(fp, name); + free(name); + if (ferror(fp)) + err(1, "ferror: %s", argv[i]); + fclose(fp); + } + } + return 0; +}