001/**
002 *
003 * Copyright 2003-2007 Jive Software, 2014-2015 Florian Schmaus
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 */
017package org.jivesoftware.smackx.muc.packet;
018
019import org.jivesoftware.smack.packet.NamedElement;
020import org.jivesoftware.smack.packet.Stanza;
021import org.jivesoftware.smack.util.XmlStringBuilder;
022
023import org.jivesoftware.smackx.muc.MUCAffiliation;
024import org.jivesoftware.smackx.muc.MUCRole;
025
026import org.jxmpp.jid.Jid;
027import org.jxmpp.jid.parts.Resourcepart;
028
029/**
030 * Item child that holds information about roles, affiliation, jids and nicks.
031 *
032 * @author Gaston Dombiak
033 */
034public class MUCItem implements NamedElement {
035    public static final String ELEMENT = Stanza.ITEM;
036
037    private final MUCAffiliation affiliation;
038    private final MUCRole role;
039    private final Jid actor;
040    private final Resourcepart actorNick;
041    private final String reason;
042    private final Jid jid;
043    private final Resourcepart nick;
044
045    public MUCItem(MUCAffiliation affiliation) {
046        this(affiliation, null, null, null, null, null, null);
047    }
048
049    public MUCItem(MUCRole role) {
050        this(null, role, null, null, null, null, null);
051    }
052
053    public MUCItem(MUCRole role, Resourcepart nick) {
054        this(null, role, null, null, null, nick, null);
055    }
056
057    public MUCItem(MUCAffiliation affiliation, Jid jid, String reason) {
058        this(affiliation, null, null, reason, jid, null, null);
059    }
060
061    public MUCItem(MUCAffiliation affiliation, Jid jid) {
062        this(affiliation, null, null, null, jid, null, null);
063    }
064
065    public MUCItem(MUCRole role, Resourcepart nick, String reason) {
066        this(null, role, null, reason, null, nick, null);
067    }
068
069    /**
070     * Creates a new item child.
071     *
072     * @param affiliation the actor's affiliation to the room
073     * @param role the privilege level of an occupant within a room.
074     * @param actor TODO javadoc me please
075     * @param reason TODO javadoc me please
076     * @param jid TODO javadoc me please
077     * @param nick TODO javadoc me please
078     * @param actorNick TODO javadoc me please
079     */
080    public MUCItem(MUCAffiliation affiliation, MUCRole role, Jid actor,
081                    String reason, Jid jid, Resourcepart nick, Resourcepart actorNick) {
082        this.affiliation = affiliation;
083        this.role = role;
084        this.actor = actor;
085        this.reason = reason;
086        this.jid = jid;
087        this.nick = nick;
088        this.actorNick = actorNick;
089    }
090
091    /**
092     * Returns the actor (JID of an occupant in the room) that was kicked or banned.
093     *
094     * @return the JID of an occupant in the room that was kicked or banned.
095     */
096    public Jid getActor() {
097        return actor;
098    }
099
100    /**
101     * Get the nickname of the actor.
102     *
103     * @return the nickname of the actor.
104     * @since 4.2
105     */
106    public Resourcepart getActorNick() {
107        return actorNick;
108    }
109
110    /**
111     * Returns the reason for the item child. The reason is optional and could be used to explain
112     * the reason why a user (occupant) was kicked or banned.
113     *
114     * @return the reason for the item child.
115     */
116    public String getReason() {
117        return reason;
118    }
119
120    /**
121     * Returns the occupant's affiliation to the room. The affiliation is a semi-permanent
122     * association or connection with a room. The possible affiliations are "owner", "admin",
123     * "member", and "outcast" (naturally it is also possible to have no affiliation). An
124     * affiliation lasts across a user's visits to a room.
125     *
126     * @return the actor's affiliation to the room
127     */
128    public MUCAffiliation getAffiliation() {
129        return affiliation;
130    }
131
132    /**
133     * Returns the <room@service/nick> by which an occupant is identified within the context of a
134     * room. If the room is non-anonymous, the JID will be included in the item.
135     *
136     * @return the room JID by which an occupant is identified within the room.
137     */
138    public Jid getJid() {
139        return jid;
140    }
141
142    /**
143     * Returns the new nickname of an occupant that is changing his/her nickname. The new nickname
144     * is sent as part of the unavailable presence.
145     *
146     * @return the new nickname of an occupant that is changing his/her nickname.
147     */
148    public Resourcepart getNick() {
149        return nick;
150    }
151
152    /**
153     * Returns the temporary position or privilege level of an occupant within a room. The possible
154     * roles are "moderator", "participant", "visitor" and "none" (it is also possible to have no defined
155     * role). A role lasts only for the duration of an occupant's visit to a room.
156     *
157     * @return the privilege level of an occupant within a room.
158     */
159    public MUCRole getRole() {
160        return role;
161    }
162
163    @Override
164    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
165        XmlStringBuilder xml = new XmlStringBuilder(this);
166        xml.optAttribute("affiliation", getAffiliation());
167        xml.optAttribute("jid", getJid());
168        xml.optAttribute("nick", getNick());
169        xml.optAttribute("role", getRole());
170        xml.rightAngleBracket();
171        xml.optElement("reason", getReason());
172        if (getActor() != null) {
173            xml.halfOpenElement("actor").attribute("jid", getActor()).closeEmptyElement();
174        }
175        xml.closeElement(Stanza.ITEM);
176        return xml;
177    }
178
179    @Override
180    public String getElementName() {
181        return ELEMENT;
182    }
183}