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 org.jivesoftware.smack.packet.IQ;
     23 
     24 import java.util.ArrayList;
     25 import java.util.Collections;
     26 import java.util.Iterator;
     27 import java.util.List;
     28 
     29 /**
     30  * IQ packet that serves for granting and revoking ownership privileges, granting
     31  * and revoking administrative privileges and destroying a room. All these operations
     32  * are scoped by the 'http://jabber.org/protocol/muc#owner' namespace.
     33  *
     34  * @author Gaston Dombiak
     35  */
     36 public class MUCOwner extends IQ {
     37 
     38     private List<Item> items = new ArrayList<Item>();
     39     private Destroy destroy;
     40 
     41     /**
     42      * Returns an Iterator for item childs that holds information about affiliation,
     43      * jids and nicks.
     44      *
     45      * @return an Iterator for item childs that holds information about 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      * Returns a request to the server to destroy a room. The sender of the request
     56      * should be the room's owner. If the sender of the destroy request is not the room's owner
     57      * then the server will answer a "Forbidden" error.
     58      *
     59      * @return a request to the server to destroy a room.
     60      */
     61     public Destroy getDestroy() {
     62         return destroy;
     63     }
     64 
     65     /**
     66      * Sets a request to the server to destroy a room. The sender of the request
     67      * should be the room's owner. If the sender of the destroy request is not the room's owner
     68      * then the server will answer a "Forbidden" error.
     69      *
     70      * @param destroy the request to the server to destroy a room.
     71      */
     72     public void setDestroy(Destroy destroy) {
     73         this.destroy = destroy;
     74     }
     75 
     76     /**
     77      * Adds an item child that holds information about affiliation, jids and nicks.
     78      *
     79      * @param item the item child that holds information about affiliation, jids and nicks.
     80      */
     81     public void addItem(Item item) {
     82         synchronized (items) {
     83             items.add(item);
     84         }
     85     }
     86 
     87     public String getChildElementXML() {
     88         StringBuilder buf = new StringBuilder();
     89         buf.append("<query xmlns=\"http://jabber.org/protocol/muc#owner\">");
     90         synchronized (items) {
     91             for (int i = 0; i < items.size(); i++) {
     92                 Item item = (Item) items.get(i);
     93                 buf.append(item.toXML());
     94             }
     95         }
     96         if (getDestroy() != null) {
     97             buf.append(getDestroy().toXML());
     98         }
     99         // Add packet extensions, if any are defined.
    100         buf.append(getExtensionsXML());
    101         buf.append("</query>");
    102         return buf.toString();
    103     }
    104 
    105     /**
    106      * Item child that holds information about affiliation, jids and nicks.
    107      *
    108      * @author Gaston Dombiak
    109      */
    110     public static class Item {
    111 
    112         private String actor;
    113         private String reason;
    114         private String affiliation;
    115         private String jid;
    116         private String nick;
    117         private String role;
    118 
    119         /**
    120          * Creates a new item child.
    121          *
    122          * @param affiliation the actor's affiliation to the room
    123          */
    124         public Item(String affiliation) {
    125             this.affiliation = affiliation;
    126         }
    127 
    128         /**
    129          * Returns the actor (JID of an occupant in the room) that was kicked or banned.
    130          *
    131          * @return the JID of an occupant in the room that was kicked or banned.
    132          */
    133         public String getActor() {
    134             return actor;
    135         }
    136 
    137         /**
    138          * Returns the reason for the item child. The reason is optional and could be used to
    139          * explain the reason why a user (occupant) was kicked or banned.
    140          *
    141          * @return the reason for the item child.
    142          */
    143         public String getReason() {
    144             return reason;
    145         }
    146 
    147         /**
    148          * Returns the occupant's affiliation to the room. The affiliation is a semi-permanent
    149          * association or connection with a room. The possible affiliations are "owner", "admin",
    150          * "member", and "outcast" (naturally it is also possible to have no affiliation). An
    151          * affiliation lasts across a user's visits to a room.
    152          *
    153          * @return the actor's affiliation to the room
    154          */
    155         public String getAffiliation() {
    156             return affiliation;
    157         }
    158 
    159         /**
    160          * Returns the <room@service/nick> by which an occupant is identified within the context
    161          * of a room. If the room is non-anonymous, the JID will be included in the item.
    162          *
    163          * @return the room JID by which an occupant is identified within the room.
    164          */
    165         public String getJid() {
    166             return jid;
    167         }
    168 
    169         /**
    170          * Returns the new nickname of an occupant that is changing his/her nickname. The new
    171          * nickname is sent as part of the unavailable presence.
    172          *
    173          * @return the new nickname of an occupant that is changing his/her nickname.
    174          */
    175         public String getNick() {
    176             return nick;
    177         }
    178 
    179         /**
    180          * Returns the temporary position or privilege level of an occupant within a room. The
    181          * possible roles are "moderator", "participant", and "visitor" (it is also possible to
    182          * have no defined role). A role lasts only for the duration of an occupant's visit to
    183          * a room.
    184          *
    185          * @return the privilege level of an occupant within a room.
    186          */
    187         public String getRole() {
    188             return role;
    189         }
    190 
    191         /**
    192          * Sets the actor (JID of an occupant in the room) that was kicked or banned.
    193          *
    194          * @param actor the actor (JID of an occupant in the room) that was kicked or banned.
    195          */
    196         public void setActor(String actor) {
    197             this.actor = actor;
    198         }
    199 
    200         /**
    201          * Sets the reason for the item child. The reason is optional and could be used to
    202          * explain the reason why a user (occupant) was kicked or banned.
    203          *
    204          * @param reason the reason why a user (occupant) was kicked or banned.
    205          */
    206         public void setReason(String reason) {
    207             this.reason = reason;
    208         }
    209 
    210         /**
    211          * Sets the <room@service/nick> by which an occupant is identified within the context
    212          * of a room. If the room is non-anonymous, the JID will be included in the item.
    213          *
    214          * @param jid the JID by which an occupant is identified within a room.
    215          */
    216         public void setJid(String jid) {
    217             this.jid = jid;
    218         }
    219 
    220         /**
    221          * Sets the new nickname of an occupant that is changing his/her nickname. The new
    222          * nickname is sent as part of the unavailable presence.
    223          *
    224          * @param nick the new nickname of an occupant that is changing his/her nickname.
    225          */
    226         public void setNick(String nick) {
    227             this.nick = nick;
    228         }
    229 
    230         /**
    231          * Sets the temporary position or privilege level of an occupant within a room. The
    232          * possible roles are "moderator", "participant", and "visitor" (it is also possible to
    233          * have no defined role). A role lasts only for the duration of an occupant's visit to
    234          * a room.
    235          *
    236          * @param role the new privilege level of an occupant within a room.
    237          */
    238         public void setRole(String role) {
    239             this.role = role;
    240         }
    241 
    242         public String toXML() {
    243             StringBuilder buf = new StringBuilder();
    244             buf.append("<item");
    245             if (getAffiliation() != null) {
    246                 buf.append(" affiliation=\"").append(getAffiliation()).append("\"");
    247             }
    248             if (getJid() != null) {
    249                 buf.append(" jid=\"").append(getJid()).append("\"");
    250             }
    251             if (getNick() != null) {
    252                 buf.append(" nick=\"").append(getNick()).append("\"");
    253             }
    254             if (getRole() != null) {
    255                 buf.append(" role=\"").append(getRole()).append("\"");
    256             }
    257             if (getReason() == null && getActor() == null) {
    258                 buf.append("/>");
    259             }
    260             else {
    261                 buf.append(">");
    262                 if (getReason() != null) {
    263                     buf.append("<reason>").append(getReason()).append("</reason>");
    264                 }
    265                 if (getActor() != null) {
    266                     buf.append("<actor jid=\"").append(getActor()).append("\"/>");
    267                 }
    268                 buf.append("</item>");
    269             }
    270             return buf.toString();
    271         }
    272     };
    273 
    274     /**
    275      * Represents a request to the server to destroy a room. The sender of the request
    276      * should be the room's owner. If the sender of the destroy request is not the room's owner
    277      * then the server will answer a "Forbidden" error.
    278      *
    279      * @author Gaston Dombiak
    280      */
    281     public static class Destroy {
    282         private String reason;
    283         private String jid;
    284 
    285 
    286         /**
    287          * Returns the JID of an alternate location since the current room is being destroyed.
    288          *
    289          * @return the JID of an alternate location.
    290          */
    291         public String getJid() {
    292             return jid;
    293         }
    294 
    295         /**
    296          * Returns the reason for the room destruction.
    297          *
    298          * @return the reason for the room destruction.
    299          */
    300         public String getReason() {
    301             return reason;
    302         }
    303 
    304         /**
    305          * Sets the JID of an alternate location since the current room is being destroyed.
    306          *
    307          * @param jid the JID of an alternate location.
    308          */
    309         public void setJid(String jid) {
    310             this.jid = jid;
    311         }
    312 
    313         /**
    314          * Sets the reason for the room destruction.
    315          *
    316          * @param reason the reason for the room destruction.
    317          */
    318         public void setReason(String reason) {
    319             this.reason = reason;
    320         }
    321 
    322         public String toXML() {
    323             StringBuilder buf = new StringBuilder();
    324             buf.append("<destroy");
    325             if (getJid() != null) {
    326                 buf.append(" jid=\"").append(getJid()).append("\"");
    327             }
    328             if (getReason() == null) {
    329                 buf.append("/>");
    330             }
    331             else {
    332                 buf.append(">");
    333                 if (getReason() != null) {
    334                     buf.append("<reason>").append(getReason()).append("</reason>");
    335                 }
    336                 buf.append("</destroy>");
    337             }
    338             return buf.toString();
    339         }
    340 
    341     }
    342 }
    343