static ssize_t mime_part_write_to_stream (GMimeObject *object, GMimeStream *stream) { GMimePart *mime_part = (GMimePart *) object; ssize_t nwritten, total = 0; /* write the content headers */ if ((nwritten = g_mime_header_list_write_to_stream (object->headers, stream)) == -1) return -1; total += nwritten; /* terminate the headers */ if (g_mime_stream_write (stream, "\n", 1) == -1) return -1; total++; if ((nwritten = write_content (mime_part, stream)) == -1) return -1; total += nwritten; return total; }
/** * g_mime_header_list_to_string: * @headers: a #GMimeHeaderList * * Allocates a string buffer containing the raw rfc822 headers * contained in @headers. * * Returns: a string containing the header block. **/ char * g_mime_header_list_to_string (const GMimeHeaderList *headers) { GMimeStream *stream; GByteArray *array; char *str; g_return_val_if_fail (headers != NULL, NULL); array = g_byte_array_new (); stream = g_mime_stream_mem_new (); g_mime_stream_mem_set_byte_array (GMIME_STREAM_MEM (stream), array); if (headers->stream) { g_mime_stream_reset (headers->stream); g_mime_stream_write_to_stream (headers->stream, stream); } else { g_mime_header_list_write_to_stream (headers, stream); } g_object_unref (stream); g_byte_array_append (array, (unsigned char *) "", 1); str = (char *) array->data; g_byte_array_free (array, FALSE); return str; }
static ssize_t message_part_write_to_stream (GMimeObject *object, GMimeFormatOptions *options, gboolean content_only, GMimeStream *stream) { GMimeMessagePart *part = (GMimeMessagePart *) object; GMimeMessage *message = part->message; ssize_t nwritten, total = 0; const char *newline, *eoln; gboolean match; size_t len; newline = g_mime_format_options_get_newline (options); if (!content_only) { /* write the content headers */ if ((nwritten = g_mime_header_list_write_to_stream (object->headers, options, stream)) == -1) return -1; total += nwritten; /* terminate the headers */ if ((nwritten = g_mime_stream_write_string (stream, newline)) == -1) return -1; total += nwritten; } /* write the message */ if (message) { if (message->marker && (len = strlen (message->marker)) > 0) { if (*(eoln = message->marker + (len - 1)) == '\n') { if (eoln > message->marker && eoln[-1] == '\r') eoln--; /* check if newline sequences match... */ if (!(match = !strcmp (eoln, newline))) { /* they don't match... trim off the eoln sequence */ len = (size_t) (eoln - message->marker); } } else { match = FALSE; } if ((nwritten = g_mime_stream_write (stream, message->marker, len)) == -1) return -1; total += nwritten; if (!match) { if ((nwritten = g_mime_stream_write_string (stream, newline)) == -1) return -1; total += nwritten; } } if ((nwritten = g_mime_object_write_to_stream ((GMimeObject *) message, options, stream)) == -1) return -1; total += nwritten; } return total; }
static ssize_t multipart_write_to_stream (GMimeObject *object, GMimeStream *stream) { GMimeMultipart *multipart = (GMimeMultipart *) object; ssize_t nwritten, total = 0; const char *boundary; GMimeObject *part; guint i; /* make sure a boundary is set unless we are writing out a raw * header (in which case it should already be set... or if * not, then it's a broken multipart and so we don't want to * alter it or we'll completely break the output) */ boundary = g_mime_object_get_content_type_parameter (object, "boundary"); if (!boundary && !g_mime_header_list_get_stream (object->headers)) { g_mime_multipart_set_boundary (multipart, NULL); boundary = g_mime_object_get_content_type_parameter (object, "boundary"); } /* write the content headers */ if ((nwritten = g_mime_header_list_write_to_stream (object->headers, stream)) == -1) return -1; total += nwritten; /* write the preface */ if (multipart->preface) { /* terminate the headers */ if (g_mime_stream_write (stream, "\n", 1) == -1) return -1; total++; if ((nwritten = g_mime_stream_write_string (stream, multipart->preface)) == -1) return -1; total += nwritten; } for (i = 0; i < multipart->children->len; i++) { part = multipart->children->pdata[i]; /* write the boundary */ if ((nwritten = g_mime_stream_printf (stream, "\n--%s\n", boundary)) == -1) return -1; total += nwritten; /* write this part out */ if ((nwritten = g_mime_object_write_to_stream (part, stream)) == -1) return -1; total += nwritten; } /* write the end-boundary (but only if a boundary is set) */ if (boundary) { if ((nwritten = g_mime_stream_printf (stream, "\n--%s--\n", boundary)) == -1) return -1; total += nwritten; } /* write the postface */ if (multipart->postface) { if ((nwritten = g_mime_stream_write_string (stream, multipart->postface)) == -1) return -1; total += nwritten; } return total; }