citadel
About: Citadel is an advanced messaging and collaboration system for groupware and BBS applications (preferred OS: Linux).
  Fossies Dox: citadel.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
xmpp_presence.c
Go to the documentation of this file.
1/*
2 * Handle XMPP presence exchanges
3 *
4 * Copyright (c) 2007-2021 by Art Cancro and citadel.org
5 *
6 * This program is open source 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.
10 *
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.
15 *
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
19 *
20 */
21
22#include "../../sysdep.h"
23#include <stdlib.h>
24#include <unistd.h>
25#include <stdio.h>
26#include <fcntl.h>
27#include <signal.h>
28#include <pwd.h>
29#include <errno.h>
30#include <sys/types.h>
31#include <assert.h>
32#include <time.h>
33#include <sys/wait.h>
34#include <string.h>
35#include <limits.h>
36#include <ctype.h>
37#include <expat.h>
38#include <libcitadel.h>
39#include "../../citadel_defs.h"
40#include "../../server.h"
41#include "../../citserver.h"
42#include "../../support.h"
43#include "../../config.h"
44#include "../../internet_addressing.h"
45#include "../../ctdl_module.h"
46#include "serv_xmpp.h"
47
48
49/*
50 * Indicate the presence of another user to the client
51 * (used in several places)
52 */
53void xmpp_indicate_presence(char *presence_jid) {
54 char xmlbuf[256];
55
56 syslog(LOG_DEBUG, "xmpp: indicating presence of <%s> to <%s>", presence_jid, XMPP->client_jid);
57 cprintf("<presence from=\"%s\" ", xmlesc(xmlbuf, presence_jid, sizeof xmlbuf));
58 cprintf("to=\"%s\"></presence>", xmlesc(xmlbuf, XMPP->client_jid, sizeof xmlbuf));
59}
60
61
62/*
63 * Convenience function to determine whether any given session is 'visible' to any other given session,
64 * and is capable of receiving instant messages from that session.
65 */
66int xmpp_is_visible(struct CitContext *cptr, struct CitContext *to_whom) {
67 int aide = (to_whom->user.axlevel >= AxAideU);
68
69 if ( (cptr->logged_in)
70 && (((cptr->cs_flags&CS_STEALTH)==0) || (aide)) /* aides see everyone */
71 && (cptr->user.usernum != to_whom->user.usernum) /* don't show myself */
72 && (cptr->can_receive_im) /* IM-capable session */
73 ) {
74 return(1);
75 }
76 else {
77 return(0);
78 }
79}
80
81
82/*
83 * Initial dump of the entire wholist
84 */
86 struct CitContext *cptr = NULL;
87 int nContexts, i;
88
89 cptr = CtdlGetContextArray(&nContexts);
90 if (!cptr) {
91 return;
92 }
93
94 for (i=0; i<nContexts; i++) {
95 if (xmpp_is_visible(&cptr[i], CC)) {
97 }
98 }
99 free(cptr);
100}
101
102
103/*
104 * Function to remove a buddy subscription and delete from the roster
105 * (used in several places)
106 */
107void xmpp_destroy_buddy(char *presence_jid, int aggressively) {
108 static int unsolicited_id = 1;
109 char xmlbuf1[256];
110 char xmlbuf2[256];
111
112 if (!presence_jid) return;
113 if (!XMPP) return;
114 if (!XMPP->client_jid) return;
115
116 /* Transmit non-presence information */
117 cprintf("<presence type=\"unavailable\" from=\"%s\" to=\"%s\"></presence>",
118 xmlesc(xmlbuf1, presence_jid, sizeof xmlbuf1),
119 xmlesc(xmlbuf2, XMPP->client_jid, sizeof xmlbuf2)
120 );
121
122 /*
123 * Setting the "aggressively" flag also sends an "unsubscribed" presence update.
124 * We only ask for this when flushing the client side roster, because if we do it
125 * in the middle of a session when another user logs off, some clients (Jitsi) interpret
126 * it as a rejection of a subscription request.
127 */
128 if (aggressively) {
129 cprintf("<presence type=\"unsubscribed\" from=\"%s\" to=\"%s\"></presence>",
130 xmlesc(xmlbuf1, presence_jid, sizeof xmlbuf1),
131 xmlesc(xmlbuf2, XMPP->client_jid, sizeof xmlbuf2)
132 );
133 }
134
135 // note: we should implement xmpp_indicate_nonpresence so we can use it elsewhere
136
137 /* Do an unsolicited roster update that deletes the contact. */
138 cprintf("<iq from=\"%s\" to=\"%s\" id=\"unbuddy_%x\" type=\"result\">",
139 xmlesc(xmlbuf1, CC->cs_principal_id, sizeof xmlbuf1),
140 xmlesc(xmlbuf2, XMPP->client_jid, sizeof xmlbuf2),
141 ++unsolicited_id
142 );
143 cprintf("<query xmlns=\"jabber:iq:roster\">");
144 cprintf("<item jid=\"%s\" subscription=\"remove\">", xmlesc(xmlbuf1, presence_jid, sizeof xmlbuf1));
145 cprintf("<group>%s</group>", xmlesc(xmlbuf1, CtdlGetConfigStr("c_humannode"), sizeof xmlbuf1));
146 cprintf("</item>");
147 cprintf("</query>"
148 "</iq>"
149 );
150}
151
152
153/*
154 * When a user logs in or out of the local Citadel system, notify all XMPP sessions about it.
155 * THIS FUNCTION HAS A BUG IN IT THAT ENUMERATES THE SESSIONS WRONG.
156 */
157void xmpp_presence_notify(char *presence_jid, int event_type) {
158 struct CitContext *cptr;
159 static int unsolicited_id = 12345;
160 int visible_sessions = 0;
161 int nContexts, i;
162 int which_cptr_is_relevant = (-1);
163
164 if (IsEmptyStr(presence_jid)) return;
165 if (CC->kill_me) return;
166
167 cptr = CtdlGetContextArray(&nContexts);
168 if (!cptr) {
169 return;
170 }
171
172 /* Count the visible sessions for this user */
173 for (i=0; i<nContexts; i++) {
174 if ( (!strcasecmp(cptr[i].cs_principal_id, presence_jid))
175 && (xmpp_is_visible(&cptr[i], CC))
176 ) {
177 ++visible_sessions;
178 which_cptr_is_relevant = i;
179 }
180 }
181
182 syslog(LOG_DEBUG, "xmpp: %d sessions for <%s> are now visible to session %d", visible_sessions, presence_jid, CC->cs_pid);
183
184 if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
185
186 syslog(LOG_DEBUG, "xmpp: telling session %d that <%s> logged in", CC->cs_pid, presence_jid);
187
188 /* Do an unsolicited roster update that adds a new contact. */
189 assert(which_cptr_is_relevant >= 0);
190 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">", ++unsolicited_id);
191 cprintf("<query xmlns=\"jabber:iq:roster\">");
192 xmpp_roster_item(&cptr[which_cptr_is_relevant]);
193 cprintf("</query></iq>");
194
195 /* Transmit presence information */
196 xmpp_indicate_presence(presence_jid);
197 }
198
199 if (visible_sessions == 0) {
200 syslog(LOG_DEBUG, "xmpp: telling session %d that <%s> logged out", CC->cs_pid, presence_jid);
201 xmpp_destroy_buddy(presence_jid, 0); /* non aggressive presence update */
202 }
203
204 free(cptr);
205}
206
207
208void xmpp_fetch_mortuary_backend(long msgnum, void *userdata) {
209 HashList *mortuary = (HashList *) userdata;
210 struct CtdlMessage *msg;
211 char *ptr = NULL;
212 char *lasts = NULL;
213
214 msg = CtdlFetchMessage(msgnum, 1);
215 if (msg == NULL) {
216 return;
217 }
218
219 /* now add anyone we find into the hashlist */
220
221 /* skip past the headers */
222 ptr = strstr(msg->cm_fields[eMesageText], "\n\n");
223 if (ptr != NULL) {
224 ptr += 2;
225 }
226 else {
227 ptr = strstr(msg->cm_fields[eMesageText], "\n\r\n");
228 if (ptr != NULL) {
229 ptr += 3;
230 }
231 }
232
233 /* the remaining lines are addresses */
234 if (ptr != NULL) {
235 ptr = strtok_r(ptr, "\n", &lasts);
236 while (ptr != NULL) {
237 char *pch = strdup(ptr);
238 Put(mortuary, pch, strlen(pch), pch, NULL);
239 ptr = strtok_r(NULL, "\n", &lasts);
240 }
241 }
242
243 CM_Free(msg);
244}
245
246
247/*
248 * Fetch the "mortuary" - a list of dead buddies which we keep around forever
249 * so we can remove them from any client's roster that still has them listed
250 */
251HashList *xmpp_fetch_mortuary(void) {
252 HashList *mortuary = NewHash(1, NULL);
253 if (!mortuary) {
254 syslog(LOG_ALERT, "xmpp: NewHash() failed!");
255 return(NULL);
256 }
257
258 if (CtdlGetRoom(&CC->room, USERCONFIGROOM) != 0) {
259 /* no config room exists - no further processing is required. */
260 return(mortuary);
261 }
262 CtdlForEachMessage(MSGS_LAST, 1, NULL, XMPPMORTUARY, NULL,
263 xmpp_fetch_mortuary_backend, (void *)mortuary );
264
265 return(mortuary);
266}
267
268
269/*
270 * Fetch the "mortuary" - a list of dead buddies which we keep around forever
271 * so we can remove them from any client's roster that still has them listed
272 */
273void xmpp_store_mortuary(HashList *mortuary) {
274 HashPos *HashPos;
275 long len;
276 void *Value;
277 const char *Key;
278 StrBuf *themsg;
279
280 themsg = NewStrBuf();
281 StrBufPrintf(themsg, "Content-type: " XMPPMORTUARY "\n"
282 "Content-transfer-encoding: 7bit\n"
283 "\n"
284 );
285
286 HashPos = GetNewHashPos(mortuary, 0);
287 while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0) {
288 StrBufAppendPrintf(themsg, "%s\n", (char *)Value);
289 }
290 DeleteHashPos(&HashPos);
291
292 /* Delete the old mortuary */
293 CtdlDeleteMessages(USERCONFIGROOM, NULL, 0, XMPPMORTUARY);
294
295 /* And save the new one to disk */
296 quickie_message(CC->user.fullname, NULL, NULL, USERCONFIGROOM, ChrPtr(themsg), 4, "XMPP Mortuary");
297 FreeStrBuf(&themsg);
298}
299
300
301/*
302 * Upon logout we make an attempt to delete the whole roster, in order to
303 * try to keep "ghost" buddies from remaining in the client-side roster.
304 *
305 * Since the client is probably not still alive, also remember the current
306 * roster for next time so we can delete dead buddies then.
307 */
309 struct CitContext *cptr;
310 int nContexts, i;
311 HashList *mortuary = xmpp_fetch_mortuary();
312
313 cptr = CtdlGetContextArray(&nContexts);
314 if (cptr) {
315 for (i=0; i<nContexts; i++) {
316 if (xmpp_is_visible(&cptr[i], CC)) {
317 if (mortuary) {
318 char *buddy = strdup(cptr[i].cs_principal_id);
319 Put(mortuary, buddy, strlen(buddy), buddy, NULL);
320 }
321 }
322 }
323 free (cptr);
324 }
325
326 if (mortuary) {
327 xmpp_store_mortuary(mortuary);
328 DeleteHash(&mortuary);
329 }
330}
331
332
333/*
334 * Stupidly, XMPP does not specify a way to tell the client to flush its client-side roster
335 * and prepare to receive a new one. So instead we remember every buddy we've ever told the
336 * client about, and push delete operations out at the beginning of a session.
337 *
338 * We omit any users who happen to be online right now, but we still keep them in the mortuary,
339 * which needs to be maintained as a list of every buddy the user has ever seen. We don't know
340 * when they're connecting from the same client and when they're connecting from a different client,
341 * so we have no guarantee of what is in the client side roster at connect time.
342 */
344 long len;
345 void *Value;
346 const char *Key;
347 struct CitContext *cptr;
348 int nContexts, i;
349 int online_now = 0;
350 HashList *mortuary = xmpp_fetch_mortuary();
351 HashPos *HashPos = GetNewHashPos(mortuary, 0);
352
353 /* we need to omit anyone who is currently online */
354 cptr = CtdlGetContextArray(&nContexts);
355
356 /* go through the list of users in the mortuary... */
357 while (GetNextHashPos(mortuary, HashPos, &len, &Key, &Value) != 0)
358 {
359
360 online_now = 0;
361 if (cptr) for (i=0; i<nContexts; i++) {
362 if (xmpp_is_visible(&cptr[i], CC)) {
363 if (!strcasecmp(cptr[i].cs_principal_id, (char *)Value)) {
364 online_now = 1;
365 }
366 }
367 }
368
369 if (!online_now) {
370 xmpp_destroy_buddy((char *)Value, 1); /* aggressive presence update */
371 }
372
373 }
374 DeleteHashPos(&HashPos);
375 DeleteHash(&mortuary);
376 free(cptr);
377}
@ eMesageText
Definition: citadel_defs.h:263
#define CS_STEALTH
Definition: citadel_defs.h:141
char * CtdlGetConfigStr(char *key)
Definition: config.c:363
CitContext * CtdlGetContextArray(int *count)
Definition: context.c:403
#define CC
Definition: context.h:136
int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name)
Definition: room_ops.c:304
void CM_Free(struct CtdlMessage *msg)
Definition: msgbase.c:305
int CtdlForEachMessage(int mode, long ref, char *search_string, char *content_type, struct CtdlMessage *compare, ForEachMsgCallback CallBack, void *userdata)
Definition: msgbase.c:623
struct CtdlMessage * CtdlFetchMessage(long msgnum, int with_body)
Definition: msgbase.c:1130
int CtdlDeleteMessages(const char *room_name, long *dmsgnums, int num_dmsgnums, char *content_type)
Definition: msgbase.c:3229
long quickie_message(char *from, char *fromaddr, char *to, char *room, char *text, int format_type, char *subject)
Definition: msgbase.c:2906
@ MSGS_LAST
Definition: msgbase.h:10
void free(void *)
char * xmlesc(char *buf, char *str, int bufsiz)
Definition: serv_xmpp.c:100
#define XMPP
Definition: serv_xmpp.h:43
@ XMPP_EVT_LOGIN
Definition: serv_xmpp.h:58
void xmpp_roster_item(struct CitContext *)
int logged_in
Definition: context.h:66
struct ctdluser user
Definition: context.h:105
unsigned cs_flags
Definition: context.h:73
int can_receive_im
Definition: context.h:77
char cs_principal_id[256]
Definition: context.h:90
char * cm_fields[256]
Definition: server.h:29
cit_uint8_t axlevel
Definition: server.h:134
long usernum
Definition: server.h:135
#define USERCONFIGROOM
Definition: sysconfig.h:64
void cprintf(const char *format,...)
Definition: sysdep.c:369
void xmpp_fetch_mortuary_backend(long msgnum, void *userdata)
int xmpp_is_visible(struct CitContext *cptr, struct CitContext *to_whom)
Definition: xmpp_presence.c:66
void xmpp_indicate_presence(char *presence_jid)
Definition: xmpp_presence.c:53
void xmpp_presence_notify(char *presence_jid, int event_type)
void xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster(void)
void xmpp_store_mortuary(HashList *mortuary)
void xmpp_wholist_presence_dump(void)
Definition: xmpp_presence.c:85
void xmpp_massacre_roster(void)
HashList * xmpp_fetch_mortuary(void)
void xmpp_destroy_buddy(char *presence_jid, int aggressively)