]> git.notmuchmail.org Git - notmuch/blob - notmuch-config.c
cli: config: make notmuch_config_open() "is new" parameter input only
[notmuch] / notmuch-config.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
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 http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22
23 #include <pwd.h>
24 #include <netdb.h>
25 #include <assert.h>
26
27 static const char toplevel_config_comment[] =
28     " .notmuch-config - Configuration file for the notmuch mail system\n"
29     "\n"
30     " For more information about notmuch, see http://notmuchmail.org";
31
32 static const char database_config_comment[] =
33     " Database configuration\n"
34     "\n"
35     " The only value supported here is 'path' which should be the top-level\n"
36     " directory where your mail currently exists and to where mail will be\n"
37     " delivered in the future. Files should be individual email messages.\n"
38     " Notmuch will store its database within a sub-directory of the path\n"
39     " configured here named \".notmuch\".\n";
40
41 static const char new_config_comment[] =
42     " Configuration for \"notmuch new\"\n"
43     "\n"
44     " The following options are supported here:\n"
45     "\n"
46     "\ttags     A list (separated by ';') of the tags that will be\n"
47     "\t added to all messages incorporated by \"notmuch new\".\n"
48     "\n"
49     "\tignore   A list (separated by ';') of file and directory names\n"
50     "\t that will not be searched for messages by \"notmuch new\".\n"
51     "\n"
52     "\t NOTE: *Every* file/directory that goes by one of those\n"
53     "\t names will be ignored, independent of its depth/location\n"
54     "\t in the mail store.\n";
55
56 static const char user_config_comment[] =
57     " User configuration\n"
58     "\n"
59     " Here is where you can let notmuch know how you would like to be\n"
60     " addressed. Valid settings are\n"
61     "\n"
62     "\tname             Your full name.\n"
63     "\tprimary_email    Your primary email address.\n"
64     "\tother_email      A list (separated by ';') of other email addresses\n"
65     "\t         at which you receive email.\n"
66     "\n"
67     " Notmuch will use the various email addresses configured here when\n"
68     " formatting replies. It will avoid including your own addresses in the\n"
69     " recipient list of replies, and will set the From address based on the\n"
70     " address to which the original email was addressed.\n";
71
72 static const char maildir_config_comment[] =
73     " Maildir compatibility configuration\n"
74     "\n"
75     " The following option is supported here:\n"
76     "\n"
77     "\tsynchronize_flags      Valid values are true and false.\n"
78     "\n"
79     "\tIf true, then the following maildir flags (in message filenames)\n"
80     "\twill be synchronized with the corresponding notmuch tags:\n"
81     "\n"
82     "\t\tFlag   Tag\n"
83     "\t\t----   -------\n"
84     "\t\tD      draft\n"
85     "\t\tF      flagged\n"
86     "\t\tP      passed\n"
87     "\t\tR      replied\n"
88     "\t\tS      unread (added when 'S' flag is not present)\n"
89     "\n"
90     "\tThe \"notmuch new\" command will notice flag changes in filenames\n"
91     "\tand update tags, while the \"notmuch tag\" and \"notmuch restore\"\n"
92     "\tcommands will notice tag changes and update flags in filenames\n";
93
94 static const char search_config_comment[] =
95     " Search configuration\n"
96     "\n"
97     " The following option is supported here:\n"
98     "\n"
99     "\texclude_tags\n"
100     "\t\tA ;-separated list of tags that will be excluded from\n"
101     "\t\tsearch results by default.  Using an excluded tag in a\n"
102     "\t\tquery will override that exclusion.\n";
103
104 struct _notmuch_config {
105     char *filename;
106     GKeyFile *key_file;
107     notmuch_bool_t is_new;
108
109     char *database_path;
110     char *user_name;
111     char *user_primary_email;
112     const char **user_other_email;
113     size_t user_other_email_length;
114     const char **new_tags;
115     size_t new_tags_length;
116     const char **new_ignore;
117     size_t new_ignore_length;
118     notmuch_bool_t maildir_synchronize_flags;
119     const char **search_exclude_tags;
120     size_t search_exclude_tags_length;
121 };
122
123 static int
124 notmuch_config_destructor (notmuch_config_t *config)
125 {
126     if (config->key_file)
127         g_key_file_free (config->key_file);
128
129     return 0;
130 }
131
132 static char *
133 get_name_from_passwd_file (void *ctx)
134 {
135     long pw_buf_size;
136     char *pw_buf;
137     struct passwd passwd, *ignored;
138     char *name;
139     int e;
140
141     pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
142     if (pw_buf_size == -1) pw_buf_size = 64;
143     pw_buf = talloc_size (ctx, pw_buf_size);
144
145     while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
146                             pw_buf_size, &ignored)) == ERANGE) {
147         pw_buf_size = pw_buf_size * 2;
148         pw_buf = talloc_zero_size(ctx, pw_buf_size);
149     }
150
151     if (e == 0) {
152         char *comma = strchr (passwd.pw_gecos, ',');
153         if (comma)
154             name = talloc_strndup (ctx, passwd.pw_gecos,
155                                    comma - passwd.pw_gecos);
156         else
157             name = talloc_strdup (ctx, passwd.pw_gecos);
158     } else {
159         name = talloc_strdup (ctx, "");
160     }
161
162     talloc_free (pw_buf);
163
164     return name;
165 }
166
167 static char *
168 get_username_from_passwd_file (void *ctx)
169 {
170     long pw_buf_size;
171     char *pw_buf;
172     struct passwd passwd, *ignored;
173     char *name;
174     int e;
175
176     pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
177     if (pw_buf_size == -1) pw_buf_size = 64;
178     pw_buf = talloc_zero_size (ctx, pw_buf_size);
179
180     while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
181                             pw_buf_size, &ignored)) == ERANGE) {
182         pw_buf_size = pw_buf_size * 2;
183         pw_buf = talloc_zero_size(ctx, pw_buf_size);
184     }
185
186     if (e == 0)
187         name = talloc_strdup (ctx, passwd.pw_name);
188     else
189         name = talloc_strdup (ctx, "");
190
191     talloc_free (pw_buf);
192
193     return name;
194 }
195
196 /* Open the named notmuch configuration file. If the filename is NULL,
197  * the value of the environment variable $NOTMUCH_CONFIG will be used.
198  * If $NOTMUCH_CONFIG is unset, the default configuration file
199  * ($HOME/.notmuch-config) will be used.
200  *
201  * If any error occurs, (out of memory, or a permission-denied error,
202  * etc.), this function will print a message to stderr and return
203  * NULL.
204  *
205  * FILE NOT FOUND: When the specified configuration file (whether from
206  * 'filename' or the $NOTMUCH_CONFIG environment variable) does not
207  * exist, the behavior of this function depends on the 'is_new_ret'
208  * variable.
209  *
210  *      If is_new_ret is NULL, then a "file not found" message will be
211  *      printed to stderr and NULL will be returned.
212
213  *      If is_new_ret is non-NULL then a default configuration will be
214  *      returned and *is_new_ret will be set to 1 on return so that
215  *      the caller can recognize this case.
216  *
217  *      These default configuration settings are determined as
218  *      follows:
219  *
220  *              database_path:          $HOME/mail
221  *
222  *              user_name:              From /etc/passwd
223  *
224  *              user_primary_mail:      $EMAIL variable if set, otherwise
225  *                                      constructed from the username and
226  *                                      hostname of the current machine.
227  *
228  *              user_other_email:       Not set.
229  *
230  *      The default configuration also contains comments to guide the
231  *      user in editing the file directly.
232  */
233 notmuch_config_t *
234 notmuch_config_open (void *ctx,
235                      const char *filename,
236                      notmuch_bool_t create_new)
237 {
238     GError *error = NULL;
239     size_t tmp;
240     char *notmuch_config_env = NULL;
241     int file_had_database_group;
242     int file_had_new_group;
243     int file_had_user_group;
244     int file_had_maildir_group;
245     int file_had_search_group;
246
247     notmuch_config_t *config = talloc (ctx, notmuch_config_t);
248     if (config == NULL) {
249         fprintf (stderr, "Out of memory.\n");
250         return NULL;
251     }
252     
253     talloc_set_destructor (config, notmuch_config_destructor);
254
255     if (filename) {
256         config->filename = talloc_strdup (config, filename);
257     } else if ((notmuch_config_env = getenv ("NOTMUCH_CONFIG"))) {
258         config->filename = talloc_strdup (config, notmuch_config_env);
259     } else {
260         config->filename = talloc_asprintf (config, "%s/.notmuch-config",
261                                             getenv ("HOME"));
262     }
263
264     config->key_file = g_key_file_new ();
265
266     config->is_new = FALSE;
267     config->database_path = NULL;
268     config->user_name = NULL;
269     config->user_primary_email = NULL;
270     config->user_other_email = NULL;
271     config->user_other_email_length = 0;
272     config->new_tags = NULL;
273     config->new_tags_length = 0;
274     config->new_ignore = NULL;
275     config->new_ignore_length = 0;
276     config->maildir_synchronize_flags = TRUE;
277     config->search_exclude_tags = NULL;
278     config->search_exclude_tags_length = 0;
279
280     if (! g_key_file_load_from_file (config->key_file,
281                                      config->filename,
282                                      G_KEY_FILE_KEEP_COMMENTS,
283                                      &error))
284     {
285         /* If create_new is true, then the caller is prepared for a
286          * default configuration file in the case of FILE NOT
287          * FOUND. Otherwise, any read failure is an error.
288          */
289         if (create_new &&
290             error->domain == G_FILE_ERROR &&
291             error->code == G_FILE_ERROR_NOENT)
292         {
293             g_error_free (error);
294             config->is_new = TRUE;
295         }
296         else
297         {
298             fprintf (stderr, "Error reading configuration file %s: %s\n",
299                      config->filename, error->message);
300             talloc_free (config);
301             g_error_free (error);
302             return NULL;
303         }
304     }
305
306     /* Whenever we know of configuration sections that don't appear in
307      * the configuration file, we add some comments to help the user
308      * understand what can be done.
309      *
310      * It would be convenient to just add those comments now, but
311      * apparently g_key_file will clear any comments when keys are
312      * added later that create the groups. So we have to check for the
313      * groups now, but add the comments only after setting all of our
314      * values.
315      */
316     file_had_database_group = g_key_file_has_group (config->key_file,
317                                                     "database");
318     file_had_new_group = g_key_file_has_group (config->key_file, "new");
319     file_had_user_group = g_key_file_has_group (config->key_file, "user");
320     file_had_maildir_group = g_key_file_has_group (config->key_file, "maildir");
321     file_had_search_group = g_key_file_has_group (config->key_file, "search");
322
323
324     if (notmuch_config_get_database_path (config) == NULL) {
325         char *path = talloc_asprintf (config, "%s/mail",
326                                       getenv ("HOME"));
327         notmuch_config_set_database_path (config, path);
328         talloc_free (path);
329     }
330
331     if (notmuch_config_get_user_name (config) == NULL) {
332         char *name = get_name_from_passwd_file (config);
333         notmuch_config_set_user_name (config, name);
334         talloc_free (name);
335     }
336
337     if (notmuch_config_get_user_primary_email (config) == NULL) {
338         char *email = getenv ("EMAIL");
339         if (email) {
340             notmuch_config_set_user_primary_email (config, email);
341         } else {
342             char hostname[256];
343             struct hostent *hostent;
344             const char *domainname;
345
346             char *username = get_username_from_passwd_file (config);
347
348             gethostname (hostname, 256);
349             hostname[255] = '\0';
350
351             hostent = gethostbyname (hostname);
352             if (hostent && (domainname = strchr (hostent->h_name, '.')))
353                 domainname += 1;
354             else
355                 domainname = "(none)";
356
357             email = talloc_asprintf (config, "%s@%s.%s",
358                                      username, hostname, domainname);
359
360             notmuch_config_set_user_primary_email (config, email);
361
362             talloc_free (username);
363             talloc_free (email);
364         }
365     }
366
367     if (notmuch_config_get_new_tags (config, &tmp) == NULL) {
368         const char *tags[] = { "unread", "inbox" };
369         notmuch_config_set_new_tags (config, tags, 2);
370     }
371
372     if (notmuch_config_get_new_ignore (config, &tmp) == NULL) {
373         notmuch_config_set_new_ignore (config, NULL, 0);
374     }
375
376     if (notmuch_config_get_search_exclude_tags (config, &tmp) == NULL) {
377         if (config->is_new) {
378             const char *tags[] = { "deleted", "spam" };
379             notmuch_config_set_search_exclude_tags (config, tags, 2);
380         } else {
381             notmuch_config_set_search_exclude_tags (config, NULL, 0);
382         }
383     }
384
385     error = NULL;
386     config->maildir_synchronize_flags =
387         g_key_file_get_boolean (config->key_file,
388                                 "maildir", "synchronize_flags", &error);
389     if (error) {
390         notmuch_config_set_maildir_synchronize_flags (config, TRUE);
391         g_error_free (error);
392     }
393
394     /* Whenever we know of configuration sections that don't appear in
395      * the configuration file, we add some comments to help the user
396      * understand what can be done. */
397     if (config->is_new)
398     {
399         g_key_file_set_comment (config->key_file, NULL, NULL,
400                                 toplevel_config_comment, NULL);
401     }
402
403     if (! file_had_database_group)
404     {
405         g_key_file_set_comment (config->key_file, "database", NULL,
406                                 database_config_comment, NULL);
407     }
408
409     if (! file_had_new_group)
410     {
411         g_key_file_set_comment (config->key_file, "new", NULL,
412                                 new_config_comment, NULL);
413     }
414
415     if (! file_had_user_group)
416     {
417         g_key_file_set_comment (config->key_file, "user", NULL,
418                                 user_config_comment, NULL);
419     }
420
421     if (! file_had_maildir_group)
422     {
423         g_key_file_set_comment (config->key_file, "maildir", NULL,
424                                 maildir_config_comment, NULL);
425     }
426
427     if (! file_had_search_group) {
428         g_key_file_set_comment (config->key_file, "search", NULL,
429                                 search_config_comment, NULL);
430     }
431
432     return config;
433 }
434
435 /* Close the given notmuch_config_t object, freeing all resources.
436  * 
437  * Note: Any changes made to the configuration are *not* saved by this
438  * function. To save changes, call notmuch_config_save before
439  * notmuch_config_close.
440 */
441 void
442 notmuch_config_close (notmuch_config_t *config)
443 {
444     talloc_free (config);
445 }
446
447 /* Save any changes made to the notmuch configuration.
448  *
449  * Any comments originally in the file will be preserved.
450  *
451  * Returns 0 if successful, and 1 in case of any error, (after
452  * printing a description of the error to stderr).
453  */
454 int
455 notmuch_config_save (notmuch_config_t *config)
456 {
457     size_t length;
458     char *data;
459     GError *error = NULL;
460
461     data = g_key_file_to_data (config->key_file, &length, NULL);
462     if (data == NULL) {
463         fprintf (stderr, "Out of memory.\n");
464         return 1;
465     }
466
467     if (! g_file_set_contents (config->filename, data, length, &error)) {
468         fprintf (stderr, "Error saving configuration to %s: %s\n",
469                  config->filename, error->message);
470         g_error_free (error);
471         g_free (data);
472         return 1;
473     }
474
475     g_free (data);
476     return 0;
477 }
478
479 notmuch_bool_t
480 notmuch_config_is_new (notmuch_config_t *config)
481 {
482     return config->is_new;
483 }
484
485
486 static const char **
487 _config_get_list (notmuch_config_t *config,
488                   const char *section, const char *key,
489                   const char ***outlist, size_t *list_length, size_t *ret_length)
490 {
491     assert(outlist);
492
493     if (*outlist == NULL) {
494
495         char **inlist = g_key_file_get_string_list (config->key_file,
496                                              section, key, list_length, NULL);
497         if (inlist) {
498             unsigned int i;
499
500             *outlist = talloc_size (config, sizeof (char *) * (*list_length + 1));
501
502             for (i = 0; i < *list_length; i++)
503                 (*outlist)[i] = talloc_strdup (*outlist, inlist[i]);
504
505             (*outlist)[i] = NULL;
506
507             g_strfreev (inlist);
508         }
509     }
510
511     if (ret_length)
512         *ret_length = *list_length;
513
514     return *outlist;
515 }
516
517 static void
518 _config_set_list (notmuch_config_t *config,
519                   const char *group, const char *name,
520                   const char *list[],
521                   size_t length, const char ***config_var )
522 {
523     g_key_file_set_string_list (config->key_file, group, name, list, length);
524     talloc_free (*config_var);
525     *config_var = NULL;
526 }
527
528 const char *
529 notmuch_config_get_database_path (notmuch_config_t *config)
530 {
531     char *path;
532
533     if (config->database_path == NULL) {
534         path = g_key_file_get_string (config->key_file,
535                                       "database", "path", NULL);
536         if (path) {
537             config->database_path = talloc_strdup (config, path);
538             free (path);
539         }
540     }
541
542     return config->database_path;
543 }
544
545 void
546 notmuch_config_set_database_path (notmuch_config_t *config,
547                                   const char *database_path)
548 {
549     g_key_file_set_string (config->key_file,
550                            "database", "path", database_path);
551
552     talloc_free (config->database_path);
553     config->database_path = NULL;
554 }
555
556 const char *
557 notmuch_config_get_user_name (notmuch_config_t *config)
558 {
559     char *name;
560
561     if (config->user_name == NULL) {
562         name = g_key_file_get_string (config->key_file,
563                                       "user", "name", NULL);
564         if (name) {
565             config->user_name = talloc_strdup (config, name);
566             free (name);
567         }
568     }
569
570     return config->user_name;
571 }
572
573 void
574 notmuch_config_set_user_name (notmuch_config_t *config,
575                               const char *user_name)
576 {
577     g_key_file_set_string (config->key_file,
578                            "user", "name", user_name);
579
580     talloc_free (config->user_name);
581     config->user_name = NULL;
582 }
583
584 const char *
585 notmuch_config_get_user_primary_email (notmuch_config_t *config)
586 {
587     char *email;
588
589     if (config->user_primary_email == NULL) {
590         email = g_key_file_get_string (config->key_file,
591                                        "user", "primary_email", NULL);
592         if (email) {
593             config->user_primary_email = talloc_strdup (config, email);
594             free (email);
595         }
596     }
597
598     return config->user_primary_email;
599 }
600
601 void
602 notmuch_config_set_user_primary_email (notmuch_config_t *config,
603                                        const char *primary_email)
604 {
605     g_key_file_set_string (config->key_file,
606                            "user", "primary_email", primary_email);
607
608     talloc_free (config->user_primary_email);
609     config->user_primary_email = NULL;
610 }
611
612 const char **
613 notmuch_config_get_user_other_email (notmuch_config_t *config,   size_t *length)
614 {
615     return _config_get_list (config, "user", "other_email",
616                              &(config->user_other_email),
617                              &(config->user_other_email_length), length);
618 }
619
620 const char **
621 notmuch_config_get_new_tags (notmuch_config_t *config,   size_t *length)
622 {
623     return _config_get_list (config, "new", "tags",
624                              &(config->new_tags),
625                              &(config->new_tags_length), length);
626 }
627
628 const char **
629 notmuch_config_get_new_ignore (notmuch_config_t *config, size_t *length)
630 {
631     return _config_get_list (config, "new", "ignore",
632                              &(config->new_ignore),
633                              &(config->new_ignore_length), length);
634 }
635
636 void
637 notmuch_config_set_user_other_email (notmuch_config_t *config,
638                                      const char *list[],
639                                      size_t length)
640 {
641     _config_set_list (config, "user", "other_email", list, length,
642                      &(config->user_other_email));
643 }
644
645 void
646 notmuch_config_set_new_tags (notmuch_config_t *config,
647                                      const char *list[],
648                                      size_t length)
649 {
650     _config_set_list (config, "new", "tags", list, length,
651                      &(config->new_tags));
652 }
653
654 void
655 notmuch_config_set_new_ignore (notmuch_config_t *config,
656                                const char *list[],
657                                size_t length)
658 {
659     _config_set_list (config, "new", "ignore", list, length,
660                      &(config->new_ignore));
661 }
662
663 const char **
664 notmuch_config_get_search_exclude_tags (notmuch_config_t *config, size_t *length)
665 {
666     return _config_get_list (config, "search", "exclude_tags",
667                              &(config->search_exclude_tags),
668                              &(config->search_exclude_tags_length), length);
669 }
670
671 void
672 notmuch_config_set_search_exclude_tags (notmuch_config_t *config,
673                                       const char *list[],
674                                       size_t length)
675 {
676     _config_set_list (config, "search", "exclude_tags", list, length,
677                       &(config->search_exclude_tags));
678 }
679
680 /* Given a configuration item of the form <group>.<key> return the
681  * component group and key. If any error occurs, print a message on
682  * stderr and return 1. Otherwise, return 0.
683  *
684  * Note: This function modifies the original 'item' string.
685  */
686 static int
687 _item_split (char *item, char **group, char **key)
688 {
689     char *period;
690
691     *group = item;
692
693     period = index (item, '.');
694     if (period == NULL || *(period+1) == '\0') {
695         fprintf (stderr,
696                  "Invalid configuration name: %s\n"
697                  "(Should be of the form <section>.<item>)\n", item);
698         return 1;
699     }
700
701     *period = '\0';
702     *key = period + 1;
703
704     return 0;
705 }
706
707 static int
708 notmuch_config_command_get (void *ctx, char *item)
709 {
710     notmuch_config_t *config;
711
712     config = notmuch_config_open (ctx, NULL, FALSE);
713     if (config == NULL)
714         return 1;
715
716     if (strcmp(item, "database.path") == 0) {
717         printf ("%s\n", notmuch_config_get_database_path (config));
718     } else if (strcmp(item, "user.name") == 0) {
719         printf ("%s\n", notmuch_config_get_user_name (config));
720     } else if (strcmp(item, "user.primary_email") == 0) {
721         printf ("%s\n", notmuch_config_get_user_primary_email (config));
722     } else if (strcmp(item, "user.other_email") == 0) {
723         const char **other_email;
724         size_t i, length;
725         
726         other_email = notmuch_config_get_user_other_email (config, &length);
727         for (i = 0; i < length; i++)
728             printf ("%s\n", other_email[i]);
729     } else if (strcmp(item, "new.tags") == 0) {
730         const char **tags;
731         size_t i, length;
732
733         tags = notmuch_config_get_new_tags (config, &length);
734         for (i = 0; i < length; i++)
735             printf ("%s\n", tags[i]);
736     } else {
737         char **value;
738         size_t i, length;
739         char *group, *key;
740
741         if (_item_split (item, &group, &key))
742             return 1;
743
744         value = g_key_file_get_string_list (config->key_file,
745                                             group, key,
746                                             &length, NULL);
747         if (value == NULL) {
748             fprintf (stderr, "Unknown configuration item: %s.%s\n",
749                      group, key);
750             return 1;
751         }
752
753         for (i = 0; i < length; i++)
754             printf ("%s\n", value[i]);
755
756         g_strfreev (value);
757     }
758
759     notmuch_config_close (config);
760
761     return 0;
762 }
763
764 static int
765 notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[])
766 {
767     notmuch_config_t *config;
768     char *group, *key;
769     int ret;
770
771     if (_item_split (item, &group, &key))
772         return 1;
773
774     config = notmuch_config_open (ctx, NULL, FALSE);
775     if (config == NULL)
776         return 1;
777
778     /* With only the name of an item, we clear it from the
779      * configuration file.
780      *
781      * With a single value, we set it as a string.
782      *
783      * With multiple values, we set them as a string list.
784      */
785     switch (argc) {
786     case 0:
787         g_key_file_remove_key (config->key_file, group, key, NULL);
788         break;
789     case 1:
790         g_key_file_set_string (config->key_file, group, key, argv[0]);
791         break;
792     default:
793         g_key_file_set_string_list (config->key_file, group, key,
794                                     (const gchar **) argv, argc);
795         break;
796     }
797
798     ret = notmuch_config_save (config);
799     notmuch_config_close (config);
800
801     return ret;
802 }
803
804 static int
805 notmuch_config_command_list (void *ctx)
806 {
807     notmuch_config_t *config;
808     char **groups;
809     size_t g, groups_length;
810
811     config = notmuch_config_open (ctx, NULL, FALSE);
812     if (config == NULL)
813         return 1;
814
815     groups = g_key_file_get_groups (config->key_file, &groups_length);
816     if (groups == NULL)
817         return 1;
818
819     for (g = 0; g < groups_length; g++) {
820         char **keys;
821         size_t k, keys_length;
822
823         keys = g_key_file_get_keys (config->key_file,
824                                     groups[g], &keys_length, NULL);
825         if (keys == NULL)
826             continue;
827
828         for (k = 0; k < keys_length; k++) {
829             char *value;
830
831             value = g_key_file_get_string (config->key_file,
832                                            groups[g], keys[k], NULL);
833             if (value != NULL) {
834                 printf ("%s.%s=%s\n", groups[g], keys[k], value);
835                 free (value);
836             }
837         }
838
839         g_strfreev (keys);
840     }
841
842     g_strfreev (groups);
843
844     notmuch_config_close (config);
845
846     return 0;
847 }
848
849 int
850 notmuch_config_command (void *ctx, int argc, char *argv[])
851 {
852     argc--; argv++; /* skip subcommand argument */
853
854     if (argc < 1) {
855         fprintf (stderr, "Error: notmuch config requires at least one argument.\n");
856         return 1;
857     }
858
859     if (strcmp (argv[0], "get") == 0) {
860         if (argc != 2) {
861             fprintf (stderr, "Error: notmuch config get requires exactly "
862                      "one argument.\n");
863             return 1;
864         }
865         return notmuch_config_command_get (ctx, argv[1]);
866     } else if (strcmp (argv[0], "set") == 0) {
867         if (argc < 2) {
868             fprintf (stderr, "Error: notmuch config set requires at least "
869                      "one argument.\n");
870             return 1;
871         }
872         return notmuch_config_command_set (ctx, argv[1], argc - 2, argv + 2);
873     } else if (strcmp (argv[0], "list") == 0) {
874         return notmuch_config_command_list (ctx);
875     }
876
877     fprintf (stderr, "Unrecognized argument for notmuch config: %s\n",
878              argv[0]);
879     return 1;
880 }
881
882 notmuch_bool_t
883 notmuch_config_get_maildir_synchronize_flags (notmuch_config_t *config)
884 {
885     return config->maildir_synchronize_flags;
886 }
887
888 void
889 notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,
890                                               notmuch_bool_t synchronize_flags)
891 {
892     g_key_file_set_boolean (config->key_file,
893                             "maildir", "synchronize_flags", synchronize_flags);
894     config->maildir_synchronize_flags = synchronize_flags;
895 }