MUCRoleImpl.java 7.42 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
package org.jivesoftware.messenger.muc.spi;

import org.jivesoftware.messenger.muc.*;
import org.jivesoftware.messenger.IQ;
import org.jivesoftware.messenger.Message;
import org.jivesoftware.messenger.MetaDataFragment;
import org.jivesoftware.messenger.PacketRouter;
import org.jivesoftware.messenger.Presence;
import org.jivesoftware.messenger.XMPPAddress;
import org.jivesoftware.messenger.auth.UnauthorizedException;

/**
 * Simple in-memory implementation of a role in a chatroom
 * 
 * @author Gaston Dombiak
 */
// TODO Review name of this class that represents role and affiliation at the same time.!!!!
public class MUCRoleImpl implements MUCRole {

    /**
     * The room this role is valid in.
     */
    private MUCRoomImpl room;

    /**
     * The user of the role.
     */
    private MUCUserImpl user;

    /**
     * The user's nickname in the room.
     */
    private String nick;

    /**
     * The user's presence in the room.
     */
    private Presence presence;

    /**
     * The chatserver that hosts this role.
     */
    private MultiUserChatServer server;

    /**
     * The role ID.
     */
    private int role;

    /**
     * The affiliation ID.
     */
    private int affiliation;

    /**
     * The router used to send packets from this role.
     */
    private PacketRouter router;

    /**
     * The address of the person masquerading in this role.
     */
    private XMPPAddress rJID;

    /**
     * A fragment containing the x-extension for non-anonymous rooms.
     */
    private MetaDataFragment extendedInformation;

    /**
     * Create a new role.
     * 
     * @param chatserver the server hosting the role.
     * @param chatroom the room the role is valid in.
     * @param nickname the nickname of the user in the role.
     * @param role the role of the user in the room.
     * @param affiliation the affiliation of the user in the room.
     * @param chatuser the user on the chat server.
     * @param packetRouter the packet router for sending messages from this role.
     * @throws UnauthorizedException if the role could not be created due to security or permission
     *             violations
     */
    public MUCRoleImpl(MultiUserChatServer chatserver,
                       MUCRoomImpl chatroom,
                       String nickname,
                       int role,
                       int affiliation,
                       MUCUserImpl chatuser,
                       PacketRouter packetRouter) throws UnauthorizedException {
        this.room = chatroom;
        this.nick = nickname;
        this.user = chatuser;
        this.server = chatserver;
        this.router = packetRouter;
        this.role = role;
        this.affiliation = affiliation;
        extendedInformation = new MetaDataFragment("http://jabber.org/protocol/muc#user", "x");
        calculateExtendedInformation();
110
        rJID = new XMPPAddress(room.getName(), server.getServiceName(), nick);
Matt Tucker's avatar
Matt Tucker committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        setPresence(room.createPresence(Presence.STATUS_ONLINE));
    }

    public Presence getPresence() throws UnauthorizedException {
        return presence;
    }

    public MetaDataFragment getExtendedPresenceInformation() throws UnauthorizedException {
        return extendedInformation;
    }

    public void setPresence(Presence newPresence) throws UnauthorizedException {
        this.presence = newPresence;
        if (extendedInformation != null) {
            presence.addFragment(extendedInformation);
        }
    }

    public void setRole(int newRole) throws UnauthorizedException, NotAllowedException {
        // Don't allow to change the role to an owner or admin unless the new role is moderator
        if (MUCRole.OWNER == affiliation || MUCRole.ADMINISTRATOR == affiliation) {
            if (MUCRole.MODERATOR != newRole) {
                throw new NotAllowedException();
            }
        }
        // A moderator cannot be kicked from a room
        if (MUCRole.MODERATOR == role && MUCRole.NONE_ROLE == newRole) {
            throw new NotAllowedException();
        }
        // TODO A moderator MUST NOT be able to revoke voice from a user whose affiliation is at or
        // above the moderator's level.

        role = newRole;
        if (MUCRole.NONE_ROLE == role) {
            presence.setAvailable(false);
            presence.setVisible(false);
        }
        calculateExtendedInformation();
    }

    public int getRole() {
        return role;
    }

    public String getRoleAsString() {
        if (MUCRole.MODERATOR == role) {
            return "moderator";
        }
        else if (MUCRole.PARTICIPANT == role) {
            return "participant";
        }
        else if (MUCRole.VISITOR == role) {
            return "visitor";
        }
        return "none";
    }

    public void setAffiliation(int newAffiliation) throws UnauthorizedException,
            NotAllowedException {
        // Don't allow to ban an owner or an admin
        if (MUCRole.OWNER == affiliation || MUCRole.ADMINISTRATOR == affiliation) {
            if (MUCRole.OUTCAST == newAffiliation) {
                throw new NotAllowedException();
            }
        }
        affiliation = newAffiliation;
        // TODO The fragment is being calculated twice (1. setting the role & 2. setting the aff)
        calculateExtendedInformation();
    }

    public int getAffiliation() {
        return affiliation;
    }

    public String getAffiliationAsString() {
        if (MUCRole.OWNER == affiliation) {
            return "owner";
        }
        else if (MUCRole.ADMINISTRATOR == affiliation) {
            return "admin";
        }
        else if (MUCRole.MEMBER == affiliation) {
            return "member";
        }
        else if (MUCRole.OUTCAST == affiliation) {
            return "outcast";
        }
        return "none";
    }

    public String getNickname() {
        return nick;
    }

    public void kick() throws UnauthorizedException {
        getChatUser().removeRole(room.getName());
    }

    public void changeNickname(String nickname) {
        this.nick = nickname;
211
        rJID = new XMPPAddress(room.getName(), server.getServiceName(), nick);
Matt Tucker's avatar
Matt Tucker committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    }

    public MUCUser getChatUser() {
        return user;
    }

    public MUCRoom getChatRoom() {
        return room;
    }

    public XMPPAddress getRoleAddress() {
        return rJID;
    }

    public void send(Presence packet) throws UnauthorizedException {
        packet.setRecipient(user.getAddress());
        router.route(packet);
    }

    public void send(Message packet) throws UnauthorizedException {
        packet.setRecipient(user.getAddress());
        router.route(packet);
    }

    public void send(IQ packet) throws UnauthorizedException {
        packet.setRecipient(user.getAddress());
        router.route(packet);
    }

    /**
     * Calculates and sets the extended presence information to add to the presence. The information
     * to add contains the user's jid, affiliation and role.
     */
    private void calculateExtendedInformation() throws UnauthorizedException {
        extendedInformation.setProperty("x.item:jid", user.getAddress().toString());
        extendedInformation.setProperty("x.item:affiliation", getAffiliationAsString());
        extendedInformation.setProperty("x.item:role", getRoleAsString());
    }
}