Update coding style and make feedback module working

This commit is contained in:
Ilja Kartašov 2019-04-25 13:54:07 +02:00
parent 6fba6f42a0
commit dc52c20098
16 changed files with 304 additions and 246 deletions

24
components/log.c Normal file
View File

@ -0,0 +1,24 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file log.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/
*/
#include "log.h"
#if AX_LOG_ENABLED == 1
struct cstuff_log axLog;
#endif /* AX_LOG_ENABLED == 1 */

51
components/log.h Normal file
View File

@ -0,0 +1,51 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file log.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Log macro wrapper header-only module
*
* @see https://lowenware.com/
*/
#ifndef LOG_H_97A757CA_02E8_4D9C_BE42_29A930928F49
#define LOG_H_97A757CA_02E8_4D9C_BE42_29A930928F49
#include <cStuff/log.h>
#ifndef AX_LOG_ENABLED
#define AX_LOG_ENABLED 1
#endif
#if AX_LOG_ENABLED == 1
extern struct cstuff_log axLog;
#define AX_LOG_INIT(LEVEL) cstuff_log_init(&axLog, LEVEL)
#define AX_LOG_RELEASE() cstuff_log_release(&axLog)
#define AX_LOG_DEBUG(...) cstuff_log_printf(&axLog, CSTUFF_LOG_DEBUG, __VA_ARGS__)
#define AX_LOG_STATE(...) cstuff_log_printf(&axLog, CSTUFF_LOG_STATE, __VA_ARGS__)
#define AX_LOG_ALERT(...) cstuff_log_printf(&axLog, CSTUFF_LOG_ALERT, __VA_ARGS__)
#define AX_LOG_ERROR(...) cstuff_log_printf(&axLog, CSTUFF_LOG_ERROR, __VA_ARGS__)
#else
#define AX_LOG_DECLARE()
#define AX_LOG_INIT(TARGET)
#define AX_LOG_RELEASE()
#define AX_LOG_DEBUG(...)
#define AX_LOG_STATE(...)
#define AX_LOG_ALERT(...)
#define AX_LOG_ERROR(...)
#endif
#endif /* !LOG_H */

View File

