* Renamed the 'jabber' module to 'xmpp' module. Also renamed functions starting...
[citadel.git] / citadel / modules / xmpp / 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 xmpp_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 xmpp 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 XMPP sessions about it.
95  */
96 void xmpp_presence_notify(char *presence_jid, int event_type) {
97         struct CitContext *cptr;
98         static int unsolicited_id;
99         int visible_sessions = 0;
100         int nContexts, i;
101         int aide = (CC->user.axlevel >= 6);
102
103         if (IsEmptyStr(presence_jid)) return;
104
105         cptr = CtdlGetContextArray(&nContexts);
106         if (!cptr)
107                 return ; /** FIXME: Does XMPP need to send something to maintain the protocol?  */
108                 
109         /* Count the visible sessions for this user */
110         for (i=0; i<nContexts; i++) {
111                 if (cptr[i].logged_in) {
112                         if (
113                                 (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) 
114                                 && (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
115                                 && (cptr[i].can_receive_im)
116                         ) {
117                                 ++visible_sessions;
118                         }
119                 }
120         }
121
122         CtdlLogPrintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
123                 visible_sessions, presence_jid, CC->cs_pid);
124
125         if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
126
127                 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
128
129                 /* Do an unsolicited roster update that adds a new contact. */
130                 for (i=0; i<nContexts; i++) {
131                         if (cptr[i].logged_in) {
132                                 if (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) {
133                                         cprintf("<iq id=\"unsolicited_%x\" type=\"result\">",
134                                                 ++unsolicited_id);
135                                         cprintf("<query xmlns=\"jabber:iq:roster\">");
136                                         xmpp_roster_item(&cptr[i]);
137                                         cprintf("</query>"
138                                                 "</iq>");
139                                 }
140                         }
141                 }
142
143                 /* Transmit presence information */
144                 cprintf("<presence type=\"available\" from=\"%s\"></presence>", presence_jid);
145         }
146
147         if (visible_sessions == 0) {
148                 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n", CC->cs_pid, presence_jid);
149
150                 /* Transmit non-presence information */
151                 cprintf("<presence type=\"unavailable\" from=\"%s\"></presence>", presence_jid);
152                 cprintf("<presence type=\"unsubscribed\" from=\"%s\"></presence>", presence_jid);
153
154                 /* Do an unsolicited roster update that deletes the contact. */
155                 cprintf("<iq id=\"unsolicited_%x\" type=\"result\">", ++unsolicited_id);
156                 cprintf("<query xmlns=\"jabber:iq:roster\">");
157                 cprintf("<item jid=\"%s\" subscription=\"remove\">", presence_jid);
158                 cprintf("<group>%s</group>", config.c_humannode);
159                 cprintf("</item>");
160                 cprintf("</query>"
161                         "</iq>");
162         }
163         free(cptr);
164 }