Search in sources :

Example 16 with XMPPServer

use of org.jivesoftware.openfire.XMPPServer in project Openfire by igniterealtime.

the class ConversationManager method processMessage.

/**
	 * Processes an incoming message of a one-to-one chat. The message will mapped to a conversation and then queued for storage if archiving is
	 * turned on.
	 *
	 * @param sender
	 *            sender of the message.
	 * @param receiver
	 *            receiver of the message.
	 * @param body
	 *            body of the message.
	 * @param stanza
	 * 			  String encoded message stanza
	 * @param date
	 *            date when the message was sent.
	 */
void processMessage(JID sender, JID receiver, String body, String stanza, Date date) {
    String conversationKey = getConversationKey(sender, receiver);
    synchronized (conversationKey.intern()) {
        Conversation conversation = conversations.get(conversationKey);
        // Create a new conversation if necessary.
        if (conversation == null) {
            Collection<JID> participants = new ArrayList<JID>(2);
            participants.add(sender);
            participants.add(receiver);
            XMPPServer server = XMPPServer.getInstance();
            // Check to see if this is an external conversation; i.e. one of the participants
            // is on a different server. We can use XOR since we know that both JID's can't
            // be external.
            boolean external = isExternal(server, sender) ^ isExternal(server, receiver);
            // Make sure that the user joined the conversation before a message was received
            Date start = new Date(date.getTime() - 1);
            conversation = new Conversation(this, participants, external, start);
            conversations.put(conversationKey, conversation);
            // Notify listeners of the newly created conversation.
            for (ConversationListener listener : conversationListeners) {
                listener.conversationCreated(conversation);
            }
        } else // or max conversation time.
        if ((date.getTime() - conversation.getLastActivity().getTime() > idleTime) || (date.getTime() - conversation.getStartDate().getTime() > maxTime)) {
            removeConversation(conversationKey, conversation, conversation.getLastActivity());
            Collection<JID> participants = new ArrayList<JID>(2);
            participants.add(sender);
            participants.add(receiver);
            XMPPServer server = XMPPServer.getInstance();
            // Check to see if this is an external conversation; i.e. one of the participants
            // is on a different server. We can use XOR since we know that both JID's can't
            // be external.
            boolean external = isExternal(server, sender) ^ isExternal(server, receiver);
            // Make sure that the user joined the conversation before a message was received
            Date start = new Date(date.getTime() - 1);
            conversation = new Conversation(this, participants, external, start);
            conversations.put(conversationKey, conversation);
            // Notify listeners of the newly created conversation.
            for (ConversationListener listener : conversationListeners) {
                listener.conversationCreated(conversation);
            }
        }
        // Record the newly received message.
        conversation.messageReceived(sender, date);
        if (metadataArchivingEnabled) {
            conversationQueue.add(conversation);
        }
        if (messageArchivingEnabled) {
            if (body != null) {
                /* OF-677 - Workaround to prevent null messages being archived */
                messageQueue.add(new ArchivedMessage(conversation.getConversationID(), sender, receiver, date, body, stanza, false));
            }
        }
        // Notify listeners of the conversation update.
        for (ConversationListener listener : conversationListeners) {
            listener.conversationUpdated(conversation, date);
        }
    }
}
Also used : XMPPServer(org.jivesoftware.openfire.XMPPServer) JID(org.xmpp.packet.JID) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Collection(java.util.Collection) Date(java.util.Date)

Example 17 with XMPPServer

use of org.jivesoftware.openfire.XMPPServer in project Openfire by igniterealtime.

the class PresencePlugin method initializePlugin.

public void initializePlugin(PluginManager manager, File pluginDirectory) {
    pluginManager = manager;
    XMPPServer server = XMPPServer.getInstance();
    userManager = server.getUserManager();
    presenceManager = server.getPresenceManager();
    hostname = server.getServerInfo().getXMPPDomain();
    probedPresence = new ConcurrentHashMap<String, Presence>();
    componentJID = new JID(subdomain + "." + hostname);
    // Register new component
    componentManager = ComponentManagerFactory.getComponentManager();
    try {
        componentManager.addComponent(subdomain, this);
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    }
}
Also used : XMPPServer(org.jivesoftware.openfire.XMPPServer) JID(org.xmpp.packet.JID) Presence(org.xmpp.packet.Presence) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 18 with XMPPServer

use of org.jivesoftware.openfire.XMPPServer in project Openfire by igniterealtime.

the class TextPresenceProvider method sendInfo.

