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;
84 if (getpwuid_r (getuid (), &passwd, pw_buf, pw_buf_size, &ignored) == 0) {
85 char *comma = strchr (passwd.pw_gecos, ',');
87 name = talloc_strndup (ctx, passwd.pw_gecos,
88 comma - passwd.pw_gecos);
90 name = talloc_strdup (ctx, passwd.pw_gecos);
92 name = talloc_strdup (ctx, "");
101 get_username_from_passwd_file (void *ctx)
103 long pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
104 char *pw_buf = talloc_zero_size (ctx, pw_buf_size);
105 struct passwd passwd, *ignored;
108 if (getpwuid_r (getuid (), &passwd, pw_buf, pw_buf_size, &ignored) == 0)
109 name = talloc_strdup (ctx, passwd.pw_name);
111 name = talloc_strdup (ctx, "");
113 talloc_free (pw_buf);
118 /* Open the named notmuch configuration file. A filename of NULL will
119 * be interpreted as the default configuration file
120 * ($HOME/.notmuch-config).
122 * If any error occurs, (out of memory, or a permission-denied error,
123 * etc.), this function will print a message to stderr and return
126 * Note: It is *not* an error if the specified configuration file does
127 * not exist. In this case, a default configuration will be created
128 * and returned. Subsequently calling notmuch_config_save will cause
129 * the configuration to be written to the filename specified at the
130 * time of notmuch_config_open.
132 * The default configuration settings are determined as follows:
134 * database_path: $HOME/mail
136 * user_name: From /etc/passwd
138 * user_primary_mail: $EMAIL variable if set, otherwise
139 * constructed from the username and
140 * hostname of the current machine.
142 * user_other_email: Not set.
144 * The default configuration also contains comments to guide the user
145 * in editing the file directly.
148 notmuch_config_open (void *ctx, const char *filename)
150 GError *error = NULL;
151 int config_file_is_new = 0;
153 notmuch_config_t *config = talloc (ctx, notmuch_config_t);
154 if (config == NULL) {
155 fprintf (stderr, "Out of memory.\n");
159 talloc_set_destructor (config, notmuch_config_destructor);
162 config->filename = talloc_strdup (config, filename);
164 config->filename = talloc_asprintf (config, "%s/.notmuch-config",
167 config->key_file = g_key_file_new ();
169 config->database_path = NULL;
170 config->user_name = NULL;
171 config->user_primary_email = NULL;
172 config->user_other_email = NULL;
173 config->user_other_email_length = 0;
175 if (! g_key_file_load_from_file (config->key_file,
177 G_KEY_FILE_KEEP_COMMENTS,
180 /* We are capable of dealing with a non-existent configuration
181 * file, so be silent about that. */
182 if (!(error->domain == G_FILE_ERROR &&
183 error->code == G_FILE_ERROR_NOENT))
185 fprintf (stderr, "Error reading configuration file %s: %s\n",
186 config->filename, error->message);
187 talloc_free (config);
191 config_file_is_new = 1;
194 if (notmuch_config_get_database_path (config) == NULL) {
195 char *path = talloc_asprintf (config, "%s/mail",
197 notmuch_config_set_database_path (config, path);
201 if (notmuch_config_get_user_name (config) == NULL) {
202 char *name = get_name_from_passwd_file (config);
203 notmuch_config_set_user_name (config, name);
207 if (notmuch_config_get_user_primary_email (config) == NULL) {
208 char *email = getenv ("EMAIL");
210 notmuch_config_set_user_primary_email (config, email);
213 struct hostent *hostent;
214 const char *domainname;
216 char *username = get_username_from_passwd_file (config);
218 gethostname (hostname, 256);
219 hostname[255] = '\0';
221 hostent = gethostbyname (hostname);
222 if (hostent && (domainname = strchr (hostent->h_name, '.')))
225 domainname = "(none)";
227 email = talloc_asprintf (config, "%s@%s.%s",
228 username, hostname, domainname);
230 notmuch_config_set_user_primary_email (config, email);
232 talloc_free (username);
237 /* When we create a new configuration file here, we add some
238 * comments to help the user understand what can be done. */
239 if (config_file_is_new) {
240 g_key_file_set_comment (config->key_file, NULL, NULL,
241 toplevel_config_comment, NULL);
242 g_key_file_set_comment (config->key_file, "database", NULL,
243 database_config_comment, NULL);
244 g_key_file_set_comment (config->key_file, "user", NULL,
245 user_config_comment, NULL);
251 /* Close the given notmuch_config_t object, freeing all resources.
253 * Note: Any changes made to the configuration are *not* saved by this
254 * function. To save changes, call notmuch_config_save before
255 * notmuch_config_close.
258 notmuch_config_close (notmuch_config_t *config)
260 talloc_free (config);
263 /* Save any changes made to the notmuch configuration.
265 * Any comments originally in the file will be preserved.
267 * Returns 0 if successful, and 1 in case of any error, (after
268 * printing a description of the error to stderr).
271 notmuch_config_save (notmuch_config_t *config)
275 GError *error = NULL;
277 data = g_key_file_to_data (config->key_file, &length, NULL);
279 fprintf (stderr, "Out of memory.\n");
283 if (! g_file_set_contents (config->filename, data, length, &error)) {
284 fprintf (stderr, "Error saving configuration to %s: %s\n",
285 config->filename, error->message);
293 notmuch_config_get_database_path (notmuch_config_t *config)
297 if (config->database_path == NULL) {
298 path = g_key_file_get_string (config->key_file,
299 "database", "path", NULL);
301 config->database_path = talloc_strdup (config, path);
306 return config->database_path;
310 notmuch_config_set_database_path (notmuch_config_t *config,
311 const char *database_path)
313 g_key_file_set_string (config->key_file,
314 "database", "path", database_path);
316 talloc_free (config->database_path);
317 config->database_path = NULL;
321 notmuch_config_get_user_name (notmuch_config_t *config)
325 if (config->user_name == NULL) {
326 name = g_key_file_get_string (config->key_file,
327 "user", "name", NULL);
329 config->user_name = talloc_strdup (config, name);
334 return config->user_name;
338 notmuch_config_set_user_name (notmuch_config_t *config,
339 const char *user_name)
341 g_key_file_set_string (config->key_file,
342 "user", "name", user_name);
344 talloc_free (config->user_name);
345 config->user_name = NULL;
349 notmuch_config_get_user_primary_email (notmuch_config_t *config)
353 if (config->user_primary_email == NULL) {
354 email = g_key_file_get_string (config->key_file,
355 "user", "primary_email", NULL);
357 config->user_primary_email = talloc_strdup (config, email);
362 return config->user_primary_email;
366 notmuch_config_set_user_primary_email (notmuch_config_t *config,
367 const char *primary_email)
369 g_key_file_set_string (config->key_file,
370 "user", "primary_email", primary_email);
372 talloc_free (config->user_primary_email);
373 config->user_primary_email = NULL;
377 notmuch_config_get_user_other_email (notmuch_config_t *config,
381 size_t emails_length;
384 if (config->user_other_email == NULL) {
385 emails = g_key_file_get_string_list (config->key_file,
386 "user", "other_email",
387 &emails_length, NULL);
389 config->user_other_email = talloc_size (config,
391 (emails_length + 1));
392 for (i = 0; i < emails_length; i++)
393 config->user_other_email[i] = talloc_strdup (config->user_other_email,
395 config->user_other_email[i] = NULL;
399 config->user_other_email_length = emails_length;
403 *length = config->user_other_email_length;
404 return config->user_other_email;
408 notmuch_config_set_user_other_email (notmuch_config_t *config,
409 const char *other_email[],
412 g_key_file_set_string_list (config->key_file,
413 "user", "other_email",
414 other_email, length);
416 talloc_free (config->user_other_email);
417 config->user_other_email = NULL;