From 084731fec24932ad48c48c5147d129ce2af0ccc8 Mon Sep 17 00:00:00 2001 From: default Date: Fri, 21 Oct 2022 19:07:20 +0200 Subject: [PATCH] Send notification emails through the queue. --- activitypub.c | 53 ++++++++++++++++++++++++++++++++------------------- data.c | 21 +++++++++++++++++++- snac.h | 1 + 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/activitypub.c b/activitypub.c index 24bf04e..5b2d981 100644 --- a/activitypub.c +++ b/activitypub.c @@ -626,8 +626,6 @@ d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to, char } - - void notify(snac *snac, char *type, char *utype, char *actor, char *msg) /* notifies the user of relevant events */ { @@ -670,7 +668,7 @@ void notify(snac *snac, char *type, char *utype, char *actor, char *msg) xs *subject = xs_fmt("snac notify for @%s@%s", xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host")); - xs *from = xs_fmt("snac-daemon@%s (snac daemon)", xs_dict_get(srv_config, "host")); + xs *from = xs_fmt("snac-daemon ", xs_dict_get(srv_config, "host")); xs *header = xs_fmt( "From: %s\n" "To: %s\n" @@ -699,23 +697,7 @@ void notify(snac *snac, char *type, char *utype, char *actor, char *msg) body = xs_str_cat(body, s1); } - /* now write */ - FILE *f; - - if ((f = popen("/usr/sbin/sendmail -t", "w")) != NULL) { - fprintf(f, "%s\n", body); - - if (fclose(f) == EOF) { - snac_log(snac, xs_fmt("fclose error in pipe to sendmail (errno: %d)", errno)); - - if ((f = fopen("/tmp/dead-letter", "w")) != NULL) { - fprintf(f, "%s\n", body); - fclose(f); - } - } - } - else - snac_log(snac, xs_fmt("cannot pipe to sendmail (errno: %d)", errno)); + enqueue_email(snac, body, 0); } @@ -958,6 +940,35 @@ void process_queue(snac *snac) } } } + else + if (strcmp(type, "email") == 0) { + /* send this email */ + char *msg = xs_dict_get(q_item, "message"); + int retries = xs_number_get(xs_dict_get(q_item, "retries")); + FILE *f; + int ok = 0; + + if ((f = popen("/usr/sbin/sendmail -t", "w")) != NULL) { + fprintf(f, "%s\n", msg); + + if (fclose(f) != EOF) + ok = 1; + } + + if (ok) + snac_debug(snac, 1, xs_fmt("email message sent")); + else { + if (retries > queue_retry_max) + snac_log(snac, xs_fmt("process_queue email giving up (errno: %d)", errno)); + else { + /* requeue */ + snac_log(snac, xs_fmt( + "process_queue email requeue %d (errno: %d)", retries + 1, errno)); + + enqueue_email(snac, msg, retries + 1); + } + } + } } } @@ -973,6 +984,8 @@ void post(snac *snac, char *msg) enqueue_output(snac, msg, v, 0); } } + + /** HTTP handlers */ int activitypub_get_handler(d_char *req, char *q_path, diff --git a/data.c b/data.c index cef4028..7b623af 100644 --- a/data.c +++ b/data.c @@ -980,6 +980,25 @@ void enqueue_output(snac *snac, char *msg, char *actor, int retries) } +void enqueue_email(snac *snac, char *msg, int retries) +/* enqueues an email message to be sent */ +{ + int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes")); + xs *ntid = tid(retries * 60 * qrt); + xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid); + xs *qmsg = xs_dict_new(); + xs *rn = xs_number_new(retries); + + qmsg = xs_dict_append(qmsg, "type", "email"); + qmsg = xs_dict_append(qmsg, "message", msg); + qmsg = xs_dict_append(qmsg, "retries", rn); + + _enqueue_put(fn, qmsg); + + snac_debug(snac, 1, xs_fmt("enqueue_email %d", retries)); +} + + d_char *queue(snac *snac) /* returns a list with filenames that can be dequeued */ { @@ -997,7 +1016,7 @@ d_char *queue(snac *snac) time_t t2 = atol(bn + 1); if (t2 > t) - snac_debug(snac, 2, xs_fmt("queue not yet time for %s", v)); + snac_debug(snac, 2, xs_fmt("queue not yet time for %s [%ld]", v, t)); else { list = xs_list_append(list, v); snac_debug(snac, 2, xs_fmt("queue ready for %s", v)); diff --git a/snac.h b/snac.h index 3e82585..e19638c 100644 --- a/snac.h +++ b/snac.h @@ -91,6 +91,7 @@ d_char *history_list(snac *snac); void enqueue_input(snac *snac, char *msg, char *req, int retries); void enqueue_output(snac *snac, char *msg, char *actor, int retries); +void enqueue_email(snac *snac, char *msg, int retries); d_char *queue(snac *snac); d_char *dequeue(snac *snac, char *fn);