package org.jivesoftware.openfire.plugin.gojara.messagefilter.interceptors;

import java.util.List;

import org.dom4j.Element;
import org.dom4j.Node;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.plugin.gojara.permissions.PermissionManager;
import org.jivesoftware.openfire.plugin.gojara.utils.XpathHelper;
import org.jivesoftware.openfire.session.Session;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Packet;

/**
 * 
 * If the access to external components or gateways is limited to a special
 * group in GoJara settings we have to filter the disco#infos from the server to
 * the client. If the user is not on the list we are hiding the specified info
 * and remove the item containing the gateways subdomain
 * 
 * @author Holger Bergunde
 * 
 */
public class DiscoPackageInterceptorHandler implements PacketInterceptor {

	private PermissionManager _permissions;
	private String _subDomain;
	private String _serverDomain;

	public DiscoPackageInterceptorHandler(String subdomain) {
		_permissions = new PermissionManager();
		_subDomain = subdomain;
		XMPPServer server = XMPPServer.getInstance();
		_serverDomain = server.getServerInfo().getXMPPDomain();
	}

	public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed)
			throws PacketRejectedException {
		if (_permissions.isGatewayLimited(_subDomain)) {
			if (packet instanceof IQ) {
				IQ iqpacket = (IQ) packet;
				Element root = iqpacket.getChildElement();
				if (root == null)
					return; 
				
				String ns = root.getNamespaceURI();
				if (ns.equals("http://jabber.org/protocol/disco#items") && iqpacket.getType().equals(IQ.Type.result)) {
					if (!_permissions.allowedForUser(_subDomain, iqpacket.getTo())) {
						if (iqpacket.getFrom().toString().equals(_serverDomain)) {
							List<Node> nodes = XpathHelper.findNodesInDocument(root.getDocument(), "//discoitems:item");
							for (Node node : nodes) {
								if (node.valueOf("@jid").equals(_subDomain)) {
									root.remove(node);
								}
							}
						}
					}
				}
			}
		}
	}

}

@@ -1709,14 +1709,75 @@
 ; %0D%0A
+%0D%0A
 %09%09%09%09
+if (iqpacket.getFrom().toString().equals(_serverDomain)) %7B
 %0D%0A
+%09
 %09%09%09%09
@@ -1809,24 +1809,25 @@
 paceURI();%0D%0A
+%09
 %09%09%09%09if (ns.e
@@ -1924,24 +1924,25 @@
 t)) %7B%0D%0A%09%09%09%09%09
+%09
 if (!_permis
@@ -2000,74 +2000,8 @@
  %7B%0D%0A
-%09%09%09%09%09%09if (iqpacket.getFrom().toString().equals(_serverDomain)) %7B%0D%0A
 %09%09%09%09

test for serverDomain earlier