Wed, 12 Oct 2005 04:24:20 +0000
[gaim-migrate @ 13924]
this has been bugging me
| 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 | } | |
| 8296 | 52 | if(js->protocol_version == JABBER_PROTO_0_9) |
| 53 | js->auth_type = JABBER_AUTH_IQ_AUTH; | |
| 7014 | 54 | |
| 55 | if(js->state == JABBER_STREAM_INITIALIZING) | |
| 56 | jabber_stream_set_state(js, JABBER_STREAM_AUTHENTICATING); | |
| 57 | } else { | |
| 58 | ||
| 59 | if(js->current) | |
| 60 | node = xmlnode_new_child(js->current, element_name); | |
| 61 | else | |
| 62 | node = xmlnode_new(element_name); | |
| 63 | ||
| 64 | for(i=0; attrib_names[i]; i++) { | |
| 65 | xmlnode_set_attrib(node, attrib_names[i], attrib_values[i]); | |
| 66 | } | |
| 67 | ||
| 68 | js->current = node; | |
| 69 | } | |
| 70 | } | |
| 71 | ||
| 72 | static void | |
| 73 | jabber_parser_element_end(GMarkupParseContext *context, | |
| 74 | const char *element_name, gpointer user_data, GError **error) | |
| 75 | { | |
| 76 | JabberStream *js = user_data; | |
| 77 | ||
| 78 | if(!js->current) | |
| 79 | return; | |
| 80 | ||
| 81 | if(js->current->parent) { | |
| 82 | if(!strcmp(js->current->name, element_name)) | |
| 83 | js->current = js->current->parent; | |
| 84 | } else { | |
| 7072 | 85 | xmlnode *packet = js->current; |
| 7014 | 86 | js->current = NULL; |
| 7072 | 87 | jabber_process_packet(js, packet); |
| 88 | xmlnode_free(packet); | |
| 7014 | 89 | } |
| 90 | } | |
| 91 | ||
| 92 | static void | |
| 93 | jabber_parser_element_text(GMarkupParseContext *context, const char *text, | |
| 94 | gsize text_len, gpointer user_data, GError **error) | |
| 95 | { | |
| 96 | JabberStream *js = user_data; | |
| 97 | ||
| 98 | if(!js->current) | |
| 99 | return; | |
| 100 | ||
| 101 | if(!text || !text_len) | |
| 102 | return; | |
| 103 | ||
| 104 | xmlnode_insert_data(js->current, text, text_len); | |
| 105 | } | |
| 106 | ||
| 107 | static GMarkupParser jabber_parser = { | |
| 108 | jabber_parser_element_start, | |
| 109 | jabber_parser_element_end, | |
| 110 | jabber_parser_element_text, | |
| 111 | NULL, | |
| 112 | NULL | |
| 113 | }; | |
| 114 | ||
| 115 | void | |
| 116 | jabber_parser_setup(JabberStream *js) | |
| 117 | { | |
| 118 | if(!js->context) | |
| 119 | js->context = g_markup_parse_context_new(&jabber_parser, 0, js, NULL); | |
| 120 | } | |
| 121 | ||
| 122 | ||
| 123 | void jabber_parser_process(JabberStream *js, const char *buf, int len) | |
| 124 | { | |
| 125 | ||
| 126 | /* May need to check for other encodings and do the conversion here */ | |
| 127 | ||
| 128 | if(!g_markup_parse_context_parse(js->context, buf, len, NULL)) { | |
| 129 | g_markup_parse_context_free(js->context); | |
| 130 | js->context = NULL; | |
| 131 | gaim_connection_error(js->gc, _("XML Parse error")); | |
| 132 | } | |
| 133 | } | |
| 134 |