annotate src/mime.c @ 13339:d46a27d29eed

[gaim-migrate @ 15709] Clear formatting is only useful if the conversation imhtml is editable. committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Mon, 27 Feb 2006 19:40:46 +0000
parents d4cd7d443795
children 443aaa05a7c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
1
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
2 /*
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
3 Gaim
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
4
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
5 Gaim is the legal property of its developers, whose names are too
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
6 numerous to list here. Please refer to the COPYRIGHT file distributed
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
7 with this source distribution
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
8
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
9 This program is free software; you can redistribute it and/or modify
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
10 it under the terms of the GNU General Public License as published by
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
11 the Free Software Foundation; either version 2 of the License, or (at
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
12 your option) any later version.
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
13
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
14 This program is distributed in the hope that it will be useful, but
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
15 WITHOUT ANY WARRANTY; without even the implied warranty of
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
17 General Public License for more details.
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
18
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
19 You should have received a copy of the GNU General Public License
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
20 along with this program; if not, write to the Free Software
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
22 USA.
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
23 */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
24
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
25 #include <stdio.h>
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
26 #include <string.h>
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
27
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
28 #include <glib.h>
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
29 #include <glib/ghash.h>
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
30 #include <glib/glist.h>
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
31 #include <glib/gstring.h>
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
32
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
33 /* this should become "util.h" if we ever get this into gaim proper */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
34 #include <debug.h>
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
35 #include <util.h>
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
36
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
37 #include "mime.h"
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
38
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
39
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
40 /** @struct mime_fields
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
41
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
42 Utility structure used in both MIME document and parts, which maps
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
43 field names to their values, and keeps an easily accessible list of
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
44 keys
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
45 */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
46 struct mime_fields {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
47 GHashTable *map;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
48 GList *keys;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
49 };
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
50
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
51
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
52 struct _GaimMimeDocument {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
53 struct mime_fields fields;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
54 GList *parts;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
55 };
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
56
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
57
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
58 struct _GaimMimePart {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
59 struct mime_fields fields;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
60 struct _GaimMimeDocument *doc;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
61 GString *data;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
62 };
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
63
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
64
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
65 static void fields_set(struct mime_fields *mf,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
66 const char *key, const char *val) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
67 char *k, *v;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
68
11291
57fccea36e36 [gaim-migrate @ 13491]
Mark Doliner <mark@kingant.net>
parents: 11137
diff changeset
69 g_return_if_fail(mf != NULL);
57fccea36e36 [gaim-migrate @ 13491]
Mark Doliner <mark@kingant.net>
parents: 11137
diff changeset
70 g_return_if_fail(mf->map != NULL);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
71
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
72 k = g_ascii_strdown(key, -1);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
73 v = g_strdup(val);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
74
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
75 /* append to the keys list only if it's not already there */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
76 if(! g_hash_table_lookup(mf->map, k)) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
77 mf->keys = g_list_append(mf->keys, k);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
78 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
79
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
80 /* important to use insert. If the key is already in the table, then
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
81 it's already in the keys list. Insert will free the new instance
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
82 of the key rather than the old instance. */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
83 g_hash_table_insert(mf->map, k, v);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
84 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
85
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
86
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
87 static const char *fields_get(struct mime_fields *mf,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
88 const char *key) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
89 char *kdown;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
90 const char *ret;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
91
11291
57fccea36e36 [gaim-migrate @ 13491]
Mark Doliner <mark@kingant.net>
parents: 11137
diff changeset
92 g_return_val_if_fail(mf != NULL, NULL);
57fccea36e36 [gaim-migrate @ 13491]
Mark Doliner <mark@kingant.net>
parents: 11137
diff changeset
93 g_return_val_if_fail(mf->map != NULL, NULL);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
94
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
95 kdown = g_ascii_strdown(key, -1);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
96 ret = g_hash_table_lookup(mf->map, kdown);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
97 g_free(kdown);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
98
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
99 return ret;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
100 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
101
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
102
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
103 static void fields_init(struct mime_fields *mf) {
11291
57fccea36e36 [gaim-migrate @ 13491]
Mark Doliner <mark@kingant.net>
parents: 11137
diff changeset
104 g_return_if_fail(mf != NULL);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
105
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
106 mf->map = g_hash_table_new_full(g_str_hash, g_str_equal,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
107 g_free, g_free);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
108 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
109
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
110
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
111 static void fields_loadline(struct mime_fields *mf,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
112 const char *line, gsize len) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
113
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
114 /* split the line into key: value */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
115 char *key, *val;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
116 char **tokens;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
117
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
118 /* feh, need it to be NUL terminated */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
119 key = g_strndup(line, len);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
120
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
121 /* split */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
122 val = strchr(key, ':');
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
123 if(! val) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
124 g_free(key);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
125 return;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
126 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
127 *val++ = '\0';
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
128
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
129 /* normalize whitespace (sorta) and trim on key and value */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
130 tokens = g_strsplit(key, "\t\r\n", 0);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
131 key = g_strjoinv("", tokens);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
132 key = g_strstrip(key);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
133 g_strfreev(tokens);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
134
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
135 tokens = g_strsplit(val, "\t\r\n", 0);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
136 val = g_strjoinv("", tokens);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
137 val = g_strstrip(val);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
138 g_strfreev(tokens);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
139
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
140 fields_set(mf, key, val);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
141
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
142 g_free(key);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
143 g_free(val);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
144 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
145
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
146
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
147 static void fields_load(struct mime_fields *mf,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
148 char **buf, gsize *len) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
149 char *tail;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
150
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
151 while( (tail = g_strstr_len(*buf, *len, "\r\n")) ) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
152 char *line;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
153 gsize ln;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
154
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
155 /* determine the current line */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
156 line = *buf;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
157 ln = tail - line;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
158
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
159 /* advance our search space past the CRLF */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
160 *buf = tail + 2;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
161 *len -= (ln + 2);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
162
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
163 /* empty line, end of headers */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
164 if(! ln) return;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
165
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
166 /* look out for line continuations */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
167 if(line[ln-1] == ';') {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
168 tail = g_strstr_len(*buf, *len, "\r\n");
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
169 if(tail) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
170 gsize cln;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
171
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
172 cln = tail - *buf;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
173 ln = tail - line;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
174
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
175 /* advance our search space past the CRLF (again) */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
176 *buf = tail + 2;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
177 *len -= (cln + 2);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
178 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
179 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
180
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
181 /* process our super-cool line */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
182 fields_loadline(mf, line, ln);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
183 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
184 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
185
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
186
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
187 static void field_write(const char *key, const char *val, GString *str) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
188 g_string_append_printf(str, "%s: %s\r\n", key, val);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
189 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
190
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
191
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
192 static void fields_write(struct mime_fields *mf, GString *str) {
11291
57fccea36e36 [gaim-migrate @ 13491]
Mark Doliner <mark@kingant.net>
parents: 11137
diff changeset
193 g_return_if_fail(mf != NULL);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
194
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
195 g_hash_table_foreach(mf->map, (GHFunc) field_write, str);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
196 g_string_append(str, "\r\n");
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
197 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
198
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
199
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
200 static void fields_destroy(struct mime_fields *mf) {
11291
57fccea36e36 [gaim-migrate @ 13491]
Mark Doliner <mark@kingant.net>
parents: 11137
diff changeset
201 g_return_if_fail(mf != NULL);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
202
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
203 g_hash_table_destroy(mf->map);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
204 g_list_free(mf->keys);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
205
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
206 mf->map = NULL;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
207 mf->keys = NULL;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
208 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
209
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
210
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
211 static GaimMimePart *part_new(GaimMimeDocument *doc) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
212 GaimMimePart *part;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
213
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
214 part = g_new0(GaimMimePart, 1);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
215 fields_init(&part->fields);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
216 part->doc = doc;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
217 part->data = g_string_new(NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
218
12105
d4cd7d443795 [gaim-migrate @ 14402]
Christopher O'Brien <siege@pidgin.im>
parents: 11780
diff changeset
219 doc->parts = g_list_prepend(doc->parts, part);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
220
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
221 return part;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
222 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
223
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
224
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
225 static void part_load(GaimMimePart *part,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
226 const char *buf, gsize len) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
227
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
228 char *b = (char *) buf;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
229 gsize n = len;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
230
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
231 fields_load(&part->fields, &b, &n);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
232
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
233 /* the remainder will have a blank line, if there's anything at all,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
234 so check if there's anything then trim off the trailing four
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
235 bytes, \r\n\r\n */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
236 if(n > 4) n -= 4;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
237 g_string_append_len(part->data, b, n);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
238 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
239
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
240
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
241 static void part_write(GaimMimePart *part, GString *str) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
242 fields_write(&part->fields, str);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
243 g_string_append_printf(str, "%s\r\n\r\n", part->data->str);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
244 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
245
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
246
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
247 static void part_free(GaimMimePart *part) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
248
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
249 fields_destroy(&part->fields);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
250
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
251 g_string_free(part->data, TRUE);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
252 part->data = NULL;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
253
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
254 g_free(part);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
255 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
256
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
257
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
258 GaimMimePart *gaim_mime_part_new(GaimMimeDocument *doc) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
259 g_return_val_if_fail(doc != NULL, NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
260 return part_new(doc);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
261 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
262
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
263
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
264 const GList *gaim_mime_part_get_fields(GaimMimePart *part) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
265 g_return_val_if_fail(part != NULL, NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
266 return part->fields.keys;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
267 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
268
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
269
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
270 const char *gaim_mime_part_get_field(GaimMimePart *part,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
271 const char *field) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
272
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
273 g_return_val_if_fail(part != NULL, NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
274 return fields_get(&part->fields, field);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
275 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
276
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
277
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
278 char *gaim_mime_part_get_field_decoded(GaimMimePart *part,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
279 const char *field) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
280
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
281 const char *f;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
282
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
283 g_return_val_if_fail(part != NULL, NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
284
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
285 f = fields_get(&part->fields, field);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
286 return gaim_mime_decode_field(f);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
287 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
288
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
289
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
290 void gaim_mime_part_set_field(GaimMimePart *part,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
291 const char *field,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
292 const char *value) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
293 g_return_if_fail(part != NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
294 fields_set(&part->fields, field, value);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
295 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
296
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
297
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
298 const char *gaim_mime_part_get_data(GaimMimePart *part) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
299 g_return_val_if_fail(part != NULL, NULL);
11291
57fccea36e36 [gaim-migrate @ 13491]
Mark Doliner <mark@kingant.net>
parents: 11137
diff changeset
300 g_return_val_if_fail(part->data != NULL, NULL);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
301
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
302 return part->data->str;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
303 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
304
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
305
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
306 void gaim_mime_part_get_data_decoded(GaimMimePart *part,
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11132
diff changeset
307 guchar **data, gsize *len) {
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
308 const char *enc;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
309
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
310 g_return_if_fail(part != NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
311 g_return_if_fail(data != NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
312 g_return_if_fail(len != NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
313
11291
57fccea36e36 [gaim-migrate @ 13491]
Mark Doliner <mark@kingant.net>
parents: 11137
diff changeset
314 g_return_if_fail(part->data != NULL);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
315
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
316 enc = gaim_mime_part_get_field(part, "content-transfer-encoding");
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
317
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
318 if(! enc) {
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11132
diff changeset
319 *data = (guchar *)g_strdup(part->data->str);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
320 *len = part->data->len;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
321
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
322 } else if(! g_ascii_strcasecmp(enc, "7bit")) {
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11132
diff changeset
323 *data = (guchar *)g_strdup(part->data->str);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
324 *len = part->data->len;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
325
11066
2507d20c3d0b [gaim-migrate @ 13047]
Christopher O'Brien <siege@pidgin.im>
parents: 10978
diff changeset
326 } else if(! g_ascii_strcasecmp(enc, "8bit")) {
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11132
diff changeset
327 *data = (guchar *)g_strdup(part->data->str);
11066
2507d20c3d0b [gaim-migrate @ 13047]
Christopher O'Brien <siege@pidgin.im>
parents: 10978
diff changeset
328 *len = part->data->len;
2507d20c3d0b [gaim-migrate @ 13047]
Christopher O'Brien <siege@pidgin.im>
parents: 10978
diff changeset
329
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
330 } else if(! g_ascii_strcasecmp(enc, "base16")) {
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 11066
diff changeset
331 *data = gaim_base16_decode(part->data->str, len);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
332
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
333 } else if(! g_ascii_strcasecmp(enc, "base64")) {
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 11066
diff changeset
334 *data = gaim_base64_decode(part->data->str, len);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
335
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
336 } else if(! g_ascii_strcasecmp(enc, "quoted-printable")) {
11132
a4611130e3eb [gaim-migrate @ 13189]
Mark Doliner <mark@kingant.net>
parents: 11127
diff changeset
337 *data = gaim_quotedp_decode(part->data->str, len);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
338
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
339 } else {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
340 gaim_debug_warning("mime", "gaim_mime_part_get_data_decoded:"
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
341 " unknown encoding '%s'\n", enc);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
342 *data = NULL;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
343 *len = 0;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
344 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
345 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
346
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
347
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
348 gsize gaim_mime_part_get_length(GaimMimePart *part) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
349 g_return_val_if_fail(part != NULL, 0);
11291
57fccea36e36 [gaim-migrate @ 13491]
Mark Doliner <mark@kingant.net>
parents: 11137
diff changeset
350 g_return_val_if_fail(part->data != NULL, 0);
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
351
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
352 return part->data->len;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
353 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
354
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
355
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
356 void gaim_mime_part_set_data(GaimMimePart *part, const char *data) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
357 g_return_if_fail(part != NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
358 g_string_free(part->data, TRUE);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
359 part->data = g_string_new(data);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
360 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
361
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
362
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
363 GaimMimeDocument *gaim_mime_document_new() {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
364 GaimMimeDocument *doc;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
365
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
366 doc = g_new0(GaimMimeDocument, 1);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
367 fields_init(&doc->fields);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
368
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
369 return doc;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
370 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
371
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
372
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
373 static void doc_parts_load(GaimMimeDocument *doc,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
374 const char *boundary,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
375 const char *buf, gsize len) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
376
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
377 char *b = (char *) buf;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
378 gsize n = len;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
379
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
380 const char *bnd;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
381 gsize bl;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
382
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
383 bnd = g_strdup_printf("--%s", boundary);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
384 bl = strlen(bnd);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
385
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
386 for(b = g_strstr_len(b, n, bnd); b; ) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
387 char *tail;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
388
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
389 /* skip the boundary */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
390 b += bl;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
391 n -= bl;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
392
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
393 /* skip the trailing \r\n or -- as well */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
394 if(n >= 2) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
395 b += 2;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
396 n -= 2;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
397 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
398
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
399 /* find the next boundary */
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
400 tail = g_strstr_len(b, n, bnd);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
401
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
402 if(tail) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
403 gsize sl;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
404
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
405 sl = tail - b;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
406 if(sl) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
407 GaimMimePart *part = part_new(doc);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
408 part_load(part, b, sl);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
409 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
410 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
411
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
412 b = tail;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
413 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
414 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
415
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
416
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
417 GaimMimeDocument *gaim_mime_document_parsen(const char *buf, gsize len) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
418
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
419 GaimMimeDocument *doc;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
420
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
421 char *b = (char *) buf;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
422 gsize n = len;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
423
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
424 g_return_val_if_fail(buf != NULL, NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
425
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
426 doc = gaim_mime_document_new();
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
427
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
428 if(! len) return doc;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
429
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
430 fields_load(&doc->fields, &b, &n);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
431
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
432 {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
433 const char *ct = fields_get(&doc->fields, "content-type");
11780
8cb75ba77f9d [gaim-migrate @ 14071]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11291
diff changeset
434 if(ct && gaim_str_has_prefix(ct, "multipart")) {
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
435 char *bd = strrchr(ct, '=');
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
436 if(bd++) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
437 doc_parts_load(doc, bd, b, n);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
438 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
439 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
440 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
441
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
442 return doc;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
443 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
444
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
445
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
446 GaimMimeDocument *gaim_mime_document_parse(const char *buf) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
447 g_return_val_if_fail(buf != NULL, NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
448 return gaim_mime_document_parsen(buf, strlen(buf));
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
449 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
450
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
451
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
452 void gaim_mime_document_write(GaimMimeDocument *doc, GString *str) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
453 const char *bd = NULL;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
454
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
455 g_return_if_fail(doc != NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
456 g_return_if_fail(str != NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
457
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
458 {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
459 const char *ct = fields_get(&doc->fields, "content-type");
11780
8cb75ba77f9d [gaim-migrate @ 14071]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11291
diff changeset
460 if(ct && gaim_str_has_prefix(ct, "multipart")) {
10978
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
461 char *b = strrchr(ct, '=');
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
462 if(b++) bd = b;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
463 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
464 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
465
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
466 fields_write(&doc->fields, str);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
467
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
468 if(bd) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
469 GList *l;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
470
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
471 for(l = doc->parts; l; l = l->next) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
472 g_string_append_printf(str, "--%s\r\n", bd);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
473
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
474 part_write(l->data, str);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
475
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
476 if(! l->next) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
477 g_string_append_printf(str, "--%s--\r\n", bd);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
478 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
479 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
480 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
481 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
482
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
483
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
484 const GList *gaim_mime_document_get_fields(GaimMimeDocument *doc) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
485 g_return_val_if_fail(doc != NULL, NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
486 return doc->fields.keys;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
487 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
488
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
489
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
490 const char *gaim_mime_document_get_field(GaimMimeDocument *doc,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
491 const char *field) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
492 g_return_val_if_fail(doc != NULL, NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
493 return fields_get(&doc->fields, field);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
494 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
495
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
496
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
497 void gaim_mime_document_set_field(GaimMimeDocument *doc,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
498 const char *field,
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
499 const char *value) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
500 g_return_if_fail(doc != NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
501 fields_set(&doc->fields, field, value);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
502 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
503
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
504
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
505 const GList *gaim_mime_document_get_parts(GaimMimeDocument *doc) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
506 g_return_val_if_fail(doc != NULL, NULL);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
507 return doc->parts;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
508 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
509
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
510
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
511 void gaim_mime_document_free(GaimMimeDocument *doc) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
512 if(! doc) return;
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
513
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
514 fields_destroy(&doc->fields);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
515
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
516 while(doc->parts) {
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
517 part_free(doc->parts->data);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
518 doc->parts = g_list_delete_link(doc->parts, doc->parts);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
519 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
520
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
521 g_free(doc);
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
522 }
8ab19bf9c3bc [gaim-migrate @ 12804]
Christopher O'Brien <siege@pidgin.im>
parents:
diff changeset
523