Back to index

citadel  8.12
xmpp_query_namespace.c
Go to the documentation of this file.
00001 /*
00002  * Handle <iq> <get> <query> type situations (namespace queries)
00003  *
00004  * Copyright (c) 2007-2009 by Art Cancro
00005  *
00006  *  This program is open source software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License version 3.
00008  *  
00009  *  
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  
00017  *  
00018  *  
00019  */
00020 
00021 #include "sysdep.h"
00022 #include <stdlib.h>
00023 #include <unistd.h>
00024 #include <stdio.h>
00025 #include <fcntl.h>
00026 #include <signal.h>
00027 #include <pwd.h>
00028 #include <errno.h>
00029 #include <sys/types.h>
00030 
00031 #if TIME_WITH_SYS_TIME
00032 # include <sys/time.h>
00033 # include <time.h>
00034 #else
00035 # if HAVE_SYS_TIME_H
00036 #  include <sys/time.h>
00037 # else
00038 #  include <time.h>
00039 # endif
00040 #endif
00041 
00042 #include <sys/wait.h>
00043 #include <string.h>
00044 #include <limits.h>
00045 #include <ctype.h>
00046 #include <expat.h>
00047 #include <libcitadel.h>
00048 #include "citadel.h"
00049 #include "server.h"
00050 #include "citserver.h"
00051 #include "support.h"
00052 #include "config.h"
00053 #include "internet_addressing.h"
00054 #include "md5.h"
00055 #include "ctdl_module.h"
00056 #include "serv_xmpp.h"
00057 
00058 
00059 /*
00060  * Output a single roster item, for roster queries or pushes
00061  */
00062 void xmpp_roster_item(struct CitContext *cptr) {
00063        char xmlbuf1[256];
00064        char xmlbuf2[256];
00065 
00066        cprintf("<item jid=\"%s\" name=\"%s\" subscription=\"both\">",
00067               xmlesc(xmlbuf1, cptr->cs_inet_email, sizeof xmlbuf1),
00068               xmlesc(xmlbuf2, cptr->user.fullname, sizeof xmlbuf2)
00069        );
00070        cprintf("<group>%s</group>", xmlesc(xmlbuf1, config.c_humannode, sizeof xmlbuf1));
00071        cprintf("</item>");
00072 }
00073 
00074 /* 
00075  * Return the results for a "jabber:iq:roster:query"
00076  *
00077  * Since we are not yet managing a roster, we simply return the entire wholist
00078  * (minus any entries for this user -- don't tell me about myself)
00079  *
00080  */
00081 void xmpp_iq_roster_query(void)
00082 {
00083        struct CitContext *cptr;
00084        int nContexts, i;
00085 
00086        cprintf("<query xmlns=\"jabber:iq:roster\">");
00087        cptr = CtdlGetContextArray(&nContexts);
00088        if (cptr) {
00089               for (i=0; i<nContexts; i++) {
00090                      if (xmpp_is_visible(&cptr[i], CC)) {
00091                             XMPP_syslog(LOG_DEBUG, "Rosterizing %s\n", cptr[i].user.fullname);
00092                             xmpp_roster_item(&cptr[i]);
00093                      }
00094               }
00095               free (cptr);
00096        }
00097        cprintf("</query>");
00098 }
00099 
00100 
00101 /*
00102  * TODO: handle queries on some or all of these namespaces
00103  *
00104 xmpp_query_namespace(purple5b5c1e58, splorph.xand.com, http://jabber.org/protocol/disco#items:query)
00105 xmpp_query_namespace(purple5b5c1e59, splorph.xand.com, http://jabber.org/protocol/disco#info:query)
00106 xmpp_query_namespace(purple5b5c1e5a, , vcard-temp:query)
00107  *
00108  */
00109 
00110 void xmpp_query_namespace(char *iq_id, char *iq_from, char *iq_to, char *query_xmlns)
00111 {
00112        int supported_namespace = 0;
00113        int roster_query = 0;
00114        char xmlbuf[256];
00115 
00116        /* We need to know before we begin the response whether this is a supported namespace, so
00117         * unfortunately all supported namespaces need to be defined here *and* down below where
00118         * they are handled.
00119         */
00120        if (
00121               (!strcasecmp(query_xmlns, "jabber:iq:roster:query"))
00122               || (!strcasecmp(query_xmlns, "jabber:iq:auth:query"))
00123        ) {
00124               supported_namespace = 1;
00125        }
00126 
00127        XMPP_syslog(LOG_DEBUG, "xmpp_query_namespace(%s, %s, %s, %s)\n", iq_id, iq_from, iq_to, query_xmlns);
00128 
00129        /*
00130         * Beginning of query result.
00131         */
00132        if (supported_namespace) {
00133               cprintf("<iq type=\"result\" ");
00134        }
00135        else {
00136               cprintf("<iq type=\"error\" ");
00137        }
00138        if (!IsEmptyStr(iq_from)) {
00139               cprintf("to=\"%s\" ", xmlesc(xmlbuf, iq_from, sizeof xmlbuf));
00140        }
00141        cprintf("id=\"%s\">", xmlesc(xmlbuf, iq_id, sizeof xmlbuf));
00142 
00143        /*
00144         * Is this a query we know how to handle?
00145         */
00146 
00147        if (!strcasecmp(query_xmlns, "jabber:iq:roster:query")) {
00148               roster_query = 1;
00149               xmpp_iq_roster_query();
00150        }
00151 
00152        else if (!strcasecmp(query_xmlns, "jabber:iq:auth:query")) {
00153               cprintf("<query xmlns=\"jabber:iq:auth\">"
00154                      "<username/><password/><resource/>"
00155                      "</query>"
00156               );
00157        }
00158 
00159        /*
00160         * If we didn't hit any known query namespaces then we should deliver a
00161         * "service unavailable" error (see RFC3921 section 2.4 and 11.1.5.4)
00162         */
00163 
00164        else {
00165               XMPP_syslog(LOG_DEBUG,
00166                          "Unknown query namespace '%s' - returning <service-unavailable/>\n",
00167                          query_xmlns
00168               );
00169               cprintf("<error code=\"503\" type=\"cancel\">"
00170                      "<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
00171                      "</error>"
00172               );
00173        }
00174 
00175        cprintf("</iq>");
00176 
00177        /* If we told the client who is on the roster, we also need to tell the client
00178         * who is *not* on the roster.  (It's down here because we can't do it in the same
00179         * stanza; this will be an unsolicited push.)
00180         */
00181        if (roster_query) {
00182               xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster();
00183        }
00184 }