comparison libgaim/protocols/jabber/parser.c @ 14192:60b1bc8dbf37

[gaim-migrate @ 16863] Renamed 'core' to 'libgaim' committer: Tailor Script <tailor@pidgin.im>
author Evan Schoenberg <evan.s@dreskin.net>
date Sat, 19 Aug 2006 01:50:10 +0000
parents
children e52d5626824a
comparison
equal deleted inserted replaced
14191:009db0b357b5 14192:60b1bc8dbf37
1 /*
2 * gaim - Jabber XML parser stuff
3 *
4 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21 #include "internal.h"
22
23 #ifdef HAVE_LIBXML
24 #include <libxml/parser.h>
25 #endif
26
27 #include "connection.h"
28 #include "debug.h"
29 #include "jabber.h"
30 #include "parser.h"
31 #include "xmlnode.h"
32
33 #ifndef HAVE_LIBXML
34 static void
35 jabber_parser_element_start(GMarkupParseContext *context,
36 const char *element_name, const char **attrib_names,
37 const char **attrib_values, gpointer user_data, GError **error)
38 {
39 JabberStream *js = user_data;
40 xmlnode *node;
41 int i;
42
43 if(!element_name) {
44 return;
45 } else if(!strcmp(element_name, "stream:stream")) {
46 js->protocol_version = JABBER_PROTO_0_9;
47 for(i=0; attrib_names[i]; i++) {
48 if(!strcmp(attrib_names[i], "version")
49 && !strcmp(attrib_values[i], "1.0")) {
50 js->protocol_version = JABBER_PROTO_1_0;
51 } else if(!strcmp(attrib_names[i], "id")) {
52 if(js->stream_id)
53 g_free(js->stream_id);
54 js->stream_id = g_strdup(attrib_values[i]);
55 }
56 }
57 if(js->protocol_version == JABBER_PROTO_0_9)
58 js->auth_type = JABBER_AUTH_IQ_AUTH;
59
60 if(js->state == JABBER_STREAM_INITIALIZING)
61 jabber_stream_set_state(js, JABBER_STREAM_AUTHENTICATING);
62 } else {
63
64 if(js->current)
65 node = xmlnode_new_child(js->current, element_name);
66 else
67 node = xmlnode_new(element_name);
68
69 for(i=0; attrib_names[i]; i++) {
70 xmlnode_set_attrib(node, attrib_names[i], attrib_values[i]);
71 }
72
73 js->current = node;
74 }
75 }
76
77 static void
78 jabber_parser_element_end(GMarkupParseContext *context,
79 const char *element_name, gpointer user_data, GError **error)
80 {
81 JabberStream *js = user_data;
82
83 if(!js->current)
84 return;
85
86 if(js->current->parent) {
87 if(!strcmp(js->current->name, element_name))
88 js->current = js->current->parent;
89 } else {
90 xmlnode *packet = js->current;
91 js->current = NULL;
92 jabber_process_packet(js, packet);
93 xmlnode_free(packet);
94 }
95 }
96
97 static void
98 jabber_parser_element_text(GMarkupParseContext *context, const char *text,
99 gsize text_len, gpointer user_data, GError **error)
100 {
101 JabberStream *js = user_data;
102
103 if(!js->current)
104 return;
105
106 if(!text || !text_len)
107 return;
108
109 xmlnode_insert_data(js->current, text, text_len);
110 }
111
112 #else /* HAVE_LIBXML */
113
114 static void
115 jabber_parser_element_start_libxml(void *user_data,
116 const xmlChar *element_name, const xmlChar *prefix, const xmlChar *namespace,
117 int nb_namespaces, const xmlChar **namespaces,
118 int nb_attributes, int nb_defaulted, const xmlChar **attributes)
119 {
120 JabberStream *js = user_data;
121 xmlnode *node;
122 int i;
123
124 if(!element_name) {
125 return;
126 } else if(!strcmp(element_name, "stream")) {
127 js->protocol_version = JABBER_PROTO_0_9;
128 for(i=0; i < nb_attributes * 5; i += 5) {
129 int attrib_len = attributes[i+4] - attributes[i+3];
130 char *attrib = g_malloc(attrib_len + 1);
131 memcpy(attrib, attributes[i+3], attrib_len);
132 attrib[attrib_len] = '\0';
133
134 if(!strcmp(attributes[i], "version")
135 && !strcmp(attrib, "1.0")) {
136 js->protocol_version = JABBER_PROTO_1_0;
137 } else if(!strcmp(attributes[i], "id")) {
138 if(js->stream_id)
139 g_free(js->stream_id);
140 js->stream_id = g_strdup(attrib);
141 }
142 g_free(attrib);
143 }
144 if(js->protocol_version == JABBER_PROTO_0_9)
145 js->auth_type = JABBER_AUTH_IQ_AUTH;
146
147 if(js->state == JABBER_STREAM_INITIALIZING)
148 jabber_stream_set_state(js, JABBER_STREAM_AUTHENTICATING);
149 } else {
150
151 if(js->current)
152 node = xmlnode_new_child(js->current, element_name);
153 else
154 node = xmlnode_new(element_name);
155 xmlnode_set_namespace(node, namespace);
156
157 for(i=0; i < nb_attributes * 5; i+=5) {
158 int attrib_len = attributes[i+4] - attributes[i+3];
159 char *attrib = g_malloc(attrib_len + 1);
160 memcpy(attrib, attributes[i+3], attrib_len);
161 attrib[attrib_len] = '\0';
162 xmlnode_set_attrib(node, attributes[i], attrib);
163 g_free(attrib);
164 }
165
166 js->current = node;
167 }
168 }
169
170 static void
171 jabber_parser_element_end_libxml(void *user_data, const xmlChar *element_name,
172 const xmlChar *prefix, const xmlChar *namespace)
173 {
174 JabberStream *js = user_data;
175
176 if(!js->current)
177 return;
178
179 if(js->current->parent) {
180 if(!strcmp(js->current->name, element_name))
181 js->current = js->current->parent;
182 } else {
183 xmlnode *packet = js->current;
184 js->current = NULL;
185 jabber_process_packet(js, packet);
186 xmlnode_free(packet);
187 }
188 }
189
190 static void
191 jabber_parser_element_text_libxml(void *user_data, const xmlChar *text, int text_len)
192 {
193 JabberStream *js = user_data;
194
195 if(!js->current)
196 return;
197
198 if(!text || !text_len)
199 return;
200
201 xmlnode_insert_data(js->current, text, text_len);
202 }
203 #endif /* HAVE_LIBXML */
204
205
206 #ifdef HAVE_LIBXML
207 static xmlSAXHandler jabber_parser_libxml = {
208 .internalSubset = NULL,
209 .isStandalone = NULL,
210 .hasInternalSubset = NULL,
211 .hasExternalSubset = NULL,
212 .resolveEntity = NULL,
213 .getEntity = NULL,
214 .entityDecl = NULL,
215 .notationDecl = NULL,
216 .attributeDecl = NULL,
217 .elementDecl = NULL,
218 .unparsedEntityDecl = NULL,
219 .setDocumentLocator = NULL,
220 .startDocument = NULL,
221 .endDocument = NULL,
222 .startElement = NULL,
223 .endElement = NULL,
224 .reference = NULL,
225 .characters = jabber_parser_element_text_libxml,
226 .ignorableWhitespace = NULL,
227 .processingInstruction = NULL,
228 .comment = NULL,
229 .warning = NULL,
230 .error = NULL,
231 .fatalError = NULL,
232 .getParameterEntity = NULL,
233 .cdataBlock = NULL,
234 .externalSubset = NULL,
235 .initialized = XML_SAX2_MAGIC,
236 ._private = NULL,
237 .startElementNs = jabber_parser_element_start_libxml,
238 .endElementNs = jabber_parser_element_end_libxml,
239 .serror = NULL
240 };
241 #else
242 static GMarkupParser jabber_parser = {
243 jabber_parser_element_start,
244 jabber_parser_element_end,
245 jabber_parser_element_text,
246 NULL,
247 NULL
248 };
249 #endif
250
251 void
252 jabber_parser_setup(JabberStream *js)
253 {
254 #ifdef HAVE_LIBXML
255 /* This seems backwards, but it makes sense. The libxml code creates the parser
256 * context when you try to use it (this way, it can figure out the encoding at
257 * creation time. So, setting up the parser is just a matter of destroying any
258 * current parser. */
259 if (js->context) {
260 xmlParseChunk(js->context, NULL,0,1);
261 xmlFreeParserCtxt(js->context);
262 js->context = NULL;
263 }
264 #else
265 if(!js->context)
266 js->context = g_markup_parse_context_new(&jabber_parser, 0, js, NULL);
267 #endif
268 }
269
270
271 void jabber_parser_process(JabberStream *js, const char *buf, int len)
272 {
273
274 #ifndef HAVE_LIBXML
275 /* May need to check for other encodings and do the conversion here */
276 if(!g_markup_parse_context_parse(js->context, buf, len, NULL)) {
277 g_markup_parse_context_free(js->context);
278 js->context = NULL;
279 gaim_connection_error(js->gc, _("XML Parse error"));
280 }
281 #else
282 if (js->context == NULL) {
283 /* libxml inconsistently starts parsing on creating the parser, so so a ParseChunk
284 * right afterwards to force it. */
285 js->context = xmlCreatePushParserCtxt(&jabber_parser_libxml, js, buf, len, NULL);
286 xmlParseChunk(js->context, NULL, 0, 0);
287 } else if (xmlParseChunk(js->context, buf, len, 0) < 0) {
288 gaim_connection_error(js->gc, _("XML Parse error"));
289 }
290 #endif
291 }
292