From e68209dd3d390955fa07d6937fd3c2fe36ce8654 Mon Sep 17 00:00:00 2001 From: Matt Tucker Date: Tue, 17 Jun 2003 17:20:30 +0000 Subject: [PATCH] Support more subscription modes. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@1959 b35dd754-fafc-0310-a699-88a17e54d16e --- source/org/jivesoftware/smack/Roster.java | 73 +++++++++++++++++++++-- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/source/org/jivesoftware/smack/Roster.java b/source/org/jivesoftware/smack/Roster.java index a6e4e3f62..5a4c85bd2 100644 --- a/source/org/jivesoftware/smack/Roster.java +++ b/source/org/jivesoftware/smack/Roster.java @@ -62,6 +62,12 @@ import java.util.*; * Represents a user's roster, which is the collection of users a person receives * presence updates for. Roster items are categorized into groups for easier management.

* + * Others users may attempt to subscribe to this user using a subscription request. Three + * modes are supported for handling these requests:

+ * * All presence subscription requests are automatically approved to this client * are automatically approved. This logic will be updated in the future to allow for * pluggable behavior. @@ -71,6 +77,25 @@ import java.util.*; */ public class Roster { + /** + * Automatically accept all subscription requests. This is the default mode + * and is suitable for simple client. More complex client will likely wish to + * handle subscription requests manually. + */ + public static final int SUBCRIPTION_ACCEPT_ALL = 0; + + /** + * Automatically reject all subscription requests. + */ + public static final int SUBCRIPTION_REJECT_ALL = 1; + + /** + * Subscription requests are ignored, which means they must be manually + * processed by registering a listener for presence packets and then looking + * for any presence requests that have the type Presence.Type.SUBSCRIBE. + */ + public static final int SUBSCRIPTION_MANUAL = 2; + private XMPPConnection connection; private Map groups; private List unfiledEntries; @@ -80,6 +105,8 @@ public class Roster { // has been recieved and processed. boolean rosterInitialized = false; + private int subscriptionMode = SUBCRIPTION_ACCEPT_ALL; + /** * Creates a new roster. * @@ -99,6 +126,32 @@ public class Roster { connection.addPacketListener(new PresencePacketListener(), presenceFilter); } + /** + * Returns the subscription processing mode, which dictates what action + * Smack will take when subscription requests from other users are made. + * + * @return the subscription mode. + */ + public int getSubscriptionMode() { + return subscriptionMode; + } + + /** + * Sets the subscription processing mode, which dictates what action + * Smack will take when subscription requests from other users are made. + * + * @param subscriptionMode the subscription mode. + */ + public void setSubscriptionMode(int subscriptionMode) { + if (subscriptionMode != SUBCRIPTION_ACCEPT_ALL && + subscriptionMode != SUBCRIPTION_REJECT_ALL && + subscriptionMode != SUBSCRIPTION_MANUAL) + { + throw new IllegalArgumentException("Invalid mode."); + } + this.subscriptionMode = subscriptionMode; + } + /** * Reloads the entire roster from the server. This is an asynchronous operation, * which means the method will return immediately, and the roster will be @@ -285,10 +338,19 @@ public class Roster { } else if (presence.getType() == Presence.Type.SUBSCRIBE) { - // Accept all subscription requests. - Presence response = new Presence(Presence.Type.SUBSCRIBED); - response.setTo(presence.getFrom()); - connection.sendPacket(response); + if (subscriptionMode == SUBCRIPTION_ACCEPT_ALL) { + // Accept all subscription requests. + Presence response = new Presence(Presence.Type.SUBSCRIBED); + response.setTo(presence.getFrom()); + connection.sendPacket(response); + } + else if (subscriptionMode == SUBCRIPTION_REJECT_ALL) { + // Reject all subscription requests. + Presence response = new Presence(Presence.Type.UNSUBSCRIBED); + response.setTo(presence.getFrom()); + connection.sendPacket(response); + } + // Otherwise, in manual mode so ignore. } } } @@ -303,8 +365,7 @@ public class Roster { for (Iterator i=rosterPacket.getRosterItems(); i.hasNext(); ) { RosterPacket.Item item = (RosterPacket.Item)i.next(); if (item.getItemType() == RosterPacket.ItemType.TO || - item.getItemType() == RosterPacket.ItemType.BOTH || - item.getItemType() == RosterPacket.ItemType.PENDING) + item.getItemType() == RosterPacket.ItemType.BOTH) { }