root/trunk/src/browse.c

Revision 680, 4.1 kB (checked in by jajcus, 3 years ago)

- dates in the copyright header updated

Line 
1 /* $Id: browse.c,v 1.18 2004/04/13 17:44:07 jajcus Exp $ */
2
3 /*
4  *  (C) Copyright 2002-2006 Jacek Konieczny [jajcus(a)jajcus,net]
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License Version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include "ggtrans.h"
21 #include "jabber.h"
22 #include "iq.h"
23 #include "browse.h"
24 #include "conf.h"
25 #include "jid.h"
26 #include "sessions.h"
27 #include "debug.h"
28
29 void browse_session(gpointer key,gpointer value,gpointer data){
30 const char *jid=(char *)key;
31 const Session *sess=(Session *)value;
32 xmlnode result=(xmlnode)data;
33 xmlnode n;
34 char *str;
35
36         n=xmlnode_insert_tag(result,"item");
37         xmlnode_put_attrib(n,"category","user");
38         xmlnode_put_attrib(n,"type","client");
39         xmlnode_put_attrib(n,"jid",jid);
40         str=session_get_info_string(sess);
41         xmlnode_put_attrib(n,"name",str);
42         g_free(str);
43 }
44
45 void browse_admin(Stream *s,const char *from,const char * to, const char *id,xmlnode q){
46 xmlnode result;
47 char *jid;
48
49         jid=jid_normalized(from,0);
50         if (jid==NULL){
51                 debug(L_("Bad 'from' address"));
52                 return;
53         }
54         if (g_list_find_custom(admins,jid,(GCompareFunc)strcmp)==NULL){
55                 g_free(jid);
56                 jabber_iq_send_error(s,from,to,id,405,_("You are not allowed to browse users"));
57                 return;
58         }
59         g_free(jid);
60
61         result=xmlnode_new_tag("item");
62         xmlnode_put_attrib(result,"xmlns","jabber:iq:browse");
63         xmlnode_put_attrib(result,"jid",to);
64         xmlnode_put_attrib(result,"name",_("Online users"));
65
66         g_hash_table_foreach(sessions_jid,browse_session,result);
67
68         jabber_iq_send_result(s,from,to,id,result);
69         xmlnode_free(result);
70 }
71
72 void jabber_iq_get_server_browse(Stream *s,const char *from,const char * to,const char *id,xmlnode q){
73 xmlnode result;
74 xmlnode n;
75 char *str,*jid,*resource;
76 int i;
77
78         jid=jid_normalized(from,0);
79         if (jid==NULL){
80                 debug(L_("Bad 'from' address"));
81                 return;
82         }
83         resource=strchr(to,'/');
84         if (resource){
85                 resource++;
86                 if (!strcmp(resource,"admin"))
87                         browse_admin(s,from,to,id,q);
88                 else jabber_iq_send_error(s,from,to,id,404,_("Browse request for unknown resource"));
89                 return;
90         }
91         result=xmlnode_new_tag("item");
92         xmlnode_put_attrib(result,"xmlns","jabber:iq:browse");
93         xmlnode_put_attrib(result,"category","service");
94         xmlnode_put_attrib(result,"type","x-gadugadu");
95         xmlnode_put_attrib(result,"jid",my_name);
96         xmlnode_put_attrib(result,"version",VERSION);
97         n=xmlnode_get_tag(config,"vCard/FN");
98         if (n){
99                 str=xmlnode_get_data(n);
100                 if (str) xmlnode_put_attrib(result,"name",str);
101         }
102
103         for(i=0;server_iq_ns[i].ns;i++){
104                 if (i && !strcmp(server_iq_ns[i-1].ns,server_iq_ns[i].ns)){
105                         /* workaround for the WinJab bug :-) */
106                         continue;
107                 }
108                 n=xmlnode_insert_tag(result,"ns");
109                 xmlnode_insert_cdata(n,server_iq_ns[i].ns,-1);
110         }
111         if (g_list_find_custom(admins,jid,(GCompareFunc)strcmp)){
112                 n=xmlnode_insert_tag(result,"item");
113                 str=g_strdup_printf("%s/admin",my_name);
114                 xmlnode_put_attrib(n,"jid",str);
115                 xmlnode_put_attrib(n,"name",_("Online users"));
116         }
117         g_free(jid);
118         jabber_iq_send_result(s,from,to,id,result);
119         xmlnode_free(result);
120 }
121
122 void jabber_iq_get_client_browse(Stream *s,const char *from,const char * to,const char *id,xmlnode q){
123 xmlnode result;
124 xmlnode n;
125 int i;
126
127         result=xmlnode_new_tag("item");
128         xmlnode_put_attrib(result,"xmlns","jabber:iq:browse");
129         xmlnode_put_attrib(result,"category","user");
130         xmlnode_put_attrib(result,"type","client");
131         xmlnode_put_attrib(result,"jid",to);
132         for(i=0;client_iq_ns[i].ns;i++){
133                 if (i && !strcmp(client_iq_ns[i-1].ns,client_iq_ns[i].ns)){
134                         /* workaround for the WinJab bug workaround :-) */
135                         continue;
136                 }
137                 n=xmlnode_insert_tag(result,"ns");
138                 xmlnode_insert_cdata(n,client_iq_ns[i].ns,-1);
139         }
140         jabber_iq_send_result(s,from,to,id,result);
141         xmlnode_free(result);
142 }
143
Note: See TracBrowser for help on using the browser.