* More license declarations
[citadel.git] / citadel / modules / jabber / xmpp_presence.c
1 /*
2  * $Id$ 
3  *
4  * Handle XMPP presence exchanges
5  *
6  * Copyright (c) 2007-2009 by Art Cancro
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include "sysdep.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <pwd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <sys/wait.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <ctype.h>
49 #include <expat.h>
50 #include <libcitadel.h>
51 #include "citadel.h"
52 #include "server.h"
53 #include "citserver.h"
54 #include "support.h"
55 #include "config.h"
56 #include "internet_addressing.h"
57 #include "md5.h"
58 #include "ctdl_module.h"
59 #include "serv_xmpp.h"
60
61
62 /* 
63  * Initial dump of the entire wholist
64  */
65 void jabber_wholist_presence_dump(void)
66 {
67         struct CitContext *cptr = NULL;
68         int nContexts, i;
69         
70         int aide = (CC->user.axlevel >= 6);
71
72         cptr = CtdlGetContextArray(&nContexts);
73         if (!cptr)
74                 return ; /** FIXME: Does jabber need to send something to maintain the protocol?  */
75                 
76         for (i=0; i<nContexts; i++) {
77                 if (cptr[i].logged_in) {
78                         if (
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 */
82                            ) {
83                                 cprintf("<presence type=\"available\" from=\"%s\"></presence>",
84                                         cptr[i].cs_inet_email);
85                         }
86                 }
87         }
88         free(cptr);
89 }
90
91
92
93 /*
94  * When a user logs in or out of the local Citadel system, notify all Jabber sessions
95  * about it.
96  */
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;
101         int nContexts, i;
102         int aide = (CC->user.axlevel >= 6);
103
104         if (IsEmptyStr(presence_jid)) return;
105
106         cptr = CtdlGetContextArray(&nContexts);
107         if (!cptr)
108                 return ; /** FIXME: Does jabber need to send something to maintain the protocol?  */
109                 
110         /* Count the visible sessions for this user */
111         for (i=0; i<nContexts; i++) {
112                 if (cptr[i].logged_in) {
113                         if (
114                                 (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) 
115                                 && (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
116                                 && (cptr[i].can_receive_im)
117                         ) {
118                                 ++visible_sessions;
119                         }
120                 }
121         }
122
123         CtdlLogPrintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
124                 visible_sessions, presence_jid, CC->cs_pid);
125
126         if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
127
128                 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
129
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\">",
135                                                 ++unsolicited_id);
136                                         cprintf("<query xmlns=\"jabber:iq:roster\">");
137                                         jabber_roster_item(&cptr[i]);
138                                         cprintf("</query>"
139                                                 "</iq>");
140                                 }
141                         }
142                 }
143
144                 /* Transmit presence information */
145                 cprintf("<presence type=\"available\" from=\"%s\"></presence>", presence_jid);
146         }
147
148         if (visible_sessions == 0) {
149                 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n", CC->cs_pid, presence_jid);
150
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);
154
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);
160                 cprintf("</item>");
161                 cprintf("</query>"
162                         "</iq>");
163         }
164         free(cptr);
165 }