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 --- xs_curl.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'xs_curl.h') diff --git a/xs_curl.h b/xs_curl.h index f0cfd98..886aee0 100644 --- 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 @@ -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 */ -- 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 --- xs_curl.h | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'xs_curl.h') diff --git a/xs_curl.h b/xs_curl.h index 886aee0..d98ac4c 100644 --- a/xs_curl.h +++ b/xs_curl.h @@ -10,7 +10,8 @@ xs_dict *xs_http_request(const char *method, const char *url, 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); + const char *from, const char *to, const xs_str *body, + int use_ssl); #ifdef XS_IMPLEMENTATION @@ -198,7 +199,8 @@ xs_dict *xs_http_request(const char *method, const char *url, } int xs_smtp_request(const char *url, const char *user, const char *pass, - const char *from, const char *to, const xs_str *body) + const char *from, const char *to, const xs_str *body, + int use_ssl) { CURL *curl; CURLcode res = CURLE_OK; @@ -212,11 +214,19 @@ int xs_smtp_request(const char *url, const char *user, const char *pass, 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); + if (user && pass) { + /* allow authless connections, to, e.g. localhost */ + curl_easy_setopt(curl, CURLOPT_USERNAME, user); + curl_easy_setopt(curl, CURLOPT_PASSWORD, pass); + } + + if (use_ssl) + curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from); - curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt = curl_slist_append(rcpt, to)); + + rcpt = curl_slist_append(rcpt, to); + curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt); curl_easy_setopt(curl, CURLOPT_READDATA, &pd); curl_easy_setopt(curl, CURLOPT_READFUNCTION, _post_callback); @@ -224,8 +234,8 @@ int xs_smtp_request(const char *url, const char *user, const char *pass, res = curl_easy_perform(curl); - curl_slist_free_all(rcpt); curl_easy_cleanup(curl); + curl_slist_free_all(rcpt); return (int)res; } -- cgit v1.2.3 From 94a07f93d527ecef3376de566ccf7d74fa2dadd3 Mon Sep 17 00:00:00 2001 From: grunfink Date: Sun, 27 Apr 2025 06:01:05 +0200 Subject: Fixed length error in xs_smtp_request(). --- xs_curl.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'xs_curl.h') diff --git a/xs_curl.h b/xs_curl.h index 0609a08..ac543b1 100644 --- a/xs_curl.h +++ b/xs_curl.h @@ -200,6 +200,7 @@ 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, int use_ssl) @@ -209,7 +210,7 @@ int xs_smtp_request(const char *url, const char *user, const char *pass, struct curl_slist *rcpt = NULL; struct _payload_data pd = { .data = (char *)body, - .size = xs_size(body), + .size = strlen(body), .offset = 0 }; -- cgit v1.2.3 From 64d99af19ce864ffefec50fcf0d19d2d65411c35 Mon Sep 17 00:00:00 2001 From: grunfink Date: Sun, 4 May 2025 11:05:47 +0200 Subject: xs_webmention.h new file. --- xs_curl.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'xs_curl.h') diff --git a/xs_curl.h b/xs_curl.h index ac543b1..2301661 100644 --- a/xs_curl.h +++ b/xs_curl.h @@ -225,7 +225,7 @@ int xs_smtp_request(const char *url, const char *user, const char *pass, if (use_ssl) curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); - + curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from); rcpt = curl_slist_append(rcpt, to); @@ -236,7 +236,7 @@ int xs_smtp_request(const char *url, const char *user, const char *pass, curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); res = curl_easy_perform(curl); - + curl_easy_cleanup(curl); curl_slist_free_all(rcpt); -- cgit v1.2.3