2009-11-28 13:06:21 +01:00
|
|
|
/*
|
2010-01-01 14:56:45 +01:00
|
|
|
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2009-11-28 13:06:21 +01:00
|
|
|
**
|
|
|
|
** 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, 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, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2010-01-08 19:54:55 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2009-11-28 13:06:21 +01:00
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h> /* memset */
|
2009-11-29 23:04:16 +01:00
|
|
|
|
|
|
|
#include "mu-util.h"
|
2009-11-28 13:06:21 +01:00
|
|
|
#include "mu-config.h"
|
|
|
|
|
|
|
|
|
2010-01-21 19:05:19 +01:00
|
|
|
static GOptionGroup*
|
|
|
|
config_options_group_mu (MuConfigOptions *opts)
|
2009-11-28 13:06:21 +01:00
|
|
|
{
|
|
|
|
GOptionGroup *og;
|
|
|
|
GOptionEntry entries[] = {
|
2010-01-06 20:43:28 +01:00
|
|
|
{ "debug", 'd', 0, G_OPTION_ARG_NONE, &opts->debug,
|
2009-12-23 23:41:56 +01:00
|
|
|
"print debug output to standard error", NULL },
|
2009-11-28 13:06:21 +01:00
|
|
|
{ "quiet", 'q', 0, G_OPTION_ARG_NONE, &opts->quiet,
|
|
|
|
"don't give any progress information", NULL },
|
2009-11-28 19:55:50 +01:00
|
|
|
{"version", 'v', 0, G_OPTION_ARG_NONE, &opts->version,
|
|
|
|
"display version and copyright information", NULL},
|
2010-01-06 20:43:28 +01:00
|
|
|
{"muhome", 'a', 0, G_OPTION_ARG_FILENAME, &opts->muhome,
|
2010-02-11 19:19:43 +01:00
|
|
|
"specify an alternative mu directory", NULL},
|
2009-12-11 18:45:37 +01:00
|
|
|
{"log-stderr", 'e', 0, G_OPTION_ARG_NONE, &opts->log_stderr,
|
2009-11-28 19:55:50 +01:00
|
|
|
"log to standard error", NULL},
|
2009-12-11 21:04:54 +01:00
|
|
|
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY,
|
|
|
|
&opts->params, "parameters", NULL },
|
2009-12-08 23:02:55 +01:00
|
|
|
{ NULL, 0, 0, 0, NULL, NULL, NULL }
|
2009-11-28 13:06:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
og = g_option_group_new ("mu",
|
|
|
|
"general mu options",
|
|
|
|
"", NULL, NULL);
|
|
|
|
g_option_group_add_entries (og, entries);
|
|
|
|
|
|
|
|
return og;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-01-21 19:05:19 +01:00
|
|
|
static GOptionGroup*
|
|
|
|
config_options_group_index (MuConfigOptions *opts)
|
2009-11-28 13:06:21 +01:00
|
|
|
{
|
|
|
|
GOptionGroup *og;
|
|
|
|
GOptionEntry entries[] = {
|
|
|
|
{"maildir", 'm', 0, G_OPTION_ARG_FILENAME, &opts->maildir,
|
|
|
|
"top of the maildir", NULL},
|
|
|
|
{"reindex", 'r', 0, G_OPTION_ARG_NONE, &opts->reindex,
|
2010-01-06 00:24:37 +01:00
|
|
|
"index already indexed messages too", NULL},
|
2010-02-08 20:21:30 +01:00
|
|
|
{"rebuild", 'y', 0, G_OPTION_ARG_NONE, &opts->rebuild,
|
|
|
|
"rebuild the database from scratch", NULL},
|
2010-01-23 20:57:57 +01:00
|
|
|
{"autoupgrade", 'u', 0, G_OPTION_ARG_NONE, &opts->autoupgrade,
|
2010-02-02 20:54:00 +01:00
|
|
|
"automatically upgrade the database with new mu versions", NULL},
|
2010-01-23 21:02:26 +01:00
|
|
|
{"nocleanup", 'n', 0, G_OPTION_ARG_NONE, &opts->nocleanup,
|
2010-01-08 19:54:55 +01:00
|
|
|
"don't clean up the database after indexing", NULL},
|
2009-12-08 23:02:55 +01:00
|
|
|
{ NULL, 0, 0, 0, NULL, NULL, NULL }
|
2009-11-28 13:06:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
og = g_option_group_new ("index",
|
|
|
|
"options for the 'index' command",
|
|
|
|
"", NULL, NULL);
|
|
|
|
g_option_group_add_entries (og, entries);
|
|
|
|
|
|
|
|
return og;
|
|
|
|
}
|
|
|
|
|
2010-01-21 19:05:19 +01:00
|
|
|
static GOptionGroup*
|
|
|
|
config_options_group_find (MuConfigOptions *opts)
|
2009-11-28 13:06:21 +01:00
|
|
|
{
|
|
|
|
GOptionGroup *og;
|
|
|
|
GOptionEntry entries[] = {
|
|
|
|
{"xquery", 'x', 0, G_OPTION_ARG_NONE, &opts->xquery,
|
2009-12-11 18:45:37 +01:00
|
|
|
"print the Xapian query", NULL},
|
2009-11-28 13:06:21 +01:00
|
|
|
{"fields", 'f', 0, G_OPTION_ARG_STRING, &opts->fields,
|
2009-12-23 23:41:56 +01:00
|
|
|
"fields to display in the output", NULL},
|
2009-12-11 22:55:35 +01:00
|
|
|
{"sortfield", 's', 0, G_OPTION_ARG_STRING, &opts->sortfield,
|
2009-11-28 13:06:21 +01:00
|
|
|
"field to sort on", NULL},
|
2010-01-06 20:43:28 +01:00
|
|
|
{"descending", 'z', 0, G_OPTION_ARG_NONE, &opts->descending,
|
|
|
|
"sort in descending order (z -> a)", NULL},
|
|
|
|
{"linksdir", 'l', 0, G_OPTION_ARG_STRING, &opts->linksdir,
|
2010-01-01 14:37:39 +01:00
|
|
|
"output as symbolic links to a target maildir", NULL },
|
2010-01-03 12:41:56 +01:00
|
|
|
{"clearlinks", 'c', 0, G_OPTION_ARG_NONE, &opts->clearlinks,
|
|
|
|
"clear old links before filling a linksdir", NULL},
|
2009-12-08 23:02:55 +01:00
|
|
|
{ NULL, 0, 0, 0, NULL, NULL, NULL }
|
2009-11-28 13:06:21 +01:00
|
|
|
};
|
|
|
|
|
2010-01-08 19:54:55 +01:00
|
|
|
og = g_option_group_new ("find",
|
|
|
|
"options for the 'find' command",
|
2009-11-28 13:06:21 +01:00
|
|
|
"", NULL, NULL);
|
|
|
|
g_option_group_add_entries (og, entries);
|
|
|
|
|
|
|
|
return og;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-21 19:05:19 +01:00
|
|
|
static GOptionGroup*
|
|
|
|
config_options_group_mkdir (MuConfigOptions *opts)
|
2009-11-28 13:06:21 +01:00
|
|
|
{
|
2010-01-21 19:05:19 +01:00
|
|
|
GOptionGroup *og;
|
|
|
|
GOptionEntry entries[] = {
|
|
|
|
{"mode", 'p', 0, G_OPTION_ARG_INT, &opts->dirmode,
|
|
|
|
"set the mode (as in chmod), in octal notation", NULL},
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL, NULL }
|
|
|
|
};
|
2009-11-28 13:06:21 +01:00
|
|
|
|
2010-01-21 19:05:19 +01:00
|
|
|
og = g_option_group_new ("mkdir",
|
|
|
|
"options for the 'mkdir' command",
|
|
|
|
"", NULL, NULL);
|
|
|
|
g_option_group_add_entries (og, entries);
|
|
|
|
|
|
|
|
return og;
|
2010-01-02 13:44:26 +01:00
|
|
|
}
|
|
|
|
|
2010-01-21 19:05:19 +01:00
|
|
|
|
2010-01-06 00:24:37 +01:00
|
|
|
static gchar*
|
2010-01-15 21:11:51 +01:00
|
|
|
guess_muhome (void)
|
2010-01-06 00:24:37 +01:00
|
|
|
{
|
|
|
|
const char* home;
|
|
|
|
|
|
|
|
home = g_getenv ("HOME");
|
|
|
|
if (!home)
|
|
|
|
home = g_get_home_dir ();
|
|
|
|
|
|
|
|
if (!home)
|
|
|
|
MU_WRITE_LOG ("failed to determine homedir");
|
|
|
|
|
|
|
|
return g_strdup_printf ("%s%c%s", home ? home : ".", G_DIR_SEPARATOR,
|
|
|
|
".mu");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-21 19:05:19 +01:00
|
|
|
static gboolean
|
|
|
|
parse_params (MuConfigOptions *config, int *argcp, char ***argvp)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
GOptionContext *context;
|
|
|
|
gboolean rv;
|
|
|
|
|
|
|
|
context = g_option_context_new ("- maildir utilities");
|
|
|
|
|
|
|
|
g_option_context_set_main_group (context,
|
|
|
|
config_options_group_mu (config));
|
|
|
|
g_option_context_add_group (context,
|
|
|
|
config_options_group_index (config));
|
|
|
|
g_option_context_add_group (context,
|
|
|
|
config_options_group_find (config));
|
|
|
|
g_option_context_add_group (context,
|
|
|
|
config_options_group_mkdir (config));
|
|
|
|
|
|
|
|
rv = g_option_context_parse (context, argcp, argvp, &error);
|
|
|
|
if (!rv) {
|
|
|
|
g_printerr ("error in options: %s\n", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
} else
|
|
|
|
g_option_context_free (context);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
mu_config_init (MuConfigOptions *opts, int *argcp, char ***argvp)
|
2010-01-02 13:44:26 +01:00
|
|
|
{
|
2010-01-06 00:24:37 +01:00
|
|
|
gchar *old;
|
2010-01-21 19:05:19 +01:00
|
|
|
|
2010-01-23 23:52:15 +01:00
|
|
|
g_return_val_if_fail (opts, FALSE);
|
2010-01-21 19:05:19 +01:00
|
|
|
memset (opts, 0, sizeof(MuConfigOptions));
|
|
|
|
|
|
|
|
/* set dirmode before, because '0000' is a valid mode */
|
|
|
|
opts->dirmode = 0755;
|
|
|
|
|
2010-01-23 19:50:06 +01:00
|
|
|
if (argcp && argvp)
|
|
|
|
if (!parse_params (opts, argcp, argvp))
|
|
|
|
return FALSE;
|
2010-01-04 19:20:35 +01:00
|
|
|
|
2010-01-02 13:44:26 +01:00
|
|
|
if (!opts->muhome)
|
2010-01-15 21:11:51 +01:00
|
|
|
opts->muhome = guess_muhome ();
|
2009-11-28 13:06:21 +01:00
|
|
|
|
2010-01-06 00:24:37 +01:00
|
|
|
/* note: xpath is is *not* settable from the cmdline */
|
|
|
|
opts->xpath = g_strdup_printf ("%s%c%s", opts->muhome,G_DIR_SEPARATOR,
|
|
|
|
MU_XAPIAN_DIR_NAME);
|
|
|
|
|
|
|
|
/* indexing */
|
|
|
|
old = opts->maildir;
|
2010-01-04 19:20:35 +01:00
|
|
|
if (opts->maildir)
|
|
|
|
opts->maildir = mu_util_dir_expand (opts->maildir);
|
|
|
|
else
|
2010-01-02 13:44:26 +01:00
|
|
|
opts->maildir = mu_util_guess_maildir();
|
2010-01-06 00:24:37 +01:00
|
|
|
g_free (old);
|
2010-01-21 19:05:19 +01:00
|
|
|
|
2009-11-28 13:06:21 +01:00
|
|
|
/* querying */
|
2010-01-02 13:44:26 +01:00
|
|
|
|
|
|
|
/* note, when no fields are specified, we use
|
2010-01-06 00:24:37 +01:00
|
|
|
* date-from-subject, and sort descending by date. If fields
|
2010-01-02 13:44:26 +01:00
|
|
|
* *are* specified, we sort in ascending order. */
|
|
|
|
if (!opts->fields) {
|
|
|
|
opts->descending = TRUE;
|
|
|
|
opts->fields = "d f s";
|
|
|
|
opts->sortfield = "d";
|
|
|
|
}
|
2010-01-02 15:07:07 +01:00
|
|
|
|
|
|
|
if (opts->linksdir) {
|
|
|
|
gchar *old = opts->linksdir;
|
|
|
|
opts->linksdir = mu_util_dir_expand (opts->linksdir);
|
|
|
|
g_free(old);
|
|
|
|
}
|
2009-11-29 23:04:16 +01:00
|
|
|
|
2010-01-21 19:05:19 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
2009-11-29 23:04:16 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
mu_config_uninit (MuConfigOptions *opts)
|
|
|
|
{
|
|
|
|
g_return_if_fail (opts);
|
|
|
|
|
2010-01-01 14:37:39 +01:00
|
|
|
g_free (opts->muhome);
|
2010-01-06 00:24:37 +01:00
|
|
|
g_free (opts->xpath);
|
2010-01-01 14:37:39 +01:00
|
|
|
g_free (opts->maildir);
|
|
|
|
g_free (opts->linksdir);
|
2010-01-01 19:44:19 +01:00
|
|
|
|
2009-12-11 21:04:54 +01:00
|
|
|
g_strfreev (opts->params);
|
2009-11-29 23:04:16 +01:00
|
|
|
}
|
2009-12-31 13:25:20 +01:00
|
|
|
|
2010-01-01 19:44:19 +01:00
|
|
|
|