]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/xmpp/serv_xmpp.c
* XMPP login hooks which output protocol data should only activate if this is actuall...
[citadel.git] / citadel / modules / xmpp / serv_xmpp.c
index 0b7da46e9cf755380a2e2acb456fb9f0db4e77bd..067faa0a1997ba915c5b5c6440d3f40c127cfb39 100644 (file)
@@ -52,7 +52,6 @@
 #include "support.h"
 #include "config.h"
 #include "user_ops.h"
-#include "policy.h"
 #include "database.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
@@ -207,7 +206,7 @@ void xmpp_xml_end(void *data, const char *supplied_el) {
                }
        }
 
-       if (!strcasecmp(el, "username")) {              /* NON SASL ONLY */
+       else if (!strcasecmp(el, "username")) {         /* NON SASL ONLY */
                if (XMPP->chardata_len > 0) {
                        safestrncpy(XMPP->iq_client_username, XMPP->chardata,
                                sizeof XMPP->iq_client_username);
@@ -215,7 +214,7 @@ void xmpp_xml_end(void *data, const char *supplied_el) {
                }
        }
 
-       if (!strcasecmp(el, "password")) {              /* NON SASL ONLY */
+       else if (!strcasecmp(el, "password")) {         /* NON SASL ONLY */
                if (XMPP->chardata_len > 0) {
                        safestrncpy(XMPP->iq_client_password, XMPP->chardata,
                                sizeof XMPP->iq_client_password);
@@ -253,10 +252,13 @@ void xmpp_xml_end(void *data, const char *supplied_el) {
                        }
 
                        /*
-                        * Unknown queries ... return the XML equivalent of a blank stare
+                        * Unknown query ... return the XML equivalent of a blank stare
                         */
                        else {
-                               CtdlLogPrintf(CTDL_DEBUG, "Unknown query; <service-unavailable/>\n");
+                               CtdlLogPrintf(CTDL_DEBUG,
+                                       "Unknown query <%s> - returning <service-unavailable/>\n",
+                                       el
+                               );
                                cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
                                cprintf("<error code=\"503\" type=\"cancel\">"
                                        "<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
@@ -316,7 +318,7 @@ void xmpp_xml_end(void *data, const char *supplied_el) {
 
                else {
                        cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
-                       cprintf("<error></error>");
+                       cprintf("<error>Don't know howto do '%s'!</error>", XMPP->iq_type);
                        cprintf("</iq>");
                }
 
@@ -374,12 +376,12 @@ void xmpp_xml_end(void *data, const char *supplied_el) {
 
        else if (!strcasecmp(el, "starttls")) {
 #ifdef HAVE_OPENSSL
-       cprintf("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
-       CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
-       if (!CC->redirect_ssl) CC->kill_me = 1;
+               cprintf("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
+               CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
+               if (!CC->redirect_ssl) CC->kill_me = 1;
 #else
-       cprintf("<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
-       CC->kill_me = 1;
+               cprintf("<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
+               CC->kill_me = 1;
 #endif
        }
 
@@ -387,6 +389,17 @@ void xmpp_xml_end(void *data, const char *supplied_el) {
                XMPP->ping_requested = 1;
        }
 
+       else if (!strcasecmp(el, "stream")) {
+               CtdlLogPrintf(CTDL_DEBUG, "XMPP client shut down their stream\n");
+               xmpp_massacre_roster();
+               cprintf("</stream>\n");
+               CC->kill_me = 1;
+       }
+
+       else {
+               CtdlLogPrintf(CTDL_DEBUG, "Ignoring unknown tag <%s>\n", el);
+       }
+
        XMPP->chardata_len = 0;
        if (XMPP->chardata_alloc > 0) {
                XMPP->chardata[0] = 0;
@@ -396,7 +409,7 @@ void xmpp_xml_end(void *data, const char *supplied_el) {
 
 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
 {
-       struct citxmpp *X = XMPP;
+       citxmpp *X = XMPP;
 
        if (X->chardata_alloc == 0) {
                X->chardata_alloc = SIZ;
@@ -440,8 +453,8 @@ void xmpp_cleanup_function(void) {
  */
 void xmpp_greeting(void) {
        strcpy(CC->cs_clientname, "XMPP session");
-       CC->session_specific_data = malloc(sizeof(struct citxmpp));
-       memset(XMPP, 0, sizeof(struct citxmpp));
+       CC->session_specific_data = malloc(sizeof(citxmpp));
+       memset(XMPP, 0, sizeof(citxmpp));
        XMPP->last_event_processed = queue_event_seq;
 
        /* XMPP does not use a greeting, but we still have to initialize some things. */
@@ -463,22 +476,22 @@ void xmpp_greeting(void) {
 
 /* 
  * Main command loop for XMPP sessions.
+ * Right now we're reading one byte at a time and sending it to the XML parser.
+ * There's got to be a better way to do this.
  */
 void xmpp_command_loop(void) {
-       char cmdbuf[16];
+       char cmdbuf[2];
        int retval;
 
        time(&CC->lastcmd);
-       memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
+       memset(cmdbuf, 0, sizeof cmdbuf);
        retval = client_read(cmdbuf, 1);
        if (retval != 1) {
-               CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
+               CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\n");
                CC->kill_me = 1;
                return;
        }
 
-       /* FIXME ... this is woefully inefficient. */
-
        XML_Parse(XMPP->xp, cmdbuf, 1, 0);
 }
 
@@ -497,6 +510,11 @@ void xmpp_async_loop(void) {
  */
 void xmpp_login_hook(void) {
        xmpp_queue_event(XMPP_EVT_LOGIN, CC->cs_inet_email);
+
+       /* only do the protocol stuff if this is an XMPP session */
+       if (CC->h_command_function == xmpp_command_loop) {
+               xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster();
+       }
 }
 
 
@@ -518,7 +536,8 @@ CTDL_MODULE_INIT(xmpp)
                                        xmpp_greeting,
                                        xmpp_command_loop,
                                        xmpp_async_loop,
-                                       CitadelServiceXMPP);
+                                       CitadelServiceXMPP
+               );
                CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
                 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGIN);
                 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_LOGOUT);