Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / zjab / xmpp.c
blobc699c037a1179af852a4619d4813cfcb81654b37
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 "xml.h"
25 #include "xmpp.h"
26 #include "cmds.h"
27 #include "utils.h"
29 static xmpp_t xmpp;
31 int xmpp_session ()
33 char buf[256];
35 int r = sprintf (buf, "<iq xmlns='jabber:client' type='set' id='%s_1'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>", xmpp.session);
37 net_send (buf, r);
39 return 0;
42 int xmpp_bind ()
44 char buf[256];
46 int r = sprintf (buf, "<iq type='set' id='%s'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>%s</resource></bind></iq>", xmpp.session, xmpp.resource);
48 net_send (buf, r);
50 return 0;
53 int xmpp_presence (char *show, char *status)
55 char buf[256];
57 int r = sprintf (buf, "<presence type='available'><show>%s</show><status>%s</status></presence>", show, status);
59 net_send (buf, r);
61 return 0;
64 int xmpp_message (char *to, char *message, unsigned len)
66 if (len > 480)
67 return -1;
69 char buf[512];
71 int r = sprintf (buf, "<message from='%s@%s' to='%s' type='chat'><body>%s</body></message>", xmpp.user, xmpp.server, to, message);
73 net_send (buf, r);
75 return 0;
78 void xmpp_setup (char *user, char *password, char *resource)
80 xmpp.user = user;
81 xmpp.password = password;
82 xmpp.resource = resource;
85 int xmpp_connect (char *server, int port)
87 if (net_connect (server, port) == -1)
88 return -1;
90 xmpp.session = 0;
91 xmpp.server = server;
92 xmpp.port = port;
93 xmpp.mechanism = 0;
95 char buf[512];
97 int r = sprintf (buf, "<?xml version='1.0'?><stream:stream to='%s' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>", server);
99 net_send (buf, r);
101 int len = net_recv (buf, 511);
103 if (len > 0) {
104 buf[len] = '\0';
105 xml_handler (buf, len);
108 if (xmpp.mechanism)
109 goto success;
111 len = net_recv (buf, 511);
113 if (len > 0) {
114 buf[len] = '\0';
115 xml_handler (buf, len);
117 success:
118 r = sprintf (buf, "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='%s'/>", xmpp.mechanism);
120 net_send (buf, r);
122 len = net_recv (buf, 511);
124 if (len > 0) {
125 buf[len] = '\0';
126 xml_handler (buf, len);
129 unsigned user_len = strlen (xmpp.user);
130 unsigned pwd_len = strlen (xmpp.password);
132 buf[0] = '\0';
133 strcpy (buf+1, xmpp.user);
134 buf[1+user_len] = '\0';
135 strcpy (buf+2+user_len, xmpp.password);
137 r = sprintf (buf, "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>%s</response>", base64 (buf, 2+user_len+pwd_len));
139 net_send (buf, r);
141 len = net_recv (buf, 511);
143 if (len > 0) {
144 buf[len] = '\0';
145 xml_handler (buf, len);
148 r = sprintf (buf, "<?xml version='1.0'?><stream:stream to='%s' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>", server);
150 net_send (buf, r);
152 len = net_recv (buf, 511);
154 if (len > 0) {
155 buf[len] = '\0';
156 xml_handler (buf, len);
159 len = net_recv (buf, 511);
161 if (len > 0) {
162 buf[len] = '\0';
163 xml_handler (buf, len);
166 xmpp_bind ();
168 len = net_recv (buf, 511);
170 if (len > 0) {
171 buf[len] = '\0';
172 xml_handler (buf, len);
175 xmpp_session ();
177 len = net_recv (buf, 511);
179 if (len > 0) {
180 buf[len] = '\0';
181 xml_handler (buf, len);
184 xmpp_presence ("online", "ZeXOS");
186 return 0;
189 int xmpp_loop ()
191 if (net_nonblock () == -1)
192 return -1;
194 for (;;) {
195 char buf[1024];
197 int len = net_recv (buf, 1023);
199 if (len > 0) {
200 buf[len] = '\0';
201 xml_handler (buf, len);
204 if (cmds_get () == -1)
205 break;
206 #ifndef LINUX
207 schedule ();
208 #endif
211 return 0;
214 int xmpp_close ()
216 net_send ("</stream>", 9);
218 printf ("> Disconnected from server\n");
220 if (xmpp.mechanism)
221 free (xmpp.mechanism);
223 return net_close ();
226 int xmpp_session_set (char *id, unsigned len)
228 if (!id || !len)
229 return -1;
231 if (xmpp.session)
232 free (xmpp.session);
234 xmpp.session = strndup (id, len);
236 if (!xmpp.session)
237 return -1;
239 // printf ("XMPP -> session id: '%s'\n", xmpp.session);
241 return 0;
244 int xmpp_mechanism_set (char *mechanism, unsigned len)
246 if (!mechanism || !len)
247 return -1;
249 if (!strncmp (mechanism, "PLAIN", 5))
250 xmpp.mechanism = strndup (mechanism, 5);
251 if (!strncmp (mechanism, "DIGEST-MD5", 9))
252 return 0; /*TODO */
254 if (!xmpp.mechanism)
255 return -1;
257 // printf ("XMPP -> mechanism: '%s'\n", xmpp.mechanism);
259 return 0;
262 int xmpp_message_from (char *from, unsigned from_len, char *msg, unsigned msg_len)
264 setcolor (15, 0);
266 unsigned i;
267 for (i = 0; i < from_len; i ++)
268 putchar (from[i]);
270 printf (": ");
272 setcolor (14, 0);
274 for (i = 0; i < msg_len; i ++)
275 putchar (msg[i]);
277 putchar ('\n');
279 setcolor (7, 0);
281 return 0;