aisl-sdk/components/mail.c

259 lines
5.0 KiB
C
Raw Normal View History

2019-04-16 14:26:37 +02:00
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file mail.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Mail sending component source file
*
* @see https://lowenware.com/aisl/
*/
#include <stdlib.h>
#include <string.h>
2019-04-16 14:26:37 +02:00
#include <cStuff/string.h>
#include <curl/curl.h>
#include <components/log.h>
2019-04-16 14:26:37 +02:00
#include "mail.h"
struct payload {
char now[40];
2019-04-16 14:26:37 +02:00
char *to;
char *from;
char *subject;
char *msg_id;
2019-04-16 14:26:37 +02:00
char *smtp_user;
char *smtp_pass;
char *smtp_url;
char *data;
size_t offset;
size_t total;
2019-04-16 14:26:37 +02:00
};
static size_t
payload_feed(void *ptr, size_t size, size_t nmemb, void *userp)
{
struct payload *payload = (struct payload *)userp;
size_t left;
left = payload->total - payload->offset;
if (left && (size = size*nmemb)) {
if (size > left)
2019-04-16 14:26:37 +02:00
size = left;
memcpy(ptr, &payload->data[payload->offset], size);
2019-04-16 14:26:37 +02:00
payload->offset += size;
return size;
}
return 0;
}
static void
payload__get_now(char *now, size_t size)
2019-04-16 14:26:37 +02:00
{
time_t time_now = time(NULL);
2019-04-16 14:26:37 +02:00
struct tm tm;
gmtime_r(&time_now, &tm);
2019-04-16 14:26:37 +02:00
strftime(now, size, "%a, %d %b %Y %H:%M:%S GMT", &tm);
}
static char *
payload__get_id(uuid_t uuid, const char *server)
2019-04-16 14:26:37 +02:00
{
size_t sz = strlen(server);
char *result = malloc( sz + 3 + 36 + 1 );
2019-04-16 14:26:37 +02:00
if (result) {
2019-04-16 14:26:37 +02:00
char * p =result;
*(p++) = '<';
uuid_unparse(uuid, p);
p += 36;
*(p++) = '@';
strncpy(p, server, sz);
p += sz;
*(p++) = '>';
*p = 0;
}
return result;
}
static void
payload_free(struct payload *payload)
{
if (payload->data)
free(payload->data);
if (payload->to)
free(payload->to);
if (payload->from)
free(payload->from);
if (payload->subject)
free(payload->subject);
if (payload->msg_id)
free(payload->msg_id);
if (payload->smtp_user)
free(payload->smtp_user);
if (payload->smtp_pass)
free(payload->smtp_pass);
if (payload->smtp_url)
free(payload->smtp_url);
free(payload);
}
2019-04-16 14:26:37 +02:00
static struct payload *
payload_new(AxMail mail, struct ax_mail_smtp *smtp)
2019-04-16 14:26:37 +02:00
{
size_t total;
struct payload *payload;
if (!(payload = calloc(1, sizeof(struct payload))))
goto except;
2019-04-16 14:26:37 +02:00
payload__get_now(payload->now, sizeof(payload->now));
if (cstuff_strcpy(&payload->to, mail->to) == -1)
goto e_cleanup;
if (cstuff_strcpy(&payload->from, mail->from) == -1)
goto e_cleanup;
if (cstuff_strcpy(&payload->smtp_user, smtp->user) == -1)
goto e_cleanup;
if (cstuff_strcpy(&payload->smtp_pass, smtp->pass) == -1)
goto e_cleanup;
if ( !(payload->msg_id = payload__get_id(mail->msg_id, smtp->host)) )
2019-04-16 14:26:37 +02:00
goto e_cleanup;
if (cstuff_sprintf(&payload->smtp_url, "smtps://%s:%d/", smtp->host,
smtp->port & 0xFFFF) == -1) {
goto e_cleanup;
}
total = cstuff_sprintf( &payload->data, AX_MAIL_FORMAT
, payload->now
, payload->to
, payload->from
, mail->reply_to
, payload->msg_id
, mail->subject
, mail->msg );
2019-04-16 14:26:37 +02:00
if (!(total > 0))
goto e_cleanup;
payload->total = total;
return payload;
e_cleanup:
payload_free(payload);
except:
return payload;
}
static void *
ax_mail_execute(void *p_ctx)
2019-04-16 14:26:37 +02:00
{
struct payload * payload = (struct payload *)p_ctx;
AislStatus status = AISL_MALLOC_ERROR;
2019-04-16 14:26:37 +02:00
CURL *curl;
struct curl_slist *rcpts;
if ((curl = curl_easy_init()) != NULL) {
2019-04-16 14:26:37 +02:00
curl_easy_setopt(curl, CURLOPT_USERNAME, payload->smtp_user);
curl_easy_setopt(curl, CURLOPT_PASSWORD, payload->smtp_pass);
curl_easy_setopt(curl, CURLOPT_URL, payload->smtp_url);
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long) CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
2019-04-16 14:26:37 +02:00
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_feed);
curl_easy_setopt(curl, CURLOPT_READDATA, payload);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, payload->from);
if ((rcpts = curl_slist_append(NULL, payload->to)) != NULL) {
2019-04-16 14:26:37 +02:00
CURLcode res;
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpts);
if ((res = curl_easy_perform(curl)) != CURLE_OK) {
2019-04-16 14:26:37 +02:00
fprintf(stderr, "curl_easy_perform() failed: %s\n"
, curl_easy_strerror(res));
status = AISL_INPUT_ERROR;
} else {
2019-04-16 14:26:37 +02:00
status = AISL_SUCCESS;
}
curl_slist_free_all(rcpts);
}
curl_easy_cleanup(curl);
}
return (void*)status;
2019-04-16 14:26:37 +02:00
}
AislStatus
ax_mail_send(AxMail mail, struct ax_mail_smtp *smtp, int flags)
2019-04-16 14:26:37 +02:00
{
int rc;
struct payload *payload;
2019-04-16 14:26:37 +02:00
if (!(payload = payload_new(mail, smtp)))
2019-04-16 14:26:37 +02:00
return AISL_MALLOC_ERROR;
rc = pthread_create(&mail->thread, NULL, ax_mail_execute, (void*)payload);
if (rc) {
2019-04-16 14:26:37 +02:00
payload_free(payload);
return AISL_SYSCALL_ERROR;
}
return AISL_SUCCESS;
}
AislStatus
ax_mail_get_status(AxMail mail)
2019-04-16 14:26:37 +02:00
{
int rc;
void * retval;
rc = pthread_tryjoin_np(mail->thread, &retval);
if (rc == 0) {
rc = (AislStatus)retval;
2019-04-16 14:26:37 +02:00
return rc;
} else {
2019-04-16 14:26:37 +02:00
return AISL_IDLE;
}
}