2011-05-22 16:39:10 +02:00
|
|
|
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
|
2010-08-20 20:38:54 +02:00
|
|
|
/*
|
2011-01-12 22:09:09 +01:00
|
|
|
** Copyright (C) 2010-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2010-08-20 20:38:54 +02: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-11-30 08:02:29 +01:00
|
|
|
#if HAVE_CONFIG_H
|
2010-08-20 20:38:54 +02:00
|
|
|
#include "config.h"
|
2010-08-24 23:57:16 +02:00
|
|
|
#endif /*HAVE_CONFIG_H*/
|
2010-08-20 20:38:54 +02:00
|
|
|
|
2010-08-22 21:50:19 +02:00
|
|
|
#include <stdlib.h>
|
2011-05-25 21:02:24 +02:00
|
|
|
#include <stdio.h>
|
2011-08-02 07:14:19 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2010-08-22 21:50:19 +02:00
|
|
|
|
2010-08-24 23:57:16 +02:00
|
|
|
#include "mu-msg.h"
|
|
|
|
#include "mu-msg-part.h"
|
2010-08-20 20:38:54 +02:00
|
|
|
#include "mu-cmd.h"
|
2010-10-17 10:20:02 +02:00
|
|
|
#include "mu-util.h"
|
2011-01-05 19:38:33 +01:00
|
|
|
#include "mu-str.h"
|
2011-07-20 23:31:17 +02:00
|
|
|
#include "mu-date.h"
|
2011-01-05 19:38:33 +01:00
|
|
|
#include "mu-maildir.h"
|
2011-03-02 22:26:54 +01:00
|
|
|
#include "mu-contacts.h"
|
|
|
|
#include "mu-runtime.h"
|
2011-08-11 21:44:23 +02:00
|
|
|
#include "mu-flags.h"
|
2011-08-02 20:27:32 +02:00
|
|
|
#include "mu-store.h"
|
2011-01-05 19:38:33 +01:00
|
|
|
|
2011-07-07 21:59:44 +02:00
|
|
|
#define VIEW_TERMINATOR '\f' /* form-feed */
|
2011-06-13 23:08:13 +02:00
|
|
|
|
2011-05-21 19:20:38 +02:00
|
|
|
|
2011-07-31 11:17:14 +02:00
|
|
|
static gboolean
|
|
|
|
view_msg_sexp (MuMsg *msg)
|
|
|
|
{
|
2011-08-29 22:38:55 +02:00
|
|
|
char *sexp;
|
|
|
|
|
|
|
|
sexp = mu_msg_to_sexp (msg, NULL, FALSE);
|
|
|
|
fputs (sexp, stdout);
|
|
|
|
g_free (sexp);
|
|
|
|
|
2011-07-31 11:17:14 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-21 19:20:38 +02:00
|
|
|
static void
|
|
|
|
each_part (MuMsg *msg, MuMsgPart *part, gchar **attach)
|
|
|
|
{
|
|
|
|
if (mu_msg_part_looks_like_attachment (part, TRUE) &&
|
|
|
|
(part->file_name)) {
|
|
|
|
|
|
|
|
char *tmp = *attach;
|
|
|
|
|
|
|
|
*attach = g_strdup_printf ("%s%s'%s'",
|
|
|
|
*attach ? *attach : "",
|
|
|
|
*attach ? ", " : "",
|
|
|
|
part->file_name);
|
|
|
|
g_free (tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return comma-sep'd list of attachments */
|
2011-05-22 16:39:10 +02:00
|
|
|
static gchar *
|
2011-05-21 19:20:38 +02:00
|
|
|
get_attach_str (MuMsg *msg)
|
|
|
|
{
|
|
|
|
gchar *attach;
|
|
|
|
|
|
|
|
attach = NULL;
|
|
|
|
mu_msg_part_foreach (msg, (MuMsgPartForeachFunc)each_part, &attach);
|
|
|
|
|
|
|
|
return attach;
|
2011-08-29 22:38:55 +02:00
|
|
|
}
|
2011-05-21 19:20:38 +02:00
|
|
|
|
2011-06-02 10:09:04 +02:00
|
|
|
#define color_maybe(C) do{ if (color) fputs ((C),stdout);}while(0)
|
2011-05-21 19:20:38 +02:00
|
|
|
|
2011-05-25 21:02:24 +02:00
|
|
|
static void
|
|
|
|
print_field (const char* field, const char *val, gboolean color)
|
|
|
|
{
|
|
|
|
if (!val)
|
|
|
|
return;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-06-02 10:09:04 +02:00
|
|
|
color_maybe (MU_COLOR_MAGENTA);
|
|
|
|
mu_util_fputs_encoded (field, stdout);
|
|
|
|
color_maybe (MU_COLOR_DEFAULT);
|
|
|
|
fputs (": ", stdout);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-06-02 10:09:04 +02:00
|
|
|
if (val) {
|
|
|
|
color_maybe (MU_COLOR_GREEN);
|
|
|
|
mu_util_fputs_encoded (val, stdout);
|
|
|
|
}
|
2011-05-25 21:02:24 +02:00
|
|
|
|
2011-06-02 10:09:04 +02:00
|
|
|
color_maybe (MU_COLOR_DEFAULT);
|
|
|
|
fputs ("\n", stdout);
|
2011-05-25 21:02:24 +02:00
|
|
|
}
|
|
|
|
|
2011-05-21 19:20:38 +02:00
|
|
|
|
2011-06-19 20:24:08 +02:00
|
|
|
static void
|
|
|
|
body_or_summary (MuMsg *msg, gboolean summary, gboolean color)
|
|
|
|
{
|
|
|
|
const char* field;
|
|
|
|
const int SUMMARY_LEN = 5;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-06-19 20:24:08 +02:00
|
|
|
field = mu_msg_get_body_text (msg);
|
|
|
|
if (!field)
|
|
|
|
return; /* no body -- nothing more to do */
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-06-19 20:24:08 +02:00
|
|
|
if (summary) {
|
|
|
|
gchar *summ;
|
|
|
|
summ = mu_str_summarize (field, SUMMARY_LEN);
|
|
|
|
print_field ("Summary", summ, color);
|
|
|
|
g_free (summ);
|
|
|
|
} else {
|
|
|
|
color_maybe (MU_COLOR_YELLOW);
|
|
|
|
mu_util_print_encoded ("\n%s\n", field);
|
|
|
|
color_maybe (MU_COLOR_DEFAULT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-05 19:38:33 +01:00
|
|
|
/* we ignore fields for now */
|
|
|
|
static gboolean
|
2011-07-31 11:17:14 +02:00
|
|
|
view_msg_plain (MuMsg *msg, const gchar *fields, gboolean summary,
|
2011-05-25 21:02:24 +02:00
|
|
|
gboolean color)
|
2011-01-05 19:38:33 +01:00
|
|
|
{
|
2011-05-21 19:20:38 +02:00
|
|
|
gchar *attachs;
|
2011-01-05 19:38:33 +01:00
|
|
|
time_t date;
|
2011-06-16 07:14:07 +02:00
|
|
|
const GSList *lst;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-05-26 22:38:30 +02:00
|
|
|
print_field ("From", mu_msg_get_from (msg), color);
|
|
|
|
print_field ("To", mu_msg_get_to (msg), color);
|
|
|
|
print_field ("Cc", mu_msg_get_cc (msg), color);
|
|
|
|
print_field ("Bcc", mu_msg_get_bcc (msg), color);
|
|
|
|
print_field ("Subject", mu_msg_get_subject (msg), color);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
|
|
|
if ((date = mu_msg_get_date (msg)))
|
2011-07-20 23:31:17 +02:00
|
|
|
print_field ("Date", mu_date_str_s ("%c", date),
|
2011-05-25 21:02:24 +02:00
|
|
|
color);
|
2011-01-05 19:38:33 +01:00
|
|
|
|
2011-06-16 07:14:07 +02:00
|
|
|
if ((lst = mu_msg_get_tags (msg))) {
|
|
|
|
gchar *tags;
|
|
|
|
tags = mu_str_from_list (lst,',');
|
|
|
|
print_field ("Tags", tags, color);
|
|
|
|
g_free (tags);
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-05-21 19:20:38 +02:00
|
|
|
if ((attachs = get_attach_str (msg))) {
|
2011-05-26 22:38:30 +02:00
|
|
|
print_field ("Attachments", attachs, color);
|
2011-05-21 19:20:38 +02:00
|
|
|
g_free (attachs);
|
|
|
|
}
|
2011-06-19 20:24:08 +02:00
|
|
|
|
|
|
|
body_or_summary (msg, summary, color);
|
|
|
|
|
2011-01-05 19:38:33 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-06-13 23:08:13 +02:00
|
|
|
|
2011-08-09 06:52:55 +02:00
|
|
|
static gboolean
|
2011-08-11 19:20:22 +02:00
|
|
|
handle_msg (const char *fname, MuConfig *opts, MuError *code)
|
2011-06-13 23:08:13 +02:00
|
|
|
{
|
|
|
|
GError *err;
|
|
|
|
MuMsg *msg;
|
2011-08-09 06:52:55 +02:00
|
|
|
gboolean rv;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-06-13 23:08:13 +02:00
|
|
|
err = NULL;
|
|
|
|
msg = mu_msg_new_from_file (fname, NULL, &err);
|
|
|
|
|
|
|
|
if (!msg) {
|
2011-08-29 22:38:55 +02:00
|
|
|
if (err && err->message) {
|
|
|
|
g_warning ("%s", err->message);
|
|
|
|
g_error_free (err);
|
|
|
|
}
|
2011-08-11 19:20:22 +02:00
|
|
|
*code = MU_ERROR;
|
2011-08-09 06:52:55 +02:00
|
|
|
return FALSE;
|
2011-06-13 23:08:13 +02:00
|
|
|
}
|
2011-07-31 11:17:14 +02:00
|
|
|
|
|
|
|
switch (opts->format) {
|
|
|
|
case MU_CONFIG_FORMAT_PLAIN:
|
|
|
|
rv = view_msg_plain (msg, NULL, opts->summary, opts->color);
|
|
|
|
break;
|
|
|
|
case MU_CONFIG_FORMAT_SEXP:
|
|
|
|
rv = view_msg_sexp (msg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_critical ("bug: should not be reached");
|
2011-08-11 19:20:22 +02:00
|
|
|
*code = MU_ERROR_INTERNAL;
|
2011-08-09 06:52:55 +02:00
|
|
|
rv = FALSE;
|
2011-07-31 11:17:14 +02:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-06-13 23:08:13 +02:00
|
|
|
mu_msg_unref (msg);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:20:06 +02:00
|
|
|
static gboolean
|
|
|
|
view_params_valid (MuConfig *opts)
|
|
|
|
{
|
|
|
|
/* note: params[0] will be 'view' */
|
|
|
|
if (!opts->params[0] || !opts->params[1]) {
|
|
|
|
g_warning ("usage: mu view [options] <file> [<files>]");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-07-27 07:20:06 +02:00
|
|
|
switch (opts->format) {
|
|
|
|
case MU_CONFIG_FORMAT_PLAIN:
|
|
|
|
case MU_CONFIG_FORMAT_SEXP:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_warning ("invalid output format %s",
|
|
|
|
opts->formatstr ? opts->formatstr : "<none>");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-07-27 07:20:06 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-06-13 23:08:13 +02:00
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
MuError
|
2011-01-05 19:38:33 +01:00
|
|
|
mu_cmd_view (MuConfig *opts)
|
|
|
|
{
|
2011-08-09 06:52:55 +02:00
|
|
|
int i;
|
|
|
|
gboolean rv;
|
2011-08-11 19:20:22 +02:00
|
|
|
MuError code;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
g_return_val_if_fail (opts, MU_ERROR_INTERNAL);
|
2011-01-05 19:38:33 +01:00
|
|
|
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_VIEW,
|
2011-08-11 19:20:22 +02:00
|
|
|
MU_ERROR_INTERNAL);
|
2011-07-27 07:20:06 +02:00
|
|
|
|
|
|
|
if (!view_params_valid(opts))
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_ERROR_IN_PARAMETERS;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
for (i = 1, code = MU_OK; opts->params[i]; ++i) {
|
2011-06-13 23:08:13 +02:00
|
|
|
|
2011-08-09 06:52:55 +02:00
|
|
|
rv = handle_msg (opts->params[i], opts, &code);
|
|
|
|
if (!rv)
|
2011-06-13 23:08:13 +02:00
|
|
|
break;
|
|
|
|
/* add a separator between two messages? */
|
2011-07-07 21:59:44 +02:00
|
|
|
if (opts->terminator)
|
|
|
|
g_print ("%c", VIEW_TERMINATOR);
|
2011-01-05 19:38:33 +01:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-09 06:52:55 +02:00
|
|
|
return code;
|
2011-01-05 19:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
MuError
|
2011-01-05 19:38:33 +01:00
|
|
|
mu_cmd_mkdir (MuConfig *opts)
|
|
|
|
{
|
|
|
|
int i;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
g_return_val_if_fail (opts, MU_ERROR_INTERNAL);
|
2011-01-05 19:38:33 +01:00
|
|
|
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_MKDIR,
|
2011-08-11 19:20:22 +02:00
|
|
|
MU_ERROR_INTERNAL);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-05 19:38:33 +01:00
|
|
|
if (!opts->params[1]) {
|
|
|
|
g_warning ("usage: mu mkdir [-u,--mode=<mode>] "
|
|
|
|
"<dir> [more dirs]");
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_ERROR_IN_PARAMETERS;
|
2011-01-05 19:38:33 +01:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-05 19:38:33 +01:00
|
|
|
for (i = 1; opts->params[i]; ++i) {
|
2011-01-12 22:09:09 +01:00
|
|
|
|
|
|
|
GError *err;
|
|
|
|
err = NULL;
|
|
|
|
|
2011-01-05 19:38:33 +01:00
|
|
|
if (!mu_maildir_mkdir (opts->params[i], opts->dirmode,
|
2011-01-06 12:14:42 +01:00
|
|
|
FALSE, &err)) {
|
2011-01-05 19:38:33 +01:00
|
|
|
if (err && err->message) {
|
|
|
|
g_warning ("%s", err->message);
|
|
|
|
g_error_free (err);
|
|
|
|
}
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_ERROR;
|
2011-01-06 12:14:42 +01:00
|
|
|
}
|
2011-01-05 19:38:33 +01:00
|
|
|
}
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_OK;
|
2011-01-05 19:38:33 +01:00
|
|
|
}
|
2011-03-02 22:26:54 +01:00
|
|
|
|
2011-08-01 21:42:23 +02:00
|
|
|
|
2011-08-02 07:14:19 +02:00
|
|
|
static gboolean
|
2011-08-11 21:44:23 +02:00
|
|
|
mv_check_params (MuConfig *opts, MuFlags *flags)
|
2011-08-02 07:14:19 +02:00
|
|
|
{
|
2011-08-16 22:42:47 +02:00
|
|
|
if (!opts->params[1]) {
|
2011-08-13 01:00:35 +02:00
|
|
|
g_warning ("usage: mu mv [--flags=<flags>] <mailfile> "
|
2011-08-16 22:42:47 +02:00
|
|
|
"[<maildir>]");
|
2011-08-02 07:14:19 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
/* FIXME: check for invalid flags */
|
2011-08-02 07:14:19 +02:00
|
|
|
if (!opts->flagstr)
|
2011-08-11 21:44:23 +02:00
|
|
|
*flags = MU_FLAG_INVALID; /* ie., ignore flags */
|
2011-08-16 22:42:47 +02:00
|
|
|
else {
|
|
|
|
/* if there's a '+' or '-' sign in the string, it must
|
|
|
|
* be a flag-delta */
|
|
|
|
if (strstr (opts->flagstr, "+") || strstr (opts->flagstr, "-")) {
|
|
|
|
MuFlags oldflags;
|
|
|
|
oldflags = mu_maildir_get_flags_from_path (opts->params[1]);
|
|
|
|
*flags = mu_flags_from_str_delta (opts->flagstr,
|
|
|
|
oldflags,
|
|
|
|
MU_FLAG_TYPE_MAILDIR|
|
|
|
|
MU_FLAG_TYPE_MAILFILE);
|
|
|
|
} else
|
|
|
|
*flags = mu_flags_from_str (opts->flagstr,
|
|
|
|
MU_FLAG_TYPE_MAILDIR |
|
|
|
|
MU_FLAG_TYPE_MAILFILE);
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-02 07:14:19 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
static MuError
|
2011-08-10 22:57:34 +02:00
|
|
|
cmd_mv_dev_null (MuConfig *opts)
|
|
|
|
{
|
|
|
|
if (unlink (opts->params[1]) != 0) {
|
|
|
|
g_warning ("unlink failed: %s", strerror (errno));
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_ERROR_FILE;
|
2011-08-10 22:57:34 +02:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-16 22:42:47 +02:00
|
|
|
if (opts->print_target)
|
2011-08-11 07:22:24 +02:00
|
|
|
g_print ("/dev/null\n"); /* /dev/null */
|
2011-08-10 22:57:34 +02:00
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_OK;
|
2011-08-10 22:57:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
MuError
|
2011-08-03 22:00:48 +02:00
|
|
|
mu_cmd_mv (MuConfig *opts)
|
|
|
|
{
|
|
|
|
GError *err;
|
|
|
|
gchar *fullpath;
|
2011-08-11 21:44:23 +02:00
|
|
|
MuFlags flags;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-03 22:00:48 +02:00
|
|
|
if (!mv_check_params (opts, &flags))
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_ERROR_IN_PARAMETERS;
|
2011-08-03 22:00:48 +02:00
|
|
|
|
|
|
|
/* special case: /dev/null */
|
2011-08-10 22:57:34 +02:00
|
|
|
if (g_strcmp0 (opts->params[2], "/dev/null") == 0)
|
|
|
|
return cmd_mv_dev_null (opts);
|
2011-08-03 22:00:48 +02:00
|
|
|
|
|
|
|
err = NULL;
|
2011-08-13 01:00:35 +02:00
|
|
|
fullpath = mu_maildir_move_message (opts->params[1],
|
|
|
|
opts->params[2],
|
2011-08-16 22:42:47 +02:00
|
|
|
flags, opts->ignore_dups,
|
|
|
|
&err);
|
2011-08-03 22:00:48 +02:00
|
|
|
if (!fullpath) {
|
|
|
|
if (err) {
|
2011-08-11 19:20:22 +02:00
|
|
|
MuError code;
|
|
|
|
code = err->code;
|
2011-08-03 22:00:48 +02:00
|
|
|
g_warning ("move failed: %s", err->message);
|
|
|
|
g_error_free (err);
|
2011-08-11 19:20:22 +02:00
|
|
|
return code;
|
2011-08-03 22:00:48 +02:00
|
|
|
}
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_ERROR_FILE;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-05 07:20:26 +02:00
|
|
|
} else {
|
2011-08-29 22:38:55 +02:00
|
|
|
if (opts->print_target)
|
|
|
|
g_print ("%s\n", fullpath);
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_OK;
|
2011-08-05 07:20:26 +02:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-03 22:00:48 +02:00
|
|
|
g_free (fullpath);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_OK;
|
2011-08-09 06:52:55 +02:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
|
|
|
|
2011-08-02 20:27:32 +02:00
|
|
|
static gboolean
|
2011-08-03 22:00:48 +02:00
|
|
|
check_file_okay (const char *path, gboolean cmd_add)
|
|
|
|
{
|
|
|
|
if (!g_path_is_absolute (path)) {
|
|
|
|
g_warning ("path is not absolute: %s", path);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-11 07:22:24 +02:00
|
|
|
if (cmd_add && access(path, R_OK) != 0) {
|
2011-08-03 22:00:48 +02:00
|
|
|
g_warning ("path is not readable: %s: %s",
|
|
|
|
path, strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-03 22:00:48 +02:00
|
|
|
|
2011-08-10 22:57:34 +02:00
|
|
|
static MuStore*
|
|
|
|
get_store (void)
|
2011-08-02 20:27:32 +02:00
|
|
|
{
|
|
|
|
MuStore *store;
|
|
|
|
GError *err;
|
2011-08-10 22:57:34 +02:00
|
|
|
|
2011-08-02 20:27:32 +02:00
|
|
|
err = NULL;
|
2011-08-29 22:38:55 +02:00
|
|
|
store = mu_store_new_writable
|
|
|
|
(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB),
|
|
|
|
mu_runtime_path(MU_RUNTIME_PATH_CONTACTS),
|
|
|
|
&err);
|
2011-08-02 20:27:32 +02:00
|
|
|
if (!store) {
|
|
|
|
if (err) {
|
|
|
|
g_warning ("store error: %s", err->message);
|
|
|
|
g_error_free (err);
|
|
|
|
} else
|
|
|
|
g_warning ("failed to create store object");
|
|
|
|
}
|
|
|
|
|
2011-08-10 22:57:34 +02:00
|
|
|
return store;
|
2011-08-02 20:27:32 +02:00
|
|
|
}
|
|
|
|
|
2011-08-03 22:00:48 +02:00
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
MuError
|
2011-08-03 22:00:48 +02:00
|
|
|
mu_cmd_add (MuConfig *opts)
|
2011-08-02 20:27:32 +02:00
|
|
|
{
|
2011-08-10 22:57:34 +02:00
|
|
|
MuStore *store;
|
|
|
|
gboolean allok;
|
|
|
|
int i;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
g_return_val_if_fail (opts, MU_ERROR_INTERNAL);
|
2011-08-03 22:00:48 +02:00
|
|
|
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_ADD,
|
2011-08-11 19:20:22 +02:00
|
|
|
MU_ERROR_INTERNAL);
|
2011-08-03 22:00:48 +02:00
|
|
|
|
|
|
|
/* note: params[0] will be 'add' */
|
|
|
|
if (!opts->params[0] || !opts->params[1]) {
|
|
|
|
g_warning ("usage: mu add <file> [<files>]");
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_ERROR_IN_PARAMETERS;
|
2011-08-02 20:27:32 +02:00
|
|
|
}
|
|
|
|
|
2011-08-10 22:57:34 +02:00
|
|
|
store = get_store ();
|
|
|
|
if (!store)
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_ERROR_INTERNAL;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-10 22:57:34 +02:00
|
|
|
for (i = 1, allok = TRUE; opts->params[i]; ++i) {
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-10 22:57:34 +02:00
|
|
|
const char* src;
|
|
|
|
src = opts->params[i];
|
|
|
|
|
|
|
|
if (!check_file_okay (src, TRUE)) {
|
|
|
|
allok = FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-10 22:57:34 +02:00
|
|
|
if (!mu_store_store_path (store, src)) {
|
|
|
|
allok = FALSE;
|
|
|
|
g_warning ("failed to store %s", src);
|
|
|
|
}
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-10 22:57:34 +02:00
|
|
|
mu_store_destroy (store);
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
return allok ? MU_OK : MU_ERROR;
|
2011-08-02 20:27:32 +02:00
|
|
|
}
|
|
|
|
|
2011-08-01 21:42:23 +02:00
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
MuError
|
2011-08-03 22:00:48 +02:00
|
|
|
mu_cmd_remove (MuConfig *opts)
|
2011-08-10 22:57:34 +02:00
|
|
|
{
|
|
|
|
MuStore *store;
|
|
|
|
gboolean allok;
|
|
|
|
int i;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
g_return_val_if_fail (opts, MU_ERROR_INTERNAL);
|
2011-08-03 22:00:48 +02:00
|
|
|
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_REMOVE,
|
2011-08-11 19:20:22 +02:00
|
|
|
MU_ERROR_INTERNAL);
|
2011-08-02 20:27:32 +02:00
|
|
|
|
2011-08-03 22:00:48 +02:00
|
|
|
/* note: params[0] will be 'add' */
|
|
|
|
if (!opts->params[0] || !opts->params[1]) {
|
|
|
|
g_warning ("usage: mu remove <file> [<files>]");
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_ERROR_IN_PARAMETERS;
|
2011-08-03 22:00:48 +02:00
|
|
|
}
|
2011-08-02 20:27:32 +02:00
|
|
|
|
2011-08-10 22:57:34 +02:00
|
|
|
store = get_store ();
|
|
|
|
if (!store)
|
2011-08-11 19:20:22 +02:00
|
|
|
return MU_ERROR_INTERNAL;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-10 22:57:34 +02:00
|
|
|
for (i = 1, allok = TRUE; opts->params[i]; ++i) {
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-10 22:57:34 +02:00
|
|
|
const char* src;
|
|
|
|
src = opts->params[i];
|
|
|
|
|
|
|
|
if (!check_file_okay (src, FALSE)) {
|
|
|
|
allok = FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-10 22:57:34 +02:00
|
|
|
if (!mu_store_remove_path (store, src)) {
|
|
|
|
allok = FALSE;
|
|
|
|
g_warning ("failed to remove %s", src);
|
|
|
|
}
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-10 22:57:34 +02:00
|
|
|
mu_store_destroy (store);
|
|
|
|
|
2011-08-11 19:20:22 +02:00
|
|
|
return allok ? MU_OK : MU_ERROR;
|
2011-08-01 21:42:23 +02:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
|