2011-05-19 22:26:46 +02:00
|
|
|
/*
|
2023-06-11 10:50:20 +02:00
|
|
|
** Copyright (C) 2011-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2021-11-03 23:32:13 +01:00
|
|
|
#define _POSIX_C_SOURCE 1
|
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*
|
2021-11-03 21:08:16 +01:00
|
|
|
get_recip(GMimeMessage* msg, GMimeAddressType atype)
|
2011-05-19 22:26:46 +02:00
|
|
|
{
|
2021-11-03 21:08:16 +01:00
|
|
|
char* recep;
|
|
|
|
InternetAddressList* receps;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2021-11-03 21:08:16 +01: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) {
|
2021-11-03 21:08:16 +01:00
|
|
|
g_free(recep);
|
2017-05-06 11:50:55 +02:00
|
|
|
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*
|
2021-11-03 21:08:16 +01:00
|
|
|
get_refs_str(GMimeMessage* msg)
|
2011-06-24 11:39:46 +02:00
|
|
|
{
|
2021-11-03 21:08:16 +01:00
|
|
|
const gchar* str;
|
|
|
|
GMimeReferences* mime_refs;
|
|
|
|
int i, refs_len;
|
|
|
|
gchar* rv;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2021-11-03 21:08:16 +01: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
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
mime_refs = g_mime_references_parse(NULL, str);
|
|
|
|
refs_len = g_mime_references_length(mime_refs);
|
2017-07-30 16:47:38 +02:00
|
|
|
for (rv = NULL, i = 0; i < refs_len; ++i) {
|
2011-06-24 11:39:46 +02:00
|
|
|
const char* msgid;
|
2023-06-11 10:50:20 +02:00
|
|
|
char *tmp;
|
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
msgid = g_mime_references_get_message_id(mime_refs, i);
|
2023-06-11 10:50:20 +02:00
|
|
|
tmp = rv;
|
2021-11-03 21:08:16 +01:00
|
|
|
rv = g_strdup_printf("%s%s%s", rv ? rv : "", rv ? "," : "", msgid);
|
2023-06-11 10:50:20 +02:00
|
|
|
g_free(tmp);
|
2011-06-24 11:39:46 +02:00
|
|
|
}
|
2021-11-03 21:08:16 +01:00
|
|
|
g_mime_references_free(mime_refs);
|
2011-06-24 11:39:46 +02:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2011-05-19 22:26:46 +02:00
|
|
|
|
2013-10-06 23:50:36 +02:00
|
|
|
static void
|
2021-11-03 21:08:16 +01:00
|
|
|
print_date(GMimeMessage* msg)
|
2013-10-06 23:50:36 +02:00
|
|
|
{
|
2021-11-03 21:08:16 +01:00
|
|
|
GDateTime* dt;
|
|
|
|
gchar* buf;
|
2013-10-06 23:50:36 +02:00
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
dt = g_mime_message_get_date(msg);
|
2017-07-30 16:47:38 +02:00
|
|
|
if (!dt)
|
|
|
|
return;
|
2013-10-06 23:50:36 +02:00
|
|
|
|
2021-11-03 21:08:16 +01: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) {
|
2021-11-03 21:08:16 +01:00
|
|
|
g_print("Date : %s\n", buf);
|
|
|
|
g_free(buf);
|
2017-07-30 16:47:38 +02:00
|
|
|
}
|
2013-10-06 23:50:36 +02:00
|
|
|
}
|
|
|
|
|
2017-05-06 11:50:55 +02:00
|
|
|
static void
|
2021-11-03 21:08:16 +01:00
|
|
|
print_body(GMimeMessage* msg)
|
2017-05-06 11:50:55 +02:00
|
|
|
{
|
2021-11-03 21:08:16 +01:00
|
|
|
GMimeObject* body;
|
|
|
|
GMimeDataWrapper* wrapper;
|
|
|
|
GMimeStream* stream;
|
2017-05-06 11:50:55 +02:00
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
body = g_mime_message_get_body(msg);
|
2017-05-06 11:50:55 +02:00
|
|
|
|
|
|
|
if (GMIME_IS_MULTIPART(body))
|
2021-11-03 21:08:16 +01:00
|
|
|
body = g_mime_multipart_get_part(GMIME_MULTIPART(body), 0);
|
2017-05-06 11:50:55 +02:00
|
|
|
if (!GMIME_IS_PART(body))
|
|
|
|
return;
|
|
|
|
|
2021-11-03 21:08:16 +01: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;
|
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
stream = g_mime_data_wrapper_get_stream(wrapper);
|
2017-05-06 11:50:55 +02:00
|
|
|
if (!GMIME_IS_STREAM(stream))
|
|
|
|
return;
|
|
|
|
|
|
|
|
do {
|
2021-11-03 21:08:16 +01:00
|
|
|
char buf[512];
|
|
|
|
ssize_t len;
|
2017-05-06 11:50:55 +02:00
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
len = g_mime_stream_read(stream, buf, sizeof(buf));
|
2017-05-06 11:50:55 +02:00
|
|
|
if (len == -1)
|
|
|
|
break;
|
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
if (write(fileno(stdout), buf, len) == -1)
|
2017-05-06 11:50:55 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (len < (int)sizeof(buf))
|
|
|
|
break;
|
|
|
|
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
static gboolean
|
2021-11-03 21:08:16 +01:00
|
|
|
test_message(GMimeMessage* msg)
|
2011-05-19 22:26:46 +02:00
|
|
|
{
|
2021-11-03 21:08:16 +01:00
|
|
|
gchar* val;
|
|
|
|
const gchar* str;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2021-11-03 21:08:16 +01: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
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
val = get_recip(msg, GMIME_ADDRESS_TYPE_TO);
|
|
|
|
g_print("To : %s\n", val ? val : "<none>");
|
|
|
|
g_free(val);
|
2011-05-19 22:26:46 +02:00
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
val = get_recip(msg, GMIME_ADDRESS_TYPE_CC);
|
|
|
|
g_print("Cc : %s\n", val ? val : "<none>");
|
|
|
|
g_free(val);
|
2011-05-19 22:26:46 +02:00
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
val = get_recip(msg, GMIME_ADDRESS_TYPE_BCC);
|
|
|
|
g_print("Bcc : %s\n", val ? val : "<none>");
|
|
|
|
g_free(val);
|
2011-05-19 22:26:46 +02:00
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
str = g_mime_message_get_subject(msg);
|
|
|
|
g_print("Subject: %s\n", str ? str : "<none>");
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
print_date(msg);
|
2013-10-06 23:50:36 +02:00
|
|
|
|
2021-11-03 21:08:16 +01: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
|
|
|
|
|
|
|
{
|
2021-11-03 21:08:16 +01:00
|
|
|
gchar* refsstr;
|
|
|
|
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
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
print_body(msg);
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-11-03 21:08:16 +01:00
|
|
|
test_stream(GMimeStream* stream)
|
2011-05-19 22:26:46 +02:00
|
|
|
{
|
2021-11-03 21:08:16 +01:00
|
|
|
GMimeParser* parser;
|
|
|
|
GMimeMessage* msg;
|
|
|
|
gboolean rv;
|
2011-05-19 22:26:46 +02:00
|
|
|
|
|
|
|
parser = NULL;
|
|
|
|
msg = NULL;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
parser = g_mime_parser_new_with_stream(stream);
|
2011-05-19 22:26:46 +02:00
|
|
|
if (!parser) {
|
2021-11-03 21:08:16 +01:00
|
|
|
g_warning("failed to create parser");
|
2011-05-19 22:26:46 +02:00
|
|
|
rv = FALSE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
msg = g_mime_parser_construct_message(parser, NULL);
|
2011-05-19 22:26:46 +02:00
|
|
|
if (!msg) {
|
2021-11-03 21:08:16 +01:00
|
|
|
g_warning("failed to construct message");
|
2011-05-19 22:26:46 +02:00
|
|
|
rv = FALSE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
rv = test_message(msg);
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
leave:
|
|
|
|
if (parser)
|
2021-11-03 21:08:16 +01:00
|
|
|
g_object_unref(parser);
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
if (msg)
|
2021-11-03 21:08:16 +01:00
|
|
|
g_object_unref(msg);
|
2011-05-19 22:26:46 +02:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-11-03 21:08:16 +01:00
|
|
|
test_file(const char* path)
|
2011-05-19 22:26:46 +02:00
|
|
|
{
|
2021-11-03 21:08:16 +01:00
|
|
|
FILE* file;
|
|
|
|
GMimeStream* stream;
|
|
|
|
gboolean rv;
|
2011-05-19 22:26:46 +02:00
|
|
|
|
|
|
|
stream = NULL;
|
|
|
|
file = NULL;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
file = fopen(path, "r");
|
2011-05-19 22:26:46 +02:00
|
|
|
if (!file) {
|
2021-11-03 21:08:16 +01:00
|
|
|
g_warning("cannot open file '%s': %s", path, g_strerror(errno));
|
2011-05-19 22:26:46 +02:00
|
|
|
rv = FALSE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
stream = g_mime_stream_file_new(file);
|
2011-05-19 22:26:46 +02:00
|
|
|
if (!stream) {
|
2021-11-03 21:08:16 +01:00
|
|
|
g_warning("cannot open stream for '%s'", path);
|
2011-05-19 22:26:46 +02:00
|
|
|
rv = FALSE;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
rv = test_stream(stream);
|
|
|
|
g_object_unref(stream);
|
2017-07-30 16:47:38 +02:00
|
|
|
return rv;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
leave:
|
|
|
|
if (file)
|
2021-11-03 21:08:16 +01:00
|
|
|
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
|
2021-11-03 21:08:16 +01:00
|
|
|
main(int argc, char* argv[])
|
2011-05-19 22:26:46 +02:00
|
|
|
{
|
|
|
|
gboolean rv;
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2011-05-19 22:26:46 +02:00
|
|
|
if (argc != 2) {
|
2021-11-03 21:08:16 +01:00
|
|
|
g_printerr("usage: %s <msg-file>\n", argv[0]);
|
2011-05-19 22:26:46 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
setlocale(LC_ALL, "");
|
2012-05-17 09:57:40 +02:00
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
g_mime_init();
|
2011-05-19 22:26:46 +02:00
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
rv = test_file(argv[1]);
|
2011-08-29 22:41:33 +02:00
|
|
|
|
2021-11-03 21:08:16 +01:00
|
|
|
g_mime_shutdown();
|
2011-05-19 22:26:46 +02:00
|
|
|
|
|
|
|
return rv ? 0 : 1;
|
|
|
|
}
|