249 lines
5.3 KiB
C
249 lines
5.3 KiB
C
/******************************************************************************
|
|
*
|
|
* 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>
|
|
#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;
|
|
|
|
char * email;
|
|
char * msg;
|
|
};
|
|
|
|
typedef struct context * context_t;
|
|
|
|
|
|
static void
|
|
context_free(context_t ctx)
|
|
{
|
|
aislx_query_release(&ctx->qs);
|
|
|
|
if (ctx->email)
|
|
free(ctx->email);
|
|
|
|
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 )
|
|
{
|
|
context_t ctx = (context_t) p_ctx;
|
|
aislx_feedback_t mod = (aislx_feedback_t)((aislx_ctx_t)ctx)->mod;
|
|
|
|
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)
|
|
{
|
|
context_t ctx;
|
|
aisl_stream_t s = (aisl_stream_t) (evt->evt.source);
|
|
|
|
if (!(ctx = (context_t)aislx_ctx_new(AISLX_MODULE(mod))))
|
|
return AISL_MALLOC_ERROR;
|
|
|
|
ctx->mail.from = mod->mail_from;
|
|
ctx->mail.to = mod->mail_to;
|
|
ctx->mail.subject = mod->mail_subject;
|
|
|
|
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);
|
|
|
|
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)
|
|
{
|
|
context_t ctx = aisl_get_context((aisl_stream_t)evt->evt.source);
|
|
|
|
aislx_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)
|
|
{
|
|
aisl_status_t status;
|
|
context_t ctx = aisl_get_context((aisl_stream_t)evt->source);
|
|
aisl_stream_t s = (aisl_stream_t)evt->source;
|
|
|
|
/* verify input */
|
|
if (!ctx->email || !ctx->msg || aislx_validate_email(ctx->email) != 0)
|
|
aislx_quick_response(s, AISL_HTTP_BAD_REQUEST);
|
|
|
|
ctx->mail.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);
|
|
|
|
aisl_set_output_event(s, true); /**< enable output event */
|
|
|
|
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);
|
|
|
|
aisl_http_response_t rc = AISL_HTTP_OK;
|
|
|
|
switch (aislx_mail_get_status(&ctx->mail))
|
|
{
|
|
case AISL_SUCCESS: break;
|
|
case AISL_IDLE: return AISL_SUCCESS;
|
|
|
|
default:
|
|
rc = AISL_HTTP_INTERNAL_SERVER_ERROR;
|
|
}
|
|
|
|
aislx_quick_response((aisl_stream_t)evt->source, rc);
|
|
|
|
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));
|
|
return AISL_SUCCESS;
|
|
}
|
|
|
|
|
|
static aisl_status_t
|
|
aislx_feedback_on_event(aislx_feedback_t mod, aisl_evt_t const evt)
|
|
{
|
|
switch(evt->code)
|
|
{
|
|
case AISL_EVENT_STREAM_OPEN:
|
|
return on_stream_open(mod, (aisl_evt_stream_open_t)evt);
|
|
|
|
case AISL_EVENT_STREAM_HEADER:
|
|
return on_stream_header(mod, (aisl_evt_stream_header_t)evt);
|
|
|
|
case AISL_EVENT_STREAM_INPUT:
|
|
return on_stream_input(mod, (aisl_evt_stream_input_t)evt);
|
|
|
|
case AISL_EVENT_STREAM_REQUEST:
|
|
return on_stream_request(mod, evt);
|
|
|
|
case AISL_EVENT_STREAM_OUTPUT:
|
|
return on_stream_output(mod, evt);
|
|
|
|
case AISL_EVENT_STREAM_CLOSE:
|
|
return on_stream_close(mod, evt);
|
|
|
|
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);
|
|
|
|
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;
|
|
|
|
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)
|
|
{
|
|
|
|
}
|