2 * Handle XMPP presence exchanges
4 * Copyright (c) 2007-2010 by Art Cancro
6 * This program is free 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 cprintf("<presence from=\"%s\" ", xmlesc(xmlbuf, presence_jid, sizeof xmlbuf));
71 cprintf("to=\"%s\"></presence>", xmlesc(xmlbuf, XMPP->client_jid, sizeof xmlbuf));
77 * Convenience function to determine whether any given session is 'visible' to any other given session,
78 * and is capable of receiving instant messages from that session.
80 int xmpp_is_visible(struct CitContext *cptr, struct CitContext *to_whom) {
81 int aide = (to_whom->user.axlevel >= AxAideU);
83 if ( (cptr->logged_in)
84 && (((cptr->cs_flags&CS_STEALTH)==0) || (aide)) /* aides see everyone */
85 && (cptr->user.usernum != to_whom->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], CC)) {
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;
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 xmlesc(xmlbuf1, presence_jid, sizeof xmlbuf1),
134 xmlesc(xmlbuf2, XMPP->client_jid, sizeof xmlbuf2)
136 cprintf("<presence type=\"unsubscribed\" from=\"%s\" to=\"%s\"></presence>",
137 xmlesc(xmlbuf1, presence_jid, sizeof xmlbuf1),
138 xmlesc(xmlbuf2, XMPP->client_jid, sizeof xmlbuf2)
140 // FIXME ... we should implement xmpp_indicate_nonpresence so we can use it elsewhere
142 /* Do an unsolicited roster update that deletes the contact. */
143 cprintf("<iq from=\"%s\" to=\"%s\" id=\"unbuddy_%x\" type=\"result\">",
144 xmlesc(xmlbuf1, CC->cs_inet_email, sizeof xmlbuf1),
145 xmlesc(xmlbuf2, XMPP->client_jid, sizeof xmlbuf2),
148 cprintf("<query xmlns=\"jabber:iq:roster\">");
149 cprintf("<item jid=\"%s\" subscription=\"remove\">", xmlesc(xmlbuf1, presence_jid, sizeof xmlbuf1));
150 cprintf("<group>%s</group>", xmlesc(xmlbuf1, config.c_humannode, sizeof xmlbuf1));
159 * When a user logs in or out of the local Citadel system, notify all XMPP sessions about it.
160 * THIS FUNCTION HAS A BUG IN IT THAT ENUMERATES THE SESSIONS WRONG.
162 void xmpp_presence_notify(char *presence_jid, int event_type) {
163 struct CitContext *cptr;
164 static int unsolicited_id;
165 int visible_sessions = 0;
167 int which_cptr_is_relevant = (-1);
169 if (IsEmptyStr(presence_jid)) return;
170 if (CC->kill_me) return;
172 cptr = CtdlGetContextArray(&nContexts);
177 /* Count the visible sessions for this user */
178 for (i=0; i<nContexts; i++) {
179 if ( (!strcasecmp(cptr[i].cs_inet_email, presence_jid))
180 && (xmpp_is_visible(&cptr[i], CC))
183 which_cptr_is_relevant = i;
187 syslog(LOG_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
188 visible_sessions, presence_jid, CC->cs_pid);
190 if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
192 syslog(LOG_DEBUG, "Telling session %d that <%s> logged in\n",
193 CC->cs_pid, presence_jid);
195 /* Do an unsolicited roster update that adds a new contact. */
196 assert(which_cptr_is_relevant >= 0);
197 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">", ++unsolicited_id);
198 cprintf("<query xmlns=\"jabber:iq:roster\">");
199 xmpp_roster_item(&cptr[which_cptr_is_relevant]);
200 cprintf("</query></iq>");
202 /* Transmit presence information */
203 xmpp_indicate_presence(presence_jid);
206 if (visible_sessions == 0) {
207 syslog(LOG_DEBUG, "Telling session %d that <%s> logged out\n",
208 CC->cs_pid, presence_jid);
209 xmpp_destroy_buddy(presence_jid);
217 void xmpp_fetch_mortuary_backend(long msgnum, void *userdata) {
218 HashList *mortuary = (HashList *) userdata;
219 struct CtdlMessage *msg;
223 msg = CtdlFetchMessage(msgnum, 1);
228 /* now add anyone we find into the hashlist */
230 /* skip past the headers */
231 ptr = strstr(msg->cm_fields['M'], "\n\n");
236 ptr = strstr(msg->cm_fields['M'], "\n\r\n");
242 /* the remaining lines are addresses */
244 ptr = strtok_r(ptr, "\n", &lasts);
245 while (ptr != NULL) {
246 char *pch = strdup(ptr);
247 Put(mortuary, pch, strlen(pch), pch, NULL);
248 ptr = strtok_r(NULL, "\n", &lasts);
252 CtdlFreeMessage(msg);
258 * Fetch the "mortuary" - a list of dead buddies which we keep around forever
259 * so we can remove them from any client's roster that still has them listed
261 HashList *xmpp_fetch_mortuary(void) {
262 HashList *mortuary = NewHash(1, NULL);
264 syslog(LOG_ALERT, "NewHash() failed!\n");
268 if (CtdlGetRoom(&CC->room, USERCONFIGROOM) != 0) {
269 /* no config room exists - no further processing is required. */
272 CtdlForEachMessage(MSGS_LAST, 1, NULL, XMPPMORTUARY, NULL,
273 xmpp_fetch_mortuary_backend, (void *)mortuary );
281 * Fetch the "mortuary" - a list of dead buddies which we keep around forever
282 * so we can remove them from any client's roster that still has them listed
284 void xmpp_store_mortuary(HashList *mortuary) {
291 themsg = NewStrBuf();
292 StrBufPrintf(themsg, "Content-type: " XMPPMORTUARY "\n"
293 "Content-transfer-encoding: 7bit\n"
297 HashPos = GetNewHashPos(mortuary, 0);
298 while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
300 StrBufAppendPrintf(themsg, "%s\n", (char *)Value);
302 DeleteHashPos(&HashPos);
305 StrBufAppendPrintf(themsg, "foo@bar.com\n");
306 StrBufAppendPrintf(themsg, "baz@quux.com\n");
307 StrBufAppendPrintf(themsg, "haha%c\n", 1);
308 StrBufAppendPrintf(themsg, "baaaz@quux.com\n");
309 StrBufAppendPrintf(themsg, "baaaz@quuuuuux.com\n");
312 /* Delete the old mortuary */
313 CtdlDeleteMessages(USERCONFIGROOM, NULL, 0, XMPPMORTUARY);
315 /* And save the new one to disk */
316 quickie_message("Citadel", NULL, NULL, USERCONFIGROOM, ChrPtr(themsg), 4, "XMPP Mortuary");
323 * Upon logout we make an attempt to delete the whole roster, in order to
324 * try to keep "ghost" buddies from remaining in the client-side roster.
326 * Since the client is probably not still alive, also remember the current
327 * roster for next time so we can delete dead buddies then.
329 void xmpp_massacre_roster(void)
331 struct CitContext *cptr;
333 HashList *mortuary = xmpp_fetch_mortuary();
335 cptr = CtdlGetContextArray(&nContexts);
337 for (i=0; i<nContexts; i++) {
338 if (xmpp_is_visible(&cptr[i], CC)) {
340 char *buddy = strdup(cptr[i].cs_inet_email);
341 Put(mortuary, buddy, strlen(buddy), buddy, NULL);
349 xmpp_store_mortuary(mortuary);
350 DeleteHash(&mortuary);
357 * Stupidly, XMPP does not specify a way to tell the client to flush its client-side roster
358 * and prepare to receive a new one. So instead we remember every buddy we've ever told the
359 * client about, and push delete operations out at the beginning of a session.
361 * We omit any users who happen to be online right now, but we still keep them in the mortuary,
362 * which needs to be maintained as a list of every buddy the user has ever seen. We don't know
363 * when they're connecting from the same client and when they're connecting from a different client,
364 * so we have no guarantee of what is in the client side roster at connect time.
366 void xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster(void)
371 struct CitContext *cptr;
374 HashList *mortuary = xmpp_fetch_mortuary();
375 HashPos *HashPos = GetNewHashPos(mortuary, 0);
377 /* we need to omit anyone who is currently online */
378 cptr = CtdlGetContextArray(&nContexts);
380 /* go through the list of users in the mortuary... */
381 while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
385 if (cptr) for (i=0; i<nContexts; i++) {
386 if (xmpp_is_visible(&cptr[i], CC)) {
387 if (!strcasecmp(cptr[i].cs_inet_email, (char *)Value)) {
394 xmpp_destroy_buddy((char *)Value);
398 DeleteHashPos(&HashPos);
399 DeleteHash(&mortuary);