2010-08-20 20:38:54 +02:00
|
|
|
/*
|
2010-08-24 23:57:16 +02:00
|
|
|
** Copyright (C) 2008-2010 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-08-24 23:57:16 +02:00
|
|
|
#ifdef 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>
|
|
|
|
|
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-msg-str.h"
|
|
|
|
#include "mu-cmd.h"
|
|
|
|
|
2010-08-22 21:50:19 +02:00
|
|
|
static gboolean
|
2010-08-29 21:13:06 +02:00
|
|
|
save_numbered_parts (MuMsg *msg, MuConfigOptions *opts)
|
2010-08-22 21:50:19 +02:00
|
|
|
{
|
2010-08-29 21:13:06 +02:00
|
|
|
gboolean rv;
|
|
|
|
char **parts, **cur;
|
|
|
|
|
|
|
|
parts = g_strsplit (opts->parts, ",", 0);
|
|
|
|
|
|
|
|
for (rv = TRUE, cur = parts; cur && *cur; ++cur) {
|
|
|
|
|
|
|
|
int idx;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
idx = (int)strtol (*cur, &endptr, 10);
|
|
|
|
if (idx < 0 || *cur == endptr) {
|
|
|
|
g_warning ("invalid MIME-part index '%s'", *cur);
|
|
|
|
rv = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-09-01 22:35:34 +02:00
|
|
|
if (!mu_msg_mime_part_save
|
|
|
|
(msg, idx, opts->targetdir, opts->overwrite)) {
|
2010-08-29 21:13:06 +02:00
|
|
|
g_warning ("failed to save MIME-part %d", idx);
|
|
|
|
rv = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev (parts);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct _SaveData {
|
|
|
|
MuMsg *msg;
|
|
|
|
gboolean attachments_only;
|
|
|
|
gboolean result;
|
|
|
|
guint saved_num;
|
2010-09-01 22:35:34 +02:00
|
|
|
const gchar* targetdir;
|
|
|
|
gboolean overwrite;
|
2010-08-29 21:13:06 +02:00
|
|
|
};
|
|
|
|
typedef struct _SaveData SaveData;
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
save_part_if (MuMsgPart *part, SaveData *sd)
|
|
|
|
{
|
|
|
|
/* something went wrong somewhere; stop */
|
|
|
|
if (!sd->result)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* filter out non-attachments if only want attachments. Note,
|
|
|
|
* the attachment check may be a bit too strict */
|
|
|
|
if (sd->attachments_only)
|
|
|
|
if (!part->disposition ||
|
|
|
|
g_ascii_strcasecmp (part->disposition, "attachment") != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* ignore multiparts */
|
|
|
|
if (part->type &&
|
|
|
|
g_ascii_strcasecmp (part->type, "multipart") == 0)
|
|
|
|
return;
|
|
|
|
|
2010-09-01 22:35:34 +02:00
|
|
|
sd->result = mu_msg_mime_part_save (sd->msg, part->index,
|
|
|
|
sd->targetdir, sd->overwrite);
|
2010-08-29 21:13:06 +02:00
|
|
|
if (!sd->result)
|
|
|
|
g_warning ("failed to save MIME-part %u", part->index);
|
|
|
|
else
|
|
|
|
++sd->saved_num;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2010-09-01 22:35:34 +02:00
|
|
|
save_certain_parts (MuMsg *msg, gboolean attachments_only, const gchar *targetdir,
|
|
|
|
gboolean overwrite)
|
2010-08-29 21:13:06 +02:00
|
|
|
{
|
|
|
|
SaveData sd;
|
|
|
|
|
|
|
|
sd.msg = msg;
|
|
|
|
sd.result = TRUE;
|
|
|
|
sd.saved_num = 0;
|
|
|
|
sd.attachments_only = attachments_only;
|
2010-09-01 22:35:34 +02:00
|
|
|
sd.overwrite = overwrite;
|
|
|
|
sd.targetdir = targetdir;
|
2010-08-29 21:13:06 +02:00
|
|
|
|
|
|
|
mu_msg_msg_part_foreach (msg,
|
|
|
|
(MuMsgPartForeachFunc)save_part_if,
|
|
|
|
&sd);
|
|
|
|
|
|
|
|
if (sd.saved_num == 0) {
|
|
|
|
g_warning ("no %s extracted from this message",
|
|
|
|
attachments_only ? "attachments" : "parts");
|
|
|
|
sd.result = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sd.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
save_parts (const char *path, MuConfigOptions *opts)
|
|
|
|
{
|
2010-08-24 23:57:16 +02:00
|
|
|
MuMsg* msg;
|
2010-08-29 21:13:06 +02:00
|
|
|
gboolean rv;
|
2010-08-22 21:50:19 +02:00
|
|
|
|
2010-08-24 23:57:16 +02:00
|
|
|
msg = mu_msg_new (path, NULL);
|
2010-08-22 21:50:19 +02:00
|
|
|
if (!msg)
|
|
|
|
return FALSE;
|
2010-08-29 21:13:06 +02:00
|
|
|
|
|
|
|
/* note, mu_cmd_extract already checks whether what's in opts
|
|
|
|
* is somewhat, so no need for extensive checking here */
|
2010-08-22 21:50:19 +02:00
|
|
|
|
2010-08-29 21:13:06 +02:00
|
|
|
/* should we save some explicit parts? */
|
|
|
|
if (opts->parts)
|
|
|
|
rv = save_numbered_parts (msg, opts);
|
|
|
|
else if (opts->save_attachments) /* all attachments */
|
2010-09-01 22:35:34 +02:00
|
|
|
rv = save_certain_parts (msg, TRUE,
|
|
|
|
opts->targetdir, opts->overwrite);
|
2010-08-29 21:13:06 +02:00
|
|
|
else if (opts->save_all) /* all parts */
|
2010-09-01 22:35:34 +02:00
|
|
|
rv = save_certain_parts (msg, FALSE,
|
|
|
|
opts->targetdir, opts->overwrite);
|
2010-08-29 21:13:06 +02:00
|
|
|
else
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
2010-08-24 23:57:16 +02:00
|
|
|
mu_msg_destroy (msg);
|
2010-08-22 21:50:19 +02:00
|
|
|
|
2010-08-29 21:13:06 +02:00
|
|
|
return rv;
|
2010-08-22 21:50:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-22 18:31:21 +02:00
|
|
|
static void
|
2010-08-29 21:13:06 +02:00
|
|
|
each_part_show (MuMsgPart *part, gpointer user_data)
|
2010-08-22 18:31:21 +02:00
|
|
|
{
|
2010-08-29 21:13:06 +02:00
|
|
|
g_print (" %u %s %s/%s [%s]\n",
|
2010-08-22 18:31:21 +02:00
|
|
|
part->index,
|
|
|
|
part->file_name ? part->file_name : "<none>",
|
|
|
|
part->type ? part->type : "",
|
|
|
|
part->subtype ? part->subtype : "",
|
|
|
|
part->disposition ? part->disposition : "<none>");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-20 20:38:54 +02:00
|
|
|
static gboolean
|
2010-08-29 21:13:06 +02:00
|
|
|
show_parts (const char* path, MuConfigOptions *opts)
|
2010-08-20 20:38:54 +02:00
|
|
|
{
|
2010-08-24 23:57:16 +02:00
|
|
|
MuMsg* msg;
|
2010-08-20 20:38:54 +02:00
|
|
|
|
2010-08-24 23:57:16 +02:00
|
|
|
msg = mu_msg_new (path, NULL);
|
2010-08-20 20:38:54 +02:00
|
|
|
if (!msg)
|
|
|
|
return FALSE;
|
|
|
|
|
2010-08-29 21:13:06 +02:00
|
|
|
g_print ("MIME-parts in this message:\n");
|
|
|
|
mu_msg_msg_part_foreach (msg, each_part_show, NULL);
|
2010-08-22 18:31:21 +02:00
|
|
|
|
2010-08-24 23:57:16 +02:00
|
|
|
mu_msg_destroy (msg);
|
2010-08-20 20:38:54 +02:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
mu_cmd_extract (MuConfigOptions *opts)
|
|
|
|
{
|
|
|
|
gboolean rv;
|
|
|
|
|
|
|
|
g_return_val_if_fail (opts, FALSE);
|
2010-08-29 15:39:27 +02:00
|
|
|
g_return_val_if_fail (mu_cmd_equals (opts, "extract"), FALSE);
|
|
|
|
|
2010-08-29 21:13:06 +02:00
|
|
|
if (!opts->params[1]) {
|
2010-08-24 23:57:16 +02:00
|
|
|
g_warning ("missing mail file to extract something from");
|
2010-08-20 20:38:54 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
2010-08-29 21:13:06 +02:00
|
|
|
|
|
|
|
if (opts->save_attachments && opts->save_all) {
|
|
|
|
g_warning ("only one of --save-attachments and --save-all is allowed");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((opts->save_attachments || opts->save_all) && opts->parts) {
|
|
|
|
g_warning ("with --save-attachments/--save-all, parts should not be specified");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts->parts &&
|
|
|
|
!opts->save_attachments &&
|
|
|
|
!opts->save_all) /* show, don't save */
|
|
|
|
rv = show_parts (opts->params[1], opts);
|
|
|
|
else
|
|
|
|
rv = save_parts (opts->params[1], opts); /* save */
|
2010-08-20 20:38:54 +02:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|