]> git.notmuchmail.org Git - notmuch/blob - notmuch-config.c
notmuch-config: make new message tags configurable
[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 messages_config_comment[] =
41     " Messages configuration\n"
42     "\n"
43     " The only value supported here is 'new_tags' which lists the tags that\n"
44     " should be applied to new messages.\n";
45
46 static const char user_config_comment[] =
47     " User configuration\n"
48     "\n"
49     " Here is where you can let notmuch know how you would like to be\n"
50     " addressed. Valid settings are\n"
51     "\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"
56     "\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";
61
62 struct _notmuch_config {
63     char *filename;
64     GKeyFile *key_file;
65
66     char *database_path;
67     char *user_name;
68     char *user_primary_email;
69     char **user_other_email;
70     size_t user_other_email_length;
71     char **new_tags;
72     size_t new_tags_length;
73 };
74
75 static int
76 notmuch_config_destructor (notmuch_config_t *config)
77 {
78     if (config->key_file)
79         g_key_file_free (config->key_file);
80
81     return 0;
82 }
83
84 static char *
85 get_name_from_passwd_file (void *ctx)
86 {
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;
90     char *name;
91     int e;
92
93     if (pw_buf_size == -1) pw_buf_size = 64;
94
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);
99     }
100
101     if (e == 0) {
102         char *comma = strchr (passwd.pw_gecos, ',');
103         if (comma)
104             name = talloc_strndup (ctx, passwd.pw_gecos,
105                                    comma - passwd.pw_gecos);
106         else
107             name = talloc_strdup (ctx, passwd.pw_gecos);
108     } else {
109         name = talloc_strdup (ctx, "");
110     }
111
112     talloc_free (pw_buf);
113
114     return name;
115 }
116
117 static char *
118 get_username_from_passwd_file (void *ctx)
119 {
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;
123     char *name;
124     int e;
125
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);
131     }
132
133     if (e == 0)
134         name = talloc_strdup (ctx, passwd.pw_name);
135     else
136         name = talloc_strdup (ctx, "");
137
138     talloc_free (pw_buf);
139
140     return name;
141 }
142
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.
147  *
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
150  * NULL.
151  *
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.
157  *
158  * The default configuration settings are determined as follows:
159  *
160  *      database_path:          $HOME/mail
161  *
162  *      user_name:              From /etc/passwd
163  *
164  *      user_primary_mail:      $EMAIL variable if set, otherwise
165  *                              constructed from the username and
166  *                              hostname of the current machine.
167  *
168  *      user_other_email:       Not set.
169  *
170  * The default configuration also contains comments to guide the user
171  * in editing the file directly.
172  */
173 notmuch_config_t *
174 notmuch_config_open (void *ctx,
175                      const char *filename,
176                      notmuch_bool_t *is_new_ret)
177 {
178     GError *error = NULL;
179     int is_new = 0;
180     size_t tmp;
181     char *notmuch_config_env = NULL;
182
183     if (is_new_ret)
184         *is_new_ret = 0;
185
186     notmuch_config_t *config = talloc (ctx, notmuch_config_t);
187     if (config == NULL) {
188         fprintf (stderr, "Out of memory.\n");
189         return NULL;
190     }
191     
192     talloc_set_destructor (config, notmuch_config_destructor);
193
194     if (filename) {
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);
198     } else {
199         config->filename = talloc_asprintf (config, "%s/.notmuch-config",
200                                             getenv ("HOME"));
201     }
202
203     config->key_file = g_key_file_new ();
204
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;
212
213     if (! g_key_file_load_from_file (config->key_file,
214                                      config->filename,
215                                      G_KEY_FILE_KEEP_COMMENTS,
216                                      &error))
217     {
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
221          * variable)
222          */
223         if (notmuch_config_env ||
224             !(error->domain == G_FILE_ERROR &&
225               error->code == G_FILE_ERROR_NOENT))
226         {
227             fprintf (stderr, "Error reading configuration file %s: %s\n",
228                      config->filename, error->message);
229             talloc_free (config);
230             g_error_free (error);
231             return NULL;
232         }
233
234         g_error_free (error);
235         is_new = 1;
236     }
237
238     if (notmuch_config_get_database_path (config) == NULL) {
239         char *path = talloc_asprintf (config, "%s/mail",
240                                       getenv ("HOME"));
241         notmuch_config_set_database_path (config, path);
242         talloc_free (path);
243     }
244
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);
248         talloc_free (name);
249     }
250
251     if (notmuch_config_get_user_primary_email (config) == NULL) {
252         char *email = getenv ("EMAIL");
253         if (email) {
254             notmuch_config_set_user_primary_email (config, email);
255         } else {
256             char hostname[256];
257             struct hostent *hostent;
258             const char *domainname;
259
260             char *username = get_username_from_passwd_file (config);
261
262             gethostname (hostname, 256);
263             hostname[255] = '\0';
264
265             hostent = gethostbyname (hostname);
266             if (hostent && (domainname = strchr (hostent->h_name, '.')))
267                 domainname += 1;
268             else
269                 domainname = "(none)";
270
271             email = talloc_asprintf (config, "%s@%s.%s",
272                                      username, hostname, domainname);
273
274             notmuch_config_set_user_primary_email (config, email);
275
276             talloc_free (username);
277             talloc_free (email);
278         }
279     }
280
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);
284     }
285
286     /* When we create a new configuration file here, we  add some
287      * comments to help the user understand what can be done. */
288     if (is_new) {
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);
297     }
298
299     if (is_new_ret)
300         *is_new_ret = is_new;
301
302     return config;
303 }
304
305 /* Close the given notmuch_config_t object, freeing all resources.
306  * 
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.
310 */
311 void
312 notmuch_config_close (notmuch_config_t *config)
313 {
314     talloc_free (config);
315 }
316
317 /* Save any changes made to the notmuch configuration.
318  *
319  * Any comments originally in the file will be preserved.
320  *
321  * Returns 0 if successful, and 1 in case of any error, (after
322  * printing a description of the error to stderr).
323  */
324 int
325 notmuch_config_save (notmuch_config_t *config)
326 {
327     size_t length;
328     char *data;
329     GError *error = NULL;
330
331     data = g_key_file_to_data (config->key_file, &length, NULL);
332     if (data == NULL) {
333         fprintf (stderr, "Out of memory.\n");
334         return 1;
335     }
336
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);
341         g_free (data);
342         return 1;
343     }
344
345     g_free (data);
346     return 0;
347 }
348
349 const char *
350 notmuch_config_get_database_path (notmuch_config_t *config)
351 {
352     char *path;
353
354     if (config->database_path == NULL) {
355         path = g_key_file_get_string (config->key_file,
356                                       "database", "path", NULL);
357         if (path) {
358             config->database_path = talloc_strdup (config, path);
359             free (path);
360         }
361     }
362
363     return config->database_path;
364 }
365
366 void
367 notmuch_config_set_database_path (notmuch_config_t *config,
368                                   const char *database_path)
369 {
370     g_key_file_set_string (config->key_file,
371                            "database", "path", database_path);
372
373     talloc_free (config->database_path);
374     config->database_path = NULL;
375 }
376
377 const char *
378 notmuch_config_get_user_name (notmuch_config_t *config)
379 {
380     char *name;
381
382     if (config->user_name == NULL) {
383         name = g_key_file_get_string (config->key_file,
384                                       "user", "name", NULL);
385         if (name) {
386             config->user_name = talloc_strdup (config, name);
387             free (name);
388         }
389     }
390
391     return config->user_name;
392 }
393
394 void
395 notmuch_config_set_user_name (notmuch_config_t *config,
396                               const char *user_name)
397 {
398     g_key_file_set_string (config->key_file,
399                            "user", "name", user_name);
400
401     talloc_free (config->user_name);
402     config->user_name = NULL;
403 }
404
405 const char *
406 notmuch_config_get_user_primary_email (notmuch_config_t *config)
407 {
408     char *email;
409
410     if (config->user_primary_email == NULL) {
411         email = g_key_file_get_string (config->key_file,
412                                        "user", "primary_email", NULL);
413         if (email) {
414             config->user_primary_email = talloc_strdup (config, email);
415             free (email);
416         }
417     }
418
419     return config->user_primary_email;
420 }
421
422 void
423 notmuch_config_set_user_primary_email (notmuch_config_t *config,
424                                        const char *primary_email)
425 {
426     g_key_file_set_string (config->key_file,
427                            "user", "primary_email", primary_email);
428
429     talloc_free (config->user_primary_email);
430     config->user_primary_email = NULL;
431 }
432
433 char **
434 notmuch_config_get_user_other_email (notmuch_config_t *config,
435                                      size_t *length)
436 {
437     char **emails;
438     size_t emails_length;
439     unsigned int i;
440
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);
445         if (emails) {
446             config->user_other_email = talloc_size (config,
447                                                     sizeof (char *) *
448                                                     (emails_length + 1));
449             for (i = 0; i < emails_length; i++)
450                 config->user_other_email[i] = talloc_strdup (config->user_other_email,
451                                                              emails[i]);
452             config->user_other_email[i] = NULL;
453
454             g_strfreev (emails);
455
456             config->user_other_email_length = emails_length;
457         }
458     }
459
460     *length = config->user_other_email_length;
461     return config->user_other_email;
462 }
463
464 void
465 notmuch_config_set_user_other_email (notmuch_config_t *config,
466                                      const char *other_email[],
467                                      size_t length)
468 {
469     g_key_file_set_string_list (config->key_file,
470                                 "user", "other_email",
471                                 other_email, length);
472
473     talloc_free (config->user_other_email);
474     config->user_other_email = NULL;
475 }
476
477 char **
478 notmuch_config_get_new_tags (notmuch_config_t *config,
479                              size_t *length)
480 {
481     char **tags;
482     size_t tags_length;
483     unsigned int i;
484
485     if (config->new_tags == NULL) {
486         tags = g_key_file_get_string_list (config->key_file,
487                                            "messages", "new_tags",
488                                            &tags_length, NULL);
489         if (tags) {
490             config->new_tags = talloc_size (config,
491                                             sizeof (char *) *
492                                             (tags_length + 1));
493             for (i = 0; i < tags_length; i++)
494                 config->new_tags[i] = talloc_strdup (config->new_tags,
495                                                      tags[i]);
496             config->new_tags[i] = NULL;
497
498             g_strfreev (tags);
499
500             config->new_tags_length = tags_length;
501         }
502     }
503
504     *length = config->new_tags_length;
505     return config->new_tags;
506 }
507
508 void
509 notmuch_config_set_new_tags (notmuch_config_t *config,
510                              const char *new_tags[],
511                              size_t length)
512 {
513     g_key_file_set_string_list (config->key_file,
514                                 "messages", "new_tags",
515                                 new_tags, length);
516
517     talloc_free (config->new_tags);
518     config->new_tags = NULL;
519 }
520