@Override
public void sendInfo(HttpServletRequest request, HttpServletResponse response, Presence presence) throws IOException {
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();
    if (presence == null) {
        // Recreate the unavailable presence with the last known status
        JID targetJID = new JID(request.getParameter("jid"));
        presence = new Presence(Presence.Type.unavailable);
        XMPPServer server = XMPPServer.getInstance();
        try {
            User user = server.getUserManager().getUser(targetJID.getNode());
            String status = server.getPresenceManager().getLastPresenceStatus(user);
            if (status != null) {
                presence.setStatus(status);
            } else {
                presence.setStatus(JiveGlobals.getProperty("plugin.presence.unavailable.status", "Unavailable"));
            }
        } catch (UserNotFoundException e) {
        }
        presence.setFrom(targetJID);
    }
    out.println(presence.getStatus());
    out.flush();
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) XMPPServer(org.jivesoftware.openfire.XMPPServer) User(org.jivesoftware.openfire.user.User) JID(org.xmpp.packet.JID) Presence(org.xmpp.packet.Presence) PrintWriter(java.io.PrintWriter)

Example 19 with XMPPServer

use of org.jivesoftware.openfire.XMPPServer in project Openfire by igniterealtime.

the class XMLPresenceProvider method sendInfo.

@Override
public void sendInfo(HttpServletRequest request, HttpServletResponse response, Presence presence) throws IOException {
    response.setContentType("text/xml");
    PrintWriter out = response.getWriter();
    if (presence == null) {
        // Recreate the unavailable presence with the last known status
        JID targetJID = new JID(request.getParameter("jid"));
        presence = new Presence(Presence.Type.unavailable);
        XMPPServer server = XMPPServer.getInstance();
        try {
            User user = server.getUserManager().getUser(targetJID.getNode());
            String status = server.getPresenceManager().getLastPresenceStatus(user);
            if (status != null) {
                presence.setStatus(status);
            } else {
                presence.setStatus(JiveGlobals.getProperty("plugin.presence.unavailable.status", "Unavailable"));
            }
        } catch (UserNotFoundException e) {
        }
        presence.setFrom(targetJID);
    }
    out.println(presence.toXML());
    out.flush();
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) XMPPServer(org.jivesoftware.openfire.XMPPServer) User(org.jivesoftware.openfire.user.User) JID(org.xmpp.packet.JID) Presence(org.xmpp.packet.Presence) PrintWriter(java.io.PrintWriter)

Example 20 with XMPPServer

use of org.jivesoftware.openfire.XMPPServer in project Openfire by igniterealtime.

the class LdapGroupProvider method processGroup.

