0855cc89892c4b59e40de39ba301debb1eefa24a
[citadel.git] / citadel / modules / jabber / xmpp_presence.c
1 /*
2  * $Id$ 
3  *
4  * Handle XMPP presence exchanges
5  *
6  * Copyright (c) 2007 by Art Cancro
7  * This code is released under the terms of the GNU General Public License.
8  *
9  */
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <pwd.h>
18 #include <errno.h>
19 #include <sys/types.h>
20
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 #  include <sys/time.h>
27 # else
28 #  include <time.h>
29 # endif
30 #endif
31
32 #include <sys/wait.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <ctype.h>
36 #include <libcitadel.h>
37 #include "citadel.h"
38 #include "server.h"
39 #include "citserver.h"
40 #include "support.h"
41 #include "config.h"
42 #include "internet_addressing.h"
43 #include "md5.h"
44 #include "ctdl_module.h"
45
46 #ifdef HAVE_EXPAT
47 #include <expat.h>
48 #include "serv_xmpp.h"
49
50
51 /* 
52  * Initial dump of the entire wholist
53  */
54 void jabber_wholist_presence_dump(void)
55 {
56         struct CitContext *cptr = NULL;
57         int nContexts, i;
58         
59         int aide = (CC->user.axlevel >= 6);
60
61         cptr = CtdlGetContextArray(&nContexts);
62         if (!cptr)
63                 return ; /** FIXME: Does jabber need to send something to maintain the protocol?  */
64                 
65         for (i=0; i<nContexts; i++) {
66                 if (cptr[i].logged_in) {
67                         if (
68                            (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))               /* aides see everyone */
69                            && (cptr[i].user.usernum != CC->user.usernum)                /* don't show myself */
70                            ) {
71                                 cprintf("<presence type=\"available\" from=\"%s\"></presence>",
72                                         cptr[i].cs_inet_email);
73                         }
74                 }
75         }
76         free(cptr);
77 }
78
79
80
81 /*
82  * When a user logs in or out of the local Citadel system, notify all Jabber sessions
83  * about it.
84  */
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 nContexts, i;
90         int aide = (CC->user.axlevel >= 6);
91
92         if (IsEmptyStr(presence_jid)) return;
93
94         cptr = CtdlGetContextArray(&nContexts);
95         if (!cptr)
96                 return ; /** FIXME: Does jabber need to send something to maintain the protocol?  */
97                 
98         /* Count the visible sessions for this user */
99         for (i=0; i<nContexts; i++) {
100                 if (cptr[i].logged_in) {
101                         if (  (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) 
102                            && (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
103                            ) {
104                                 ++visible_sessions;
105                         }
106                 }
107         }
108
109         lprintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
110                 visible_sessions, presence_jid, CC->cs_pid);
111
112         if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
113
114                 lprintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
115
116                 /* Do an unsolicited roster update that adds a new contact. */
117                 for (i=0; i<nContexts; i++) {
118                         if (cptr[i].logged_in) {
119                                 if (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) {
120                                         cprintf("<iq id=\"unsolicited_%x\" type=\"result\">",
121                                                 ++unsolicited_id);
122                                         cprintf("<query xmlns=\"jabber:iq:roster\">");
123                                         jabber_roster_item(&cptr[i]);
124                                         cprintf("</query>"
125                                                 "</iq>");
126                                 }
127                         }
128                 }
129
130                 /* Transmit presence information */
131                 cprintf("<presence type=\"available\" from=\"%s\"></presence>", presence_jid);
132         }
133
134         if (visible_sessions == 0) {
135                 lprintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n", CC->cs_pid, presence_jid);
136
137                 /* Transmit non-presence information */
138                 cprintf("<presence type=\"unavailable\" from=\"%s\"></presence>", presence_jid);
139                 cprintf("<presence type=\"unsubscribed\" from=\"%s\"></presence>", presence_jid);
140
141                 /* Do an unsolicited roster update that deletes the contact. */
142                 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">", ++unsolicited_id);
143                 cprintf("<query xmlns=\"jabber:iq:roster\">");
144                 cprintf("<item jid=\"%s\" subscription=\"remove\">", presence_jid);
145                 cprintf("<group>%s</group>", config.c_humannode);
146                 cprintf("</item>");
147                 cprintf("</query>"
148                         "</iq>");
149         }
150         free(cptr);
151 }
152
153
154 #endif  /* HAVE_EXPAT */