Search in sources :

Example 6 with Message

use of org.xmpp.packet.Message in project Openfire by igniterealtime.

the class LocalMUCRoom method sendInvitationRejection.

@Override
public void sendInvitationRejection(JID to, String reason, JID sender) {
    if (((MultiUserChatServiceImpl) mucService).getMUCDelegate() != null) {
        switch(((MultiUserChatServiceImpl) mucService).getMUCDelegate().sendingInvitationRejection(this, to, sender, reason)) {
            case HANDLED_BY_DELEGATE:
                //if the delegate is taking care of it, there's nothing for us to do
                return;
            case HANDLED_BY_OPENFIRE:
                //continue as normal if we're asked to handle it
                break;
        }
    }
    Message message = new Message();
    message.setFrom(role.getRoleAddress());
    message.setTo(to);
    Element frag = message.addChildElement("x", "http://jabber.org/protocol/muc#user");
    frag.addElement("decline").addAttribute("from", sender.toBareJID());
    if (reason != null && reason.length() > 0) {
        frag.element("decline").addElement("reason").setText(reason);
    }
    // Send the message with the invitation
    router.route(message);
}
Also used : Message(org.xmpp.packet.Message) Element(org.dom4j.Element)

Example 7 with Message

use of org.xmpp.packet.Message in project Openfire by igniterealtime.

the class LocalMUCRoom method serverBroadcast.

@Override
public void serverBroadcast(String msg) {
    Message message = new Message();
    message.setType(Message.Type.groupchat);
    message.setBody(msg);
    message.setFrom(role.getRoleAddress());
    broadcast(message);
}
Also used : Message(org.xmpp.packet.Message)

Example 8 with Message

use of org.xmpp.packet.Message in project Openfire by igniterealtime.

the class LocalMUCRoom method joinRoom.

