bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainenstatic struct dict *dict_driver_lookup(const char *name)
701eb90460d6c57845dc4e0bf595a5d0b90b01c1Timo Sirainenvoid dict_transaction_commit_async_noop_callback(
701eb90460d6c57845dc4e0bf595a5d0b90b01c1Timo Sirainen const struct dict_commit_result *result ATTR_UNUSED,
75a7ba70c7b377eff0f7124b8943cf2976ac2533Aki Tuomi /* do nothing */
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen if (dict_driver_lookup(driver->name) != NULL) {
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen i_fatal("dict_driver_register(%s): Already registered",
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainenvoid dict_driver_unregister(struct dict *driver)
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen idx = array_foreach_idx(&dict_drivers, dicts);
20e04227229970d148801c507946666e2a9bd838Timo Sirainenint dict_init(const char *uri, const struct dict_settings *set,
eca38954bcf972618f6b85932a3690acbd2b673aTimo Sirainen *error_r = t_strdup_printf("Dictionary URI is missing ':': %s",
eca38954bcf972618f6b85932a3690acbd2b673aTimo Sirainen *error_r = t_strdup_printf("Unknown dict module: %s", name);
39ea5717264668e2c7f9f7986eb821d21785f47fTimo Sirainen if (dict->v.init(dict, p+1, set, dict_r, &error) < 0) {
eca38954bcf972618f6b85932a3690acbd2b673aTimo Sirainen *error_r = t_strdup_printf("dict %s: %s", name, error);
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainenstatic bool dict_key_prefix_is_valid(const char *key)
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen return strncmp(key, DICT_PATH_SHARED, strlen(DICT_PATH_SHARED)) == 0 ||
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen strncmp(key, DICT_PATH_PRIVATE, strlen(DICT_PATH_PRIVATE)) == 0;
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainenint dict_lookup(struct dict *dict, pool_t pool, const char *key,
b5052fbfdbc2678cc8f12899afe55c998f43b740Timo Sirainen return dict->v.lookup(dict, pool, key, value_r, error_r);
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainenvoid dict_lookup_async(struct dict *dict, const char *key,
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainen dict_lookup_callback_t *callback, void *context)
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainen result.ret = dict_lookup(dict, pool_datastack_create(),
85b234661baa110e046d3d9ad22f59e69fa75c69Timo Sirainen const char *const values[] = { result.value, NULL };
f990dde096949bd2b76aab28936211689bd6cadcTimo Sirainen dict->v.lookup_async(dict, key, callback, context);
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainendict_iterate_init(struct dict *dict, const char *path,
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen return dict_iterate_init_multiple(dict, paths, flags);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainendict_iterate_init_multiple(struct dict *dict, const char *const *paths,
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen unsigned int i;
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen /* not supported by backend */
b198cd6da331eb55d300b0e83f59695c58d5885cTimo Sirainen ctx = dict->v.iterate_init(dict, paths, flags);
10f126b558e39b0f69fe2baecc9e74d2bfad8c7dAki Tuomi /* the dict in context can differ from the dict
10f126b558e39b0f69fe2baecc9e74d2bfad8c7dAki Tuomi passed as parameter, e.g. it can be dict-fail when
10f126b558e39b0f69fe2baecc9e74d2bfad8c7dAki Tuomi iteration is not supported. */
8d25b6ad05b99e75613cb045a121efd51e6afbb6Timo Sirainenbool dict_iterate(struct dict_iterate_context *ctx,
6acd1a48e1377e74d3008288e5e95e006f41265cTimo Sirainen if (ctx->max_rows > 0 && ctx->row_count >= ctx->max_rows) {
6acd1a48e1377e74d3008288e5e95e006f41265cTimo Sirainen /* row count was limited */
6acd1a48e1377e74d3008288e5e95e006f41265cTimo Sirainen if (!ctx->dict->v.iterate(ctx, key_r, value_r))
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainenvoid dict_iterate_set_async_callback(struct dict_iterate_context *ctx,
6acd1a48e1377e74d3008288e5e95e006f41265cTimo Sirainenvoid dict_iterate_set_limit(struct dict_iterate_context *ctx,
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainenbool dict_iterate_has_more(struct dict_iterate_context *ctx)
055389c58fa3915e12fb4e72ec86782ce77c5c72Timo Sirainenint dict_iterate_deinit(struct dict_iterate_context **_ctx,
055389c58fa3915e12fb4e72ec86782ce77c5c72Timo Sirainen const char **error_r)
055389c58fa3915e12fb4e72ec86782ce77c5c72Timo Sirainen return ctx->dict->v.iterate_deinit(ctx, error_r);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenstruct dict_transaction_context *dict_transaction_begin(struct dict *dict)
f32da6b73c1edb6963eae0d4a5c1f995ad23151aAki Tuomi /* the dict in context can differ from the dict
f32da6b73c1edb6963eae0d4a5c1f995ad23151aAki Tuomi passed as parameter, e.g. it can be dict-fail when
f32da6b73c1edb6963eae0d4a5c1f995ad23151aAki Tuomi transactions are not supported. */
5012586ed3a75857ced48302bf0b8a8dc049796aTimo Sirainen DLLIST_PREPEND(&ctx->dict->transactions, ctx);
aef407f147034a569591c0f59593342a8c7b39eaTimo Sirainenvoid dict_transaction_no_slowness_warning(struct dict_transaction_context *ctx)
e28b4fc2b62be020156a857485b61842b3b5d791Timo Sirainenvoid dict_transaction_set_timestamp(struct dict_transaction_context *ctx,
e28b4fc2b62be020156a857485b61842b3b5d791Timo Sirainen /* These asserts are mainly here to guarantee a possibility in future
e28b4fc2b62be020156a857485b61842b3b5d791Timo Sirainen to change the API to support multiple timestamps within the same
e28b4fc2b62be020156a857485b61842b3b5d791Timo Sirainen transaction, so this call would apply only to the following
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainendict_transaction_commit_sync_callback(const struct dict_commit_result *result,
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen struct dict_commit_sync_result *sync_result = context;
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainenint dict_transaction_commit(struct dict_transaction_context **_ctx,
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen const char **error_r)
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen dict_transaction_commit_sync_callback, &result);
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainenvoid dict_transaction_commit_async(struct dict_transaction_context **_ctx,
701eb90460d6c57845dc4e0bf595a5d0b90b01c1Timo Sirainen callback = dict_transaction_commit_async_noop_callback;
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen ctx->dict->v.transaction_commit(ctx, TRUE, callback, context);
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainenvoid dict_transaction_rollback(struct dict_transaction_context **_ctx)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid dict_set(struct dict_transaction_context *ctx,
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainenvoid dict_unset(struct dict_transaction_context *ctx,
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen const char *key)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid dict_atomic_inc(struct dict_transaction_context *ctx,
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainenconst char *dict_escape_string(const char *str)
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen const char *p;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen /* see if we need to escape it */
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*p == '\0')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen for (; *p != '\0'; p++) {
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen switch (*p) {
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainenconst char *dict_unescape_string(const char *str)
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen const char *p;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen /* see if we need to unescape it */
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*p == '\\')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*p == '\0')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen /* unescape */
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen ret = t_str_new((size_t) (p - str) + strlen(p) + 1);
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen for (; *p != '\0'; p++) {
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*p != '\\')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*++p == '|')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen else if (*p == '\0')