commit 5ac3b1f94b6153033d9de6dff3fc31b68e8aed59
parent 86e672a51b6730c0ebdf42a0aef247af022323c1
Author: Tomas Hlavaty <tom@logand.com>
Date: Mon, 17 Jan 2011 22:51:18 +0100
in and out accept format-like varargs; Date: field sent in the email header
Diffstat:
M | utils.c | | | 28 | +++++++++++++++------------- |
M | utils.h | | | 6 | ++---- |
M | w3mail.c | | | 43 | +++++++++++++++++++++++++++++-------------- |
3 files changed, 46 insertions(+), 31 deletions(-)
diff --git a/utils.c b/utils.c
@@ -33,26 +33,28 @@ void rtrim(char *s) {
while(s < e && isspace(*e)) *e-- = '\0';
}
-void fpr(FILE *out, ...) {
+void in(void *udata, in_cb cb, char *cmd, ...) {
va_list v;
- va_start(v, out);
- char *a;
- while((a = va_arg(v, char *))) fprintf(out, "%s", a);
- va_end(v);
-}
-
-void in(void *udata, in_cb cb, char *cmd) {
- FILE *pipe = popen(cmd, "r");
- if(!pipe) die(1, "in(): cannot open pipe to '%s'", cmd);
+ va_start(v, cmd);
+ char buf[BLEN];
+ vsnprintf(buf, BLEN, cmd, v);
+ FILE *pipe = popen(buf, "r");
+ if(!pipe) die(1, "in(): cannot open pipe to '%s'", buf);
cb(udata, pipe);
pclose(pipe);
+ va_end(v);
}
-void out(void *udata, out_cb cb, char *cmd) {
- FILE *pipe = popen(cmd, "w");
- if(!pipe) die(1, "out(): cannot open pipe to '%s'", cmd);
+void out(void *udata, out_cb cb, char *cmd, ...) {
+ va_list v;
+ va_start(v, cmd);
+ char buf[BLEN];
+ vsnprintf(buf, BLEN, cmd, v);
+ FILE *pipe = popen(buf, "w");
+ if(!pipe) die(1, "out(): cannot open pipe to '%s'", buf);
cb(udata, pipe);
pclose(pipe);
+ va_end(v);
}
void tmpf(void *udata, tmpf_cb cb, char *fname) {
diff --git a/utils.h b/utils.h
@@ -12,15 +12,13 @@ extern void die(int code, char *fmt, ...);
extern void rtrim(char *s);
-extern void fpr(FILE *out, ...);
-
typedef void (*in_cb)(void *udata, FILE* in);
typedef void (*out_cb)(void *udata, FILE* out);
typedef void (*tmpf_cb)(void *udata, char *fname);
typedef void (*dir_cb)(void *udata, char *path, struct dirent *e, struct stat *s); // TODO return bool
-extern void in(void *udata, in_cb cb, char *cmd);
-extern void out(void *udata, out_cb cb, char *cmd);
+extern void in(void *udata, in_cb cb, char *cmd, ...);
+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);
diff --git a/w3mail.c b/w3mail.c
@@ -22,6 +22,7 @@
#include <sys/wait.h>
#include <unistd.h>
+#include <time.h>
#include "utils.h"
@@ -69,9 +70,7 @@ static void cb_md5sum(void *udata, FILE* in) {
}
static void md5sum(char *fname, char *sum) {
- char cmd[BLEN];
- snprintf(cmd, BLEN, "md5sum '%s'", fname);
- in(sum, cb_md5sum, cmd);
+ in(sum, cb_md5sum, "md5sum '%s'", fname);
}
static void cb_echo(void *udata, FILE* in) {
@@ -90,22 +89,38 @@ struct sendmail {
char *fname;
};
-#define pr(...) {fpr(out, __VA_ARGS__, NULL); fprintf(out, "\n");}
+#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: ", x->from);
- pr("To: ", x->to);
- pr("Message-ID: ", x->id);
- pr("Subject: ", x->subj);
+ pr("From: %s", x->from);
+ pr("To: %s", x->to);
+ pr("Message-ID: %s", x->id);
+ pr("Subject: %s", x->subj);
pr("User-Agent: w3mail");
pr("MIME-Version: 1.0");
- pr("Content-Type: text/html"); // TODO detect and ; charset=utf-8
+ 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("");
- char cmd[BLEN];
- snprintf(cmd, BLEN, "base64 %s", x->fname);
- in(out, cb_echo, cmd);
+ 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);
}
struct w3mail {
@@ -124,7 +139,7 @@ static void cb_w3mail(void *udata, char *fname) {
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};
- out(&y, cb_sendmail, x->cmd);
+ out(&y, cb_sendmail, "%s", x->cmd);
}
static void cb_config(void *udata, FILE* in) {