* Changed the logic of how we inform the client about dead buddies. Instead of pushi...
[citadel.git] / citadel / modules / xmpp / xmpp_presence.c
1 /*
2  * $Id$ 
3  *
4  * Handle XMPP presence exchanges
5  *
6  * Copyright (c) 2007-2010 by Art Cancro
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "sysdep.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <pwd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <sys/wait.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <ctype.h>
49 #include <expat.h>
50 #include <libcitadel.h>
51 #include "citadel.h"
52 #include "server.h"
53 #include "citserver.h"
54 #include "support.h"
55 #include "config.h"
56 #include "internet_addressing.h"
57 #include "md5.h"
58 #include "ctdl_module.h"
59 #include "serv_xmpp.h"
60
61
62
63 /* 
64  * Indicate the presence of another user to the client
65  * (used in several places)
66  */
67 void xmpp_indicate_presence(char *presence_jid)
68 {
69         cprintf("<presence from=\"%s\" to=\"%s\"></presence>",
70                 presence_jid,
71                 XMPP->client_jid
72         );
73 }
74
75
76
77 /*
78  * Convenience function to determine whether a particular session is visible to this user
79  */
80 int xmpp_is_visible(struct CitContext *cptr) {
81         int aide = (CC->user.axlevel >= AxAideU);
82
83         if (    (cptr->logged_in)
84                 &&      (((cptr->cs_flags&CS_STEALTH)==0) || (aide))    /* aides see everyone */
85                 &&      (cptr->user.usernum != CC->user.usernum)        /* don't show myself */
86                 &&      (cptr->can_receive_im)                          /* IM-capable session */
87         ) {
88                 return(1);
89         }
90         else {
91                 return(0);
92         }
93 }
94
95
96 /* 
97  * Initial dump of the entire wholist
98  */
99 void xmpp_wholist_presence_dump(void)
100 {
101         struct CitContext *cptr = NULL;
102         int nContexts, i;
103         
104         cptr = CtdlGetContextArray(&nContexts);
105         if (!cptr) {
106                 return;
107         }
108
109         for (i=0; i<nContexts; i++) {
110                 if (xmpp_is_visible(&cptr[i])) {
111                         xmpp_indicate_presence(cptr[i].cs_inet_email);
112                 }
113         }
114         free(cptr);
115 }
116
117
118 /*
119  * Function to remove a buddy subscription and delete from the roster
120  * (used in several places)
121  */
122 void xmpp_destroy_buddy(char *presence_jid) {
123         static int unsolicited_id = 1;
124
125         if (!presence_jid) return;
126         if (!XMPP) return;
127         if (!XMPP->client_jid) return;
128
129         CtdlLogPrintf(CTDL_DEBUG, "\033[31mdestroy_buddy(%s)\033[0m\n", presence_jid);
130         /* Transmit non-presence information */
131         cprintf("<presence type=\"unavailable\" from=\"%s\" to=\"%s\"></presence>",
132                 presence_jid, XMPP->client_jid
133         );
134         cprintf("<presence type=\"unsubscribed\" from=\"%s\" to=\"%s\"></presence>",
135                 presence_jid, XMPP->client_jid
136         );
137         // FIXME ... we should implement xmpp_indicate_nonpresence so we can use it elsewhere
138
139         /* Do an unsolicited roster update that deletes the contact. */
140         cprintf("<iq from=\"%s\" to=\"%s\" id=\"unbuddy_%x\" type=\"result\">",
141                 CC->cs_inet_email,
142                 XMPP->client_jid,
143                 ++unsolicited_id
144         );
145         cprintf("<query xmlns=\"jabber:iq:roster\">");
146         cprintf("<item jid=\"%s\" subscription=\"remove\">", presence_jid);
147         cprintf("<group>%s</group>", config.c_humannode);
148         cprintf("</item>");
149         cprintf("</query>"
150                 "</iq>"
151         );
152 }
153
154
155 /*
156  * When a user logs in or out of the local Citadel system, notify all XMPP sessions about it.
157  */
158 void xmpp_presence_notify(char *presence_jid, int event_type) {
159         struct CitContext *cptr;
160         static int unsolicited_id;
161         int visible_sessions = 0;
162         int nContexts, i;
163
164         if (IsEmptyStr(presence_jid)) return;
165         if (CC->kill_me) return;
166
167         cptr = CtdlGetContextArray(&nContexts);
168         if (!cptr) {
169                 return;
170         }
171                 
172         /* Count the visible sessions for this user */
173         for (i=0; i<nContexts; i++) {
174                 if (xmpp_is_visible(&cptr[i])) {
175                         ++visible_sessions;
176                 }
177         }
178
179         CtdlLogPrintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
180                 visible_sessions, presence_jid, CC->cs_pid);
181
182         if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
183
184                 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
185
186                 /* Do an unsolicited roster update that adds a new contact. */
187                 for (i=0; i<nContexts; i++) {
188                         if (xmpp_is_visible(&cptr[i])) {
189                                 if (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) {
190                                         cprintf("<iq id=\"unsolicited_%x\" type=\"result\">",
191                                                 ++unsolicited_id);
192                                         cprintf("<query xmlns=\"jabber:iq:roster\">");
193                                         xmpp_roster_item(&cptr[i]);
194                                         cprintf("</query>"
195                                                 "</iq>");
196                                 }
197                         }
198                 }
199
200                 /* Transmit presence information */
201                 xmpp_indicate_presence(presence_jid);
202         }
203
204         if (visible_sessions == 0) {
205                 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n", CC->cs_pid, presence_jid);
206
207                 xmpp_destroy_buddy(presence_jid);
208         }
209         free(cptr);
210 }
211
212
213
214 void xmpp_fetch_mortuary_backend(long msgnum, void *userdata) {
215         HashList *mortuary = (HashList *) userdata;
216         struct CtdlMessage *msg;
217         char *ptr = NULL;
218         char *lasts = NULL;
219
220         msg = CtdlFetchMessage(msgnum, 1);
221         if (msg == NULL) {
222                 return;
223         }
224
225         /* now add anyone we find into the hashlist */
226
227         /* skip past the headers */
228         ptr = strstr(msg->cm_fields['M'], "\n\n");
229         if (ptr != NULL) {
230                 ptr += 2;
231         }
232         else {
233                 ptr = strstr(msg->cm_fields['M'], "\n\r\n");
234                 if (ptr != NULL) {
235                         ptr += 3;
236                 }
237         }
238
239         /* the remaining lines are addresses */
240         if (ptr != NULL) {
241                 ptr = strtok_r(ptr, "\n", &lasts);
242                 while (ptr != NULL) {
243                         char *pch = strdup(ptr);
244                         Put(mortuary, pch, strlen(pch), pch, NULL);
245                         ptr = strtok_r(NULL, "\n", &lasts);
246                 }
247         }
248
249         CtdlFreeMessage(msg);
250 }
251
252
253
254 /*
255  * Fetch the "mortuary" - a list of dead buddies which we keep around forever
256  * so we can remove them from any client's roster that still has them listed
257  */
258 HashList *xmpp_fetch_mortuary(void) {
259         HashList *mortuary = NewHash(1, NULL);
260         if (!mortuary) {
261                 CtdlLogPrintf(CTDL_ALERT, "NewHash() failed!\n");
262                 return(NULL);
263         }
264
265         if (CtdlGetRoom(&CC->room, USERCONFIGROOM) != 0) {
266                 /* no config room exists - no further processing is required. */
267                 return(mortuary);
268         }
269         CtdlForEachMessage(MSGS_LAST, 1, NULL, XMPPMORTUARY, NULL,
270                 xmpp_fetch_mortuary_backend, (void *)mortuary );
271
272         return(mortuary);
273 }
274
275
276
277 /*
278  * Fetch the "mortuary" - a list of dead buddies which we keep around forever
279  * so we can remove them from any client's roster that still has them listed
280  */
281 void xmpp_store_mortuary(HashList *mortuary) {
282         HashPos *HashPos;
283         long len;
284         void *Value;
285         const char *Key;
286         StrBuf *themsg;
287
288         themsg = NewStrBuf();
289         StrBufPrintf(themsg,    "Content-type: " XMPPMORTUARY "\n"
290                                 "Content-transfer-encoding: 7bit\n"
291                                 "\n"
292         );
293
294         HashPos = GetNewHashPos(mortuary, 0);
295         while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
296         {
297                 StrBufAppendPrintf(themsg, "%s\n", (char *)Value);
298         }
299         DeleteHashPos(&HashPos);
300
301         /* Delete the old mortuary */
302         CtdlDeleteMessages(USERCONFIGROOM, NULL, 0, XMPPMORTUARY);
303
304         /* And save the new one to disk */
305         quickie_message("Citadel", NULL, NULL, USERCONFIGROOM, ChrPtr(themsg), 4, "XMPP Mortuary");
306         FreeStrBuf(&themsg);
307 }
308
309
310
311 /*
312  * Upon logout we make an attempt to delete the whole roster, in order to
313  * try to keep "ghost" buddies from remaining in the client-side roster.
314  *
315  * Since the client is probably not still alive, also remember the current
316  * roster for next time so we can delete dead buddies then.
317  */
318 void xmpp_massacre_roster(void)
319 {
320         struct CitContext *cptr;
321         int nContexts, i;
322         HashList *mortuary = xmpp_fetch_mortuary();
323
324         cptr = CtdlGetContextArray(&nContexts);
325         if (cptr) {
326                 for (i=0; i<nContexts; i++) {
327                         if (xmpp_is_visible(&cptr[i])) {
328                                 if (mortuary) {
329                                         char *buddy = strdup(cptr[i].cs_inet_email);
330                                         Put(mortuary, buddy, strlen(buddy), buddy, NULL);
331                                 }
332                         }
333                 }
334                 free (cptr);
335         }
336
337         if (mortuary) {
338                 xmpp_store_mortuary(mortuary);
339                 DeleteHash(&mortuary);
340         }
341 }
342
343
344
345 /*
346  * Stupidly, XMPP does not specify a way to tell the client to flush its client-side roster
347  * and prepare to receive a new one.  So instead we remember every buddy we've ever told the
348  * client about, and push delete operations out at the beginning of a session.
349  * 
350  * We omit any users who happen to be online right now, but we still keep them in the mortuary,
351  * which needs to be maintained as a list of every buddy the user has ever seen.  We don't know
352  * when they're connecting from the same client and when they're connecting from a different client,
353  * so we have no guarantee of what is in the client side roster at connect time.
354  */
355 void xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster(void)
356 {
357         long len;
358         void *Value;
359         const char *Key;
360         struct CitContext *cptr;
361         int nContexts, i;
362         int online_now = 0;
363         HashList *mortuary = xmpp_fetch_mortuary();
364         HashPos *HashPos = GetNewHashPos(mortuary, 0);
365
366         /* we need to omit anyone who is currently online */
367         cptr = CtdlGetContextArray(&nContexts);
368
369         /* go through the list of users in the mortuary... */
370         while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
371         {
372
373                 online_now = 0;
374                 if (cptr) for (i=0; i<nContexts; i++) {
375                         if (xmpp_is_visible(&cptr[i])) {
376                                 if (!strcasecmp(cptr[i].cs_inet_email, (char *)Value)) {
377                                         online_now = 1;
378                                 }
379                         }
380                 }
381
382                 if (!online_now) {
383                         xmpp_destroy_buddy((char *)Value);
384                 }
385
386         }
387         DeleteHashPos(&HashPos);
388         DeleteHash(&mortuary);
389         free(cptr);
390 }
391