1 /* config.cc - API for database metadata
3 * Copyright © 2016 David Bremner
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see https://www.gnu.org/licenses/ .
18 * Author: David Bremner <david@tethera.net>
22 #include "notmuch-private.h"
23 #include "database-private.h"
28 static const std::string CONFIG_PREFIX = "C";
30 struct _notmuch_config_list {
31 notmuch_database_t *notmuch;
32 Xapian::TermIterator iterator;
37 struct _notmuch_config_values {
41 void *children; /* talloc_context */
44 struct _notmuch_config_pairs {
45 notmuch_string_map_iterator_t *iter;
48 static const char *_notmuch_config_key_to_string (notmuch_config_key_t key);
51 _notmuch_config_list_destroy (notmuch_config_list_t *list)
53 /* invoke destructor w/o deallocating memory */
54 list->iterator.~TermIterator();
59 notmuch_database_set_config (notmuch_database_t *notmuch,
63 notmuch_status_t status;
65 status = _notmuch_database_ensure_writable (notmuch);
69 if (! notmuch->config) {
70 if ((status = _notmuch_config_load_from_database (notmuch)))
75 notmuch->writable_xapian_db->set_metadata (CONFIG_PREFIX + key, value);
76 } catch (const Xapian::Error &error) {
77 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
78 notmuch->exception_reported = true;
79 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",
80 error.get_msg ().c_str ());
86 _notmuch_string_map_set (notmuch->config, key, value);
88 return NOTMUCH_STATUS_SUCCESS;
91 static notmuch_status_t
92 _metadata_value (notmuch_database_t *notmuch,
96 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
99 value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX + key);
100 } catch (const Xapian::Error &error) {
101 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
102 notmuch->exception_reported = true;
103 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",
104 error.get_msg ().c_str ());
110 notmuch_database_get_config (notmuch_database_t *notmuch,
114 const char *stored_val;
115 notmuch_status_t status;
117 if (! notmuch->config) {
118 if ((status = _notmuch_config_load_from_database (notmuch)))
123 return NOTMUCH_STATUS_NULL_POINTER;
125 stored_val = _notmuch_string_map_get (notmuch->config, key);
127 /* XXX in principle this API should be fixed so empty string
128 * is distinguished from not found */
129 *value = strdup ("");
131 *value = strdup (stored_val);
134 return NOTMUCH_STATUS_SUCCESS;
138 notmuch_database_get_config_list (notmuch_database_t *notmuch,
140 notmuch_config_list_t **out)
142 notmuch_config_list_t *list = NULL;
143 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
145 list = talloc (notmuch, notmuch_config_list_t);
147 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
151 list->notmuch = notmuch;
152 list->current_key = NULL;
153 list->current_val = NULL;
157 new(&(list->iterator)) Xapian::TermIterator (notmuch->xapian_db->metadata_keys_begin
158 (CONFIG_PREFIX + (prefix ? prefix : "")));
159 talloc_set_destructor (list, _notmuch_config_list_destroy);
161 } catch (const Xapian::Error &error) {
162 _notmuch_database_log (notmuch,
163 "A Xapian exception occurred getting metadata iterator: %s.\n",
164 error.get_msg ().c_str ());
165 notmuch->exception_reported = true;
166 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
175 if (status != NOTMUCH_STATUS_XAPIAN_EXCEPTION)
176 _notmuch_config_list_destroy (list);
179 talloc_set_destructor (list, _notmuch_config_list_destroy);
186 notmuch_config_list_valid (notmuch_config_list_t *metadata)
188 if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
195 _key_from_iterator (notmuch_config_list_t *list)
197 return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
201 notmuch_config_list_key (notmuch_config_list_t *list)
203 if (list->current_key)
204 talloc_free (list->current_key);
206 list->current_key = _key_from_iterator (list);
208 return list->current_key;
212 notmuch_config_list_value (notmuch_config_list_t *list)
215 notmuch_status_t status;
216 char *key = _key_from_iterator (list);
218 /* TODO: better error reporting?? */
219 status = _metadata_value (list->notmuch, key, strval);
223 if (list->current_val)
224 talloc_free (list->current_val);
226 list->current_val = talloc_strdup (list, strval.c_str ());
228 return list->current_val;
232 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
238 notmuch_config_list_destroy (notmuch_config_list_t *list)
244 _notmuch_config_load_from_database (notmuch_database_t *notmuch)
246 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
247 notmuch_config_list_t *list;
249 if (notmuch->config == NULL)
250 notmuch->config = _notmuch_string_map_create (notmuch);
252 if (unlikely (notmuch->config == NULL))
253 return NOTMUCH_STATUS_OUT_OF_MEMORY;
255 status = notmuch_database_get_config_list (notmuch, "", &list);
259 for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
260 _notmuch_string_map_append (notmuch->config,
261 notmuch_config_list_key (list),
262 notmuch_config_list_value (list));
268 notmuch_config_values_t *
269 notmuch_config_get_values (notmuch_database_t *notmuch, notmuch_config_key_t key)
271 const char *key_str = _notmuch_config_key_to_string (key);
276 return notmuch_config_get_values_string (notmuch, key_str);
279 notmuch_config_values_t *
280 notmuch_config_get_values_string (notmuch_database_t *notmuch, const char *key_str)
282 notmuch_config_values_t *values = NULL;
285 values = talloc (notmuch, notmuch_config_values_t);
286 if (unlikely (! values))
289 values->children = talloc_new (values);
291 values->string = _notmuch_string_map_get (notmuch->config, key_str);
292 if (! values->string)
295 values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
301 talloc_free (values);
308 notmuch_config_values_valid (notmuch_config_values_t *values)
313 return (values->iterator != NULL);
317 notmuch_config_values_get (notmuch_config_values_t *values)
319 return talloc_strndup (values, values->iterator, values->tok_len);
323 notmuch_config_values_start (notmuch_config_values_t *values)
327 if (values->children) {
328 talloc_free (values->children);
331 values->children = talloc_new (values);
333 values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
337 notmuch_config_values_move_to_next (notmuch_config_values_t *values)
339 values->iterator += values->tok_len;
340 values->iterator = strsplit_len (values->iterator, ';', &(values->tok_len));
344 notmuch_config_values_destroy (notmuch_config_values_t *values)
346 talloc_free (values);
349 notmuch_config_pairs_t *
350 notmuch_config_get_pairs (notmuch_database_t *notmuch,
353 notmuch_config_pairs_t *pairs = talloc (notmuch, notmuch_config_pairs_t);
355 pairs->iter = _notmuch_string_map_iterator_create (notmuch->config, prefix, false);
360 notmuch_config_pairs_valid (notmuch_config_pairs_t *pairs)
362 return _notmuch_string_map_iterator_valid (pairs->iter);
366 notmuch_config_pairs_move_to_next (notmuch_config_pairs_t *pairs)
368 _notmuch_string_map_iterator_move_to_next (pairs->iter);
372 notmuch_config_pairs_key (notmuch_config_pairs_t *pairs)
374 return _notmuch_string_map_iterator_key (pairs->iter);
378 notmuch_config_pairs_value (notmuch_config_pairs_t *pairs)
380 return _notmuch_string_map_iterator_value (pairs->iter);
384 notmuch_config_pairs_destroy (notmuch_config_pairs_t *pairs)
386 _notmuch_string_map_iterator_destroy (pairs->iter);
391 _notmuch_config_load_from_file (notmuch_database_t *notmuch,
394 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
395 gchar **groups = NULL, **keys, *val;
397 if (notmuch->config == NULL)
398 notmuch->config = _notmuch_string_map_create (notmuch);
400 if (unlikely (notmuch->config == NULL)) {
401 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
405 groups = g_key_file_get_groups (file, NULL);
406 for (gchar **grp = groups; *grp; grp++) {
407 keys = g_key_file_get_keys (file, *grp, NULL, NULL);
408 for (gchar **keys_p = keys; *keys_p; keys_p++) {
409 char *absolute_key = talloc_asprintf (notmuch, "%s.%s", *grp, *keys_p);
410 val = g_key_file_get_value (file, *grp, *keys_p, NULL);
412 status = NOTMUCH_STATUS_FILE_ERROR;
415 _notmuch_string_map_set (notmuch->config, absolute_key, val);
417 talloc_free (absolute_key);
432 notmuch_config_get_bool (notmuch_database_t *notmuch, notmuch_config_key_t key, notmuch_bool_t *val)
434 const char *key_string, *val_string;
436 key_string = _notmuch_config_key_to_string (key);
438 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
441 val_string = _notmuch_string_map_get (notmuch->config, key_string);
444 return NOTMUCH_STATUS_SUCCESS;
447 if (strcase_equal (val_string, "false") || strcase_equal (val_string, "no"))
449 else if (strcase_equal (val_string, "true") || strcase_equal (val_string, "yes"))
452 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
454 return NOTMUCH_STATUS_SUCCESS;
458 _get_name_from_passwd_file (void *ctx)
462 struct passwd passwd, *ignored;
466 pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
467 if (pw_buf_size == -1) pw_buf_size = 64;
468 pw_buf = (char *) talloc_size (ctx, pw_buf_size);
470 while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
471 pw_buf_size, &ignored)) == ERANGE) {
472 pw_buf_size = pw_buf_size * 2;
473 pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
477 char *comma = strchr (passwd.pw_gecos, ',');
479 name = talloc_strndup (ctx, passwd.pw_gecos,
480 comma - passwd.pw_gecos);
482 name = talloc_strdup (ctx, passwd.pw_gecos);
484 name = talloc_strdup (ctx, "");
487 talloc_free (pw_buf);
493 _get_username_from_passwd_file (void *ctx)
497 struct passwd passwd, *ignored;
501 pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
502 if (pw_buf_size == -1) pw_buf_size = 64;
503 pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
505 while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
506 pw_buf_size, &ignored)) == ERANGE) {
507 pw_buf_size = pw_buf_size * 2;
508 pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
512 name = talloc_strdup (ctx, passwd.pw_name);
514 name = talloc_strdup (ctx, "");
516 talloc_free (pw_buf);
522 _get_email_from_passwd_file (void *ctx)
526 struct hostent *hostent;
527 const char *domainname;
530 char *username = _get_username_from_passwd_file (ctx);
532 gethostname (hostname, 256);
533 hostname[255] = '\0';
535 hostent = gethostbyname (hostname);
536 if (hostent && (domainname = strchr (hostent->h_name, '.')))
539 domainname = "(none)";
541 email = talloc_asprintf (ctx, "%s@%s.%s",
542 username, hostname, domainname);
544 talloc_free (username);
550 _notmuch_config_key_to_string (notmuch_config_key_t key)
553 case NOTMUCH_CONFIG_DATABASE_PATH:
554 return "database.path";
555 case NOTMUCH_CONFIG_MAIL_ROOT:
556 return "database.mail_root";
557 case NOTMUCH_CONFIG_HOOK_DIR:
558 return "database.hook_dir";
559 case NOTMUCH_CONFIG_BACKUP_DIR:
560 return "database.backup_dir";
561 case NOTMUCH_CONFIG_EXCLUDE_TAGS:
562 return "search.exclude_tags";
563 case NOTMUCH_CONFIG_NEW_TAGS:
565 case NOTMUCH_CONFIG_NEW_IGNORE:
567 case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
568 return "maildir.synchronize_flags";
569 case NOTMUCH_CONFIG_PRIMARY_EMAIL:
570 return "user.primary_email";
571 case NOTMUCH_CONFIG_OTHER_EMAIL:
572 return "user.other_email";
573 case NOTMUCH_CONFIG_USER_NAME:
581 _notmuch_config_default (notmuch_database_t *notmuch, notmuch_config_key_t key)
584 const char *name, *email;
587 case NOTMUCH_CONFIG_DATABASE_PATH:
588 path = getenv ("MAILDIR");
590 path = talloc_strdup (notmuch, path);
592 path = talloc_asprintf (notmuch, "%s/mail",
595 case NOTMUCH_CONFIG_MAIL_ROOT:
596 /* by default, mail root is the same as database path */
597 return notmuch_database_get_path (notmuch);
598 case NOTMUCH_CONFIG_EXCLUDE_TAGS:
600 case NOTMUCH_CONFIG_NEW_TAGS:
601 return "unread;inbox";
602 case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
604 case NOTMUCH_CONFIG_USER_NAME:
605 name = getenv ("NAME");
607 name = talloc_strdup (notmuch, name);
609 name = _get_name_from_passwd_file (notmuch);
611 case NOTMUCH_CONFIG_PRIMARY_EMAIL:
612 email = getenv ("EMAIL");
614 email = talloc_strdup (notmuch, email);
616 email = _get_email_from_passwd_file (notmuch);
618 case NOTMUCH_CONFIG_NEW_IGNORE:
620 case NOTMUCH_CONFIG_HOOK_DIR:
621 case NOTMUCH_CONFIG_BACKUP_DIR:
622 case NOTMUCH_CONFIG_OTHER_EMAIL:
625 case NOTMUCH_CONFIG_LAST:
626 INTERNAL_ERROR ("illegal key enum %d", key);
631 _notmuch_config_load_defaults (notmuch_database_t *notmuch)
633 notmuch_config_key_t key;
635 for (key = NOTMUCH_CONFIG_FIRST;
636 key < NOTMUCH_CONFIG_LAST;
637 key = notmuch_config_key_t (key + 1)) {
638 const char *val = notmuch_config_get (notmuch, key);
639 const char *key_string = _notmuch_config_key_to_string (key);
641 val = _notmuch_string_map_get (notmuch->config, key_string);
643 _notmuch_string_map_set (notmuch->config, key_string, _notmuch_config_default (notmuch,
647 return NOTMUCH_STATUS_SUCCESS;
651 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key)
654 return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key));
658 notmuch_config_path (notmuch_database_t *notmuch)
660 return notmuch->config_path;
664 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
667 return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val);
671 _notmuch_config_cache (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
673 if (notmuch->config == NULL)
674 notmuch->config = _notmuch_string_map_create (notmuch);
676 _notmuch_string_map_set (notmuch->config, _notmuch_config_key_to_string (key), val);