ChatRoomImpl.java 10.4 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5 6 7 8 9 10 11 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 110 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 211 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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
/*
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * Copyright (C) 1999-2003 CoolServlets, Inc. All rights reserved.
 *
 * This software is the proprietary information of CoolServlets, Inc.
 * Use is subject to license terms.
 */
package org.jivesoftware.messenger.chat.spi;

import org.jivesoftware.messenger.chat.*;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.spi.MessageImpl;
import org.jivesoftware.messenger.spi.PresenceImpl;
import org.jivesoftware.messenger.user.UserAlreadyExistsException;
import org.jivesoftware.messenger.user.UserNotFoundException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Simple in-memory implementation of a chatroom.
 *
 * @author Derek DeMoro
 * @author Iain Shigeoka
 */
public class ChatRoomImpl implements ChatRoom {

    private ChatServer server;
    private Map<String, ChatRole> members = new ConcurrentHashMap<String, ChatRole>();
    private String name;
    private ChatRole role;
    private PacketRouter router;
    private HistoryStrategy historyStrategy;
    private long startTime;
    private long endTime;
    private ChatRoomHistory roomHistory;

    /**
     * Create a new chat room.
     *
     * @param chatserver the server hosting the room.
     * @param roomname the name of the room.
     * @param packetRouter the router for sending packets from the room.
     */
    ChatRoomImpl(ChatServer chatserver, String roomname, PacketRouter packetRouter) {
        server = chatserver;
        name = roomname;
        router = packetRouter;
        startTime = System.currentTimeMillis();
        roomHistory = new ChatRoomHistory(roomname);
        roomHistory.setStartTime(startTime);
        role = new RoomRole(this);
        historyStrategy = new HistoryStrategy(server.getHistoryStrategy());
    }

    public String getName() {
        return name;
    }

    public long getID() {
        return -1;
    }

    public HistoryStrategy getHistoryStrategy() {
        return historyStrategy;
    }

    public ChatRole getRole() throws UnauthorizedException {
        return role;
    }

    public ChatRole getMember(String nickname) throws UnauthorizedException, UserNotFoundException {
        return members.get(nickname.toLowerCase());
    }

    public Iterator<ChatRole> getMembers() throws UnauthorizedException {
        return Collections.unmodifiableCollection(members.values()).iterator();
    }

    public boolean hasMember(String nickname) throws UnauthorizedException {
        return members.containsKey(nickname.toLowerCase());
    }

    public synchronized ChatRole joinRoom(String nickname, ChatUser user)
            throws UnauthorizedException, UserAlreadyExistsException
    {
        ChatRoleImpl joinRole = null;
        if (members.containsKey(nickname.toLowerCase())) {
            throw new UserAlreadyExistsException();
        }
        joinRole = new ChatRoleImpl(server, this, nickname, (ChatUserImpl)user, router);

        // Handle ChatRoomHistory
        if (roomHistory.getUserID() == null) {
            roomHistory.setUserID(nickname);
        }
        else {
            roomHistory.userJoined(user, new Date());
        }

        Iterator iter = members.values().iterator();
        while (iter.hasNext()) {
            ChatRole memberRole = (ChatRole)iter.next();
            Presence memberPresence =
                    (Presence)memberRole.getPresence().createDeepCopy();
            memberPresence.setSender(memberRole.getRoleAddress());
            joinRole.send(memberPresence);
        }
        members.put(nickname.toLowerCase(), joinRole);
        if (joinRole != null) {
            List params = new ArrayList();
            params.add(nickname);
            try {
                Presence joinPresence = (Presence)joinRole.getPresence().createDeepCopy();
                joinPresence.setSender(joinRole.getRoleAddress());
                broadcast(joinPresence);
            }
            catch (Exception e) {
                Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
            }
            Iterator history = historyStrategy.getMessageHistory();
            while (history.hasNext()) {
                joinRole.send((Message)history.next());
            }
            serverBroadcast(LocaleUtils.getLocalizedString("chat.join", params));
        }
        return joinRole;
    }

