Did away with lprintf all together now its called CtdlLogPrintf()
[citadel.git] / citadel / modules / jabber / xmpp_query_namespace.c
1 /*
2  * $Id$ 
3  *
4  * Handle <iq> <get> <query> type situations (namespace queries)
5  *
6  * Copyright (c) 2007 by Art Cancro
7  * This code is released under the terms of the GNU General Public License.
8  *
9  */
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <pwd.h>
18 #include <errno.h>
19 #include <sys/types.h>
20
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 #  include <sys/time.h>
27 # else
28 #  include <time.h>
29 # endif
30 #endif
31
32 #include <sys/wait.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <ctype.h>
36 #include <expat.h>
37 #include <libcitadel.h>
38 #include "citadel.h"
39 #include "server.h"
40 #include "citserver.h"
41 #include "support.h"
42 #include "config.h"
43 #include "internet_addressing.h"
44 #include "md5.h"
45 #include "ctdl_module.h"
46 #include "serv_xmpp.h"
47
48
49 /*
50  * Output a single roster item, for roster queries or pushes
51  */
52 void jabber_roster_item(struct CitContext *cptr) {
53         cprintf("<item jid=\"%s\" name=\"%s\" subscription=\"both\">",
54                 cptr->cs_inet_email,
55                 cptr->user.fullname
56         );
57         cprintf("<group>%s</group>", config.c_humannode);
58         cprintf("</item>");
59 }
60
61 /* 
62  * Return the results for a "jabber:iq:roster:query"
63  *
64  * Since we are not yet managing a roster, we simply return the entire wholist
65  * (minus any entries for this user -- don't tell me about myself)
66  *
67  */
68 void jabber_iq_roster_query(void)
69 {
70         struct CitContext *cptr;
71         int nContexts, i;
72         int aide = (CC->user.axlevel >= 6);
73
74         cprintf("<query xmlns=\"jabber:iq:roster\">");
75
76         cptr = CtdlGetContextArray(&nContexts);
77         if (!cptr)
78                 return ; /** FIXME: Does jabber need to send something to maintain the protocol?  */
79                 
80         for (i=0; i<nContexts; i++) {
81                 if (cptr[i].logged_in) {
82                         if (
83                            (((cptr[i].cs_flags&CS_STEALTH)==0) || (aide))
84                            && (cptr[i].user.usernum != CC->user.usernum)
85                            ) {
86                                 jabber_roster_item(&cptr[i]);
87                         }
88                 }
89         }
90         free (cptr);
91         cprintf("</query>");
92 }
93
94
95 /*
96  * TODO: handle queries on some or all of these namespaces
97  *
98 xmpp_query_namespace(purple5b5c1e58, splorph.xand.com, http://jabber.org/protocol/disco#items:query)
99 xmpp_query_namespace(purple5b5c1e59, splorph.xand.com, http://jabber.org/protocol/disco#info:query)
100 xmpp_query_namespace(purple5b5c1e5a, , vcard-temp:query)
101  *
102  */
103
104 void xmpp_query_namespace(char *iq_id, char *iq_from, char *iq_to, char *query_xmlns) {
105
106         CtdlLogPrintf(CTDL_DEBUG, "xmpp_query_namespace(%s, %s, %s, %s)\n", iq_id, iq_from, iq_to, query_xmlns);
107
108         /*
109          * Beginning of query result.
110          */
111         cprintf("<iq type=\"result\" ");
112         if (!IsEmptyStr(iq_from)) {
113                 cprintf("to=\"%s\" ", iq_from);
114         }
115         cprintf("id=\"%s\">", iq_id);
116
117         /*
118          * Is this a query we know how to handle?
119          */
120
121         if (!strcasecmp(query_xmlns, "jabber:iq:roster:query")) {
122                 jabber_iq_roster_query();
123         }
124
125         else if (!strcasecmp(query_xmlns, "jabber:iq:auth:query")) {
126                 cprintf("<query xmlns=\"jabber:iq:auth\">"
127                         "<username/><password/><resource/>"
128                         "</query>"
129                 );
130         }
131
132         /*
133          * End of query result.  If we didn't hit any known namespaces then we will
134          * have simply delivered an empty result stanza, which should be ok.
135          */
136         cprintf("</iq>");
137
138 }