comparison mcabber/libjabber/xstream.c @ 417:c3ae9251c197

Sync libjabber with upstream Sync with jabberd-1.4.4.
author Mikael Berthe <mikael@lilotux.net>
date Thu, 01 Sep 2005 23:29:21 +0200
parents f7f07794d2df
children 0aa9015f06df
comparison
equal deleted inserted replaced
416:48e7808c4191 417:c3ae9251c197
11 * 11 *
12 * You should have received a copy of the GNU General Public License 12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software 13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 * 15 *
16 * Jabber 16 * Copyrights
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/ 17 *
18 * Portions created by or assigned to Jabber.com, Inc. are
19 * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact
20 * information for Jabber.com, Inc. is available at http://www.jabber.com/.
21 *
22 * Portions Copyright (c) 1998-1999 Jeremie Miller.
23 *
24 * Acknowledgements
25 *
26 * Special thanks to the Jabber Open Source Contributors for their
27 * suggestions and support of Jabber.
28 *
29 */
30
31 /**
32 * @file xstream.c
33 * @brief handling of incoming XML stream based events
34 *
35 * xstream is a way to have a consistent method of handling incoming XML stream based events ...
36 * it doesn't handle the generation of an XML stream, but provides some facilities to help doing that
18 */ 37 */
19 38
20 #include <time.h> 39 #include <time.h>
21 #include <libxode.h> 40 #include <libxode.h>
22 41
23 /* xstream is a way to have a consistent method of handling incoming XML Stream based events... it doesn't handle the generation of an XML Stream, but provides some facilities to help do that */ 42 /* ========== internal expat callbacks =========== */
24 43
25 /******* internal expat callbacks *********/ 44 /**
45 * internal expat callback for read start tags of an element
46 */
26 void _xstream_startElement(xstream xs, const char* name, const char** atts) 47 void _xstream_startElement(xstream xs, const char* name, const char** atts)
27 { 48 {
28 pool p; 49 pool p;
29 50
30 /* if xstream is bad, get outa here */ 51 /* if xstream is bad, get outa here */
51 xs->depth++; 72 xs->depth++;
52 if(xs->depth > XSTREAM_MAXDEPTH) 73 if(xs->depth > XSTREAM_MAXDEPTH)
53 xs->status = XSTREAM_ERR; 74 xs->status = XSTREAM_ERR;
54 } 75 }
55 76
56 77 /**
78 * internal expat callback for read end tags of an element
79 */
57 void _xstream_endElement(xstream xs, const char* name) 80 void _xstream_endElement(xstream xs, const char* name)
58 { 81 {
59 xmlnode parent; 82 xmlnode parent;
60 83
61 /* if xstream is bad, get outa here */ 84 /* if xstream is bad, get outa here */
76 xs->node = parent; 99 xs->node = parent;
77 } 100 }
78 xs->depth--; 101 xs->depth--;
79 } 102 }
80 103
81 104 /**
105 * internal expat callback for read CDATA
106 */
82 void _xstream_charData(xstream xs, const char *str, int len) 107 void _xstream_charData(xstream xs, const char *str, int len)
83 { 108 {
84 /* if xstream is bad, get outa here */ 109 /* if xstream is bad, get outa here */
85 if(xs->status > XSTREAM_NODE) return; 110 if(xs->status > XSTREAM_NODE) return;
86 111
91 } 116 }
92 117
93 xmlnode_insert_cdata(xs->node, str, len); 118 xmlnode_insert_cdata(xs->node, str, len);
94 } 119 }
95 120
96 121 /**
122 * internal function to be registered as pool cleaner, frees a stream if the associated memory pool is freed
123 *
124 * @param pointer to the xstream to free
125 */
97 void _xstream_cleanup(void *arg) 126 void _xstream_cleanup(void *arg)
98 { 127 {
99 xstream xs = (xstream)arg; 128 xstream xs = (xstream)arg;
100 129
101 xmlnode_free(xs->node); /* cleanup anything left over */ 130 xmlnode_free(xs->node); /* cleanup anything left over */
102 XML_ParserFree(xs->parser); 131 XML_ParserFree(xs->parser);
103 } 132 }
104 133
105 134
106 /* creates a new xstream with given pool, xstream will be cleaned up w/ pool */ 135 /**
136 * creates a new xstream with given pool, xstream will be cleaned up w/ pool
137 *
138 * @param p the memory pool to use for the stream
139 * @param f function pointer to the event handler function
140 * @param arg parameter to pass to the event handler function
141 * @return the created xstream
142 */
107 xstream xstream_new(pool p, xstream_onNode f, void *arg) 143 xstream xstream_new(pool p, xstream_onNode f, void *arg)
108 { 144 {
109 xstream newx; 145 xstream newx;
110 146
111 if(p == NULL || f == NULL) 147 if(p == NULL || f == NULL)
127 pool_cleanup(p, _xstream_cleanup, (void *)newx); 163 pool_cleanup(p, _xstream_cleanup, (void *)newx);
128 164
129 return newx; 165 return newx;
130 } 166 }
131 167
132 /* attempts to parse the buff onto this stream firing events to the handler, returns the last known status */ 168 /**
169 * attempts to parse the buff onto this stream firing events to the handler
170 *
171 * @param xs the xstream to parse the data on
172 * @param buff the new data
173 * @param len length of the data
174 * @return last known xstream status
175 */
133 int xstream_eat(xstream xs, char *buff, int len) 176 int xstream_eat(xstream xs, char *buff, int len)
134 { 177 {
135 char *err; 178 char *err;
136 xmlnode xerr; 179 xmlnode xerr;
137 static char maxerr[] = "maximum node size reached"; 180 static char maxerr[] = "maximum node size reached";
172 } 215 }
173 216
174 217
175 /* STREAM CREATION UTILITIES */ 218 /* STREAM CREATION UTILITIES */
176 219
177 /* give a standard template xmlnode to work from */ 220 /** give a standard template xmlnode to work from
221 *
222 * @param namespace ("jabber:client", "jabber:server", ...)
223 * @param to where the stream is sent to
224 * @param from where we are (source of the stream)
225 * @return the xmlnode that has been generated as the template
226 */
178 xmlnode xstream_header(char *namespace, char *to, char *from) 227 xmlnode xstream_header(char *namespace, char *to, char *from)
179 { 228 {
180 xmlnode x; 229 xmlnode x;
181 char id[10]; 230 char id[11];
182 231
183 sprintf(id,"%X",(int)time(NULL)); 232 sprintf(id,"%X",(int)time(NULL));
184 233
185 x = xmlnode_new_tag("stream:stream"); 234 x = xmlnode_new_tag("stream:stream");
186 xmlnode_put_attrib(x, "xmlns:stream", "http://etherx.jabber.org/streams"); 235 xmlnode_put_attrib(x, "xmlns:stream", "http://etherx.jabber.org/streams");
193 xmlnode_put_attrib(x, "from", from); 242 xmlnode_put_attrib(x, "from", from);
194 243
195 return x; 244 return x;
196 } 245 }
197 246
198 /* trim the xmlnode to only the opening header :) [NO CHILDREN ALLOWED] */ 247 /**
248 * trim the xmlnode to only the opening header :)
249 *
250 * @note NO CHILDREN ALLOWED
251 *
252 * @param x the xmlnode
253 * @return string representation of the start tag
254 */
199 char *xstream_header_char(xmlnode x) 255 char *xstream_header_char(xmlnode x)
200 { 256 {
201 spool s; 257 spool s;
202 char *fixr, *head; 258 char *fixr, *head;
203 259
204 if(xmlnode_has_children(x)) 260 if(xmlnode_has_children(x)) {
205 {
206 fprintf(stderr,"Fatal Programming Error: xstream_header_char() was sent a header with children!\n"); 261 fprintf(stderr,"Fatal Programming Error: xstream_header_char() was sent a header with children!\n");
207 return NULL; 262 return NULL;
208 } 263 }
209 264
210 s = spool_new(xmlnode_pool(x)); 265 s = spool_new(xmlnode_pool(x));