From c7a6946e0afa361e1897dd8b8fa9faf1ab5054dc Mon Sep 17 00:00:00 2001 From: Alon Albert Date: Wed, 3 Jan 2018 12:49:18 +0200 Subject: [PATCH] Protect setLabel in case plugin isn't installed. --- .../daemon/Deluge/DelugeAdapter.java | 8 +- .../daemon/Deluge/DelugeCommon.java | 349 +++++++++--------- .../daemon/Deluge/DelugeRpcAdapter.java | 23 +- 3 files changed, 194 insertions(+), 186 deletions(-) diff --git a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeAdapter.java b/app/src/main/java/org/transdroid/daemon/Deluge/DelugeAdapter.java index 5eee5d8f..aba430ac 100644 --- a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeAdapter.java +++ b/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_TRACKERS; 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_TRACKER_URL; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIER; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_URL; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER; 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 for (int i = 0; i < trackersTask.getNewTrackers().size(); i++) { JSONObject trackerObj = new JSONObject(); - trackerObj.put(RPC_TRACKER_TIER, i); - trackerObj.put(RPC_TRACKER_URL, trackersTask.getNewTrackers().get(i)); + trackerObj.put(RPC_TIER, i); + trackerObj.put(RPC_URL, trackersTask.getNewTrackers().get(i)); trackers.put(trackerObj); } params.put(new JSONArray().put(task.getTargetTorrent().getUniqueID())); diff --git a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java b/app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java index 3a50e963..613cd8ee 100644 --- a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java +++ b/app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java @@ -28,194 +28,195 @@ import org.transdroid.daemon.TorrentStatus; * @author alon.albert */ 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 = { - RPC_TRACKERS, - RPC_TRACKER_STATUS, - }; - static final String[] RPC_FIELDS_ARRAY = { - RPC_HASH, - RPC_NAME, - RPC_STATUS, - RPC_SAVEPATH, - RPC_RATEDOWNLOAD, - RPC_RATEUPLOAD, - RPC_NUMPEERS, - RPC_NUMSEEDS, - 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 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_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) { - // 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; + static final String[] RPC_DETAILS_FIELDS_ARRAY = { + RPC_TRACKERS, + RPC_TRACKER_STATUS, + }; + static final String[] RPC_FIELDS_ARRAY = { + RPC_HASH, + RPC_NAME, + RPC_STATUS, + RPC_SAVEPATH, + RPC_RATEDOWNLOAD, + RPC_RATEUPLOAD, + RPC_NUMPEERS, + RPC_NUMSEEDS, + 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 - static Priority convertDelugePriority(int priority, int version) { - if (version >= 10303) { - // Priority codes changes from Deluge 1.3.3 onwards - switch (priority) { - case 0: - return Priority.Off; - case 1: - return Priority.Low; - case 7: - return Priority.High; - default: - return Priority.Normal; - } - } else { - switch (priority) { - case 0: - return Priority.Off; - case 2: - return Priority.Normal; - case 5: - return Priority.High; - default: - return Priority.Low; - } + @NonNull + static Priority convertDelugePriority(int priority, int version) { + if (version >= 10303) { + // Priority codes changes from Deluge 1.3.3 onwards + switch (priority) { + case 0: + return Priority.Off; + case 1: + return Priority.Low; + case 7: + return Priority.High; + default: + return Priority.Normal; + } + } else { + switch (priority) { + case 0: + return Priority.Off; + case 2: + return Priority.Normal; + case 5: + return Priority.High; + default: + return Priority.Low; } } + } - static int convertPriority(Priority priority, int version) { - if (version >= 10303) { - // Priority codes changes from Deluge 1.3.3 onwards - switch (priority) { - case Off: - return 0; - case Low: - return 1; - case High: - return 7; - default: - return 5; - } - } else { - switch (priority) { - case Off: - return 0; - case Normal: - return 2; - case High: - return 5; - default: - return 1; - } + static int convertPriority(Priority priority, int version) { + if (version >= 10303) { + // Priority codes changes from Deluge 1.3.3 onwards + switch (priority) { + case Off: + return 0; + case Low: + return 1; + case High: + return 7; + default: + return 5; + } + } else { + switch (priority) { + case Off: + return 0; + case Normal: + return 2; + case High: + return 5; + default: + return 1; } } + } - static int getVersionString(String versionString) { - int version = 0; - final String[] parts = versionString.split("\\."); + static int getVersionString(String versionString) { + int version = 0; + final String[] parts = versionString.split("\\."); - if (parts.length > 0) { - version = Integer.parseInt(parts[0]) * 100 * 100; - if (parts.length > 1) { - version += Integer.parseInt(parts[1]) * 100; - if (parts.length > 2) { - // 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 - String numbers = ""; - for (char c : parts[2].toCharArray()) { - if (Character.isDigit(c)) - // Still a number; add it to the numbers string - { - numbers += Character.toString(c); - } else { - // No longer reading numbers; stop reading - break; - } + if (parts.length > 0) { + version = Integer.parseInt(parts[0]) * 100 * 100; + if (parts.length > 1) { + version += Integer.parseInt(parts[1]) * 100; + if (parts.length > 2) { + // 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 + String numbers = ""; + for (char c : parts[2].toCharArray()) { + if (Character.isDigit(c)) + // Still a number; add it to the numbers string + { + numbers += Character.toString(c); + } else { + // No longer reading numbers; stop reading + break; } - version += Integer.parseInt(numbers); } + version += Integer.parseInt(numbers); } } - return version; } + return version; + } } diff --git a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java b/app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java index a3b38d55..4596a73c 100644 --- a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java +++ b/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_TRACKERS; 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_TRACKER_URL; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIER; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_URL; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER; import android.support.annotation.NonNull; @@ -209,9 +209,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { final List torrents = getTorrents(torrentsStatus.values()); // Check if Label plugin is enabled - //noinspection unchecked - final List methods = (List) client.sendRequest(RPC_METHOD_GET_METHOD_LIST); - final boolean hasLabelPlugin = methods.contains(RPC_METHOD_GET_LABELS); + final boolean hasLabelPlugin = hasMethod(client, RPC_METHOD_GET_LABELS); // Get label list from server //noinspection unchecked @@ -239,7 +237,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { .get(RPC_TRACKERS); final List trackers = new ArrayList<>(); for (Map trackerResponse : trackerResponses) { - trackers.add((String) trackerResponse.get(RPC_TRACKER_URL)); + trackers.add((String) trackerResponse.get(RPC_URL)); } return new GetTorrentDetailsTaskSuccessResult(task, new TorrentDetails( @@ -295,6 +293,9 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @NonNull 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 label = task.getNewLabel() == null ? "" : task.getNewLabel(); client.sendRequest(RPC_METHOD_SETLABEL, torrentId, label); @@ -345,8 +346,8 @@ public class DelugeRpcAdapter implements IDaemonAdapter { final ArrayList newTrackers = task.getNewTrackers(); for (int i = 0, n = newTrackers.size(); i < n; i++) { final Map tracker = new HashMap<>(); - tracker.put(RPC_TRACKER_TIER, i); - tracker.put(RPC_TRACKER_URL, newTrackers.get(i)); + tracker.put(RPC_TIER, i); + tracker.put(RPC_URL, newTrackers.get(i)); trackers.add(tracker); } client.sendRequest(RPC_METHOD_SETTRACKERS, task.getTargetTorrent().getUniqueID(), trackers); @@ -539,6 +540,12 @@ public class DelugeRpcAdapter implements IDaemonAdapter { return new String[]{task.getTargetTorrent().getUniqueID()}; } + private boolean hasMethod(DelugeRpcClient client, String method) throws DaemonException { + //noinspection unchecked + final List methods = (List) client.sendRequest(RPC_METHOD_GET_METHOD_LIST); + return methods.contains(method); + } + /** * Used to count torrents in labels. */