9a6826a7681923501a2c22cc0ee9360db34443dd
[citadel.git] / citadel / modules / xmpp / xmpp_query_namespace.c
1 /*
2  * $Id$ 
3  *
4  * Handle <iq> <get> <query> type situations (namespace queries)
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 #include "sysdep.h"
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <pwd.h>
30 #include <errno.h>
31 #include <sys/types.h>
32
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
35 # include <time.h>
36 #else
37 # if HAVE_SYS_TIME_H
38 #  include <sys/time.h>
39 # else
40 #  include <time.h>
41 # endif
42 #endif
43
44 #include <sys/wait.h>
45 #include <string.h>
46 #include <limits.h>
47 #include <ctype.h>
48 #include <expat.h>
49 #include <libcitadel.h>
50 #include "citadel.h"
51 #include "server.h"
52 #include "citserver.h"
53 #include "support.h"
54 #include "config.h"
55 #include "internet_addressing.h"
56 #include "md5.h"
57 #include "ctdl_module.h"
58 #include "serv_xmpp.h"
59
60
61 /*
62  * Output a single roster item, for roster queries or pushes
63  */
64 void xmpp_roster_item(struct CitContext *cptr) {
65         char xmlbuf1[256];
66         char xmlbuf2[256];
67
68         cprintf("<item jid=\"%s\" name=\"%s\" subscription=\"both\">",
69                 xmlesc(xmlbuf1, cptr->cs_inet_email, sizeof xmlbuf1),
70                 xmlesc(xmlbuf2, cptr->user.fullname, sizeof xmlbuf2)
71         );
72         cprintf("<group>%s</group>", xmlesc(xmlbuf1, config.c_humannode, sizeof xmlbuf1));
73         cprintf("</item>");
74 }
75
76 /* 
77  * Return the results for a "jabber:iq:roster:query"
78  *
79  * Since we are not yet managing a roster, we simply return the entire wholist
80  * (minus any entries for this user -- don't tell me about myself)
81  *
82  */
83 void xmpp_iq_roster_query(void)
84 {
85         struct CitContext *cptr;
86         int nContexts, i;
87
88         cprintf("<query xmlns=\"jabber:iq:roster\">");
89         cptr = CtdlGetContextArray(&nContexts);
90         if (cptr) {
91                 for (i=0; i<nContexts; i++) {
92                         if (xmpp_is_visible(&cptr[i], CC)) {
93                                 CtdlLogPrintf(CTDL_DEBUG, "Rosterizing %s\n", cptr[i].user.fullname);
94                                 xmpp_roster_item(&cptr[i]);
95                         }
96                 }
97                 free (cptr);
98         }
99         cprintf("</query>");
100 }
101
102
103 /*
104  * TODO: handle queries on some or all of these namespaces
105  *
106 xmpp_query_namespace(purple5b5c1e58, splorph.xand.com, http://jabber.org/protocol/disco#items:query)
107 xmpp_query_namespace(purple5b5c1e59, splorph.xand.com, http://jabber.org/protocol/disco#info:query)
108 xmpp_query_namespace(purple5b5c1e5a, , vcard-temp:query)
109  *
110  */
111
112 void xmpp_query_namespace(char *iq_id, char *iq_from, char *iq_to, char *query_xmlns)
113 {
114         int supported_namespace = 0;
115         int roster_query = 0;
116         char xmlbuf[256];
117
118         /* We need to know before we begin the response whether this is a supported namespace, so
119          * unfortunately all supported namespaces need to be defined here *and* down below where
120          * they are handled.
121          */
122         if (
123                 (!strcasecmp(query_xmlns, "jabber:iq:roster:query"))
124                 || (!strcasecmp(query_xmlns, "jabber:iq:auth:query"))
125         ) {
126                 supported_namespace = 1;
127         }
128
129         CtdlLogPrintf(CTDL_DEBUG, "xmpp_query_namespace(%s, %s, %s, %s)\n", iq_id, iq_from, iq_to, query_xmlns);
130
131         /*
132          * Beginning of query result.
133          */
134         if (supported_namespace) {
135                 cprintf("<iq type=\"result\" ");
136         }
137         else {
138                 cprintf("<iq type=\"error\" ");
139         }
140         if (!IsEmptyStr(iq_from)) {
141                 cprintf("to=\"%s\" ", xmlesc(xmlbuf, iq_from, sizeof xmlbuf));
142         }
143         cprintf("id=\"%s\">", xmlesc(xmlbuf, iq_id, sizeof xmlbuf));
144
145         /*
146          * Is this a query we know how to handle?
147          */
148
149         if (!strcasecmp(query_xmlns, "jabber:iq:roster:query")) {
150                 roster_query = 1;
151                 xmpp_iq_roster_query();
152         }
153
154         else if (!strcasecmp(query_xmlns, "jabber:iq:auth:query")) {
155                 cprintf("<query xmlns=\"jabber:iq:auth\">"
156                         "<username/><password/><resource/>"
157                         "</query>"
158                 );
159         }
160
161         /*
162          * If we didn't hit any known query namespaces then we should deliver a
163          * "service unavailable" error (see RFC3921 section 2.4 and 11.1.5.4)
164          */
165
166         else {
167                 CtdlLogPrintf(CTDL_DEBUG,
168                         "Unknown query namespace '%s' - returning <service-unavailable/>\n",
169                         query_xmlns
170                 );
171                 cprintf("<error code=\"503\" type=\"cancel\">"
172                         "<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
173                         "</error>"
174                 );
175         }
176
177         cprintf("</iq>");
178
179         /* If we told the client who is on the roster, we also need to tell the client
180          * who is *not* on the roster.  (It's down here because we can't do it in the same
181          * stanza; this will be an unsolicited push.)
182          */
183         if (roster_query) {
184                 xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster();
185         }
186 }