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