use of org.jivesoftware.openfire.privacy.PrivacyList in project Openfire by igniterealtime.
the class OfflineMessageStrategy method storeOffline.
public void storeOffline(Message message) {
if (message != null) {
// Do nothing if the message was sent to the server itself, an anonymous user or a non-existent user
// Also ignore message carbons
JID recipientJID = message.getTo();
if (recipientJID == null || serverAddress.equals(recipientJID) || recipientJID.getNode() == null || message.getExtension("received", "urn:xmpp:carbons:2") != null || !UserManager.getInstance().isRegisteredUser(recipientJID, false)) {
return;
}
// Do not store messages if communication is blocked
PrivacyList list = PrivacyListManager.getInstance().getDefaultPrivacyList(recipientJID.getNode());
if (list != null && list.shouldBlockPacket(message)) {
if (message.getType() == Message.Type.error) {
Log.debug("Avoid generating an error in response to a stanza that itself is an error (to avoid the chance of entering an endless back-and-forth of exchanging errors). Suppress sending an {} error in response to: {}", PacketError.Condition.service_unavailable, message);
return;
}
Message result = message.createCopy();
result.setTo(message.getFrom());
result.setFrom(message.getTo());
result.setError(PacketError.Condition.service_unavailable);
XMPPServer.getInstance().getPacketRouter().route(result);
return;
}
// 8.5.2.2. No Available or Connected Resources
if (recipientJID.getResource() == null) {
if (message.getType() == Message.Type.headline || message.getType() == Message.Type.error) {
// For a message stanza of type "headline" or "error", the server MUST silently ignore the message.
return;
} else // // For a message stanza of type "groupchat", the server MUST return an error to the sender, which SHOULD be <service-unavailable/>.
if (message.getType() == Message.Type.groupchat) {
bounce(message);
return;
}
} else {
// or (b) return an error stanza to the sender, which SHOULD be <service-unavailable/>.
if (message.getType() == Message.Type.normal || message.getType() == Message.Type.groupchat || message.getType() == Message.Type.headline) {
// Depending on the OfflineMessageStragey, we may silently ignore or bounce
if (type == Type.bounce) {
bounce(message);
}
// Either bounce or silently ignore, never store such messages
return;
} else // For a message stanza of type "error", the server MUST silently ignore the stanza.
if (message.getType() == Message.Type.error) {
return;
}
}
switch(type) {
case bounce:
bounce(message);
break;
case store:
store(message);
break;
case store_and_bounce:
if (underQuota(message)) {
store(message);
} else {
Log.debug("Unable to store, as user is over storage quota. Bouncing message instead: " + message.toXML());
bounce(message);
}
break;
case store_and_drop:
if (underQuota(message)) {
store(message);
} else {
Log.debug("Unable to store, as user is over storage quota. Silently dropping message: " + message.toXML());
}
break;
case drop:
// Drop essentially means silently ignore/do nothing
break;
}
}
}
use of org.jivesoftware.openfire.privacy.PrivacyList in project Openfire by igniterealtime.
the class IQPrivacyHandler method deleteList.
private IQ deleteList(IQ packet, JID from, String listName) {
ClientSession currentSession;
IQ result = IQ.createResultIQ(packet);
Element childElement = packet.getChildElement().createCopy();
result.setChildElement(childElement);
// Get the list to delete
PrivacyList list = manager.getPrivacyList(from.getNode(), listName);
if (list == null) {
// List to delete was not found
result.setError(PacketError.Condition.item_not_found);
return result;
} else {
currentSession = sessionManager.getSession(from);
// Check if the list is being used by another session
for (ClientSession session : sessionManager.getSessions(from.getNode())) {
if (currentSession == session) {
// Ignore the active session for this checking
continue;
}
if (list.equals(session.getDefaultList()) || list.equals(session.getActiveList())) {
// List to delete is being used by another session so return a conflict error
result.setError(PacketError.Condition.conflict);
return result;
}
}
}
// Remove the list from the active session (if it was being used)
if (list.equals(currentSession.getDefaultList())) {
currentSession.setDefaultList(null);
}
if (list.equals(currentSession.getActiveList())) {
currentSession.setActiveList(null);
}
manager.deletePrivacyList(from.getNode(), listName);
return result;
}
use of org.jivesoftware.openfire.privacy.PrivacyList in project Openfire by igniterealtime.
the class IQBlockingHandler method removeFromBlockList.
/**
* Removes a collection of JIDs from the blocklist of the provided user.
*
* This method removes the JIDs to the default privacy list. When no default privacy list exists for this user, this
* method does nothing.
*
* @param user The owner of the blocklist to which JIDs are to be added (cannot be null).
* @param toUnblocks The JIDs to be removed (can be null, which results in a noop).
* @return The JIDs that are removed (never null, possibly empty).
*/
protected Set<JID> removeFromBlockList(User user, Collection<JID> toUnblocks) {
final Set<JID> result = new HashSet<>();
if (toUnblocks == null || toUnblocks.isEmpty()) {
return result;
}
Log.debug("Obtain the default privacy list for '{}'", user.getUsername());
PrivacyList defaultPrivacyList = PrivacyListManager.getInstance().getDefaultPrivacyList(user.getUsername());
if (defaultPrivacyList == null) {
return result;
}
Log.debug("Removing {} JIDs as blocked items from list '{}' (belonging to '{}')", toUnblocks.size(), defaultPrivacyList.getName(), user.getUsername());
final Element listElement = defaultPrivacyList.asElement();
final Set<Element> toRemove = new HashSet<>();
for (final Element element : listElement.elements("item")) {
final JID jid = new JID(element.attributeValue("value"));
if ("jid".equals(element.attributeValue("type")) && "deny".equals(element.attributeValue("action")) && toUnblocks.contains(jid)) {
toRemove.add(element);
result.add(jid);
}
}
if (!toRemove.isEmpty()) {
for (final Element remove : toRemove) {
listElement.remove(remove);
}
defaultPrivacyList.updateList(listElement);
PrivacyListProvider.getInstance().updatePrivacyList(user.getUsername(), defaultPrivacyList);
}
return result;
}
use of org.jivesoftware.openfire.privacy.PrivacyList in project Openfire by igniterealtime.
the class IQBlockingHandler method removeAllFromBlocklist.
/**
* Removes all JIDs from the blocklist of the provided user.
*
* This method removes the JIDs to the default privacy list. When no default privacy list exists for this user, this
* method does nothing.
*
* @param user The owner of the blocklist to which JIDs are to be added (cannot be null).
* @return The JIDs that are removed (never null, possibly empty).
*/
private Set<JID> removeAllFromBlocklist(User user) {
Log.debug("Obtain the default privacy list for '{}'", user.getUsername());
final Set<JID> result = new HashSet<>();
PrivacyList defaultPrivacyList = PrivacyListManager.getInstance().getDefaultPrivacyList(user.getUsername());
if (defaultPrivacyList == null) {
return result;
}
Log.debug("Removing all JIDs from blocklist '{}' (belonging to '{}')", defaultPrivacyList.getName(), user.getUsername());
final Element listElement = defaultPrivacyList.asElement();
final Set<Element> toRemove = new HashSet<>();
for (final Element element : listElement.elements("item")) {
if ("jid".equals(element.attributeValue("type")) && "deny".equals(element.attributeValue("action"))) {
toRemove.add(element);
result.add(new JID(element.attributeValue("value")));
}
}
if (!toRemove.isEmpty()) {
for (final Element remove : toRemove) {
listElement.remove(remove);
}
defaultPrivacyList.updateList(listElement);
PrivacyListProvider.getInstance().updatePrivacyList(user.getUsername(), defaultPrivacyList);
}
return result;
}
use of org.jivesoftware.openfire.privacy.PrivacyList in project Openfire by igniterealtime.
the class Roster method broadcastPresence.
/**
* <p>Broadcast the presence update to all subscribers of the roster.</p>
* <p>Any presence change typically results in a broadcast to the roster members.</p>
*
* @param packet The presence packet to broadcast
*/
public void broadcastPresence(Presence packet) {
final RoutingTable routingTable = XMPPServer.getInstance().getRoutingTable();
if (routingTable == null) {
return;
}
// Get the privacy list of this user
PrivacyList list = null;
JID from = packet.getFrom();
if (from != null) {
// Try to use the active list of the session. If none was found then try to use
// the default privacy list of the session
ClientSession session = SessionManager.getInstance().getSession(from);
if (session != null) {
list = session.getActiveList();
list = list == null ? session.getDefaultList() : list;
}
}
if (list == null) {
// No privacy list was found (based on the session) so check if there is a default list
list = PrivacyListManager.getInstance().getDefaultPrivacyList(username);
}
// Broadcast presence to subscribed entities
for (RosterItem item : rosterItems.values()) {
if (item.getSubStatus() == RosterItem.SUB_BOTH || item.getSubStatus() == RosterItem.SUB_FROM) {
packet.setTo(item.getJid());
if (list != null && list.shouldBlockPacket(packet)) {
// Outgoing presence notifications are blocked for this contact
continue;
}
JID searchNode = new JID(item.getJid().getNode(), item.getJid().getDomain(), null, true);
final List<JID> routingTableRoutes = routingTable.getRoutes(searchNode, null);
for (JID jid : routingTableRoutes) {
try {
routingTable.routePacket(jid, packet, false);
} catch (Exception e) {
// Theoretically only happens if session has been closed.
Log.debug(e.getMessage(), e);
}
}
}
}
// Broadcast presence to shared contacts whose subscription status is FROM
final Set<String> implicitFroms = implicitFrom.keySet();
for (String contact : implicitFroms) {
if (contact.contains("@")) {
String node = contact.substring(0, contact.lastIndexOf("@"));
String domain = contact.substring(contact.lastIndexOf("@") + 1);
node = JID.escapeNode(node);
contact = new JID(node, domain, null).toBareJID();
}
packet.setTo(contact);
if (list != null && list.shouldBlockPacket(packet)) {
// Outgoing presence notifications are blocked for this contact
continue;
}
final List<JID> routingTableRoutes = routingTable.getRoutes(new JID(contact), null);
for (JID jid : routingTableRoutes) {
try {
routingTable.routePacket(jid, packet, false);
} catch (Exception e) {
// Theoretically only happens if session has been closed.
Log.debug(e.getMessage(), e);
}
}
}
if (from != null) {
// Broadcast presence to all resources of the user.
SessionManager.getInstance().broadcastPresenceToResources(from, packet);
}
}
Aggregations