]> git.notmuchmail.org Git - notmuch/blob - notmuch-config.c
notmuch reply: Add Bcc to the user's primary email address.
[notmuch] / notmuch-config.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22
23 #include <pwd.h>
24 #include <netdb.h>
25
26 static const char toplevel_config_comment[] =
27     " .notmuch-config - Configuration file for the notmuch mail system\n"
28     "\n"
29     " For more information about notmuch, see http://notmuchmail.org";
30
31 static const char database_config_comment[] =
32     " Database configuration\n"
33     "\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";
39
40 static const char user_config_comment[] =
41     " User configuration\n"
42     "\n"
43     " Here is where you can let notmuch know how you would like to be\n"
44     " addressed. Valid settings are\n"
45     "\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"
50     "\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";
55
56 struct _notmuch_config {
57     char *filename;
58     GKeyFile *key_file;
59
60     char *database_path;
61     char *user_name;
62     char *user_primary_email;
63     char **user_other_email;
64     size_t user_other_email_length;
65 };
66
67 static int
68 notmuch_config_destructor (notmuch_config_t *config)
69 {
70     if (config->key_file)
71         g_key_file_free (config->key_file);
72
73     return 0;
74 }
75
76 static char *
77 get_name_from_passwd_file (void *ctx)
78 {
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;
82     char *name;
83
84     if (getpwuid_r (getuid (), &passwd, pw_buf, pw_buf_size, &ignored) == 0) {
85         char *comma = strchr (passwd.pw_gecos, ',');
86         if (comma)
87             name = talloc_strndup (ctx, passwd.pw_gecos,
88                                    comma - passwd.pw_gecos);
89         else
90             name = talloc_strdup (ctx, passwd.pw_gecos);
91     } else {
92         name = talloc_strdup (ctx, "");
93     }
94
95     talloc_free (pw_buf);
96
97     return name;
98 }
99
100 static char *
101 get_username_from_passwd_file (void *ctx)
102 {
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;
106     char *name;
107
108     if (getpwuid_r (getuid (), &passwd, pw_buf, pw_buf_size, &ignored) == 0)
109         name = talloc_strdup (ctx, passwd.pw_name);
110     else
111         name = talloc_strdup (ctx, "");
112
113     talloc_free (pw_buf);
114
115     return name;
116 }
117
118 /* Open the named notmuch configuration file. A filename of NULL will
119  * be interpreted as the default configuration file
120  * ($HOME/.notmuch-config).
121  *
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
124  * NULL.
125  *
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.
131  *
132  * The default configuration settings are determined as follows:
133  *
134  *      database_path:          $HOME/mail
135  *
136  *      user_name:              From /etc/passwd
137  *
138  *      user_primary_mail:      $EMAIL variable if set, otherwise
139  *                              constructed from the username and
140  *                              hostname of the current machine.
141  *
142  *      user_other_email:       Not set.
143  *
144  * The default configuration also contains comments to guide the user
145  * in editing the file directly.
146  */
147 notmuch_config_t *
148 notmuch_config_open (void *ctx,
149                      const char *filename,
150                      notmuch_bool_t *is_new_ret)
151 {
152     GError *error = NULL;
153     int is_new = 0;
154
155     if (is_new_ret)
156         *is_new_ret = 0;
157
158     notmuch_config_t *config = talloc (ctx, notmuch_config_t);
159     if (config == NULL) {
160         fprintf (stderr, "Out of memory.\n");
161         return NULL;
162     }
163     
164     talloc_set_destructor (config, notmuch_config_destructor);
165
166     if (filename)
167         config->filename = talloc_strdup (config, filename);
168     else
169         config->filename = talloc_asprintf (config, "%s/.notmuch-config",
170                                             getenv ("HOME"));
171
172     config->key_file = g_key_file_new ();
173
174     config->database_path = NULL;
175     config->user_name = NULL;
176     config->user_primary_email = NULL;
177     config->user_other_email = NULL;
178     config->user_other_email_length = 0;
179
180     if (! g_key_file_load_from_file (config->key_file,
181                                      config->filename,
182                                      G_KEY_FILE_KEEP_COMMENTS,
183                                      &error))
184     {
185         /* We are capable of dealing with a non-existent configuration
186          * file, so be silent about that. */
187         if (!(error->domain == G_FILE_ERROR &&
188               error->code == G_FILE_ERROR_NOENT))
189         {
190             fprintf (stderr, "Error reading configuration file %s: %s\n",
191                      config->filename, error->message);
192             talloc_free (config);
193             return NULL;
194         }
195
196         is_new = 1;
197     }
198
199     if (notmuch_config_get_database_path (config) == NULL) {
200         char *path = talloc_asprintf (config, "%s/mail",
201                                       getenv ("HOME"));
202         notmuch_config_set_database_path (config, path);
203         talloc_free (path);
204     }
205
206     if (notmuch_config_get_user_name (config) == NULL) {
207         char *name = get_name_from_passwd_file (config);
208         notmuch_config_set_user_name (config, name);
209         talloc_free (name);
210     }
211
212     if (notmuch_config_get_user_primary_email (config) == NULL) {
213         char *email = getenv ("EMAIL");
214         if (email) {
215             notmuch_config_set_user_primary_email (config, email);
216         } else {
217             char hostname[256];
218             struct hostent *hostent;
219             const char *domainname;
220
221             char *username = get_username_from_passwd_file (config);
222
223             gethostname (hostname, 256);
224             hostname[255] = '\0';
225
226             hostent = gethostbyname (hostname);
227             if (hostent && (domainname = strchr (hostent->h_name, '.')))
228                 domainname += 1;
229             else
230                 domainname = "(none)";
231
232             email = talloc_asprintf (config, "%s@%s.%s",
233                                      username, hostname, domainname);
234
235             notmuch_config_set_user_primary_email (config, email);
236
237             talloc_free (username);
238             talloc_free (email);
239         }
240     }
241
242     /* When we create a new configuration file here, we  add some
243      * comments to help the user understand what can be done. */
244     if (is_new) {
245         g_key_file_set_comment (config->key_file, NULL, NULL,
246                                 toplevel_config_comment, NULL);
247         g_key_file_set_comment (config->key_file, "database", NULL,
248                                 database_config_comment, NULL);
249         g_key_file_set_comment (config->key_file, "user", NULL,
250                                 user_config_comment, NULL);
251     }
252
253     if (is_new_ret)
254         *is_new_ret = is_new;
255
256     return config;
257 }
258
259 /* Close the given notmuch_config_t object, freeing all resources.
260  * 
261  * Note: Any changes made to the configuration are *not* saved by this
262  * function. To save changes, call notmuch_config_save before
263  * notmuch_config_close.
264 */
265 void
266 notmuch_config_close (notmuch_config_t *config)
267 {
268     talloc_free (config);
269 }
270
271 /* Save any changes made to the notmuch configuration.
272  *
273  * Any comments originally in the file will be preserved.
274  *
275  * Returns 0 if successful, and 1 in case of any error, (after
276  * printing a description of the error to stderr).
277  */
278 int
279 notmuch_config_save (notmuch_config_t *config)
280 {
281     size_t length;
282     char *data;
283     GError *error = NULL;
284
285     data = g_key_file_to_data (config->key_file, &length, NULL);
286     if (data == NULL) {
287         fprintf (stderr, "Out of memory.\n");
288         return 1;
289     }
290
291     if (! g_file_set_contents (config->filename, data, length, &error)) {
292         fprintf (stderr, "Error saving configuration to %s: %s\n",
293                  config->filename, error->message);
294         return 1;
295     }
296
297     return 0;
298 }
299
300 const char *
301 notmuch_config_get_database_path (notmuch_config_t *config)
302 {
303     char *path;
304
305     if (config->database_path == NULL) {
306         path = g_key_file_get_string (config->key_file,
307                                       "database", "path", NULL);
308         if (path) {
309             config->database_path = talloc_strdup (config, path);
310             free (path);
311         }
312     }
313
314     return config->database_path;
315 }
316
317 void
318 notmuch_config_set_database_path (notmuch_config_t *config,
319                                   const char *database_path)
320 {
321     g_key_file_set_string (config->key_file,
322                            "database", "path", database_path);
323
324     talloc_free (config->database_path);
325     config->database_path = NULL;
326 }
327
328 const char *
329 notmuch_config_get_user_name (notmuch_config_t *config)
330 {
331     char *name;
332
333     if (config->user_name == NULL) {
334         name = g_key_file_get_string (config->key_file,
335                                       "user", "name", NULL);
336         if (name) {
337             config->user_name = talloc_strdup (config, name);
338             free (name);
339         }
340     }
341
342     return config->user_name;
343 }
344
345 void
346 notmuch_config_set_user_name (notmuch_config_t *config,
347                               const char *user_name)
348 {
349     g_key_file_set_string (config->key_file,
350                            "user", "name", user_name);
351
352     talloc_free (config->user_name);
353     config->user_name = NULL;
354 }
355
356 const char *
357 notmuch_config_get_user_primary_email (notmuch_config_t *config)
358 {
359     char *email;
360
361     if (config->user_primary_email == NULL) {
362         email = g_key_file_get_string (config->key_file,
363                                        "user", "primary_email", NULL);
364         if (email) {
365             config->user_primary_email = talloc_strdup (config, email);
366             free (email);
367         }
368     }
369
370     return config->user_primary_email;
371 }
372
373 void
374 notmuch_config_set_user_primary_email (notmuch_config_t *config,
375                                        const char *primary_email)
376 {
377     g_key_file_set_string (config->key_file,
378                            "user", "primary_email", primary_email);
379
380     talloc_free (config->user_primary_email);
381     config->user_primary_email = NULL;
382 }
383
384 char **
385 notmuch_config_get_user_other_email (notmuch_config_t *config,
386                                      size_t *length)
387 {
388     char **emails;
389     size_t emails_length;
390     unsigned int i;
391
392     if (config->user_other_email == NULL) {
393         emails = g_key_file_get_string_list (config->key_file,
394                                              "user", "other_email",
395                                              &emails_length, NULL);
396         if (emails) {
397             config->user_other_email = talloc_size (config,
398                                                     sizeof (char *) *
399                                                     (emails_length + 1));
400             for (i = 0; i < emails_length; i++)
401                 config->user_other_email[i] = talloc_strdup (config->user_other_email,
402                                                              emails[i]);
403             config->user_other_email[i] = NULL;
404
405             g_strfreev (emails);
406
407             config->user_other_email_length = emails_length;
408         }
409     }
410
411     *length = config->user_other_email_length;
412     return config->user_other_email;
413 }
414
415 void
416 notmuch_config_set_user_other_email (notmuch_config_t *config,
417                                      const char *other_email[],
418                                      size_t length)
419 {
420     g_key_file_set_string_list (config->key_file,
421                                 "user", "other_email",
422                                 other_email, length);
423
424     talloc_free (config->user_other_email);
425     config->user_other_email = NULL;
426 }