236 lines
5.0 KiB
C
236 lines
5.0 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 <components/log.h>
|
|
#include <cStuff/string.h>
|
|
#include "feedback.h"
|
|
|
|
|
|
struct context
|
|
{
|
|
struct ax_context root;
|
|
struct ax_query qs;
|
|
struct ax_mail mail;
|
|
char *email;
|
|
char *msg;
|
|
};
|
|
|
|
typedef struct context * Context;
|
|
|
|
|
|
static void
|
|
context_free(Context ctx)
|
|
{
|
|
ax_query_release(&ctx->qs);
|
|
|
|
if (ctx->email)
|
|
free(ctx->email);
|
|
|
|
if (ctx->msg)
|
|
free(ctx->msg);
|
|
|
|
ax_context_free((AxContext)ctx);
|
|
}
|
|
|
|
|
|
static int
|
|
on_input_var( const char *key, uint32_t k_len,
|
|
const char *val, uint32_t v_len,
|
|
void *p_ctx )
|
|
{
|
|
Context ctx = (Context) p_ctx;
|
|
AxFeedback mod = (AxFeedback)((AxContext)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) == -1);
|
|
}
|
|
|
|
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) == -1);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
static AislStatus
|
|
on_stream_open(AxFeedback mod, const struct aisl_evt_open *evt)
|
|
{
|
|
Context ctx;
|
|
AislStream s = (AislStream) (evt->evt.source);
|
|
|
|
if (!(ctx = (Context)ax_context_new(AX_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 AislStatus
|
|
on_stream_header(AxFeedback mod, const struct aisl_evt_header *evt)
|
|
{
|
|
AislStream s = (AislStream)evt->evt.source;
|
|
Context ctx = aisl_get_context(s);
|
|
|
|
if (strcmp(evt->key, "content-length") == 0) {
|
|
size_t total = strtoll(evt->value, NULL, 10);
|
|
|
|
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 AislStatus
|
|
on_stream_input(AxFeedback mod, const struct aisl_evt_input *evt)
|
|
{
|
|
Context ctx = aisl_get_context((AislStream)evt->evt.source);
|
|
|
|
ax_query_feed(&ctx->qs, evt->data, evt->size);
|
|
|
|
return AISL_SUCCESS;
|
|
}
|
|
|
|
|
|
static AislStatus
|
|
on_stream_request(AxFeedback mod, const struct aisl_evt *evt)
|
|
{
|
|
Context ctx = aisl_get_context((AislStream)evt->source);
|
|
AislStream s = (AislStream)evt->source;
|
|
|
|
/* verify input */
|
|
if (!ctx->email || !ctx->msg || ax_validate_email(ctx->email) != 0) {
|
|
ax_quick_response(s, AISL_HTTP_BAD_REQUEST);
|
|
goto finally;
|
|
}
|
|
|
|
ctx->mail.reply_to = ctx->email;
|
|
ctx->mail.msg = ctx->msg;
|
|
uuid_generate(ctx->mail.msg_id);
|
|
|
|
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 */
|
|
|
|
finally:
|
|
return AISL_SUCCESS;
|
|
}
|
|
|
|
|
|
static AislStatus
|
|
on_stream_output(AxFeedback mod, const struct aisl_evt *evt)
|
|
{
|
|
Context ctx = aisl_get_context((AislStream)evt->source);
|
|
|
|
AislHttpResponse rc = AISL_HTTP_OK;
|
|
|
|
switch (ax_mail_get_status(&ctx->mail)) {
|
|
case AISL_SUCCESS: break;
|
|
case AISL_IDLE: return AISL_SUCCESS;
|
|
|
|
default:
|
|
rc = AISL_HTTP_INTERNAL_SERVER_ERROR;
|
|
}
|
|
|
|
ax_quick_response((AislStream)evt->source, rc);
|
|
|
|
return AISL_SUCCESS;
|
|
}
|
|
|
|
|
|
static AislStatus
|
|
on_stream_close(AxFeedback mod, const struct aisl_evt *evt)
|
|
{
|
|
context_free((Context)aisl_get_context((AislStream)evt->source));
|
|
return AISL_SUCCESS;
|
|
}
|
|
|
|
|
|
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, (const struct aisl_evt_open *)evt);
|
|
|
|
case AISL_EVENT_STREAM_HEADER:
|
|
return on_stream_header(mod, (const struct aisl_evt_header *)evt);
|
|
|
|
case AISL_EVENT_STREAM_INPUT:
|
|
return on_stream_input(mod, (const struct aisl_evt_input *)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;
|
|
}
|
|
}
|
|
|
|
|
|
AislStatus
|
|
ax_feedback_init(AxFeedback mod, const struct ax_feedback_cfg *cfg)
|
|
{
|
|
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;
|
|
memcpy(&mod->smtp, &cfg->smtp, sizeof (cfg->smtp));
|
|
|
|
mod->name_email_length = strlen(cfg->name_email);
|
|
mod->name_msg_length = strlen(cfg->name_msg);
|
|
|
|
return AISL_SUCCESS;
|
|
}
|
|
|
|
|
|
void
|
|
ax_feedback_release(AxFeedback mod)
|
|
{
|
|
|
|
}
|