Skip to content

Commit

Permalink
Add support for IoT Friend approvals
Browse files Browse the repository at this point in the history
This adds supports for an experimental protocol flow where a pending
friend request's decission is later on deliverd to the requestor after
the owner made its decission.
  • Loading branch information
Flowdalic committed Oct 31, 2016
1 parent 5a2326a commit 1d3c48e
Show file tree
Hide file tree
Showing 11 changed files with 404 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
*
* Copyright 2016 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.smackx.iot.provisioning;

import org.jivesoftware.smack.packet.Presence;
import org.jxmpp.jid.BareJid;

public interface BecameFriendListener {

void becameFriend(BareJid jid, Presence presence);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -41,6 +43,7 @@
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.roster.AbstractPresenceEventListener;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.roster.SubscribeListener;
Expand All @@ -50,6 +53,7 @@
import org.jivesoftware.smackx.iot.provisioning.element.ClearCache;
import org.jivesoftware.smackx.iot.provisioning.element.ClearCacheResponse;
import org.jivesoftware.smackx.iot.provisioning.element.Constants;
import org.jivesoftware.smackx.iot.provisioning.element.Friend;
import org.jivesoftware.smackx.iot.provisioning.element.IoTIsFriend;
import org.jivesoftware.smackx.iot.provisioning.element.IoTIsFriendResponse;
import org.jivesoftware.smackx.iot.provisioning.element.Unfriend;
Expand All @@ -68,6 +72,8 @@ public final class IoTProvisioningManager extends Manager {

private static final Logger LOGGER = Logger.getLogger(IoTProvisioningManager.class.getName());

private static final StanzaFilter FRIEND_MESSAGE = new AndFilter(StanzaTypeFilter.MESSAGE,
new StanzaExtensionFilter(Friend.ELEMENT, Friend.NAMESPACE));
private static final StanzaFilter UNFRIEND_MESSAGE = new AndFilter(StanzaTypeFilter.MESSAGE,
new StanzaExtensionFilter(Unfriend.ELEMENT, Unfriend.NAMESPACE));

Expand Down Expand Up @@ -99,6 +105,13 @@ public static synchronized IoTProvisioningManager getInstanceFor(XMPPConnection

private final Roster roster;
private final LruCache<Jid, LruCache<BareJid, Void>> negativeFriendshipRequestCache = new LruCache<>(8);
private final LruCache<BareJid, Void> friendshipDeniedCache = new LruCache<>(16);

private final LruCache<BareJid, Void> friendshipRequestedCache = new LruCache<>(16);

private final Set<BecameFriendListener> becameFriendListeners = new CopyOnWriteArraySet<>();

private final Set<WasUnfriendedListener> wasUnfriendedListeners = new CopyOnWriteArraySet<>();

private Jid configuredProvisioningServer;

Expand Down Expand Up @@ -129,6 +142,47 @@ public void processPacket(Stanza stanza) throws NotConnectedException, Interrupt
}
}, UNFRIEND_MESSAGE);

// Stanza listener for XEP-0324 § 3.2.4.
connection.addAsyncStanzaListener(new StanzaListener() {
@Override
public void processPacket(final Stanza stanza) throws NotConnectedException, InterruptedException {
final Message friendMessage = (Message) stanza;
final Friend friend = Friend.from(friendMessage);
final BareJid friendJid = friend.getFriend();

if (isFromProvisioningService(friendMessage)) {
// We received a recommendation from a provisioning server.
// Notify the recommended friend that we will now accept his
// friendship requests.
final XMPPConnection connection = connection();
Friend friendNotifiacation = new Friend(connection.getUser().asBareJid());
Message notificationMessage = new Message(friendJid, friendNotifiacation);
connection.sendStanza(notificationMessage);
} else {
// Check is the message was send from a thing we previously
// tried to become friends with. If this is the case, then
// thing is likely telling us that we can become now
// friends.
Jid from = friendMessage.getFrom();
if (!friendshipDeniedCache.containsKey(from)) {
return;
}

BareJid bareFrom = from.asBareJid();
// Sanity check: If a thing recommends us itself as friend,
// which should be the case once we reach this code, then
// the bare 'from' JID should be equals to the JID of the
// recommended friend.
if (!bareFrom.equals(friendJid)) {
return;
}

// Re-try the friendship request.
sendFriendshipRequest(friendJid);
}
}
}, FRIEND_MESSAGE);

connection.registerIQRequestHandler(
new AbstractIqRequestHandler(ClearCache.ELEMENT, ClearCache.NAMESPACE, Type.set, Mode.async) {
@Override
Expand Down Expand Up @@ -193,6 +247,25 @@ public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
}
}
});

roster.addPresenceEventListener(new AbstractPresenceEventListener() {
@Override
public void presenceSubscribed(BareJid address, Presence subscribedPresence) {
friendshipRequestedCache.remove(address);
for (BecameFriendListener becameFriendListener : becameFriendListeners) {
becameFriendListener.becameFriend(address, subscribedPresence);
}
}
@Override
public void presenceUnsubscribed(BareJid address, Presence unsubscribedPresence) {
if (friendshipRequestedCache.containsKey(address)) {
friendshipDeniedCache.put(address, null);
}
for (WasUnfriendedListener wasUnfriendedListener : wasUnfriendedListeners) {
wasUnfriendedListener.wasUnfriendedListener(address, unsubscribedPresence);
}
}
});
}