    public synchronized void leaveRoom(String nickname) throws UnauthorizedException,
            UserNotFoundException
    {
        ChatRole leaveRole = null;
        leaveRole = (ChatRole)members.remove(nickname.toLowerCase());
        //Testing
        ChatUser user = leaveRole.getChatUser();

        roomHistory.userLeft(user, new Date());

        if (members.isEmpty()) {
            // Adding new ChatTranscript logic.
            endTime = System.currentTimeMillis();

            // Update RoomHistory with HistoryStrategy
            roomHistory.setHistory(getHistoryStrategy());
            roomHistory.setEndTime(System.currentTimeMillis());

            // Update ChatAuditManager
            ChatAuditManager cAuditManager = ChatAuditManager.getInstance();
            cAuditManager.addChatHistory(roomHistory);
            cAuditManager.fireChatRoomClosed(server.getChatRoom(name));

            server.removeChatRoom(name);
        }

        if (leaveRole != null) {
            try {
                Presence presence = createPresence(Presence.STATUS_OFFLINE);
                presence.setSender(leaveRole.getRoleAddress());
                broadcast((Presence)presence.createDeepCopy());
                leaveRole.kick();
                List params = new ArrayList();
                params.add(nickname);
                serverBroadcast(LocaleUtils.getLocalizedString("chat.leave", params));
            }
            catch (Exception e) {
                Log.error(e);
            }
        }
    }

    public Presence createPresence(int presenceStatus) throws UnauthorizedException {
        Presence presence = new PresenceImpl();
        presence.setSender(role.getRoleAddress());
        switch (presenceStatus) {
            case Presence.STATUS_INVISIBLE:
                presence.setAvailable(true);
                presence.setVisible(false);
                break;
            case Presence.STATUS_ONLINE:
                presence.setAvailable(true);
                presence.setVisible(true);
                break;
            case Presence.STATUS_OFFLINE:
                presence.setAvailable(false);
                presence.setVisible(false);
                break;
            default:
        }
        return presence;
    }

    public void serverBroadcast(String msg) throws UnauthorizedException {
        Message message = new MessageImpl();
        message.setType(Message.GROUP_CHAT);
        message.setBody(msg);
        message.setSender(role.getRoleAddress());
        historyStrategy.addMessage(message);
        broadcast(message);
    }

    public void send(Message packet) throws UnauthorizedException {
        String resource = packet.getRecipient().getResource();
        if (resource == null || resource.trim().length() == 0) {
            resource = null;
        }
        if (resource == null && Message.GROUP_CHAT == packet.getType()) {
            // normal groupchat
            historyStrategy.addMessage(packet);
            broadcast(packet);
        }
        else if (resource != null && (Message.CHAT == packet.getType()
                || Message.NORMAL == packet.getType()))
        {
            // private message
            ChatRole member = members.get(resource.toLowerCase());
            if (member != null) {
                packet.setRecipient(member.getChatUser().getAddress());
                router.route(packet);
            }
        }
        else {
            packet = (Message)packet.createDeepCopy();
            packet.setError(XMPPError.Code.BAD_REQUEST);
            packet.setRecipient(packet.getSender());
            packet.setSender(role.getRoleAddress());
            router.route(packet);
        }
    }

    public void send(Presence packet) throws UnauthorizedException {
        broadcast(packet);
    }

    public void send(IQ packet) throws UnauthorizedException {
        packet = (IQ)packet.createDeepCopy();
        packet.setError(XMPPError.Code.BAD_REQUEST);
        packet.setRecipient(packet.getSender());
        packet.setSender(role.getRoleAddress());
        router.route(packet);
    }


    private void broadcast(XMPPPacket packet) {
        for (ChatRole member : members.values()) {
            packet.setRecipient(member.getChatUser().getAddress());
            router.route(packet);
        }
    }

    /**
     * An empty role that represents the room itself in the chatroom.
     * Chatrooms need to be able to speak (server messages) and so
     * must have their own role in the chatroom.
     */
    private class RoomRole implements ChatRole {

        private ChatRoom room;

        private RoomRole(ChatRoom room) {
            this.room = room;
        }

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

        public void setPresence(Presence presence) throws UnauthorizedException {
        }

        public void setRole(int newRole) throws UnauthorizedException {
        }

        public int getRole() throws UnauthorizedException {
            return ChatRole.OWNER;
        }

        public String getNickname() throws UnauthorizedException {
            return null;
        }

        public void kick() throws UnauthorizedException {
        }

        public ChatUser getChatUser() {
            return null;
        }

        public ChatRoom getChatRoom() {
            return room;
        }

        private XMPPAddress crJID;

        public XMPPAddress getRoleAddress() {
            if (crJID == null) {
                crJID = new XMPPAddress(room.getName(), server.getChatServerName(), "");
            }
            return crJID;
        }

        public void send(Message packet) throws UnauthorizedException {
            room.send(packet);
        }

        public void send(Presence packet) throws UnauthorizedException {
            room.send(packet);
        }

        public void send(IQ packet) throws UnauthorizedException {
            room.send(packet);
        }
    }

    public long getChatLength() {
        return endTime - startTime;
    }
}