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"
26 static const char toplevel_config_comment[] =
27 " .notmuch-config - Configuration file for the notmuch mail system\n"
29 " For more information about notmuch, see http://notmuchmail.org";
31 static const char database_config_comment[] =
32 " Database configuration\n"
34 " The only value supported here is 'path' which should be the top-level\n"
35 " directory where your mail currently exists and to where mail will be\n"
36 " delivered in the future. Files should be individual email messages.\n"
37 " Notmuch will store its database within a sub-directory of the path\n"
38 " configured here named \".notmuch\".\n";
40 static const char messages_config_comment[] =
41 " Messages configuration\n"
43 " The following options are supported here:\n"
45 "\tnew_tags A list (separated by ';') of the tags that will be\n"
46 "\t added to all messages incorporated by \"notmuch new\".\n";
48 static const char user_config_comment[] =
49 " User configuration\n"
51 " Here is where you can let notmuch know how you would like to be\n"
52 " addressed. Valid settings are\n"
54 "\tname Your full name.\n"
55 "\tprimary_email Your primary email address.\n"
56 "\tother_email A list (separated by ';') of other email addresses\n"
57 "\t at which you receive email.\n"
59 " Notmuch will use the various email addresses configured here when\n"
60 " formatting replies. It will avoid including your own addresses in the\n"
61 " recipient list of replies, and will set the From address based on the\n"
62 " address to which the original email was addressed.\n";
64 struct _notmuch_config {
70 char *user_primary_email;
71 char **user_other_email;
72 size_t user_other_email_length;
73 const char **new_tags;
74 size_t new_tags_length;
78 notmuch_config_destructor (notmuch_config_t *config)
81 g_key_file_free (config->key_file);
87 get_name_from_passwd_file (void *ctx)
89 long pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
90 char *pw_buf = talloc_zero_size (ctx, pw_buf_size);
91 struct passwd passwd, *ignored;
95 if (pw_buf_size == -1) pw_buf_size = 64;
97 while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
98 pw_buf_size, &ignored)) == ERANGE) {
99 pw_buf_size = pw_buf_size * 2;
100 pw_buf = talloc_zero_size(ctx, pw_buf_size);
104 char *comma = strchr (passwd.pw_gecos, ',');
106 name = talloc_strndup (ctx, passwd.pw_gecos,
107 comma - passwd.pw_gecos);
109 name = talloc_strdup (ctx, passwd.pw_gecos);
111 name = talloc_strdup (ctx, "");
114 talloc_free (pw_buf);
120 get_username_from_passwd_file (void *ctx)
122 long pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
123 char *pw_buf = talloc_zero_size (ctx, pw_buf_size);
124 struct passwd passwd, *ignored;
128 if (pw_buf_size == -1) pw_buf_size = 64;
129 while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
130 pw_buf_size, &ignored)) == ERANGE) {
131 pw_buf_size = pw_buf_size * 2;
132 pw_buf = talloc_zero_size(ctx, pw_buf_size);
136 name = talloc_strdup (ctx, passwd.pw_name);
138 name = talloc_strdup (ctx, "");
140 talloc_free (pw_buf);
145 /* Open the named notmuch configuration file. If the filename is NULL,
146 * the value of the environment variable $NOTMUCH_CONFIG will be used.
147 * If $NOTMUCH_CONFIG is unset, the default configuration file
148 * ($HOME/.notmuch-config) will be used.
150 * If any error occurs, (out of memory, or a permission-denied error,
151 * etc.), this function will print a message to stderr and return
154 * Note: It is *not* an error if the specified configuration file does
155 * not exist. In this case, a default configuration will be created
156 * and returned. Subsequently calling notmuch_config_save will cause
157 * the configuration to be written to the filename specified at the
158 * time of notmuch_config_open.
160 * The default configuration settings are determined as follows:
162 * database_path: $HOME/mail
164 * user_name: From /etc/passwd
166 * user_primary_mail: $EMAIL variable if set, otherwise
167 * constructed from the username and
168 * hostname of the current machine.
170 * user_other_email: Not set.
172 * The default configuration also contains comments to guide the user
173 * in editing the file directly.
176 notmuch_config_open (void *ctx,
177 const char *filename,
178 notmuch_bool_t *is_new_ret)
180 GError *error = NULL;
183 char *notmuch_config_env = NULL;
184 int file_had_database_group;
185 int file_had_messages_group;
186 int file_had_user_group;
191 notmuch_config_t *config = talloc (ctx, notmuch_config_t);
192 if (config == NULL) {
193 fprintf (stderr, "Out of memory.\n");
197 talloc_set_destructor (config, notmuch_config_destructor);
200 config->filename = talloc_strdup (config, filename);
201 } else if ((notmuch_config_env = getenv ("NOTMUCH_CONFIG"))) {
202 config->filename = talloc_strdup (config, notmuch_config_env);
204 config->filename = talloc_asprintf (config, "%s/.notmuch-config",
208 config->key_file = g_key_file_new ();
210 config->database_path = NULL;
211 config->user_name = NULL;
212 config->user_primary_email = NULL;
213 config->user_other_email = NULL;
214 config->user_other_email_length = 0;
215 config->new_tags = NULL;
216 config->new_tags_length = 0;
218 if (! g_key_file_load_from_file (config->key_file,
220 G_KEY_FILE_KEEP_COMMENTS,
223 /* We are capable of dealing with a non-existent configuration
224 * file, so be silent about that (unless the user had set a
225 * non-default configuration file with the NOTMUCH_CONFIG
228 if (notmuch_config_env ||
229 !(error->domain == G_FILE_ERROR &&
230 error->code == G_FILE_ERROR_NOENT))
232 fprintf (stderr, "Error reading configuration file %s: %s\n",
233 config->filename, error->message);
234 talloc_free (config);
235 g_error_free (error);
239 g_error_free (error);
243 /* Whenever we know of configuration sections that don't appear in
244 * the configuration file, we add some comments to help the user
245 * understand what can be done.
247 * It would be convenient to just add those comments now, but
248 * apparently g_key_file will clear any comments when keys are
249 * added later that create the groups. So we have to check for the
250 * groups now, but add the comments only after setting all of our
253 file_had_database_group = g_key_file_has_group (config->key_file,
255 file_had_messages_group = g_key_file_has_group (config->key_file,
257 file_had_user_group = g_key_file_has_group (config->key_file, "user");
260 if (notmuch_config_get_database_path (config) == NULL) {
261 char *path = talloc_asprintf (config, "%s/mail",
263 notmuch_config_set_database_path (config, path);
267 if (notmuch_config_get_user_name (config) == NULL) {
268 char *name = get_name_from_passwd_file (config);
269 notmuch_config_set_user_name (config, name);
273 if (notmuch_config_get_user_primary_email (config) == NULL) {
274 char *email = getenv ("EMAIL");
276 notmuch_config_set_user_primary_email (config, email);
279 struct hostent *hostent;
280 const char *domainname;
282 char *username = get_username_from_passwd_file (config);
284 gethostname (hostname, 256);
285 hostname[255] = '\0';
287 hostent = gethostbyname (hostname);
288 if (hostent && (domainname = strchr (hostent->h_name, '.')))
291 domainname = "(none)";
293 email = talloc_asprintf (config, "%s@%s.%s",
294 username, hostname, domainname);
296 notmuch_config_set_user_primary_email (config, email);
298 talloc_free (username);
303 if (notmuch_config_get_new_tags (config, &tmp) == NULL) {
304 const char *tags[] = { "unread", "inbox" };
305 notmuch_config_set_new_tags (config, tags, 2);
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. */
313 g_key_file_set_comment (config->key_file, NULL, NULL,
314 toplevel_config_comment, NULL);
317 if (! file_had_database_group)
319 g_key_file_set_comment (config->key_file, "database", NULL,
320 database_config_comment, NULL);
323 if (! file_had_messages_group)
325 g_key_file_set_comment (config->key_file, "messages", NULL,
326 messages_config_comment, NULL);
329 if (! file_had_user_group)
331 g_key_file_set_comment (config->key_file, "user", NULL,
332 user_config_comment, NULL);
336 *is_new_ret = is_new;
341 /* Close the given notmuch_config_t object, freeing all resources.
343 * Note: Any changes made to the configuration are *not* saved by this
344 * function. To save changes, call notmuch_config_save before
345 * notmuch_config_close.
348 notmuch_config_close (notmuch_config_t *config)
350 talloc_free (config);
353 /* Save any changes made to the notmuch configuration.
355 * Any comments originally in the file will be preserved.
357 * Returns 0 if successful, and 1 in case of any error, (after
358 * printing a description of the error to stderr).
361 notmuch_config_save (notmuch_config_t *config)
365 GError *error = NULL;
367 data = g_key_file_to_data (config->key_file, &length, NULL);
369 fprintf (stderr, "Out of memory.\n");
373 if (! g_file_set_contents (config->filename, data, length, &error)) {
374 fprintf (stderr, "Error saving configuration to %s: %s\n",
375 config->filename, error->message);
376 g_error_free (error);
386 notmuch_config_get_database_path (notmuch_config_t *config)
390 if (config->database_path == NULL) {
391 path = g_key_file_get_string (config->key_file,
392 "database", "path", NULL);
394 config->database_path = talloc_strdup (config, path);
399 return config->database_path;
403 notmuch_config_set_database_path (notmuch_config_t *config,
404 const char *database_path)
406 g_key_file_set_string (config->key_file,
407 "database", "path", database_path);
409 talloc_free (config->database_path);
410 config->database_path = NULL;
414 notmuch_config_get_user_name (notmuch_config_t *config)
418 if (config->user_name == NULL) {
419 name = g_key_file_get_string (config->key_file,
420 "user", "name", NULL);
422 config->user_name = talloc_strdup (config, name);
427 return config->user_name;
431 notmuch_config_set_user_name (notmuch_config_t *config,
432 const char *user_name)
434 g_key_file_set_string (config->key_file,
435 "user", "name", user_name);
437 talloc_free (config->user_name);
438 config->user_name = NULL;
442 notmuch_config_get_user_primary_email (notmuch_config_t *config)
446 if (config->user_primary_email == NULL) {
447 email = g_key_file_get_string (config->key_file,
448 "user", "primary_email", NULL);
450 config->user_primary_email = talloc_strdup (config, email);
455 return config->user_primary_email;
459 notmuch_config_set_user_primary_email (notmuch_config_t *config,
460 const char *primary_email)
462 g_key_file_set_string (config->key_file,
463 "user", "primary_email", primary_email);
465 talloc_free (config->user_primary_email);
466 config->user_primary_email = NULL;
470 notmuch_config_get_user_other_email (notmuch_config_t *config,
474 size_t emails_length;
477 if (config->user_other_email == NULL) {
478 emails = g_key_file_get_string_list (config->key_file,
479 "user", "other_email",
480 &emails_length, NULL);
482 config->user_other_email = talloc_size (config,
484 (emails_length + 1));
485 for (i = 0; i < emails_length; i++)
486 config->user_other_email[i] = talloc_strdup (config->user_other_email,
488 config->user_other_email[i] = NULL;
492 config->user_other_email_length = emails_length;
496 *length = config->user_other_email_length;
497 return config->user_other_email;
501 notmuch_config_set_user_other_email (notmuch_config_t *config,
502 const char *other_email[],
505 g_key_file_set_string_list (config->key_file,
506 "user", "other_email",
507 other_email, length);
509 talloc_free (config->user_other_email);
510 config->user_other_email = NULL;
514 notmuch_config_get_new_tags (notmuch_config_t *config,
521 if (config->new_tags == NULL) {
522 tags = g_key_file_get_string_list (config->key_file,
523 "messages", "new_tags",
526 config->new_tags = talloc_size (config,
529 for (i = 0; i < tags_length; i++)
530 config->new_tags[i] = talloc_strdup (config->new_tags,
532 config->new_tags[i] = NULL;
536 config->new_tags_length = tags_length;
540 *length = config->new_tags_length;
541 return config->new_tags;
545 notmuch_config_set_new_tags (notmuch_config_t *config,
546 const char *new_tags[],
549 g_key_file_set_string_list (config->key_file,
550 "messages", "new_tags",
553 talloc_free (config->new_tags);
554 config->new_tags = NULL;