]> 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
50 static int
51 _notmuch_config_list_destroy (notmuch_config_list_t *list)
52 {
53     /* invoke destructor w/o deallocating memory */
54     list->iterator.~TermIterator();
55     return 0;
56 }
57
58 notmuch_status_t
59 notmuch_database_set_config (notmuch_database_t *notmuch,
60                              const char *key,
61                              const char *value)
62 {
63     notmuch_status_t status;
64
65     status = _notmuch_database_ensure_writable (notmuch);
66     if (status)
67         return status;
68
69     if (! notmuch->config) {
70         if ((status = _notmuch_config_load_from_database (notmuch)))
71             return status;
72     }
73
74     try {
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 ());
81     }
82
83     if (status)
84         return status;
85
86     _notmuch_string_map_set (notmuch->config, key, value);
87
88     return NOTMUCH_STATUS_SUCCESS;
89 }
90
91 static notmuch_status_t
92 _metadata_value (notmuch_database_t *notmuch,
93                  const char *key,
94                  std::string &value)
95 {
96     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
97
98     try {
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 ());
105     }
106     return status;
107 }
108
109 notmuch_status_t
110 notmuch_database_get_config (notmuch_database_t *notmuch,
111                              const char *key,
112                              char **value)
113 {
114     const char *stored_val;
115     notmuch_status_t status;
116
117     if (! notmuch->config) {
118         if ((status = _notmuch_config_load_from_database (notmuch)))
119             return status;
120     }
121
122     if (! value)
123         return NOTMUCH_STATUS_NULL_POINTER;
124
125     stored_val = _notmuch_string_map_get (notmuch->config, key);
126     if (! stored_val) {
127         /* XXX in principle this API should be fixed so empty string
128          * is distinguished from not found */
129         *value = strdup ("");
130     } else {
131         *value = strdup (stored_val);
132     }
133
134     return NOTMUCH_STATUS_SUCCESS;
135 }
136
137 notmuch_status_t
138 notmuch_database_get_config_list (notmuch_database_t *notmuch,
139                                   const char *prefix,
140                                   notmuch_config_list_t **out)
141 {
142     notmuch_config_list_t *list = NULL;
143     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
144
145     list = talloc (notmuch, notmuch_config_list_t);
146     if (! list) {
147         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
148         goto DONE;
149     }
150
151     list->notmuch = notmuch;
152     list->current_key = NULL;
153     list->current_val = NULL;
154
155     try {
156
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);
160
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;
167     }
168
169     *out = list;
170
171   DONE:
172     if (status) {
173         if (list) {
174             talloc_free (list);
175             if (status != NOTMUCH_STATUS_XAPIAN_EXCEPTION)
176                 _notmuch_config_list_destroy (list);
177         }
178     } else {
179         talloc_set_destructor (list, _notmuch_config_list_destroy);
180     }
181
182     return status;
183 }
184
185 notmuch_bool_t
186 notmuch_config_list_valid (notmuch_config_list_t *metadata)
187 {
188     if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
189         return false;
190
191     return true;
192 }
193
194 static inline char *
195 _key_from_iterator (notmuch_config_list_t *list)
196 {
197     return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
198 }
199
200 const char *
201 notmuch_config_list_key (notmuch_config_list_t *list)
202 {
203     if (list->current_key)
204         talloc_free (list->current_key);
205
206     list->current_key = _key_from_iterator (list);
207
208     return list->current_key;
209 }
210
211 const char *
212 notmuch_config_list_value (notmuch_config_list_t *list)
213 {
214     std::string strval;
215     notmuch_status_t status;
216     char *key = _key_from_iterator (list);
217
218     /* TODO: better error reporting?? */
219     status = _metadata_value (list->notmuch, key, strval);
220     if (status)
221         return NULL;
222
223     if (list->current_val)
224         talloc_free (list->current_val);
225
226     list->current_val = talloc_strdup (list, strval.c_str ());
227     talloc_free (key);
228     return list->current_val;
229 }
230
231 void
232 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
233 {
234     list->iterator++;
235 }
236
237 void
238 notmuch_config_list_destroy (notmuch_config_list_t *list)
239 {
240     talloc_free (list);
241 }
242
243 notmuch_status_t
244 _notmuch_config_load_from_database (notmuch_database_t *notmuch)
245 {
246     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
247     notmuch_config_list_t *list;
248
249     if (notmuch->config == NULL)
250         notmuch->config = _notmuch_string_map_create (notmuch);
251
252     if (unlikely (notmuch->config == NULL))
253         return NOTMUCH_STATUS_OUT_OF_MEMORY;
254
255     status = notmuch_database_get_config_list (notmuch, "", &list);
256     if (status)
257         return status;
258
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));
263     }
264
265     return status;
266 }
267
268 notmuch_config_values_t *
269 notmuch_config_get_values (notmuch_database_t *notmuch, notmuch_config_key_t key)
270 {
271     const char *key_str = _notmuch_config_key_to_string (key);
272
273     if (! key_str)
274         return NULL;
275
276     return notmuch_config_get_values_string (notmuch, key_str);
277 }
278
279 notmuch_config_values_t *
280 notmuch_config_get_values_string (notmuch_database_t *notmuch, const char *key_str)
281 {
282     notmuch_config_values_t *values = NULL;
283     bool ok = false;
284
285     values = talloc (notmuch, notmuch_config_values_t);
286     if (unlikely (! values))
287         goto DONE;
288
289     values->children = talloc_new (values);
290
291     values->string = _notmuch_string_map_get (notmuch->config, key_str);
292     if (! values->string)
293         goto DONE;
294
295     values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
296     ok = true;
297
298   DONE:
299     if (! ok) {
300         if (values)
301             talloc_free (values);
302         return NULL;
303     }
304     return values;
305 }
306
307 notmuch_bool_t
308 notmuch_config_values_valid (notmuch_config_values_t *values)
309 {
310     if (! values)
311         return false;
312
313     return (values->iterator != NULL);
314 }
315
316 const char *
317 notmuch_config_values_get (notmuch_config_values_t *values)
318 {
319     return talloc_strndup (values, values->iterator, values->tok_len);
320 }
321
322 void
323 notmuch_config_values_start (notmuch_config_values_t *values)
324 {
325     if (values == NULL)
326         return;
327     if (values->children) {
328         talloc_free (values->children);
329     }
330
331     values->children = talloc_new (values);
332
333     values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
334 }
335
336 void
337 notmuch_config_values_move_to_next (notmuch_config_values_t *values)
338 {
339     values->iterator += values->tok_len;
340     values->iterator = strsplit_len (values->iterator, ';', &(values->tok_len));
341 }
342
343 void
344 notmuch_config_values_destroy (notmuch_config_values_t *values)
345 {
346     talloc_free (values);
347 }
348
349 notmuch_config_pairs_t *
350 notmuch_config_get_pairs (notmuch_database_t *notmuch,
351                           const char *prefix)
352 {
353     notmuch_config_pairs_t *pairs = talloc (notmuch, notmuch_config_pairs_t);
354
355     pairs->iter = _notmuch_string_map_iterator_create (notmuch->config, prefix, false);
356     return pairs;
357 }
358
359 notmuch_bool_t
360 notmuch_config_pairs_valid (notmuch_config_pairs_t *pairs)
361 {
362     return _notmuch_string_map_iterator_valid (pairs->iter);
363 }
364
365 void
366 notmuch_config_pairs_move_to_next (notmuch_config_pairs_t *pairs)
367 {
368     _notmuch_string_map_iterator_move_to_next (pairs->iter);
369 }
370
371 const char *
372 notmuch_config_pairs_key (notmuch_config_pairs_t *pairs)
373 {
374     return _notmuch_string_map_iterator_key (pairs->iter);
375 }
376
377 const char *
378 notmuch_config_pairs_value (notmuch_config_pairs_t *pairs)
379 {
380     return _notmuch_string_map_iterator_value (pairs->iter);
381 }
382
383 void
384 notmuch_config_pairs_destroy (notmuch_config_pairs_t *pairs)
385 {
386     _notmuch_string_map_iterator_destroy (pairs->iter);
387     talloc_free (pairs);
388 }
389
390 notmuch_status_t
391 _notmuch_config_load_from_file (notmuch_database_t *notmuch,
392                                 GKeyFile *file)
393 {
394     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
395     gchar **groups = NULL, **keys, *val;
396
397     if (notmuch->config == NULL)
398         notmuch->config = _notmuch_string_map_create (notmuch);
399
400     if (unlikely (notmuch->config == NULL)) {
401         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
402         goto DONE;
403     }
404
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);
411             if (! val) {
412                 status = NOTMUCH_STATUS_FILE_ERROR;
413                 goto DONE;
414             }
415             _notmuch_string_map_set (notmuch->config, absolute_key, val);
416             g_free (val);
417             talloc_free (absolute_key);
418             if (status)
419                 goto DONE;
420         }
421         g_strfreev (keys);
422     }
423
424   DONE:
425     if (groups)
426         g_strfreev (groups);
427
428     return status;
429 }
430
431 notmuch_status_t
432 notmuch_config_get_bool (notmuch_database_t *notmuch, notmuch_config_key_t key, notmuch_bool_t *val)
433 {
434     const char *key_string, *val_string;
435
436     key_string = _notmuch_config_key_to_string (key);
437     if (! key_string) {
438         return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
439     }
440
441     val_string = _notmuch_string_map_get (notmuch->config, key_string);
442     if (! val_string) {
443         *val = FALSE;
444         return NOTMUCH_STATUS_SUCCESS;
445     }
446
447     if (strcase_equal (val_string, "false") || strcase_equal (val_string, "no"))
448         *val = FALSE;
449     else if (strcase_equal (val_string, "true") || strcase_equal (val_string, "yes"))
450         *val = TRUE;
451     else
452         return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
453
454     return NOTMUCH_STATUS_SUCCESS;
455 }
456
457 static const char *
458 _get_name_from_passwd_file (void *ctx)
459 {
460     long pw_buf_size;
461     char *pw_buf;
462     struct passwd passwd, *ignored;
463     const char *name;
464     int e;
465
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);
469
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);
474     }
475
476     if (e == 0) {
477         char *comma = strchr (passwd.pw_gecos, ',');
478         if (comma)
479             name = talloc_strndup (ctx, passwd.pw_gecos,
480                                    comma - passwd.pw_gecos);
481         else
482             name = talloc_strdup (ctx, passwd.pw_gecos);
483     } else {
484         name = talloc_strdup (ctx, "");
485     }
486
487     talloc_free (pw_buf);
488
489     return name;
490 }
491
492 static char *
493 _get_username_from_passwd_file (void *ctx)
494 {
495     long pw_buf_size;
496     char *pw_buf;
497     struct passwd passwd, *ignored;
498     char *name;
499     int e;
500
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);
504
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);
509     }
510
511     if (e == 0)
512         name = talloc_strdup (ctx, passwd.pw_name);
513     else
514         name = talloc_strdup (ctx, "");
515
516     talloc_free (pw_buf);
517
518     return name;
519 }
520
521 static const char *
522 _get_email_from_passwd_file (void *ctx)
523 {
524
525     char hostname[256];
526     struct hostent *hostent;
527     const char *domainname;
528     char *email;
529
530     char *username = _get_username_from_passwd_file (ctx);
531
532     gethostname (hostname, 256);
533     hostname[255] = '\0';
534
535     hostent = gethostbyname (hostname);
536     if (hostent && (domainname = strchr (hostent->h_name, '.')))
537         domainname += 1;
538     else
539         domainname = "(none)";
540
541     email = talloc_asprintf (ctx, "%s@%s.%s",
542                              username, hostname, domainname);
543
544     talloc_free (username);
545     talloc_free (email);
546     return email;
547 }
548
549 static const char *
550 _notmuch_config_key_to_string (notmuch_config_key_t key)
551 {
552     switch (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:
564         return "new.tags";
565     case NOTMUCH_CONFIG_NEW_IGNORE:
566         return "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:
574         return "user.name";
575     default:
576         return NULL;
577     }
578 }
579
580 static const char *
581 _notmuch_config_default (notmuch_database_t *notmuch, notmuch_config_key_t key)
582 {
583     char *path;
584     const char *name, *email;
585
586     switch (key) {
587     case NOTMUCH_CONFIG_DATABASE_PATH:
588         path = getenv ("MAILDIR");
589         if (path)
590             path = talloc_strdup (notmuch, path);
591         else
592             path = talloc_asprintf (notmuch, "%s/mail",
593                                     getenv ("HOME"));
594         return path;
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:
599         return "";
600     case NOTMUCH_CONFIG_NEW_TAGS:
601         return "unread;inbox";
602     case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
603         return "true";
604     case NOTMUCH_CONFIG_USER_NAME:
605         name = getenv ("NAME");
606         if (name)
607             name = talloc_strdup (notmuch, name);
608         else
609             name = _get_name_from_passwd_file (notmuch);
610         return name;
611     case NOTMUCH_CONFIG_PRIMARY_EMAIL:
612         email = getenv ("EMAIL");
613         if (email)
614             email = talloc_strdup (notmuch, email);
615         else
616             email = _get_email_from_passwd_file (notmuch);
617         return email;
618     case NOTMUCH_CONFIG_NEW_IGNORE:
619         return "";
620     case NOTMUCH_CONFIG_HOOK_DIR:
621     case NOTMUCH_CONFIG_BACKUP_DIR:
622     case NOTMUCH_CONFIG_OTHER_EMAIL:
623         return NULL;
624     default:
625     case NOTMUCH_CONFIG_LAST:
626         INTERNAL_ERROR ("illegal key enum %d", key);
627     }
628 }
629
630 notmuch_status_t
631 _notmuch_config_load_defaults (notmuch_database_t *notmuch)
632 {
633     notmuch_config_key_t key;
634
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);
640
641         val = _notmuch_string_map_get (notmuch->config, key_string);
642         if (! val) {
643             _notmuch_string_map_set (notmuch->config, key_string, _notmuch_config_default (notmuch,
644                                                                                            key));
645         }
646     }
647     return NOTMUCH_STATUS_SUCCESS;
648 }
649
650 const char *
651 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key)
652 {
653
654     return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key));
655 }
656
657 const char *
658 notmuch_config_path (notmuch_database_t *notmuch)
659 {
660     return notmuch->config_path;
661 }
662
663 notmuch_status_t
664 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
665 {
666
667     return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val);
668 }
669
670 void
671 _notmuch_config_cache (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
672 {
673     if (notmuch->config == NULL)
674         notmuch->config = _notmuch_string_map_create (notmuch);
675
676     _notmuch_string_map_set (notmuch->config, _notmuch_config_key_to_string (key), val);
677 }