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