7ac4439d716c618c4277074ffe083df8a1dd5e14
[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 <expat.h>
37 #include <libcitadel.h>
38 #include "citadel.h"
39 #include "server.h"
40 #include "citserver.h"
41 #include "support.h"
42 #include "config.h"
43 #include "internet_addressing.h"
44 #include "md5.h"
45 #include "ctdl_module.h"
46 #include "serv_xmpp.h"
47
48
49 /* 
50  * Initial dump of the entire wholist
51  */
52 void jabber_wholist_presence_dump(void)
53 {
54         struct CitContext *cptr = NULL;
55         int nContexts, i;
56         
57         int aide = (CC->user.axlevel >= 6);
58
59         cptr = CtdlGetContextArray(&nContexts);
60         if (!cptr)
61                 return ; /** FIXME: Does jabber need to send something to maintain the protocol?  */
62                 
63         for (i=0; i<nContexts; i++) {
64                 if (cptr[i].logged_in) {
65                         if (
66                                 (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))  /* aides see everyone */
67                                 && (cptr[i].user.usernum != CC->user.usernum)   /* don't show myself */
68                                 && (cptr[i].can_receive_im)                     /* IM-capable session */
69                            ) {
70                                 cprintf("<presence type=\"available\" from=\"%s\"></presence>",
71                                         cptr[i].cs_inet_email);
72                         }
73                 }
74         }
75         free(cptr);
76 }
77
78
79
80 /*
81  * When a user logs in or out of the local Citadel system, notify all Jabber sessions
82  * about it.
83  */
84 void xmpp_presence_notify(char *presence_jid, int event_type) {
85         struct CitContext *cptr;
86         static int unsolicited_id;
87         int visible_sessions = 0;
88         int nContexts, i;
89         int aide = (CC->user.axlevel >= 6);
90
91         if (IsEmptyStr(presence_jid)) return;
92
93         cptr = CtdlGetContextArray(&nContexts);
94         if (!cptr)
95                 return ; /** FIXME: Does jabber need to send something to maintain the protocol?  */
96                 
97         /* Count the visible sessions for this user */
98         for (i=0; i<nContexts; i++) {
99                 if (cptr[i].logged_in) {
100                         if (
101                                 (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) 
102                                 && (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
103                                 && (cptr[i].can_receive_im)
104                         ) {
105                                 ++visible_sessions;
106                         }
107                 }
108         }
109
110         CtdlLogPrintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
111                 visible_sessions, presence_jid, CC->cs_pid);
112
113         if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
114
115                 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
116
117                 /* Do an unsolicited roster update that adds a new contact. */
118                 for (i=0; i<nContexts; i++) {
119                         if (cptr[i].logged_in) {
120                                 if (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) {
121                                         cprintf("<iq id=\"unsolicited_%x\" type=\"result\">",
122                                                 ++unsolicited_id);
123                                         cprintf("<query xmlns=\"jabber:iq:roster\">");
124                                         jabber_roster_item(&cptr[i]);
125                                         cprintf("</query>"
126                                                 "</iq>");
127                                 }
128                         }
129                 }
130
131                 /* Transmit presence information */
132                 cprintf("<presence type=\"available\" from=\"%s\"></presence>", presence_jid);
133         }
134
135         if (visible_sessions == 0) {
136                 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n", CC->cs_pid, presence_jid);
137
138                 /* Transmit non-presence information */
139                 cprintf("<presence type=\"unavailable\" from=\"%s\"></presence>", presence_jid);
140                 cprintf("<presence type=\"unsubscribed\" from=\"%s\"></presence>", presence_jid);
141
142                 /* Do an unsolicited roster update that deletes the contact. */
143                 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">", ++unsolicited_id);
144                 cprintf("<query xmlns=\"jabber:iq:roster\">");
145                 cprintf("<item jid=\"%s\" subscription=\"remove\">", presence_jid);
146                 cprintf("<group>%s</group>", config.c_humannode);
147                 cprintf("</item>");
148                 cprintf("</query>"
149                         "</iq>");
150         }
151         free(cptr);
152 }