Initial code to output any instant messages which
authorArt Cancro <ajc@citadel.org>
Thu, 29 Nov 2007 23:00:44 +0000 (23:00 +0000)
committerArt Cancro <ajc@citadel.org>
Thu, 29 Nov 2007 23:00:44 +0000 (23:00 +0000)
have arrived for the user.  The Citadel server's built-in
infrastructure for asynchronous (unsolicited) protocol messages
made this really easy.

citadel/modules/jabber/serv_xmpp.c
citadel/modules/jabber/serv_xmpp.h
citadel/modules/jabber/xmpp_messages.c [new file with mode: 0644]

index ecf73671c4cd4cecaafec9644b1dcd717cd0a4ce..31c0142bc98a6da8bc140434bcd43e15c5981135 100644 (file)
@@ -89,6 +89,8 @@ void xmpp_stream_start(void *data, const char *supplied_el, const char **attr)
        }
 
        cprintf("</stream:features>");
+
+       CC->is_async = 1;               /* XMPP sessions are inherently async-capable */
 }
 
 
@@ -354,6 +356,15 @@ void xmpp_command_loop(void) {
        XML_Parse(XMPP->xp, cmdbuf, 1, 0);
 }
 
+
+/*
+ * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
+ */
+void xmpp_async_loop(void) {
+       jabber_output_incoming_messages();
+}
+
+
 const char *CitadelServiceXMPP="XMPP";
 
 #endif /* HAVE_EXPAT */
@@ -362,12 +373,11 @@ CTDL_MODULE_INIT(jabber)
 {
 #ifdef HAVE_EXPAT
        if (!threading) {
-               /* CtdlRegisterServiceHook(config.c_xmpp_port,          FIXME   */
-               CtdlRegisterServiceHook(5222,
+               CtdlRegisterServiceHook(5222,                   /* FIXME change to config.c_xmpp_port */
                                        NULL,
                                        xmpp_greeting,
                                        xmpp_command_loop,
-                                       NULL,
+                                       xmpp_async_loop,
                                        CitadelServiceXMPP);
                CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
        #else
index e45a744bcc24f197370f0a38487e53a6f80253fc..f6f3438a4701fd3b94cf20a445b18a2445924297 100644 (file)
@@ -31,3 +31,4 @@ void xmpp_sasl_auth(char *, char *);
 void xmpp_output_auth_mechs(void);
 void xmpp_query_namespace(char *, char *, char *, char *);
 void jabber_wholist_presence_dump(void);
+void jabber_output_incoming_messages(void);
diff --git a/citadel/modules/jabber/xmpp_messages.c b/citadel/modules/jabber/xmpp_messages.c
new file mode 100644 (file)
index 0000000..046f758
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * $Id$ 
+ *
+ * Handle messages sent and received using XMPP (Jabber) protocol
+ *
+ * Copyright (c) 2007 by Art Cancro
+ * This code is released under the terms of the GNU General Public License.
+ *
+ */
+
+#include "sysdep.h"
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <pwd.h>
+#include <errno.h>
+#include <sys/types.h>
+
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+
+#include <sys/wait.h>
+#include <string.h>
+#include <limits.h>
+#include <ctype.h>
+#include <libcitadel.h>
+#include "citadel.h"
+#include "server.h"
+#include "citserver.h"
+#include "support.h"
+#include "config.h"
+#include "internet_addressing.h"
+#include "md5.h"
+#include "ctdl_module.h"
+
+#ifdef HAVE_EXPAT
+#include <expat.h>
+#include "serv_xmpp.h"
+
+
+/*
+ * This function is called by the XMPP service's async loop.
+ * If the client session has instant messages waiting, it outputs
+ * unsolicited XML stanzas containing them.
+ */
+void jabber_output_incoming_messages(void) {
+
+       struct ExpressMessage *ptr;
+
+       while (CC->FirstExpressMessage != NULL) {
+
+               begin_critical_section(S_SESSION_TABLE);
+               ptr = CC->FirstExpressMessage;
+               CC->FirstExpressMessage = CC->FirstExpressMessage->next;
+               end_critical_section(S_SESSION_TABLE);
+
+               /* FIXME we have to get the sender's email address.  This may involve tweaking
+                * the core IM module a bit.
+                */
+               cprintf("<message to=\"%s\" from=\"FIXME@example.org\" type=\"chat\">", XMPP->client_jid);
+               if (ptr->text != NULL) {
+                       cprintf("<body>");
+                       memfmout(ptr->text, 0, "\n");
+                       if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
+                       free(ptr->text);
+                       cprintf("</body>");
+               }
+               cprintf("</message>");
+               free(ptr);
+       }
+}
+
+
+#endif /* HAVE_EXPAT */