/**
Expand Down Expand Up @@ -272,6 +345,9 @@ public boolean isFriend(Jid provisioningServer, BareJid friendInQuestion) throws
public void sendFriendshipRequest(BareJid bareJid) throws NotConnectedException, InterruptedException {
Presence presence = new Presence(Presence.Type.subscribe);
presence.setTo(bareJid);

friendshipRequestedCache.put(bareJid, null);

connection().sendStanza(presence);
}

Expand All @@ -295,6 +371,22 @@ public void unfriend(Jid friend) throws NotConnectedException, InterruptedExcept
}
}

public boolean addBecameFriendListener(BecameFriendListener becameFriendListener) {
return becameFriendListeners.add(becameFriendListener);
}

public boolean removeBecameFriendListener(BecameFriendListener becameFriendListener) {
return becameFriendListeners.remove(becameFriendListener);
}

public boolean addWasUnfriendedListener(WasUnfriendedListener wasUnfriendedListener) {
return wasUnfriendedListeners.add(wasUnfriendedListener);
}

public boolean removeWasUnfriendedListener(WasUnfriendedListener wasUnfriendedListener) {
return wasUnfriendedListeners.remove(wasUnfriendedListener);
}

private boolean isFromProvisioningService(Stanza stanza) {
Jid provisioningServer;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
*
* Copyright 2016 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.smackx.iot.provisioning;

import org.jivesoftware.smack.packet.Presence;
import org.jxmpp.jid.BareJid;

public interface WasUnfriendedListener {

void wasUnfriendedListener(BareJid jid, Presence presence);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
*
* Copyright © 2016 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.smackx.iot.provisioning.element;

import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jxmpp.jid.BareJid;

public class Friend implements ExtensionElement {

public static final String ELEMENT = "friend";
public static final String NAMESPACE = Constants.IOT_PROVISIONING_NAMESPACE;

private final BareJid friend;

public Friend(BareJid friend) {
this.friend = Objects.requireNonNull(friend, "Friend must not be null");
}

@Override
public String getElementName() {
return ELEMENT;
}

@Override
public String getNamespace() {
return NAMESPACE;
}

@Override
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this);
xml.attribute("jid", friend);
xml.closeEmptyElement();
return xml;
}

public BareJid getFriend() {
return friend;
}

public static Friend from(Message message) {
return message.getExtension(ELEMENT, NAMESPACE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
*
* Copyright © 2016 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.smackx.iot.provisioning.provider;

import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smackx.iot.provisioning.element.Friend;
import org.jxmpp.jid.BareJid;
import org.jxmpp.stringprep.XmppStringprepException;
import org.xmlpull.v1.XmlPullParser;

public class FriendProvider extends ExtensionElementProvider<Friend> {

@Override
public Friend parse(XmlPullParser parser, int initialDepth) throws XmppStringprepException {
BareJid jid = ParserUtils.getBareJidAttribute(parser);
return new Friend(jid);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@
<namespace>urn:xmpp:iot:provisioning</namespace>
<className>org.jivesoftware.smackx.iot.provisioning.provider.ClearCacheResponseProvider</className>
</iqProvider>
<extensionProvider>
<elementName>friend</elementName>
<namespace>urn:xmpp:iot:provisioning</namespace>
<className>org.jivesoftware.smackx.iot.provisioning.provider.FriendProvider</className>
</extensionProvider>
<extensionProvider>
<elementName>unfriend</elementName>
<namespace>urn:xmpp:iot:provisioning</namespace>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ public boolean addSubscribeListener(SubscribeListener subscribeListener) {
Objects.requireNonNull(subscribeListener, "SubscribeListener argument must not be null");
if (subscriptionMode != SubscriptionMode.manual) {
previousSubscriptionMode = subscriptionMode;
subscriptionMode = SubscriptionMode.manual;
}
return subscribeListeners.add(subscribeListener);
}
Expand Down Expand Up @@ -1228,6 +1229,7 @@ private void addUpdateEntry(Collection<Jid> addedEntries, Collection<Jid> update
RosterPacket.Item oldItem = RosterEntry.toRosterItem(oldEntry);
if (!oldEntry.equalsDeep(entry) || !item.getGroupNames().equals(oldItem.getGroupNames())) {
updatedEntries.add(item.getJid());
oldEntry.updateItem(item);
} else {
// Record the entry as unchanged, so that it doesn't end up as deleted entry
unchangedEntries.add(item.getJid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
import org.jivesoftware.smack.roster.packet.RosterPacket;
import org.jxmpp.jid.BareJid;

Expand All @@ -41,7 +43,7 @@
*/
public final class RosterEntry extends Manager {

private final RosterPacket.Item item;
private RosterPacket.Item item;
final private Roster roster;

/**
Expand Down Expand Up @@ -120,10 +122,9 @@ public synchronized void setName(String name) throws NotConnectedException, NoRe
* @param type the subscription type.
* @param subscriptionPending TODO
*/
void updateState(String name, RosterPacket.ItemType type, boolean subscriptionPending) {
item.setName(name);
item.setItemType(type);
item.setSubscriptionPending(subscriptionPending);
void updateItem(RosterPacket.Item item) {
assert(item != null);
this.item = item;
}

/**
Expand Down Expand Up @@ -209,6 +210,18 @@ public boolean canSeeHisPresence() {
}
}

/**
* Cancel the presence subscription the XMPP entity representing this roster entry has with us.
*
* @throws NotConnectedException
* @throws InterruptedException
* @since 4.2
*/
public void cancelSubscription() throws NotConnectedException, InterruptedException {
Presence unsubscribed = new Presence(item.getJid(), Type.unsubscribed);
connection().sendStanza(unsubscribed);
}

public String toString() {
StringBuilder buf = new StringBuilder();
if (getName() != null) {
Expand Down
Loading

0 comments on commit 1d3c48e

Please sign in to comment.