4 * XMPP (Jabber) service for the Citadel system
5 * Copyright (c) 2007 by Art Cancro
6 * This code is released under the terms of the GNU General Public License.
18 #include <sys/types.h>
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
35 #include <libcitadel.h>
38 #include "citserver.h"
46 #include "internet_addressing.h"
48 #include "ctdl_module.h"
52 #include "serv_xmpp.h"
55 /* We have just received a <stream> tag from the client, so send them ours */
57 void xmpp_stream_start(void *data, const char *supplied_el, const char **attr)
60 lprintf(CTDL_DEBUG, "New stream detected.\n");
63 if (!strcasecmp(attr[0], "to")) {
64 safestrncpy(XMPP->server_name, attr[1], sizeof XMPP->server_name);
69 cprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
71 cprintf("<stream:stream ");
72 cprintf("from=\"%s\" ", XMPP->server_name);
73 cprintf("id=\"%08x\" ", CC->cs_pid);
74 cprintf("version=\"1.0\" ");
75 cprintf("xmlns:stream=\"http://etherx.jabber.org/streams\" ");
76 cprintf("xmlns=\"jabber:client\">");
78 /* The features of this stream are... */
79 cprintf("<stream:features>");
82 /* If we're not logged in yet, offer SASL as our feature set */
83 xmpp_output_auth_mechs();
86 /* If we've logged in, now offer binding and sessions as our feature set */
87 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
88 cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
91 cprintf("</stream:features>");
95 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
100 /* Axe the namespace, we don't care about it */
101 safestrncpy(el, supplied_el, sizeof el);
102 while (sep = strchr(el, ':'), sep) {
106 lprintf(CTDL_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
108 for (i=0; attr[i] != NULL; i+=2) {
109 lprintf(CTDL_DEBUG, " Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
112 if (!strcasecmp(el, "stream")) {
113 xmpp_stream_start(data, supplied_el, attr);
116 else if (!strcasecmp(el, "query")) {
117 XMPP->iq_query_xmlns[0] = 0;
118 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
121 else if (!strcasecmp(el, "iq")) {
122 for (i=0; attr[i] != NULL; i+=2) {
123 if (!strcasecmp(attr[i], "type")) {
124 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
126 else if (!strcasecmp(attr[i], "id")) {
127 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
129 else if (!strcasecmp(attr[i], "from")) {
130 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
132 else if (!strcasecmp(attr[i], "to")) {
133 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
138 else if (!strcasecmp(el, "auth")) {
139 XMPP->sasl_auth_mech[0] = 0;
140 for (i=0; attr[i] != NULL; i+=2) {
141 if (!strcasecmp(attr[i], "mechanism")) {
142 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
150 void xmpp_xml_end(void *data, const char *supplied_el) {
154 /* Axe the namespace, we don't care about it */
155 safestrncpy(el, supplied_el, sizeof el);
156 while (sep = strchr(el, ':'), sep) {
160 lprintf(CTDL_DEBUG, "XMPP ELEMENT END : <%s>\n", el);
161 if (XMPP->chardata_len > 0) {
162 lprintf(CTDL_DEBUG, " chardata: %s\n", XMPP->chardata);
165 if (!strcasecmp(el, "resource")) {
166 if (XMPP->chardata_len > 0) {
167 safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
168 sizeof XMPP->iq_client_resource);
172 else if (!strcasecmp(el, "iq")) {
175 * iq type="get" (handle queries)
177 if (!strcasecmp(XMPP->iq_type, "get")) {
180 * Query on a namespace
182 if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
183 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
184 XMPP->iq_to, XMPP->iq_query_xmlns);
188 * Unknown queries ... return the XML equivalent of a blank stare
191 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
197 * If this <iq> stanza was a "bind" attempt, process it ...
199 else if ( (!IsEmptyStr(XMPP->iq_id)) && (!IsEmptyStr(XMPP->iq_client_resource)) ) {
201 /* Generate the "full JID" of the client resource */
203 // snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
207 // XMPP->iq_client_resource
210 snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
213 XMPP->iq_client_resource
216 /* Tell the client what its JID is */
218 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
219 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
220 cprintf("<jid>%s</jid>", XMPP->client_jid);
225 else if (XMPP->iq_session) {
226 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
231 cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
232 cprintf("<error></error>");
236 /* Now clear these fields out so they don't get used by a future stanza */
238 XMPP->iq_from[0] = 0;
240 XMPP->iq_type[0] = 0;
241 XMPP->iq_client_resource[0] = 0;
242 XMPP->iq_session = 0;
243 XMPP->iq_query_xmlns[0] = 0;
246 else if (!strcasecmp(el, "auth")) {
248 /* Try to authenticate (this function is responsible for the output stanza) */
249 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
251 /* Now clear these fields out so they don't get used by a future stanza */
252 XMPP->sasl_auth_mech[0] = 0;
255 else if (!strcasecmp(el, "session")) {
256 XMPP->iq_session = 1;
259 else if (!strcasecmp(el, "presence")) {
261 /* Respond to a <presence> update by firing back with presence information
262 * on the entire wholist. Check this assumption, it's probably wrong.
264 jabber_wholist_presence_dump();
267 XMPP->chardata_len = 0;
268 if (XMPP->chardata_alloc > 0) {
269 XMPP->chardata[0] = 0;
274 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
276 struct citxmpp *X = XMPP;
278 if (X->chardata_alloc == 0) {
279 X->chardata_alloc = SIZ;
280 X->chardata = malloc(X->chardata_alloc);
282 if ((X->chardata_len + len + 1) > X->chardata_alloc) {
283 X->chardata_alloc = X->chardata_len + len + 1024;
284 X->chardata = realloc(X->chardata, X->chardata_alloc);
286 memcpy(&X->chardata[X->chardata_len], s, len);
287 X->chardata_len += len;
288 X->chardata[X->chardata_len] = 0;
293 * This cleanup function blows away the temporary memory and files used by the XMPP service.
295 void xmpp_cleanup_function(void) {
297 /* Don't do this stuff if this is not a XMPP session! */
298 if (CC->h_command_function != xmpp_command_loop) return;
300 lprintf(CTDL_DEBUG, "Performing XMPP cleanup hook\n");
301 if (XMPP->chardata != NULL) {
302 free(XMPP->chardata);
303 XMPP->chardata = NULL;
304 XMPP->chardata_len = 0;
305 XMPP->chardata_alloc = 0;
307 XML_ParserFree(XMPP->xp);
314 * Here's where our XMPP session begins its happy day.
316 void xmpp_greeting(void) {
317 strcpy(CC->cs_clientname, "Jabber session");
318 CC->session_specific_data = malloc(sizeof(struct citxmpp));
319 memset(XMPP, 0, sizeof(struct citxmpp));
321 /* XMPP does not use a greeting, but we still have to initialize some things. */
323 XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
324 if (XMPP->xp == NULL) {
325 lprintf(CTDL_ALERT, "Cannot create XML parser!\n");
330 XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
331 XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
332 // XML_SetUserData(XMPP->xp, something...);
337 * Main command loop for XMPP sessions.
339 void xmpp_command_loop(void) {
344 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
345 retval = client_read(cmdbuf, 1);
347 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
352 /* FIXME ... this is woefully inefficient. */
354 XML_Parse(XMPP->xp, cmdbuf, 1, 0);
357 const char *CitadelServiceXMPP="XMPP";
359 #endif /* HAVE_EXPAT */
361 CTDL_MODULE_INIT(jabber)
365 /* CtdlRegisterServiceHook(config.c_xmpp_port, FIXME */
366 CtdlRegisterServiceHook(5222,
372 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
374 lprintf(CTDL_INFO, "This server is missing the Expat XML parser. Jabber service will be disabled.\n");
378 /* return our Subversion id for the Log */