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