From 4e3d596bee1cee2c4146265eb6ac827f09820c53 Mon Sep 17 00:00:00 2001 From: shtrophic Date: Mon, 20 Jan 2025 19:43:42 +0100 Subject: add xs_smtp_request --- activitypub.c | 48 +++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) (limited to 'activitypub.c') diff --git a/activitypub.c b/activitypub.c index 8b44dc8..ca5cc3e 100644 --- a/activitypub.c +++ b/activitypub.c @@ -962,17 +962,20 @@ void notify(snac *snac, const char *type, const char *utype, const char *actor, 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 ", xs_dict_get(srv_config, "host")); + xs *from = xs_fmt("", xs_dict_get(srv_config, "host")); xs *header = xs_fmt( - "From: %s\n" + "From: snac-daemon %s\n" "To: %s\n" "Subject: %s\n" "\n", from, email, subject); - xs *email_body = xs_fmt("%s%s", header, body); + xs_dict *mailinfo = xs_dict_new(); + xs_dict_append(mailinfo, "from", from); + xs_dict_append(mailinfo, "to", email); + xs_dict_append(mailinfo, "body", xs_fmt("%s%s", header, body)); - enqueue_email(email_body, 0); + enqueue_email(mailinfo, 0); } /* telegram */ @@ -2461,32 +2464,19 @@ int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req) } -int send_email(const char *msg) +int send_email(const xs_dict *mailinfo) /* invoke sendmail with email headers and body in msg */ { - FILE *f; - int status; - int fds[2]; - pid_t pid; - if (pipe(fds) == -1) return -1; - pid = vfork(); - if (pid == -1) return -1; - else if (pid == 0) { - dup2(fds[0], 0); - close(fds[0]); - close(fds[1]); - execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL); - _exit(1); - } - close(fds[0]); - if ((f = fdopen(fds[1], "w")) == NULL) { - close(fds[1]); - return -1; - } - fprintf(f, "%s\n", msg); - fclose(f); - if (waitpid(pid, &status, 0) == -1) return -1; - return status; + const xs_dict *smtp_cfg = xs_dict_get(srv_config, "smtp"); + const char + *url = xs_dict_get(smtp_cfg, "url"), + *user = xs_dict_get(smtp_cfg, "username"), + *pass = xs_dict_get(smtp_cfg, "password"), + *from = xs_dict_get(mailinfo, "from"), + *to = xs_dict_get(mailinfo, "to"), + *body = xs_dict_get(mailinfo, "body"); + + return xs_smtp_request(url, user, pass, from, to, body); } @@ -2749,7 +2739,7 @@ void process_queue_item(xs_dict *q_item) else if (strcmp(type, "email") == 0) { /* send this email */ - const xs_str *msg = xs_dict_get(q_item, "message"); + const xs_dict *msg = xs_dict_get(q_item, "message"); int retries = xs_number_get(xs_dict_get(q_item, "retries")); if (!send_email(msg)) -- cgit v1.2.3 From 4c1a2d24d374d00c656c4489db7d28f80d64f9dc Mon Sep 17 00:00:00 2001 From: shtrophic Date: Mon, 20 Jan 2025 22:59:30 +0100 Subject: add port parsing for sandboxing --- activitypub.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activitypub.c') diff --git a/activitypub.c b/activitypub.c index ca5cc3e..e5fc715 100644 --- a/activitypub.c +++ b/activitypub.c @@ -2465,9 +2465,9 @@ int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req) int send_email(const xs_dict *mailinfo) -/* invoke sendmail with email headers and body in msg */ +/* invoke curl */ { - const xs_dict *smtp_cfg = xs_dict_get(srv_config, "smtp"); + const xs_dict *smtp_cfg = xs_dict_get(srv_config, "email_notifications"); const char *url = xs_dict_get(smtp_cfg, "url"), *user = xs_dict_get(smtp_cfg, "username"), -- cgit v1.2.3 From 13e4a7cda59db29063efc5dad0823fe99ec7b764 Mon Sep 17 00:00:00 2001 From: shtrophic Date: Mon, 20 Jan 2025 23:03:27 +0100 Subject: fix memory leak --- activitypub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activitypub.c') diff --git a/activitypub.c b/activitypub.c index e5fc715..2b10435 100644 --- a/activitypub.c +++ b/activitypub.c @@ -970,7 +970,7 @@ void notify(snac *snac, const char *type, const char *utype, const char *actor, "\n", from, email, subject); - xs_dict *mailinfo = xs_dict_new(); + xs *mailinfo = xs_dict_new(); xs_dict_append(mailinfo, "from", from); xs_dict_append(mailinfo, "to", email); xs_dict_append(mailinfo, "body", xs_fmt("%s%s", header, body)); -- cgit v1.2.3 From 3d96c576287736ebdf87e4a7b956842a6c6e055f Mon Sep 17 00:00:00 2001 From: shtrophic Date: Fri, 24 Jan 2025 22:24:05 +0100 Subject: enforce tls when supported && add tests --- activitypub.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activitypub.c') diff --git a/activitypub.c b/activitypub.c index 4cb779a..f3b2bae 100644 --- a/activitypub.c +++ b/activitypub.c @@ -2538,8 +2538,9 @@ int send_email(const xs_dict *mailinfo) *from = xs_dict_get(mailinfo, "from"), *to = xs_dict_get(mailinfo, "to"), *body = xs_dict_get(mailinfo, "body"); + int smtp_port = parse_port(url, NULL); - return xs_smtp_request(url, user, pass, from, to, body); + return xs_smtp_request(url, user, pass, from, to, body, smtp_port == 465 || smtp_port == 587); } -- cgit v1.2.3 From 2ca7735779e79dbe6fe62f0111a12c145f428d8f Mon Sep 17 00:00:00 2001 From: shtrophic Date: Wed, 19 Feb 2025 08:56:14 +0100 Subject: fix ownership-problem of mailinfo --- activitypub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activitypub.c') diff --git a/activitypub.c b/activitypub.c index e2519e6..a91821d 100644 --- a/activitypub.c +++ b/activitypub.c @@ -1025,7 +1025,7 @@ void notify(snac *snac, const char *type, const char *utype, const char *actor, "\n", from, email, subject); - xs *mailinfo = xs_dict_new(); + xs_dict *mailinfo = xs_dict_new(); xs_dict_append(mailinfo, "from", from); xs_dict_append(mailinfo, "to", email); xs_dict_append(mailinfo, "body", xs_fmt("%s%s", header, body)); -- cgit v1.2.3 From 59a34a9646e28cc9a63194ff55547de0005d0110 Mon Sep 17 00:00:00 2001 From: shtrophic Date: Thu, 20 Feb 2025 17:13:12 +0100 Subject: Revert "fix ownership-problem of mailinfo" This reverts commit 2ca7735779e79dbe6fe62f0111a12c145f428d8f. --- activitypub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activitypub.c') diff --git a/activitypub.c b/activitypub.c index a91821d..e2519e6 100644 --- a/activitypub.c +++ b/activitypub.c @@ -1025,7 +1025,7 @@ void notify(snac *snac, const char *type, const char *utype, const char *actor, "\n", from, email, subject); - xs_dict *mailinfo = xs_dict_new(); + xs *mailinfo = xs_dict_new(); xs_dict_append(mailinfo, "from", from); xs_dict_append(mailinfo, "to", email); xs_dict_append(mailinfo, "body", xs_fmt("%s%s", header, body)); -- cgit v1.2.3 From d19fe4157ced8789e43b7c061b11fcc7baf91c78 Mon Sep 17 00:00:00 2001 From: shtrophic Date: Thu, 20 Feb 2025 18:15:48 +0100 Subject: use xs_dict_append correctly --- activitypub.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activitypub.c') diff --git a/activitypub.c b/activitypub.c index e2519e6..a209abd 100644 --- a/activitypub.c +++ b/activitypub.c @@ -1026,9 +1026,9 @@ void notify(snac *snac, const char *type, const char *utype, const char *actor, from, email, subject); xs *mailinfo = xs_dict_new(); - xs_dict_append(mailinfo, "from", from); - xs_dict_append(mailinfo, "to", email); - xs_dict_append(mailinfo, "body", xs_fmt("%s%s", header, body)); + mailinfo = xs_dict_append(mailinfo, "from", from); + mailinfo = xs_dict_append(mailinfo, "to", email); + mailinfo = xs_dict_append(mailinfo, "body", xs_fmt("%s%s", header, body)); enqueue_email(mailinfo, 0); } -- cgit v1.2.3 From 7f936d2d1a88840209895c6091801e8c4cfdaaa1 Mon Sep 17 00:00:00 2001 From: shtrophic Date: Thu, 24 Apr 2025 12:18:23 +0200 Subject: default to `smtp://localhost` --- activitypub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activitypub.c') diff --git a/activitypub.c b/activitypub.c index 061d1f8..df68151 100644 --- a/activitypub.c +++ b/activitypub.c @@ -2571,7 +2571,7 @@ int send_email(const xs_dict *mailinfo) { const xs_dict *smtp_cfg = xs_dict_get(srv_config, "email_notifications"); const char - *url = xs_dict_get(smtp_cfg, "url"), + *url = xs_dict_get_def(smtp_cfg, "url", "smtp://localhost"), *user = xs_dict_get(smtp_cfg, "username"), *pass = xs_dict_get(smtp_cfg, "password"), *from = xs_dict_get(mailinfo, "from"), -- cgit v1.2.3