]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
plugins: push-notification: Reformat push-notification-driver-lua.c.
authorStephan Bosch <stephan.bosch@open-xchange.com>
Sun, 1 Dec 2019 10:23:55 +0000 (11:23 +0100)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Thu, 6 Feb 2020 12:18:41 +0000 (12:18 +0000)
src/plugins/push-notification/push-notification-driver-lua.c

index bf9bb94d02fc80955b9748449c6bfb3263125287..09eab7b4925ec0d3b133d27eaeac6ae636a53e6a 100644 (file)
@@ -58,19 +58,21 @@ struct dlua_push_notification_txn_context {
 };
 
 #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;
@@ -83,10 +85,11 @@ push_notification_driver_lua_init(struct push_notification_driver_config *config
 
        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;
@@ -94,7 +97,8 @@ push_notification_driver_lua_init(struct push_notification_driver_config *config
                }
 
                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;
@@ -105,19 +109,19 @@ push_notification_driver_lua_init(struct push_notification_driver_config *config
        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);
@@ -130,7 +134,8 @@ push_notification_driver_lua_init(struct push_notification_driver_config *config
 }
 
 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;
@@ -139,7 +144,7 @@ push_notification_driver_lua_init_events(struct push_notification_driver_txn *dt
        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);
@@ -151,13 +156,17 @@ push_notification_driver_lua_init_events(struct push_notification_driver_txn *dt
                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);
@@ -169,21 +178,23 @@ push_notification_driver_lua_init_events(struct push_notification_driver_txn *dt
        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;
@@ -195,9 +206,10 @@ static bool push_notification_driver_lua_begin_txn
                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);
@@ -210,9 +222,10 @@ static bool push_notification_driver_lua_begin_txn
        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;
@@ -221,14 +234,16 @@ static bool push_notification_driver_lua_begin_txn
        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');
@@ -240,7 +255,7 @@ static const char *push_notification_driver_lua_to_fn(const char *evname)
        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);
@@ -286,10 +301,11 @@ dlua_pushkeywords(struct dlua_script *script, const char *const *keywords,
 }
 
 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;
 
@@ -312,8 +328,9 @@ push_notification_lua_push_flagsclear(const struct push_notification_txn_event *
 }
 
 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;
@@ -330,8 +347,9 @@ push_notification_lua_push_flagsset(const struct push_notification_txn_event *ev
 }
 
 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;
 
@@ -343,8 +361,9 @@ push_notification_lua_push_mailboxrename(const struct push_notification_txn_even
        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");
@@ -361,8 +380,9 @@ push_notification_lua_push_message_ext(const struct push_notification_message_ex
 }
 
 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;
 
@@ -384,7 +404,8 @@ push_notification_lua_push_messageappend(const struct push_notification_txn_even
        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);
@@ -394,8 +415,9 @@ push_notification_lua_push_messageappend(const struct push_notification_txn_even
 }
 
 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;
 
@@ -417,7 +439,8 @@ push_notification_lua_push_messagenew(const struct push_notification_txn_event *
        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);
@@ -426,7 +449,7 @@ push_notification_lua_push_messagenew(const struct push_notification_txn_event *
        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,
@@ -455,16 +478,17 @@ static struct push_notification_event_to_lua {
 };
 
 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");
 
@@ -474,11 +498,12 @@ push_notification_driver_lua_pushevent(const struct push_notification_txn_event
 }
 
 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 =
@@ -488,22 +513,22 @@ push_notification_driver_lua_call(struct dlua_push_notification_context *ctx,
        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);
@@ -512,15 +537,15 @@ push_notification_driver_lua_call(struct dlua_push_notification_context *ctx,
                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);
@@ -533,8 +558,9 @@ push_notification_driver_lua_call(struct dlua_push_notification_context *ctx,
 }
 
 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;
@@ -549,8 +575,9 @@ push_notification_driver_lua_process_mbox(struct push_notification_driver_txn *d
 }
 
 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;
@@ -568,7 +595,7 @@ static void
 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;
@@ -594,9 +621,9 @@ push_notification_driver_lua_end_txn(struct push_notification_driver_txn *dtxn,
        }
 
        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);
 }
@@ -604,7 +631,7 @@ push_notification_driver_lua_end_txn(struct push_notification_driver_txn *dtxn,
 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);
@@ -626,8 +653,8 @@ struct push_notification_driver push_notification_driver_lua = {
                .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);