@ -17,11 +17,12 @@
#include <string.h>
#include <cStuff/string.h>
#include <curl/curl.h>
#include <components/log.h>
#include "mail.h"
struct payload {
char now[40];
char now[40];
char *to;
char *from;
char *subject;
@ -30,8 +31,8 @@ struct payload {
char *smtp_pass;
char *smtp_url;
char *data;
size_t offset;
size_t total;
size_t offset;
size_t total;
};
@ -122,7 +123,7 @@ payload_free(struct payload *payload)
static struct payload *
payload_new(aislx_mail_t mail, const char *server)
payload_new(AxMail mail, struct ax_mail_smtp *smtp)
{
size_t total;
struct payload *payload;
@ -130,6 +131,7 @@ payload_new(aislx_mail_t mail, const char *server)
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)
@ -138,10 +140,21 @@ payload_new(aislx_mail_t mail, const char *server)
if (cstuff_strcpy(&payload->from, mail->from) == -1)
goto e_cleanup;
if ( !(payload->msg_id = payload__get_id(mail->msg_id, server)) )
if (cstuff_strcpy(&payload->smtp_user, smtp->user) == -1)
goto e_cleanup;
total = cstuff_sprintf( &payload->data, AISLX_MAIL_FORMAT
if (cstuff_strcpy(&payload->smtp_pass, smtp->pass) == -1)
goto e_cleanup;
if ( !(payload->msg_id = payload__get_id(mail->msg_id, smtp->host)) )
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
@ -166,10 +179,10 @@ except:
static void *
aislx_mail_execute(void *p_ctx)
ax_mail_execute(void *p_ctx)
{
struct payload * payload = (struct payload *)p_ctx;
aisl_status_t status = AISL_MALLOC_ERROR;
AislStatus status = AISL_MALLOC_ERROR;
CURL *curl;
struct curl_slist *rcpts;
@ -180,6 +193,7 @@ aislx_mail_execute(void *p_ctx)
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);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_feed);
curl_easy_setopt(curl, CURLOPT_READDATA, payload);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
@ -190,6 +204,8 @@ aislx_mail_execute(void *p_ctx)
if ((rcpts = curl_slist_append(NULL, payload->to)) != NULL) {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpts);
if ((res = curl_easy_perform(curl)) != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n"
, curl_easy_strerror(res));
@ -206,20 +222,16 @@ aislx_mail_execute(void *p_ctx)
}
aisl_status_t
aislx_mail_send( aislx_mail_t mail,
const char *smtp_server,
const char *smtp_user,
const char *smtp_pass,
int flags )
AislStatus
ax_mail_send(AxMail mail, struct ax_mail_smtp *smtp, int flags)
{
int rc;
struct payload *payload;
if (!(payload = payload_new(mail, smtp_server)))
if (!(payload = payload_new(mail, smtp)))
return AISL_MALLOC_ERROR;
rc = pthread_create(&mail->thread, NULL, aislx_mail_execute, (void*)payload);
rc = pthread_create(&mail->thread, NULL, ax_mail_execute, (void*)payload);
if (rc) {
payload_free(payload);
return AISL_SYSCALL_ERROR;
@ -228,8 +240,8 @@ aislx_mail_send( aislx_mail_t mail,
}
aisl_status_t
aislx_mail_get_status(aislx_mail_t mail)
AislStatus
ax_mail_get_status(AxMail mail)
{
int rc;
void * retval;
@ -237,7 +249,7 @@ aislx_mail_get_status(aislx_mail_t mail)
rc = pthread_tryjoin_np(mail->thread, &retval);
if (rc == 0) {
rc = (aisl_status_t)retval;
rc = (AislStatus)retval;
return rc;
} else {
return AISL_IDLE;

View File

@ -13,8 +13,8 @@
* @see https://lowenware.com/aisl/
*/
#ifndef AISLX_MAIL_H_2738BEC4_CF82_4D77_A41F_0E2615848194
#define AISLX_MAIL_H_2738BEC4_CF82_4D77_A41F_0E2615848194
#ifndef MAIL_H_2738BEC4_CF82_4D77_A41F_0E2615848194
#define MAIL_H_2738BEC4_CF82_4D77_A41F_0E2615848194
#include <pthread.h>
@ -22,9 +22,9 @@
#include <uuid/uuid.h>
#ifndef AISLX_MAIL_FORMAT
#ifndef AX_MAIL_FORMAT
#define AISLX_MAIL_FORMAT \
#define AX_MAIL_FORMAT \
"Date: %s\r\n" \
"To: %s\r\n" \
"From: %s\r\n" \
@ -37,8 +37,7 @@
#endif
struct aislx_mail
{
struct ax_mail {
pthread_t thread;
const char *to;
const char *reply_to;
@ -48,19 +47,23 @@ struct aislx_mail
uuid_t msg_id;
};
typedef struct aislx_mail * aislx_mail_t;
typedef struct ax_mail * AxMail;
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 ax_mail_smtp {
const char *user;
const char *pass;
const char *host;
uint16_t port;
};
aisl_status_t
aislx_mail_get_status(aislx_mail_t mail);
AislStatus
ax_mail_send(AxMail mail, struct ax_mail_smtp *smtp, int flags);
#endif /* !AISLX_MAIL_H */
AislStatus
ax_mail_get_status(AxMail mail);
#endif /* !MAIL_H */

View File

@ -16,20 +16,18 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "log.h"
#include "query.h"
aisl_status_t
aislx_query_init( aislx_query_t query,
size_t total,
aislx_query_on_var on_var,
void * p_ctx )
AislStatus
ax_query_init(AxQuery query, size_t total, AxQueryHandler 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->total = total;
query->separator = 0;
return AISL_SUCCESS;
@ -37,7 +35,7 @@ aislx_query_init( aislx_query_t query,
void
aislx_query_release(aislx_query_t query)
ax_query_release(AxQuery query)
{
if (query->data)
free(query->data);
@ -45,9 +43,9 @@ aislx_query_release(aislx_query_t query)
static void
aislx_query_notify(const char * key, uint32_t k_len,
const char * val, uint32_t v_len,
aislx_query_t query)
ax_query_notify(const char *key, uint32_t k_len,
const char *val, uint32_t v_len,
AxQuery query)
{
if (query->on_var) {
if (query->on_var(key, k_len, val, v_len, query->p_ctx) != 0)
@ -57,7 +55,7 @@ aislx_query_notify(const char * key, uint32_t k_len,
static int32_t
aislx_query_process(aislx_query_t query, const char * data, int32_t length)
ax_query_process(AxQuery query, const char *data, int32_t length)
{
const char *i = data,
*key = data,
@ -86,7 +84,7 @@ aislx_query_process(aislx_query_t query, const char * data, int32_t length)
query->separator = c;
if (query->separator == c) {
aislx_query_notify(key, k_len, val, i-val, query);
ax_query_notify(key, k_len, val, i-val, query);
/* reset parser */
key = (c == ';') ? i+2 : i+1;
k_len = 0;
@ -98,7 +96,7 @@ aislx_query_process(aislx_query_t query, const char * data, int32_t length)
}
if (query->total == result) {
aislx_query_notify(key, k_len, val, i-val, query);
ax_query_notify(key, k_len, val, i-val, query);
} else {
result = (key > i) ? i - data : key - data;
}
@ -109,8 +107,8 @@ aislx_query_process(aislx_query_t query, const char * data, int32_t length)
}
aisl_status_t
aislx_query_feed(aislx_query_t query, const char *data, int32_t length)
AislStatus
ax_query_feed(AxQuery query, const char *data, int32_t length)
{
size_t processed;
const char *source;
@ -128,7 +126,7 @@ aislx_query_feed(aislx_query_t query, const char *data, int32_t length)
source = data;
}
processed = aislx_query_process(query, source, length);
processed = ax_query_process(query, source, length);
if (processed < 0)
return AISL_INPUT_ERROR;
@ -137,6 +135,7 @@ aislx_query_feed(aislx_query_t query, const char *data, int32_t length)
char *buffer;
length -= processed;
if (!(buffer = malloc(length)))
return AISL_MALLOC_ERROR;

View File

@ -13,8 +13,8 @@
* @see https://lowenware.com/aisl
*/
#ifndef AISLX_QUERY_H_9306EAEB_6DFC_4936_934A_6472F00E490C
#define AISLX_QUERY_H_9306EAEB_6DFC_4936_934A_6472F00E490C
#ifndef QUERY_H_9306EAEB_6DFC_4936_934A_6472F00E490C
#define QUERY_H_9306EAEB_6DFC_4936_934A_6472F00E490C
#include <stdint.h>
#include <stddef.h>
@ -22,14 +22,13 @@
typedef int
(*aislx_query_on_var)( const char *key, uint32_t k_len,
const char *val, uint32_t v_len,
void *p_ctx );
(*AxQueryHandler)(const char *key, uint32_t k_len,
const char *val, uint32_t v_len,
void *p_ctx );
struct aislx_query
{
aislx_query_on_var on_var;
struct ax_query {
AxQueryHandler on_var;
void *p_ctx;
char *data;
size_t size;
@ -37,22 +36,19 @@ struct aislx_query
char separator;
};
typedef struct aislx_query * aislx_query_t;
typedef struct ax_query * AxQuery;
aisl_status_t
aislx_query_init( aislx_query_t query,
size_t total,
aislx_query_on_var on_var,
void *p_ctx );
AislStatus
ax_query_init(AxQuery query, size_t total, AxQueryHandler on_var, void *p_ctx);
void
aislx_query_release(aislx_query_t query);
ax_query_release(AxQuery query);
aisl_status_t
aislx_query_feed(aislx_query_t query, const char *data, int32_t length);
AislStatus
ax_query_feed(AxQuery query, const char *data, int32_t length);
#endif /* !AISLX_QUERY_H */
#endif /* !QUERY_H */

View File

@ -16,10 +16,10 @@
#include "quick.h"
aisl_status_t
aislx_quick_response(aisl_stream_t stream, aisl_http_response_t http_response)
AislStatus
ax_quick_response(AislStream stream, AislHttpResponse http_response)
{
aisl_status_t result;
AislStatus result;
result = aisl_response(stream, http_response, 0);

View File

@ -13,12 +13,12 @@
* @see https://lowenware.com/aisl/
*/
#ifndef AISLX_QUICK_H_39A26EF5_2352_4D54_A10C_203CBBEDF1DF
#define AISLX_QUICK_H_39A26EF5_2352_4D54_A10C_203CBBEDF1DF
#ifndef QUICK_H_39A26EF5_2352_4D54_A10C_203CBBEDF1DF
#define 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);
AislStatus
ax_quick_response(AislStream stream, AislHttpResponse http_response);
#endif /* !AISLX_QUICK_H */
#endif /* !QUICK_H */

View File

@ -20,7 +20,7 @@
int
aislx_validate_email(const char *value)
ax_validate_email(const char *value)
{
const char *at = NULL,
*dot = NULL,

View File

@ -13,11 +13,11 @@
* @see https://lowenware.com/
*/
#ifndef AISLX_VALIDATE_H_5EEF42FB_E99B_4768_AF28_E548CF72D2E6
#define AISLX_VALIDATE_H_5EEF42FB_E99B_4768_AF28_E548CF72D2E6
#ifndef VALIDATE_H_5EEF42FB_E99B_4768_AF28_E548CF72D2E6
#define VALIDATE_H_5EEF42FB_E99B_4768_AF28_E548CF72D2E6
int
aislx_validate_email(const char * value);
ax_validate_email(const char *value);
#endif /* !AISLX_VALIDATE_H */
#endif /* !AX_VALIDATE_H */

View File

@ -13,13 +13,13 @@
* @see https://lowenware.com/
*/
#include "ctx.h"
#include "context.h"
aislx_ctx_t
aislx_ctx_new(aislx_module_t mod)
AxContext
ax_context_new(AxModule mod)
{
aislx_ctx_t ctx;
AxContext ctx;
if ((ctx = calloc(1, mod->ctx_size)) != NULL) {
ctx->mod = mod;
@ -30,7 +30,7 @@ aislx_ctx_new(aislx_module_t mod)
void
aislx_ctx_free(aislx_ctx_t ctx)
ax_context_free(AxContext ctx)
{
free(ctx);
}

View File

@ -13,36 +13,36 @@
* @see https://lowenware.com/aisl/
*/
#ifndef AISLX_CTX_H_683B0A86_2A15_483D_B0F9_7BFBB4BA068F
#define AISLX_CTX_H_683B0A86_2A15_483D_B0F9_7BFBB4BA068F
#ifndef CONTEXT_H_683B0A86_2A15_483D_B0F9_7BFBB4BA068F
#define CONTEXT_H_683B0A86_2A15_483D_B0F9_7BFBB4BA068F
#define AISLX_CTX(x) ((aislx_ctx_t)x)
#define AX_CONTEXT(x) ((AxContext)x)
#include <mods/module.h>
/** @brief Root level ASIL mod's context structure
*/
struct aislx_ctx {
aislx_module_t mod; /**< Mod's pointer */
struct ax_context {
AxModule mod; /**< Mod's pointer */
};
/** @brief Root level ASIL mod's context structure pointer
*/
typedef struct aislx_ctx * aislx_ctx_t;
typedef struct ax_context * AxContext;
/** @brief Allocates zeroed #aislx_t context
* @param mod an instance of #aislx_t
* @return pointer to newly allocated #aisl_mox_ctx_t
*/
aislx_ctx_t
aislx_ctx_new(aislx_module_t mod);
AxContext
ax_context_new(AxModule mod);
/** @brief Frees previosly allocated #aislx_ctx_t
* @param ctx an instance of mod's context structure
* */
void
aislx_ctx_free(aislx_ctx_t ctx);
ax_context_free(AxContext ctx);
#endif /* !AISLX_CTX_H */

View File

@ -18,27 +18,27 @@
#include <components/mail.h>
#include <components/validate.h>
#include <components/quick.h>
#include <components/log.h>
#include <cStuff/string.h>
#include "feedback.h"
struct context
{
struct aislx_ctx root;
struct aislx_query qs;
struct aislx_mail mail;
char * email;
char * msg;
struct ax_context root;
struct ax_query qs;
struct ax_mail mail;
char *email;
char *msg;
};
typedef struct context * context_t;
typedef struct context * Context;
static void
context_free(context_t ctx)
context_free(Context ctx)
{
aislx_query_release(&ctx->qs);
ax_query_release(&ctx->qs);
if (ctx->email)
free(ctx->email);
@ -46,7 +46,7 @@ context_free(context_t ctx)
if (ctx->msg)
free(ctx->msg);
aislx_ctx_free((aislx_ctx_t)ctx);
ax_context_free((AxContext)ctx);
}
@ -55,32 +55,30 @@ on_input_var( const char *key, uint32_t k_len,
const char *val, uint32_t v_len,
void *p_ctx )
{
context_t ctx = (context_t) p_ctx;
aislx_feedback_t mod = (aislx_feedback_t)((aislx_ctx_t)ctx)->mod;
Context ctx = (Context) p_ctx;
AxFeedback mod = (AxFeedback)((AxContext)ctx)->mod;
if (!ctx->email && k_len == mod->name_email_length)
{
if (!ctx->email && k_len == mod->name_email_length) {
if (strncmp(key, mod->name_email, k_len)==0)
return cstuff_strncpy(&ctx->email, val, v_len);
return (cstuff_strncpy(&ctx->email, val, v_len) == -1);
}
if (!ctx->msg && k_len == mod->name_msg_length)
{
if (!ctx->msg && k_len == mod->name_msg_length) {
if (strncmp(key, mod->name_msg, k_len)==0)
return cstuff_strncpy(&ctx->msg, val, v_len);
return (cstuff_strncpy(&ctx->msg, val, v_len) == -1);
}
return 0;
}
static aisl_status_t
on_stream_open(aislx_feedback_t mod, aisl_evt_stream_open_t const evt)
static AislStatus
on_stream_open(AxFeedback mod, const struct aisl_evt_open *evt)
{
context_t ctx;
aisl_stream_t s = (aisl_stream_t) (evt->evt.source);
Context ctx;
AislStream s = (AislStream) (evt->evt.source);
if (!(ctx = (context_t)aislx_ctx_new(AISLX_MODULE(mod))))
if (!(ctx = (Context)ax_context_new(AX_MODULE(mod))))
return AISL_MALLOC_ERROR;
ctx->mail.from = mod->mail_from;
@ -99,60 +97,50 @@ on_stream_open(aislx_feedback_t mod, aisl_evt_stream_open_t const evt)
}
static aisl_status_t
on_stream_header(aislx_feedback_t mod, aisl_evt_stream_header_t const evt)
static AislStatus
on_stream_header(AxFeedback mod, const struct aisl_evt_header *evt)
{
aisl_stream_t s = (aisl_stream_t)evt->evt.source;
context_t ctx = aisl_get_context(s);
AislStream s = (AislStream)evt->evt.source;
Context ctx = aisl_get_context(s);
if (strcmp(evt->key, "content-length")==0)
{
if (strcmp(evt->key, "content-length") == 0) {
size_t total = strtoll(evt->value, NULL, 10);
if(aislx_query_init(&ctx->qs, total, on_input_var, (void*)ctx) != 0)
{
aislx_quick_response(s, AISL_HTTP_INTERNAL_SERVER_ERROR);
if(ax_query_init(&ctx->qs, total, on_input_var, (void*)ctx) != 0) {
ax_quick_response(s, AISL_HTTP_INTERNAL_SERVER_ERROR);
}
}
return AISL_SUCCESS;
}
static aisl_status_t
on_stream_input(aislx_feedback_t mod, aisl_evt_stream_input_t const evt)
static AislStatus
on_stream_input(AxFeedback mod, const struct aisl_evt_input *evt)
{
context_t ctx = aisl_get_context((aisl_stream_t)evt->evt.source);
Context ctx = aisl_get_context((AislStream)evt->evt.source);
aislx_query_feed(&ctx->qs, evt->data, evt->size);
ax_query_feed(&ctx->qs, evt->data, evt->size);
return AISL_SUCCESS;
}
static aisl_status_t
on_stream_request(aislx_feedback_t mod, aisl_evt_t const evt)
static AislStatus
on_stream_request(AxFeedback mod, const struct aisl_evt *evt)
{
aisl_status_t status;
context_t ctx = aisl_get_context((aisl_stream_t)evt->source);
aisl_stream_t s = (aisl_stream_t)evt->source;
Context ctx = aisl_get_context((AislStream)evt->source);
AislStream s = (AislStream)evt->source;
/* verify input */
if (!ctx->email || !ctx->msg || aislx_validate_email(ctx->email) != 0)
aislx_quick_response(s, AISL_HTTP_BAD_REQUEST);
if (!ctx->email || !ctx->msg || ax_validate_email(ctx->email) != 0)
ax_quick_response(s, AISL_HTTP_BAD_REQUEST);
ctx->mail.to = ctx->email;
ctx->mail.reply_to = ctx->email;
ctx->mail.msg = ctx->msg;
uuid_generate(ctx->mail.msg_id);
/* create thread */
status = aislx_mail_send(&ctx->mail
, mod->smtp_host
, mod->smtp_user
, mod->smtp_pass
, 0);
if (status != AISL_SUCCESS)
aislx_quick_response(s, AISL_HTTP_INTERNAL_SERVER_ERROR);
if (ax_mail_send(&ctx->mail, &mod->smtp, 0) != AISL_SUCCESS)
ax_quick_response(s, AISL_HTTP_INTERNAL_SERVER_ERROR);
aisl_set_output_event(s, true); /**< enable output event */
@ -160,49 +148,48 @@ on_stream_request(aislx_feedback_t mod, aisl_evt_t const evt)
}
static aisl_status_t
on_stream_output(aislx_feedback_t mod, aisl_evt_t const evt)
static AislStatus
on_stream_output(AxFeedback mod, const struct aisl_evt *evt)
{
context_t ctx = aisl_get_context((aisl_stream_t)evt->source);
Context ctx = aisl_get_context((AislStream)evt->source);
aisl_http_response_t rc = AISL_HTTP_OK;
AislHttpResponse rc = AISL_HTTP_OK;
switch (aislx_mail_get_status(&ctx->mail))
{
case AISL_SUCCESS: break;
case AISL_IDLE: return AISL_SUCCESS;
switch (ax_mail_get_status(&ctx->mail)) {
case AISL_SUCCESS: break;
case AISL_IDLE: return AISL_SUCCESS;
default:
rc = AISL_HTTP_INTERNAL_SERVER_ERROR;
default:
rc = AISL_HTTP_INTERNAL_SERVER_ERROR;
}
aislx_quick_response((aisl_stream_t)evt->source, rc);
ax_quick_response((AislStream)evt->source, rc);
return AISL_SUCCESS;
}
static aisl_status_t
on_stream_close(aislx_feedback_t mod, aisl_evt_t const evt)
static AislStatus
on_stream_close(AxFeedback mod, const struct aisl_evt *evt)
{
context_free((context_t)aisl_get_context((aisl_stream_t)evt->source));
context_free((Context)aisl_get_context((AislStream)evt->source));
return AISL_SUCCESS;
}
static aisl_status_t
aislx_feedback_on_event(aislx_feedback_t mod, aisl_evt_t const evt)
static AislStatus
ax_feedback_on_event(AxFeedback mod, const struct aisl_evt *evt)
{
switch(evt->code)
{
case AISL_EVENT_STREAM_OPEN:
return on_stream_open(mod, (aisl_evt_stream_open_t)evt);
return on_stream_open(mod, (const struct aisl_evt_open *)evt);
case AISL_EVENT_STREAM_HEADER:
return on_stream_header(mod, (aisl_evt_stream_header_t)evt);
return on_stream_header(mod, (const struct aisl_evt_header *)evt);
case AISL_EVENT_STREAM_INPUT:
return on_stream_input(mod, (aisl_evt_stream_input_t)evt);
return on_stream_input(mod, (const struct aisl_evt_input *)evt);
case AISL_EVENT_STREAM_REQUEST:
return on_stream_request(mod, evt);
@ -219,20 +206,17 @@ aislx_feedback_on_event(aislx_feedback_t mod, aisl_evt_t const evt)
}
aisl_status_t
aislx_feedback_init(aislx_feedback_t mod, aislx_feedback_cfg_t cfg)
AislStatus
ax_feedback_init(AxFeedback mod, const struct ax_feedback_cfg *cfg)
{
AISLX_MODULE_INIT(aislx_feedback, cfg->end_point);
AX_MODULE_INIT(ax_feedback, cfg->end_point);
mod->mail_subject = cfg->mail_subject;
mod->mail_from = cfg->mail_from;
mod->mail_to = cfg->mail_to;
mod->name_email = cfg->name_email;
mod->name_msg = cfg->name_msg;
mod->smtp_host = cfg->smtp_host;
mod->smtp_user = cfg->smtp_user;
mod->smtp_pass = cfg->smtp_pass;
mod->smtp_port = cfg->smtp_port;
memcpy(&mod->smtp, &cfg->smtp, sizeof (cfg->smtp));
mod->name_email_length = strlen(cfg->name_email);
mod->name_msg_length = strlen(cfg->name_msg);
@ -242,7 +226,7 @@ aislx_feedback_init(aislx_feedback_t mod, aislx_feedback_cfg_t cfg)
void
aislx_feedback_release(aislx_feedback_t mod)
ax_feedback_release(AxFeedback mod)
{
}

View File

@ -13,57 +13,45 @@
* @see https://lowenware.com/aisl/
*/
#ifndef AISLX_MOD_FEEDBACK_H_6CC516E4_A7F2_4A9D_B467_75DCF6F58108
#define AISLX_MOD_FEEDBACK_H_6CC516E4_A7F2_4A9D_B467_75DCF6F58108
#ifndef FEEDBACK_H_6CC516E4_A7F2_4A9D_B467_75DCF6F58108
#define FEEDBACK_H_6CC516E4_A7F2_4A9D_B467_75DCF6F58108
#include <mods/ctx.h>
#include <mods/context.h>
#include <mods/module.h>
#include <components/mail.h>
struct aislx_feedback_cfg
{
const char *end_point;
const char *name_email;
const char *name_msg;
const char *mail_subject;
const char *mail_from;
const char *mail_to;
const char *smtp_host;
const char *smtp_user;
const char *smtp_pass;
uint16_t smtp_port;
};
typedef struct aislx_feedback_cfg * aislx_feedback_cfg_t;
struct aislx_feedback
{
struct aislx_module root;
const char *name_email;
const char *name_msg;
const char *mail_subject;
const char *mail_from;
const char *mail_to;
const char *smtp_host;
const char *smtp_user;
const char *smtp_pass;
uint16_t smtp_port;
uint16_t name_email_length;
uint16_t name_msg_length;
struct ax_feedback_cfg {
struct ax_mail_smtp smtp;
const char *end_point;
const char *name_email;
const char *name_msg;
const char *mail_subject;
const char *mail_from;
const char *mail_to;
};
typedef struct aislx_feedback * aislx_feedback_t;
struct ax_feedback {
struct ax_module root;
struct ax_mail_smtp smtp;
const char *name_email;
const char *name_msg;
const char *mail_subject;
const char *mail_from;
const char *mail_to;
uint16_t name_email_length;
uint16_t name_msg_length;
};
aisl_status_t
aislx_feedback_init(aislx_feedback_t mod,
aislx_feedback_cfg_t cfg);
typedef struct ax_feedback * AxFeedback;
AislStatus
ax_feedback_init(AxFeedback mod, const struct ax_feedback_cfg *cfg);
void
aislx_feedback_release(aislx_feedback_t mod);
ax_feedback_release(AxFeedback mod);
#endif /* !AISLX_MOD_FEEDBACK_H */
#endif /* !FEEDBACK_H */

View File

@ -14,20 +14,21 @@
*/
#include <stdlib.h>
#include <string.h>
#include <components/log.h>
#include "module.h"
#include "ctx.h"
#include "context.h"
aisl_status_t
aislx_module_on_event(aislx_module_t mod, aisl_evt_t const evt)
AislStatus
ax_module_on_event(AxModule mod, const struct aisl_evt *evt)
{
aislx_ctx_t ctx;
aisl_evt_stream_open_t so_evt;
AxContext ctx;
struct aisl_evt_open *so_evt;
switch(evt->code)
{
case AISL_EVENT_STREAM_OPEN:
so_evt = (aisl_evt_stream_open_t)evt;
so_evt = (struct aisl_evt_open *)evt;
if (strncmp(so_evt->path, mod->end_point, mod->ep_length))
return AISL_IDLE;
@ -38,7 +39,7 @@ aislx_module_on_event(aislx_module_t mod, aisl_evt_t const evt)
case AISL_EVENT_STREAM_REQUEST:
case AISL_EVENT_STREAM_OUTPUT:
case AISL_EVENT_STREAM_CLOSE:
if (!(ctx = aisl_get_context((aisl_stream_t)evt->source)))
if (!(ctx = aisl_get_context((AislStream)evt->source)))
return AISL_SUCCESS;
if (ctx->mod == mod)

View File

@ -13,50 +13,50 @@
* @see https://lowenware.com/aisl/
*/
#ifndef AISLX_MODULE_H_1AEFECA5_9341_431B_8194_EA0C2E76B5EA
#define AISLX_MODULE_H_1AEFECA5_9341_431B_8194_EA0C2E76B5EA
#ifndef MODULE_H_1AEFECA5_9341_431B_8194_EA0C2E76B5EA
#define MODULE_H_1AEFECA5_9341_431B_8194_EA0C2E76B5EA
#include <aisl/aisl.h>
#define AISLX_MODULE_INIT(MOD, END_POINT) \
#define AX_MODULE_INIT(MOD, END_POINT) \
do { \
((aislx_module_t) mod)->end_point = END_POINT; \
((aislx_module_t) mod)->ep_length = strlen(END_POINT); \
((aislx_module_t) mod)->on_event = (aislx_observer_t)MOD##_on_event; \
((aislx_module_t) mod)->ctx_size = sizeof(struct context); \
((AxModule) mod)->end_point = END_POINT; \
((AxModule) mod)->ep_length = strlen(END_POINT); \
((AxModule) mod)->on_event = (AxObserver)MOD##_on_event; \
((AxModule) mod)->ctx_size = sizeof (struct context); \
} while(0) \
#define AISLX_MODULE(x) ((aislx_module_t)x)
#define AX_MODULE(x) ((AxModule)x)
/** @brief AISL module structure pointer
*/
typedef struct aislx_module * aislx_module_t;
typedef struct ax_module * AxModule;
/** @brief Pointer to AISL stream event observer
*/
typedef aisl_status_t
(* aislx_observer_t)(aislx_module_t mod, aisl_evt_t const evt);
typedef AislStatus
(* AxObserver)(AxModule mod, const struct aisl_evt *evt);
/** @brief Root level AISL mod's structure
*/
struct aislx_module {
const char *end_point; /**< Root mod's URL or NULL */
aislx_observer_t on_event; /**< Mod's stream event observer */
size_t ctx_size; /**< Mod's context size */
uint8_t ep_length; /**< End-Point length */
struct ax_module {
const char *end_point; /**< Root mod's URL or NULL */
AxObserver on_event; /**< Mod's stream event observer */
size_t ctx_size; /**< Mod's context size */
uint8_t ep_length; /**< End-Point length */
};
/** @brief Wrapper function that calles mod's observer
* @param mod an instance of #aislx_t
* @param mod an instance of AxModule#
* @param evt an AISL stream event
* @return AISL_SUCCESS if event handled, AISL_IDLE if event not handled or an
* error code
*/
aisl_status_t
aislx_module_on_event(aislx_module_t mod, aisl_evt_t const evt);
AislStatus
ax_module_on_event(AxModule mod, const struct aisl_evt *evt);
#endif /* !AISLX_MODULE;_H */
#endif /* !MODULE;_H */