1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
27 static const char toplevel_config_comment[] =
28 " .notmuch-config - Configuration file for the notmuch mail system\n"
30 " For more information about notmuch, see http://notmuchmail.org";
32 static const char database_config_comment[] =
33 " Database configuration\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";
41 static const char new_config_comment[] =
42 " Configuration for \"notmuch new\"\n"
44 " The following options are supported here:\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"
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"
52 "\t NOTE: *Every* file/directory that goes by one of those names will\n"
53 "\t be ignored, independent of its depth/location in the mail store.\n";
55 static const char user_config_comment[] =
56 " User configuration\n"
58 " Here is where you can let notmuch know how you would like to be\n"
59 " addressed. Valid settings are\n"
61 "\tname Your full name.\n"
62 "\tprimary_email Your primary email address.\n"
63 "\tother_email A list (separated by ';') of other email addresses\n"
64 "\t at which you receive email.\n"
66 " Notmuch will use the various email addresses configured here when\n"
67 " formatting replies. It will avoid including your own addresses in the\n"
68 " recipient list of replies, and will set the From address based on the\n"
69 " address to which the original email was addressed.\n";
71 static const char maildir_config_comment[] =
72 " Maildir compatibility configuration\n"
74 " The following option is supported here:\n"
76 "\tsynchronize_flags Valid values are true and false.\n"
78 "\tIf true, then the following maildir flags (in message filenames)\n"
79 "\twill be synchronized with the corresponding notmuch tags:\n"
87 "\t\tS unread (added when 'S' flag is not present)\n"
89 "\tThe \"notmuch new\" command will notice flag changes in filenames\n"
90 "\tand update tags, while the \"notmuch tag\" and \"notmuch restore\"\n"
91 "\tcommands will notice tag changes and update flags in filenames\n";
93 static const char search_config_comment[] =
94 " Search configuration\n"
96 " The following option is supported here:\n"
99 "\t\tA ;-separated list of tags that will be excluded from\n"
100 "\t\tsearch results by default. Using an excluded tag in a\n"
101 "\t\tquery will override that exclusion.\n";
103 struct _notmuch_config {
109 char *user_primary_email;
110 const char **user_other_email;
111 size_t user_other_email_length;
112 const char **new_tags;
113 size_t new_tags_length;
114 const char **new_ignore;
115 size_t new_ignore_length;
116 notmuch_bool_t maildir_synchronize_flags;
117 const char **search_exclude_tags;
118 size_t search_exclude_tags_length;
122 notmuch_config_destructor (notmuch_config_t *config)
124 if (config->key_file)
125 g_key_file_free (config->key_file);
131 get_name_from_passwd_file (void *ctx)
135 struct passwd passwd, *ignored;
139 pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
140 if (pw_buf_size == -1) pw_buf_size = 64;
141 pw_buf = talloc_size (ctx, pw_buf_size);
143 while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
144 pw_buf_size, &ignored)) == ERANGE) {
145 pw_buf_size = pw_buf_size * 2;
146 pw_buf = talloc_zero_size(ctx, pw_buf_size);
150 char *comma = strchr (passwd.pw_gecos, ',');
152 name = talloc_strndup (ctx, passwd.pw_gecos,
153 comma - passwd.pw_gecos);
155 name = talloc_strdup (ctx, passwd.pw_gecos);
157 name = talloc_strdup (ctx, "");
160 talloc_free (pw_buf);
166 get_username_from_passwd_file (void *ctx)
170 struct passwd passwd, *ignored;
174 pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
175 if (pw_buf_size == -1) pw_buf_size = 64;
176 pw_buf = talloc_zero_size (ctx, pw_buf_size);
178 while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
179 pw_buf_size, &ignored)) == ERANGE) {
180 pw_buf_size = pw_buf_size * 2;
181 pw_buf = talloc_zero_size(ctx, pw_buf_size);
185 name = talloc_strdup (ctx, passwd.pw_name);
187 name = talloc_strdup (ctx, "");
189 talloc_free (pw_buf);
194 /* Open the named notmuch configuration file. If the filename is NULL,
195 * the value of the environment variable $NOTMUCH_CONFIG will be used.
196 * If $NOTMUCH_CONFIG is unset, the default configuration file
197 * ($HOME/.notmuch-config) will be used.
199 * If any error occurs, (out of memory, or a permission-denied error,
200 * etc.), this function will print a message to stderr and return
203 * FILE NOT FOUND: When the specified configuration file (whether from
204 * 'filename' or the $NOTMUCH_CONFIG environment variable) does not
205 * exist, the behavior of this function depends on the 'is_new_ret'
208 * If is_new_ret is NULL, then a "file not found" message will be
209 * printed to stderr and NULL will be returned.
211 * If is_new_ret is non-NULL then a default configuration will be
212 * returned and *is_new_ret will be set to 1 on return so that
213 * the caller can recognize this case.
215 * These default configuration settings are determined as
218 * database_path: $HOME/mail
220 * user_name: From /etc/passwd
222 * user_primary_mail: $EMAIL variable if set, otherwise
223 * constructed from the username and
224 * hostname of the current machine.
226 * user_other_email: Not set.
228 * The default configuration also contains comments to guide the
229 * user in editing the file directly.
232 notmuch_config_open (void *ctx,
233 const char *filename,
234 notmuch_bool_t *is_new_ret)
236 GError *error = NULL;
239 char *notmuch_config_env = NULL;
240 int file_had_database_group;
241 int file_had_new_group;
242 int file_had_user_group;
243 int file_had_maildir_group;
244 int file_had_search_group;
249 notmuch_config_t *config = talloc (ctx, notmuch_config_t);
250 if (config == NULL) {
251 fprintf (stderr, "Out of memory.\n");
255 talloc_set_destructor (config, notmuch_config_destructor);
258 config->filename = talloc_strdup (config, filename);
259 } else if ((notmuch_config_env = getenv ("NOTMUCH_CONFIG"))) {
260 config->filename = talloc_strdup (config, notmuch_config_env);
262 config->filename = talloc_asprintf (config, "%s/.notmuch-config",
266 config->key_file = g_key_file_new ();
268 config->database_path = NULL;
269 config->user_name = NULL;
270 config->user_primary_email = NULL;
271 config->user_other_email = NULL;
272 config->user_other_email_length = 0;
273 config->new_tags = NULL;
274 config->new_tags_length = 0;
275 config->new_ignore = NULL;
276 config->new_ignore_length = 0;
277 config->maildir_synchronize_flags = TRUE;
278 config->search_exclude_tags = NULL;
279 config->search_exclude_tags_length = 0;
281 if (! g_key_file_load_from_file (config->key_file,
283 G_KEY_FILE_KEEP_COMMENTS,
286 /* If the caller passed a non-NULL value for is_new_ret, then
287 * the caller is prepared for a default configuration file in
288 * the case of FILE NOT FOUND. Otherwise, any read failure is
292 error->domain == G_FILE_ERROR &&
293 error->code == G_FILE_ERROR_NOENT)
295 g_error_free (error);
300 fprintf (stderr, "Error reading configuration file %s: %s\n",
301 config->filename, error->message);
302 talloc_free (config);
303 g_error_free (error);
308 /* Whenever we know of configuration sections that don't appear in
309 * the configuration file, we add some comments to help the user
310 * understand what can be done.
312 * It would be convenient to just add those comments now, but
313 * apparently g_key_file will clear any comments when keys are
314 * added later that create the groups. So we have to check for the
315 * groups now, but add the comments only after setting all of our
318 file_had_database_group = g_key_file_has_group (config->key_file,
320 file_had_new_group = g_key_file_has_group (config->key_file, "new");
321 file_had_user_group = g_key_file_has_group (config->key_file, "user");
322 file_had_maildir_group = g_key_file_has_group (config->key_file, "maildir");
323 file_had_search_group = g_key_file_has_group (config->key_file, "search");
326 if (notmuch_config_get_database_path (config) == NULL) {
327 char *path = talloc_asprintf (config, "%s/mail",
329 notmuch_config_set_database_path (config, path);
333 if (notmuch_config_get_user_name (config) == NULL) {
334 char *name = get_name_from_passwd_file (config);
335 notmuch_config_set_user_name (config, name);
339 if (notmuch_config_get_user_primary_email (config) == NULL) {
340 char *email = getenv ("EMAIL");
342 notmuch_config_set_user_primary_email (config, email);
345 struct hostent *hostent;
346 const char *domainname;
348 char *username = get_username_from_passwd_file (config);
350 gethostname (hostname, 256);
351 hostname[255] = '\0';
353 hostent = gethostbyname (hostname);
354 if (hostent && (domainname = strchr (hostent->h_name, '.')))
357 domainname = "(none)";
359 email = talloc_asprintf (config, "%s@%s.%s",
360 username, hostname, domainname);
362 notmuch_config_set_user_primary_email (config, email);
364 talloc_free (username);
369 if (notmuch_config_get_new_tags (config, &tmp) == NULL) {
370 const char *tags[] = { "unread", "inbox" };
371 notmuch_config_set_new_tags (config, tags, 2);
374 if (notmuch_config_get_new_ignore (config, &tmp) == NULL) {
375 notmuch_config_set_new_ignore (config, NULL, 0);
378 if (notmuch_config_get_search_exclude_tags (config, &tmp) == NULL) {
380 /* We do not set default search_exclude_tags for 0.12 */
382 notmuch_config_set_search_exclude_tags (config, NULL, 0);
387 config->maildir_synchronize_flags =
388 g_key_file_get_boolean (config->key_file,
389 "maildir", "synchronize_flags", &error);
391 notmuch_config_set_maildir_synchronize_flags (config, TRUE);
392 g_error_free (error);
395 /* Whenever we know of configuration sections that don't appear in
396 * the configuration file, we add some comments to help the user
397 * understand what can be done. */
400 g_key_file_set_comment (config->key_file, NULL, NULL,
401 toplevel_config_comment, NULL);
404 if (! file_had_database_group)
406 g_key_file_set_comment (config->key_file, "database", NULL,
407 database_config_comment, NULL);
410 if (! file_had_new_group)
412 g_key_file_set_comment (config->key_file, "new", NULL,
413 new_config_comment, NULL);
416 if (! file_had_user_group)
418 g_key_file_set_comment (config->key_file, "user", NULL,
419 user_config_comment, NULL);
422 if (! file_had_maildir_group)
424 g_key_file_set_comment (config->key_file, "maildir", NULL,
425 maildir_config_comment, NULL);
428 if (! file_had_search_group) {
429 g_key_file_set_comment (config->key_file, "search", NULL,
430 search_config_comment, NULL);
434 *is_new_ret = is_new;
439 /* Close the given notmuch_config_t object, freeing all resources.
441 * Note: Any changes made to the configuration are *not* saved by this
442 * function. To save changes, call notmuch_config_save before
443 * notmuch_config_close.
446 notmuch_config_close (notmuch_config_t *config)
448 talloc_free (config);
451 /* Save any changes made to the notmuch configuration.
453 * Any comments originally in the file will be preserved.
455 * Returns 0 if successful, and 1 in case of any error, (after
456 * printing a description of the error to stderr).
459 notmuch_config_save (notmuch_config_t *config)
463 GError *error = NULL;
465 data = g_key_file_to_data (config->key_file, &length, NULL);
467 fprintf (stderr, "Out of memory.\n");
471 if (! g_file_set_contents (config->filename, data, length, &error)) {
472 fprintf (stderr, "Error saving configuration to %s: %s\n",
473 config->filename, error->message);
474 g_error_free (error);
484 _config_get_list (notmuch_config_t *config,
485 const char *section, const char *key,
486 const char ***outlist, size_t *list_length, size_t *ret_length)
490 if (*outlist == NULL) {
492 char **inlist = g_key_file_get_string_list (config->key_file,
493 section, key, list_length, NULL);
497 *outlist = talloc_size (config, sizeof (char *) * (*list_length + 1));
499 for (i = 0; i < *list_length; i++)
500 (*outlist)[i] = talloc_strdup (*outlist, inlist[i]);
502 (*outlist)[i] = NULL;
509 *ret_length = *list_length;
515 _config_set_list (notmuch_config_t *config,
516 const char *group, const char *name,
518 size_t length, const char ***config_var )
520 g_key_file_set_string_list (config->key_file, group, name, list, length);
521 talloc_free (*config_var);
526 notmuch_config_get_database_path (notmuch_config_t *config)
530 if (config->database_path == NULL) {
531 path = g_key_file_get_string (config->key_file,
532 "database", "path", NULL);
534 config->database_path = talloc_strdup (config, path);
539 return config->database_path;
543 notmuch_config_set_database_path (notmuch_config_t *config,
544 const char *database_path)
546 g_key_file_set_string (config->key_file,
547 "database", "path", database_path);
549 talloc_free (config->database_path);
550 config->database_path = NULL;
554 notmuch_config_get_user_name (notmuch_config_t *config)
558 if (config->user_name == NULL) {
559 name = g_key_file_get_string (config->key_file,
560 "user", "name", NULL);
562 config->user_name = talloc_strdup (config, name);
567 return config->user_name;
571 notmuch_config_set_user_name (notmuch_config_t *config,
572 const char *user_name)
574 g_key_file_set_string (config->key_file,
575 "user", "name", user_name);
577 talloc_free (config->user_name);
578 config->user_name = NULL;
582 notmuch_config_get_user_primary_email (notmuch_config_t *config)
586 if (config->user_primary_email == NULL) {
587 email = g_key_file_get_string (config->key_file,
588 "user", "primary_email", NULL);
590 config->user_primary_email = talloc_strdup (config, email);
595 return config->user_primary_email;
599 notmuch_config_set_user_primary_email (notmuch_config_t *config,
600 const char *primary_email)
602 g_key_file_set_string (config->key_file,
603 "user", "primary_email", primary_email);
605 talloc_free (config->user_primary_email);
606 config->user_primary_email = NULL;
610 notmuch_config_get_user_other_email (notmuch_config_t *config, size_t *length)
612 return _config_get_list (config, "user", "other_email",
613 &(config->user_other_email),
614 &(config->user_other_email_length), length);
618 notmuch_config_get_new_tags (notmuch_config_t *config, size_t *length)
620 return _config_get_list (config, "new", "tags",
622 &(config->new_tags_length), length);
626 notmuch_config_get_new_ignore (notmuch_config_t *config, size_t *length)
628 return _config_get_list (config, "new", "ignore",
629 &(config->new_ignore),
630 &(config->new_ignore_length), length);
634 notmuch_config_set_user_other_email (notmuch_config_t *config,
638 _config_set_list (config, "user", "other_email", list, length,
639 &(config->user_other_email));
643 notmuch_config_set_new_tags (notmuch_config_t *config,
647 _config_set_list (config, "new", "tags", list, length,
648 &(config->new_tags));
652 notmuch_config_set_new_ignore (notmuch_config_t *config,
656 _config_set_list (config, "new", "ignore", list, length,
657 &(config->new_ignore));
661 notmuch_config_get_search_exclude_tags (notmuch_config_t *config, size_t *length)
663 return _config_get_list (config, "search", "exclude_tags",
664 &(config->search_exclude_tags),
665 &(config->search_exclude_tags_length), length);
669 notmuch_config_set_search_exclude_tags (notmuch_config_t *config,
673 _config_set_list (config, "search", "exclude_tags", list, length,
674 &(config->search_exclude_tags));
677 /* Given a configuration item of the form <group>.<key> return the
678 * component group and key. If any error occurs, print a message on
679 * stderr and return 1. Otherwise, return 0.
681 * Note: This function modifies the original 'item' string.
684 _item_split (char *item, char **group, char **key)
690 period = index (item, '.');
691 if (period == NULL || *(period+1) == '\0') {
693 "Invalid configuration name: %s\n"
694 "(Should be of the form <section>.<item>)\n", item);
705 notmuch_config_command_get (void *ctx, char *item)
707 notmuch_config_t *config;
709 config = notmuch_config_open (ctx, NULL, NULL);
713 if (strcmp(item, "database.path") == 0) {
714 printf ("%s\n", notmuch_config_get_database_path (config));
715 } else if (strcmp(item, "user.name") == 0) {
716 printf ("%s\n", notmuch_config_get_user_name (config));
717 } else if (strcmp(item, "user.primary_email") == 0) {
718 printf ("%s\n", notmuch_config_get_user_primary_email (config));
719 } else if (strcmp(item, "user.other_email") == 0) {
720 const char **other_email;
723 other_email = notmuch_config_get_user_other_email (config, &length);
724 for (i = 0; i < length; i++)
725 printf ("%s\n", other_email[i]);
726 } else if (strcmp(item, "new.tags") == 0) {
730 tags = notmuch_config_get_new_tags (config, &length);
731 for (i = 0; i < length; i++)
732 printf ("%s\n", tags[i]);
738 if (_item_split (item, &group, &key))
741 value = g_key_file_get_string_list (config->key_file,
745 fprintf (stderr, "Unknown configuration item: %s.%s\n",
750 for (i = 0; i < length; i++)
751 printf ("%s\n", value[i]);
756 notmuch_config_close (config);
762 notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[])
764 notmuch_config_t *config;
768 if (_item_split (item, &group, &key))
771 config = notmuch_config_open (ctx, NULL, NULL);
775 /* With only the name of an item, we clear it from the
776 * configuration file.
778 * With a single value, we set it as a string.
780 * With multiple values, we set them as a string list.
784 g_key_file_remove_key (config->key_file, group, key, NULL);
787 g_key_file_set_string (config->key_file, group, key, argv[0]);
790 g_key_file_set_string_list (config->key_file, group, key,
791 (const gchar **) argv, argc);
795 ret = notmuch_config_save (config);
796 notmuch_config_close (config);
802 notmuch_config_command (void *ctx, int argc, char *argv[])
804 argc--; argv++; /* skip subcommand argument */
807 fprintf (stderr, "Error: notmuch config requires at least two arguments.\n");
811 if (strcmp (argv[0], "get") == 0)
812 return notmuch_config_command_get (ctx, argv[1]);
813 else if (strcmp (argv[0], "set") == 0)
814 return notmuch_config_command_set (ctx, argv[1], argc - 2, argv + 2);
816 fprintf (stderr, "Unrecognized argument for notmuch config: %s\n",
822 notmuch_config_get_maildir_synchronize_flags (notmuch_config_t *config)
824 return config->maildir_synchronize_flags;
828 notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,
829 notmuch_bool_t synchronize_flags)
831 g_key_file_set_boolean (config->key_file,
832 "maildir", "synchronize_flags", synchronize_flags);
833 config->maildir_synchronize_flags = synchronize_flags;