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