diff options
Diffstat (limited to '')
| -rw-r--r-- | activitypub.c | 48 | ||||
| -rw-r--r-- | data.c | 2 | ||||
| -rw-r--r-- | xs_curl.h | 36 |
3 files changed, 56 insertions, 30 deletions
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, | |||
| 962 | 962 | ||
| 963 | xs *subject = xs_fmt("snac notify for @%s@%s", | 963 | xs *subject = xs_fmt("snac notify for @%s@%s", |
| 964 | xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host")); | 964 | xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host")); |
| 965 | xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host")); | 965 | xs *from = xs_fmt("<snac-daemon@%s>", xs_dict_get(srv_config, "host")); |
| 966 | xs *header = xs_fmt( | 966 | xs *header = xs_fmt( |
| 967 | "From: %s\n" | 967 | "From: snac-daemon %s\n" |
| 968 | "To: %s\n" | 968 | "To: %s\n" |
| 969 | "Subject: %s\n" | 969 | "Subject: %s\n" |
| 970 | "\n", | 970 | "\n", |
| 971 | from, email, subject); | 971 | from, email, subject); |
| 972 | 972 | ||
| 973 | xs *email_body = xs_fmt("%s%s", header, body); | 973 | xs_dict *mailinfo = xs_dict_new(); |
| 974 | xs_dict_append(mailinfo, "from", from); | ||
| 975 | xs_dict_append(mailinfo, "to", email); | ||
| 976 | xs_dict_append(mailinfo, "body", xs_fmt("%s%s", header, body)); | ||
| 974 | 977 | ||
| 975 | enqueue_email(email_body, 0); | 978 | enqueue_email(mailinfo, 0); |
| 976 | } | 979 | } |
| 977 | 980 | ||
| 978 | /* telegram */ | 981 | /* telegram */ |
| @@ -2461,32 +2464,19 @@ int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req) | |||
| 2461 | } | 2464 | } |
| 2462 | 2465 | ||
| 2463 | 2466 | ||
| 2464 | int send_email(const char *msg) | 2467 | int send_email(const xs_dict *mailinfo) |
| 2465 | /* invoke sendmail with email headers and body in msg */ | 2468 | /* invoke sendmail with email headers and body in msg */ |
| 2466 | { | 2469 | { |
| 2467 | FILE *f; | 2470 | const xs_dict *smtp_cfg = xs_dict_get(srv_config, "smtp"); |
| 2468 | int status; | 2471 | const char |
| 2469 | int fds[2]; | 2472 | *url = xs_dict_get(smtp_cfg, "url"), |
| 2470 | pid_t pid; | 2473 | *user = xs_dict_get(smtp_cfg, "username"), |
| 2471 | if (pipe(fds) == -1) return -1; | 2474 | *pass = xs_dict_get(smtp_cfg, "password"), |
| 2472 | pid = vfork(); | 2475 | *from = xs_dict_get(mailinfo, "from"), |
| 2473 | if (pid == -1) return -1; | 2476 | *to = xs_dict_get(mailinfo, "to"), |
| 2474 | else if (pid == 0) { | 2477 | *body = xs_dict_get(mailinfo, "body"); |
| 2475 | dup2(fds[0], 0); | 2478 | |
| 2476 | close(fds[0]); | 2479 | return xs_smtp_request(url, user, pass, from, to, body); |
| 2477 | close(fds[1]); | ||
| 2478 | execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL); | ||
| 2479 | _exit(1); | ||
| 2480 | } | ||
| 2481 | close(fds[0]); | ||
| 2482 | if ((f = fdopen(fds[1], "w")) == NULL) { | ||
| 2483 | close(fds[1]); | ||
| 2484 | return -1; | ||
| 2485 | } | ||
| 2486 | fprintf(f, "%s\n", msg); | ||
| 2487 | fclose(f); | ||
| 2488 | if (waitpid(pid, &status, 0) == -1) return -1; | ||
| 2489 | return status; | ||
| 2490 | } | 2480 | } |
| 2491 | 2481 | ||
| 2492 | 2482 | ||
| @@ -2749,7 +2739,7 @@ void process_queue_item(xs_dict *q_item) | |||
| 2749 | else | 2739 | else |
| 2750 | if (strcmp(type, "email") == 0) { | 2740 | if (strcmp(type, "email") == 0) { |
| 2751 | /* send this email */ | 2741 | /* send this email */ |
| 2752 | const xs_str *msg = xs_dict_get(q_item, "message"); | 2742 | const xs_dict *msg = xs_dict_get(q_item, "message"); |
| 2753 | int retries = xs_number_get(xs_dict_get(q_item, "retries")); | 2743 | int retries = xs_number_get(xs_dict_get(q_item, "retries")); |
| 2754 | 2744 | ||
| 2755 | if (!send_email(msg)) | 2745 | if (!send_email(msg)) |
| @@ -3195,7 +3195,7 @@ void enqueue_output_by_actor(snac *snac, const xs_dict *msg, | |||
| 3195 | } | 3195 | } |
| 3196 | 3196 | ||
| 3197 | 3197 | ||
| 3198 | void enqueue_email(const xs_str *msg, int retries) | 3198 | void enqueue_email(const xs_dict *msg, int retries) |
| 3199 | /* enqueues an email message to be sent */ | 3199 | /* enqueues an email message to be sent */ |
| 3200 | { | 3200 | { |
| 3201 | xs *qmsg = _new_qmsg("email", msg, retries); | 3201 | xs *qmsg = _new_qmsg("email", msg, retries); |
| @@ -9,6 +9,9 @@ xs_dict *xs_http_request(const char *method, const char *url, | |||
| 9 | const xs_str *body, int b_size, int *status, | 9 | const xs_str *body, int b_size, int *status, |
| 10 | xs_str **payload, int *p_size, int timeout); | 10 | xs_str **payload, int *p_size, int timeout); |
| 11 | 11 | ||
| 12 | int xs_smtp_request(const char *url, const char *user, const char *pass, | ||
| 13 | const char *from, const char *to, const xs_str *body); | ||
| 14 | |||
| 12 | #ifdef XS_IMPLEMENTATION | 15 | #ifdef XS_IMPLEMENTATION |
| 13 | 16 | ||
| 14 | #include <curl/curl.h> | 17 | #include <curl/curl.h> |
| @@ -194,6 +197,39 @@ xs_dict *xs_http_request(const char *method, const char *url, | |||
| 194 | return response; | 197 | return response; |
| 195 | } | 198 | } |
| 196 | 199 | ||
| 200 | int xs_smtp_request(const char *url, const char *user, const char *pass, | ||
| 201 | const char *from, const char *to, const xs_str *body) | ||
| 202 | { | ||
| 203 | CURL *curl; | ||
| 204 | CURLcode res = CURLE_OK; | ||
| 205 | struct curl_slist *rcpt = NULL; | ||
| 206 | struct _payload_data pd = { | ||
| 207 | .data = (char *)body, | ||
| 208 | .size = xs_size(body), | ||
| 209 | .offset = 0 | ||
| 210 | }; | ||
| 211 | |||
| 212 | curl = curl_easy_init(); | ||
| 213 | |||
| 214 | curl_easy_setopt(curl, CURLOPT_URL, url); | ||
| 215 | curl_easy_setopt(curl, CURLOPT_USERNAME, user); | ||
| 216 | curl_easy_setopt(curl, CURLOPT_PASSWORD, pass); | ||
| 217 | |||
| 218 | curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from); | ||
| 219 | curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt = curl_slist_append(rcpt, to)); | ||
| 220 | |||
| 221 | curl_easy_setopt(curl, CURLOPT_READDATA, &pd); | ||
| 222 | curl_easy_setopt(curl, CURLOPT_READFUNCTION, _post_callback); | ||
| 223 | curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); | ||
| 224 | |||
| 225 | res = curl_easy_perform(curl); | ||
| 226 | |||
| 227 | curl_slist_free_all(rcpt); | ||
| 228 | curl_easy_cleanup(curl); | ||
| 229 | |||
| 230 | return (int)res; | ||
| 231 | } | ||
| 232 | |||
| 197 | #endif /* XS_IMPLEMENTATION */ | 233 | #endif /* XS_IMPLEMENTATION */ |
| 198 | 234 | ||
| 199 | #endif /* _XS_CURL_H */ | 235 | #endif /* _XS_CURL_H */ |