adium: allow build without building Adium
[siplcs.git] / src / core / sipe-mime.c
blobfad98e00dd6466e363ee537f6c6c004f65be64a4
1 /**
2 * @file sipe-mime.c
4 * pidgin-sipe
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
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 #if !GMIME_CHECK_VERSION(2,6,0)
45 #error GMime >= 2.6.0 is required to build SIPE
46 #endif
48 #if !GMIME_CHECK_VERSION(3,0,0)
49 #define g_mime_part_get_content(p) g_mime_part_get_content_object(p)
50 #define g_mime_parser_construct_part(p, o) g_mime_parser_construct_part(p)
51 #endif
53 #include "sipe-mime.h"
55 #include "sipe-backend.h"
56 #include "sipe-utils.h"
58 void sipe_mime_init(void)
60 g_mime_init(
61 #if !GMIME_CHECK_VERSION(3,0,0)
63 #endif
67 void sipe_mime_shutdown(void)
69 g_mime_shutdown();
72 struct gmime_callback_data {
73 sipe_mime_parts_cb callback;
74 gpointer user_data;
77 static GSList *gmime_fields_to_nameval(GMimeObject *part)
79 GMimeHeaderList *headers = g_mime_object_get_header_list(part);
80 GSList *fields = NULL;
81 #if GMIME_CHECK_VERSION(3,0,0)
82 guint count = g_mime_header_list_get_count(headers);
83 guint index;
85 for (index = 0; index < count; index++) {
86 GMimeHeader *header = g_mime_header_list_get_header_at(headers,
87 index);
88 fields = sipe_utils_nameval_add(fields,
89 g_mime_header_get_name(header),
90 g_mime_header_get_value(header));
92 #else
93 GMimeHeaderIter *iter = g_mime_header_iter_new();
95 if (g_mime_header_list_get_iter(headers, iter)) {
96 do {
97 fields = sipe_utils_nameval_add(fields,
98 g_mime_header_iter_get_name(iter),
99 g_mime_header_iter_get_value(iter));
100 } while (g_mime_header_iter_next(iter));
102 g_mime_header_iter_free(iter);
103 #endif
105 return fields;
108 static void gmime_callback(SIPE_UNUSED_PARAMETER GMimeObject *parent,
109 GMimeObject *part,
110 gpointer user_data)
112 GMimeDataWrapper *data = g_mime_part_get_content((GMimePart *)part);
114 if (data) {
115 GMimeStream *stream = g_mime_data_wrapper_get_stream(data);
117 if (stream) {
118 ssize_t length = 0;
119 const char *encoding;
120 gchar *buffer;
121 GString *content;
123 encoding = g_mime_object_get_header(part,
124 "Content-Transfer-Encoding");
125 if (encoding) {
126 GMimeFilter *filter = g_mime_filter_basic_new(
127 g_mime_content_encoding_from_string(encoding), FALSE);
128 stream = g_mime_stream_filter_new (stream);
129 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream), filter);
130 g_object_unref (filter);
133 /* g_mime_stream_read() might not read everything in one call */
134 content = g_string_new(NULL);
135 buffer = g_malloc(4096);
136 while ((length = g_mime_stream_read(stream, buffer, 4096)) > 0) {
137 g_string_append_len(content, buffer, length);
139 g_free(buffer);
141 if (length == 0) {
142 struct gmime_callback_data *cd = user_data;
143 GSList *fields = gmime_fields_to_nameval(part);
145 cd->callback(cd->user_data, fields,
146 content->str, content->len);
148 sipe_utils_nameval_free(fields);
151 g_string_free(content, TRUE);
153 if (encoding) {
154 // Unref GMimeStreamFilter wrapping GMimeStream.
155 g_object_unref(stream);
161 void sipe_mime_parts_foreach(const gchar *type,
162 const gchar *body,
163 sipe_mime_parts_cb callback,
164 gpointer user_data)
166 gchar *doc = g_strdup_printf("Content-Type: %s\r\n\r\n%s", type, body);
167 GMimeStream *stream = g_mime_stream_mem_new_with_buffer(doc, strlen(doc));
169 if (stream) {
170 GMimeParser *parser = g_mime_parser_new_with_stream(stream);
171 GMimeMultipart *multipart = (GMimeMultipart *)g_mime_parser_construct_part(parser,
172 NULL);
174 if (multipart) {
175 struct gmime_callback_data cd = {callback, user_data};
177 SIPE_DEBUG_INFO("sipe_mime_parts_foreach: %d parts", g_mime_multipart_get_count(multipart));
179 g_mime_multipart_foreach(multipart, gmime_callback, &cd);
180 g_object_unref(multipart);
183 g_object_unref(parser);
184 g_object_unref(stream);
186 g_free(doc);
190 Local Variables:
191 mode: c
192 c-file-style: "bsd"
193 indent-tabs-mode: t
194 tab-width: 8
195 End: