Search in sources :

Example 81 with Message

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

the class MessageRouter method sendMessageToAdmins.

/**
     * Forwards the received message to the list of users defined in the property
     * <b>xmpp.forward.admins</b>. The property may include bare JIDs or just usernames separated
     * by commas or white spaces. When using bare JIDs the target user may belong to a remote
     * server.<p>
     *
     * If the property <b>xmpp.forward.admins</b> was not defined then the message will be sent
     * to all the users allowed to enter the admin console.
     *
     * @param packet the message to forward.
     */
private void sendMessageToAdmins(Message packet) {
    String jids = JiveGlobals.getProperty("xmpp.forward.admins");
    if (jids != null && jids.trim().length() > 0) {
        // Forward the message to the users specified in the "xmpp.forward.admins" property
        StringTokenizer tokenizer = new StringTokenizer(jids, ", ");
        while (tokenizer.hasMoreTokens()) {
            String username = tokenizer.nextToken();
            Message forward = packet.createCopy();
            if (username.contains("@")) {
                // Use the specified bare JID address as the target address
                forward.setTo(username);
            } else {
                forward.setTo(username + "@" + serverName);
            }
            route(forward);
        }
    } else {
        // Forward the message to the users allowed to log into the admin console
        for (JID jid : XMPPServer.getInstance().getAdmins()) {
            Message forward = packet.createCopy();
            forward.setTo(jid);
            route(forward);
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) Message(org.xmpp.packet.Message) JID(org.xmpp.packet.JID)

Example 82 with Message

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

the class SessionManager method changePriority.

/**
     * Change the priority of a session, that was already available, associated with the sender.
     *
     * @param session   The session whose presence priority has been modified
     * @param oldPriority The old priority for the session
     */
public void changePriority(LocalClientSession session, int oldPriority) {
    if (session.getAuthToken().isAnonymous()) {
        // Do nothing if the session belongs to an anonymous user
        return;
    }
    int newPriority = session.getPresence().getPriority();
    if (newPriority < 0 || oldPriority >= 0) {
        // Do nothing if new presence priority is not positive and old presence negative
        return;
    }
    // Check presence's priority of other available resources
    JID searchJID = session.getAddress().asBareJID();
    for (JID address : routingTable.getRoutes(searchJID, null)) {
        if (address.equals(session.getAddress())) {
            continue;
        }
        ClientSession otherSession = routingTable.getClientRoute(address);
        if (otherSession.getPresence().getPriority() >= 0) {
            return;
        }
    }
    // User sessions had negative presence before this change so deliver messages
    if (session.canFloodOfflineMessages()) {
        OfflineMessageStore messageStore = server.getOfflineMessageStore();
        Collection<OfflineMessage> messages = messageStore.getMessages(session.getAuthToken().getUsername(), true);
        for (Message message : messages) {
            session.process(message);
        }
    }
}
Also used : JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) ClientSession(org.jivesoftware.openfire.session.ClientSession)

Example 83 with Message

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

the class UpdateManager method processServerUpdateResponse.

private void processServerUpdateResponse(String response, boolean notificationsEnabled) throws DocumentException {
    // Reset last known update information
    serverUpdate = null;
    SAXReader xmlReader = new SAXReader();
    xmlReader.setEncoding("UTF-8");
    Element xmlResponse = xmlReader.read(new StringReader(response)).getRootElement();
    // Parse response and keep info as Update objects
    Element openfire = xmlResponse.element("openfire");
    if (openfire != null) {
        // A new version of openfire was found
        Version latestVersion = new Version(openfire.attributeValue("latest"));
        if (latestVersion.isNewerThan(XMPPServer.getInstance().getServerInfo().getVersion())) {
            String changelog = openfire.attributeValue("changelog");
            String url = openfire.attributeValue("url");
            // Keep information about the available server update
            serverUpdate = new Update("Openfire", latestVersion.getVersionString(), changelog, url);
        }
    }
    // Check if we need to send notifications to admins
    if (notificationsEnabled && isNotificationEnabled() && serverUpdate != null) {
        Collection<JID> admins = XMPPServer.getInstance().getAdmins();
        Message notification = new Message();
        notification.setFrom(serverName);
        notification.setBody(getNotificationMessage() + " " + serverUpdate.getComponentName() + " " + serverUpdate.getLatestVersion());
        for (JID jid : admins) {
            notification.setTo(jid);
            router.route(notification);
        }
    }
    // Save response in a file for later retrieval
    saveLatestServerInfo();
}
Also used : JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) Version(org.jivesoftware.util.Version) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader)

