Commit 96dd35f0 96dd35f02fe175507d5ff91ccdcc2c59ff5a24fd by Sergey Poznyakoff

mail: new option --skip-empty-attachments

* libmailutils/cli/cli.c (mu_cli): Use "no-" prefix to indicate negation
* mail/mail.c (skip_empty_attachments): New global.
(mail_options): New option --skip-empty-attachments
* mail/mail.h (skip_empty_attachments): New extern.
* mail/send.c (atchinfo) <skip_empty>: New field.
(send_attach_file): Initialize skip_empty
(saveatt): Optionally skip empty attachments

* NEWS: Mention the new option.
* doc/texinfo/programs.texi: Document the new option.
1 parent 888e2a9a
GNU mailutils NEWS -- history of user-visible changes. 2017-01-13
GNU mailutils NEWS -- history of user-visible changes. 2017-01-16
Copyright (C) 2002-2017 Free Software Foundation, Inc.
See the end of file for copying conditions.
......@@ -40,6 +40,9 @@ obvious reasons, the interactive mode is suppressed in this case.
The `--attach-fd' option is useful when calling `mail' from another
program.
The new option `--skip-empty-attachments' instructs `mail' to omit
attachments that have zero-size body.
Example:
Suppose that the 'mail' binary is opened at file descriptor 5 and
......
......@@ -3044,6 +3044,13 @@ Cause interrupts to terminate program.
Sets the return email address for outgoing mail.
@xref{return-address}.
@item --skip-empty-attachments
@itemx --no-skip-empty-attachments
Don't create attachments that would have zero-size body. This
option affects all attachments created by @option{--attach} and
@option{--attach-fd} options appearing after it in the command line.
To cancel its effect, use the @option{--no-skip-empty-attachments} option.
@item -s @var{subj}
@itemx --subject=@var{subj}
Send a message with a Subject of @var{subj}. Valid only in sending
......@@ -3531,6 +3538,12 @@ Attachments created with this option have neither filename nor
description set, so normally the use of @option{--content-name} and/or
@option{--content-filename} is advised.
The option @option{--skip-empty-attachments} instructs @command{mail}
to skip creating attachments that would have zero-size body. This
option affects all attachments created by @option{--attach} and
@option{--attach-fd} options appearing after it in the command line.
To cancel its effect, use the @option{--no-skip-empty-attachments} option.
The following Perl program serves as an example of using
@command{mail} from a script to construct a MIME message on the fly.
It scans all mounted file systems for executable files that have
......
......@@ -710,6 +710,9 @@ mu_cli (int argc, char **argv, struct mu_cli_setup *setup, char **capa,
pohint.po_version_hook = mu_version_hook;
pohint.po_flags |= MU_PARSEOPT_VERSION_HOOK;
pohint.po_negation = "no-";
pohint.po_flags |= MU_PARSEOPT_NEGATION;
cfhint.site_file = mu_site_config_file ();
cfhint.flags = MU_CFHINT_SITE_FILE | MU_CFHINT_PER_USER_FILE;
......
......@@ -36,10 +36,11 @@ int hint;
char *file;
char *user;
int skip_empty_attachments;
char *default_encoding;
char *default_content_type;
char *content_name;
char *content_filename;
static char *content_name;
static char *content_filename;
static void
cli_f_option (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
......@@ -108,14 +109,12 @@ cli_command_option (struct mu_parseopt *po, struct mu_option *opt,
break;
case 0:
mu_parseopt_error (po,
_("--%s: option should have been recognized"),
mu_parseopt_error (po, _("--%s: option should have been recognized"),
opt->opt_long);
exit (po->po_exit_error);
default:
mu_parseopt_error (po,
_("-%c: option should have been recognized"),
mu_parseopt_error (po, _("-%c: option should have been recognized"),
opt->opt_short);
exit (po->po_exit_error);
}
......@@ -240,6 +239,10 @@ static struct mu_option mail_options[] = {
{ "append", 'a', N_("HEADER: VALUE"), MU_OPTION_DEFAULT,
N_("append given header to the message being sent"),
mu_c_string, NULL, cli_append },
{ "skip-empty-attachments", 0, NULL, MU_OPTION_DEFAULT,
N_("skip attachments with empty body"),
mu_c_bool, &skip_empty_attachments },
{ "exec" , 'E', N_("COMMAND"), MU_OPTION_DEFAULT,
N_("execute COMMAND"),
......
......@@ -173,7 +173,8 @@ extern int interactive;
extern const char *program_version;
extern char *default_encoding;
extern char *default_content_type;
extern int skip_empty_attachments;
/* Functions */
extern int mail_alias (int argc, char **argv);
extern int mail_alt (int argc, char **argv); /* command alternates */
......
......@@ -140,6 +140,7 @@ struct atchinfo
char *name;
char *filename;
mu_stream_t source;
int skip_empty;
};
static mu_list_t attlist;
......@@ -256,6 +257,7 @@ send_attach_file (int fd,
aptr->name = content_name ? mu_strdup (content_name) : NULL;
aptr->filename = content_filename ? mu_strdup (content_filename) : NULL;
aptr->source = stream;
aptr->skip_empty = skip_empty_attachments;
rc = mu_list_append (attlist, aptr);
if (rc)
{
......@@ -363,11 +365,31 @@ saveatt (void *item, void *data)
rc = mu_attachment_copy_from_stream (part, aptr->source, aptr->encoding);
if (rc)
{
mu_error (_("cannot attach %s: %s"), aptr->filename,
mu_strerror (rc));
mu_error (_("cannot attach %s: %s"), aptr->id, mu_strerror (rc));
return 1;
}
if (aptr->skip_empty)
{
mu_body_t body;
size_t size;
rc = mu_message_get_body (part, &body);
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_message_get_body", aptr->id, rc);
return 1;
}
rc = mu_body_size (body, &size);
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_body_size", aptr->id, rc);
return 1;
}
if (size == 0)
return 0;
}
mu_mime_get_num_parts (mime, &nparts);
mu_message_get_header (part, &hdr);
mu_rfc2822_msg_id (nparts, &p);
......