Initial code to output any instant messages which
[citadel.git] / citadel / modules / jabber / serv_xmpp.c
1 /*
2  * $Id$ 
3  *
4  * XMPP (Jabber) service for the Citadel system
5  * Copyright (c) 2007 by Art Cancro
6  * This code is released under the terms of the GNU General Public License.
7  *
8  */
9
10 #include "sysdep.h"
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <pwd.h>
17 #include <errno.h>
18 #include <sys/types.h>
19
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
22 # include <time.h>
23 #else
24 # if HAVE_SYS_TIME_H
25 #  include <sys/time.h>
26 # else
27 #  include <time.h>
28 # endif
29 #endif
30
31 #include <sys/wait.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <ctype.h>
35 #include <libcitadel.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "citserver.h"
39 #include "support.h"
40 #include "config.h"
41 #include "room_ops.h"
42 #include "user_ops.h"
43 #include "policy.h"
44 #include "database.h"
45 #include "msgbase.h"
46 #include "internet_addressing.h"
47 #include "md5.h"
48 #include "ctdl_module.h"
49
50 #ifdef HAVE_EXPAT
51 #include <expat.h>
52 #include "serv_xmpp.h"
53
54
55 /* We have just received a <stream> tag from the client, so send them ours */
56
57 void xmpp_stream_start(void *data, const char *supplied_el, const char **attr)
58 {
59
60         lprintf(CTDL_DEBUG, "New stream detected.\n");
61
62         while (*attr) {
63                 if (!strcasecmp(attr[0], "to")) {
64                         safestrncpy(XMPP->server_name, attr[1], sizeof XMPP->server_name);
65                 }
66                 attr += 2;
67         }
68
69         cprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
70
71         cprintf("<stream:stream ");
72         cprintf("from=\"%s\" ", XMPP->server_name);
73         cprintf("id=\"%08x\" ", CC->cs_pid);
74         cprintf("version=\"1.0\" ");
75         cprintf("xmlns:stream=\"http://etherx.jabber.org/streams\" ");
76         cprintf("xmlns=\"jabber:client\">");
77
78         /* The features of this stream are... */
79         cprintf("<stream:features>");
80
81         if (!CC->logged_in) {
82                 /* If we're not logged in yet, offer SASL as our feature set */
83                 xmpp_output_auth_mechs();
84         }
85         else {
86                 /* If we've logged in, now offer binding and sessions as our feature set */
87                 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
88                 cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
89         }
90
91         cprintf("</stream:features>");
92
93         CC->is_async = 1;               /* XMPP sessions are inherently async-capable */
94 }
95
96
97 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
98         char el[256];
99         char *sep = NULL;
100         int i;
101
102         /* Axe the namespace, we don't care about it */
103         safestrncpy(el, supplied_el, sizeof el);
104         while (sep = strchr(el, ':'), sep) {
105                 strcpy(el, ++sep);
106         }
107
108         lprintf(CTDL_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
109
110         for (i=0; attr[i] != NULL; i+=2) {
111                 lprintf(CTDL_DEBUG, "                    Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
112         }
113
114         if (!strcasecmp(el, "stream")) {
115                 xmpp_stream_start(data, supplied_el, attr);
116         }
117
118         else if (!strcasecmp(el, "query")) {
119                 XMPP->iq_query_xmlns[0] = 0;
120                 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
121         }
122
123         else if (!strcasecmp(el, "iq")) {
124                 for (i=0; attr[i] != NULL; i+=2) {
125                         if (!strcasecmp(attr[i], "type")) {
126                                 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
127                         }
128                         else if (!strcasecmp(attr[i], "id")) {
129                                 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
130                         }
131                         else if (!strcasecmp(attr[i], "from")) {
132                                 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
133                         }
134                         else if (!strcasecmp(attr[i], "to")) {
135                                 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
136                         }
137                 }
138         }
139
140         else if (!strcasecmp(el, "auth")) {
141                 XMPP->sasl_auth_mech[0] = 0;
142                 for (i=0; attr[i] != NULL; i+=2) {
143                         if (!strcasecmp(attr[i], "mechanism")) {
144                                 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
145                         }
146                 }
147         }
148 }
149
150
151
152 void xmpp_xml_end(void *data, const char *supplied_el) {
153         char el[256];
154         char *sep = NULL;
155
156         /* Axe the namespace, we don't care about it */
157         safestrncpy(el, supplied_el, sizeof el);
158         while (sep = strchr(el, ':'), sep) {
159                 strcpy(el, ++sep);
160         }
161
162         lprintf(CTDL_DEBUG, "XMPP ELEMENT END  : <%s>\n", el);
163         if (XMPP->chardata_len > 0) {
164                 lprintf(CTDL_DEBUG, "          chardata: %s\n", XMPP->chardata);
165         }
166
167         if (!strcasecmp(el, "resource")) {
168                 if (XMPP->chardata_len > 0) {
169                         safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
170                                 sizeof XMPP->iq_client_resource);
171                 }
172         }
173
174         else if (!strcasecmp(el, "iq")) {
175
176                 /*
177                  * iq type="get" (handle queries)
178                  */
179                 if (!strcasecmp(XMPP->iq_type, "get")) {
180
181                         /*
182                          * Query on a namespace
183                          */
184                         if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
185                                 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
186                                                 XMPP->iq_to, XMPP->iq_query_xmlns);
187                         }
188
189                         /*
190                          * Unknown queries ... return the XML equivalent of a blank stare
191                          */
192                         else {
193                                 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
194                                 cprintf("</iq>");
195                         }
196                 }
197
198                 /*
199                  * If this <iq> stanza was a "bind" attempt, process it ...
200                  */
201                 else if ( (!IsEmptyStr(XMPP->iq_id)) && (!IsEmptyStr(XMPP->iq_client_resource)) ) {
202
203                         /* Generate the "full JID" of the client resource */
204
205                         // snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
206                         //      "%d@%s/%s",
207                         //      CC->cs_pid,
208                         //      config.c_fqdn,
209                         //      XMPP->iq_client_resource
210                         //);
211
212                         snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
213                                 "%s/%s",
214                                 CC->cs_inet_email,
215                                 XMPP->iq_client_resource
216                         );
217
218                         /* Tell the client what its JID is */
219
220                         cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
221                         cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
222                         cprintf("<jid>%s</jid>", XMPP->client_jid);
223                         cprintf("</bind>");
224                         cprintf("</iq>");
225                 }
226
227                 else if (XMPP->iq_session) {
228                         cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
229                         cprintf("</iq>");
230                 }
231
232                 else {
233                         cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
234                         cprintf("<error></error>");
235                         cprintf("</iq>");
236                 }
237
238                 /* Now clear these fields out so they don't get used by a future stanza */
239                 XMPP->iq_id[0] = 0;
240                 XMPP->iq_from[0] = 0;
241                 XMPP->iq_to[0] = 0;
242                 XMPP->iq_type[0] = 0;
243                 XMPP->iq_client_resource[0] = 0;
244                 XMPP->iq_session = 0;
245                 XMPP->iq_query_xmlns[0] = 0;
246         }
247
248         else if (!strcasecmp(el, "auth")) {
249
250                 /* Try to authenticate (this function is responsible for the output stanza) */
251                 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
252
253                 /* Now clear these fields out so they don't get used by a future stanza */
254                 XMPP->sasl_auth_mech[0] = 0;
255         }
256
257         else if (!strcasecmp(el, "session")) {
258                 XMPP->iq_session = 1;
259         }
260
261         else if (!strcasecmp(el, "presence")) {
262
263                 /* Respond to a <presence> update by firing back with presence information
264                  * on the entire wholist.  Check this assumption, it's probably wrong.
265                  */
266                 jabber_wholist_presence_dump();
267         }
268
269         XMPP->chardata_len = 0;
270         if (XMPP->chardata_alloc > 0) {
271                 XMPP->chardata[0] = 0;
272         }
273 }
274
275
276 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
277 {
278         struct citxmpp *X = XMPP;
279
280         if (X->chardata_alloc == 0) {
281                 X->chardata_alloc = SIZ;
282                 X->chardata = malloc(X->chardata_alloc);
283         }
284         if ((X->chardata_len + len + 1) > X->chardata_alloc) {
285                 X->chardata_alloc = X->chardata_len + len + 1024;
286                 X->chardata = realloc(X->chardata, X->chardata_alloc);
287         }
288         memcpy(&X->chardata[X->chardata_len], s, len);
289         X->chardata_len += len;
290         X->chardata[X->chardata_len] = 0;
291 }
292
293
294 /*
295  * This cleanup function blows away the temporary memory and files used by the XMPP service.
296  */
297 void xmpp_cleanup_function(void) {
298
299         /* Don't do this stuff if this is not a XMPP session! */
300         if (CC->h_command_function != xmpp_command_loop) return;
301
302         lprintf(CTDL_DEBUG, "Performing XMPP cleanup hook\n");
303         if (XMPP->chardata != NULL) {
304                 free(XMPP->chardata);
305                 XMPP->chardata = NULL;
306                 XMPP->chardata_len = 0;
307                 XMPP->chardata_alloc = 0;
308         }
309         XML_ParserFree(XMPP->xp);
310         free(XMPP);
311 }
312
313
314
315 /*
316  * Here's where our XMPP session begins its happy day.
317  */
318 void xmpp_greeting(void) {
319         strcpy(CC->cs_clientname, "Jabber session");
320         CC->session_specific_data = malloc(sizeof(struct citxmpp));
321         memset(XMPP, 0, sizeof(struct citxmpp));
322
323         /* XMPP does not use a greeting, but we still have to initialize some things. */
324
325         XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
326         if (XMPP->xp == NULL) {
327                 lprintf(CTDL_ALERT, "Cannot create XML parser!\n");
328                 CC->kill_me = 1;
329                 return;
330         }
331
332         XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
333         XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
334         // XML_SetUserData(XMPP->xp, something...);
335 }
336
337
338 /* 
339  * Main command loop for XMPP sessions.
340  */
341 void xmpp_command_loop(void) {
342         char cmdbuf[16];
343         int retval;
344
345         time(&CC->lastcmd);
346         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
347         retval = client_read(cmdbuf, 1);
348         if (retval != 1) {
349                 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
350                 CC->kill_me = 1;
351                 return;
352         }
353
354         /* FIXME ... this is woefully inefficient. */
355
356         XML_Parse(XMPP->xp, cmdbuf, 1, 0);
357 }
358
359
360 /*
361  * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
362  */
363 void xmpp_async_loop(void) {
364         jabber_output_incoming_messages();
365 }
366
367
368 const char *CitadelServiceXMPP="XMPP";
369
370 #endif  /* HAVE_EXPAT */
371
372 CTDL_MODULE_INIT(jabber)
373 {
374 #ifdef HAVE_EXPAT
375         if (!threading) {
376                 CtdlRegisterServiceHook(5222,                   /* FIXME change to config.c_xmpp_port */
377                                         NULL,
378                                         xmpp_greeting,
379                                         xmpp_command_loop,
380                                         xmpp_async_loop,
381                                         CitadelServiceXMPP);
382                 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
383         #else
384                 lprintf(CTDL_INFO, "This server is missing the Expat XML parser.  Jabber service will be disabled.\n");
385 #endif
386         }
387
388         /* return our Subversion id for the Log */
389         return "$Id$";
390 }