/*
 * Tigase Jabber/XMPP Multi-User Chat Component
 * Copyright (C) 2008 "Bartosz M. Małkowski" <bartosz.malkowski@tigase.org>
 *
 * 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 3 of the License.
 *
 * 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. Look for COPYING file in the top folder.
 * If not, see http://www.gnu.org/licenses/.
 *
 * $Rev$
 * Last modified by $Author$
 * $Date$
 */
package tigase.muc.modules;

import java.util.List;
import java.util.Set;

import tigase.criteria.Criteria;
import tigase.criteria.ElementCriteria;
import tigase.muc.MucConfig;
import tigase.muc.Room;
import tigase.muc.exceptions.MUCException;
import tigase.muc.repository.IMucRepository;
import tigase.xml.Element;
import tigase.xmpp.Authorization;

/**
 * @author bmalkow
 * 
 */
public class DiscoInfoModule extends AbstractModule {

	private static final Criteria CRIT = ElementCriteria.nameType("iq", "get").add(
			ElementCriteria.name("query", "http://jabber.org/protocol/disco#info"));

	private static void addFeature(Element query, String feature) {
		query.addChild(new Element("feature", new String[] { "var" }, new String[] { feature }));
	}

	private final Set<String> features;

	public DiscoInfoModule(MucConfig config, IMucRepository mucRepository, Set<String> features) {
		super(config, mucRepository);
		this.features = features;
	}

	@Override
	public String[] getFeatures() {
		return null;
	}

	@Override
	public Criteria getModuleCriteria() {
		return CRIT;
	}

	@Override
	public List<Element> process(Element element) throws MUCException {
		try {
			final String roomID = getRoomId(element.getAttribute("to"));
			Element result = createResultIQ(element);
			Element resultQuery = new Element("query", new String[] { "xmlns" },
					new String[] { "http://jabber.org/protocol/disco#info" });
			result.addChild(resultQuery);

			if (roomID == null) {
				Element resultIdentity = new Element("identity", new String[] { "category", "name", "type" }, new String[] {
						"conference", "Multi User Chat", "text" });
				resultQuery.addChild(resultIdentity);
				if (this.features != null) {
					for (String featur : this.features) {
						result.addChild(new Element("feature", new String[] { "var" }, new String[] { featur }));
					}
				}
			} else {
				Room room = repository.getRoom(roomID);
				if (room == null) {
					throw new MUCException(Authorization.ITEM_NOT_FOUND);
				}

				Element resultIdentity = new Element("identity", new String[] { "category", "name", "type" }, new String[] {
						"conference", room.getConfig().getRoomName(), "text" });
				resultQuery.addChild(resultIdentity);

				addFeature(resultQuery, "http://jabber.org/protocol/muc");

				switch (room.getConfig().getRoomAnonymity()) {
				case fullanonymous:
					addFeature(resultQuery, "muc_fullyanonymous");
					break;
				case semianonymous:
					addFeature(resultQuery, "muc_semianonymous");
					break;
				case nonanonymous:
					addFeature(resultQuery, "muc_nonanonymous");
					break;
				}

				if (room.getConfig().isRoomModerated()) {
					addFeature(resultQuery, "muc_moderated");
				} else {
					addFeature(resultQuery, "muc_unmoderated");
				}

				if (room.getConfig().isRoomMembersOnly()) {
					addFeature(resultQuery, "muc_membersonly");
				} else {
					addFeature(resultQuery, "muc_open");
				}

				if (room.getConfig().isPersistentRoom()) {
					addFeature(resultQuery, "muc_persistent");
				} else {
					addFeature(resultQuery, "muc_temporary");
				}

			}
			return makeArray(result);
		} catch (MUCException e1) {
			throw e1;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

}

@@ -963,32 +963,57 @@
 lementCriteria;%0A
+import tigase.form.Form;%0A
 import tigase.mu
@@ -3946,16 +3946,354 @@
 %0A%09%09%09%09%7D%0A%0A
+%09%09%09%09if (!room.getConfig().isRoomconfigPublicroom()) %7B%0A%09%09%09%09%09addFeature(resultQuery, %22muc_hidden%22);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09addFeature(resultQuery, %22muc_public%22);%0A%09%09%09%09%7D%0A%0A%09%09%09%09if (room.getConfig().isPasswordProtectedRoom()) %7B%0A%09%09%09%09%09addFeature(resultQuery, %22muc_passwordprotected%22);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09addFeature(resultQuery, %22muc_unsecured%22);%0A%09%09%09%09%7D%0A%0A
 %09%09%09%7D%0A%09%09%09

add room features