1b4d78424fccba42bfb83ff2aff54dbf00131e31
[citadel.git] / citadel / modules / xmpp / serv_xmpp.c
1 /*
2  * XMPP (Jabber) service for the Citadel system
3  * Copyright (c) 2007-2021 by Art Cancro and citadel.org
4  *
5  * This program is open source 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.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include "sysdep.h"
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <sys/types.h>
29
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
32 # include <time.h>
33 #else
34 # if HAVE_SYS_TIME_H
35 #  include <sys/time.h>
36 # else
37 #  include <time.h>
38 # endif
39 #endif
40
41 #include <sys/wait.h>
42 #include <string.h>
43 #include <limits.h>
44 #include <ctype.h>
45 #include <libcitadel.h>
46 #include <expat.h>
47 #include "citadel.h"
48 #include "server.h"
49 #include "citserver.h"
50 #include "support.h"
51 #include "config.h"
52 #include "user_ops.h"
53 #include "database.h"
54 #include "msgbase.h"
55 #include "internet_addressing.h"
56 #include "ctdl_module.h"
57 #include "serv_xmpp.h"
58
59 /* uncomment for more verbosity - it will log all received XML tags */
60 // #define XMPP_XML_DEBUG
61
62 /* XML_StopParser is present in expat 2.x */
63 #if XML_MAJOR_VERSION > 1
64 #define HAVE_XML_STOPPARSER
65 #endif
66
67 struct xmpp_event *xmpp_queue = NULL;
68
69 #ifdef HAVE_XML_STOPPARSER
70 /* Stop the parser if an entity declaration is hit. */
71 static void xmpp_entity_declaration(void *userData, const XML_Char *entityName,
72                                 int is_parameter_entity, const XML_Char *value,
73                                 int value_length, const XML_Char *base,
74                                 const XML_Char *systemId, const XML_Char *publicId,
75                                 const XML_Char *notationName
76 ) {
77         syslog(LOG_WARNING, "xmpp: illegal entity declaration encountered; stopping parser.");
78         XML_StopParser(XMPP->xp, XML_FALSE);
79 }
80 #endif
81
82
83 /*
84  * support function for xmlesc() which helps with UTF-8 strings
85  */
86 static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE)
87 {
88         int n = 0;
89         unsigned char test = (1<<7);
90
91         if ((*CharS & 0xC0) != 0xC0) {
92                 return 1;
93         }
94
95         while ((n < 8) && ((test & ((unsigned char)*CharS)) != 0))  {
96                 test = test >> 1;
97                 n ++;
98         }
99         if ((n > 6) || ((CharE - CharS) < n)) {
100                 n = 0;
101         }
102         return n;
103 }
104
105
106 /*
107  * Given a source string and a target buffer, returns the string
108  * properly escaped for insertion into an XML stream.  Returns a
109  * pointer to the target buffer for convenience.
110  */
111 char *xmlesc(char *buf, char *str, int bufsiz)
112 {
113         int IsUtf8Sequence;
114         char *ptr, *pche;
115         unsigned char ch;
116         int inlen;
117         int len = 0;
118
119         if (!buf) return(NULL);
120         buf[0] = 0;
121         len = 0;
122         if (!str) {
123                 return(buf);
124         }
125         inlen = strlen(str);
126         pche = str + inlen;
127
128         for (ptr=str; *ptr; ptr++) {
129                 ch = *ptr;
130                 if (ch == '<') {
131                         strcpy(&buf[len], "&lt;");
132                         len += 4;
133                 }
134                 else if (ch == '>') {
135                         strcpy(&buf[len], "&gt;");
136                         len += 4;
137                 }
138                 else if (ch == '&') {
139                         strcpy(&buf[len], "&amp;");
140                         len += 5;
141                 }
142                 else if ((ch >= 0x20) && (ch <= 0x7F)) {
143                         buf[len++] = ch;
144                         buf[len] = 0;
145                 }
146                 else if (ch < 0x20) {
147                         buf[len++] = '_';       // we probably shouldn't be doing this
148                         buf[len] = 0;
149                 }
150                 else {
151                         IsUtf8Sequence =  Ctdl_GetUtf8SequenceLength(ptr, pche);
152                         if (IsUtf8Sequence)
153                         {
154                                 while ((IsUtf8Sequence > 0) && 
155                                        (ptr < pche))
156                                 {
157                                         buf[len] = *ptr;
158                                         ptr ++;
159                                         --IsUtf8Sequence;
160                                 }
161                         }
162                         else
163                         {
164                                 char oct[10];
165                                 sprintf(oct, "&#%o;", ch);
166                                 strcpy(&buf[len], oct);
167                                 len += strlen(oct);
168                         }
169                 }
170                 if ((len + 6) > bufsiz) {
171                         return(buf);
172                 }
173         }
174         return(buf);
175 }
176
177
178 /*
179  * We have just received a <stream> tag from the client, so send them ours
180  */
181 void xmpp_stream_start(void *data, const char *supplied_el, const char **attr)
182 {
183         char xmlbuf[256];
184
185         while (*attr) {
186                 if (!strcasecmp(attr[0], "to")) {
187                         safestrncpy(XMPP->server_name, attr[1], sizeof XMPP->server_name);
188                 }
189                 attr += 2;
190         }
191
192         cprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
193         cprintf("<stream:stream ");
194         cprintf("from=\"%s\" ", xmlesc(xmlbuf, XMPP->server_name, sizeof xmlbuf));
195         cprintf("id=\"%08x\" ", CC->cs_pid);
196         cprintf("version=\"1.0\" ");
197         cprintf("xmlns:stream=\"http://etherx.jabber.org/streams\" ");
198         cprintf("xmlns=\"jabber:client\">");
199
200         /* The features of this stream are... */
201         cprintf("<stream:features>");
202
203         /*
204          * TLS encryption (but only if it isn't already active)
205          */ 
206 #ifdef HAVE_OPENSSL
207         if (!CC->redirect_ssl) {
208                 cprintf("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>");
209         }
210 #endif
211
212         if (!CC->logged_in) {
213                 /* If we're not logged in yet, offer SASL as our feature set */
214                 xmpp_output_auth_mechs();
215
216                 /* Also offer non-SASL authentication */
217                 cprintf("<auth xmlns=\"http://jabber.org/features/iq-auth\"/>");
218         }
219
220         /* Offer binding and sessions as part of our feature set */
221         cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
222         cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
223         cprintf("</stream:features>");
224
225         CC->is_async = 1;               /* XMPP sessions are inherently async-capable */
226 }
227
228
229 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
230         char el[256];
231         char *sep = NULL;
232         int i;
233
234         /* Create a version of the element with the namespace removed.
235          * Now we can access "el" or "supplied_el" depending on whether we want to see the whole namespace.
236          */
237         safestrncpy(el, supplied_el, sizeof el);
238         while (sep = strchr(el, ':'), sep) {
239                 strcpy(el, ++sep);
240         }
241
242 #ifdef XMPP_XML_DEBUG
243         syslog(LOG_DEBUG, "xmpp: ELEMENT START: <%s>", el);
244         for (i=0; attr[i] != NULL; i+=2) {
245                 syslog(LOG_DEBUG, "xmpp: Attribute '%s' = '%s'", attr[i], attr[i+1]);
246         }
247 #endif
248
249         if (!strcasecmp(el, "stream")) {
250                 xmpp_stream_start(data, supplied_el, attr);
251         }
252
253         else if (!strcasecmp(el, "query")) {
254                 XMPP->iq_query_xmlns[0] = 0;
255                 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
256         }
257
258         else if (!strcasecmp(el, "bind")) {
259                 XMPP->bind_requested = 1;
260         }
261
262         else if (!strcasecmp(el, "iq")) {
263                 for (i=0; attr[i] != NULL; i+=2) {
264                         if (!strcasecmp(attr[i], "type")) {
265                                 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
266                         }
267                         else if (!strcasecmp(attr[i], "id")) {
268                                 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
269                         }
270                         else if (!strcasecmp(attr[i], "from")) {
271                                 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
272                         }
273                         else if (!strcasecmp(attr[i], "to")) {
274                                 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
275                         }
276                 }
277         }
278
279         else if (!strcasecmp(el, "auth")) {
280                 XMPP->sasl_auth_mech[0] = 0;
281                 for (i=0; attr[i] != NULL; i+=2) {
282                         if (!strcasecmp(attr[i], "mechanism")) {
283                                 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
284                         }
285                 }
286         }
287
288         else if (!strcasecmp(el, "message")) {
289                 for (i=0; attr[i] != NULL; i+=2) {
290                         if (!strcasecmp(attr[i], "to")) {
291                                 safestrncpy(XMPP->message_to, attr[i+1], sizeof XMPP->message_to);
292                         }
293                 }
294         }
295
296         else if (!strcasecmp(el, "html")) {
297                 ++XMPP->html_tag_level;
298         }
299 }
300
301
302 void xmpp_xml_end(void *data, const char *supplied_el) {
303         char el[256];
304         char *sep = NULL;
305         char xmlbuf[256];
306
307         /* Create a version of the element with the namespace removed.
308          * Now we can access "el" or "supplied_el" depending on whether we want to see the whole namespace.
309          */
310         safestrncpy(el, supplied_el, sizeof el);
311         while (sep = strchr(el, ':'), sep) {
312                 strcpy(el, ++sep);
313         }
314
315 #ifdef XMPP_XML_DEBUG
316         syslog(LOG_DEBUG, "xmpp: ELEMENT END  : <%s>", el);
317         if (XMPP->chardata_len > 0) {
318                 syslog(LOG_DEBUG, "xmpp: chardata: %s", XMPP->chardata);
319         }
320 #endif
321
322         if (!strcasecmp(el, "resource")) {
323                 if (XMPP->chardata_len > 0) {
324                         safestrncpy(XMPP->iq_client_resource, XMPP->chardata, sizeof XMPP->iq_client_resource);
325                         striplt(XMPP->iq_client_resource);
326                 }
327         }
328
329         else if (!strcasecmp(el, "username")) {         /* NON SASL ONLY */
330                 if (XMPP->chardata_len > 0) {
331                         safestrncpy(XMPP->iq_client_username, XMPP->chardata, sizeof XMPP->iq_client_username);
332                         striplt(XMPP->iq_client_username);
333                 }
334         }
335
336         else if (!strcasecmp(el, "password")) {         /* NON SASL ONLY */
337                 if (XMPP->chardata_len > 0) {
338                         safestrncpy(XMPP->iq_client_password, XMPP->chardata, sizeof XMPP->iq_client_password);
339                         striplt(XMPP->iq_client_password);
340                 }
341         }
342
343         else if (!strcasecmp(el, "iq")) {
344
345                 /*
346                  * iq type="get" (handle queries)
347                  */
348                 if (!strcasecmp(XMPP->iq_type, "get")) {
349
350                         /*
351                          * Query on a namespace
352                          */
353                         if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
354                                 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from, XMPP->iq_to, XMPP->iq_query_xmlns);
355                         }
356
357                         /*
358                          * ping ( http://xmpp.org/extensions/xep-0199.html )
359                          */
360                         else if (XMPP->ping_requested) {
361                                 cprintf("<iq type=\"result\" ");
362                                 if (!IsEmptyStr(XMPP->iq_from)) {
363                                         cprintf("to=\"%s\" ", xmlesc(xmlbuf, XMPP->iq_from, sizeof xmlbuf));
364                                 }
365                                 if (!IsEmptyStr(XMPP->iq_to)) {
366                                         cprintf("from=\"%s\" ", xmlesc(xmlbuf, XMPP->iq_to, sizeof xmlbuf));
367                                 }
368                                 cprintf("id=\"%s\"/>", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
369                         }
370
371                         /*
372                          * Client is requesting its own vCard
373                          * (If we make this more elaborate, move it to a separate function)
374                          */
375                         else if (XMPP->iq_vcard) {
376                                 cprintf("<iq type=\"result\" id=\"%s\" ", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
377                                 cprintf("to=\"%s\">", xmlesc(xmlbuf, XMPP->iq_from, sizeof xmlbuf));
378                                 cprintf("<vCard xmlns=\"vcard-temp\">");
379                                 cprintf("<fn>%s</fn>", xmlesc(xmlbuf, CC->user.fullname, sizeof xmlbuf));
380                                 cprintf("<nickname>%s</nickname>", xmlesc(xmlbuf, CC->user.fullname, sizeof xmlbuf));
381                                 cprintf("</vCard>");
382                                 cprintf("</iq>");
383                         }
384
385                         /*
386                          * Unknown query ... return the XML equivalent of a blank stare
387                          */
388                         else {
389                                 syslog(LOG_DEBUG, "xmpp: Unknown query <%s> - returning <service-unavailable/>", el);
390                                 cprintf("<iq type=\"error\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
391                                 cprintf("<error code=\"503\" type=\"cancel\">"
392                                         "<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
393                                         "</error>"
394                                 );
395                                 cprintf("</iq>");
396                         }
397                 }
398
399                 /*
400                  * Non SASL authentication
401                  */
402                 else if (
403                         (!strcasecmp(XMPP->iq_type, "set"))
404                         && (!strcasecmp(XMPP->iq_query_xmlns, "jabber:iq:auth:query"))
405                 ) {
406                         xmpp_non_sasl_authenticate(XMPP->iq_id, XMPP->iq_client_username, XMPP->iq_client_password);
407                 }       
408
409                 /*
410                  * If this <iq> stanza was a "bind" attempt, process it ...
411                  */
412                 else if (
413                         (XMPP->bind_requested)
414                         && (!IsEmptyStr(XMPP->iq_id))
415                         && (CC->logged_in)
416                 ) {
417                         /* If the client has not specified a client resource, generate one */
418                         if (IsEmptyStr(XMPP->iq_client_resource)) {
419                                 snprintf(XMPP->iq_client_resource, sizeof XMPP->iq_client_resource, "%d", CC->cs_pid);
420                         }
421
422                         /* Generate the "full JID" of the client (user@host/resource) */
423                         snprintf(XMPP->client_jid, sizeof XMPP->client_jid, "%s/%s", CC->cs_principal_id, XMPP->iq_client_resource);
424
425                         /* Tell the client what its JID is */
426                         cprintf("<iq type=\"result\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
427                         cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
428                         cprintf("<jid>%s</jid>", xmlesc(xmlbuf, XMPP->client_jid, sizeof xmlbuf));
429                         cprintf("</bind>");
430                         cprintf("</iq>");
431                 }
432
433                 else if (XMPP->iq_session) {
434                         cprintf("<iq type=\"result\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
435                         cprintf("</iq>");
436                 }
437
438                 else {
439                         cprintf("<iq type=\"error\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
440                         cprintf("<error>Don't know how to do '%s'!</error>", xmlesc(xmlbuf, XMPP->iq_type, sizeof xmlbuf));
441                         cprintf("</iq>");
442                         syslog(LOG_DEBUG, "XMPP: don't know how to do iq_type='%s' with iq_query_xmlns='%s'", XMPP->iq_type, XMPP->iq_query_xmlns);
443                 }
444
445                 /* Now clear these fields out so they don't get used by a future stanza */
446                 XMPP->iq_id[0] = 0;
447                 XMPP->iq_from[0] = 0;
448                 XMPP->iq_to[0] = 0;
449                 XMPP->iq_type[0] = 0;
450                 XMPP->iq_client_resource[0] = 0;
451                 XMPP->iq_session = 0;
452                 XMPP->iq_vcard = 0;
453                 XMPP->iq_query_xmlns[0] = 0;
454                 XMPP->bind_requested = 0;
455                 XMPP->ping_requested = 0;
456         }
457
458         else if (!strcasecmp(el, "auth")) {
459                 /* Try to authenticate (this function is responsible for the output stanza) */
460                 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
461
462                 /* Now clear these fields out so they don't get used by a future stanza */
463                 XMPP->sasl_auth_mech[0] = 0;
464         }
465
466         else if (!strcasecmp(el, "session")) {
467                 XMPP->iq_session = 1;
468         }
469
470         else if (!strcasecmp(supplied_el, "vcard-temp:vCard")) {
471                 XMPP->iq_vcard = 1;
472         }
473
474         else if (!strcasecmp(el, "presence")) {
475                 /* Respond to a <presence> update by firing back with presence information
476                  * on the entire wholist.  Check this assumption, it's probably wrong.
477                  */
478                 xmpp_wholist_presence_dump();
479         }
480
481         else if ( (!strcasecmp(el, "body")) && (XMPP->html_tag_level == 0) ) {
482                 if (XMPP->message_body != NULL) {
483                         free(XMPP->message_body);
484                         XMPP->message_body = NULL;
485                 }
486                 if (XMPP->chardata_len > 0) {
487                         XMPP->message_body = strdup(XMPP->chardata);
488                 }
489         }
490
491         else if (!strcasecmp(el, "message")) {
492                 xmpp_send_message(XMPP->message_to, XMPP->message_body);
493                 XMPP->html_tag_level = 0;
494         }
495
496         else if (!strcasecmp(el, "html")) {
497                 --XMPP->html_tag_level;
498         }
499
500         else if (!strcasecmp(el, "starttls")) {
501 #ifdef HAVE_OPENSSL
502                 cprintf("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
503                 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
504                 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO;
505 #else
506                 cprintf("<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
507                 CC->kill_me = KILLME_NO_CRYPTO;
508 #endif
509         }
510
511         else if (!strcasecmp(el, "ping")) {
512                 XMPP->ping_requested = 1;
513         }
514
515         else if (!strcasecmp(el, "stream")) {
516                 syslog(LOG_DEBUG, "xmpp: client shut down their stream");
517                 xmpp_massacre_roster();
518                 cprintf("</stream>\n");
519                 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
520         }
521
522         else if (!strcasecmp(el, "query")) {
523                 /* already processed , no further action needed here */
524         }
525
526         else if (!strcasecmp(el, "bind")) {
527                 /* already processed , no further action needed here */
528         }
529
530         else {
531                 syslog(LOG_DEBUG, "xmpp: ignoring unknown tag <%s>", el);
532         }
533
534         XMPP->chardata_len = 0;
535         if (XMPP->chardata_alloc > 0) {
536                 XMPP->chardata[0] = 0;
537         }
538 }
539
540
541 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
542 {
543         citxmpp *X = XMPP;
544
545         if (X->chardata_alloc == 0) {
546                 X->chardata_alloc = SIZ;
547                 X->chardata = malloc(X->chardata_alloc);
548         }
549         if ((X->chardata_len + len + 1) > X->chardata_alloc) {
550                 X->chardata_alloc = X->chardata_len + len + 1024;
551                 X->chardata = realloc(X->chardata, X->chardata_alloc);
552         }
553         memcpy(&X->chardata[X->chardata_len], s, len);
554         X->chardata_len += len;
555         X->chardata[X->chardata_len] = 0;
556 }
557
558
559 /*
560  * This cleanup function blows away the temporary memory and files used by the XMPP service.
561  */
562 void xmpp_cleanup_function(void) {
563
564         /* Don't do this stuff if this is not a XMPP session! */
565         if (CC->h_command_function != xmpp_command_loop) return;
566
567         if (XMPP->chardata != NULL) {
568                 free(XMPP->chardata);
569                 XMPP->chardata = NULL;
570                 XMPP->chardata_len = 0;
571                 XMPP->chardata_alloc = 0;
572                 if (XMPP->message_body != NULL) {
573                         free(XMPP->message_body);
574                 }
575         }
576         XML_ParserFree(XMPP->xp);
577         free(XMPP);
578 }
579
580
581 /*
582  * Here's where our XMPP session begins its happy day.
583  */
584 void xmpp_greeting(void) {
585         client_set_inbound_buf(4);
586         strcpy(CC->cs_clientname, "XMPP session");
587         CC->session_specific_data = malloc(sizeof(citxmpp));
588         memset(XMPP, 0, sizeof(citxmpp));
589         XMPP->last_event_processed = queue_event_seq;
590
591         /* XMPP does not use a greeting, but we still have to initialize some things. */
592
593         XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
594         if (XMPP->xp == NULL) {
595                 syslog(LOG_ERR, "xmpp: cannot create XML parser");
596                 CC->kill_me = KILLME_XML_PARSER;
597                 return;
598         }
599
600         XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
601         XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
602         // XML_SetUserData(XMPP->xp, something...);
603
604         /* Prevent the "billion laughs" attack against expat by disabling
605          * internal entity expansion.  With 2.x, forcibly stop the parser
606          * if an entity is declared - this is safer and a more obvious
607          * failure mode.  With older versions, simply prevent expansion
608          * of such entities. */
609 #ifdef HAVE_XML_STOPPARSER
610         XML_SetEntityDeclHandler(XMPP->xp, xmpp_entity_declaration);
611 #else
612         XML_SetDefaultHandler(XMPP->xp, NULL);
613 #endif
614
615         CC->can_receive_im = 1;         /* This protocol is capable of receiving instant messages */
616 }
617
618
619 /* 
620  * Main command loop for XMPP sessions.
621  */
622 void xmpp_command_loop(void) {
623         int rc;
624         StrBuf *stream_input = NewStrBuf();
625
626         time(&CC->lastcmd);
627         rc = client_read_random_blob(stream_input, 30);
628         if (rc > 0) {
629                 XML_Parse(XMPP->xp, ChrPtr(stream_input), rc, 0);
630         }
631         else {
632                 syslog(LOG_ERR, "xmpp: client disconnected: ending session.");
633                 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
634         }
635         FreeStrBuf(&stream_input);
636 }
637
638
639 /*
640  * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
641  */
642 void xmpp_async_loop(void) {
643         xmpp_process_events();
644         xmpp_output_incoming_messages();
645 }
646
647
648 /*
649  * Login hook for XMPP sessions
650  */
651 void xmpp_login_hook(void) {
652         xmpp_queue_event(XMPP_EVT_LOGIN, CC->cs_principal_id);
653 }
654
655
656 /*
657  * Logout hook for XMPP sessions
658  */
659 void xmpp_logout_hook(void) {
660         xmpp_queue_event(XMPP_EVT_LOGOUT, CC->cs_principal_id);
661 }
662
663
664 const char *CitadelServiceXMPP="XMPP";
665 CTDL_MODULE_INIT(xmpp)
666 {
667         if (!threading) {
668                 CtdlRegisterServiceHook(CtdlGetConfigInt("c_xmpp_c2s_port"),
669                                         NULL,
670                                         xmpp_greeting,
671                                         xmpp_command_loop,
672                                         xmpp_async_loop,
673                                         CitadelServiceXMPP
674                 );
675                 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP, PRIO_STOP + 70);
676                 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGIN, PRIO_LOGIN + 90);
677                 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 90);
678                 CtdlRegisterSessionHook(xmpp_login_hook, EVT_UNSTEALTH, PRIO_UNSTEALTH + 1);
679                 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_STEALTH, PRIO_STEALTH + 1);
680
681         }
682
683         /* return our module name for the log */
684         return "xmpp";
685 }