Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
Add an admin handler that can be used to unsubscribe other users from
Browse files Browse the repository at this point in the history
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 2e1297e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/info/persistent/pushbot/XmppReceiverServlet.java
Expand Up @@ -55,7 +55,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
body = partychatMatcher.replaceFirst(""); body = partychatMatcher.replaceFirst("");
} }


String[] bodyPieces = body.split("\\s+", 2); String[] bodyPieces = body.split("\\s+");


String commandName = bodyPieces[0]; String commandName = bodyPieces[0];
String[] args = Arrays.copyOfRange(bodyPieces, 1, bodyPieces.length); String[] args = Arrays.copyOfRange(bodyPieces, 1, bodyPieces.length);
Expand Down
34 changes: 34 additions & 0 deletions src/info/persistent/pushbot/commands/AdminCommandHandler.java
@@ -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);
}
});
}

}
7 changes: 6 additions & 1 deletion src/info/persistent/pushbot/commands/Command.java
Expand Up @@ -32,7 +32,12 @@ public enum Command {
" _URL_", " _URL_",
"Subscribe to all of the feed URLs in the given OPML file.", "Subscribe to all of the feed URLs in the given OPML file.",
new OpmlImportCommandHandler()), 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 name;
private final String argSample; private final String argSample;
Expand Down
3 changes: 3 additions & 0 deletions src/info/persistent/pushbot/commands/HelpCommandHandler.java
Expand Up @@ -15,6 +15,9 @@ public void handle(JID user, String... args) {
+ "PubSubHubbub-enabled feed updates in realtime. Possible commands:"); + "PubSubHubbub-enabled feed updates in realtime. Possible commands:");


for (Command command : Command.values()) { for (Command command : Command.values()) {
if (command.getHandler() instanceof AdminCommandHandler) {
continue;
}
message.append("\n /" + command.getName() + command.getArgSample() + ": " message.append("\n /" + command.getName() + command.getArgSample() + ": "
+ command.getDescription()); + command.getDescription());
} }
Expand Down

0 comments on commit 2e1297e

Please sign in to comment.