1 | /* |
---|
2 | * Jeti, a Java Jabber client, Copyright (C) 2007 E.S. de Boer |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
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. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | * |
---|
18 | * For questions, comments etc, |
---|
19 | * use the website at http://jeti.jabberstudio.org |
---|
20 | */ |
---|
21 | |
---|
22 | //created on Feb 21, 2007 |
---|
23 | package nu.fw.jeti.backend; |
---|
24 | |
---|
25 | |
---|
26 | import java.util.ArrayList; |
---|
27 | import java.util.HashMap; |
---|
28 | import java.util.Iterator; |
---|
29 | import java.util.LinkedList; |
---|
30 | import java.util.List; |
---|
31 | import java.util.Map; |
---|
32 | |
---|
33 | import javax.naming.LinkLoopException; |
---|
34 | |
---|
35 | import nu.fw.jeti.jabber.Backend; |
---|
36 | import nu.fw.jeti.jabber.elements.DiscoIdentity; |
---|
37 | import nu.fw.jeti.jabber.elements.IQDiscoInfo; |
---|
38 | import nu.fw.jeti.jabber.elements.IQXCaps; |
---|
39 | import nu.fw.jeti.jabber.elements.InfoQuery; |
---|
40 | |
---|
41 | /** |
---|
42 | * @author eric |
---|
43 | * |
---|
44 | */ |
---|
45 | public class OwnCapabilities { |
---|
46 | private IQXCaps caps; |
---|
47 | private List features; |
---|
48 | private List coreFeatures; |
---|
49 | private Map capabilities; |
---|
50 | boolean changed = true; |
---|
51 | private Backend backend; |
---|
52 | |
---|
53 | public OwnCapabilities(Backend backend){ |
---|
54 | this.backend = backend; |
---|
55 | features = new ArrayList(); |
---|
56 | coreFeatures = new ArrayList(); |
---|
57 | capabilities = new HashMap(); |
---|
58 | addCapability("http://jabber.org/protocol/disco#info"); |
---|
59 | //addCapability("jabber:iq:last"); |
---|
60 | addCapability("jabber:iq:oob"); |
---|
61 | addCapability("jabber:iq:time"); |
---|
62 | addCapability("jabber:iq:version"); |
---|
63 | addCapability("jabber:x:event"); |
---|
64 | } |
---|
65 | |
---|
66 | public void addCapability(String feature){ |
---|
67 | coreFeatures.add(feature); |
---|
68 | features.add(feature); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | public void addCapability(String capability,String feature){ |
---|
73 | features.add(feature); |
---|
74 | Object cap = capabilities.get(capability); |
---|
75 | if(cap !=null){ |
---|
76 | ((List)cap).add(feature); |
---|
77 | } |
---|
78 | else { |
---|
79 | List list = new LinkedList(); |
---|
80 | list.add(feature); |
---|
81 | capabilities.put(list,feature); |
---|
82 | } |
---|
83 | changed = true; |
---|
84 | } |
---|
85 | |
---|
86 | public void removeCapability(String capability,String feature){ |
---|
87 | features.remove(feature); |
---|
88 | capabilities.remove(capability); |
---|
89 | changed = true; |
---|
90 | } |
---|
91 | |
---|
92 | private void createCaps(){ |
---|
93 | StringBuffer buffer = new StringBuffer(); |
---|
94 | for (Iterator i = capabilities.keySet().iterator(); i.hasNext();) { |
---|
95 | buffer.append(i.next()); |
---|
96 | if(i.hasNext())buffer.append(' '); |
---|
97 | } |
---|
98 | caps = new IQXCaps("http://jeti.jabberstudio.org/caps", |
---|
99 | Start.VERSION,buffer.toString()); |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * answers disco#info requests with capabilities info |
---|
104 | * @param iq the Infoquery packet |
---|
105 | * @param info The disco#info packet |
---|
106 | */ |
---|
107 | public void answerInfoRequest(InfoQuery iq, IQDiscoInfo info){ |
---|
108 | List identities = new LinkedList(); |
---|
109 | identities.add(new DiscoIdentity("client","pc",null)); |
---|
110 | IQDiscoInfo returnInfo = null; |
---|
111 | String node = info.getNode(); |
---|
112 | if(("http://jeti.jabberstudio.org/caps#"+Start.VERSION).equals(node)){ |
---|
113 | returnInfo = new IQDiscoInfo(info.getNode(),identities,coreFeatures); |
---|
114 | } |
---|
115 | else if(node!=null && node.startsWith("http://jeti.jabberstudio.org/caps#")){ |
---|
116 | List list =(List) capabilities.get(node.substring(node.lastIndexOf('#')+1,node.length())); |
---|
117 | if(list!=null) returnInfo = new IQDiscoInfo(node,identities,list); |
---|
118 | } |
---|
119 | else returnInfo = new IQDiscoInfo(node,identities,features); |
---|
120 | |
---|
121 | backend.send(new InfoQuery(iq.getFrom(),"result",iq.getID(),returnInfo)); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * gets a capabilties packet with all correct attributes set |
---|
126 | * @return the capabilities packet |
---|
127 | */ |
---|
128 | public IQXCaps getCaps(){ |
---|
129 | if(changed){ |
---|
130 | createCaps(); |
---|
131 | changed = false; |
---|
132 | } |
---|
133 | return caps; |
---|
134 | } |
---|
135 | |
---|
136 | } |
---|