Change indent spaces to tabs

This commit is contained in:
Ilja Kartašov 2019-04-16 14:26:37 +02:00
parent a74fee2b3c
commit 77ebc47a63
17 changed files with 1001 additions and 331 deletions

View File

@ -54,9 +54,9 @@ cstuff_log_printf(cstuff_log_t self, uint8_t level, const char * format, ...);
int int
cstuff_log_vprintf(cstuff_log_t self, cstuff_log_vprintf( cstuff_log_t self,
uint8_t level, uint8_t level,
const char * format, const char * format,
va_list vl); va_list vl );
#endif /* !CSTUFF_LOG_H */ #endif /* !CSTUFF_LOG_H */

99
cStuff/string.c Normal file
View File

@ -0,0 +1,99 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file string.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief cStuff string module implementation
*
* @see https://lowenware.com/
*/
#include "string.h"
int
cstuff_strcpy(char **out, const char *in)
{
return cstuff_strncpy(out, in, strlen(in));
}
int
cstuff_strncpy(char **out, const char *in, int len)
{
char *s;
if (!(s = malloc( len + 1 )))
return -1;
strncpy(s, in, len);
s[len]=0;
*out = s;
return len;
}
int
cstuff_strset(char **out, const char *in)
{
return cstuff_strnset(out, in, strlen(in));
}
int
cstuff_strnset(char **out, const char *in, int len)
{
char *s;
if (!(s = realloc(*out, len+1)))
return -1;
strncpy(s, in, len);
s[len] = 0;
*out = s;
return len;
}
int
cstuff_sprintf(char **out, const char *format, ...)
{
int result;
va_list vl;
va_start(vl, format);
result = cstuff_vsprintf(out, format, vl);
va_end(vl);
return result;
}
int
cstuff_vsprintf(char **out, const char *format, va_list args)
{
int result;
va_list vc;
char tmp, *s;
va_copy(vc, args);
result = vsprintf(&tmp, 1, format, vc);
va_end(vc);
if (!(s = malloc(result + 1)))
return -1;
result = vsprintf(s, result+1, format, args);
return result;
}

58
cStuff/string.h Normal file
View File

@ -0,0 +1,58 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file string.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief String module declarations
*
* @see https://lowenware.com/
*/
#ifndef CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87
#define CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87
#include <string.h>
#include <stdarg.h>
#include "retcode.h"
/** @brief Copies in string to newly allocated out buffer
* @param out a pointer to an address where new pointer must be stored
* @param in an input string
* @return length of string or -1 if out of memory
*/
int
cstuff_strcpy(char **out, const char *in);
int
cstuff_strncpy(char **out, const char *in, int len);
/** @brief Overwrites content of out buffer with in string using realloc
* @param out a pointer to an address where new string must be stored
* @param in an input string
* @return length of string or -1 if out of memory
*/
int
cstuff_strset(char **out, const char *in);
/** @brief Allocates memory and prints a formatted string into it
* @param out a pointer to an address where new string must be stored
* @param format a format for string (see manual for stdlib sprintf)
* @return length of string or -1 if out of memory
*/
int
cstuff_sprintf(char **out, const char *format, ...);
int
cstuff_vsprintf(char **out, const char *format, va_list args);
#endif /* !CSTUFF_STRING_H */

235
components/mail.c Normal file
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
components/mail.h Normal file
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 */

32
components/quick.c Normal file
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
components/quick.h Normal file
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
components/validate.c Normal file
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
components/validate.h Normal file
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 */

View File

@ -12,7 +12,8 @@
* *
* @see https://lowenware.com/ * @see https://lowenware.com/
*/ */
#include <stdlib.h>
#include <string.h>
#include "interface.h" #include "interface.h"

View File

