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 user_config_comment[] =
41 " User configuration\n"
43 " Here is where you can let notmuch know how you would like to be\n"
44 " addressed. Valid settings are\n"
46 "\tname Your full name.\n"
47 "\tprimary_email Your primary email address.\n"
48 "\tother_email A list (separated by ';') of other email addresses\n"
49 "\t at which you receive email.\n"
51 " Notmuch will use the various email addresses configured here when\n"
52 " formatting replies. It will avoid including your own addresses in the\n"
53 " recipient list of replies, and will set the From address based on the\n"
54 " address to which the original email was addressed.\n";
56 struct _notmuch_config {
62 char *user_primary_email;
63 char **user_other_email;
64 size_t user_other_email_length;
68 notmuch_config_destructor (notmuch_config_t *config)
71 g_key_file_free (config->key_file);
77 get_name_from_passwd_file (void *ctx)
79 long pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
80 char *pw_buf = talloc_zero_size (ctx, pw_buf_size);
81 struct passwd passwd, *ignored;
85 if (pw_buf_size == -1) pw_buf_size = 64;
87 while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
88 pw_buf_size, &ignored)) == ERANGE) {
89 pw_buf_size = pw_buf_size * 2;
90 pw_buf = talloc_zero_size(ctx, pw_buf_size);
94 char *comma = strchr (passwd.pw_gecos, ',');
96 name = talloc_strndup (ctx, passwd.pw_gecos,
97 comma - passwd.pw_gecos);
99 name = talloc_strdup (ctx, passwd.pw_gecos);
101 name = talloc_strdup (ctx, "");
104 talloc_free (pw_buf);
110 get_username_from_passwd_file (void *ctx)
112 long pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
113 char *pw_buf = talloc_zero_size (ctx, pw_buf_size);
114 struct passwd passwd, *ignored;
118 if (pw_buf_size == -1) pw_buf_size = 64;
119 while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
120 pw_buf_size, &ignored)) == ERANGE) {
121 pw_buf_size = pw_buf_size * 2;
122 pw_buf = talloc_zero_size(ctx, pw_buf_size);
126 name = talloc_strdup (ctx, passwd.pw_name);
128 name = talloc_strdup (ctx, "");
130 talloc_free (pw_buf);
135 /* Open the named notmuch configuration file. If the filename is NULL,
136 * the value of the environment variable $NOTMUCH_CONFIG will be used.
137 * If $NOTMUCH_CONFIG is unset, the default configuration file
138 * ($HOME/.notmuch-config) will be used.
140 * If any error occurs, (out of memory, or a permission-denied error,
141 * etc.), this function will print a message to stderr and return
144 * Note: It is *not* an error if the specified configuration file does
145 * not exist. In this case, a default configuration will be created
146 * and returned. Subsequently calling notmuch_config_save will cause
147 * the configuration to be written to the filename specified at the
148 * time of notmuch_config_open.
150 * The default configuration settings are determined as follows:
152 * database_path: $HOME/mail
154 * user_name: From /etc/passwd
156 * user_primary_mail: $EMAIL variable if set, otherwise
157 * constructed from the username and
158 * hostname of the current machine.
160 * user_other_email: Not set.
162 * The default configuration also contains comments to guide the user
163 * in editing the file directly.
166 notmuch_config_open (void *ctx,
167 const char *filename,
168 notmuch_bool_t *is_new_ret)
170 GError *error = NULL;
172 char *notmuch_config_env = NULL;
177 notmuch_config_t *config = talloc (ctx, notmuch_config_t);
178 if (config == NULL) {
179 fprintf (stderr, "Out of memory.\n");
183 talloc_set_destructor (config, notmuch_config_destructor);
186 config->filename = talloc_strdup (config, filename);
187 } else if ((notmuch_config_env = getenv ("NOTMUCH_CONFIG"))) {
188 config->filename = talloc_strdup (config, notmuch_config_env);
189 notmuch_config_env = NULL;
191 config->filename = talloc_asprintf (config, "%s/.notmuch-config",
195 config->key_file = g_key_file_new ();
197 config->database_path = NULL;
198 config->user_name = NULL;
199 config->user_primary_email = NULL;
200 config->user_other_email = NULL;
201 config->user_other_email_length = 0;
203 if (! g_key_file_load_from_file (config->key_file,
205 G_KEY_FILE_KEEP_COMMENTS,
208 /* We are capable of dealing with a non-existent configuration
209 * file, so be silent about that. */
210 if (!(error->domain == G_FILE_ERROR &&
211 error->code == G_FILE_ERROR_NOENT))
213 fprintf (stderr, "Error reading configuration file %s: %s\n",
214 config->filename, error->message);
215 talloc_free (config);
216 g_error_free (error);
220 g_error_free (error);
224 if (notmuch_config_get_database_path (config) == NULL) {
225 char *path = talloc_asprintf (config, "%s/mail",
227 notmuch_config_set_database_path (config, path);
231 if (notmuch_config_get_user_name (config) == NULL) {
232 char *name = get_name_from_passwd_file (config);
233 notmuch_config_set_user_name (config, name);
237 if (notmuch_config_get_user_primary_email (config) == NULL) {
238 char *email = getenv ("EMAIL");
240 notmuch_config_set_user_primary_email (config, email);
243 struct hostent *hostent;
244 const char *domainname;
246 char *username = get_username_from_passwd_file (config);
248 gethostname (hostname, 256);
249 hostname[255] = '\0';
251 hostent = gethostbyname (hostname);
252 if (hostent && (domainname = strchr (hostent->h_name, '.')))
255 domainname = "(none)";
257 email = talloc_asprintf (config, "%s@%s.%s",
258 username, hostname, domainname);
260 notmuch_config_set_user_primary_email (config, email);
262 talloc_free (username);
267 /* When we create a new configuration file here, we add some
268 * comments to help the user understand what can be done. */
270 g_key_file_set_comment (config->key_file, NULL, NULL,
271 toplevel_config_comment, NULL);
272 g_key_file_set_comment (config->key_file, "database", NULL,
273 database_config_comment, NULL);
274 g_key_file_set_comment (config->key_file, "user", NULL,
275 user_config_comment, NULL);
279 *is_new_ret = is_new;
284 /* Close the given notmuch_config_t object, freeing all resources.
286 * Note: Any changes made to the configuration are *not* saved by this
287 * function. To save changes, call notmuch_config_save before
288 * notmuch_config_close.
291 notmuch_config_close (notmuch_config_t *config)
293 talloc_free (config);
296 /* Save any changes made to the notmuch configuration.
298 * Any comments originally in the file will be preserved.
300 * Returns 0 if successful, and 1 in case of any error, (after
301 * printing a description of the error to stderr).
304 notmuch_config_save (notmuch_config_t *config)
308 GError *error = NULL;
310 data = g_key_file_to_data (config->key_file, &length, NULL);
312 fprintf (stderr, "Out of memory.\n");
316 if (! g_file_set_contents (config->filename, data, length, &error)) {
317 fprintf (stderr, "Error saving configuration to %s: %s\n",
318 config->filename, error->message);
319 g_error_free (error);
327 notmuch_config_get_database_path (notmuch_config_t *config)
331 if (config->database_path == NULL) {
332 path = g_key_file_get_string (config->key_file,
333 "database", "path", NULL);
335 config->database_path = talloc_strdup (config, path);
340 return config->database_path;
344 notmuch_config_set_database_path (notmuch_config_t *config,
345 const char *database_path)
347 g_key_file_set_string (config->key_file,
348 "database", "path", database_path);
350 talloc_free (config->database_path);
351 config->database_path = NULL;
355 notmuch_config_get_user_name (notmuch_config_t *config)
359 if (config->user_name == NULL) {
360 name = g_key_file_get_string (config->key_file,
361 "user", "name", NULL);
363 config->user_name = talloc_strdup (config, name);
368 return config->user_name;
372 notmuch_config_set_user_name (notmuch_config_t *config,
373 const char *user_name)
375 g_key_file_set_string (config->key_file,
376 "user", "name", user_name);
378 talloc_free (config->user_name);
379 config->user_name = NULL;
383 notmuch_config_get_user_primary_email (notmuch_config_t *config)
387 if (config->user_primary_email == NULL) {
388 email = g_key_file_get_string (config->key_file,
389 "user", "primary_email", NULL);
391 config->user_primary_email = talloc_strdup (config, email);
396 return config->user_primary_email;
400 notmuch_config_set_user_primary_email (notmuch_config_t *config,
401 const char *primary_email)
403 g_key_file_set_string (config->key_file,
404 "user", "primary_email", primary_email);
406 talloc_free (config->user_primary_email);
407 config->user_primary_email = NULL;
411 notmuch_config_get_user_other_email (notmuch_config_t *config,
415 size_t emails_length;
418 if (config->user_other_email == NULL) {
419 emails = g_key_file_get_string_list (config->key_file,
420 "user", "other_email",
421 &emails_length, NULL);
423 config->user_other_email = talloc_size (config,
425 (emails_length + 1));
426 for (i = 0; i < emails_length; i++)
427 config->user_other_email[i] = talloc_strdup (config->user_other_email,
429 config->user_other_email[i] = NULL;
433 config->user_other_email_length = emails_length;
437 *length = config->user_other_email_length;
438 return config->user_other_email;
442 notmuch_config_set_user_other_email (notmuch_config_t *config,
443 const char *other_email[],
446 g_key_file_set_string_list (config->key_file,
447 "user", "other_email",
448 other_email, length);
450 talloc_free (config->user_other_email);
451 config->user_other_email = NULL;