/* * Jeti, a Java Jabber client, Copyright (C) 2007 E.S. de Boer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * For questions, comments etc, * use the website at http://jeti.jabberstudio.org */ //created on Feb 21, 2007 package nu.fw.jeti.backend; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import javax.naming.LinkLoopException; import nu.fw.jeti.jabber.Backend; import nu.fw.jeti.jabber.elements.DiscoIdentity; import nu.fw.jeti.jabber.elements.IQDiscoInfo; import nu.fw.jeti.jabber.elements.IQXCaps; import nu.fw.jeti.jabber.elements.InfoQuery; /** * @author eric * */ public class OwnCapabilities { private IQXCaps caps; private List features; private List coreFeatures; private Map capabilities; boolean changed = true; private Backend backend; public OwnCapabilities(Backend backend){ this.backend = backend; features = new ArrayList(); coreFeatures = new ArrayList(); capabilities = new HashMap(); addCapability("http://jabber.org/protocol/disco#info"); //addCapability("jabber:iq:last"); addCapability("jabber:iq:oob"); addCapability("jabber:iq:time"); addCapability("jabber:iq:version"); addCapability("jabber:x:event"); } public void addCapability(String feature){ coreFeatures.add(feature); features.add(feature); } public void addCapability(String capability,String feature){ features.add(feature); Object cap = capabilities.get(capability); if(cap !=null){ ((List)cap).add(feature); } else { List list = new LinkedList(); list.add(feature); capabilities.put(list,feature); } changed = true; } public void removeCapability(String capability,String feature){ features.remove(feature); capabilities.remove(capability); changed = true; } private void createCaps(){ StringBuffer buffer = new StringBuffer(); for (Iterator i = capabilities.keySet().iterator(); i.hasNext();) { buffer.append(i.next()); if(i.hasNext())buffer.append(' '); } caps = new IQXCaps("http://jeti.jabberstudio.org/caps", Start.VERSION,buffer.toString()); } /** * answers disco#info requests with capabilities info * @param iq the Infoquery packet * @param info The disco#info packet */ public void answerInfoRequest(InfoQuery iq, IQDiscoInfo info){ List identities = new LinkedList(); identities.add(new DiscoIdentity("client","pc",null)); IQDiscoInfo returnInfo = null; String node = info.getNode(); if(("http://jeti.jabberstudio.org/caps#"+Start.VERSION).equals(node)){ returnInfo = new IQDiscoInfo(info.getNode(),identities,coreFeatures); } else if(node!=null && node.startsWith("http://jeti.jabberstudio.org/caps#")){ List list =(List) capabilities.get(node.substring(node.lastIndexOf('#')+1,node.length())); if(list!=null) returnInfo = new IQDiscoInfo(node,identities,list); } else returnInfo = new IQDiscoInfo(node,identities,features); backend.send(new InfoQuery(iq.getFrom(),"result",iq.getID(),returnInfo)); } /** * gets a capabilties packet with all correct attributes set * @return the capabilities packet */ public IQXCaps getCaps(){ if(changed){ createCaps(); changed = false; } return caps; } }