FileDocCategorySizeDatePackage
TorrentCommand.javaAPI DocAzureus 3.0.3.45627Mon Aug 21 05:59:08 BST 2006org.gudy.azureus2.ui.console.commands

TorrentCommand

public abstract class TorrentCommand extends IConsoleCommand
base class for objects which need to operate on specific torrents. this class allows the torrent to be identified by hash, number or 'all' and will pass the appropriate torrent(s) to the subclasses 'performCommand' method
author
tobi

Fields Summary
private final String
primaryCommandName
private final String
action
Constructors Summary
public TorrentCommand(String[] commandNames, String action)
initializes the torrent command

param
commandNames (the first item in the array is regarded as the primary command name)
param
action a description to be used when this command is executed

		super(commandNames);
		this.primaryCommandName = commandNames[0];
		this.action = action;
	
Methods Summary
public voidexecute(java.lang.String commandName, org.gudy.azureus2.ui.console.ConsoleInput ci, java.util.List args)

		if (!args.isEmpty()) {
		    String subcommand = (String) args.remove(0);
			if (ci.torrents.isEmpty()) {
				ci.out.println("> Command '" + getCommandName() + "': No torrents in list (Maybe you forgot to 'show torrents' first).");
			} else {
				String name;
				DownloadManager dm;
				try {
					int number = Integer.parseInt(subcommand);
					if ((number > 0) && (number <= ci.torrents.size())) {
						dm = (DownloadManager) ci.torrents.get(number - 1);
						if (dm.getDisplayName() == null)
							name = "?";
						else
							name = dm.getDisplayName();
						performCommandIfAllowed(ci, args, dm, "#" + subcommand, name);
					} else
						ci.out.println("> Command '" + getCommandName() + "': Torrent #" + subcommand + " unknown.");
				} catch (NumberFormatException e) {
					if ("all".equalsIgnoreCase(subcommand)) {
						Iterator torrent = ci.torrents.iterator();
						while (torrent.hasNext()) {
							dm = (DownloadManager) torrent.next();
							if (dm.getDisplayName() == null)
								name = "?";
							else
								name = dm.getDisplayName();
							performCommandIfAllowed(ci, args, dm, subcommand, name);
						}
					} else if ("hash".equalsIgnoreCase(subcommand)) {
						String hash = (String) args.remove(0); 
						List torrents = ci.getGlobalManager().getDownloadManagers();
						boolean foundit = false;
						if (!torrents.isEmpty()) {
							Iterator torrent = torrents.iterator();
							while (torrent.hasNext()) {
								dm = (DownloadManager) torrent.next();
								if (hash.equals(TorrentUtils.nicePrintTorrentHash(dm.getTorrent(), true))) {
									if (dm.getDisplayName() == null)
										name = "?";
									else
										name = dm.getDisplayName();
									// FIXME: check user permission here and fix it to take torrent hash instead of subcommand
									
									performCommandIfAllowed(ci, args, dm, hash, name);
									foundit = true;
									break;
								}
							}
							if (!foundit)
								ci.out.println("> Command '" + getCommandName() + "': Hash '" + hash + "' unknown.");
						}
					} else {
						ci.out.println("> Command '" + getCommandName() + "': Subcommand '" + subcommand + "' unknown.");
					}
				}
			}
		} else {
			ci.out.println("> Missing subcommand for '" + getCommandName() + "'");
			printHelp(ci.out, args);			
		}
	
protected java.lang.StringgetAction()

		return action;
	
protected java.lang.StringgetCommandName()

		return primaryCommandName;
	
protected abstract booleanperformCommand(org.gudy.azureus2.ui.console.ConsoleInput ci, org.gudy.azureus2.core3.download.DownloadManager dm, java.util.List args)

private voidperformCommandIfAllowed(org.gudy.azureus2.ui.console.ConsoleInput ci, java.util.List args, org.gudy.azureus2.core3.download.DownloadManager dm, java.lang.String desc, java.lang.String name)
checks the role of the user. if the user is a 'guest', they are not able to perform any actions on the torrents. they are a 'read only' role. if they are a 'user' role, they are only able to modify their own torrents. users with the 'admin' role can modify anybody's torrents

param
ci
param
args
param
dm
param
name
param
subcommand

		if( ! UserProfile.ADMIN.equals( ci.getUserProfile().getUserType() ) )
		{
			if( UserProfile.USER.equals( ci.getUserProfile().getUserType() ) )
			{
				String owner = dm.getDownloadState().getAttribute(DownloadManagerState.AT_USER);
				if(! ci.getUserProfile().getUsername().equals(owner) )
				{
					ci.out.println("> " + getAction() + " torrent " + desc + " (" + name + ") failed: Permission Denied. Users can only modify their own torrents");
					return;
				}
			}
			else
			{
				ci.out.println("> " + getAction() + " torrent " + desc + " (" + name + ") failed: Permission Denied. Guests cannot modify torrents");
				return;
			}
		}
		if (performCommand(ci, dm, args))
			ci.out.println("> " + getAction() + " Torrent " + desc + " (" + name + ") succeeded.");
		else
			ci.out.println("> " + getAction() + " Torrent " + desc + " (" + name + ") failed.");		
		
	
public voidprintHelp(java.io.PrintStream out, java.util.List args)
prints out the syntax of this command

		out.println("> " + getCommandName() + " syntax: " + getCommandName() + " (<#>|all|hash <hash>)");