@Override
public LocalMUCRole joinRoom(String nickname, String password, HistoryRequest historyRequest, LocalMUCUser user, Presence presence) throws UnauthorizedException, UserAlreadyExistsException, RoomLockedException, ForbiddenException, RegistrationRequiredException, ConflictException, ServiceUnavailableException, NotAcceptableException {
    if (((MultiUserChatServiceImpl) mucService).getMUCDelegate() != null) {
        if (!((MultiUserChatServiceImpl) mucService).getMUCDelegate().joiningRoom(this, user.getAddress())) {
            // Delegate said no, reject join.
            throw new UnauthorizedException();
        }
    }
    LocalMUCRole joinRole = null;
    lock.writeLock().lock();
    boolean clientOnlyJoin = false;
    // A "client only join" here is one where the client is already joined, but has re-joined.
    try {
        // If the room has a limit of max user then check if the limit has been reached
        if (!canJoinRoom(user)) {
            throw new ServiceUnavailableException();
        }
        final JID bareJID = user.getAddress().asBareJID();
        boolean isOwner = owners.includes(bareJID);
        // If the room is locked and this user is not an owner raise a RoomLocked exception
        if (isLocked()) {
            if (!isOwner) {
                throw new RoomLockedException();
            }
        }
        // Check if the nickname is already used in the room
        if (occupantsByNickname.containsKey(nickname.toLowerCase())) {
            List<MUCRole> occupants = occupantsByNickname.get(nickname.toLowerCase());
            MUCRole occupant = occupants.size() > 0 ? occupants.get(0) : null;
            if (occupant != null && !occupant.getUserAddress().toBareJID().equals(bareJID.toBareJID())) {
                // Nickname is already used, and not by the same JID
                throw new UserAlreadyExistsException();
            }
            if (occupant.getUserAddress().equals(user.getAddress())) {
                // This user is already an occupant. The client thinks it isn't. (Or else this is a broken gmail).
                clientOnlyJoin = true;
            }
        }
        // Unauthorized exception
        if (isPasswordProtected()) {
            if (password == null || !password.equals(getPassword())) {
                throw new UnauthorizedException();
            }
        }
        // raise a ConflictException
        if (members.containsValue(nickname.toLowerCase())) {
            if (!nickname.toLowerCase().equals(members.get(bareJID))) {
                throw new ConflictException();
            }
        }
        if (isLoginRestrictedToNickname()) {
            String reservedNickname = members.get(bareJID);
            if (reservedNickname != null && !nickname.toLowerCase().equals(reservedNickname)) {
                throw new NotAcceptableException();
            }
        }
        // Set the corresponding role based on the user's affiliation
        MUCRole.Role role;
        MUCRole.Affiliation affiliation;
        if (isOwner) {
            // The user is an owner. Set the role and affiliation accordingly.
            role = MUCRole.Role.moderator;
            affiliation = MUCRole.Affiliation.owner;
        } else if (mucService.isSysadmin(bareJID)) {
            // The user is a system administrator of the MUC service. Treat him as an owner
            // although he won't appear in the list of owners
            role = MUCRole.Role.moderator;
            affiliation = MUCRole.Affiliation.owner;
        } else if (admins.includes(bareJID)) {
            // The user is an admin. Set the role and affiliation accordingly.
            role = MUCRole.Role.moderator;
            affiliation = MUCRole.Affiliation.admin;
        } else // explicit outcast status has higher precedence than member status
        if (outcasts.contains(bareJID)) {
            // The user is an outcast. Raise a "Forbidden" exception.
            throw new ForbiddenException();
        } else if (members.includesKey(bareJID)) {
            // The user is a member. Set the role and affiliation accordingly.
            role = MUCRole.Role.participant;
            affiliation = MUCRole.Affiliation.member;
        } else // this checks if the user is an outcast implicitly (via a group)
        if (outcasts.includes(bareJID)) {
            // The user is an outcast. Raise a "Forbidden" exception.
            throw new ForbiddenException();
        } else {
            // The user has no affiliation (i.e. NONE). Set the role accordingly.
            if (isMembersOnly()) {
                // "Registration Required" exception.
                throw new RegistrationRequiredException();
            }
            role = (isModerated() ? MUCRole.Role.visitor : MUCRole.Role.participant);
            affiliation = MUCRole.Affiliation.none;
        }
        if (!clientOnlyJoin) {
            // Create a new role for this user in this room
            joinRole = new LocalMUCRole(mucService, this, nickname, role, affiliation, user, presence, router);
            // Add the new user as an occupant of this room
            List<MUCRole> occupants = occupantsByNickname.get(nickname.toLowerCase());
            if (occupants == null) {
                occupants = new ArrayList<>();
                occupantsByNickname.put(nickname.toLowerCase(), occupants);
            }
            occupants.add(joinRole);
            // Update the tables of occupants based on the bare and full JID
            List<MUCRole> list = occupantsByBareJID.get(bareJID);
            if (list == null) {
                list = new ArrayList<>();
                occupantsByBareJID.put(bareJID, list);
            }
            list.add(joinRole);
            occupantsByFullJID.put(user.getAddress(), joinRole);
        } else {
            // Grab the existing one.
            joinRole = (LocalMUCRole) occupantsByFullJID.get(user.getAddress());
        }
    } finally {
        lock.writeLock().unlock();
    }
    // Notify other cluster nodes that a new occupant joined the room
    CacheFactory.doClusterTask(new OccupantAddedEvent(this, joinRole));
    // Send presence of existing occupants to new occupant
    sendInitialPresences(joinRole);
    // It is assumed that the room is new based on the fact that it's locked and
    // that it was locked when it was created.
    boolean isRoomNew = isLocked() && creationDate.getTime() == lockedTime;
    try {
        // Send the presence of this new occupant to existing occupants
        Presence joinPresence = joinRole.getPresence().createCopy();
        broadcastPresence(joinPresence, true);
    } catch (Exception e) {
        Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
    }
    // confirmed" message
    if (!isRoomNew && isLocked()) {
        // http://xmpp.org/extensions/xep-0045.html#enter-locked
        Presence presenceItemNotFound = new Presence(Presence.Type.error);
        presenceItemNotFound.setError(PacketError.Condition.item_not_found);
        presenceItemNotFound.setFrom(role.getRoleAddress());
        joinRole.send(presenceItemNotFound);
    }
    if (historyRequest == null) {
        Iterator<Message> history = roomHistory.getMessageHistory();
        while (history.hasNext()) {
            joinRole.send(history.next());
        }
    } else {
        historyRequest.sendHistory(joinRole, roomHistory);
    }
    Message roomSubject = roomHistory.getChangedSubject();
    if (roomSubject != null) {
        joinRole.send(roomSubject);
    }
    if (!clientOnlyJoin) {
        // Update the date when the last occupant left the room
        setEmptyDate(null);
        // Fire event that occupant joined the room
        MUCEventDispatcher.occupantJoined(getRole().getRoleAddress(), user.getAddress(), joinRole.getNickname());
    }
    return joinRole;
}
Also used : ForbiddenException(org.jivesoftware.openfire.muc.ForbiddenException) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) ConflictException(org.jivesoftware.openfire.muc.ConflictException) OccupantAddedEvent(org.jivesoftware.openfire.muc.cluster.OccupantAddedEvent) ServiceUnavailableException(org.jivesoftware.openfire.muc.ServiceUnavailableException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) ForbiddenException(org.jivesoftware.openfire.muc.ForbiddenException) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException) RoomLockedException(org.jivesoftware.openfire.muc.RoomLockedException) CannotBeInvitedException(org.jivesoftware.openfire.muc.CannotBeInvitedException) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) NotFoundException(org.jivesoftware.util.NotFoundException) ConflictException(org.jivesoftware.openfire.muc.ConflictException) RegistrationRequiredException(org.jivesoftware.openfire.muc.RegistrationRequiredException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) IOException(java.io.IOException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) NotAcceptableException(org.jivesoftware.openfire.muc.NotAcceptableException) ServiceUnavailableException(org.jivesoftware.openfire.muc.ServiceUnavailableException) MUCRole(org.jivesoftware.openfire.muc.MUCRole) NotAcceptableException(org.jivesoftware.openfire.muc.NotAcceptableException) RoomLockedException(org.jivesoftware.openfire.muc.RoomLockedException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence) RegistrationRequiredException(org.jivesoftware.openfire.muc.RegistrationRequiredException)

