2011-05-19 18:57:26 +02:00
|
|
|
/* -*- mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
|
|
|
**
|
2011-05-09 01:58:33 +02:00
|
|
|
** Copyright (C) 2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2010-12-07 22:14:11 +01: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.
|
|
|
|
**
|
|
|
|
*/
|
2011-05-09 01:58:33 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2011-05-14 17:07:51 +02:00
|
|
|
/* hopefully, the should get us a sane PATH_MAX */
|
|
|
|
#include <limits.h>
|
|
|
|
/* not all systems provide PATH_MAX in limits.h */
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#include <sys/param.h>
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX MAXPATHLEN
|
|
|
|
#endif /*!PATH_MAX */
|
|
|
|
#endif /*PATH_MAX */
|
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
#include <gmime/gmime.h>
|
|
|
|
#include "mu-util.h"
|
|
|
|
#include "mu-str.h"
|
|
|
|
#include "mu-maildir.h"
|
|
|
|
#include "mu-msg-priv.h"
|
|
|
|
|
2011-05-14 17:07:51 +02:00
|
|
|
|
2011-05-09 08:20:27 +02:00
|
|
|
static gboolean init_file_metadata (MuMsgFile *self, const char* path,
|
|
|
|
const char *mdir, GError **err);
|
2011-05-14 17:07:51 +02:00
|
|
|
static gboolean init_mime_msg (MuMsgFile *msg, const char *path, GError **err);
|
2011-05-09 01:58:33 +02:00
|
|
|
|
2011-05-18 22:35:44 +02:00
|
|
|
|
2011-10-23 23:20:05 +02:00
|
|
|
MuMsgFile*
|
2011-05-09 01:58:33 +02:00
|
|
|
mu_msg_file_new (const char* filepath, const char *mdir, GError **err)
|
|
|
|
{
|
|
|
|
MuMsgFile *self;
|
2011-05-14 17:07:51 +02:00
|
|
|
|
2011-05-18 22:35:44 +02:00
|
|
|
g_return_val_if_fail (filepath, NULL);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
|
|
|
self = g_slice_new0 (MuMsgFile);
|
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
if (!init_file_metadata (self, filepath, mdir, err)) {
|
|
|
|
mu_msg_file_destroy (self);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-14 17:07:51 +02:00
|
|
|
if (!init_mime_msg (self, filepath, err)) {
|
2011-05-09 01:58:33 +02:00
|
|
|
mu_msg_file_destroy (self);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
mu_msg_file_destroy (MuMsgFile *self)
|
|
|
|
{
|
|
|
|
if (!self)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (self->_mime_msg)
|
|
|
|
g_object_unref (self->_mime_msg);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
g_slice_free (MuMsgFile, self);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
init_file_metadata (MuMsgFile *self, const char* path, const gchar* mdir,
|
|
|
|
GError **err)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
if (access (path, R_OK) != 0) {
|
|
|
|
g_set_error (err, 0, MU_ERROR_FILE,
|
|
|
|
"cannot read file %s: %s",
|
|
|
|
path, strerror(errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat (path, &statbuf) < 0) {
|
|
|
|
g_set_error (err, 0, MU_ERROR_FILE,
|
|
|
|
"cannot stat %s: %s",
|
|
|
|
path, strerror(errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
if (!S_ISREG(statbuf.st_mode)) {
|
|
|
|
g_set_error (err, 0, MU_ERROR_FILE,
|
|
|
|
"not a regular file: %s", path);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-14 17:07:51 +02:00
|
|
|
self->_timestamp = statbuf.st_mtime;
|
|
|
|
self->_size = (size_t)statbuf.st_size;
|
2011-05-09 01:58:33 +02:00
|
|
|
|
2011-05-14 17:07:51 +02:00
|
|
|
strncpy (self->_path, path, PATH_MAX);
|
2011-10-23 23:20:05 +02:00
|
|
|
strncpy (self->_maildir, mdir ? mdir : "", PATH_MAX);
|
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static GMimeStream*
|
2011-05-14 17:07:51 +02:00
|
|
|
get_mime_stream (MuMsgFile *self, const char *path, GError **err)
|
2011-05-09 01:58:33 +02:00
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
GMimeStream *stream;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-14 17:07:51 +02:00
|
|
|
file = fopen (path, "r");
|
2011-05-09 01:58:33 +02:00
|
|
|
if (!file) {
|
|
|
|
g_set_error (err, 0, MU_ERROR_FILE,
|
|
|
|
"cannot open %s: %s",
|
2011-05-14 17:07:51 +02:00
|
|
|
path, strerror (errno));
|
2011-05-09 01:58:33 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
stream = g_mime_stream_file_new (file);
|
|
|
|
if (!stream) {
|
|
|
|
g_set_error (err, 0, MU_ERROR_GMIME,
|
|
|
|
"cannot create mime stream for %s",
|
2011-05-14 17:07:51 +02:00
|
|
|
path);
|
2011-05-09 01:58:33 +02:00
|
|
|
fclose (file);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-05-14 17:07:51 +02:00
|
|
|
init_mime_msg (MuMsgFile *self, const char* path, GError **err)
|
2011-05-09 01:58:33 +02:00
|
|
|
{
|
|
|
|
GMimeStream *stream;
|
|
|
|
GMimeParser *parser;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-14 17:07:51 +02:00
|
|
|
stream = get_mime_stream (self, path, err);
|
2011-05-09 01:58:33 +02:00
|
|
|
if (!stream)
|
|
|
|
return FALSE;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
parser = g_mime_parser_new_with_stream (stream);
|
|
|
|
g_object_unref (stream);
|
|
|
|
if (!parser) {
|
|
|
|
g_set_error (err, 0, MU_ERROR_GMIME,
|
2011-05-14 17:07:51 +02:00
|
|
|
"%s: cannot create mime parser for %s",
|
|
|
|
__FUNCTION__, path);
|
2011-05-09 01:58:33 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
self->_mime_msg = g_mime_parser_construct_message (parser);
|
|
|
|
g_object_unref (parser);
|
|
|
|
if (!self->_mime_msg) {
|
|
|
|
g_set_error (err, 0, MU_ERROR_GMIME,
|
2011-05-14 17:07:51 +02:00
|
|
|
"%s: cannot construct mime message for %s",
|
|
|
|
__FUNCTION__, path);
|
2011-05-09 01:58:33 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-14 17:07:51 +02:00
|
|
|
static char*
|
2011-05-09 01:58:33 +02:00
|
|
|
get_recipient (MuMsgFile *self, GMimeRecipientType rtype)
|
|
|
|
{
|
2011-05-18 21:20:39 +02:00
|
|
|
char *recip;
|
|
|
|
InternetAddressList *recips;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-18 21:20:39 +02:00
|
|
|
recips = g_mime_message_get_recipients (self->_mime_msg, rtype);
|
|
|
|
|
|
|
|
/* FALSE --> don't encode */
|
|
|
|
recip = (char*)internet_address_list_to_string (recips, FALSE);
|
2011-07-17 13:35:59 +02:00
|
|
|
|
|
|
|
if (recip && !g_utf8_validate (recip, -1, NULL)) {
|
|
|
|
g_debug ("invalid recipient in %s\n", self->_path);
|
|
|
|
mu_str_asciify_in_place (recip); /* ugly... */
|
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-18 21:20:39 +02:00
|
|
|
if (mu_str_is_empty(recip)) {
|
|
|
|
g_free (recip);
|
2011-05-09 01:58:33 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-05-18 21:20:39 +02:00
|
|
|
return recip;
|
2011-05-09 01:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-13 11:48:40 +01:00
|
|
|
looks_like_attachment (GMimeObject *part)
|
2011-05-09 01:58:33 +02:00
|
|
|
{
|
|
|
|
const char *str;
|
2011-11-13 11:48:40 +01:00
|
|
|
GMimeContentDisposition *disp;
|
|
|
|
GMimeContentType *ct;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-11-13 11:48:40 +01:00
|
|
|
disp = g_mime_object_get_content_disposition (GMIME_OBJECT(part));
|
2011-05-09 01:58:33 +02:00
|
|
|
if (!GMIME_IS_CONTENT_DISPOSITION(disp))
|
2011-11-13 11:48:40 +01:00
|
|
|
return FALSE;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
str = g_mime_content_disposition_get_disposition (disp);
|
|
|
|
if (!str)
|
2011-11-13 11:48:40 +01:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ct = g_mime_object_get_content_type (part);
|
|
|
|
if (!ct)
|
|
|
|
return FALSE; /* ignore this part... */
|
|
|
|
|
|
|
|
/* note, some mailers use ATTACHMENT, INLINE instead of their
|
|
|
|
* more common lower-case counterparts */
|
|
|
|
if (g_ascii_strcasecmp(str, GMIME_DISPOSITION_ATTACHMENT) == 0)
|
2011-05-09 01:58:33 +02:00
|
|
|
return TRUE;
|
2011-11-13 11:48:40 +01:00
|
|
|
|
|
|
|
if (g_ascii_strcasecmp(str, GMIME_DISPOSITION_INLINE) == 0) {
|
|
|
|
/* some inline parts are also considered attachments... */
|
|
|
|
int i;
|
|
|
|
const char* att_types[][2] = {
|
|
|
|
{"image", "*"},
|
|
|
|
{"application", "*"},
|
|
|
|
{"message", "*"}};
|
|
|
|
|
|
|
|
for (i = 0; i != G_N_ELEMENTS (att_types); ++i)
|
|
|
|
if (g_mime_content_type_is_type (ct,
|
|
|
|
att_types[i][0],
|
|
|
|
att_types[i][1]))
|
|
|
|
return TRUE; /* looks like an attachment */
|
2011-05-09 01:58:33 +02:00
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-11-13 11:48:40 +01:00
|
|
|
return FALSE; /* does not look like an attachment */
|
2011-05-09 01:58:33 +02:00
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
|
|
|
|
static void
|
2011-08-11 21:44:23 +02:00
|
|
|
msg_cflags_cb (GMimeObject *parent, GMimeObject *part, MuFlags *flags)
|
2011-05-09 01:58:33 +02:00
|
|
|
{
|
2011-08-11 21:44:23 +02:00
|
|
|
if (*flags & MU_FLAG_HAS_ATTACH)
|
2011-05-09 01:58:33 +02:00
|
|
|
return;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
if (!GMIME_IS_PART(part))
|
|
|
|
return;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-11-13 11:48:40 +01:00
|
|
|
if (!(*flags & MU_FLAG_HAS_ATTACH) && looks_like_attachment(part))
|
2011-08-11 21:44:23 +02:00
|
|
|
*flags |= MU_FLAG_HAS_ATTACH;
|
2011-05-09 01:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-11 21:44:23 +02:00
|
|
|
static MuFlags
|
2011-05-09 01:58:33 +02:00
|
|
|
get_content_flags (MuMsgFile *self)
|
|
|
|
{
|
|
|
|
GMimeContentType *ctype;
|
2011-08-11 21:44:23 +02:00
|
|
|
MuFlags flags;
|
2011-05-09 01:58:33 +02:00
|
|
|
GMimeObject *part;
|
|
|
|
|
|
|
|
if (!GMIME_IS_MESSAGE(self->_mime_msg))
|
2011-08-11 21:44:23 +02:00
|
|
|
return MU_FLAG_NONE;
|
2011-05-09 01:58:33 +02:00
|
|
|
|
|
|
|
flags = 0;
|
|
|
|
g_mime_message_foreach (self->_mime_msg,
|
2011-10-23 23:20:05 +02:00
|
|
|
(GMimeObjectForeachFunc)msg_cflags_cb,
|
2011-05-09 01:58:33 +02:00
|
|
|
&flags);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
/* note: signed or encrypted status for a message is determined by
|
|
|
|
* the top-level mime-part
|
|
|
|
*/
|
|
|
|
if ((part = g_mime_message_get_mime_part(self->_mime_msg))) {
|
|
|
|
ctype = g_mime_object_get_content_type
|
|
|
|
(GMIME_OBJECT(part));
|
|
|
|
if (!ctype) {
|
|
|
|
g_warning ("not a content type!");
|
|
|
|
return 0;
|
2011-10-23 23:20:05 +02:00
|
|
|
}
|
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
if (ctype) {
|
|
|
|
if (g_mime_content_type_is_type
|
2011-10-23 23:20:05 +02:00
|
|
|
(ctype,"*", "signed"))
|
2011-08-11 21:44:23 +02:00
|
|
|
flags |= MU_FLAG_SIGNED;
|
2011-05-09 01:58:33 +02:00
|
|
|
if (g_mime_content_type_is_type
|
2011-10-23 23:20:05 +02:00
|
|
|
(ctype,"*", "encrypted"))
|
2011-08-11 21:44:23 +02:00
|
|
|
flags |= MU_FLAG_ENCRYPTED;
|
2011-05-09 01:58:33 +02:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
g_warning ("no top level mime part found");
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-11 21:44:23 +02:00
|
|
|
static MuFlags
|
2011-05-09 01:58:33 +02:00
|
|
|
get_flags (MuMsgFile *self)
|
|
|
|
{
|
2011-08-11 21:44:23 +02:00
|
|
|
MuFlags flags;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-08-13 09:40:31 +02:00
|
|
|
g_return_val_if_fail (self, MU_FLAG_INVALID);
|
2011-05-14 17:07:51 +02:00
|
|
|
|
|
|
|
flags = mu_maildir_get_flags_from_path (self->_path);
|
|
|
|
flags |= get_content_flags (self);
|
2011-08-13 09:40:31 +02:00
|
|
|
|
|
|
|
/* pseudo-flag --> unread means either NEW or NOT SEEN, just
|
|
|
|
* for searching convenience */
|
2011-08-15 23:45:42 +02:00
|
|
|
if ((flags & MU_FLAG_NEW) || !(flags & MU_FLAG_SEEN))
|
2011-08-13 09:40:31 +02:00
|
|
|
flags |= MU_FLAG_UNREAD;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-14 17:07:51 +02:00
|
|
|
return flags;
|
2011-05-09 01:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
get_size (MuMsgFile *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (self, 0);
|
|
|
|
|
|
|
|
return self->_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char*
|
|
|
|
to_lower (char *s)
|
|
|
|
{
|
|
|
|
char *t = s;
|
|
|
|
while (t&&*t) {
|
|
|
|
t[0] = g_ascii_tolower(t[0]);
|
|
|
|
++t;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char*
|
|
|
|
get_prio_header_field (MuMsgFile *self)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
GMimeObject *obj;
|
|
|
|
|
|
|
|
obj = GMIME_OBJECT(self->_mime_msg);
|
|
|
|
|
|
|
|
str = g_mime_object_get_header (obj, "X-Priority");
|
|
|
|
if (!str)
|
|
|
|
str = g_mime_object_get_header (obj, "X-MSMail-Priority");
|
|
|
|
if (!str)
|
|
|
|
str = g_mime_object_get_header (obj, "Importance");
|
|
|
|
if (!str)
|
|
|
|
str = g_mime_object_get_header (obj, "Precedence");
|
2011-10-23 23:20:05 +02:00
|
|
|
if (str)
|
2011-05-09 01:58:33 +02:00
|
|
|
return (to_lower(g_strdup(str)));
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MuMsgPrio
|
|
|
|
parse_prio_str (const char* priostr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct {
|
2011-05-14 17:07:51 +02:00
|
|
|
const char* _str;
|
|
|
|
MuMsgPrio _prio;
|
2011-05-09 01:58:33 +02:00
|
|
|
} str_prio[] = {
|
|
|
|
{ "high", MU_MSG_PRIO_HIGH },
|
|
|
|
{ "1", MU_MSG_PRIO_HIGH },
|
|
|
|
{ "2", MU_MSG_PRIO_HIGH },
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
{ "normal", MU_MSG_PRIO_NORMAL },
|
|
|
|
{ "3", MU_MSG_PRIO_NORMAL },
|
|
|
|
|
|
|
|
{ "low", MU_MSG_PRIO_LOW },
|
|
|
|
{ "list", MU_MSG_PRIO_LOW },
|
|
|
|
{ "bulk", MU_MSG_PRIO_LOW },
|
|
|
|
{ "4", MU_MSG_PRIO_LOW },
|
|
|
|
{ "5", MU_MSG_PRIO_LOW }
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i != G_N_ELEMENTS(str_prio); ++i)
|
|
|
|
if (g_strstr_len (priostr, -1, str_prio[i]._str) != NULL)
|
|
|
|
return str_prio[i]._prio;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
/* e.g., last-fm uses 'fm-user'... as precedence */
|
|
|
|
return MU_MSG_PRIO_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static MuMsgPrio
|
|
|
|
get_prio (MuMsgFile *self)
|
|
|
|
{
|
2011-05-14 17:07:51 +02:00
|
|
|
MuMsgPrio prio;
|
2011-05-09 01:58:33 +02:00
|
|
|
char* priostr;
|
|
|
|
|
2011-05-14 17:07:51 +02:00
|
|
|
g_return_val_if_fail (self, MU_MSG_PRIO_NONE);
|
2011-05-09 01:58:33 +02:00
|
|
|
|
|
|
|
priostr = get_prio_header_field (self);
|
|
|
|
if (!priostr)
|
|
|
|
return MU_MSG_PRIO_NORMAL;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-14 17:07:51 +02:00
|
|
|
prio = parse_prio_str (priostr);
|
2011-05-09 01:58:33 +02:00
|
|
|
g_free (priostr);
|
|
|
|
|
2011-05-14 17:07:51 +02:00
|
|
|
return prio;
|
2011-05-09 01:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct _GetBodyData {
|
|
|
|
GMimeObject *_txt_part, *_html_part;
|
|
|
|
gboolean _want_html;
|
|
|
|
};
|
|
|
|
typedef struct _GetBodyData GetBodyData;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_body_cb (GMimeObject *parent, GMimeObject *part, GetBodyData *data)
|
|
|
|
{
|
2011-10-23 23:20:05 +02:00
|
|
|
GMimeContentType *ct;
|
2011-05-09 01:58:33 +02:00
|
|
|
|
|
|
|
/* already found what we're looking for? */
|
|
|
|
if ((data->_want_html && data->_html_part != NULL) ||
|
|
|
|
(!data->_want_html && data->_txt_part != NULL))
|
|
|
|
return;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
ct = g_mime_object_get_content_type (part);
|
|
|
|
if (!GMIME_IS_CONTENT_TYPE(ct)) {
|
|
|
|
g_warning ("not a content type!");
|
|
|
|
return;
|
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
if (looks_like_attachment (part))
|
|
|
|
return; /* not the body */
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
/* is it right content type? */
|
|
|
|
if (g_mime_content_type_is_type (ct, "text", "plain"))
|
|
|
|
data->_txt_part = part;
|
|
|
|
else if (g_mime_content_type_is_type (ct, "text", "html"))
|
|
|
|
data->_html_part = part;
|
|
|
|
else
|
|
|
|
return; /* wrong type */
|
2011-10-23 23:20:05 +02:00
|
|
|
}
|
2011-05-09 01:58:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* NOTE: buffer will be *freed* or returned unchanged */
|
|
|
|
static char*
|
|
|
|
convert_to_utf8 (GMimePart *part, char *buffer)
|
|
|
|
{
|
|
|
|
GMimeContentType *ctype;
|
|
|
|
const char* charset;
|
|
|
|
unsigned char *cur;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
/* optimization: if the buffer is plain ascii, no conversion
|
|
|
|
* is done... */
|
|
|
|
for (cur = (unsigned char*)buffer; *cur && *cur < 0x80; ++cur);
|
|
|
|
if (*cur == '\0')
|
|
|
|
return buffer;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
ctype = g_mime_object_get_content_type (GMIME_OBJECT(part));
|
|
|
|
g_return_val_if_fail (GMIME_IS_CONTENT_TYPE(ctype), NULL);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
charset = g_mime_content_type_get_parameter (ctype, "charset");
|
2011-10-23 23:20:05 +02:00
|
|
|
if (charset)
|
2011-05-09 01:58:33 +02:00
|
|
|
charset = g_mime_charset_iconv_name (charset);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
/* of course, the charset specified may be incorrect... */
|
|
|
|
if (charset) {
|
2011-07-17 13:35:59 +02:00
|
|
|
char *utf8 = mu_str_convert_to_utf8 (buffer, charset);
|
2011-05-09 01:58:33 +02:00
|
|
|
if (utf8) {
|
|
|
|
g_free (buffer);
|
|
|
|
return utf8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hmmm.... no charset at all, or conversion failed; ugly
|
2011-07-17 13:35:59 +02:00
|
|
|
* hack: replace all non-ascii chars with '.' */
|
|
|
|
mu_str_asciify_in_place (buffer);
|
2011-05-09 01:58:33 +02:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gchar*
|
|
|
|
stream_to_string (GMimeStream *stream, size_t buflen)
|
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
ssize_t bytes;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
buffer = g_new(char, buflen + 1);
|
|
|
|
g_mime_stream_reset (stream);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
/* we read everything in one go */
|
|
|
|
bytes = g_mime_stream_read (stream, buffer, buflen);
|
|
|
|
if (bytes < 0) {
|
|
|
|
g_warning ("%s: failed to read from stream", __FUNCTION__);
|
|
|
|
g_free (buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
|
|
|
buffer[bytes]='\0';
|
2011-05-09 01:58:33 +02:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-23 23:11:45 +01:00
|
|
|
gchar*
|
|
|
|
mu_msg_mime_part_to_string (GMimePart *part, gboolean *err)
|
2011-05-09 01:58:33 +02:00
|
|
|
{
|
|
|
|
GMimeDataWrapper *wrapper;
|
|
|
|
GMimeStream *stream = NULL;
|
|
|
|
ssize_t buflen;
|
|
|
|
char *buffer = NULL;
|
|
|
|
|
|
|
|
*err = TRUE;
|
|
|
|
g_return_val_if_fail (GMIME_IS_PART(part), NULL);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
wrapper = g_mime_part_get_content_object (part);
|
|
|
|
if (!wrapper) {
|
|
|
|
/* this happens with invalid mails */
|
|
|
|
g_debug ("failed to create data wrapper");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = g_mime_stream_mem_new ();
|
|
|
|
if (!stream) {
|
|
|
|
g_warning ("failed to create mem stream");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
buflen = g_mime_data_wrapper_write_to_stream (wrapper, stream);
|
|
|
|
if (buflen <= 0) {/* empty buffer, not an error */
|
|
|
|
*err = FALSE;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
buffer = stream_to_string (stream, (size_t)buflen);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
/* convert_to_utf8 will free the old 'buffer' if needed */
|
|
|
|
buffer = convert_to_utf8 (part, buffer);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
*err = FALSE;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
|
|
|
cleanup:
|
2011-05-09 01:58:33 +02:00
|
|
|
if (stream)
|
|
|
|
g_object_unref (G_OBJECT(stream));
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-23 23:11:45 +01:00
|
|
|
GMimePart*
|
|
|
|
mu_msg_mime_get_body_part (GMimeMessage *msg, gboolean want_html)
|
2011-05-09 01:58:33 +02:00
|
|
|
{
|
|
|
|
GetBodyData data;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-11-23 23:11:45 +01:00
|
|
|
g_return_val_if_fail (GMIME_IS_MESSAGE(msg), NULL);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
memset (&data, 0, sizeof(GetBodyData));
|
|
|
|
data._want_html = want_html;
|
|
|
|
|
2011-11-23 23:11:45 +01:00
|
|
|
g_mime_message_foreach (msg,
|
2011-05-09 01:58:33 +02:00
|
|
|
(GMimeObjectForeachFunc)get_body_cb,
|
|
|
|
&data);
|
|
|
|
if (want_html)
|
2011-11-23 23:11:45 +01:00
|
|
|
return (GMimePart*)data._html_part;
|
2011-05-09 01:58:33 +02:00
|
|
|
else
|
2011-11-23 23:11:45 +01:00
|
|
|
return (GMimePart*)data._txt_part;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char*
|
|
|
|
get_body (MuMsgFile *self, gboolean want_html)
|
|
|
|
{
|
|
|
|
GMimePart *part;
|
|
|
|
|
|
|
|
g_return_val_if_fail (self, NULL);
|
|
|
|
g_return_val_if_fail (GMIME_IS_MESSAGE(self->_mime_msg), NULL);
|
|
|
|
|
|
|
|
part = mu_msg_mime_get_body_part (self->_mime_msg, want_html);
|
|
|
|
if (GMIME_IS_PART(part)) {
|
|
|
|
gboolean err;
|
|
|
|
gchar *str;
|
|
|
|
|
|
|
|
err = FALSE;
|
|
|
|
str = mu_msg_mime_part_to_string (part, &err);
|
|
|
|
|
|
|
|
/* note, str may be NULL (no body), but that's not necessarily
|
|
|
|
* an error; we only warn when an actual error occured */
|
|
|
|
if (err)
|
|
|
|
g_warning ("error occured while retrieving %s body "
|
|
|
|
"for message %s",
|
|
|
|
want_html ? "html" : "text", self->_path);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2011-05-09 01:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-26 13:52:27 +02:00
|
|
|
|
2011-06-30 22:45:53 +02:00
|
|
|
static gboolean
|
2011-06-26 13:52:27 +02:00
|
|
|
contains (GSList *lst, const char *str)
|
|
|
|
{
|
|
|
|
for (; lst; lst = g_slist_next(lst))
|
|
|
|
if (g_strcmp0 ((char*)lst->data, str) == 0)
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
static GSList*
|
2011-06-26 13:52:27 +02:00
|
|
|
get_references (MuMsgFile *self)
|
2011-05-09 01:58:33 +02:00
|
|
|
{
|
|
|
|
GSList *msgids;
|
|
|
|
const char *str;
|
2011-06-26 13:52:27 +02:00
|
|
|
unsigned u;
|
|
|
|
const char *headers[] = { "References", "In-reply-to", NULL };
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-06-26 13:52:27 +02:00
|
|
|
for (msgids = NULL, u = 0; headers[u]; ++u) {
|
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
const GMimeReferences *cur;
|
|
|
|
GMimeReferences *mime_refs;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-06-26 13:52:27 +02:00
|
|
|
str = g_mime_object_get_header (GMIME_OBJECT(self->_mime_msg),
|
|
|
|
headers[u]);
|
|
|
|
if (!str)
|
|
|
|
continue;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
mime_refs = g_mime_references_decode (str);
|
|
|
|
for (cur = mime_refs; cur; cur = g_mime_references_get_next(cur)) {
|
|
|
|
const char* msgid;
|
|
|
|
msgid = g_mime_references_get_message_id (cur);
|
2011-06-26 13:52:27 +02:00
|
|
|
/* don't include duplicates */
|
|
|
|
if (msgid && !contains (msgids, msgid))
|
2011-05-09 01:58:33 +02:00
|
|
|
msgids = g_slist_prepend (msgids, g_strdup (msgid));
|
|
|
|
}
|
|
|
|
|
2011-06-26 13:52:27 +02:00
|
|
|
g_mime_references_free (mime_refs);
|
2011-05-09 01:58:33 +02:00
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-06-26 13:52:27 +02:00
|
|
|
return g_slist_reverse (msgids);
|
2011-05-09 01:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-15 23:26:30 +02:00
|
|
|
static GSList*
|
|
|
|
get_tags (MuMsgFile *self)
|
|
|
|
{
|
|
|
|
GMimeObject *obj;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-06-15 23:26:30 +02:00
|
|
|
obj = GMIME_OBJECT(self->_mime_msg);
|
|
|
|
|
|
|
|
return mu_str_to_list (g_mime_object_get_header
|
2011-10-23 23:20:05 +02:00
|
|
|
(obj, "X-Label"), ',', TRUE);
|
2011-06-15 23:26:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-17 13:35:59 +02:00
|
|
|
/* wrongly encoded messages my cause GMime to return invalid
|
|
|
|
* UTF8... we double check, and ensure our output is always correct
|
|
|
|
* utf8 */
|
|
|
|
gchar *
|
|
|
|
maybe_cleanup (const char* str, const char *path, gboolean *do_free)
|
|
|
|
{
|
|
|
|
if (!str || G_LIKELY(g_utf8_validate(str, -1, NULL)))
|
|
|
|
return (char*)str;
|
|
|
|
|
|
|
|
g_debug ("invalid utf8 in %s", path);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-07-17 13:35:59 +02:00
|
|
|
if (*do_free)
|
|
|
|
return mu_str_asciify_in_place ((char*)str);
|
|
|
|
else {
|
|
|
|
gchar *ascii;
|
|
|
|
ascii = mu_str_asciify_in_place(g_strdup (str));
|
|
|
|
*do_free = TRUE;
|
|
|
|
return ascii;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-23 23:11:45 +01:00
|
|
|
G_GNUC_CONST static GMimeRecipientType
|
|
|
|
recipient_type (MuMsgFieldId mfid)
|
|
|
|
{
|
|
|
|
switch (mfid) {
|
|
|
|
case MU_MSG_FIELD_ID_BCC: return GMIME_RECIPIENT_TYPE_BCC;
|
|
|
|
case MU_MSG_FIELD_ID_CC: return GMIME_RECIPIENT_TYPE_CC;
|
|
|
|
case MU_MSG_FIELD_ID_TO: return GMIME_RECIPIENT_TYPE_TO;
|
|
|
|
default: g_return_val_if_reached (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-14 17:07:51 +02:00
|
|
|
char*
|
2011-07-02 11:50:29 +02:00
|
|
|
mu_msg_file_get_str_field (MuMsgFile *self, MuMsgFieldId mfid,
|
|
|
|
gboolean *do_free)
|
2011-05-09 01:58:33 +02:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (self, NULL);
|
|
|
|
g_return_val_if_fail (mu_msg_field_is_string(mfid), NULL);
|
2011-05-14 17:07:51 +02:00
|
|
|
|
|
|
|
*do_free = FALSE; /* default */
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
switch (mfid) {
|
|
|
|
|
2011-12-01 20:23:13 +01:00
|
|
|
case MU_MSG_FIELD_ID_EMBEDDED_TEXT: *do_free = TRUE;
|
2011-11-23 23:11:45 +01:00
|
|
|
return NULL; /* FIXME */
|
2011-05-09 01:58:33 +02:00
|
|
|
|
2011-11-23 23:11:45 +01:00
|
|
|
case MU_MSG_FIELD_ID_BCC:
|
|
|
|
case MU_MSG_FIELD_ID_CC:
|
|
|
|
case MU_MSG_FIELD_ID_TO: *do_free = TRUE;
|
|
|
|
return get_recipient (self, recipient_type(mfid));
|
2011-05-14 17:07:51 +02:00
|
|
|
|
2011-11-23 23:11:45 +01:00
|
|
|
case MU_MSG_FIELD_ID_BODY_TEXT:
|
2011-05-19 19:22:55 +02:00
|
|
|
case MU_MSG_FIELD_ID_BODY_HTML: *do_free = TRUE;
|
2011-11-23 23:11:45 +01:00
|
|
|
return get_body
|
|
|
|
(self, mfid == MU_MSG_FIELD_ID_BODY_HTML ? TRUE : FALSE);
|
2011-05-09 01:58:33 +02:00
|
|
|
|
2011-07-20 23:36:39 +02:00
|
|
|
case MU_MSG_FIELD_ID_FROM:
|
2011-07-17 13:35:59 +02:00
|
|
|
return (char*)maybe_cleanup
|
|
|
|
(g_mime_message_get_sender (self->_mime_msg),
|
|
|
|
self->_path, do_free);
|
|
|
|
|
2011-07-20 23:36:39 +02:00
|
|
|
case MU_MSG_FIELD_ID_PATH: return self->_path;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
case MU_MSG_FIELD_ID_SUBJECT:
|
2011-07-17 13:35:59 +02:00
|
|
|
return (char*)maybe_cleanup
|
|
|
|
(g_mime_message_get_subject (self->_mime_msg),
|
|
|
|
self->_path, do_free);
|
2011-05-09 01:58:33 +02:00
|
|
|
|
|
|
|
case MU_MSG_FIELD_ID_MSGID:
|
2011-05-14 17:07:51 +02:00
|
|
|
return (char*)g_mime_message_get_message_id (self->_mime_msg);
|
2011-05-09 01:58:33 +02:00
|
|
|
|
2011-07-20 23:36:39 +02:00
|
|
|
case MU_MSG_FIELD_ID_MAILDIR: return self->_maildir;
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-07-20 23:36:39 +02:00
|
|
|
default: g_return_val_if_reached (NULL);
|
2011-06-15 22:50:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GSList*
|
|
|
|
mu_msg_file_get_str_list_field (MuMsgFile *self, MuMsgFieldId mfid,
|
|
|
|
gboolean *do_free)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (self, NULL);
|
|
|
|
g_return_val_if_fail (mu_msg_field_is_string_list(mfid), NULL);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-06-15 22:50:17 +02:00
|
|
|
switch (mfid) {
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-06-15 22:50:17 +02:00
|
|
|
case MU_MSG_FIELD_ID_REFS:
|
|
|
|
*do_free = TRUE;
|
|
|
|
return get_references (self);
|
2011-06-15 23:26:30 +02:00
|
|
|
case MU_MSG_FIELD_ID_TAGS:
|
|
|
|
*do_free = TRUE;
|
|
|
|
return get_tags (self);
|
2011-05-09 01:58:33 +02:00
|
|
|
default:
|
|
|
|
g_return_val_if_reached (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 22:50:17 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
gint64
|
|
|
|
mu_msg_file_get_num_field (MuMsgFile *self, const MuMsgFieldId mfid)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (self, -1);
|
|
|
|
g_return_val_if_fail (mu_msg_field_is_numeric(mfid), -1);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
switch (mfid) {
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
case MU_MSG_FIELD_ID_DATE: {
|
|
|
|
time_t t;
|
|
|
|
g_mime_message_get_date (self->_mime_msg, &t, NULL);
|
|
|
|
return (time_t)t;
|
|
|
|
}
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-09 01:58:33 +02:00
|
|
|
case MU_MSG_FIELD_ID_FLAGS:
|
|
|
|
return (gint64)get_flags(self);
|
|
|
|
|
|
|
|
case MU_MSG_FIELD_ID_PRIO:
|
|
|
|
return (gint64)get_prio(self);
|
|
|
|
|
|
|
|
case MU_MSG_FIELD_ID_SIZE:
|
|
|
|
return (gint64)get_size(self);
|
|
|
|
|
|
|
|
default: g_return_val_if_reached (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-19 21:25:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
const char*
|
|
|
|
mu_msg_file_get_header (MuMsgFile *self, const char *header)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (self, NULL);
|
|
|
|
g_return_val_if_fail (header, NULL);
|
2011-10-23 23:20:05 +02:00
|
|
|
|
2011-05-19 21:25:27 +02:00
|
|
|
return g_mime_object_get_header
|
|
|
|
(GMIME_OBJECT(self->_mime_msg), header);
|
|
|
|
}
|