commit 4e3d596bee1cee2c4146265eb6ac827f09820c53
parent 7add876a9334e87138be7edf72db35832755e118
Author: shtrophic <christoph@liebender.dev>
Date: Mon, 20 Jan 2025 19:43:42 +0100
add xs_smtp_request
Diffstat:
3 files changed, 56 insertions(+), 30 deletions(-)
diff --git 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 <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
+ xs *from = xs_fmt("<snac-daemon@%s>", 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))
diff --git a/data.c b/data.c
@@ -3195,7 +3195,7 @@ void enqueue_output_by_actor(snac *snac, const xs_dict *msg,
}
-void enqueue_email(const xs_str *msg, int retries)
+void enqueue_email(const xs_dict *msg, int retries)
/* enqueues an email message to be sent */
{
xs *qmsg = _new_qmsg("email", msg, retries);
diff --git a/xs_curl.h b/xs_curl.h
@@ -9,6 +9,9 @@ xs_dict *xs_http_request(const char *method, const char *url,
const xs_str *body, int b_size, int *status,
xs_str **payload, int *p_size, int timeout);
+int xs_smtp_request(const char *url, const char *user, const char *pass,
+ const char *from, const char *to, const xs_str *body);
+
#ifdef XS_IMPLEMENTATION
#include <curl/curl.h>
@@ -194,6 +197,39 @@ xs_dict *xs_http_request(const char *method, const char *url,
return response;
}
+int xs_smtp_request(const char *url, const char *user, const char *pass,
+ const char *from, const char *to, const xs_str *body)
+{
+ CURL *curl;
+ CURLcode res = CURLE_OK;
+ struct curl_slist *rcpt = NULL;
+ struct _payload_data pd = {
+ .data = (char *)body,
+ .size = xs_size(body),
+ .offset = 0
+ };
+
+ curl = curl_easy_init();
+
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+ curl_easy_setopt(curl, CURLOPT_USERNAME, user);
+ curl_easy_setopt(curl, CURLOPT_PASSWORD, pass);
+
+ curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
+ curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt = curl_slist_append(rcpt, to));
+
+ curl_easy_setopt(curl, CURLOPT_READDATA, &pd);
+ curl_easy_setopt(curl, CURLOPT_READFUNCTION, _post_callback);
+ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
+
+ res = curl_easy_perform(curl);
+
+ curl_slist_free_all(rcpt);
+ curl_easy_cleanup(curl);
+
+ return (int)res;
+}
+
#endif /* XS_IMPLEMENTATION */
#endif /* _XS_CURL_H */