bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2014-2018 Dovecot authors, see the included COPYING file */
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomicmd_dict_init_full(struct doveadm_cmd_context *cctx,
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi doveadm_command_ver2_t *cmd ATTR_UNUSED, enum dict_iterate_flags *iter_flags,
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi const char *dict_uri, *error, *key, *username = "";
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi if (doveadm_cmd_param_bool(cctx, "exact", &set) && set)
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi if (doveadm_cmd_param_bool(cctx, "recurse", &set) && set)
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi if (doveadm_cmd_param_bool(cctx, "no-value", &set) && set)
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi (void)doveadm_cmd_param_str(cctx, "user", &username);
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi if (!doveadm_cmd_param_str(cctx, "dict-uri", &dict_uri)) {
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi if (!doveadm_cmd_param_str(cctx, "prefix", &key) &&
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi if (strncmp(key, DICT_PATH_PRIVATE, strlen(DICT_PATH_PRIVATE)) != 0 &&
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi strncmp(key, DICT_PATH_SHARED, strlen(DICT_PATH_SHARED)) != 0) {
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi strncmp(key, DICT_PATH_PRIVATE, strlen(DICT_PATH_PRIVATE)) == 0) {
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi i_error("-u must be specified for "DICT_PATH_PRIVATE" keys");
20e04227229970d148801c507946666e2a9bd838Timo Sirainen dict_set.base_dir = doveadm_settings->base_dir;
20e04227229970d148801c507946666e2a9bd838Timo Sirainen if (dict_init(dict_uri, &dict_set, &dict, &error) < 0) {
aea3116a9a5d4ba53ddcff460ba213d26058af81Aki Tuomi i_error("dict_init(%s) failed: %s", dict_uri, error);
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi doveadm_command_ver2_t *cmd, struct dict **dict_r)
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi return cmd_dict_init_full(cctx, cmd, NULL, dict_r);
92b98c7ec5a2295ae96819e9b7b4e2be716a1fb2Timo Sirainenstatic void dict_lookup_callback(const struct dict_lookup_result *result,
92b98c7ec5a2295ae96819e9b7b4e2be716a1fb2Timo Sirainen ctx->error = p_strdup(ctx->pool, result->error);
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomistatic void cmd_dict_get(struct doveadm_cmd_context *cctx)
26f14cde12faa09df2de399e2384fd3d955d8bb4Timo Sirainen doveadm_print_header("value", "", DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
92b98c7ec5a2295ae96819e9b7b4e2be716a1fb2Timo Sirainen ctx.pool = pool_alloconly_create("doveadm dict lookup", 512);
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi dict_lookup_async(dict, key, dict_lookup_callback, &ctx);
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi i_error("dict_lookup(%s) failed: %s", key, ctx.error);
92b98c7ec5a2295ae96819e9b7b4e2be716a1fb2Timo Sirainen unsigned int i, values_count = str_array_length(ctx.values);
92b98c7ec5a2295ae96819e9b7b4e2be716a1fb2Timo Sirainen doveadm_print_header("value", "", DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
92b98c7ec5a2295ae96819e9b7b4e2be716a1fb2Timo Sirainen for (i = 0; i < values_count; i++)
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomistatic void cmd_dict_set(struct doveadm_cmd_context *cctx)
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi if (!doveadm_cmd_param_str(cctx, "value", &value)) {
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen if (dict_transaction_commit(&trans, &error) <= 0) {
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen i_error("dict_transaction_commit() failed: %s", error);
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomistatic void cmd_dict_unset(struct doveadm_cmd_context *cctx)
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi if (cmd_dict_init(cctx, cmd_dict_unset, &dict) < 0)
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen if (dict_transaction_commit(&trans, &error) <= 0) {
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen i_error("dict_transaction_commit() failed: %s", error);
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomistatic void cmd_dict_inc(struct doveadm_cmd_context *cctx)
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi if (!doveadm_cmd_param_int64(cctx, "difference", &diff)) {
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen ret = dict_transaction_commit(&trans, &error);
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen i_error("dict_transaction_commit() failed: %s", error);
26f14cde12faa09df2de399e2384fd3d955d8bb4Timo Sirainen } else if (ret == 0) {
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomistatic void cmd_dict_iter(struct doveadm_cmd_context *cctx)
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi if (cmd_dict_init_full(cctx, cmd_dict_iter, &iter_flags, &dict) < 0)
efa1a91c5eb82e49edb00b4a194f6fc11de5641cTimo Sirainen if ((iter_flags & DICT_ITERATE_FLAG_NO_VALUE) == 0)
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi (void)doveadm_cmd_param_str(cctx, "prefix", &prefix);
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi iter = dict_iterate_init(dict, prefix, iter_flags);
efa1a91c5eb82e49edb00b4a194f6fc11de5641cTimo Sirainen if ((iter_flags & DICT_ITERATE_FLAG_NO_VALUE) == 0)
42db3821524726962aa35fdae82db5644a52bcd9Aki Tuomi i_error("dict_iterate_deinit(%s) failed: %s", prefix, error);
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki Tuomistatic struct doveadm_cmd_ver2 doveadm_cmd_dict[] = {
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('\0', "dict-uri", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('\0', "key", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('\0', "dict-uri", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('\0', "key", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('\0', "value", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('\0', "dict-uri", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('\0', "key", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('\0', "dict-uri", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('\0', "key", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('\0', "difference", CMD_PARAM_INT64, CMD_PARAM_FLAG_POSITIONAL)
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki Tuomi .usage = "[-u <user>] [-1RV] <dict uri> <prefix>",
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('R', "recurse", CMD_PARAM_BOOL, 0)
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('V', "no-value", CMD_PARAM_BOOL, 0)
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('\0', "dict-uri", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
19f03098ca92dbfbfd936059a94f37e8d077ee66Aki TuomiDOVEADM_CMD_PARAM('\0', "prefix", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
26f14cde12faa09df2de399e2384fd3d955d8bb4Timo Sirainen unsigned int i;
26f14cde12faa09df2de399e2384fd3d955d8bb4Timo Sirainen for (i = 0; i < N_ELEMENTS(doveadm_cmd_dict); i++)