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\n"
53 "\t names will be ignored, independent of its depth/location\n"
54 "\t in the mail store.\n";
56 static const char user_config_comment[] =
57 " User configuration\n"
59 " Here is where you can let notmuch know how you would like to be\n"
60 " addressed. Valid settings are\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"
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";
72 static const char maildir_config_comment[] =
73 " Maildir compatibility configuration\n"
75 " The following option is supported here:\n"
77 "\tsynchronize_flags Valid values are true and false.\n"
79 "\tIf true, then the following maildir flags (in message filenames)\n"
80 "\twill be synchronized with the corresponding notmuch tags:\n"
88 "\t\tS unread (added when 'S' flag is not present)\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";
94 static const char search_config_comment[] =
95 " Search configuration\n"
97 " The following option is supported here:\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";
104 struct _notmuch_config {
107 notmuch_bool_t is_new;
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;
124 notmuch_config_destructor (notmuch_config_t *config)
126 if (config->key_file)
127 g_key_file_free (config->key_file);
133 get_name_from_passwd_file (void *ctx)
137 struct passwd passwd, *ignored;
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);
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);
152 char *comma = strchr (passwd.pw_gecos, ',');
154 name = talloc_strndup (ctx, passwd.pw_gecos,
155 comma - passwd.pw_gecos);
157 name = talloc_strdup (ctx, passwd.pw_gecos);
159 name = talloc_strdup (ctx, "");
162 talloc_free (pw_buf);
168 get_username_from_passwd_file (void *ctx)
172 struct passwd passwd, *ignored;
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);
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);
187 name = talloc_strdup (ctx, passwd.pw_name);
189 name = talloc_strdup (ctx, "");
191 talloc_free (pw_buf);
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.
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
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'
210 * If is_new_ret is NULL, then a "file not found" message will be
211 * printed to stderr and NULL will be returned.
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.
217 * These default configuration settings are determined as
220 * database_path: $MAILDIR, otherwise $HOME/mail
222 * user_name: $NAME variable if set, otherwise
223 * read from /etc/passwd
225 * user_primary_mail: $EMAIL variable if set, otherwise
226 * constructed from the username and
227 * hostname of the current machine.
229 * user_other_email: Not set.
231 * The default configuration also contains comments to guide the
232 * user in editing the file directly.
235 notmuch_config_open (void *ctx,
236 const char *filename,
237 notmuch_bool_t create_new)
239 GError *error = NULL;
241 char *notmuch_config_env = NULL;
242 int file_had_database_group;
243 int file_had_new_group;
244 int file_had_user_group;
245 int file_had_maildir_group;
246 int file_had_search_group;
248 notmuch_config_t *config = talloc (ctx, notmuch_config_t);
249 if (config == NULL) {
250 fprintf (stderr, "Out of memory.\n");
254 talloc_set_destructor (config, notmuch_config_destructor);
257 config->filename = talloc_strdup (config, filename);
258 } else if ((notmuch_config_env = getenv ("NOTMUCH_CONFIG"))) {
259 config->filename = talloc_strdup (config, notmuch_config_env);
261 config->filename = talloc_asprintf (config, "%s/.notmuch-config",
265 config->key_file = g_key_file_new ();
267 config->is_new = FALSE;
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 create_new is true, then the caller is prepared for a
287 * default configuration file in the case of FILE NOT
288 * FOUND. Otherwise, any read failure is an error.
291 error->domain == G_FILE_ERROR &&
292 error->code == G_FILE_ERROR_NOENT)
294 g_error_free (error);
295 config->is_new = TRUE;
299 fprintf (stderr, "Error reading configuration file %s: %s\n",
300 config->filename, error->message);
301 talloc_free (config);
302 g_error_free (error);
307 /* Whenever we know of configuration sections that don't appear in
308 * the configuration file, we add some comments to help the user
309 * understand what can be done.
311 * It would be convenient to just add those comments now, but
312 * apparently g_key_file will clear any comments when keys are
313 * added later that create the groups. So we have to check for the
314 * groups now, but add the comments only after setting all of our
317 file_had_database_group = g_key_file_has_group (config->key_file,
319 file_had_new_group = g_key_file_has_group (config->key_file, "new");
320 file_had_user_group = g_key_file_has_group (config->key_file, "user");
321 file_had_maildir_group = g_key_file_has_group (config->key_file, "maildir");
322 file_had_search_group = g_key_file_has_group (config->key_file, "search");
325 if (notmuch_config_get_database_path (config) == NULL) {
326 char *path = getenv ("MAILDIR");
328 path = talloc_asprintf (config, "%s/mail",
330 notmuch_config_set_database_path (config, path);
334 if (notmuch_config_get_user_name (config) == NULL) {
335 char *name = getenv ("NAME");
337 name = get_name_from_passwd_file (config);
338 notmuch_config_set_user_name (config, name);
342 if (notmuch_config_get_user_primary_email (config) == NULL) {
343 char *email = getenv ("EMAIL");
345 notmuch_config_set_user_primary_email (config, email);
348 struct hostent *hostent;
349 const char *domainname;
351 char *username = get_username_from_passwd_file (config);
353 gethostname (hostname, 256);
354 hostname[255] = '\0';
356 hostent = gethostbyname (hostname);
357 if (hostent && (domainname = strchr (hostent->h_name, '.')))
360 domainname = "(none)";
362 email = talloc_asprintf (config, "%s@%s.%s",
363 username, hostname, domainname);
365 notmuch_config_set_user_primary_email (config, email);
367 talloc_free (username);
372 if (notmuch_config_get_new_tags (config, &tmp) == NULL) {
373 const char *tags[] = { "unread", "inbox" };
374 notmuch_config_set_new_tags (config, tags, 2);
377 if (notmuch_config_get_new_ignore (config, &tmp) == NULL) {
378 notmuch_config_set_new_ignore (config, NULL, 0);
381 if (notmuch_config_get_search_exclude_tags (config, &tmp) == NULL) {
382 if (config->is_new) {
383 const char *tags[] = { "deleted", "spam" };
384 notmuch_config_set_search_exclude_tags (config, tags, 2);
386 notmuch_config_set_search_exclude_tags (config, NULL, 0);
391 config->maildir_synchronize_flags =
392 g_key_file_get_boolean (config->key_file,
393 "maildir", "synchronize_flags", &error);
395 notmuch_config_set_maildir_synchronize_flags (config, TRUE);
396 g_error_free (error);
399 /* Whenever we know of configuration sections that don't appear in
400 * the configuration file, we add some comments to help the user
401 * understand what can be done. */
403 g_key_file_set_comment (config->key_file, NULL, NULL,
404 toplevel_config_comment, NULL);
406 if (! file_had_database_group)
407 g_key_file_set_comment (config->key_file, "database", NULL,
408 database_config_comment, NULL);
410 if (! file_had_new_group)
411 g_key_file_set_comment (config->key_file, "new", NULL,
412 new_config_comment, NULL);
414 if (! file_had_user_group)
415 g_key_file_set_comment (config->key_file, "user", NULL,
416 user_config_comment, NULL);
418 if (! file_had_maildir_group)
419 g_key_file_set_comment (config->key_file, "maildir", NULL,
420 maildir_config_comment, NULL);
422 if (! file_had_search_group)
423 g_key_file_set_comment (config->key_file, "search", NULL,
424 search_config_comment, NULL);
429 /* Close the given notmuch_config_t object, freeing all resources.
431 * Note: Any changes made to the configuration are *not* saved by this
432 * function. To save changes, call notmuch_config_save before
433 * notmuch_config_close.
436 notmuch_config_close (notmuch_config_t *config)
438 talloc_free (config);
441 /* Save any changes made to the notmuch configuration.
443 * Any comments originally in the file will be preserved.
445 * Returns 0 if successful, and 1 in case of any error, (after
446 * printing a description of the error to stderr).
449 notmuch_config_save (notmuch_config_t *config)
452 char *data, *filename;
453 GError *error = NULL;
455 data = g_key_file_to_data (config->key_file, &length, NULL);
457 fprintf (stderr, "Out of memory.\n");
461 /* Try not to overwrite symlinks. */
462 filename = canonicalize_file_name (config->filename);
464 if (errno == ENOENT) {
465 filename = strdup (config->filename);
467 fprintf (stderr, "Out of memory.\n");
472 fprintf (stderr, "Error canonicalizing %s: %s\n", config->filename,
479 if (! g_file_set_contents (filename, data, length, &error)) {
480 if (strcmp (filename, config->filename) != 0) {
481 fprintf (stderr, "Error saving configuration to %s (-> %s): %s\n",
482 config->filename, filename, error->message);
484 fprintf (stderr, "Error saving configuration to %s: %s\n",
485 filename, error->message);
487 g_error_free (error);
499 notmuch_config_is_new (notmuch_config_t *config)
501 return config->is_new;
505 _config_get (notmuch_config_t *config, char **field,
506 const char *group, const char *key)
508 /* read from config file and cache value, if not cached already */
509 if (*field == NULL) {
511 value = g_key_file_get_string (config->key_file, group, key, NULL);
513 *field = talloc_strdup (config, value);
521 _config_set (notmuch_config_t *config, char **field,
522 const char *group, const char *key, const char *value)
524 g_key_file_set_string (config->key_file, group, key, value);
526 /* drop the cached value */
527 talloc_free (*field);
532 _config_get_list (notmuch_config_t *config,
533 const char *section, const char *key,
534 const char ***outlist, size_t *list_length, size_t *ret_length)
538 /* read from config file and cache value, if not cached already */
539 if (*outlist == NULL) {
541 char **inlist = g_key_file_get_string_list (config->key_file,
542 section, key, list_length, NULL);
546 *outlist = talloc_size (config, sizeof (char *) * (*list_length + 1));
548 for (i = 0; i < *list_length; i++)
549 (*outlist)[i] = talloc_strdup (*outlist, inlist[i]);
551 (*outlist)[i] = NULL;
558 *ret_length = *list_length;
564 _config_set_list (notmuch_config_t *config,
565 const char *group, const char *name,
567 size_t length, const char ***config_var )
569 g_key_file_set_string_list (config->key_file, group, name, list, length);
571 /* drop the cached value */
572 talloc_free (*config_var);
577 notmuch_config_get_database_path (notmuch_config_t *config)
579 return _config_get (config, &config->database_path, "database", "path");
583 notmuch_config_set_database_path (notmuch_config_t *config,
584 const char *database_path)
586 _config_set (config, &config->database_path, "database", "path", database_path);
590 notmuch_config_get_user_name (notmuch_config_t *config)
592 return _config_get (config, &config->user_name, "user", "name");
596 notmuch_config_set_user_name (notmuch_config_t *config,
597 const char *user_name)
599 _config_set (config, &config->user_name, "user", "name", user_name);
603 notmuch_config_get_user_primary_email (notmuch_config_t *config)
605 return _config_get (config, &config->user_primary_email, "user", "primary_email");
609 notmuch_config_set_user_primary_email (notmuch_config_t *config,
610 const char *primary_email)
612 _config_set (config, &config->user_primary_email, "user", "primary_email", primary_email);
616 notmuch_config_get_user_other_email (notmuch_config_t *config, size_t *length)
618 return _config_get_list (config, "user", "other_email",
619 &(config->user_other_email),
620 &(config->user_other_email_length), length);
624 notmuch_config_get_new_tags (notmuch_config_t *config, size_t *length)
626 return _config_get_list (config, "new", "tags",
628 &(config->new_tags_length), length);
632 notmuch_config_get_new_ignore (notmuch_config_t *config, size_t *length)
634 return _config_get_list (config, "new", "ignore",
635 &(config->new_ignore),
636 &(config->new_ignore_length), length);
640 notmuch_config_set_user_other_email (notmuch_config_t *config,
644 _config_set_list (config, "user", "other_email", list, length,
645 &(config->user_other_email));
649 notmuch_config_set_new_tags (notmuch_config_t *config,
653 _config_set_list (config, "new", "tags", list, length,
654 &(config->new_tags));
658 notmuch_config_set_new_ignore (notmuch_config_t *config,
662 _config_set_list (config, "new", "ignore", list, length,
663 &(config->new_ignore));
667 notmuch_config_get_search_exclude_tags (notmuch_config_t *config, size_t *length)
669 return _config_get_list (config, "search", "exclude_tags",
670 &(config->search_exclude_tags),
671 &(config->search_exclude_tags_length), length);
675 notmuch_config_set_search_exclude_tags (notmuch_config_t *config,
679 _config_set_list (config, "search", "exclude_tags", list, length,
680 &(config->search_exclude_tags));
683 /* Given a configuration item of the form <group>.<key> return the
684 * component group and key. If any error occurs, print a message on
685 * stderr and return 1. Otherwise, return 0.
687 * Note: This function modifies the original 'item' string.
690 _item_split (char *item, char **group, char **key)
696 period = strchr (item, '.');
697 if (period == NULL || *(period+1) == '\0') {
699 "Invalid configuration name: %s\n"
700 "(Should be of the form <section>.<item>)\n", item);
711 notmuch_config_command_get (notmuch_config_t *config, char *item)
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]);
760 notmuch_config_command_set (notmuch_config_t *config, char *item, int argc, char *argv[])
764 if (_item_split (item, &group, &key))
767 /* With only the name of an item, we clear it from the
768 * configuration file.
770 * With a single value, we set it as a string.
772 * With multiple values, we set them as a string list.
776 g_key_file_remove_key (config->key_file, group, key, NULL);
779 g_key_file_set_string (config->key_file, group, key, argv[0]);
782 g_key_file_set_string_list (config->key_file, group, key,
783 (const gchar **) argv, argc);
787 return notmuch_config_save (config);
791 notmuch_config_command_list (notmuch_config_t *config)
794 size_t g, groups_length;
796 groups = g_key_file_get_groups (config->key_file, &groups_length);
800 for (g = 0; g < groups_length; g++) {
802 size_t k, keys_length;
804 keys = g_key_file_get_keys (config->key_file,
805 groups[g], &keys_length, NULL);
809 for (k = 0; k < keys_length; k++) {
812 value = g_key_file_get_string (config->key_file,
813 groups[g], keys[k], NULL);
815 printf ("%s.%s=%s\n", groups[g], keys[k], value);
829 notmuch_config_command (notmuch_config_t *config, int argc, char *argv[])
833 argc--; argv++; /* skip subcommand argument */
836 fprintf (stderr, "Error: notmuch config requires at least one argument.\n");
840 if (strcmp (argv[0], "get") == 0) {
842 fprintf (stderr, "Error: notmuch config get requires exactly "
846 ret = notmuch_config_command_get (config, argv[1]);
847 } else if (strcmp (argv[0], "set") == 0) {
849 fprintf (stderr, "Error: notmuch config set requires at least "
853 ret = notmuch_config_command_set (config, argv[1], argc - 2, argv + 2);
854 } else if (strcmp (argv[0], "list") == 0) {
855 ret = notmuch_config_command_list (config);
857 fprintf (stderr, "Unrecognized argument for notmuch config: %s\n",
862 return ret ? EXIT_FAILURE : EXIT_SUCCESS;
867 notmuch_config_get_maildir_synchronize_flags (notmuch_config_t *config)
869 return config->maildir_synchronize_flags;
873 notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,
874 notmuch_bool_t synchronize_flags)
876 g_key_file_set_boolean (config->key_file,
877 "maildir", "synchronize_flags", synchronize_flags);
878 config->maildir_synchronize_flags = synchronize_flags;