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

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


    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);

            boolean nickConflict = room.isInRoom(nick);
            boolean nickRewritten = false;
            int counter = 1; // max conflicts, to avoid DoS attacks
            String rewrittenNick = null;
            while (nickConflict && counter < 100 && room.rewritesDuplicateNick()) {
                rewrittenNick = nick + "_" + counter;
                nickConflict = room.isInRoom(rewrittenNick);
                if (nickConflict) {
                    counter++;
                } else {
                    nick = rewrittenNick;
                    nickRewritten = true;
                }
            }
               
            if (nickConflict) {
                // 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);
            }

            // if the new occupant is a server admin, he will be for the room, too
            final ServerAdministrationService adhocCommandsService = (ServerAdministrationService)serverRuntimeContext.getServerRuntimeContextService(ServerAdministrationService.SERVICE_ID);
            if (adhocCommandsService != null && adhocCommandsService.isAdmin(newOccupantJid.getBareJID())) {
                final Affiliations roomAffiliations = room.getAffiliations();
                // make new occupant an Admin, but do not downgrade from Owner
                // Admin affilitation implies Moderator role (see XEP-0045 5.1.2)
                if (roomAffiliations.getAffiliation(newOccupantJid) != Affiliation.Owner) {
                    roomAffiliations.add(newOccupantJid, Affiliation.Admin);
                    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, nickRewritten);
            }

            // 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) {
                Collection<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_USER)) {
                        // 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

    @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 == null || moderator.getRole() != Role.Moderator) {
            // only moderators are allowed to continue
            logger.debug("Only moderators are allowed to issue admin stanzas");
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

        assertPresenceErrorStanza(response, ROOM1_JID, OCCUPANT1_JID, StanzaErrorType.MODIFY, StanzaErrorCondition.JID_MALFORMED);
    }

    public void testEnterWithPassword() throws Exception {
        Room room = conference.createRoom(ROOM2_JID, "Room 1", RoomType.PasswordProtected);
        room.setPassword("secret");

        // no error should be returned
        assertNull(enterRoom(OCCUPANT1_JID, ROOM2_JID_WITH_NICK, "secret", null, false));
        assertEquals(1, room.getOccupantCount());
    }
View Full Code Here

        assertNull(enterRoom(OCCUPANT1_JID, ROOM2_JID_WITH_NICK, "secret", null, false));
        assertEquals(1, room.getOccupantCount());
    }

    public void testEnterWithoutPassword() throws Exception {
        Room room = conference.createRoom(ROOM2_JID, "Room 1", RoomType.PasswordProtected);
        room.setPassword("secret");

        // try entering without a password
        Stanza response = enterRoom(OCCUPANT1_JID, ROOM2_JID_WITH_NICK);

        assertPresenceErrorStanza(response, ROOM2_JID, OCCUPANT1_JID, StanzaErrorType.AUTH, StanzaErrorCondition.NOT_AUTHORIZED);
View Full Code Here

    }

    public void testEnterRoomWithRelays() throws Exception {

        // add one occupant to the room
        Room room = conference.findOrCreateRoom(ROOM1_JID, "Room 1");
        room.addOccupant(OCCUPANT2_JID, "Some nick");

        // now, let user 1 enter room
        enterRoom(OCCUPANT1_JID, ROOM1_JID_WITH_NICK);

        // verify stanzas to existing occupants on the new user
View Full Code Here

    }

    public void testDiscussionHistory() throws Exception {
        // add some messages
        Room room = conference.findOrCreateRoom(ROOM1_JID, "Room 1");
        room.getHistory().append(
                StanzaBuilder.createMessageStanza(OCCUPANT2_JID, ROOM1_JID, MessageStanzaType.GROUPCHAT, null, "Body")
                        .build(), new Occupant(OCCUPANT2_JID, "nick2", room, Role.Participant));
        room.getHistory().append(
                StanzaBuilder.createMessageStanza(OCCUPANT2_JID, ROOM1_JID, MessageStanzaType.GROUPCHAT, null, "Body2")
                        .build(), new Occupant(OCCUPANT2_JID, "nick2", room, Role.Participant));
        room.getHistory().append(
                StanzaBuilder.createMessageStanza(OCCUPANT2_JID, ROOM1_JID, MessageStanzaType.GROUPCHAT, null, "Body3")
                        .build(), new Occupant(OCCUPANT2_JID, "nick2", room, Role.Participant));

        // now, let user 1 enter room
        enterRoom(OCCUPANT1_JID, ROOM1_JID_WITH_NICK, null, new History(2, null, null, null), false);
View Full Code Here

/**
*/
public class MessageTestCase extends AbstractMUCMessageHandlerTestCase {

    public void testMessageWithNoVoice() throws Exception {
        Room room = conference.findOrCreateRoom(ROOM1_JID, "Room 1");
        Occupant occupant = room.addOccupant(OCCUPANT1_JID, "nick");
        // make sure the occupant has no voice
        occupant.setRole(Role.Visitor);

        testNotAllowedMessage(room, StanzaErrorCondition.FORBIDDEN);
    }
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.