2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Transmission authors and contributors.
|
2022-01-20 18:27:56 +00:00
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
#include <array>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <cstdio> /* fprintf () */
|
|
|
|
#include <cstdlib> /* atoi () */
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2023-06-29 13:51:39 +00:00
|
|
|
#include <thread>
|
2007-07-18 23:04:26 +00:00
|
|
|
|
2022-07-27 21:53:39 +00:00
|
|
|
#include <signal.h>
|
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-04-02 19:34:29 +00:00
|
|
|
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2022-01-13 02:13:58 +00:00
|
|
|
|
2014-09-21 17:55:39 +00:00
|
|
|
#include <libtransmission/error.h>
|
2014-07-08 00:08:43 +00:00
|
|
|
#include <libtransmission/file.h>
|
2008-07-08 16:50:34 +00:00
|
|
|
#include <libtransmission/tr-getopt.h>
|
2023-06-29 13:51:39 +00:00
|
|
|
#include <libtransmission/utils.h> // _()
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2009-04-13 19:04:21 +00:00
|
|
|
#include <libtransmission/version.h>
|
2022-02-13 04:16:55 +00:00
|
|
|
#include <libtransmission/web-utils.h>
|
2022-02-16 18:33:50 +00:00
|
|
|
#include <libtransmission/web.h> // tr_sessionFetch()
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2023-01-12 18:03:14 +00:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-12-28 17:23:47 +00:00
|
|
|
static auto constexpr MemK = size_t{ 1024 };
|
|
|
|
static char constexpr MemKStr[] = "KiB";
|
|
|
|
static char constexpr MemMStr[] = "MiB";
|
|
|
|
static char constexpr MemGStr[] = "GiB";
|
|
|
|
static char constexpr MemTStr[] = "TiB";
|
|
|
|
|
|
|
|
static auto constexpr DiskK = size_t{ 1000 };
|
|
|
|
static char constexpr DiskKStr[] = "kB";
|
|
|
|
static char constexpr DiskMStr[] = "MB";
|
|
|
|
static char constexpr DiskGStr[] = "GB";
|
|
|
|
static char constexpr DiskTStr[] = "TB";
|
|
|
|
|
|
|
|
static auto constexpr SpeedK = size_t{ 1000 };
|
2012-02-03 21:21:52 +00:00
|
|
|
#define SPEED_K_STR "kB/s"
|
2021-12-28 17:23:47 +00:00
|
|
|
static char constexpr SpeedKStr[] = SPEED_K_STR;
|
|
|
|
static char constexpr SpeedMStr[] = "MB/s";
|
|
|
|
static char constexpr SpeedGStr[] = "GB/s";
|
|
|
|
static char constexpr SpeedTStr[] = "TB/s";
|
2010-07-03 00:25:22 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-12-28 17:23:47 +00:00
|
|
|
static auto constexpr LineWidth = int{ 80 };
|
2021-12-17 20:48:02 +00:00
|
|
|
|
|
|
|
static char constexpr MyConfigName[] = "transmission";
|
|
|
|
static char constexpr MyReadableName[] = "transmission-cli";
|
|
|
|
static char constexpr Usage
|
|
|
|
[] = "A fast and easy BitTorrent client\n"
|
|
|
|
"\n"
|
|
|
|
"Usage: transmission-cli [options] <file|url|magnet>";
|
2008-01-16 16:47:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool showVersion = false;
|
|
|
|
static bool verify = false;
|
|
|
|
static sig_atomic_t gotsig = false;
|
|
|
|
static sig_atomic_t manualUpdate = false;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
static char const* torrentPath = nullptr;
|
2009-05-15 15:42:13 +00:00
|
|
|
|
2023-04-14 16:47:54 +00:00
|
|
|
static auto constexpr Options = std::array<tr_option, 20>{
|
2021-12-17 20:48:02 +00:00
|
|
|
{ { 'b', "blocklist", "Enable peer blocklists", "b", false, nullptr },
|
|
|
|
{ 'B', "no-blocklist", "Disable peer blocklists", "B", false, nullptr },
|
|
|
|
{ 'd', "downlimit", "Set max download speed in " SPEED_K_STR, "d", true, "<speed>" },
|
|
|
|
{ 'D', "no-downlimit", "Don't limit the download speed", "D", false, nullptr },
|
|
|
|
{ 910, "encryption-required", "Encrypt all peer connections", "er", false, nullptr },
|
|
|
|
{ 911, "encryption-preferred", "Prefer encrypted peer connections", "ep", false, nullptr },
|
|
|
|
{ 912, "encryption-tolerated", "Prefer unencrypted peer connections", "et", false, nullptr },
|
|
|
|
{ 'f', "finish", "Run a script when the torrent finishes", "f", true, "<script>" },
|
|
|
|
{ 'g', "config-dir", "Where to find configuration files", "g", true, "<path>" },
|
|
|
|
{ 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", false, nullptr },
|
|
|
|
{ 'M', "no-portmap", "Disable portmapping", "M", false, nullptr },
|
|
|
|
{ 'p', "port", "Port for incoming peers (Default: " TR_DEFAULT_PEER_PORT_STR ")", "p", true, "<port>" },
|
2022-02-10 21:35:28 +00:00
|
|
|
{ 't',
|
|
|
|
"tos",
|
|
|
|
"Peer socket DSCP / ToS setting (number, or a DSCP string, e.g. 'af11' or 'cs0', default=" TR_DEFAULT_PEER_SOCKET_TOS_STR
|
|
|
|
")",
|
|
|
|
"t",
|
|
|
|
true,
|
|
|
|
"<dscp-or-tos>" },
|
2021-12-17 20:48:02 +00:00
|
|
|
{ 'u', "uplimit", "Set max upload speed in " SPEED_K_STR, "u", true, "<speed>" },
|
|
|
|
{ 'U', "no-uplimit", "Don't limit the upload speed", "U", false, nullptr },
|
|
|
|
{ 'v', "verify", "Verify the specified torrent", "v", false, nullptr },
|
|
|
|
{ 'V', "version", "Show version number and exit", "V", false, nullptr },
|
|
|
|
{ 'w', "download-dir", "Where to save downloaded data", "w", true, "<path>" },
|
2023-04-14 16:47:54 +00:00
|
|
|
{ 500, "sequential-download", "Download pieces sequentially", "seq", false, nullptr },
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
{ 0, nullptr, nullptr, nullptr, false, nullptr } }
|
2008-10-13 22:26:02 +00:00
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int parseCommandLine(tr_variant*, int argc, char const** argv);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void sigHandler(int signal);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2022-04-02 19:34:29 +00:00
|
|
|
static std::string tr_strlratio(double ratio)
|
2007-02-15 20:56:25 +00:00
|
|
|
{
|
2022-04-02 19:34:29 +00:00
|
|
|
if (static_cast<int>(ratio) == TR_RATIO_NA)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-02 19:34:29 +00:00
|
|
|
return _("None");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-04-02 19:34:29 +00:00
|
|
|
|
|
|
|
if (static_cast<int>(ratio) == TR_RATIO_INF)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-02 19:34:29 +00:00
|
|
|
return _("Inf");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-04-02 19:34:29 +00:00
|
|
|
|
|
|
|
if (ratio < 10.0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-02 19:34:29 +00:00
|
|
|
return fmt::format(FMT_STRING("{:.2f}"), ratio);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-04-02 19:34:29 +00:00
|
|
|
|
|
|
|
if (ratio < 100.0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-02 19:34:29 +00:00
|
|
|
return fmt::format(FMT_STRING("{:.1f}"), ratio);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 19:34:29 +00:00
|
|
|
return fmt::format(FMT_STRING("{:.0f}"), ratio);
|
2007-02-15 20:56:25 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool waitingOnWeb;
|
2009-12-02 22:56:57 +00:00
|
|
|
|
2022-02-17 23:35:57 +00:00
|
|
|
static void onTorrentFileDownloaded(tr_web::FetchResponse const& response)
|
2009-12-02 22:56:57 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
auto* ctor = static_cast<tr_ctor*>(response.user_data);
|
|
|
|
tr_ctorSetMetainfo(ctor, std::data(response.body), std::size(response.body), nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
waitingOnWeb = false;
|
2009-12-02 22:56:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 19:34:29 +00:00
|
|
|
static std::string getStatusStr(tr_stat const* st)
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (st->activity == TR_STATUS_CHECK_WAIT)
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
2022-04-02 19:34:29 +00:00
|
|
|
return "Waiting to verify local files";
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
2022-04-02 19:34:29 +00:00
|
|
|
|
|
|
|
if (st->activity == TR_STATUS_CHECK)
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
2022-04-02 19:34:29 +00:00
|
|
|
return fmt::format(
|
|
|
|
FMT_STRING("Verifying local files ({:.2f}%, {:.2f}% valid)"),
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_truncd(100 * st->recheckProgress, 2),
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_truncd(100 * st->percentDone, 2));
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-02 19:34:29 +00:00
|
|
|
if (st->activity == TR_STATUS_DOWNLOAD)
|
|
|
|
{
|
|
|
|
return fmt::format(
|
|
|
|
FMT_STRING("Progress: {:.1f}%, dl from {:d} of {:d} peers ({:s}), ul to {:d} ({:s}) [{:s}]"),
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_truncd(100 * st->percentDone, 1),
|
|
|
|
st->peersSendingToUs,
|
|
|
|
st->peersConnected,
|
2022-04-02 19:34:29 +00:00
|
|
|
tr_formatter_speed_KBps(st->pieceDownloadSpeed_KBps),
|
2021-08-15 09:41:48 +00:00
|
|
|
st->peersGettingFromUs,
|
2022-04-02 19:34:29 +00:00
|
|
|
tr_formatter_speed_KBps(st->pieceUploadSpeed_KBps),
|
|
|
|
tr_strlratio(st->ratio));
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2022-04-02 19:34:29 +00:00
|
|
|
if (st->activity == TR_STATUS_SEED)
|
|
|
|
{
|
|
|
|
return fmt::format(
|
|
|
|
FMT_STRING("Seeding, uploading to {:d} of {:d} peer(s), {:s} [{:s}]"),
|
2021-08-15 09:41:48 +00:00
|
|
|
st->peersGettingFromUs,
|
|
|
|
st->peersConnected,
|
2022-04-02 19:34:29 +00:00
|
|
|
tr_formatter_speed_KBps(st->pieceUploadSpeed_KBps),
|
|
|
|
tr_strlratio(st->ratio));
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
2022-04-02 19:34:29 +00:00
|
|
|
|
|
|
|
return "";
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 00:28:57 +00:00
|
|
|
static std::string getConfigDir(int argc, char const** argv)
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int c;
|
2021-11-16 18:31:29 +00:00
|
|
|
char const* my_optarg;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const ind = tr_optind;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
while ((c = tr_getopt(Usage, argc, argv, std::data(Options), &my_optarg)) != TR_OPT_DONE)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (c == 'g')
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-08-17 00:28:57 +00:00
|
|
|
return my_optarg;
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2009-01-09 19:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_optind = ind;
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2022-08-17 00:28:57 +00:00
|
|
|
return tr_getDefaultConfigDir(MyConfigName);
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_main(int argc, char* argv[])
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2023-07-06 03:05:03 +00:00
|
|
|
auto const init_mgr = tr_lib_init();
|
|
|
|
|
2023-04-27 14:10:33 +00:00
|
|
|
tr_locale_set_global("");
|
|
|
|
|
2021-12-28 17:23:47 +00:00
|
|
|
tr_formatter_mem_init(MemK, MemKStr, MemMStr, MemGStr, MemTStr);
|
|
|
|
tr_formatter_size_init(DiskK, DiskKStr, DiskMStr, DiskGStr, DiskTStr);
|
|
|
|
tr_formatter_speed_init(SpeedK, SpeedKStr, SpeedMStr, SpeedGStr, SpeedTStr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
printf("%s %s\n", MyReadableName, LONG_VERSION_STRING);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* user needs to pass in at least one argument */
|
|
|
|
if (argc < 2)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_getopt_usage(MyReadableName, Usage, std::data(Options));
|
2017-04-19 12:04:45 +00:00
|
|
|
return EXIT_FAILURE;
|
2008-10-13 22:26:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* load the defaults from config file + libtransmission defaults */
|
2022-08-17 00:28:57 +00:00
|
|
|
auto const config_dir = getConfigDir(argc, (char const**)argv);
|
2023-09-08 00:05:16 +00:00
|
|
|
auto settings = tr_sessionLoadSettings(config_dir.c_str(), MyConfigName);
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the command line overrides defaults */
|
2017-04-30 16:25:26 +00:00
|
|
|
if (parseCommandLine(&settings, argc, (char const**)argv) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2010-10-18 03:11:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (showVersion)
|
|
|
|
{
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2010-10-18 03:11:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Check the options for validity */
|
2021-10-06 16:32:17 +00:00
|
|
|
if (torrentPath == nullptr)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "No torrent specified!\n");
|
|
|
|
return EXIT_FAILURE;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2008-06-04 20:04:19 +00:00
|
|
|
|
2021-11-18 00:17:09 +00:00
|
|
|
if (auto sv = std::string_view{}; tr_variantDictFindStrView(&settings, TR_KEY_download_dir, &sv))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2021-11-16 05:20:06 +00:00
|
|
|
auto const sz_download_dir = std::string{ sv };
|
2020-11-01 21:47:57 +00:00
|
|
|
|
2022-05-22 01:17:00 +00:00
|
|
|
if (!tr_sys_path_exists(sz_download_dir))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2021-11-16 05:20:06 +00:00
|
|
|
tr_error* error = nullptr;
|
|
|
|
|
2022-05-22 01:17:00 +00:00
|
|
|
if (!tr_sys_dir_create(sz_download_dir, TR_SYS_DIR_CREATE_PARENTS, 0700, &error))
|
2021-11-16 05:20:06 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to create download directory \"%s\": %s\n", sz_download_dir.c_str(), error->message);
|
|
|
|
tr_error_free(error);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2011-10-25 16:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 13:23:35 +00:00
|
|
|
auto* const h = tr_sessionInit(config_dir.c_str(), false, settings);
|
2022-02-13 04:16:55 +00:00
|
|
|
auto* const ctor = tr_ctorNew(h);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctorSetPaused(ctor, TR_FORCE, false);
|
|
|
|
|
2023-04-20 22:40:34 +00:00
|
|
|
if (tr_sys_path_exists(torrentPath) ? tr_ctorSetMetainfoFromFile(ctor, torrentPath, nullptr) :
|
|
|
|
tr_ctorSetMetainfoFromMagnetLink(ctor, torrentPath, nullptr))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-02-13 04:16:55 +00:00
|
|
|
// all good
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2022-02-13 04:16:55 +00:00
|
|
|
else if (tr_urlIsValid(torrentPath))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-02-13 04:16:55 +00:00
|
|
|
// fetch it
|
2022-02-16 18:33:50 +00:00
|
|
|
tr_sessionFetch(h, { torrentPath, onTorrentFileDownloaded, ctor });
|
2017-04-19 12:04:45 +00:00
|
|
|
waitingOnWeb = true;
|
|
|
|
while (waitingOnWeb)
|
|
|
|
{
|
2023-06-29 13:51:39 +00:00
|
|
|
std::this_thread::sleep_for(1s);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "ERROR: Unrecognized torrent \"%s\".\n", torrentPath);
|
|
|
|
fprintf(stderr, " * If you're trying to create a torrent, use transmission-create.\n");
|
|
|
|
fprintf(stderr, " * If you're trying to see a torrent's info, use transmission-show.\n");
|
|
|
|
tr_sessionClose(h);
|
|
|
|
return EXIT_FAILURE;
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 22:05:17 +00:00
|
|
|
tr_torrent* tor = tr_torrentNew(ctor, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctorFree(ctor);
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tor == nullptr)
|
2008-01-16 16:47:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "Failed opening torrent file `%s'\n", torrentPath);
|
|
|
|
tr_sessionClose(h);
|
|
|
|
return EXIT_FAILURE;
|
2008-01-16 16:47:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
signal(SIGINT, sigHandler);
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
signal(SIGHUP, sigHandler);
|
2008-09-05 15:13:06 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrentStart(tor);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (verify)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
verify = false;
|
2022-02-09 18:02:59 +00:00
|
|
|
tr_torrentVerify(tor);
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (;;)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2022-04-01 19:16:33 +00:00
|
|
|
static auto constexpr messageName = std::array<char const*, 4>{
|
2021-10-06 16:32:17 +00:00
|
|
|
nullptr,
|
2017-04-19 12:04:45 +00:00
|
|
|
"Tracker gave a warning:",
|
|
|
|
"Tracker gave an error:",
|
2021-08-15 09:41:48 +00:00
|
|
|
"Error:",
|
2017-04-19 12:04:45 +00:00
|
|
|
};
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2023-06-29 13:51:39 +00:00
|
|
|
std::this_thread::sleep_for(200ms);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (gotsig)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
gotsig = false;
|
|
|
|
printf("\nStopping torrent...\n");
|
|
|
|
tr_torrentStop(tor);
|
2007-02-13 05:20:52 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (manualUpdate)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
manualUpdate = false;
|
|
|
|
|
|
|
|
if (!tr_torrentCanManualUpdate(tor))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "\nReceived SIGHUP, but can't send a manual update now\n");
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "\nReceived SIGHUP: manual update scheduled\n");
|
|
|
|
tr_torrentManualUpdate(tor);
|
2007-10-19 21:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2022-04-02 19:34:29 +00:00
|
|
|
auto const* const st = tr_torrentStat(tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
if (st->activity == TR_STATUS_STOPPED)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2009-08-05 01:25:36 +00:00
|
|
|
|
2022-04-02 19:34:29 +00:00
|
|
|
auto const status_str = getStatusStr(st);
|
|
|
|
printf("\r%-*s", TR_ARG_TUPLE(LineWidth, status_str.c_str()));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (messageName[st->error])
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\n%s: %s\n", messageName[st->error], st->errorString);
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2023-09-16 13:23:35 +00:00
|
|
|
tr_sessionSaveSettings(h, config_dir.c_str(), settings);
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\n");
|
|
|
|
tr_sessionClose(h);
|
|
|
|
return EXIT_SUCCESS;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-07-08 16:50:34 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int parseCommandLine(tr_variant* d, int argc, char const** argv)
|
2008-07-08 16:50:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int c;
|
2021-11-16 18:31:29 +00:00
|
|
|
char const* my_optarg;
|
2008-07-08 16:50:34 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
while ((c = tr_getopt(Usage, argc, argv, std::data(Options), &my_optarg)) != TR_OPT_DONE)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (c)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'b':
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_blocklist_enabled, true);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'B':
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_blocklist_enabled, false);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'd':
|
2021-11-16 18:31:29 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_speed_limit_down, atoi(my_optarg));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_speed_limit_down_enabled, true);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'D':
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_speed_limit_down_enabled, false);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'f':
|
2021-11-16 18:31:29 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_script_torrent_done_filename, my_optarg);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_script_torrent_done_enabled, true);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'g': /* handled above */
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'm':
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_port_forwarding_enabled, true);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'M':
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_port_forwarding_enabled, false);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'p':
|
2021-11-16 18:31:29 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_port, atoi(my_optarg));
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 't':
|
2022-02-10 21:35:28 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_peer_socket_tos, my_optarg);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'u':
|
2021-11-16 18:31:29 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_speed_limit_up, atoi(my_optarg));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_speed_limit_up_enabled, true);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'U':
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_speed_limit_up_enabled, false);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'v':
|
2012-12-05 17:29:46 +00:00
|
|
|
verify = true;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'V':
|
2012-12-05 17:29:46 +00:00
|
|
|
showVersion = true;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'w':
|
2021-11-16 18:31:29 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_download_dir, my_optarg);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 910:
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_encryption, TR_ENCRYPTION_REQUIRED);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 911:
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_encryption, TR_ENCRYPTION_PREFERRED);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 912:
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_encryption, TR_CLEAR_PREFERRED);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2023-04-14 16:47:54 +00:00
|
|
|
case 500:
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_sequentialDownload, true);
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_OPT_UNK:
|
2021-10-06 16:32:17 +00:00
|
|
|
if (torrentPath == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-16 18:31:29 +00:00
|
|
|
torrentPath = my_optarg;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2012-12-05 17:29:46 +00:00
|
|
|
return 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void sigHandler(int signal)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (signal)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case SIGINT:
|
2012-12-05 17:29:46 +00:00
|
|
|
gotsig = true;
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case SIGHUP:
|
2012-12-05 17:29:46 +00:00
|
|
|
manualUpdate = true;
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-09-05 15:13:06 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
default:
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|