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>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
50 #include <libcitadel.h>
53 #include "citserver.h"
56 #include "internet_addressing.h"
58 #include "ctdl_module.h"
59 #include "serv_xmpp.h"
64 * Indicate the presence of another user to the client
65 * (used in several places)
67 void xmpp_indicate_presence(char *presence_jid)
69 cprintf("<presence from=\"%s\" to=\"%s\"></presence>",
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) {
124 static int unsolicited_id = 1;
126 if (!presence_jid) return;
128 if (!XMPP->client_jid) return;
130 CtdlLogPrintf(CTDL_DEBUG, "\033[31mdestroy_buddy(%s)\033[0m\n", presence_jid);
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;
166 if (IsEmptyStr(presence_jid)) return;
167 if (CC->kill_me) return;
169 cptr = CtdlGetContextArray(&nContexts);
174 /* Count the visible sessions for this user */
175 for (i=0; i<nContexts; i++) {
176 if (xmpp_is_visible(CC, &cptr[i])) {
181 CtdlLogPrintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
182 visible_sessions, presence_jid, CC->cs_pid);
184 if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
186 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
188 /* Do an unsolicited roster update that adds a new contact. */
189 for (i=0; i<nContexts; i++) {
190 if (xmpp_is_visible(CC, &cptr[i])) {
191 if (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) {
192 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">",
194 cprintf("<query xmlns=\"jabber:iq:roster\">");
195 xmpp_roster_item(&cptr[i]);
202 /* Transmit presence information */
203 xmpp_indicate_presence(presence_jid);
206 if (visible_sessions == 0) {
207 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n", CC->cs_pid, presence_jid);
209 xmpp_destroy_buddy(presence_jid);
216 void xmpp_fetch_mortuary_backend(long msgnum, void *userdata) {
217 HashList *mortuary = (HashList *) userdata;
218 struct CtdlMessage *msg;
222 msg = CtdlFetchMessage(msgnum, 1);
227 /* now add anyone we find into the hashlist */
229 /* skip past the headers */
230 ptr = strstr(msg->cm_fields['M'], "\n\n");
235 ptr = strstr(msg->cm_fields['M'], "\n\r\n");
241 /* the remaining lines are addresses */
243 ptr = strtok_r(ptr, "\n", &lasts);
244 while (ptr != NULL) {
245 char *pch = strdup(ptr);
246 Put(mortuary, pch, strlen(pch), pch, NULL);
247 ptr = strtok_r(NULL, "\n", &lasts);
251 CtdlFreeMessage(msg);
257 * Fetch the "mortuary" - a list of dead buddies which we keep around forever
258 * so we can remove them from any client's roster that still has them listed
260 HashList *xmpp_fetch_mortuary(void) {
261 HashList *mortuary = NewHash(1, NULL);
263 CtdlLogPrintf(CTDL_ALERT, "NewHash() failed!\n");
267 if (CtdlGetRoom(&CC->room, USERCONFIGROOM) != 0) {
268 /* no config room exists - no further processing is required. */
271 CtdlForEachMessage(MSGS_LAST, 1, NULL, XMPPMORTUARY, NULL,
272 xmpp_fetch_mortuary_backend, (void *)mortuary );
280 * Fetch the "mortuary" - a list of dead buddies which we keep around forever
281 * so we can remove them from any client's roster that still has them listed
283 void xmpp_store_mortuary(HashList *mortuary) {
290 themsg = NewStrBuf();
291 StrBufPrintf(themsg, "Content-type: " XMPPMORTUARY "\n"
292 "Content-transfer-encoding: 7bit\n"
296 HashPos = GetNewHashPos(mortuary, 0);
297 while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
299 StrBufAppendPrintf(themsg, "%s\n", (char *)Value);
301 DeleteHashPos(&HashPos);
303 /* Delete the old mortuary */
304 CtdlDeleteMessages(USERCONFIGROOM, NULL, 0, XMPPMORTUARY);
306 /* And save the new one to disk */
307 quickie_message("Citadel", NULL, NULL, USERCONFIGROOM, ChrPtr(themsg), 4, "XMPP Mortuary");
314 * Upon logout we make an attempt to delete the whole roster, in order to
315 * try to keep "ghost" buddies from remaining in the client-side roster.
317 * Since the client is probably not still alive, also remember the current
318 * roster for next time so we can delete dead buddies then.
320 void xmpp_massacre_roster(void)
322 struct CitContext *cptr;
324 HashList *mortuary = xmpp_fetch_mortuary();
326 cptr = CtdlGetContextArray(&nContexts);
328 for (i=0; i<nContexts; i++) {
329 if (xmpp_is_visible(&cptr[i], CC)) {
331 char *buddy = strdup(cptr[i].cs_inet_email);
332 Put(mortuary, buddy, strlen(buddy), buddy, NULL);
340 xmpp_store_mortuary(mortuary);
341 DeleteHash(&mortuary);
348 * Stupidly, XMPP does not specify a way to tell the client to flush its client-side roster
349 * and prepare to receive a new one. So instead we remember every buddy we've ever told the
350 * client about, and push delete operations out at the beginning of a session.
352 * We omit any users who happen to be online right now, but we still keep them in the mortuary,
353 * which needs to be maintained as a list of every buddy the user has ever seen. We don't know
354 * when they're connecting from the same client and when they're connecting from a different client,
355 * so we have no guarantee of what is in the client side roster at connect time.
357 void xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster(void)
362 struct CitContext *cptr;
365 HashList *mortuary = xmpp_fetch_mortuary();
366 HashPos *HashPos = GetNewHashPos(mortuary, 0);
368 /* we need to omit anyone who is currently online */
369 cptr = CtdlGetContextArray(&nContexts);
371 /* go through the list of users in the mortuary... */
372 while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
376 if (cptr) for (i=0; i<nContexts; i++) {
377 if (xmpp_is_visible(&cptr[i], CC)) {
378 if (!strcasecmp(cptr[i].cs_inet_email, (char *)Value)) {
385 xmpp_destroy_buddy((char *)Value);
389 DeleteHashPos(&HashPos);
390 DeleteHash(&mortuary);