4 * Handle XMPP presence exchanges
6 * Copyright (c) 2007-2009 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"
63 * Initial dump of the entire wholist
65 void jabber_wholist_presence_dump(void)
67 struct CitContext *cptr = NULL;
70 int aide = (CC->user.axlevel >= 6);
72 cptr = CtdlGetContextArray(&nContexts);
74 return ; /** FIXME: Does jabber need to send something to maintain the protocol? */
76 for (i=0; i<nContexts; i++) {
77 if (cptr[i].logged_in) {
79 (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide)) /* aides see everyone */
80 && (cptr[i].user.usernum != CC->user.usernum) /* don't show myself */
81 && (cptr[i].can_receive_im) /* IM-capable session */
83 cprintf("<presence type=\"available\" from=\"%s\"></presence>",
84 cptr[i].cs_inet_email);
94 * When a user logs in or out of the local Citadel system, notify all Jabber sessions
97 void xmpp_presence_notify(char *presence_jid, int event_type) {
98 struct CitContext *cptr;
99 static int unsolicited_id;
100 int visible_sessions = 0;
102 int aide = (CC->user.axlevel >= 6);
104 if (IsEmptyStr(presence_jid)) return;
106 cptr = CtdlGetContextArray(&nContexts);
108 return ; /** FIXME: Does jabber need to send something to maintain the protocol? */
110 /* Count the visible sessions for this user */
111 for (i=0; i<nContexts; i++) {
112 if (cptr[i].logged_in) {
114 (!strcasecmp(cptr[i].cs_inet_email, presence_jid))
115 && (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
116 && (cptr[i].can_receive_im)
123 CtdlLogPrintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
124 visible_sessions, presence_jid, CC->cs_pid);
126 if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
128 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
130 /* Do an unsolicited roster update that adds a new contact. */
131 for (i=0; i<nContexts; i++) {
132 if (cptr[i].logged_in) {
133 if (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) {
134 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">",
136 cprintf("<query xmlns=\"jabber:iq:roster\">");
137 jabber_roster_item(&cptr[i]);
144 /* Transmit presence information */
145 cprintf("<presence type=\"available\" from=\"%s\"></presence>", presence_jid);
148 if (visible_sessions == 0) {
149 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n", CC->cs_pid, presence_jid);
151 /* Transmit non-presence information */
152 cprintf("<presence type=\"unavailable\" from=\"%s\"></presence>", presence_jid);
153 cprintf("<presence type=\"unsubscribed\" from=\"%s\"></presence>", presence_jid);
155 /* Do an unsolicited roster update that deletes the contact. */
156 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">", ++unsolicited_id);
157 cprintf("<query xmlns=\"jabber:iq:roster\">");
158 cprintf("<item jid=\"%s\" subscription=\"remove\">", presence_jid);
159 cprintf("<group>%s</group>", config.c_humannode);