001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.muc;
019
020import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
021import org.jivesoftware.smackx.xdata.Form;
022import org.jivesoftware.smackx.xdata.FormField;
023
024/**
025 * Represents the room information that was discovered using Service Discovery. It's possible to
026 * obtain information about a room before joining the room but only for rooms that are public (i.e.
027 * rooms that may be discovered).
028 *
029 * @author Gaston Dombiak
030 */
031public class RoomInfo {
032
033    /**
034     * JID of the room. The node of the JID is commonly used as the ID of the room or name.
035     */
036    private String room;
037    /**
038     * Description of the room.
039     */
040    private String description = "";
041    /**
042     * Last known subject of the room.
043     */
044    private String subject = "";
045    /**
046     * Current number of occupants in the room.
047     */
048    private int occupantsCount = -1;
049    /**
050     * A room is considered members-only if an invitation is required in order to enter the room.
051     * Any user that is not a member of the room won't be able to join the room unless the user
052     * decides to register with the room (thus becoming a member).
053     */
054    private boolean membersOnly;
055    /**
056     * Moderated rooms enable only participants to speak. Users that join the room and aren't
057     * participants can't speak (they are just visitors).
058     */
059    private boolean moderated;
060    /**
061     * Every presence packet can include the JID of every occupant unless the owner deactives this
062     * configuration.
063     */
064    private boolean nonanonymous;
065    /**
066     * Indicates if users must supply a password to join the room.
067     */
068    private boolean passwordProtected;
069    /**
070     * Persistent rooms are saved to the database to make sure that rooms configurations can be
071     * restored in case the server goes down.
072     */
073    private boolean persistent;
074
075    RoomInfo(DiscoverInfo info) {
076        super();
077        this.room = info.getFrom();
078        // Get the information based on the discovered features
079        this.membersOnly = info.containsFeature("muc_membersonly");
080        this.moderated = info.containsFeature("muc_moderated");
081        this.nonanonymous = info.containsFeature("muc_nonanonymous");
082        this.passwordProtected = info.containsFeature("muc_passwordprotected");
083        this.persistent = info.containsFeature("muc_persistent");
084        // Get the information based on the discovered extended information
085        Form form = Form.getFormFrom(info);
086        if (form != null) {
087            FormField descField = form.getField("muc#roominfo_description");
088            this.description = ( descField == null || descField.getValues().isEmpty() ) ? "" : descField.getValues().get(0);
089
090            FormField subjField = form.getField("muc#roominfo_subject");
091            this.subject = ( subjField == null || subjField.getValues().isEmpty() ) ? "" : subjField.getValues().get(0);
092
093            FormField occCountField = form.getField("muc#roominfo_occupants");
094            this.occupantsCount = occCountField == null ? -1 : Integer.parseInt(occCountField.getValues()
095                    .get(0));
096        }
097    }
098
099    /**
100     * Returns the JID of the room whose information was discovered.
101     *
102     * @return the JID of the room whose information was discovered.
103     */
104    public String getRoom() {
105        return room;
106    }
107
108    /**
109     * Returns the discovered description of the room.
110     *
111     * @return the discovered description of the room.
112     */
113    public String getDescription() {
114        return description;
115    }
116
117    /**
118     * Returns the discovered subject of the room. The subject may be empty if the room does not
119     * have a subject.
120     *
121     * @return the discovered subject of the room.
122     */
123    public String getSubject() {
124        return subject;
125    }
126
127    /**
128     * Returns the discovered number of occupants that are currently in the room. If this
129     * information was not discovered (i.e. the server didn't send it) then a value of -1 will be
130     * returned.
131     *
132     * @return the number of occupants that are currently in the room or -1 if that information was
133     * not provided by the server.
134     */
135    public int getOccupantsCount() {
136        return occupantsCount;
137    }
138
139    /**
140     * Returns true if the room has restricted the access so that only members may enter the room.
141     *
142     * @return true if the room has restricted the access so that only members may enter the room.
143     */
144    public boolean isMembersOnly() {
145        return membersOnly;
146    }
147
148    /**
149     * Returns true if the room enabled only participants to speak. Occupants with a role of
150     * visitor won't be able to speak in the room.
151     *
152     * @return true if the room enabled only participants to speak.
153     */
154    public boolean isModerated() {
155        return moderated;
156    }
157
158    /**
159     * Returns true if presence packets will include the JID of every occupant.
160     *
161     * @return true if presence packets will include the JID of every occupant.
162     */
163    public boolean isNonanonymous() {
164        return nonanonymous;
165    }
166
167    /**
168     * Returns true if users musy provide a valid password in order to join the room.
169     *
170     * @return true if users musy provide a valid password in order to join the room.
171     */
172    public boolean isPasswordProtected() {
173        return passwordProtected;
174    }
175
176    /**
177     * Returns true if the room will persist after the last occupant have left the room.
178     *
179     * @return true if the room will persist after the last occupant have left the room.
180     */
181    public boolean isPersistent() {
182        return persistent;
183    }
184
185}