MUCItem.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software, 2014-2015 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.NamedElement;
  19. import org.jivesoftware.smack.packet.Stanza;
  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 = Stanza.ITEM;

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

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

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

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

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

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

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

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

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

  86.     /**
  87.      * Get the nickname of the actor.
  88.      *
  89.      * @return the nickname of the actor.
  90.      * @since 4.2
  91.      */
  92.     public Resourcepart getActorNick() {
  93.         return actorNick;
  94.     }

  95.     /**
  96.      * Returns the reason for the item child. The reason is optional and could be used to explain
  97.      * the reason why a user (occupant) was kicked or banned.
  98.      *
  99.      * @return the reason for the item child.
  100.      */
  101.     public String getReason() {
  102.         return reason;
  103.     }

  104.     /**
  105.      * Returns the occupant's affiliation to the room. The affiliation is a semi-permanent
  106.      * association or connection with a room. The possible affiliations are "owner", "admin",
  107.      * "member", and "outcast" (naturally it is also possible to have no affiliation). An
  108.      * affiliation lasts across a user's visits to a room.
  109.      *
  110.      * @return the actor's affiliation to the room
  111.      */
  112.     public MUCAffiliation getAffiliation() {
  113.         return affiliation;
  114.     }

  115.     /**
  116.      * Returns the <room@service/nick> by which an occupant is identified within the context of a
  117.      * room. If the room is non-anonymous, the JID will be included in the item.
  118.      *
  119.      * @return the room JID by which an occupant is identified within the room.
  120.      */
  121.     public Jid getJid() {
  122.         return jid;
  123.     }

  124.     /**
  125.      * Returns the new nickname of an occupant that is changing his/her nickname. The new nickname
  126.      * is sent as part of the unavailable presence.
  127.      *
  128.      * @return the new nickname of an occupant that is changing his/her nickname.
  129.      */
  130.     public Resourcepart getNick() {
  131.         return nick;
  132.     }

  133.     /**
  134.      * Returns the temporary position or privilege level of an occupant within a room. The possible
  135.      * roles are "moderator", "participant", "visitor" and "none" (it is also possible to have no defined
  136.      * role). A role lasts only for the duration of an occupant's visit to a room.
  137.      *
  138.      * @return the privilege level of an occupant within a room.
  139.      */
  140.     public MUCRole getRole() {
  141.         return role;
  142.     }

  143.     @Override
  144.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  145.         XmlStringBuilder xml = new XmlStringBuilder(this);
  146.         xml.optAttribute("affiliation", getAffiliation());
  147.         xml.optAttribute("jid", getJid());
  148.         xml.optAttribute("nick", getNick());
  149.         xml.optAttribute("role", getRole());
  150.         xml.rightAngleBracket();
  151.         xml.optElement("reason", getReason());
  152.         if (getActor() != null || getActorNick() != null) {
  153.             xml.halfOpenElement("actor");
  154.             xml.optAttribute("jid", getActor());
  155.             xml.optAttribute("nick", getActorNick());
  156.             xml.closeEmptyElement();
  157.         }
  158.         xml.closeElement(Stanza.ITEM);
  159.         return xml;
  160.     }

  161.     @Override
  162.     public String getElementName() {
  163.         return ELEMENT;
  164.     }
  165. }