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