Browse Source

Protect setLabel in case plugin isn't installed.

pull/407/head
Alon Albert 7 years ago
parent
commit
c7a6946e0a
  1. 8
      app/src/main/java/org/transdroid/daemon/Deluge/DelugeAdapter.java
  2. 349
      app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java
  3. 23
      app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java

8
app/src/main/java/org/transdroid/daemon/Deluge/DelugeAdapter.java

@ -69,8 +69,8 @@ import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSEEDS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSIZE; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSIZE;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKERS; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKERS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_STATUS; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_STATUS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_TIER; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIER;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_URL; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_URL;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER;
import com.android.internalcopy.http.multipart.FilePart; import com.android.internalcopy.http.multipart.FilePart;
@ -388,8 +388,8 @@ public class DelugeAdapter implements IDaemonAdapter {
// Build an JSON arrays of objcts that each have a tier (order) number and an url // Build an JSON arrays of objcts that each have a tier (order) number and an url
for (int i = 0; i < trackersTask.getNewTrackers().size(); i++) { for (int i = 0; i < trackersTask.getNewTrackers().size(); i++) {
JSONObject trackerObj = new JSONObject(); JSONObject trackerObj = new JSONObject();
trackerObj.put(RPC_TRACKER_TIER, i); trackerObj.put(RPC_TIER, i);
trackerObj.put(RPC_TRACKER_URL, trackersTask.getNewTrackers().get(i)); trackerObj.put(RPC_URL, trackersTask.getNewTrackers().get(i));
trackers.put(trackerObj); trackers.put(trackerObj);
} }
params.put(new JSONArray().put(task.getTargetTorrent().getUniqueID())); params.put(new JSONArray().put(task.getTargetTorrent().getUniqueID()));

349
app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java

@ -28,194 +28,195 @@ import org.transdroid.daemon.TorrentStatus;
* @author alon.albert * @author alon.albert
*/ */
class DelugeCommon { class DelugeCommon {
static final String RPC_DETAILS = "files";
static final String RPC_DOWNLOADEDEVER = "total_done";
static final String RPC_ETA = "eta";
static final String RPC_FILE = "file";
static final String RPC_FILEPRIORITIES = "file_priorities";
static final String RPC_FILEPROGRESS = "file_progress";
static final String RPC_HASH = "hash";
static final String RPC_INDEX = "index";
static final String RPC_LABEL = "label";
static final String RPC_MAXDOWNLOAD = "max_download_speed";
static final String RPC_MAXUPLOAD = "max_upload_speed";
static final String RPC_MESSAGE = "message";
static final String RPC_METHOD = "method";
static final String RPC_METHOD_ADD = "core.add_torrent_url";
static final String RPC_METHOD_ADD_FILE = "core.add_torrent_file";
static final String RPC_METHOD_ADD_MAGNET = "core.add_torrent_magnet";
static final String RPC_METHOD_AUTH_LOGIN = "auth.login";
static final String RPC_METHOD_DAEMON_LOGIN = "daemon.login";
static final String RPC_METHOD_FORCERECHECK = "core.force_recheck";
static final String RPC_METHOD_GET = "web.update_ui";
static final String RPC_METHOD_GET_LABELS = "label.get_labels";
static final String RPC_METHOD_GET_METHOD_LIST = "daemon.get_method_list";
static final String RPC_METHOD_GET_TORRENTS_STATUS = "core.get_torrents_status";
static final String RPC_METHOD_INFO = "daemon.info";
static final String RPC_METHOD_MOVESTORAGE = "core.move_storage";
static final String RPC_METHOD_PAUSE = "core.pause_torrent";
static final String RPC_METHOD_PAUSE_ALL = "core.pause_all_torrents";
static final String RPC_METHOD_REMOVE = "core.remove_torrent";
static final String RPC_METHOD_RESUME = "core.resume_torrent";
static final String RPC_METHOD_RESUME_ALL = "core.resume_all_torrents";
static final String RPC_METHOD_SETCONFIG = "core.set_config";
static final String RPC_METHOD_SETFILE = "core.set_torrent_file_priorities";
static final String RPC_METHOD_SETLABEL = "label.set_torrent";
static final String RPC_METHOD_SETTRACKERS = "core.set_torrent_trackers";
static final String RPC_METHOD_SET_TORRENT_OPTIONS = "core.set_torrent_options";
static final String RPC_METHOD_STATUS = "core.get_torrent_status";
static final String RPC_NAME = "name";
static final String RPC_NUMPEERS = "num_peers";
static final String RPC_NUMSEEDS = "num_seeds";
static final String RPC_PARAMS = "params";
static final String RPC_PARTDONE = "progress";
static final String RPC_PATH = "path";
static final String RPC_RATEDOWNLOAD = "download_payload_rate";
static final String RPC_RATEUPLOAD = "upload_payload_rate";
static final String RPC_RESULT = "result";
static final String RPC_SAVEPATH = "save_path";
static final String RPC_SESSION_ID = "_session_id";
static final String RPC_SIZE = "size";
static final String RPC_STATUS = "state";
static final String RPC_TIMEADDED = "time_added";
static final String RPC_TORRENTS = "torrents";
static final String RPC_TOTALPEERS = "total_peers";
static final String RPC_TOTALSEEDS = "total_seeds";
static final String RPC_TOTALSIZE = "total_size";
static final String RPC_TRACKERS = "trackers";
static final String RPC_TRACKER_STATUS = "tracker_status";
static final String RPC_TRACKER_TIER = "tier";
static final String RPC_TRACKER_URL = "url";
static final String RPC_UPLOADEDEVER = "total_uploaded";
static final String[] RPC_DETAILS_FIELDS_ARRAY = { static final String RPC_DETAILS = "files";
RPC_TRACKERS, static final String RPC_DOWNLOADEDEVER = "total_done";
RPC_TRACKER_STATUS, static final String RPC_ETA = "eta";
}; static final String RPC_FILE = "file";
static final String[] RPC_FIELDS_ARRAY = { static final String RPC_FILEPRIORITIES = "file_priorities";
RPC_HASH, static final String RPC_FILEPROGRESS = "file_progress";
RPC_NAME, static final String RPC_HASH = "hash";
RPC_STATUS, static final String RPC_INDEX = "index";
RPC_SAVEPATH, static final String RPC_LABEL = "label";
RPC_RATEDOWNLOAD, static final String RPC_MAXDOWNLOAD = "max_download_speed";
RPC_RATEUPLOAD, static final String RPC_MAXUPLOAD = "max_upload_speed";
RPC_NUMPEERS, static final String RPC_MESSAGE = "message";
RPC_NUMSEEDS, static final String RPC_METHOD = "method";
RPC_TOTALPEERS, static final String RPC_METHOD_ADD = "core.add_torrent_url";
RPC_TOTALSEEDS, static final String RPC_METHOD_ADD_FILE = "core.add_torrent_file";
RPC_ETA, static final String RPC_METHOD_ADD_MAGNET = "core.add_torrent_magnet";
RPC_DOWNLOADEDEVER, static final String RPC_METHOD_AUTH_LOGIN = "auth.login";
RPC_UPLOADEDEVER, static final String RPC_METHOD_DAEMON_LOGIN = "daemon.login";
RPC_TOTALSIZE, static final String RPC_METHOD_FORCERECHECK = "core.force_recheck";
RPC_PARTDONE, static final String RPC_METHOD_GET = "web.update_ui";
RPC_LABEL, static final String RPC_METHOD_GET_LABELS = "label.get_labels";
RPC_MESSAGE, static final String RPC_METHOD_GET_METHOD_LIST = "daemon.get_method_list";
RPC_TIMEADDED, static final String RPC_METHOD_GET_TORRENTS_STATUS = "core.get_torrents_status";
RPC_TRACKER_STATUS, static final String RPC_METHOD_INFO = "daemon.info";
}; static final String RPC_METHOD_MOVESTORAGE = "core.move_storage";
static final String[] RPC_FILE_FIELDS_ARRAY = { static final String RPC_METHOD_PAUSE = "core.pause_torrent";
RPC_DETAILS, static final String RPC_METHOD_PAUSE_ALL = "core.pause_all_torrents";
RPC_FILEPROGRESS, static final String RPC_METHOD_REMOVE = "core.remove_torrent";
RPC_FILEPRIORITIES, static final String RPC_METHOD_RESUME = "core.resume_torrent";
}; static final String RPC_METHOD_RESUME_ALL = "core.resume_all_torrents";
static final String RPC_METHOD_SETCONFIG = "core.set_config";
static final String RPC_METHOD_SETFILE = "core.set_torrent_file_priorities";
static final String RPC_METHOD_SETLABEL = "label.set_torrent";
static final String RPC_METHOD_SETTRACKERS = "core.set_torrent_trackers";
static final String RPC_METHOD_SET_TORRENT_OPTIONS = "core.set_torrent_options";
static final String RPC_METHOD_STATUS = "core.get_torrent_status";
static final String RPC_NAME = "name";
static final String RPC_NUMPEERS = "num_peers";
static final String RPC_NUMSEEDS = "num_seeds";
static final String RPC_PARAMS = "params";
static final String RPC_PARTDONE = "progress";
static final String RPC_PATH = "path";
static final String RPC_RATEDOWNLOAD = "download_payload_rate";
static final String RPC_RATEUPLOAD = "upload_payload_rate";
static final String RPC_RESULT = "result";
static final String RPC_SAVEPATH = "save_path";
static final String RPC_SESSION_ID = "_session_id";
static final String RPC_SIZE = "size";
static final String RPC_STATUS = "state";
static final String RPC_TIMEADDED = "time_added";
static final String RPC_TORRENTS = "torrents";
static final String RPC_TOTALPEERS = "total_peers";
static final String RPC_TOTALSEEDS = "total_seeds";
static final String RPC_TIER = "tier";
static final String RPC_TOTALSIZE = "total_size";
static final String RPC_TRACKERS = "trackers";
static final String RPC_TRACKER_STATUS = "tracker_status";
static final String RPC_URL = "url";
static final String RPC_UPLOADEDEVER = "total_uploaded";
static TorrentStatus convertDelugeState(String state) { static final String[] RPC_DETAILS_FIELDS_ARRAY = {
// Deluge sends a string with status code RPC_TRACKERS,
if (state.compareTo("Paused") == 0) { RPC_TRACKER_STATUS,
return TorrentStatus.Paused; };
} else if (state.compareTo("Seeding") == 0) { static final String[] RPC_FIELDS_ARRAY = {
return TorrentStatus.Seeding; RPC_HASH,
} else if (state.compareTo("Downloading") == 0 || state.compareTo("Active") == 0) { RPC_NAME,
return TorrentStatus.Downloading; RPC_STATUS,
} else if (state.compareTo("Checking") == 0) { RPC_SAVEPATH,
return TorrentStatus.Checking; RPC_RATEDOWNLOAD,
} else if (state.compareTo("Queued") == 0) { RPC_RATEUPLOAD,
return TorrentStatus.Queued; RPC_NUMPEERS,
} RPC_NUMSEEDS,
return TorrentStatus.Unknown; RPC_TOTALPEERS,
RPC_TOTALSEEDS,
RPC_ETA,
RPC_DOWNLOADEDEVER,
RPC_UPLOADEDEVER,
RPC_TOTALSIZE,
RPC_PARTDONE,
RPC_LABEL,
RPC_MESSAGE,
RPC_TIMEADDED,
RPC_TRACKER_STATUS,
};
static final String[] RPC_FILE_FIELDS_ARRAY = {
RPC_DETAILS,
RPC_FILEPROGRESS,
RPC_FILEPRIORITIES,
};
static TorrentStatus convertDelugeState(String state) {
// Deluge sends a string with status code
if (state.compareTo("Paused") == 0) {
return TorrentStatus.Paused;
} else if (state.compareTo("Seeding") == 0) {
return TorrentStatus.Seeding;
} else if (state.compareTo("Downloading") == 0 || state.compareTo("Active") == 0) {
return TorrentStatus.Downloading;
} else if (state.compareTo("Checking") == 0) {
return TorrentStatus.Checking;
} else if (state.compareTo("Queued") == 0) {
return TorrentStatus.Queued;
} }
return TorrentStatus.Unknown;
}
@NonNull @NonNull
static Priority convertDelugePriority(int priority, int version) { static Priority convertDelugePriority(int priority, int version) {
if (version >= 10303) { if (version >= 10303) {
// Priority codes changes from Deluge 1.3.3 onwards // Priority codes changes from Deluge 1.3.3 onwards
switch (priority) { switch (priority) {
case 0: case 0:
return Priority.Off; return Priority.Off;
case 1: case 1:
return Priority.Low; return Priority.Low;
case 7: case 7:
return Priority.High; return Priority.High;
default: default:
return Priority.Normal; return Priority.Normal;
} }
} else { } else {
switch (priority) { switch (priority) {
case 0: case 0:
return Priority.Off; return Priority.Off;
case 2: case 2:
return Priority.Normal; return Priority.Normal;
case 5: case 5:
return Priority.High; return Priority.High;
default: default:
return Priority.Low; return Priority.Low;
}
} }
} }
}
static int convertPriority(Priority priority, int version) { static int convertPriority(Priority priority, int version) {
if (version >= 10303) { if (version >= 10303) {
// Priority codes changes from Deluge 1.3.3 onwards // Priority codes changes from Deluge 1.3.3 onwards
switch (priority) { switch (priority) {
case Off: case Off:
return 0; return 0;
case Low: case Low:
return 1; return 1;
case High: case High:
return 7; return 7;
default: default:
return 5; return 5;
} }
} else { } else {
switch (priority) { switch (priority) {
case Off: case Off:
return 0; return 0;
case Normal: case Normal:
return 2; return 2;
case High: case High:
return 5; return 5;
default: default:
return 1; return 1;
}
} }
} }
}
static int getVersionString(String versionString) { static int getVersionString(String versionString) {
int version = 0; int version = 0;
final String[] parts = versionString.split("\\."); final String[] parts = versionString.split("\\.");
if (parts.length > 0) { if (parts.length > 0) {
version = Integer.parseInt(parts[0]) * 100 * 100; version = Integer.parseInt(parts[0]) * 100 * 100;
if (parts.length > 1) { if (parts.length > 1) {
version += Integer.parseInt(parts[1]) * 100; version += Integer.parseInt(parts[1]) * 100;
if (parts.length > 2) { if (parts.length > 2) {
// For the last part only read until a non-numeric character is read // For the last part only read until a non-numeric character is read
// For example version 3.0.0-alpha5 is read as version code 30000 // For example version 3.0.0-alpha5 is read as version code 30000
String numbers = ""; String numbers = "";
for (char c : parts[2].toCharArray()) { for (char c : parts[2].toCharArray()) {
if (Character.isDigit(c)) if (Character.isDigit(c))
// Still a number; add it to the numbers string // Still a number; add it to the numbers string
{ {
numbers += Character.toString(c); numbers += Character.toString(c);
} else { } else {
// No longer reading numbers; stop reading // No longer reading numbers; stop reading
break; break;
}
} }
version += Integer.parseInt(numbers);
} }
version += Integer.parseInt(numbers);
} }
} }
return version;
} }
return version;
}
} }

23
app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java

@ -66,8 +66,8 @@ import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSEEDS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSIZE; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSIZE;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKERS; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKERS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_STATUS; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_STATUS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_TIER; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIER;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_URL; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_URL;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -209,9 +209,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
final List<Torrent> torrents = getTorrents(torrentsStatus.values()); final List<Torrent> torrents = getTorrents(torrentsStatus.values());
// Check if Label plugin is enabled // Check if Label plugin is enabled
//noinspection unchecked final boolean hasLabelPlugin = hasMethod(client, RPC_METHOD_GET_LABELS);
final List<String> methods = (List<String>) client.sendRequest(RPC_METHOD_GET_METHOD_LIST);
final boolean hasLabelPlugin = methods.contains(RPC_METHOD_GET_LABELS);
// Get label list from server // Get label list from server
//noinspection unchecked //noinspection unchecked
@ -239,7 +237,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
.get(RPC_TRACKERS); .get(RPC_TRACKERS);
final List<String> trackers = new ArrayList<>(); final List<String> trackers = new ArrayList<>();
for (Map<String, Object> trackerResponse : trackerResponses) { for (Map<String, Object> trackerResponse : trackerResponses) {
trackers.add((String) trackerResponse.get(RPC_TRACKER_URL)); trackers.add((String) trackerResponse.get(RPC_URL));
} }
return new GetTorrentDetailsTaskSuccessResult(task, new TorrentDetails( return new GetTorrentDetailsTaskSuccessResult(task, new TorrentDetails(
@ -295,6 +293,9 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
@NonNull @NonNull
private DaemonTaskResult doSetLabel(DelugeRpcClient client, SetLabelTask task) throws DaemonException { private DaemonTaskResult doSetLabel(DelugeRpcClient client, SetLabelTask task) throws DaemonException {
if (!hasMethod(client, RPC_METHOD_SETLABEL)) {
throw new DaemonException(ExceptionType.MethodUnsupported, "Label plugin not installed");
}
final String torrentId = task.getTargetTorrent().getUniqueID(); final String torrentId = task.getTargetTorrent().getUniqueID();
final String label = task.getNewLabel() == null ? "" : task.getNewLabel(); final String label = task.getNewLabel() == null ? "" : task.getNewLabel();
client.sendRequest(RPC_METHOD_SETLABEL, torrentId, label); client.sendRequest(RPC_METHOD_SETLABEL, torrentId, label);
@ -345,8 +346,8 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
final ArrayList<String> newTrackers = task.getNewTrackers(); final ArrayList<String> newTrackers = task.getNewTrackers();
for (int i = 0, n = newTrackers.size(); i < n; i++) { for (int i = 0, n = newTrackers.size(); i < n; i++) {
final Map<String, Object> tracker = new HashMap<>(); final Map<String, Object> tracker = new HashMap<>();
tracker.put(RPC_TRACKER_TIER, i); tracker.put(RPC_TIER, i);
tracker.put(RPC_TRACKER_URL, newTrackers.get(i)); tracker.put(RPC_URL, newTrackers.get(i));
trackers.add(tracker); trackers.add(tracker);
} }
client.sendRequest(RPC_METHOD_SETTRACKERS, task.getTargetTorrent().getUniqueID(), trackers); client.sendRequest(RPC_METHOD_SETTRACKERS, task.getTargetTorrent().getUniqueID(), trackers);
@ -539,6 +540,12 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
return new String[]{task.getTargetTorrent().getUniqueID()}; return new String[]{task.getTargetTorrent().getUniqueID()};
} }
private boolean hasMethod(DelugeRpcClient client, String method) throws DaemonException {
//noinspection unchecked
final List<String> methods = (List<String>) client.sendRequest(RPC_METHOD_GET_METHOD_LIST);
return methods.contains(method);
}
/** /**
* Used to count torrents in labels. * Used to count torrents in labels.
*/ */

Loading…
Cancel
Save