};
#define DLUA_DEFAULT_EVENTS (\
- PUSH_NOTIFICATION_MESSAGE_HDR_FROM | PUSH_NOTIFICATION_MESSAGE_HDR_TO | \
- PUSH_NOTIFICATION_MESSAGE_HDR_SUBJECT | PUSH_NOTIFICATION_MESSAGE_HDR_DATE | \
- PUSH_NOTIFICATION_MESSAGE_BODY_SNIPPET | PUSH_NOTIFICATION_MESSAGE_FLAGS | \
- PUSH_NOTIFICATION_MESSAGE_KEYWORDS | PUSH_NOTIFICATION_MESSAGE_HDR_MESSAGE_ID)
+ PUSH_NOTIFICATION_MESSAGE_HDR_FROM | \
+ PUSH_NOTIFICATION_MESSAGE_HDR_TO | \
+ PUSH_NOTIFICATION_MESSAGE_HDR_SUBJECT | \
+ PUSH_NOTIFICATION_MESSAGE_HDR_DATE | \
+ PUSH_NOTIFICATION_MESSAGE_BODY_SNIPPET | \
+ PUSH_NOTIFICATION_MESSAGE_FLAGS | \
+ PUSH_NOTIFICATION_MESSAGE_KEYWORDS | \
+ PUSH_NOTIFICATION_MESSAGE_HDR_MESSAGE_ID)
static const char *push_notification_driver_lua_to_fn(const char *evname);
static int
-push_notification_driver_lua_init(struct push_notification_driver_config *config,
- struct mail_user *user,
- pool_t pool,
- void **context,
- const char **error_r)
+push_notification_driver_lua_init(
+ struct push_notification_driver_config *config, struct mail_user *user,
+ pool_t pool, void **context, const char **error_r)
{
struct dlua_push_notification_context *ctx;
const char *tmp, *file;
if (tmp == NULL) {
struct dlua_script *script;
- /* if there is a script loaded, use the same context */
+ /* If there is a script loaded, use the same context */
if (mail_lua_plugin_get_script(user, &script)) {
dlua_script_ref(script);
- ctx = p_new(pool, struct dlua_push_notification_context, 1);
+ ctx = p_new(
+ pool, struct dlua_push_notification_context, 1);
ctx->script = script;
ctx->event = event;
*context = ctx;
}
event_unref(&event);
- *error_r = "No file in config and no " DLUA_LOG_USERENV_KEY " set";
+ *error_r = "No file in config and no "
+ DLUA_LOG_USERENV_KEY " set";
return -1;
}
file = tmp;
e_debug(ctx->event, "Loading %s", file);
if (dlua_script_create_file(file, &ctx->script, event, error_r) < 0) {
- /* there is a T_POP after this, which will break errors */
+ /* There is a T_POP after this, which will break errors */
event_unref(&event);
*error_r = p_strdup(pool, *error_r);
return -1;
}
- /* register dovecot helpers */
+ /* Register dovecot helpers */
dlua_dovecot_register(ctx->script);
dlua_register_mail_storage(ctx->script);
e_debug(ctx->event, "Calling script_init");
- /* initialize script */
+ /* Initialize script */
if (dlua_script_init(ctx->script, error_r) < 0) {
*error_r = p_strdup(pool, *error_r);
event_unref(&event);
}
static bool
-push_notification_driver_lua_init_events(struct push_notification_driver_txn *dtxn)
+push_notification_driver_lua_init_events(
+ struct push_notification_driver_txn *dtxn)
{
struct dlua_push_notification_context *ctx = dtxn->duser->context;
const struct push_notification_event *const *event;
ctx->config_fc.store_old = TRUE;
bool found_one = FALSE;
- /* register *all* events that are present in Lua */
+ /* Register *all* events that are present in Lua */
array_foreach(push_notification_get_events(), event) {
const char *name = (*event)->name;
const char *fn = push_notification_driver_lua_to_fn(name);
e_debug(ctx->event, "Found %s, handling %s event", fn, name);
if (strcmp(name, "MessageNew") == 0) {
- push_notification_event_init(dtxn, name, &ctx->config_mn);
+ push_notification_event_init(dtxn, name,
+ &ctx->config_mn);
} else if (strcmp(name, "MessageAppend") == 0) {
- push_notification_event_init(dtxn, name, &ctx->config_ma);
+ push_notification_event_init(dtxn, name,
+ &ctx->config_ma);
} else if (strcmp(name, "FlagsSet") == 0) {
- push_notification_event_init(dtxn, name, &ctx->config_fs);
+ push_notification_event_init(dtxn, name,
+ &ctx->config_fs);
} else if (strcmp(name, "FlagsClear") == 0) {
- push_notification_event_init(dtxn, name, &ctx->config_fc);
+ push_notification_event_init(dtxn, name,
+ &ctx->config_fc);
} else if ((*event)->init.default_config != NULL) {
void *config = (*event)->init.default_config();
push_notification_event_init(dtxn, name, config);
return found_one;
}
-static bool push_notification_driver_lua_begin_txn
-(struct push_notification_driver_txn *dtxn)
+static bool
+push_notification_driver_lua_begin_txn(
+ struct push_notification_driver_txn *dtxn)
{
struct mail_user *user = dtxn->ptxn->muser;
struct dlua_push_notification_context *ctx = dtxn->duser->context;
- struct event *event = event_create(ctx->event);
- event_set_name(event, DLUA_CALL_FINISHED);
- event_add_str(event, "function_name", DLUA_FN_BEGIN_TXN);
+ struct event *event = event_create(ctx->event);
+ event_set_name(event, DLUA_CALL_FINISHED);
+ event_add_str(event, "function_name", DLUA_FN_BEGIN_TXN);
int luaerr;
- /* start txn and store whatever LUA gives us back, it's our txid */
+ /* Start txn and store whatever LUA gives us back, it's our txid */
lua_getglobal(ctx->script->L, DLUA_FN_BEGIN_TXN);
if (!lua_isfunction(ctx->script->L, -1)) {
- event_add_str(event, "error", "Missing function " DLUA_FN_BEGIN_TXN);
+ event_add_str(event, "error",
+ "Missing function " DLUA_FN_BEGIN_TXN);
e_error(event, "Missing function " DLUA_FN_BEGIN_TXN);
event_unref(&event);
return FALSE;
return FALSE;
}
- e_debug(ctx->event, "Calling " DLUA_FN_BEGIN_TXN "(%s)", user->username);
+ e_debug(ctx->event, "Calling " DLUA_FN_BEGIN_TXN "(%s)",
+ user->username);
- /* push mail user as argument */
+ /* Push mail user as argument */
dlua_push_mail_user(ctx->script, user);
if ((luaerr = lua_pcall(ctx->script->L, 1, 1, 0)) != 0) {
const char *error = lua_tostring(ctx->script->L, -1);
e_debug(event, "Called " DLUA_FN_BEGIN_TXN);
event_unref(&event);
- /* store the result */
+ /* Store the result */
struct dlua_push_notification_txn_context *tctx =
- p_new(dtxn->ptxn->pool, struct dlua_push_notification_txn_context, 1);
+ p_new(dtxn->ptxn->pool,
+ struct dlua_push_notification_txn_context, 1);
tctx->tx_ref = luaL_ref(ctx->script->L, LUA_REGISTRYINDEX);
dtxn->context = tctx;
return TRUE;
}
-/* this function only works here, it converts MessageType to event_message_type */
+/* This function only works here, it converts MessageType to event_message_type
+ */
static const char *push_notification_driver_lua_to_fn(const char *evname)
{
- /* camelcase to event_event_name (most events have two underscores) */
- string_t *fn = t_str_new(strlen(evname)+strlen(DLUA_FN_EVENT_PREFIX)+2);
+ /* Camelcase to event_event_name (most events have two underscores) */
+ string_t *fn = t_str_new(strlen(evname) +
+ strlen(DLUA_FN_EVENT_PREFIX) + 2);
str_append(fn, DLUA_FN_EVENT_PREFIX);
- for(;*evname != '\0'; evname++) {
+ for(; *evname != '\0'; evname++) {
if (*evname >= 'A' && *evname <= 'Z') {
str_append_c(fn, '_');
str_append_c(fn, (*evname) - 'A' + 'a');
return str_c(fn);
}
-/* pushes lua list of flags */
+/* Pushes lua list of flags */
static void dlua_pushflags(struct dlua_script *script, enum mail_flags flags)
{
lua_newtable(script->L);
}
static void
-push_notification_lua_push_flagsclear(const struct push_notification_txn_event *event,
- struct dlua_script *script)
+push_notification_lua_push_flagsclear(
+ const struct push_notification_txn_event *event,
+ struct dlua_script *script)
{
- /* push cleared flags */
+ /* Push cleared flags */
unsigned int size = 0;
struct push_notification_event_flagsclear_data *data = event->data;
}
static void
-push_notification_lua_push_flagsset(const struct push_notification_txn_event *event,
- struct dlua_script *script)
+push_notification_lua_push_flagsset(
+ const struct push_notification_txn_event *event,
+ struct dlua_script *script)
{
/* push set flags */
unsigned int size = 0;
}
static void
-push_notification_lua_push_mailboxrename(const struct push_notification_txn_event *event,
- struct dlua_script *script)
+push_notification_lua_push_mailboxrename(
+ const struct push_notification_txn_event *event,
+ struct dlua_script *script)
{
struct push_notification_event_mailboxrename_data *data = event->data;
lua_pushstring((L), (value) == NULL ? "" : (value))
static void
-push_notification_lua_push_message_ext(const struct push_notification_message_ext *ext,
- struct dlua_script *script)
+push_notification_lua_push_message_ext(
+ const struct push_notification_message_ext *ext,
+ struct dlua_script *script)
{
push_notification_lua_pushstring(script->L, ext->from_address);
lua_setfield(script->L, -2, "from_address");
}
static void
-push_notification_lua_push_messageappend(const struct push_notification_txn_event *event,
- struct dlua_script *script)
+push_notification_lua_push_messageappend(
+ const struct push_notification_txn_event *event,
+ struct dlua_script *script)
{
struct push_notification_event_messageappend_data *data = event->data;
dlua_pushflags(script, data->flags);
lua_setfield(script->L, -2, "flags");
- dlua_pushkeywords(script, data->keywords, str_array_length(data->keywords));
+ dlua_pushkeywords(script, data->keywords,
+ str_array_length(data->keywords));
lua_setfield(script->L, -2, "keywords");
lua_pushstring(script->L, data->message_id);
}
static void
-push_notification_lua_push_messagenew(const struct push_notification_txn_event *event,
- struct dlua_script *script)
+push_notification_lua_push_messagenew(
+ const struct push_notification_txn_event *event,
+ struct dlua_script *script)
{
struct push_notification_event_messagenew_data *data = event->data;
dlua_pushflags(script, data->flags);
lua_setfield(script->L, -2, "flags");
- dlua_pushkeywords(script, data->keywords, str_array_length(data->keywords));
+ dlua_pushkeywords(script, data->keywords,
+ str_array_length(data->keywords));
lua_setfield(script->L, -2, "keywords");
lua_pushstring(script->L, data->message_id);
push_notification_lua_push_message_ext(&data->ext, script);
}
-/* events that need special treatment */
+/* Events that need special treatment */
static struct push_notification_event_to_lua {
const char *event_name;
void (*push)(const struct push_notification_txn_event *event,
};
static void
-push_notification_driver_lua_pushevent(const struct push_notification_txn_event *event,
- struct dlua_push_notification_context *ctx)
+push_notification_driver_lua_pushevent(
+ const struct push_notification_txn_event *event,
+ struct dlua_push_notification_context *ctx)
{
struct dlua_script *script = ctx->script;
const char *name = event->event->event->name;
- /* create a table */
+ /* Create a table */
lua_newtable(script->L);
- /* event name */
+ /* Event name */
lua_pushstring(script->L, name);
lua_setfield(script->L, -2, "name");
}
static void
-push_notification_driver_lua_call(struct dlua_push_notification_context *ctx,
- struct dlua_push_notification_txn_context *tctx,
- const struct push_notification_txn_event *event,
- const struct push_notification_txn_mbox *mbox,
- struct push_notification_txn_msg *msg)
+push_notification_driver_lua_call(
+ struct dlua_push_notification_context *ctx,
+ struct dlua_push_notification_txn_context *tctx,
+ const struct push_notification_txn_event *event,
+ const struct push_notification_txn_mbox *mbox,
+ struct push_notification_txn_msg *msg)
{
int luaerr;
const char *fn =
event_add_str(e, "event_name", event->event->event->name);
event_add_str(e, "function_name", fn);
- /* this has been assured already in init */
+ /* This has been assured already in init */
lua_getglobal(ctx->script->L, fn);
i_assert(lua_isfunction(ctx->script->L, -1));
- /* push context */
+ /* Push context */
lua_rawgeti(ctx->script->L, LUA_REGISTRYINDEX, tctx->tx_ref);
- /* push event + common fields */
+ /* Push event + common fields */
push_notification_driver_lua_pushevent(event, ctx);
if (mbox != NULL) {
lua_pushstring(ctx->script->L, mbox->mailbox);
lua_setfield(ctx->script->L, -2, "mailbox");
- e_debug(ctx->event, "Calling %s(ctx, event[name=%s,mailbox=%s])",
- fn, event->event->event->name,
- mbox->mailbox);
+ e_debug(ctx->event,
+ "Calling %s(ctx, event[name=%s,mailbox=%s])",
+ fn, event->event->event->name, mbox->mailbox);
event_add_str(e, "mailbox", mbox->mailbox);
} else if (msg != NULL) {
lua_pushstring(ctx->script->L, msg->mailbox);
lua_setfield(ctx->script->L, -2, "uid");
lua_pushnumber(ctx->script->L, msg->uid_validity);
lua_setfield(ctx->script->L, -2, "uid_validity");
- e_debug(ctx->event, "Calling %s(ctx, event[name=%s,mailbox=%s,uid=%u])",
- fn, event->event->event->name,
- msg->mailbox, msg->uid);
+ e_debug(ctx->event,
+ "Calling %s(ctx, event[name=%s,mailbox=%s,uid=%u])",
+ fn, event->event->event->name, msg->mailbox, msg->uid);
event_add_str(e, "mailbox", msg->mailbox);
event_add_int(e, "uid", msg->uid);
} else
i_unreached();
- /* perform call */
+ /* Perform call */
if ((luaerr = lua_pcall(ctx->script->L, 2, 0, 0)) != 0) {
const char *error = lua_tostring(ctx->script->L, -1);
event_add_str(e, "error", error);
}
static void
-push_notification_driver_lua_process_mbox(struct push_notification_driver_txn *dtxn,
- struct push_notification_txn_mbox *mbox)
+push_notification_driver_lua_process_mbox(
+ struct push_notification_driver_txn *dtxn,
+ struct push_notification_txn_mbox *mbox)
{
struct push_notification_txn_event *const *event;
struct dlua_push_notification_context *ctx = dtxn->duser->context;
}
static void
-push_notification_driver_lua_process_msg(struct push_notification_driver_txn *dtxn,
- struct push_notification_txn_msg *msg)
+push_notification_driver_lua_process_msg(
+ struct push_notification_driver_txn *dtxn,
+ struct push_notification_txn_msg *msg)
{
struct push_notification_txn_event *const *event;
struct dlua_push_notification_context *ctx = dtxn->duser->context;
push_notification_driver_lua_end_txn(struct push_notification_driver_txn *dtxn,
bool success)
{
- /* call end txn */
+ /* Call end txn */
struct dlua_push_notification_context *ctx = dtxn->duser->context;
struct dlua_push_notification_txn_context *tctx = dtxn->context;
struct mail_user *user = dtxn->ptxn->muser;
}
event_unref(&event);
- /* release context */
+ /* Release context */
luaL_unref(ctx->script->L, LUA_REGISTRYINDEX, tctx->tx_ref);
- /* call gc here */
+ /* Call gc here */
(void)lua_gc(ctx->script->L, LUA_GCCOLLECT, 1);
mail_user_unref(&user);
}
static void
push_notification_driver_lua_deinit(struct push_notification_driver_user *duser)
{
- /* call lua deinit */
+ /* Call lua deinit */
struct dlua_push_notification_context *ctx = duser->context;
dlua_script_unref(&ctx->script);
event_unref(&ctx->event);
.process_msg = push_notification_driver_lua_process_msg,
.end_txn = push_notification_driver_lua_end_txn,
.deinit = push_notification_driver_lua_deinit,
- .cleanup = push_notification_driver_lua_cleanup
- }
+ .cleanup = push_notification_driver_lua_cleanup,
+ },
};
void push_notification_lua_plugin_init(struct module *module);