+const char **
+notmuch_config_get_search_exclude_tags (notmuch_config_t *config, size_t *length)
+{
+ return _config_get_list (config, "search", "exclude_tags",
+ &(config->search_exclude_tags),
+ &(config->search_exclude_tags_length), length);
+}
+
+void
+notmuch_config_set_search_exclude_tags (notmuch_config_t *config,
+ const char *list[],
+ size_t length)
+{
+ _config_set_list (config, "search", "exclude_tags", list, length,
+ &(config->search_exclude_tags));
+}
+
+
+/* Given a configuration item of the form <group>.<key> return the
+ * component group and key. If any error occurs, print a message on
+ * stderr and return 1. Otherwise, return 0.
+ *
+ * Note: This function modifies the original 'item' string.
+ */
+static int
+_item_split (char *item, char **group, char **key)
+{
+ char *period;
+
+ *group = item;
+
+ period = strchr (item, '.');
+ if (period == NULL || *(period+1) == '\0') {
+ fprintf (stderr,
+ "Invalid configuration name: %s\n"
+ "(Should be of the form <section>.<item>)\n", item);
+ return 1;
+ }
+
+ *period = '\0';
+ *key = period + 1;
+
+ return 0;
+}
+
+/* These are more properly called Xapian fields, but the user facing
+ docs call them prefixes, so make the error message match */
+static bool
+validate_field_name (const char *str)
+{
+ const char *key;
+
+ if (! g_utf8_validate (str, -1, NULL)) {
+ fprintf (stderr, "Invalid utf8: %s\n", str);
+ return false;
+ }
+
+ key = g_utf8_strrchr (str, -1, '.');
+ if (! key ) {
+ INTERNAL_ERROR ("Impossible code path on input: %s\n", str);
+ }
+
+ key++;
+
+ if (! *key) {
+ fprintf (stderr, "Empty prefix name: %s\n", str);
+ return false;
+ }
+
+ if (! unicode_word_utf8 (key)) {
+ fprintf (stderr, "Non-word character in prefix name: %s\n", key);
+ return false;
+ }
+
+ if (key[0] >= 'a' && key[0] <= 'z') {
+ fprintf (stderr, "Prefix names starting with lower case letters are reserved: %s\n", key);
+ return false;
+ }
+
+ return true;
+}
+
+#define BUILT_WITH_PREFIX "built_with."
+
+typedef struct config_key {
+ const char *name;
+ bool in_db;
+ bool prefix;
+ bool (*validate)(const char *);
+} config_key_info_t;
+
+static struct config_key
+config_key_table[] = {
+ {"index.decrypt", true, false, NULL},
+ {"index.header.", true, true, validate_field_name},
+ {"query.", true, true, NULL},
+};
+
+static config_key_info_t *
+_config_key_info (const char *item)
+{
+ for (size_t i = 0; i < ARRAY_SIZE (config_key_table); i++) {
+ if (config_key_table[i].prefix &&
+ strncmp (item, config_key_table[i].name,
+ strlen(config_key_table[i].name)) == 0)
+ return config_key_table+i;
+ if (strcmp (item, config_key_table[i].name) == 0)
+ return config_key_table+i;
+ }
+ return NULL;
+}
+
+static bool
+_stored_in_db (const char *item)
+{
+ config_key_info_t *info;
+ info = _config_key_info (item);
+
+ return (info && info->in_db);
+}
+
+static int
+_print_db_config(notmuch_config_t *config, const char *name)
+{
+ notmuch_database_t *notmuch;
+ char *val;
+
+ if (notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
+ return EXIT_FAILURE;
+
+ /* XXX Handle UUID mismatch? */
+
+ if (print_status_database ("notmuch config", notmuch,
+ notmuch_database_get_config (notmuch, name, &val)))
+ return EXIT_FAILURE;
+
+ puts (val);
+
+ return EXIT_SUCCESS;
+}
+
+static int
+notmuch_config_command_get (notmuch_config_t *config, char *item)
+{
+ if (strcmp(item, "database.path") == 0) {
+ printf ("%s\n", notmuch_config_get_database_path (config));
+ } else if (strcmp(item, "user.name") == 0) {
+ printf ("%s\n", notmuch_config_get_user_name (config));
+ } else if (strcmp(item, "user.primary_email") == 0) {
+ printf ("%s\n", notmuch_config_get_user_primary_email (config));
+ } else if (strcmp(item, "user.other_email") == 0) {
+ const char **other_email;
+ size_t i, length;
+
+ other_email = notmuch_config_get_user_other_email (config, &length);
+ for (i = 0; i < length; i++)
+ printf ("%s\n", other_email[i]);
+ } else if (strcmp(item, "new.tags") == 0) {
+ const char **tags;
+ size_t i, length;
+
+ tags = notmuch_config_get_new_tags (config, &length);
+ for (i = 0; i < length; i++)
+ printf ("%s\n", tags[i]);
+ } else if (STRNCMP_LITERAL (item, BUILT_WITH_PREFIX) == 0) {
+ printf ("%s\n",
+ notmuch_built_with (item + strlen (BUILT_WITH_PREFIX)) ? "true" : "false");
+ } else if (_stored_in_db (item)) {
+ return _print_db_config (config, item);
+ } else {
+ char **value;
+ size_t i, length;
+ char *group, *key;
+
+ if (_item_split (item, &group, &key))
+ return 1;
+
+ value = g_key_file_get_string_list (config->key_file,
+ group, key,
+ &length, NULL);
+ if (value == NULL) {
+ fprintf (stderr, "Unknown configuration item: %s.%s\n",
+ group, key);
+ return 1;