MUCItem.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software, 2014 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.muc.packet;

  18. import org.jivesoftware.smack.packet.IQ;
  19. import org.jivesoftware.smack.packet.NamedElement;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;
  21. import org.jivesoftware.smackx.muc.MUCAffiliation;
  22. import org.jivesoftware.smackx.muc.MUCRole;
  23. import org.jxmpp.jid.Jid;
  24. import org.jxmpp.jid.parts.Resourcepart;

  25. /**
  26.  * Item child that holds information about roles, affiliation, jids and nicks.
  27.  *
  28.  * @author Gaston Dombiak
  29.  */
  30. public class MUCItem implements NamedElement {
  31.     public static final String ELEMENT = IQ.ITEM;

  32.     private final MUCAffiliation affiliation;
  33.     private final MUCRole role;
  34.     private final Jid actor;
  35.     private final String reason;
  36.     private final Jid jid;
  37.     private final Resourcepart nick;

  38.     public MUCItem(MUCAffiliation affiliation) {
  39.         this(affiliation, null, null, null, null, null);
  40.     }

  41.     public MUCItem(MUCRole role) {
  42.         this(null, role, null, null, null, null);
  43.     }

  44.     public MUCItem(MUCRole role, Resourcepart nick) {
  45.         this(null, role, null, null, null, nick);
  46.     }

  47.     public MUCItem(MUCAffiliation affiliation, Jid jid, String reason) {
  48.         this(affiliation, null, null, reason, jid, null);
  49.     }

  50.     public MUCItem(MUCAffiliation affiliation, Jid jid) {
  51.         this(affiliation, null, null, null, jid, null);
  52.     }

  53.     public MUCItem(MUCRole role, Resourcepart nick, String reason) {
  54.         this(null, role, null, reason, null, nick);
  55.     }

  56.     /**
  57.      * Creates a new item child.
  58.      *
  59.      * @param affiliation the actor's affiliation to the room
  60.      * @param role the privilege level of an occupant within a room.
  61.      * @param actor
  62.      * @param reason
  63.      * @param jid
  64.      * @param nick
  65.      */
  66.     public MUCItem(MUCAffiliation affiliation, MUCRole role, Jid actor,
  67.                     String reason, Jid jid, Resourcepart nick) {
  68.         this.affiliation = affiliation;
  69.         this.role = role;
  70.         this.actor = actor;
  71.         this.reason = reason;
  72.         this.jid = jid;
  73.         this.nick = nick;
  74.     }

  75.     /**
  76.      * Returns the actor (JID of an occupant in the room) that was kicked or banned.
  77.      *
  78.      * @return the JID of an occupant in the room that was kicked or banned.
  79.      */
  80.     public Jid getActor() {
  81.         return actor;
  82.     }

  83.     /**
  84.      * Returns the reason for the item child. The reason is optional and could be used to explain
  85.      * the reason why a user (occupant) was kicked or banned.
  86.      *
  87.      * @return the reason for the item child.
  88.      */
  89.     public String getReason() {
  90.         return reason;
  91.     }

  92.     /**
  93.      * Returns the occupant's affiliation to the room. The affiliation is a semi-permanent
  94.      * association or connection with a room. The possible affiliations are "owner", "admin",
  95.      * "member", and "outcast" (naturally it is also possible to have no affiliation). An
  96.      * affiliation lasts across a user's visits to a room.
  97.      *
  98.      * @return the actor's affiliation to the room
  99.      */
  100.     public MUCAffiliation getAffiliation() {
  101.         return affiliation;
  102.     }

  103.     /**
  104.      * Returns the <room@service/nick> by which an occupant is identified within the context of a
  105.      * room. If the room is non-anonymous, the JID will be included in the item.
  106.      *
  107.      * @return the room JID by which an occupant is identified within the room.
  108.      */
  109.     public Jid getJid() {
  110.         return jid;
  111.     }

  112.     /**
  113.      * Returns the new nickname of an occupant that is changing his/her nickname. The new nickname
  114.      * is sent as part of the unavailable presence.
  115.      *
  116.      * @return the new nickname of an occupant that is changing his/her nickname.
  117.      */
  118.     public Resourcepart getNick() {
  119.         return nick;
  120.     }

  121.     /**
  122.      * Returns the temporary position or privilege level of an occupant within a room. The possible
  123.      * roles are "moderator", "participant", "visitor" and "none" (it is also possible to have no defined
  124.      * role). A role lasts only for the duration of an occupant's visit to a room.
  125.      *
  126.      * @return the privilege level of an occupant within a room.
  127.      */
  128.     public MUCRole getRole() {
  129.         return role;
  130.     }

  131.     public XmlStringBuilder toXML() {
  132.         XmlStringBuilder xml = new XmlStringBuilder(this);
  133.         xml.optAttribute("affiliation", getAffiliation());
  134.         xml.optAttribute("jid", getJid());
  135.         xml.optAttribute("nick", getNick());
  136.         xml.optAttribute("role", getRole());
  137.         xml.rightAngleBracket();
  138.         xml.optElement("reason", getReason());
  139.         if (getActor() != null) {
  140.             xml.halfOpenElement("actor").attribute("jid", getActor()).closeEmptyElement();
  141.         }
  142.         xml.closeElement(IQ.ITEM);
  143.         return xml;
  144.     }

  145.     @Override
  146.     public String getElementName() {
  147.         return ELEMENT;
  148.     }
  149. }