]> git.notmuchmail.org Git - notmuch/blob - lib/config.cc
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / lib / config.cc
1 /* config.cc - API for database metadata
2  *
3  * Copyright © 2016 David Bremner
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: David Bremner <david@tethera.net>
19  */
20
21 #include "notmuch.h"
22 #include "notmuch-private.h"
23 #include "database-private.h"
24
25 #include <pwd.h>
26 #include <netdb.h>
27
28 static const std::string CONFIG_PREFIX = "C";
29
30 struct _notmuch_config_list {
31     notmuch_database_t *notmuch;
32     Xapian::TermIterator iterator;
33     char *current_key;
34     char *current_val;
35 };
36
37 struct _notmuch_config_values {
38     const char *iterator;
39     size_t tok_len;
40     const char *string;
41     void *children; /* talloc_context */
42 };
43
44 struct _notmuch_config_pairs {
45     notmuch_string_map_iterator_t *iter;
46 };
47
48 static const char *_notmuch_config_key_to_string (notmuch_config_key_t key);
49 static char *_expand_path (void *ctx, const char *key, const char *val);
50
51 static int
52 _notmuch_config_list_destroy (notmuch_config_list_t *list)
53 {
54     /* invoke destructor w/o deallocating memory */
55     list->iterator.~TermIterator();
56     return 0;
57 }
58
59 notmuch_status_t
60 notmuch_database_set_config (notmuch_database_t *notmuch,
61                              const char *key,
62                              const char *value)
63 {
64     notmuch_status_t status;
65
66     status = _notmuch_database_ensure_writable (notmuch);
67     if (status)
68         return status;
69
70     if (! notmuch->config) {
71         if ((status = _notmuch_config_load_from_database (notmuch)))
72             return status;
73     }
74
75     try {
76         notmuch->writable_xapian_db->set_metadata (CONFIG_PREFIX + key, value);
77     } catch (const Xapian::Error &error) {
78         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
79         notmuch->exception_reported = true;
80         _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",
81                                error.get_msg ().c_str ());
82     }
83
84     if (status)
85         return status;
86
87     _notmuch_string_map_set (notmuch->config, key, value);
88
89     return NOTMUCH_STATUS_SUCCESS;
90 }
91
92 static notmuch_status_t
93 _metadata_value (notmuch_database_t *notmuch,
94                  const char *key,
95                  std::string &value)
96 {
97     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
98
99     try {
100         value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX + key);
101     } catch (const Xapian::Error &error) {
102         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
103         notmuch->exception_reported = true;
104         _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",
105                                error.get_msg ().c_str ());
106     }
107     return status;
108 }
109
110 notmuch_status_t
111 notmuch_database_get_config (notmuch_database_t *notmuch,
112                              const char *key,
113                              char **value)
114 {
115     const char *stored_val;
116     notmuch_status_t status;
117
118     if (! notmuch->config) {
119         if ((status = _notmuch_config_load_from_database (notmuch)))
120             return status;
121     }
122
123     if (! value)
124         return NOTMUCH_STATUS_NULL_POINTER;
125
126     stored_val = _notmuch_string_map_get (notmuch->config, key);
127     if (! stored_val) {
128         /* XXX in principle this API should be fixed so empty string
129          * is distinguished from not found */
130         *value = strdup ("");
131     } else {
132         *value = strdup (stored_val);
133     }
134
135     return NOTMUCH_STATUS_SUCCESS;
136 }
137
138 notmuch_status_t
139 notmuch_database_get_config_list (notmuch_database_t *notmuch,
140                                   const char *prefix,
141                                   notmuch_config_list_t **out)
142 {
143     notmuch_config_list_t *list = NULL;
144     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
145
146     list = talloc (notmuch, notmuch_config_list_t);
147     if (! list) {
148         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
149         goto DONE;
150     }
151
152     list->notmuch = notmuch;
153     list->current_key = NULL;
154     list->current_val = NULL;
155
156     try {
157
158         new(&(list->iterator)) Xapian::TermIterator (notmuch->xapian_db->metadata_keys_begin
159                                                          (CONFIG_PREFIX + (prefix ? prefix : "")));
160         talloc_set_destructor (list, _notmuch_config_list_destroy);
161
162     } catch (const Xapian::Error &error) {
163         _notmuch_database_log (notmuch,
164                                "A Xapian exception occurred getting metadata iterator: %s.\n",
165                                error.get_msg ().c_str ());
166         notmuch->exception_reported = true;
167         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
168     }
169
170     *out = list;
171
172   DONE:
173     if (status) {
174         if (list) {
175             talloc_free (list);
176             if (status != NOTMUCH_STATUS_XAPIAN_EXCEPTION)
177                 _notmuch_config_list_destroy (list);
178         }
179     } else {
180         talloc_set_destructor (list, _notmuch_config_list_destroy);
181     }
182
183     return status;
184 }
185
186 notmuch_bool_t
187 notmuch_config_list_valid (notmuch_config_list_t *metadata)
188 {
189     if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
190         return false;
191
192     return true;
193 }
194
195 static inline char *
196 _key_from_iterator (notmuch_config_list_t *list)
197 {
198     return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
199 }
200
201 const char *
202 notmuch_config_list_key (notmuch_config_list_t *list)
203 {
204     if (list->current_key)
205         talloc_free (list->current_key);
206
207     list->current_key = _key_from_iterator (list);
208
209     return list->current_key;
210 }
211
212 const char *
213 notmuch_config_list_value (notmuch_config_list_t *list)
214 {
215     std::string strval;
216     notmuch_status_t status;
217     char *key = _key_from_iterator (list);
218
219     /* TODO: better error reporting?? */
220     status = _metadata_value (list->notmuch, key, strval);
221     if (status)
222         return NULL;
223
224     if (list->current_val)
225         talloc_free (list->current_val);
226
227     list->current_val = talloc_strdup (list, strval.c_str ());
228     talloc_free (key);
229     return list->current_val;
230 }
231
232 void
233 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
234 {
235     list->iterator++;
236 }
237
238 void
239 notmuch_config_list_destroy (notmuch_config_list_t *list)
240 {
241     talloc_free (list);
242 }
243
244 notmuch_status_t
245 _notmuch_config_load_from_database (notmuch_database_t *notmuch)
246 {
247     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
248     notmuch_config_list_t *list;
249
250     if (notmuch->config == NULL)
251         notmuch->config = _notmuch_string_map_create (notmuch);
252
253     if (unlikely (notmuch->config == NULL))
254         return NOTMUCH_STATUS_OUT_OF_MEMORY;
255
256     status = notmuch_database_get_config_list (notmuch, "", &list);
257     if (status)
258         return status;
259
260     for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
261         const char *key = notmuch_config_list_key (list);
262         char *normalized_val = _expand_path (list, key, notmuch_config_list_value (list));
263         _notmuch_string_map_append (notmuch->config, key, normalized_val);
264         talloc_free (normalized_val);
265     }
266
267     return status;
268 }
269
270 notmuch_config_values_t *
271 notmuch_config_get_values (notmuch_database_t *notmuch, notmuch_config_key_t key)
272 {
273     const char *key_str = _notmuch_config_key_to_string (key);
274
275     if (! key_str)
276         return NULL;
277
278     return notmuch_config_get_values_string (notmuch, key_str);
279 }
280
281 notmuch_config_values_t *
282 notmuch_config_get_values_string (notmuch_database_t *notmuch, const char *key_str)
283 {
284     notmuch_config_values_t *values = NULL;
285     bool ok = false;
286
287     values = talloc (notmuch, notmuch_config_values_t);
288     if (unlikely (! values))
289         goto DONE;
290
291     values->children = talloc_new (values);
292
293     values->string = _notmuch_string_map_get (notmuch->config, key_str);
294     if (! values->string)
295         goto DONE;
296
297     values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
298     ok = true;
299
300   DONE:
301     if (! ok) {
302         if (values)
303             talloc_free (values);
304         return NULL;
305     }
306     return values;
307 }
308
309 notmuch_bool_t
310 notmuch_config_values_valid (notmuch_config_values_t *values)
311 {
312     if (! values)
313         return false;
314
315     return (values->iterator != NULL);
316 }
317
318 const char *
319 notmuch_config_values_get (notmuch_config_values_t *values)
320 {
321     return talloc_strndup (values->children, values->iterator, values->tok_len);
322 }
323
324 void
325 notmuch_config_values_start (notmuch_config_values_t *values)
326 {
327     if (values == NULL)
328         return;
329     if (values->children) {
330         talloc_free (values->children);
331     }
332
333     values->children = talloc_new (values);
334
335     values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
336 }
337
338 void
339 notmuch_config_values_move_to_next (notmuch_config_values_t *values)
340 {
341     values->iterator += values->tok_len;
342     values->iterator = strsplit_len (values->iterator, ';', &(values->tok_len));
343 }
344
345 void
346 notmuch_config_values_destroy (notmuch_config_values_t *values)
347 {
348     talloc_free (values);
349 }
350
351 notmuch_config_pairs_t *
352 notmuch_config_get_pairs (notmuch_database_t *notmuch,
353                           const char *prefix)
354 {
355     notmuch_config_pairs_t *pairs = talloc (notmuch, notmuch_config_pairs_t);
356
357     pairs->iter = _notmuch_string_map_iterator_create (notmuch->config, prefix, false);
358     return pairs;
359 }
360
361 notmuch_bool_t
362 notmuch_config_pairs_valid (notmuch_config_pairs_t *pairs)
363 {
364     return _notmuch_string_map_iterator_valid (pairs->iter);
365 }
366
367 void
368 notmuch_config_pairs_move_to_next (notmuch_config_pairs_t *pairs)
369 {
370     _notmuch_string_map_iterator_move_to_next (pairs->iter);
371 }
372
373 const char *
374 notmuch_config_pairs_key (notmuch_config_pairs_t *pairs)
375 {
376     return _notmuch_string_map_iterator_key (pairs->iter);
377 }
378
379 const char *
380 notmuch_config_pairs_value (notmuch_config_pairs_t *pairs)
381 {
382     return _notmuch_string_map_iterator_value (pairs->iter);
383 }
384
385 void
386 notmuch_config_pairs_destroy (notmuch_config_pairs_t *pairs)
387 {
388     _notmuch_string_map_iterator_destroy (pairs->iter);
389     talloc_free (pairs);
390 }
391
392 static char *
393 _expand_path (void *ctx, const char *key, const char *val)
394 {
395     char *expanded_val;
396
397     if ((strcmp (key, "database.path") == 0 ||
398          strcmp (key, "database.mail_root") == 0 ||
399          strcmp (key, "database.hook_dir") == 0 ||
400          strcmp (key, "database.backup_path") == 0 ) &&
401         val[0] != '/')
402         expanded_val = talloc_asprintf (ctx, "%s/%s", getenv ("HOME"), val);
403     else
404         expanded_val = talloc_strdup (ctx, val);
405
406     return expanded_val;
407 }
408
409 notmuch_status_t
410 _notmuch_config_load_from_file (notmuch_database_t *notmuch,
411                                 GKeyFile *file)
412 {
413     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
414     gchar **groups = NULL, **keys, *val;
415
416     if (notmuch->config == NULL)
417         notmuch->config = _notmuch_string_map_create (notmuch);
418
419     if (unlikely (notmuch->config == NULL)) {
420         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
421         goto DONE;
422     }
423
424     groups = g_key_file_get_groups (file, NULL);
425     for (gchar **grp = groups; *grp; grp++) {
426         keys = g_key_file_get_keys (file, *grp, NULL, NULL);
427         for (gchar **keys_p = keys; *keys_p; keys_p++) {
428             char *absolute_key = talloc_asprintf (notmuch, "%s.%s", *grp,  *keys_p);
429             char *normalized_val;
430             val = g_key_file_get_value (file, *grp, *keys_p, NULL);
431             if (! val) {
432                 status = NOTMUCH_STATUS_FILE_ERROR;
433                 goto DONE;
434             }
435             normalized_val = _expand_path (notmuch, absolute_key, val);
436             _notmuch_string_map_set (notmuch->config, absolute_key, normalized_val);
437             g_free (val);
438             talloc_free (absolute_key);
439             talloc_free (normalized_val);
440             if (status)
441                 goto DONE;
442         }
443         g_strfreev (keys);
444     }
445
446   DONE:
447     if (groups)
448         g_strfreev (groups);
449
450     return status;
451 }
452
453 notmuch_status_t
454 notmuch_config_get_bool (notmuch_database_t *notmuch, notmuch_config_key_t key, notmuch_bool_t *val)
455 {
456     const char *key_string, *val_string;
457
458     key_string = _notmuch_config_key_to_string (key);
459     if (! key_string) {
460         return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
461     }
462
463     val_string = _notmuch_string_map_get (notmuch->config, key_string);
464     if (! val_string) {
465         *val = FALSE;
466         return NOTMUCH_STATUS_SUCCESS;
467     }
468
469     if (strcase_equal (val_string, "false") || strcase_equal (val_string, "no"))
470         *val = FALSE;
471     else if (strcase_equal (val_string, "true") || strcase_equal (val_string, "yes"))
472         *val = TRUE;
473     else
474         return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
475
476     return NOTMUCH_STATUS_SUCCESS;
477 }
478
479 static const char *
480 _get_name_from_passwd_file (void *ctx)
481 {
482     long pw_buf_size;
483     char *pw_buf;
484     struct passwd passwd, *ignored;
485     const char *name;
486     int e;
487
488     pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
489     if (pw_buf_size == -1) pw_buf_size = 64;
490     pw_buf = (char *) talloc_size (ctx, pw_buf_size);
491
492     while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
493                             pw_buf_size, &ignored)) == ERANGE) {
494         pw_buf_size = pw_buf_size * 2;
495         pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
496     }
497
498     if (e == 0) {
499         char *comma = strchr (passwd.pw_gecos, ',');
500         if (comma)
501             name = talloc_strndup (ctx, passwd.pw_gecos,
502                                    comma - passwd.pw_gecos);
503         else
504             name = talloc_strdup (ctx, passwd.pw_gecos);
505     } else {
506         name = talloc_strdup (ctx, "");
507     }
508
509     talloc_free (pw_buf);
510
511     return name;
512 }
513
514 static char *
515 _get_username_from_passwd_file (void *ctx)
516 {
517     long pw_buf_size;
518     char *pw_buf;
519     struct passwd passwd, *ignored;
520     char *name;
521     int e;
522
523     pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
524     if (pw_buf_size == -1) pw_buf_size = 64;
525     pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
526
527     while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
528                             pw_buf_size, &ignored)) == ERANGE) {
529         pw_buf_size = pw_buf_size * 2;
530         pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
531     }
532
533     if (e == 0)
534         name = talloc_strdup (ctx, passwd.pw_name);
535     else
536         name = talloc_strdup (ctx, "");
537
538     talloc_free (pw_buf);
539
540     return name;
541 }
542
543 static const char *
544 _get_email_from_passwd_file (void *ctx)
545 {
546     char *email;
547
548     char *username = _get_username_from_passwd_file (ctx);
549
550     email = talloc_asprintf (ctx, "%s@localhost", username);
551
552     talloc_free (username);
553     return email;
554 }
555
556 static const char *
557 _notmuch_config_key_to_string (notmuch_config_key_t key)
558 {
559     switch (key) {
560     case NOTMUCH_CONFIG_DATABASE_PATH:
561         return "database.path";
562     case NOTMUCH_CONFIG_MAIL_ROOT:
563         return "database.mail_root";
564     case NOTMUCH_CONFIG_HOOK_DIR:
565         return "database.hook_dir";
566     case NOTMUCH_CONFIG_BACKUP_DIR:
567         return "database.backup_dir";
568     case NOTMUCH_CONFIG_EXCLUDE_TAGS:
569         return "search.exclude_tags";
570     case NOTMUCH_CONFIG_NEW_TAGS:
571         return "new.tags";
572     case NOTMUCH_CONFIG_NEW_IGNORE:
573         return "new.ignore";
574     case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
575         return "maildir.synchronize_flags";
576     case NOTMUCH_CONFIG_PRIMARY_EMAIL:
577         return "user.primary_email";
578     case NOTMUCH_CONFIG_OTHER_EMAIL:
579         return "user.other_email";
580     case NOTMUCH_CONFIG_USER_NAME:
581         return "user.name";
582     case NOTMUCH_CONFIG_AUTOCOMMIT:
583         return "database.autocommit";
584     default:
585         return NULL;
586     }
587 }
588
589 static const char *
590 _notmuch_config_default (notmuch_database_t *notmuch, notmuch_config_key_t key)
591 {
592     char *path;
593     const char *name, *email;
594
595     switch (key) {
596     case NOTMUCH_CONFIG_DATABASE_PATH:
597         path = getenv ("MAILDIR");
598         if (path)
599             path = talloc_strdup (notmuch, path);
600         else
601             path = talloc_asprintf (notmuch, "%s/mail",
602                                     getenv ("HOME"));
603         return path;
604     case NOTMUCH_CONFIG_MAIL_ROOT:
605         /* by default, mail root is the same as database path */
606         return notmuch_database_get_path (notmuch);
607     case NOTMUCH_CONFIG_EXCLUDE_TAGS:
608         return "";
609     case NOTMUCH_CONFIG_NEW_TAGS:
610         return "unread;inbox";
611     case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
612         return "true";
613     case NOTMUCH_CONFIG_USER_NAME:
614         name = getenv ("NAME");
615         if (name)
616             name = talloc_strdup (notmuch, name);
617         else
618             name = _get_name_from_passwd_file (notmuch);
619         return name;
620     case NOTMUCH_CONFIG_PRIMARY_EMAIL:
621         email = getenv ("EMAIL");
622         if (email)
623             email = talloc_strdup (notmuch, email);
624         else
625             email = _get_email_from_passwd_file (notmuch);
626         return email;
627     case NOTMUCH_CONFIG_NEW_IGNORE:
628         return "";
629     case NOTMUCH_CONFIG_AUTOCOMMIT:
630         return "8000";
631     case NOTMUCH_CONFIG_HOOK_DIR:
632     case NOTMUCH_CONFIG_BACKUP_DIR:
633     case NOTMUCH_CONFIG_OTHER_EMAIL:
634         return NULL;
635     default:
636     case NOTMUCH_CONFIG_LAST:
637         INTERNAL_ERROR ("illegal key enum %d", key);
638     }
639 }
640
641 notmuch_status_t
642 _notmuch_config_load_defaults (notmuch_database_t *notmuch)
643 {
644     notmuch_config_key_t key;
645
646     for (key = NOTMUCH_CONFIG_FIRST;
647          key < NOTMUCH_CONFIG_LAST;
648          key = notmuch_config_key_t (key + 1)) {
649         const char *val = notmuch_config_get (notmuch, key);
650         const char *key_string = _notmuch_config_key_to_string (key);
651
652         val = _notmuch_string_map_get (notmuch->config, key_string);
653         if (! val) {
654             _notmuch_string_map_set (notmuch->config, key_string, _notmuch_config_default (notmuch,
655                                                                                            key));
656         }
657     }
658     return NOTMUCH_STATUS_SUCCESS;
659 }
660
661 const char *
662 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key)
663 {
664
665     return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key));
666 }
667
668 const char *
669 notmuch_config_path (notmuch_database_t *notmuch)
670 {
671     return notmuch->config_path;
672 }
673
674 notmuch_status_t
675 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
676 {
677
678     return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val);
679 }
680
681 void
682 _notmuch_config_cache (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
683 {
684     if (notmuch->config == NULL)
685         notmuch->config = _notmuch_string_map_create (notmuch);
686
687     _notmuch_string_map_set (notmuch->config, _notmuch_config_key_to_string (key), val);
688 }