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>",
77 * Initial dump of the entire wholist
79 void xmpp_wholist_presence_dump(void)
81 struct CitContext *cptr = NULL;
84 int aide = (CC->user.axlevel >= AxAideU);
86 cptr = CtdlGetContextArray(&nContexts);
91 for (i=0; i<nContexts; i++) {
92 if (cptr[i].logged_in) {
94 (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide)) /* aides see everyone */
95 && (cptr[i].user.usernum != CC->user.usernum) /* don't show myself */
96 && (cptr[i].can_receive_im) /* IM-capable session */
98 xmpp_indicate_presence(cptr[i].cs_inet_email);
107 * Function to remove a buddy subscription and delete from the roster
108 * (used in several places)
110 void xmpp_destroy_buddy(char *presence_jid) {
111 static int unsolicited_id = 1;
113 /* Transmit non-presence information */
114 cprintf("<presence type=\"unavailable\" from=\"%s\" to=\"%s\"></presence>",
115 presence_jid, XMPP->client_jid
117 cprintf("<presence type=\"unsubscribed\" from=\"%s\" to=\"%s\"></presence>",
118 presence_jid, XMPP->client_jid
120 // FIXME ... we should implement xmpp_indicate_nonpresence so we can use it elsewhere
122 /* Do an unsolicited roster update that deletes the contact. */
123 cprintf("<iq from=\"%s\" to=\"%s\" id=\"unbuddy_%x\" type=\"result\">",
128 cprintf("<query xmlns=\"jabber:iq:roster\">");
129 cprintf("<item jid=\"%s\" subscription=\"remove\">", presence_jid);
130 cprintf("<group>%s</group>", config.c_humannode);
139 * When a user logs in or out of the local Citadel system, notify all XMPP sessions about it.
141 void xmpp_presence_notify(char *presence_jid, int event_type) {
142 struct CitContext *cptr;
143 static int unsolicited_id;
144 int visible_sessions = 0;
146 int aide = (CC->user.axlevel >= AxAideU);
148 if (IsEmptyStr(presence_jid)) return;
149 if (CC->kill_me) return;
151 cptr = CtdlGetContextArray(&nContexts);
156 /* Count the visible sessions for this user */
157 for (i=0; i<nContexts; i++) {
158 if (cptr[i].logged_in) {
160 (!strcasecmp(cptr[i].cs_inet_email, presence_jid))
161 && (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
162 && (cptr[i].can_receive_im)
169 CtdlLogPrintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
170 visible_sessions, presence_jid, CC->cs_pid);
172 if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
174 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
176 /* Do an unsolicited roster update that adds a new contact. */
177 for (i=0; i<nContexts; i++) {
178 if (cptr[i].logged_in) {
179 if (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) {
180 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">",
182 cprintf("<query xmlns=\"jabber:iq:roster\">");
183 xmpp_roster_item(&cptr[i]);
190 /* Transmit presence information */
191 xmpp_indicate_presence(presence_jid);
194 if (visible_sessions == 0) {
195 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n", CC->cs_pid, presence_jid);
197 xmpp_destroy_buddy(presence_jid);
204 void xmpp_fetch_mortuary_backend(long msgnum, void *userdata) {
205 HashList *mortuary = (HashList *) userdata;
206 struct CtdlMessage *msg;
207 const char *ptr = NULL;
208 const char *endptr = NULL;
212 msg = CtdlFetchMessage(msgnum, 1);
217 /* now add anyone we find into the hashlist */
219 ptr = msg->cm_fields['M'];
220 endptr = ptr + strlen(ptr); // only do strlen once :)
221 while (ptr = memreadline(ptr, buf, (sizeof buf - 2)), ((ptr < endptr) && (*ptr != 0)) ) {
223 Put(mortuary, buf, strlen(buf), buf, generic_free_handler);
225 if (IsEmptyStr(buf)) in_body = 1;
228 CtdlFreeMessage(msg);
234 * Fetch the "mortuary" - a list of dead buddies which we keep around forever
235 * so we can remove them from any client's roster that still has them listed
237 HashList *xmpp_fetch_mortuary(void) {
238 HashList *mortuary = NewHash(1, NULL);
240 CtdlLogPrintf(CTDL_ALERT, "NewHash() failed!\n");
244 if (CtdlGetRoom(&CC->room, USERCONFIGROOM) != 0) {
245 /* no config room exists - no further processing is required. */
248 CtdlForEachMessage(MSGS_LAST, 1, NULL, XMPPMORTUARY, NULL,
249 xmpp_fetch_mortuary_backend, (void *)mortuary );
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 void xmpp_store_mortuary(HashList *mortuary) {
267 themsg = NewStrBuf();
268 StrBufPrintf(themsg, "Content-type: " XMPPMORTUARY "\n"
269 "Content-transfer-encoding: 7bit\n"
273 HashPos = GetNewHashPos(mortuary, 0);
274 while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
276 StrBufAppendPrintf(themsg, "%s\n", (char *)Value);
278 DeleteHashPos(&HashPos);
280 /* Delete the old mortuary */
281 CtdlDeleteMessages(USERCONFIGROOM, NULL, 0, XMPPMORTUARY);
283 /* And save the new one to disk */
284 quickie_message("Citadel", NULL, NULL, USERCONFIGROOM, ChrPtr(themsg), 4, "XMPP Mortuary");
291 * Upon logout we make an attempt to delete the whole roster, in order to
292 * try to keep "ghost" buddies from remaining in the client-side roster.
294 * Since the client is probably not still alive, also remember the current
295 * roster for next time so we can delete dead buddies then.
297 void xmpp_massacre_roster(void)
299 struct CitContext *cptr;
301 int aide = (CC->user.axlevel >= AxAideU);
302 HashList *mortuary = xmpp_fetch_mortuary();
304 cptr = CtdlGetContextArray(&nContexts);
306 for (i=0; i<nContexts; i++) {
307 if (cptr[i].logged_in) {
309 (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
310 && (cptr[i].user.usernum != CC->user.usernum)
312 xmpp_destroy_buddy(cptr[i].cs_inet_email);
314 char *buddy = strdup(cptr[i].cs_inet_email);
315 Put(mortuary, buddy, strlen(buddy),
316 buddy, generic_free_handler);
325 xmpp_store_mortuary(mortuary);
326 DeleteHash(&mortuary);