Commit 2f5ecc4e authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Include last status message of the unavailable presence when answering a presence probe. JM-183


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1077 b35dd754-fafc-0310-a699-88a17e54d16e
parent f20724ec
......@@ -133,4 +133,25 @@ public interface PresenceManager {
* @param probee The XMPPAddress whos presence we would like sent have have probed
*/
public void probePresence(JID prober, JID probee);
/**
* Saves the last unavailable presence of the user so that future presence probes may answer
* this presence which may contain valuable information. If a last unavailable presence does
* not exist for a contact then no unavailable presence will be sent to the owner of the
* contact.
*
* @param username the name of the user whose last presence is going to be saved to the
* database.
* @param presence the last unavailable presence to save to the database for this user.
*/
public void saveLastUnavailablePresence(String username, Presence presence);
/**
* Deletes the last unavailable presence of a user. This may be necessary if the user has
* become available.
*
* @param username the name of the user whose last presence is going to be delete from the
* database.
*/
public void deleteLastUnavailablePresence(String username);
}
\ No newline at end of file
......@@ -96,6 +96,12 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
session.setInitialized(true);
}
}
// Delete the last unavailable presence of this user since the user is now
// available. Only perform this operation if this is an available presence sent to
// THE SERVER and the presence belongs to a local user.
if (presence.getTo() == null && localServer.isLocal(presence.getFrom())) {
presenceManager.deleteLastUnavailablePresence(presence.getFrom().getNode());
}
}
else if (Presence.Type.unavailable == type) {
broadcastUpdate(presence.createCopy());
......@@ -103,6 +109,15 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
if (session != null) {
session.setPresence(presence);
}
// Save the last unavailable presence of this user if the presence contains any
// child element such as <status>. Only perform this operation if this is an
// unavailable presence sent to THE SERVER and the presence belongs to a local user.
if (presence.getTo() == null && localServer.isLocal(presence.getFrom())) {
if (!presence.getElement().elements().isEmpty()) {
presenceManager.saveLastUnavailablePresence(presence.getFrom().getNode(),
presence);
}
}
}
else {
presence = presence.createCopy();
......@@ -373,8 +388,12 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
Map<ChannelHandler, Set<String>> map = directedPresences.get(update.getFrom().toString());
if (map != null) {
// Iterate over all the entities that the user sent a directed presence
for (ChannelHandler handler : map.keySet()) {
for (String jid : map.get(handler)) {
for (ChannelHandler handler : new HashSet<ChannelHandler>(map.keySet())) {
Set<String> jids = map.get(handler);
if (jids == null) {
continue;
}
for (String jid : jids) {
Presence presence = update.createCopy();
presence.setTo(new JID(jid));
try {
......
......@@ -21,6 +21,8 @@ import org.jivesoftware.util.Log;
import org.xmpp.packet.JID;
import org.xmpp.packet.Presence;
import org.xmpp.component.Component;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
......@@ -32,6 +34,8 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class PresenceManagerImpl extends BasicModule implements PresenceManager {
private static final String lastPresenceProp = "lastUnavailablePresence";
private Map<String, Presence> onlineGuests;
private Map<String, Presence> onlineUsers;
......@@ -281,15 +285,46 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
if (probee.getNode() != null && !"".equals(probee.getNode())) {
Collection<ClientSession> sessions =
sessionManager.getSessions(probee.getNode());
for (ClientSession session : sessions) {
Presence presencePacket = session.getPresence().createCopy();
presencePacket.setFrom(session.getAddress());
presencePacket.setTo(prober);
if (sessions.isEmpty()) {
// If the probee is not online then try to retrieve his last unavailable
// presence which may contain particular information and send it to the
// prober
try {
deliverer.deliver(presencePacket);
User probeeUser = UserManager.getInstance().getUser(probee.getNode());
String presenceXML = probeeUser.getProperty(lastPresenceProp);
if (presenceXML != null) {
try {
// Parse the element
Document element = DocumentHelper.parseText(presenceXML);
// Create the presence from the parsed element
Presence presencePacket = new Presence(element.getRootElement());
presencePacket.setFrom(probee.toBareJID());
presencePacket.setTo(prober);
// Send the presence to the prober
deliverer.deliver(presencePacket);
}
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
}
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
catch (UserNotFoundException e) {
}
}
else {
// The contact is online so send to the prober all the resources where the
// probee is connected
for (ClientSession session : sessions) {
Presence presencePacket = session.getPresence().createCopy();
presencePacket.setFrom(session.getAddress());
presencePacket.setTo(prober);
try {
deliverer.deliver(presencePacket);
}
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
}
}
......@@ -322,6 +357,30 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
}
}
public void deleteLastUnavailablePresence(String username) {
if (username == null) {
return;
}
try {
User probeeUser = UserManager.getInstance().getUser(username);
probeeUser.deleteProperty(lastPresenceProp);
}
catch (UserNotFoundException e) {
}
}
public void saveLastUnavailablePresence(String username, Presence presence) {
if (username == null) {
return;
}
try {
User probeeUser = UserManager.getInstance().getUser(username);
probeeUser.setProperty(lastPresenceProp, presence.toXML());
}
catch (UserNotFoundException e) {
}
}
// #####################################################################
// Module management
// #####################################################################
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment