* Eliminate generic_free_handler() since it is not needed
[citadel.git] / citadel / modules / xmpp / xmpp_presence.c
index 9eaf88728e7eae30ed3dc4c67281d8da8fe68733..ca33d1e57336aab409cb401ab7778b2cc74f4bdd 100644 (file)
@@ -5,19 +5,19 @@
  *
  * Copyright (c) 2007-2010 by Art Cancro
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 3 of the License, or
- *  (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
  *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */
 
 #include "serv_xmpp.h"
 
 
+
+/* 
+ * Indicate the presence of another user to the client
+ * (used in several places)
+ */
+void xmpp_indicate_presence(char *presence_jid)
+{
+       cprintf("<presence from=\"%s\" to=\"%s\"></presence>",
+               presence_jid,
+               XMPP->client_jid
+       );
+}
+
+
 /* 
  * Initial dump of the entire wholist
  */
@@ -67,7 +81,7 @@ void xmpp_wholist_presence_dump(void)
        struct CitContext *cptr = NULL;
        int nContexts, i;
        
-       int aide = (CC->user.axlevel >= 6);
+       int aide = (CC->user.axlevel >= AxAideU);
 
        cptr = CtdlGetContextArray(&nContexts);
        if (!cptr) {
@@ -81,8 +95,7 @@ void xmpp_wholist_presence_dump(void)
                                && (cptr[i].user.usernum != CC->user.usernum)   /* don't show myself */
                                && (cptr[i].can_receive_im)                     /* IM-capable session */
                        ) {
-                               cprintf("<presence type=\"available\" from=\"%s\"></presence>",
-                                       cptr[i].cs_inet_email);
+                               xmpp_indicate_presence(cptr[i].cs_inet_email);
                        }
                }
        }
