2009-11-25 21:55:06 +01:00
|
|
|
/*
|
2010-01-01 14:56:45 +01:00
|
|
|
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2009-11-25 21:55:06 +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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2009-11-28 19:55:50 +01:00
|
|
|
#include <config.h>
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
#include <glib.h>
|
|
|
|
#include <glib-object.h>
|
|
|
|
#include <string.h>
|
2009-11-29 23:05:05 +01:00
|
|
|
#include <stdio.h> /* for fileno() */
|
2009-11-25 21:55:06 +01:00
|
|
|
|
|
|
|
#include "mu-index.h"
|
|
|
|
#include "mu-query.h"
|
2009-12-31 13:25:20 +01:00
|
|
|
#include "mu-maildir.h"
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
#include "mu-util.h"
|
2009-11-28 13:06:21 +01:00
|
|
|
#include "mu-config.h"
|
2010-01-01 19:44:19 +01:00
|
|
|
#include "mu-cmd.h"
|
2009-11-29 23:05:05 +01:00
|
|
|
#include "mu-log.h"
|
2009-11-25 21:55:06 +01:00
|
|
|
|
|
|
|
#include "mu-msg-gmime.h"
|
|
|
|
|
2009-11-26 19:29:17 +01:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
static MuResult
|
|
|
|
msg_cb (MuIndexStats* stats, void *user_data)
|
|
|
|
{
|
|
|
|
char *kars="-\\|/";
|
2009-12-05 15:38:02 +01:00
|
|
|
char output[100];
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
static int i = 0;
|
2009-12-05 15:38:02 +01:00
|
|
|
static int len = 0;
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2009-12-05 15:38:02 +01:00
|
|
|
while (len --> 0)
|
|
|
|
printf ("\b");
|
|
|
|
|
|
|
|
len = snprintf (output, sizeof(output),
|
|
|
|
"%c mu is indexing your mails; processed: %d; "
|
|
|
|
"updated/new: %d",
|
|
|
|
kars[i % 4], stats->_processed, stats->_updated);
|
|
|
|
g_print ("%s", output);
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
++i;
|
|
|
|
|
|
|
|
return MU_OK;
|
|
|
|
}
|
|
|
|
|
2009-12-31 13:25:20 +01:00
|
|
|
|
2009-11-26 19:29:17 +01:00
|
|
|
static int
|
2009-12-31 13:25:20 +01:00
|
|
|
make_maildir (MuConfigOptions *opts)
|
2009-11-25 21:55:06 +01:00
|
|
|
{
|
2009-12-31 13:25:20 +01:00
|
|
|
int i;
|
2009-12-23 23:39:22 +01:00
|
|
|
|
2009-12-31 13:25:20 +01:00
|
|
|
if (!opts->params[0])
|
|
|
|
return 1; /* shouldn't happen */
|
|
|
|
|
|
|
|
if (!opts->params[1]) {
|
|
|
|
g_printerr ("usage: mu mkdir <dir> [more dirs]\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 1;
|
|
|
|
while (opts->params[i]) {
|
2010-01-01 15:15:41 +01:00
|
|
|
if (!mu_maildir_mkmdir (opts->params[i], 0755, FALSE))
|
2009-12-31 13:25:20 +01:00
|
|
|
return 1;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2009-11-26 19:29:17 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2009-12-31 18:27:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
make_symlink (MuConfigOptions *opts)
|
|
|
|
{
|
|
|
|
if (!opts->params[0])
|
|
|
|
return 1; /* shouldn't happen */
|
|
|
|
|
|
|
|
if (!opts->params[1] || !opts->params[2]) {
|
|
|
|
g_printerr ("usage: mu link <src> <targetdir>\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-01-01 15:15:41 +01:00
|
|
|
return mu_maildir_link (opts->params[1], opts->params[2]) ? 0 : 1;
|
2009-12-31 18:27:41 +01:00
|
|
|
}
|
|
|
|
|
2009-12-31 13:25:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
show_usage (gboolean noerror)
|
|
|
|
{
|
|
|
|
const char* usage=
|
|
|
|
"usage: mu [options] command [parameters]\n"
|
|
|
|
"\twhere command is one of index, query, help\n"
|
|
|
|
"see mu(1) for for information\n";
|
|
|
|
|
|
|
|
if (noerror)
|
|
|
|
g_print ("%s", usage);
|
|
|
|
else
|
|
|
|
g_printerr ("%s", usage);
|
|
|
|
|
|
|
|
return noerror ? 0 : 1;
|
|
|
|
}
|
2009-11-26 19:29:17 +01:00
|
|
|
|
2009-11-28 19:55:50 +01:00
|
|
|
static int
|
|
|
|
show_version (void)
|
|
|
|
{
|
|
|
|
const char* msg =
|
|
|
|
"mu (mail indexer / searcher version) " VERSION "\n\n"
|
2010-01-01 14:56:45 +01:00
|
|
|
"Copyright (C) 2010 Dirk-Jan C. Binnema\n"
|
2009-11-29 21:49:45 +01:00
|
|
|
"License GPLv3+: GNU GPL version 3 or later "
|
2009-11-28 19:55:50 +01:00
|
|
|
"<http://gnu.org/licenses/gpl.html>.\n\n"
|
|
|
|
"This is free software: you are free to change "
|
|
|
|
"and redistribute it.\n"
|
|
|
|
"There is NO WARRANTY, to the extent permitted by law.";
|
|
|
|
|
|
|
|
g_print ("%s\n", msg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-26 19:29:17 +01:00
|
|
|
static int
|
2009-12-31 13:25:20 +01:00
|
|
|
show_help (MuConfigOptions *opts)
|
2009-11-26 19:29:17 +01:00
|
|
|
{
|
2009-12-31 13:25:20 +01:00
|
|
|
/* FIXME: get context-sensitive help */
|
|
|
|
show_version ();
|
2009-12-31 18:27:41 +01:00
|
|
|
return show_usage (FALSE);
|
2009-11-25 21:55:06 +01:00
|
|
|
}
|
2009-11-29 23:05:05 +01:00
|
|
|
|
2010-01-01 19:44:19 +01:00
|
|
|
static int
|
|
|
|
run_index (MuConfigOptions *opts)
|
|
|
|
{
|
|
|
|
MuIndex *midx;
|
|
|
|
MuIndexStats stats;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
midx = mu_index_new (opts->muhome);
|
|
|
|
rv = mu_index_run (midx,
|
|
|
|
opts->maildir,
|
|
|
|
opts->reindex,
|
|
|
|
&stats,
|
|
|
|
opts->quiet ? NULL : msg_cb,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
g_print ("\n");
|
|
|
|
mu_index_destroy (midx);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-31 13:25:20 +01:00
|
|
|
|
2009-11-29 23:05:05 +01:00
|
|
|
static gboolean
|
|
|
|
init_log (MuConfigOptions *opts)
|
|
|
|
{
|
|
|
|
gboolean rv;
|
|
|
|
|
|
|
|
/* TODO: check incompatible options (eg., silent + log_append) */
|
|
|
|
|
|
|
|
if (opts->quiet)
|
|
|
|
rv = mu_log_init_silence ();
|
|
|
|
else if (opts->log_stderr)
|
|
|
|
rv = mu_log_init_with_fd (fileno(stderr), FALSE,
|
|
|
|
opts->debug);
|
2009-12-05 12:13:19 +01:00
|
|
|
else
|
|
|
|
rv = mu_log_init (opts->muhome, opts->log_append,
|
|
|
|
opts->debug);
|
2009-11-29 23:05:05 +01:00
|
|
|
|
2009-12-05 12:13:19 +01:00
|
|
|
if (!rv)
|
|
|
|
g_print ("error: failed to initialize log\n");
|
|
|
|
|
2009-11-29 23:05:05 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
GOptionContext *context;
|
2009-11-28 13:06:21 +01:00
|
|
|
MuConfigOptions config;
|
2009-11-25 21:55:06 +01:00
|
|
|
MuResult rv;
|
2009-11-26 19:29:17 +01:00
|
|
|
MuCmd cmd;
|
2009-12-08 23:54:14 +01:00
|
|
|
gboolean ok;
|
2009-11-25 21:55:06 +01:00
|
|
|
|
|
|
|
g_type_init ();
|
|
|
|
|
2010-01-01 19:44:19 +01:00
|
|
|
context = g_option_context_new ("- maildir utilities");
|
2009-11-28 13:06:21 +01:00
|
|
|
|
|
|
|
g_option_context_set_main_group (context,
|
|
|
|
mu_config_options_group_mu(&config));
|
|
|
|
g_option_context_add_group (context,
|
|
|
|
mu_config_options_group_index(&config));
|
|
|
|
g_option_context_add_group (context,
|
|
|
|
mu_config_options_group_query(&config));
|
|
|
|
|
2009-11-29 23:05:05 +01:00
|
|
|
mu_config_init (&config);
|
2009-12-08 23:54:14 +01:00
|
|
|
ok = g_option_context_parse (context, &argc, &argv, &error);
|
|
|
|
g_option_context_free (context);
|
2010-01-01 19:44:19 +01:00
|
|
|
|
|
|
|
if (!init_log (&config))
|
|
|
|
return 1;
|
|
|
|
|
2009-12-08 23:54:14 +01:00
|
|
|
if (!ok) {
|
2010-01-01 19:44:19 +01:00
|
|
|
g_printerr ("error in options: %s\n", error->message);
|
2009-11-25 21:55:06 +01:00
|
|
|
g_error_free (error);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-11-29 23:05:05 +01:00
|
|
|
|
2009-11-28 19:55:50 +01:00
|
|
|
if (config.version)
|
|
|
|
return show_version ();
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2009-12-11 21:05:50 +01:00
|
|
|
if (!config.params[0]) /* no command? */
|
2009-11-26 19:29:17 +01:00
|
|
|
return show_usage (FALSE);
|
2009-11-28 19:55:50 +01:00
|
|
|
|
2010-01-01 19:44:19 +01:00
|
|
|
cmd = mu_cmd_from_string (config.params[0]);
|
2009-11-26 19:29:17 +01:00
|
|
|
if (cmd == MU_CMD_UNKNOWN)
|
|
|
|
return show_usage (FALSE);
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2010-01-01 19:44:19 +01:00
|
|
|
if (!mu_cmd_check_parameters (cmd, &config))
|
|
|
|
return 1;
|
|
|
|
|
2009-11-26 19:29:17 +01:00
|
|
|
if (cmd == MU_CMD_HELP)
|
2009-12-31 13:25:20 +01:00
|
|
|
return show_help (&config);
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2009-12-31 13:25:20 +01:00
|
|
|
if (cmd == MU_CMD_MKDIR)
|
|
|
|
return make_maildir (&config);
|
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
if (cmd == MU_CMD_LINK)
|
|
|
|
return make_symlink (&config);
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
mu_msg_gmime_init ();
|
|
|
|
rv = MU_OK;
|
2009-11-28 13:06:21 +01:00
|
|
|
|
2010-01-01 19:44:19 +01:00
|
|
|
if (cmd == MU_CMD_INDEX)
|
|
|
|
rv = run_index (&config);
|
|
|
|
else if (cmd == MU_CMD_QUERY)
|
|
|
|
rv = mu_query_run (&config, &config.params[1]);
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2009-12-11 18:43:18 +01:00
|
|
|
mu_msg_gmime_uninit();
|
2009-11-29 23:05:05 +01:00
|
|
|
mu_log_uninit();
|
2009-12-11 18:43:18 +01:00
|
|
|
mu_config_uninit(&config);
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
return rv == MU_OK ? 0 : 1;
|
|
|
|
}
|
2009-12-11 21:05:50 +01:00
|
|
|
|