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