d943df16db6f7e1e7139fec860ca41b2cca0a41a
[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 {
64         struct CitContext *CCC=CC;
65
66         XPrint(HKEY("item"), 0,
67                XCPROPERTY("subscription", "both"),
68                XPROPERTY("jid",  cptr->cs_inet_email, strlen(cptr->cs_inet_email)),
69                XPROPERTY("name", cptr->user.fullname, strlen(cptr->user.fullname)),
70                TYPE_ARGEND);
71
72         XPrint(HKEY("group"), XCLOSED,
73                XCFGBODY(c_humannode),
74                TYPE_ARGEND);
75
76         XPUT("</item>");
77 }
78
79 /* 
80  * Return the results for a "jabber:iq:roster:query"
81  *
82  * Since we are not yet managing a roster, we simply return the entire wholist
83  * (minus any entries for this user -- don't tell me about myself)
84  *
85  */
86 void xmpp_iq_roster_query(void)
87 {
88         struct CitContext *cptr;
89         int nContexts, i;
90
91         XPUT("<query xmlns=\"jabber:iq:roster\">");
92         cptr = CtdlGetContextArray(&nContexts);
93         if (cptr) {
94                 for (i=0; i<nContexts; i++) {
95                         if (xmpp_is_visible(&cptr[i], CC)) {
96                                 XMPP_syslog(LOG_DEBUG, "Rosterizing %s\n", cptr[i].user.fullname);
97                                 xmpp_roster_item(&cptr[i]);
98                         }
99                 }
100                 free (cptr);
101         }
102         XPUT("</query>");
103 }
104
105
106 /*
107  * TODO: handle queries on some or all of these namespaces
108  *
109 xmpp_query_namespace(purple5b5c1e58, splorph.xand.com, http://jabber.org/protocol/disco#items:query)
110 xmpp_query_namespace(purple5b5c1e59, splorph.xand.com, http://jabber.org/protocol/disco#info:query)
111 xmpp_query_namespace(purple5b5c1e5a, , vcard-temp:query)
112  *
113  */
114
115 void xmpp_query_namespace(TheToken_iq *IQ/*char *iq_id, char *iq_from, char *iq_to*/, char *query_xmlns)
116 {
117         int supported_namespace = 0;
118         int roster_query = 0;
119         static const ConstStr Type[] = {
120                 {HKEY("result")},
121                 {HKEY("error")}
122         };
123         
124         /* We need to know before we begin the response whether this is a supported namespace, so
125          * unfortunately all supported namespaces need to be defined here *and* down below where
126          * they are handled.
127          */
128         if (
129                 (!strcasecmp(query_xmlns, "jabber:iq:roster:query"))
130                 || (!strcasecmp(query_xmlns, "jabber:iq:auth:query"))
131         ) {
132                 supported_namespace = 1;
133         }
134
135         XMPP_syslog(LOG_DEBUG, "xmpp_query_namespace(%s, %s, %s, %s)\n", ChrPtr(IQ->id), ChrPtr(IQ->from), ChrPtr(IQ->to), query_xmlns);
136
137         /*
138          * Beginning of query result.
139          */
140         if (supported_namespace) {
141                 XPrint(HKEY("iq"), 0,
142                        XPROPERTY("type", Type[0].Key, Type[0].len),
143                        XSPROPERTY("to",  IQ->from),
144                        XSPROPERTY("id",   IQ->id),
145                        TYPE_ARGEND);
146         }
147         else {
148                 XPrint(HKEY("iq"), 0,
149                        XPROPERTY("type", Type[1].Key, Type[1].len),
150                        XSPROPERTY("id",   IQ->id),
151                        TYPE_ARGEND);
152         }
153
154         /*
155          * Is this a query we know how to handle?
156          */
157
158         if (!strcasecmp(query_xmlns, "jabber:iq:roster:query")) {
159                 roster_query = 1;
160                 xmpp_iq_roster_query();
161         }
162
163         else if (!strcasecmp(query_xmlns, "jabber:iq:auth:query")) {
164                 XPUT("<query xmlns=\"jabber:iq:auth\">"
165                      "<username/><password/><resource/>"
166                      "</query>"
167                 );
168         }
169
170         /*
171          * If we didn't hit any known query namespaces then we should deliver a
172          * "service unavailable" error (see RFC3921 section 2.4 and 11.1.5.4)
173          */
174
175         else {
176                 XMPP_syslog(LOG_DEBUG,
177                             "Unknown query namespace '%s' - returning <service-unavailable/>\n",
178                             query_xmlns
179                 );
180                 XPUT("<error code=\"503\" type=\"cancel\">"
181                      "<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
182                      "</error>"
183                 );
184         }
185
186         XPUT("</iq>");
187
188         /* If we told the client who is on the roster, we also need to tell the client
189          * who is *not* on the roster.  (It's down here because we can't do it in the same
190          * stanza; this will be an unsolicited push.)
191          */
192         if (roster_query) {
193                 xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster();
194         }
195 }