]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-storage: Split off the struct mail lua code
authorJosef 'Jeff' Sipek <jeff.sipek@open-xchange.com>
Tue, 1 Dec 2020 16:47:26 +0000 (11:47 -0500)
committerJosef 'Jeff' Sipek <jeff.sipek@open-xchange.com>
Tue, 1 Dec 2020 16:47:26 +0000 (11:47 -0500)
Move the code into a separate file to mirror the native C code layout.

src/lib-storage/Makefile.am
src/lib-storage/mail-lua.c [new file with mode: 0644]
src/lib-storage/mail-storage-lua-private.h [new file with mode: 0644]
src/lib-storage/mail-storage-lua.c

index a04b3cfc4b1b27184cd1baee2d7c6adb601d3ff6..1fcc3a3257a44ccb8585399974321b38466399c3 100644 (file)
@@ -138,6 +138,7 @@ libdovecot_storage_la_LDFLAGS = -export-dynamic
 if HAVE_LUA
 pkglib_LTLIBRARIES += libdovecot-storage-lua.la
 libdovecot_storage_lua_la_SOURCES = \
+       mail-lua.c \
        mail-storage-lua.c
 libdovecot_storage_lua_la_CPPFLAGS = \
        $(AM_CPPFLAGS) \
@@ -152,7 +153,8 @@ libdovecot_storage_lua_la_DEPENDENCIES = \
 libdovecot_storage_lua_la_LDFLAGS = -export-dynamic
 
 headers += \
-       mail-storage-lua.h
+       mail-storage-lua.h \
+       mail-storage-lua-private.h
 endif
 
 test_programs = \
