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 {
|
7072
|
83 xmlnode *packet = js->current;
|
7014
|
84 js->current = NULL;
|
7072
|
85 jabber_process_packet(js, packet);
|
|
86 xmlnode_free(packet);
|
7014
|
87 }
|
|
88 }
|
|
89
|
|
90 static void
|
|
91 jabber_parser_element_text(GMarkupParseContext *context, const char *text,
|
|
92 gsize text_len, gpointer user_data, GError **error)
|
|
93 {
|
|
94 JabberStream *js = user_data;
|
|
95
|
|
96 if(!js->current)
|
|
97 return;
|
|
98
|
|
99 if(!text || !text_len)
|
|
100 return;
|
|
101
|
|
102 xmlnode_insert_data(js->current, text, text_len);
|
|
103 }
|
|
104
|
|
105 static GMarkupParser jabber_parser = {
|
|
106 jabber_parser_element_start,
|
|
107 jabber_parser_element_end,
|
|
108 jabber_parser_element_text,
|
|
109 NULL,
|
|
110 NULL
|
|
111 };
|
|
112
|
|
113 void
|
|
114 jabber_parser_setup(JabberStream *js)
|
|
115 {
|
|
116 if(!js->context)
|
|
117 js->context = g_markup_parse_context_new(&jabber_parser, 0, js, NULL);
|
|
118 }
|
|
119
|
|
120
|
|
121 void jabber_parser_process(JabberStream *js, const char *buf, int len)
|
|
122 {
|
|
123
|
|
124 /* May need to check for other encodings and do the conversion here */
|
|
125
|
|
126 if(!g_markup_parse_context_parse(js->context, buf, len, NULL)) {
|
|
127 g_markup_parse_context_free(js->context);
|
|
128 js->context = NULL;
|
|
129 gaim_connection_error(js->gc, _("XML Parse error"));
|
|
130 }
|
|
131 }
|
|
132
|