]> git.notmuchmail.org Git - notmuch/blob - lib/config.cc
abdc19c3de000b610d57f9f98d71127a51797f7f
[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 static char *
391 _expand_path (void *ctx, const char *key, const char *val)
392 {
393     char *expanded_val;
394
395     if ((strcmp (key, "database.path") == 0 ||
396          strcmp (key, "database.mail_root") == 0 ||
397          strcmp (key, "database.hook_dir") == 0 ||
398          strcmp (key, "database.backup_path") == 0 ) &&
399         val[0] != '/')
400         expanded_val = talloc_asprintf (ctx, "%s/%s", getenv ("HOME"), val);
401     else
402         expanded_val = talloc_strdup (ctx, val);
403
404     return expanded_val;
405 }
406
407 notmuch_status_t
408 _notmuch_config_load_from_file (notmuch_database_t *notmuch,
409                                 GKeyFile *file)
410 {
411     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
412     gchar **groups = NULL, **keys, *val;
413
414     if (notmuch->config == NULL)
415         notmuch->config = _notmuch_string_map_create (notmuch);
416
417     if (unlikely (notmuch->config == NULL)) {
418         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
419         goto DONE;
420     }
421
422     groups = g_key_file_get_groups (file, NULL);
423     for (gchar **grp = groups; *grp; grp++) {
424         keys = g_key_file_get_keys (file, *grp, NULL, NULL);
425         for (gchar **keys_p = keys; *keys_p; keys_p++) {
426             char *absolute_key = talloc_asprintf (notmuch, "%s.%s", *grp,  *keys_p);
427             char *normalized_val;
428             val = g_key_file_get_value (file, *grp, *keys_p, NULL);
429             if (! val) {
430                 status = NOTMUCH_STATUS_FILE_ERROR;
431                 goto DONE;
432             }
433             normalized_val = _expand_path (notmuch, absolute_key, val);
434             _notmuch_string_map_set (notmuch->config, absolute_key, normalized_val);
435             g_free (val);
436             talloc_free (absolute_key);
437             talloc_free (normalized_val);
438             if (status)
439                 goto DONE;
440         }
441         g_strfreev (keys);
442     }
443
444   DONE:
445     if (groups)
446         g_strfreev (groups);
447
448     return status;
449 }
450
451 notmuch_status_t
452 notmuch_config_get_bool (notmuch_database_t *notmuch, notmuch_config_key_t key, notmuch_bool_t *val)
453 {
454     const char *key_string, *val_string;
455
456     key_string = _notmuch_config_key_to_string (key);
457     if (! key_string) {
458         return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
459     }
460
461     val_string = _notmuch_string_map_get (notmuch->config, key_string);
462     if (! val_string) {
463         *val = FALSE;
464         return NOTMUCH_STATUS_SUCCESS;
465     }
466
467     if (strcase_equal (val_string, "false") || strcase_equal (val_string, "no"))
468         *val = FALSE;
469     else if (strcase_equal (val_string, "true") || strcase_equal (val_string, "yes"))
470         *val = TRUE;
471     else
472         return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
473
474     return NOTMUCH_STATUS_SUCCESS;
475 }
476
477 static const char *
478 _get_name_from_passwd_file (void *ctx)
479 {
480     long pw_buf_size;
481     char *pw_buf;
482     struct passwd passwd, *ignored;
483     const char *name;
484     int e;
485
486     pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
487     if (pw_buf_size == -1) pw_buf_size = 64;
488     pw_buf = (char *) talloc_size (ctx, pw_buf_size);
489
490     while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
491                             pw_buf_size, &ignored)) == ERANGE) {
492         pw_buf_size = pw_buf_size * 2;
493         pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
494     }
495
496     if (e == 0) {
497         char *comma = strchr (passwd.pw_gecos, ',');
498         if (comma)
499             name = talloc_strndup (ctx, passwd.pw_gecos,
500                                    comma - passwd.pw_gecos);
501         else
502             name = talloc_strdup (ctx, passwd.pw_gecos);
503     } else {
504         name = talloc_strdup (ctx, "");
505     }
506
507     talloc_free (pw_buf);
508
509     return name;
510 }
511
512 static char *
513 _get_username_from_passwd_file (void *ctx)
514 {
515     long pw_buf_size;
516     char *pw_buf;
517     struct passwd passwd, *ignored;
518     char *name;
519     int e;
520
521     pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
522     if (pw_buf_size == -1) pw_buf_size = 64;
523     pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
524
525     while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
526                             pw_buf_size, &ignored)) == ERANGE) {
527         pw_buf_size = pw_buf_size * 2;
528         pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
529     }
530
531     if (e == 0)
532         name = talloc_strdup (ctx, passwd.pw_name);
533     else
534         name = talloc_strdup (ctx, "");
535
536     talloc_free (pw_buf);
537
538     return name;
539 }
540
541 static const char *
542 _get_email_from_passwd_file (void *ctx)
543 {
544
545     char hostname[256];
546     struct hostent *hostent;
547     const char *domainname;
548     char *email;
549
550     char *username = _get_username_from_passwd_file (ctx);
551
552     gethostname (hostname, 256);
553     hostname[255] = '\0';
554
555     hostent = gethostbyname (hostname);
556     if (hostent && (domainname = strchr (hostent->h_name, '.')))
557         domainname += 1;
558     else
559         domainname = "(none)";
560
561     email = talloc_asprintf (ctx, "%s@%s.%s",
562                              username, hostname, domainname);
563
564     talloc_free (username);
565     return email;
566 }
567
568 static const char *
569 _notmuch_config_key_to_string (notmuch_config_key_t key)
570 {
571     switch (key) {
572     case NOTMUCH_CONFIG_DATABASE_PATH:
573         return "database.path";
574     case NOTMUCH_CONFIG_MAIL_ROOT:
575         return "database.mail_root";
576     case NOTMUCH_CONFIG_HOOK_DIR:
577         return "database.hook_dir";
578     case NOTMUCH_CONFIG_BACKUP_DIR:
579         return "database.backup_dir";
580     case NOTMUCH_CONFIG_EXCLUDE_TAGS:
581         return "search.exclude_tags";
582     case NOTMUCH_CONFIG_NEW_TAGS:
583         return "new.tags";
584     case NOTMUCH_CONFIG_NEW_IGNORE:
585         return "new.ignore";
586     case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
587         return "maildir.synchronize_flags";
588     case NOTMUCH_CONFIG_PRIMARY_EMAIL:
589         return "user.primary_email";
590     case NOTMUCH_CONFIG_OTHER_EMAIL:
591         return "user.other_email";
592     case NOTMUCH_CONFIG_USER_NAME:
593         return "user.name";
594     default:
595         return NULL;
596     }
597 }
598
599 static const char *
600 _notmuch_config_default (notmuch_database_t *notmuch, notmuch_config_key_t key)
601 {
602     char *path;
603     const char *name, *email;
604
605     switch (key) {
606     case NOTMUCH_CONFIG_DATABASE_PATH:
607         path = getenv ("MAILDIR");
608         if (path)
609             path = talloc_strdup (notmuch, path);
610         else
611             path = talloc_asprintf (notmuch, "%s/mail",
612                                     getenv ("HOME"));
613         return path;
614     case NOTMUCH_CONFIG_MAIL_ROOT:
615         /* by default, mail root is the same as database path */
616         return notmuch_database_get_path (notmuch);
617     case NOTMUCH_CONFIG_EXCLUDE_TAGS:
618         return "";
619     case NOTMUCH_CONFIG_NEW_TAGS:
620         return "unread;inbox";
621     case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
622         return "true";
623     case NOTMUCH_CONFIG_USER_NAME:
624         name = getenv ("NAME");
625         if (name)
626             name = talloc_strdup (notmuch, name);
627         else
628             name = _get_name_from_passwd_file (notmuch);
629         return name;
630     case NOTMUCH_CONFIG_PRIMARY_EMAIL:
631         email = getenv ("EMAIL");
632         if (email)
633             email = talloc_strdup (notmuch, email);
634         else
635             email = _get_email_from_passwd_file (notmuch);
636         return email;
637     case NOTMUCH_CONFIG_NEW_IGNORE:
638         return "";
639     case NOTMUCH_CONFIG_HOOK_DIR:
640     case NOTMUCH_CONFIG_BACKUP_DIR:
641     case NOTMUCH_CONFIG_OTHER_EMAIL:
642         return NULL;
643     default:
644     case NOTMUCH_CONFIG_LAST:
645         INTERNAL_ERROR ("illegal key enum %d", key);
646     }
647 }
648
649 notmuch_status_t
650 _notmuch_config_load_defaults (notmuch_database_t *notmuch)
651 {
652     notmuch_config_key_t key;
653
654     for (key = NOTMUCH_CONFIG_FIRST;
655          key < NOTMUCH_CONFIG_LAST;
656          key = notmuch_config_key_t (key + 1)) {
657         const char *val = notmuch_config_get (notmuch, key);
658         const char *key_string = _notmuch_config_key_to_string (key);
659
660         val = _notmuch_string_map_get (notmuch->config, key_string);
661         if (! val) {
662             _notmuch_string_map_set (notmuch->config, key_string, _notmuch_config_default (notmuch,
663                                                                                            key));
664         }
665     }
666     return NOTMUCH_STATUS_SUCCESS;
667 }
668
669 const char *
670 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key)
671 {
672
673     return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key));
674 }
675
676 const char *
677 notmuch_config_path (notmuch_database_t *notmuch)
678 {
679     return notmuch->config_path;
680 }
681
682 notmuch_status_t
683 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
684 {
685
686     return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val);
687 }
688
689 void
690 _notmuch_config_cache (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
691 {
692     if (notmuch->config == NULL)
693         notmuch->config = _notmuch_string_map_create (notmuch);
694
695     _notmuch_string_map_set (notmuch->config, _notmuch_config_key_to_string (key), val);
696 }