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 java.net.MalformedURLException; 021import java.net.URL; 022import java.util.Collections; 023import java.util.List; 024import java.util.logging.Level; 025import java.util.logging.Logger; 026 027import org.jivesoftware.smackx.disco.packet.DiscoverInfo; 028import org.jivesoftware.smackx.xdata.Form; 029import org.jivesoftware.smackx.xdata.FormField; 030 031import org.jxmpp.jid.EntityBareJid; 032import org.jxmpp.jid.Jid; 033import org.jxmpp.jid.util.JidUtil; 034 035/** 036 * Represents the room information that was discovered using Service Discovery. It's possible to 037 * obtain information about a room before joining the room but only for rooms that are public (i.e. 038 * rooms that may be discovered). 039 * 040 * @author Gaston Dombiak 041 */ 042public class RoomInfo { 043 044 private static final Logger LOGGER = Logger.getLogger(RoomInfo.class.getName()); 045 046 /** 047 * JID of the room. The localpart of the JID is commonly used as the ID of the room or name. 048 */ 049 private final EntityBareJid room; 050 /** 051 * Description of the room. 052 */ 053 private final String description; 054 055 /** 056 * Name of the room. 057 */ 058 private final String name; 059 060 /** 061 * Last known subject of the room. 062 */ 063 private final String subject; 064 /** 065 * Current number of occupants in the room. 066 */ 067 private final int occupantsCount; 068 /** 069 * A room is considered members-only if an invitation is required in order to enter the room. 070 * Any user that is not a member of the room won't be able to join the room unless the user 071 * decides to register with the room (thus becoming a member). 072 */ 073 private final boolean membersOnly; 074 /** 075 * Moderated rooms enable only participants to speak. Users that join the room and aren't 076 * participants can't speak (they are just visitors). 077 */ 078 private final boolean moderated; 079 /** 080 * Every presence stanza can include the JID of every occupant unless the owner deactives this 081 * configuration. 082 */ 083 private final boolean nonanonymous; 084 /** 085 * Indicates if users must supply a password to join the room. 086 */ 087 private final boolean passwordProtected; 088 /** 089 * Persistent rooms are saved to the database to make sure that rooms configurations can be 090 * restored in case the server goes down. 091 */ 092 private final boolean persistent; 093 094 /** 095 * Maximum number of history messages returned by the room. 096 */ 097 private final int maxhistoryfetch; 098 099 /** 100 * Contact Address 101 */ 102 private final List<EntityBareJid> contactJid; 103 104 /** 105 * Natural Language for Room Discussions 106 */ 107 private final String lang; 108 109 /** 110 * An associated LDAP group that defined room membership. Should be an LDAP 111 * Distinguished Name 112 */ 113 private final String ldapgroup; 114 115 /** 116 * True if the room subject can be modified by participants 117 */ 118 private final Boolean subjectmod; 119 120 /** 121 * URL for archived discussion logs 122 */ 123 private final URL logs; 124 125 /** 126 * An associated pubsub node 127 */ 128 private final String pubsub; 129 130 /** 131 * The rooms extended configuration form; 132 */ 133 private final Form form; 134 135 RoomInfo(DiscoverInfo info) { 136 final Jid from = info.getFrom(); 137 if (from != null) { 138 this.room = info.getFrom().asEntityBareJidIfPossible(); 139 } else { 140 this.room = null; 141 } 142 // Get the information based on the discovered features 143 this.membersOnly = info.containsFeature("muc_membersonly"); 144 this.moderated = info.containsFeature("muc_moderated"); 145 this.nonanonymous = info.containsFeature("muc_nonanonymous"); 146 this.passwordProtected = info.containsFeature("muc_passwordprotected"); 147 this.persistent = info.containsFeature("muc_persistent"); 148 149 List<DiscoverInfo.Identity> identities = info.getIdentities(); 150 // XEP-45 6.4 is not really clear on the topic if an identity needs to 151 // be send together with the disco result and how to call this description. 152 if (!identities.isEmpty()) { 153 this.name = identities.get(0).getName(); 154 } else { 155 LOGGER.warning("DiscoverInfo does not contain any Identity: " + info.toXML(null)); 156 this.name = ""; 157 } 158 String subject = ""; 159 int occupantsCount = -1; 160 String description = ""; 161 int maxhistoryfetch = -1; 162 List<EntityBareJid> contactJid = null; 163 String lang = null; 164 String ldapgroup = null; 165 Boolean subjectmod = null; 166 URL logs = null; 167 String pubsub = null; 168 // Get the information based on the discovered extended information 169 form = Form.getFormFrom(info); 170 if (form != null) { 171 FormField descField = form.getField("muc#roominfo_description"); 172 if (descField != null && !descField.getValues().isEmpty()) { 173 // Prefer the extended result description 174 description = descField.getFirstValue(); 175 } 176 177 FormField subjField = form.getField("muc#roominfo_subject"); 178 if (subjField != null && !subjField.getValues().isEmpty()) { 179 subject = subjField.getFirstValue(); 180 } 181 182 FormField occCountField = form.getField("muc#roominfo_occupants"); 183 if (occCountField != null && !occCountField.getValues().isEmpty()) { 184 occupantsCount = Integer.parseInt(occCountField.getFirstValue()); 185 } 186 187 FormField maxhistoryfetchField = form.getField("muc#maxhistoryfetch"); 188 if (maxhistoryfetchField != null && !maxhistoryfetchField.getValues().isEmpty()) { 189 maxhistoryfetch = Integer.parseInt(maxhistoryfetchField.getFirstValue()); 190 } 191 192 FormField contactJidField = form.getField("muc#roominfo_contactjid"); 193 if (contactJidField != null && !contactJidField.getValues().isEmpty()) { 194 List<CharSequence> contactJidValues = contactJidField.getValues(); 195 contactJid = JidUtil.filterEntityBareJidList(JidUtil.jidSetFrom(contactJidValues)); 196 } 197 198 FormField langField = form.getField("muc#roominfo_lang"); 199 if (langField != null && !langField.getValues().isEmpty()) { 200 lang = langField.getFirstValue(); 201 } 202 203 FormField ldapgroupField = form.getField("muc#roominfo_ldapgroup"); 204 if (ldapgroupField != null && !ldapgroupField.getValues().isEmpty()) { 205 ldapgroup = ldapgroupField.getFirstValue(); 206 } 207 208 FormField subjectmodField = form.getField("muc#roominfo_subjectmod"); 209 if (subjectmodField != null && !subjectmodField.getValues().isEmpty()) { 210 subjectmod = Boolean.valueOf(subjectmodField.getFirstValue()); 211 } 212 213 FormField urlField = form.getField("muc#roominfo_logs"); 214 if (urlField != null && !urlField.getValues().isEmpty()) { 215 String urlString = urlField.getFirstValue(); 216 try { 217 logs = new URL(urlString); 218 } catch (MalformedURLException e) { 219 LOGGER.log(Level.SEVERE, "Could not parse URL", e); 220 } 221 } 222 223 FormField pubsubField = form.getField("muc#roominfo_pubsub"); 224 if (pubsubField != null && !pubsubField.getValues().isEmpty()) { 225 pubsub = pubsubField.getFirstValue(); 226 } 227 } 228 this.description = description; 229 this.subject = subject; 230 this.occupantsCount = occupantsCount; 231 this.maxhistoryfetch = maxhistoryfetch; 232 this.contactJid = contactJid; 233 this.lang = lang; 234 this.ldapgroup = ldapgroup; 235 this.subjectmod = subjectmod; 236 this.logs = logs; 237 this.pubsub = pubsub; 238 } 239 240 /** 241 * Returns the JID of the room whose information was discovered. 242 * 243 * @return the JID of the room whose information was discovered. 244 */ 245 public EntityBareJid getRoom() { 246 return room; 247 } 248 249 /** 250 * Returns the room name. 251 * <p> 252 * The name returnd here was provided as value of the name attribute 253 * of the returned identity within the disco#info result. 254 * </p> 255 * 256 * @return the name of the room. 257 */ 258 public String getName() { 259 return name; 260 } 261 262 /** 263 * Returns the discovered description of the room. 264 * <p> 265 * The description returned by this method was provided as value of the form 266 * field of the extended disco info result. It may be <code>null</code>. 267 * </p> 268 * 269 * @return the discovered description of the room or null 270 */ 271 public String getDescription() { 272 return description; 273 } 274 275 /** 276 * Returns the discovered subject of the room. The subject may be null if the room does not 277 * have a subject. 278 * 279 * @return the discovered subject of the room or null 280 */ 281 public String getSubject() { 282 return subject; 283 } 284 285 /** 286 * Returns the discovered number of occupants that are currently in the room. If this 287 * information was not discovered (i.e. the server didn't send it) then a value of -1 will be 288 * returned. 289 * 290 * @return the number of occupants that are currently in the room or -1 if that information was 291 * not provided by the server. 292 */ 293 public int getOccupantsCount() { 294 return occupantsCount; 295 } 296 297 /** 298 * Returns true if the room has restricted the access so that only members may enter the room. 299 * 300 * @return true if the room has restricted the access so that only members may enter the room. 301 */ 302 public boolean isMembersOnly() { 303 return membersOnly; 304 } 305 306 /** 307 * Returns true if the room enabled only participants to speak. Occupants with a role of 308 * visitor won't be able to speak in the room. 309 * 310 * @return true if the room enabled only participants to speak. 311 */ 312 public boolean isModerated() { 313 return moderated; 314 } 315 316 /** 317 * Returns true if presence packets will include the JID of every occupant. 318 * 319 * @return true if presence packets will include the JID of every occupant. 320 */ 321 public boolean isNonanonymous() { 322 return nonanonymous; 323 } 324 325 /** 326 * Returns true if users musy provide a valid password in order to join the room. 327 * 328 * @return true if users musy provide a valid password in order to join the room. 329 */ 330 public boolean isPasswordProtected() { 331 return passwordProtected; 332 } 333 334 /** 335 * Returns true if the room will persist after the last occupant have left the room. 336 * 337 * @return true if the room will persist after the last occupant have left the room. 338 */ 339 public boolean isPersistent() { 340 return persistent; 341 } 342 343 /** 344 * Returns the maximum number of history messages which are returned by the 345 * room or '-1' if this property is not reported by the room. 346 * 347 * @return the maximum number of history messages or '-1' 348 */ 349 public int getMaxHistoryFetch() { 350 return maxhistoryfetch; 351 } 352 353 /** 354 * Returns Contact Addresses as JIDs, if such are reported. 355 * 356 * @return a list of contact addresses for this room. 357 */ 358 public List<EntityBareJid> getContactJids() { 359 return Collections.unmodifiableList(contactJid); 360 } 361 362 /** 363 * Returns the natural language of the room discussion, or <code>null</code>. 364 * 365 * @return the language of the room discussion or <code>null</code>. 366 */ 367 public String getLang() { 368 return lang; 369 } 370 371 /** 372 * Returns an associated LDAP group that defines room membership. The 373 * value should be an LDAP Distinguished Name according to an 374 * implementation-specific or deployment-specific definition of a group. 375 * 376 * @return an associated LDAP group or <code>null</code> 377 */ 378 public String getLdapGroup() { 379 return ldapgroup; 380 } 381 382 /** 383 * Returns an Boolean instance with the value 'true' if the subject can be 384 * modified by the room participants, 'false' if not, or <code>null</code> 385 * if this information is reported by the room. 386 * 387 * @return an boolean that is true if the subject can be modified by 388 * participants or <code>null</code> 389 */ 390 public Boolean isSubjectModifiable() { 391 return subjectmod; 392 } 393 394 /** 395 * An associated pubsub node for this room or <code>null</code>. 396 * 397 * @return the associated pubsub node or <code>null</code> 398 */ 399 public String getPubSub() { 400 return pubsub; 401 } 402 403 /** 404 * Returns the URL where archived discussion logs can be found or 405 * <code>null</code> if there is no such URL. 406 * 407 * @return the URL where archived logs can be found or <code>null</code> 408 */ 409 public URL getLogsUrl() { 410 return logs; 411 } 412 413 /** 414 * Returns the form included in the extended disco info result or 415 * <code>null</code> if no such form was sent. 416 * 417 * @return The room info form or <code>null</code> 418 * @see <a 419 * href="http://xmpp.org/extensions/xep-0045.html#disco-roominfo">XEP-45: 420 * Multi User Chat - 6.5 Querying for Room Information</a> 421 */ 422 public Form getForm() { 423 return form; 424 } 425 426}