Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / zjab / xml.c
blob9c6609369380e223eaef0398f2aa19b1f4fdc44c
1 /*
2 * ZeX/OS
3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "net.h"
24 #include "xmpp.h"
26 /** XML tags */
28 char *xml_arg_handler (char *buf, unsigned len, char *arg, unsigned *arg_len)
30 int arg_start = -1;
31 int arg_end = -1;
32 unsigned l = *arg_len;
34 unsigned i;
35 for (i = 0; i < len; i ++) {
36 if (arg_start == -1) {
37 if (buf[i] == ' ')
38 arg_start = i + 1;
39 // for (; buf[i+1] == ' '; i ++);
40 } else if (arg_end == -1) {
41 if (buf[i] == ' ')
42 arg_end = i - 1;
43 else if ((i + 1) == len)
44 arg_end = i;
47 if (arg_start != -1 && arg_end != -1) {
48 if (!strncmp (buf + arg_start, arg, *arg_len)) {
49 *arg_len = arg_end - arg_start - l - 1;
50 return buf + arg_start + l + 1;
53 arg_start = -1;
54 arg_end = -1;
55 i --;
59 return 0;
62 char *xml_value_handler (char *val, unsigned *l)
64 char *buf = val + *l + 1;
66 int val_start = -1;
67 int val_end = -1;
69 unsigned len = strlen (buf);
71 unsigned i;
72 for (i = 0; i < len; i ++) {
73 if (val_start == -1) {
74 if (buf[i] == '<')
75 val_start = i + 1;
76 // for (; buf[i+1] == ' '; i ++);
77 } else if (val_end == -1)
78 if (buf[i] == '>')
79 val_end = i;
81 if (val_start != -1 && val_end != -1) {
82 if (buf[val_start - 1] == '<' &&
83 buf[val_start] == '/' &&
84 !strncmp (buf + val_start + 1, val, *l) &&
85 buf[*l + val_start + 1] == '>') {
86 *l = val_start - 1;
87 return buf;
90 val_start = -1;
91 val_end = -1;
92 i --;
96 return 0;
99 int xml_tag_message (char *buf, unsigned len)
101 unsigned l = 5;
103 char *arg = xml_arg_handler (buf, len, "from=", &l);
105 if (!arg)
106 return -1;
108 char *body = strstr (buf, "<body>");
110 if (!body)
111 return 0;
113 unsigned l2 = 4;
115 char *val = xml_value_handler (body+1, &l2);
117 if (!val)
118 return -1;
120 xmpp_message_from (arg, l, val, l2);
122 return 0;
125 int xml_tag_stream (char *buf, unsigned len)
127 unsigned l = 3;
129 char *arg = xml_arg_handler (buf, len, "id=", &l);
131 if (!arg)
132 return -1;
134 return xmpp_session_set (arg, l);
137 int xml_tag_mechanism (char *buf, unsigned len)
139 unsigned l = len;
141 char *val = xml_value_handler (buf, &l);
143 if (!val)
144 return -1;
146 xmpp_mechanism_set (val, l);
148 return 0;
151 int xml_tag_success ()
153 printf ("XMPP -> login successfull\n");
154 return 0;
157 int xml_tag_failure ()
159 printf ("XMPP -> unknown failure\n");
160 return -1;
163 int xml_tag (char *buf, unsigned len)
165 if (!strncmp (buf, "message", 7))
166 return xml_tag_message (buf, len);
167 if (!strncmp (buf, "?xml", 4))
168 return 0;
169 if (!strncmp (buf, "stream:stream", 13))
170 return xml_tag_stream (buf, len);
171 if (!strncmp (buf, "mechanisms", 10))
172 return 0;
173 if (!strncmp (buf, "mechanism", 9))
174 return xml_tag_mechanism (buf, len);
175 if (!strncmp (buf, "success", 7))
176 return xml_tag_success ();
177 if (!strncmp (buf, "failure", 7))
178 return xml_tag_failure ();
180 return 0;
183 int xml_handler (char *buf, unsigned len)
185 int tag_start = -1;
186 int tag_end = -1;
188 unsigned i;
189 for (i = 0; i < len; i ++) {
190 if (tag_start == -1) {
191 if (buf[i] == '<')
192 tag_start = i + 1;
193 } else if (tag_end == -1)
194 if (buf[i] == '>')
195 tag_end = i;
197 if (tag_start != -1 && tag_end != -1) {
198 if (!xml_tag (buf + tag_start, tag_end - tag_start) == -1)
199 return -1;
201 tag_start = -1;
202 tag_end = -1;
206 return 0;