Example 84 with Message

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

the class CallbackOnOffline method interceptPacket.

public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException {
    if (processed && incoming && packet instanceof Message && packet.getTo() != null) {
        Message msg = (Message) packet;
        if (msg.getType() != Message.Type.chat) {
            return;
        }
        try {
            JID to = packet.getTo();
            User userTo = userManager.getUser(to.getNode());
            boolean available = presenceManager.isAvailable(userTo);
            if (debug) {
                Log.debug("intercepted message from {} to {}, recipient is available {}", packet.getFrom().toBareJID(), to.toBareJID(), available);
            }
            if (!available) {
                JID from = packet.getFrom();
                WebTarget target = client.target(url).queryParam("token", token).queryParam("from", from.toBareJID()).queryParam("to", to.toBareJID());
                if (debug) {
                    Log.debug("sending request to url='{}'", target);
                }
                Future<Response> responseFuture = target.request().async().get();
                if (debug) {
                    try {
                        Response response = responseFuture.get();
                        Log.debug("got response status url='{}' status='{}'", target, response.getStatus());
                    } catch (Exception e) {
                        Log.debug("can't get response status url='{}'", target, e);
                    }
                }
            }
        } catch (UserNotFoundException e) {
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) User(org.jivesoftware.openfire.user.User) Message(org.xmpp.packet.Message) JID(org.xmpp.packet.JID) WebTarget(javax.ws.rs.client.WebTarget) PacketRejectedException(org.jivesoftware.openfire.interceptor.PacketRejectedException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 85 with Message

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

the class emailOnAway method interceptPacket.

public void interceptPacket(Packet packet, Session session, boolean read, boolean processed) throws PacketRejectedException {
    String emailTo = null;
    String emailFrom = null;
    if ((!processed) && (!read) && (packet instanceof Message) && (packet.getTo() != null)) {
        Message msg = (Message) packet;
        if (msg.getType() == Message.Type.chat) {
            try {
                User userTo = userManager.getUser(packet.getTo().getNode());
                if (presenceManager.getPresence(userTo).toString().toLowerCase().indexOf("away") != -1) {
                    // Status isn't away
                    if (msg.getBody() != null) {
                        // Build email/sms
                        // The to email address
                        emailTo = vcardManager.getVCardProperty(userTo.getUsername(), "EMAIL");
                        if (emailTo == null || emailTo.length() == 0) {
                            emailTo = vcardManager.getVCardProperty(userTo.getUsername(), "EMAIL:USERID");
                            if (emailTo == null || emailTo.length() == 0) {
                                emailTo = userTo.getEmail();
                                if (emailTo == null || emailTo.length() == 0) {
                                    emailTo = packet.getTo().getNode() + "@" + packet.getTo().getDomain();
                                }
                            }
                        }
                        // The From email address
                        User userFrom = userManager.getUser(packet.getFrom().getNode());
                        emailFrom = vcardManager.getVCardProperty(userFrom.getUsername(), "EMAIL");
                        if (emailFrom == null || emailFrom.length() == 0) {
                            emailFrom = vcardManager.getVCardProperty(userFrom.getUsername(), "EMAIL:USERID");
                            if (emailFrom == null || emailFrom.length() == 0) {
                                emailFrom = userFrom.getEmail();
                                if (emailFrom == null || emailFrom.length() == 0) {
                                    emailFrom = packet.getFrom().getNode() + "@" + packet.getFrom().getDomain();
                                }
                            }
                        }
                        //			    System.err.println(vcardManager.getVCardProperty(userTo.getUsername(), "EMAIL:USERID"));
                        // Send email/sms
                        // if this is an sms... modify the recipient address
                        emailService.sendMessage(userTo.getName(), emailTo, userFrom.getName(), emailFrom, "IM", msg.getBody(), null);
                        // Notify the sender that this went to email/sms
                        messageRouter.route(createServerMessage(packet.getFrom().getNode() + "@" + packet.getFrom().getDomain(), packet.getTo().getNode() + "@" + packet.getTo().getDomain(), emailTo));
                    }
                }
            } catch (UserNotFoundException e) {
            }
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) User(org.jivesoftware.openfire.user.User) 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