2011-05-24 21:43:22 +02:00
|
|
|
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
|
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
/*
|
2017-05-06 11:50:55 +02:00
|
|
|
** Copyright (C) 2011-2017 Dirk-Jan C. Binnema <djcb@cthulhu>
|
2011-05-19 22:26:46 +02:00
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* gmime-test; compile with:
|
2017-05-06 11:50:55 +02:00
|
|
|
gcc -o gmime-test gmime-test.c -Wall -O0 -ggdb \
|
|
|
|
`pkg-config --cflags --libs gmime-2.6`
|
2011-05-19 22:26:46 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gmime/gmime.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2012-05-17 09:57:40 +02:00
|
|
|
#include <locale.h>
|
2011-05-19 22:26:46 +02:00
|
|
|
|
2012-05-17 09:57:40 +02:00
|
|
|
static gchar*
|
2017-07-30 16:47:38 +02:00
|
|
|
get_recip (GMimeMessage *msg, GMimeAddressType atype)
|
2011-05-19 22:26:46 +02:00
|
|
|
{
|
|
|
|
char *recep;
|
2017-05-06 11:50:55 +02:00
|
|
|
InternetAddressList *receps;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
receps = g_mime_message_get_addresses (msg, atype);
|
|
|
|
recep = (char*)internet_address_list_to_string (receps, NULL, FALSE);
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2017-05-06 11:50:55 +02:00
|
|
|
if (!recep || !*recep) {
|
|
|
|
g_free (recep);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-05-19 22:26:46 +02:00
|
|
|
|
2017-05-06 11:50:55 +02:00
|
|
|
return recep;
|
2011-05-19 22:26:46 +02:00
|
|
|
}
|
|
|
|
|
2011-06-24 11:39:46 +02:00
|
|
|
static gchar*
|
|
|
|
get_refs_str (GMimeMessage *msg)
|
|
|
|
{
|
|
|
|
const gchar *str;
|
|
|
|
GMimeReferences *mime_refs;
|
2017-07-30 16:47:38 +02:00
|
|
|
int i, refs_len;
|
2011-06-24 11:39:46 +02:00
|
|
|
gchar *rv;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2012-05-17 09:57:40 +02:00
|
|
|
str = g_mime_object_get_header (GMIME_OBJECT(msg), "References");
|
2011-06-24 11:39:46 +02:00
|
|
|
if (!str)
|
|
|
|
return NULL;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
mime_refs = g_mime_references_parse (NULL, str);
|
|
|
|
refs_len = g_mime_references_length (mime_refs);
|
|
|
|
for (rv = NULL, i = 0; i < refs_len; ++i) {
|
2011-06-24 11:39:46 +02:00
|
|
|
const char* msgid;
|
2017-07-30 16:47:38 +02:00
|
|
|
msgid = g_mime_references_get_message_id (mime_refs, i);
|
2011-06-24 11:39:46 +02:00
|
|
|
rv = g_strdup_printf ("%s%s%s",
|
|
|
|
rv ? rv : "",
|
|
|
|
rv ? "," : "",
|
|
|
|
msgid);
|
|
|
|
}
|
|
|
|
g_mime_references_free (mime_refs);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2011-05-19 22:26:46 +02:00
|
|
|
|
2013-10-06 23:50:36 +02:00
|
|
|
static void
|
|
|
|
print_date (GMimeMessage *msg)
|
|
|
|
{
|
2017-07-30 16:47:38 +02:00
|
|
|
GDateTime *dt;
|
|
|
|
gchar *buf;
|
2013-10-06 23:50:36 +02:00
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
dt = g_mime_message_get_date (msg);
|
|
|
|
if (!dt)
|
|
|
|
return;
|
2013-10-06 23:50:36 +02:00
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
dt = g_date_time_to_local (dt);
|
|
|
|
buf = g_date_time_format (dt, "%c");
|
|
|
|
g_date_time_unref (dt);
|
2013-10-06 23:50:36 +02:00
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
if (buf) {
|
|
|
|
g_print ("Date : %s\n", buf);
|
|
|
|
g_free (buf);
|
|
|
|
}
|
2013-10-06 23:50:36 +02:00
|
|
|
}
|
|
|
|
|
2017-05-06 11:50:55 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
print_body (GMimeMessage *msg)
|
|
|
|
{
|
|
|
|
GMimeObject *body;
|
|
|
|
GMimeDataWrapper *wrapper;
|
|
|
|
GMimeStream *stream;
|
|
|
|
|
|
|
|
body = g_mime_message_get_body (msg);
|
|
|
|
|
|
|
|
if (GMIME_IS_MULTIPART(body))
|
|
|
|
body = g_mime_multipart_get_part (GMIME_MULTIPART(body), 0);
|
|
|
|
|
|
|
|
if (!GMIME_IS_PART(body))
|
|
|
|
return;
|
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
wrapper = g_mime_part_get_content (GMIME_PART(body));
|
2017-05-06 11:50:55 +02:00
|
|
|
if (!GMIME_IS_DATA_WRAPPER(wrapper))
|
|
|
|
return;
|
|
|
|
|
|
|
|
stream = g_mime_data_wrapper_get_stream (wrapper);
|
|
|
|
if (!GMIME_IS_STREAM(stream))
|
|
|
|
return;
|
|
|
|
|
|
|
|
do {
|
|
|
|
char buf[512];
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
len = g_mime_stream_read (stream, buf, sizeof(buf));
|
|
|
|
if (len == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (write (fileno(stdout), buf, len) == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (len < (int)sizeof(buf))
|
|
|
|
break;
|
|
|
|
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
static gboolean
|
|
|
|
test_message (GMimeMessage *msg)
|
|
|
|
{
|
2017-05-06 11:50:55 +02:00
|
|
|
gchar *val;
|
|
|
|
const gchar *str;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
val = get_recip (msg, GMIME_ADDRESS_TYPE_FROM);
|
|
|
|
g_print ("From : %s\n", val ? val : "<none>" );
|
|
|
|
g_free (val);
|
2011-05-19 22:26:46 +02:00
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
val = get_recip (msg, GMIME_ADDRESS_TYPE_TO);
|
2011-05-19 22:26:46 +02:00
|
|
|
g_print ("To : %s\n", val ? val : "<none>" );
|
|
|
|
g_free (val);
|
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
val = get_recip (msg, GMIME_ADDRESS_TYPE_CC);
|
2011-05-19 22:26:46 +02:00
|
|
|
g_print ("Cc : %s\n", val ? val : "<none>" );
|
|
|
|
g_free (val);
|
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
val = get_recip (msg, GMIME_ADDRESS_TYPE_BCC);
|
2011-05-19 22:26:46 +02:00
|
|
|
g_print ("Bcc : %s\n", val ? val : "<none>" );
|
|
|
|
g_free (val);
|
|
|
|
|
|
|
|
str = g_mime_message_get_subject (msg);
|
|
|
|
g_print ("Subject: %s\n", str ? str : "<none>");
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2013-10-06 23:50:36 +02:00
|
|
|
print_date (msg);
|
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
str = g_mime_message_get_message_id (msg);
|
|
|
|
g_print ("Msg-id : %s\n", str ? str : "<none>");
|
2011-06-23 23:19:09 +02:00
|
|
|
|
|
|
|
{
|
2017-05-06 11:50:55 +02:00
|
|
|
gchar *refsstr;
|
2011-06-24 11:39:46 +02:00
|
|
|
refsstr = get_refs_str (msg);
|
|
|
|
g_print ("Refs : %s\n", refsstr ? refsstr : "<none>");
|
|
|
|
g_free (refsstr);
|
2011-06-23 23:19:09 +02:00
|
|
|
}
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2017-05-06 11:50:55 +02:00
|
|
|
print_body (msg);
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
test_stream (GMimeStream *stream)
|
|
|
|
{
|
|
|
|
GMimeParser *parser;
|
|
|
|
GMimeMessage *msg;
|
|
|
|
gboolean rv;
|
|
|
|
|
|
|
|
parser = NULL;
|
|
|
|
msg = NULL;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
parser = g_mime_parser_new_with_stream (stream);
|
|
|
|
if (!parser) {
|
|
|
|
g_warning ("failed to create parser");
|
|
|
|
rv = FALSE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
msg = g_mime_parser_construct_message (parser, NULL);
|
2011-05-19 22:26:46 +02:00
|
|
|
if (!msg) {
|
|
|
|
g_warning ("failed to construct message");
|
|
|
|
rv = FALSE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = test_message (msg);
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
leave:
|
|
|
|
if (parser)
|
|
|
|
g_object_unref (parser);
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
if (msg)
|
|
|
|
g_object_unref (msg);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
test_file (const char *path)
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
GMimeStream *stream;
|
|
|
|
gboolean rv;
|
|
|
|
|
|
|
|
stream = NULL;
|
|
|
|
file = NULL;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
file = fopen (path, "r");
|
|
|
|
if (!file) {
|
|
|
|
g_warning ("cannot open file '%s': %s", path,
|
|
|
|
strerror(errno));
|
|
|
|
rv = FALSE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = g_mime_stream_file_new (file);
|
|
|
|
if (!stream) {
|
|
|
|
g_warning ("cannot open stream for '%s'", path);
|
|
|
|
rv = FALSE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
rv = test_stream (stream);
|
|
|
|
g_object_unref (stream);
|
|
|
|
return rv;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
leave:
|
|
|
|
if (file)
|
|
|
|
fclose (file);
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
gboolean rv;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
if (argc != 2) {
|
|
|
|
g_printerr ("usage: %s <msg-file>\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-05-17 09:57:40 +02:00
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
g_mime_init();
|
2011-05-19 22:26:46 +02:00
|
|
|
|
|
|
|
rv = test_file (argv[1]);
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
g_mime_shutdown ();
|
|
|
|
|
|
|
|
return rv ? 0 : 1;
|
|
|
|
}
|