From 339b9580cf408a934bbeddb39243a54aadf91d06 Mon Sep 17 00:00:00 2001 From: orbital-mango <106028940+orbital-mango@users.noreply.github.com> Date: Mon, 5 Sep 2022 00:12:23 +0100 Subject: [PATCH] transmission-edit: Allow changes to torrent source flag (#3760) --- utils/edit.cc | 39 ++++++++++++++++++++++++++++++++++++--- utils/transmission-edit.1 | 3 +++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/utils/edit.cc b/utils/edit.cc index a4258c6a2..3cab0405c 100644 --- a/utils/edit.cc +++ b/utils/edit.cc @@ -28,13 +28,15 @@ struct app_options char const* add = nullptr; char const* deleteme = nullptr; std::array replace; + char const* source = nullptr; bool show_version = false; }; -static auto constexpr Options = std::array{ +static auto constexpr Options = std::array{ { { 'a', "add", "Add a tracker's announce URL", "a", true, "" }, { 'd', "delete", "Delete a tracker's announce URL", "d", true, "" }, { 'r', "replace", "Search and replace a substring in the announce URLs", "r", true, " " }, + { 's', "source", "Set the source", "s", true, "" }, { 'V', "version", "Show version number and exit", "V", false, nullptr }, { 0, nullptr, nullptr, nullptr, false, nullptr } } }; @@ -68,6 +70,10 @@ static int parseCommandLine(app_options& opts, int argc, char const* const* argv opts.replace[1] = optarg; break; + case 's': + opts.source = optarg; + break; + case 'V': opts.show_version = true; break; @@ -296,6 +302,28 @@ static bool addURL(tr_variant* metainfo, char const* url) return changed; } +static bool setSource(tr_variant* metainfo, char const* source_value) +{ + auto current_source = std::string_view{}; + bool const had_source = tr_variantDictFindStrView(metainfo, TR_KEY_source, ¤t_source); + bool changed = false; + + if (!had_source) + { + printf("\tAdded \"%s\" as source\n", source_value); + tr_variantDictAddStr(metainfo, TR_KEY_source, source_value); + changed = true; + } + else if (current_source.compare(source_value) != 0) + { + printf("\tUpdated source: \"%s\" -> \"%s\"\n", current_source.data(), source_value); + tr_variantDictAddStr(metainfo, TR_KEY_source, source_value); + changed = true; + } + + return changed; +} + int tr_main(int argc, char* argv[]) { int changedCount = 0; @@ -322,9 +350,9 @@ int tr_main(int argc, char* argv[]) return EXIT_FAILURE; } - if (options.add == nullptr && options.deleteme == nullptr && options.replace[0] == nullptr) + if (options.add == nullptr && options.deleteme == nullptr && options.replace[0] == nullptr && options.source == nullptr) { - fprintf(stderr, "ERROR: Must specify -a, -d or -r\n"); + fprintf(stderr, "ERROR: Must specify -a, -d, -r or -s\n"); tr_getopt_usage(MyName, Usage, std::data(Options)); fprintf(stderr, "\n"); return EXIT_FAILURE; @@ -360,6 +388,11 @@ int tr_main(int argc, char* argv[]) changed |= replaceURL(&top, options.replace[0], options.replace[1]); } + if (options.source != nullptr) + { + changed = setSource(&top, options.source); + } + if (changed) { ++changedCount; diff --git a/utils/transmission-edit.1 b/utils/transmission-edit.1 index e6a59a378..d3975a521 100644 --- a/utils/transmission-edit.1 +++ b/utils/transmission-edit.1 @@ -11,6 +11,7 @@ .Op Fl a Ar url .Op Fl d Ar url .Op Fl r Ar search Ar replace +.Op Fl s Ar source .Ar torrentfile(s) .Ek .Sh DESCRIPTION @@ -26,6 +27,8 @@ Add an announce URL to the torrent's announce-list if it's not already in the li Remove an announce URL from the torrent's announce-list .It Fl r Fl -replace Ar search Ar replace Substring search-and-replace inside a torrent's announce URLs. This can be used to change an announce URL when the tracker moves or your passcode changes. +.It Fl s Fl -source Ar source +Set the source tag within a torrent .El .Sh EXAMPLES Update a tracker passcode in all your torrents: