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;
186 notmuch_config_t *config = talloc (ctx, notmuch_config_t);
187 if (config == NULL) {
188 fprintf (stderr, "Out of memory.\n");
192 talloc_set_destructor (config, notmuch_config_destructor);
195 config->filename = talloc_strdup (config, filename);
196 } else if ((notmuch_config_env = getenv ("NOTMUCH_CONFIG"))) {
197 config->filename = talloc_strdup (config, notmuch_config_env);
199 config->filename = talloc_asprintf (config, "%s/.notmuch-config",
203 config->key_file = g_key_file_new ();
205 config->database_path = NULL;
206 config->user_name = NULL;
207 config->user_primary_email = NULL;
208 config->user_other_email = NULL;
209 config->user_other_email_length = 0;
210 config->new_tags = NULL;
211 config->new_tags_length = 0;
213 if (! g_key_file_load_from_file (config->key_file,
215 G_KEY_FILE_KEEP_COMMENTS,
218 /* We are capable of dealing with a non-existent configuration
219 * file, so be silent about that (unless the user had set a
220 * non-default configuration file with the NOTMUCH_CONFIG
223 if (notmuch_config_env ||
224 !(error->domain == G_FILE_ERROR &&
225 error->code == G_FILE_ERROR_NOENT))
227 fprintf (stderr, "Error reading configuration file %s: %s\n",
228 config->filename, error->message);
229 talloc_free (config);
230 g_error_free (error);
234 g_error_free (error);
238 if (notmuch_config_get_database_path (config) == NULL) {
239 char *path = talloc_asprintf (config, "%s/mail",
241 notmuch_config_set_database_path (config, path);
245 if (notmuch_config_get_user_name (config) == NULL) {
246 char *name = get_name_from_passwd_file (config);
247 notmuch_config_set_user_name (config, name);
251 if (notmuch_config_get_user_primary_email (config) == NULL) {
252 char *email = getenv ("EMAIL");
254 notmuch_config_set_user_primary_email (config, email);
257 struct hostent *hostent;
258 const char *domainname;
260 char *username = get_username_from_passwd_file (config);
262 gethostname (hostname, 256);
263 hostname[255] = '\0';
265 hostent = gethostbyname (hostname);
266 if (hostent && (domainname = strchr (hostent->h_name, '.')))
269 domainname = "(none)";
271 email = talloc_asprintf (config, "%s@%s.%s",
272 username, hostname, domainname);
274 notmuch_config_set_user_primary_email (config, email);
276 talloc_free (username);
281 if (notmuch_config_get_new_tags (config, &tmp) == NULL) {
282 const char *tags[] = { "unread", "inbox" };
283 notmuch_config_set_new_tags (config, tags, 2);
286 /* When we create a new configuration file here, we add some
287 * comments to help the user understand what can be done. */
289 g_key_file_set_comment (config->key_file, NULL, NULL,
290 toplevel_config_comment, NULL);
291 g_key_file_set_comment (config->key_file, "database", NULL,
292 database_config_comment, NULL);
293 g_key_file_set_comment (config->key_file, "messages", NULL,
294 messages_config_comment, NULL);
295 g_key_file_set_comment (config->key_file, "user", NULL,
296 user_config_comment, NULL);
300 *is_new_ret = is_new;
305 /* Close the given notmuch_config_t object, freeing all resources.
307 * Note: Any changes made to the configuration are *not* saved by this
308 * function. To save changes, call notmuch_config_save before
309 * notmuch_config_close.
312 notmuch_config_close (notmuch_config_t *config)
314 talloc_free (config);
317 /* Save any changes made to the notmuch configuration.
319 * Any comments originally in the file will be preserved.
321 * Returns 0 if successful, and 1 in case of any error, (after
322 * printing a description of the error to stderr).
325 notmuch_config_save (notmuch_config_t *config)
329 GError *error = NULL;
331 data = g_key_file_to_data (config->key_file, &length, NULL);
333 fprintf (stderr, "Out of memory.\n");
337 if (! g_file_set_contents (config->filename, data, length, &error)) {
338 fprintf (stderr, "Error saving configuration to %s: %s\n",
339 config->filename, error->message);
340 g_error_free (error);
350 notmuch_config_get_database_path (notmuch_config_t *config)
354 if (config->database_path == NULL) {
355 path = g_key_file_get_string (config->key_file,
356 "database", "path", NULL);
358 config->database_path = talloc_strdup (config, path);
363 return config->database_path;
367 notmuch_config_set_database_path (notmuch_config_t *config,
368 const char *database_path)
370 g_key_file_set_string (config->key_file,
371 "database", "path", database_path);
373 talloc_free (config->database_path);
374 config->database_path = NULL;
378 notmuch_config_get_user_name (notmuch_config_t *config)
382 if (config->user_name == NULL) {
383 name = g_key_file_get_string (config->key_file,
384 "user", "name", NULL);
386 config->user_name = talloc_strdup (config, name);
391 return config->user_name;
395 notmuch_config_set_user_name (notmuch_config_t *config,
396 const char *user_name)
398 g_key_file_set_string (config->key_file,
399 "user", "name", user_name);
401 talloc_free (config->user_name);
402 config->user_name = NULL;
406 notmuch_config_get_user_primary_email (notmuch_config_t *config)
410 if (config->user_primary_email == NULL) {
411 email = g_key_file_get_string (config->key_file,
412 "user", "primary_email", NULL);
414 config->user_primary_email = talloc_strdup (config, email);
419 return config->user_primary_email;
423 notmuch_config_set_user_primary_email (notmuch_config_t *config,
424 const char *primary_email)
426 g_key_file_set_string (config->key_file,
427 "user", "primary_email", primary_email);
429 talloc_free (config->user_primary_email);
430 config->user_primary_email = NULL;
434 notmuch_config_get_user_other_email (notmuch_config_t *config,
438 size_t emails_length;
441 if (config->user_other_email == NULL) {
442 emails = g_key_file_get_string_list (config->key_file,
443 "user", "other_email",
444 &emails_length, NULL);
446 config->user_other_email = talloc_size (config,
448 (emails_length + 1));
449 for (i = 0; i < emails_length; i++)
450 config->user_other_email[i] = talloc_strdup (config->user_other_email,
452 config->user_other_email[i] = NULL;
456 config->user_other_email_length = emails_length;
460 *length = config->user_other_email_length;
461 return config->user_other_email;
465 notmuch_config_set_user_other_email (notmuch_config_t *config,
466 const char *other_email[],
469 g_key_file_set_string_list (config->key_file,
470 "user", "other_email",
471 other_email, length);
473 talloc_free (config->user_other_email);
474 config->user_other_email = NULL;
478 notmuch_config_get_new_tags (notmuch_config_t *config,
485 if (config->new_tags == NULL) {
486 tags = g_key_file_get_string_list (config->key_file,
487 "messages", "new_tags",
490 config->new_tags = talloc_size (config,
493 for (i = 0; i < tags_length; i++)
494 config->new_tags[i] = talloc_strdup (config->new_tags,
496 config->new_tags[i] = NULL;
500 config->new_tags_length = tags_length;
504 *length = config->new_tags_length;
505 return config->new_tags;
509 notmuch_config_set_new_tags (notmuch_config_t *config,
510 const char *new_tags[],
513 g_key_file_set_string_list (config->key_file,
514 "messages", "new_tags",
517 talloc_free (config->new_tags);
518 config->new_tags = NULL;