6 * Copyright (C) 2010-2017 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "sipe-common.h"
30 * GMIME interfaces fail to compile on ARM architecture with -Wcast-align
32 * Diagnostic #pragma was added in GCC 4.2.0
35 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2)) || (__GNUC__ >= 5)
36 #if defined(__ARMEL__) || defined(__ARMEB__) || defined(__mips__) || defined(__sparc__) || (defined(__powerpc__) && defined(__NO_FPRS__))
37 #pragma GCC diagnostic ignored "-Wcast-align"
42 #include <gmime/gmime.h>
44 #if !GMIME_CHECK_VERSION(3,0,0)
45 #define g_mime_part_get_content(p) g_mime_part_get_content_object(p)
46 #define g_mime_parser_construct_part(p, o) g_mime_parser_construct_part(p)
49 #include "sipe-mime.h"
51 #include "sipe-backend.h"
52 #include "sipe-utils.h"
54 void sipe_mime_init(void)
57 #if !GMIME_CHECK_VERSION(3,0,0)
63 void sipe_mime_shutdown(void)
68 struct gmime_callback_data
{
69 sipe_mime_parts_cb callback
;
73 static GSList
*gmime_fields_to_nameval(GMimeObject
*part
)
75 GMimeHeaderList
*headers
= g_mime_object_get_header_list(part
);
76 GSList
*fields
= NULL
;
77 #if GMIME_CHECK_VERSION(3,0,0)
78 guint count
= g_mime_header_list_get_count(headers
);
81 for (index
= 0; index
< count
; index
++) {
82 GMimeHeader
*header
= g_mime_header_list_get_header_at(headers
,
84 fields
= sipe_utils_nameval_add(fields
,
85 g_mime_header_get_name(header
),
86 g_mime_header_get_value(header
));
89 GMimeHeaderIter
*iter
= g_mime_header_iter_new();
91 if (g_mime_header_list_get_iter(headers
, iter
)) {
93 fields
= sipe_utils_nameval_add(fields
,
94 g_mime_header_iter_get_name(iter
),
95 g_mime_header_iter_get_value(iter
));
96 } while (g_mime_header_iter_next(iter
));
98 g_mime_header_iter_free(iter
);
104 static void gmime_callback(SIPE_UNUSED_PARAMETER GMimeObject
*parent
,
108 GMimeDataWrapper
*data
= g_mime_part_get_content((GMimePart
*)part
);
111 GMimeStream
*stream
= g_mime_data_wrapper_get_stream(data
);
115 const char *encoding
;
119 encoding
= g_mime_object_get_header(part
,
120 "Content-Transfer-Encoding");
122 GMimeFilter
*filter
= g_mime_filter_basic_new(
123 g_mime_content_encoding_from_string(encoding
), FALSE
);
124 stream
= g_mime_stream_filter_new (stream
);
125 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream
), filter
);
126 g_object_unref (filter
);
129 /* g_mime_stream_read() might not read everything in one call */
130 content
= g_string_new(NULL
);
131 buffer
= g_malloc(4096);
132 while ((length
= g_mime_stream_read(stream
, buffer
, 4096)) > 0) {
133 g_string_append_len(content
, buffer
, length
);
138 struct gmime_callback_data
*cd
= user_data
;
139 GSList
*fields
= gmime_fields_to_nameval(part
);
141 cd
->callback(cd
->user_data
, fields
,
142 content
->str
, content
->len
);
144 sipe_utils_nameval_free(fields
);
147 g_string_free(content
, TRUE
);
150 // Unref GMimeStreamFilter wrapping GMimeStream.
151 g_object_unref(stream
);
157 void sipe_mime_parts_foreach(const gchar
*type
,
159 sipe_mime_parts_cb callback
,
162 gchar
*doc
= g_strdup_printf("Content-Type: %s\r\n\r\n%s", type
, body
);
163 GMimeStream
*stream
= g_mime_stream_mem_new_with_buffer(doc
, strlen(doc
));
166 GMimeParser
*parser
= g_mime_parser_new_with_stream(stream
);
167 GMimeMultipart
*multipart
= (GMimeMultipart
*)g_mime_parser_construct_part(parser
,
171 struct gmime_callback_data cd
= {callback
, user_data
};
173 SIPE_DEBUG_INFO("sipe_mime_parts_foreach: %d parts", g_mime_multipart_get_count(multipart
));
175 g_mime_multipart_foreach(multipart
, gmime_callback
, &cd
);
176 g_object_unref(multipart
);
179 g_object_unref(parser
);
180 g_object_unref(stream
);