@@ -98,11 +111,20 @@ void xmpp_destroy_buddy(char *presence_jid) {
        static int unsolicited_id = 1;
 
        /* Transmit non-presence information */
-       cprintf("<presence type=\"unavailable\" from=\"%s\"></presence>", presence_jid);
-       cprintf("<presence type=\"unsubscribed\" from=\"%s\"></presence>", presence_jid);
+       cprintf("<presence type=\"unavailable\" from=\"%s\" to=\"%s\"></presence>",
+               presence_jid, XMPP->client_jid
+       );
+       cprintf("<presence type=\"unsubscribed\" from=\"%s\" to=\"%s\"></presence>",
+               presence_jid, XMPP->client_jid
+       );
+       // FIXME ... we should implement xmpp_indicate_nonpresence so we can use it elsewhere
 
        /* Do an unsolicited roster update that deletes the contact. */
-       cprintf("<iq id=\"unbuddy_%x\" type=\"result\">", ++unsolicited_id);
+       cprintf("<iq from=\"%s\" to=\"%s\" id=\"unbuddy_%x\" type=\"result\">",
+               CC->cs_inet_email,
+               XMPP->client_jid,
+               ++unsolicited_id
+       );
        cprintf("<query xmlns=\"jabber:iq:roster\">");
        cprintf("<item jid=\"%s\" subscription=\"remove\">", presence_jid);
        cprintf("<group>%s</group>", config.c_humannode);
@@ -121,7 +143,7 @@ void xmpp_presence_notify(char *presence_jid, int event_type) {
        static int unsolicited_id;
        int visible_sessions = 0;
        int nContexts, i;
-       int aide = (CC->user.axlevel >= 6);
+       int aide = (CC->user.axlevel >= AxAideU);
 
        if (IsEmptyStr(presence_jid)) return;
        if (CC->kill_me) return;
@@ -166,7 +188,7 @@ void xmpp_presence_notify(char *presence_jid, int event_type) {
                }
 
                /* Transmit presence information */
-               cprintf("<presence type=\"available\" from=\"%s\"></presence>", presence_jid);
+               xmpp_indicate_presence(presence_jid);
        }
 
        if (visible_sessions == 0) {
@@ -179,15 +201,110 @@ void xmpp_presence_notify(char *presence_jid, int event_type) {
 
 
 
+void xmpp_fetch_mortuary_backend(long msgnum, void *userdata) {
+       HashList *mortuary = (HashList *) userdata;
+       struct CtdlMessage *msg;
+       const char *ptr = NULL;
+       const char *endptr = NULL;
+       int in_body = 0;
+       int len;
+       char buf[256];
+
+       msg = CtdlFetchMessage(msgnum, 1);
+       if (msg == NULL) {
+               return;
+       }
+
+       /* now add anyone we find into the hashlist */
+
+       ptr = msg->cm_fields['M'];
+       endptr = ptr + strlen(ptr);     // only do strlen once :)
+       CtdlLogPrintf(CTDL_DEBUG, "\033[33m%s\033[0m\n", ptr);
+       while (ptr = memreadlinelen(ptr, buf, (sizeof buf - 2), &len), ((ptr < endptr) && (*ptr != 0)) ) {
+               if (in_body) {
+                       char *pch; 
+                       pch = malloc(len + 1);
+                       memcpy(pch, buf, len + 1);
+                       Put(mortuary, pch, len, pch, NULL);
+               }
+               if (IsEmptyStr(buf)) in_body = 1;
+       }
+
+       CtdlFreeMessage(msg);
+}
+
+
+
+/*
+ * Fetch the "mortuary" - a list of dead buddies which we keep around forever
+ * so we can remove them from any client's roster that still has them listed
+ */
+HashList *xmpp_fetch_mortuary(void) {
+       HashList *mortuary = NewHash(1, NULL);
+       if (!mortuary) {
+               CtdlLogPrintf(CTDL_ALERT, "NewHash() failed!\n");
+               return(NULL);
+       }
+
+        if (CtdlGetRoom(&CC->room, USERCONFIGROOM) != 0) {
+               /* no config room exists - no further processing is required. */
+                return(mortuary);
+        }
+        CtdlForEachMessage(MSGS_LAST, 1, NULL, XMPPMORTUARY, NULL,
+                xmpp_fetch_mortuary_backend, (void *)mortuary );
+
+       return(mortuary);
+}
+
+
+
+/*
+ * Fetch the "mortuary" - a list of dead buddies which we keep around forever
+ * so we can remove them from any client's roster that still has them listed
+ */
+void xmpp_store_mortuary(HashList *mortuary) {
+       HashPos *HashPos;
+       long len;
+       void *Value;
+       const char *Key;
+       StrBuf *themsg;
+
+       themsg = NewStrBuf();
+       StrBufPrintf(themsg,    "Content-type: " XMPPMORTUARY "\n"
+                               "Content-transfer-encoding: 7bit\n"
+                               "\n"
+       );
+
+       HashPos = GetNewHashPos(mortuary, 0);
+       while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
+       {
+               StrBufAppendPrintf(themsg, "%s\n", (char *)Value);
+       }
+       DeleteHashPos(&HashPos);
+
+       /* Delete the old mortuary */
+       CtdlDeleteMessages(USERCONFIGROOM, NULL, 0, XMPPMORTUARY);
+
+       /* And save the new one to disk */
+       quickie_message("Citadel", NULL, NULL, USERCONFIGROOM, ChrPtr(themsg), 4, "XMPP Mortuary");
+       FreeStrBuf(&themsg);
+}
+
+
+
 /*
  * Upon logout we make an attempt to delete the whole roster, in order to
  * try to keep "ghost" buddies from remaining in the client-side roster.
+ *
+ * Since the client is probably not still alive, also remember the current
+ * roster for next time so we can delete dead buddies then.
  */
 void xmpp_massacre_roster(void)
 {
        struct CitContext *cptr;
        int nContexts, i;
-       int aide = (CC->user.axlevel >= 6);
+       int aide = (CC->user.axlevel >= AxAideU);
+       HashList *mortuary = xmpp_fetch_mortuary();
 
        cptr = CtdlGetContextArray(&nContexts);
        if (cptr) {
@@ -198,12 +315,43 @@ void xmpp_massacre_roster(void)
                                        && (cptr[i].user.usernum != CC->user.usernum)
                                ) {
                                        xmpp_destroy_buddy(cptr[i].cs_inet_email);
+                                       if (mortuary) {
+                                               char *buddy = strdup(cptr[i].cs_inet_email);
+                                               Put(mortuary, buddy, strlen(buddy), buddy, NULL);
+                                       }
                                }
                        }
                }
                free (cptr);
        }
+
+       if (mortuary) {
+               xmpp_store_mortuary(mortuary);
+               DeleteHash(&mortuary);
+       }
 }
 
 
 
+/*
+ * Stupidly, XMPP does not specify a way to tell the client to flush its client-side roster
+ * and prepare to receive a new one.  So instead we remember every buddy we've ever told the
+ * client about, and push delete operations out at the beginning of a session.
+ */
+void xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster(void)
+{
+       long len;
+       void *Value;
+       const char *Key;
+       HashList *mortuary = xmpp_fetch_mortuary();
+       HashPos *HashPos = GetNewHashPos(mortuary, 0);
+
+       CtdlLogPrintf(CTDL_DEBUG, "\033[32mHAHA\033[0m\n");
+       while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
+       {
+               CtdlLogPrintf(CTDL_DEBUG, "\033[31mDELETE: %s\033[0m\n", (char *)Value);
+       }
+       CtdlLogPrintf(CTDL_DEBUG, "\033[32mHOHO\033[0m\n");
+       DeleteHashPos(&HashPos);
+       DeleteHash(&mortuary);
+}