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 a particular session is visible to this user
80 int xmpp_is_visible(struct CitContext *cptr) {
81 int aide = (CC->user.axlevel >= AxAideU);
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 */
97 * Initial dump of the entire wholist
99 void xmpp_wholist_presence_dump(void)
101 struct CitContext *cptr = NULL;
104 cptr = CtdlGetContextArray(&nContexts);
109 for (i=0; i<nContexts; i++) {
110 if (xmpp_is_visible(&cptr[i])) {
111 xmpp_indicate_presence(cptr[i].cs_inet_email);
119 * Function to remove a buddy subscription and delete from the roster
120 * (used in several places)
122 void xmpp_destroy_buddy(char *presence_jid) {
123 static int unsolicited_id = 1;
125 /* Transmit non-presence information */
126 cprintf("<presence type=\"unavailable\" from=\"%s\" to=\"%s\"></presence>",
127 presence_jid, XMPP->client_jid
129 cprintf("<presence type=\"unsubscribed\" from=\"%s\" to=\"%s\"></presence>",
130 presence_jid, XMPP->client_jid
132 // FIXME ... we should implement xmpp_indicate_nonpresence so we can use it elsewhere
134 /* Do an unsolicited roster update that deletes the contact. */
135 cprintf("<iq from=\"%s\" to=\"%s\" id=\"unbuddy_%x\" type=\"result\">",
140 cprintf("<query xmlns=\"jabber:iq:roster\">");
141 cprintf("<item jid=\"%s\" subscription=\"remove\">", presence_jid);
142 cprintf("<group>%s</group>", config.c_humannode);
151 * When a user logs in or out of the local Citadel system, notify all XMPP sessions about it.
153 void xmpp_presence_notify(char *presence_jid, int event_type) {
154 struct CitContext *cptr;
155 static int unsolicited_id;
156 int visible_sessions = 0;
159 if (IsEmptyStr(presence_jid)) return;
160 if (CC->kill_me) return;
162 cptr = CtdlGetContextArray(&nContexts);
167 /* Count the visible sessions for this user */
168 for (i=0; i<nContexts; i++) {
169 if (xmpp_is_visible(&cptr[i])) {
174 CtdlLogPrintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
175 visible_sessions, presence_jid, CC->cs_pid);
177 if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
179 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
181 /* Do an unsolicited roster update that adds a new contact. */
182 for (i=0; i<nContexts; i++) {
183 if (xmpp_is_visible(&cptr[i])) {
184 if (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) {
185 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">",
187 cprintf("<query xmlns=\"jabber:iq:roster\">");
188 xmpp_roster_item(&cptr[i]);
195 /* Transmit presence information */
196 xmpp_indicate_presence(presence_jid);
199 if (visible_sessions == 0) {
200 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n", CC->cs_pid, presence_jid);
202 xmpp_destroy_buddy(presence_jid);
209 void xmpp_fetch_mortuary_backend(long msgnum, void *userdata) {
210 HashList *mortuary = (HashList *) userdata;
211 struct CtdlMessage *msg;
215 msg = CtdlFetchMessage(msgnum, 1);
220 /* now add anyone we find into the hashlist */
222 /* skip past the headers */
223 ptr = strstr(msg->cm_fields['M'], "\n\n");
228 ptr = strstr(msg->cm_fields['M'], "\n\r\n");
234 /* the remaining lines are addresses */
236 ptr = strtok_r(ptr, "\n", &lasts);
237 while (ptr != NULL) {
238 char *pch = strdup(ptr);
239 Put(mortuary, pch, strlen(pch), pch, NULL);
240 ptr = strtok_r(NULL, "\n", &lasts);
244 CtdlFreeMessage(msg);
250 * Fetch the "mortuary" - a list of dead buddies which we keep around forever
251 * so we can remove them from any client's roster that still has them listed
253 HashList *xmpp_fetch_mortuary(void) {
254 HashList *mortuary = NewHash(1, NULL);
256 CtdlLogPrintf(CTDL_ALERT, "NewHash() failed!\n");
260 if (CtdlGetRoom(&CC->room, USERCONFIGROOM) != 0) {
261 /* no config room exists - no further processing is required. */
264 CtdlForEachMessage(MSGS_LAST, 1, NULL, XMPPMORTUARY, NULL,
265 xmpp_fetch_mortuary_backend, (void *)mortuary );
273 * Fetch the "mortuary" - a list of dead buddies which we keep around forever
274 * so we can remove them from any client's roster that still has them listed
276 void xmpp_store_mortuary(HashList *mortuary) {
283 themsg = NewStrBuf();
284 StrBufPrintf(themsg, "Content-type: " XMPPMORTUARY "\n"
285 "Content-transfer-encoding: 7bit\n"
289 HashPos = GetNewHashPos(mortuary, 0);
290 while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
292 StrBufAppendPrintf(themsg, "%s\n", (char *)Value);
294 DeleteHashPos(&HashPos);
296 /* Delete the old mortuary */
297 CtdlDeleteMessages(USERCONFIGROOM, NULL, 0, XMPPMORTUARY);
299 /* And save the new one to disk */
300 quickie_message("Citadel", NULL, NULL, USERCONFIGROOM, ChrPtr(themsg), 4, "XMPP Mortuary");
307 * Upon logout we make an attempt to delete the whole roster, in order to
308 * try to keep "ghost" buddies from remaining in the client-side roster.
310 * Since the client is probably not still alive, also remember the current
311 * roster for next time so we can delete dead buddies then.
313 void xmpp_massacre_roster(void)
315 struct CitContext *cptr;
317 HashList *mortuary = xmpp_fetch_mortuary();
319 cptr = CtdlGetContextArray(&nContexts);
321 for (i=0; i<nContexts; i++) {
322 if (xmpp_is_visible(&cptr[i])) {
324 char *buddy = strdup(cptr[i].cs_inet_email);
325 Put(mortuary, buddy, strlen(buddy), buddy, NULL);
333 xmpp_store_mortuary(mortuary);
334 DeleteHash(&mortuary);
341 * Stupidly, XMPP does not specify a way to tell the client to flush its client-side roster
342 * and prepare to receive a new one. So instead we remember every buddy we've ever told the
343 * client about, and push delete operations out at the beginning of a session.
345 * We omit any users who happen to be online right now, but we still keep them in the mortuary,
346 * which needs to be maintained as a list of every buddy the user has ever seen. We don't know
347 * when they're connecting from the same client and when they're connecting from a different client,
348 * so we have no guarantee of what is in the client side roster at connect time.
350 void xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster(void)
355 struct CitContext *cptr;
358 HashList *mortuary = xmpp_fetch_mortuary();
359 HashPos *HashPos = GetNewHashPos(mortuary, 0);
361 /* we need to omit anyone who is currently online */
362 cptr = CtdlGetContextArray(&nContexts);
364 /* go through the list of users in the mortuary... */
365 while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
369 if (cptr) for (i=0; i<nContexts; i++) {
370 if (xmpp_is_visible(&cptr[i])) {
371 if (!strcasecmp(cptr[i].cs_inet_email, (char *)Value)) {
378 xmpp_destroy_buddy((char *)Value);
382 DeleteHashPos(&HashPos);
383 DeleteHash(&mortuary);