private Group processGroup(LdapContext ctx, Attributes a) throws NamingException {
    XMPPServer server = XMPPServer.getInstance();
    String serverName = server.getServerInfo().getXMPPDomain();
    // Build `3 groups.
    // group 1: uid=
    // group 2: rest of the text until first comma
    // group 3: rest of the text
    Pattern pattern = Pattern.compile("(?i)(^" + manager.getUsernameField() + "=)([^,]+)(.+)");
    // We have to process Active Directory differently.
    boolean isAD = manager.getUsernameField().equals("sAMAccountName");
    String[] returningAttributes = isAD ? new String[] { "distinguishedName", manager.getUsernameField() } : new String[] { manager.getUsernameField() };
    SearchControls searchControls = new SearchControls();
    searchControls.setReturningAttributes(returningAttributes);
    // See if recursive searching is enabled. Otherwise, only search one level.
    if (manager.isSubTreeSearch()) {
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    } else {
        searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    }
    String name;
    String description;
    try {
        name = ((String) ((a.get(manager.getGroupNameField())).get()));
    } catch (Exception e) {
        name = "";
    }
    try {
        description = ((String) ((a.get(manager.getGroupDescriptionField())).get()));
    } catch (Exception e) {
        description = "";
    }
    Set<JID> members = new TreeSet<>();
    Attribute memberField = a.get(manager.getGroupMemberField());
    if (memberField != null) {
        NamingEnumeration ne = memberField.getAll();
        while (ne.hasMore()) {
            String username = (String) ne.next();
            // If not posix mode, each group member is stored as a full DN.
            if (!manager.isPosixMode()) {
                try {
                    // Try to find the username with a regex pattern match.
                    Matcher matcher = pattern.matcher(username);
                    if (matcher.matches() && matcher.groupCount() == 3) {
                        // The username is in the DN, no additional search needed
                        username = matcher.group(2);
                    } else // The regex pattern match failed. This will happen if the
                    // the member DN's don't use the standard username field. For
                    // example, Active Directory has a username field of
                    // sAMAccountName, but stores group members as "CN=...".
                    {
                        // Create an LDAP name with the full DN.
                        LdapName ldapName = new LdapName(username);
                        // Turn the LDAP name into something we can use in a
                        // search by stripping off the comma.
                        StringBuilder userFilter = new StringBuilder();
                        userFilter.append("(&(");
                        userFilter.append(ldapName.get(ldapName.size() - 1));
                        userFilter.append(')');
                        userFilter.append(MessageFormat.format(manager.getSearchFilter(), "*"));
                        userFilter.append(')');
                        NamingEnumeration usrAnswer = ctx.search("", userFilter.toString(), searchControls);
                        if (usrAnswer != null && usrAnswer.hasMoreElements()) {
                            SearchResult searchResult = null;
                            // Iterate through the entire set to find a matching distinguished name.
                            while (usrAnswer.hasMoreElements()) {
                                searchResult = (SearchResult) usrAnswer.nextElement();
                                Attributes attrs = searchResult.getAttributes();
                                if (isAD) {
                                    Attribute userdnAttr = attrs.get("distinguishedName");
                                    if (username.equals((String) userdnAttr.get())) {
                                        // Exact match found, use it.
                                        username = (String) attrs.get(manager.getUsernameField()).get();
                                        break;
                                    }
                                } else {
                                    // No iteration occurs here, which is probably a bug.
                                    username = (String) attrs.get(manager.getUsernameField()).get();
                                    break;
                                }
                            }
                        }
                        // Close the enumeration.
                        usrAnswer.close();
                    }
                } catch (Exception e) {
                    // TODO: A NPE is occuring here
                    Log.error(e.getMessage(), e);
                }
            }
            // it passes the filter.
            try {
                JID userJID;
                int position = username.indexOf("@" + serverName);
                // Create JID of local user if JID does not match a component's JID
                if (position == -1) {
                    // In order to lookup a username from the manager, the username
                    // must be a properly escaped JID node.
                    String escapedUsername = JID.escapeNode(username);
                    if (!escapedUsername.equals(username)) {
                        // Check if escaped username is valid
                        userManager.getUser(escapedUsername);
                    }
                    // No exception, so the user must exist. Add the user as a group
                    // member using the escaped username.
                    userJID = server.createJID(escapedUsername, null);
                } else {
                    // This is a JID of a component or node of a server's component
                    String node = username.substring(0, position);
                    String escapedUsername = JID.escapeNode(node);
                    userJID = new JID(escapedUsername + "@" + serverName);
                }
                members.add(userJID);
            } catch (UserNotFoundException e) {
                // So, we want to simply ignore the user as a group member.
                if (manager.isDebugEnabled()) {
                    Log.debug("LdapGroupProvider: User not found: " + username);
                }
            }
        }
        // Close the enumeration.
        ne.close();
    }
    if (manager.isDebugEnabled()) {
        Log.debug("LdapGroupProvider: Adding group \"" + name + "\" with " + members.size() + " members.");
    }
    Collection<JID> admins = Collections.emptyList();
    return new Group(name, description, members, admins);
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Pattern(java.util.regex.Pattern) Group(org.jivesoftware.openfire.group.Group) JID(org.xmpp.packet.JID) Attribute(javax.naming.directory.Attribute) Matcher(java.util.regex.Matcher) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) LdapName(javax.naming.ldap.LdapName) XMPPServer(org.jivesoftware.openfire.XMPPServer) TreeSet(java.util.TreeSet) SearchControls(javax.naming.directory.SearchControls)

Aggregations

XMPPServer (org.jivesoftware.openfire.XMPPServer)27 JID (org.xmpp.packet.JID)11 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)8 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)5 NamingException (javax.naming.NamingException)3 LdapName (javax.naming.ldap.LdapName)3 Group (org.jivesoftware.openfire.group.Group)3 User (org.jivesoftware.openfire.user.User)3 Presence (org.xmpp.packet.Presence)3 PrintWriter (java.io.PrintWriter)2 Locale (java.util.Locale)2 TreeSet (java.util.TreeSet)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 NamingEnumeration (javax.naming.NamingEnumeration)2 Attribute (javax.naming.directory.Attribute)2 Attributes (javax.naming.directory.Attributes)2 SearchControls (javax.naming.directory.SearchControls)2 SearchResult (javax.naming.directory.SearchResult)2 Plugin (org.jivesoftware.openfire.container.Plugin)2