Search in sources :

Example 71 with Message

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

the class LocalMUCRoom method sendInvitation.

@Override
public void sendInvitation(JID to, String reason, MUCRole senderRole, List<Element> extensions) throws ForbiddenException, CannotBeInvitedException {
    if (!isMembersOnly() || canOccupantsInvite() || MUCRole.Affiliation.admin == senderRole.getAffiliation() || MUCRole.Affiliation.owner == senderRole.getAffiliation()) {
        // If the room is not members-only OR if the room is members-only and anyone can send
        // invitations or the sender is an admin or an owner, then send the invitation
        Message message = new Message();
        message.setFrom(role.getRoleAddress());
        message.setTo(to);
        if (((MultiUserChatServiceImpl) mucService).getMUCDelegate() != null) {
            switch(((MultiUserChatServiceImpl) mucService).getMUCDelegate().sendingInvitation(this, to, senderRole.getUserAddress(), 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;
                case REJECTED:
                    //we can't invite that person
                    throw new CannotBeInvitedException();
            }
        }
        // Add a list of extensions sent with the original message invitation (if any)
        if (extensions != null) {
            for (Element element : extensions) {
                element.setParent(null);
                message.getElement().add(element);
            }
        }
        Element frag = message.addChildElement("x", "http://jabber.org/protocol/muc#user");
        // ChatUser will be null if the room itself (ie. via admin console) made the request
        if (senderRole.getUserAddress() != null) {
            frag.addElement("invite").addAttribute("from", senderRole.getUserAddress().toBareJID());
        }
        if (reason != null && reason.length() > 0) {
            Element invite = frag.element("invite");
            if (invite == null) {
                invite = frag.addElement("invite");
            }
            invite.addElement("reason").setText(reason);
        }
        if (isPasswordProtected()) {
            frag.addElement("password").setText(getPassword());
        }
        // Include the jabber:x:conference information for backward compatibility
        frag = message.addChildElement("x", "jabber:x:conference");
        frag.addAttribute("jid", role.getRoleAddress().toBareJID());
        // Send the message with the invitation
        router.route(message);
    } else {
        throw new ForbiddenException();
    }
}
Also used : CannotBeInvitedException(org.jivesoftware.openfire.muc.CannotBeInvitedException) ForbiddenException(org.jivesoftware.openfire.muc.ForbiddenException) Message(org.xmpp.packet.Message) Element(org.dom4j.Element)

Example 72 with Message

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

the class LocalMUCRoom method sendPrivatePacket.

@Override
public void sendPrivatePacket(Packet packet, MUCRole senderRole) throws NotFoundException, ForbiddenException {
    switch(// intended fall-through
    senderRole.getRole()) {
        case none:
            throw new ForbiddenException();
        default:
        case visitor:
            if (canSendPrivateMessage().equals("participants"))
                throw new ForbiddenException();
        case participant:
            if (canSendPrivateMessage().equals("moderators"))
                throw new ForbiddenException();
        case moderator:
            if (canSendPrivateMessage().equals("none"))
                throw new ForbiddenException();
    }
    String resource = packet.getTo().getResource();
    List<MUCRole> occupants = occupantsByNickname.get(resource.toLowerCase());
    if (occupants == null || occupants.size() == 0) {
        throw new NotFoundException();
    }
    for (MUCRole occupant : occupants) {
        packet.setFrom(senderRole.getRoleAddress());
        occupant.send(packet);
        if (packet instanceof Message) {
            Message message = (Message) packet;
            MUCEventDispatcher.privateMessageRecieved(occupant.getUserAddress(), senderRole.getUserAddress(), message);
        }
    }
}
Also used : ForbiddenException(org.jivesoftware.openfire.muc.ForbiddenException) MUCRole(org.jivesoftware.openfire.muc.MUCRole) Message(org.xmpp.packet.Message) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException) NotFoundException(org.jivesoftware.util.NotFoundException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 73 with Message

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

the class ComponentStanzaHandler method processMessage.

@Override
protected void processMessage(Message packet) throws UnauthorizedException {
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Session is not authenticated so return error
        Message reply = new Message();
        reply.setID(packet.getID());
        reply.setTo(packet.getFrom());
        reply.setFrom(packet.getTo());
        reply.setError(PacketError.Condition.not_authorized);
        session.process(reply);
        return;
    }
    super.processMessage(packet);
}
Also used : Message(org.xmpp.packet.Message)

Example 74 with Message

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

the class LocalMUCRoom method broadcast.

public void broadcast(BroadcastMessageRequest messageRequest) {
    Message message = messageRequest.getMessage();
    // Add message to the room history
    roomHistory.addMessage(message);
    // Send message to occupants connected to this JVM
    for (MUCRole occupant : occupantsByFullJID.values()) {
        // other cluster nodes
        if (occupant.isLocal() && !occupant.isVoiceOnly()) {
            occupant.send(message);
        }
    }
    if (messageRequest.isOriginator() && isLogEnabled()) {
        MUCRole senderRole = null;
        JID senderAddress;
        // convert the MUC nickname/role JID back into a real user JID
        if (message.getFrom() != null && message.getFrom().getResource() != null) {
            // get the first MUCRole for the sender
            List<MUCRole> occupants = occupantsByNickname.get(message.getFrom().getResource().toLowerCase());
            senderRole = occupants == null ? null : occupants.get(0);
        }
        if (senderRole == null) {
            // The room itself is sending the message
            senderAddress = getRole().getRoleAddress();
        } else {
            // An occupant is sending the message
            senderAddress = senderRole.getUserAddress();
        }
        // Log the conversation
        mucService.logConversation(this, message, senderAddress);
    }
    mucService.messageBroadcastedTo(messageRequest.getOccupants());
}
Also used : MUCRole(org.jivesoftware.openfire.muc.MUCRole) Message(org.xmpp.packet.Message) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID)