@ -36,7 +36,7 @@ typedef struct aislx_mod * aislx_mod_t;
/** @brief Pointer to AISL stream event observer /** @brief Pointer to AISL stream event observer
*/ */
typedef aisl_status_t typedef aisl_status_t
(* aislx_mod_observer_t)(aislx_mode * aislx_mod_t, aisl_evt_t const evt); (* aislx_mod_observer_t)(aislx_mod_t mod, aisl_evt_t const evt);
/** @brief Root level AISL mod's structure /** @brief Root level AISL mod's structure
@ -66,8 +66,8 @@ typedef struct aislx_mod_ctx * aislx_mod_ctx_t;
* @param mod an instance of #aislx_mod_t * @param mod an instance of #aislx_mod_t
* @return pointer to newly allocated #aisl_mox_ctx_t * @return pointer to newly allocated #aisl_mox_ctx_t
*/ */
aisl_mod_ctx_t aislx_mod_ctx_t
aisl_mod_ctx_new(aislx_mod_t mod); aislx_mod_ctx_new(aislx_mod_t mod);
/** @brief Frees previosly allocated #aislx_mod_ctx_t /** @brief Frees previosly allocated #aislx_mod_ctx_t

View File

@ -26,7 +26,7 @@ struct context
struct aislx_mail mail; struct aislx_mail mail;
char * email; char * email;
char * message; char * msg;
}; };
typedef struct context * context_t; typedef struct context * context_t;
@ -41,17 +41,17 @@ context_free(context_t ctx)
if (ctx->email) if (ctx->email)
free(ctx->email); free(ctx->email);
if (ctx->message) if (ctx->msg)
free(ctx->message); free(ctx->msg);
aislx_mod_ctx_free(ctx); aislx_mod_ctx_free(ctx);
} }
static int static int
on_input_var( const char * key, int k_len, on_input_var( const char *key, int k_len,
const char * val, int v_len, const char *val, int v_len,
void * p_ctx ) void *p_ctx )
{ {
context_t ctx = (context_t) p_ctx; context_t ctx = (context_t) p_ctx;
aislx_mod_feedback_t mod = (aislx_mod_feedback_t)((aislx_mod_ctx_t)ctx)->mod; aislx_mod_feedback_t mod = (aislx_mod_feedback_t)((aislx_mod_ctx_t)ctx)->mod;
@ -62,10 +62,10 @@ on_input_var( const char * key, int k_len,
return cstuff_strncpy(&ctx->email, val, v_len); return cstuff_strncpy(&ctx->email, val, v_len);
} }
if (!ctx->message && k_len == mod->name_message_length) if (!ctx->msg && k_len == mod->name_msg_length)
{ {
if (strncmp(key, mod->name_message, k_len)==0) if (strncmp(key, mod->name_msg, k_len)==0)
return cstuff_strncpy(&ctx->message, val, v_len); return cstuff_strncpy(&ctx->msg, val, v_len);
} }
return 0; return 0;
@ -81,7 +81,11 @@ on_stream_open(aislx_mod_feedback_t mod, aisl_evt_stream_open_t const evt)
if (!(ctx = (context_t)aislx_mod_ctx_new(mod))) if (!(ctx = (context_t)aislx_mod_ctx_new(mod)))
return AISL_MALLOC_ERROR; return AISL_MALLOC_ERROR;
if (aislx_mail_init(&ctx->mail) != 0)
goto except;
ctx->mail.from = mod->mail_from; ctx->mail.from = mod->mail_from;
ctx->mail.reply_to = mod->mail_reply_to;
ctx->mail.subject = mod->mail_subject; ctx->mail.subject = mod->mail_subject;
ctx->mail.smtp_server = mod->smtp_server; ctx->mail.smtp_server = mod->smtp_server;
ctx->mail.smtp_user = mod->smtp_user; ctx->mail.smtp_user = mod->smtp_user;
@ -91,6 +95,10 @@ on_stream_open(aislx_mod_feedback_t mod, aisl_evt_stream_open_t const evt)
aisl_set_context(s, ctx); aisl_set_context(s, ctx);
return AISL_SUCCESS; return AISL_SUCCESS;
except:
context_free(ctx);
return AISL_MALLOC_ERROR;
} }
@ -132,11 +140,12 @@ on_stream_request(aislx_mod_feedback_t mod, aisl_evt_t const evt)
aislx_query_flush(ctx->qs); aislx_query_flush(ctx->qs);
/* verify input */ /* verify input */
if (!ctx->email || !ctx->message || aislx_validate_email(ctx->email) != 0) if (!ctx->email || !ctx->msg || aislx_validate_email(ctx->email) != 0)
aislx_quick_response(s, AISL_HTTP_BAD_REQUEST); aislx_quick_response(s, AISL_HTTP_BAD_REQUEST);
ctx->mail.to = ctx->email; ctx->mail.to = ctx->email;
ctx->mail.message = ctx->message; ctx->mail.msg = ctx->msg;
uuid_generate(ctx->mail.msg_id);
/* create thread */ /* create thread */
if (aislx_mail_send(&ctx->mail) != AISL_SUCCESS) if (aislx_mail_send(&ctx->mail) != AISL_SUCCESS)
@ -214,15 +223,16 @@ aislx_mod_feedback_init(aislx_mod_feedback_t mod, aislx_mod_feedback_cfg_t cfg)
mod->mail_subject = cfg->mail_subject; mod->mail_subject = cfg->mail_subject;
mod->mail_from = cfg->mail_from; mod->mail_from = cfg->mail_from;
mod->mail_reply_to = cfg->mail_reply_to;
mod->name_email = cfg->name_email; mod->name_email = cfg->name_email;
mod->name_message = cfg->name_message; mod->name_msg = cfg->name_msg;
mod->smtp_server = cfg->smtp_server; mod->smtp_server = cfg->smtp_server;
mod->smtp_user = cfg->smtp_user; mod->smtp_user = cfg->smtp_user;
mod->smtp_password = cfg->smtp_password; mod->smtp_password = cfg->smtp_password;
mod->smtp_port = cfg->smtp_port; mod->smtp_port = cfg->smtp_port;
mod->name_email_length = strlen(cfg->name_email); mod->name_email_length = strlen(cfg->name_email);
mod->name_message_length = strlen(cfg->name_message); mod->name_msg_length = strlen(cfg->name_msg);
return AISL_SUCCESS; return AISL_SUCCESS;
} }

