3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
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/>.
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
);
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
);
53 int xmpp_presence (char *show
, char *status
)
57 int r
= sprintf (buf
, "<presence type='available'><show>%s</show><status>%s</status></presence>", show
, status
);
64 int xmpp_message (char *to
, char *message
, unsigned len
)
71 int r
= sprintf (buf
, "<message from='%s@%s' to='%s' type='chat'><body>%s</body></message>", xmpp
.user
, xmpp
.server
, to
, message
);
78 void xmpp_setup (char *user
, char *password
, char *resource
)
81 xmpp
.password
= password
;
82 xmpp
.resource
= resource
;
85 int xmpp_connect (char *server
, int port
)
87 if (net_connect (server
, port
) == -1)
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
);
101 int len
= net_recv (buf
, 511);
105 xml_handler (buf
, len
);
111 len
= net_recv (buf
, 511);
115 xml_handler (buf
, len
);
118 r
= sprintf (buf
, "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='%s'/>", xmpp
.mechanism
);
122 len
= net_recv (buf
, 511);
126 xml_handler (buf
, len
);
129 unsigned user_len
= strlen (xmpp
.user
);
130 unsigned pwd_len
= strlen (xmpp
.password
);
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
));
141 len
= net_recv (buf
, 511);
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
);
152 len
= net_recv (buf
, 511);
156 xml_handler (buf
, len
);
159 len
= net_recv (buf
, 511);
163 xml_handler (buf
, len
);
168 len
= net_recv (buf
, 511);
172 xml_handler (buf
, len
);
177 len
= net_recv (buf
, 511);
181 xml_handler (buf
, len
);
184 xmpp_presence ("online", "ZeXOS");
191 if (net_nonblock () == -1)
197 int len
= net_recv (buf
, 1023);
201 xml_handler (buf
, len
);
204 if (cmds_get () == -1)
216 net_send ("</stream>", 9);
218 printf ("> Disconnected from server\n");
221 free (xmpp
.mechanism
);
226 int xmpp_session_set (char *id
, unsigned len
)
234 xmpp
.session
= strndup (id
, len
);
239 // printf ("XMPP -> session id: '%s'\n", xmpp.session);
244 int xmpp_mechanism_set (char *mechanism
, unsigned len
)
246 if (!mechanism
|| !len
)
249 if (!strncmp (mechanism
, "PLAIN", 5))
250 xmpp
.mechanism
= strndup (mechanism
, 5);
251 if (!strncmp (mechanism
, "DIGEST-MD5", 9))
257 // printf ("XMPP -> mechanism: '%s'\n", xmpp.mechanism);
262 int xmpp_message_from (char *from
, unsigned from_len
, char *msg
, unsigned msg_len
)
267 for (i
= 0; i
< from_len
; i
++)
274 for (i
= 0; i
< msg_len
; i
++)