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/>.
28 char *xml_arg_handler (char *buf
, unsigned len
, char *arg
, unsigned *arg_len
)
32 unsigned l
= *arg_len
;
35 for (i
= 0; i
< len
; i
++) {
36 if (arg_start
== -1) {
39 // for (; buf[i+1] == ' '; i ++);
40 } else if (arg_end
== -1) {
43 else if ((i
+ 1) == len
)
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;
62 char *xml_value_handler (char *val
, unsigned *l
)
64 char *buf
= val
+ *l
+ 1;
69 unsigned len
= strlen (buf
);
72 for (i
= 0; i
< len
; i
++) {
73 if (val_start
== -1) {
76 // for (; buf[i+1] == ' '; i ++);
77 } else if (val_end
== -1)
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] == '>') {
99 int xml_tag_message (char *buf
, unsigned len
)
103 char *arg
= xml_arg_handler (buf
, len
, "from=", &l
);
108 char *body
= strstr (buf
, "<body>");
115 char *val
= xml_value_handler (body
+1, &l2
);
120 xmpp_message_from (arg
, l
, val
, l2
);
125 int xml_tag_stream (char *buf
, unsigned len
)
129 char *arg
= xml_arg_handler (buf
, len
, "id=", &l
);
134 return xmpp_session_set (arg
, l
);
137 int xml_tag_mechanism (char *buf
, unsigned len
)
141 char *val
= xml_value_handler (buf
, &l
);
146 xmpp_mechanism_set (val
, l
);
151 int xml_tag_success ()
153 printf ("XMPP -> login successfull\n");
157 int xml_tag_failure ()
159 printf ("XMPP -> unknown failure\n");
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))
169 if (!strncmp (buf
, "stream:stream", 13))
170 return xml_tag_stream (buf
, len
);
171 if (!strncmp (buf
, "mechanisms", 10))
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 ();
183 int xml_handler (char *buf
, unsigned len
)
189 for (i
= 0; i
< len
; i
++) {
190 if (tag_start
== -1) {
193 } else if (tag_end
== -1)
197 if (tag_start
!= -1 && tag_end
!= -1) {
198 if (!xml_tag (buf
+ tag_start
, tag_end
- tag_start
) == -1)