Back to index

citadel  8.12
xmpp_presence.c
Go to the documentation of this file.
00001 /*
00002  * Handle XMPP presence exchanges
00003  *
00004  * Copyright (c) 2007-2010 by Art Cancro
00005  *
00006  * This program is open source software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #include "sysdep.h"
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 #include <stdio.h>
00026 #include <fcntl.h>
00027 #include <signal.h>
00028 #include <pwd.h>
00029 #include <errno.h>
00030 #include <sys/types.h>
00031 #include <assert.h>
00032 
00033 #if TIME_WITH_SYS_TIME
00034 # include <sys/time.h>
00035 # include <time.h>
00036 #else
00037 # if HAVE_SYS_TIME_H
00038 #  include <sys/time.h>
00039 # else
00040 #  include <time.h>
00041 # endif
00042 #endif
00043 
00044 #include <sys/wait.h>
00045 #include <string.h>
00046 #include <limits.h>
00047 #include <ctype.h>
00048 #include <expat.h>
00049 #include <libcitadel.h>
00050 #include "citadel.h"
00051 #include "server.h"
00052 #include "citserver.h"
00053 #include "support.h"
00054 #include "config.h"
00055 #include "internet_addressing.h"
00056 #include "md5.h"
00057 #include "ctdl_module.h"
00058 #include "serv_xmpp.h"
00059 
00060 
00061 
00062 /* 
00063  * Indicate the presence of another user to the client
00064  * (used in several places)
00065  */
00066 void xmpp_indicate_presence(char *presence_jid)
00067 {
00068        char xmlbuf[256];
00069 
00070        cprintf("<presence from=\"%s\" ", xmlesc(xmlbuf, presence_jid, sizeof xmlbuf));
00071        cprintf("to=\"%s\"></presence>", xmlesc(xmlbuf, XMPP->client_jid, sizeof xmlbuf));
00072 }
00073 
00074 
00075 
00076 /*
00077  * Convenience function to determine whether any given session is 'visible' to any other given session,
00078  * and is capable of receiving instant messages from that session.
00079  */
00080 int xmpp_is_visible(struct CitContext *cptr, struct CitContext *to_whom) {
00081        int aide = (to_whom->user.axlevel >= AxAideU);
00082 
00083        if (   (cptr->logged_in)
00084               &&     (((cptr->cs_flags&CS_STEALTH)==0) || (aide))     /* aides see everyone */
00085               &&     (cptr->user.usernum != to_whom->user.usernum)    /* don't show myself */
00086               &&     (cptr->can_receive_im)                           /* IM-capable session */
00087        ) {
00088               return(1);
00089        }
00090        else {
00091               return(0);
00092        }
00093 }
00094 
00095 
00096 /* 
00097  * Initial dump of the entire wholist
00098  */
00099 void xmpp_wholist_presence_dump(void)
00100 {
00101        struct CitContext *cptr = NULL;
00102        int nContexts, i;
00103        
00104        cptr = CtdlGetContextArray(&nContexts);
00105        if (!cptr) {
00106               return;
00107        }
00108 
00109        for (i=0; i<nContexts; i++) {
00110               if (xmpp_is_visible(&cptr[i], CC)) {
00111                      xmpp_indicate_presence(cptr[i].cs_inet_email);
00112               }
00113        }
00114        free(cptr);
00115 }
00116 
00117 
00118 /*
00119  * Function to remove a buddy subscription and delete from the roster
00120  * (used in several places)
00121  */
00122 void xmpp_destroy_buddy(char *presence_jid, int aggressively) {
00123        static int unsolicited_id = 1;
00124        char xmlbuf1[256];
00125        char xmlbuf2[256];
00126 
00127        if (!presence_jid) return;
00128        if (!XMPP) return;
00129        if (!XMPP->client_jid) return;
00130 
00131        /* Transmit non-presence information */
00132        cprintf("<presence type=\"unavailable\" from=\"%s\" to=\"%s\"></presence>",
00133               xmlesc(xmlbuf1, presence_jid, sizeof xmlbuf1),
00134               xmlesc(xmlbuf2, XMPP->client_jid, sizeof xmlbuf2)
00135        );
00136 
00137        /*
00138         * Setting the "aggressively" flag also sends an "unsubscribed" presence update.
00139         * We only ask for this when flushing the client side roster, because if we do it
00140         * in the middle of a session when another user logs off, some clients (Jitsi) interpret
00141         * it as a rejection of a subscription request.
00142         */
00143        if (aggressively) {
00144               cprintf("<presence type=\"unsubscribed\" from=\"%s\" to=\"%s\"></presence>",
00145                      xmlesc(xmlbuf1, presence_jid, sizeof xmlbuf1),
00146                      xmlesc(xmlbuf2, XMPP->client_jid, sizeof xmlbuf2)
00147               );
00148        }
00149 
00150        // FIXME ... we should implement xmpp_indicate_nonpresence so we can use it elsewhere
00151 
00152        /* Do an unsolicited roster update that deletes the contact. */
00153        cprintf("<iq from=\"%s\" to=\"%s\" id=\"unbuddy_%x\" type=\"result\">",
00154               xmlesc(xmlbuf1, CC->cs_inet_email, sizeof xmlbuf1),
00155               xmlesc(xmlbuf2, XMPP->client_jid, sizeof xmlbuf2),
00156               ++unsolicited_id
00157        );
00158        cprintf("<query xmlns=\"jabber:iq:roster\">");
00159        cprintf("<item jid=\"%s\" subscription=\"remove\">", xmlesc(xmlbuf1, presence_jid, sizeof xmlbuf1));
00160        cprintf("<group>%s</group>", xmlesc(xmlbuf1, config.c_humannode, sizeof xmlbuf1));
00161        cprintf("</item>");
00162        cprintf("</query>"
00163               "</iq>"
00164        );
00165 }
00166 
00167 
00168 /*
00169  * When a user logs in or out of the local Citadel system, notify all XMPP sessions about it.
00170  * THIS FUNCTION HAS A BUG IN IT THAT ENUMERATES THE SESSIONS WRONG.
00171  */
00172 void xmpp_presence_notify(char *presence_jid, int event_type) {
00173        struct CitContext *cptr;
00174        static int unsolicited_id;
00175        int visible_sessions = 0;
00176        int nContexts, i;
00177        int which_cptr_is_relevant = (-1);
00178 
00179        if (IsEmptyStr(presence_jid)) return;
00180        if (CC->kill_me) return;
00181 
00182        cptr = CtdlGetContextArray(&nContexts);
00183        if (!cptr) {
00184               return;
00185        }
00186 
00187        /* Count the visible sessions for this user */
00188        for (i=0; i<nContexts; i++) {
00189               if ( (!strcasecmp(cptr[i].cs_inet_email, presence_jid))
00190                  && (xmpp_is_visible(&cptr[i], CC))
00191               )  {
00192                      ++visible_sessions;
00193                      which_cptr_is_relevant = i;
00194               }
00195        }
00196 
00197        XMPP_syslog(LOG_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
00198                   visible_sessions, presence_jid, CC->cs_pid);
00199 
00200        if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
00201 
00202               XMPP_syslog(LOG_DEBUG, "Telling session %d that <%s> logged in\n",
00203                          CC->cs_pid, presence_jid);
00204 
00205               /* Do an unsolicited roster update that adds a new contact. */
00206               assert(which_cptr_is_relevant >= 0);
00207               cprintf("<iq id=\"unsolicited_%x\" type=\"result\">", ++unsolicited_id);
00208               cprintf("<query xmlns=\"jabber:iq:roster\">");
00209               xmpp_roster_item(&cptr[which_cptr_is_relevant]);
00210               cprintf("</query></iq>");
00211 
00212               /* Transmit presence information */
00213               xmpp_indicate_presence(presence_jid);
00214        }
00215 
00216        if (visible_sessions == 0) {
00217               XMPP_syslog(LOG_DEBUG, "Telling session %d that <%s> logged out\n",
00218                          CC->cs_pid, presence_jid);
00219               xmpp_destroy_buddy(presence_jid, 0);      /* non aggressive presence update */
00220        }
00221 
00222        free(cptr);
00223 }
00224 
00225 
00226 
00227 void xmpp_fetch_mortuary_backend(long msgnum, void *userdata) {
00228        HashList *mortuary = (HashList *) userdata;
00229        struct CtdlMessage *msg;
00230        char *ptr = NULL;
00231        char *lasts = NULL;
00232 
00233        msg = CtdlFetchMessage(msgnum, 1);
00234        if (msg == NULL) {
00235               return;
00236        }
00237 
00238        /* now add anyone we find into the hashlist */
00239 
00240        /* skip past the headers */
00241        ptr = strstr(msg->cm_fields['M'], "\n\n");
00242        if (ptr != NULL) {
00243               ptr += 2;
00244        }
00245        else {
00246               ptr = strstr(msg->cm_fields['M'], "\n\r\n");
00247               if (ptr != NULL) {
00248                      ptr += 3;
00249               }
00250        }
00251 
00252        /* the remaining lines are addresses */
00253        if (ptr != NULL) {
00254               ptr = strtok_r(ptr, "\n", &lasts);
00255               while (ptr != NULL) {
00256                      char *pch = strdup(ptr);
00257                      Put(mortuary, pch, strlen(pch), pch, NULL);
00258                      ptr = strtok_r(NULL, "\n", &lasts);
00259               }
00260        }
00261 
00262        CtdlFreeMessage(msg);
00263 }
00264 
00265 
00266 
00267 /*
00268  * Fetch the "mortuary" - a list of dead buddies which we keep around forever
00269  * so we can remove them from any client's roster that still has them listed
00270  */
00271 HashList *xmpp_fetch_mortuary(void) {
00272        HashList *mortuary = NewHash(1, NULL);
00273        if (!mortuary) {
00274               XMPPM_syslog(LOG_ALERT, "NewHash() failed!\n");
00275               return(NULL);
00276        }
00277 
00278         if (CtdlGetRoom(&CC->room, USERCONFIGROOM) != 0) {
00279               /* no config room exists - no further processing is required. */
00280                 return(mortuary);
00281         }
00282         CtdlForEachMessage(MSGS_LAST, 1, NULL, XMPPMORTUARY, NULL,
00283                 xmpp_fetch_mortuary_backend, (void *)mortuary );
00284 
00285        return(mortuary);
00286 }
00287 
00288 
00289 
00290 /*
00291  * Fetch the "mortuary" - a list of dead buddies which we keep around forever
00292  * so we can remove them from any client's roster that still has them listed
00293  */
00294 void xmpp_store_mortuary(HashList *mortuary) {
00295        HashPos *HashPos;
00296        long len;
00297        void *Value;
00298        const char *Key;
00299        StrBuf *themsg;
00300 
00301        themsg = NewStrBuf();
00302        StrBufPrintf(themsg, "Content-type: " XMPPMORTUARY "\n"
00303                             "Content-transfer-encoding: 7bit\n"
00304                             "\n"
00305        );
00306 
00307        HashPos = GetNewHashPos(mortuary, 0);
00308        while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
00309        {
00310               StrBufAppendPrintf(themsg, "%s\n", (char *)Value);
00311        }
00312        DeleteHashPos(&HashPos);
00313 
00314        /* FIXME temp crap 
00315        StrBufAppendPrintf(themsg, "foo@bar.com\n");
00316        StrBufAppendPrintf(themsg, "baz@quux.com\n");
00317        StrBufAppendPrintf(themsg, "haha%c\n", 1);
00318        StrBufAppendPrintf(themsg, "baaaz@quux.com\n");
00319        StrBufAppendPrintf(themsg, "baaaz@quuuuuux.com\n");
00320        */
00321 
00322        /* Delete the old mortuary */
00323        CtdlDeleteMessages(USERCONFIGROOM, NULL, 0, XMPPMORTUARY);
00324 
00325        /* And save the new one to disk */
00326        quickie_message("Citadel", NULL, NULL, USERCONFIGROOM, ChrPtr(themsg), 4, "XMPP Mortuary");
00327        FreeStrBuf(&themsg);
00328 }
00329 
00330 
00331 
00332 /*
00333  * Upon logout we make an attempt to delete the whole roster, in order to
00334  * try to keep "ghost" buddies from remaining in the client-side roster.
00335  *
00336  * Since the client is probably not still alive, also remember the current
00337  * roster for next time so we can delete dead buddies then.
00338  */
00339 void xmpp_massacre_roster(void)
00340 {
00341        struct CitContext *cptr;
00342        int nContexts, i;
00343        HashList *mortuary = xmpp_fetch_mortuary();
00344 
00345        cptr = CtdlGetContextArray(&nContexts);
00346        if (cptr) {
00347               for (i=0; i<nContexts; i++) {
00348                      if (xmpp_is_visible(&cptr[i], CC)) {
00349                             if (mortuary) {
00350                                    char *buddy = strdup(cptr[i].cs_inet_email);
00351                                    Put(mortuary, buddy, strlen(buddy), buddy, NULL);
00352                             }
00353                      }
00354               }
00355               free (cptr);
00356        }
00357 
00358        if (mortuary) {
00359               xmpp_store_mortuary(mortuary);
00360               DeleteHash(&mortuary);
00361        }
00362 }
00363 
00364 
00365 
00366 /*
00367  * Stupidly, XMPP does not specify a way to tell the client to flush its client-side roster
00368  * and prepare to receive a new one.  So instead we remember every buddy we've ever told the
00369  * client about, and push delete operations out at the beginning of a session.
00370  * 
00371  * We omit any users who happen to be online right now, but we still keep them in the mortuary,
00372  * which needs to be maintained as a list of every buddy the user has ever seen.  We don't know
00373  * when they're connecting from the same client and when they're connecting from a different client,
00374  * so we have no guarantee of what is in the client side roster at connect time.
00375  */
00376 void xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster(void)
00377 {
00378        long len;
00379        void *Value;
00380        const char *Key;
00381        struct CitContext *cptr;
00382        int nContexts, i;
00383        int online_now = 0;
00384        HashList *mortuary = xmpp_fetch_mortuary();
00385        HashPos *HashPos = GetNewHashPos(mortuary, 0);
00386 
00387        /* we need to omit anyone who is currently online */
00388        cptr = CtdlGetContextArray(&nContexts);
00389 
00390        /* go through the list of users in the mortuary... */
00391        while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
00392        {
00393 
00394               online_now = 0;
00395               if (cptr) for (i=0; i<nContexts; i++) {
00396                      if (xmpp_is_visible(&cptr[i], CC)) {
00397                             if (!strcasecmp(cptr[i].cs_inet_email, (char *)Value)) {
00398                                    online_now = 1;
00399                             }
00400                      }
00401               }
00402 
00403               if (!online_now) {
00404                      xmpp_destroy_buddy((char *)Value, 1);     /* aggressive presence update */
00405               }
00406 
00407        }
00408        DeleteHashPos(&HashPos);
00409        DeleteHash(&mortuary);
00410        free(cptr);
00411 }
00412