mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-09-09 10:19:41 +02:00
Add Roster.addSubscribeListener()
Remove unnecessary 'Unsubscribe' case in Roster: It is not required to acknowledge these.
This commit is contained in:
parent
8db0403138
commit
2365c4c208
6 changed files with 296 additions and 33 deletions
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2015 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.roster;
|
||||
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.jxmpp.jid.Jid;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Provides empty implementations for {@link RosterListener}.
|
||||
*
|
||||
* @since 4.2
|
||||
*/
|
||||
public abstract class AbstractRosterListener implements RosterListener {
|
||||
|
||||
@Override
|
||||
public void entriesAdded(Collection<Jid> addresses) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void entriesUpdated(Collection<Jid> addresses) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void entriesDeleted(Collection<Jid> addresses) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void presenceChanged(Presence presence) {
|
||||
}
|
||||
}
|
|
@ -46,6 +46,7 @@ import org.jivesoftware.smack.SmackException.NotConnectedException;
|
|||
import org.jivesoftware.smack.SmackException.NotLoggedInException;
|
||||
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
||||
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
||||
import org.jivesoftware.smack.filter.PresenceTypeFilter;
|
||||
import org.jivesoftware.smack.filter.StanzaFilter;
|
||||
import org.jivesoftware.smack.filter.StanzaTypeFilter;
|
||||
import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
|
||||
|
@ -55,6 +56,7 @@ import org.jivesoftware.smack.packet.Stanza;
|
|||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.jivesoftware.smack.packet.XMPPError;
|
||||
import org.jivesoftware.smack.packet.XMPPError.Condition;
|
||||
import org.jivesoftware.smack.roster.SubscribeListener.SubscribeAnswer;
|
||||
import org.jivesoftware.smack.roster.packet.RosterPacket;
|
||||
import org.jivesoftware.smack.roster.packet.RosterVer;
|
||||
import org.jivesoftware.smack.roster.packet.RosterPacket.Item;
|
||||
|
@ -169,6 +171,8 @@ public final class Roster extends Manager {
|
|||
|
||||
private SubscriptionMode subscriptionMode = getDefaultSubscriptionMode();
|
||||
|
||||
private SubscribeListener subscribeListener;
|
||||
|
||||
/**
|
||||
* Returns the default subscription processing mode to use when a new Roster is created. The
|
||||
* subscription processing mode dictates what action Smack will take when subscription
|
||||
|
@ -208,6 +212,46 @@ public final class Roster extends Manager {
|
|||
// Listen for any presence packets.
|
||||
connection.addSyncStanzaListener(presencePacketListener, PRESENCE_PACKET_FILTER);
|
||||
|
||||
connection.addAsyncStanzaListener(new StanzaListener() {
|
||||
@Override
|
||||
public void processPacket(Stanza stanza) throws NotConnectedException,
|
||||
InterruptedException {
|
||||
Presence presence = (Presence) stanza;
|
||||
Jid from = presence.getFrom();
|
||||
SubscribeAnswer subscribeAnswer = null;
|
||||
switch (subscriptionMode) {
|
||||
case manual:
|
||||
final SubscribeListener subscribeListener = Roster.this.subscribeListener;
|
||||
if (subscribeListener == null) {
|
||||
return;
|
||||
}
|
||||
subscribeAnswer = subscribeListener.processSubscribe(from, presence);
|
||||
if (subscribeAnswer == null) {
|
||||
return;
|
||||
}
|
||||
break;
|
||||
case accept_all:
|
||||
// Accept all subscription requests.
|
||||
subscribeAnswer = SubscribeAnswer.Approve;
|
||||
break;
|
||||
case reject_all:
|
||||
// Reject all subscription requests.
|
||||
subscribeAnswer = SubscribeAnswer.Deny;
|
||||
break;
|
||||
}
|
||||
|
||||
Presence response;
|
||||
if (subscribeAnswer == SubscribeAnswer.Approve) {
|
||||
response = new Presence(Presence.Type.subscribed);
|
||||
}
|
||||
else {
|
||||
response = new Presence(Presence.Type.unsubscribed);
|
||||
}
|
||||
response.setTo(presence.getFrom());
|
||||
connection.sendStanza(response);
|
||||
}
|
||||
}, PresenceTypeFilter.SUBSCRIBE);
|
||||
|
||||
// Listen for connection events
|
||||
connection.addConnectionListener(new AbstractConnectionClosedListener() {
|
||||
|
||||
|
@ -530,6 +574,22 @@ public final class Roster extends Manager {
|
|||
return connection.hasFeature(SubscriptionPreApproval.ELEMENT, SubscriptionPreApproval.NAMESPACE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the subscribe listener, which is invoked on incoming subscription requests and if
|
||||
* {@link SubscriptionMode} is set to {@link SubscriptionMode#manual}. If
|
||||
* <code>subscribeListener</code> is not <code>null</code>, then this also sets subscription
|
||||
* mode to {@link SubscriptionMode#manual}.
|
||||
*
|
||||
* @param subscribeListener the subscribe listener to set.
|
||||
* @since 4.2
|
||||
*/
|
||||
public void setSubscribeListener(SubscribeListener subscribeListener) {
|
||||
if (subscribeListener != null) {
|
||||
setSubscriptionMode(SubscriptionMode.manual);
|
||||
}
|
||||
this.subscribeListener = subscribeListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a roster entry from the roster. The roster entry will also be removed from the
|
||||
* unfiled entries or from any roster group where it could belong and will no longer be part
|
||||
|
@ -1230,7 +1290,6 @@ public final class Roster extends Manager {
|
|||
|
||||
@Override
|
||||
public void processPacket(Stanza packet) throws NotConnectedException, InterruptedException {
|
||||
final XMPPConnection connection = connection();
|
||||
Presence presence = (Presence) packet;
|
||||
Jid from = presence.getFrom();
|
||||
Resourcepart fromResource = Resourcepart.EMPTY;
|
||||
|
@ -1242,7 +1301,6 @@ public final class Roster extends Manager {
|
|||
}
|
||||
Jid key = getMapKey(from);
|
||||
Map<Resourcepart, Presence> userPresences;
|
||||
Presence response = null;
|
||||
|
||||
// If an "available" presence, add it to the presence map. Each presence
|
||||
// map will hold for a particular user a map with the presence
|
||||
|
@ -1282,37 +1340,6 @@ public final class Roster extends Manager {
|
|||
fireRosterPresenceEvent(presence);
|
||||
}
|
||||
break;
|
||||
case subscribe:
|
||||
switch (subscriptionMode) {
|
||||
case accept_all:
|
||||
// Accept all subscription requests.
|
||||
response = new Presence(Presence.Type.subscribed);
|
||||
break;
|
||||
case reject_all:
|
||||
// Reject all subscription requests.
|
||||
response = new Presence(Presence.Type.unsubscribed);
|
||||
break;
|
||||
case manual:
|
||||
default:
|
||||
// Otherwise, in manual mode so ignore.
|
||||
break;
|
||||
}
|
||||
if (response != null) {
|
||||
response.setTo(presence.getFrom());
|
||||
connection.sendStanza(response);
|
||||
}
|
||||
break;
|
||||
case unsubscribe:
|
||||
if (subscriptionMode != SubscriptionMode.manual) {
|
||||
// Acknowledge and accept unsubscription notification so that the
|
||||
// server will stop sending notifications saying that the contact
|
||||
// has unsubscribed to our presence.
|
||||
response = new Presence(Presence.Type.unsubscribed);
|
||||
response.setTo(presence.getFrom());
|
||||
connection.sendStanza(response);
|
||||
}
|
||||
// Otherwise, in manual mode so ignore.
|
||||
break;
|
||||
// Error presence packets from a bare JID mean we invalidate all existing
|
||||
// presence info for the user.
|
||||
case error:
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2015 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.roster;
|
||||
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.jxmpp.jid.Jid;
|
||||
|
||||
|
||||
/**
|
||||
* Handle incoming requests to subscribe to our presence.
|
||||
*
|
||||
*/
|
||||
public interface SubscribeListener {
|
||||
|
||||
public enum SubscribeAnswer {
|
||||
Approve,
|
||||
Deny,
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle incoming presence subscription requests.
|
||||
*
|
||||
* @param from the JID requesting the subscription.
|
||||
* @param subscribeRequest the presence stanza used for the request.
|
||||
* @return a answer to the request, or <code>null</code>
|
||||
*/
|
||||
public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest);
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue