notmuch: Add a configuration system.

This will allow for things like the database path to be specified
without any cheesy NOTMUCH_BASE environment variable. It also will
allow "notmuch reply" to recognize the user's email address when
constructing a reply in order to do the right thing, (that is, to use
the user's address to which mail was sent as From:, and not to reply
to the user's own addresses).

With this change, the "notmuch setup" command is now strictly for
changing the configuration of notmuch. It no longer creates the
database, but instead instructs the user to call "notmuch new" to do
that.
This commit is contained in:
Carl Worth 2009-11-11 17:01:55 -08:00
parent 69611e66a5
commit 305e76bc0a
7 changed files with 545 additions and 161 deletions

View file

@ -2,6 +2,7 @@ all: notmuch
notmuch_client_srcs = \
notmuch.c \
notmuch-config.c \
notmuch-dump.c \
notmuch-new.c \
notmuch-reply.c \

View file

@ -391,22 +391,6 @@ parse_references (void *ctx,
}
}
char *
notmuch_database_default_path (void)
{
char *path;
if (getenv ("NOTMUCH_BASE"))
return strdup (getenv ("NOTMUCH_BASE"));
if (asprintf (&path, "%s/mail", getenv ("HOME")) == -1) {
fprintf (stderr, "Out of memory.\n");
return xstrdup("");
}
return path;
}
notmuch_database_t *
notmuch_database_create (const char *path)
{
@ -414,10 +398,11 @@ notmuch_database_create (const char *path)
char *notmuch_path = NULL;
struct stat st;
int err;
char *local_path = NULL;
if (path == NULL)
path = local_path = notmuch_database_default_path ();
if (path == NULL) {
fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
goto DONE;
}
err = stat (path, &st);
if (err) {
@ -447,8 +432,6 @@ notmuch_database_create (const char *path)
DONE:
if (notmuch_path)
talloc_free (notmuch_path);
if (local_path)
free (local_path);
return notmuch;
}
@ -460,12 +443,8 @@ notmuch_database_open (const char *path)
char *notmuch_path = NULL, *xapian_path = NULL;
struct stat st;
int err;
char *local_path = NULL;
unsigned int i;
if (path == NULL)
path = local_path = notmuch_database_default_path ();
if (asprintf (&notmuch_path, "%s/%s", path, ".notmuch") == -1) {
notmuch_path = NULL;
fprintf (stderr, "Out of memory\n");
@ -520,8 +499,6 @@ notmuch_database_open (const char *path)
}
DONE:
if (local_path)
free (local_path);
if (notmuch_path)
free (notmuch_path);
if (xapian_path)

View file

@ -114,19 +114,6 @@ typedef struct _notmuch_messages notmuch_messages_t;
typedef struct _notmuch_message notmuch_message_t;
typedef struct _notmuch_tags notmuch_tags_t;
/* Lookup the default database path.
*
* This is the path that will be used by notmuch_database_create and
* notmuch_database_open if given a NULL path. Specifically it will be
* the value of the NOTMUCH_BASE environment variable if set,
* otherwise ${HOME}/mail
*
* Returns a newly allocated string which the caller should free()
* when finished with it.
*/
char *
notmuch_database_default_path (void);
/* Create a new, empty notmuch database located at 'path'.
*
* The path should be a top-level directory to a collection of
@ -134,11 +121,6 @@ notmuch_database_default_path (void);
* create a new ".notmuch" directory within 'path' where notmuch will
* store its data.
*
* Passing a value of NULL for 'path' will cause notmuch to open the
* default database. The default database path can be specified by the
* NOTMUCH_BASE environment variable, and is equivalent to
* ${HOME}/mail if NOTMUCH_BASE is not set.
*
* After a successful call to notmuch_database_create, the returned
* database will be open so the caller should call
* notmuch_database_close when finished with it.
@ -157,7 +139,7 @@ notmuch_database_create (const char *path);
/* XXX: I think I'd like this to take an extra argument of
* notmuch_status_t* for returning a status value on failure. */
/* Open a an existing notmuch database located at 'path'.
/* Open an existing notmuch database located at 'path'.
*
* The database should have been created at some time in the past,
* (not necessarily by this process), by calling
@ -166,11 +148,6 @@ notmuch_database_create (const char *path);
* An existing notmuch database can be identified by the presence of a
* directory named ".notmuch" below 'path'.
*
* Passing a value of NULL for 'path' will cause notmuch to open the
* default database. The default database path can be specified by the
* NOTMUCH_BASE environment variable, and is equivalent to
* ${HOME}/mail if NOTMUCH_BASE is not set.
*
* The caller should call notmuch_database_close when finished with
* this database.
*

View file

@ -140,4 +140,47 @@ notmuch_status_t
show_message_body (const char *filename,
void (*show_part) (GMimeObject *part, int *part_count));
/* notmuch-config.c */
typedef struct _notmuch_config notmuch_config_t;
notmuch_config_t *
notmuch_config_open (void *ctx, const char *filename);
void
notmuch_config_close (notmuch_config_t *config);
int
notmuch_config_save (notmuch_config_t *config);
const char *
notmuch_config_get_database_path (notmuch_config_t *config);
void
notmuch_config_set_database_path (notmuch_config_t *config,
const char *database_path);
const char *
notmuch_config_get_user_name (notmuch_config_t *config);
void
notmuch_config_set_user_name (notmuch_config_t *config,
const char *user_name);
const char *
notmuch_config_get_user_primary_email (notmuch_config_t *config);
void
notmuch_config_set_user_primary_email (notmuch_config_t *config,
const char *primary_email);
char **
notmuch_config_get_user_other_email (notmuch_config_t *config,
size_t *length);
void
notmuch_config_set_user_other_email (notmuch_config_t *config,
const char *other_email[],
size_t length);
#endif

374
notmuch-config.c Normal file
View file

@ -0,0 +1,374 @@
/* notmuch - Not much of an email program, (just index and search)
*
* Copyright © 2009 Carl Worth
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/ .
*
* Author: Carl Worth <cworth@cworth.org>
*/
#include "notmuch-client.h"
#include <pwd.h>
#include <netdb.h>
struct _notmuch_config {
char *filename;
GKeyFile *key_file;
char *database_path;
char *user_name;
char *user_primary_email;
char **user_other_email;
size_t user_other_email_length;
};
static int
notmuch_config_destructor (notmuch_config_t *config)
{
if (config->key_file)
g_key_file_free (config->key_file);
return 0;
}
static char *
get_name_from_passwd_file (void *ctx)
{
long pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
char *pw_buf = talloc_zero_size (ctx, pw_buf_size);
struct passwd passwd, *ignored;
char *name;
if (getpwuid_r (getuid (), &passwd, pw_buf, pw_buf_size, &ignored) == 0) {
char *comma = strchr (passwd.pw_gecos, ',');
if (comma)
name = talloc_strndup (ctx, passwd.pw_gecos,
comma - passwd.pw_gecos);
else
name = talloc_strdup (ctx, passwd.pw_gecos);
} else {
name = talloc_strdup (ctx, "");
}
talloc_free (pw_buf);
return name;
}
static char *
get_username_from_passwd_file (void *ctx)
{
long pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
char *pw_buf = talloc_zero_size (ctx, pw_buf_size);
struct passwd passwd, *ignored;
char *name;
if (getpwuid_r (getuid (), &passwd, pw_buf, pw_buf_size, &ignored) == 0)
name = talloc_strdup (ctx, passwd.pw_name);
else
name = talloc_strdup (ctx, "");
talloc_free (pw_buf);
return name;
}
/* Open the named notmuch configuration file. A filename of NULL will
* be interpreted as the default configuration file
* ($HOME/.notmuch-config).
*
* If any error occurs, (out of memory, or a permission-denied error,
* etc.), this function will print a message to stderr and return
* NULL.
*
* Note: It is *not* an error if the specified configuration file does
* not exist. In this case, a default configuration will be created
* and returned. Subsequently calling notmuch_config_save will cause
* the configuration to be written to the filename specified at the
* time of notmuch_config_open.
*
* The default configuration settings are determined as follows:
*
* database_path: $HOME/mail
*
* user_name: From /etc/passwd
*
* user_primary_mail: $EMAIL variable if set, otherwise
* constructed from the username and
* hostname of the current machine.
*
* user_other_email: Not set.
*
* The default configuration also contains comments to guide the user
* in editing the file directly.
*/
notmuch_config_t *
notmuch_config_open (void *ctx, const char *filename)
{
GError *error = NULL;
notmuch_config_t *config = talloc (ctx, notmuch_config_t);
if (config == NULL) {
fprintf (stderr, "Out of memory.\n");
return NULL;
}
talloc_set_destructor (config, notmuch_config_destructor);
if (filename)
config->filename = talloc_strdup (config, filename);
else
config->filename = talloc_asprintf (config, "%s/.notmuch-config",
getenv ("HOME"));
config->key_file = g_key_file_new ();
config->database_path = NULL;
config->user_name = NULL;
config->user_primary_email = NULL;
config->user_other_email = NULL;
config->user_other_email_length = 0;
if (! g_key_file_load_from_file (config->key_file,
config->filename,
G_KEY_FILE_KEEP_COMMENTS,
&error))
{
/* We are capable of dealing with a non-existent configuration
* file, so be silent about that. */
if (!(error->domain == G_FILE_ERROR &&
error->code == G_FILE_ERROR_NOENT))
{
fprintf (stderr, "Error reading configuration file %s: %s\n",
config->filename, error->message);
talloc_free (config);
return NULL;
}
}
if (notmuch_config_get_database_path (config) == NULL) {
char *path = talloc_asprintf (config, "%s/mail",
getenv ("HOME"));
notmuch_config_set_database_path (config, path);
talloc_free (path);
}
if (notmuch_config_get_user_name (config) == NULL) {
char *name = get_name_from_passwd_file (config);
notmuch_config_set_user_name (config, name);
talloc_free (name);
}
if (notmuch_config_get_user_primary_email (config) == NULL) {
char *email = getenv ("EMAIL");
if (email) {
notmuch_config_set_user_primary_email (config, email);
} else {
char hostname[256];
struct hostent *hostent;
const char *domainname;
char *username = get_username_from_passwd_file (config);
gethostname (hostname, 256);
hostname[255] = '\0';
hostent = gethostbyname (hostname);
if (hostent && (domainname = strchr (hostent->h_name, '.')))
domainname += 1;
else
domainname = "(none)";
email = talloc_asprintf (config, "%s@%s.%s",
username, hostname, domainname);
notmuch_config_set_user_primary_email (config, email);
talloc_free (username);
talloc_free (email);
}
}
return config;
}
/* Close the given notmuch_config_t object, freeing all resources.
*
* Note: Any changes made to the configuration are *not* saved by this
* function. To save changes, call notmuch_config_save before
* notmuch_config_close.
*/
void
notmuch_config_close (notmuch_config_t *config)
{
talloc_free (config);
}
/* Save any changes made to the notmuch configuration.
*
* Any comments originally in the file will be preserved.
*
* Returns 0 if successful, and 1 in case of any error, (after
* printing a description of the error to stderr).
*/
int
notmuch_config_save (notmuch_config_t *config)
{
size_t length;
char *data;
GError *error = NULL;
data = g_key_file_to_data (config->key_file, &length, NULL);
if (data == NULL) {
fprintf (stderr, "Out of memory.\n");
return 1;
}
if (! g_file_set_contents (config->filename, data, length, &error)) {
fprintf (stderr, "Error saving configuration to %s: %s\n",
config->filename, error->message);
return 1;
}
return 0;
}
const char *
notmuch_config_get_database_path (notmuch_config_t *config)
{
char *path;
if (config->database_path == NULL) {
path = g_key_file_get_string (config->key_file,
"database", "path", NULL);
if (path) {
config->database_path = talloc_strdup (config, path);
free (path);
}
}
return config->database_path;
}
void
notmuch_config_set_database_path (notmuch_config_t *config,
const char *database_path)
{
g_key_file_set_string (config->key_file,
"database", "path", database_path);
talloc_free (config->database_path);
config->database_path = NULL;
}
const char *
notmuch_config_get_user_name (notmuch_config_t *config)
{
char *name;
if (config->user_name == NULL) {
name = g_key_file_get_string (config->key_file,
"user", "name", NULL);
if (name) {
config->user_name = talloc_strdup (config, name);
free (name);
}
}
return config->user_name;
}
void
notmuch_config_set_user_name (notmuch_config_t *config,
const char *user_name)
{
g_key_file_set_string (config->key_file,
"user", "name", user_name);
talloc_free (config->user_name);
config->user_name = NULL;
}
const char *
notmuch_config_get_user_primary_email (notmuch_config_t *config)
{
char *email;
if (config->user_primary_email == NULL) {
email = g_key_file_get_string (config->key_file,
"user", "primary_email", NULL);
if (email) {
config->user_primary_email = talloc_strdup (config, email);
free (email);
}
}
return config->user_primary_email;
}
void
notmuch_config_set_user_primary_email (notmuch_config_t *config,
const char *primary_email)
{
g_key_file_set_string (config->key_file,
"user", "primary_email", primary_email);
talloc_free (config->user_primary_email);
config->user_primary_email = NULL;
}
char **
notmuch_config_get_user_other_email (notmuch_config_t *config,
size_t *length)
{
char **emails;
size_t emails_length;
unsigned int i;
if (config->user_other_email == NULL) {
emails = g_key_file_get_string_list (config->key_file,
"user", "other_email",
&emails_length, NULL);
if (emails) {
config->user_other_email = talloc_size (config,
sizeof (char *) *
(emails_length + 1));
for (i = 0; i < emails_length; i++)
config->user_other_email[i] = talloc_strdup (config->user_other_email,
emails[i]);
config->user_other_email[i] = NULL;
g_strfreev (emails);
config->user_other_email_length = emails_length;
}
}
*length = config->user_other_email_length;
return config->user_other_email;
}
void
notmuch_config_set_user_other_email (notmuch_config_t *config,
const char *other_email[],
size_t length)
{
g_key_file_set_string_list (config->key_file,
"user", "other_email",
other_email, length);
talloc_free (config->user_other_email);
config->user_other_email = NULL;
}

View file

@ -145,134 +145,95 @@ count_files (const char *path, int *count)
closedir (dir);
}
static void
welcome_message (void)
static const char *
make_path_absolute (void *ctx, const char *path)
{
printf (
"Welcome to notmuch!\n\n"
"The goal of notmuch is to help you manage and search your collection of\n"
"email, and to efficiently keep up with the flow of email as it comes in.\n\n"
"Notmuch needs to know the top-level directory of your email archive,\n"
"(where you already have mail stored and where messages will be delivered\n"
"in the future). This directory can contain any number of sub-directories\n"
"and primarily just files with indvidual email messages (eg. maildir or mh\n"
"archives are perfect). If there are other, non-email files (such as\n"
"indexes maintained by other email programs) then notmuch will do its\n"
"best to detect those and ignore them.\n\n"
"Mail storage that uses mbox format, (where one mbox file contains many\n"
"messages), will not work with notmuch. If that's how your mail is currently\n"
"stored, we recommend you first convert it to maildir format with a utility\n"
"such as mb2md. In that case, press Control-C now and run notmuch again\n"
"once the conversion is complete.\n\n");
}
char *cwd;
static char *
prompt_user_for_mail_directory ()
{
char *default_path, *mail_directory = NULL;
size_t line_size;
if (*path == '/')
return path;
default_path = notmuch_database_default_path ();
printf ("Top-level mail directory [%s]: ", default_path);
fflush (stdout);
getline (&mail_directory, &line_size, stdin);
chomp_newline (mail_directory);
printf ("\n");
if (mail_directory == NULL || strlen (mail_directory) == 0) {
if (mail_directory)
free (mail_directory);
mail_directory = default_path;
} else {
/* XXX: Instead of telling the user to use an environment
* variable here, we should really be writing out a configuration
* file and loading that on the next run. */
if (strcmp (mail_directory, default_path)) {
printf ("Note: Since you are not using the default path, you will want to set\n"
"the NOTMUCH_BASE environment variable to %s so that\n"
"future calls to notmuch commands will know where to find your mail.\n",
mail_directory);
printf ("For example, if you are using bash for your shell, add:\n\n");
printf ("\texport NOTMUCH_BASE=%s\n\n", mail_directory);
printf ("to your ~/.bashrc file.\n\n");
}
free (default_path);
cwd = getcwd (NULL, 0);
if (cwd == NULL) {
fprintf (stderr, "Out of memory.\n");
return NULL;
}
/* Coerce the directory into an absolute directory name. */
if (*mail_directory != '/') {
char *cwd, *absolute_mail_directory;
path = talloc_asprintf (ctx, "%s/%s", cwd, path);
if (path == NULL)
fprintf (stderr, "Out of memory.\n");
cwd = getcwd (NULL, 0);
if (cwd == NULL) {
fprintf (stderr, "Out of memory.\n");
exit (1);
}
free (cwd);
if (asprintf (&absolute_mail_directory, "%s/%s",
cwd, mail_directory) < 0)
{
fprintf (stderr, "Out of memory.\n");
exit (1);
}
free (cwd);
free (mail_directory);
mail_directory = absolute_mail_directory;
}
return mail_directory;
return path;
}
int
notmuch_setup_command (unused (void *ctx),
unused (int argc), unused (char *argv[]))
{
notmuch_database_t *notmuch = NULL;
char *mail_directory = NULL;
int count;
notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
char *response = NULL;
size_t response_size;
notmuch_config_t *config;
char **old_other_emails;
size_t old_other_emails_len;
GPtrArray *other_emails;
unsigned int i;
welcome_message ();
#define prompt(format, ...) \
do { \
printf (format, ##__VA_ARGS__); \
fflush (stdout); \
getline (&response, &response_size, stdin); \
chomp_newline (response); \
} while (0)
mail_directory = prompt_user_for_mail_directory ();
config = notmuch_config_open (ctx, NULL);
notmuch = notmuch_database_create (mail_directory);
if (notmuch == NULL) {
fprintf (stderr, "Failed to create new notmuch database at %s\n",
mail_directory);
ret = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
prompt ("Your full name [%s]: ", notmuch_config_get_user_name (config));
if (strlen (response))
notmuch_config_set_user_name (config, response);
prompt ("Your primary email address [%s]: ",
notmuch_config_get_user_primary_email (config));
if (strlen (response))
notmuch_config_set_user_primary_email (config, response);
other_emails = g_ptr_array_new ();
old_other_emails = notmuch_config_get_user_other_email (config,
&old_other_emails_len);
for (i = 0; i < old_other_emails_len; i++) {
prompt ("Additional email address [%s]: ", old_other_emails[i]);
if (strlen (response))
g_ptr_array_add (other_emails, talloc_strdup (ctx, response));
else
g_ptr_array_add (other_emails, talloc_strdup (ctx,
old_other_emails[i]));
}
printf ("OK. Let's take a look at the mail we can find in the directory\n");
printf ("%s ...\n", mail_directory);
do {
prompt ("Additional email address [Press 'Enter' if none]: ");
if (strlen (response))
g_ptr_array_add (other_emails, talloc_strdup (ctx, response));
} while (strlen (response));
if (other_emails->len)
notmuch_config_set_user_other_email (config,
(const char **)
other_emails->pdata,
other_emails->len);
g_ptr_array_free (other_emails, TRUE);
count = 0;
count_files (mail_directory, &count);
prompt ("Top-level directory of your email archive [%s]: ",
notmuch_config_get_database_path (config));
if (strlen (response)) {
const char *absolute_path;
printf ("Found %d total files. That's not much mail.\n\n", count);
printf ("Next, we'll inspect the messages and create a database of threads:\n");
ret = add_all_files (notmuch, mail_directory, count);
printf ("When new mail is delivered to %s in the future,\n"
"run \"notmuch new\" to add it to the database.\n\n",
mail_directory);
if (ret) {
printf ("Note: At least one error was encountered: %s\n",
notmuch_status_to_string (ret));
absolute_path = make_path_absolute (ctx, response);
notmuch_config_set_database_path (config, absolute_path);
}
DONE:
if (mail_directory)
free (mail_directory);
if (notmuch)
notmuch_database_close (notmuch);
notmuch_config_save (config);
return ret;
return 0;
}

View file

@ -204,18 +204,69 @@ notmuch_help_command (unused (void *ctx), int argc, char *argv[])
argv[0]);
return 1;
}
/* Handle the case of "notmuch" being invoked with no command
* argument. Print a welcome and explanatory message, then invoke
* notmuch_setup_command.
*/
static int
notmuch (void *ctx)
{
int ret;
printf (
"Welcome to notmuch!\n\n"
"The goal of notmuch is to help you manage and search your collection of\n"
"email, and to efficiently keep up with the flow of email as it comes in.\n\n"
"Notmuch needs to know a few things about you such as your name and email\n"
"address, as well as the directory that contains your email. This is where\n"
"you already have mail stored and where messages will be delivered in the\n"
"future. This directory can contain any number of sub-directories. Regular\n"
"files in these directories should be individual email messages. If there\n"
"are other, non-email files (such as indexes maintained by other email\n"
"programs) then notmuch will do its best to detect those and ignore them.\n\n"
"If you already have your email being delivered to directories in either\n"
"maildir or mh format, then that's perfect. Mail storage that uses mbox\n"
"format, (where one mbox file contains many messages), will not work with\n"
"notmuch. If that's how your mail is currently stored, we recommend you\n"
"first convert it to maildir format with a utility such as mb2md. You can\n"
"continue configuring notmuch now, but be sure to complete the conversion\n"
"before you run \"notmuch new\" for the first time.\n\n");
ret = notmuch_setup_command (ctx, 0, NULL);
printf ("\n"
"Notmuch is now configured, and the configuration settings are saved in\n"
"a file in your home directory named .notmuch-config . If you'd like to\n"
"change the configuration in the future, you can either edit that file\n"
"directly or run \"notmuch setup\".\n\n"
"The next step is to run \"notmuch new\" which will create a database\n"
"that indexes all of your mail. Depending on the amount of mail you have\n"
"the initial indexing process can take a long time, so expect that.\n"
"Also, the resulting database will require roughly the same amount of\n"
"storage space as your current collection of email. So please ensure you\n"
"have sufficient storage space available now.\n\n");
return ret;
}
int
main (int argc, char *argv[])
{
void *local = talloc_new (NULL);
void *local;
command_t *command;
unsigned int i;
local = talloc_new (NULL);
g_mime_init (0);
if (argc == 1)
return notmuch_setup_command (local, 0, NULL);
return notmuch (local);
for (i = 0; i < ARRAY_SIZE (commands); i++) {
command = &commands[i];