/
mu-cmd-extract.c
413 lines (328 loc) · 8.63 KB
/
mu-cmd-extract.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
/*
** Copyright (C) 2010-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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.
**
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif /*HAVE_CONFIG_H*/
#include <stdlib.h>
#include <string.h>
#include "mu-msg.h"
#include "mu-msg-part.h"
#include "mu-cmd.h"
#include "mu-util.h"
#include "mu-str.h"
static gboolean
save_part (MuMsg *msg, const char *targetdir, guint partidx, gboolean overwrite,
gboolean play)
{
GError *err;
gchar *filepath;
filepath = mu_msg_part_filepath (msg, targetdir, partidx);
if (!filepath) {
g_warning ("failed to get filepath");
return FALSE;
}
err = NULL;
if (!mu_msg_part_save (msg, filepath, partidx, overwrite, FALSE, &err)) {
if (err) {
g_warning ("failed to save MIME-part: %s",
err->message);
g_error_free (err);
}
g_free (filepath);
return FALSE;
}
if (play)
mu_util_play (filepath, TRUE, FALSE);
g_free (filepath);
return TRUE;
}
static gboolean
save_numbered_parts (MuMsg *msg, MuConfig *opts)
{
gboolean rv;
char **parts, **cur;
parts = g_strsplit (opts->parts, ",", 0);
for (rv = TRUE, cur = parts; cur && *cur; ++cur) {
unsigned idx;
int i;
char *endptr;
idx = (unsigned)(i = strtol (*cur, &endptr, 10));
if (i < 0 || *cur == endptr) {
g_warning ("invalid MIME-part index '%s'", *cur);
rv = FALSE;
break;
}
if (!save_part (msg, opts->targetdir, idx, opts->overwrite,
opts->play)) {
g_warning ("failed to save MIME-part %d", idx);
rv = FALSE;
break;
}
}
g_strfreev (parts);
return rv;
}
static GRegex*
anchored_regex (const char* pattern)
{
GRegex *rx;
GError *err;
gchar *anchored;
anchored = g_strdup_printf
("%s%s%s",
pattern[0] == '^' ? "" : "^",
pattern,
pattern[strlen(pattern)-1] == '$' ? "" : "$");
err = NULL;
rx = g_regex_new (anchored, G_REGEX_CASELESS|G_REGEX_OPTIMIZE, 0,
&err);
g_free (anchored);
if (!rx) {
g_warning ("error in regular expression '%s': %s",
pattern, err->message ? err->message : "error");
g_error_free (err);
return NULL;
}
return rx;
}
static gboolean
save_part_with_filename (MuMsg *msg, const char *pattern, MuConfig *opts)
{
GSList *lst, *cur;
GRegex *rx;
gboolean rv;
/* 'anchor' the pattern with '^...$' if not already */
rx = anchored_regex (pattern);
if (!rx)
return FALSE;
lst = mu_msg_part_find_files (msg, rx);
g_regex_unref (rx);
if (!lst) {
g_warning ("no matching attachments found");
return FALSE;
}
for (cur = lst, rv = TRUE; cur; cur = g_slist_next (cur))
rv &= save_part (msg, opts->targetdir,
GPOINTER_TO_UINT(cur->data),
opts->overwrite, opts->play);
g_slist_free (lst);
return rv;
}
struct _SaveData {
gboolean attachments_only;
gboolean result;
guint saved_num;
const gchar* targetdir;
gboolean overwrite;
gboolean play;
};
typedef struct _SaveData SaveData;
static gboolean
ignore_part (MuMsg *msg, MuMsgPart *part, SaveData *sd)
{
/* something went wrong somewhere; stop */
if (!sd->result)
return TRUE;
/* filter out non-attachments if only want attachments */
if (sd->attachments_only &&
!mu_msg_part_looks_like_attachment (part, TRUE))
return TRUE;
/* ignore multiparts */
if (part->type &&
g_ascii_strcasecmp (part->type, "multipart") == 0)
return TRUE;
return FALSE;
}
static void
save_part_if (MuMsg *msg, MuMsgPart *part, SaveData *sd)
{
gchar *filepath;
gboolean rv;
GError *err;
if (ignore_part (msg, part, sd))
return;
rv = FALSE;
filepath = NULL;
filepath = mu_msg_part_filepath (msg, sd->targetdir, part->index);
if (!filepath)
goto leave;
err = NULL;
if (!mu_msg_part_save (msg, filepath, part->index,
sd->overwrite, FALSE, &err)) {
g_warning ("failed to save MIME-part: %s",
err&&err->message ? err->message : "error");
if (err)
g_error_free (err);
goto leave;
}
if (sd->play && !mu_util_play (filepath, TRUE, FALSE))
goto leave;
rv = TRUE;
++sd->saved_num;
leave:
sd->result = rv;
g_free (filepath);
}
static gboolean
save_certain_parts (MuMsg *msg, gboolean attachments_only,
const gchar *targetdir, gboolean overwrite, gboolean play)
{
SaveData sd;
sd.result = TRUE;
sd.saved_num = 0;
sd.attachments_only = attachments_only;
sd.overwrite = overwrite;
sd.targetdir = targetdir;
sd.play = play;
mu_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, const char *filename, MuConfig *opts)
{
MuMsg* msg;
gboolean rv;
GError *err;
err = NULL;
msg = mu_msg_new_from_file (path, NULL, &err);
if (!msg) {
if (err) {
g_warning ("error: %s", err->message);
g_error_free (err);
}
return FALSE;
}
/* note, mu_cmd_extract already checks whether what's in opts
* is somewhat, so no need for extensive checking here */
/* should we save some explicit parts? */
if (opts->parts)
rv = save_numbered_parts (msg, opts);
else if (filename)
rv = save_part_with_filename (msg, filename, opts);
else if (opts->save_attachments) /* all attachments */
rv = save_certain_parts (msg, TRUE,
opts->targetdir, opts->overwrite,
opts->play);
else if (opts->save_all) /* all parts */
rv = save_certain_parts (msg, FALSE,
opts->targetdir, opts->overwrite,
opts->play);
else
g_assert_not_reached ();
mu_msg_unref (msg);
return rv;
}
#define color_maybe(C) do{ if (color) fputs ((C),stdout);}while(0)
static void
each_part_show (MuMsg *msg, MuMsgPart *part, gboolean color)
{
/* index */
g_print (" %u ", part->index);
/* filename */
color_maybe (MU_COLOR_GREEN);
mu_util_fputs_encoded (part->file_name ? part->file_name : "<none>",
stdout);
/* content-type */
color_maybe (MU_COLOR_BLUE);
mu_util_print_encoded (
" %s/%s ",
part->type ? part->type : "<none>",
part->subtype ? part->subtype : "<none>");
/* disposition */
color_maybe (MU_COLOR_MAGENTA);
mu_util_print_encoded (
"[%s]", part->disposition ? part->disposition : "<none>");
color_maybe (MU_COLOR_DEFAULT);
fputs ("\n", stdout);
}
static gboolean
show_parts (const char* path, MuConfig *opts)
{
MuMsg* msg;
GError *err;
err = NULL;
msg = mu_msg_new_from_file (path, NULL, &err);
if (!msg) {
if (err) {
g_warning ("error: %s", err->message);
g_error_free (err);
}
return FALSE;
}
g_print ("MIME-parts in this message:\n");
mu_msg_part_foreach
(msg,(MuMsgPartForeachFunc)each_part_show,
GUINT_TO_POINTER(opts->color));
mu_msg_unref (msg);
return TRUE;
}
static gboolean
check_params (MuConfig *opts)
{
guint param_num;
param_num = mu_config_param_num(opts);
if (param_num < 2) {
g_warning ("usage: mu extract [options] <file> [<pattern>]");
return FALSE;
}
if (opts->save_attachments || opts->save_all)
if (opts->parts || param_num == 3) {
g_warning ("--save-attachments --save-all don't accept "
"a filename pattern or --parts");
return FALSE;
}
if (opts->save_attachments && opts->save_all) {
g_warning ("only one of --save-attachments and"
" --save-all is allowed");
return FALSE;
}
return TRUE;
}
MuExitCode
mu_cmd_extract (MuConfig *opts)
{
int rv;
g_return_val_if_fail (opts, MU_EXITCODE_ERROR);
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_EXTRACT,
MU_EXITCODE_ERROR);
if (!check_params (opts))
return MU_EXITCODE_ERROR;
if (!opts->params[2] && !opts->parts &&
!opts->save_attachments && !opts->save_all)
rv = show_parts (opts->params[1], opts); /* show, don't save */
else {
rv = mu_util_check_dir(opts->targetdir, FALSE, TRUE);
if (!rv)
g_warning ("target '%s' is not a writable directory",
opts->targetdir);
else
rv = save_parts (opts->params[1],
opts->params[2],
opts); /* save */
}
return rv ? MU_EXITCODE_OK : MU_EXITCODE_ERROR;
}