purple: work around broken dbus-server.h
[siplcs.git] / src / core / sipe-mime.c
blob74f7241ce33d7a97ae8431446bd4de79cda155a4
1 /**
2 * @file sipe-mime.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2016 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
23 #include <string.h>
25 #include "sipe-common.h"
27 #include <glib.h>
30 * GMIME interfaces fail to compile on ARM architecture with -Wcast-align
32 * Diagnostic #pragma was added in GCC 4.2.0
34 #if defined(__GNUC__)
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"
38 #endif
39 #endif
40 #endif
42 #include <gmime/gmime.h>
44 #include "sipe-mime.h"
46 #include "sipe-backend.h"
47 #include "sipe-utils.h"
49 void sipe_mime_init(void)
51 g_mime_init(0);
54 void sipe_mime_shutdown(void)
56 g_mime_shutdown();
59 struct gmime_callback_data {
60 sipe_mime_parts_cb callback;
61 gpointer user_data;
64 static GSList *gmime_fields_to_nameval(GMimeObject *part)
66 GMimeHeaderList *headers = g_mime_object_get_header_list(part);
67 GMimeHeaderIter *iter = g_mime_header_iter_new();
68 GSList *fields = NULL;
70 if (g_mime_header_list_get_iter(headers, iter)) {
71 do {
72 fields = sipe_utils_nameval_add(fields,
73 g_mime_header_iter_get_name(iter),
74 g_mime_header_iter_get_value(iter));
76 } while (g_mime_header_iter_next(iter));
78 g_mime_header_iter_free(iter);
80 return fields;
83 static void gmime_callback(SIPE_UNUSED_PARAMETER GMimeObject *parent,
84 GMimeObject *part,
85 gpointer user_data)
87 GMimeDataWrapper *data = g_mime_part_get_content_object((GMimePart *)part);
89 if (data) {
90 GMimeStream *stream = g_mime_data_wrapper_get_stream(data);
92 if (stream) {
93 ssize_t length = 0;
94 const char *encoding;
95 gchar *buffer;
96 GString *content;
98 encoding = g_mime_object_get_header(part,
99 "Content-Transfer-Encoding");
100 if (encoding) {
101 GMimeFilter *filter = g_mime_filter_basic_new(
102 g_mime_content_encoding_from_string(encoding), FALSE);
103 stream = g_mime_stream_filter_new (stream);
104 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream), filter);
105 g_object_unref (filter);
108 /* g_mime_stream_read() might not read everything in one call */
109 content = g_string_new(NULL);
110 buffer = g_malloc(4096);
111 while ((length = g_mime_stream_read(stream, buffer, 4096)) > 0) {
112 g_string_append_len(content, buffer, length);
114 g_free(buffer);
116 if (length == 0) {
117 struct gmime_callback_data *cd = user_data;
118 GSList *fields = gmime_fields_to_nameval(part);
120 cd->callback(cd->user_data, fields,
121 content->str, content->len);
123 sipe_utils_nameval_free(fields);
126 g_string_free(content, TRUE);
128 if (encoding) {
129 // Unref GMimeStreamFilter wrapping GMimeStream.
130 g_object_unref(stream);
136 void sipe_mime_parts_foreach(const gchar *type,
137 const gchar *body,
138 sipe_mime_parts_cb callback,
139 gpointer user_data)
141 gchar *doc = g_strdup_printf("Content-Type: %s\r\n\r\n%s", type, body);
142 GMimeStream *stream = g_mime_stream_mem_new_with_buffer(doc, strlen(doc));
144 if (stream) {
145 GMimeParser *parser = g_mime_parser_new_with_stream(stream);
146 GMimeMultipart *multipart = (GMimeMultipart *)g_mime_parser_construct_part(parser);
148 if (multipart) {
149 struct gmime_callback_data cd = {callback, user_data};
151 SIPE_DEBUG_INFO("sipe_mime_parts_foreach: %d parts", g_mime_multipart_get_count(multipart));
153 g_mime_multipart_foreach(multipart, gmime_callback, &cd);
154 g_object_unref(multipart);
157 g_object_unref(parser);
158 g_object_unref(stream);
160 g_free(doc);
164 Local Variables:
165 mode: c
166 c-file-style: "bsd"
167 indent-tabs-mode: t
168 tab-width: 8
169 End: