2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
#include <array>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cctype> /* isspace */
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring> /* strcmp */
|
|
|
|
#include <string>
|
2021-10-20 02:30:50 +00:00
|
|
|
#include <string_view>
|
2007-07-18 23:04:26 +00:00
|
|
|
|
2010-12-24 09:11:23 +00:00
|
|
|
#include <event2/buffer.h>
|
2019-03-17 14:37:52 +00:00
|
|
|
#include <event2/util.h>
|
2009-01-24 00:28:41 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <curl/curl.h>
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2014-12-04 19:58:34 +00:00
|
|
|
#include <libtransmission/crypto-utils.h>
|
2014-09-21 17:55:39 +00:00
|
|
|
#include <libtransmission/error.h>
|
|
|
|
#include <libtransmission/file.h>
|
2013-01-25 23:34:20 +00:00
|
|
|
#include <libtransmission/log.h>
|
2008-09-05 14:31:58 +00:00
|
|
|
#include <libtransmission/rpcimpl.h>
|
2008-07-08 14:29:05 +00:00
|
|
|
#include <libtransmission/tr-getopt.h>
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <libtransmission/version.h>
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2021-12-16 02:09:46 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
#define SPEED_K_STR "kB/s"
|
2010-07-03 00:25:22 +00:00
|
|
|
#define MEM_M_STR "MiB"
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
static auto constexpr DefaultPort = int{ TR_DEFAULT_RPC_PORT };
|
|
|
|
static char constexpr DefaultHost[] = "localhost";
|
|
|
|
static char constexpr DefaultUrl[] = TR_DEFAULT_RPC_URL_STR "rpc/";
|
|
|
|
|
|
|
|
static char constexpr MyName[] = "transmission-remote";
|
|
|
|
static char constexpr Usage[] = "transmission-remote " LONG_VERSION_STRING
|
|
|
|
"\n"
|
|
|
|
"A fast and easy BitTorrent client\n"
|
|
|
|
"https://transmissionbt.com/\n"
|
|
|
|
"\n"
|
|
|
|
"Usage: transmission-remote [host] [options]\n"
|
|
|
|
" transmission-remote [port] [options]\n"
|
|
|
|
" transmission-remote [host:port] [options]\n"
|
|
|
|
" transmission-remote [http(s?)://host:port/transmission/] [options]\n"
|
|
|
|
"\n"
|
|
|
|
"See the man page for detailed explanations and many examples.";
|
|
|
|
|
|
|
|
static auto constexpr Arguments = TR_KEY_arguments;
|
|
|
|
|
|
|
|
static auto constexpr MemK = size_t{ 1024 };
|
|
|
|
static char constexpr MemKStr[] = "KiB";
|
|
|
|
static char constexpr MemMStr[] = MEM_M_STR;
|
|
|
|
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 };
|
|
|
|
static auto 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
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Display Utilities
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void etaToString(char* buf, size_t buflen, int64_t eta)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (eta < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_snprintf(buf, buflen, "Unknown");
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (eta < 60)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_snprintf(buf, buflen, "%" PRId64 " sec", eta);
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (eta < (60 * 60))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_snprintf(buf, buflen, "%" PRId64 " min", eta / 60);
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (eta < (60 * 60 * 24))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_snprintf(buf, buflen, "%" PRId64 " hrs", eta / (60 * 60));
|
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_snprintf(buf, buflen, "%" PRId64 " days", eta / (60 * 60 * 24));
|
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static char* tr_strltime(char* buf, int seconds, size_t buflen)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
char b[128];
|
|
|
|
char h[128];
|
|
|
|
char m[128];
|
|
|
|
char s[128];
|
|
|
|
char t[128];
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (seconds < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-04-29 03:01:39 +00:00
|
|
|
seconds = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2021-12-28 17:23:47 +00:00
|
|
|
auto const total_seconds = seconds;
|
|
|
|
auto const days = seconds / 86400;
|
|
|
|
auto const hours = (seconds % 86400) / 3600;
|
|
|
|
auto const minutes = (seconds % 3600) / 60;
|
2012-12-05 17:29:46 +00:00
|
|
|
seconds = (seconds % 3600) % 60;
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(h, sizeof(h), "%d %s", hours, hours == 1 ? "hour" : "hours");
|
|
|
|
tr_snprintf(m, sizeof(m), "%d %s", minutes, minutes == 1 ? "minute" : "minutes");
|
|
|
|
tr_snprintf(s, sizeof(s), "%d %s", seconds, seconds == 1 ? "second" : "seconds");
|
|
|
|
tr_snprintf(t, sizeof(t), "%d %s", total_seconds, total_seconds == 1 ? "second" : "seconds");
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (days != 0)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2021-12-28 17:23:47 +00:00
|
|
|
char d[128];
|
|
|
|
tr_snprintf(d, sizeof(d), "%d %s", days, days == 1 ? "day" : "days");
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (days >= 4 || hours == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_strlcpy(b, d, sizeof(b));
|
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_snprintf(b, sizeof(b), "%s, %s", d, h);
|
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (hours != 0)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (hours >= 4 || minutes == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_strlcpy(b, h, sizeof(b));
|
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_snprintf(b, sizeof(b), "%s, %s", h, m);
|
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (minutes != 0)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (minutes >= 4 || seconds == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_strlcpy(b, m, sizeof(b));
|
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_snprintf(b, sizeof(b), "%s, %s", m, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_strlcpy(b, s, sizeof(b));
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(buf, buflen, "%s (%s)", b, t);
|
2010-04-29 03:01:39 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
static std::string strlpercent(double x)
|
2010-06-22 22:30:58 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
return tr_strpercent(x);
|
2010-06-22 22:30:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
static std::string strlratio2(double ratio)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
return tr_strratio(ratio, "Inf");
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
static std::string strlratio(int64_t numerator, int64_t denominator)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
|
|
|
double ratio;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (denominator != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-07-04 06:07:21 +00:00
|
|
|
ratio = numerator / (double)denominator;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (numerator != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-04-29 03:01:39 +00:00
|
|
|
ratio = TR_RATIO_INF;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-04-29 03:01:39 +00:00
|
|
|
ratio = TR_RATIO_NA;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
return strlratio2(ratio);
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
static std::string strlmem(int64_t bytes)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
return bytes == 0 ? "None"s : tr_formatter_mem_B(bytes);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
static std::string strlsize(int64_t bytes)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (bytes < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
return "Unknown"s;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-12-31 00:30:21 +00:00
|
|
|
|
|
|
|
if (bytes == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
return "None"s;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
return tr_formatter_size_B(bytes);
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
|
2010-07-24 12:30:16 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
TAG_SESSION,
|
|
|
|
TAG_STATS,
|
|
|
|
TAG_DETAILS,
|
|
|
|
TAG_FILES,
|
|
|
|
TAG_LIST,
|
|
|
|
TAG_PEERS,
|
|
|
|
TAG_PIECES,
|
|
|
|
TAG_PORTTEST,
|
|
|
|
TAG_TORRENT_ADD,
|
|
|
|
TAG_TRACKERS
|
|
|
|
};
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2010-04-29 13:35:57 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Command-Line Arguments
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-02-14 04:07:12 +00:00
|
|
|
static auto constexpr Options = std::array<tr_option, 89>{
|
2021-12-17 20:48:02 +00:00
|
|
|
{ { 'a', "add", "Add torrent files by filename or URL", "a", false, nullptr },
|
|
|
|
{ 970, "alt-speed", "Use the alternate Limits", "as", false, nullptr },
|
|
|
|
{ 971, "no-alt-speed", "Don't use the alternate Limits", "AS", false, nullptr },
|
|
|
|
{ 972, "alt-speed-downlimit", "max alternate download speed (in " SPEED_K_STR ")", "asd", true, "<speed>" },
|
|
|
|
{ 973, "alt-speed-uplimit", "max alternate upload speed (in " SPEED_K_STR ")", "asu", true, "<speed>" },
|
|
|
|
{ 974, "alt-speed-scheduler", "Use the scheduled on/off times", "asc", false, nullptr },
|
|
|
|
{ 975, "no-alt-speed-scheduler", "Don't use the scheduled on/off times", "ASC", false, nullptr },
|
|
|
|
{ 976, "alt-speed-time-begin", "Time to start using the alt speed limits (in hhmm)", nullptr, true, "<time>" },
|
|
|
|
{ 977, "alt-speed-time-end", "Time to stop using the alt speed limits (in hhmm)", nullptr, true, "<time>" },
|
|
|
|
{ 978, "alt-speed-days", "Numbers for any/all days of the week - eg. \"1-7\"", nullptr, true, "<days>" },
|
|
|
|
{ 963, "blocklist-update", "Blocklist update", nullptr, false, nullptr },
|
|
|
|
{ 'c', "incomplete-dir", "Where to store new torrents until they're complete", "c", true, "<dir>" },
|
|
|
|
{ 'C', "no-incomplete-dir", "Don't store incomplete torrents in a different location", "C", false, nullptr },
|
|
|
|
{ 'b', "debug", "Print debugging information", "b", false, nullptr },
|
|
|
|
{ 'd',
|
|
|
|
"downlimit",
|
|
|
|
"Set the max download speed in " SPEED_K_STR " for the current torrent(s) or globally",
|
|
|
|
"d",
|
|
|
|
true,
|
|
|
|
"<speed>" },
|
|
|
|
{ 'D', "no-downlimit", "Disable max download speed for the current torrent(s) or globally", "D", false, nullptr },
|
|
|
|
{ 'e', "cache", "Set the maximum size of the session's memory cache (in " MEM_M_STR ")", "e", true, "<size>" },
|
|
|
|
{ 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 },
|
|
|
|
{ 850, "exit", "Tell the transmission session to shut down", nullptr, false, nullptr },
|
|
|
|
{ 940, "files", "List the current torrent(s)' files", "f", false, nullptr },
|
|
|
|
{ 'g', "get", "Mark files for download", "g", true, "<files>" },
|
|
|
|
{ 'G', "no-get", "Mark files for not downloading", "G", true, "<files>" },
|
|
|
|
{ 'i', "info", "Show the current torrent(s)' details", "i", false, nullptr },
|
|
|
|
{ 940, "info-files", "List the current torrent(s)' files", "if", false, nullptr },
|
|
|
|
{ 941, "info-peers", "List the current torrent(s)' peers", "ip", false, nullptr },
|
|
|
|
{ 942, "info-pieces", "List the current torrent(s)' pieces", "ic", false, nullptr },
|
|
|
|
{ 943, "info-trackers", "List the current torrent(s)' trackers", "it", false, nullptr },
|
|
|
|
{ 920, "session-info", "Show the session's details", "si", false, nullptr },
|
|
|
|
{ 921, "session-stats", "Show the session's statistics", "st", false, nullptr },
|
|
|
|
{ 'l', "list", "List all torrents", "l", false, nullptr },
|
|
|
|
{ 'L', "labels", "Set the current torrents' labels", "L", true, "<label[,label...]>" },
|
|
|
|
{ 960, "move", "Move current torrent's data to a new folder", nullptr, true, "<path>" },
|
|
|
|
{ 961, "find", "Tell Transmission where to find a torrent's data", nullptr, true, "<path>" },
|
|
|
|
{ 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", false, nullptr },
|
|
|
|
{ 'M', "no-portmap", "Disable portmapping", "M", false, nullptr },
|
|
|
|
{ 'n', "auth", "Set username and password", "n", true, "<user:pw>" },
|
|
|
|
{ 810, "authenv", "Set authentication info from the TR_AUTH environment variable (user:pw)", "ne", false, nullptr },
|
|
|
|
{ 'N', "netrc", "Set authentication info from a .netrc file", "N", true, "<file>" },
|
|
|
|
{ 820, "ssl", "Use SSL when talking to daemon", nullptr, false, nullptr },
|
|
|
|
{ 'o', "dht", "Enable distributed hash tables (DHT)", "o", false, nullptr },
|
|
|
|
{ 'O', "no-dht", "Disable distributed hash tables (DHT)", "O", false, nullptr },
|
|
|
|
{ 'p', "port", "Port for incoming peers (Default: " TR_DEFAULT_PEER_PORT_STR ")", "p", true, "<port>" },
|
|
|
|
{ 962, "port-test", "Port testing", "pt", false, nullptr },
|
|
|
|
{ 'P', "random-port", "Random port for incoming peers", "P", false, nullptr },
|
|
|
|
{ 900, "priority-high", "Try to download these file(s) first", "ph", true, "<files>" },
|
|
|
|
{ 901, "priority-normal", "Try to download these file(s) normally", "pn", true, "<files>" },
|
|
|
|
{ 902, "priority-low", "Try to download these file(s) last", "pl", true, "<files>" },
|
|
|
|
{ 700, "bandwidth-high", "Give this torrent first chance at available bandwidth", "Bh", false, nullptr },
|
|
|
|
{ 701, "bandwidth-normal", "Give this torrent bandwidth left over by high priority torrents", "Bn", false, nullptr },
|
|
|
|
{ 702,
|
|
|
|
"bandwidth-low",
|
|
|
|
"Give this torrent bandwidth left over by high and normal priority torrents",
|
|
|
|
"Bl",
|
|
|
|
false,
|
|
|
|
nullptr },
|
|
|
|
{ 600, "reannounce", "Reannounce the current torrent(s)", nullptr, false, nullptr },
|
|
|
|
{ 'r', "remove", "Remove the current torrent(s)", "r", false, nullptr },
|
|
|
|
{ 930, "peers", "Set the maximum number of peers for the current torrent(s) or globally", "pr", true, "<max>" },
|
|
|
|
{ 840, "remove-and-delete", "Remove the current torrent(s) and delete local data", "rad", false, nullptr },
|
2022-02-14 04:07:12 +00:00
|
|
|
{ 800, "torrent-done-script", "A script to run when a torrent finishes downloading", nullptr, true, "<file>" },
|
|
|
|
{ 801, "no-torrent-done-script", "Don't run the done-downloading script", nullptr, false, nullptr },
|
|
|
|
{ 802, "torrent-done-seeding-script", "A script to run when a torrent finishes seeding", nullptr, true, "<file>" },
|
|
|
|
{ 803, "no-torrent-done-seeding-script", "Don't run the done-seeding script", nullptr, false, nullptr },
|
2021-12-17 20:48:02 +00:00
|
|
|
{ 950, "seedratio", "Let the current torrent(s) seed until a specific ratio", "sr", true, "ratio" },
|
|
|
|
{ 951, "seedratio-default", "Let the current torrent(s) use the global seedratio settings", "srd", false, nullptr },
|
|
|
|
{ 952, "no-seedratio", "Let the current torrent(s) seed regardless of ratio", "SR", false, nullptr },
|
|
|
|
{ 953,
|
|
|
|
"global-seedratio",
|
|
|
|
"All torrents, unless overridden by a per-torrent setting, should seed until a specific ratio",
|
|
|
|
"gsr",
|
|
|
|
true,
|
|
|
|
"ratio" },
|
|
|
|
{ 954,
|
|
|
|
"no-global-seedratio",
|
|
|
|
"All torrents, unless overridden by a per-torrent setting, should seed regardless of ratio",
|
|
|
|
"GSR",
|
|
|
|
false,
|
|
|
|
nullptr },
|
|
|
|
{ 710, "tracker-add", "Add a tracker to a torrent", "td", true, "<tracker>" },
|
|
|
|
{ 712, "tracker-remove", "Remove a tracker from a torrent", "tr", true, "<trackerId>" },
|
|
|
|
{ 's', "start", "Start the current torrent(s)", "s", false, nullptr },
|
|
|
|
{ 'S', "stop", "Stop the current torrent(s)", "S", false, nullptr },
|
|
|
|
{ 't', "torrent", "Set the current torrent(s)", "t", true, "<torrent>" },
|
|
|
|
{ 990, "start-paused", "Start added torrents paused", nullptr, false, nullptr },
|
|
|
|
{ 991, "no-start-paused", "Start added torrents unpaused", nullptr, false, nullptr },
|
|
|
|
{ 992, "trash-torrent", "Delete torrents after adding", nullptr, false, nullptr },
|
|
|
|
{ 993, "no-trash-torrent", "Do not delete torrents after adding", nullptr, false, nullptr },
|
|
|
|
{ 984, "honor-session", "Make the current torrent(s) honor the session limits", "hl", false, nullptr },
|
|
|
|
{ 985, "no-honor-session", "Make the current torrent(s) not honor the session limits", "HL", false, nullptr },
|
|
|
|
{ 'u',
|
|
|
|
"uplimit",
|
|
|
|
"Set the max upload speed in " SPEED_K_STR " for the current torrent(s) or globally",
|
|
|
|
"u",
|
|
|
|
true,
|
|
|
|
"<speed>" },
|
|
|
|
{ 'U', "no-uplimit", "Disable max upload speed for the current torrent(s) or globally", "U", false, nullptr },
|
|
|
|
{ 830, "utp", "Enable uTP for peer connections", nullptr, false, nullptr },
|
|
|
|
{ 831, "no-utp", "Disable uTP for peer connections", nullptr, false, nullptr },
|
|
|
|
{ 'v', "verify", "Verify the current torrent(s)", "v", false, nullptr },
|
|
|
|
{ 'V', "version", "Show version number and exit", "V", false, nullptr },
|
|
|
|
{ 'w',
|
|
|
|
"download-dir",
|
|
|
|
"When used in conjunction with --add, set the new torrent's download folder. "
|
|
|
|
"Otherwise, set the default download folder",
|
|
|
|
"w",
|
|
|
|
true,
|
|
|
|
"<path>" },
|
|
|
|
{ 'x', "pex", "Enable peer exchange (PEX)", "x", false, nullptr },
|
|
|
|
{ 'X', "no-pex", "Disable peer exchange (PEX)", "X", false, nullptr },
|
|
|
|
{ 'y', "lpd", "Enable local peer discovery (LPD)", "y", false, nullptr },
|
|
|
|
{ 'Y', "no-lpd", "Disable local peer discovery (LPD)", "Y", false, nullptr },
|
|
|
|
{ 941, "peer-info", "List the current torrent(s)' peers", "pi", false, nullptr },
|
|
|
|
{ 0, nullptr, nullptr, nullptr, false, nullptr } }
|
2010-04-29 13:35:57 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void showUsage(void)
|
2010-04-29 13:35:57 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_getopt_usage(MyName, Usage, std::data(Options));
|
2010-04-29 13:35:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int numarg(char const* arg)
|
2010-04-29 13:35:57 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
char* end = nullptr;
|
2017-04-20 16:02:19 +00:00
|
|
|
long const num = strtol(arg, &end, 10);
|
2010-04-29 13:35:57 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (*end != '\0')
|
2010-04-29 13:35:57 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "Not a number: \"%s\"\n", arg);
|
|
|
|
showUsage();
|
|
|
|
exit(EXIT_FAILURE);
|
2010-04-29 13:35:57 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return num;
|
2010-04-29 13:35:57 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
enum
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
MODE_TORRENT_START = (1 << 0),
|
|
|
|
MODE_TORRENT_STOP = (1 << 1),
|
|
|
|
MODE_TORRENT_VERIFY = (1 << 2),
|
|
|
|
MODE_TORRENT_REANNOUNCE = (1 << 3),
|
|
|
|
MODE_TORRENT_SET = (1 << 4),
|
|
|
|
MODE_TORRENT_GET = (1 << 5),
|
|
|
|
MODE_TORRENT_ADD = (1 << 6),
|
|
|
|
MODE_TORRENT_REMOVE = (1 << 7),
|
|
|
|
MODE_TORRENT_SET_LOCATION = (1 << 8),
|
|
|
|
MODE_SESSION_SET = (1 << 9),
|
|
|
|
MODE_SESSION_GET = (1 << 10),
|
|
|
|
MODE_SESSION_STATS = (1 << 11),
|
|
|
|
MODE_SESSION_CLOSE = (1 << 12),
|
|
|
|
MODE_BLOCKLIST_UPDATE = (1 << 13),
|
|
|
|
MODE_PORT_TEST = (1 << 14)
|
2010-04-29 03:01:39 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int getOptMode(int val)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case TR_OPT_ERR:
|
|
|
|
case TR_OPT_UNK:
|
|
|
|
case 'a': /* add torrent */
|
|
|
|
case 'b': /* debug */
|
|
|
|
case 'n': /* auth */
|
|
|
|
case 810: /* authenv */
|
|
|
|
case 'N': /* netrc */
|
|
|
|
case 820: /* UseSSL */
|
|
|
|
case 't': /* set current torrent */
|
|
|
|
case 'V': /* show version number */
|
2012-12-22 20:35:19 +00:00
|
|
|
return 0;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'c': /* incomplete-dir */
|
|
|
|
case 'C': /* no-incomplete-dir */
|
|
|
|
case 'e': /* cache */
|
|
|
|
case 'm': /* portmap */
|
|
|
|
case 'M': /* "no-portmap */
|
|
|
|
case 'o': /* dht */
|
|
|
|
case 'O': /* no-dht */
|
|
|
|
case 'p': /* incoming peer port */
|
|
|
|
case 'P': /* random incoming peer port */
|
|
|
|
case 'x': /* pex */
|
|
|
|
case 'X': /* no-pex */
|
|
|
|
case 'y': /* lpd */
|
|
|
|
case 'Y': /* no-lpd */
|
|
|
|
case 800: /* torrent-done-script */
|
|
|
|
case 801: /* no-torrent-done-script */
|
2022-02-14 04:07:12 +00:00
|
|
|
case 802: /* torrent-done-seeding-script */
|
|
|
|
case 803: /* no-torrent-done-seeding-script */
|
2017-04-19 12:04:45 +00:00
|
|
|
case 830: /* utp */
|
|
|
|
case 831: /* no-utp */
|
|
|
|
case 970: /* alt-speed */
|
|
|
|
case 971: /* no-alt-speed */
|
|
|
|
case 972: /* alt-speed-downlimit */
|
|
|
|
case 973: /* alt-speed-uplimit */
|
|
|
|
case 974: /* alt-speed-scheduler */
|
|
|
|
case 975: /* no-alt-speed-scheduler */
|
|
|
|
case 976: /* alt-speed-time-begin */
|
|
|
|
case 977: /* alt-speed-time-end */
|
|
|
|
case 978: /* alt-speed-days */
|
|
|
|
case 910: /* encryption-required */
|
|
|
|
case 911: /* encryption-preferred */
|
|
|
|
case 912: /* encryption-tolerated */
|
|
|
|
case 953: /* global-seedratio */
|
|
|
|
case 954: /* no-global-seedratio */
|
|
|
|
case 990: /* start-paused */
|
|
|
|
case 991: /* no-start-paused */
|
|
|
|
case 992: /* trash-torrent */
|
|
|
|
case 993: /* no-trash-torrent */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_SESSION_SET;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 712: /* tracker-remove */
|
|
|
|
case 950: /* seedratio */
|
|
|
|
case 951: /* seedratio-default */
|
|
|
|
case 952: /* no-seedratio */
|
|
|
|
case 984: /* honor-session */
|
|
|
|
case 985: /* no-honor-session */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_TORRENT_SET;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 920: /* session-info */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_SESSION_GET;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'g': /* get */
|
|
|
|
case 'G': /* no-get */
|
2022-02-03 06:02:30 +00:00
|
|
|
case 'L': /* labels */
|
2017-04-19 12:04:45 +00:00
|
|
|
case 700: /* torrent priority-high */
|
|
|
|
case 701: /* torrent priority-normal */
|
|
|
|
case 702: /* torrent priority-low */
|
|
|
|
case 710: /* tracker-add */
|
|
|
|
case 900: /* file priority-high */
|
|
|
|
case 901: /* file priority-normal */
|
|
|
|
case 902: /* file priority-low */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_TORRENT_SET | MODE_TORRENT_ADD;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 961: /* find */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_TORRENT_SET_LOCATION | MODE_TORRENT_ADD;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'i': /* info */
|
|
|
|
case 'l': /* list all torrents */
|
|
|
|
case 940: /* info-files */
|
|
|
|
case 941: /* info-peer */
|
|
|
|
case 942: /* info-pieces */
|
|
|
|
case 943: /* info-tracker */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_TORRENT_GET;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'd': /* download speed limit */
|
|
|
|
case 'D': /* no download speed limit */
|
|
|
|
case 'u': /* upload speed limit */
|
|
|
|
case 'U': /* no upload speed limit */
|
|
|
|
case 930: /* peers */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_SESSION_SET | MODE_TORRENT_SET;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 's': /* start */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_TORRENT_START | MODE_TORRENT_ADD;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'S': /* stop */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_TORRENT_STOP | MODE_TORRENT_ADD;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'w': /* download-dir */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_SESSION_SET | MODE_TORRENT_ADD;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 850: /* session-close */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_SESSION_CLOSE;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 963: /* blocklist-update */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_BLOCKLIST_UPDATE;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 921: /* session-stats */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_SESSION_STATS;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'v': /* verify */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_TORRENT_VERIFY;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 600: /* reannounce */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_TORRENT_REANNOUNCE;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 962: /* port-test */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_PORT_TEST;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'r': /* remove */
|
|
|
|
case 840: /* remove and delete */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_TORRENT_REMOVE;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 960: /* move */
|
2012-12-22 20:35:19 +00:00
|
|
|
return MODE_TORRENT_SET_LOCATION;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "unrecognized argument %d\n", val);
|
|
|
|
assert("unrecognized argument" && 0);
|
2012-12-22 20:35:19 +00:00
|
|
|
return 0;
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-14 12:40:41 +00:00
|
|
|
static bool debug = false;
|
2021-10-06 16:32:17 +00:00
|
|
|
static char* auth = nullptr;
|
|
|
|
static char* netrc = nullptr;
|
2021-12-25 02:07:33 +00:00
|
|
|
static char* session_id = nullptr;
|
2011-08-24 19:30:28 +00:00
|
|
|
static bool UseSSL = false;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2022-01-08 12:46:25 +00:00
|
|
|
static std::string getEncodedMetainfo(char const* filename)
|
2008-05-20 17:33:54 +00:00
|
|
|
{
|
2022-02-01 17:30:51 +00:00
|
|
|
auto contents = std::vector<char>{};
|
|
|
|
if (tr_loadFile(contents, filename))
|
2008-08-11 16:04:46 +00:00
|
|
|
{
|
2022-01-08 12:46:25 +00:00
|
|
|
return tr_base64_encode({ std::data(contents), std::size(contents) });
|
2008-08-11 16:04:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-01-08 12:46:25 +00:00
|
|
|
return {};
|
2008-05-20 17:33:54 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 21:07:45 +00:00
|
|
|
static void addIdArg(tr_variant* args, char const* id_str, char const* fallback)
|
2008-07-07 21:23:34 +00:00
|
|
|
{
|
2020-09-11 21:07:45 +00:00
|
|
|
if (tr_str_is_empty(id_str))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2020-09-11 21:07:45 +00:00
|
|
|
id_str = fallback;
|
2013-01-15 18:10:16 +00:00
|
|
|
|
2020-09-11 21:07:45 +00:00
|
|
|
if (tr_str_is_empty(id_str))
|
2013-01-15 18:10:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "No torrent specified! Please use the -t option first.\n");
|
2020-09-11 21:07:45 +00:00
|
|
|
id_str = "-1"; /* no torrent will have this ID, so will act as a no-op */
|
2013-01-15 18:10:16 +00:00
|
|
|
}
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
2013-01-15 18:10:16 +00:00
|
|
|
|
2020-09-11 21:07:45 +00:00
|
|
|
if (tr_strcmp0(id_str, "active") == 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(args, TR_KEY_ids, "recently-active"sv);
|
2013-01-03 05:10:47 +00:00
|
|
|
}
|
2020-09-11 21:07:45 +00:00
|
|
|
else if (strcmp(id_str, "all") != 0)
|
2013-01-03 05:10:47 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
bool isList = strchr(id_str, ',') != nullptr || strchr(id_str, '-') != nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
bool isNum = true;
|
2013-01-03 05:10:47 +00:00
|
|
|
|
2020-09-11 21:07:45 +00:00
|
|
|
for (char const* pch = id_str; isNum && *pch != '\0'; ++pch)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
isNum = isdigit(*pch);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-03 05:10:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (isNum || isList)
|
|
|
|
{
|
2021-10-15 23:34:22 +00:00
|
|
|
tr_rpc_parse_list_str(tr_variantDictAdd(args, TR_KEY_ids), id_str);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-11 21:07:45 +00:00
|
|
|
tr_variantDictAddStr(args, TR_KEY_ids, id_str); /* it's a torrent sha hash */
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-07 21:23:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void addTime(tr_variant* args, tr_quark const key, char const* arg)
|
2009-08-17 20:41:06 +00:00
|
|
|
{
|
2020-09-11 21:07:45 +00:00
|
|
|
int time = 0;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool success = false;
|
2009-08-17 20:41:06 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (arg != nullptr && strlen(arg) == 4)
|
2009-08-17 20:41:06 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const hh[3] = { arg[0], arg[1], '\0' };
|
|
|
|
char const mm[3] = { arg[2], arg[3], '\0' };
|
|
|
|
int const hour = atoi(hh);
|
|
|
|
int const min = atoi(mm);
|
2009-08-17 20:41:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (0 <= hour && hour < 24 && 0 <= min && min < 60)
|
2009-08-17 20:41:06 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
time = min + (hour * 60);
|
2011-03-22 15:19:54 +00:00
|
|
|
success = true;
|
2009-08-17 20:41:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (success)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddInt(args, key, time);
|
|
|
|
}
|
2009-08-17 20:41:06 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Please specify the time of day in 'hhmm' format.\n");
|
|
|
|
}
|
2009-08-17 20:41:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void addDays(tr_variant* args, tr_quark const key, char const* arg)
|
2009-08-17 20:41:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int days = 0;
|
2009-08-17 20:41:06 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (arg != nullptr)
|
2009-08-17 20:41:06 +00:00
|
|
|
{
|
2021-10-15 22:15:33 +00:00
|
|
|
for (int& day : tr_parseNumberRange(arg))
|
2009-08-17 20:41:06 +00:00
|
|
|
{
|
2021-09-25 19:37:28 +00:00
|
|
|
if (day < 0 || day > 7)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2021-09-25 19:37:28 +00:00
|
|
|
if (day == 7)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-25 19:37:28 +00:00
|
|
|
day = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-08-17 20:41:06 +00:00
|
|
|
|
2021-09-25 19:37:28 +00:00
|
|
|
days |= 1 << day;
|
2009-08-17 20:41:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (days != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddInt(args, key, days);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Please specify the days of the week in '1-3,4,7' format.\n");
|
|
|
|
}
|
2009-08-17 20:41:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 00:13:47 +00:00
|
|
|
static void addLabels(tr_variant* args, std::string_view comma_delimited_labels)
|
2019-02-17 09:33:57 +00:00
|
|
|
{
|
|
|
|
tr_variant* labels;
|
|
|
|
if (!tr_variantDictFindList(args, TR_KEY_labels, &labels))
|
|
|
|
{
|
|
|
|
labels = tr_variantDictAddList(args, TR_KEY_labels, 10);
|
|
|
|
}
|
|
|
|
|
2021-11-10 00:13:47 +00:00
|
|
|
auto label = std::string_view{};
|
|
|
|
while (tr_strvSep(&comma_delimited_labels, &label, ','))
|
2019-02-17 09:33:57 +00:00
|
|
|
{
|
2021-11-10 00:13:47 +00:00
|
|
|
tr_variantListAddStr(labels, label);
|
2019-02-17 09:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void addFiles(tr_variant* args, tr_quark const key, char const* arg)
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* files = tr_variantDictAddList(args, key, 100);
|
2008-07-10 20:59:15 +00:00
|
|
|
|
2019-07-13 08:52:44 +00:00
|
|
|
if (tr_str_is_empty(arg))
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "No files specified!\n");
|
|
|
|
arg = "-1"; /* no file will have this index, so should be a no-op */
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (strcmp(arg, "all") != 0)
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
2021-10-15 22:15:33 +00:00
|
|
|
for (auto const& idx : tr_parseNumberRange(arg))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-25 19:37:28 +00:00
|
|
|
tr_variantListAddInt(files, idx);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static tr_quark const files_keys[] = {
|
2012-12-22 20:35:19 +00:00
|
|
|
TR_KEY_files,
|
|
|
|
TR_KEY_name,
|
|
|
|
TR_KEY_priorities,
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_KEY_wanted,
|
2008-07-26 14:47:07 +00:00
|
|
|
};
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static tr_quark const details_keys[] = {
|
2012-12-22 20:35:19 +00:00
|
|
|
TR_KEY_activityDate,
|
|
|
|
TR_KEY_addedDate,
|
|
|
|
TR_KEY_bandwidthPriority,
|
|
|
|
TR_KEY_comment,
|
|
|
|
TR_KEY_corruptEver,
|
|
|
|
TR_KEY_creator,
|
|
|
|
TR_KEY_dateCreated,
|
|
|
|
TR_KEY_desiredAvailable,
|
|
|
|
TR_KEY_doneDate,
|
|
|
|
TR_KEY_downloadDir,
|
|
|
|
TR_KEY_downloadedEver,
|
|
|
|
TR_KEY_downloadLimit,
|
|
|
|
TR_KEY_downloadLimited,
|
|
|
|
TR_KEY_error,
|
|
|
|
TR_KEY_errorString,
|
|
|
|
TR_KEY_eta,
|
|
|
|
TR_KEY_hashString,
|
|
|
|
TR_KEY_haveUnchecked,
|
|
|
|
TR_KEY_haveValid,
|
|
|
|
TR_KEY_honorsSessionLimits,
|
|
|
|
TR_KEY_id,
|
|
|
|
TR_KEY_isFinished,
|
|
|
|
TR_KEY_isPrivate,
|
2019-02-17 09:33:57 +00:00
|
|
|
TR_KEY_labels,
|
2012-12-22 20:35:19 +00:00
|
|
|
TR_KEY_leftUntilDone,
|
|
|
|
TR_KEY_magnetLink,
|
|
|
|
TR_KEY_name,
|
|
|
|
TR_KEY_peersConnected,
|
|
|
|
TR_KEY_peersGettingFromUs,
|
|
|
|
TR_KEY_peersSendingToUs,
|
|
|
|
TR_KEY_peer_limit,
|
|
|
|
TR_KEY_pieceCount,
|
|
|
|
TR_KEY_pieceSize,
|
|
|
|
TR_KEY_rateDownload,
|
|
|
|
TR_KEY_rateUpload,
|
|
|
|
TR_KEY_recheckProgress,
|
|
|
|
TR_KEY_secondsDownloading,
|
|
|
|
TR_KEY_secondsSeeding,
|
|
|
|
TR_KEY_seedRatioMode,
|
|
|
|
TR_KEY_seedRatioLimit,
|
|
|
|
TR_KEY_sizeWhenDone,
|
2021-10-18 23:05:39 +00:00
|
|
|
TR_KEY_source,
|
2012-12-22 20:35:19 +00:00
|
|
|
TR_KEY_startDate,
|
|
|
|
TR_KEY_status,
|
|
|
|
TR_KEY_totalSize,
|
|
|
|
TR_KEY_uploadedEver,
|
|
|
|
TR_KEY_uploadLimit,
|
|
|
|
TR_KEY_uploadLimited,
|
|
|
|
TR_KEY_webseeds,
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_KEY_webseedsSendingToUs,
|
2008-07-26 14:47:07 +00:00
|
|
|
};
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static tr_quark const list_keys[] = {
|
|
|
|
TR_KEY_error, //
|
|
|
|
TR_KEY_errorString, //
|
|
|
|
TR_KEY_eta, //
|
|
|
|
TR_KEY_id, //
|
|
|
|
TR_KEY_isFinished, //
|
|
|
|
TR_KEY_leftUntilDone, //
|
|
|
|
TR_KEY_name, //
|
|
|
|
TR_KEY_peersGettingFromUs, //
|
|
|
|
TR_KEY_peersSendingToUs, //
|
|
|
|
TR_KEY_rateDownload, //
|
|
|
|
TR_KEY_rateUpload, //
|
|
|
|
TR_KEY_sizeWhenDone, //
|
|
|
|
TR_KEY_status, //
|
|
|
|
TR_KEY_uploadRatio, //
|
2008-07-26 14:47:07 +00:00
|
|
|
};
|
|
|
|
|
2021-09-21 23:03:39 +00:00
|
|
|
static size_t writeFunc(void* ptr, size_t size, size_t nmemb, void* vbuf)
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2021-09-21 23:03:39 +00:00
|
|
|
auto* const buf = static_cast<evbuffer*>(vbuf);
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const byteCount = size * nmemb;
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(buf, ptr, byteCount);
|
2010-04-29 03:01:39 +00:00
|
|
|
return byteCount;
|
|
|
|
}
|
2008-07-07 21:23:34 +00:00
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
/* look for a session id in the header in case the server gives back a 409 */
|
2021-10-24 16:41:54 +00:00
|
|
|
static size_t parseResponseHeader(void* ptr, size_t size, size_t nmemb, void* /*stream*/)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2021-09-21 23:03:39 +00:00
|
|
|
auto const* const line = static_cast<char const*>(ptr);
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const line_len = size * nmemb;
|
|
|
|
char const* key = TR_RPC_SESSION_ID_HEADER ": ";
|
|
|
|
size_t const key_len = strlen(key);
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2019-03-17 14:37:52 +00:00
|
|
|
if (line_len >= key_len && evutil_ascii_strncasecmp(line, key, key_len) == 0)
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* begin = line + key_len;
|
|
|
|
char const* end = begin;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
while (!isspace(*end))
|
|
|
|
{
|
2010-04-29 03:01:39 +00:00
|
|
|
++end;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 02:07:33 +00:00
|
|
|
session_id = tr_strvDup(std::string_view{ begin, size_t(end - begin) });
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
return line_len;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
static long getTimeoutSecs(std::string_view req)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2021-12-27 22:47:25 +00:00
|
|
|
if (req.find("\"method\":\"blocklist-update\""sv) != std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return 300L;
|
|
|
|
}
|
2009-10-19 05:34:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 60L; /* default value */
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
2009-10-19 05:34:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static char* getStatusString(tr_variant* t, char* buf, size_t buflen)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
|
|
|
int64_t status;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool boolVal;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_variantDictFindInt(t, TR_KEY_status, &status))
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
|
|
|
*buf = '\0';
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (status)
|
|
|
|
{
|
2011-08-13 01:15:35 +00:00
|
|
|
case TR_STATUS_DOWNLOAD_WAIT:
|
|
|
|
case TR_STATUS_SEED_WAIT:
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlcpy(buf, "Queued", buflen);
|
2011-08-13 01:15:35 +00:00
|
|
|
break;
|
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
case TR_STATUS_STOPPED:
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(t, TR_KEY_isFinished, &boolVal) && boolVal)
|
|
|
|
{
|
|
|
|
tr_strlcpy(buf, "Finished", buflen);
|
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_strlcpy(buf, "Stopped", buflen);
|
|
|
|
}
|
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
case TR_STATUS_CHECK_WAIT:
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_STATUS_CHECK:
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str = status == TR_STATUS_CHECK_WAIT ? "Will Verify" : "Verifying";
|
2017-04-19 12:04:45 +00:00
|
|
|
double percent;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindReal(t, TR_KEY_recheckProgress, &percent))
|
|
|
|
{
|
2020-09-11 21:07:45 +00:00
|
|
|
tr_snprintf(buf, buflen, "%s (%.0f%%)", str, floor(percent * 100.0));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_strlcpy(buf, str, buflen);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
case TR_STATUS_DOWNLOAD:
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_STATUS_SEED:
|
|
|
|
{
|
|
|
|
int64_t fromUs = 0;
|
|
|
|
int64_t toUs = 0;
|
|
|
|
tr_variantDictFindInt(t, TR_KEY_peersGettingFromUs, &fromUs);
|
|
|
|
tr_variantDictFindInt(t, TR_KEY_peersSendingToUs, &toUs);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (fromUs != 0 && toUs != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_strlcpy(buf, "Up & Down", buflen);
|
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (toUs != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_strlcpy(buf, "Downloading", buflen);
|
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (fromUs != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
int64_t leftUntilDone = 0;
|
|
|
|
tr_variantDictFindInt(t, TR_KEY_leftUntilDone, &leftUntilDone);
|
|
|
|
|
|
|
|
if (leftUntilDone > 0)
|
|
|
|
{
|
|
|
|
tr_strlcpy(buf, "Uploading", buflen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_strlcpy(buf, "Seeding", buflen);
|
|
|
|
}
|
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_strlcpy(buf, "Idle", buflen);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
2011-10-25 20:48:39 +00:00
|
|
|
|
|
|
|
default:
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlcpy(buf, "Unknown", buflen);
|
2011-10-25 20:48:39 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
return buf;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* bandwidthPriorityNames[] = {
|
2017-04-19 12:04:45 +00:00
|
|
|
"Low",
|
|
|
|
"Normal",
|
|
|
|
"High",
|
2021-08-15 09:41:48 +00:00
|
|
|
"Invalid",
|
2017-04-19 12:04:45 +00:00
|
|
|
};
|
2010-06-26 20:48:52 +00:00
|
|
|
|
2020-09-11 21:07:45 +00:00
|
|
|
static char* format_date(char* buf, size_t buflen, time_t now)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
tr_localtime_r(&now, &tm);
|
2021-12-09 20:30:39 +00:00
|
|
|
strftime(buf, buflen, "%a %b %d %T %Y%n", &tm); /* ctime equiv */
|
2020-09-11 21:07:45 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void printDetails(tr_variant* top)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_variant* args;
|
|
|
|
tr_variant* torrents;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindDict(top, TR_KEY_arguments, &args) && tr_variantDictFindList(args, TR_KEY_torrents, &torrents))
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int ti = 0, tCount = tr_variantListSize(torrents); ti < tCount; ++ti)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* t = tr_variantListChild(torrents, ti);
|
|
|
|
tr_variant* l;
|
|
|
|
char buf[512];
|
2017-05-01 15:46:41 +00:00
|
|
|
int64_t i;
|
|
|
|
int64_t j;
|
|
|
|
int64_t k;
|
2017-04-19 12:04:45 +00:00
|
|
|
bool boolVal;
|
|
|
|
double d;
|
2021-11-14 17:11:29 +00:00
|
|
|
auto sv = std::string_view{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
printf("NAME\n");
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_id, &i))
|
|
|
|
{
|
|
|
|
printf(" Id: %" PRId64 "\n", i);
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(t, TR_KEY_name, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Name: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(t, TR_KEY_hashString, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Hash: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(t, TR_KEY_magnetLink, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Magnet: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2019-02-17 09:33:57 +00:00
|
|
|
if (tr_variantDictFindList(t, TR_KEY_labels, &l))
|
|
|
|
{
|
2022-02-10 16:24:13 +00:00
|
|
|
printf(" Labels: ");
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
size_t child_pos = 0;
|
2020-11-09 03:31:02 +00:00
|
|
|
tr_variant const* child;
|
2020-08-11 18:11:55 +00:00
|
|
|
while ((child = tr_variantListChild(l, child_pos++)))
|
2019-02-17 09:33:57 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantGetStrView(child, &sv))
|
2019-02-17 09:33:57 +00:00
|
|
|
{
|
2022-02-10 16:24:13 +00:00
|
|
|
printf(child_pos == 1 ? "%" TR_PRIsv : ", %" TR_PRIsv, TR_PRIsv_ARG(sv));
|
2019-02-17 09:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("TRANSFER\n");
|
|
|
|
getStatusString(t, buf, sizeof(buf));
|
|
|
|
printf(" State: %s\n", buf);
|
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(t, TR_KEY_downloadDir, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Location: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_sizeWhenDone, &i) && tr_variantDictFindInt(t, TR_KEY_leftUntilDone, &j))
|
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Percent Done: %s%%\n", strlpercent(100.0 * (i - j) / i).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_eta, &i))
|
|
|
|
{
|
|
|
|
printf(" ETA: %s\n", tr_strltime(buf, i, sizeof(buf)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_rateDownload, &i))
|
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Download Speed: %s\n", tr_formatter_speed_KBps(i / (double)tr_speed_K).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_rateUpload, &i))
|
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Upload Speed: %s\n", tr_formatter_speed_KBps(i / (double)tr_speed_K).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_haveUnchecked, &i) && tr_variantDictFindInt(t, TR_KEY_haveValid, &j))
|
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Have: %s (%s verified)\n", strlsize(i + j).c_str(), strlsize(j).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_sizeWhenDone, &i))
|
2008-07-07 05:53:15 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (i < 1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf(" Availability: None\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_desiredAvailable, &j) && tr_variantDictFindInt(t, TR_KEY_leftUntilDone, &k))
|
2010-04-20 01:48:40 +00:00
|
|
|
{
|
|
|
|
j += i - k;
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Availability: %s%%\n", strlpercent(100.0 * j / i).c_str());
|
2010-04-20 01:48:40 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_totalSize, &j))
|
2010-04-20 01:48:40 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Total size: %s (%s wanted)\n", strlsize(j).c_str(), strlsize(i).c_str());
|
2010-04-20 01:48:40 +00:00
|
|
|
}
|
2008-07-07 05:53:15 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_downloadedEver, &i) && tr_variantDictFindInt(t, TR_KEY_uploadedEver, &j))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-02-11 07:15:10 +00:00
|
|
|
if (auto corrupt = int64_t{}; tr_variantDictFindInt(t, TR_KEY_corruptEver, &corrupt) && corrupt != 0)
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
" Downloaded: %s (+%s discarded after failed checksum)\n",
|
|
|
|
strlsize(i).c_str(),
|
|
|
|
strlsize(corrupt).c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf(" Downloaded: %s\n", strlsize(i).c_str());
|
|
|
|
}
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Uploaded: %s\n", strlsize(j).c_str());
|
|
|
|
printf(" Ratio: %s\n", strlratio(j, i).c_str());
|
2008-07-07 05:53:15 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(t, TR_KEY_errorString, &sv) && !std::empty(sv) &&
|
2017-04-30 16:25:26 +00:00
|
|
|
tr_variantDictFindInt(t, TR_KEY_error, &i) && i != 0)
|
2009-08-05 01:25:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (i)
|
|
|
|
{
|
|
|
|
case TR_STAT_TRACKER_WARNING:
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Tracker gave a warning: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STAT_TRACKER_ERROR:
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Tracker gave an error: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STAT_LOCAL_ERROR:
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Error: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break; /* no error */
|
2009-08-05 01:25:36 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_peersConnected, &i) &&
|
|
|
|
tr_variantDictFindInt(t, TR_KEY_peersGettingFromUs, &j) &&
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictFindInt(t, TR_KEY_peersSendingToUs, &k))
|
2008-07-27 14:29:43 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printf(" Peers: connected to %" PRId64 ", uploading to %" PRId64 ", downloading from %" PRId64 "\n", i, j, k);
|
2008-07-27 14:29:43 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindList(t, TR_KEY_webseeds, &l) && tr_variantDictFindInt(t, TR_KEY_webseedsSendingToUs, &i))
|
2008-07-27 14:29:43 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int64_t const n = tr_variantListSize(l);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (n > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf(" Web Seeds: downloading from %" PRId64 " of %" PRId64 " web seeds\n", i, n);
|
|
|
|
}
|
2008-07-27 14:29:43 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("HISTORY\n");
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_addedDate, &i) && i != 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2020-09-11 21:07:45 +00:00
|
|
|
printf(" Date added: %s", format_date(buf, sizeof(buf), i));
|
2008-07-07 05:53:15 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_doneDate, &i) && i != 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2020-09-11 21:07:45 +00:00
|
|
|
printf(" Date finished: %s", format_date(buf, sizeof(buf), i));
|
2008-07-07 05:53:15 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_startDate, &i) && i != 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2020-09-11 21:07:45 +00:00
|
|
|
printf(" Date started: %s", format_date(buf, sizeof(buf), i));
|
2008-07-07 05:53:15 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_activityDate, &i) && i != 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2020-09-11 21:07:45 +00:00
|
|
|
printf(" Latest activity: %s", format_date(buf, sizeof(buf), i));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_secondsDownloading, &i) && i > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf(" Downloading Time: %s\n", tr_strltime(buf, i, sizeof(buf)));
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_secondsSeeding, &i) && i > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf(" Seeding Time: %s\n", tr_strltime(buf, i, sizeof(buf)));
|
2008-07-07 05:53:15 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("ORIGINS\n");
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_dateCreated, &i) && i != 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2020-09-11 21:07:45 +00:00
|
|
|
printf(" Date created: %s", format_date(buf, sizeof(buf), i));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(t, TR_KEY_isPrivate, &boolVal))
|
|
|
|
{
|
|
|
|
printf(" Public torrent: %s\n", (boolVal ? "No" : "Yes"));
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(t, TR_KEY_comment, &sv) && !std::empty(sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Comment: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(t, TR_KEY_creator, &sv) && !std::empty(sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Creator: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(t, TR_KEY_source, &sv) && !std::empty(sv))
|
2021-10-18 23:05:39 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Source: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2021-10-18 23:05:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_pieceCount, &i))
|
|
|
|
{
|
|
|
|
printf(" Piece Count: %" PRId64 "\n", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_pieceSize, &i))
|
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Piece Size: %s\n", strlmem(i).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("LIMITS & BANDWIDTH\n");
|
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
if (tr_variantDictFindBool(t, TR_KEY_downloadLimited, &boolVal) &&
|
|
|
|
tr_variantDictFindInt(t, TR_KEY_downloadLimit, &i))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf(" Download Limit: ");
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (boolVal)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
printf("%s\n", tr_formatter_speed_KBps(i).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-10-27 21:30:00 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf("Unlimited\n");
|
|
|
|
}
|
2009-10-27 21:30:00 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (tr_variantDictFindBool(t, TR_KEY_uploadLimited, &boolVal) && tr_variantDictFindInt(t, TR_KEY_uploadLimit, &i))
|
2009-10-27 21:30:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printf(" Upload Limit: ");
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (boolVal)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
printf("%s\n", tr_formatter_speed_KBps(i).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-10-27 21:30:00 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf("Unlimited\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_seedRatioMode, &i))
|
|
|
|
{
|
|
|
|
switch (i)
|
|
|
|
{
|
|
|
|
case TR_RATIOLIMIT_GLOBAL:
|
|
|
|
printf(" Ratio Limit: Default\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_RATIOLIMIT_SINGLE:
|
|
|
|
if (tr_variantDictFindReal(t, TR_KEY_seedRatioLimit, &d))
|
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Ratio Limit: %s\n", strlratio2(d).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_RATIOLIMIT_UNLIMITED:
|
|
|
|
printf(" Ratio Limit: Unlimited\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(t, TR_KEY_honorsSessionLimits, &boolVal))
|
|
|
|
{
|
|
|
|
printf(" Honors Session Limits: %s\n", (boolVal ? "Yes" : "No"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_peer_limit, &i))
|
|
|
|
{
|
|
|
|
printf(" Peer limit: %" PRId64 "\n", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_bandwidthPriority, &i))
|
|
|
|
{
|
|
|
|
printf(" Bandwidth Priority: %s\n", bandwidthPriorityNames[(i + 1) & 3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
2008-07-07 05:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void printFileList(tr_variant* top)
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_variant* args;
|
|
|
|
tr_variant* torrents;
|
2008-07-07 03:38:22 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindDict(top, TR_KEY_arguments, &args) && tr_variantDictFindList(args, TR_KEY_torrents, &torrents))
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0, in = tr_variantListSize(torrents); i < in; ++i)
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* d = tr_variantListChild(torrents, i);
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_variant* files;
|
|
|
|
tr_variant* priorities;
|
|
|
|
tr_variant* wanteds;
|
2021-11-14 17:11:29 +00:00
|
|
|
auto name = std::string_view{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(d, TR_KEY_name, &name) && tr_variantDictFindList(d, TR_KEY_files, &files) &&
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictFindList(d, TR_KEY_priorities, &priorities) && tr_variantDictFindList(d, TR_KEY_wanted, &wanteds))
|
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
int const jn = tr_variantListSize(files);
|
2021-11-14 17:11:29 +00:00
|
|
|
printf("%" TR_PRIsv " (%d files):\n", TR_PRIsv_ARG(name), jn);
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("%3s %4s %8s %3s %9s %s\n", "#", "Done", "Priority", "Get", "Size", "Name");
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int j = 0; j < jn; ++j)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
int64_t have;
|
|
|
|
int64_t length;
|
|
|
|
int64_t priority;
|
2019-07-14 12:40:41 +00:00
|
|
|
bool wanted;
|
2021-11-14 17:11:29 +00:00
|
|
|
auto filename = std::string_view{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* file = tr_variantListChild(files, j);
|
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
if (tr_variantDictFindInt(file, TR_KEY_length, &length) &&
|
2021-11-14 17:11:29 +00:00
|
|
|
tr_variantDictFindStrView(file, TR_KEY_name, &filename) &&
|
2019-02-10 11:05:16 +00:00
|
|
|
tr_variantDictFindInt(file, TR_KEY_bytesCompleted, &have) &&
|
|
|
|
tr_variantGetInt(tr_variantListChild(priorities, j), &priority) &&
|
2019-07-14 12:40:41 +00:00
|
|
|
tr_variantGetBool(tr_variantListChild(wanteds, j), &wanted))
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
double percent = (double)have / length;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* pristr;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (priority)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_PRI_LOW:
|
|
|
|
pristr = "Low";
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_PRI_HIGH:
|
|
|
|
pristr = "High";
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
|
|
|
pristr = "Normal";
|
|
|
|
break;
|
2008-07-07 03:38:22 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
printf(
|
2021-11-14 17:11:29 +00:00
|
|
|
"%3d: %3.0f%% %-8s %-3s %9s %" TR_PRIsv "\n",
|
2021-08-15 09:41:48 +00:00
|
|
|
j,
|
|
|
|
floor(100.0 * percent),
|
|
|
|
pristr,
|
|
|
|
wanted ? "Yes" : "No",
|
2021-12-31 00:30:21 +00:00
|
|
|
strlsize(length).c_str(),
|
2021-11-14 17:11:29 +00:00
|
|
|
TR_PRIsv_ARG(filename));
|
2008-07-07 03:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void printPeersImpl(tr_variant* peers)
|
2008-08-22 17:59:31 +00:00
|
|
|
{
|
2017-04-16 23:42:19 +00:00
|
|
|
printf("%-40s %-12s %-5s %-6s %-6s %s\n", "Address", "Flags", "Done", "Down", "Up", "Client");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0, n = tr_variantListSize(peers); i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
auto address = std::string_view{};
|
|
|
|
auto client = std::string_view{};
|
|
|
|
auto flagstr = std::string_view{};
|
|
|
|
auto progress = double{};
|
|
|
|
auto rateToClient = int64_t{};
|
|
|
|
auto rateToPeer = int64_t{};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* d = tr_variantListChild(peers, i);
|
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(d, TR_KEY_address, &address) &&
|
|
|
|
tr_variantDictFindStrView(d, TR_KEY_clientName, &client) && tr_variantDictFindReal(d, TR_KEY_progress, &progress) &&
|
|
|
|
tr_variantDictFindStrView(d, TR_KEY_flagStr, &flagstr) &&
|
2019-02-10 11:05:16 +00:00
|
|
|
tr_variantDictFindInt(d, TR_KEY_rateToClient, &rateToClient) &&
|
|
|
|
tr_variantDictFindInt(d, TR_KEY_rateToPeer, &rateToPeer))
|
2008-11-04 17:04:41 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
printf(
|
2021-11-14 17:11:29 +00:00
|
|
|
"%-40s %-12s %-5.1f %6.1f %6.1f %" TR_PRIsv "\n",
|
|
|
|
std::string{ address }.c_str(),
|
|
|
|
std::string{ flagstr }.c_str(),
|
2021-08-15 09:41:48 +00:00
|
|
|
(progress * 100.0),
|
|
|
|
rateToClient / (double)tr_speed_K,
|
|
|
|
rateToPeer / (double)tr_speed_K,
|
2021-11-14 17:11:29 +00:00
|
|
|
TR_PRIsv_ARG(client));
|
2008-11-04 17:04:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-22 17:59:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void printPeers(tr_variant* top)
|
2008-11-04 17:04:41 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_variant* args;
|
|
|
|
tr_variant* torrents;
|
2008-11-04 17:04:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindDict(top, TR_KEY_arguments, &args) && tr_variantDictFindList(args, TR_KEY_torrents, &torrents))
|
2008-08-22 17:59:31 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0, n = tr_variantListSize(torrents); i < n; ++i)
|
2008-08-22 17:59:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* peers;
|
|
|
|
tr_variant* torrent = tr_variantListChild(torrents, i);
|
|
|
|
|
|
|
|
if (tr_variantDictFindList(torrent, TR_KEY_peers, &peers))
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printPeersImpl(peers);
|
|
|
|
|
|
|
|
if (i + 1 < n)
|
|
|
|
{
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
2008-08-22 17:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 12:46:25 +00:00
|
|
|
static void printPiecesImpl(std::string_view raw, size_t piece_count)
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2022-01-08 12:46:25 +00:00
|
|
|
auto const str = tr_base64_decode(raw);
|
2017-04-19 12:04:45 +00:00
|
|
|
printf(" ");
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
size_t piece = 0;
|
|
|
|
size_t const col_width = 64;
|
2022-01-08 12:46:25 +00:00
|
|
|
for (auto const ch : str)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-08 19:54:40 +00:00
|
|
|
for (int bit = 0; piece < piece_count && bit < 8; ++bit, ++piece)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-08 12:46:25 +00:00
|
|
|
printf("%c", (ch & (1 << (7 - bit))) != 0 ? '1' : '0');
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf(" ");
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
if (piece % col_width == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf("\n ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
2010-07-24 12:30:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void printPieces(tr_variant* top)
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_variant* args;
|
|
|
|
tr_variant* torrents;
|
2010-07-24 12:30:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindDict(top, TR_KEY_arguments, &args) && tr_variantDictFindList(args, TR_KEY_torrents, &torrents))
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0, n = tr_variantListSize(torrents); i < n; ++i)
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
|
|
|
int64_t j;
|
2022-01-08 12:46:25 +00:00
|
|
|
auto raw = std::string_view{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* torrent = tr_variantListChild(torrents, i);
|
|
|
|
|
2022-01-08 12:46:25 +00:00
|
|
|
if (tr_variantDictFindStrView(torrent, TR_KEY_pieces, &raw) &&
|
2019-02-10 11:05:16 +00:00
|
|
|
tr_variantDictFindInt(torrent, TR_KEY_pieceCount, &j))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
assert(j >= 0);
|
2022-01-08 12:46:25 +00:00
|
|
|
printPiecesImpl(raw, (size_t)j);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (i + 1 < n)
|
|
|
|
{
|
|
|
|
printf("\n");
|
|
|
|
}
|
2010-07-24 12:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void printPortTest(tr_variant* top)
|
2010-01-29 16:43:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* args;
|
|
|
|
|
2019-03-17 06:18:27 +00:00
|
|
|
if (tr_variantDictFindDict(top, TR_KEY_arguments, &args))
|
2010-01-29 16:43:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool boolVal;
|
2010-01-29 16:43:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args, TR_KEY_port_is_open, &boolVal))
|
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
printf("Port is open: %s\n", boolVal ? "Yes" : "No");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-01-29 16:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void printTorrentList(tr_variant* top)
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_variant* args;
|
|
|
|
tr_variant* list;
|
2008-07-07 03:38:22 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindDict(top, TR_KEY_arguments, &args) && tr_variantDictFindList(args, TR_KEY_torrents, &list))
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t total_size = 0;
|
2017-05-01 15:46:41 +00:00
|
|
|
double total_up = 0;
|
|
|
|
double total_down = 0;
|
2009-02-25 18:06:57 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
printf(
|
|
|
|
"%6s %-4s %9s %-8s %6s %6s %-5s %-11s %s\n",
|
|
|
|
"ID",
|
|
|
|
"Done",
|
|
|
|
"Have",
|
|
|
|
"ETA",
|
|
|
|
"Up",
|
|
|
|
"Down",
|
|
|
|
"Ratio",
|
|
|
|
"Status",
|
|
|
|
"Name");
|
2009-02-25 18:06:57 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0, n = tr_variantListSize(list); i < n; ++i)
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
2020-09-11 21:07:45 +00:00
|
|
|
int64_t torId;
|
2017-05-01 15:46:41 +00:00
|
|
|
int64_t eta;
|
|
|
|
int64_t status;
|
|
|
|
int64_t up;
|
|
|
|
int64_t down;
|
|
|
|
int64_t sizeWhenDone;
|
|
|
|
int64_t leftUntilDone;
|
2017-04-19 12:04:45 +00:00
|
|
|
double ratio;
|
2021-11-14 17:11:29 +00:00
|
|
|
auto name = std::string_view{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* d = tr_variantListChild(list, i);
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if (tr_variantDictFindInt(d, TR_KEY_eta, &eta) && tr_variantDictFindInt(d, TR_KEY_id, &torId) &&
|
2019-02-10 11:05:16 +00:00
|
|
|
tr_variantDictFindInt(d, TR_KEY_leftUntilDone, &leftUntilDone) &&
|
2021-11-14 17:11:29 +00:00
|
|
|
tr_variantDictFindStrView(d, TR_KEY_name, &name) && tr_variantDictFindInt(d, TR_KEY_rateDownload, &down) &&
|
2019-02-10 11:05:16 +00:00
|
|
|
tr_variantDictFindInt(d, TR_KEY_rateUpload, &up) &&
|
|
|
|
tr_variantDictFindInt(d, TR_KEY_sizeWhenDone, &sizeWhenDone) &&
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_variantDictFindInt(d, TR_KEY_status, &status) && tr_variantDictFindReal(d, TR_KEY_uploadRatio, &ratio))
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
|
|
|
char etaStr[16];
|
2008-10-31 23:56:14 +00:00
|
|
|
char statusStr[64];
|
2009-02-09 17:44:47 +00:00
|
|
|
char doneStr[8];
|
2009-02-23 03:26:01 +00:00
|
|
|
int64_t error;
|
|
|
|
char errorMark;
|
2009-02-09 17:44:47 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (sizeWhenDone != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_snprintf(doneStr, sizeof(doneStr), "%d%%", (int)(100.0 * (sizeWhenDone - leftUntilDone) / sizeWhenDone));
|
|
|
|
}
|
2009-02-09 17:44:47 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_strlcpy(doneStr, "n/a", sizeof(doneStr));
|
|
|
|
}
|
2009-01-30 23:29:18 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (leftUntilDone != 0 || eta != -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
etaToString(etaStr, sizeof(etaStr), eta);
|
|
|
|
}
|
2008-07-07 03:38:22 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_snprintf(etaStr, sizeof(etaStr), "Done");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(d, TR_KEY_error, &error) && error)
|
|
|
|
{
|
2009-02-23 03:26:01 +00:00
|
|
|
errorMark = '*';
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-02-23 03:26:01 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-02-23 03:26:01 +00:00
|
|
|
errorMark = ' ';
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
printf(
|
2021-11-14 17:11:29 +00:00
|
|
|
"%6d%c %4s %9s %-8s %6.1f %6.1f %5s %-11s %" TR_PRIsv "\n",
|
2021-08-15 09:41:48 +00:00
|
|
|
(int)torId,
|
|
|
|
errorMark,
|
|
|
|
doneStr,
|
2021-12-31 00:30:21 +00:00
|
|
|
strlsize(sizeWhenDone - leftUntilDone).c_str(),
|
2021-08-15 09:41:48 +00:00
|
|
|
etaStr,
|
|
|
|
up / (double)tr_speed_K,
|
|
|
|
down / (double)tr_speed_K,
|
2021-12-31 00:30:21 +00:00
|
|
|
strlratio2(ratio).c_str(),
|
2021-08-15 09:41:48 +00:00
|
|
|
getStatusString(d, statusStr, sizeof(statusStr)),
|
2021-11-14 17:11:29 +00:00
|
|
|
TR_PRIsv_ARG(name));
|
2009-02-25 18:06:57 +00:00
|
|
|
|
|
|
|
total_up += up;
|
2009-02-27 15:59:09 +00:00
|
|
|
total_down += down;
|
2009-02-25 18:06:57 +00:00
|
|
|
total_size += sizeWhenDone - leftUntilDone;
|
2008-07-07 03:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-25 18:06:57 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
printf(
|
|
|
|
"Sum: %9s %6.1f %6.1f\n",
|
2021-12-31 00:30:21 +00:00
|
|
|
strlsize(total_size).c_str(),
|
2021-08-15 09:41:48 +00:00
|
|
|
total_up / (double)tr_speed_K,
|
|
|
|
total_down / (double)tr_speed_K);
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void printTrackersImpl(tr_variant* trackerStats)
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char buf[512];
|
2010-07-24 12:30:16 +00:00
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
for (size_t i = 0, n = tr_variantListSize(trackerStats); i < n; ++i)
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2020-11-08 19:54:40 +00:00
|
|
|
tr_variant* const t = tr_variantListChild(trackerStats, i);
|
2021-11-14 17:11:29 +00:00
|
|
|
|
|
|
|
auto announceState = int64_t{};
|
|
|
|
auto downloadCount = int64_t{};
|
|
|
|
auto hasAnnounced = bool{};
|
|
|
|
auto hasScraped = bool{};
|
|
|
|
auto host = std::string_view{};
|
|
|
|
auto isBackup = bool{};
|
|
|
|
auto lastAnnouncePeerCount = int64_t{};
|
|
|
|
auto lastAnnounceResult = std::string_view{};
|
|
|
|
auto lastAnnounceStartTime = int64_t{};
|
|
|
|
auto lastAnnounceTime = int64_t{};
|
|
|
|
auto lastScrapeResult = std::string_view{};
|
|
|
|
auto lastScrapeStartTime = int64_t{};
|
|
|
|
auto lastScrapeSucceeded = bool{};
|
|
|
|
auto lastScrapeTime = int64_t{};
|
|
|
|
auto lastScrapeTimedOut = bool{};
|
|
|
|
auto leecherCount = int64_t{};
|
|
|
|
auto nextAnnounceTime = int64_t{};
|
|
|
|
auto nextScrapeTime = int64_t{};
|
|
|
|
auto scrapeState = int64_t{};
|
|
|
|
auto seederCount = int64_t{};
|
|
|
|
auto tier = int64_t{};
|
|
|
|
auto trackerId = int64_t{};
|
2011-03-22 15:19:54 +00:00
|
|
|
bool lastAnnounceSucceeded;
|
|
|
|
bool lastAnnounceTimedOut;
|
2010-07-24 12:30:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(t, TR_KEY_downloadCount, &downloadCount) &&
|
|
|
|
tr_variantDictFindBool(t, TR_KEY_hasAnnounced, &hasAnnounced) &&
|
2021-11-14 17:11:29 +00:00
|
|
|
tr_variantDictFindBool(t, TR_KEY_hasScraped, &hasScraped) && tr_variantDictFindStrView(t, TR_KEY_host, &host) &&
|
|
|
|
tr_variantDictFindInt(t, TR_KEY_id, &trackerId) && tr_variantDictFindBool(t, TR_KEY_isBackup, &isBackup) &&
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictFindInt(t, TR_KEY_announceState, &announceState) &&
|
|
|
|
tr_variantDictFindInt(t, TR_KEY_scrapeState, &scrapeState) &&
|
|
|
|
tr_variantDictFindInt(t, TR_KEY_lastAnnouncePeerCount, &lastAnnouncePeerCount) &&
|
2021-11-14 17:11:29 +00:00
|
|
|
tr_variantDictFindStrView(t, TR_KEY_lastAnnounceResult, &lastAnnounceResult) &&
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictFindInt(t, TR_KEY_lastAnnounceStartTime, &lastAnnounceStartTime) &&
|
|
|
|
tr_variantDictFindBool(t, TR_KEY_lastAnnounceSucceeded, &lastAnnounceSucceeded) &&
|
|
|
|
tr_variantDictFindInt(t, TR_KEY_lastAnnounceTime, &lastAnnounceTime) &&
|
|
|
|
tr_variantDictFindBool(t, TR_KEY_lastAnnounceTimedOut, &lastAnnounceTimedOut) &&
|
2021-11-14 17:11:29 +00:00
|
|
|
tr_variantDictFindStrView(t, TR_KEY_lastScrapeResult, &lastScrapeResult) &&
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictFindInt(t, TR_KEY_lastScrapeStartTime, &lastScrapeStartTime) &&
|
|
|
|
tr_variantDictFindBool(t, TR_KEY_lastScrapeSucceeded, &lastScrapeSucceeded) &&
|
|
|
|
tr_variantDictFindInt(t, TR_KEY_lastScrapeTime, &lastScrapeTime) &&
|
|
|
|
tr_variantDictFindBool(t, TR_KEY_lastScrapeTimedOut, &lastScrapeTimedOut) &&
|
|
|
|
tr_variantDictFindInt(t, TR_KEY_leecherCount, &leecherCount) &&
|
|
|
|
tr_variantDictFindInt(t, TR_KEY_nextAnnounceTime, &nextAnnounceTime) &&
|
|
|
|
tr_variantDictFindInt(t, TR_KEY_nextScrapeTime, &nextScrapeTime) &&
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_variantDictFindInt(t, TR_KEY_seederCount, &seederCount) && tr_variantDictFindInt(t, TR_KEY_tier, &tier))
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
time_t const now = time(nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
printf("\n");
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Tracker %d: %" TR_PRIsv "\n", (int)trackerId, TR_PRIsv_ARG(host));
|
2010-07-24 12:30:16 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (isBackup)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf(" Backup on tier %d\n", (int)tier);
|
|
|
|
}
|
2010-07-24 12:30:16 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf(" Active in tier %d\n", (int)tier);
|
|
|
|
}
|
2010-07-24 12:30:16 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!isBackup)
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (hasAnnounced && announceState != TR_TRACKER_INACTIVE)
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strltime(buf, now - lastAnnounceTime, sizeof(buf));
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (lastAnnounceSucceeded)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf(" Got a list of %d peers %s ago\n", (int)lastAnnouncePeerCount, buf);
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (lastAnnounceTimedOut)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf(" Peer list request timed out; will retry\n");
|
|
|
|
}
|
2010-07-24 12:30:16 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Got an error \"%" TR_PRIsv "\" %s ago\n", TR_PRIsv_ARG(lastAnnounceResult), buf);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-07-24 12:30:16 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (announceState)
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_TRACKER_INACTIVE:
|
|
|
|
printf(" No updates scheduled\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_WAITING:
|
|
|
|
tr_strltime(buf, nextAnnounceTime - now, sizeof(buf));
|
|
|
|
printf(" Asking for more peers in %s\n", buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_QUEUED:
|
|
|
|
printf(" Queued to ask for more peers\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_ACTIVE:
|
|
|
|
tr_strltime(buf, now - lastAnnounceStartTime, sizeof(buf));
|
|
|
|
printf(" Asking for more peers now... %s\n", buf);
|
|
|
|
break;
|
2010-07-24 12:30:16 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (hasScraped)
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strltime(buf, now - lastScrapeTime, sizeof(buf));
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (lastScrapeSucceeded)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf(" Tracker had %d seeders and %d leechers %s ago\n", (int)seederCount, (int)leecherCount, buf);
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (lastScrapeTimedOut)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf(" Tracker scrape timed out; will retry\n");
|
|
|
|
}
|
2010-07-24 12:30:16 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Got a scrape error \"%" TR_PRIsv "\" %s ago\n", TR_PRIsv_ARG(lastScrapeResult), buf);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-07-24 12:30:16 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (scrapeState)
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_TRACKER_INACTIVE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_WAITING:
|
|
|
|
tr_strltime(buf, nextScrapeTime - now, sizeof(buf));
|
|
|
|
printf(" Asking for peer counts in %s\n", buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_QUEUED:
|
|
|
|
printf(" Queued to ask for peer counts\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_ACTIVE:
|
|
|
|
tr_strltime(buf, now - lastScrapeStartTime, sizeof(buf));
|
|
|
|
printf(" Asking for peer counts now... %s\n", buf);
|
|
|
|
break;
|
2010-07-24 12:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void printTrackers(tr_variant* top)
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_variant* args;
|
|
|
|
tr_variant* torrents;
|
2010-07-24 12:30:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindDict(top, TR_KEY_arguments, &args) && tr_variantDictFindList(args, TR_KEY_torrents, &torrents))
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0, n = tr_variantListSize(torrents); i < n; ++i)
|
2010-07-24 12:30:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* trackerStats;
|
|
|
|
tr_variant* torrent = tr_variantListChild(torrents, i);
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindList(torrent, TR_KEY_trackerStats, &trackerStats))
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printTrackersImpl(trackerStats);
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (i + 1 < n)
|
|
|
|
{
|
|
|
|
printf("\n");
|
|
|
|
}
|
2010-07-24 12:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void printSession(tr_variant* top)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* args;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindDict(top, TR_KEY_arguments, &args))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
int64_t i;
|
|
|
|
bool boolVal;
|
2021-11-14 17:11:29 +00:00
|
|
|
auto sv = std::string_view{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
printf("VERSION\n");
|
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(args, TR_KEY_version, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Daemon version: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(args, TR_KEY_rpc_version, &i))
|
|
|
|
{
|
|
|
|
printf(" RPC version: %" PRId64 "\n", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(args, TR_KEY_rpc_version_minimum, &i))
|
|
|
|
{
|
|
|
|
printf(" RPC minimum version: %" PRId64 "\n", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("CONFIG\n");
|
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(args, TR_KEY_config_dir, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Configuration directory: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(args, TR_KEY_download_dir, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Download directory: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(args, TR_KEY_peer_port, &i))
|
|
|
|
{
|
|
|
|
printf(" Listenport: %" PRId64 "\n", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(args, TR_KEY_port_forwarding_enabled, &boolVal))
|
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
printf(" Portforwarding enabled: %s\n", boolVal ? "Yes" : "No");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(args, TR_KEY_utp_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
printf(" uTP enabled: %s\n", (boolVal ? "Yes" : "No"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(args, TR_KEY_dht_enabled, &boolVal))
|
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
printf(" Distributed hash table enabled: %s\n", boolVal ? "Yes" : "No");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(args, TR_KEY_lpd_enabled, &boolVal))
|
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
printf(" Local peer discovery enabled: %s\n", boolVal ? "Yes" : "No");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(args, TR_KEY_pex_enabled, &boolVal))
|
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
printf(" Peer exchange allowed: %s\n", boolVal ? "Yes" : "No");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(args, TR_KEY_encryption, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf(" Encryption: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(args, TR_KEY_cache_size_mb, &i))
|
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Maximum memory cache size: %s\n", tr_formatter_mem_MB(i).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
2010-04-29 03:01:39 +00:00
|
|
|
|
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
bool altEnabled;
|
|
|
|
bool altTimeEnabled;
|
|
|
|
bool upEnabled;
|
|
|
|
bool downEnabled;
|
|
|
|
bool seedRatioLimited;
|
|
|
|
int64_t altDown;
|
|
|
|
int64_t altUp;
|
|
|
|
int64_t altBegin;
|
|
|
|
int64_t altEnd;
|
|
|
|
int64_t altDay;
|
|
|
|
int64_t upLimit;
|
|
|
|
int64_t downLimit;
|
|
|
|
int64_t peerLimit;
|
2010-04-29 03:01:39 +00:00
|
|
|
double seedRatioLimit;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args, TR_KEY_alt_speed_down, &altDown) &&
|
|
|
|
tr_variantDictFindBool(args, TR_KEY_alt_speed_enabled, &altEnabled) &&
|
|
|
|
tr_variantDictFindInt(args, TR_KEY_alt_speed_time_begin, &altBegin) &&
|
|
|
|
tr_variantDictFindBool(args, TR_KEY_alt_speed_time_enabled, &altTimeEnabled) &&
|
|
|
|
tr_variantDictFindInt(args, TR_KEY_alt_speed_time_end, &altEnd) &&
|
|
|
|
tr_variantDictFindInt(args, TR_KEY_alt_speed_time_day, &altDay) &&
|
|
|
|
tr_variantDictFindInt(args, TR_KEY_alt_speed_up, &altUp) &&
|
|
|
|
tr_variantDictFindInt(args, TR_KEY_peer_limit_global, &peerLimit) &&
|
|
|
|
tr_variantDictFindInt(args, TR_KEY_speed_limit_down, &downLimit) &&
|
|
|
|
tr_variantDictFindBool(args, TR_KEY_speed_limit_down_enabled, &downEnabled) &&
|
|
|
|
tr_variantDictFindInt(args, TR_KEY_speed_limit_up, &upLimit) &&
|
|
|
|
tr_variantDictFindBool(args, TR_KEY_speed_limit_up_enabled, &upEnabled) &&
|
|
|
|
tr_variantDictFindReal(args, TR_KEY_seedRatioLimit, &seedRatioLimit) &&
|
|
|
|
tr_variantDictFindBool(args, TR_KEY_seedRatioLimited, &seedRatioLimited))
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("LIMITS\n");
|
|
|
|
printf(" Peer limit: %" PRId64 "\n", peerLimit);
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Default seed ratio limit: %s\n", seedRatioLimited ? strlratio2(seedRatioLimit).c_str() : "Unlimited");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
std::string effective_up_limit;
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (altEnabled)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
effective_up_limit = tr_formatter_speed_KBps(altUp);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (upEnabled)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
effective_up_limit = tr_formatter_speed_KBps(upLimit);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
effective_up_limit = "Unlimited"s;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
printf(
|
|
|
|
" Upload speed limit: %s (%s limit: %s; %s turtle limit: %s)\n",
|
2021-12-31 00:30:21 +00:00
|
|
|
effective_up_limit.c_str(),
|
2021-08-15 09:41:48 +00:00
|
|
|
upEnabled ? "Enabled" : "Disabled",
|
2021-12-31 00:30:21 +00:00
|
|
|
tr_formatter_speed_KBps(upLimit).c_str(),
|
2021-08-15 09:41:48 +00:00
|
|
|
altEnabled ? "Enabled" : "Disabled",
|
2021-12-31 00:30:21 +00:00
|
|
|
tr_formatter_speed_KBps(altUp).c_str());
|
|
|
|
|
|
|
|
std::string effective_down_limit;
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (altEnabled)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
effective_down_limit = tr_formatter_speed_KBps(altDown);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (downEnabled)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
effective_down_limit = tr_formatter_speed_KBps(downLimit);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
effective_down_limit = "Unlimited"s;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
printf(
|
|
|
|
" Download speed limit: %s (%s limit: %s; %s turtle limit: %s)\n",
|
2021-12-31 00:30:21 +00:00
|
|
|
effective_down_limit.c_str(),
|
2021-08-15 09:41:48 +00:00
|
|
|
downEnabled ? "Enabled" : "Disabled",
|
2021-12-31 00:30:21 +00:00
|
|
|
tr_formatter_speed_KBps(downLimit).c_str(),
|
2021-08-15 09:41:48 +00:00
|
|
|
altEnabled ? "Enabled" : "Disabled",
|
2021-12-31 00:30:21 +00:00
|
|
|
tr_formatter_speed_KBps(altDown).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (altTimeEnabled)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
printf(
|
|
|
|
" Turtle schedule: %02d:%02d - %02d:%02d ",
|
|
|
|
(int)(altBegin / 60),
|
|
|
|
(int)(altBegin % 60),
|
|
|
|
(int)(altEnd / 60),
|
|
|
|
(int)(altEnd % 60));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((altDay & TR_SCHED_SUN) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf("Sun ");
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((altDay & TR_SCHED_MON) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf("Mon ");
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((altDay & TR_SCHED_TUES) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf("Tue ");
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((altDay & TR_SCHED_WED) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf("Wed ");
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((altDay & TR_SCHED_THURS) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf("Thu ");
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((altDay & TR_SCHED_FRI) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf("Fri ");
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((altDay & TR_SCHED_SAT) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printf("Sat ");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("MISC\n");
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(args, TR_KEY_start_added_torrents, &boolVal))
|
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
printf(" Autostart added torrents: %s\n", boolVal ? "Yes" : "No");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(args, TR_KEY_trash_original_torrent_files, &boolVal))
|
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
printf(" Delete automatically added torrents: %s\n", boolVal ? "Yes" : "No");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void printSessionStats(tr_variant* top)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_variant* args;
|
|
|
|
tr_variant* d;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindDict(top, TR_KEY_arguments, &args))
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char buf[512];
|
2017-05-01 15:46:41 +00:00
|
|
|
int64_t up;
|
|
|
|
int64_t down;
|
|
|
|
int64_t secs;
|
|
|
|
int64_t sessions;
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindDict(args, TR_KEY_current_stats, &d) && tr_variantDictFindInt(d, TR_KEY_uploadedBytes, &up) &&
|
|
|
|
tr_variantDictFindInt(d, TR_KEY_downloadedBytes, &down) && tr_variantDictFindInt(d, TR_KEY_secondsActive, &secs))
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\nCURRENT SESSION\n");
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Uploaded: %s\n", strlsize(up).c_str());
|
|
|
|
printf(" Downloaded: %s\n", strlsize(down).c_str());
|
|
|
|
printf(" Ratio: %s\n", strlratio(up, down).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
printf(" Duration: %s\n", tr_strltime(buf, secs, sizeof(buf)));
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
if (tr_variantDictFindDict(args, TR_KEY_cumulative_stats, &d) &&
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_variantDictFindInt(d, TR_KEY_sessionCount, &sessions) && tr_variantDictFindInt(d, TR_KEY_uploadedBytes, &up) &&
|
|
|
|
tr_variantDictFindInt(d, TR_KEY_downloadedBytes, &down) && tr_variantDictFindInt(d, TR_KEY_secondsActive, &secs))
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\nTOTAL\n");
|
|
|
|
printf(" Started %lu times\n", (unsigned long)sessions);
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Uploaded: %s\n", strlsize(up).c_str());
|
|
|
|
printf(" Downloaded: %s\n", strlsize(down).c_str());
|
|
|
|
printf(" Ratio: %s\n", strlratio(up, down).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
printf(" Duration: %s\n", tr_strltime(buf, secs, sizeof(buf)));
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
2008-07-07 03:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
static char id[4096];
|
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
static int processResponse(char const* rpcurl, std::string_view response)
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant top;
|
2009-05-18 16:05:56 +00:00
|
|
|
int status = EXIT_SUCCESS;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (debug)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
fprintf(
|
|
|
|
stderr,
|
2021-11-14 17:11:29 +00:00
|
|
|
"got response (len %d):\n--------\n%" TR_PRIsv "\n--------\n",
|
|
|
|
int(std::size(response)),
|
|
|
|
TR_PRIsv_ARG(response));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-05-27 21:47:07 +00:00
|
|
|
|
2021-11-18 05:37:35 +00:00
|
|
|
if (!tr_variantFromBuf(&top, TR_VARIANT_PARSE_JSON | TR_VARIANT_PARSE_INPLACE, response))
|
2009-05-18 16:05:56 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_logAddNamedError(MyName, "Unable to parse response \"%" TR_PRIsv "\"", TR_PRIsv_ARG(response));
|
2009-05-18 16:05:56 +00:00
|
|
|
status |= EXIT_FAILURE;
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
else
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t tag = -1;
|
2021-11-14 17:11:29 +00:00
|
|
|
auto sv = std::string_view{};
|
2011-08-24 18:50:39 +00:00
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (tr_variantDictFindStrView(&top, TR_KEY_result, &sv))
|
2011-08-24 18:50:39 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
if (sv != "success"sv)
|
2011-08-24 18:50:39 +00:00
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf("Error: %" TR_PRIsv "\n", TR_PRIsv_ARG(sv));
|
2011-08-24 18:50:39 +00:00
|
|
|
status |= EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictFindInt(&top, TR_KEY_tag, &tag);
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (tag)
|
|
|
|
{
|
|
|
|
case TAG_SESSION:
|
|
|
|
printSession(&top);
|
|
|
|
break;
|
2009-01-23 19:36:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TAG_STATS:
|
|
|
|
printSessionStats(&top);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TAG_DETAILS:
|
|
|
|
printDetails(&top);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TAG_FILES:
|
|
|
|
printFileList(&top);
|
|
|
|
break;
|
2009-08-13 03:54:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TAG_LIST:
|
|
|
|
printTorrentList(&top);
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TAG_PEERS:
|
|
|
|
printPeers(&top);
|
|
|
|
break;
|
2010-07-24 12:30:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TAG_PIECES:
|
|
|
|
printPieces(&top);
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TAG_PORTTEST:
|
|
|
|
printPortTest(&top);
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TAG_TRACKERS:
|
|
|
|
printTrackers(&top);
|
|
|
|
break;
|
2010-07-24 12:30:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TAG_TORRENT_ADD:
|
|
|
|
{
|
|
|
|
int64_t i;
|
|
|
|
tr_variant* b = ⊤
|
2010-01-29 16:43:39 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
if (tr_variantDictFindDict(&top, Arguments, &b) &&
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_variantDictFindDict(b, TR_KEY_torrent_added, &b) && tr_variantDictFindInt(b, TR_KEY_id, &i))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_snprintf(id, sizeof(id), "%" PRId64, i);
|
|
|
|
}
|
2021-10-06 17:24:02 +00:00
|
|
|
[[fallthrough]];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2021-11-14 17:11:29 +00:00
|
|
|
if (!tr_variantDictFindStrView(&top, TR_KEY_result, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-05-18 16:05:56 +00:00
|
|
|
status |= EXIT_FAILURE;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-14 17:11:29 +00:00
|
|
|
printf("%s responded: \"%" TR_PRIsv "\"\n", rpcurl, TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-14 17:11:29 +00:00
|
|
|
if (sv != "success"sv)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
status |= EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2009-05-18 16:05:56 +00:00
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&top);
|
|
|
|
}
|
2011-08-24 18:50:39 +00:00
|
|
|
}
|
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-08-24 18:50:39 +00:00
|
|
|
status |= EXIT_FAILURE;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-08-24 18:50:39 +00:00
|
|
|
}
|
2009-05-18 16:05:56 +00:00
|
|
|
|
|
|
|
return status;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static CURL* tr_curl_easy_init(struct evbuffer* writebuf)
|
2009-05-08 14:56:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
CURL* curl = curl_easy_init();
|
2021-12-17 20:48:02 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, tr_strvJoin(MyName, "/", LONG_VERSION_STRING).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunc);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, writebuf);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, parseResponseHeader);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, debug);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_ENCODING, ""); /* "" tells curl to fill in the blanks with what it was compiled to support */
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (netrc != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NETRC_FILE, netrc);
|
|
|
|
}
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (auth != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
curl_easy_setopt(curl, CURLOPT_USERPWD, auth);
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (UseSSL)
|
2014-01-19 01:53:07 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); /* do not verify subject/hostname */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); /* since most certs will be self-signed, do not verify against CA */
|
2014-01-19 01:53:07 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-25 02:07:33 +00:00
|
|
|
if (!tr_str_is_empty(session_id))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-16 02:09:46 +00:00
|
|
|
auto const h = tr_strvJoin(TR_RPC_SESSION_ID_HEADER, ": "sv, session_id);
|
|
|
|
auto* const custom_headers = curl_slist_append(nullptr, h.c_str());
|
2018-02-03 09:39:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, custom_headers);
|
2018-02-03 09:39:24 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_PRIVATE, custom_headers);
|
2009-05-08 14:56:11 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2009-05-08 14:56:11 +00:00
|
|
|
return curl;
|
|
|
|
}
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2018-02-03 09:39:24 +00:00
|
|
|
static void tr_curl_easy_cleanup(CURL* curl)
|
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
struct curl_slist* custom_headers = nullptr;
|
2018-02-03 09:39:24 +00:00
|
|
|
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &custom_headers);
|
|
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (custom_headers != nullptr)
|
2018-02-03 09:39:24 +00:00
|
|
|
{
|
|
|
|
curl_slist_free_all(custom_headers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int flush(char const* rpcurl, tr_variant** benc)
|
2010-03-31 19:59:51 +00:00
|
|
|
{
|
2010-04-29 03:01:39 +00:00
|
|
|
int status = EXIT_SUCCESS;
|
2021-12-27 22:47:25 +00:00
|
|
|
auto const json = tr_variantToStr(*benc, TR_VARIANT_FMT_JSON_LEAN);
|
2021-12-16 02:09:46 +00:00
|
|
|
auto const rpcurl_http = tr_strvJoin(UseSSL ? "https://" : "http://", rpcurl);
|
2010-03-31 19:59:51 +00:00
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
auto* const buf = evbuffer_new();
|
|
|
|
auto* curl = tr_curl_easy_init(buf);
|
2021-12-16 02:09:46 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, rpcurl_http.c_str());
|
2021-12-27 22:47:25 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, getTimeoutSecs(json));
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (debug)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-27 22:47:25 +00:00
|
|
|
fprintf(stderr, "posting:\n--------\n%s\n--------\n", json.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2022-02-01 17:30:51 +00:00
|
|
|
auto const res = curl_easy_perform(curl);
|
|
|
|
if (res != CURLE_OK)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_logAddNamedError(MyName, " (%s) %s", rpcurl_http.c_str(), curl_easy_strerror(res));
|
2010-04-29 03:01:39 +00:00
|
|
|
status |= EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
long response;
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
|
|
|
|
|
|
|
|
switch (response)
|
|
|
|
{
|
|
|
|
case 200:
|
2021-11-14 17:11:29 +00:00
|
|
|
status |= processResponse(
|
|
|
|
rpcurl,
|
|
|
|
std::string_view{ reinterpret_cast<char const*>(evbuffer_pullup(buf, -1)), evbuffer_get_length(buf) });
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 409:
|
|
|
|
/* Session id failed. Our curl header func has already
|
|
|
|
* pulled the new session id from this response's headers,
|
|
|
|
* build a new CURL* and try again */
|
2018-02-03 09:39:24 +00:00
|
|
|
tr_curl_easy_cleanup(curl);
|
2021-10-06 16:32:17 +00:00
|
|
|
curl = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, benc);
|
2021-10-06 16:32:17 +00:00
|
|
|
benc = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-02-02 03:03:59 +00:00
|
|
|
evbuffer_add(buf, "", 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "Unexpected response: %s\n", evbuffer_pullup(buf, -1));
|
|
|
|
status |= EXIT_FAILURE;
|
|
|
|
break;
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_free(buf);
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (curl != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2018-02-03 09:39:24 +00:00
|
|
|
tr_curl_easy_cleanup(curl);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (benc != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantFree(*benc);
|
2018-02-03 09:39:24 +00:00
|
|
|
tr_free(*benc);
|
2021-10-06 16:32:17 +00:00
|
|
|
*benc = nullptr;
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_variant* ensure_sset(tr_variant** sset)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* args;
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (*sset != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
args = tr_variantDictFind(*sset, Arguments);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*sset = tr_new0(tr_variant, 1);
|
|
|
|
tr_variantInitDict(*sset, 3);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(*sset, TR_KEY_method, "session-set"sv);
|
2021-12-17 20:48:02 +00:00
|
|
|
args = tr_variantDictAddDict(*sset, Arguments, 0);
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_variant* ensure_tset(tr_variant** tset)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* args;
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (*tset != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
args = tr_variantDictFind(*tset, Arguments);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*tset = tr_new0(tr_variant, 1);
|
|
|
|
tr_variantInitDict(*tset, 3);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(*tset, TR_KEY_method, "torrent-set"sv);
|
2021-12-17 20:48:02 +00:00
|
|
|
args = tr_variantDictAddDict(*tset, Arguments, 1);
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
2010-03-31 19:59:51 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int processArgs(char const* rpcurl, int argc, char const* const* argv)
|
2009-05-08 14:56:11 +00:00
|
|
|
{
|
2010-04-29 03:01:39 +00:00
|
|
|
int c;
|
2009-05-18 16:05:56 +00:00
|
|
|
int status = EXIT_SUCCESS;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* optarg;
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_variant* sset = nullptr;
|
|
|
|
tr_variant* tset = nullptr;
|
|
|
|
tr_variant* tadd = nullptr;
|
2010-04-29 03:01:39 +00:00
|
|
|
|
|
|
|
*id = '\0';
|
2009-05-08 14:56:11 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
while ((c = tr_getopt(Usage, argc, argv, std::data(Options), &optarg)) != TR_OPT_DONE)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const stepMode = getOptMode(c);
|
2009-05-08 14:56:11 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (stepMode == 0) /* meta commands */
|
2009-05-08 14:56:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (c)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'a': /* add torrent */
|
2021-10-06 16:32:17 +00:00
|
|
|
if (sset != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
status |= flush(rpcurl, &sset);
|
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tadd != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
status |= flush(rpcurl, &tadd);
|
|
|
|
}
|
2009-05-08 14:56:11 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tset != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
addIdArg(tr_variantDictFind(tset, Arguments), id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &tset);
|
|
|
|
}
|
2009-05-08 14:56:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tadd = tr_new0(tr_variant, 1);
|
|
|
|
tr_variantInitDict(tadd, 3);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(tadd, TR_KEY_method, "torrent-add"sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(tadd, TR_KEY_tag, TAG_TORRENT_ADD);
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_variantDictAddDict(tadd, Arguments, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2010-12-12 15:26:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'b': /* debug */
|
|
|
|
debug = true;
|
|
|
|
break;
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'n': /* auth */
|
|
|
|
auth = tr_strdup(optarg);
|
|
|
|
break;
|
2011-08-24 19:30:28 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 810: /* authenv */
|
2021-10-06 16:32:17 +00:00
|
|
|
auth = tr_env_get_string("TR_AUTH", nullptr);
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (auth == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "The TR_AUTH environment variable is not set\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'N': /* netrc */
|
|
|
|
netrc = tr_strdup(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 820: /* UseSSL */
|
|
|
|
UseSSL = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't': /* set current torrent */
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tadd != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
status |= flush(rpcurl, &tadd);
|
|
|
|
}
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tset != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
addIdArg(tr_variantDictFind(tset, Arguments), id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &tset);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_strlcpy(id, optarg, sizeof(id));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V': /* show version number */
|
2021-12-17 20:48:02 +00:00
|
|
|
fprintf(stderr, "%s %s\n", MyName, LONG_VERSION_STRING);
|
2017-04-19 12:04:45 +00:00
|
|
|
exit(0);
|
|
|
|
|
|
|
|
case TR_OPT_ERR:
|
|
|
|
fprintf(stderr, "invalid option\n");
|
|
|
|
showUsage();
|
|
|
|
status |= EXIT_FAILURE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_OPT_UNK:
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tadd != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_variant* args = tr_variantDictFind(tadd, Arguments);
|
2022-01-08 12:46:25 +00:00
|
|
|
std::string const tmp = getEncodedMetainfo(optarg);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-01-08 12:46:25 +00:00
|
|
|
if (!std::empty(tmp))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-31 05:08:20 +00:00
|
|
|
tr_variantDictAddStr(args, TR_KEY_metainfo, tmp);
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_variantDictAddStr(args, TR_KEY_filename, optarg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unknown option: %s\n", optarg);
|
|
|
|
status |= EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (stepMode == MODE_TORRENT_GET)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* top = tr_new0(tr_variant, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* args;
|
|
|
|
tr_variant* fields;
|
|
|
|
tr_variantInitDict(top, 3);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(top, TR_KEY_method, "torrent-get"sv);
|
2021-12-17 20:48:02 +00:00
|
|
|
args = tr_variantDictAddDict(top, Arguments, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
fields = tr_variantDictAddList(args, TR_KEY_fields, 0);
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tset != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
addIdArg(tr_variantDictFind(tset, Arguments), id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &tset);
|
|
|
|
}
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (c)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'i':
|
|
|
|
tr_variantDictAddInt(top, TR_KEY_tag, TAG_DETAILS);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < TR_N_ELEMENTS(details_keys); ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantListAddQuark(fields, details_keys[i]);
|
|
|
|
}
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
addIdArg(args, id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
tr_variantDictAddInt(top, TR_KEY_tag, TAG_LIST);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < TR_N_ELEMENTS(list_keys); ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantListAddQuark(fields, list_keys[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
addIdArg(args, id, "all");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 940:
|
|
|
|
tr_variantDictAddInt(top, TR_KEY_tag, TAG_FILES);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < TR_N_ELEMENTS(files_keys); ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantListAddQuark(fields, files_keys[i]);
|
|
|
|
}
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
addIdArg(args, id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 941:
|
|
|
|
tr_variantDictAddInt(top, TR_KEY_tag, TAG_PEERS);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantListAddStrView(fields, "peers"sv);
|
2021-10-06 16:32:17 +00:00
|
|
|
addIdArg(args, id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 942:
|
|
|
|
tr_variantDictAddInt(top, TR_KEY_tag, TAG_PIECES);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantListAddStrView(fields, "pieces"sv);
|
|
|
|
tr_variantListAddStrView(fields, "pieceCount"sv);
|
2021-10-06 16:32:17 +00:00
|
|
|
addIdArg(args, id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 943:
|
|
|
|
tr_variantDictAddInt(top, TR_KEY_tag, TAG_TRACKERS);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantListAddStrView(fields, "trackerStats"sv);
|
2021-10-06 16:32:17 +00:00
|
|
|
addIdArg(args, id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert("unhandled value" && 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
status |= flush(rpcurl, &top);
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (stepMode == MODE_SESSION_SET)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* args = ensure_sset(&sset);
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (c)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 800:
|
|
|
|
tr_variantDictAddStr(args, TR_KEY_script_torrent_done_filename, optarg);
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_script_torrent_done_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 801:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_script_torrent_done_enabled, false);
|
|
|
|
break;
|
|
|
|
|
2022-02-14 04:07:12 +00:00
|
|
|
case 802:
|
|
|
|
tr_variantDictAddStr(args, TR_KEY_script_torrent_done_seeding_filename, optarg);
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_script_torrent_done_seeding_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 803:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_script_torrent_done_seeding_enabled, false);
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 970:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_alt_speed_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 971:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_alt_speed_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 972:
|
|
|
|
tr_variantDictAddInt(args, TR_KEY_alt_speed_down, numarg(optarg));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 973:
|
|
|
|
tr_variantDictAddInt(args, TR_KEY_alt_speed_up, numarg(optarg));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 974:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_alt_speed_time_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 975:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_alt_speed_time_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 976:
|
|
|
|
addTime(args, TR_KEY_alt_speed_time_begin, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 977:
|
|
|
|
addTime(args, TR_KEY_alt_speed_time_end, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 978:
|
|
|
|
addDays(args, TR_KEY_alt_speed_time_day, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
tr_variantDictAddStr(args, TR_KEY_incomplete_dir, optarg);
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_incomplete_dir_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_incomplete_dir_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e':
|
|
|
|
tr_variantDictAddInt(args, TR_KEY_cache_size_mb, atoi(optarg));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 910:
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(args, TR_KEY_encryption, "required"sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 911:
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(args, TR_KEY_encryption, "preferred"sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 912:
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(args, TR_KEY_encryption, "tolerated"sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_port_forwarding_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_port_forwarding_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_dht_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'O':
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_dht_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 830:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_utp_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 831:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_utp_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
tr_variantDictAddInt(args, TR_KEY_peer_port, numarg(optarg));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P':
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_peer_port_random_on_start, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_pex_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'X':
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_pex_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'y':
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_lpd_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Y':
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_lpd_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 953:
|
|
|
|
tr_variantDictAddReal(args, TR_KEY_seedRatioLimit, atof(optarg));
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_seedRatioLimited, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 954:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_seedRatioLimited, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 990:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_start_added_torrents, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 991:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_start_added_torrents, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 992:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_trash_original_torrent_files, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 993:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_trash_original_torrent_files, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert("unhandled value" && 0);
|
|
|
|
break;
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (stepMode == (MODE_SESSION_SET | MODE_TORRENT_SET))
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_variant* targs = nullptr;
|
|
|
|
tr_variant* sargs = nullptr;
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2019-07-13 08:52:44 +00:00
|
|
|
if (!tr_str_is_empty(id))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
targs = ensure_tset(&tset);
|
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
sargs = ensure_sset(&sset);
|
|
|
|
}
|
2010-12-12 16:43:19 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (c)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'd':
|
2021-10-06 16:32:17 +00:00
|
|
|
if (targs != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddInt(targs, TR_KEY_downloadLimit, numarg(optarg));
|
|
|
|
tr_variantDictAddBool(targs, TR_KEY_downloadLimited, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_variantDictAddInt(sargs, TR_KEY_speed_limit_down, numarg(optarg));
|
|
|
|
tr_variantDictAddBool(sargs, TR_KEY_speed_limit_down_enabled, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
2021-10-06 16:32:17 +00:00
|
|
|
if (targs != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddBool(targs, TR_KEY_downloadLimited, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_variantDictAddBool(sargs, TR_KEY_speed_limit_down_enabled, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
2021-10-06 16:32:17 +00:00
|
|
|
if (targs != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddInt(targs, TR_KEY_uploadLimit, numarg(optarg));
|
|
|
|
tr_variantDictAddBool(targs, TR_KEY_uploadLimited, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_variantDictAddInt(sargs, TR_KEY_speed_limit_up, numarg(optarg));
|
|
|
|
tr_variantDictAddBool(sargs, TR_KEY_speed_limit_up_enabled, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'U':
|
2021-10-06 16:32:17 +00:00
|
|
|
if (targs != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddBool(targs, TR_KEY_uploadLimited, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_variantDictAddBool(sargs, TR_KEY_speed_limit_up_enabled, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 930:
|
2021-10-06 16:32:17 +00:00
|
|
|
if (targs != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddInt(targs, TR_KEY_peer_limit, atoi(optarg));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_variantDictAddInt(sargs, TR_KEY_peer_limit_global, atoi(optarg));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert("unhandled value" && 0);
|
|
|
|
break;
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (stepMode == MODE_TORRENT_SET)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* args = ensure_tset(&tset);
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (c)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2021-12-10 04:34:43 +00:00
|
|
|
case 712:
|
2021-12-10 04:33:37 +00:00
|
|
|
{
|
2021-12-10 04:34:43 +00:00
|
|
|
tr_variant* list;
|
|
|
|
if (!tr_variantDictFindList(args, TR_KEY_trackerRemove, &list))
|
|
|
|
{
|
|
|
|
list = tr_variantDictAddList(args, TR_KEY_trackerRemove, 1);
|
|
|
|
}
|
|
|
|
tr_variantListAddInt(list, atoi(optarg));
|
|
|
|
break;
|
2021-12-10 04:33:37 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case 950:
|
|
|
|
tr_variantDictAddReal(args, TR_KEY_seedRatioLimit, atof(optarg));
|
|
|
|
tr_variantDictAddInt(args, TR_KEY_seedRatioMode, TR_RATIOLIMIT_SINGLE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 951:
|
|
|
|
tr_variantDictAddInt(args, TR_KEY_seedRatioMode, TR_RATIOLIMIT_GLOBAL);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 952:
|
|
|
|
tr_variantDictAddInt(args, TR_KEY_seedRatioMode, TR_RATIOLIMIT_UNLIMITED);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 984:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_honorsSessionLimits, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 985:
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_honorsSessionLimits, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert("unhandled value" && 0);
|
|
|
|
break;
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (stepMode == (MODE_TORRENT_SET | MODE_TORRENT_ADD))
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* args;
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tadd != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
args = tr_variantDictFind(tadd, Arguments);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
args = ensure_tset(&tset);
|
|
|
|
}
|
2010-12-12 16:43:19 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (c)
|
2010-12-12 16:43:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'g':
|
|
|
|
addFiles(args, TR_KEY_files_wanted, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'G':
|
|
|
|
addFiles(args, TR_KEY_files_unwanted, optarg);
|
|
|
|
break;
|
|
|
|
|
2022-02-03 06:02:30 +00:00
|
|
|
case 'L':
|
|
|
|
addLabels(args, optarg ? optarg : "");
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 900:
|
|
|
|
addFiles(args, TR_KEY_priority_high, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 901:
|
|
|
|
addFiles(args, TR_KEY_priority_normal, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 902:
|
|
|
|
addFiles(args, TR_KEY_priority_low, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 700:
|
|
|
|
tr_variantDictAddInt(args, TR_KEY_bandwidthPriority, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 701:
|
|
|
|
tr_variantDictAddInt(args, TR_KEY_bandwidthPriority, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 702:
|
|
|
|
tr_variantDictAddInt(args, TR_KEY_bandwidthPriority, -1);
|
|
|
|
break;
|
|
|
|
|
2021-12-10 04:34:43 +00:00
|
|
|
case 710:
|
2021-12-10 04:33:37 +00:00
|
|
|
{
|
2021-12-10 04:34:43 +00:00
|
|
|
tr_variant* list;
|
|
|
|
if (!tr_variantDictFindList(args, TR_KEY_trackerAdd, &list))
|
|
|
|
{
|
|
|
|
list = tr_variantDictAddList(args, TR_KEY_trackerAdd, 1);
|
|
|
|
}
|
|
|
|
tr_variantListAddStr(list, optarg);
|
|
|
|
break;
|
2021-12-10 04:33:37 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert("unhandled value" && 0);
|
|
|
|
break;
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (c == 961) /* set location */
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tadd != nullptr)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_variant* args = tr_variantDictFind(tadd, Arguments);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(args, TR_KEY_download_dir, optarg);
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* top = tr_new0(tr_variant, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(top, 2);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(top, TR_KEY_method, "torrent-set-location"sv);
|
2022-01-17 18:39:50 +00:00
|
|
|
tr_variant* args = tr_variantDictAddDict(top, Arguments, 3);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(args, TR_KEY_location, optarg);
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_move, false);
|
2021-10-06 16:32:17 +00:00
|
|
|
addIdArg(args, id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &top);
|
2010-04-29 03:01:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (c)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 920: /* session-info */
|
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* top = tr_new0(tr_variant, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(top, 2);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(top, TR_KEY_method, "session-get"sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(top, TR_KEY_tag, TAG_SESSION);
|
|
|
|
status |= flush(rpcurl, &top);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
case 's': /* start */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tadd != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddBool(tr_variantDictFind(tadd, TR_KEY_arguments), TR_KEY_paused, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* top = tr_new0(tr_variant, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(top, 2);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(top, TR_KEY_method, "torrent-start"sv);
|
2021-12-17 20:48:02 +00:00
|
|
|
addIdArg(tr_variantDictAddDict(top, Arguments, 1), id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &top);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
case 'S': /* stop */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tadd != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddBool(tr_variantDictFind(tadd, TR_KEY_arguments), TR_KEY_paused, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* top = tr_new0(tr_variant, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(top, 2);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(top, TR_KEY_method, "torrent-stop"sv);
|
2021-12-17 20:48:02 +00:00
|
|
|
addIdArg(tr_variantDictAddDict(top, Arguments, 1), id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &top);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
case 'w':
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_variant* args = tadd != nullptr ? tr_variantDictFind(tadd, TR_KEY_arguments) : ensure_sset(&sset);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(args, TR_KEY_download_dir, optarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-01-15 18:12:45 +00:00
|
|
|
case 850:
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* top = tr_new0(tr_variant, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(top, 1);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(top, TR_KEY_method, "session-close"sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &top);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
case 963:
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* top = tr_new0(tr_variant, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(top, 1);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(top, TR_KEY_method, "blocklist-update"sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &top);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
case 921:
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* top = tr_new0(tr_variant, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(top, 2);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(top, TR_KEY_method, "session-stats"sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(top, TR_KEY_tag, TAG_STATS);
|
|
|
|
status |= flush(rpcurl, &top);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
case 962:
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* top = tr_new0(tr_variant, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(top, 2);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(top, TR_KEY_method, "port-test"sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(top, TR_KEY_tag, TAG_PORTTEST);
|
|
|
|
status |= flush(rpcurl, &top);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-12-12 15:58:06 +00:00
|
|
|
case 600:
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tset != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
addIdArg(tr_variantDictFind(tset, Arguments), id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &tset);
|
|
|
|
}
|
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* top = tr_new0(tr_variant, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(top, 2);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(top, TR_KEY_method, "torrent-reannounce"sv);
|
2021-12-17 20:48:02 +00:00
|
|
|
addIdArg(tr_variantDictAddDict(top, Arguments, 1), id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &top);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
case 'v':
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tset != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
addIdArg(tr_variantDictFind(tset, Arguments), id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &tset);
|
|
|
|
}
|
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* top = tr_new0(tr_variant, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(top, 2);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(top, TR_KEY_method, "torrent-verify"sv);
|
2021-12-17 20:48:02 +00:00
|
|
|
addIdArg(tr_variantDictAddDict(top, Arguments, 1), id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &top);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
case 'r':
|
2016-03-05 17:27:40 +00:00
|
|
|
case 840:
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* top = tr_new0(tr_variant, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(top, 2);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(top, TR_KEY_method, "torrent-remove"sv);
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* args = tr_variantDictAddDict(top, Arguments, 2);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(args, TR_KEY_delete_local_data, c == 840);
|
2021-10-06 16:32:17 +00:00
|
|
|
addIdArg(args, id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &top);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
case 960:
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* top = tr_new0(tr_variant, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(top, 2);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(top, TR_KEY_method, "torrent-set-location"sv);
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* args = tr_variantDictAddDict(top, Arguments, 3);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(args, TR_KEY_location, optarg);
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_move, true);
|
2021-10-06 16:32:17 +00:00
|
|
|
addIdArg(args, id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &top);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-29 03:01:39 +00:00
|
|
|
default:
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "got opt [%d]\n", c);
|
|
|
|
showUsage();
|
|
|
|
break;
|
|
|
|
}
|
2009-05-08 14:56:11 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tadd != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
status |= flush(rpcurl, &tadd);
|
|
|
|
}
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tset != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
addIdArg(tr_variantDictFind(tset, Arguments), id, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
status |= flush(rpcurl, &tset);
|
|
|
|
}
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (sset != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
status |= flush(rpcurl, &sset);
|
|
|
|
}
|
|
|
|
|
2009-05-18 16:05:56 +00:00
|
|
|
return status;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 03:23:53 +00:00
|
|
|
static bool parsePortString(char const* s, int* port)
|
|
|
|
{
|
|
|
|
int const errno_stack = errno;
|
|
|
|
errno = 0;
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
char* end = nullptr;
|
2022-01-17 18:39:50 +00:00
|
|
|
auto const i = int(strtol(s, &end, 10));
|
2021-10-06 16:32:17 +00:00
|
|
|
bool const ok = (end != nullptr) && (*end == '\0') && (errno == 0);
|
2020-11-04 03:23:53 +00:00
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
*port = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = errno_stack;
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* [host:port] or [host] or [port] or [http(s?)://host:port/transmission/] */
|
2021-12-16 02:09:46 +00:00
|
|
|
static void getHostAndPortAndRpcUrl(int* argc, char** argv, std::string* host, int* port, std::string* rpcurl)
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-06-29 06:59:47 +00:00
|
|
|
if (*argv[1] == '-')
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2017-06-29 06:59:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-06-29 06:59:47 +00:00
|
|
|
char const* const s = argv[1];
|
2020-11-04 03:23:53 +00:00
|
|
|
char const* const last_colon = strrchr(s, ':');
|
2017-06-29 06:59:47 +00:00
|
|
|
|
|
|
|
if (strncmp(s, "http://", 7) == 0) /* user passed in http rpc url */
|
|
|
|
{
|
2021-12-16 02:09:46 +00:00
|
|
|
*rpcurl = tr_strvJoin(s + 7, "/rpc/"sv);
|
2017-06-29 06:59:47 +00:00
|
|
|
}
|
|
|
|
else if (strncmp(s, "https://", 8) == 0) /* user passed in https rpc url */
|
|
|
|
{
|
|
|
|
UseSSL = true;
|
2021-12-16 02:09:46 +00:00
|
|
|
*rpcurl = tr_strvJoin(s + 8, "/rpc/"sv);
|
2017-06-29 06:59:47 +00:00
|
|
|
}
|
2020-11-04 03:23:53 +00:00
|
|
|
else if (parsePortString(s, port))
|
|
|
|
{
|
|
|
|
// it was just a port
|
|
|
|
}
|
2021-10-06 16:32:17 +00:00
|
|
|
else if (last_colon == nullptr)
|
2020-11-04 03:23:53 +00:00
|
|
|
{
|
|
|
|
// it was a non-ipv6 host with no port
|
2021-12-16 02:09:46 +00:00
|
|
|
*host = s;
|
2020-11-04 03:23:53 +00:00
|
|
|
}
|
2017-06-29 06:59:47 +00:00
|
|
|
else
|
|
|
|
{
|
2020-11-04 03:23:53 +00:00
|
|
|
char const* hend;
|
2017-06-29 06:59:47 +00:00
|
|
|
|
2020-11-04 03:23:53 +00:00
|
|
|
// if only one colon, it's probably "$host:$port"
|
|
|
|
if ((strchr(s, ':') == last_colon) && parsePortString(last_colon + 1, port))
|
2010-04-29 03:01:39 +00:00
|
|
|
{
|
2020-11-04 03:23:53 +00:00
|
|
|
hend = last_colon;
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-04 03:23:53 +00:00
|
|
|
hend = s + strlen(s);
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
2020-11-04 03:23:53 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
bool const is_unbracketed_ipv6 = (*s != '[') && (memchr(s, ':', hend - s) != nullptr);
|
2020-11-04 03:23:53 +00:00
|
|
|
|
2021-12-16 02:09:46 +00:00
|
|
|
auto const sv = std::string_view{ s, size_t(hend - s) };
|
|
|
|
*host = is_unbracketed_ipv6 ? tr_strvJoin("[", sv, "]") : sv;
|
2017-06-29 06:59:47 +00:00
|
|
|
}
|
2010-04-29 03:01:39 +00:00
|
|
|
|
2017-06-29 06:59:47 +00:00
|
|
|
*argc -= 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-06-29 06:59:47 +00:00
|
|
|
for (int i = 1; i < *argc; ++i)
|
|
|
|
{
|
|
|
|
argv[i] = argv[i + 1];
|
2010-04-29 03:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_main(int argc, char* argv[])
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2021-12-16 02:09:46 +00:00
|
|
|
auto port = DefaultPort;
|
|
|
|
auto host = std::string{};
|
|
|
|
auto rpcurl = std::string{};
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
showUsage();
|
2009-05-18 16:05:56 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2021-12-17 20:48:02 +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
|
|
|
|
|
|
|
getHostAndPortAndRpcUrl(&argc, argv, &host, &port, &rpcurl);
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2021-12-16 02:09:46 +00:00
|
|
|
if (std::empty(host))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-16 02:09:46 +00:00
|
|
|
host = DefaultHost;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 02:09:46 +00:00
|
|
|
if (std::empty(rpcurl))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
rpcurl = tr_strvJoin(host, ":", std::to_string(port), DefaultUrl);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2021-12-16 02:09:46 +00:00
|
|
|
return processArgs(rpcurl.c_str(), argc, (char const* const*)argv);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|