Example 9 with Message

use of org.xmpp.packet.Message in project Openfire by igniterealtime.

the class LocalMUCRoom method send.

@Override
public void send(Packet packet) {
    if (packet instanceof Message) {
        broadcast((Message) packet);
    } else if (packet instanceof Presence) {
        broadcastPresence((Presence) packet, false);
    } else if (packet instanceof IQ) {
        IQ reply = IQ.createResultIQ((IQ) packet);
        reply.setChildElement(((IQ) packet).getChildElement());
        reply.setError(PacketError.Condition.bad_request);
        router.route(reply);
    }
}
Also used : Message(org.xmpp.packet.Message) IQ(org.xmpp.packet.IQ) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence)

Example 10 with Message

use of org.xmpp.packet.Message in project Openfire by igniterealtime.

the class SocketReader method process.

protected void process(Element doc) throws Exception {
    if (doc == null) {
        return;
    }
    String tag = doc.getName();
    if ("message".equals(tag)) {
        Message packet;
        try {
            packet = new Message(doc);
        } catch (IllegalArgumentException e) {
            Log.debug("SocketReader: Rejecting packet. JID malformed", e);
            // The original packet contains a malformed JID so answer with an error.
            Message reply = new Message();
            reply.setID(doc.attributeValue("id"));
            reply.setTo(session.getAddress());
            reply.getElement().addAttribute("from", doc.attributeValue("to"));
            reply.setError(PacketError.Condition.jid_malformed);
            session.process(reply);
            return;
        }
        processMessage(packet);
    } else if ("presence".equals(tag)) {
        Presence packet;
        try {
            packet = new Presence(doc);
        } catch (IllegalArgumentException e) {
            Log.debug("SocketReader: Rejecting packet. JID malformed", e);
            // The original packet contains a malformed JID so answer an error
            Presence reply = new Presence();
            reply.setID(doc.attributeValue("id"));
            reply.setTo(session.getAddress());
            reply.getElement().addAttribute("from", doc.attributeValue("to"));
            reply.setError(PacketError.Condition.jid_malformed);
            session.process(reply);
            return;
        }
        // Check that the presence type is valid. If not then assume available type
        try {
            packet.getType();
        } catch (IllegalArgumentException e) {
            Log.debug("Invalid presence (type): " + packet);
            // The presence packet contains an invalid presence type so replace it with
            // an available presence type
            packet.setType(null);
        }
        // Check that the presence show is valid. If not then assume available show value
        try {
            packet.getShow();
        } catch (IllegalArgumentException e) {
            Log.debug("Invalid presence (show): " + packet);
            // The presence packet contains an invalid presence show so replace it with
            // an available presence show
            packet.setShow(null);
        }
        if (session.getStatus() == Session.STATUS_CLOSED && packet.isAvailable()) {
            // Ignore available presence packets sent from a closed session. A closed
            // session may have buffered data pending to be processes so we want to ignore
            // just Presences of type available
            Log.warn("Ignoring available presence packet of closed session: " + packet);
            return;
        }
        processPresence(packet);
    } else if ("iq".equals(tag)) {
        IQ packet;
        try {
            packet = getIQ(doc);
        } catch (IllegalArgumentException e) {
            Log.debug("SocketReader: Rejecting packet. JID malformed", e);
            // The original packet contains a malformed JID so answer an error
            IQ reply = new IQ();
            if (!doc.elements().isEmpty()) {
                reply.setChildElement(((Element) doc.elements().get(0)).createCopy());
            }
            reply.setID(doc.attributeValue("id"));
            reply.setTo(session.getAddress());
            if (doc.attributeValue("to") != null) {
                reply.getElement().addAttribute("from", doc.attributeValue("to"));
            }
            reply.setError(PacketError.Condition.jid_malformed);
            session.process(reply);
            return;
        }
        processIQ(packet);
    } else {
        if (!processUnknowPacket(doc)) {
            Log.warn(LocaleUtils.getLocalizedString("admin.error.packet.tag") + doc.asXML());
            open = false;
        }
    }
}
Also used : Message(org.xmpp.packet.Message) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) Presence(org.xmpp.packet.Presence)

Aggregations

Message (org.xmpp.packet.Message)111 Element (org.dom4j.Element)35 JID (org.xmpp.packet.JID)25 Test (org.junit.Test)23 Presence (org.xmpp.packet.Presence)18 IQ (org.xmpp.packet.IQ)16 ArrayList (java.util.ArrayList)10 Packet (org.xmpp.packet.Packet)10 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)9 Date (java.util.Date)6 ClientSession (org.jivesoftware.openfire.session.ClientSession)6 NotFoundException (org.jivesoftware.util.NotFoundException)6 StringReader (java.io.StringReader)4 List (java.util.List)4 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)4 User (org.jivesoftware.openfire.user.User)4 IOException (java.io.IOException)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 SAXReader (org.dom4j.io.SAXReader)3 DefaultElement (org.dom4j.tree.DefaultElement)3