Remove protocol detection altogether
[cascardo/rnetproxy.git] / iksemel_extra.c
1 /*
2 ** Copyright (C) 2006 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3 **  
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **  
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU General Public License for more details.
13 **  
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 **  
18 */
19
20 #include "iksemel_extra.h"
21 #include <glib.h>
22
23 #ifndef FALSE
24 #define FALSE 0
25 #endif
26 #ifndef TRUE
27 #define TRUE 1
28 #endif
29
30 struct _iks_extra_stream_t
31 {
32   iksStreamHook* hook;
33   void* data;
34   iks* node;
35   int top;
36 };
37
38 void iks_extra_insert_attrib (iks* node, char** atts)
39 {
40   if (atts == NULL)
41     return;
42   while (*atts)
43     {
44       iks_insert_attrib (node, atts[0], atts[1]);
45       atts += 2;
46     }
47 }
48
49 int tagHook (void* user_data, char* name, char** atts, int type)
50 {
51   struct _iks_extra_stream_t* pdata;
52   pdata = (struct _iks_extra_stream_t*) user_data;
53   switch (type)
54     {
55     case IKS_OPEN:
56       if (pdata->top == FALSE || g_str_equal (name, "stream:stream"))
57         {
58           pdata->top = TRUE;
59           pdata->node = iks_new (name);
60           iks_extra_insert_attrib (pdata->node, atts);
61           pdata->hook (pdata->data, IKS_NODE_START, pdata->node);
62           pdata->node = NULL;
63         }
64       else if (pdata->node == NULL)
65         {
66           pdata->node = iks_new (name);
67           iks_extra_insert_attrib (pdata->node, atts);
68         }
69       else
70         {
71           pdata->node = iks_insert (pdata->node, name);
72           iks_extra_insert_attrib (pdata->node, atts);
73         }
74       break;
75     case IKS_CLOSE:
76       if (pdata->node == NULL)
77         {
78           pdata->node = iks_new (name);
79           pdata->hook (pdata->data, IKS_NODE_STOP, pdata->node);
80           pdata->node = NULL;
81           pdata->top = FALSE;
82         }
83       else if (iks_parent (pdata->node) == NULL)
84         {
85           pdata->hook (pdata->data, IKS_NODE_NORMAL, pdata->node);
86           pdata->node = NULL;
87         }
88       else
89         {
90           pdata->node = iks_parent (pdata->node);
91         }
92       break;
93     case IKS_SINGLE:
94       if (pdata->top == FALSE)
95         {
96           return -IKS_OK;
97         }
98       else if (pdata->node == NULL)
99         {
100           pdata->node = iks_new (name);
101           iks_extra_insert_attrib (pdata->node, atts);
102           pdata->hook (pdata->data, IKS_NODE_NORMAL, pdata->node);
103           pdata->node = NULL;
104         }
105       else
106         {
107           pdata->node = iks_insert (pdata->node, name);
108           iks_extra_insert_attrib (pdata->node, atts);
109           pdata->node = iks_parent (pdata->node);
110         }
111       break;
112     default:
113       break;
114     }
115   return IKS_OK;
116 }
117
118 int dataHook (void* user_data, char* data, size_t len)
119 {
120   struct _iks_extra_stream_t* pdata;
121   pdata = (struct _iks_extra_stream_t*) user_data;
122   iks_insert_cdata (pdata->node, data, len);
123   return IKS_OK;
124 }
125
126 void deleteHook (void* user_data)
127 {
128   iks_free (user_data);
129 }
130
131 iksparser* iks_extra_stream_new (void* data, iksStreamHook* hook)
132 {
133   iksparser* parser;
134   struct _iks_extra_stream_t* pdata;
135   pdata = iks_malloc (sizeof (struct _iks_extra_stream_t));
136   pdata->hook = hook;
137   pdata->data = data;
138   pdata->node = NULL;
139   pdata->top = FALSE;
140   parser = iks_sax_extend (iks_stack_new (256, 0),
141                            pdata, tagHook, dataHook, deleteHook);
142   return parser;
143 }