2010-08-24 23:57:16 +02:00
|
|
|
/*
|
2020-11-28 09:15:49 +01:00
|
|
|
** Copyright (C) 2008-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2010-08-24 23:57:16 +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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-11-28 09:15:49 +01:00
|
|
|
#include "mu-msg.hh"
|
2019-12-16 20:44:35 +01:00
|
|
|
#include "utils/mu-util.h"
|
|
|
|
#include "utils/mu-str.h"
|
2020-11-28 09:15:49 +01:00
|
|
|
#include "mu-msg-priv.hh"
|
|
|
|
#include "mu-msg-part.hh"
|
|
|
|
|
|
|
|
using namespace Mu;
|
2010-08-24 23:57:16 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
struct _DoData {
|
|
|
|
GMimeObject *mime_obj;
|
|
|
|
unsigned index;
|
2011-01-10 23:45:03 +01:00
|
|
|
};
|
2012-08-01 09:45:03 +02:00
|
|
|
typedef struct _DoData DoData;
|
2011-01-10 23:45:03 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
static void
|
|
|
|
do_it_with_index (MuMsg *msg, MuMsgPart *part, DoData *ddata)
|
|
|
|
{
|
|
|
|
if (ddata->mime_obj)
|
|
|
|
return;
|
2011-01-10 23:45:03 +01:00
|
|
|
|
2014-10-10 17:34:57 +02:00
|
|
|
if (part->index == ddata->index) {
|
|
|
|
/* Add a reference to this object, this way if it is
|
|
|
|
* encrypted it will not be garbage collected before
|
|
|
|
* we are done with it. */
|
|
|
|
g_object_ref (part->data);
|
2012-08-01 09:45:03 +02:00
|
|
|
ddata->mime_obj = (GMimeObject*)part->data;
|
2014-10-10 17:34:57 +02:00
|
|
|
}
|
2012-08-01 09:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static GMimeObject*
|
|
|
|
get_mime_object_at_index (MuMsg *msg, MuMsgOptions opts, unsigned index)
|
2012-02-13 21:35:22 +01:00
|
|
|
{
|
2012-08-01 09:45:03 +02:00
|
|
|
DoData ddata;
|
|
|
|
|
|
|
|
ddata.mime_obj = NULL;
|
|
|
|
ddata.index = index;
|
|
|
|
|
2020-05-05 20:25:36 +02:00
|
|
|
/* wipe out some irrelevant options */
|
2020-11-28 09:15:49 +01:00
|
|
|
opts &= (MuMsgOptions)~MU_MSG_OPTION_VERIFY;
|
2020-05-05 20:25:36 +02:00
|
|
|
opts &= ~MU_MSG_OPTION_EXTRACT_IMAGES;
|
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
mu_msg_part_foreach (msg, opts,
|
|
|
|
(MuMsgPartForeachFunc)do_it_with_index,
|
|
|
|
&ddata);
|
|
|
|
|
|
|
|
return ddata.mime_obj;
|
2012-02-13 21:35:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
typedef gboolean (*MuMsgPartMatchFunc) (MuMsgPart *, gpointer);
|
|
|
|
struct _MatchData {
|
|
|
|
MuMsgPartMatchFunc match_func;
|
|
|
|
gpointer user_data;
|
|
|
|
int index;
|
|
|
|
};
|
|
|
|
typedef struct _MatchData MatchData;
|
|
|
|
|
2011-01-10 23:45:03 +01:00
|
|
|
static void
|
2012-08-01 09:45:03 +02:00
|
|
|
check_match (MuMsg *msg, MuMsgPart *part, MatchData *mdata)
|
2011-01-10 23:45:03 +01:00
|
|
|
{
|
2012-08-01 09:45:03 +02:00
|
|
|
if (mdata->index != -1)
|
2012-02-13 21:35:22 +01:00
|
|
|
return;
|
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
if (mdata->match_func (part, mdata->user_data))
|
|
|
|
mdata->index = part->index;
|
2011-01-10 23:45:03 +01:00
|
|
|
}
|
|
|
|
|
2012-09-13 20:12:12 +02:00
|
|
|
static int
|
2012-08-01 09:45:03 +02:00
|
|
|
get_matching_part_index (MuMsg *msg, MuMsgOptions opts,
|
|
|
|
MuMsgPartMatchFunc func, gpointer user_data)
|
2011-01-10 23:45:03 +01:00
|
|
|
{
|
2012-08-01 09:45:03 +02:00
|
|
|
MatchData mdata;
|
2011-01-10 23:45:03 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
mdata.match_func = func;
|
|
|
|
mdata.user_data = user_data;
|
|
|
|
mdata.index = -1;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
mu_msg_part_foreach (msg, opts,
|
|
|
|
(MuMsgPartForeachFunc)check_match,
|
|
|
|
&mdata);
|
|
|
|
return mdata.index;
|
2011-01-10 23:45:03 +01:00
|
|
|
}
|
|
|
|
|
2012-07-24 21:12:22 +02:00
|
|
|
|
|
|
|
static void
|
2012-08-07 11:02:44 +02:00
|
|
|
accumulate_text_message (MuMsg *msg, MuMsgPart *part, GString **gstrp)
|
2012-07-24 21:12:22 +02:00
|
|
|
{
|
2020-02-15 13:10:59 +01:00
|
|
|
const gchar *str;
|
|
|
|
char *adrs;
|
|
|
|
GMimeMessage *mimemsg;
|
|
|
|
InternetAddressList *addresses;
|
2012-08-07 11:02:44 +02:00
|
|
|
|
|
|
|
/* put sender, recipients and subject in the string, so they
|
|
|
|
* can be indexed as well */
|
2020-02-15 13:10:59 +01:00
|
|
|
mimemsg = GMIME_MESSAGE (part->data);
|
2017-07-30 16:47:38 +02:00
|
|
|
addresses = g_mime_message_get_addresses (mimemsg, GMIME_ADDRESS_TYPE_FROM);
|
2020-02-15 13:10:59 +01:00
|
|
|
adrs = internet_address_list_to_string (addresses, NULL, FALSE);
|
|
|
|
|
2012-08-07 11:02:44 +02:00
|
|
|
g_string_append_printf
|
2017-07-30 16:47:38 +02:00
|
|
|
(*gstrp, "%s%s", adrs ? adrs : "", adrs ? "\n" : "");
|
|
|
|
g_free (adrs);
|
|
|
|
|
2012-08-07 11:02:44 +02:00
|
|
|
str = g_mime_message_get_subject (mimemsg);
|
|
|
|
g_string_append_printf
|
|
|
|
(*gstrp, "%s%s", str ? str : "", str ? "\n" : "");
|
2017-07-30 16:47:38 +02:00
|
|
|
|
|
|
|
addresses = g_mime_message_get_all_recipients (mimemsg);
|
2020-02-15 13:10:59 +01:00
|
|
|
adrs = internet_address_list_to_string (addresses, NULL, FALSE);
|
2017-07-30 16:47:38 +02:00
|
|
|
g_object_unref (addresses);
|
2020-02-15 13:10:59 +01:00
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
g_string_append_printf
|
|
|
|
(*gstrp, "%s%s", adrs ? adrs : "", adrs ? "\n" : "");
|
|
|
|
g_free (adrs);
|
2012-08-07 11:02:44 +02:00
|
|
|
}
|
2012-08-01 16:02:11 +02:00
|
|
|
|
2012-08-07 11:02:44 +02:00
|
|
|
static void
|
|
|
|
accumulate_text_part (MuMsg *msg, MuMsgPart *part, GString **gstrp)
|
|
|
|
{
|
2020-02-15 13:10:59 +01:00
|
|
|
GMimeContentType *ctype;
|
|
|
|
gboolean err;
|
|
|
|
char *txt;
|
|
|
|
|
2012-08-07 11:02:44 +02:00
|
|
|
ctype = g_mime_object_get_content_type ((GMimeObject*)part->data);
|
|
|
|
if (!g_mime_content_type_is_type (ctype, "text", "plain"))
|
|
|
|
return; /* not plain text */
|
2020-02-15 13:10:59 +01:00
|
|
|
|
|
|
|
txt = mu_msg_mime_part_to_string((GMimePart*)part->data, &err);
|
2012-08-07 11:02:44 +02:00
|
|
|
if (txt)
|
|
|
|
g_string_append (*gstrp, txt);
|
2020-02-15 13:10:59 +01:00
|
|
|
|
2012-08-07 11:02:44 +02:00
|
|
|
g_free (txt);
|
2012-07-24 21:12:22 +02:00
|
|
|
}
|
|
|
|
|
2012-08-07 11:02:44 +02:00
|
|
|
static void
|
|
|
|
accumulate_text (MuMsg *msg, MuMsgPart *part, GString **gstrp)
|
|
|
|
{
|
|
|
|
if (GMIME_IS_MESSAGE(part->data))
|
|
|
|
accumulate_text_message (msg, part, gstrp);
|
|
|
|
else if (GMIME_IS_PART (part->data))
|
|
|
|
accumulate_text_part (msg, part, gstrp);
|
|
|
|
}
|
2012-07-24 21:12:22 +02:00
|
|
|
|
2014-10-10 06:40:37 +02:00
|
|
|
/* declaration, so we can use it earlier */
|
2014-10-19 02:20:21 +02:00
|
|
|
static gboolean
|
|
|
|
handle_mime_object (MuMsg *msg, GMimeObject *mobj, GMimeObject *parent,
|
2016-10-25 19:34:08 +02:00
|
|
|
MuMsgOptions opts, unsigned *index, gboolean decrypted,
|
|
|
|
MuMsgPartForeachFunc func, gpointer user_data);
|
2014-10-10 06:40:37 +02:00
|
|
|
|
2012-09-14 09:06:14 +02:00
|
|
|
static char*
|
2014-10-12 21:38:53 +02:00
|
|
|
get_text_from_mime_msg (MuMsg *msg, GMimeMessage *mmsg, MuMsgOptions opts)
|
2012-09-14 09:06:14 +02:00
|
|
|
{
|
|
|
|
GString *gstr;
|
2014-10-12 21:38:53 +02:00
|
|
|
unsigned index;
|
2012-09-14 09:06:14 +02:00
|
|
|
|
2014-10-17 14:53:32 +02:00
|
|
|
index = 1;
|
2014-10-12 21:38:53 +02:00
|
|
|
gstr = g_string_sized_new (4096);
|
2014-10-10 06:40:37 +02:00
|
|
|
handle_mime_object (msg,
|
2016-10-25 19:34:08 +02:00
|
|
|
mmsg->mime_part,
|
2014-10-10 06:40:37 +02:00
|
|
|
(GMimeObject *) mmsg,
|
|
|
|
opts,
|
2014-10-12 21:38:53 +02:00
|
|
|
&index,
|
2016-10-25 19:34:08 +02:00
|
|
|
FALSE,
|
2014-10-10 06:40:37 +02:00
|
|
|
(MuMsgPartForeachFunc)accumulate_text,
|
|
|
|
&gstr);
|
2012-09-14 09:06:14 +02:00
|
|
|
|
|
|
|
return g_string_free (gstr, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-23 23:11:45 +01:00
|
|
|
char*
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_msg_part_get_text (MuMsg *msg, MuMsgPart *self, MuMsgOptions opts)
|
2011-11-23 23:11:45 +01:00
|
|
|
{
|
2012-08-11 10:54:08 +02:00
|
|
|
GMimeObject *mobj;
|
|
|
|
GMimeMessage *mime_msg;
|
2012-08-22 23:00:28 +02:00
|
|
|
gboolean err;
|
2011-12-01 20:23:13 +01:00
|
|
|
|
2012-07-27 17:04:17 +02:00
|
|
|
g_return_val_if_fail (msg, NULL);
|
2013-04-17 23:12:37 +02:00
|
|
|
g_return_val_if_fail (self && GMIME_IS_OBJECT(self->data),
|
|
|
|
NULL);
|
2012-08-07 10:43:54 +02:00
|
|
|
|
2011-12-01 20:23:13 +01:00
|
|
|
mobj = (GMimeObject*)self->data;
|
|
|
|
|
2012-08-22 23:00:28 +02:00
|
|
|
err = FALSE;
|
2012-08-11 10:54:08 +02:00
|
|
|
if (GMIME_IS_PART (mobj)) {
|
|
|
|
if (self->part_type & MU_MSG_PART_TYPE_TEXT_PLAIN)
|
2012-09-13 20:12:12 +02:00
|
|
|
return mu_msg_mime_part_to_string ((GMimePart*)mobj,
|
|
|
|
&err);
|
2012-08-11 10:54:08 +02:00
|
|
|
else
|
|
|
|
return NULL; /* non-text MimePart */
|
|
|
|
}
|
|
|
|
|
|
|
|
mime_msg = NULL;
|
2012-09-14 09:06:14 +02:00
|
|
|
|
2012-08-11 10:54:08 +02:00
|
|
|
if (GMIME_IS_MESSAGE_PART (mobj))
|
|
|
|
mime_msg = g_mime_message_part_get_message
|
|
|
|
((GMimeMessagePart*)mobj);
|
|
|
|
else if (GMIME_IS_MESSAGE (mobj))
|
|
|
|
mime_msg = (GMimeMessage*)mobj;
|
2012-09-14 09:06:14 +02:00
|
|
|
|
2012-09-17 11:43:53 +02:00
|
|
|
/* apparently, g_mime_message_part_get_message may still
|
|
|
|
* return NULL */
|
|
|
|
if (mime_msg)
|
2014-10-12 21:38:53 +02:00
|
|
|
return get_text_from_mime_msg (msg, mime_msg, opts);
|
2012-09-17 11:43:53 +02:00
|
|
|
return NULL;
|
2011-11-23 23:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-11 17:25:49 +02:00
|
|
|
/* note: this will return -1 in case of error or if the size is
|
|
|
|
* unknown */
|
2011-11-18 11:20:42 +01:00
|
|
|
static ssize_t
|
|
|
|
get_part_size (GMimePart *part)
|
|
|
|
{
|
|
|
|
GMimeDataWrapper *wrapper;
|
|
|
|
GMimeStream *stream;
|
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
wrapper = g_mime_part_get_content (part);
|
2012-08-01 09:45:03 +02:00
|
|
|
if (!GMIME_IS_DATA_WRAPPER(wrapper))
|
2011-11-18 11:20:42 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
stream = g_mime_data_wrapper_get_stream (wrapper);
|
|
|
|
if (!stream)
|
2012-04-11 17:25:49 +02:00
|
|
|
return -1; /* no stream -> size is 0 */
|
|
|
|
else
|
|
|
|
return g_mime_stream_length (stream);
|
2011-11-18 11:20:42 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
/* NOTE: stream/wrapper are owned by gmime, no unreffing */
|
2011-11-18 11:20:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
Use Unicode characters when cleaning up attachment filename
Improve the function ``cleanup_filename()`` of ``lib/mu-msg-part.c`` to
use Unicode characters when replacing the control characters, slashes
and colons with ``-``.
Originally, this function just use plain C characters (i.e., assuming
ASCII string) when checking each character is or not a control character,
slash or colon. However, when the attachment filename contains non-ASCII
(e.g., Chinese characters), all the non-ASCII characters are replaced
with ``-``.
For example:
* Before:
```
> mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '------------.txt', '-------test.txt'
Hello,
This is a simple test email with three attachments:
1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```
* After:
```
> ./build/mu/mu/mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '测试附件.txt', '附件-test.txt'
Hello,
This is a simple test email with three attachments:
1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```
2016-05-23 13:26:04 +02:00
|
|
|
static char*
|
2012-08-07 11:02:44 +02:00
|
|
|
cleanup_filename (char *fname)
|
|
|
|
{
|
Use Unicode characters when cleaning up attachment filename
Improve the function ``cleanup_filename()`` of ``lib/mu-msg-part.c`` to
use Unicode characters when replacing the control characters, slashes
and colons with ``-``.
Originally, this function just use plain C characters (i.e., assuming
ASCII string) when checking each character is or not a control character,
slash or colon. However, when the attachment filename contains non-ASCII
(e.g., Chinese characters), all the non-ASCII characters are replaced
with ``-``.
For example:
* Before:
```
> mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '------------.txt', '-------test.txt'
Hello,
This is a simple test email with three attachments:
1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```
* After:
```
> ./build/mu/mu/mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '测试附件.txt', '附件-test.txt'
Hello,
This is a simple test email with three attachments:
1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```
2016-05-23 13:26:04 +02:00
|
|
|
GString *gstr;
|
2012-08-07 11:02:44 +02:00
|
|
|
gchar *cur;
|
Use Unicode characters when cleaning up attachment filename
Improve the function ``cleanup_filename()`` of ``lib/mu-msg-part.c`` to
use Unicode characters when replacing the control characters, slashes
and colons with ``-``.
Originally, this function just use plain C characters (i.e., assuming
ASCII string) when checking each character is or not a control character,
slash or colon. However, when the attachment filename contains non-ASCII
(e.g., Chinese characters), all the non-ASCII characters are replaced
with ``-``.
For example:
* Before:
```
> mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '------------.txt', '-------test.txt'
Hello,
This is a simple test email with three attachments:
1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```
* After:
```
> ./build/mu/mu/mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '测试附件.txt', '附件-test.txt'
Hello,
This is a simple test email with three attachments:
1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```
2016-05-23 13:26:04 +02:00
|
|
|
gunichar uc;
|
2012-08-07 11:02:44 +02:00
|
|
|
|
Use Unicode characters when cleaning up attachment filename
Improve the function ``cleanup_filename()`` of ``lib/mu-msg-part.c`` to
use Unicode characters when replacing the control characters, slashes
and colons with ``-``.
Originally, this function just use plain C characters (i.e., assuming
ASCII string) when checking each character is or not a control character,
slash or colon. However, when the attachment filename contains non-ASCII
(e.g., Chinese characters), all the non-ASCII characters are replaced
with ``-``.
For example:
* Before:
```
> mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '------------.txt', '-------test.txt'
Hello,
This is a simple test email with three attachments:
1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```
* After:
```
> ./build/mu/mu/mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '测试附件.txt', '附件-test.txt'
Hello,
This is a simple test email with three attachments:
1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```
2016-05-23 13:26:04 +02:00
|
|
|
gstr = g_string_sized_new (strlen (fname));
|
|
|
|
|
|
|
|
/* replace control characters, slashes, and colons by '-' */
|
|
|
|
for (cur = fname; cur && *cur; cur = g_utf8_next_char (cur)) {
|
|
|
|
uc = g_utf8_get_char (cur);
|
2020-05-23 03:21:57 +02:00
|
|
|
if (g_unichar_iscntrl (uc) || uc == '/' || uc == ':' || uc == ';')
|
Use Unicode characters when cleaning up attachment filename
Improve the function ``cleanup_filename()`` of ``lib/mu-msg-part.c`` to
use Unicode characters when replacing the control characters, slashes
and colons with ``-``.
Originally, this function just use plain C characters (i.e., assuming
ASCII string) when checking each character is or not a control character,
slash or colon. However, when the attachment filename contains non-ASCII
(e.g., Chinese characters), all the non-ASCII characters are replaced
with ``-``.
For example:
* Before:
```
> mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '------------.txt', '-------test.txt'
Hello,
This is a simple test email with three attachments:
1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```
* After:
```
> ./build/mu/mu/mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '测试附件.txt', '附件-test.txt'
Hello,
This is a simple test email with three attachments:
1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```
2016-05-23 13:26:04 +02:00
|
|
|
g_string_append_unichar (gstr, '-');
|
|
|
|
else
|
|
|
|
g_string_append_unichar (gstr, uc);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (fname);
|
|
|
|
return g_string_free (gstr, FALSE);
|
2012-08-07 11:02:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-21 07:06:40 +02:00
|
|
|
/*
|
|
|
|
* when a part doesn't have a filename, it can be useful to 'guess' one based on
|
|
|
|
* its mime-type, which allows other tools to handle them correctly, e.g. from
|
|
|
|
* mu4e.
|
|
|
|
*
|
|
|
|
* For now, we only handle calendar invitations in that way, but others may
|
|
|
|
* follow.
|
|
|
|
*/
|
|
|
|
static char*
|
|
|
|
guess_file_name (GMimeObject *mobj, unsigned index)
|
|
|
|
{
|
|
|
|
GMimeContentType *ctype;
|
|
|
|
|
|
|
|
ctype = g_mime_object_get_content_type (mobj);
|
|
|
|
|
|
|
|
/* special case for calendars; map to '.vcs' */
|
|
|
|
if (g_mime_content_type_is_type (ctype, "text", "calendar"))
|
|
|
|
return g_strdup_printf ("vcal-%u.vcs", index);
|
|
|
|
|
|
|
|
/* fallback */
|
2015-10-24 10:42:57 +02:00
|
|
|
return g_strdup_printf ("%u.msgpart", index);
|
2015-10-21 07:06:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
static char*
|
|
|
|
mime_part_get_filename (GMimeObject *mobj, unsigned index,
|
|
|
|
gboolean construct_if_needed)
|
2012-07-24 21:12:22 +02:00
|
|
|
{
|
2012-08-07 11:02:44 +02:00
|
|
|
gchar *fname;
|
2012-07-24 21:12:22 +02:00
|
|
|
|
2012-08-01 22:22:50 +02:00
|
|
|
fname = NULL;
|
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
if (GMIME_IS_PART (mobj)) {
|
|
|
|
/* the easy case: the part has a filename */
|
|
|
|
fname = (gchar*)g_mime_part_get_filename (GMIME_PART(mobj));
|
|
|
|
if (fname) /* don't include directory components */
|
|
|
|
fname = g_path_get_basename (fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fname && !construct_if_needed)
|
|
|
|
return NULL;
|
2012-07-24 21:12:22 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
if (GMIME_IS_MESSAGE_PART(mobj)) {
|
|
|
|
GMimeMessage *msg;
|
|
|
|
const char *subj;
|
|
|
|
msg = g_mime_message_part_get_message
|
|
|
|
(GMIME_MESSAGE_PART(mobj));
|
|
|
|
subj = g_mime_message_get_subject (msg);
|
|
|
|
fname = g_strdup_printf ("%s.eml", subj ? subj : "message");
|
2012-07-24 21:12:22 +02:00
|
|
|
}
|
2012-07-27 17:04:17 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
if (!fname)
|
2015-10-21 07:06:40 +02:00
|
|
|
fname = guess_file_name (mobj, index);
|
Use Unicode characters when cleaning up attachment filename
Improve the function ``cleanup_filename()`` of ``lib/mu-msg-part.c`` to
use Unicode characters when replacing the control characters, slashes
and colons with ``-``.
Originally, this function just use plain C characters (i.e., assuming
ASCII string) when checking each character is or not a control character,
slash or colon. However, when the attachment filename contains non-ASCII
(e.g., Chinese characters), all the non-ASCII characters are replaced
with ``-``.
For example:
* Before:
```
> mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '------------.txt', '-------test.txt'
Hello,
This is a simple test email with three attachments:
1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```
* After:
```
> ./build/mu/mu/mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '测试附件.txt', '附件-test.txt'
Hello,
This is a simple test email with three attachments:
1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```
2016-05-23 13:26:04 +02:00
|
|
|
|
|
|
|
/* replace control characters, slashes, and colons */
|
|
|
|
fname = cleanup_filename (fname);
|
2015-10-21 07:06:40 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
return fname;
|
|
|
|
}
|
2012-07-27 17:04:17 +02:00
|
|
|
|
2012-01-15 13:10:04 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
char*
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_msg_part_get_filename (MuMsgPart *mpart, gboolean construct_if_needed)
|
2012-01-15 13:10:04 +01:00
|
|
|
{
|
2012-08-01 09:45:03 +02:00
|
|
|
g_return_val_if_fail (mpart, NULL);
|
|
|
|
g_return_val_if_fail (GMIME_IS_OBJECT(mpart->data), NULL);
|
2012-02-13 21:35:22 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
return mime_part_get_filename ((GMimeObject*)mpart->data,
|
|
|
|
mpart->index, construct_if_needed);
|
|
|
|
}
|
2012-01-15 13:10:04 +01:00
|
|
|
|
2016-04-16 16:23:46 +02:00
|
|
|
const gchar*
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_msg_part_get_content_id (MuMsgPart *mpart)
|
2016-04-16 16:23:46 +02:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (mpart, NULL);
|
|
|
|
g_return_val_if_fail (GMIME_IS_OBJECT(mpart->data), NULL);
|
|
|
|
return g_mime_object_get_content_id((GMimeObject*)mpart->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-15 13:10:04 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
static MuMsgPartType
|
|
|
|
get_disposition (GMimeObject *mobj)
|
|
|
|
{
|
|
|
|
const char *disp;
|
2012-01-15 13:10:04 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
disp = g_mime_object_get_disposition (mobj);
|
|
|
|
if (!disp)
|
|
|
|
return MU_MSG_PART_TYPE_NONE;
|
2012-07-24 21:12:22 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
if (strcasecmp (disp, GMIME_DISPOSITION_ATTACHMENT) == 0)
|
|
|
|
return MU_MSG_PART_TYPE_ATTACHMENT;
|
2012-02-13 21:35:22 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
if (strcasecmp (disp, GMIME_DISPOSITION_INLINE) == 0)
|
|
|
|
return MU_MSG_PART_TYPE_INLINE;
|
2012-07-24 21:12:22 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
return MU_MSG_PART_TYPE_NONE;
|
|
|
|
}
|
2012-02-13 21:35:22 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
/* call 'func' with information about this MIME-part */
|
2015-02-14 23:07:38 +01:00
|
|
|
static inline void
|
2012-08-30 11:53:52 +02:00
|
|
|
check_signature (MuMsg *msg, GMimeMultipartSigned *part, MuMsgOptions opts)
|
2012-08-01 09:45:03 +02:00
|
|
|
{
|
2012-08-30 11:53:52 +02:00
|
|
|
GError *err;
|
|
|
|
|
2015-02-14 23:07:38 +01:00
|
|
|
err = NULL;
|
|
|
|
mu_msg_crypto_verify_part (part, opts, &err);
|
2012-08-30 11:53:52 +02:00
|
|
|
if (err) {
|
|
|
|
g_warning ("error verifying signature: %s", err->message);
|
|
|
|
g_clear_error (&err);
|
|
|
|
}
|
2012-01-15 13:10:04 +01:00
|
|
|
}
|
|
|
|
|
2012-09-13 21:57:04 +02:00
|
|
|
|
2012-09-14 11:17:40 +02:00
|
|
|
/* Note: this is function will be called by GMime when it needs a
|
|
|
|
* password. However, GMime <= 2.6.10 does not handle
|
|
|
|
* getting passwords correctly, so this might fail. see:
|
|
|
|
* password_requester in mu-msg-crypto.c */
|
2012-09-13 21:57:04 +02:00
|
|
|
static gchar*
|
|
|
|
get_console_pw (const char* user_id, const char *prompt_ctx,
|
|
|
|
gboolean reprompt, gpointer user_data)
|
|
|
|
{
|
|
|
|
char *prompt, *pass;
|
|
|
|
|
2012-09-14 11:17:40 +02:00
|
|
|
if (!g_mime_check_version(2,6,11))
|
|
|
|
g_printerr (
|
|
|
|
"*** the gmime library you are using has version "
|
|
|
|
"%u.%u.%u (<= 2.6.10)\n"
|
|
|
|
"*** this version has a bug in its password "
|
|
|
|
"retrieval routine, and probably won't work.\n",
|
|
|
|
gmime_major_version, gmime_minor_version,
|
|
|
|
gmime_micro_version);
|
|
|
|
|
2012-09-13 21:57:04 +02:00
|
|
|
if (reprompt)
|
|
|
|
g_print ("Authentication failed. Please try again\n");
|
|
|
|
|
|
|
|
prompt = g_strdup_printf ("Password for %s: ", user_id);
|
|
|
|
|
|
|
|
pass = mu_util_read_password (prompt);
|
|
|
|
g_free (prompt);
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
2012-09-14 11:17:40 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
static gboolean
|
2014-10-19 02:20:21 +02:00
|
|
|
handle_encrypted_part (MuMsg *msg, GMimeMultipartEncrypted *part,
|
2014-10-12 21:38:53 +02:00
|
|
|
MuMsgOptions opts, unsigned *index,
|
2012-08-01 09:45:03 +02:00
|
|
|
MuMsgPartForeachFunc func, gpointer user_data)
|
2012-02-13 21:35:22 +01:00
|
|
|
{
|
2012-09-12 11:12:24 +02:00
|
|
|
GError *err;
|
2014-10-18 11:33:20 +02:00
|
|
|
gboolean rv;
|
2012-09-12 11:12:24 +02:00
|
|
|
GMimeObject *dec;
|
2012-09-13 21:57:04 +02:00
|
|
|
MuMsgPartPasswordFunc pw_func;
|
|
|
|
|
|
|
|
if (opts & MU_MSG_OPTION_CONSOLE_PASSWORD)
|
|
|
|
pw_func = (MuMsgPartPasswordFunc)get_console_pw;
|
|
|
|
else
|
|
|
|
pw_func = NULL;
|
|
|
|
|
2012-09-12 11:12:24 +02:00
|
|
|
|
|
|
|
err = NULL;
|
2012-09-13 21:57:04 +02:00
|
|
|
dec = mu_msg_crypto_decrypt_part (part, opts, pw_func, NULL, &err);
|
2012-09-12 11:12:24 +02:00
|
|
|
if (err) {
|
|
|
|
g_warning ("error decrypting part: %s", err->message);
|
|
|
|
g_clear_error (&err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dec) {
|
2014-10-19 02:20:21 +02:00
|
|
|
rv = handle_mime_object (msg, dec, (GMimeObject *) part,
|
2016-10-25 19:34:08 +02:00
|
|
|
opts, index, TRUE, func, user_data);
|
2012-09-12 11:12:24 +02:00
|
|
|
g_object_unref (dec);
|
2014-10-18 11:33:20 +02:00
|
|
|
} else {
|
2014-10-19 10:38:49 +02:00
|
|
|
/* On failure to decrypt, list the encrypted part as
|
|
|
|
* an attachment
|
|
|
|
*/
|
2014-10-18 11:33:20 +02:00
|
|
|
GMimeObject *encrypted;
|
|
|
|
|
2014-10-19 10:38:49 +02:00
|
|
|
encrypted = g_mime_multipart_get_part (
|
|
|
|
GMIME_MULTIPART (part), 1);
|
2014-10-18 11:33:20 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail (GMIME_IS_PART(encrypted), FALSE);
|
|
|
|
|
2014-10-19 02:20:21 +02:00
|
|
|
rv = handle_mime_object (msg, encrypted, (GMimeObject *) part,
|
2016-10-25 19:34:08 +02:00
|
|
|
opts, index, FALSE, func, user_data);
|
2012-09-12 11:12:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-18 11:33:20 +02:00
|
|
|
return rv;
|
2012-08-01 09:45:03 +02:00
|
|
|
}
|
2013-07-21 13:44:44 +02:00
|
|
|
|
2016-11-12 12:50:32 +01:00
|
|
|
static gboolean
|
|
|
|
looks_like_text_body_part (GMimeContentType *ctype)
|
|
|
|
{
|
|
|
|
unsigned u;
|
|
|
|
static struct {
|
|
|
|
const char *type;
|
|
|
|
const char *subtype;
|
|
|
|
} types[] = {
|
|
|
|
{ "text", "plain" },
|
2019-11-06 02:04:27 +01:00
|
|
|
{ "text", "x-markdown" },
|
2016-11-12 12:50:32 +01:00
|
|
|
{ "text", "x-diff" },
|
|
|
|
{ "text", "x-patch" },
|
|
|
|
{ "application", "x-patch"}
|
|
|
|
/* possible other types */
|
|
|
|
};
|
|
|
|
|
|
|
|
for (u = 0; u != G_N_ELEMENTS(types); ++u)
|
|
|
|
if (g_mime_content_type_is_type (
|
|
|
|
ctype, types[u].type, types[u].subtype))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-02-13 21:35:22 +01:00
|
|
|
|
2020-02-13 23:36:54 +01:00
|
|
|
static MuMsgPartSigStatusReport*
|
|
|
|
copy_status_report_maybe (GObject *obj)
|
|
|
|
{
|
|
|
|
MuMsgPartSigStatusReport *report, *copy;
|
|
|
|
|
2020-11-28 09:15:49 +01:00
|
|
|
report = (MuMsgPartSigStatusReport*)
|
|
|
|
g_object_get_data (obj, SIG_STATUS_REPORT);
|
2020-02-13 23:36:54 +01:00
|
|
|
if (!report)
|
|
|
|
return NULL; /* nothing to copy */
|
|
|
|
|
2020-02-23 14:20:01 +01:00
|
|
|
copy = g_slice_new0(MuMsgPartSigStatusReport);
|
2020-02-13 23:36:54 +01:00
|
|
|
copy->verdict = report->verdict;
|
|
|
|
|
|
|
|
if (report->report)
|
|
|
|
copy->report = g_strdup (report->report);
|
|
|
|
if (report->signers)
|
|
|
|
copy->signers = g_strdup (report->signers);
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-08 18:13:59 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
/* call 'func' with information about this MIME-part */
|
|
|
|
static gboolean
|
|
|
|
handle_part (MuMsg *msg, GMimePart *part, GMimeObject *parent,
|
2014-10-19 02:20:21 +02:00
|
|
|
MuMsgOptions opts, unsigned *index, gboolean decrypted,
|
2012-08-01 09:45:03 +02:00
|
|
|
MuMsgPartForeachFunc func, gpointer user_data)
|
|
|
|
{
|
2016-11-12 12:50:32 +01:00
|
|
|
GMimeContentType *ct;
|
|
|
|
MuMsgPart msgpart;
|
2012-02-13 21:35:22 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
memset (&msgpart, 0, sizeof(MuMsgPart));
|
2012-02-13 21:35:22 +01:00
|
|
|
|
2012-08-09 08:38:22 +02:00
|
|
|
msgpart.size = get_part_size (part);
|
|
|
|
msgpart.part_type = MU_MSG_PART_TYPE_LEAF;
|
2014-10-10 06:40:37 +02:00
|
|
|
msgpart.part_type |= get_disposition ((GMimeObject*)part);
|
2014-10-19 02:20:21 +02:00
|
|
|
if (decrypted)
|
|
|
|
msgpart.part_type |= MU_MSG_PART_TYPE_DECRYPTED;
|
|
|
|
else if ((opts & MU_MSG_OPTION_DECRYPT) &&
|
2016-10-25 19:34:08 +02:00
|
|
|
GMIME_IS_MULTIPART_ENCRYPTED (parent))
|
2014-10-19 02:20:21 +02:00
|
|
|
msgpart.part_type |= MU_MSG_PART_TYPE_ENCRYPTED;
|
|
|
|
|
2012-08-09 08:38:22 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
ct = g_mime_object_get_content_type ((GMimeObject*)part);
|
|
|
|
if (GMIME_IS_CONTENT_TYPE(ct)) {
|
|
|
|
msgpart.type = g_mime_content_type_get_media_type (ct);
|
|
|
|
msgpart.subtype = g_mime_content_type_get_media_subtype (ct);
|
2016-11-12 12:50:32 +01:00
|
|
|
/* store in the part_type as well, for quick checking */
|
|
|
|
if (looks_like_text_body_part (ct))
|
2012-08-09 08:38:22 +02:00
|
|
|
msgpart.part_type |= MU_MSG_PART_TYPE_TEXT_PLAIN;
|
|
|
|
else if (g_mime_content_type_is_type (ct, "text", "html"))
|
|
|
|
msgpart.part_type |= MU_MSG_PART_TYPE_TEXT_HTML;
|
2012-02-13 21:35:22 +01:00
|
|
|
}
|
|
|
|
|
2015-02-14 23:07:38 +01:00
|
|
|
/* put the verification info in the pgp-signature and every
|
|
|
|
* descendent of a pgp-encrypted part */
|
2012-08-30 19:26:35 +02:00
|
|
|
msgpart.sig_status_report = NULL;
|
2015-02-14 23:07:38 +01:00
|
|
|
if (g_ascii_strcasecmp (msgpart.subtype, "pgp-signature") == 0 ||
|
|
|
|
decrypted) {
|
2012-08-30 19:26:35 +02:00
|
|
|
msgpart.sig_status_report =
|
2020-02-13 23:36:54 +01:00
|
|
|
copy_status_report_maybe (G_OBJECT(parent));
|
2015-02-14 23:07:38 +01:00
|
|
|
if (msgpart.sig_status_report)
|
|
|
|
msgpart.part_type |= MU_MSG_PART_TYPE_SIGNED;
|
|
|
|
}
|
|
|
|
|
2012-08-08 18:13:59 +02:00
|
|
|
msgpart.data = (gpointer)part;
|
2014-10-12 21:38:53 +02:00
|
|
|
msgpart.index = (*index)++;
|
2012-02-13 21:35:22 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
func (msg, &msgpart, user_data);
|
2012-02-13 21:35:22 +01:00
|
|
|
|
2020-02-13 23:36:54 +01:00
|
|
|
mu_msg_part_sig_status_report_destroy (msgpart.sig_status_report);
|
|
|
|
|
2012-02-13 21:35:22 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
/* call 'func' with information about this MIME-part */
|
|
|
|
static gboolean
|
2014-10-19 10:38:49 +02:00
|
|
|
handle_message_part (MuMsg *msg, GMimeMessagePart *mimemsgpart,
|
|
|
|
GMimeObject *parent, MuMsgOptions opts, unsigned *index,
|
|
|
|
gboolean decrypted,
|
2012-08-11 10:54:08 +02:00
|
|
|
MuMsgPartForeachFunc func, gpointer user_data)
|
2012-02-13 21:35:22 +01:00
|
|
|
{
|
2012-08-01 09:45:03 +02:00
|
|
|
MuMsgPart msgpart;
|
2012-09-17 11:43:53 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
memset (&msgpart, 0, sizeof(MuMsgPart));
|
2012-02-13 21:35:22 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
msgpart.type = "message";
|
|
|
|
msgpart.subtype = "rfc822";
|
2014-10-12 21:38:53 +02:00
|
|
|
msgpart.index = (*index)++;
|
2012-07-17 18:16:42 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
/* msgpart.size = 0; /\* maybe calculate this? *\/ */
|
2012-02-13 21:35:22 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
msgpart.part_type = MU_MSG_PART_TYPE_MESSAGE;
|
2012-09-17 11:43:53 +02:00
|
|
|
msgpart.part_type |= get_disposition ((GMimeObject*)mimemsgpart);
|
2011-11-23 23:11:45 +01:00
|
|
|
|
2012-09-17 11:43:53 +02:00
|
|
|
msgpart.data = (gpointer)mimemsgpart;
|
2012-08-01 09:45:03 +02:00
|
|
|
func (msg, &msgpart, user_data);
|
2012-07-24 21:12:22 +02:00
|
|
|
|
2012-09-17 11:43:53 +02:00
|
|
|
if (opts & MU_MSG_OPTION_RECURSE_RFC822) {
|
2014-10-19 10:38:49 +02:00
|
|
|
GMimeMessage *mmsg; /* may return NULL for some
|
|
|
|
* messages */
|
2012-09-17 11:43:53 +02:00
|
|
|
mmsg = g_mime_message_part_get_message (mimemsgpart);
|
|
|
|
if (mmsg)
|
2014-10-10 06:40:37 +02:00
|
|
|
return handle_mime_object (msg,
|
2016-10-25 19:34:08 +02:00
|
|
|
mmsg->mime_part,
|
|
|
|
parent,
|
|
|
|
opts,
|
|
|
|
index,
|
|
|
|
decrypted,
|
|
|
|
func,
|
|
|
|
user_data);
|
2012-09-17 11:43:53 +02:00
|
|
|
}
|
2012-08-11 10:54:08 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
2012-07-24 22:38:37 +02:00
|
|
|
|
2014-10-10 06:40:37 +02:00
|
|
|
static gboolean
|
2015-02-14 23:07:38 +01:00
|
|
|
handle_multipart (MuMsg *msg, GMimeMultipart *mpart, GMimeObject *parent,
|
2016-10-25 19:34:08 +02:00
|
|
|
MuMsgOptions opts, unsigned *index, gboolean decrypted,
|
2014-10-19 02:20:21 +02:00
|
|
|
MuMsgPartForeachFunc func, gpointer user_data)
|
2014-10-10 06:40:37 +02:00
|
|
|
{
|
|
|
|
gboolean res;
|
|
|
|
GMimeObject *part;
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
res = TRUE;
|
|
|
|
for (i = 0; i < mpart->children->len; i++) {
|
|
|
|
part = (GMimeObject *) mpart->children->pdata[i];
|
2015-02-14 23:07:38 +01:00
|
|
|
res &= handle_mime_object (msg, part, parent,
|
2016-10-25 19:34:08 +02:00
|
|
|
opts, index, decrypted,
|
|
|
|
func, user_data);
|
2014-10-10 06:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-07-24 22:38:37 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
static gboolean
|
2014-10-19 02:20:21 +02:00
|
|
|
handle_mime_object (MuMsg *msg, GMimeObject *mobj, GMimeObject *parent,
|
|
|
|
MuMsgOptions opts, unsigned *index, gboolean decrypted,
|
|
|
|
MuMsgPartForeachFunc func, gpointer user_data)
|
2012-08-01 09:45:03 +02:00
|
|
|
{
|
|
|
|
if (GMIME_IS_PART (mobj))
|
|
|
|
return handle_part
|
|
|
|
(msg, GMIME_PART(mobj), parent,
|
2014-10-19 02:20:21 +02:00
|
|
|
opts, index, decrypted, func, user_data);
|
2012-08-01 09:45:03 +02:00
|
|
|
else if (GMIME_IS_MESSAGE_PART (mobj))
|
|
|
|
return handle_message_part
|
|
|
|
(msg, GMIME_MESSAGE_PART(mobj),
|
2014-10-19 02:20:21 +02:00
|
|
|
parent, opts, index, decrypted, func, user_data);
|
2012-08-30 19:26:35 +02:00
|
|
|
else if ((opts & MU_MSG_OPTION_VERIFY) &&
|
2016-10-25 19:34:08 +02:00
|
|
|
GMIME_IS_MULTIPART_SIGNED (mobj)) {
|
2015-02-14 23:07:38 +01:00
|
|
|
check_signature
|
2012-08-30 11:53:52 +02:00
|
|
|
(msg, GMIME_MULTIPART_SIGNED (mobj), opts);
|
2015-02-14 23:07:38 +01:00
|
|
|
return handle_multipart
|
2015-02-21 23:18:24 +01:00
|
|
|
(msg, GMIME_MULTIPART (mobj), mobj, opts,
|
2014-10-19 02:39:32 +02:00
|
|
|
index, decrypted, func, user_data);
|
2014-10-12 21:38:53 +02:00
|
|
|
} else if ((opts & MU_MSG_OPTION_DECRYPT) &&
|
2016-10-25 19:34:08 +02:00
|
|
|
GMIME_IS_MULTIPART_ENCRYPTED (mobj))
|
2012-08-01 09:45:03 +02:00
|
|
|
return handle_encrypted_part
|
|
|
|
(msg, GMIME_MULTIPART_ENCRYPTED (mobj),
|
2014-10-19 02:20:21 +02:00
|
|
|
opts, index, func, user_data);
|
2014-10-10 06:40:37 +02:00
|
|
|
else if (GMIME_IS_MULTIPART (mobj))
|
|
|
|
return handle_multipart
|
2015-02-14 23:07:38 +01:00
|
|
|
(msg, GMIME_MULTIPART (mobj), parent, opts,
|
2014-10-19 02:20:21 +02:00
|
|
|
index, decrypted, func, user_data);
|
2012-08-11 10:54:08 +02:00
|
|
|
return TRUE;
|
2012-08-01 09:45:03 +02:00
|
|
|
}
|
|
|
|
|
2012-08-11 10:54:08 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
gboolean
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_msg_part_foreach (MuMsg *msg, MuMsgOptions opts,
|
2012-08-01 09:45:03 +02:00
|
|
|
MuMsgPartForeachFunc func, gpointer user_data)
|
2010-08-24 23:57:16 +02:00
|
|
|
{
|
2014-10-12 21:38:53 +02:00
|
|
|
unsigned index;
|
|
|
|
|
2014-10-17 14:53:32 +02:00
|
|
|
index = 1;
|
2012-08-01 09:45:03 +02:00
|
|
|
g_return_val_if_fail (msg, FALSE);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2012-07-16 11:55:54 +02:00
|
|
|
if (!mu_msg_load_msg_file (msg, NULL))
|
2012-08-01 09:45:03 +02:00
|
|
|
return FALSE;
|
2011-11-23 23:11:45 +01:00
|
|
|
|
2014-10-10 06:40:37 +02:00
|
|
|
return handle_mime_object (msg,
|
2016-10-25 19:34:08 +02:00
|
|
|
msg->_file->_mime_msg->mime_part,
|
|
|
|
(GMimeObject *) msg->_file->_mime_msg,
|
|
|
|
opts,
|
|
|
|
&index,
|
|
|
|
FALSE,
|
|
|
|
func,
|
|
|
|
user_data);
|
2010-08-24 23:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-11 17:33:31 +01:00
|
|
|
static gboolean
|
2012-02-13 21:35:22 +01:00
|
|
|
write_part_to_fd (GMimePart *part, int fd, GError **err)
|
2010-08-24 23:57:16 +02:00
|
|
|
{
|
2011-05-21 13:12:01 +02:00
|
|
|
GMimeStream *stream;
|
|
|
|
GMimeDataWrapper *wrapper;
|
|
|
|
gboolean rv;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-05-21 13:12:01 +02:00
|
|
|
stream = g_mime_stream_fs_new (fd);
|
|
|
|
if (!GMIME_IS_STREAM(stream)) {
|
2012-04-01 12:08:02 +02:00
|
|
|
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_GMIME,
|
2011-09-12 19:38:40 +02:00
|
|
|
"failed to create stream");
|
2011-05-21 13:12:01 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
g_mime_stream_fs_set_owner (GMIME_STREAM_FS(stream), FALSE);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2017-07-30 16:47:38 +02:00
|
|
|
wrapper = g_mime_part_get_content (part);
|
2011-05-21 13:12:01 +02:00
|
|
|
if (!GMIME_IS_DATA_WRAPPER(wrapper)) {
|
2012-04-01 12:08:02 +02:00
|
|
|
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_GMIME,
|
2011-09-12 19:38:40 +02:00
|
|
|
"failed to create wrapper");
|
2011-05-21 13:12:01 +02:00
|
|
|
g_object_unref (stream);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
g_object_ref (part); /* FIXME: otherwise, the unrefs below
|
|
|
|
* give errors...*/
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2012-04-04 16:56:01 +02:00
|
|
|
if (g_mime_data_wrapper_write_to_stream (wrapper, stream) == -1) {
|
|
|
|
rv = FALSE;
|
2012-04-01 12:08:02 +02:00
|
|
|
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_GMIME,
|
2011-09-12 19:38:40 +02:00
|
|
|
"failed to write to stream");
|
2012-04-04 16:56:01 +02:00
|
|
|
} else
|
|
|
|
rv = TRUE;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2013-04-16 23:07:05 +02:00
|
|
|
/* g_object_unref (wrapper); we don't own it */
|
2011-05-21 13:12:01 +02:00
|
|
|
g_object_unref (stream);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-05-21 13:12:01 +02:00
|
|
|
return rv;
|
2011-01-09 13:55:25 +01:00
|
|
|
}
|
|
|
|
|
2010-08-24 23:57:16 +02:00
|
|
|
|
2012-02-13 21:35:22 +01:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
write_object_to_fd (GMimeObject *obj, int fd, GError **err)
|
|
|
|
{
|
|
|
|
gchar *str;
|
2017-07-30 16:47:38 +02:00
|
|
|
str = g_mime_object_to_string (obj, NULL);
|
2012-02-13 21:35:22 +01:00
|
|
|
|
|
|
|
if (!str) {
|
2012-04-01 12:08:02 +02:00
|
|
|
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_GMIME,
|
2012-02-13 21:35:22 +01:00
|
|
|
"could not get string from object");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write (fd, str, strlen(str)) == -1) {
|
2012-04-01 12:08:02 +02:00
|
|
|
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_GMIME,
|
2012-02-13 21:35:22 +01:00
|
|
|
"failed to write object: %s",
|
2021-07-28 22:58:55 +02:00
|
|
|
g_strerror(errno));
|
2012-02-13 21:35:22 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
static gboolean
|
|
|
|
save_object (GMimeObject *obj, MuMsgOptions opts, const char *fullpath,
|
|
|
|
GError **err)
|
2011-01-09 13:55:25 +01:00
|
|
|
{
|
2011-05-21 13:12:01 +02:00
|
|
|
int fd;
|
|
|
|
gboolean rv;
|
2012-08-01 09:45:03 +02:00
|
|
|
gboolean use_existing, overwrite;
|
|
|
|
|
|
|
|
use_existing = opts & MU_MSG_OPTION_USE_EXISTING;
|
|
|
|
overwrite = opts & MU_MSG_OPTION_OVERWRITE;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-05-21 13:12:01 +02:00
|
|
|
/* don't try to overwrite when we already have it; useful when
|
|
|
|
* you're sure it's not a different file with the same name */
|
|
|
|
if (use_existing && access (fullpath, F_OK) == 0)
|
2011-01-10 23:45:03 +01:00
|
|
|
return TRUE;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-05-21 13:12:01 +02:00
|
|
|
/* ok, try to create the file */
|
|
|
|
fd = mu_util_create_writeable_fd (fullpath, 0600, overwrite);
|
|
|
|
if (fd == -1) {
|
2012-04-01 12:08:02 +02:00
|
|
|
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_FILE,
|
2011-05-21 13:12:01 +02:00
|
|
|
"could not open '%s' for writing: %s",
|
2021-07-28 22:58:55 +02:00
|
|
|
fullpath, errno ? g_strerror(errno) : "error");
|
2011-05-21 13:12:01 +02:00
|
|
|
return FALSE;
|
2011-09-12 19:38:40 +02:00
|
|
|
}
|
2011-05-21 13:12:01 +02:00
|
|
|
|
2012-02-13 21:35:22 +01:00
|
|
|
if (GMIME_IS_PART (obj))
|
|
|
|
rv = write_part_to_fd ((GMimePart*)obj, fd, err);
|
|
|
|
else
|
|
|
|
rv = write_object_to_fd (obj, fd, err);
|
|
|
|
|
2011-09-12 19:38:40 +02:00
|
|
|
if (close (fd) != 0 && !err) { /* don't write on top of old err */
|
2012-04-01 12:08:02 +02:00
|
|
|
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_FILE,
|
2011-05-21 13:12:01 +02:00
|
|
|
"could not close '%s': %s",
|
2021-07-28 22:58:55 +02:00
|
|
|
fullpath, errno ? g_strerror(errno) : "error");
|
2011-05-21 13:12:01 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-09-12 19:38:40 +02:00
|
|
|
return rv;
|
2011-01-10 23:45:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gchar*
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_msg_part_get_path (MuMsg *msg, MuMsgOptions opts,
|
|
|
|
const char* targetdir, unsigned index, GError **err)
|
2011-01-10 23:45:03 +01:00
|
|
|
{
|
2011-05-21 13:12:01 +02:00
|
|
|
char *fname, *filepath;
|
2012-02-13 21:35:22 +01:00
|
|
|
GMimeObject* mobj;
|
2011-05-21 13:12:01 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
g_return_val_if_fail (msg, NULL);
|
2011-09-19 23:21:37 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
if (!mu_msg_load_msg_file (msg, NULL))
|
2011-05-21 13:12:01 +02:00
|
|
|
return NULL;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
mobj = get_mime_object_at_index (msg, opts, index);
|
|
|
|
if (!mobj){
|
2012-07-20 10:54:37 +02:00
|
|
|
mu_util_g_set_error (err, MU_ERROR_GMIME,
|
2012-08-01 09:45:03 +02:00
|
|
|
"cannot find part %u", index);
|
2012-02-13 21:35:22 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-05-21 13:12:01 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
fname = mime_part_get_filename (mobj, index, TRUE);
|
2011-05-21 13:12:01 +02:00
|
|
|
filepath = g_build_path (G_DIR_SEPARATOR_S, targetdir ? targetdir : "",
|
|
|
|
fname, NULL);
|
2014-10-10 17:34:57 +02:00
|
|
|
|
|
|
|
/* Unref it since it was referenced earlier by
|
|
|
|
* get_mime_object_at_index */
|
|
|
|
g_object_unref (mobj);
|
2011-05-21 13:12:01 +02:00
|
|
|
g_free (fname);
|
|
|
|
|
|
|
|
return filepath;
|
2010-08-24 23:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-10 23:45:03 +01:00
|
|
|
|
|
|
|
gchar*
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_msg_part_get_cache_path (MuMsg *msg, MuMsgOptions opts, guint partid,
|
2012-08-01 09:45:03 +02:00
|
|
|
GError **err)
|
2010-08-24 23:57:16 +02:00
|
|
|
{
|
2011-05-21 13:12:01 +02:00
|
|
|
char *dirname, *filepath;
|
|
|
|
const char* path;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-05-21 13:12:01 +02:00
|
|
|
g_return_val_if_fail (msg, NULL);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2012-07-16 11:55:54 +02:00
|
|
|
if (!mu_msg_load_msg_file (msg, NULL))
|
2011-09-19 23:21:37 +02:00
|
|
|
return NULL;
|
|
|
|
|
2011-05-21 13:12:01 +02:00
|
|
|
path = mu_msg_get_path (msg);
|
2011-01-10 23:45:03 +01:00
|
|
|
|
2011-05-21 13:12:01 +02:00
|
|
|
/* g_compute_checksum_for_string may be better, but requires
|
|
|
|
* rel. new glib (2.16) */
|
2016-10-25 19:34:08 +02:00
|
|
|
dirname = g_strdup_printf ("%s%c%x%c%u",
|
2011-05-21 13:12:01 +02:00
|
|
|
mu_util_cache_dir(), G_DIR_SEPARATOR,
|
|
|
|
g_str_hash (path), G_DIR_SEPARATOR,
|
|
|
|
partid);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-08-09 06:54:23 +02:00
|
|
|
if (!mu_util_create_dir_maybe (dirname, 0700, FALSE)) {
|
2012-08-09 08:38:22 +02:00
|
|
|
mu_util_g_set_error (err, MU_ERROR_FILE,
|
|
|
|
"failed to create dir %s", dirname);
|
2011-01-10 23:45:03 +01:00
|
|
|
g_free (dirname);
|
2011-05-21 13:12:01 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
filepath = mu_msg_part_get_path (msg, opts, dirname, partid, err);
|
2011-05-21 13:12:01 +02:00
|
|
|
g_free (dirname);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-05-21 13:12:01 +02:00
|
|
|
return filepath;
|
2010-08-24 23:57:16 +02:00
|
|
|
}
|
|
|
|
|
2011-01-10 23:45:03 +01:00
|
|
|
|
2010-08-24 23:57:16 +02:00
|
|
|
gboolean
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_msg_part_save (MuMsg *msg, MuMsgOptions opts,
|
2012-08-01 09:45:03 +02:00
|
|
|
const char *fullpath, guint partidx, GError **err)
|
2010-08-24 23:57:16 +02:00
|
|
|
{
|
2020-02-15 13:10:59 +01:00
|
|
|
gboolean rv;
|
|
|
|
GMimeObject *part;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-05-21 13:12:01 +02:00
|
|
|
g_return_val_if_fail (msg, FALSE);
|
|
|
|
g_return_val_if_fail (fullpath, FALSE);
|
2012-08-01 09:45:03 +02:00
|
|
|
g_return_val_if_fail (!((opts & MU_MSG_OPTION_OVERWRITE) &&
|
2016-10-25 19:34:08 +02:00
|
|
|
(opts & MU_MSG_OPTION_USE_EXISTING)), FALSE);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2020-02-15 13:10:59 +01:00
|
|
|
rv = FALSE;
|
|
|
|
|
2012-07-17 18:16:42 +02:00
|
|
|
if (!mu_msg_load_msg_file (msg, err))
|
2020-02-15 13:10:59 +01:00
|
|
|
return rv;
|
2011-09-19 23:21:37 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
part = get_mime_object_at_index (msg, opts, partidx);
|
2012-09-21 11:35:53 +02:00
|
|
|
|
|
|
|
/* special case: convert a message-part into a message */
|
|
|
|
if (GMIME_IS_MESSAGE_PART (part))
|
|
|
|
part = (GMimeObject*)g_mime_message_part_get_message
|
|
|
|
(GMIME_MESSAGE_PART (part));
|
|
|
|
|
2020-02-15 13:10:59 +01:00
|
|
|
if (!part)
|
2015-06-30 23:28:26 +02:00
|
|
|
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_GMIME,
|
|
|
|
"part %u does not exist", partidx);
|
2020-02-15 13:10:59 +01:00
|
|
|
else if (!GMIME_IS_PART(part) && !GMIME_IS_MESSAGE(part))
|
2012-04-01 12:08:02 +02:00
|
|
|
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_GMIME,
|
2012-02-13 21:35:22 +01:00
|
|
|
"unexpected type %s for part %u",
|
|
|
|
G_OBJECT_TYPE_NAME((GObject*)part),
|
|
|
|
partidx);
|
2020-02-15 13:10:59 +01:00
|
|
|
else
|
|
|
|
rv = save_object (part, opts, fullpath, err);
|
2011-01-10 23:45:03 +01:00
|
|
|
|
2020-02-15 13:10:59 +01:00
|
|
|
g_clear_object(&part);
|
2012-09-21 11:35:53 +02:00
|
|
|
|
2020-02-15 13:10:59 +01:00
|
|
|
return rv;
|
2011-01-10 23:45:03 +01:00
|
|
|
}
|
|
|
|
|
2012-01-15 13:10:04 +01:00
|
|
|
|
|
|
|
gchar*
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_msg_part_save_temp (MuMsg *msg, MuMsgOptions opts, guint partidx,
|
2014-10-19 10:38:49 +02:00
|
|
|
GError **err)
|
2012-01-15 13:10:04 +01:00
|
|
|
{
|
|
|
|
gchar *filepath;
|
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
filepath = mu_msg_part_get_cache_path (msg, opts, partidx, err);
|
|
|
|
if (!filepath)
|
2012-01-15 13:10:04 +01:00
|
|
|
return NULL;
|
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
if (!mu_msg_part_save (msg, opts, filepath, partidx, err)) {
|
2012-01-15 13:10:04 +01:00
|
|
|
g_free (filepath);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath;
|
|
|
|
}
|
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
static gboolean
|
|
|
|
match_cid (MuMsgPart *mpart, const char *cid)
|
2011-01-10 23:45:03 +01:00
|
|
|
{
|
2012-08-01 09:45:03 +02:00
|
|
|
const char *this_cid;
|
2010-08-24 23:57:16 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
this_cid = g_mime_object_get_content_id ((GMimeObject*)mpart->data);
|
2011-01-10 23:45:03 +01:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
return g_strcmp0 (this_cid, cid) ? TRUE : FALSE;
|
2011-01-10 23:45:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_msg_find_index_for_cid (MuMsg *msg, MuMsgOptions opts,
|
2014-10-19 10:38:49 +02:00
|
|
|
const char *sought_cid)
|
2011-01-10 23:45:03 +01:00
|
|
|
{
|
2011-05-21 13:12:01 +02:00
|
|
|
const char* cid;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-05-22 12:42:19 +02:00
|
|
|
g_return_val_if_fail (msg, -1);
|
2011-05-21 13:12:01 +02:00
|
|
|
g_return_val_if_fail (sought_cid, -1);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2012-07-16 11:55:54 +02:00
|
|
|
if (!mu_msg_load_msg_file (msg, NULL))
|
2011-09-19 23:21:37 +02:00
|
|
|
return -1;
|
|
|
|
|
2011-05-21 13:12:01 +02:00
|
|
|
cid = g_str_has_prefix (sought_cid, "cid:") ?
|
2011-09-12 19:38:40 +02:00
|
|
|
sought_cid + 4 : sought_cid;
|
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
return get_matching_part_index (msg, opts,
|
|
|
|
(MuMsgPartMatchFunc)match_cid,
|
2016-10-25 19:34:08 +02:00
|
|
|
(gpointer)cid);
|
2011-01-10 23:45:03 +01:00
|
|
|
}
|
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
struct _RxMatchData {
|
|
|
|
GSList *_lst;
|
2011-05-22 12:42:19 +02:00
|
|
|
const GRegex *_rx;
|
|
|
|
guint _idx;
|
|
|
|
};
|
2012-08-01 09:45:03 +02:00
|
|
|
typedef struct _RxMatchData RxMatchData;
|
2011-01-10 23:45:03 +01:00
|
|
|
|
2011-05-22 12:42:19 +02:00
|
|
|
|
|
|
|
static void
|
2012-08-01 09:45:03 +02:00
|
|
|
match_filename_rx (MuMsg *msg, MuMsgPart *mpart, RxMatchData *mdata)
|
2011-05-21 13:12:01 +02:00
|
|
|
{
|
2012-08-01 09:45:03 +02:00
|
|
|
char *fname;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
fname = mu_msg_part_get_filename (mpart, FALSE);
|
|
|
|
if (!fname)
|
2012-02-13 21:35:22 +01:00
|
|
|
return;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2020-11-28 09:15:49 +01:00
|
|
|
if (g_regex_match (mdata->_rx, fname, (GRegexMatchFlags)0, NULL))
|
2011-05-22 12:42:19 +02:00
|
|
|
mdata->_lst = g_slist_prepend (mdata->_lst,
|
2012-08-01 09:45:03 +02:00
|
|
|
GUINT_TO_POINTER(mpart->index));
|
|
|
|
g_free (fname);
|
2011-05-21 13:12:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-22 12:42:19 +02:00
|
|
|
GSList*
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_msg_find_files (MuMsg *msg, MuMsgOptions opts, const GRegex *pattern)
|
2011-05-21 13:12:01 +02:00
|
|
|
{
|
2012-08-01 09:45:03 +02:00
|
|
|
RxMatchData mdata;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-05-22 12:42:19 +02:00
|
|
|
g_return_val_if_fail (msg, NULL);
|
|
|
|
g_return_val_if_fail (pattern, NULL);
|
|
|
|
|
2012-07-16 11:55:54 +02:00
|
|
|
if (!mu_msg_load_msg_file (msg, NULL))
|
2011-09-19 23:21:37 +02:00
|
|
|
return NULL;
|
|
|
|
|
2011-05-22 12:42:19 +02:00
|
|
|
mdata._lst = NULL;
|
|
|
|
mdata._rx = pattern;
|
|
|
|
mdata._idx = 0;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2012-08-01 09:45:03 +02:00
|
|
|
mu_msg_part_foreach (msg, opts,
|
|
|
|
(MuMsgPartForeachFunc)match_filename_rx,
|
|
|
|
&mdata);
|
2011-05-22 12:42:19 +02:00
|
|
|
return mdata._lst;
|
2011-05-21 13:12:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-11 22:17:03 +01:00
|
|
|
gboolean
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_msg_part_maybe_attachment (MuMsgPart *part)
|
2011-01-11 22:17:03 +01:00
|
|
|
{
|
2011-05-21 13:12:01 +02:00
|
|
|
g_return_val_if_fail (part, FALSE);
|
2011-01-11 22:17:03 +01:00
|
|
|
|
2012-08-09 10:45:04 +02:00
|
|
|
/* attachments must be leaf parts */
|
2012-09-25 14:20:26 +02:00
|
|
|
if (!(part->part_type & MU_MSG_PART_TYPE_LEAF))
|
2011-05-21 13:12:01 +02:00
|
|
|
return FALSE;
|
2011-01-11 22:17:03 +01:00
|
|
|
|
2012-09-27 16:31:49 +02:00
|
|
|
/* parts other than text/plain, text/html are considered
|
|
|
|
* attachments as well */
|
|
|
|
if (!(part->part_type & MU_MSG_PART_TYPE_TEXT_PLAIN) &&
|
|
|
|
!(part->part_type & MU_MSG_PART_TYPE_TEXT_HTML))
|
|
|
|
return TRUE;
|
2012-08-09 10:45:04 +02:00
|
|
|
|
2012-09-25 14:20:26 +02:00
|
|
|
return part->part_type & MU_MSG_PART_TYPE_ATTACHMENT ? TRUE : FALSE;
|
2011-01-11 22:17:03 +01:00
|
|
|
}
|