aisl-sdk/mods/feedback.c

249 lines
5.3 KiB
C
Raw Normal View History

/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file mod-feedback.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief AISL feedback module source file
*
* @see https://lowenware.com/aisl/
*/
#include <stdlib.h>
#include <string.h>
2019-04-09 13:23:40 +02:00
#include <components/query.h>
#include <components/mail.h>
#include <components/validate.h>
#include <components/quick.h>
#include <cStuff/string.h>
#include "feedback.h"
struct context
{
struct aislx_ctx root;
struct aislx_query qs;
struct aislx_mail mail;
2019-04-16 14:26:37 +02:00
char * email;
char * msg;
};
typedef struct context * context_t;
static void
context_free(context_t ctx)
{
2019-04-16 14:26:37 +02:00
aislx_query_release(&ctx->qs);
2019-04-16 14:26:37 +02:00
if (ctx->email)
free(ctx->email);
2019-04-16 14:26:37 +02:00
if (ctx->msg)
free(ctx->msg);
aislx_ctx_free((aislx_ctx_t)ctx);
}
static int
on_input_var( const char *key, uint32_t k_len,
const char *val, uint32_t v_len,
void *p_ctx )
{
2019-04-16 14:26:37 +02:00
context_t ctx = (context_t) p_ctx;
aislx_feedback_t mod = (aislx_feedback_t)((aislx_ctx_t)ctx)->mod;
2019-04-16 14:26:37 +02:00
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);
}
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 0;
}
static aisl_status_t
on_stream_open(aislx_feedback_t mod, aisl_evt_stream_open_t const evt)
{
2019-04-16 14:26:37 +02:00
context_t ctx;
aisl_stream_t s = (aisl_stream_t) (evt->evt.source);
if (!(ctx = (context_t)aislx_ctx_new(AISLX_MODULE(mod))))
2019-04-16 14:26:37 +02:00
return AISL_MALLOC_ERROR;
ctx->mail.from = mod->mail_from;
ctx->mail.to = mod->mail_to;
ctx->mail.subject = mod->mail_subject;
2019-04-16 14:26:37 +02:00
aisl_set_context(s, ctx);
return AISL_SUCCESS;
/*
except:
context_free(ctx);
return AISL_MALLOC_ERROR;
*/
}
static aisl_status_t
on_stream_header(aislx_feedback_t mod, aisl_evt_stream_header_t const evt)
{
aisl_stream_t s = (aisl_stream_t)evt->evt.source;
context_t ctx = aisl_get_context(s);
2019-04-16 14:26:37 +02:00
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);
}
}
return AISL_SUCCESS;
}
static aisl_status_t
on_stream_input(aislx_feedback_t mod, aisl_evt_stream_input_t const evt)
{
2019-04-16 14:26:37 +02:00
context_t ctx = aisl_get_context((aisl_stream_t)evt->evt.source);
aislx_query_feed(&ctx->qs, evt->data, evt->size);
2019-04-16 14:26:37 +02:00
return AISL_SUCCESS;
}
static aisl_status_t
on_stream_request(aislx_feedback_t mod, aisl_evt_t const evt)
{
aisl_status_t status;
context_t ctx = aisl_get_context((aisl_stream_t)evt->source);
2019-04-16 14:26:37 +02:00
aisl_stream_t s = (aisl_stream_t)evt->source;
2019-04-16 14:26:37 +02:00
/* verify input */
if (!ctx->email || !ctx->msg || aislx_validate_email(ctx->email) != 0)
aislx_quick_response(s, AISL_HTTP_BAD_REQUEST);
2019-04-16 14:26:37 +02:00
ctx->mail.to = ctx->email;
ctx->mail.msg = ctx->msg;
uuid_generate(ctx->mail.msg_id);
2019-04-16 14:26:37 +02:00
/* create thread */
status = aislx_mail_send(&ctx->mail
, mod->smtp_host
, mod->smtp_user
, mod->smtp_pass
, 0);
if (status != AISL_SUCCESS)
2019-04-16 14:26:37 +02:00
aislx_quick_response(s, AISL_HTTP_INTERNAL_SERVER_ERROR);
2019-04-16 14:26:37 +02:00
aisl_set_output_event(s, true); /**< enable output event */
2019-04-16 14:26:37 +02:00
return AISL_SUCCESS;
}
static aisl_status_t
on_stream_output(aislx_feedback_t mod, aisl_evt_t const evt)
{
context_t ctx = aisl_get_context((aisl_stream_t)evt->source);
2019-04-16 14:26:37 +02:00
aisl_http_response_t rc = AISL_HTTP_OK;
2019-04-16 14:26:37 +02:00
switch (aislx_mail_get_status(&ctx->mail))
{
case AISL_SUCCESS: break;
case AISL_IDLE: return AISL_SUCCESS;
2019-04-16 14:26:37 +02:00
default:
rc = AISL_HTTP_INTERNAL_SERVER_ERROR;
}
2019-04-16 14:26:37 +02:00
aislx_quick_response((aisl_stream_t)evt->source, rc);
2019-04-16 14:26:37 +02:00
return AISL_SUCCESS;
}
static aisl_status_t
on_stream_close(aislx_feedback_t mod, aisl_evt_t const evt)
{
context_free((context_t)aisl_get_context((aisl_stream_t)evt->source));
2019-04-16 14:26:37 +02:00
return AISL_SUCCESS;
}
static aisl_status_t
aislx_feedback_on_event(aislx_feedback_t mod, aisl_evt_t const evt)
{
2019-04-16 14:26:37 +02:00
switch(evt->code)
{
case AISL_EVENT_STREAM_OPEN:
return on_stream_open(mod, (aisl_evt_stream_open_t)evt);
2019-04-16 14:26:37 +02:00
case AISL_EVENT_STREAM_HEADER:
return on_stream_header(mod, (aisl_evt_stream_header_t)evt);
2019-04-16 14:26:37 +02:00
case AISL_EVENT_STREAM_INPUT:
return on_stream_input(mod, (aisl_evt_stream_input_t)evt);
2019-04-16 14:26:37 +02:00
case AISL_EVENT_STREAM_REQUEST:
return on_stream_request(mod, evt);
2019-04-16 14:26:37 +02:00
case AISL_EVENT_STREAM_OUTPUT:
return on_stream_output(mod, evt);
2019-04-16 14:26:37 +02:00
case AISL_EVENT_STREAM_CLOSE:
return on_stream_close(mod, evt);
2019-04-16 14:26:37 +02:00
default:
return AISL_IDLE;
}
}
aisl_status_t
aislx_feedback_init(aislx_feedback_t mod, aislx_feedback_cfg_t cfg)
{
AISLX_MODULE_INIT(aislx_feedback, cfg->end_point);
2019-04-16 14:26:37 +02:00
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;
2019-04-16 14:26:37 +02:00
mod->name_email_length = strlen(cfg->name_email);
mod->name_msg_length = strlen(cfg->name_msg);
return AISL_SUCCESS;
}
void
aislx_feedback_release(aislx_feedback_t mod)
{
}