1/**
2 * $RCSfile$
3 * $Revision$
4 * $Date$
5 *
6 * Copyright 2003-2007 Jive Software.
7 *
8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *     http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21package org.jivesoftware.smackx.muc;
22
23import org.jivesoftware.smackx.Form;
24import org.jivesoftware.smackx.FormField;
25import org.jivesoftware.smackx.packet.DiscoverInfo;
26
27import java.util.Iterator;
28
29/**
30 * Represents the room information that was discovered using Service Discovery. It's possible to
31 * obtain information about a room before joining the room but only for rooms that are public (i.e.
32 * rooms that may be discovered).
33 *
34 * @author Gaston Dombiak
35 */
36public class RoomInfo {
37
38    /**
39     * JID of the room. The node of the JID is commonly used as the ID of the room or name.
40     */
41    private String room;
42    /**
43     * Description of the room.
44     */
45    private String description = "";
46    /**
47     * Last known subject of the room.
48     */
49    private String subject = "";
50    /**
51     * Current number of occupants in the room.
52     */
53    private int occupantsCount = -1;
54    /**
55     * A room is considered members-only if an invitation is required in order to enter the room.
56     * Any user that is not a member of the room won't be able to join the room unless the user
57     * decides to register with the room (thus becoming a member).
58     */
59    private boolean membersOnly;
60    /**
61     * Moderated rooms enable only participants to speak. Users that join the room and aren't
62     * participants can't speak (they are just visitors).
63     */
64    private boolean moderated;
65    /**
66     * Every presence packet can include the JID of every occupant unless the owner deactives this
67     * configuration.
68     */
69    private boolean nonanonymous;
70    /**
71     * Indicates if users must supply a password to join the room.
72     */
73    private boolean passwordProtected;
74    /**
75     * Persistent rooms are saved to the database to make sure that rooms configurations can be
76     * restored in case the server goes down.
77     */
78    private boolean persistent;
79
80    RoomInfo(DiscoverInfo info) {
81        super();
82        this.room = info.getFrom();
83        // Get the information based on the discovered features
84        this.membersOnly = info.containsFeature("muc_membersonly");
85        this.moderated = info.containsFeature("muc_moderated");
86        this.nonanonymous = info.containsFeature("muc_nonanonymous");
87        this.passwordProtected = info.containsFeature("muc_passwordprotected");
88        this.persistent = info.containsFeature("muc_persistent");
89        // Get the information based on the discovered extended information
90        Form form = Form.getFormFrom(info);
91        if (form != null) {
92            FormField descField = form.getField("muc#roominfo_description");
93            this.description = ( descField == null || !(descField.getValues().hasNext()) )? "" : descField.getValues().next();
94
95            FormField subjField = form.getField("muc#roominfo_subject");
96            this.subject = ( subjField == null || !(subjField.getValues().hasNext()) ) ? "" : subjField.getValues().next();
97
98            FormField occCountField = form.getField("muc#roominfo_occupants");
99            this.occupantsCount = occCountField == null ? -1 : Integer.parseInt(occCountField.getValues()
100                    .next());
101        }
102    }
103
104    /**
105     * Returns the JID of the room whose information was discovered.
106     *
107     * @return the JID of the room whose information was discovered.
108     */
109    public String getRoom() {
110        return room;
111    }
112
113    /**
114     * Returns the discovered description of the room.
115     *
116     * @return the discovered description of the room.
117     */
118    public String getDescription() {
119        return description;
120    }
121
122    /**
123     * Returns the discovered subject of the room. The subject may be empty if the room does not
124     * have a subject.
125     *
126     * @return the discovered subject of the room.
127     */
128    public String getSubject() {
129        return subject;
130    }
131
132    /**
133     * Returns the discovered number of occupants that are currently in the room. If this
134     * information was not discovered (i.e. the server didn't send it) then a value of -1 will be
135     * returned.
136     *
137     * @return the number of occupants that are currently in the room or -1 if that information was
138     * not provided by the server.
139     */
140    public int getOccupantsCount() {
141        return occupantsCount;
142    }
143
144    /**
145     * Returns true if the room has restricted the access so that only members may enter the room.
146     *
147     * @return true if the room has restricted the access so that only members may enter the room.
148     */
149    public boolean isMembersOnly() {
150        return membersOnly;
151    }
152
153    /**
154     * Returns true if the room enabled only participants to speak. Occupants with a role of
155     * visitor won't be able to speak in the room.
156     *
157     * @return true if the room enabled only participants to speak.
158     */
159    public boolean isModerated() {
160        return moderated;
161    }
162
163    /**
164     * Returns true if presence packets will include the JID of every occupant.
165     *
166     * @return true if presence packets will include the JID of every occupant.
167     */
168    public boolean isNonanonymous() {
169        return nonanonymous;
170    }
171
172    /**
173     * Returns true if users musy provide a valid password in order to join the room.
174     *
175     * @return true if users musy provide a valid password in order to join the room.
176     */
177    public boolean isPasswordProtected() {
178        return passwordProtected;
179    }
180
181    /**
182     * Returns true if the room will persist after the last occupant have left the room.
183     *
184     * @return true if the room will persist after the last occupant have left the room.
185     */
186    public boolean isPersistent() {
187        return persistent;
188    }
189
190}
191