2 * Handle XMPP presence exchanges
4 * Copyright (c) 2007-2010 by Art Cancro
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sys/types.h>
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
38 # include <sys/time.h>
49 #include <libcitadel.h>
52 #include "citserver.h"
55 #include "internet_addressing.h"
57 #include "ctdl_module.h"
58 #include "serv_xmpp.h"
63 * Indicate the presence of another user to the client
64 * (used in several places)
66 void xmpp_indicate_presence(char *presence_jid)
70 syslog(LOG_DEBUG, "XMPP: indicating presence of <%s> to <%s>", presence_jid, XMPP->client_jid);
71 cprintf("<presence from=\"%s\" ", xmlesc(xmlbuf, presence_jid, sizeof xmlbuf));
72 cprintf("to=\"%s\"></presence>", xmlesc(xmlbuf, XMPP->client_jid, sizeof xmlbuf));
78 * Convenience function to determine whether any given session is 'visible' to any other given session,
79 * and is capable of receiving instant messages from that session.
81 int xmpp_is_visible(struct CitContext *cptr, struct CitContext *to_whom) {
82 int aide = (to_whom->user.axlevel >= AxAideU);
84 if ( (cptr->logged_in)
85 && (((cptr->cs_flags&CS_STEALTH)==0) || (aide)) /* aides see everyone */
86 && (cptr->user.usernum != to_whom->user.usernum) /* don't show myself */
87 && (cptr->can_receive_im) /* IM-capable session */
98 * Initial dump of the entire wholist
100 void xmpp_wholist_presence_dump(void)
102 struct CitContext *cptr = NULL;
105 cptr = CtdlGetContextArray(&nContexts);
110 for (i=0; i<nContexts; i++) {
111 if (xmpp_is_visible(&cptr[i], CC)) {
112 xmpp_indicate_presence(cptr[i].cs_inet_email);
120 * Function to remove a buddy subscription and delete from the roster
121 * (used in several places)
123 void xmpp_destroy_buddy(char *presence_jid, int aggressively) {
124 static int unsolicited_id = 1;
128 if (!presence_jid) return;
130 if (!XMPP->client_jid) return;
132 /* Transmit non-presence information */
133 cprintf("<presence type=\"unavailable\" from=\"%s\" to=\"%s\"></presence>",
134 xmlesc(xmlbuf1, presence_jid, sizeof xmlbuf1),
135 xmlesc(xmlbuf2, XMPP->client_jid, sizeof xmlbuf2)
139 * Setting the "aggressively" flag also sends an "unsubscribed" presence update.
140 * We only ask for this when flushing the client side roster, because if we do it
141 * in the middle of a session when another user logs off, some clients (Jitsi) interpret
142 * it as a rejection of a subscription request.
145 cprintf("<presence type=\"unsubscribed\" from=\"%s\" to=\"%s\"></presence>",
146 xmlesc(xmlbuf1, presence_jid, sizeof xmlbuf1),
147 xmlesc(xmlbuf2, XMPP->client_jid, sizeof xmlbuf2)
151 // FIXME ... we should implement xmpp_indicate_nonpresence so we can use it elsewhere
153 /* Do an unsolicited roster update that deletes the contact. */
154 cprintf("<iq from=\"%s\" to=\"%s\" id=\"unbuddy_%x\" type=\"result\">",
155 xmlesc(xmlbuf1, CC->cs_inet_email, sizeof xmlbuf1),
156 xmlesc(xmlbuf2, XMPP->client_jid, sizeof xmlbuf2),
159 cprintf("<query xmlns=\"jabber:iq:roster\">");
160 cprintf("<item jid=\"%s\" subscription=\"remove\">", xmlesc(xmlbuf1, presence_jid, sizeof xmlbuf1));
161 cprintf("<group>%s</group>", xmlesc(xmlbuf1, config.c_humannode, sizeof xmlbuf1));
170 * When a user logs in or out of the local Citadel system, notify all XMPP sessions about it.
171 * THIS FUNCTION HAS A BUG IN IT THAT ENUMERATES THE SESSIONS WRONG.
173 void xmpp_presence_notify(char *presence_jid, int event_type) {
174 struct CitContext *cptr;
175 static int unsolicited_id = 12345;
176 int visible_sessions = 0;
178 int which_cptr_is_relevant = (-1);
180 if (IsEmptyStr(presence_jid)) return;
181 if (CC->kill_me) return;
183 cptr = CtdlGetContextArray(&nContexts);
188 /* Count the visible sessions for this user */
189 for (i=0; i<nContexts; i++) {
190 if ( (!strcasecmp(cptr[i].cs_inet_email, presence_jid))
191 && (xmpp_is_visible(&cptr[i], CC))
194 which_cptr_is_relevant = i;
198 syslog(LOG_DEBUG, "%d sessions for <%s> are now visible to session %d\n", visible_sessions, presence_jid, CC->cs_pid);
200 if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
202 syslog(LOG_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
204 /* Do an unsolicited roster update that adds a new contact. */
205 assert(which_cptr_is_relevant >= 0);
206 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">", ++unsolicited_id);
207 cprintf("<query xmlns=\"jabber:iq:roster\">");
208 xmpp_roster_item(&cptr[which_cptr_is_relevant]);
209 cprintf("</query></iq>");
211 /* Transmit presence information */
212 xmpp_indicate_presence(presence_jid);
215 if (visible_sessions == 0) {
216 syslog(LOG_DEBUG, "Telling session %d that <%s> logged out\n",
217 CC->cs_pid, presence_jid);
218 xmpp_destroy_buddy(presence_jid, 0); /* non aggressive presence update */
226 void xmpp_fetch_mortuary_backend(long msgnum, void *userdata) {
227 HashList *mortuary = (HashList *) userdata;
228 struct CtdlMessage *msg;
232 msg = CtdlFetchMessage(msgnum, 1);
237 /* now add anyone we find into the hashlist */
239 /* skip past the headers */
240 ptr = strstr(msg->cm_fields[eMesageText], "\n\n");
245 ptr = strstr(msg->cm_fields[eMesageText], "\n\r\n");
251 /* the remaining lines are addresses */
253 ptr = strtok_r(ptr, "\n", &lasts);
254 while (ptr != NULL) {
255 char *pch = strdup(ptr);
256 Put(mortuary, pch, strlen(pch), pch, NULL);
257 ptr = strtok_r(NULL, "\n", &lasts);
267 * Fetch the "mortuary" - a list of dead buddies which we keep around forever
268 * so we can remove them from any client's roster that still has them listed
270 HashList *xmpp_fetch_mortuary(void) {
271 HashList *mortuary = NewHash(1, NULL);
273 syslog(LOG_ALERT, "NewHash() failed!\n");
277 if (CtdlGetRoom(&CC->room, USERCONFIGROOM) != 0) {
278 /* no config room exists - no further processing is required. */
281 CtdlForEachMessage(MSGS_LAST, 1, NULL, XMPPMORTUARY, NULL,
282 xmpp_fetch_mortuary_backend, (void *)mortuary );
290 * Fetch the "mortuary" - a list of dead buddies which we keep around forever
291 * so we can remove them from any client's roster that still has them listed
293 void xmpp_store_mortuary(HashList *mortuary) {
300 themsg = NewStrBuf();
301 StrBufPrintf(themsg, "Content-type: " XMPPMORTUARY "\n"
302 "Content-transfer-encoding: 7bit\n"
306 HashPos = GetNewHashPos(mortuary, 0);
307 while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
309 StrBufAppendPrintf(themsg, "%s\n", (char *)Value);
311 DeleteHashPos(&HashPos);
314 StrBufAppendPrintf(themsg, "foo@bar.com\n");
315 StrBufAppendPrintf(themsg, "baz@quux.com\n");
316 StrBufAppendPrintf(themsg, "haha%c\n", 1);
317 StrBufAppendPrintf(themsg, "baaaz@quux.com\n");
318 StrBufAppendPrintf(themsg, "baaaz@quuuuuux.com\n");
321 /* Delete the old mortuary */
322 CtdlDeleteMessages(USERCONFIGROOM, NULL, 0, XMPPMORTUARY);
324 /* And save the new one to disk */
325 quickie_message("Citadel", NULL, NULL, USERCONFIGROOM, ChrPtr(themsg), 4, "XMPP Mortuary");
332 * Upon logout we make an attempt to delete the whole roster, in order to
333 * try to keep "ghost" buddies from remaining in the client-side roster.
335 * Since the client is probably not still alive, also remember the current
336 * roster for next time so we can delete dead buddies then.
338 void xmpp_massacre_roster(void)
340 struct CitContext *cptr;
342 HashList *mortuary = xmpp_fetch_mortuary();
344 cptr = CtdlGetContextArray(&nContexts);
346 for (i=0; i<nContexts; i++) {
347 if (xmpp_is_visible(&cptr[i], CC)) {
349 char *buddy = strdup(cptr[i].cs_inet_email);
350 Put(mortuary, buddy, strlen(buddy), buddy, NULL);
358 xmpp_store_mortuary(mortuary);
359 DeleteHash(&mortuary);
366 * Stupidly, XMPP does not specify a way to tell the client to flush its client-side roster
367 * and prepare to receive a new one. So instead we remember every buddy we've ever told the
368 * client about, and push delete operations out at the beginning of a session.
370 * We omit any users who happen to be online right now, but we still keep them in the mortuary,
371 * which needs to be maintained as a list of every buddy the user has ever seen. We don't know
372 * when they're connecting from the same client and when they're connecting from a different client,
373 * so we have no guarantee of what is in the client side roster at connect time.
375 void xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster(void)
380 struct CitContext *cptr;
383 HashList *mortuary = xmpp_fetch_mortuary();
384 HashPos *HashPos = GetNewHashPos(mortuary, 0);
386 /* we need to omit anyone who is currently online */
387 cptr = CtdlGetContextArray(&nContexts);
389 /* go through the list of users in the mortuary... */
390 while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
394 if (cptr) for (i=0; i<nContexts; i++) {
395 if (xmpp_is_visible(&cptr[i], CC)) {
396 if (!strcasecmp(cptr[i].cs_inet_email, (char *)Value)) {
403 xmpp_destroy_buddy((char *)Value, 1); /* aggressive presence update */
407 DeleteHashPos(&HashPos);
408 DeleteHash(&mortuary);