w3mail

program to send a web page by email
git clone https://logand.com/git/w3mail.git/
Log | Files | Refs | README | LICENSE

commit 094e8a216592321bd680e6705f3981d6bf78c927
parent 463c035f38af82db2a7c26b227976f28b81b452f
Author: Tomas Hlavaty <tom@logand.com>
Date:   Wed, 19 Jan 2011 09:21:32 +0100

config moved to ~/.w3mail dir, tidy introduced

Diffstat:
Mpostfeed.sh | 3+--
Mutils.c | 9+++++++++
Mutils.h | 1+
Mw3mail.c | 130+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
Mw3maild.sh | 3++-
5 files changed, 117 insertions(+), 29 deletions(-)

diff --git a/postfeed.sh b/postfeed.sh @@ -1,3 +1,2 @@ #!/bin/sh - -cat ~/.postfeed | xargs -n1 ./rss2links.sh | grep -v '^$' >>~/.w3mail.in +cat ~/.w3mail/feed | xargs -n1 ./rss2links.sh | grep -v '^$' >>~/.w3mail/in diff --git a/utils.c b/utils.c @@ -91,6 +91,15 @@ void dir(void *udata, dir_cb cb, char *path, ...) { va_end(v); } +int systemf(char *cmd, ...) { + va_list v; + va_start(v, cmd); + char buf[BLEN]; + vsnprintf(buf, BLEN, cmd, v); + va_end(v); + return system(buf); +} + void wget(char *url, char *fname) { // http://www.gnu.org/software/wget/manual/html_node/Exit-Status.html static char *wgetmsg[] = { diff --git a/utils.h b/utils.h @@ -22,6 +22,7 @@ extern void out(void *udata, out_cb cb, char *cmd, ...); extern void tmpf(void *udata, tmpf_cb cb, char *fname, ...); extern void dir(void *udata, dir_cb cb, char *path, ...); +extern int systemf(char *cmd, ...); extern void wget(char *url, char *fname); #endif /* UTILS_H */ diff --git a/w3mail.c b/w3mail.c @@ -20,6 +20,7 @@ */ +#include <string.h> #include <unistd.h> #include <time.h> @@ -42,26 +43,68 @@ static void cb_echo(void *udata, FILE* in) { fwrite(buf, sizeof(char), n, out); } +static void now(char *buf) { + time_t t = time(NULL); + struct tm *tm = localtime(&t); + strftime(buf, BLEN, "%a, %d %b %Y %T %z", tm); +} + +#define pr(...) {fprintf(out, __VA_ARGS__); fprintf(out, "\n");} + +struct tidy { + char *fin; + char *fout; + char *url; +}; + +static void cb_tidy(void *udata, FILE* in) { + struct tidy *x = udata; + char name[BLEN], arg[BLEN]; + strcpy(name, "bbc"); + strcpy(arg, ""); + // TODO compute filter name from url + /* while(!feof(in)) { */ + /* char line[BLEN]; */ + /* if(!fgets(line, BLEN, in)) break; */ + /* rtrim(line); */ + /* // TODO run filter based on url */ + /* // if head(url) then */ + /* // if filters/%s filter exist */ + /* // in(???, ???, "filters/%s '%s'", filter, arg); */ + /* } */ + // w3m -dump /tmp/a.html + systemf("~/.w3mail/filter/%s '%s' <%s >%s", name, arg, x->fin, x->fout); +} + +static void tidy(char *fin, char *fout, char *url) { + struct tidy x = {fin, fout, url}; + in(&x, cb_tidy, "cat ~/.w3mail/tidy"); +} + +struct xtidy { + FILE *out; + char *fname; + char *url; +}; + +static void cb_xtidy(void *udata, char *fname) { + struct xtidy *x = udata; + tidy(x->fname, fname, x->url); + in(x->out, cb_echo, "base64 %s", fname); + //in(x->out, cb_echo, "w3m -dump -T text/html %s", fname); +} + struct sendmail { char *from; char *to; char *id; char *subj; + char *url; + char *mime; + int html; char *fname; }; -#define pr(...) {fprintf(out, __VA_ARGS__); fprintf(out, "\n");} - -static void cb_read_first_line(void *udata, FILE* in) { - rtrim(fgets(udata, BLEN, in)); -} - -static void now(char *buf) { - time_t t = time(NULL); - struct tm *tm = localtime(&t); - strftime(buf, BLEN, "%a, %d %b %Y %T %z", tm); -} - static void cb_sendmail(void *udata, FILE* out) { struct sendmail *x = udata; pr("From: %s", x->from); @@ -70,18 +113,35 @@ static void cb_sendmail(void *udata, FILE* out) { pr("Subject: %s", x->subj); pr("User-Agent: w3mail"); pr("MIME-Version: 1.0"); + //pr("Content-Type: %s", x->html ? "text/plain; charset=utf8" : x->mime); + pr("Content-Type: %s", x->mime); char buf[BLEN]; - //in(buf, cb_read_first_line, "date -R"); now(buf); - pr("Date: %s", buf); - in(buf, cb_read_first_line, "file -bi %s", x->fname); - // TODO fix application/xml if html - pr("Content-Type: %s", buf); - pr("Content-Transfer-Encoding: base64"); + pr("Date: %s", buf); // TODO from feed? + //pr("Content-Transfer-Encoding: %s", x->html ? "8bit" : "base64"); + pr("Content-Transfer-Encoding: %s", "base64"); pr("%s", ""); - // TODO if html > use xpath to select interesting content - // TODO if html > filter out javascript (script tags) - in(out, cb_echo, "base64 %s", x->fname); + if(x->html) { + struct xtidy y = {out, x->fname, x->url}; + tmpf(&y, cb_xtidy, "/tmp/w3mail-XXXXXX"); + } + else in(out, cb_echo, "base64 %s", x->fname); +} + +static void cb_read_first_line(void *udata, FILE* in) { + rtrim(fgets(udata, BLEN, in)); +} + +static int head(char *sub, char *str) { + return 0 == strncmp(sub, str, strlen(sub)); +} + +static int equal(char *str1, char *str2) { + return 0 == strcmp(str1, str2); +} + +static int is_html(char *elt) { + return equal("html", elt) || equal("xhtml", elt); } struct w3mail { @@ -94,12 +154,30 @@ struct w3mail { static void cb_w3mail(void *udata, char *fname) { struct w3mail *x = udata; - char sum[BLEN], id[BLEN], subj[BLEN]; + char sum[BLEN], id[BLEN], subj[BLEN], mime[BLEN]; + int html = 0; wget(x->url, fname); + in(mime, cb_read_first_line, "file -bi %s", fname); + if(head("application/xml;", mime)) { + char elt[BLEN]; + in(elt, cb_read_first_line, "xmlstarlet el %s", fname); + if(equal("rss", elt)) { + // TODO handle rss feed + } + if(equal("atom", elt)) { + // TODO handle atom feed + } + else if(is_html(elt)) { + // TODO fix mime application/xml to html + html = 1; + } + else html = is_html(elt); + } + else html = head("text/html;", mime); md5sum(fname, sum); snprintf(id, BLEN, "<%s@%s>", sum, x->host); - snprintf(subj, BLEN, "[w3mail] %s", x->url); - struct sendmail y = {x->from, x->to, id, subj, fname}; + snprintf(subj, BLEN, "[w3mail] %s", x->url); // TODO from feed? + struct sendmail y = {x->from, x->to, id, subj, x->url, mime, html, fname}; out(&y, cb_sendmail, "%s", x->cmd); } @@ -119,7 +197,7 @@ int main(int argc, char *argv[]) { if(argc == 1) { char cmd[BLEN], from[BLEN], to[BLEN], host[BLEN], url[BLEN]; struct w3mail x = {cmd, from, to, host, url}; - in(&x, cb_config, "cat ~/.w3mail"); + in(&x, cb_config, "cat ~/.w3mail/config"); while(!feof(stdin)) { char *line = fgets(url, BLEN, stdin); if(!line) break; @@ -130,7 +208,7 @@ int main(int argc, char *argv[]) { else if(argc == 2) { char cmd[BLEN], from[BLEN], to[BLEN], host[BLEN]; struct w3mail x = {cmd, from, to, host, argv[1]}; - in(&x, cb_config, "cat ~/.w3mail"); + in(&x, cb_config, "cat ~/.w3mail/config"); run(&x); } else if(argc == 6) { diff --git a/w3maild.sh b/w3maild.sh @@ -1 +1,2 @@ -echo >~/.w3mail.in; tail -f ~/.w3mail.in | xargs -n1 -P20 w3mail 2>>~/.w3mail.log +#!/bin/sh +echo >~/.w3mail/in; tail -f ~/.w3mail/in | xargs -n1 -P20 w3mail 2>>~/.w3mail/log