diff --git a/src/lib-storage/mail-lua.c b/src/lib-storage/mail-lua.c
new file mode 100644 (file)
index 0000000..c0ae136
--- /dev/null
@@ -0,0 +1,148 @@
+/* Copyright (c) 2018 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "str.h"
+#include "istream.h"
+#include "array.h"
+#include "var-expand.h"
+#include "dlua-script.h"
+#include "dlua-script-private.h"
+#include "mail-storage.h"
+#include "mailbox-attribute.h"
+#include "mail-storage-lua.h"
+#include "mail-storage-lua-private.h"
+#include "mail-user.h"
+
+#define LUA_STORAGE_MAIL "struct mail"
+
+void dlua_push_mail(struct dlua_script *script, struct mail *mail)
+{
+       luaL_checkstack(script->L, 20, "out of memory");
+       /* create a table for holding few things */
+       lua_createtable(script->L, 0, 20);
+       luaL_setmetatable(script->L, LUA_STORAGE_MAIL);
+
+       lua_pushlightuserdata(script->L, mail);
+       lua_setfield(script->L, -2, "item");
+
+#undef LUA_TABLE_SETNUMBER
+#define LUA_TABLE_SETNUMBER(field) \
+       lua_pushnumber(script->L, mail->field); \
+       lua_setfield(script->L, -2, #field);
+#undef LUA_TABLE_SETBOOL
+#define LUA_TABLE_SETBOOL(field) \
+       lua_pushboolean(script->L, mail->field); \
+       lua_setfield(script->L, -2, #field);
+
+       LUA_TABLE_SETNUMBER(seq);
+       LUA_TABLE_SETNUMBER(uid);
+       LUA_TABLE_SETBOOL(expunged);
+
+       dlua_push_mailbox(script, mail->box);
+       lua_setfield(script->L, -2, "mailbox");
+
+}
+
+static struct mail *
+lua_check_storage_mail(struct dlua_script *script, int arg)
+{
+       if (!lua_istable(script->L, arg)) {
+               (void)luaL_error(script->L, "Bad argument #%d, expected %s got %s",
+                                arg, LUA_STORAGE_MAIL,
+                                lua_typename(script->L, lua_type(script->L, arg)));
+       }
+       lua_pushliteral(script->L, "item");
+       lua_rawget(script->L, arg);
+       void *bp = (void*)lua_touserdata(script->L, -1);
+       lua_pop(script->L, 1);
+       return (struct mail*)bp;
+}
+
+static int lua_storage_mail_tostring(lua_State *L)
+{
+       struct dlua_script *script = dlua_script_from_state(L);
+       DLUA_REQUIRE_ARGS(script, 1);
+       struct mail *mail = lua_check_storage_mail(script, 1);
+
+       const char *str =
+               t_strdup_printf("<%s:UID %u>", mailbox_get_vname(mail->box),
+                               mail->uid);
+       lua_pushstring(script->L, str);
+       return 1;
+}
+
+static int lua_storage_mail_eq(lua_State *L)
+{
+       struct dlua_script *script = dlua_script_from_state(L);
+       DLUA_REQUIRE_ARGS(script, 2);
+       struct mail *mail = lua_check_storage_mail(script, 1);
+       struct mail *mail2 = lua_check_storage_mail(script, 2);
+
+       if (!DLUA_MAILBOX_EQUALS(mail->box, mail2->box))
+               lua_pushboolean(script->L, FALSE);
+       else
+               lua_pushboolean(script->L, mail->uid != mail2->uid);
+       return 1;
+}
+
+static int lua_storage_mail_lt(lua_State *L)
+{
+       struct dlua_script *script = dlua_script_from_state(L);
+       DLUA_REQUIRE_ARGS(script, 2);
+       struct mail *mail = lua_check_storage_mail(script, 1);
+       struct mail *mail2 = lua_check_storage_mail(script, 2);
+
+       if (!DLUA_MAILBOX_EQUALS(mail->box, mail2->box))
+               return luaL_error(script->L,
+                                 "For lt, Mail can only be compared within same mailbox");
+       else
+               lua_pushboolean(script->L, mail->uid < mail2->uid);
+       return 1;
+}
+
+static int lua_storage_mail_le(lua_State *L)
+{
+       struct dlua_script *script = dlua_script_from_state(L);
+       DLUA_REQUIRE_ARGS(script, 2);
+       struct mail *mail = lua_check_storage_mail(script, 1);
+       struct mail *mail2 = lua_check_storage_mail(script, 2);
+
+       if (!DLUA_MAILBOX_EQUALS(mail->box, mail2->box))
+               return luaL_error(script->L,
+                                "For le, mails can only be within same mailbox");
+       else
+               lua_pushboolean(script->L, mail->uid <= mail2->uid);
+
+       return 1;
+}
+
+static int lua_storage_mail_gc(lua_State *L)
+{
+       struct dlua_script *script = dlua_script_from_state(L);
+       (void)lua_check_storage_mail(script, 1);
+
+       /* reset value to NULL */
+       lua_pushliteral(script->L, "item");
+       lua_pushnil(script->L);
+       lua_rawset(script->L, 1);
+
+       return 0;
+}
+
+static luaL_Reg lua_storage_mail_methods[] = {
+       { "__tostring", lua_storage_mail_tostring },
+       { "__eq", lua_storage_mail_eq },
+       { "__lt", lua_storage_mail_lt },
+       { "__le", lua_storage_mail_le },
+       { "__gc", lua_storage_mail_gc },
+       { NULL, NULL }
+};
+
+void lua_storage_mail_register(struct dlua_script *script)
+{
+       luaL_newmetatable(script->L, LUA_STORAGE_MAIL);
+       lua_pushvalue(script->L, -1);
+       lua_setfield(script->L, -2, "__index");
+       luaL_setfuncs(script->L, lua_storage_mail_methods, 0);
+       lua_pop(script->L, 1);
+}
diff --git a/src/lib-storage/mail-storage-lua-private.h b/src/lib-storage/mail-storage-lua-private.h
new file mode 100644 (file)
index 0000000..bcc2813
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef MAIL_STORAGE_LUA_PRIVATE_H
+#define MAIL_STORAGE_LUA_PRIVATE_H 1
+
+#define DLUA_MAILBOX_EQUALS(a, b) \
+       mailbox_equals((a), mailbox_get_namespace(b), mailbox_get_vname(b))
+
+void lua_storage_mail_register(struct dlua_script *script);
+
+#endif
index 2adda0a962047b0555d0f7e2f897cde104ce679a..ac5a535029330272d1f2cba1ffeb0d9a936ec77c 100644 (file)
 #include "mail-storage.h"
 #include "mailbox-attribute.h"
 #include "mail-storage-lua.h"
+#include "mail-storage-lua-private.h"
 #include "mail-user.h"
 
 #define LUA_SCRIPT_STORAGE "storage"
 #define LUA_STORAGE_MAIL_USER "struct mail_user"
 #define LUA_STORAGE_MAILBOX "struct mailbox"
-#define LUA_STORAGE_MAIL "struct mail"
 
 /** shared functions
  */
@@ -594,9 +594,6 @@ static void lua_storage_mail_user_register(struct dlua_script *script)
 /** MAILBOX
  */
 
-#define DLUA_MAILBOX_EQUALS(a, b) mailbox_equals((a), mailbox_get_namespace(b), \
-                                                mailbox_get_vname(b))
-
 static int lua_storage_mailbox_gc(lua_State *L);
 
 void dlua_push_mailbox(struct dlua_script *script, struct mailbox *box)
@@ -965,142 +962,6 @@ static void lua_storage_mailbox_register(struct dlua_script *script)
        lua_pop(script->L, 1);
 }
 
