4 * Handle XMPP presence exchanges
6 * Copyright (c) 2007-2010 by Art Cancro
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.
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.
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
32 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
51 #include <libcitadel.h>
54 #include "citserver.h"
57 #include "internet_addressing.h"
59 #include "ctdl_module.h"
60 #include "serv_xmpp.h"
65 * Indicate the presence of another user to the client
66 * (used in several places)
68 void xmpp_indicate_presence(char *presence_jid)
70 cprintf("<presence from=\"%s\" to=\"%s\"></presence>",
79 * Convenience function to determine whether any given session is 'visible' to any other given session,
80 * and is capable of receiving instant messages from that session.
82 int xmpp_is_visible(struct CitContext *cptr, struct CitContext *to_whom) {
83 int aide = (to_whom->user.axlevel >= AxAideU);
85 if ( (cptr->logged_in)
86 && (((cptr->cs_flags&CS_STEALTH)==0) || (aide)) /* aides see everyone */
87 && (cptr->user.usernum != to_whom->user.usernum) /* don't show myself */
88 && (cptr->can_receive_im) /* IM-capable session */
99 * Initial dump of the entire wholist
101 void xmpp_wholist_presence_dump(void)
103 struct CitContext *cptr = NULL;
106 cptr = CtdlGetContextArray(&nContexts);
111 for (i=0; i<nContexts; i++) {
112 if (xmpp_is_visible(&cptr[i], CC)) {
113 xmpp_indicate_presence(cptr[i].cs_inet_email);
121 * Function to remove a buddy subscription and delete from the roster
122 * (used in several places)
124 void xmpp_destroy_buddy(char *presence_jid) {
125 static int unsolicited_id = 1;
127 if (!presence_jid) return;
129 if (!XMPP->client_jid) return;
131 /* Transmit non-presence information */
132 cprintf("<presence type=\"unavailable\" from=\"%s\" to=\"%s\"></presence>",
133 presence_jid, XMPP->client_jid
135 cprintf("<presence type=\"unsubscribed\" from=\"%s\" to=\"%s\"></presence>",
136 presence_jid, XMPP->client_jid
138 // FIXME ... we should implement xmpp_indicate_nonpresence so we can use it elsewhere
140 /* Do an unsolicited roster update that deletes the contact. */
141 cprintf("<iq from=\"%s\" to=\"%s\" id=\"unbuddy_%x\" type=\"result\">",
146 cprintf("<query xmlns=\"jabber:iq:roster\">");
147 cprintf("<item jid=\"%s\" subscription=\"remove\">", presence_jid);
148 cprintf("<group>%s</group>", config.c_humannode);
157 * When a user logs in or out of the local Citadel system, notify all XMPP sessions about it.
158 * THIS FUNCTION HAS A BUG IN IT THAT ENUMERATES THE SESSIONS WRONG.
160 void xmpp_presence_notify(char *presence_jid, int event_type) {
161 struct CitContext *cptr;
162 static int unsolicited_id;
163 int visible_sessions = 0;
165 int which_cptr_is_relevant = (-1);
167 if (IsEmptyStr(presence_jid)) return;
168 if (CC->kill_me) return;
170 cptr = CtdlGetContextArray(&nContexts);
175 /* Count the visible sessions for this user */
176 for (i=0; i<nContexts; i++) {
177 if ( (!strcasecmp(cptr[i].cs_inet_email, presence_jid))
178 && (xmpp_is_visible(&cptr[i], CC))
181 which_cptr_is_relevant = i;
185 CtdlLogPrintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
186 visible_sessions, presence_jid, CC->cs_pid);
188 if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
190 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n",
191 CC->cs_pid, presence_jid);
193 /* Do an unsolicited roster update that adds a new contact. */
194 assert(which_cptr_is_relevant >= 0);
195 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">", ++unsolicited_id);
196 cprintf("<query xmlns=\"jabber:iq:roster\">");
197 xmpp_roster_item(&cptr[which_cptr_is_relevant]);
198 cprintf("</query></iq>");
200 /* Transmit presence information */
201 xmpp_indicate_presence(presence_jid);
204 if (visible_sessions == 0) {
205 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n",
206 CC->cs_pid, presence_jid);
207 xmpp_destroy_buddy(presence_jid);
215 void xmpp_fetch_mortuary_backend(long msgnum, void *userdata) {
216 HashList *mortuary = (HashList *) userdata;
217 struct CtdlMessage *msg;
221 msg = CtdlFetchMessage(msgnum, 1);
226 /* now add anyone we find into the hashlist */
228 /* skip past the headers */
229 ptr = strstr(msg->cm_fields['M'], "\n\n");
234 ptr = strstr(msg->cm_fields['M'], "\n\r\n");
240 /* the remaining lines are addresses */
242 ptr = strtok_r(ptr, "\n", &lasts);
243 while (ptr != NULL) {
244 char *pch = strdup(ptr);
245 Put(mortuary, pch, strlen(pch), pch, NULL);
246 ptr = strtok_r(NULL, "\n", &lasts);
250 CtdlFreeMessage(msg);
256 * Fetch the "mortuary" - a list of dead buddies which we keep around forever
257 * so we can remove them from any client's roster that still has them listed
259 HashList *xmpp_fetch_mortuary(void) {
260 HashList *mortuary = NewHash(1, NULL);
262 CtdlLogPrintf(CTDL_ALERT, "NewHash() failed!\n");
266 if (CtdlGetRoom(&CC->room, USERCONFIGROOM) != 0) {
267 /* no config room exists - no further processing is required. */
270 CtdlForEachMessage(MSGS_LAST, 1, NULL, XMPPMORTUARY, NULL,
271 xmpp_fetch_mortuary_backend, (void *)mortuary );
279 * Fetch the "mortuary" - a list of dead buddies which we keep around forever
280 * so we can remove them from any client's roster that still has them listed
282 void xmpp_store_mortuary(HashList *mortuary) {
289 themsg = NewStrBuf();
290 StrBufPrintf(themsg, "Content-type: " XMPPMORTUARY "\n"
291 "Content-transfer-encoding: 7bit\n"
295 HashPos = GetNewHashPos(mortuary, 0);
296 while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
298 StrBufAppendPrintf(themsg, "%s\n", (char *)Value);
300 DeleteHashPos(&HashPos);
302 /* Delete the old mortuary */
303 CtdlDeleteMessages(USERCONFIGROOM, NULL, 0, XMPPMORTUARY);
305 /* And save the new one to disk */
306 quickie_message("Citadel", NULL, NULL, USERCONFIGROOM, ChrPtr(themsg), 4, "XMPP Mortuary");
313 * Upon logout we make an attempt to delete the whole roster, in order to
314 * try to keep "ghost" buddies from remaining in the client-side roster.
316 * Since the client is probably not still alive, also remember the current
317 * roster for next time so we can delete dead buddies then.
319 void xmpp_massacre_roster(void)
321 struct CitContext *cptr;
323 HashList *mortuary = xmpp_fetch_mortuary();
325 cptr = CtdlGetContextArray(&nContexts);
327 for (i=0; i<nContexts; i++) {
328 if (xmpp_is_visible(&cptr[i], CC)) {
330 char *buddy = strdup(cptr[i].cs_inet_email);
331 Put(mortuary, buddy, strlen(buddy), buddy, NULL);
339 xmpp_store_mortuary(mortuary);
340 DeleteHash(&mortuary);
347 * Stupidly, XMPP does not specify a way to tell the client to flush its client-side roster
348 * and prepare to receive a new one. So instead we remember every buddy we've ever told the
349 * client about, and push delete operations out at the beginning of a session.
351 * We omit any users who happen to be online right now, but we still keep them in the mortuary,
352 * which needs to be maintained as a list of every buddy the user has ever seen. We don't know
353 * when they're connecting from the same client and when they're connecting from a different client,
354 * so we have no guarantee of what is in the client side roster at connect time.
356 void xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster(void)
361 struct CitContext *cptr;
364 HashList *mortuary = xmpp_fetch_mortuary();
365 HashPos *HashPos = GetNewHashPos(mortuary, 0);
367 /* we need to omit anyone who is currently online */
368 cptr = CtdlGetContextArray(&nContexts);
370 /* go through the list of users in the mortuary... */
371 while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
375 if (cptr) for (i=0; i<nContexts; i++) {
376 if (xmpp_is_visible(&cptr[i], CC)) {
377 if (!strcasecmp(cptr[i].cs_inet_email, (char *)Value)) {
384 xmpp_destroy_buddy((char *)Value);
388 DeleteHashPos(&HashPos);
389 DeleteHash(&mortuary);