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.
23 #include <sys/types.h>
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
41 #include <libcitadel.h>
44 #include "citserver.h"
47 #include "internet_addressing.h"
49 #include "ctdl_module.h"
50 #include "serv_xmpp.h"
54 * Output a single roster item, for roster queries or pushes
56 void xmpp_roster_item(struct CitContext *cptr) {
60 cprintf("<item jid=\"%s\" name=\"%s\" subscription=\"both\">",
61 xmlesc(xmlbuf1, cptr->cs_inet_email, sizeof xmlbuf1),
62 xmlesc(xmlbuf2, cptr->user.fullname, sizeof xmlbuf2)
64 cprintf("<group>%s</group>", xmlesc(xmlbuf1, config.c_humannode, sizeof xmlbuf1));
70 * Return the results for a "jabber:iq:roster:query"
72 * Since we are not yet managing a roster, we simply return the entire wholist
73 * (minus any entries for this user -- don't tell me about myself)
76 void xmpp_iq_roster_query(void)
78 struct CitContext *cptr;
81 syslog(LOG_DEBUG, "Roster push!");
82 cprintf("<query xmlns=\"jabber:iq:roster\">");
83 cptr = CtdlGetContextArray(&nContexts);
85 for (i=0; i<nContexts; i++) {
86 if (xmpp_is_visible(&cptr[i], CC)) {
87 XMPP_syslog(LOG_DEBUG, "Rosterizing %s\n", cptr[i].user.fullname);
88 xmpp_roster_item(&cptr[i]);
99 * Client is doing a namespace query. These are all handled differently.
100 * A "rumplestiltskin lookup" is the most efficient way to handle this. Please do not refactor this code.
102 void xmpp_query_namespace(char *iq_id, char *iq_from, char *iq_to, char *query_xmlns)
104 int supported_namespace = 0;
105 int roster_query = 0;
107 int reply_must_be_from_my_jid = 0;
109 /* We need to know before we begin the response whether this is a supported namespace, so
110 * unfortunately all supported namespaces need to be defined here *and* down below where
114 (!strcasecmp(query_xmlns, "jabber:iq:roster:query"))
115 || (!strcasecmp(query_xmlns, "jabber:iq:auth:query"))
116 || (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#items:query"))
117 || (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#info:query"))
119 supported_namespace = 1;
122 XMPP_syslog(LOG_DEBUG, "xmpp_query_namespace(id=%s, from=%s, to=%s, xmlns=%s)\n", iq_id, iq_from, iq_to, query_xmlns);
125 * Beginning of query result.
128 if (!strcasecmp(query_xmlns, "jabber:iq:roster:query")) {
129 reply_must_be_from_my_jid = 1;
132 char dom[1024]; // client is expecting to see the reply
133 if (reply_must_be_from_my_jid) { // coming "from" the user's jid
134 safestrncpy(dom, XMPP->client_jid, sizeof(dom));
135 char *slash = strchr(dom, '/');
141 safestrncpy(dom, XMPP->client_jid, sizeof(dom)); // client is expecting to see the reply
142 if (IsEmptyStr(dom)) { // coming "from" the domain of the user's jid
143 safestrncpy(dom, XMPP->server_name, sizeof(dom));
145 char *at = strrchr(dom, '@');
149 char *slash = strchr(dom, '/');
155 if (supported_namespace) {
156 cprintf("<iq type=\"result\" from=\"%s\" ", xmlesc(xmlbuf, dom, sizeof xmlbuf) );
159 cprintf("<iq type=\"error\" from=\"%s\" ", xmlesc(xmlbuf, dom, sizeof xmlbuf) );
161 if (!IsEmptyStr(iq_from)) {
162 cprintf("to=\"%s\" ", xmlesc(xmlbuf, iq_from, sizeof xmlbuf));
164 cprintf("id=\"%s\">", xmlesc(xmlbuf, iq_id, sizeof xmlbuf));
167 * Is this a query we know how to handle?
170 if (!strcasecmp(query_xmlns, "jabber:iq:roster:query")) {
172 xmpp_iq_roster_query();
175 else if (!strcasecmp(query_xmlns, "jabber:iq:auth:query")) {
176 cprintf("<query xmlns=\"jabber:iq:auth\">"
177 "<username/><password/><resource/>"
182 // Extension "xep-0030" (http://xmpp.org/extensions/xep-0030.html) (return an empty set of results)
183 else if (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#items:query")) {
184 cprintf("<query xmlns=\"%s\"/>", xmlesc(xmlbuf, query_xmlns, sizeof xmlbuf));
187 // Extension "xep-0030" (http://xmpp.org/extensions/xep-0030.html) (return an empty set of results)
188 else if (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#info:query")) {
189 cprintf("<query xmlns=\"%s\"/>", xmlesc(xmlbuf, query_xmlns, sizeof xmlbuf));
193 * If we didn't hit any known query namespaces then we should deliver a
194 * "service unavailable" error (see RFC3921 section 2.4 and 11.1.5.4)
198 XMPP_syslog(LOG_DEBUG, "Unknown query namespace '%s' - returning <service-unavailable/>\n", query_xmlns);
199 cprintf("<error code=\"503\" type=\"cancel\">"
200 "<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
207 /* If we told the client who is on the roster, we also need to tell the client
208 * who is *not* on the roster. (It's down here because we can't do it in the same
209 * stanza; this will be an unsolicited push.)
212 xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster();