media: fix relay-info with Farstream 0.2
[siplcs.git] / src / core / sipe-mime.c
blob8af6b56977a46305cb8963f7baa438ef95c7ad04
1 /**
2 * @file sipe-mime.c
4 * pidgin-sipe
6 * Copyright (C) 2010-11 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>
28 #include <gmime/gmime.h>
30 #include "sipe-mime.h"
32 #include "sipe-backend.h"
33 #include "sipe-utils.h"
35 void sipe_mime_init(void)
37 g_mime_init(0);
40 void sipe_mime_shutdown(void)
42 g_mime_shutdown();
45 struct gmime_callback_data {
46 sipe_mime_parts_cb callback;
47 gpointer user_data;
50 static GSList *gmime_fields_to_nameval(GMimeObject *part)
52 GMimeHeaderList *headers = g_mime_object_get_header_list(part);
53 GMimeHeaderIter *iter = g_mime_header_iter_new();
54 GSList *fields = NULL;
56 if (g_mime_header_list_get_iter(headers, iter)) {
57 do {
58 fields = sipe_utils_nameval_add(fields,
59 g_mime_header_iter_get_name(iter),
60 g_mime_header_iter_get_value(iter));
62 } while (g_mime_header_iter_next(iter));
64 g_mime_header_iter_free(iter);
66 return fields;
69 static void gmime_callback(SIPE_UNUSED_PARAMETER GMimeObject *parent,
70 GMimeObject *part,
71 gpointer user_data)
73 GMimeDataWrapper *data = g_mime_part_get_content_object((GMimePart *)part);
75 if (data) {
76 GMimeStream *stream = g_mime_data_wrapper_get_stream(data);
78 if (stream) {
79 ssize_t length = g_mime_stream_length(stream);
81 if (length != -1) {
82 gchar *content = g_malloc(length + 1);
84 if (g_mime_stream_read(stream, content, length) == length) {
85 struct gmime_callback_data *cd = user_data;
86 GSList *fields = gmime_fields_to_nameval(part);
88 (*(cd->callback))(cd->user_data, fields, content, length);
90 sipe_utils_nameval_free(fields);
92 g_free(content);
98 void sipe_mime_parts_foreach(const gchar *type,
99 const gchar *body,
100 sipe_mime_parts_cb callback,
101 gpointer user_data)
103 gchar *doc = g_strdup_printf("Content-Type: %s\r\n\r\n%s", type, body);
104 GMimeStream *stream = g_mime_stream_mem_new_with_buffer(doc, strlen(doc));
106 if (stream) {
107 GMimeParser *parser = g_mime_parser_new_with_stream(stream);
108 GMimeMultipart *multipart = (GMimeMultipart *)g_mime_parser_construct_part(parser);
110 if (multipart) {
111 struct gmime_callback_data cd = {callback, user_data};
113 SIPE_DEBUG_INFO("sipe_mime_parts_foreach: %d parts", g_mime_multipart_get_count(multipart));
115 g_mime_multipart_foreach(multipart, gmime_callback, &cd);
116 g_object_unref(multipart);
119 g_object_unref(parser);
120 g_object_unref(stream);
122 g_free(doc);
126 Local Variables:
127 mode: c
128 c-file-style: "bsd"
129 indent-tabs-mode: t
130 tab-width: 8
131 End: