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