]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-dict: lua: Initialize dict_op_settings for dict operations
authorSiavash Tavakoli <siavash.tavakoli@open-xchange.com>
Thu, 3 Jun 2021 17:41:14 +0000 (18:41 +0100)
committerSiavash Tavakoli <siavash.tavakoli@open-xchange.com>
Thu, 1 Jul 2021 19:40:48 +0000 (20:40 +0100)
- Add new mandatory argument for lookup, set, and transaction_begin.
- Use the username to initialize dict_op_settings passed to lib-dict API

src/lib-dict/dict-iter-lua.c
src/lib-dict/dict-lua.c
src/lib-dict/dict-txn-lua.c

index 71d00c12ad9f70d7e2f046dc595d3e4f93564f1b..1af3009be04ed7d44a4024ac4b00fc8e9def85c8 100644 (file)
@@ -141,35 +141,43 @@ static void lua_dict_iterate_callback(struct lua_dict_iter *iter)
 }
 
 /*
- * Iterate a dict at key [-3,+2,e]
+ * Iterate a dict at key [-(3|4),+2,e]
  *
  * Args:
  *   1) userdata: sturct dict *dict
  *   2) string: key
  *   3) integer: flags
+ *   4*) string: username
  *
  * Returns:
  *   Returns a iteration step function and dict iter userdata.
+ *   Username will be NULL if not provided in args.
  */
 int lua_dict_iterate(lua_State *L)
 {
        enum dict_iterate_flags flags;
        struct lua_dict_iter *iter;
        struct dict *dict;
-       const char *path;
+       const char *path, *username = NULL;
        pool_t pool;
 
-       DLUA_REQUIRE_ARGS(L, 3);
+       DLUA_REQUIRE_ARGS_IN(L, 3, 4);
 
        dict = dlua_check_dict(L, 1);
        path = luaL_checkstring(L, 2);
        flags = luaL_checkinteger(L, 3);
+       if (lua_gettop(L) >= 4)
+               username = luaL_checkstring(L, 4);
+
+       struct dict_op_settings set = {
+               .username = username,
+       };
 
        /* set up iteration */
        pool = pool_alloconly_create("lua dict iter", 128);
        iter = p_new(pool, struct lua_dict_iter, 1);
        iter->pool = pool;
-       iter->iter = dict_iterate_init(dict, NULL, path, flags |
+       iter->iter = dict_iterate_init(dict, &set, path, flags |
                                      DICT_ITERATE_FLAG_ASYNC);
        p_array_init(&iter->refs, iter->pool, 32);
        iter->L = L;
index bfd2b5bbbe9c15ee69c92867e7484e5b8e5ed0da..1be6b61fa4bdda4ddcb5a6117c273f55a95963b2 100644 (file)
@@ -57,27 +57,34 @@ static void lua_dict_lookup_callback(const struct dict_lookup_result *result,
 }
 
 /*
- * Lookup a key in dict [-2,+1,e]
+ * Lookup a key in dict [-(2|3),+1,e]
  *
  * Args:
  *   1) userdata: struct dict *dict
  *   2) string: key
+ *   3*) string: username
  *
  * Returns:
  *   If key is found, returns a table with values.  If key is not found,
  *   returns nil.
+ *   Username will be NULL if not provided in args.
  */
 static int lua_dict_lookup(lua_State *L)
 {
        struct dict *dict;
-       const char *key;
+       const char *key, *username = NULL;
 
-       DLUA_REQUIRE_ARGS(L, 2);
+       DLUA_REQUIRE_ARGS_IN(L, 2, 3);
 
        dict = xlua_dict_getptr(L, 1, NULL);
        key = luaL_checkstring(L, 2);
+       if (lua_gettop(L) >= 3)
+               username = luaL_checkstring(L, 3);
 
-       dict_lookup_async(dict, NULL, key, lua_dict_lookup_callback, L);
+       struct dict_op_settings set = {
+               .username = username,
+       };
+       dict_lookup_async(dict, &set, key, lua_dict_lookup_callback, L);
 
        return lua_dict_async_continue(L,
                lua_yieldk(L, 0, 0, lua_dict_async_continue), 0);
index 8e13e9d22714e3542dbb73ea5360dc09d1221f93..61e9e48e800199edc34908385850de29bb152b19 100644 (file)
@@ -164,28 +164,37 @@ static int lua_dict_set(lua_State *L)
 }
 
 /*
- * Start a dict transaction [-1,+1,e]
+ * Start a dict transaction [-(1|2),+1,e]
  *
  * Args:
  *   1) userdata: struct dict *
+ *   2*) string: username
  *
  * Returns:
  *   Returns a new transaction object.
+ *   Username will be NULL if not provided in args.
  */
 int lua_dict_transaction_begin(lua_State *L)
 {
        struct lua_dict_txn *txn;
        struct dict *dict;
+       const char *username = NULL;
        pool_t pool;
 
-       DLUA_REQUIRE_ARGS(L, 1);
+       DLUA_REQUIRE_ARGS_IN(L, 1, 2);
 
        dict = dlua_check_dict(L, 1);
+       if (lua_gettop(L) >= 2)
+               username = luaL_checkstring(L, 2);
 
        pool = pool_alloconly_create("lua dict txn", 128);
        txn = p_new(pool, struct lua_dict_txn, 1);
        txn->pool = pool;
-       txn->txn = dict_transaction_begin(dict, NULL);
+
+       struct dict_op_settings set = {
+               .username = username,
+       };
+       txn->txn = dict_transaction_begin(dict, &set);
        txn->state = STATE_OPEN;
        txn->L = L;