Package org.apache.vysper.xmpp.modules.extension.xep0045_muc.model

Examples of org.apache.vysper.xmpp.modules.extension.xep0045_muc.model.Room


    public void initialize() {
        // do nothing
    }

    public Room createRoom(Entity jid, String name, RoomType... roomTypes) {
        Room room = new Room(jid, name, roomTypes);
        rooms.put(jid, room);
        return room;
    }
View Full Code Here


    @Override
    protected Stanza handleSet(IQStanza stanza, ServerRuntimeContext serverRuntimeContext, SessionContext sessionContext) {
        logger.debug("Received MUC admin stanza");
       
        Room room = conference.findRoom(stanza.getTo());

        Occupant moderator = room.findOccupantByJID(stanza.getFrom());

        // check if moderator
        if (moderator.getRole() != Role.Moderator) {
            // only moderators are allowed to continue
            logger.debug("Only moderators are allowed to issue admin stanzas");
View Full Code Here

    private Stanza available(PresenceStanza stanza, Entity roomJid, Entity newOccupantJid, String nick,
            ServerRuntimeContext serverRuntimeContext) {

        boolean newRoom = false;
        // TODO what to use for the room name?
        Room room = conference.findRoom(roomJid);
        if(room == null) {
            room = conference.createRoom(roomJid, roomJid.getNode());
            newRoom = true;
        }

        if (room.isInRoom(newOccupantJid)) {
            // user is already in room, change nick
            logger.debug("{} has requested to change nick in room {}", newOccupantJid, roomJid);

            // occupant is already in room
            Occupant occupant = room.findOccupantByJID(newOccupantJid);
            if (nick.equals(occupant.getNick())) {
                // nick unchanged, change show and status
                for (Occupant receiver : room.getOccupants()) {
                    sendChangeShowStatus(occupant, receiver, room, getInnerElementText(stanza, "show"),
                            getInnerElementText(stanza, "status"), serverRuntimeContext);
                }
            } else {
                if (room.isInRoom(nick)) {
                    // user with this nick is already in room
                    return createPresenceErrorStanza(roomJid, newOccupantJid, stanza.getID(), "cancel", "conflict");
                }

                String oldNick = occupant.getNick();
                // update the nick
                occupant.setNick(nick);

                // send out unavailable presences to all existing occupants
                for (Occupant receiver : room.getOccupants()) {
                    sendChangeNickUnavailable(occupant, oldNick, receiver, room, serverRuntimeContext);
                }

                // send out available presences to all existing occupants
                for (Occupant receiver : room.getOccupants()) {
                    sendChangeNickAvailable(occupant, receiver, room, serverRuntimeContext);
                }

            }
        } else {
            logger.debug("{} has requested to enter room {}", newOccupantJid, roomJid);

            if (room.isInRoom(nick)) {
                // user with this nick is already in room
                return createPresenceErrorStanza(roomJid, newOccupantJid, stanza.getID(), "cancel", "conflict");
            }

            // check password if password protected
            if (room.isRoomType(RoomType.PasswordProtected)) {
                X x = X.fromStanza(stanza);
                String password = null;
                if (x != null) {
                    password = x.getPasswordValue();
                }

                if (password == null || !password.equals(room.getPassword())) {
                    // password missing or not matching
                    return createPresenceErrorStanza(roomJid, newOccupantJid, stanza.getID(), "auth", "not-authorized");
                }
            }

            Occupant newOccupant;
            try {
                newOccupant = room.addOccupant(newOccupantJid, nick);
            } catch(RuntimeException e) {
                return createPresenceErrorStanza(roomJid, newOccupantJid, stanza.getID(), "auth", e.getMessage());
            }
           
            if(newRoom) {
                room.getAffiliations().add(newOccupantJid, Affiliation.Owner);
                newOccupant.setRole(Role.Moderator);
            }

            // relay presence of all existing room occupants to the now joined occupant
            for (Occupant occupant : room.getOccupants()) {
                sendExistingOccupantToNewOccupant(newOccupant, occupant, room, serverRuntimeContext);
            }

            // relay presence of the newly added occupant to all existing occupants
            for (Occupant occupant : room.getOccupants()) {
                sendNewOccupantPresenceToExisting(newOccupant, occupant, room, serverRuntimeContext);
            }

            // send discussion history to user
            boolean includeJid = room.isRoomType(RoomType.NonAnonymous);
            List<Stanza> history = room.getHistory().createStanzas(newOccupant, includeJid, History.fromStanza(stanza));
            relayStanzas(newOccupantJid, history, serverRuntimeContext);

            logger.debug("{} successfully entered room {}", newOccupantJid, roomJid);
        }
        return null;
View Full Code Here

        return null;
    }

    private Stanza unavailable(PresenceStanza stanza, Entity roomJid, Entity occupantJid, String nick,
            ServerRuntimeContext serverRuntimeContext) {
        Room room = conference.findRoom(roomJid);

        // room must exist, or we do nothing
        if (room != null) {
            Occupant exitingOccupant = room.findOccupantByJID(occupantJid);

            // user must by in room, or we do nothing
            if (exitingOccupant != null) {
                Set<Occupant> allOccupants = room.getOccupants();

                room.removeOccupant(occupantJid);

                // TODO replace with use of X
                String statusMessage = null;
                try {
                    XMLElement statusElement = stanza.getSingleInnerElementsNamed("status");
                    if (statusElement != null && statusElement.getInnerText() != null) {
                        statusMessage = statusElement.getInnerText().getText();
                    }
                } catch (XMLSemanticError e) {
                    // ignore, status element did not exist
                }

                // relay presence of the newly added occupant to all existing occupants
                for (Occupant occupant : allOccupants) {
                    sendExitRoomPresenceToExisting(exitingOccupant, occupant, room, statusMessage, serverRuntimeContext);
                }

                if (room.isRoomType(RoomType.Temporary) && room.isEmpty()) {
                    conference.deleteRoom(roomJid);
                }
            }
        }
View Full Code Here

                return createMessageErrorStanza(roomJid, from, stanza.getID(), StanzaErrorType.MODIFY,
                        StanzaErrorCondition.BAD_REQUEST, stanza);
            }

            logger.debug("Received groupchat message to {}", roomJid);
            Room room = conference.findRoom(roomJid);
            if (room != null) {
                Occupant sendingOccupant = room.findOccupantByJID(from);

                // sender must be participant in room
                if (sendingOccupant != null) {

                    Entity roomAndSendingNick = new EntityImpl(room.getJID(), sendingOccupant.getNick());
                    if (sendingOccupant.hasVoice()) {
                        // relay message to all occupants in room

                        try {
                            if (stanza.getSubjects() != null && !stanza.getSubjects().isEmpty()) {
                                // subject message
                                if (!room.isRoomType(RoomType.OpenSubject) && !sendingOccupant.isModerator()) {
                                    // room only allows moderators to change the subject, and sender is not a moderator
                                    return createMessageErrorStanza(room.getJID(), from, stanza.getID(),
                                            StanzaErrorType.AUTH, StanzaErrorCondition.FORBIDDEN, stanza);
                                }
                            }
                        } catch (XMLSemanticError e) {
                            // not a subject message, ignore exception
                        }

                        logger.debug("Relaying message to all room occupants");
                        for (Occupant occupent : room.getOccupants()) {
                            logger.debug("Relaying message to  {}", occupent);
                            List<Attribute> replaceAttributes = new ArrayList<Attribute>();
                            replaceAttributes.add(new Attribute("from", roomAndSendingNick.getFullQualifiedName()));
                            replaceAttributes.add(new Attribute("to", occupent.getJid().getFullQualifiedName()));

                            relayStanza(occupent.getJid(), StanzaBuilder.createClone(stanza, true, replaceAttributes)
                                    .build(), serverRuntimeContext);

                        }

                        // add to discussion history
                        room.getHistory().append(stanza, sendingOccupant);
                    } else {
                        return createMessageErrorStanza(room.getJID(), from, stanza.getID(), StanzaErrorType.MODIFY,
                                StanzaErrorCondition.FORBIDDEN, stanza);
                    }
                } else {
                    return createMessageErrorStanza(room.getJID(), from, stanza.getID(), StanzaErrorType.MODIFY,
                            StanzaErrorCondition.NOT_ACCEPTABLE, stanza);
                }
            } else {
                return createMessageErrorStanza(moduleDomain, from, stanza.getID(), StanzaErrorType.MODIFY,
                        StanzaErrorCondition.ITEM_NOT_FOUND, stanza);
            }
        } else if (type == null || type == MessageStanzaType.CHAT || type == MessageStanzaType.NORMAL) {
            // private message
            logger.debug("Received direct message to {}", roomWithNickJid);
            Room room = conference.findRoom(roomJid);
            if (room != null) {
                Occupant sendingOccupant = room.findOccupantByJID(from);

                // sender must be participant in room
                if(roomWithNickJid.equals(roomJid)) {
                    // check x element
                   
                    if(stanza.getVerifier().onlySubelementEquals("x", NamespaceURIs.JABBER_X_DATA)) {
                        // voice requests
                        logger.debug("Received voice request for room {}", roomJid);
                       
                        handleVoiceRequest(from, sendingOccupant, room, stanza, serverRuntimeContext);
                    } else if(stanza.getVerifier().onlySubelementEquals("x", NamespaceURIs.XEP0045_MUC)) {
                        // invites/declines
                        return handleInvites(stanza, from, sendingOccupant, room, serverRuntimeContext);
                    }
                } else if (roomWithNickJid.isResourceSet()) {
                    if (sendingOccupant != null) {
                        // got resource, private message for occupant
                        Occupant receivingOccupant = room.findOccupantByNick(roomWithNickJid.getResource());

                        // must be sent to an existing occupant in the room
                        if (receivingOccupant != null) {

                            Entity roomAndSendingNick = new EntityImpl(room.getJID(), sendingOccupant.getNick());
                            logger.debug("Relaying message to  {}", receivingOccupant);
                            List<Attribute> replaceAttributes = new ArrayList<Attribute>();
                            replaceAttributes.add(new Attribute("from", roomAndSendingNick.getFullQualifiedName()));
                            replaceAttributes
                                    .add(new Attribute("to", receivingOccupant.getJid().getFullQualifiedName()));

                            relayStanza(receivingOccupant.getJid(), StanzaBuilder.createClone(stanza, true,
                                    replaceAttributes).build(), serverRuntimeContext);
                        } else {
                            // TODO correct error?
                            return createMessageErrorStanza(moduleDomain, from, stanza.getID(), StanzaErrorType.MODIFY,
                                    StanzaErrorCondition.ITEM_NOT_FOUND, stanza);
                        }
                    } else {
                        // user must be occupant to send direct message
                        return createMessageErrorStanza(room.getJID(), from, stanza.getID(), StanzaErrorType.MODIFY,
                                StanzaErrorCondition.NOT_ACCEPTABLE, stanza);
                    }
                }
            } else {
                return createMessageErrorStanza(moduleDomain, from, stanza.getID(), StanzaErrorType.MODIFY,
View Full Code Here

public class EnterExitRoomIntegrationTestCase extends AbstractMUCIntegrationTestCase {

    public void testEnterRoom() throws Exception {
        chat.join("Nick");

        Room room = conference.findRoom(EntityImpl.parseUnchecked(ROOM_JID));
        assertEquals(1, room.getOccupantCount());
        Occupant occupant = room.getOccupants().iterator().next();
        assertEquals(TEST_USERNAME1, occupant.getJid().getBareJID().getFullQualifiedName());
        assertEquals("Nick", occupant.getNick());

        final BlockingQueue<String> joinedQueue = new LinkedBlockingQueue<String>();
        chat.addParticipantStatusListener(new ParticipantStatusListenerAdapter() {

            @Override
            public void joined(String participant) {
                joinedQueue.add(participant);
            }
        });
        chat2.join("Nick2");
        assertEquals(2, room.getOccupantCount());

        // chat should be notified
        assertEquals(ROOM_JID + "/Nick2", joinedQueue.poll(5000, TimeUnit.MILLISECONDS));
    }
View Full Code Here

    public void testExitRoom() throws Exception {
        chat.join("Nick");
        chat2.join("Nick2");

        Room room = conference.findRoom(EntityImpl.parseUnchecked(ROOM_JID));
        assertEquals(2, room.getOccupantCount());

        final BlockingQueue<String> leftQueue = new LinkedBlockingQueue<String>();
        chat.addParticipantStatusListener(new ParticipantStatusListenerAdapter() {
            @Override
            public void left(String participant) {
                leftQueue.add(participant);
            }
        });

        chat2.leave();

        // wait for status update
        assertEquals(ROOM_JID + "/Nick2", leftQueue.poll(5000, TimeUnit.MILLISECONDS));
        assertEquals(1, room.getOccupantCount());

    }
View Full Code Here

public class ModerationIntegrationTestCase extends AbstractMUCIntegrationTestCase {

    public void testGrantModeration() throws Exception {
        chat.join(NICK1);
       
        Room room = conference.findRoom(EntityImpl.parseUnchecked(ROOM_JID));
        assertEquals(Role.Moderator, room.findOccupantByNick(NICK1).getRole());
       
        chat2.join(NICK2);
       
        chat.grantModerator(NICK2);
        assertEquals(Role.Moderator, room.findOccupantByNick(NICK2).getRole());
       
    }
View Full Code Here

    }

    public void testEnterRoom() throws Exception {
        chat.join("Nick");

        Room room = conference.findRoom(EntityImpl.parseUnchecked(ROOM_JID));
        assertEquals(1, room.getOccupantCount());
        Occupant occupant = room.getOccupants().iterator().next();
        assertEquals(TEST_USERNAME1, occupant.getJid().getBareJID().getFullQualifiedName());
        assertEquals("Nick", occupant.getName());

        final BlockingQueue<String> joinedQueue = new LinkedBlockingQueue<String>();
        chat.addParticipantStatusListener(new ParticipantStatusListenerAdapter() {

            @Override
            public void joined(String participant) {
                joinedQueue.add(participant);
            }
        });
        chat2.join("Nick2");
        assertEquals(2, room.getOccupantCount());

        // chat should be notified
        assertEquals(ROOM_JID + "/Nick2", joinedQueue.poll(5000, TimeUnit.MILLISECONDS));
    }
View Full Code Here

    public void testExitRoom() throws Exception {
        chat.join("Nick");
        chat2.join("Nick2");

        Room room = conference.findRoom(EntityImpl.parseUnchecked(ROOM_JID));
        assertEquals(2, room.getOccupantCount());

        final BlockingQueue<String> leftQueue = new LinkedBlockingQueue<String>();
        chat.addParticipantStatusListener(new ParticipantStatusListenerAdapter() {
            @Override
            public void left(String participant) {
                leftQueue.add(participant);
            }
        });

        chat2.leave();

        // wait for status update
        assertEquals(ROOM_JID + "/Nick2", leftQueue.poll(5000, TimeUnit.MILLISECONDS));
        assertEquals(1, room.getOccupantCount());

    }
View Full Code Here

TOP

Related Classes of org.apache.vysper.xmpp.modules.extension.xep0045_muc.model.Room

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.