View File

@ -21,33 +21,36 @@
struct aislx_mod_feedback_cfg struct aislx_mod_feedback_cfg
{ {
const char * end_point; const char *end_point;
const char * mail_subject; const char *mail_subject;
const char * mail_from; const char *mail_from;
const char * name_email; const char *mail_to;
const char * name_message; const char *name_email;
const char * smtp_server; const char *name_msg;
const char * smtp_user; const char *smtp_host;
const char * smtp_password; const char *smtp_user;
const char *smtp_pass;
uint16_t smtp_port; uint16_t smtp_port;
}; };
typedef struct aislx_mod_feedback_cfg * aislx_mod_feedback_cfg_t;
struct aislx_mod_feedback struct aislx_mod_feedback
{ {
struct aislx_mod root; struct aislx_mod root;
const char * mail_subject; const char *mail_subject;
const char * mail_from; const char *mail_from;
const char * name_email; const char *name_email;
const char * name_message; const char *name_msg;
const char * smtp_server; const char *smtp_host;
const char * smtp_user; const char *smtp_user;
const char * smtp_password; const char *smtp_pass;
uint16_t smtp_port; uint16_t smtp_port;
uint16_t name_email_length; uint16_t name_email_length;
uint16_t name_message_length; uint16_t name_msg_length;
}; };
@ -55,7 +58,8 @@ typedef struct aislx_mod_feedback * aislx_mod_feedback_t;
aisl_status_t aisl_status_t
aislx_mod_feedback_init(aislx_mod_feedback_t mod, aislx_mod_feedback_cfg_t cfg); aislx_mod_feedback_init(aislx_mod_feedback_t mod,
aislx_mod_feedback_cfg_t cfg);
void void