9eaf88728e7eae30ed3dc4c67281d8da8fe68733
[citadel.git] / citadel / modules / xmpp / xmpp_presence.c
1 /*
2  * $Id$ 
3  *
4  * Handle XMPP presence exchanges
5  *
6  * Copyright (c) 2007-2010 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;
75         }
76
77         for (i=0; i<nContexts; i++) {
78                 if (cptr[i].logged_in) {
79                         if (
80                                 (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))  /* aides see everyone */
81                                 && (cptr[i].user.usernum != CC->user.usernum)   /* don't show myself */
82                                 && (cptr[i].can_receive_im)                     /* IM-capable session */
83                         ) {
84                                 cprintf("<presence type=\"available\" from=\"%s\"></presence>",
85                                         cptr[i].cs_inet_email);
86                         }
87                 }
88         }
89         free(cptr);
90 }
91
92
93 /*
94  * Function to remove a buddy subscription and delete from the roster
95  * (used in several places)
96  */
97 void xmpp_destroy_buddy(char *presence_jid) {
98         static int unsolicited_id = 1;
99
100         /* Transmit non-presence information */
101         cprintf("<presence type=\"unavailable\" from=\"%s\"></presence>", presence_jid);
102         cprintf("<presence type=\"unsubscribed\" from=\"%s\"></presence>", presence_jid);
103
104         /* Do an unsolicited roster update that deletes the contact. */
105         cprintf("<iq id=\"unbuddy_%x\" type=\"result\">", ++unsolicited_id);
106         cprintf("<query xmlns=\"jabber:iq:roster\">");
107         cprintf("<item jid=\"%s\" subscription=\"remove\">", presence_jid);
108         cprintf("<group>%s</group>", config.c_humannode);
109         cprintf("</item>");
110         cprintf("</query>"
111                 "</iq>"
112         );
113 }
114
115
116 /*
117  * When a user logs in or out of the local Citadel system, notify all XMPP sessions about it.
118  */
119 void xmpp_presence_notify(char *presence_jid, int event_type) {
120         struct CitContext *cptr;
121         static int unsolicited_id;
122         int visible_sessions = 0;
123         int nContexts, i;
124         int aide = (CC->user.axlevel >= 6);
125
126         if (IsEmptyStr(presence_jid)) return;
127         if (CC->kill_me) return;
128
129         cptr = CtdlGetContextArray(&nContexts);
130         if (!cptr) {
131                 return;
132         }
133                 
134         /* Count the visible sessions for this user */
135         for (i=0; i<nContexts; i++) {
136                 if (cptr[i].logged_in) {
137                         if (
138                                 (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) 
139                                 && (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
140                                 && (cptr[i].can_receive_im)
141                         ) {
142                                 ++visible_sessions;
143                         }
144                 }
145         }
146
147         CtdlLogPrintf(CTDL_DEBUG, "%d sessions for <%s> are now visible to session %d\n",
148                 visible_sessions, presence_jid, CC->cs_pid);
149
150         if ( (event_type == XMPP_EVT_LOGIN) && (visible_sessions == 1) ) {
151
152                 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged in\n", CC->cs_pid, presence_jid);
153
154                 /* Do an unsolicited roster update that adds a new contact. */
155                 for (i=0; i<nContexts; i++) {
156                         if (cptr[i].logged_in) {
157                                 if (!strcasecmp(cptr[i].cs_inet_email, presence_jid)) {
158                                         cprintf("<iq id=\"unsolicited_%x\" type=\"result\">",
159                                                 ++unsolicited_id);
160                                         cprintf("<query xmlns=\"jabber:iq:roster\">");
161                                         xmpp_roster_item(&cptr[i]);
162                                         cprintf("</query>"
163                                                 "</iq>");
164                                 }
165                         }
166                 }
167
168                 /* Transmit presence information */
169                 cprintf("<presence type=\"available\" from=\"%s\"></presence>", presence_jid);
170         }
171
172         if (visible_sessions == 0) {
173                 CtdlLogPrintf(CTDL_DEBUG, "Telling session %d that <%s> logged out\n", CC->cs_pid, presence_jid);
174
175                 xmpp_destroy_buddy(presence_jid);
176         }
177         free(cptr);
178 }
179
180
181
182 /*
183  * Upon logout we make an attempt to delete the whole roster, in order to
184  * try to keep "ghost" buddies from remaining in the client-side roster.
185  */
186 void xmpp_massacre_roster(void)
187 {
188         struct CitContext *cptr;
189         int nContexts, i;
190         int aide = (CC->user.axlevel >= 6);
191
192         cptr = CtdlGetContextArray(&nContexts);
193         if (cptr) {
194                 for (i=0; i<nContexts; i++) {
195                         if (cptr[i].logged_in) {
196                                 if (
197                                         (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
198                                         && (cptr[i].user.usernum != CC->user.usernum)
199                                 ) {
200                                         xmpp_destroy_buddy(cptr[i].cs_inet_email);
201                                 }
202                         }
203                 }
204                 free (cptr);
205         }
206 }
207
208
209