Search in sources :

Example 31 with Message

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

the class SparkManager method notifyDiscoInfoChanged.

/**
     * Notify all users who have requested disco information from this component that settings have been changed.
     * Clients should perform a new service discovery to see what has changed.
     */
public void notifyDiscoInfoChanged() {
    final Message message = new Message();
    message.setFrom(serviceName + "." + componentManager.getServerName());
    Element child = message.addChildElement("event", "http://jabber.org/protocol/disco#info");
    buildFeatureSet(child);
    sessionManager.broadcast(message);
}
Also used : Message(org.xmpp.packet.Message) Element(org.dom4j.Element)

Example 32 with Message

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

the class SparkManager method closeSession.

/**
     * Sends an unsupported version error and name of client the user attempted to connect with.
     *
     * @param session    the users current session.
     * @param clientName the name of the client they were connecting with.
     */
private void closeSession(final Session session, String clientName) {
    // Increase the number of logins not allowed by 1.
    disconnects.incrementAndGet();
    Log.debug("Closed connection to client attempting to connect from " + clientName);
    // Send message information user.
    final Message message = new Message();
    message.setFrom(serviceName + "." + componentManager.getServerName());
    message.setTo(session.getAddress());
    message.setBody("You are using an invalid client, and therefore will be disconnected. " + "Please ask your system administrator for client choices.");
    // Send Message
    sendPacket(message);
    // Disconnect user after 5 seconds.
    taskEngine.schedule(new TimerTask() {

        @Override
        public void run() {
            // Include the not-authorized error in the response
            StreamError error = new StreamError(StreamError.Condition.policy_violation);
            session.deliverRawText(error.toXML());
            // Close the underlying connection
            session.close();
        }
    }, 5000);
}
Also used : StreamError(org.xmpp.packet.StreamError) Message(org.xmpp.packet.Message) TimerTask(java.util.TimerTask)

Example 33 with Message

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

the class BroadcastPlugin method processPacket.

public void processPacket(Packet packet) {
    boolean canProceed = false;
    Group group = null;
    String toNode = packet.getTo().getNode();
    // Check if user is allowed to send packet to this service[+group]
    boolean targetAll = "all".equals(toNode);
    if (targetAll) {
        // See if the user is allowed to send the packet.
        JID address = new JID(packet.getFrom().toBareJID());
        if (allowedUsers.isEmpty() || allowedUsers.contains(address)) {
            canProceed = true;
        }
    } else {
        try {
            if (toNode != null) {
                group = groupManager.getGroup(toNode);
                boolean isGroupUser = group.isUser(packet.getFrom()) || group.isUser(new JID(packet.getFrom().toBareJID()));
                if (disableGroupPermissions || (groupMembersAllowed && isGroupUser) || allowedUsers.contains(new JID(packet.getFrom().toBareJID()))) {
                    canProceed = true;
                }
            }
        } catch (GroupNotFoundException e) {
        // Ignore.
        }
    }
    if (packet instanceof Message) {
        // Respond to incoming messages
        Message message = (Message) packet;
        processMessage(message, targetAll, group, canProceed);
    } else if (packet instanceof Presence) {
        // Respond to presence subscription request or presence probe
        Presence presence = (Presence) packet;
        processPresence(canProceed, presence);
    } else if (packet instanceof IQ) {
        // Handle disco packets
        IQ iq = (IQ) packet;
        // Ignore IQs of type ERROR or RESULT
        if (IQ.Type.error == iq.getType() || IQ.Type.result == iq.getType()) {
            return;
        }
        processIQ(iq, targetAll, group, canProceed);
    }
}
Also used : Group(org.jivesoftware.openfire.group.Group) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) IQ(org.xmpp.packet.IQ) Presence(org.xmpp.packet.Presence) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException)

Example 34 with Message

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

the class BroadcastPlugin method processMessage.

private void processMessage(Message message, boolean targetAll, Group group, boolean canProceed) {
    // Check to see if trying to broadcast to all connected users.
    if (targetAll) {
        if (!canProceed) {
            Message error = new Message();
            if (message.getID() != null) {
                error.setID(message.getID());
            }
            error.setError(PacketError.Condition.not_allowed);
            error.setTo(message.getFrom());
            error.setFrom(message.getTo());
            error.setSubject("Error sending broadcast message");
            error.setBody("Not allowed to send a broadcast message to " + message.getTo());
            try {
                componentManager.sendPacket(this, error);
            } catch (Exception e) {
                Log.error(e.getMessage(), e);
            }
            return;
        }
        if ((messagePrefix != null) && (message.getBody() != null)) {
            message.setBody(messagePrefix + " " + message.getBody());
        }
        if (all2ofline == false) {
            // send to online users
            sessionManager.broadcast(message);
        } else {
            // send to all users
            Collection<User> users = userManager.getUsers();
            String xmppdomain = "@" + JiveGlobals.getProperty("xmpp.domain");
            for (User u : users) {
                Message newMessage = message.createCopy();
                newMessage.setTo(u.getUsername() + xmppdomain);
                try {
                    componentManager.sendPacket(this, newMessage);
                } catch (Exception e) {
                    Log.error(e.getMessage(), e);
                }
            }
        }
    } else // See if the name is a group.
    {
        if (group == null) {
            // The address is not recognized so send an error message back.
            Message error = new Message();
            if (message.getID() != null) {
                error.setID(message.getID());
            }
            error.setTo(message.getFrom());
            error.setFrom(message.getTo());
            error.setError(PacketError.Condition.not_allowed);
            error.setSubject("Error sending broadcast message");
            error.setBody("Address not valid: " + message.getTo());
            try {
                componentManager.sendPacket(this, error);
            } catch (Exception e) {
                Log.error(e.getMessage(), e);
            }
        } else if (canProceed) {
            // the message when they come back online
            if ((messagePrefix != null) && (message.getBody() != null)) {
                message.setBody(messagePrefix + " " + message.getBody());
            }
            for (JID userJID : group.getMembers()) {
                Message newMessage = message.createCopy();
                newMessage.setTo(userJID);
                try {
                    componentManager.sendPacket(this, newMessage);
                } catch (Exception e) {
                    Log.error(e.getMessage(), e);
                }
            }
        } else {
            // Otherwise, the address is recognized so send an error message back.
            Message error = new Message();
            if (message.getID() != null) {
                error.setID(message.getID());
            }
            error.setTo(message.getFrom());
            error.setFrom(message.getTo());
            error.setError(PacketError.Condition.not_allowed);
            error.setSubject("Error sending broadcast message");
            error.setBody("Not allowed to send a broadcast message to " + message.getTo());
            try {
                componentManager.sendPacket(this, error);
            } catch (Exception e) {
                Log.error(e.getMessage(), e);
            }
        }
    }
}
Also used : User(org.jivesoftware.openfire.user.User) Message(org.xmpp.packet.Message) JID(org.xmpp.packet.JID) ComponentException(org.xmpp.component.ComponentException) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException)

Example 35 with Message

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

the class emailOnAway method createServerMessage.

private Message createServerMessage(String to, String from, String emailTo) {
    Message message = new Message();
    message.setTo(to);
    message.setFrom(from);
    message.setSubject("I'm away");
    if (JiveGlobals.getBooleanProperty("plugin.emailonaway.showemail", true)) {
        message.setBody("I'm currently away. Your message has been forwarded to my email address  (" + emailTo + ").");
    } else {
        message.setBody("I'm currently away. Your message has been forwarded to my email address.");
    }
    return message;
}
Also used : Message(org.xmpp.packet.Message)

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