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 only value supported here is 'new_tags' which lists the tags that\n"
44 " should be applied to new messages.\n";
46 static const char user_config_comment[] =
47 " User configuration\n"
49 " Here is where you can let notmuch know how you would like to be\n"
50 " addressed. Valid settings are\n"
52 "\tname Your full name.\n"
53 "\tprimary_email Your primary email address.\n"
54 "\tother_email A list (separated by ';') of other email addresses\n"
55 "\t at which you receive email.\n"
57 " Notmuch will use the various email addresses configured here when\n"
58 " formatting replies. It will avoid including your own addresses in the\n"
59 " recipient list of replies, and will set the From address based on the\n"
60 " address to which the original email was addressed.\n";
62 struct _notmuch_config {
68 char *user_primary_email;
69 char **user_other_email;
70 size_t user_other_email_length;
71 const char **new_tags;
72 size_t new_tags_length;
76 notmuch_config_destructor (notmuch_config_t *config)
79 g_key_file_free (config->key_file);
85 get_name_from_passwd_file (void *ctx)
87 long pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
88 char *pw_buf = talloc_zero_size (ctx, pw_buf_size);
89 struct passwd passwd, *ignored;
93 if (pw_buf_size == -1) pw_buf_size = 64;
95 while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
96 pw_buf_size, &ignored)) == ERANGE) {
97 pw_buf_size = pw_buf_size * 2;
98 pw_buf = talloc_zero_size(ctx, pw_buf_size);
102 char *comma = strchr (passwd.pw_gecos, ',');
104 name = talloc_strndup (ctx, passwd.pw_gecos,
105 comma - passwd.pw_gecos);
107 name = talloc_strdup (ctx, passwd.pw_gecos);
109 name = talloc_strdup (ctx, "");
112 talloc_free (pw_buf);
118 get_username_from_passwd_file (void *ctx)
120 long pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
121 char *pw_buf = talloc_zero_size (ctx, pw_buf_size);
122 struct passwd passwd, *ignored;
126 if (pw_buf_size == -1) pw_buf_size = 64;
127 while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
128 pw_buf_size, &ignored)) == ERANGE) {
129 pw_buf_size = pw_buf_size * 2;
130 pw_buf = talloc_zero_size(ctx, pw_buf_size);
134 name = talloc_strdup (ctx, passwd.pw_name);
136 name = talloc_strdup (ctx, "");
138 talloc_free (pw_buf);
143 /* Open the named notmuch configuration file. If the filename is NULL,
144 * the value of the environment variable $NOTMUCH_CONFIG will be used.
145 * If $NOTMUCH_CONFIG is unset, the default configuration file
146 * ($HOME/.notmuch-config) will be used.
148 * If any error occurs, (out of memory, or a permission-denied error,
149 * etc.), this function will print a message to stderr and return
152 * Note: It is *not* an error if the specified configuration file does
153 * not exist. In this case, a default configuration will be created
154 * and returned. Subsequently calling notmuch_config_save will cause
155 * the configuration to be written to the filename specified at the
156 * time of notmuch_config_open.
158 * The default configuration settings are determined as follows:
160 * database_path: $HOME/mail
162 * user_name: From /etc/passwd
164 * user_primary_mail: $EMAIL variable if set, otherwise
165 * constructed from the username and
166 * hostname of the current machine.
168 * user_other_email: Not set.
170 * The default configuration also contains comments to guide the user
171 * in editing the file directly.
174 notmuch_config_open (void *ctx,
175 const char *filename,
176 notmuch_bool_t *is_new_ret)
178 GError *error = NULL;
181 char *notmuch_config_env = NULL;
182 int file_had_database_group;
183 int file_had_messages_group;
184 int file_had_user_group;
189 notmuch_config_t *config = talloc (ctx, notmuch_config_t);
190 if (config == NULL) {
191 fprintf (stderr, "Out of memory.\n");
195 talloc_set_destructor (config, notmuch_config_destructor);
198 config->filename = talloc_strdup (config, filename);
199 } else if ((notmuch_config_env = getenv ("NOTMUCH_CONFIG"))) {
200 config->filename = talloc_strdup (config, notmuch_config_env);
202 config->filename = talloc_asprintf (config, "%s/.notmuch-config",
206 config->key_file = g_key_file_new ();
208 config->database_path = NULL;
209 config->user_name = NULL;
210 config->user_primary_email = NULL;
211 config->user_other_email = NULL;
212 config->user_other_email_length = 0;
213 config->new_tags = NULL;
214 config->new_tags_length = 0;
216 if (! g_key_file_load_from_file (config->key_file,
218 G_KEY_FILE_KEEP_COMMENTS,
221 /* We are capable of dealing with a non-existent configuration
222 * file, so be silent about that (unless the user had set a
223 * non-default configuration file with the NOTMUCH_CONFIG
226 if (notmuch_config_env ||
227 !(error->domain == G_FILE_ERROR &&
228 error->code == G_FILE_ERROR_NOENT))
230 fprintf (stderr, "Error reading configuration file %s: %s\n",
231 config->filename, error->message);
232 talloc_free (config);
233 g_error_free (error);
237 g_error_free (error);
241 /* Whenever we know of configuration sections that don't appear in
242 * the configuration file, we add some comments to help the user
243 * understand what can be done.
245 * It would be convenient to just add those comments now, but
246 * apparently g_key_file will clear any comments when keys are
247 * added later that create the groups. So we have to check for the
248 * groups now, but add the comments only after setting all of our
251 file_had_database_group = g_key_file_has_group (config->key_file,
253 file_had_messages_group = g_key_file_has_group (config->key_file,
255 file_had_user_group = g_key_file_has_group (config->key_file, "user");
258 if (notmuch_config_get_database_path (config) == NULL) {
259 char *path = talloc_asprintf (config, "%s/mail",
261 notmuch_config_set_database_path (config, path);
265 if (notmuch_config_get_user_name (config) == NULL) {
266 char *name = get_name_from_passwd_file (config);
267 notmuch_config_set_user_name (config, name);
271 if (notmuch_config_get_user_primary_email (config) == NULL) {
272 char *email = getenv ("EMAIL");
274 notmuch_config_set_user_primary_email (config, email);
277 struct hostent *hostent;
278 const char *domainname;
280 char *username = get_username_from_passwd_file (config);
282 gethostname (hostname, 256);
283 hostname[255] = '\0';
285 hostent = gethostbyname (hostname);
286 if (hostent && (domainname = strchr (hostent->h_name, '.')))
289 domainname = "(none)";
291 email = talloc_asprintf (config, "%s@%s.%s",
292 username, hostname, domainname);
294 notmuch_config_set_user_primary_email (config, email);
296 talloc_free (username);
301 if (notmuch_config_get_new_tags (config, &tmp) == NULL) {
302 const char *tags[] = { "unread", "inbox" };
303 notmuch_config_set_new_tags (config, tags, 2);
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. */
311 g_key_file_set_comment (config->key_file, NULL, NULL,
312 toplevel_config_comment, NULL);
315 if (! file_had_database_group)
317 g_key_file_set_comment (config->key_file, "database", NULL,
318 database_config_comment, NULL);
321 if (! file_had_messages_group)
323 g_key_file_set_comment (config->key_file, "messages", NULL,
324 messages_config_comment, NULL);
327 if (! file_had_user_group)
329 g_key_file_set_comment (config->key_file, "user", NULL,
330 user_config_comment, NULL);
334 *is_new_ret = is_new;
339 /* Close the given notmuch_config_t object, freeing all resources.
341 * Note: Any changes made to the configuration are *not* saved by this
342 * function. To save changes, call notmuch_config_save before
343 * notmuch_config_close.
346 notmuch_config_close (notmuch_config_t *config)
348 talloc_free (config);
351 /* Save any changes made to the notmuch configuration.
353 * Any comments originally in the file will be preserved.
355 * Returns 0 if successful, and 1 in case of any error, (after
356 * printing a description of the error to stderr).
359 notmuch_config_save (notmuch_config_t *config)
363 GError *error = NULL;
365 data = g_key_file_to_data (config->key_file, &length, NULL);
367 fprintf (stderr, "Out of memory.\n");
371 if (! g_file_set_contents (config->filename, data, length, &error)) {
372 fprintf (stderr, "Error saving configuration to %s: %s\n",
373 config->filename, error->message);
374 g_error_free (error);
384 notmuch_config_get_database_path (notmuch_config_t *config)
388 if (config->database_path == NULL) {
389 path = g_key_file_get_string (config->key_file,
390 "database", "path", NULL);
392 config->database_path = talloc_strdup (config, path);
397 return config->database_path;
401 notmuch_config_set_database_path (notmuch_config_t *config,
402 const char *database_path)
404 g_key_file_set_string (config->key_file,
405 "database", "path", database_path);
407 talloc_free (config->database_path);
408 config->database_path = NULL;
412 notmuch_config_get_user_name (notmuch_config_t *config)
416 if (config->user_name == NULL) {
417 name = g_key_file_get_string (config->key_file,
418 "user", "name", NULL);
420 config->user_name = talloc_strdup (config, name);
425 return config->user_name;
429 notmuch_config_set_user_name (notmuch_config_t *config,
430 const char *user_name)
432 g_key_file_set_string (config->key_file,
433 "user", "name", user_name);
435 talloc_free (config->user_name);
436 config->user_name = NULL;
440 notmuch_config_get_user_primary_email (notmuch_config_t *config)
444 if (config->user_primary_email == NULL) {
445 email = g_key_file_get_string (config->key_file,
446 "user", "primary_email", NULL);
448 config->user_primary_email = talloc_strdup (config, email);
453 return config->user_primary_email;
457 notmuch_config_set_user_primary_email (notmuch_config_t *config,
458 const char *primary_email)
460 g_key_file_set_string (config->key_file,
461 "user", "primary_email", primary_email);
463 talloc_free (config->user_primary_email);
464 config->user_primary_email = NULL;
468 notmuch_config_get_user_other_email (notmuch_config_t *config,
472 size_t emails_length;
475 if (config->user_other_email == NULL) {
476 emails = g_key_file_get_string_list (config->key_file,
477 "user", "other_email",
478 &emails_length, NULL);
480 config->user_other_email = talloc_size (config,
482 (emails_length + 1));
483 for (i = 0; i < emails_length; i++)
484 config->user_other_email[i] = talloc_strdup (config->user_other_email,
486 config->user_other_email[i] = NULL;
490 config->user_other_email_length = emails_length;
494 *length = config->user_other_email_length;
495 return config->user_other_email;
499 notmuch_config_set_user_other_email (notmuch_config_t *config,
500 const char *other_email[],
503 g_key_file_set_string_list (config->key_file,
504 "user", "other_email",
505 other_email, length);
507 talloc_free (config->user_other_email);
508 config->user_other_email = NULL;
512 notmuch_config_get_new_tags (notmuch_config_t *config,
519 if (config->new_tags == NULL) {
520 tags = g_key_file_get_string_list (config->key_file,
521 "messages", "new_tags",
524 config->new_tags = talloc_size (config,
527 for (i = 0; i < tags_length; i++)
528 config->new_tags[i] = talloc_strdup (config->new_tags,
530 config->new_tags[i] = NULL;
534 config->new_tags_length = tags_length;
538 *length = config->new_tags_length;
539 return config->new_tags;
543 notmuch_config_set_new_tags (notmuch_config_t *config,
544 const char *new_tags[],
547 g_key_file_set_string_list (config->key_file,
548 "messages", "new_tags",
551 talloc_free (config->new_tags);
552 config->new_tags = NULL;