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"
54 struct xmpp_event *xmpp_queue = NULL;
56 /* We have just received a <stream> tag from the client, so send them ours */
58 void xmpp_stream_start(void *data, const char *supplied_el, const char **attr)
61 lprintf(CTDL_DEBUG, "New stream detected.\n");
64 if (!strcasecmp(attr[0], "to")) {
65 safestrncpy(XMPP->server_name, attr[1], sizeof XMPP->server_name);
70 cprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
72 cprintf("<stream:stream ");
73 cprintf("from=\"%s\" ", XMPP->server_name);
74 cprintf("id=\"%08x\" ", CC->cs_pid);
75 cprintf("version=\"1.0\" ");
76 cprintf("xmlns:stream=\"http://etherx.jabber.org/streams\" ");
77 cprintf("xmlns=\"jabber:client\">");
79 /* The features of this stream are... */
80 cprintf("<stream:features>");
83 /* If we're not logged in yet, offer SASL as our feature set */
84 xmpp_output_auth_mechs();
87 /* If we've logged in, now offer binding and sessions as our feature set */
88 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
89 cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
92 cprintf("</stream:features>");
94 CC->is_async = 1; /* XMPP sessions are inherently async-capable */
98 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
103 /* Axe the namespace, we don't care about it */
104 safestrncpy(el, supplied_el, sizeof el);
105 while (sep = strchr(el, ':'), sep) {
109 lprintf(CTDL_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
111 for (i=0; attr[i] != NULL; i+=2) {
112 lprintf(CTDL_DEBUG, " Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
115 if (!strcasecmp(el, "stream")) {
116 xmpp_stream_start(data, supplied_el, attr);
119 else if (!strcasecmp(el, "query")) {
120 XMPP->iq_query_xmlns[0] = 0;
121 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
124 else if (!strcasecmp(el, "iq")) {
125 for (i=0; attr[i] != NULL; i+=2) {
126 if (!strcasecmp(attr[i], "type")) {
127 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
129 else if (!strcasecmp(attr[i], "id")) {
130 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
132 else if (!strcasecmp(attr[i], "from")) {
133 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
135 else if (!strcasecmp(attr[i], "to")) {
136 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
141 else if (!strcasecmp(el, "auth")) {
142 XMPP->sasl_auth_mech[0] = 0;
143 for (i=0; attr[i] != NULL; i+=2) {
144 if (!strcasecmp(attr[i], "mechanism")) {
145 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
153 void xmpp_xml_end(void *data, const char *supplied_el) {
157 /* Axe the namespace, we don't care about it */
158 safestrncpy(el, supplied_el, sizeof el);
159 while (sep = strchr(el, ':'), sep) {
163 lprintf(CTDL_DEBUG, "XMPP ELEMENT END : <%s>\n", el);
164 if (XMPP->chardata_len > 0) {
165 lprintf(CTDL_DEBUG, " chardata: %s\n", XMPP->chardata);
168 if (!strcasecmp(el, "resource")) {
169 if (XMPP->chardata_len > 0) {
170 safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
171 sizeof XMPP->iq_client_resource);
175 else if (!strcasecmp(el, "iq")) {
178 * iq type="get" (handle queries)
180 if (!strcasecmp(XMPP->iq_type, "get")) {
183 * Query on a namespace
185 if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
186 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
187 XMPP->iq_to, XMPP->iq_query_xmlns);
191 * Unknown queries ... return the XML equivalent of a blank stare
194 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
200 * If this <iq> stanza was a "bind" attempt, process it ...
202 else if ( (!IsEmptyStr(XMPP->iq_id)) && (!IsEmptyStr(XMPP->iq_client_resource)) ) {
204 /* Generate the "full JID" of the client resource */
206 // snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
210 // XMPP->iq_client_resource
213 snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
216 XMPP->iq_client_resource
219 /* Tell the client what its JID is */
221 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
222 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
223 cprintf("<jid>%s</jid>", XMPP->client_jid);
228 else if (XMPP->iq_session) {
229 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
234 cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
235 cprintf("<error></error>");
239 /* Now clear these fields out so they don't get used by a future stanza */
241 XMPP->iq_from[0] = 0;
243 XMPP->iq_type[0] = 0;
244 XMPP->iq_client_resource[0] = 0;
245 XMPP->iq_session = 0;
246 XMPP->iq_query_xmlns[0] = 0;
249 else if (!strcasecmp(el, "auth")) {
251 /* Try to authenticate (this function is responsible for the output stanza) */
252 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
254 /* Now clear these fields out so they don't get used by a future stanza */
255 XMPP->sasl_auth_mech[0] = 0;
258 else if (!strcasecmp(el, "session")) {
259 XMPP->iq_session = 1;
262 else if (!strcasecmp(el, "presence")) {
264 /* Respond to a <presence> update by firing back with presence information
265 * on the entire wholist. Check this assumption, it's probably wrong.
267 jabber_wholist_presence_dump();
270 XMPP->chardata_len = 0;
271 if (XMPP->chardata_alloc > 0) {
272 XMPP->chardata[0] = 0;
277 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
279 struct citxmpp *X = XMPP;
281 if (X->chardata_alloc == 0) {
282 X->chardata_alloc = SIZ;
283 X->chardata = malloc(X->chardata_alloc);
285 if ((X->chardata_len + len + 1) > X->chardata_alloc) {
286 X->chardata_alloc = X->chardata_len + len + 1024;
287 X->chardata = realloc(X->chardata, X->chardata_alloc);
289 memcpy(&X->chardata[X->chardata_len], s, len);
290 X->chardata_len += len;
291 X->chardata[X->chardata_len] = 0;
296 * This cleanup function blows away the temporary memory and files used by the XMPP service.
298 void xmpp_cleanup_function(void) {
300 /* Don't do this stuff if this is not a XMPP session! */
301 if (CC->h_command_function != xmpp_command_loop) return;
303 lprintf(CTDL_DEBUG, "Performing XMPP cleanup hook\n");
304 if (XMPP->chardata != NULL) {
305 free(XMPP->chardata);
306 XMPP->chardata = NULL;
307 XMPP->chardata_len = 0;
308 XMPP->chardata_alloc = 0;
310 XML_ParserFree(XMPP->xp);
317 * Here's where our XMPP session begins its happy day.
319 void xmpp_greeting(void) {
320 strcpy(CC->cs_clientname, "Jabber session");
321 CC->session_specific_data = malloc(sizeof(struct citxmpp));
322 memset(XMPP, 0, sizeof(struct citxmpp));
324 /* XMPP does not use a greeting, but we still have to initialize some things. */
326 XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
327 if (XMPP->xp == NULL) {
328 lprintf(CTDL_ALERT, "Cannot create XML parser!\n");
333 XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
334 XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
335 // XML_SetUserData(XMPP->xp, something...);
340 * Main command loop for XMPP sessions.
342 void xmpp_command_loop(void) {
347 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
348 retval = client_read(cmdbuf, 1);
350 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
355 /* FIXME ... this is woefully inefficient. */
357 XML_Parse(XMPP->xp, cmdbuf, 1, 0);
362 * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
364 void xmpp_async_loop(void) {
365 xmpp_process_events();
366 jabber_output_incoming_messages();
371 * Login hook for XMPP sessions
373 void xmpp_login_hook(void) {
374 xmpp_queue_event(XMPP_EVT_LOGIN, CC->cs_inet_email);
379 * Logout hook for XMPP sessions
381 void xmpp_logout_hook(void) {
382 xmpp_queue_event(XMPP_EVT_LOGOUT, CC->cs_inet_email);
386 const char *CitadelServiceXMPP="XMPP";
388 #endif /* HAVE_EXPAT */
390 CTDL_MODULE_INIT(jabber)
394 CtdlRegisterServiceHook(5222, /* FIXME change to config.c_xmpp_port */
400 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
401 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGIN);
402 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGOUT);
405 lprintf(CTDL_INFO, "This server is missing the Expat XML parser. Jabber service will be disabled.\n");
409 /* return our Subversion id for the Log */