Home | History | Annotate | Download | only in packet
      1 /**
      2  * $RCSfile$
      3  * $Revision$
      4  * $Date$
      5  *
      6  * Copyright 2003-2007 Jive Software.
      7  *
      8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      9  * you may not use this file except in compliance with the License.
     10  * You may obtain a copy of the License at
     11  *
     12  *     http://www.apache.org/licenses/LICENSE-2.0
     13  *
     14  * Unless required by applicable law or agreed to in writing, software
     15  * distributed under the License is distributed on an "AS IS" BASIS,
     16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     17  * See the License for the specific language governing permissions and
     18  * limitations under the License.
     19  */
     20 
     21 package org.jivesoftware.smackx.packet;
     22 import java.util.ArrayList;
     23 import java.util.Collections;
     24 import java.util.Iterator;
     25 import java.util.List;
     26 
     27 import org.jivesoftware.smack.packet.IQ;
     28 
     29 /**
     30  * IQ packet that serves for kicking users, granting and revoking voice, banning users,
     31  * modifying the ban list, granting and revoking membership and granting and revoking
     32  * moderator privileges. All these operations are scoped by the
     33  * 'http://jabber.org/protocol/muc#admin' namespace.
     34  *
     35  * @author Gaston Dombiak
     36  */
     37 public class MUCAdmin extends IQ {
     38 
     39     private List<Item> items = new ArrayList<Item>();
     40 
     41     /**
     42      * Returns an Iterator for item childs that holds information about roles, affiliation,
     43      * jids and nicks.
     44      *
     45      * @return an Iterator for item childs that holds information about roles, affiliation,
     46      *          jids and nicks.
     47      */
     48     public Iterator<Item> getItems() {
     49         synchronized (items) {
     50             return Collections.unmodifiableList(new ArrayList<Item>(items)).iterator();
     51         }
     52     }
     53 
     54     /**
     55      * Adds an item child that holds information about roles, affiliation, jids and nicks.
     56      *
     57      * @param item the item child that holds information about roles, affiliation, jids and nicks.
     58      */
     59     public void addItem(Item item) {
     60         synchronized (items) {
     61             items.add(item);
     62         }
     63     }
     64 
     65     public String getChildElementXML() {
     66         StringBuilder buf = new StringBuilder();
     67         buf.append("<query xmlns=\"http://jabber.org/protocol/muc#admin\">");
     68         synchronized (items) {
     69             for (int i = 0; i < items.size(); i++) {
     70                 Item item = items.get(i);
     71                 buf.append(item.toXML());
     72             }
     73         }
     74         // Add packet extensions, if any are defined.
     75         buf.append(getExtensionsXML());
     76         buf.append("</query>");
     77         return buf.toString();
     78     }
     79 
     80     /**
     81      * Item child that holds information about roles, affiliation, jids and nicks.
     82      *
     83      * @author Gaston Dombiak
     84      */
     85     public static class Item {
     86         private String actor;
     87         private String reason;
     88         private String affiliation;
     89         private String jid;
     90         private String nick;
     91         private String role;
     92 
     93         /**
     94          * Creates a new item child.
     95          *
     96          * @param affiliation the actor's affiliation to the room
     97          * @param role the privilege level of an occupant within a room.
     98          */
     99         public Item(String affiliation, String role) {
    100             this.affiliation = affiliation;
    101             this.role = role;
    102         }
    103 
    104         /**
    105          * Returns the actor (JID of an occupant in the room) that was kicked or banned.
    106          *
    107          * @return the JID of an occupant in the room that was kicked or banned.
    108          */
    109         public String getActor() {
    110             return actor;
    111         }
    112 
    113         /**
    114          * Returns the reason for the item child. The reason is optional and could be used to
    115          * explain the reason why a user (occupant) was kicked or banned.
    116          *
    117          * @return the reason for the item child.
    118          */
    119         public String getReason() {
    120             return reason;
    121         }
    122 
    123         /**
    124          * Returns the occupant's affiliation to the room. The affiliation is a semi-permanent
    125          * association or connection with a room. The possible affiliations are "owner", "admin",
    126          * "member", and "outcast" (naturally it is also possible to have no affiliation). An
    127          * affiliation lasts across a user's visits to a room.
    128          *
    129          * @return the actor's affiliation to the room
    130          */
    131         public String getAffiliation() {
    132             return affiliation;
    133         }
    134 
    135         /**
    136          * Returns the <room@service/nick> by which an occupant is identified within the context
    137          * of a room. If the room is non-anonymous, the JID will be included in the item.
    138          *
    139          * @return the room JID by which an occupant is identified within the room.
    140          */
    141         public String getJid() {
    142             return jid;
    143         }
    144 
    145         /**
    146          * Returns the new nickname of an occupant that is changing his/her nickname. The new
    147          * nickname is sent as part of the unavailable presence.
    148          *
    149          * @return the new nickname of an occupant that is changing his/her nickname.
    150          */
    151         public String getNick() {
    152             return nick;
    153         }
    154 
    155         /**
    156          * Returns the temporary position or privilege level of an occupant within a room. The
    157          * possible roles are "moderator", "participant", and "visitor" (it is also possible to
    158          * have no defined role). A role lasts only for the duration of an occupant's visit to
    159          * a room.
    160          *
    161          * @return the privilege level of an occupant within a room.
    162          */
    163         public String getRole() {
    164             return role;
    165         }
    166 
    167         /**
    168          * Sets the actor (JID of an occupant in the room) that was kicked or banned.
    169          *
    170          * @param actor the actor (JID of an occupant in the room) that was kicked or banned.
    171          */
    172         public void setActor(String actor) {
    173             this.actor = actor;
    174         }
    175 
    176         /**
    177          * Sets the reason for the item child. The reason is optional and could be used to
    178          * explain the reason why a user (occupant) was kicked or banned.
    179          *
    180          * @param reason the reason why a user (occupant) was kicked or banned.
    181          */
    182         public void setReason(String reason) {
    183             this.reason = reason;
    184         }
    185 
    186         /**
    187          * Sets the <room@service/nick> by which an occupant is identified within the context
    188          * of a room. If the room is non-anonymous, the JID will be included in the item.
    189          *
    190          * @param jid the JID by which an occupant is identified within a room.
    191          */
    192         public void setJid(String jid) {
    193             this.jid = jid;
    194         }
    195 
    196         /**
    197          * Sets the new nickname of an occupant that is changing his/her nickname. The new
    198          * nickname is sent as part of the unavailable presence.
    199          *
    200          * @param nick the new nickname of an occupant that is changing his/her nickname.
    201          */
    202         public void setNick(String nick) {
    203             this.nick = nick;
    204         }
    205 
    206         public String toXML() {
    207             StringBuilder buf = new StringBuilder();
    208             buf.append("<item");
    209             if (getAffiliation() != null) {
    210                 buf.append(" affiliation=\"").append(getAffiliation()).append("\"");
    211             }
    212             if (getJid() != null) {
    213                 buf.append(" jid=\"").append(getJid()).append("\"");
    214             }
    215             if (getNick() != null) {
    216                 buf.append(" nick=\"").append(getNick()).append("\"");
    217             }
    218             if (getRole() != null) {
    219                 buf.append(" role=\"").append(getRole()).append("\"");
    220             }
    221             if (getReason() == null && getActor() == null) {
    222                 buf.append("/>");
    223             }
    224             else {
    225                 buf.append(">");
    226                 if (getReason() != null) {
    227                     buf.append("<reason>").append(getReason()).append("</reason>");
    228                 }
    229                 if (getActor() != null) {
    230                     buf.append("<actor jid=\"").append(getActor()).append("\"/>");
    231                 }
    232                 buf.append("</item>");
    233             }
    234             return buf.toString();
    235         }
    236     };
    237 }
    238