summaryrefslogtreecommitdiff
path: root/activitypub.c
diff options
context:
space:
mode:
Diffstat (limited to 'activitypub.c')
-rw-r--r--activitypub.c99
1 files changed, 71 insertions, 28 deletions
diff --git a/activitypub.c b/activitypub.c
index 2403a62..dcbb79f 100644
--- a/activitypub.c
+++ b/activitypub.c
@@ -11,6 +11,7 @@
11#include "xs_set.h" 11#include "xs_set.h"
12#include "xs_match.h" 12#include "xs_match.h"
13#include "xs_unicode.h" 13#include "xs_unicode.h"
14#include "xs_webmention.h"
14 15
15#include "snac.h" 16#include "snac.h"
16 17
@@ -1044,17 +1045,21 @@ void notify(snac *snac, const char *type, const char *utype, const char *actor,
1044 1045
1045 xs *subject = xs_fmt("snac notify for @%s@%s", 1046 xs *subject = xs_fmt("snac notify for @%s@%s",
1046 xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host")); 1047 xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
1047 xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host")); 1048 xs *from = xs_fmt("<snac-daemon@%s>", xs_dict_get(srv_config, "host"));
1048 xs *header = xs_fmt( 1049 xs *header = xs_fmt(
1049 "From: %s\n" 1050 "From: snac-daemon %s\n"
1050 "To: %s\n" 1051 "To: %s\n"
1051 "Subject: %s\n" 1052 "Subject: %s\n"
1052 "\n", 1053 "\n",
1053 from, email, subject); 1054 from, email, subject);
1054 1055
1055 xs *email_body = xs_fmt("%s%s", header, body); 1056 xs *mailinfo = xs_dict_new();
1057 xs *bd = xs_fmt("%s%s", header, body);
1058 mailinfo = xs_dict_append(mailinfo, "from", from);
1059 mailinfo = xs_dict_append(mailinfo, "to", email);
1060 mailinfo = xs_dict_append(mailinfo, "body", bd);
1056 1061
1057 enqueue_email(email_body, 0); 1062 enqueue_email(mailinfo, 0);
1058 } 1063 }
1059 1064
1060 /* telegram */ 1065 /* telegram */
@@ -2567,32 +2572,53 @@ int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
2567} 2572}
2568 2573
2569 2574
2570int send_email(const char *msg) 2575int send_email(const xs_dict *mailinfo)
2571/* invoke sendmail with email headers and body in msg */ 2576/* invoke curl */
2572{ 2577{
2573 FILE *f; 2578 const char *url = xs_dict_get(srv_config, "smtp_url");
2574 int status; 2579
2575 int fds[2]; 2580 if (!xs_is_string(url) || *url == '\0') {
2576 pid_t pid; 2581 /* revert back to old sendmail pipe behaviour */
2577 if (pipe(fds) == -1) return -1; 2582 const char *msg = xs_dict_get(mailinfo, "body");
2578 pid = vfork(); 2583 FILE *f;
2579 if (pid == -1) return -1; 2584 int status;
2580 else if (pid == 0) { 2585 int fds[2];
2581 dup2(fds[0], 0); 2586 pid_t pid;
2587
2588 if (pipe(fds) == -1) return -1;
2589 pid = vfork();
2590 if (pid == -1) return -1;
2591 else if (pid == 0) {
2592 dup2(fds[0], 0);
2593 close(fds[0]);
2594 close(fds[1]);
2595 execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
2596 _exit(1);
2597 }
2582 close(fds[0]); 2598 close(fds[0]);
2583 close(fds[1]); 2599 if ((f = fdopen(fds[1], "w")) == NULL) {
2584 execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL); 2600 close(fds[1]);
2585 _exit(1); 2601 return -1;
2586 } 2602 }
2587 close(fds[0]); 2603 fprintf(f, "%s\n", msg);
2588 if ((f = fdopen(fds[1], "w")) == NULL) { 2604 fclose(f);
2589 close(fds[1]); 2605 if (waitpid(pid, &status, 0) == -1) return -1;
2590 return -1; 2606 return status;
2591 } 2607 }
2592 fprintf(f, "%s\n", msg); 2608
2593 fclose(f); 2609 const char
2594 if (waitpid(pid, &status, 0) == -1) return -1; 2610 *user = xs_dict_get(srv_config, "smtp_username"),
2595 return status; 2611 *pass = xs_dict_get(srv_config, "smtp_password"),
2612 *from = xs_dict_get(mailinfo, "from"),
2613 *to = xs_dict_get(mailinfo, "to"),
2614 *body = xs_dict_get(mailinfo, "body");
2615
2616 if (url == NULL || *url == '\0')
2617 url = "smtp://localhost";
2618
2619 int smtp_port = parse_port(url, NULL);
2620
2621 return xs_smtp_request(url, user, pass, from, to, body, smtp_port == 465 || smtp_port == 587);
2596} 2622}
2597 2623
2598 2624
@@ -2865,7 +2891,7 @@ void process_queue_item(xs_dict *q_item)
2865 else 2891 else
2866 if (strcmp(type, "email") == 0) { 2892 if (strcmp(type, "email") == 0) {
2867 /* send this email */ 2893 /* send this email */
2868 const xs_str *msg = xs_dict_get(q_item, "message"); 2894 const xs_dict *msg = xs_dict_get(q_item, "message");
2869 int retries = xs_number_get(xs_dict_get(q_item, "retries")); 2895 int retries = xs_number_get(xs_dict_get(q_item, "retries"));
2870 2896
2871 if (!send_email(msg)) 2897 if (!send_email(msg))
@@ -3005,6 +3031,23 @@ void process_queue_item(xs_dict *q_item)
3005 } 3031 }
3006 } 3032 }
3007 else 3033 else
3034 if (strcmp(type, "webmention") == 0) {
3035 const xs_dict *msg = xs_dict_get(q_item, "message");
3036 const char *source = xs_dict_get(msg, "id");
3037 const xs_list *atts = xs_dict_get(msg, "attachment");
3038 const xs_dict *att;
3039
3040 xs_list_foreach(atts, att) {
3041 const char *target = xs_dict_get(att, "url");
3042
3043 if (xs_is_string(source) && xs_is_string(target)) {
3044 int r = xs_webmention_send(source, target, USER_AGENT);
3045
3046 srv_debug(1, xs_fmt("webmention source=%s target=%s %d", source, target, r));
3047 }
3048 }
3049 }
3050 else
3008 srv_log(xs_fmt("unexpected q_item type '%s'", type)); 3051 srv_log(xs_fmt("unexpected q_item type '%s'", type));
3009} 3052}
3010 3053