Permalink
Browse files

Add an admin handler that can be used to unsubscribe other users from

feeds.

A user was susbcribed to http://www.dcde.ru/feeds/atom, which updates
several times a minute.
  • Loading branch information...
mihaip committed Dec 31, 2011
1 parent f763dfe commit 2e1297ec9202d9e179774bef37c416e9cf848f27
@@ -55,7 +55,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
body = partychatMatcher.replaceFirst("");
}
String[] bodyPieces = body.split("\\s+", 2);
String[] bodyPieces = body.split("\\s+");
String commandName = bodyPieces[0];
String[] args = Arrays.copyOfRange(bodyPieces, 1, bodyPieces.length);
@@ -0,0 +1,34 @@
// Copyright 2011 Google Inc. All Rights Reserved.
package info.persistent.pushbot.commands;
import com.google.appengine.api.xmpp.JID;
import info.persistent.pushbot.util.Xmpp;
import java.util.Arrays;
/**
* Base class for commands that require administrator access.
*/
public abstract class AdminCommandHandler implements CommandHandler {
@Override
public void handle(JID adminUser, String... args) {
if (!Xmpp.toShortJid(adminUser).getId().equals("mihai.parparita@gmail.com")) {
Xmpp.sendMessage(adminUser, "You're not an administrator");
return;
}
if (args.length == 0) {
Xmpp.sendMessage(adminUser, "Need arguments");
return;
}
JID targetUser = new JID(args[0]);
handle(adminUser, targetUser, Arrays.copyOfRange(args, 1, args.length));
}
protected abstract void handle(JID adminUser, JID targetUser, String... args);
}
@@ -0,0 +1,61 @@
// Copyright 2011 Google Inc. All Rights Reserved.
package info.persistent.pushbot.commands;
import com.google.appengine.api.xmpp.JID;
import info.persistent.pushbot.data.Subscription;
import info.persistent.pushbot.util.Hubs;
import info.persistent.pushbot.util.Persistence;
import info.persistent.pushbot.util.Xmpp;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
/**
* Remove another user's subscription.
*/
public class AdminUnsubscribeCommandHandler extends AdminCommandHandler {
private static final Logger logger =
Logger.getLogger(AdminUnsubscribeCommandHandler.class.getName());
@Override
public void handle(JID adminUser, JID targetUser, String... args) {
if (args.length != 1) {
Xmpp.sendMessage(adminUser, "No feed URL not specified");
return;
}
URL feedUrl;
try {
feedUrl = new URL(args[0]);
} catch (MalformedURLException err) {
logger.log(Level.INFO, "URL parse exception", err);
Xmpp.sendMessage(adminUser, "Feed URL is malformed");
return;
}
final List<Subscription> subscriptions =
Subscription.getSubscriptionsForUserAndFeedUrl(targetUser, feedUrl);
if (subscriptions.isEmpty()) {
Xmpp.sendMessage(adminUser, "No subscriptions match.");
}
for (Subscription subscription : subscriptions) {
Hubs.sendRequestToHub(targetUser, subscription.getHubUrl(), feedUrl, false);
}
Persistence.withManager(new Persistence.Closure() {
@Override public void run(PersistenceManager manager) {
manager.deletePersistentAll(subscriptions);
}
});
}
}
@@ -32,7 +32,12 @@
" _URL_",
"Subscribe to all of the feed URLs in the given OPML file.",
new OpmlImportCommandHandler()),
HELP("help", "", "This message", new HelpCommandHandler());
HELP("help", "", "This message", new HelpCommandHandler()),
ADMIN_UNSUBSCRIBE(
"admin-unsubscribe",
" _URL_ _JID_",
"Remove the given user's subscription .",
new AdminUnsubscribeCommandHandler());
private final String name;
private final String argSample;
@@ -15,6 +15,9 @@ public void handle(JID user, String... args) {
+ "PubSubHubbub-enabled feed updates in realtime. Possible commands:");
for (Command command : Command.values()) {
if (command.getHandler() instanceof AdminCommandHandler) {
continue;
}
message.append("\n /" + command.getName() + command.getArgSample() + ": "
+ command.getDescription());
}

0 comments on commit 2e1297e

Please sign in to comment.