4 * Handle XMPP presence exchanges
6 * Copyright (c) 2007 by Art Cancro
7 * This code is released under the terms of the GNU General Public License.
19 #include <sys/types.h>
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
36 #include <libcitadel.h>
39 #include "citserver.h"
42 #include "internet_addressing.h"
44 #include "ctdl_module.h"
48 #include "serv_xmpp.h"
52 * Initial dump of the entire wholist
54 void jabber_wholist_presence_dump(void)
56 struct CitContext *cptr = NULL;
59 int aide = (CC->user.axlevel >= 6);
61 cptr = CtdlGetContextArray(&nContexts);
63 return ; /** FIXME: Does jabber need to send something to maintain the protocol? */
65 for (i=0; i<nContexts; i++) {
66 if (cptr[i].logged_in) {
68 (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide)) /* aides see everyone */
69 && (cptr[i].user.usernum != CC->user.usernum) /* don't show myself */
71 cprintf("<presence type=\"available\" from=\"%s\"></presence>",
72 cptr[i].cs_inet_email);
82 * When a user logs in or out of the local Citadel system, notify all Jabber sessions
85 void xmpp_presence_notify(char *presence_jid, int event_type) {
86 struct CitContext *cptr;
87 static int unsolicited_id;
88 int visible_sessions = 0;
89 int aide = (CC->user.axlevel >= 6);
91 if (IsEmptyStr(presence_jid)) return;
93 /* Count the visible sessions for this user */
94 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
95 if (cptr->logged_in) {
96 if ( (!strcasecmp(cptr->cs_inet_email, presence_jid))
97 && (((cptr->cs_flags&CS_STEALTH)==0) || (aide))
104 lprintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
105 visible_sessions, presence_jid, CC->cs_pid);
107 if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
109 lprintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
111 /* Do an unsolicited roster update that adds a new contact. */
112 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
113 if (cptr->logged_in) {
114 if (!strcasecmp(cptr->cs_inet_email, presence_jid)) {
115 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">",
117 cprintf("<query xmlns=\"jabber:iq:roster\">");
118 jabber_roster_item(cptr);
125 /* Transmit presence information */
126 cprintf("<presence type=\"available\" from=\"%s\"></presence>", presence_jid);
129 if (visible_sessions == 0) {
130 lprintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n", CC->cs_pid, presence_jid);
132 /* Transmit non-presence information */
133 cprintf("<presence type=\"unavailable\" from=\"%s\"></presence>", presence_jid);
134 cprintf("<presence type=\"unsubscribed\" from=\"%s\"></presence>", presence_jid);
136 /* Do an unsolicited roster update that deletes the contact. */
137 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">", ++unsolicited_id);
138 cprintf("<query xmlns=\"jabber:iq:roster\">");
139 cprintf("<item jid=\"%s\" subscription=\"remove\">", presence_jid);
140 cprintf("<group>%s</group>", config.c_humannode);
148 #endif /* HAVE_EXPAT */