-/* MAIL */
-
-void dlua_push_mail(struct dlua_script *script, struct mail *mail)
-{
-       luaL_checkstack(script->L, 20, "out of memory");
-       /* create a table for holding few things */
-       lua_createtable(script->L, 0, 20);
-       luaL_setmetatable(script->L, LUA_STORAGE_MAIL);
-
-       lua_pushlightuserdata(script->L, mail);
-       lua_setfield(script->L, -2, "item");
-
-#undef LUA_TABLE_SETNUMBER
-#define LUA_TABLE_SETNUMBER(field) \
-       lua_pushnumber(script->L, mail->field); \
-       lua_setfield(script->L, -2, #field);
-#undef LUA_TABLE_SETBOOL
-#define LUA_TABLE_SETBOOL(field) \
-       lua_pushboolean(script->L, mail->field); \
-       lua_setfield(script->L, -2, #field);
-
-       LUA_TABLE_SETNUMBER(seq);
-       LUA_TABLE_SETNUMBER(uid);
-       LUA_TABLE_SETBOOL(expunged);
-
-       dlua_push_mailbox(script, mail->box);
-       lua_setfield(script->L, -2, "mailbox");
-
-}
-
-static struct mail *
-lua_check_storage_mail(struct dlua_script *script, int arg)
-{
-       if (!lua_istable(script->L, arg)) {
-               (void)luaL_error(script->L, "Bad argument #%d, expected %s got %s",
-                                arg, LUA_STORAGE_MAIL,
-                                lua_typename(script->L, lua_type(script->L, arg)));
-       }
-       lua_pushliteral(script->L, "item");
-       lua_rawget(script->L, arg);
-       void *bp = (void*)lua_touserdata(script->L, -1);
-       lua_pop(script->L, 1);
-       return (struct mail*)bp;
-}
-
-static int lua_storage_mail_tostring(lua_State *L)
-{
-       struct dlua_script *script = dlua_script_from_state(L);
-       DLUA_REQUIRE_ARGS(script, 1);
-       struct mail *mail = lua_check_storage_mail(script, 1);
-
-       const char *str =
-               t_strdup_printf("<%s:UID %u>", mailbox_get_vname(mail->box),
-                               mail->uid);
-       lua_pushstring(script->L, str);
-       return 1;
-}
-
-static int lua_storage_mail_eq(lua_State *L)
-{
-       struct dlua_script *script = dlua_script_from_state(L);
-       DLUA_REQUIRE_ARGS(script, 2);
-       struct mail *mail = lua_check_storage_mail(script, 1);
-       struct mail *mail2 = lua_check_storage_mail(script, 2);
-
-       if (!DLUA_MAILBOX_EQUALS(mail->box, mail2->box))
-               lua_pushboolean(script->L, FALSE);
-       else
-               lua_pushboolean(script->L, mail->uid != mail2->uid);
-       return 1;
-}
-
-static int lua_storage_mail_lt(lua_State *L)
-{
-       struct dlua_script *script = dlua_script_from_state(L);
-       DLUA_REQUIRE_ARGS(script, 2);
-       struct mail *mail = lua_check_storage_mail(script, 1);
-       struct mail *mail2 = lua_check_storage_mail(script, 2);
-
-       if (!DLUA_MAILBOX_EQUALS(mail->box, mail2->box))
-               return luaL_error(script->L,
-                                 "For lt, Mail can only be compared within same mailbox");
-       else
-               lua_pushboolean(script->L, mail->uid < mail2->uid);
-       return 1;
-}
-
-static int lua_storage_mail_le(lua_State *L)
-{
-       struct dlua_script *script = dlua_script_from_state(L);
-       DLUA_REQUIRE_ARGS(script, 2);
-       struct mail *mail = lua_check_storage_mail(script, 1);
-       struct mail *mail2 = lua_check_storage_mail(script, 2);
-
-       if (!DLUA_MAILBOX_EQUALS(mail->box, mail2->box))
-               return luaL_error(script->L,
-                                "For le, mails can only be within same mailbox");
-       else
-               lua_pushboolean(script->L, mail->uid <= mail2->uid);
-
-       return 1;
-}
-
-static int lua_storage_mail_gc(lua_State *L)
-{
-       struct dlua_script *script = dlua_script_from_state(L);
-       (void)lua_check_storage_mail(script, 1);
-
-       /* reset value to NULL */
-       lua_pushliteral(script->L, "item");
-       lua_pushnil(script->L);
-       lua_rawset(script->L, 1);
-
-       return 0;
-}
-
-static luaL_Reg lua_storage_mail_methods[] = {
-       { "__tostring", lua_storage_mail_tostring },
-       { "__eq", lua_storage_mail_eq },
-       { "__lt", lua_storage_mail_lt },
-       { "__le", lua_storage_mail_le },
-       { "__gc", lua_storage_mail_gc },
-       { NULL, NULL }
-};
-
-static void lua_storage_mail_register(struct dlua_script *script)
-{
-       luaL_newmetatable(script->L, LUA_STORAGE_MAIL);
-       lua_pushvalue(script->L, -1);
-       lua_setfield(script->L, -2, "__index");
-       luaL_setfuncs(script->L, lua_storage_mail_methods, 0);
-       lua_pop(script->L, 1);
-}
-
-/* end of MAIL */
-
 static struct dlua_table_values lua_storage_mail_storage_flags[] = {
        DLUA_TABLE_ENUM(STATUS_MESSAGES),
        DLUA_TABLE_ENUM(STATUS_RECENT),