2 * Handle <iq> <get> <query> type situations (namespace queries)
4 * Copyright (c) 2007-2015 by Art Cancro and citadel.org
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.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include "../../sysdep.h"
23 #include <sys/types.h>
30 #include <libcitadel.h>
31 #include "../../citadel_defs.h"
32 #include "../../server.h"
33 #include "../../citserver.h"
34 #include "../../support.h"
35 #include "../../config.h"
36 #include "../../internet_addressing.h"
37 #include "../../ctdl_module.h"
38 #include "serv_xmpp.h"
42 * Output a single roster item, for roster queries or pushes
44 void xmpp_roster_item(struct CitContext *cptr) {
48 cprintf("<item jid=\"%s\" name=\"%s\" subscription=\"both\">",
49 xmlesc(xmlbuf1, cptr->cs_principal_id, sizeof xmlbuf1),
50 xmlesc(xmlbuf2, cptr->user.fullname, sizeof xmlbuf2)
52 cprintf("<group>%s</group>", xmlesc(xmlbuf1, CtdlGetConfigStr("c_humannode"), sizeof xmlbuf1));
58 * Return the results for a "jabber:iq:roster:query"
60 * Since we are not yet managing a roster, we simply return the entire wholist
61 * (minus any entries for this user -- don't tell me about myself)
64 void xmpp_iq_roster_query(void)
66 struct CitContext *cptr;
69 syslog(LOG_DEBUG, "xmpp: roster push!");
70 cprintf("<query xmlns=\"jabber:iq:roster\">");
71 cptr = CtdlGetContextArray(&nContexts);
73 for (i=0; i<nContexts; i++) {
74 if (xmpp_is_visible(&cptr[i], CC)) {
75 syslog(LOG_DEBUG, "xmpp: rosterizing %s", cptr[i].user.fullname);
76 xmpp_roster_item(&cptr[i]);
86 * Client is doing a namespace query. These are all handled differently.
88 void xmpp_query_namespace(char *iq_id, char *iq_from, char *iq_to, char *query_xmlns)
90 int supported_namespace = 0;
93 int reply_must_be_from_my_jid = 0;
95 /* We need to know before we begin the response whether this is a supported namespace, so
96 * unfortunately all supported namespaces need to be defined here *and* down below where
100 (!strcasecmp(query_xmlns, "jabber:iq:roster:query"))
101 || (!strcasecmp(query_xmlns, "jabber:iq:auth:query"))
102 || (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#items:query"))
103 || (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#info:query"))
105 supported_namespace = 1;
108 syslog(LOG_DEBUG, "xmpp: xmpp_query_namespace(id=%s, from=%s, to=%s, xmlns=%s)", iq_id, iq_from, iq_to, query_xmlns);
111 * Beginning of query result.
114 if (!strcasecmp(query_xmlns, "jabber:iq:roster:query")) {
115 reply_must_be_from_my_jid = 1;
118 char dom[1024]; // client is expecting to see the reply
119 if (reply_must_be_from_my_jid) { // coming "from" the user's jid
120 safestrncpy(dom, XMPP->client_jid, sizeof(dom));
121 char *slash = strchr(dom, '/');
127 safestrncpy(dom, XMPP->client_jid, sizeof(dom)); // client is expecting to see the reply
128 if (IsEmptyStr(dom)) { // coming "from" the domain of the user's jid
129 safestrncpy(dom, XMPP->server_name, sizeof(dom));
131 char *at = strrchr(dom, '@');
135 char *slash = strchr(dom, '/');
141 if (supported_namespace) {
142 cprintf("<iq type=\"result\" from=\"%s\" ", xmlesc(xmlbuf, dom, sizeof xmlbuf) );
145 cprintf("<iq type=\"error\" from=\"%s\" ", xmlesc(xmlbuf, dom, sizeof xmlbuf) );
147 if (!IsEmptyStr(iq_from)) {
148 cprintf("to=\"%s\" ", xmlesc(xmlbuf, iq_from, sizeof xmlbuf));
150 cprintf("id=\"%s\">", xmlesc(xmlbuf, iq_id, sizeof xmlbuf));
153 * Is this a query we know how to handle?
156 if (!strcasecmp(query_xmlns, "jabber:iq:roster:query")) {
158 xmpp_iq_roster_query();
161 else if (!strcasecmp(query_xmlns, "jabber:iq:auth:query")) {
162 cprintf("<query xmlns=\"jabber:iq:auth\">"
163 "<username/><password/><resource/>"
168 // Extension "xep-0030" (http://xmpp.org/extensions/xep-0030.html) (return an empty set of results)
169 else if (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#items:query")) {
170 cprintf("<query xmlns=\"%s\"/>", xmlesc(xmlbuf, query_xmlns, sizeof xmlbuf));
173 // Extension "xep-0030" (http://xmpp.org/extensions/xep-0030.html) (return an empty set of results)
174 else if (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#info:query")) {
175 cprintf("<query xmlns=\"%s\"/>", xmlesc(xmlbuf, query_xmlns, sizeof xmlbuf));
179 * If we didn't hit any known query namespaces then we should deliver a
180 * "service unavailable" error (see RFC3921 section 2.4 and 11.1.5.4)
184 syslog(LOG_DEBUG, "xmpp: unknown query namespace '%s' - returning <service-unavailable/>", query_xmlns);
185 cprintf("<error code=\"503\" type=\"cancel\">"
186 "<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
193 /* If we told the client who is on the roster, we also need to tell the client
194 * who is *not* on the roster. (It's down here because we can't do it in the same
195 * stanza; this will be an unsolicited push.)
198 xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster();