tools: Simplify the mime-maker container creation.

* tools/mime-maker.c (struct part_s): Remove field MEDIATYPE.
(release_parts): Ditto.
(dump_parts): Print a body line only if tehre is a body.
(mime_maker_add_header): Check for body or container.
(mime_maker_add_container): Remove arg MEDIATYPE.  Change all callers.
(mime_maker_end_container): New.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2016-09-29 10:20:38 +02:00
parent 4ac138c84d
commit 95d60c6ce9
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
4 changed files with 34 additions and 40 deletions

View File

@ -689,7 +689,7 @@ send_confirmation_response (const char *sender, const char *address,
"protocol=\"application/pgp-encrypted\"");
if (err)
goto leave;
err = mime_maker_add_container (mime, "multipart/encrypted");
err = mime_maker_add_container (mime);
if (err)
goto leave;

View File

@ -1007,7 +1007,7 @@ send_confirmation_request (server_ctx_t ctx,
"protocol=\"application/pgp-encrypted\"");
if (err)
goto leave;
err = mime_maker_add_container (mime, "multipart/encrypted");
err = mime_maker_add_container (mime);
if (err)
goto leave;
@ -1214,7 +1214,7 @@ send_congratulation_message (const char *mbox, const char *keyfile)
"protocol=\"application/pgp-encrypted\"");
if (err)
goto leave;
err = mime_maker_add_container (mime, "multipart/encrypted");
err = mime_maker_add_container (mime);
if (err)
goto leave;

View File

@ -48,7 +48,6 @@ struct part_s
{
struct part_s *next; /* Next part in the current container. */
struct part_s *child; /* Child container. */
char *mediatype; /* Mediatype of the container (malloced). */
char *boundary; /* Malloced boundary string. */
header_t headers; /* List of headers. */
header_t *headers_tail;/* Address of last header in chain. */
@ -114,7 +113,6 @@ release_parts (part_t part)
part->headers = hdrnext;
}
release_parts (part->child);
xfree (part->mediatype);
xfree (part->boundary);
xfree (part->body);
xfree (part);
@ -166,7 +164,8 @@ dump_parts (part_t part, int level)
{
log_debug ("%*s%s: %s\n", level*2, "", hdr->name, hdr->value);
}
log_debug ("%*s[body %zu bytes]\n", level*2, "", part->bodylen);
if (part->body)
log_debug ("%*s[body %zu bytes]\n", level*2, "", part->bodylen);
if (part->child)
{
log_debug ("%*s[container]\n", level*2, "");
@ -378,13 +377,13 @@ mime_maker_add_header (mime_maker_t ctx, const char *name, const char *value)
return err;
part = ctx->current_part;
if (part->body && !parent)
if ((part->body || part->child) && !parent)
{
/* We already have a body but no parent. Adding another part is
* thus not possible. */
return gpg_error (GPG_ERR_CONFLICT);
}
if (part->body)
if (part->body || part->child)
{
/* We already have a body and there is a parent. We now append
* a new part to the current container. */
@ -474,61 +473,36 @@ mime_maker_add_stream (mime_maker_t ctx, estream_t *stream_addr)
}
/* Add a new MIME container. The caller needs to provide the media
* and media-subtype in MEDIATYPE. If MEDIATYPE is NULL
* "multipart/mixed" is assumed. This function will then add a
* Content-Type header with that media type and an approriate boundary
* string to the parent part. */
/* Add a new MIME container. A container can be used instead of a
* body. */
gpg_error_t
mime_maker_add_container (mime_maker_t ctx, const char *mediatype)
mime_maker_add_container (mime_maker_t ctx)
{
gpg_error_t err;
part_t part;
if (!mediatype)
mediatype = "multipart/mixed";
err = ensure_part (ctx, NULL);
if (err)
return err;
part = ctx->current_part;
if (part->body)
return gpg_error (GPG_ERR_CONFLICT); /* There is already a body. */
if (part->child || part->mediatype || part->boundary)
if (part->child || part->boundary)
return gpg_error (GPG_ERR_CONFLICT); /* There is already a container. */
/* If a content type has not yet been set, do it now. The boundary
* will be added while writing the headers. */
if (!have_header (ctx->mail, "Content-Type"))
{
err = add_header (ctx->mail, "Content-Type", mediatype);
if (err)
return err;
}
/* Create a child node. */
part->child = xtrycalloc (1, sizeof *part->child);
if (!part->child)
return gpg_error_from_syserror ();
part->child->headers_tail = &part->child->headers;
part->mediatype = xtrystrdup (mediatype);
if (!part->mediatype)
{
err = gpg_error_from_syserror ();
xfree (part->child);
part->child = NULL;
return err;
}
part->boundary = generate_boundary (ctx);
if (!part->boundary)
{
err = gpg_error_from_syserror ();
xfree (part->child);
part->child = NULL;
xfree (part->mediatype);
part->mediatype = NULL;
return err;
}
@ -539,6 +513,25 @@ mime_maker_add_container (mime_maker_t ctx, const char *mediatype)
}
/* Finish the current container. */
gpg_error_t
mime_maker_end_container (mime_maker_t ctx)
{
gpg_error_t err;
part_t parent;
err = ensure_part (ctx, &parent);
if (err)
return err;
if (!parent)
return gpg_error (GPG_ERR_CONFLICT); /* No container. */
while (parent->next)
parent = parent->next;
ctx->current_part = parent;
return 0;
}
/* Write the Content-Type header with the boundary value. */
static gpg_error_t
write_ct_with_boundary (mime_maker_t ctx,
@ -647,7 +640,7 @@ add_missing_headers (mime_maker_t ctx)
}
/* Create message from the tree MIME and write it to FP. Noet that
/* Create message from the tree MIME and write it to FP. Note that
* the output uses only a LF and a later called sendmail(1) is
* expected to convert them to network line endings. */
gpg_error_t

View File

@ -34,7 +34,8 @@ gpg_error_t mime_maker_add_header (mime_maker_t ctx,
const char *name, const char *value);
gpg_error_t mime_maker_add_body (mime_maker_t ctx, const char *string);
gpg_error_t mime_maker_add_stream (mime_maker_t ctx, estream_t *stream_addr);
gpg_error_t mime_maker_add_container (mime_maker_t ctx, const char *mediatype);
gpg_error_t mime_maker_add_container (mime_maker_t ctx);
gpg_error_t mime_maker_end_container (mime_maker_t ctx);
gpg_error_t mime_maker_make (mime_maker_t ctx, estream_t fp);