7014
|
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 #include "connection.h"
|
|
24
|
|
25 #include "jabber.h"
|
|
26 #include "parser.h"
|
|
27 #include "xmlnode.h"
|
|
28
|
|
29 static void
|
|
30 jabber_parser_element_start(GMarkupParseContext *context,
|
|
31 const char *element_name, const char **attrib_names,
|
|
32 const char **attrib_values, gpointer user_data, GError **error)
|
|
33 {
|
|
34 JabberStream *js = user_data;
|
|
35 xmlnode *node;
|
|
36 int i;
|
|
37
|
|
38 if(!element_name) {
|
|
39 return;
|
|
40 } else if(!strcmp(element_name, "stream:stream")) {
|
|
41 js->protocol_version = JABBER_PROTO_0_9;
|
|
42 for(i=0; attrib_names[i]; i++) {
|
|
43 if(!strcmp(attrib_names[i], "version")
|
|
44 && !strcmp(attrib_values[i], "1.0")) {
|
|
45 js->protocol_version = JABBER_PROTO_1_0;
|
|
46 } else if(!strcmp(attrib_names[i], "id")) {
|
|
47 if(js->stream_id)
|
|
48 g_free(js->stream_id);
|
|
49 js->stream_id = g_strdup(attrib_values[i]);
|
|
50 }
|
|
51 }
|
|
52
|
|
53 if(js->state == JABBER_STREAM_INITIALIZING)
|
|
54 jabber_stream_set_state(js, JABBER_STREAM_AUTHENTICATING);
|
|
55 } else {
|
|
56
|
|
57 if(js->current)
|
|
58 node = xmlnode_new_child(js->current, element_name);
|
|
59 else
|
|
60 node = xmlnode_new(element_name);
|
|
61
|
|
62 for(i=0; attrib_names[i]; i++) {
|
|
63 xmlnode_set_attrib(node, attrib_names[i], attrib_values[i]);
|
|
64 }
|
|
65
|
|
66 js->current = node;
|
|
67 }
|
|
68 }
|
|
69
|
|
70 static void
|
|
71 jabber_parser_element_end(GMarkupParseContext *context,
|
|
72 const char *element_name, gpointer user_data, GError **error)
|
|
73 {
|
|
74 JabberStream *js = user_data;
|
|
75
|
|
76 if(!js->current)
|
|
77 return;
|
|
78
|
|
79 if(js->current->parent) {
|
|
80 if(!strcmp(js->current->name, element_name))
|
|
81 js->current = js->current->parent;
|
|
82 } else {
|
|
83 jabber_process_packet(js, js->current);
|
|
84 xmlnode_free(js->current);
|
|
85 js->current = NULL;
|
|
86 }
|
|
87 }
|
|
88
|
|
89 static void
|
|
90 jabber_parser_element_text(GMarkupParseContext *context, const char *text,
|
|
91 gsize text_len, gpointer user_data, GError **error)
|
|
92 {
|
|
93 JabberStream *js = user_data;
|
|
94
|
|
95 if(!js->current)
|
|
96 return;
|
|
97
|
|
98 if(!text || !text_len)
|
|
99 return;
|
|
100
|
|
101 xmlnode_insert_data(js->current, text, text_len);
|
|
102 }
|
|
103
|
|
104 static GMarkupParser jabber_parser = {
|
|
105 jabber_parser_element_start,
|
|
106 jabber_parser_element_end,
|
|
107 jabber_parser_element_text,
|
|
108 NULL,
|
|
109 NULL
|
|
110 };
|
|
111
|
|
112 void
|
|
113 jabber_parser_setup(JabberStream *js)
|
|
114 {
|
|
115 if(!js->context)
|
|
116 js->context = g_markup_parse_context_new(&jabber_parser, 0, js, NULL);
|
|
117 }
|
|
118
|
|
119
|
|
120 void jabber_parser_process(JabberStream *js, const char *buf, int len)
|
|
121 {
|
|
122
|
|
123 /* May need to check for other encodings and do the conversion here */
|
|
124
|
|
125 if(!g_markup_parse_context_parse(js->context, buf, len, NULL)) {
|
|
126 g_markup_parse_context_free(js->context);
|
|
127 js->context = NULL;
|
|
128 gaim_connection_error(js->gc, _("XML Parse error"));
|
|
129 }
|
|
130 }
|
|
131
|