connwrap - initialize gnutls session in cw_connect
[centerim.git] / libjabber / xstream.c
blob0761d8cf81900d6a5d2b9a9cff6f7f2402dc9d07
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
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
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * Jabber
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
20 #include <libxode.h>
22 /* 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 */
24 /******* internal expat callbacks *********/
25 void _xstream_startElement(xstream xs, const char* name, const char** atts)
27 pool p;
29 /* if xstream is bad, get outa here */
30 if(xs->status > XSTREAM_NODE) return;
32 if(xs->node == NULL)
34 p = pool_heap(5*1024); /* 5k, typically 1-2k each plus copy of self and workspace */
35 xs->node = xmlnode_new_tag_pool(p,name);
36 xmlnode_put_expat_attribs(xs->node, atts);
38 if(xs->status == XSTREAM_ROOT)
40 xs->status = XSTREAM_NODE; /* flag status that we're processing nodes now */
41 (xs->f)(XSTREAM_ROOT, xs->node, xs->arg); /* send the root, f must free all nodes */
42 xs->node = NULL;
44 }else{
45 xs->node = xmlnode_insert_tag(xs->node, name);
46 xmlnode_put_expat_attribs(xs->node, atts);
49 /* depth check */
50 xs->depth++;
51 if(xs->depth > XSTREAM_MAXDEPTH)
52 xs->status = XSTREAM_ERR;
56 void _xstream_endElement(xstream xs, const char* name)
58 xmlnode parent;
60 /* if xstream is bad, get outa here */
61 if(xs->status > XSTREAM_NODE) return;
63 /* if it's already NULL we've received </stream>, tell the app and we're outta here */
64 if(xs->node == NULL)
66 xs->status = XSTREAM_CLOSE;
67 (xs->f)(XSTREAM_CLOSE, NULL, xs->arg);
68 }else{
69 parent = xmlnode_get_parent(xs->node);
71 /* we are the top-most node, feed to the app who is responsible to delete it */
72 if(parent == NULL)
73 (xs->f)(XSTREAM_NODE, xs->node, xs->arg);
75 xs->node = parent;
77 xs->depth--;
81 void _xstream_charData(xstream xs, const char *str, int len)
83 /* if xstream is bad, get outa here */
84 if(xs->status > XSTREAM_NODE) return;
86 if(xs->node == NULL)
88 /* we must be in the root of the stream where CDATA is irrelevant */
89 return;
92 xmlnode_insert_cdata(xs->node, str, len);
96 void _xstream_cleanup(void *arg)
98 xstream xs = (xstream)arg;
100 xmlnode_free(xs->node); /* cleanup anything left over */
101 XML_ParserFree(xs->parser);
105 /* creates a new xstream with given pool, xstream will be cleaned up w/ pool */
106 xstream xstream_new(pool p, xstream_onNode f, void *arg)
108 xstream newx;
110 if(p == NULL || f == NULL)
112 fprintf(stderr,"Fatal Programming Error: xstream_new() was improperly called with NULL.\n");
113 return NULL;
116 newx = pmalloco(p, sizeof(_xstream));
117 newx->p = p;
118 newx->f = f;
119 newx->arg = arg;
121 /* create expat parser and ensure cleanup */
122 newx->parser = XML_ParserCreate(NULL);
123 XML_SetUserData(newx->parser, (void *)newx);
124 XML_SetElementHandler(newx->parser, (void *)_xstream_startElement, (void *)_xstream_endElement);
125 XML_SetCharacterDataHandler(newx->parser, (void *)_xstream_charData);
126 pool_cleanup(p, _xstream_cleanup, (void *)newx);
128 return newx;
131 /* attempts to parse the buff onto this stream firing events to the handler, returns the last known status */
132 int xstream_eat(xstream xs, char *buff, int len)
134 char *err;
135 xmlnode xerr;
136 static char maxerr[] = "maximum node size reached";
137 static char deeperr[] = "maximum node depth reached";
139 if(xs == NULL)
141 fprintf(stderr,"Fatal Programming Error: xstream_eat() was improperly called with NULL.\n");
142 return XSTREAM_ERR;
145 if(len == 0 || buff == NULL)
146 return xs->status;
148 if(len == -1) /* easy for hand-fed eat calls */
149 len = strlen(buff);
151 if(!XML_Parse(xs->parser, buff, len, 0))
153 err = (char *)XML_ErrorString(XML_GetErrorCode(xs->parser));
154 xs->status = XSTREAM_ERR;
155 }else if(pool_size(xmlnode_pool(xs->node)) > XSTREAM_MAXNODE || xs->cdata_len > XSTREAM_MAXNODE){
156 err = maxerr;
157 xs->status = XSTREAM_ERR;
158 }else if(xs->status == XSTREAM_ERR){ /* set within expat handlers */
159 err = deeperr;
162 /* fire parsing error event, make a node containing the error string */
163 if(xs->status == XSTREAM_ERR)
165 xerr = xmlnode_new_tag("error");
166 xmlnode_insert_cdata(xerr,err,-1);
167 (xs->f)(XSTREAM_ERR, xerr, xs->arg);
170 return xs->status;
174 /* STREAM CREATION UTILITIES */
176 /* give a standard template xmlnode to work from */
177 xmlnode xstream_header(char *namespace, char *to, char *from)
179 xmlnode x;
180 char id[10];
182 sprintf(id,"%X",(int)time(NULL));
184 x = xmlnode_new_tag("stream:stream");
185 xmlnode_put_attrib(x, "xmlns:stream", "http://etherx.jabber.org/streams");
186 xmlnode_put_attrib(x, "id", id);
187 if(namespace != NULL)
188 xmlnode_put_attrib(x, "xmlns", namespace);
189 if(to != NULL)
190 xmlnode_put_attrib(x, "to", to);
191 if(from != NULL)
192 xmlnode_put_attrib(x, "from", from);
194 return x;
197 /* trim the xmlnode to only the opening header :) [NO CHILDREN ALLOWED] */
198 char *xstream_header_char(xmlnode x)
200 spool s;
201 char *fixr, *head;
203 if(xmlnode_has_children(x))
205 fprintf(stderr,"Fatal Programming Error: xstream_header_char() was sent a header with children!\n");
206 return NULL;
209 s = spool_new(xmlnode_pool(x));
210 spooler(s,"<?xml version='1.0'?>",xmlnode2str(x),s);
211 head = spool_print(s);
212 fixr = strstr(head,"/>");
213 if (fixr != NULL)
215 *fixr = '>';
216 ++fixr;
217 *fixr = '\0';
220 return head;