]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/xmpp/xmpp_presence.c
* Lots of changes to the XMPP Mortuary to make it keep track of useful data. We...
[citadel.git] / citadel / modules / xmpp / xmpp_presence.c
index 3df2217c3b78e996da2240ff27ebd87dae88b24c..49836f388b38a674b597b83baf5d8945d72ec542 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
  *
  */
 
@@ -200,6 +200,49 @@ 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;
+       char *ptr = NULL;
+       char *lasts = NULL;
+
+       msg = CtdlFetchMessage(msgnum, 1);
+       if (msg == NULL) {
+               return;
+       }
+
+       /* now add anyone we find into the hashlist */
+
+       /* skip past the headers */
+       ptr = strstr(msg->cm_fields['M'], "\n\n");
+       if (ptr != NULL) {
+               ptr += 2;
+       }
+       else {
+               ptr = strstr(msg->cm_fields['M'], "\n\r\n");
+               if (ptr != NULL) {
+                       ptr += 3;
+               }
+       }
+
+       /* the remaining lines are addresses */
+       if (ptr != NULL) {
+               ptr = strtok_r(ptr, "\n", &lasts);
+               while (ptr != NULL) {
+                       char *pch = strdup(ptr);
+                       Put(mortuary, pch, strlen(pch), pch, NULL);
+                       ptr = strtok_r(NULL, "\n", &lasts);
+               }
+       }
+
+       TRACE;
+       CtdlFreeMessage(msg);
+       TRACE;
+}
+
+
+
 /*
  * 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
@@ -211,7 +254,12 @@ HashList *xmpp_fetch_mortuary(void) {
                return(NULL);
        }
 
-       /* FIXME finish this */
+        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);
 }
@@ -239,16 +287,14 @@ void xmpp_store_mortuary(HashList *mortuary) {
        while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
        {
                StrBufAppendPrintf(themsg, "%s\n", (char *)Value);
-               /* note: don't free(Value) -- deleting the hash list will handle this for us */
        }
        DeleteHashPos(&HashPos);
 
-       /* Save it to disk */
-       quickie_message("Citadel", NULL, NULL, USERCONFIGROOM, ChrPtr(themsg), 4, "XMPP Mortuary");
-
-       /* Delete the old one */
+       /* 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);
 }
 
@@ -276,11 +322,9 @@ void xmpp_massacre_roster(void)
                                        (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
                                        && (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, generic_free_handler);
+                                               Put(mortuary, buddy, strlen(buddy), buddy, NULL);
                                        }
                                }
                        }
@@ -296,3 +340,26 @@ void xmpp_massacre_roster(void)
 
 
 
+/*
+ * 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);
+
+       /* FIXME delete from the list anyone who is currently online */
+
+       while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
+       {
+               CtdlLogPrintf(CTDL_DEBUG, "\033[31mDELETE: %s\033[0m\n", (char *)Value);
+               // FIXME xmpp_destroy_buddy((char *)Value);
+       }
+       DeleteHashPos(&HashPos);
+       DeleteHash(&mortuary);
+}