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"
63 * Initial dump of the entire wholist
65 void xmpp_wholist_presence_dump(void)
67 struct CitContext *cptr = NULL;
70 int aide = (CC->user.axlevel >= 6);
72 cptr = CtdlGetContextArray(&nContexts);
77 for (i=0; i<nContexts; i++) {
78 if (cptr[i].logged_in) {
80 (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide)) /* aides see everyone */
81 && (cptr[i].user.usernum != CC->user.usernum) /* don't show myself */
82 && (cptr[i].can_receive_im) /* IM-capable session */
84 cprintf("<presence type=\"available\" from=\"%s\"></presence>",
85 cptr[i].cs_inet_email);
94 * Function to remove a buddy subscription and delete from the roster
95 * (used in several places)
97 void xmpp_destroy_buddy(char *presence_jid) {
98 static int unsolicited_id = 1;
100 /* Transmit non-presence information */
101 cprintf("<presence type=\"unavailable\" from=\"%s\"></presence>", presence_jid);
102 cprintf("<presence type=\"unsubscribed\" from=\"%s\"></presence>", presence_jid);
104 /* Do an unsolicited roster update that deletes the contact. */
105 cprintf("<iq id=\"unbuddy_%x\" type=\"result\">", ++unsolicited_id);
106 cprintf("<query xmlns=\"jabber:iq:roster\">");
107 cprintf("<item jid=\"%s\" subscription=\"remove\">", presence_jid);
108 cprintf("<group>%s</group>", config.c_humannode);
117 * When a user logs in or out of the local Citadel system, notify all XMPP sessions about it.
119 void xmpp_presence_notify(char *presence_jid, int event_type) {
120 struct CitContext *cptr;
121 static int unsolicited_id;
122 int visible_sessions = 0;
124 int aide = (CC->user.axlevel >= 6);
126 if (IsEmptyStr(presence_jid)) return;
127 if (CC->kill_me) return;
129 cptr = CtdlGetContextArray(&nContexts);
134 /* Count the visible sessions for this user */
135 for (i=0; i<nContexts; i++) {
136 if (cptr[i].logged_in) {
138 (!strcasecmp(cptr[i].cs_inet_email, presence_jid))
139 && (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
140 && (cptr[i].can_receive_im)
147 CtdlLogPrintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
148 visible_sessions, presence_jid, CC->cs_pid);
150 if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
152 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
154 /* Do an unsolicited roster update that adds a new contact. */
155 for (i=0; i<nContexts; i++) {
156 if (cptr[i].logged_in) {
157 if (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) {
158 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">",
160 cprintf("<query xmlns=\"jabber:iq:roster\">");
161 xmpp_roster_item(&cptr[i]);
168 /* Transmit presence information */
169 cprintf("<presence type=\"available\" from=\"%s\"></presence>", presence_jid);
172 if (visible_sessions == 0) {
173 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n", CC->cs_pid, presence_jid);
175 xmpp_destroy_buddy(presence_jid);
183 * Upon logout we make an attempt to delete the whole roster, in order to
184 * try to keep "ghost" buddies from remaining in the client-side roster.
186 void xmpp_massacre_roster(void)
188 struct CitContext *cptr;
190 int aide = (CC->user.axlevel >= 6);
192 cptr = CtdlGetContextArray(&nContexts);
194 for (i=0; i<nContexts; i++) {
195 if (cptr[i].logged_in) {
197 (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
198 && (cptr[i].user.usernum != CC->user.usernum)
200 xmpp_destroy_buddy(cptr[i].cs_inet_email);