diff --git a/documentation/extensions/index.md b/documentation/extensions/index.md index adc963263..33c0a7eb5 100644 --- a/documentation/extensions/index.md +++ b/documentation/extensions/index.md @@ -87,6 +87,7 @@ Experimental Smack Extensions and currently supported XEPs of smack-experimental | [Internet of Things - Discovery](iot.md) | [XEP-0347](http://xmpp.org/extensions/xep-0347.html) | Describes how Things can be installed and discovered by their owners. | | Client State Indication | [XEP-0352](http://xmpp.org/extensions/xep-0352.html) | A way for the client to indicate its active/inactive state. | | [Push Notifications](pushnotifications.md) | [XEP-0357](http://xmpp.org/extensions/xep-0357.html) | Defines a way to manage push notifications from an XMPP Server. | +| [Multi-User Chat Light](muclight.md) | [XEP-xxxx](http://mongooseim.readthedocs.io/en/latest/open-extensions/xeps/xep-muc-light.html) | Multi-User Chats for mobile XMPP applications and specific enviroment. | | Google GCM JSON payload | n/a | Semantically the same as XEP-0335: JSON Containers | diff --git a/documentation/extensions/muclight.md b/documentation/extensions/muclight.md new file mode 100644 index 000000000..23ce9921c --- /dev/null +++ b/documentation/extensions/muclight.md @@ -0,0 +1,356 @@ +Multi-User Chat Light +===================== + +Allows configuration of, participation in, and administration of presenceĀ­less MultiĀ­-User Chats. +Its feature set is a response to mobile XMPP applications needs and specific environment. + + * Obtain the MUC Light Manager + * Obtain a MUC Light + * Create a new Room + * Destroy a room + * Leave a room + * Change room name + * Change room subject + * Set room configurations + * Manage changes on room name, subject and other configurations + * Get room information + * Manage room occupants + * Manage occupants modifications + * Discover MUC Light support + * Get occupied rooms + * Start a private chat + * Send message to a room + * Manage blocking list + +**XEP related:** [XEP-xxxx](http://mongooseim.readthedocs.io/en/latest/open-extensions/xeps/xep-muc-light.html) + + +Obtain the MUC Light Manager +---------------------------- + +``` +MultiUserChatLightManager multiUserChatLightManager = MultiUserChatLightManager.getInstanceFor(connection); +``` + +Obtain a MUC Light +------------------ + +``` +MultiUserChatLight multiUserChatLight = multiUserChatLightManager.getMultiUserChatLight(roomJid); +``` +`roomJid` is a EntityBareJid + + +Create a new room +----------------- + +``` +multiUserChatLight.create(roomName, occupants); +``` +or +``` +multiUserChatLight.create(roomName, subject, customConfigs, occupants); +``` + +*roomName* is a `String` + +*subject* is a `String` + +*customConfigs* is a `HashMap` + +*occupants* is a `List` + + +Destroy a room +--------------- + +``` +multiUserChatLight.destroy(); +``` + + +Leave a room +------------- + +``` +multiUserChatLight.leave(); +``` + + +Change room name +---------------- + +``` +multiUserChatLight.changeRoomName(roomName); +``` +*roomName* is a `String` + + +Change subject +-------------- + +``` +multiUserChatLight.changeSubject(subject); +``` +*subject* is a `String` + + +Set room configurations +----------------------- + +``` +multiUserChatLight.setRoomConfigs(customConfigs); +``` +or +``` +multiUserChatLight.setRoomConfigs(roomName, customConfigs); +``` +*customConfigs* is a `HashMap` (which means [property name, value]) + +*roomName* is a `String` + + +Manage changes on room name, subject and other configurations +------------------------------------------------------------- + +``` +// check if the message is because of a configurations change +if (message.hasExtension(MUCLightElements.ConfigurationsChangeExtension.ELEMENT, MUCLightElements.ConfigurationsChangeExtension.NAMESPACE)) { + + // Get the configurations extension + MUCLightElements.ConfigurationsChangeExtension configurationsChangeExtension = MUCLightElements.ConfigurationsChangeExtension.from(message); + + // Get new room name + String roomName = configurationsChangeExtension.getRoomName(); + + // Get new subject + String subject = configurationsChangeExtension.getSubject(); + + // Get new custom configurations + HashMap customConfigs = configurationsChangeExtension.getCustomConfigs(); + +} +``` + + +Get room information +-------------------- + +**Get configurations** + +``` +MUCLightRoomConfiguration configuration = multiUserChatLight.getConfiguration(version); +``` +*version* is a `String` + +or +``` +MUCLightRoomConfiguration configuration = multiUserChatLight.getConfiguration(); +``` + +``` + // Get room name + String roomName = configuration.getRoomName(); + + // Get subject + String subject = configuration.getSubject(); + + // Get custom configurations + HashMap customConfigs = configuration.getCustomConfigs(); +``` + +**Get affiliations** + +``` +HashMap affiliations = multiUserChatLight.getAffiliations(version); +``` +*version* is a `String` + +or +``` +HashMap affiliations = multiUserChatLight.getAffiliations(); +``` + +**Get full information** + +``` +MUCLightRoomInfo info = multiUserChatLight.getFullInfo(version); +``` +*version* is a `String` + +or +``` +MUCLightRoomInfo info = multiUserChatLight.getFullInfo(); +``` +``` +// Get version +String version = info.getVersion(); + +// Get room +Jid room = info.getRoom(); + +// Get configurations +MUCLightRoomConfiguration configuration = info.getConfiguration(); + +// Get occupants +HashMap occupants = info.getOccupants(); +``` + + +Manage room occupants +--------------------- + +To change room occupants: +``` +multiUserChatLight.changeAffiliations(affiliations); +``` +*affiliations* is a `HashMap` + + +Manage occupants modifications +------------------------------ + +``` +// check if the message is because of an affiliations change +if (message.hasExtension(MUCLightElements.AffiliationsChangeExtension.ELEMENT, MUCLightElements.AffiliationsChangeExtension.NAMESPACE)) { + + // Get the affiliations change extension + MUCLightElements.AffiliationsChangeExtension affiliationsChangeExtension = MUCLightElements.AffiliationsChangeExtension.from(message); + + // Get the new affiliations + HashMap affiliations = affiliationsChangeExtension.getAffiliations(); + +} +``` + + +Discover MUC Light support +-------------------------- + +**Check if MUC Light feature is supported by the server** + +``` +boolean isSupported = multiUserChatLightManager.isFeatureSupported(mucLightService); +``` +*mucLightService* is a `DomainBareJid` + +**Get MUC Light services domains** + +``` +List domains = multiUserChatLightManager.getLocalServices(); +``` + + +Get occupied rooms +------------------ + +``` +List occupiedRooms = multiUserChatLightManager.getOccupiedRooms(mucLightService); +``` +*mucLightService* is a `DomainBareJid` + + +Start a private chat +-------------------- + +``` +Chat chat = multiUserChatLight.createPrivateChat(occupant, listener); +``` +*occupant* is a `EntityJid` + +*listener* is a `ChatMessageListener` + + +Send message to a room +---------------------- + +**Create message for an specific MUC Light** + +``` +Message message = multiUserChatLight.createMessage(); +``` + +**Send a message to an specific MUC Light** + +``` +multiUserChatLight.sendMessage(message); +``` +*message* is a `Message` + + +Manage blocking list +-------------------- + +**Get blocked list** + +``` +// Get users and rooms blocked +List jids = multiUserChatLightManager.getUsersAndRoomsBlocked(mucLightService); + +// Get rooms blocked +List jids = multiUserChatLightManager.getRoomsBlocked(mucLightService); + +// Get users blocked +List jids = multiUserChatLightManager.getUsersBlocked(mucLightService); +``` +*mucLightService* is a `DomainBareJid` + +**Block rooms** + +``` +// Block one room +multiUserChatLightManager.blockRoom(mucLightService, roomJid); + +// Block several rooms +multiUserChatLightManager.blockRooms(mucLightService, roomsJids); +``` +*mucLightService* is a `DomainBareJid` + +*roomJid* is a `Jid` + +*roomsJids* is a `List` + +**Block users** + +``` +// Block one user +multiUserChatLightManager.blockUser(mucLightService, userJid); + +// Block several users +multiUserChatLightManager.blockUsers(mucLightService, usersJids); +``` +*mucLightService* is a `DomainBareJid` + +*userJid* is a `Jid` + +*usersJids* is a `List` + +**Unblock rooms** + +``` +// Unblock one room +multiUserChatLightManager.unblockRoom(mucLightService, roomJid); + +// Unblock several rooms +multiUserChatLightManager.unblockRooms(mucLightService, roomsJids); +``` +*mucLightService* is a `DomainBareJid` + +*roomJid* is a `Jid` + +*roomsJids* is a `List` + +**Unblock users** + +``` +// Unblock one user +multiUserChatLightManager.unblockUser(mucLightService, userJid); + +// Unblock several users +multiUserChatLightManager.unblockUsers(mucLightService, usersJids); +``` +*mucLightService* is a `DomainBareJid` + +*userJid* is a `Jid` + +*usersJids* is a `List` diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MUCLightAffiliation.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MUCLightAffiliation.java new file mode 100644 index 000000000..4a5e04384 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MUCLightAffiliation.java @@ -0,0 +1,40 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import java.util.Locale; + +/** + * MUCLight affiliations enum. + * + * @author Fernando Ramirez + * + */ +public enum MUCLightAffiliation { + + owner, + member, + none; + + public static MUCLightAffiliation fromString(String string) { + if (string == null) { + return null; + } + return MUCLightAffiliation.valueOf(string.toLowerCase(Locale.US)); + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MUCLightRoomConfiguration.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MUCLightRoomConfiguration.java new file mode 100644 index 000000000..4bb32e81b --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MUCLightRoomConfiguration.java @@ -0,0 +1,73 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import java.util.HashMap; + +/** + * MUC Light room configuration class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightRoomConfiguration { + + private final String roomName; + private final String subject; + private final HashMap customConfigs; + + /** + * MUC Light room configuration model constructor. + * + * @param roomName + * @param subject + * @param customConfigs + */ + public MUCLightRoomConfiguration(String roomName, String subject, HashMap customConfigs) { + this.roomName = roomName; + this.subject = subject; + this.customConfigs = customConfigs; + } + + /** + * Returns the room name. + * + * @return the name of the room. + */ + public String getRoomName() { + return roomName; + } + + /** + * Returns the room subject. + * + * @return the subject of the room. + */ + public String getSubject() { + return subject; + } + + /** + * Returns the room custom configurations. + * + * @return the custom configurations of the room. + */ + public HashMap getCustomConfigs() { + return customConfigs; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MUCLightRoomInfo.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MUCLightRoomInfo.java new file mode 100644 index 000000000..486b55d05 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MUCLightRoomInfo.java @@ -0,0 +1,86 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import java.util.HashMap; +import org.jxmpp.jid.Jid; + +/** + * MUC Light room info class. + * + * @author Fernando Ramirez + */ +public class MUCLightRoomInfo { + + private final String version; + private final Jid room; + private final MUCLightRoomConfiguration configuration; + private final HashMap occupants; + + /** + * MUC Light room info model constructor. + * + * @param version + * @param roomJid + * @param configuration + * @param occupants + */ + public MUCLightRoomInfo(String version, Jid roomJid, MUCLightRoomConfiguration configuration, + HashMap occupants) { + this.version = version; + this.room = roomJid; + this.configuration = configuration; + this.occupants = occupants; + } + + /** + * Returns the version. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * Returns the JID of the room whose information was discovered. + * + * @return the JID of the room whose information was discovered. + */ + public Jid getRoom() { + return room; + } + + /** + * Returns the configuration. + * + * @return the room configuration + */ + public MUCLightRoomConfiguration getConfiguration() { + return configuration; + } + + /** + * Returns the room occupants. + * + * @return the occupants of the room. + */ + public HashMap getOccupants() { + return occupants; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MultiUserChatLight.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MultiUserChatLight.java new file mode 100644 index 000000000..f62afbcd7 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MultiUserChatLight.java @@ -0,0 +1,502 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import java.util.HashMap; +import java.util.List; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; + +import org.jivesoftware.smack.MessageListener; +import org.jivesoftware.smack.PacketCollector; +import org.jivesoftware.smack.SmackException.NoResponseException; +import org.jivesoftware.smack.SmackException.NotConnectedException; +import org.jivesoftware.smack.StanzaListener; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException.XMPPErrorException; +import org.jivesoftware.smack.chat.Chat; +import org.jivesoftware.smack.chat.ChatManager; +import org.jivesoftware.smack.chat.ChatMessageListener; +import org.jivesoftware.smack.filter.AndFilter; +import org.jivesoftware.smack.filter.FromMatchesFilter; +import org.jivesoftware.smack.filter.MessageTypeFilter; +import org.jivesoftware.smack.filter.StanzaFilter; +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.packet.Stanza; +import org.jivesoftware.smackx.muclight.element.MUCLightAffiliationsIQ; +import org.jivesoftware.smackx.muclight.element.MUCLightChangeAffiliationsIQ; +import org.jivesoftware.smackx.muclight.element.MUCLightConfigurationIQ; +import org.jivesoftware.smackx.muclight.element.MUCLightCreateIQ; +import org.jivesoftware.smackx.muclight.element.MUCLightDestroyIQ; +import org.jivesoftware.smackx.muclight.element.MUCLightGetAffiliationsIQ; +import org.jivesoftware.smackx.muclight.element.MUCLightGetConfigsIQ; +import org.jivesoftware.smackx.muclight.element.MUCLightGetInfoIQ; +import org.jivesoftware.smackx.muclight.element.MUCLightInfoIQ; +import org.jivesoftware.smackx.muclight.element.MUCLightSetConfigsIQ; +import org.jxmpp.jid.EntityJid; +import org.jxmpp.jid.Jid; + +/** + * MUCLight class. + * + * @author Fernando Ramirez + */ +public class MultiUserChatLight { + + public static final String NAMESPACE = "urn:xmpp:muclight:0"; + + public static final String AFFILIATIONS = "#affiliations"; + public static final String INFO = "#info"; + public static final String CONFIGURATION = "#configuration"; + public static final String CREATE = "#create"; + public static final String DESTROY = "#destroy"; + public static final String BLOCKING = "#blocking"; + + private final XMPPConnection connection; + private final EntityJid room; + + private final Set messageListeners = new CopyOnWriteArraySet(); + + /** + * This filter will match all stanzas send from the groupchat or from one if + * the groupchat occupants. + */ + private final StanzaFilter fromRoomFilter; + + /** + * Same as {@link #fromRoomFilter} together with + * {@link MessageTypeFilter#GROUPCHAT}. + */ + private final StanzaFilter fromRoomGroupchatFilter; + + private final StanzaListener messageListener; + + private PacketCollector messageCollector; + + MultiUserChatLight(XMPPConnection connection, EntityJid room) { + this.connection = connection; + this.room = room; + + fromRoomFilter = FromMatchesFilter.create(room); + fromRoomGroupchatFilter = new AndFilter(fromRoomFilter, MessageTypeFilter.GROUPCHAT); + + messageListener = new StanzaListener() { + @Override + public void processPacket(Stanza packet) throws NotConnectedException { + Message message = (Message) packet; + for (MessageListener listener : messageListeners) { + listener.processMessage(message); + } + } + }; + + connection.addSyncStanzaListener(messageListener, fromRoomGroupchatFilter); + } + + /** + * Returns the JID of the room. + * + * @return the MUCLight room JID. + */ + public EntityJid getRoom() { + return room; + } + + /** + * Sends a message to the chat room. + * + * @param text + * the text of the message to send. + * @throws NotConnectedException + * @throws InterruptedException + */ + public void sendMessage(String text) throws NotConnectedException, InterruptedException { + Message message = createMessage(); + message.setBody(text); + connection.sendStanza(message); + } + + /** + * Returns a new Chat for sending private messages to a given room occupant. + * The Chat's occupant address is the room's JID (i.e. + * roomName@service/nick). The server service will change the 'from' address + * to the sender's room JID and delivering the message to the intended + * recipient's full JID. + * + * @param occupant + * occupant unique room JID (e.g. + * 'darkcave@macbeth.shakespeare.lit/Paul'). + * @param listener + * the listener is a message listener that will handle messages + * for the newly created chat. + * @return new Chat for sending private messages to a given room occupant. + */ + public Chat createPrivateChat(EntityJid occupant, ChatMessageListener listener) { + return ChatManager.getInstanceFor(connection).createChat(occupant, listener); + } + + /** + * Creates a new Message to send to the chat room. + * + * @return a new Message addressed to the chat room. + */ + public Message createMessage() { + return new Message(room, Message.Type.groupchat); + } + + /** + * Sends a Message to the chat room. + * + * @param message + * the message. + * @throws NotConnectedException + * @throws InterruptedException + */ + public void sendMessage(Message message) throws NotConnectedException, InterruptedException { + message.setTo(room); + message.setType(Message.Type.groupchat); + connection.sendStanza(message); + } + + /** + * Polls for and returns the next message. + * + * @return the next message if one is immediately available + */ + public Message pollMessage() { + return messageCollector.pollResult(); + } + + /** + * Returns the next available message in the chat. The method call will + * block (not return) until a message is available. + * + * @return the next message. + * @throws InterruptedException + */ + public Message nextMessage() throws InterruptedException { + return messageCollector.nextResult(); + } + + /** + * Returns the next available message in the chat. + * + * @param timeout + * the maximum amount of time to wait for the next message. + * @return the next message, or null if the timeout elapses without a + * message becoming available. + * @throws InterruptedException + */ + public Message nextMessage(long timeout) throws InterruptedException { + return messageCollector.nextResult(timeout); + } + + /** + * Adds a stanza(/packet) listener that will be notified of any new messages + * in the group chat. Only "group chat" messages addressed to this group + * chat will be delivered to the listener. + * + * @param listener + * a stanza(/packet) listener. + * @return true if the listener was not already added. + */ + public boolean addMessageListener(MessageListener listener) { + return messageListeners.add(listener); + } + + /** + * Removes a stanza(/packet) listener that was being notified of any new + * messages in the MUCLight. Only "group chat" messages addressed to this + * MUCLight were being delivered to the listener. + * + * @param listener + * a stanza(/packet) listener. + * @return true if the listener was removed, otherwise the listener was not + * added previously. + */ + public boolean removeMessageListener(MessageListener listener) { + return messageListeners.remove(listener); + } + + /** + * Remove the connection callbacks used by this MUC Light from the + * connection. + */ + private void removeConnectionCallbacks() { + connection.removeSyncStanzaListener(messageListener); + if (messageCollector != null) { + messageCollector.cancel(); + messageCollector = null; + } + } + + @Override + public String toString() { + return "MUC Light: " + room + "(" + connection.getUser() + ")"; + } + + /** + * Create new MUCLight. + * + * @param roomName + * @param subject + * @param customConfigs + * @param occupants + * @throws Exception + */ + public void create(String roomName, String subject, HashMap customConfigs, List occupants) + throws Exception { + MUCLightCreateIQ createMUCLightIQ = new MUCLightCreateIQ(room, roomName, occupants); + + messageCollector = connection.createPacketCollector(fromRoomGroupchatFilter); + + try { + connection.createPacketCollectorAndSend(createMUCLightIQ).nextResultOrThrow(); + } catch (InterruptedException | NoResponseException | XMPPErrorException e) { + removeConnectionCallbacks(); + throw e; + } + } + + /** + * Create new MUCLight. + * + * @param roomName + * @param occupants + * @throws Exception + */ + public void create(String roomName, List occupants) throws Exception { + create(roomName, null, null, occupants); + } + + /** + * Leave the MUCLight. + * + * @throws NotConnectedException + * @throws InterruptedException + * @throws NoResponseException + * @throws XMPPErrorException + */ + public void leave() throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException { + HashMap affiliations = new HashMap<>(); + affiliations.put(connection.getUser(), MUCLightAffiliation.none); + + MUCLightChangeAffiliationsIQ changeAffiliationsIQ = new MUCLightChangeAffiliationsIQ(room, affiliations); + IQ responseIq = connection.createPacketCollectorAndSend(changeAffiliationsIQ).nextResultOrThrow(); + boolean roomLeft = responseIq.getType().equals(IQ.Type.result); + + if (roomLeft) { + removeConnectionCallbacks(); + } + } + + /** + * Get the MUC Light info. + * + * @param version + * @return the room info + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public MUCLightRoomInfo getFullInfo(String version) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + MUCLightGetInfoIQ mucLightGetInfoIQ = new MUCLightGetInfoIQ(room, version); + + IQ responseIq = connection.createPacketCollectorAndSend(mucLightGetInfoIQ).nextResultOrThrow(); + MUCLightInfoIQ mucLightInfoResponseIQ = (MUCLightInfoIQ) responseIq; + + return new MUCLightRoomInfo(mucLightInfoResponseIQ.getVersion(), room, + mucLightInfoResponseIQ.getConfiguration(), mucLightInfoResponseIQ.getOccupants()); + } + + /** + * Get the MUC Light info. + * + * @return the room info + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public MUCLightRoomInfo getFullInfo() + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + return getFullInfo(null); + } + + /** + * Get the MUC Light configuration. + * + * @param version + * @return the room configuration + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public MUCLightRoomConfiguration getConfiguration(String version) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + MUCLightGetConfigsIQ mucLightGetConfigsIQ = new MUCLightGetConfigsIQ(room, version); + IQ responseIq = connection.createPacketCollectorAndSend(mucLightGetConfigsIQ).nextResultOrThrow(); + MUCLightConfigurationIQ mucLightConfigurationIQ = (MUCLightConfigurationIQ) responseIq; + return mucLightConfigurationIQ.getConfiguration(); + } + + /** + * Get the MUC Light configuration. + * + * @return the room configuration + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public MUCLightRoomConfiguration getConfiguration() + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + return getConfiguration(null); + } + + /** + * Get the MUC Light affiliations. + * + * @param version + * @return the room affiliations + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public HashMap getAffiliations(String version) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + MUCLightGetAffiliationsIQ mucLightGetAffiliationsIQ = new MUCLightGetAffiliationsIQ(room, version); + + IQ responseIq = connection.createPacketCollectorAndSend(mucLightGetAffiliationsIQ).nextResultOrThrow(); + MUCLightAffiliationsIQ mucLightAffiliationsIQ = (MUCLightAffiliationsIQ) responseIq; + + return mucLightAffiliationsIQ.getAffiliations(); + } + + /** + * Get the MUC Light affiliations. + * + * @return the room affiliations + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public HashMap getAffiliations() + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + return getAffiliations(null); + } + + /** + * Change the MUC Light affiliations. + * + * @param affiliations + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void changeAffiliations(HashMap affiliations) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + MUCLightChangeAffiliationsIQ changeAffiliationsIQ = new MUCLightChangeAffiliationsIQ(room, affiliations); + connection.createPacketCollectorAndSend(changeAffiliationsIQ).nextResultOrThrow(); + } + + /** + * Destroy the MUC Light. Only will work if it is requested by the owner. + * + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void destroy() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + MUCLightDestroyIQ mucLightDestroyIQ = new MUCLightDestroyIQ(room); + IQ responseIq = connection.createPacketCollectorAndSend(mucLightDestroyIQ).nextResultOrThrow(); + boolean roomDestroyed = responseIq.getType().equals(IQ.Type.result); + + if (roomDestroyed) { + removeConnectionCallbacks(); + } + } + + /** + * Change the subject of the MUC Light. + * + * @param subject + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void changeSubject(String subject) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + MUCLightSetConfigsIQ mucLightSetConfigIQ = new MUCLightSetConfigsIQ(room, null, subject, null); + connection.createPacketCollectorAndSend(mucLightSetConfigIQ).nextResultOrThrow(); + } + + /** + * Change the name of the room. + * + * @param roomName + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void changeRoomName(String roomName) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + MUCLightSetConfigsIQ mucLightSetConfigIQ = new MUCLightSetConfigsIQ(room, roomName, null); + connection.createPacketCollectorAndSend(mucLightSetConfigIQ).nextResultOrThrow(); + } + + /** + * Set the room configurations. + * + * @param customConfigs + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void setRoomConfigs(HashMap customConfigs) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + setRoomConfigs(null, customConfigs); + } + + /** + * Set the room configurations. + * + * @param roomName + * @param customConfigs + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void setRoomConfigs(String roomName, HashMap customConfigs) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + MUCLightSetConfigsIQ mucLightSetConfigIQ = new MUCLightSetConfigsIQ(room, roomName, customConfigs); + connection.createPacketCollectorAndSend(mucLightSetConfigIQ).nextResultOrThrow(); + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MultiUserChatLightManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MultiUserChatLightManager.java new file mode 100644 index 000000000..4a05da888 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/MultiUserChatLightManager.java @@ -0,0 +1,416 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; + +import org.jivesoftware.smack.Manager; +import org.jivesoftware.smack.SmackException.NoResponseException; +import org.jivesoftware.smack.SmackException.NotConnectedException; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException.XMPPErrorException; +import org.jivesoftware.smack.filter.IQReplyFilter; +import org.jivesoftware.smack.filter.StanzaFilter; +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.packet.IQ.Type; +import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; +import org.jivesoftware.smackx.disco.packet.DiscoverItems; +import org.jivesoftware.smackx.muclight.element.MUCLightBlockingIQ; +import org.jxmpp.jid.DomainBareJid; +import org.jxmpp.jid.EntityBareJid; +import org.jxmpp.jid.Jid; + +/** + * Multi-User Chat Light manager class. + * + * @author Fernando Ramirez + * + */ +public final class MultiUserChatLightManager extends Manager { + + private static final Map INSTANCES = new WeakHashMap(); + + /** + * Get a instance of a MUC Light manager for the given connection. + * + * @param connection + * @return a MUCLight manager. + */ + public static synchronized MultiUserChatLightManager getInstanceFor(XMPPConnection connection) { + MultiUserChatLightManager multiUserChatLightManager = INSTANCES.get(connection); + if (multiUserChatLightManager == null) { + multiUserChatLightManager = new MultiUserChatLightManager(connection); + INSTANCES.put(connection, multiUserChatLightManager); + } + return multiUserChatLightManager; + } + + /** + * A Map of MUC Light JIDs to instances. We use weak references for the + * values in order to allow those instances to get garbage collected. + */ + private final Map> multiUserChatLights = new HashMap<>(); + + private MultiUserChatLightManager(XMPPConnection connection) { + super(connection); + } + + /** + * Obtain the MUC Light. + * + * @param jid + * @return the MUCLight. + */ + public synchronized MultiUserChatLight getMultiUserChatLight(EntityBareJid jid) { + WeakReference weakRefMultiUserChat = multiUserChatLights.get(jid); + if (weakRefMultiUserChat == null) { + return createNewMucLightAndAddToMap(jid); + } + MultiUserChatLight multiUserChatLight = weakRefMultiUserChat.get(); + if (multiUserChatLight == null) { + return createNewMucLightAndAddToMap(jid); + } + return multiUserChatLight; + } + + private MultiUserChatLight createNewMucLightAndAddToMap(EntityBareJid jid) { + MultiUserChatLight multiUserChatLight = new MultiUserChatLight(connection(), jid); + multiUserChatLights.put(jid, new WeakReference(multiUserChatLight)); + return multiUserChatLight; + } + + /** + * Returns true if Multi-User Chat Light feature is supported by the server. + * + * @param mucLightService + * @return true if Multi-User Chat Light feature is supported by the server. + * @throws NotConnectedException + * @throws XMPPErrorException + * @throws NoResponseException + * @throws InterruptedException + */ + public boolean isFeatureSupported(DomainBareJid mucLightService) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + return ServiceDiscoveryManager.getInstanceFor(connection()).discoverInfo(mucLightService) + .containsFeature(MultiUserChatLight.NAMESPACE); + } + + /** + * Returns a List of the rooms the user occupies. + * + * @param mucLightService + * @return a List of the rooms the user occupies. + * @throws XMPPErrorException + * @throws NoResponseException + * @throws NotConnectedException + * @throws InterruptedException + */ + public List getOccupiedRooms(DomainBareJid mucLightService) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + DiscoverItems result = ServiceDiscoveryManager.getInstanceFor(connection()).discoverItems(mucLightService); + List items = result.getItems(); + List answer = new ArrayList<>(items.size()); + + for (DiscoverItems.Item item : items) { + Jid mucLight = item.getEntityID(); + answer.add(mucLight); + } + + return answer; + } + + /** + * Returns a collection with the XMPP addresses of the MUC Light services. + * + * @return a collection with the XMPP addresses of the MUC Light services. + * @throws XMPPErrorException + * @throws NoResponseException + * @throws NotConnectedException + * @throws InterruptedException + */ + public List getLocalServices() + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection()); + return sdm.findServices(MultiUserChatLight.NAMESPACE, false, false); + } + + /** + * Get users and rooms blocked. + * + * @param mucLightService + * @return the list of users and rooms blocked + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public List getUsersAndRoomsBlocked(DomainBareJid mucLightService) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + MUCLightBlockingIQ muclIghtBlockingIQResult = getBlockingList(mucLightService); + + List jids = new ArrayList<>(); + if (muclIghtBlockingIQResult.getRooms() != null) { + jids.addAll(muclIghtBlockingIQResult.getRooms().keySet()); + } + + if (muclIghtBlockingIQResult.getUsers() != null) { + jids.addAll(muclIghtBlockingIQResult.getUsers().keySet()); + } + + return jids; + } + + /** + * Get rooms blocked. + * + * @param mucLightService + * @return the list of rooms blocked + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public List getRoomsBlocked(DomainBareJid mucLightService) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + MUCLightBlockingIQ muclIghtBlockingIQResult = getBlockingList(mucLightService); + + List jids = new ArrayList<>(); + if (muclIghtBlockingIQResult.getRooms() != null) { + jids.addAll(muclIghtBlockingIQResult.getRooms().keySet()); + } + + return jids; + } + + /** + * Get users blocked. + * + * @param mucLightService + * @return the list of users blocked + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public List getUsersBlocked(DomainBareJid mucLightService) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + MUCLightBlockingIQ muclIghtBlockingIQResult = getBlockingList(mucLightService); + + List jids = new ArrayList<>(); + if (muclIghtBlockingIQResult.getUsers() != null) { + jids.addAll(muclIghtBlockingIQResult.getUsers().keySet()); + } + + return jids; + } + + private MUCLightBlockingIQ getBlockingList(DomainBareJid mucLightService) + throws NoResponseException, XMPPErrorException, InterruptedException, NotConnectedException { + MUCLightBlockingIQ mucLightBlockingIQ = new MUCLightBlockingIQ(null, null); + mucLightBlockingIQ.setType(Type.get); + mucLightBlockingIQ.setTo(mucLightService); + + StanzaFilter responseFilter = new IQReplyFilter(mucLightBlockingIQ, connection()); + IQ responseIq = connection().createPacketCollectorAndSend(responseFilter, mucLightBlockingIQ) + .nextResultOrThrow(); + MUCLightBlockingIQ muclIghtBlockingIQResult = (MUCLightBlockingIQ) responseIq; + + return muclIghtBlockingIQResult; + } + + /** + * Block a room. + * + * @param mucLightService + * @param roomJid + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void blockRoom(DomainBareJid mucLightService, Jid roomJid) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + HashMap rooms = new HashMap<>(); + rooms.put(roomJid, false); + sendBlockRooms(mucLightService, rooms); + } + + /** + * Block rooms. + * + * @param mucLightService + * @param roomsJids + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void blockRooms(DomainBareJid mucLightService, List roomsJids) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + HashMap rooms = new HashMap<>(); + for (Jid jid : roomsJids) { + rooms.put(jid, false); + } + sendBlockRooms(mucLightService, rooms); + } + + private void sendBlockRooms(DomainBareJid mucLightService, HashMap rooms) + throws NoResponseException, XMPPErrorException, InterruptedException, NotConnectedException { + MUCLightBlockingIQ mucLightBlockingIQ = new MUCLightBlockingIQ(rooms, null); + mucLightBlockingIQ.setType(Type.set); + mucLightBlockingIQ.setTo(mucLightService); + connection().createPacketCollectorAndSend(mucLightBlockingIQ).nextResultOrThrow(); + } + + /** + * Block a user. + * + * @param mucLightService + * @param userJid + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void blockUser(DomainBareJid mucLightService, Jid userJid) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + HashMap users = new HashMap<>(); + users.put(userJid, false); + sendBlockUsers(mucLightService, users); + } + + /** + * Block users. + * + * @param mucLightService + * @param usersJids + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void blockUsers(DomainBareJid mucLightService, List usersJids) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + HashMap users = new HashMap<>(); + for (Jid jid : usersJids) { + users.put(jid, false); + } + sendBlockUsers(mucLightService, users); + } + + private void sendBlockUsers(DomainBareJid mucLightService, HashMap users) + throws NoResponseException, XMPPErrorException, InterruptedException, NotConnectedException { + MUCLightBlockingIQ mucLightBlockingIQ = new MUCLightBlockingIQ(null, users); + mucLightBlockingIQ.setType(Type.set); + mucLightBlockingIQ.setTo(mucLightService); + connection().createPacketCollectorAndSend(mucLightBlockingIQ).nextResultOrThrow(); + } + + /** + * Unblock a room. + * + * @param mucLightService + * @param roomJid + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void unblockRoom(DomainBareJid mucLightService, Jid roomJid) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + HashMap rooms = new HashMap<>(); + rooms.put(roomJid, true); + sendUnblockRooms(mucLightService, rooms); + } + + /** + * Unblock rooms. + * + * @param mucLightService + * @param roomsJids + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void unblockRooms(DomainBareJid mucLightService, List roomsJids) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + HashMap rooms = new HashMap<>(); + for (Jid jid : roomsJids) { + rooms.put(jid, true); + } + sendUnblockRooms(mucLightService, rooms); + } + + private void sendUnblockRooms(DomainBareJid mucLightService, HashMap rooms) + throws NoResponseException, XMPPErrorException, InterruptedException, NotConnectedException { + MUCLightBlockingIQ mucLightBlockingIQ = new MUCLightBlockingIQ(rooms, null); + mucLightBlockingIQ.setType(Type.set); + mucLightBlockingIQ.setTo(mucLightService); + connection().createPacketCollectorAndSend(mucLightBlockingIQ).nextResultOrThrow(); + } + + /** + * Unblock a user. + * + * @param mucLightService + * @param userJid + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void unblockUser(DomainBareJid mucLightService, Jid userJid) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + HashMap users = new HashMap<>(); + users.put(userJid, true); + sendUnblockUsers(mucLightService, users); + } + + /** + * Unblock users. + * + * @param mucLightService + * @param usersJids + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void unblockUsers(DomainBareJid mucLightService, List usersJids) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + HashMap users = new HashMap<>(); + for (Jid jid : usersJids) { + users.put(jid, true); + } + sendUnblockUsers(mucLightService, users); + } + + private void sendUnblockUsers(DomainBareJid mucLightService, HashMap users) + throws NoResponseException, XMPPErrorException, InterruptedException, NotConnectedException { + MUCLightBlockingIQ mucLightBlockingIQ = new MUCLightBlockingIQ(null, users); + mucLightBlockingIQ.setType(Type.set); + mucLightBlockingIQ.setTo(mucLightService); + connection().createPacketCollectorAndSend(mucLightBlockingIQ).nextResultOrThrow(); + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightAffiliationsIQ.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightAffiliationsIQ.java new file mode 100644 index 000000000..a5b23c23f --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightAffiliationsIQ.java @@ -0,0 +1,87 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.element; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.muclight.MUCLightAffiliation; +import org.jivesoftware.smackx.muclight.MultiUserChatLight; +import org.jivesoftware.smackx.muclight.element.MUCLightElements.UserWithAffiliationElement; +import org.jxmpp.jid.Jid; + +/** + * MUC Light affiliations response IQ class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightAffiliationsIQ extends IQ { + + public static final String ELEMENT = QUERY_ELEMENT; + public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.AFFILIATIONS; + + private final String version; + private HashMap affiliations; + + /** + * MUC Light affiliations response IQ constructor. + * + * @param version + * @param affiliations + */ + public MUCLightAffiliationsIQ(String version, HashMap affiliations) { + super(ELEMENT, NAMESPACE); + this.version = version; + this.affiliations = affiliations; + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.rightAngleBracket(); + xml.optElement("version", version); + + Iterator> it = affiliations.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + xml.element(new UserWithAffiliationElement(pair.getKey(), pair.getValue())); + } + + return xml; + } + + /** + * Returns the version. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * Returns the room affiliations. + * + * @return the affiliations of the room + */ + public HashMap getAffiliations() { + return affiliations; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightBlockingIQ.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightBlockingIQ.java new file mode 100644 index 000000000..f0915a01b --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightBlockingIQ.java @@ -0,0 +1,95 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.element; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.muclight.MultiUserChatLight; +import org.jivesoftware.smackx.muclight.element.MUCLightElements.BlockingElement; +import org.jxmpp.jid.Jid; + +/** + * MUC Light blocking IQ class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightBlockingIQ extends IQ { + + public static final String ELEMENT = QUERY_ELEMENT; + public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.BLOCKING; + + private final HashMap rooms; + private final HashMap users; + + /** + * MUC Light blocking IQ constructor. + * + * @param rooms + * @param users + */ + public MUCLightBlockingIQ(HashMap rooms, HashMap users) { + super(ELEMENT, NAMESPACE); + this.rooms = rooms; + this.users = users; + } + + /** + * Get rooms JIDs with booleans (true if allow, false if deny). + * + * @return the rooms JIDs with booleans (true if allow, false if deny) + */ + public HashMap getRooms() { + return rooms; + } + + /** + * Get users JIDs with booleans (true if allow, false if deny). + * + * @return the users JIDs with booleans (true if allow, false if deny) + */ + public HashMap getUsers() { + return users; + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.rightAngleBracket(); + + if (rooms != null) { + parseBlocking(xml, rooms, true); + } + + if (users != null) { + parseBlocking(xml, users, false); + } + + return xml; + } + + private void parseBlocking(IQChildElementXmlStringBuilder xml, HashMap map, boolean isRoom) { + Iterator> it = map.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + xml.element(new BlockingElement(pair.getKey(), pair.getValue(), isRoom)); + } + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightChangeAffiliationsIQ.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightChangeAffiliationsIQ.java new file mode 100644 index 000000000..d165d24b9 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightChangeAffiliationsIQ.java @@ -0,0 +1,79 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.element; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.muclight.MUCLightAffiliation; +import org.jivesoftware.smackx.muclight.MultiUserChatLight; +import org.jivesoftware.smackx.muclight.element.MUCLightElements.UserWithAffiliationElement; +import org.jxmpp.jid.Jid; + +/** + * MUCLight change affiliations IQ class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightChangeAffiliationsIQ extends IQ { + + public static final String ELEMENT = QUERY_ELEMENT; + public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.AFFILIATIONS; + + private HashMap affiliations; + + /** + * MUCLight change affiliations IQ constructor. + * + * @param room + * @param affiliations + */ + public MUCLightChangeAffiliationsIQ(Jid room, HashMap affiliations) { + super(ELEMENT, NAMESPACE); + this.setType(Type.set); + this.setTo(room); + this.affiliations = affiliations; + } + + /** + * Get the affiliations. + * + * @return the affiliations + */ + public HashMap getAffiliations() { + return affiliations; + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.rightAngleBracket(); + + if (affiliations != null) { + Iterator> it = affiliations.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + xml.element(new UserWithAffiliationElement(pair.getKey(), pair.getValue())); + } + } + + return xml; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightConfigurationIQ.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightConfigurationIQ.java new file mode 100644 index 000000000..a0d93ce1d --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightConfigurationIQ.java @@ -0,0 +1,76 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.element; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.muclight.MUCLightRoomConfiguration; +import org.jivesoftware.smackx.muclight.MultiUserChatLight; +import org.jivesoftware.smackx.muclight.element.MUCLightElements.ConfigurationElement; + +/** + * MUC Light configuration response IQ class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightConfigurationIQ extends IQ { + + public static final String ELEMENT = QUERY_ELEMENT; + public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.CONFIGURATION; + + private final String version; + private final MUCLightRoomConfiguration configuration; + + /** + * MUC Light configuration response IQ constructor. + * + * @param version + * @param configuration + */ + public MUCLightConfigurationIQ(String version, MUCLightRoomConfiguration configuration) { + super(ELEMENT, NAMESPACE); + this.version = version; + this.configuration = configuration; + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.rightAngleBracket(); + xml.optElement("version", version); + xml.element(new ConfigurationElement(configuration)); + return xml; + } + + /** + * Returns the version. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * Returns the room configuration. + * + * @return the configuration of the room + */ + public MUCLightRoomConfiguration getConfiguration() { + return configuration; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightCreateIQ.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightCreateIQ.java new file mode 100644 index 000000000..e52e6ce1a --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightCreateIQ.java @@ -0,0 +1,109 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.element; + +import java.util.HashMap; +import java.util.List; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.muclight.MUCLightAffiliation; +import org.jivesoftware.smackx.muclight.MUCLightRoomConfiguration; +import org.jivesoftware.smackx.muclight.MultiUserChatLight; +import org.jivesoftware.smackx.muclight.element.MUCLightElements.ConfigurationElement; +import org.jivesoftware.smackx.muclight.element.MUCLightElements.OccupantsElement; +import org.jxmpp.jid.EntityJid; +import org.jxmpp.jid.Jid; + +/** + * MUCLight create IQ class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightCreateIQ extends IQ { + + public static final String ELEMENT = QUERY_ELEMENT; + public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.CREATE; + + private MUCLightRoomConfiguration configuration; + private final HashMap occupants; + + /** + * MUCLight create IQ constructor. + * + * @param room + * @param roomName + * @param subject + * @param customConfigs + * @param occupants + */ + public MUCLightCreateIQ(EntityJid room, String roomName, String subject, HashMap customConfigs, + List occupants) { + super(ELEMENT, NAMESPACE); + this.configuration = new MUCLightRoomConfiguration(roomName, subject, customConfigs); + + this.occupants = new HashMap<>(); + for (Jid occupant : occupants) { + this.occupants.put(occupant, MUCLightAffiliation.member); + } + + this.setType(Type.set); + this.setTo(room); + } + + /** + * MUCLight create IQ constructor. + * + * @param room + * @param roomName + * @param occupants + */ + public MUCLightCreateIQ(EntityJid room, String roomName, List occupants) { + this(room, roomName, null, null, occupants); + } + + /** + * Get the room configuration. + * + * @return the room configuration + */ + public MUCLightRoomConfiguration getConfiguration() { + return configuration; + } + + /** + * Get the room occupants. + * + * @return the room occupants + */ + public HashMap getOccupants() { + return occupants; + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.rightAngleBracket(); + xml.element(new ConfigurationElement(configuration)); + + if (!occupants.isEmpty()) { + xml.element(new OccupantsElement(occupants)); + } + + return xml; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightDestroyIQ.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightDestroyIQ.java new file mode 100644 index 000000000..15b027aa1 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightDestroyIQ.java @@ -0,0 +1,51 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.element; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.muclight.MultiUserChatLight; +import org.jxmpp.jid.Jid; + +/** + * MUC Light destroy IQ class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightDestroyIQ extends IQ { + + public static final String ELEMENT = QUERY_ELEMENT; + public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.DESTROY; + + /** + * MUC Light destroy IQ constructor. + * + * @param roomJid + */ + public MUCLightDestroyIQ(Jid roomJid) { + super(ELEMENT, NAMESPACE); + this.setType(Type.set); + this.setTo(roomJid); + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.setEmptyElement(); + return xml; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightElements.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightElements.java new file mode 100644 index 000000000..8df76d080 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightElements.java @@ -0,0 +1,392 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.element; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.jivesoftware.smack.packet.Element; +import org.jivesoftware.smack.packet.ExtensionElement; +import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.util.XmlStringBuilder; +import org.jivesoftware.smackx.muclight.MUCLightAffiliation; +import org.jivesoftware.smackx.muclight.MUCLightRoomConfiguration; +import org.jivesoftware.smackx.muclight.MultiUserChatLight; +import org.jivesoftware.smackx.xdata.packet.DataForm; +import org.jxmpp.jid.Jid; + +public abstract class MUCLightElements { + + /** + * Affiliations change extension element class. + * + * @author Fernando Ramirez + * + */ + public static class AffiliationsChangeExtension implements ExtensionElement { + + public static final String ELEMENT = DataForm.ELEMENT; + public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.AFFILIATIONS; + + private final HashMap affiliations; + private final String prevVersion; + private final String version; + + public AffiliationsChangeExtension(HashMap affiliations, String prevVersion, + String version) { + this.affiliations = affiliations; + this.prevVersion = prevVersion; + this.version = version; + } + + @Override + public String getElementName() { + return ELEMENT; + } + + @Override + public String getNamespace() { + return NAMESPACE; + } + + /** + * Get the affiliations. + * + * @return the affiliations + */ + public HashMap getAffiliations() { + return affiliations; + } + + /** + * Get the previous version. + * + * @return the previous version + */ + public String getPrevVersion() { + return prevVersion; + } + + /** + * Get the version. + * + * @return the version + */ + public String getVersion() { + return version; + } + + @Override + public CharSequence toXML() { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.rightAngleBracket(); + + xml.optElement("prev-version", prevVersion); + xml.optElement("version", version); + + Iterator> it = affiliations.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + xml.element(new UserWithAffiliationElement(pair.getKey(), pair.getValue())); + } + + xml.closeElement(this); + return xml; + } + + public static AffiliationsChangeExtension from(Message message) { + return message.getExtension(AffiliationsChangeExtension.ELEMENT, AffiliationsChangeExtension.NAMESPACE); + } + + } + + /** + * Configurations change extension element class. + * + * @author Fernando Ramirez + * + */ + public static class ConfigurationsChangeExtension implements ExtensionElement { + + public static final String ELEMENT = DataForm.ELEMENT; + public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.CONFIGURATION; + + private final String prevVersion; + private final String version; + private final String roomName; + private final String subject; + private final HashMap customConfigs; + + /** + * Configurations change extension constructor. + * + * @param prevVersion + * @param version + * @param roomName + * @param subject + * @param customConfigs + */ + public ConfigurationsChangeExtension(String prevVersion, String version, String roomName, String subject, + HashMap customConfigs) { + this.prevVersion = prevVersion; + this.version = version; + this.roomName = roomName; + this.subject = subject; + this.customConfigs = customConfigs; + } + + @Override + public String getElementName() { + return ELEMENT; + } + + @Override + public String getNamespace() { + return NAMESPACE; + } + + /** + * Get the previous version. + * + * @return the previous version + */ + public String getPrevVersion() { + return prevVersion; + } + + /** + * Get the version. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * Get the room name. + * + * @return the room name + */ + public String getRoomName() { + return roomName; + } + + /** + * Get the room subject. + * + * @return the room subject + */ + public String getSubject() { + return subject; + } + + /** + * Get the room custom configurations. + * + * @return the room custom configurations + */ + public HashMap getCustomConfigs() { + return customConfigs; + } + + @Override + public CharSequence toXML() { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.rightAngleBracket(); + + xml.optElement("prev-version", prevVersion); + xml.optElement("version", version); + xml.optElement("roomname", roomName); + xml.optElement("subject", subject); + + if (customConfigs != null) { + Iterator> it = customConfigs.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + xml.element(pair.getKey(), pair.getValue()); + } + } + + xml.closeElement(this); + return xml; + } + + public static ConfigurationsChangeExtension from(Message message) { + return message.getExtension(ConfigurationsChangeExtension.ELEMENT, ConfigurationsChangeExtension.NAMESPACE); + } + + } + + /** + * Configuration element class. + * + * @author Fernando Ramirez + * + */ + public static class ConfigurationElement implements Element { + + private MUCLightRoomConfiguration configuration; + + /** + * Configuration element constructor. + * + * @param configuration + */ + public ConfigurationElement(MUCLightRoomConfiguration configuration) { + this.configuration = configuration; + } + + @Override + public CharSequence toXML() { + XmlStringBuilder xml = new XmlStringBuilder(); + xml.openElement("configuration"); + + xml.element("roomname", configuration.getRoomName()); + xml.optElement("subject", configuration.getSubject()); + + if (configuration.getCustomConfigs() != null) { + Iterator> it = configuration.getCustomConfigs().entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + xml.element(pair.getKey(), pair.getValue()); + } + } + + xml.closeElement("configuration"); + return xml; + } + + } + + /** + * Occupants element class. + * + * @author Fernando Ramirez + * + */ + public static class OccupantsElement implements Element { + + private HashMap occupants; + + /** + * Occupants element constructor. + * + * @param occupants + */ + public OccupantsElement(HashMap occupants) { + this.occupants = occupants; + } + + @Override + public CharSequence toXML() { + XmlStringBuilder xml = new XmlStringBuilder(); + xml.openElement("occupants"); + + Iterator> it = occupants.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + xml.element(new UserWithAffiliationElement(pair.getKey(), pair.getValue())); + } + + xml.closeElement("occupants"); + return xml; + } + + } + + /** + * User with affiliation element class. + * + * @author Fernando Ramirez + * + */ + public static class UserWithAffiliationElement implements Element { + + private Jid user; + private MUCLightAffiliation affiliation; + + /** + * User with affiliations element constructor. + * + * @param user + * @param affiliation + */ + public UserWithAffiliationElement(Jid user, MUCLightAffiliation affiliation) { + this.user = user; + this.affiliation = affiliation; + } + + @Override + public CharSequence toXML() { + XmlStringBuilder xml = new XmlStringBuilder(); + xml.halfOpenElement("user"); + xml.attribute("affiliation", affiliation); + xml.rightAngleBracket(); + xml.escape(user); + xml.closeElement("user"); + return xml; + } + + } + + /** + * Blocking element class. + * + * @author Fernando Ramirez + * + */ + public static class BlockingElement implements Element { + + private Jid jid; + private Boolean allow; + private Boolean isRoom; + + /** + * Blocking element constructor. + * + * @param jid + * @param allow + * @param isRoom + */ + public BlockingElement(Jid jid, Boolean allow, Boolean isRoom) { + this.jid = jid; + this.allow = allow; + this.isRoom = isRoom; + } + + @Override + public CharSequence toXML() { + XmlStringBuilder xml = new XmlStringBuilder(); + + String tag = isRoom ? "room" : "user"; + xml.halfOpenElement(tag); + + String action = allow ? "allow" : "deny"; + xml.attribute("action", action); + xml.rightAngleBracket(); + + xml.escape(jid); + + xml.closeElement(tag); + return xml; + } + + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightGetAffiliationsIQ.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightGetAffiliationsIQ.java new file mode 100644 index 000000000..a50d93201 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightGetAffiliationsIQ.java @@ -0,0 +1,65 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.element; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.muclight.MultiUserChatLight; +import org.jxmpp.jid.Jid; + +/** + * MUC Light get affiliations IQ class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightGetAffiliationsIQ extends IQ { + + public static final String ELEMENT = QUERY_ELEMENT; + public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.AFFILIATIONS; + + private String version; + + /** + * MUC Light get affiliations IQ constructor. + * + * @param roomJid + * @param version + */ + public MUCLightGetAffiliationsIQ(Jid roomJid, String version) { + super(ELEMENT, NAMESPACE); + this.version = version; + this.setType(Type.get); + this.setTo(roomJid); + } + + /** + * MUC Light get affiliations IQ constructor. + * + * @param roomJid + */ + public MUCLightGetAffiliationsIQ(Jid roomJid) { + this(roomJid, null); + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.rightAngleBracket(); + xml.optElement("version", version); + return xml; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightGetConfigsIQ.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightGetConfigsIQ.java new file mode 100644 index 000000000..7743ce4ba --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightGetConfigsIQ.java @@ -0,0 +1,65 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.element; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.muclight.MultiUserChatLight; +import org.jxmpp.jid.Jid; + +/** + * MUC Light get configurations IQ class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightGetConfigsIQ extends IQ { + + public static final String ELEMENT = QUERY_ELEMENT; + public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.CONFIGURATION; + + private String version; + + /** + * MUC Light get configurations IQ constructor. + * + * @param roomJid + * @param version + */ + public MUCLightGetConfigsIQ(Jid roomJid, String version) { + super(ELEMENT, NAMESPACE); + this.version = version; + this.setType(Type.get); + this.setTo(roomJid); + } + + /** + * MUC Light get configurations IQ constructor. + * + * @param roomJid + */ + public MUCLightGetConfigsIQ(Jid roomJid) { + this(roomJid, null); + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.rightAngleBracket(); + xml.optElement("version", version); + return xml; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightGetInfoIQ.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightGetInfoIQ.java new file mode 100644 index 000000000..6c5d03125 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightGetInfoIQ.java @@ -0,0 +1,65 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.element; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.muclight.MultiUserChatLight; +import org.jxmpp.jid.Jid; + +/** + * MUC Light get info IQ class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightGetInfoIQ extends IQ { + + public static final String ELEMENT = QUERY_ELEMENT; + public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.INFO; + + private String version; + + /** + * MUC Light get info IQ constructor. + * + * @param roomJid + * @param version + */ + public MUCLightGetInfoIQ(Jid roomJid, String version) { + super(ELEMENT, NAMESPACE); + this.version = version; + this.setType(Type.get); + this.setTo(roomJid); + } + + /** + * MUC Light get info IQ constructor. + * + * @param roomJid + */ + public MUCLightGetInfoIQ(Jid roomJid) { + this(roomJid, null); + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.rightAngleBracket(); + xml.optElement("version", version); + return xml; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightInfoIQ.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightInfoIQ.java new file mode 100644 index 000000000..44f74db30 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightInfoIQ.java @@ -0,0 +1,95 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.element; + +import java.util.HashMap; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.muclight.MUCLightAffiliation; +import org.jivesoftware.smackx.muclight.MUCLightRoomConfiguration; +import org.jivesoftware.smackx.muclight.MultiUserChatLight; +import org.jivesoftware.smackx.muclight.element.MUCLightElements.ConfigurationElement; +import org.jivesoftware.smackx.muclight.element.MUCLightElements.OccupantsElement; +import org.jxmpp.jid.Jid; + +/** + * MUC Light info response IQ class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightInfoIQ extends IQ { + + public static final String ELEMENT = QUERY_ELEMENT; + public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.INFO; + + private final String version; + private final MUCLightRoomConfiguration configuration; + private final HashMap occupants; + + /** + * MUCLight info response IQ constructor. + * + * @param version + * @param configuration + * @param occupants + */ + public MUCLightInfoIQ(String version, MUCLightRoomConfiguration configuration, + HashMap occupants) { + super(ELEMENT, NAMESPACE); + this.version = version; + this.configuration = configuration; + this.occupants = occupants; + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.rightAngleBracket(); + xml.optElement("version", version); + xml.element(new ConfigurationElement(configuration)); + xml.element(new OccupantsElement(occupants)); + return xml; + } + + /** + * Returns the version. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * Returns the room configuration. + * + * @return the configuration of the room + */ + public MUCLightRoomConfiguration getConfiguration() { + return configuration; + } + + /** + * Returns the room occupants. + * + * @return the occupants of the room + */ + public HashMap getOccupants() { + return occupants; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightSetConfigsIQ.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightSetConfigsIQ.java new file mode 100644 index 000000000..d7dbfbf14 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightSetConfigsIQ.java @@ -0,0 +1,87 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.element; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.muclight.MultiUserChatLight; +import org.jxmpp.jid.Jid; + +/** + * MUC Light set configurations IQ class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightSetConfigsIQ extends IQ { + + public static final String ELEMENT = QUERY_ELEMENT; + public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.CONFIGURATION; + + private String roomName; + private String subject; + private HashMap customConfigs; + + /** + * MUC Light set configuration IQ constructor. + * + * @param roomJid + * @param roomName + * @param subject + * @param customConfigs + */ + public MUCLightSetConfigsIQ(Jid roomJid, String roomName, String subject, HashMap customConfigs) { + super(ELEMENT, NAMESPACE); + this.roomName = roomName; + this.subject = subject; + this.customConfigs = customConfigs; + this.setType(Type.set); + this.setTo(roomJid); + } + + /** + * MUC Light set configuration IQ constructor. + * + * @param roomJid + * @param roomName + * @param customConfigs + */ + public MUCLightSetConfigsIQ(Jid roomJid, String roomName, HashMap customConfigs) { + this(roomJid, roomName, null, customConfigs); + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.rightAngleBracket(); + xml.optElement("roomname", roomName); + xml.optElement("subject", subject); + + if (customConfigs != null) { + Iterator> it = customConfigs.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + xml.element(pair.getKey(), pair.getValue()); + } + } + + return xml; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/package-info.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/package-info.java new file mode 100644 index 000000000..c51b2119d --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/package-info.java @@ -0,0 +1,25 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Multi-User Chat Light (MUC Light) elements. + * + * @see + * XEP-xxxx: Multi-User Chat Light + * + */ +package org.jivesoftware.smackx.muclight.element; diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/package-info.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/package-info.java new file mode 100644 index 000000000..7fa6c4772 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/package-info.java @@ -0,0 +1,25 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Classes and Interfaces that implement Multi-User Chat Light (MUC Light). + * + * @see + * XEP-xxxx: Multi-User Chat Light + * + */ +package org.jivesoftware.smackx.muclight; diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightAffiliationsChangeProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightAffiliationsChangeProvider.java new file mode 100644 index 000000000..fc64af397 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightAffiliationsChangeProvider.java @@ -0,0 +1,72 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.provider; + +import java.util.HashMap; + +import org.jivesoftware.smack.provider.ExtensionElementProvider; +import org.jivesoftware.smackx.muclight.MUCLightAffiliation; +import org.jivesoftware.smackx.muclight.element.MUCLightElements.AffiliationsChangeExtension; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; +import org.xmlpull.v1.XmlPullParser; + +/** + * MUC Light Affiliations Change Provider class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightAffiliationsChangeProvider extends ExtensionElementProvider { + + @Override + public AffiliationsChangeExtension parse(XmlPullParser parser, int initialDepth) throws Exception { + HashMap affiliations = new HashMap<>(); + String prevVersion = null; + String version = null; + + outerloop: while (true) { + int eventType = parser.next(); + + if (eventType == XmlPullParser.START_TAG) { + + if (parser.getName().equals("prev-version")) { + prevVersion = parser.nextText(); + } + + if (parser.getName().equals("version")) { + version = parser.nextText(); + } + + if (parser.getName().equals("user")) { + MUCLightAffiliation mucLightAffiliation = MUCLightAffiliation + .fromString(parser.getAttributeValue("", "affiliation")); + Jid jid = JidCreate.from(parser.nextText()); + affiliations.put(jid, mucLightAffiliation); + } + + } else if (eventType == XmlPullParser.END_TAG) { + if (parser.getDepth() == initialDepth) { + break outerloop; + } + } + } + + return new AffiliationsChangeExtension(affiliations, prevVersion, version); + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightAffiliationsIQProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightAffiliationsIQProvider.java new file mode 100644 index 000000000..90601d841 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightAffiliationsIQProvider.java @@ -0,0 +1,66 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.provider; + +import java.util.HashMap; + +import org.jivesoftware.smack.provider.IQProvider; +import org.jivesoftware.smackx.muclight.MUCLightAffiliation; +import org.jivesoftware.smackx.muclight.element.MUCLightAffiliationsIQ; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; +import org.xmlpull.v1.XmlPullParser; + +/** + * MUC Light affiliations IQ provider class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightAffiliationsIQProvider extends IQProvider { + + @Override + public MUCLightAffiliationsIQ parse(XmlPullParser parser, int initialDepth) throws Exception { + String version = null; + HashMap occupants = new HashMap<>(); + + outerloop: while (true) { + int eventType = parser.next(); + + if (eventType == XmlPullParser.START_TAG) { + + if (parser.getName().equals("version")) { + version = parser.nextText(); + } + + if (parser.getName().equals("user")) { + MUCLightAffiliation affiliation = MUCLightAffiliation + .fromString(parser.getAttributeValue("", "affiliation")); + occupants.put(JidCreate.from(parser.nextText()), affiliation); + } + + } else if (eventType == XmlPullParser.END_TAG) { + if (parser.getDepth() == initialDepth) { + break outerloop; + } + } + } + + return new MUCLightAffiliationsIQ(version, occupants); + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightBlockingIQProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightBlockingIQProvider.java new file mode 100644 index 000000000..85803e7a3 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightBlockingIQProvider.java @@ -0,0 +1,84 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.provider; + +import java.io.IOException; +import java.util.HashMap; + +import org.jivesoftware.smack.packet.IQ.Type; +import org.jivesoftware.smack.provider.IQProvider; +import org.jivesoftware.smackx.muclight.element.MUCLightBlockingIQ; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; +import org.jxmpp.stringprep.XmppStringprepException; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + +/** + * MUC Light blocking IQ provider class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightBlockingIQProvider extends IQProvider { + + @Override + public MUCLightBlockingIQ parse(XmlPullParser parser, int initialDepth) throws Exception { + HashMap rooms = null; + HashMap users = null; + + outerloop: while (true) { + int eventType = parser.next(); + + if (eventType == XmlPullParser.START_TAG) { + + if (parser.getName().equals("room")) { + rooms = parseBlocking(parser, rooms); + } + + if (parser.getName().equals("user")) { + users = parseBlocking(parser, users); + } + + } else if (eventType == XmlPullParser.END_TAG) { + if (parser.getDepth() == initialDepth) { + break outerloop; + } + } + } + + MUCLightBlockingIQ mucLightBlockingIQ = new MUCLightBlockingIQ(rooms, users); + mucLightBlockingIQ.setType(Type.result); + return mucLightBlockingIQ; + } + + private HashMap parseBlocking(XmlPullParser parser, HashMap map) + throws XmppStringprepException, XmlPullParserException, IOException { + if (map == null) { + map = new HashMap<>(); + } + String action = parser.getAttributeValue("", "action"); + + if (action.equals("deny")) { + map.put(JidCreate.from(parser.nextText()), false); + } else if (action.equals("allow")) { + map.put(JidCreate.from(parser.nextText()), true); + } + return map; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightConfigurationIQProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightConfigurationIQProvider.java new file mode 100644 index 000000000..494ba2657 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightConfigurationIQProvider.java @@ -0,0 +1,71 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.provider; + +import java.util.HashMap; + +import org.jivesoftware.smack.provider.IQProvider; +import org.jivesoftware.smackx.muclight.MUCLightRoomConfiguration; +import org.jivesoftware.smackx.muclight.element.MUCLightConfigurationIQ; +import org.xmlpull.v1.XmlPullParser; + +/** + * MUC Light configuration IQ provider class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightConfigurationIQProvider extends IQProvider { + + @Override + public MUCLightConfigurationIQ parse(XmlPullParser parser, int initialDepth) throws Exception { + String version = null; + String roomName = null; + String subject = null; + HashMap customConfigs = null; + + outerloop: while (true) { + int eventType = parser.next(); + + if (eventType == XmlPullParser.START_TAG) { + + if (parser.getName().equals("version")) { + version = parser.nextText(); + } else if (parser.getName().equals("roomname")) { + roomName = parser.nextText(); + } else if (parser.getName().equals("subject")) { + subject = parser.nextText(); + } else { + if (customConfigs == null) { + customConfigs = new HashMap<>(); + } + customConfigs.put(parser.getName(), parser.nextText()); + } + + } else if (eventType == XmlPullParser.END_TAG) { + if (parser.getDepth() == initialDepth) { + break outerloop; + } + } + } + + MUCLightRoomConfiguration configuration = new MUCLightRoomConfiguration(roomName, subject, customConfigs); + + return new MUCLightConfigurationIQ(version, configuration); + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightConfigurationsChangeProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightConfigurationsChangeProvider.java new file mode 100644 index 000000000..efd8770a5 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightConfigurationsChangeProvider.java @@ -0,0 +1,71 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.provider; + +import java.util.HashMap; + +import org.jivesoftware.smack.provider.ExtensionElementProvider; +import org.jivesoftware.smackx.muclight.element.MUCLightElements.ConfigurationsChangeExtension; +import org.xmlpull.v1.XmlPullParser; + +/** + * MUC Light configurations change provider class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightConfigurationsChangeProvider extends ExtensionElementProvider { + + @Override + public ConfigurationsChangeExtension parse(XmlPullParser parser, int initialDepth) throws Exception { + String prevVersion = null; + String version = null; + String roomName = null; + String subject = null; + HashMap customConfigs = null; + + outerloop: while (true) { + int eventType = parser.next(); + + if (eventType == XmlPullParser.START_TAG) { + + if (parser.getName().equals("prev-version")) { + prevVersion = parser.nextText(); + } else if (parser.getName().equals("version")) { + version = parser.nextText(); + } else if (parser.getName().equals("roomname")) { + roomName = parser.nextText(); + } else if (parser.getName().equals("subject")) { + subject = parser.nextText(); + } else { + if (customConfigs == null) { + customConfigs = new HashMap<>(); + } + customConfigs.put(parser.getName(), parser.nextText()); + } + + } else if (eventType == XmlPullParser.END_TAG) { + if (parser.getDepth() == initialDepth) { + break outerloop; + } + } + } + + return new ConfigurationsChangeExtension(prevVersion, version, roomName, subject, customConfigs); + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightInfoIQProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightInfoIQProvider.java new file mode 100644 index 000000000..da6e5fdb0 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/MUCLightInfoIQProvider.java @@ -0,0 +1,116 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight.provider; + +import java.util.HashMap; + +import org.jivesoftware.smack.provider.IQProvider; +import org.jivesoftware.smackx.muclight.MUCLightAffiliation; +import org.jivesoftware.smackx.muclight.MUCLightRoomConfiguration; +import org.jivesoftware.smackx.muclight.element.MUCLightInfoIQ; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; +import org.xmlpull.v1.XmlPullParser; + +/** + * MUC Light info IQ provider class. + * + * @author Fernando Ramirez + * + */ +public class MUCLightInfoIQProvider extends IQProvider { + + @Override + public MUCLightInfoIQ parse(XmlPullParser parser, int initialDepth) throws Exception { + String version = null; + String roomName = null; + String subject = null; + HashMap customConfigs = null; + HashMap occupants = new HashMap<>(); + + outerloop: while (true) { + int eventType = parser.next(); + + if (eventType == XmlPullParser.START_TAG) { + + if (parser.getName().equals("version")) { + version = parser.nextText(); + } + if (parser.getName().equals("configuration")) { + + int depth = parser.getDepth(); + + outerloop2: while (true) { + eventType = parser.next(); + + if (eventType == XmlPullParser.START_TAG) { + if (parser.getName().equals("roomname")) { + roomName = parser.nextText(); + } else if (parser.getName().equals("subject")) { + subject = parser.nextText(); + } else { + if (customConfigs == null) { + customConfigs = new HashMap<>(); + } + customConfigs.put(parser.getName(), parser.nextText()); + } + + } else if (eventType == XmlPullParser.END_TAG) { + if (parser.getDepth() == depth) { + break outerloop2; + } + } + } + } + + if (parser.getName().equals("occupants")) { + occupants = iterateOccupants(parser); + } + + } else if (eventType == XmlPullParser.END_TAG) { + if (parser.getDepth() == initialDepth) { + break outerloop; + } + } + } + + return new MUCLightInfoIQ(version, new MUCLightRoomConfiguration(roomName, subject, customConfigs), occupants); + } + + private HashMap iterateOccupants(XmlPullParser parser) throws Exception { + HashMap occupants = new HashMap<>(); + int depth = parser.getDepth(); + + outerloop: while (true) { + int eventType = parser.next(); + if (eventType == XmlPullParser.START_TAG) { + if (parser.getName().equals("user")) { + MUCLightAffiliation affiliation = MUCLightAffiliation + .fromString(parser.getAttributeValue("", "affiliation")); + occupants.put(JidCreate.from(parser.nextText()), affiliation); + } + } else if (eventType == XmlPullParser.END_TAG) { + if (parser.getDepth() == depth) { + break outerloop; + } + } + } + + return occupants; + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/package-info.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/package-info.java new file mode 100644 index 000000000..e879013df --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/provider/package-info.java @@ -0,0 +1,25 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Multi-User Chat Light (MUC Light) providers. + * + * @see + * XEP-xxxx: Multi-User Chat Light + * + */ +package org.jivesoftware.smackx.muclight.provider; diff --git a/smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers b/smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers index def9951bd..21a546154 100644 --- a/smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers +++ b/smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers @@ -51,6 +51,38 @@ google:mobile:data org.jivesoftware.smackx.gcm.provider.GcmExtensionProvider + + + + query + urn:xmpp:muclight:0#info + org.jivesoftware.smackx.muclight.provider.MUCLightInfoIQProvider + + + x + urn:xmpp:muclight:0#affiliations + org.jivesoftware.smackx.muclight.provider.MUCLightAffiliationsChangeProvider + + + x + urn:xmpp:muclight:0#configuration + org.jivesoftware.smackx.muclight.provider.MUCLightConfigurationsChangeProvider + + + query + urn:xmpp:muclight:0#configuration + org.jivesoftware.smackx.muclight.provider.MUCLightConfigurationIQProvider + + + query + urn:xmpp:muclight:0#affiliations + org.jivesoftware.smackx.muclight.provider.MUCLightAffiliationsIQProvider + + + query + urn:xmpp:muclight:0#blocking + org.jivesoftware.smackx.muclight.provider.MUCLightBlockingIQProvider + diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightAffiliationsChangeExtensionTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightAffiliationsChangeExtensionTest.java new file mode 100644 index 000000000..04e6151db --- /dev/null +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightAffiliationsChangeExtensionTest.java @@ -0,0 +1,92 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import java.util.HashMap; + +import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.muclight.element.MUCLightElements.AffiliationsChangeExtension; +import org.junit.Assert; +import org.junit.Test; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; + +public class MUCLightAffiliationsChangeExtensionTest { + + String exampleMessageStanza = "" + + "" + + "sarasa2@shakespeare.lit" + + "sarasa1@shakespeare.lit" + + "sarasa3@shakespeare.lit" + "" + ""; + + String exampleMessageStanzaWithVersion = "" + + "" + "qwerty" + + "sarasa1@shakespeare.lit" + + "sarasa3@shakespeare.lit" + "" + "" + ""; + + String exampleMessageStanzaWithPrevVersion = "" + + "" + "njiokm" + + "qwerty" + "sarasa2@shakespeare.lit" + + "sarasa1@shakespeare.lit" + "" + "" + ""; + + @Test + public void checkAffiliationsChangeExtension() throws Exception { + Message changeAffiliationsMessage = (Message) PacketParserUtils.parseStanza(exampleMessageStanza); + AffiliationsChangeExtension affiliationsChangeExtension = AffiliationsChangeExtension + .from(changeAffiliationsMessage); + + HashMap affiliations = affiliationsChangeExtension.getAffiliations(); + Assert.assertEquals(affiliations.size(), 3); + Assert.assertEquals(affiliations.get(JidCreate.from("sarasa2@shakespeare.lit")), MUCLightAffiliation.owner); + Assert.assertEquals(affiliations.get(JidCreate.from("sarasa1@shakespeare.lit")), MUCLightAffiliation.member); + Assert.assertEquals(affiliations.get(JidCreate.from("sarasa3@shakespeare.lit")), MUCLightAffiliation.none); + } + + @Test + public void checkAffiliationsChangeExtensionWithVersion() throws Exception { + Message changeAffiliationsMessage = (Message) PacketParserUtils.parseStanza(exampleMessageStanzaWithVersion); + AffiliationsChangeExtension affiliationsChangeExtension = AffiliationsChangeExtension + .from(changeAffiliationsMessage); + + HashMap affiliations = affiliationsChangeExtension.getAffiliations(); + Assert.assertEquals(affiliations.size(), 2); + Assert.assertEquals(affiliations.get(JidCreate.from("sarasa1@shakespeare.lit")), MUCLightAffiliation.member); + Assert.assertEquals(affiliations.get(JidCreate.from("sarasa3@shakespeare.lit")), MUCLightAffiliation.none); + + Assert.assertEquals(affiliationsChangeExtension.getVersion(), "qwerty"); + } + + @Test + public void checkAffiliationsChangeExtensionWithPrevVersion() throws Exception { + Message changeAffiliationsMessage = (Message) PacketParserUtils + .parseStanza(exampleMessageStanzaWithPrevVersion); + AffiliationsChangeExtension affiliationsChangeExtension = AffiliationsChangeExtension + .from(changeAffiliationsMessage); + + HashMap affiliations = affiliationsChangeExtension.getAffiliations(); + Assert.assertEquals(affiliations.size(), 2); + Assert.assertEquals(affiliations.get(JidCreate.from("sarasa2@shakespeare.lit")), MUCLightAffiliation.owner); + Assert.assertEquals(affiliations.get(JidCreate.from("sarasa1@shakespeare.lit")), MUCLightAffiliation.member); + + Assert.assertEquals(affiliationsChangeExtension.getPrevVersion(), "njiokm"); + Assert.assertEquals(affiliationsChangeExtension.getVersion(), "qwerty"); + } + +} diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightBlockingTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightBlockingTest.java new file mode 100644 index 000000000..8e3e68185 --- /dev/null +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightBlockingTest.java @@ -0,0 +1,122 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import java.util.HashMap; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.packet.IQ.Type; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.muclight.element.MUCLightBlockingIQ; +import org.junit.Assert; +import org.junit.Test; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; + +public class MUCLightBlockingTest { + + String getBlockingListIQExample = "" + + "" + "" + ""; + + String getBlockingListIQResponse = "" + + "" + + "coven@muclight.shakespeare.lit" + + "sarasa@muclight.shakespeare.lit" + + "hag77@shakespeare.lit" + "" + ""; + + String blockingRoomsIQExample = "" + + "" + + "coven@muclight.shakespeare.lit" + + "chapel@shakespeare.lit" + "" + ""; + + String blockingUsersIQExample = "" + + "" + "hag77@shakespeare.lit" + + "hag66@shakespeare.lit" + "" + ""; + + String unblockingUsersAndRoomsExample = "" + + "" + + "coven@muclight.shakespeare.lit" + + "hag66@shakespeare.lit" + "" + ""; + + @Test + public void checkGetBlockingListIQ() throws Exception { + MUCLightBlockingIQ mucLightBlockingIQ = new MUCLightBlockingIQ(null, null); + mucLightBlockingIQ.setType(Type.get); + mucLightBlockingIQ.setStanzaId("getblock1"); + mucLightBlockingIQ.setTo(JidCreate.from("muclight.shakespeare.lit")); + + Assert.assertEquals(getBlockingListIQExample, mucLightBlockingIQ.toXML().toString()); + } + + @Test + public void checkGetBlockingListResponse() throws Exception { + IQ iqInfoResult = (IQ) PacketParserUtils.parseStanza(getBlockingListIQResponse); + MUCLightBlockingIQ mucLightBlockingIQ = (MUCLightBlockingIQ) iqInfoResult; + + Assert.assertEquals(2, mucLightBlockingIQ.getRooms().size()); + Assert.assertEquals(1, mucLightBlockingIQ.getUsers().size()); + Assert.assertEquals(false, mucLightBlockingIQ.getRooms().get(JidCreate.from("coven@muclight.shakespeare.lit"))); + Assert.assertEquals(false, + mucLightBlockingIQ.getRooms().get(JidCreate.from("sarasa@muclight.shakespeare.lit"))); + Assert.assertEquals(false, mucLightBlockingIQ.getUsers().get(JidCreate.from("hag77@shakespeare.lit"))); + } + + @Test + public void checkBlockRoomsIQ() throws Exception { + HashMap rooms = new HashMap<>(); + rooms.put(JidCreate.from("coven@muclight.shakespeare.lit"), false); + rooms.put(JidCreate.from("chapel@shakespeare.lit"), false); + + MUCLightBlockingIQ mucLightBlockingIQ = new MUCLightBlockingIQ(rooms, null); + mucLightBlockingIQ.setType(Type.set); + mucLightBlockingIQ.setTo(JidCreate.from("muclight.shakespeare.lit")); + mucLightBlockingIQ.setStanzaId("block1"); + + Assert.assertEquals(blockingRoomsIQExample, mucLightBlockingIQ.toXML().toString()); + } + + @Test + public void checkBlockUsersIQ() throws Exception { + HashMap users = new HashMap<>(); + users.put(JidCreate.from("hag77@shakespeare.lit"), false); + users.put(JidCreate.from("hag66@shakespeare.lit"), false); + + MUCLightBlockingIQ mucLightBlockingIQ = new MUCLightBlockingIQ(null, users); + mucLightBlockingIQ.setType(Type.set); + mucLightBlockingIQ.setTo(JidCreate.from("muclight.shakespeare.lit")); + mucLightBlockingIQ.setStanzaId("block2"); + + Assert.assertEquals(blockingUsersIQExample, mucLightBlockingIQ.toXML().toString()); + } + + @Test + public void checkUnblockUsersAndRoomsIQ() throws Exception { + HashMap users = new HashMap<>(); + users.put(JidCreate.from("hag66@shakespeare.lit"), true); + + HashMap rooms = new HashMap<>(); + rooms.put(JidCreate.from("coven@muclight.shakespeare.lit"), true); + + MUCLightBlockingIQ mucLightBlockingIQ = new MUCLightBlockingIQ(rooms, users); + mucLightBlockingIQ.setType(Type.set); + mucLightBlockingIQ.setTo(JidCreate.from("muclight.shakespeare.lit")); + mucLightBlockingIQ.setStanzaId("unblock1"); + + Assert.assertEquals(unblockingUsersAndRoomsExample, mucLightBlockingIQ.toXML().toString()); + } + +} diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightChangeAffiliationsIQTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightChangeAffiliationsIQTest.java new file mode 100644 index 000000000..e12dd22c5 --- /dev/null +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightChangeAffiliationsIQTest.java @@ -0,0 +1,56 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import java.util.HashMap; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.muclight.element.MUCLightChangeAffiliationsIQ; +import org.junit.Assert; +import org.junit.Test; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; + +public class MUCLightChangeAffiliationsIQTest { + + String stanza = "" + + "" + + "sarasa2@shakespeare.lit" + + "sarasa1@shakespeare.lit" + + "sarasa3@shakespeare.lit" + "" + ""; + + @Test + public void checkChangeAffiliationsMUCLightStanza() throws Exception { + HashMap affiliations = new HashMap<>(); + affiliations.put(JidCreate.from("sarasa2@shakespeare.lit"), MUCLightAffiliation.owner); + affiliations.put(JidCreate.from("sarasa1@shakespeare.lit"), MUCLightAffiliation.member); + affiliations.put(JidCreate.from("sarasa3@shakespeare.lit"), MUCLightAffiliation.none); + + MUCLightChangeAffiliationsIQ mucLightChangeAffiliationsIQ = new MUCLightChangeAffiliationsIQ( + JidCreate.from("coven@muclight.shakespeare.lit"), affiliations); + mucLightChangeAffiliationsIQ.setStanzaId("member1"); + + Assert.assertEquals(mucLightChangeAffiliationsIQ.getTo(), "coven@muclight.shakespeare.lit"); + Assert.assertEquals(mucLightChangeAffiliationsIQ.getType(), IQ.Type.set); + + HashMap iqAffiliations = mucLightChangeAffiliationsIQ.getAffiliations(); + Assert.assertEquals(iqAffiliations.get(JidCreate.from("sarasa1@shakespeare.lit")), MUCLightAffiliation.member); + Assert.assertEquals(iqAffiliations.get(JidCreate.from("sarasa2@shakespeare.lit")), MUCLightAffiliation.owner); + Assert.assertEquals(iqAffiliations.get(JidCreate.from("sarasa3@shakespeare.lit")), MUCLightAffiliation.none); + } + +} diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightConfigurationsChangeExtensionTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightConfigurationsChangeExtensionTest.java new file mode 100644 index 000000000..e3b5f2ab8 --- /dev/null +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightConfigurationsChangeExtensionTest.java @@ -0,0 +1,83 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.muclight.element.MUCLightElements.ConfigurationsChangeExtension; +import org.junit.Assert; +import org.junit.Test; + +public class MUCLightConfigurationsChangeExtensionTest { + + String messageWithSubjectChangeExample = "" + + "" + "" + + "asdfghj000" + "asdfghj" + + "To be or not to be?" + "" + ""; + + String messageWithRoomNameChangeExample = "" + + "" + "" + "zaqwsx" + + "zxcvbnm" + "A Darker Cave" + "" + ""; + + String messageWithConfigsChangeExample = "" + + "" + "" + "zaqwsx" + + "zxcvbnm" + "A Darker Cave" + "blue" + "" + + ""; + + @Test + public void checkSubjectChangeExtension() throws Exception { + Message configurationsMessage = (Message) PacketParserUtils.parseStanza(messageWithSubjectChangeExample); + ConfigurationsChangeExtension configurationsChangeExtension = ConfigurationsChangeExtension + .from(configurationsMessage); + + Assert.assertEquals("asdfghj000", configurationsChangeExtension.getPrevVersion()); + Assert.assertEquals("asdfghj", configurationsChangeExtension.getVersion()); + Assert.assertEquals("To be or not to be?", configurationsChangeExtension.getSubject()); + Assert.assertNull(configurationsChangeExtension.getRoomName()); + Assert.assertNull(configurationsChangeExtension.getCustomConfigs()); + Assert.assertEquals(messageWithSubjectChangeExample, configurationsMessage.toXML().toString()); + } + + @Test + public void checkRoomNameChangeExtension() throws Exception { + Message configurationsMessage = (Message) PacketParserUtils.parseStanza(messageWithRoomNameChangeExample); + ConfigurationsChangeExtension configurationsChangeExtension = ConfigurationsChangeExtension + .from(configurationsMessage); + + Assert.assertEquals("zaqwsx", configurationsChangeExtension.getPrevVersion()); + Assert.assertEquals("zxcvbnm", configurationsChangeExtension.getVersion()); + Assert.assertEquals("A Darker Cave", configurationsChangeExtension.getRoomName()); + Assert.assertNull(configurationsChangeExtension.getSubject()); + Assert.assertNull(configurationsChangeExtension.getCustomConfigs()); + Assert.assertEquals(messageWithRoomNameChangeExample, configurationsMessage.toXML().toString()); + } + + @Test + public void checkConfigsChangeExtension() throws Exception { + Message configurationsMessage = (Message) PacketParserUtils.parseStanza(messageWithConfigsChangeExample); + ConfigurationsChangeExtension configurationsChangeExtension = ConfigurationsChangeExtension + .from(configurationsMessage); + + Assert.assertEquals("zaqwsx", configurationsChangeExtension.getPrevVersion()); + Assert.assertEquals("zxcvbnm", configurationsChangeExtension.getVersion()); + Assert.assertEquals("A Darker Cave", configurationsChangeExtension.getRoomName()); + Assert.assertNull(configurationsChangeExtension.getSubject()); + Assert.assertEquals("blue", configurationsChangeExtension.getCustomConfigs().get("color")); + Assert.assertEquals(messageWithConfigsChangeExample, configurationsMessage.toXML().toString()); + } + +} diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightCreateIQTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightCreateIQTest.java new file mode 100644 index 000000000..ffc575006 --- /dev/null +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightCreateIQTest.java @@ -0,0 +1,55 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import org.jivesoftware.smackx.muclight.element.MUCLightCreateIQ; +import org.junit.Test; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; + +import org.junit.Assert; + +public class MUCLightCreateIQTest { + + String stanza = "" + + "" + "" + "test" + + "" + "" + "charlie@test.com" + + "pep@test.com" + "" + "" + ""; + + @Test + public void checkCreateMUCLightStanza() throws Exception { + List occupants = new ArrayList<>(); + occupants.add(JidCreate.from("charlie@test.com")); + occupants.add(JidCreate.from("pep@test.com")); + + MUCLightCreateIQ mucLightCreateIQ = new MUCLightCreateIQ( + JidCreate.from("ef498f55-5f79-4238-a5ae-4efe19cbe617@muclight.test.com").asEntityJidIfPossible(), + "test", occupants); + mucLightCreateIQ.setStanzaId("1c72W-50"); + + Assert.assertEquals(mucLightCreateIQ.getConfiguration().getRoomName(), "test"); + + HashMap iqOccupants = mucLightCreateIQ.getOccupants(); + Assert.assertEquals(iqOccupants.get(JidCreate.from("charlie@test.com")), MUCLightAffiliation.member); + Assert.assertEquals(iqOccupants.get(JidCreate.from("pep@test.com")), MUCLightAffiliation.member); + } + +} diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightDestroyTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightDestroyTest.java new file mode 100644 index 000000000..455b650c1 --- /dev/null +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightDestroyTest.java @@ -0,0 +1,36 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import org.jivesoftware.smackx.muclight.element.MUCLightDestroyIQ; +import org.junit.Assert; +import org.junit.Test; +import org.jxmpp.jid.impl.JidCreate; + +public class MUCLightDestroyTest { + + String stanza = "" + + "" + ""; + + @Test + public void checkDestroyMUCLightStanza() throws Exception{ + MUCLightDestroyIQ mucLightDestroyIQ = new MUCLightDestroyIQ(JidCreate.from("coven@muclight.shakespeare.lit")); + mucLightDestroyIQ.setStanzaId("destroy1"); + Assert.assertEquals(mucLightDestroyIQ.toXML().toString(), stanza); + } + +} diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightGetAffiliationsTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightGetAffiliationsTest.java new file mode 100644 index 000000000..7e8fdea52 --- /dev/null +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightGetAffiliationsTest.java @@ -0,0 +1,63 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import java.util.HashMap; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.muclight.element.MUCLightAffiliationsIQ; +import org.jivesoftware.smackx.muclight.element.MUCLightGetAffiliationsIQ; +import org.junit.Assert; +import org.junit.Test; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; + +public class MUCLightGetAffiliationsTest { + + String getAffiliationsIQExample = "" + + "" + "abcdefg" + "" + ""; + + String getAffiliationsResponseExample = "" + + "" + "123456" + + "user1@shakespeare.lit" + + "user2@shakespeare.lit" + + "user3@shakespeare.lit" + "" + ""; + + @Test + public void checkGetAffiliationsIQ() throws Exception { + MUCLightGetAffiliationsIQ mucLightGetAffiliationsIQ = new MUCLightGetAffiliationsIQ( + JidCreate.from("coven@muclight.shakespeare.lit"), "abcdefg"); + mucLightGetAffiliationsIQ.setStanzaId("getmembers"); + Assert.assertEquals(getAffiliationsIQExample, mucLightGetAffiliationsIQ.toXML().toString()); + } + + @Test + public void checkGetAffiliationsResponse() throws Exception { + IQ iqInfoResult = (IQ) PacketParserUtils.parseStanza(getAffiliationsResponseExample); + MUCLightAffiliationsIQ mucLightAffiliationsIQ = (MUCLightAffiliationsIQ) iqInfoResult; + + Assert.assertEquals("123456", mucLightAffiliationsIQ.getVersion()); + + HashMap affiliations = mucLightAffiliationsIQ.getAffiliations(); + Assert.assertEquals(3, affiliations.size()); + Assert.assertEquals(MUCLightAffiliation.owner, affiliations.get(JidCreate.from("user1@shakespeare.lit"))); + Assert.assertEquals(MUCLightAffiliation.member, affiliations.get(JidCreate.from("user2@shakespeare.lit"))); + Assert.assertEquals(MUCLightAffiliation.member, affiliations.get(JidCreate.from("user3@shakespeare.lit"))); + } + +} diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightGetConfigsTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightGetConfigsTest.java new file mode 100644 index 000000000..896bfb7ad --- /dev/null +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightGetConfigsTest.java @@ -0,0 +1,77 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import java.util.HashMap; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.muclight.element.MUCLightConfigurationIQ; +import org.jivesoftware.smackx.muclight.element.MUCLightGetConfigsIQ; +import org.junit.Assert; +import org.junit.Test; +import org.jxmpp.jid.impl.JidCreate; + +public class MUCLightGetConfigsTest { + + String getConfigsIQExample = "" + + "" + "abcdefg" + "" + ""; + + String getConfigsResponseExample = "" + "" + + "123456" + "A Dark Cave" + "A subject" + + "" + ""; + + String getConfigsResponseExampleWithCustomConfigs = "" + "" + + "123456" + "A Dark Cave" + "blue" + + "20" + "" + ""; + + @Test + public void checkGetConfigsIQ() throws Exception { + MUCLightGetConfigsIQ mucLightGetConfigsIQ = new MUCLightGetConfigsIQ( + JidCreate.from("coven@muclight.shakespeare.lit"), "abcdefg"); + mucLightGetConfigsIQ.setStanzaId("config0"); + Assert.assertEquals(getConfigsIQExample, mucLightGetConfigsIQ.toXML().toString()); + } + + @Test + public void checkGetConfigsResponse() throws Exception { + IQ iqInfoResult = (IQ) PacketParserUtils.parseStanza(getConfigsResponseExample); + MUCLightConfigurationIQ mucLightConfigurationIQ = (MUCLightConfigurationIQ) iqInfoResult; + + Assert.assertEquals("123456", mucLightConfigurationIQ.getVersion()); + Assert.assertEquals("A Dark Cave", mucLightConfigurationIQ.getConfiguration().getRoomName()); + Assert.assertEquals("A subject", mucLightConfigurationIQ.getConfiguration().getSubject()); + Assert.assertNull(mucLightConfigurationIQ.getConfiguration().getCustomConfigs()); + } + + @Test + public void checkGetConfigsResponseWithCustomConfigs() throws Exception { + IQ iqInfoResult = (IQ) PacketParserUtils.parseStanza(getConfigsResponseExampleWithCustomConfigs); + MUCLightConfigurationIQ mucLightConfigurationIQ = (MUCLightConfigurationIQ) iqInfoResult; + + Assert.assertEquals("123456", mucLightConfigurationIQ.getVersion()); + Assert.assertEquals("A Dark Cave", mucLightConfigurationIQ.getConfiguration().getRoomName()); + Assert.assertNull(mucLightConfigurationIQ.getConfiguration().getSubject()); + + HashMap customConfigs = mucLightConfigurationIQ.getConfiguration().getCustomConfigs(); + Assert.assertEquals("blue", customConfigs.get("color")); + Assert.assertEquals("20", customConfigs.get("size")); + } + +} diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightInfoTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightInfoTest.java new file mode 100644 index 000000000..f89661c1e --- /dev/null +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightInfoTest.java @@ -0,0 +1,72 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.muclight.element.MUCLightGetInfoIQ; +import org.jivesoftware.smackx.muclight.element.MUCLightInfoIQ; +import org.junit.Assert; +import org.junit.Test; +import org.jxmpp.jid.impl.JidCreate; + +public class MUCLightInfoTest { + + String exampleWithVersion = "" + + "" + "abcdefg" + "" + ""; + + String exampleWithoutVersion = "" + + "" + "" + ""; + + String exampleInfoResult = "" + + "" + "123456" + "" + + "test" + "" + "" + + "john@test.com" + "charlie@test.com" + + "pep@test.com" + "" + "" + ""; + + @Test + public void checkMUCLightGetInfoIQStanzaWithVersion() throws Exception { + MUCLightGetInfoIQ mucLightGetInfoIQWithVersion = new MUCLightGetInfoIQ( + JidCreate.from("coven@muclight.shakespeare.lit"), "abcdefg"); + mucLightGetInfoIQWithVersion.setStanzaId("getinfo1"); + Assert.assertEquals(mucLightGetInfoIQWithVersion.toXML().toString(), exampleWithVersion); + } + + @Test + public void checkMUCLightGetInfoIQStanzaWithoutVersion() throws Exception { + MUCLightGetInfoIQ mucLightGetInfoIQWithoutVersion = new MUCLightGetInfoIQ( + JidCreate.from("coven@muclight.shakespeare.lit"), null); + mucLightGetInfoIQWithoutVersion.setStanzaId("getinfo1"); + Assert.assertEquals(mucLightGetInfoIQWithoutVersion.toXML().toString(), exampleWithoutVersion); + } + + @Test + public void checkMUCLightInfoResult() throws Exception { + IQ iqInfoResult = (IQ) PacketParserUtils.parseStanza(exampleInfoResult); + MUCLightInfoIQ mucLightInfoResponseIQ = (MUCLightInfoIQ) iqInfoResult; + Assert.assertEquals(mucLightInfoResponseIQ.getVersion(), "123456"); + Assert.assertEquals(mucLightInfoResponseIQ.getConfiguration().getRoomName(), "test"); + Assert.assertEquals(mucLightInfoResponseIQ.getOccupants().size(), 3); + Assert.assertEquals(mucLightInfoResponseIQ.getOccupants().get(JidCreate.from("john@test.com")), + MUCLightAffiliation.owner); + Assert.assertEquals(mucLightInfoResponseIQ.getOccupants().get(JidCreate.from("charlie@test.com")), + MUCLightAffiliation.member); + Assert.assertEquals(mucLightInfoResponseIQ.getOccupants().get(JidCreate.from("pep@test.com")), + MUCLightAffiliation.member); + } + +} diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightSetConfigsIQTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightSetConfigsIQTest.java new file mode 100644 index 000000000..be644db3d --- /dev/null +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/muclight/MUCLightSetConfigsIQTest.java @@ -0,0 +1,70 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muclight; + +import java.util.HashMap; + +import org.jivesoftware.smackx.muclight.element.MUCLightSetConfigsIQ; +import org.junit.Assert; +import org.junit.Test; +import org.jxmpp.jid.impl.JidCreate; + +public class MUCLightSetConfigsIQTest { + + String setConfigsIQExample = "" + + "" + "A Darker Cave" + + "blue" + "" + ""; + + String changeRoomNameIQExample = "" + + "" + "A Darker Cave" + "" + + ""; + + String changeSubjectIQExample = "" + + "" + "To be or not to be?" + + "" + ""; + + @Test + public void checkSetConfigsStanza() throws Exception { + HashMap customConfigs = new HashMap<>(); + customConfigs.put("color", "blue"); + + MUCLightSetConfigsIQ mucLightSetConfigsIQ = new MUCLightSetConfigsIQ( + JidCreate.from("coven@muclight.shakespeare.lit"), "A Darker Cave", customConfigs); + mucLightSetConfigsIQ.setStanzaId("conf1"); + + Assert.assertEquals(setConfigsIQExample, mucLightSetConfigsIQ.toXML().toString()); + } + + @Test + public void checkChangeRoomNameStanza() throws Exception { + MUCLightSetConfigsIQ mucLightChangeRoomNameIQ = new MUCLightSetConfigsIQ( + JidCreate.from("coven@muclight.shakespeare.lit"), "A Darker Cave", null); + mucLightChangeRoomNameIQ.setStanzaId("roomName1"); + + Assert.assertEquals(changeRoomNameIQExample, mucLightChangeRoomNameIQ.toXML().toString()); + } + + @Test + public void checkChangeSubjectStanza() throws Exception { + MUCLightSetConfigsIQ mucLightChangeSubjectIQ = new MUCLightSetConfigsIQ( + JidCreate.from("coven@muclight.shakespeare.lit"), null, "To be or not to be?", null); + mucLightChangeSubjectIQ.setStanzaId("subject1"); + + Assert.assertEquals(changeSubjectIQExample, mucLightChangeSubjectIQ.toXML().toString()); + } + +}