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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* GMime
* Copyright (C) 2000-2022 Jeffrey Stedfast
*
* 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 2 of the License, 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <glib.h>
#include <gmime/gmime.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
static GMimeMessage *
parse_message (int fd)
{
GMimeMessage *message;
GMimeParser *parser;
GMimeStream *stream;
/* create a stream to read from the file descriptor */
stream = g_mime_stream_fs_new (fd);
/* create a new parser object to parse the stream */
parser = g_mime_parser_new_with_stream (stream);
/* unref the stream (parser owns a ref, so this object does not actually get free'd until we destroy the parser) */
g_object_unref (stream);
/* parse the message from the stream */
message = g_mime_parser_construct_message (parser, NULL);
/* free the parser (and the stream) */
g_object_unref (parser);
return message;
}
static void
count_foreach_callback (GMimeObject *parent, GMimeObject *part, gpointer user_data)
{
int *count = user_data;
(*count)++;
/* 'part' points to the current part node that
* g_mime_message_foreach() is iterating over */
/* find out what class 'part' is... */
if (GMIME_IS_MESSAGE_PART (part)) {
/* message/rfc822 or message/news */
GMimeMessage *message;
/* g_mime_message_foreach() won't descend into
child message parts, so if we want to count any
subparts of this child message, we'll have to call
g_mime_message_foreach() again here. */
message = g_mime_message_part_get_message ((GMimeMessagePart *) part);
g_mime_message_foreach (message, count_foreach_callback, count);
} else if (GMIME_IS_MESSAGE_PARTIAL (part)) {
/* message/partial */
/* this is an incomplete message part, probably a
large message that the sender has broken into
smaller parts and is sending us bit by bit. we
could save some info about it so that we could
piece this back together again once we get all the
parts? */
} else if (GMIME_IS_MULTIPART (part)) {
/* multipart/mixed, multipart/alternative,
* multipart/related, multipart/signed,
* multipart/encrypted, etc... */
/* we'll get to finding out if this is a
* signed/encrypted multipart later... */
} else if (GMIME_IS_PART (part)) {
/* a normal leaf part, could be text/plain or
* image/jpeg etc */
} else {
g_assert_not_reached ();
}
}
static void
count_parts_in_message (GMimeMessage *message)
{
int count = 0;
/* count the number of parts (recursively) in the message
* including the container multiparts */
g_mime_message_foreach (message, count_foreach_callback, &count);
printf ("There are %d parts in the message\n", count);
}
#ifndef G_OS_WIN32
#ifdef ENABLE_CRYPTOGRAPHY
static void
verify_foreach_callback (GMimeObject *parent, GMimeObject *part, gpointer user_data)
{
if (GMIME_IS_MULTIPART_SIGNED (part)) {
/* this is a multipart/signed part, so we can verify the pgp signature */
GMimeMultipartSigned *mps = (GMimeMultipartSigned *) part;
GMimeSignatureList *signatures;
GMimeSignature *sig;
GError *err = NULL;
const char *str;
int i;
if (!(signatures = g_mime_multipart_signed_verify (mps, GMIME_VERIFY_NONE, &err))) {
/* an error occurred - probably couldn't start gpg? */
/* for more information about GError, see:
* http://developer.gnome.org/doc/API/2.0/glib/glib-Error-Reporting.html
*/
fprintf (stderr, "Failed to verify signed part: %s\n", err->message);
g_error_free (err);
} else {
/* print out validity info - GOOD vs BAD and "why" */
for (i = 0; i < g_mime_signature_list_length (signatures); i++) {
sig = g_mime_signature_list_get_signature (signatures, i);
if ((sig->status & GMIME_SIGNATURE_STATUS_RED) != 0)
str = "Bad";
else if ((sig->status & GMIME_SIGNATURE_STATUS_GREEN) != 0)
str = "Good";
else
str = "Error";
}
g_object_unref (signatures);
}
}
}
static void
verify_signed_parts (GMimeMessage *message)
{
/* descend the mime tree and verify any signed parts */
g_mime_message_foreach (message, verify_foreach_callback, NULL);
}
#endif
#endif
static void
write_message_to_screen (GMimeMessage *message)
{
GMimeStream *stream;
/* create a new stream for writing to stdout */
stream = g_mime_stream_pipe_new (STDOUT_FILENO);
g_mime_stream_pipe_set_owner ((GMimeStreamPipe *) stream, FALSE);
/* write the message to the stream */
g_mime_object_write_to_stream ((GMimeObject *) message, NULL, stream);
/* flush the stream (kinda like fflush() in libc's stdio) */
g_mime_stream_flush (stream);
/* free the output stream */
g_object_unref (stream);
}
#define TEXT_CONTENT "Hello, this is the new text/plain part's content text."
static void
add_a_mime_part (GMimeMessage *message)
{
GMimeMultipart *multipart;
GMimeTextPart *mime_part;
/* create the new part that we are going to add... */
mime_part = g_mime_text_part_new_with_subtype ("plain");
/* set the text content of the mime part */
g_mime_text_part_set_text (mime_part, TEXT_CONTENT);
/* if we want, we can tell GMime that the content should be base64 encoded when written to disk... */
g_mime_part_set_content_encoding ((GMimePart *) mime_part, GMIME_CONTENT_ENCODING_BASE64);
/* the "polite" way to modify a mime structure that we didn't
create is to create a new toplevel multipart/mixed part and
add the previous toplevel part as one of the subparts as
well as our text part that we just created... */
/* create a multipart/mixed part */
multipart = g_mime_multipart_new_with_subtype ("mixed");
/* add our new text part to it */
g_mime_multipart_add (multipart, (GMimeObject *) mime_part);
g_object_unref (mime_part);
/* now append the message's toplevel part to our multipart */
g_mime_multipart_add (multipart, message->mime_part);
/* now replace the message's toplevel mime part with our new multipart */
g_mime_message_set_mime_part (message, (GMimeObject *) multipart);
g_object_unref (multipart);
}
static void
remove_a_mime_part (GMimeMessage *message)
{
GMimeMultipart *multipart;
/* since we know the toplevel part is a multipart (we added it
in add_a_mime_part() earlier) and we know that the first
part of that multipart is our text part, lets remove the
first part of the toplevel mime part... */
multipart = (GMimeMultipart *) message->mime_part;
/* subpart indexes start at 0 */
g_mime_multipart_remove_at (multipart, 0);
/* now we should be left with a toplevel multipart/mixed which
contains the mime parts of the original message */
}
int main (int argc, char **argv)
{
GMimeMessage *message;
int fd;
if (argc < 2) {
printf ("Usage: a.out <message file>\n");
return 0;
}
if ((fd = open (argv[1], O_RDONLY, 0)) == -1) {
fprintf (stderr, "Cannot open message `%s': %s\n", argv[1], g_strerror (errno));
return 0;
}
/* init the gmime library */
g_mime_init ();
/* parse the message */
message = parse_message (fd);
if (message == NULL) {
printf ("Error parsing message\n");
return 1;
}
/* count the number of parts in the message */
count_parts_in_message (message);
#ifndef G_OS_WIN32
#ifdef ENABLE_CRYPTOGRAPHY
/* verify any signed parts */
verify_signed_parts (message);
#endif
#endif
/* add and remove parts */
add_a_mime_part (message);
write_message_to_screen (message);
remove_a_mime_part (message);
write_message_to_screen (message);
/* free the mesage */
g_object_unref (message);
return 0;
}
|