* Tried to complete the dead buddy thing in xmpp, but the code I already wrote now...
[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  * Initial dump of the entire wholist
78  */
79 void xmpp_wholist_presence_dump(void)
80 {
81         struct CitContext *cptr = NULL;
82         int nContexts, i;
83         
84         int aide = (CC->user.axlevel >= AxAideU);
85
86         cptr = CtdlGetContextArray(&nContexts);
87         if (!cptr) {
88                 return;
89         }
90
91         for (i=0; i<nContexts; i++) {
92                 if (cptr[i].logged_in) {
93                         if (
94                                 (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))  /* aides see everyone */
95                                 && (cptr[i].user.usernum != CC->user.usernum)   /* don't show myself */
96                                 && (cptr[i].can_receive_im)                     /* IM-capable session */
97                         ) {
98                                 xmpp_indicate_presence(cptr[i].cs_inet_email);
99                         }
100                 }
101         }
102         free(cptr);
103 }
104
105
106 /*
107  * Function to remove a buddy subscription and delete from the roster
108  * (used in several places)
109  */
110 void xmpp_destroy_buddy(char *presence_jid) {
111         static int unsolicited_id = 1;
112
113         /* Transmit non-presence information */
114         cprintf("<presence type=\"unavailable\" from=\"%s\" to=\"%s\"></presence>",
115                 presence_jid, XMPP->client_jid
116         );
117         cprintf("<presence type=\"unsubscribed\" from=\"%s\" to=\"%s\"></presence>",
118                 presence_jid, XMPP->client_jid
119         );
120         // FIXME ... we should implement xmpp_indicate_nonpresence so we can use it elsewhere
121
122         /* Do an unsolicited roster update that deletes the contact. */
123         cprintf("<iq from=\"%s\" to=\"%s\" id=\"unbuddy_%x\" type=\"result\">",
124                 CC->cs_inet_email,
125                 XMPP->client_jid,
126                 ++unsolicited_id
127         );
128         cprintf("<query xmlns=\"jabber:iq:roster\">");
129         cprintf("<item jid=\"%s\" subscription=\"remove\">", presence_jid);
130         cprintf("<group>%s</group>", config.c_humannode);
131         cprintf("</item>");
132         cprintf("</query>"
133                 "</iq>"
134         );
135 }
136
137
138 /*
139  * When a user logs in or out of the local Citadel system, notify all XMPP sessions about it.
140  */
141 void xmpp_presence_notify(char *presence_jid, int event_type) {
142         struct CitContext *cptr;
143         static int unsolicited_id;
144         int visible_sessions = 0;
145         int nContexts, i;
146         int aide = (CC->user.axlevel >= AxAideU);
147
148         if (IsEmptyStr(presence_jid)) return;
149         if (CC->kill_me) return;
150
151         cptr = CtdlGetContextArray(&nContexts);
152         if (!cptr) {
153                 return;
154         }
155                 
156         /* Count the visible sessions for this user */
157         for (i=0; i<nContexts; i++) {
158                 if (cptr[i].logged_in) {
159                         if (
160                                 (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) 
161                                 && (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
162                                 && (cptr[i].can_receive_im)
163                         ) {
164                                 ++visible_sessions;
165                         }
166                 }
167         }
168
169         CtdlLogPrintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
170                 visible_sessions, presence_jid, CC->cs_pid);
171
172         if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
173
174                 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
175
176                 /* Do an unsolicited roster update that adds a new contact. */
177                 for (i=0; i<nContexts; i++) {
178                         if (cptr[i].logged_in) {
179                                 if (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) {
180                                         cprintf("<iq id=\"unsolicited_%x\" type=\"result\">",
181                                                 ++unsolicited_id);
182                                         cprintf("<query xmlns=\"jabber:iq:roster\">");
183                                         xmpp_roster_item(&cptr[i]);
184                                         cprintf("</query>"
185                                                 "</iq>");
186                                 }
187                         }
188                 }
189
190                 /* Transmit presence information */
191                 xmpp_indicate_presence(presence_jid);
192         }
193
194         if (visible_sessions == 0) {
195                 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n", CC->cs_pid, presence_jid);
196
197                 xmpp_destroy_buddy(presence_jid);
198         }
199         free(cptr);
200 }
201
202
203
204 void xmpp_fetch_mortuary_backend(long msgnum, void *userdata) {
205         HashList *mortuary = (HashList *) userdata;
206         struct CtdlMessage *msg;
207         const char *ptr = NULL;
208         const char *endptr = NULL;
209         int in_body = 0;
210         int len;
211         char buf[256];
212
213         msg = CtdlFetchMessage(msgnum, 1);
214         if (msg == NULL) {
215                 return;
216         }
217
218         /* now add anyone we find into the hashlist */
219
220         ptr = msg->cm_fields['M'];
221         endptr = ptr + strlen(ptr);     // only do strlen once :)
222         CtdlLogPrintf(CTDL_DEBUG, "\033[33m%s\033[0m\n", ptr);
223         while (ptr = memreadlinelen(ptr, buf, (sizeof buf - 2), &len), ((ptr < endptr) && (*ptr != 0)) ) {
224                 if (in_body) {
225                         char *pch; 
226                         pch = malloc(len + 1);
227                         memcpy(pch, buf, len + 1);
228                         Put(mortuary, pch, len, pch, NULL);
229                 }
230                 if (IsEmptyStr(buf)) in_body = 1;
231         }
232
233         CtdlFreeMessage(msg);
234 }
235
236
237
238 /*
239  * Fetch the "mortuary" - a list of dead buddies which we keep around forever
240  * so we can remove them from any client's roster that still has them listed
241  */
242 HashList *xmpp_fetch_mortuary(void) {
243         HashList *mortuary = NewHash(1, NULL);
244         if (!mortuary) {
245                 CtdlLogPrintf(CTDL_ALERT, "NewHash() failed!\n");
246                 return(NULL);
247         }
248
249         if (CtdlGetRoom(&CC->room, USERCONFIGROOM) != 0) {
250                 /* no config room exists - no further processing is required. */
251                 return(mortuary);
252         }
253         CtdlForEachMessage(MSGS_LAST, 1, NULL, XMPPMORTUARY, NULL,
254                 xmpp_fetch_mortuary_backend, (void *)mortuary );
255
256         return(mortuary);
257 }
258
259
260
261 /*
262  * Fetch the "mortuary" - a list of dead buddies which we keep around forever
263  * so we can remove them from any client's roster that still has them listed
264  */
265 void xmpp_store_mortuary(HashList *mortuary) {
266         HashPos *HashPos;
267         long len;
268         void *Value;
269         const char *Key;
270         StrBuf *themsg;
271
272         themsg = NewStrBuf();
273         StrBufPrintf(themsg,    "Content-type: " XMPPMORTUARY "\n"
274                                 "Content-transfer-encoding: 7bit\n"
275                                 "\n"
276         );
277
278         HashPos = GetNewHashPos(mortuary, 0);
279         while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
280         {
281                 StrBufAppendPrintf(themsg, "%s\n", (char *)Value);
282         }
283         DeleteHashPos(&HashPos);
284
285         /* Delete the old mortuary */
286         CtdlDeleteMessages(USERCONFIGROOM, NULL, 0, XMPPMORTUARY);
287
288         /* And save the new one to disk */
289         quickie_message("Citadel", NULL, NULL, USERCONFIGROOM, ChrPtr(themsg), 4, "XMPP Mortuary");
290         FreeStrBuf(&themsg);
291 }
292
293
294
295 /*
296  * Upon logout we make an attempt to delete the whole roster, in order to
297  * try to keep "ghost" buddies from remaining in the client-side roster.
298  *
299  * Since the client is probably not still alive, also remember the current
300  * roster for next time so we can delete dead buddies then.
301  */
302 void xmpp_massacre_roster(void)
303 {
304         struct CitContext *cptr;
305         int nContexts, i;
306         int aide = (CC->user.axlevel >= AxAideU);
307         HashList *mortuary = xmpp_fetch_mortuary();
308
309         cptr = CtdlGetContextArray(&nContexts);
310         if (cptr) {
311                 for (i=0; i<nContexts; i++) {
312                         if (cptr[i].logged_in) {
313                                 if (
314                                         (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
315                                         && (cptr[i].user.usernum != CC->user.usernum)
316                                 ) {
317                                         xmpp_destroy_buddy(cptr[i].cs_inet_email);
318                                         if (mortuary) {
319                                                 char *buddy = strdup(cptr[i].cs_inet_email);
320                                                 Put(mortuary, buddy, strlen(buddy),
321                                                         buddy, generic_free_handler);
322                                         }
323                                 }
324                         }
325                 }
326                 free (cptr);
327         }
328
329         if (mortuary) {
330                 xmpp_store_mortuary(mortuary);
331                 DeleteHash(&mortuary);
332         }
333 }
334
335
336
337 /*
338  * Stupidly, XMPP does not specify a way to tell the client to flush its client-side roster
339  * and prepare to receive a new one.  So instead we remember every buddy we've ever told the
340  * client about, and push delete operations out at the beginning of a session.
341  */
342 void xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster(void)
343 {
344         long len;
345         void *Value;
346         const char *Key;
347         HashList *mortuary = xmpp_fetch_mortuary();
348         HashPos *HashPos = GetNewHashPos(mortuary, 0);
349
350         CtdlLogPrintf(CTDL_DEBUG, "\033[32mHAHA\033[0m\n");
351         while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
352         {
353                 CtdlLogPrintf(CTDL_DEBUG, "\033[31mDELETE: %s\033[0m\n", (char *)Value);
354         }
355         CtdlLogPrintf(CTDL_DEBUG, "\033[32mHOHO\033[0m\n");
356         DeleteHashPos(&HashPos);
357         DeleteHash(&mortuary);
358 }