Change indent spaces to tabs

This commit is contained in:
2019-04-16 14:26:37 +02:00
parent a74fee2b3c
commit 77ebc47a63
17 changed files with 1001 additions and 331 deletions
+235
View File
@@ -0,0 +1,235 @@
/******************************************************************************
*
* 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 <cStuff/string.h>
#include <curl/curl.h>
#include "mail.h"
struct payload
{
char now[40];
char *to;
char *from;
char *subject;
char *smtp_user;
char *smtp_pass;
char *smtp_url;
char *data;
size_t offset;
size_t total;
};
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))
{
cf (size > left)
size = left;
memcpy(ptr, payload->data[payload->offset], size);
payload->offset += size;
return size;
}
return 0;
}
static void
payload__get_now(char * now, size_t size)
{
time_t now = time(NULL);
struct tm tm;
gmtime_r(&now, &tm);
strftime(now, size, "%a, %d %b %Y %H:%M:%S GMT", &tm);
}
static char *
payload__get_id(uuid_t uuid, char * server)
{
size_t sz = strlen(server);
char * result = malloc( sz + 3 + 36 + 1 );
if (result)
{
char * p =result;
*(p++) = '<';
uuid_unparse(uuid, p);
p += 36;
*(p++) = '@';
strncpy(p, server, sz);
p += sz;
*(p++) = '>';
*p = 0;
}
return result;
}
static struct payload *
payload_new(aislx_mail_t mail, const char * server)
{
size_t total;
struct payload *payload;
if (!(payload = calloc(1, sizeof(struct payload))))
goto except;
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 ( !(payload->message_id = payload__get_id(mail->msg_id, server)) )
goto e_cleanup;
total = cstuff_sprintf( &payload->data, AISLX_MAIL_FORMAT
, date
, payload->to
, payload->from
, mail->reply_to
, payload->message_id
, mail->subject
, mail->message );
if (!(total > 0))
goto e_cleanup;
payload->total = total;
return payload;
e_cleanup:
payload_free(payload);
except:
return payload;
}
static void
payload_free(struct payload *payload)
{
if (payload->data)
free(payload->data);
free(payload);
}
static void *
aislx_mail_execute(void * p_ctx)
{
void *result = NULL;
struct payload * payload = (struct payload *)p_ctx;
aisl_status_t status = AISL_MALLOC_ERROR;
CURL *curl;
struct curl_slist *rcpts;
if ((curl = curl_easy_init()) != NULL)
{
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_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)
{
CURLcode res;
if ((res = curl_easy_perform(curl)) != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n"
, curl_easy_strerror(res));
status = AISL_INPUT_ERROR;
}
else
{
status = AISL_SUCCESS;
}
curl_slist_free_all(rcpts);
}
curl_easy_cleanup(curl);
}
return result + (int)status;
}
aisl_status_t
aislx_mail_send( aislx_mail_t mail,
const char *smtp_server,
const char *smtp_user,
const char *smtp_pass,
int flags )
{
struct payload * payload;
if (!(payload = payload_new(mail, smtp_server)))
return AISL_MALLOC_ERROR;
if (pthread_create(&mail->thread, NULL, aislx_mail_execute, (void*)payload))
{
payload_free(payload);
return AISL_SYSCALL_ERROR;
}
return AISL_SUCCESS;
}
aisl_status_t
aislx_mail_get_status(aislx_mail_t mail)
{
int rc;
void * retval;
rc = pthread_tryjoin_np(mail->thread, &retval);
if (rc == 0)
{
rc = (int)(retval-0);
return rc;
}
else
{
return AISL_IDLE;
}
}
+65
View File
@@ -0,0 +1,65 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file mail.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Mail sending component header file
*
* @see https://lowenware.com/aisl/
*/
#ifndef AISLX_MAIL_H_2738BEC4_CF82_4D77_A41F_0E2615848194
#define AISLX_MAIL_H_2738BEC4_CF82_4D77_A41F_0E2615848194
#include <pthread.h>
#include <aisl/aisl.h>
#include <uuid/uuid.h>
#ifndef AISLX_MAIL_FORMAT
#define AISLX_MAIL_FORMAT \
"Date: %s\r\n" \
"To: %s\r\n" \
"From: %s\r\n" \
"Reply-To: %s\r\n" \
"Message-ID: %s\r\n" \
"Subject: %s\r\n" \
"\r\n" \
"%s" \
"\r\n" \
#endif
struct aislx_mail
{
pthread_t pthread;
const char *to;
const char *from;
const char *subject;
const char *msg;
uuid_t msg_id;
};
typedef struct aislx_mail * aislx_mail_t;
aisl_status_t
aislx_mail_send( aislx_mail_t mail,
const char *smtp_server,
const char *smtp_user,
const char *smtp_pass,
int flags );
aisl_status_t
aislx_mail_get_status(aislx_mail_t mail);
#endif /* !AISLX_MAIL_H */
+92 -92
View File
@@ -18,146 +18,146 @@
aisl_status_t
aislx_query_init( aislx_query_t query,
size_t total,
aislx_query_on_var on_var,
void * p_ctx )
size_t total,
aislx_query_on_var on_var,
void * p_ctx )
{
query->on_var = on_var;
query->p_ctx = p_ctx;
query->data = NULL;
query->size = 0;
query->total = 0;
query->separator = 0;
query->on_var = on_var;
query->p_ctx = p_ctx;
query->data = NULL;
query->size = 0;
query->total = 0;
query->separator = 0;
return AISL_SUCCESS;
return AISL_SUCCESS;
}
void
aislx_query_release(aislx_query_t query)
{
if (query->data)
free(query->data);
if (query->data)
free(query->data);
}
static void
aislx_query_notify(const char * key, uint32_t k_len,
const cahr * val, uint32_t v_len,
aislx_query_t query)
const cahr * val, uint32_t v_len,
aislx_query_t query)
{
if (query->on_var)
{
if (query->on_var(key, k_len, val, v_len, query->p_ctx) != 0)
query->on_var == NULL;
}
if (query->on_var)
{
if (query->on_var(key, k_len, val, v_len, query->p_ctx) != 0)
query->on_var == NULL;
}
}
static int32_t
aislx_query_process(aislx_query_t query, const char * data, int32_t length)
{
const char * i = data,
* key = data,
* val = NULL;
const char * i = data,
* key = data,
* val = NULL;
uint32_t k_len = 0;
uint32_t k_len = 0;
size_t result;
size_t result;
for (result=0; result < length; result++)
{
char c;
for (result=0; result < length; result++)
{
char c;
switch( (c = *i) )
{
case '=':
if (val)
return -1;
switch( (c = *i) )
{
case '=':
if (val)
return -1;
k_len = i-key;
val = i+1;
break;
k_len = i-key;
val = i+1;
break;
case '&':
case ';'
if (!val)
return -1;
case '&':
case ';'
if (!val)
return -1;
if (!query->separator)
query->separator = c;
if (!query->separator)
query->separator = c;
if (query->separator == c)
{
aislx_query_notify(key, k_len, val, i-val, query);
/* reset parser */
key = (c == ';') ? i+2 : i+1;
k_len = 0;
val = NULL;
break;
}
}
i++;
}
if (query->separator == c)
{
aislx_query_notify(key, k_len, val, i-val, query);
/* reset parser */
key = (c == ';') ? i+2 : i+1;
k_len = 0;
val = NULL;
break;
}
}
i++;
}
if (query->total == result)
{
aislx_query_notify(key, k_len, val, i-val, query);
}
else
{
result = (key > i) ? i - data : key - data;
}
if (query->total == result)
{
aislx_query_notify(key, k_len, val, i-val, query);
}
else
{
result = (key > i) ? i - data : key - data;
}
query->total -= result;
query->total -= result;
return result;
return result;
}
aisl_status_t
aislx_query_feed(aislx_query_t query, char * data, int32_t length)
{
size_t processed;
char * source;
size_t processed;
char * source;
if (query->data)
{
if (!(source = realloc(query->data, length + query->size)))
return AISL_MALLOC_ERROR;
if (query->data)
{
if (!(source = realloc(query->data, length + query->size)))
return AISL_MALLOC_ERROR;
length += query->size;
query->data = source;
query->size = length;
length += query->size;
query->data = source;
query->size = length;
memcpy(&source[query->size], data, length);
}
else
source = data;
memcpy(&source[query->size], data, length);
}
else
source = data;
processed = aislx_query_process(query, source, length);
processed = aislx_query_process(query, source, length);
if (processed < 0)
return AISL_INPUT_ERROR;
if (processed < 0)
return AISL_INPUT_ERROR;
if (processed < length)
{
char * buffer;
if (processed < length)
{
char * buffer;
length -= processed;
if (!(buffer = malloc(length)))
return AISL_MALLOC_ERROR;
length -= processed;
if (!(buffer = malloc(length)))
return AISL_MALLOC_ERROR;
memcpy(buffer, &source[processed], length);
memcpy(buffer, &source[processed], length);
if (query->data)
free(query->data);
if (query->data)
free(query->data);
query->data = buffer;
query->size = length;
}
query->data = buffer;
query->size = length;
}
return AISL_SUCCESS;
return AISL_SUCCESS;
}
+6 -6
View File
@@ -27,13 +27,13 @@ typedef int
struct aislx_query
{
aislx_query_on_var on_var;
aislx_query_on_var on_var;
void * p_ctx;
char * data;
size_t size;
size_t total;
char separator;
void * p_ctx;
char * data;
size_t size;
size_t total;
char separator;
};
typedef struct aislx_query * aislx_query_t;
+32
View File
@@ -0,0 +1,32 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file quick.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/
*/
#include "quick.h"
aisl_status_t
aislx_quick_response(aisl_stream_t stream, aisl_http_response_t http_response)
{
aisl_status_t result;
result = aisl_response(s, http_response, 0);
if (result == AISL_SUCCESS)
aisl_flush(s);
else
aisl_reject(s);
return result;
}
+24
View File
@@ -0,0 +1,24 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file quick.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Quick AISL responses component header file
*
* @see https://lowenware.com/aisl/
*/
#ifndef AISLX_QUICK_H_39A26EF5_2352_4D54_A10C_203CBBEDF1DF
#define AISLX_QUICK_H_39A26EF5_2352_4D54_A10C_203CBBEDF1DF
#include <aisl/aisl.h>
aisl_status_t
aislx_quick_response(aisl_stream_t stream, aisl_http_response_t http_response);
#endif /* !AISLX_QUICK_H */
+119
View File
@@ -0,0 +1,119 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file validate.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Validation component source file
*
* @see https://lowenware.com/aisl/
*/
#include "validate.h"
int
aislx_validate_email(const char * value)
{
const char * at = NULL,
* dot = NULL,
* i = value;
char c;
while ( (c = *i) != 0 )
{
switch(c)
{
case '@':
if (at || i == value) break;
at = i++;
dot = NULL;
continue;
case '"':
case '(':
case ')':
case ',':
case ';':
case ':':
case '<':
case '>':
case '[':
case '\\':
case ']':
case ' ':
/* These characters are allowed in double quotes only, but it is not
* implemented in this validator, so we count them as forbidden
*/
break;
case '!':
case '#':
case '$':
case '%':
case '&':
case '\'':
case '*':
case '+':
case '/':
case '=':
case '?':
case '^':
case '_':
case '`':
case '{':
case '|':
case '}':
case '~':
/* Allowed special local-part characters */
if (!at)
{
i++;
continue;
}
else
break;
case '-':
/* Allowed characters */
i++;
continue;
case '.':
if (dot && i == dot+1)
break;
if (at)
{
if (i == at+1 || *(i+1) == 0)
break;
}
else
{
if (i == value || *(i+1) == '@')
break;
}
dot = i++;
continue;
default:
if ( isalnum(c) == 0 )
continue;
break;
}
return (int)(i-value)+1;
}
if (!at || !dot)
return -1;
return 0;
}
+23
View File
@@ -0,0 +1,23 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file validate.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Validation component header file
*
* @see https://lowenware.com/
*/
#ifndef AISLX_VALIDATE_H_5EEF42FB_E99B_4768_AF28_E548CF72D2E6
#define AISLX_VALIDATE_H_5EEF42FB_E99B_4768_AF28_E548CF72D2E6
int
aislx_validate_email(const char * value);
#endif /* !AISLX_VALIDATE_H */