save items in a Maildir/

rssdl is back in a usable state! (for rss only)
master
Shokara Kou 6 months ago
parent bcc61828ad
commit cfbd4d78ee
  1. 3
      .gitignore
  2. 8
      Makefile
  3. 17
      main.c
  4. 28
      parse.c
  5. 16
      parse.h
  6. 114
      save.c
  7. 8
      save.h
  8. 6
      xml.c

3
.gitignore vendored

@ -2,3 +2,6 @@ rssdl
*.o
vgcore*
*.core
*/
*.rss
*.atom

@ -1,13 +1,13 @@
include config.mk
SRC = main.c fetch.c xml.c save.c hash.c
SRC = main.c xml.c parse.c save.c
OBJ = ${SRC:.c=.o}
all: rssdl
fetch.o: fetch.h xml.h
save.o: save.h parse.h hash.h
hash.o: hash.h
parse.o: parse.h save.h xml.h
save.o: save.h parse.h
xml.o: xml.h
rssdl: ${OBJ}
${CC} ${CPPFLAGS} ${CFLAGS} -o $@ ${OBJ} ${LDFLAGS}

@ -1,13 +1,13 @@
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include "fetch.h"
#include "save.h"
int main(int argc, char **argv)
#include "parse.h"
int main(int argc, char *argv[])
{
char *maildir;
int exit_code = 0;
if (argc != 2) {
printf("Usage: %s maildir\n", argv[0]);
@ -22,11 +22,8 @@ int main(int argc, char **argv)
mkdir("cur", 0755);
mkdir("new", 0755);
if (fetch_rss() == -1)
exit_code = 1;
unlink("tmp/summary");
unlink("tmp/desc");
if (parse_item() != 0)
return 1;
return exit_code;
return 0;
}

@ -1,8 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "parse.h"
#include "save.h"
#include "xml.h"
#define istag(name) strcmp(x->tag, name) == 0
@ -13,7 +15,6 @@ char feedtitle[256];
static void xmldata(XMLParser *, const char *, size_t);
static void xmldataentity(XMLParser *, const char *, size_t);
static void xmldatastart(XMLParser *);
static void xmltagend(XMLParser *, const char *, size_t, int);
static void xmltagstart(XMLParser *, const char *, size_t);
static void rfc822_date(char *);
@ -60,24 +61,15 @@ xmltagend(XMLParser *x, const char *t, size_t tl, int isshort)
return;
if (strcmp(x->tag, "item") == 0) {
fclose(item.desc);
isitem = 0;
fseek(item.desc, 0, SEEK_SET);
rfc822_date(item.date);
printf("From: rssdl@localhost\n"
"Subject: [%s] %s\n"
"Date: %s\n"
"Content-Type: text/html\n"
"\nArticle Link: %s\n\n",
feedtitle, item.title, item.date, item.link);
item.desc = fopen("/tmp/rssdl.abc", "r");
char line[8192];
while (fgets(line, sizeof(line), item.desc) != NULL)
printf("%s\n", line);
printf("\n");
save_item(item, feedtitle);
fclose(item.desc);
item.desc = NULL;
memset(&item, 0, sizeof(struct rss_item));
}
}
@ -87,10 +79,8 @@ xmltagstart(XMLParser *x, const char *t, size_t tl)
{
if (strcmp(x->tag, "item") == 0) {
isitem = 1;
/*
* use mkstemp or some other random temporary path name
*/
item.desc = fopen("/tmp/rssdl.abc", "w");
snprintf(item.descfn, sizeof(item.descfn), "tmp/desc.%6x", rand());
item.desc = fopen(item.descfn, "w+");
}
}
@ -112,7 +102,7 @@ rfc822_date(char *date)
}
int
main(void)
parse_item(void)
{
XMLParser x = {0};

@ -1,7 +1,15 @@
#ifndef PARSE_H_INC
#define PARSE_H_INC
struct rss_item {
char title[256];
char link[256];
char date[32];
char title[256];
char link[256];
char date[32];
long epochsec;
FILE *desc;
char descfn[16];
FILE *desc;
};
int parse_item(void);
#endif

114
save.c

@ -0,0 +1,114 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "save.h"
#include "parse.h"
static unsigned int fletcher16_file(FILE *);
static int item_file_exists(char *);
static unsigned int
fletcher16_file(FILE *fp)
{
unsigned int sum1 = 0, sum2 = 0;
int c;
while ((c = fgetc(fp)) != EOF) {
sum1 += (sum1 + c) % 255;
sum2 += (sum2 + sum1) % 255;
}
return (sum2 << 8) | sum1;
}
/* check if file exists already in cur/ or new/ */
static int
item_file_exists(char *filename)
{
DIR *dp;
struct dirent *dir;
if ((dp = opendir("cur/"))) {
while ((dir = readdir(dp))) {
if (strstr(dir->d_name, filename + 4)) {
closedir(dp);
return 1;
}
}
}
closedir(dp);
if ((dp = opendir("new/"))) {
while ((dir = readdir(dp))) {
if (strstr(dir->d_name, filename + 4)) {
closedir(dp);
return 1;
}
}
}
closedir(dp);
return 0;
}
int
save_item(struct rss_item item, char *feedtitle)
{
FILE *fp;
static char hostname[256];
char path[512], newpath[512], line[1024];
unsigned int digest;
if (gethostname(hostname, sizeof(hostname)) == -1) {
perror("failed to get hostname, setting to localhost");
strncpy(hostname, "localhost", sizeof(hostname));
}
snprintf(path, sizeof(path), "tmp/%li.rssdl-tmp.%s:2,", item.epochsec, hostname);
fp = fopen(path, "w");
if (!fp) {
fprintf(stderr, "failed to open %s for writing\n", path);
goto CLEANUP;
}
fprintf(fp, "From: rssdl@%s\n"
"Subject: [%s] %s\n"
"Date: %s\n"
"Content-Type: text/html\n"
"\nArticle Link: %s\n\n",
hostname, feedtitle, item.title, item.date, item.link);
if (item.desc) {
while (fgets(line, sizeof(line), item.desc) != NULL)
fputs(line, fp);
}
fclose(fp);
fp = fopen(path, "r");
if (!fp) {
fprintf(stderr, "failed to open %s for reading\n", path);
goto CLEANUP;
}
digest = fletcher16_file(fp);
snprintf(newpath, sizeof(newpath), "new/%li.%x.%s:2,", item.epochsec, digest, hostname);
if (item_file_exists(newpath) == 1)
goto CLEANUP;
if (rename(path, newpath) == 0)
puts(newpath);
CLEANUP:
fclose(fp);
unlink(path);
return 0;
}

@ -0,0 +1,8 @@
#ifndef SAVE_H_INC
#define SAVE_H_INC
#include "parse.h"
int save_item(struct rss_item item, char *feedtitle);
#endif

@ -224,20 +224,20 @@ codepointtoutf8(long r, char *s)
} else if (r <= 0x07FF) {
/* 2 bytes: 00000aaa aabbbbbb */
s[0] = 0xC0 | ((r & 0x0007C0) >> 6); /* 110aaaaa */
s[1] = 0x80 | (r & 0x00003F); /* 10bbbbbb */
s[1] = 0x80 | (r & 0x00003F); /* 10bbbbbb */
return 2;
} else if (r <= 0xFFFF) {
/* 3 bytes: aaaabbbb bbcccccc */
s[0] = 0xE0 | ((r & 0x00F000) >> 12); /* 1110aaaa */
s[1] = 0x80 | ((r & 0x000FC0) >> 6); /* 10bbbbbb */
s[2] = 0x80 | (r & 0x00003F); /* 10cccccc */
s[2] = 0x80 | (r & 0x00003F); /* 10cccccc */
return 3;
} else {
/* 4 bytes: 000aaabb bbbbcccc ccdddddd */
s[0] = 0xF0 | ((r & 0x1C0000) >> 18); /* 11110aaa */
s[1] = 0x80 | ((r & 0x03F000) >> 12); /* 10bbbbbb */
s[2] = 0x80 | ((r & 0x000FC0) >> 6); /* 10cccccc */
s[3] = 0x80 | (r & 0x00003F); /* 10dddddd */
s[3] = 0x80 | (r & 0x00003F); /* 10dddddd */
return 4;
}
}

Loading…
Cancel
Save