Example 75 with Message

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

the class PEPService method sendNotification.

@Override
public void sendNotification(Node node, Message message, JID recipientJID) {
    message.setTo(recipientJID);
    message.setFrom(getAddress());
    message.setID(StringUtils.randomString(8));
    // If the recipient subscribed with a bare JID and this PEPService can retrieve
    // presence information for the recipient, collect all of their full JIDs and
    // send the notification to each below.
    Set<JID> recipientFullJIDs = new HashSet<>();
    if (XMPPServer.getInstance().isLocal(recipientJID)) {
        if (recipientJID.getResource() == null) {
            for (ClientSession clientSession : SessionManager.getInstance().getSessions(recipientJID.getNode())) {
                recipientFullJIDs.add(clientSession.getAddress());
            }
        }
    } else {
        // Since recipientJID is not local, try to get presence info from cached known remote
        // presences.
        // TODO: OF-605 the old code depends on a cache that would contain presence state on all (?!) JIDS on all (?!)
        // remote domains. As we cannot depend on this information to be correct (even if we could ensure that this
        // potentially unlimited amount of data would indeed be manageable in the first place), this code was removed.
        recipientFullJIDs.add(recipientJID);
    }
    if (recipientFullJIDs.isEmpty()) {
        router.route(message);
        return;
    }
    for (JID recipientFullJID : recipientFullJIDs) {
        // to the service owner.
        try {
            JID publisher = null;
            // Get the ID of the node that had an item published to or retracted from.
            Element itemsElement = message.getElement().element("event").element("items");
            String nodeID = itemsElement.attributeValue("node");
            // Get the ID of the item that was published or retracted.
            String itemID = null;
            Element itemElement = itemsElement.element("item");
            if (itemElement == null) {
                Element retractElement = itemsElement.element("retract");
                if (retractElement != null) {
                    itemID = retractElement.attributeValue("id");
                }
            } else {
                itemID = itemElement.attributeValue("id");
            }
            // Check if the recipientFullJID is interested in notifications for this node.
            // If the recipient has not yet requested any notification filtering, continue and send
            // the notification.
            EntityCapabilities entityCaps = entityCapsManager.getEntityCapabilities(recipientFullJID);
            if (entityCaps != null) {
                if (!entityCaps.containsFeature(nodeID + "+notify")) {
                    return;
                }
            }
            // This full JID will be used as the "replyto" address in the addressing extension.
            if (node.isCollectionNode()) {
                for (Node leafNode : node.getNodes()) {
                    if (leafNode.getNodeID().equals(nodeID)) {
                        publisher = leafNode.getPublishedItem(itemID).getPublisher();
                        // Ensure the recipientJID has access to receive notifications for items published to the leaf node.
                        AccessModel accessModel = leafNode.getAccessModel();
                        if (!accessModel.canAccessItems(leafNode, recipientFullJID, publisher)) {
                            return;
                        }
                        break;
                    }
                }
            } else {
                publisher = node.getPublishedItem(itemID).getPublisher();
            }
            // Ensure the recipient is subscribed to the service owner's (publisher's) presence.
            if (canProbePresence(publisher, recipientFullJID)) {
                Element addresses = DocumentHelper.createElement(QName.get("addresses", "http://jabber.org/protocol/address"));
                Element address = addresses.addElement("address");
                address.addAttribute("type", "replyto");
                address.addAttribute("jid", publisher.toString());
                Message extendedMessage = message.createCopy();
                extendedMessage.addExtension(new PacketExtension(addresses));
                extendedMessage.setTo(recipientFullJID);
                router.route(extendedMessage);
            }
        } catch (IndexOutOfBoundsException e) {
        // Do not add addressing extension to message.
        } catch (UserNotFoundException e) {
            // Do not add addressing extension to message.
            router.route(message);
        } catch (NullPointerException e) {
            try {
                if (canProbePresence(getAddress(), recipientFullJID)) {
                    message.setTo(recipientFullJID);
                }
            } catch (UserNotFoundException e1) {
            // Do nothing
            }
            router.route(message);
        }
    }
}
Also used : PacketExtension(org.xmpp.packet.PacketExtension) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) AccessModel(org.jivesoftware.openfire.pubsub.models.AccessModel) Element(org.dom4j.Element) CollectionNode(org.jivesoftware.openfire.pubsub.CollectionNode) Node(org.jivesoftware.openfire.pubsub.Node) EntityCapabilities(org.jivesoftware.openfire.entitycaps.EntityCapabilities) ClientSession(org.jivesoftware.openfire.session.ClientSession) HashSet(java.util.HashSet)

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