]> git.notmuchmail.org Git - notmuch/blob - lib/config.cc
lib/config: move g_key_File_get_string before continue
[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 = NULL;
263
264         /* If we opened from a given path, do not overwrite it */
265         if (strcmp (key, "database.path") == 0 &&
266             (notmuch->params & NOTMUCH_PARAM_DATABASE) &&
267             notmuch->xapian_db)
268             continue;
269
270         normalized_val = _expand_path (list, key, notmuch_config_list_value (list));
271         _notmuch_string_map_append (notmuch->config, key, normalized_val);
272         talloc_free (normalized_val);
273     }
274
275     return status;
276 }
277
278 notmuch_config_values_t *
279 notmuch_config_get_values (notmuch_database_t *notmuch, notmuch_config_key_t key)
280 {
281     const char *key_str = _notmuch_config_key_to_string (key);
282
283     if (! key_str)
284         return NULL;
285
286     return notmuch_config_get_values_string (notmuch, key_str);
287 }
288
289 notmuch_config_values_t *
290 notmuch_config_get_values_string (notmuch_database_t *notmuch, const char *key_str)
291 {
292     notmuch_config_values_t *values = NULL;
293     bool ok = false;
294
295     values = talloc (notmuch, notmuch_config_values_t);
296     if (unlikely (! values))
297         goto DONE;
298
299     values->children = talloc_new (values);
300
301     values->string = _notmuch_string_map_get (notmuch->config, key_str);
302     if (! values->string)
303         goto DONE;
304
305     values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
306     ok = true;
307
308   DONE:
309     if (! ok) {
310         if (values)
311             talloc_free (values);
312         return NULL;
313     }
314     return values;
315 }
316
317 notmuch_bool_t
318 notmuch_config_values_valid (notmuch_config_values_t *values)
319 {
320     if (! values)
321         return false;
322
323     return (values->iterator != NULL);
324 }
325
326 const char *
327 notmuch_config_values_get (notmuch_config_values_t *values)
328 {
329     return talloc_strndup (values->children, values->iterator, values->tok_len);
330 }
331
332 void
333 notmuch_config_values_start (notmuch_config_values_t *values)
334 {
335     if (values == NULL)
336         return;
337     if (values->children) {
338         talloc_free (values->children);
339     }
340
341     values->children = talloc_new (values);
342
343     values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
344 }
345
346 void
347 notmuch_config_values_move_to_next (notmuch_config_values_t *values)
348 {
349     values->iterator += values->tok_len;
350     values->iterator = strsplit_len (values->iterator, ';', &(values->tok_len));
351 }
352
353 void
354 notmuch_config_values_destroy (notmuch_config_values_t *values)
355 {
356     talloc_free (values);
357 }
358
359 notmuch_config_pairs_t *
360 notmuch_config_get_pairs (notmuch_database_t *notmuch,
361                           const char *prefix)
362 {
363     notmuch_config_pairs_t *pairs = talloc (notmuch, notmuch_config_pairs_t);
364
365     pairs->iter = _notmuch_string_map_iterator_create (notmuch->config, prefix, false);
366     return pairs;
367 }
368
369 notmuch_bool_t
370 notmuch_config_pairs_valid (notmuch_config_pairs_t *pairs)
371 {
372     return _notmuch_string_map_iterator_valid (pairs->iter);
373 }
374
375 void
376 notmuch_config_pairs_move_to_next (notmuch_config_pairs_t *pairs)
377 {
378     _notmuch_string_map_iterator_move_to_next (pairs->iter);
379 }
380
381 const char *
382 notmuch_config_pairs_key (notmuch_config_pairs_t *pairs)
383 {
384     return _notmuch_string_map_iterator_key (pairs->iter);
385 }
386
387 const char *
388 notmuch_config_pairs_value (notmuch_config_pairs_t *pairs)
389 {
390     return _notmuch_string_map_iterator_value (pairs->iter);
391 }
392
393 void
394 notmuch_config_pairs_destroy (notmuch_config_pairs_t *pairs)
395 {
396     _notmuch_string_map_iterator_destroy (pairs->iter);
397     talloc_free (pairs);
398 }
399
400 static char *
401 _expand_path (void *ctx, const char *key, const char *val)
402 {
403     char *expanded_val;
404
405     if ((strcmp (key, "database.path") == 0 ||
406          strcmp (key, "database.mail_root") == 0 ||
407          strcmp (key, "database.hook_dir") == 0 ||
408          strcmp (key, "database.backup_path") == 0 ) &&
409         val[0] != '/')
410         expanded_val = talloc_asprintf (ctx, "%s/%s", getenv ("HOME"), val);
411     else
412         expanded_val = talloc_strdup (ctx, val);
413
414     return expanded_val;
415 }
416
417 notmuch_status_t
418 _notmuch_config_load_from_file (notmuch_database_t *notmuch,
419                                 GKeyFile *file)
420 {
421     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
422     gchar **groups = NULL, **keys, *val;
423
424     if (notmuch->config == NULL)
425         notmuch->config = _notmuch_string_map_create (notmuch);
426
427     if (unlikely (notmuch->config == NULL)) {
428         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
429         goto DONE;
430     }
431
432     groups = g_key_file_get_groups (file, NULL);
433     for (gchar **grp = groups; *grp; grp++) {
434         keys = g_key_file_get_keys (file, *grp, NULL, NULL);
435         for (gchar **keys_p = keys; *keys_p; keys_p++) {
436             char *absolute_key = talloc_asprintf (notmuch, "%s.%s", *grp,  *keys_p);
437             char *normalized_val;
438
439             /* If we opened from a given path, do not overwrite it */
440             if (strcmp (absolute_key, "database.path") == 0 &&
441                 (notmuch->params & NOTMUCH_PARAM_DATABASE) &&
442                 notmuch->xapian_db)
443                 continue;
444
445             val = g_key_file_get_string (file, *grp, *keys_p, NULL);
446             if (! val) {
447                 status = NOTMUCH_STATUS_FILE_ERROR;
448                 goto DONE;
449             }
450
451             normalized_val = _expand_path (notmuch, absolute_key, val);
452             _notmuch_string_map_set (notmuch->config, absolute_key, normalized_val);
453             g_free (val);
454             talloc_free (absolute_key);
455             talloc_free (normalized_val);
456             if (status)
457                 goto DONE;
458         }
459         g_strfreev (keys);
460     }
461
462   DONE:
463     if (groups)
464         g_strfreev (groups);
465
466     return status;
467 }
468
469 notmuch_status_t
470 notmuch_config_get_bool (notmuch_database_t *notmuch, notmuch_config_key_t key, notmuch_bool_t *val)
471 {
472     const char *key_string, *val_string;
473
474     key_string = _notmuch_config_key_to_string (key);
475     if (! key_string) {
476         return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
477     }
478
479     val_string = _notmuch_string_map_get (notmuch->config, key_string);
480     if (! val_string) {
481         *val = FALSE;
482         return NOTMUCH_STATUS_SUCCESS;
483     }
484
485     if (strcase_equal (val_string, "false") || strcase_equal (val_string, "no"))
486         *val = FALSE;
487     else if (strcase_equal (val_string, "true") || strcase_equal (val_string, "yes"))
488         *val = TRUE;
489     else
490         return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
491
492     return NOTMUCH_STATUS_SUCCESS;
493 }
494
495 static const char *
496 _get_name_from_passwd_file (void *ctx)
497 {
498     long pw_buf_size;
499     char *pw_buf;
500     struct passwd passwd, *ignored;
501     const char *name;
502     int e;
503
504     pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
505     if (pw_buf_size == -1) pw_buf_size = 64;
506     pw_buf = (char *) talloc_size (ctx, pw_buf_size);
507
508     while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
509                             pw_buf_size, &ignored)) == ERANGE) {
510         pw_buf_size = pw_buf_size * 2;
511         pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
512     }
513
514     if (e == 0) {
515         char *comma = strchr (passwd.pw_gecos, ',');
516         if (comma)
517             name = talloc_strndup (ctx, passwd.pw_gecos,
518                                    comma - passwd.pw_gecos);
519         else
520             name = talloc_strdup (ctx, passwd.pw_gecos);
521     } else {
522         name = talloc_strdup (ctx, "");
523     }
524
525     talloc_free (pw_buf);
526
527     return name;
528 }
529
530 static char *
531 _get_username_from_passwd_file (void *ctx)
532 {
533     long pw_buf_size;
534     char *pw_buf;
535     struct passwd passwd, *ignored;
536     char *name;
537     int e;
538
539     pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
540     if (pw_buf_size == -1) pw_buf_size = 64;
541     pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
542
543     while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
544                             pw_buf_size, &ignored)) == ERANGE) {
545         pw_buf_size = pw_buf_size * 2;
546         pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
547     }
548
549     if (e == 0)
550         name = talloc_strdup (ctx, passwd.pw_name);
551     else
552         name = talloc_strdup (ctx, "");
553
554     talloc_free (pw_buf);
555
556     return name;
557 }
558
559 static const char *
560 _get_email_from_passwd_file (void *ctx)
561 {
562     char *email;
563
564     char *username = _get_username_from_passwd_file (ctx);
565
566     email = talloc_asprintf (ctx, "%s@localhost", username);
567
568     talloc_free (username);
569     return email;
570 }
571
572 static const char *
573 _notmuch_config_key_to_string (notmuch_config_key_t key)
574 {
575     switch (key) {
576     case NOTMUCH_CONFIG_DATABASE_PATH:
577         return "database.path";
578     case NOTMUCH_CONFIG_MAIL_ROOT:
579         return "database.mail_root";
580     case NOTMUCH_CONFIG_HOOK_DIR:
581         return "database.hook_dir";
582     case NOTMUCH_CONFIG_BACKUP_DIR:
583         return "database.backup_dir";
584     case NOTMUCH_CONFIG_EXCLUDE_TAGS:
585         return "search.exclude_tags";
586     case NOTMUCH_CONFIG_NEW_TAGS:
587         return "new.tags";
588     case NOTMUCH_CONFIG_NEW_IGNORE:
589         return "new.ignore";
590     case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
591         return "maildir.synchronize_flags";
592     case NOTMUCH_CONFIG_PRIMARY_EMAIL:
593         return "user.primary_email";
594     case NOTMUCH_CONFIG_OTHER_EMAIL:
595         return "user.other_email";
596     case NOTMUCH_CONFIG_USER_NAME:
597         return "user.name";
598     case NOTMUCH_CONFIG_AUTOCOMMIT:
599         return "database.autocommit";
600     case NOTMUCH_CONFIG_EXTRA_HEADERS:
601         return "show.extra_headers";
602     default:
603         return NULL;
604     }
605 }
606
607 static const char *
608 _notmuch_config_default (notmuch_database_t *notmuch, notmuch_config_key_t key)
609 {
610     char *path;
611     const char *name, *email;
612
613     switch (key) {
614     case NOTMUCH_CONFIG_DATABASE_PATH:
615         path = getenv ("MAILDIR");
616         if (path)
617             path = talloc_strdup (notmuch, path);
618         else
619             path = talloc_asprintf (notmuch, "%s/mail",
620                                     getenv ("HOME"));
621         return path;
622     case NOTMUCH_CONFIG_MAIL_ROOT:
623         /* by default, mail root is the same as database path */
624         return notmuch_database_get_path (notmuch);
625     case NOTMUCH_CONFIG_EXCLUDE_TAGS:
626         return "";
627     case NOTMUCH_CONFIG_NEW_TAGS:
628         return "unread;inbox";
629     case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
630         return "true";
631     case NOTMUCH_CONFIG_USER_NAME:
632         name = getenv ("NAME");
633         if (name)
634             name = talloc_strdup (notmuch, name);
635         else
636             name = _get_name_from_passwd_file (notmuch);
637         return name;
638     case NOTMUCH_CONFIG_PRIMARY_EMAIL:
639         email = getenv ("EMAIL");
640         if (email)
641             email = talloc_strdup (notmuch, email);
642         else
643             email = _get_email_from_passwd_file (notmuch);
644         return email;
645     case NOTMUCH_CONFIG_NEW_IGNORE:
646         return "";
647     case NOTMUCH_CONFIG_AUTOCOMMIT:
648         return "8000";
649     case NOTMUCH_CONFIG_EXTRA_HEADERS:
650     case NOTMUCH_CONFIG_HOOK_DIR:
651     case NOTMUCH_CONFIG_BACKUP_DIR:
652     case NOTMUCH_CONFIG_OTHER_EMAIL:
653         return NULL;
654     default:
655     case NOTMUCH_CONFIG_LAST:
656         INTERNAL_ERROR ("illegal key enum %d", key);
657     }
658 }
659
660 notmuch_status_t
661 _notmuch_config_load_defaults (notmuch_database_t *notmuch)
662 {
663     notmuch_config_key_t key;
664     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
665
666     if (notmuch->config == NULL)
667         notmuch->config = _notmuch_string_map_create (notmuch);
668
669     for (key = NOTMUCH_CONFIG_FIRST;
670          key < NOTMUCH_CONFIG_LAST;
671          key = notmuch_config_key_t (key + 1)) {
672         const char *val = notmuch_config_get (notmuch, key);
673         const char *key_string = _notmuch_config_key_to_string (key);
674
675         val = _notmuch_string_map_get (notmuch->config, key_string);
676         if (! val) {
677             if (key == NOTMUCH_CONFIG_MAIL_ROOT && (notmuch->params & NOTMUCH_PARAM_SPLIT))
678                 status = NOTMUCH_STATUS_NO_MAIL_ROOT;
679
680             _notmuch_string_map_set (notmuch->config, key_string, _notmuch_config_default (notmuch,
681                                                                                            key));
682         }
683     }
684     return status;
685 }
686
687 const char *
688 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key)
689 {
690
691     return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key));
692 }
693
694 const char *
695 notmuch_config_path (notmuch_database_t *notmuch)
696 {
697     return notmuch->config_path;
698 }
699
700 notmuch_status_t
701 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
702 {
703
704     return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val);
705 }
706
707 void
708 _notmuch_config_cache (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
709 {
710     if (notmuch->config == NULL)
711         notmuch->config = _notmuch_string_map_create (notmuch);
712
713     _notmuch_string_map_set (notmuch->config, _notmuch_config_key_to_string (key), val);
714 }