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.
|
2008-05-12 00:41:55 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
2021-11-05 06:29:19 +00:00
|
|
|
#include <array>
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <cerrno>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2021-10-18 14:48:45 +00:00
|
|
|
#include <iterator>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <memory>
|
2021-11-29 01:12:54 +00:00
|
|
|
#include <numeric>
|
2022-04-25 01:49:52 +00:00
|
|
|
#include <set>
|
2021-10-15 13:28:47 +00:00
|
|
|
#include <string_view>
|
2022-02-09 01:04:29 +00:00
|
|
|
#include <utility>
|
2021-10-18 14:48:45 +00:00
|
|
|
#include <vector>
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2022-03-16 00:51:36 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2022-01-17 16:20:12 +00:00
|
|
|
#include <libdeflate.h>
|
2010-10-31 17:16:12 +00:00
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
#include "transmission.h"
|
2022-01-13 02:13:58 +00:00
|
|
|
|
2022-07-21 02:54:41 +00:00
|
|
|
#include "announcer.h"
|
2009-04-13 19:04:21 +00:00
|
|
|
#include "completion.h"
|
2014-12-04 19:58:34 +00:00
|
|
|
#include "crypto-utils.h"
|
2014-07-28 04:13:38 +00:00
|
|
|
#include "error.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2022-06-11 17:06:07 +00:00
|
|
|
#include "peer-mgr.h"
|
2022-01-13 02:13:58 +00:00
|
|
|
#include "quark.h"
|
2009-04-13 19:04:21 +00:00
|
|
|
#include "rpcimpl.h"
|
2016-09-05 19:16:56 +00:00
|
|
|
#include "session-id.h"
|
2022-01-13 02:13:58 +00:00
|
|
|
#include "session.h"
|
2008-05-12 00:41:55 +00:00
|
|
|
#include "torrent.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2019-11-09 23:02:23 +00:00
|
|
|
#include "tr-macros.h"
|
2022-03-26 00:31:27 +00:00
|
|
|
#include "tr-strbuf.h"
|
2008-05-12 00:41:55 +00:00
|
|
|
#include "utils.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2009-04-13 19:04:21 +00:00
|
|
|
#include "version.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "web-utils.h"
|
2022-01-13 02:13:58 +00:00
|
|
|
#include "web.h"
|
2008-05-12 00:41:55 +00:00
|
|
|
|
2021-10-20 02:30:50 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-03-16 00:51:36 +00:00
|
|
|
static auto constexpr RecentlyActiveSeconds = time_t{ 60 };
|
|
|
|
static auto constexpr RpcVersion = int64_t{ 17 };
|
|
|
|
static auto constexpr RpcVersionMin = int64_t{ 14 };
|
|
|
|
static auto constexpr RpcVersionSemver = "5.3.0"sv;
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
enum class TrFormat
|
2019-11-09 23:02:23 +00:00
|
|
|
{
|
2022-02-09 02:25:19 +00:00
|
|
|
Object,
|
|
|
|
Table
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2019-11-09 23:02:23 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
/* For functions that can't be immediately executed, like torrentAdd,
|
|
|
|
* this is the callback data used to pass a response to the caller
|
|
|
|
* when the task is complete */
|
|
|
|
struct tr_rpc_idle_data
|
|
|
|
{
|
2022-08-17 18:11:44 +00:00
|
|
|
tr_variant response = {};
|
|
|
|
tr_session* session = nullptr;
|
|
|
|
tr_variant* args_out = nullptr;
|
|
|
|
tr_rpc_response_func callback = nullptr;
|
|
|
|
void* callback_user_data = nullptr;
|
2009-01-18 15:24:26 +00:00
|
|
|
};
|
|
|
|
|
2022-04-03 18:23:00 +00:00
|
|
|
static auto constexpr SuccessResult = "success"sv;
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2022-04-03 18:23:00 +00:00
|
|
|
static void tr_idle_function_done(struct tr_rpc_idle_data* data, std::string_view result)
|
|
|
|
{
|
2022-08-17 18:11:44 +00:00
|
|
|
tr_variantDictAddStr(&data->response, TR_KEY_result, result);
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2022-08-17 18:11:44 +00:00
|
|
|
(*data->callback)(data->session, &data->response, data->callback_user_data);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&data->response);
|
2022-08-17 18:11:44 +00:00
|
|
|
delete data;
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-18 14:48:45 +00:00
|
|
|
static auto getTorrents(tr_session* session, tr_variant* args)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
auto torrents = std::vector<tr_torrent*>{};
|
|
|
|
|
|
|
|
auto id = int64_t{};
|
2021-11-02 00:18:09 +00:00
|
|
|
auto sv = std::string_view{};
|
2008-05-12 00:41:55 +00:00
|
|
|
|
2021-12-17 05:47:51 +00:00
|
|
|
if (tr_variant* ids = nullptr; tr_variantDictFindList(args, TR_KEY_ids, &ids))
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
size_t const n = tr_variantListSize(ids);
|
2021-10-18 14:48:45 +00:00
|
|
|
torrents.reserve(n);
|
2008-05-12 00:41:55 +00:00
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
tr_variant const* const node = tr_variantListChild(ids, i);
|
2021-10-18 14:48:45 +00:00
|
|
|
tr_torrent* tor = nullptr;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantGetInt(node, &id))
|
|
|
|
{
|
|
|
|
tor = tr_torrentFindFromId(session, id);
|
|
|
|
}
|
2021-11-02 00:18:09 +00:00
|
|
|
else if (tr_variantGetStrView(node, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-01 23:06:29 +00:00
|
|
|
tor = session->torrents().get(sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tor != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
torrents.push_back(tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (tr_variantDictFindInt(args, TR_KEY_ids, &id) || tr_variantDictFindInt(args, TR_KEY_id, &id))
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2021-12-17 05:47:51 +00:00
|
|
|
if (auto* const tor = tr_torrentFindFromId(session, id); tor != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
torrents.push_back(tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2021-11-02 00:18:09 +00:00
|
|
|
else if (tr_variantDictFindStrView(args, TR_KEY_ids, &sv))
|
2009-03-23 00:16:37 +00:00
|
|
|
{
|
2021-11-02 00:18:09 +00:00
|
|
|
if (sv == "recently-active"sv)
|
2009-03-23 00:16:37 +00:00
|
|
|
{
|
2021-12-28 17:23:47 +00:00
|
|
|
time_t const cutoff = tr_time() - RecentlyActiveSeconds;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-01 23:06:29 +00:00
|
|
|
torrents.reserve(std::size(session->torrents()));
|
2021-10-18 14:48:45 +00:00
|
|
|
std::copy_if(
|
2022-03-01 23:06:29 +00:00
|
|
|
std::begin(session->torrents()),
|
|
|
|
std::end(session->torrents()),
|
2021-10-18 14:48:45 +00:00
|
|
|
std::back_inserter(torrents),
|
2021-12-17 05:47:51 +00:00
|
|
|
[&cutoff](auto const* tor) { return tor->anyDate >= cutoff; });
|
2009-03-23 00:16:37 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2009-05-08 17:24:16 +00:00
|
|
|
{
|
2022-03-01 23:06:29 +00:00
|
|
|
auto* const tor = session->torrents().get(sv);
|
2021-10-18 14:48:45 +00:00
|
|
|
if (tor != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
torrents.push_back(tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-08 17:24:16 +00:00
|
|
|
}
|
2009-03-23 00:16:37 +00:00
|
|
|
}
|
2021-10-18 14:48:45 +00:00
|
|
|
else // all of them
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2022-03-01 23:06:29 +00:00
|
|
|
torrents.reserve(std::size(session->torrents()));
|
|
|
|
std::copy(std::begin(session->torrents()), std::end(session->torrents()), std::back_inserter(torrents));
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return torrents;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2021-10-18 14:48:45 +00:00
|
|
|
static void notifyBatchQueueChange(tr_session* session, std::vector<tr_torrent*> const& torrents)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
for (auto* tor : torrents)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-14 15:40:03 +00:00
|
|
|
session->rpcNotify(TR_RPC_TORRENT_CHANGED, tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 15:40:03 +00:00
|
|
|
session->rpcNotify(TR_RPC_SESSION_QUEUE_POSITIONS_CHANGED);
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* queueMoveTop(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
auto const torrents = getTorrents(session, args_in);
|
|
|
|
tr_torrentsQueueMoveTop(std::data(torrents), std::size(torrents));
|
|
|
|
notifyBatchQueueChange(session, torrents);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* queueMoveUp(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
auto const torrents = getTorrents(session, args_in);
|
|
|
|
tr_torrentsQueueMoveUp(std::data(torrents), std::size(torrents));
|
|
|
|
notifyBatchQueueChange(session, torrents);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* queueMoveDown(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
auto const torrents = getTorrents(session, args_in);
|
|
|
|
tr_torrentsQueueMoveDown(std::data(torrents), std::size(torrents));
|
|
|
|
notifyBatchQueueChange(session, torrents);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* queueMoveBottom(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
auto const torrents = getTorrents(session, args_in);
|
|
|
|
tr_torrentsQueueMoveBottom(std::data(torrents), std::size(torrents));
|
|
|
|
notifyBatchQueueChange(session, torrents);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 14:48:45 +00:00
|
|
|
struct CompareTorrentByQueuePosition
|
2015-01-02 11:15:31 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
bool operator()(tr_torrent const* a, tr_torrent const* b) const
|
|
|
|
{
|
|
|
|
return a->queuePosition < b->queuePosition;
|
|
|
|
}
|
|
|
|
};
|
2013-10-20 17:24:57 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* torrentStart(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
auto torrents = getTorrents(session, args_in);
|
|
|
|
std::sort(std::begin(torrents), std::end(torrents), CompareTorrentByQueuePosition{});
|
|
|
|
for (auto* tor : torrents)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tor->isRunning)
|
2010-02-21 23:03:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrentStart(tor);
|
2022-08-14 15:40:03 +00:00
|
|
|
session->rpcNotify(TR_RPC_TORRENT_STARTED, tor);
|
2010-02-21 23:03:29 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* torrentStartNow(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
auto torrents = getTorrents(session, args_in);
|
|
|
|
std::sort(std::begin(torrents), std::end(torrents), CompareTorrentByQueuePosition{});
|
|
|
|
for (auto* tor : torrents)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tor->isRunning)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrentStartNow(tor);
|
2022-08-14 15:40:03 +00:00
|
|
|
session->rpcNotify(TR_RPC_TORRENT_STARTED, tor);
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* torrentStop(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
for (auto* tor : getTorrents(session, args_in))
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-04-25 03:43:26 +00:00
|
|
|
if (tor->isRunning || tor->isQueued() || tor->verifyState() != TR_VERIFY_NONE)
|
2010-02-21 23:03:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->isStopping = true;
|
2022-08-14 15:40:03 +00:00
|
|
|
session->rpcNotify(TR_RPC_TORRENT_STOPPED, tor);
|
2010-02-21 23:03:29 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* torrentRemove(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2008-05-20 23:58:59 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
auto delete_flag = bool{ false };
|
2021-11-17 21:56:50 +00:00
|
|
|
(void)tr_variantDictFindBool(args_in, TR_KEY_delete_local_data, &delete_flag);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-07-27 14:03:13 +00:00
|
|
|
tr_rpc_callback_type const type = delete_flag ? TR_RPC_TORRENT_TRASHING : TR_RPC_TORRENT_REMOVING;
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2021-10-18 14:48:45 +00:00
|
|
|
for (auto* tor : getTorrents(session, args_in))
|
2008-05-20 23:58:59 +00:00
|
|
|
{
|
2022-08-14 15:40:03 +00:00
|
|
|
if (auto const status = session->rpcNotify(type, tor); (status & TR_RPC_NOREMOVE) == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
tr_torrentRemove(tor, delete_flag, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-05-20 23:58:59 +00:00
|
|
|
}
|
2008-12-14 11:21:11 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2008-05-20 23:58:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* torrentReannounce(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2009-03-26 18:06:54 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
for (auto* tor : getTorrents(session, args_in))
|
2009-03-26 18:06:54 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_torrentCanManualUpdate(tor))
|
2009-03-26 18:06:54 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrentManualUpdate(tor);
|
2022-08-14 15:40:03 +00:00
|
|
|
session->rpcNotify(TR_RPC_TORRENT_CHANGED, tor);
|
2009-03-26 18:06:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2009-03-26 18:06:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* torrentVerify(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
for (auto* tor : getTorrents(session, args_in))
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-02-09 18:02:59 +00:00
|
|
|
tr_torrentVerify(tor);
|
2022-08-14 15:40:03 +00:00
|
|
|
session->rpcNotify(TR_RPC_TORRENT_CHANGED, tor);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-12-14 11:21:11 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2019-02-17 09:33:57 +00:00
|
|
|
static void addLabels(tr_torrent const* tor, tr_variant* list)
|
|
|
|
{
|
2021-09-29 19:52:19 +00:00
|
|
|
tr_variantInitList(list, std::size(tor->labels));
|
|
|
|
for (auto const& label : tor->labels)
|
2019-02-17 09:33:57 +00:00
|
|
|
{
|
2022-04-18 05:53:48 +00:00
|
|
|
tr_variantListAddQuark(list, label);
|
2019-02-17 09:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void addFileStats(tr_torrent const* tor, tr_variant* list)
|
2009-03-31 22:13:43 +00:00
|
|
|
{
|
2021-12-06 21:26:04 +00:00
|
|
|
for (tr_file_index_t i = 0, n = tor->fileCount(); i < n; ++i)
|
2009-03-31 22:13:43 +00:00
|
|
|
{
|
2021-11-28 03:17:47 +00:00
|
|
|
auto const file = tr_torrentFile(tor, i);
|
|
|
|
tr_variant* d = tr_variantListAddDict(list, 3);
|
2021-11-28 05:08:56 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_bytesCompleted, file.have);
|
2021-11-28 03:17:47 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_priority, file.priority);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_wanted, file.wanted);
|
2009-03-31 22:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void addFiles(tr_torrent const* tor, tr_variant* list)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2021-12-06 21:26:04 +00:00
|
|
|
for (tr_file_index_t i = 0, n = tor->fileCount(); i < n; ++i)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2021-11-28 03:17:47 +00:00
|
|
|
auto const file = tr_torrentFile(tor, i);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* d = tr_variantListAddDict(list, 3);
|
2021-11-28 03:17:47 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_bytesCompleted, file.have);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_length, file.length);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_name, file.name);
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 08:49:04 +00:00
|
|
|
static void addWebseeds(tr_torrent const* tor, tr_variant* webseeds)
|
2008-06-16 03:47:50 +00:00
|
|
|
{
|
2021-12-16 08:49:04 +00:00
|
|
|
for (size_t i = 0, n = tor->webseedCount(); i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-16 08:49:04 +00:00
|
|
|
tr_variantListAddStr(webseeds, tor->webseed(i));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-06-16 03:47:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
static void addTrackers(tr_torrent const* tor, tr_variant* trackers)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2021-12-15 15:53:20 +00:00
|
|
|
for (auto const& tracker : tor->announceList())
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
auto* const d = tr_variantListAddDict(trackers, 5);
|
2022-04-16 20:41:51 +00:00
|
|
|
tr_variantDictAddQuark(d, TR_KEY_announce, tracker.announce.quark());
|
2021-12-14 21:59:44 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_id, tracker.id);
|
2022-04-16 20:41:51 +00:00
|
|
|
tr_variantDictAddQuark(d, TR_KEY_scrape, tracker.scrape.quark());
|
|
|
|
tr_variantDictAddStrView(d, TR_KEY_sitename, tracker.sitename);
|
2021-12-14 21:59:44 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_tier, tracker.tier);
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:55:52 +00:00
|
|
|
static void addTrackerStats(tr_tracker_view const& tracker, tr_variant* list)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
auto* const d = tr_variantListAddDict(list, 27);
|
2021-12-08 16:55:52 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_announce, tracker.announce);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_announceState, tracker.announceState);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_downloadCount, tracker.downloadCount);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_hasAnnounced, tracker.hasAnnounced);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_hasScraped, tracker.hasScraped);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_host, tracker.host);
|
2022-02-12 17:30:27 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_sitename, tracker.sitename);
|
2021-12-08 16:55:52 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_id, tracker.id);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_isBackup, tracker.isBackup);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_lastAnnouncePeerCount, tracker.lastAnnouncePeerCount);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_lastAnnounceResult, tracker.lastAnnounceResult);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_lastAnnounceStartTime, tracker.lastAnnounceStartTime);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_lastAnnounceSucceeded, tracker.lastAnnounceSucceeded);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_lastAnnounceTime, tracker.lastAnnounceTime);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_lastAnnounceTimedOut, tracker.lastAnnounceTimedOut);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_lastScrapeResult, tracker.lastScrapeResult);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_lastScrapeStartTime, tracker.lastScrapeStartTime);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_lastScrapeSucceeded, tracker.lastScrapeSucceeded);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_lastScrapeTime, tracker.lastScrapeTime);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_lastScrapeTimedOut, tracker.lastScrapeTimedOut);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_leecherCount, tracker.leecherCount);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_nextAnnounceTime, tracker.nextAnnounceTime);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_nextScrapeTime, tracker.nextScrapeTime);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_scrape, tracker.scrape);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_scrapeState, tracker.scrapeState);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_seederCount, tracker.seederCount);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_tier, tracker.tier);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 05:47:51 +00:00
|
|
|
static void addPeers(tr_torrent const* tor, tr_variant* list)
|
2008-08-22 17:59:31 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto peer_count = int{};
|
|
|
|
tr_peer_stat* peers = tr_torrentPeers(tor, &peer_count);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_variantInitList(list, peer_count);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
for (int i = 0; i < peer_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variant* d = tr_variantListAddDict(list, 16);
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_peer_stat const* peer = peers + i;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_address, peer->addr);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_clientName, peer->client);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_clientIsChoked, peer->clientIsChoked);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_clientIsInterested, peer->clientIsInterested);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_flagStr, peer->flagStr);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_isDownloadingFrom, peer->isDownloadingFrom);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_isEncrypted, peer->isEncrypted);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_isIncoming, peer->isIncoming);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_isUploadingTo, peer->isUploadingTo);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_isUTP, peer->isUTP);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_peerIsChoked, peer->peerIsChoked);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_peerIsInterested, peer->peerIsInterested);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_port, peer->port);
|
|
|
|
tr_variantDictAddReal(d, TR_KEY_progress, peer->progress);
|
2021-12-15 21:25:42 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_rateToClient, tr_toSpeedBytes(peer->rateToClient_KBps));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_rateToPeer, tr_toSpeedBytes(peer->rateToPeer_KBps));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_torrentPeersFree(peers, peer_count);
|
2008-08-22 17:59:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
static void initField(tr_torrent const* const tor, tr_stat const* const st, tr_variant* const initme, tr_quark key)
|
2008-05-12 13:05:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (key)
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_activityDate:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->activityDate);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_addedDate:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->addedDate);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2022-06-11 17:06:07 +00:00
|
|
|
case TR_KEY_availability:
|
|
|
|
tr_variantInitList(initme, tor->pieceCount());
|
|
|
|
for (tr_piece_index_t piece = 0, n = tor->pieceCount(); piece < n; ++piece)
|
|
|
|
{
|
|
|
|
tr_variantListAddInt(initme, tr_peerMgrPieceAvailability(tor, piece));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_bandwidthPriority:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, tr_torrentGetPriority(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_comment:
|
2022-01-08 23:41:05 +00:00
|
|
|
tr_variantInitStr(initme, tor->comment());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_corruptEver:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->corruptEver);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_creator:
|
2022-01-08 23:41:05 +00:00
|
|
|
tr_variantInitStrView(initme, tor->creator());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_dateCreated:
|
2022-01-08 23:41:05 +00:00
|
|
|
tr_variantInitInt(initme, tor->dateCreated());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_desiredAvailable:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->desiredAvailable);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_doneDate:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->doneDate);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_downloadDir:
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantInitStrView(initme, tr_torrentGetDownloadDir(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_downloadedEver:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->downloadedEver);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_downloadLimit:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, tr_torrentGetSpeedLimit_KBps(tor, TR_DOWN));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_downloadLimited:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitBool(initme, tr_torrentUsesSpeedLimit(tor, TR_DOWN));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_error:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->error);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_errorString:
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantInitStrView(initme, st->errorString);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_eta:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->eta);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2020-10-13 15:33:56 +00:00
|
|
|
case TR_KEY_file_count:
|
2021-12-06 21:26:04 +00:00
|
|
|
tr_variantInitInt(initme, tor->fileCount());
|
2020-10-13 15:33:56 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_files:
|
2021-12-06 21:26:04 +00:00
|
|
|
tr_variantInitList(initme, tor->fileCount());
|
2019-11-09 23:02:23 +00:00
|
|
|
addFiles(tor, initme);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_fileStats:
|
2021-12-06 21:26:04 +00:00
|
|
|
tr_variantInitList(initme, tor->fileCount());
|
2019-11-09 23:02:23 +00:00
|
|
|
addFileStats(tor, initme);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2022-03-18 13:11:59 +00:00
|
|
|
case TR_KEY_group:
|
2022-04-27 15:41:28 +00:00
|
|
|
tr_variantInitStrView(initme, tor->bandwidthGroup().sv());
|
2022-03-18 13:11:59 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_hashString:
|
2021-12-24 21:12:33 +00:00
|
|
|
tr_variantInitStrView(initme, tor->infoHashString());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_haveUnchecked:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->haveUnchecked);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_haveValid:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->haveValid);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_honorsSessionLimits:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitBool(initme, tr_torrentUsesSessionLimits(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_id:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->id);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2019-11-12 23:13:42 +00:00
|
|
|
case TR_KEY_editDate:
|
|
|
|
tr_variantInitInt(initme, st->editDate);
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_isFinished:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitBool(initme, st->finished);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_isPrivate:
|
2021-12-15 05:48:56 +00:00
|
|
|
tr_variantInitBool(initme, tor->isPrivate());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_isStalled:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitBool(initme, st->isStalled);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2019-02-17 09:33:57 +00:00
|
|
|
case TR_KEY_labels:
|
2019-11-09 23:02:23 +00:00
|
|
|
addLabels(tor, initme);
|
2019-02-17 09:33:57 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_leftUntilDone:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->leftUntilDone);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_manualAnnounceTime:
|
2022-07-21 02:54:41 +00:00
|
|
|
tr_variantInitInt(initme, tr_announcerNextManualAnnounce(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_maxConnectedPeers:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, tr_torrentGetPeerLimit(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_magnetLink:
|
2022-07-28 04:31:41 +00:00
|
|
|
tr_variantInitStr(initme, tor->metainfo_.magnet());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_metadataPercentComplete:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitReal(initme, st->metadataPercentComplete);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_name:
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantInitStrView(initme, tr_torrentName(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-13 07:27:47 +00:00
|
|
|
case TR_KEY_percentComplete:
|
|
|
|
tr_variantInitReal(initme, st->percentComplete);
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_percentDone:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitReal(initme, st->percentDone);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peer_limit:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, tr_torrentGetPeerLimit(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peers:
|
2019-11-09 23:02:23 +00:00
|
|
|
addPeers(tor, initme);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peersConnected:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->peersConnected);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peersFrom:
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitDict(initme, 7);
|
2022-04-28 01:06:51 +00:00
|
|
|
auto const* f = st->peersFrom;
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantDictAddInt(initme, TR_KEY_fromCache, f[TR_PEER_FROM_RESUME]);
|
|
|
|
tr_variantDictAddInt(initme, TR_KEY_fromDht, f[TR_PEER_FROM_DHT]);
|
|
|
|
tr_variantDictAddInt(initme, TR_KEY_fromIncoming, f[TR_PEER_FROM_INCOMING]);
|
|
|
|
tr_variantDictAddInt(initme, TR_KEY_fromLpd, f[TR_PEER_FROM_LPD]);
|
|
|
|
tr_variantDictAddInt(initme, TR_KEY_fromLtep, f[TR_PEER_FROM_LTEP]);
|
|
|
|
tr_variantDictAddInt(initme, TR_KEY_fromPex, f[TR_PEER_FROM_PEX]);
|
|
|
|
tr_variantDictAddInt(initme, TR_KEY_fromTracker, f[TR_PEER_FROM_TRACKER]);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peersGettingFromUs:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->peersGettingFromUs);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peersSendingToUs:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->peersSendingToUs);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_pieces:
|
2022-03-29 04:29:35 +00:00
|
|
|
if (tor->hasMetainfo())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
auto const bytes = tor->createPieceBitfield();
|
2022-01-08 12:46:25 +00:00
|
|
|
auto const enc = tr_base64_encode({ reinterpret_cast<char const*>(std::data(bytes)), std::size(bytes) });
|
2022-03-12 16:39:34 +00:00
|
|
|
tr_variantInitStr(initme, enc);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantInitStrView(initme, ""sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_pieceCount:
|
2022-01-08 23:41:05 +00:00
|
|
|
tr_variantInitInt(initme, tor->pieceCount());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_pieceSize:
|
2022-01-08 23:41:05 +00:00
|
|
|
tr_variantInitInt(initme, tor->pieceSize());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2020-10-13 15:33:56 +00:00
|
|
|
case TR_KEY_primary_mime_type:
|
2021-12-16 09:43:51 +00:00
|
|
|
tr_variantInitStrView(initme, tor->primaryMimeType());
|
2020-10-13 15:33:56 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_priorities:
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2021-12-06 21:26:04 +00:00
|
|
|
auto const n = tor->fileCount();
|
2021-11-28 03:17:47 +00:00
|
|
|
tr_variantInitList(initme, n);
|
|
|
|
for (tr_file_index_t i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
tr_variantListAddInt(initme, tr_torrentFile(tor, i).priority);
|
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
2019-11-09 23:02:23 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_queuePosition:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->queuePosition);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_etaIdle:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->etaIdle);
|
2013-01-13 20:39:56 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_rateDownload:
|
2021-12-15 21:25:42 +00:00
|
|
|
tr_variantInitInt(initme, tr_toSpeedBytes(st->pieceDownloadSpeed_KBps));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_rateUpload:
|
2021-12-15 21:25:42 +00:00
|
|
|
tr_variantInitInt(initme, tr_toSpeedBytes(st->pieceUploadSpeed_KBps));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_recheckProgress:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitReal(initme, st->recheckProgress);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seedIdleLimit:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, tr_torrentGetIdleLimit(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seedIdleMode:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, tr_torrentGetIdleMode(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seedRatioLimit:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitReal(initme, tr_torrentGetRatioLimit(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seedRatioMode:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, tr_torrentGetRatioMode(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_sizeWhenDone:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->sizeWhenDone);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2021-10-18 23:05:39 +00:00
|
|
|
case TR_KEY_source:
|
2022-01-08 23:41:05 +00:00
|
|
|
tr_variantInitStrView(initme, tor->source());
|
2021-10-18 23:05:39 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_startDate:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->startDate);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_status:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->activity);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_secondsDownloading:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->secondsDownloading);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_secondsSeeding:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->secondsSeeding);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_trackers:
|
2021-12-14 20:59:40 +00:00
|
|
|
tr_variantInitList(initme, tor->trackerCount());
|
|
|
|
addTrackers(tor, initme);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-18 17:52:01 +00:00
|
|
|
case TR_KEY_trackerList:
|
|
|
|
tr_variantInitStr(initme, tor->trackerList());
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_trackerStats:
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2021-12-08 16:55:52 +00:00
|
|
|
auto const n = tr_torrentTrackerCount(tor);
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitList(initme, n);
|
2021-12-08 16:55:52 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
auto const& tracker = tr_torrentTracker(tor, i);
|
|
|
|
addTrackerStats(tracker, initme);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_torrentFile:
|
2022-03-28 22:13:32 +00:00
|
|
|
tr_variantInitStr(initme, tor->torrentFile());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_totalSize:
|
2022-01-08 23:41:05 +00:00
|
|
|
tr_variantInitInt(initme, tor->totalSize());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_uploadedEver:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->uploadedEver);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_uploadLimit:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, tr_torrentGetSpeedLimit_KBps(tor, TR_UP));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_uploadLimited:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitBool(initme, tr_torrentUsesSpeedLimit(tor, TR_UP));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_uploadRatio:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitReal(initme, st->ratio);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_wanted:
|
2019-11-09 23:02:23 +00:00
|
|
|
{
|
2021-12-06 21:26:04 +00:00
|
|
|
auto const n = tor->fileCount();
|
2021-11-28 03:17:47 +00:00
|
|
|
tr_variantInitList(initme, n);
|
|
|
|
for (tr_file_index_t i = 0; i < n; ++i)
|
|
|
|
{
|
2022-02-07 04:28:36 +00:00
|
|
|
tr_variantListAddBool(initme, tr_torrentFile(tor, i).wanted);
|
2021-11-28 03:17:47 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
2019-11-09 23:02:23 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_webseeds:
|
2021-12-16 08:49:04 +00:00
|
|
|
tr_variantInitList(initme, tor->webseedCount());
|
|
|
|
addWebseeds(tor, initme);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_webseedsSendingToUs:
|
2019-11-09 23:02:23 +00:00
|
|
|
tr_variantInitInt(initme, st->webseedsSendingToUs);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-07-26 14:47:07 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
static void addTorrentInfo(tr_torrent* tor, TrFormat format, tr_variant* entry, tr_quark const* fields, size_t field_count)
|
2008-07-26 14:47:07 +00:00
|
|
|
{
|
2022-02-09 02:25:19 +00:00
|
|
|
if (format == TrFormat::Table)
|
2019-11-09 23:02:23 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_variantInitList(entry, field_count);
|
2019-11-09 23:02:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_variantInitDict(entry, field_count);
|
2019-11-09 23:02:23 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (field_count > 0)
|
2011-05-09 04:13:14 +00:00
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
tr_stat const* const st = tr_torrentStat(tor);
|
2011-05-09 04:13:14 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
for (size_t i = 0; i < field_count; ++i)
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2022-02-09 02:25:19 +00:00
|
|
|
tr_variant* child = format == TrFormat::Table ? tr_variantListAdd(entry) : tr_variantDictAdd(entry, fields[i]);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-01-08 23:41:05 +00:00
|
|
|
initField(tor, st, child, fields[i]);
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
2011-05-09 04:13:14 +00:00
|
|
|
}
|
2008-06-16 03:47:50 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static char const* torrentGet(tr_session* session, tr_variant* args_in, tr_variant* args_out, tr_rpc_idle_data* /*idle_data*/)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
auto const torrents = getTorrents(session, args_in);
|
|
|
|
tr_variant* const list = tr_variantDictAddList(args_out, TR_KEY_torrents, std::size(torrents) + 1);
|
2019-11-09 23:02:23 +00:00
|
|
|
|
2021-11-05 06:29:19 +00:00
|
|
|
auto sv = std::string_view{};
|
2022-02-09 02:25:19 +00:00
|
|
|
auto const format = tr_variantDictFindStrView(args_in, TR_KEY_format, &sv) && sv == "table"sv ? TrFormat::Table :
|
|
|
|
TrFormat::Object;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-05 06:29:19 +00:00
|
|
|
if (tr_variantDictFindStrView(args_in, TR_KEY_ids, &sv) && sv == "recently-active"sv)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-18 19:24:20 +00:00
|
|
|
auto const cutoff = tr_time() - RecentlyActiveSeconds;
|
|
|
|
auto const ids = session->torrents().removedSince(cutoff);
|
|
|
|
auto* const out = tr_variantDictAddList(args_out, TR_KEY_removed, std::size(ids));
|
|
|
|
for (auto const& id : ids)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-18 19:24:20 +00:00
|
|
|
tr_variantListAddInt(out, id);
|
2009-04-04 05:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 14:48:45 +00:00
|
|
|
tr_variant* fields = nullptr;
|
|
|
|
char const* errmsg = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_variantDictFindList(args_in, TR_KEY_fields, &fields))
|
|
|
|
{
|
|
|
|
errmsg = "no fields specified";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-26 14:36:00 +00:00
|
|
|
auto const n = tr_variantListSize(fields);
|
|
|
|
auto keys = std::vector<tr_quark>{};
|
|
|
|
keys.reserve(n);
|
|
|
|
|
2019-11-09 23:02:23 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
if (!tr_variantGetStrView(tr_variantListChild(fields, i), &sv))
|
2019-11-09 23:02:23 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-26 14:36:00 +00:00
|
|
|
if (auto const key = tr_quark_lookup(sv); key)
|
2021-11-05 06:29:19 +00:00
|
|
|
{
|
2022-05-26 14:36:00 +00:00
|
|
|
keys.emplace_back(*key);
|
2019-11-09 23:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
if (format == TrFormat::Table)
|
2019-11-09 23:02:23 +00:00
|
|
|
{
|
|
|
|
/* first entry is an array of property names */
|
2022-05-26 14:36:00 +00:00
|
|
|
tr_variant* names = tr_variantListAddList(list, std::size(keys));
|
|
|
|
for (auto const& key : keys)
|
2019-11-09 23:02:23 +00:00
|
|
|
{
|
2022-05-26 14:36:00 +00:00
|
|
|
tr_variantListAddQuark(names, key);
|
2019-11-09 23:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 14:48:45 +00:00
|
|
|
for (auto* tor : torrents)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-05-26 14:36:00 +00:00
|
|
|
addTorrentInfo(tor, format, tr_variantListAdd(list), std::data(keys), std::size(keys));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return errmsg;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2008-06-16 03:47:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-04-18 05:53:48 +00:00
|
|
|
static std::pair<std::vector<tr_quark>, char const* /*errmsg*/> makeLabels(tr_variant* list)
|
2019-02-17 09:33:57 +00:00
|
|
|
{
|
2022-04-18 05:53:48 +00:00
|
|
|
auto labels = std::vector<tr_quark>{};
|
2021-10-20 16:31:20 +00:00
|
|
|
size_t const n = tr_variantListSize(list);
|
2022-04-18 05:53:48 +00:00
|
|
|
labels.reserve(n);
|
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
2019-02-17 09:33:57 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
auto label = std::string_view{};
|
|
|
|
if (!tr_variantGetStrView(tr_variantListChild(list, i), &label))
|
2019-02-17 09:33:57 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-02-17 09:33:57 +00:00
|
|
|
|
2021-11-10 00:13:47 +00:00
|
|
|
label = tr_strvStrip(label);
|
2021-11-05 06:29:19 +00:00
|
|
|
if (std::empty(label))
|
|
|
|
{
|
2022-02-09 01:04:29 +00:00
|
|
|
return { {}, "labels cannot be empty" };
|
2021-11-05 06:29:19 +00:00
|
|
|
}
|
2019-02-17 09:33:57 +00:00
|
|
|
|
2021-11-10 00:13:47 +00:00
|
|
|
if (tr_strvContains(label, ','))
|
2021-11-05 06:29:19 +00:00
|
|
|
{
|
2022-02-09 01:04:29 +00:00
|
|
|
return { {}, "labels cannot contain comma (,) character" };
|
2019-02-17 09:33:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 05:53:48 +00:00
|
|
|
labels.emplace_back(tr_quark_new(label));
|
2022-02-03 06:02:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 01:04:29 +00:00
|
|
|
return { labels, nullptr };
|
2022-02-03 06:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char const* setLabels(tr_torrent* tor, tr_variant* list)
|
|
|
|
{
|
2022-02-09 01:04:29 +00:00
|
|
|
auto [labels, errmsg] = makeLabels(list);
|
2022-02-03 06:02:30 +00:00
|
|
|
|
2022-02-09 01:04:29 +00:00
|
|
|
if (errmsg != nullptr)
|
2022-02-03 06:02:30 +00:00
|
|
|
{
|
|
|
|
return errmsg;
|
2019-02-17 09:33:57 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 22:08:42 +00:00
|
|
|
tor->setLabels(labels);
|
2021-11-05 06:29:19 +00:00
|
|
|
return nullptr;
|
2019-02-17 09:33:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 23:11:57 +00:00
|
|
|
static char const* setFilePriorities(tr_torrent* tor, tr_priority_t priority, tr_variant* list)
|
2008-05-12 13:05:06 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
char const* errmsg = nullptr;
|
2021-12-06 21:26:04 +00:00
|
|
|
auto const n_files = tor->fileCount();
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
auto files = std::vector<tr_file_index_t>{};
|
2021-12-06 21:26:04 +00:00
|
|
|
files.reserve(n_files);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-12-01 23:11:57 +00:00
|
|
|
if (size_t const n = tr_variantListSize(list); n != 0)
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
auto tmp = int64_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantGetInt(tr_variantListChild(list, i), &tmp))
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2021-12-06 21:26:04 +00:00
|
|
|
if (0 <= tmp && tmp < n_files)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-01 23:11:57 +00:00
|
|
|
files.push_back(tr_file_index_t(tmp));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errmsg = "file index out of range";
|
|
|
|
}
|
2009-03-03 20:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
2021-11-29 01:12:54 +00:00
|
|
|
else // if empty set, apply to all
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
2021-12-06 21:26:04 +00:00
|
|
|
files.resize(n_files);
|
|
|
|
std::iota(std::begin(files), std::end(files), 0);
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
tor->setFilePriorities(std::data(files), std::size(files), priority);
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return errmsg;
|
2008-05-12 13:05:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
static char const* setFileDLs(tr_torrent* tor, bool wanted, tr_variant* list)
|
2008-05-12 13:05:06 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
char const* errmsg = nullptr;
|
|
|
|
|
2021-12-06 21:26:04 +00:00
|
|
|
auto const n_files = tor->fileCount();
|
2021-11-29 01:12:54 +00:00
|
|
|
size_t const n_items = tr_variantListSize(list);
|
|
|
|
|
|
|
|
auto files = std::vector<tr_file_index_t>{};
|
|
|
|
files.reserve(n_files);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
if (n_items != 0) // if argument list, process them
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
2021-11-29 01:12:54 +00:00
|
|
|
for (size_t i = 0; i < n_items; ++i)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
auto tmp = int64_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantGetInt(tr_variantListChild(list, i), &tmp))
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2021-12-06 21:26:04 +00:00
|
|
|
if (0 <= tmp && tmp < n_files)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-29 01:12:54 +00:00
|
|
|
files.push_back(tmp);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errmsg = "file index out of range";
|
|
|
|
}
|
2009-03-03 20:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
2021-11-29 01:12:54 +00:00
|
|
|
else // if empty set, apply to all
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
2021-11-29 01:12:54 +00:00
|
|
|
files.resize(n_files);
|
|
|
|
std::iota(std::begin(files), std::end(files), 0);
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
tor->setFilesWanted(std::data(files), std::size(files), wanted);
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return errmsg;
|
2008-05-12 13:05:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* addTrackerUrls(tr_torrent* tor, tr_variant* urls)
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2021-12-14 20:59:40 +00:00
|
|
|
auto const old_size = tor->trackerCount();
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
for (size_t i = 0, n = tr_variantListSize(urls); i < n; ++i)
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2021-12-14 20:59:40 +00:00
|
|
|
auto announce = std::string_view();
|
2022-04-02 22:42:51 +00:00
|
|
|
auto const* const val = tr_variantListChild(urls, i);
|
2021-12-14 20:59:40 +00:00
|
|
|
if (val == nullptr || !tr_variantGetStrView(val, &announce))
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2021-12-14 20:59:40 +00:00
|
|
|
continue;
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
2019-03-17 05:00:15 +00:00
|
|
|
|
2022-01-29 21:37:42 +00:00
|
|
|
tor->announceList().add(announce);
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
if (tor->trackerCount() == old_size)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-14 20:59:40 +00:00
|
|
|
return "error setting announce list";
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2021-12-15 15:53:20 +00:00
|
|
|
tor->announceList().save(tor->torrentFile());
|
2022-03-28 22:13:32 +00:00
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
return nullptr;
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* replaceTrackers(tr_torrent* tor, tr_variant* urls)
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2021-12-14 20:59:40 +00:00
|
|
|
auto changed = bool{ false };
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2019-03-17 06:09:08 +00:00
|
|
|
for (size_t i = 0, url_count = tr_variantListSize(urls); i + 1 < url_count; i += 2)
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2021-12-14 20:59:40 +00:00
|
|
|
auto id = int64_t{};
|
2021-11-05 06:29:19 +00:00
|
|
|
auto newval = std::string_view{};
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
if (tr_variantGetInt(tr_variantListChild(urls, i), &id) &&
|
|
|
|
tr_variantGetStrView(tr_variantListChild(urls, i + 1), &newval))
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2021-12-15 15:53:20 +00:00
|
|
|
changed |= tor->announceList().replace(id, newval);
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!changed)
|
|
|
|
{
|
2021-12-14 20:59:40 +00:00
|
|
|
return "error setting announce list";
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2021-12-15 15:53:20 +00:00
|
|
|
tor->announceList().save(tor->torrentFile());
|
2022-03-28 22:13:32 +00:00
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
return nullptr;
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* removeTrackers(tr_torrent* tor, tr_variant* ids)
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2021-12-14 20:59:40 +00:00
|
|
|
auto const old_size = tor->trackerCount();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
for (size_t i = 0, n = tr_variantListSize(ids); i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-14 20:59:40 +00:00
|
|
|
auto id = int64_t{};
|
2022-04-02 22:42:51 +00:00
|
|
|
auto const* const val = tr_variantListChild(ids, i);
|
2021-12-14 20:59:40 +00:00
|
|
|
if (val == nullptr || !tr_variantGetInt(val, &id))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-15 15:53:20 +00:00
|
|
|
tor->announceList().remove(id);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
if (tor->trackerCount() == old_size)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-14 20:59:40 +00:00
|
|
|
return "error setting announce list";
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 15:53:20 +00:00
|
|
|
tor->announceList().save(tor->torrentFile());
|
2022-03-28 22:13:32 +00:00
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
return nullptr;
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* torrentSet(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
char const* errmsg = nullptr;
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2021-10-18 14:48:45 +00:00
|
|
|
for (auto* tor : getTorrents(session, args_in))
|
2008-05-12 13:05:06 +00:00
|
|
|
{
|
2021-10-18 14:48:45 +00:00
|
|
|
auto tmp = int64_t{};
|
|
|
|
auto d = double{};
|
|
|
|
tr_variant* tmp_variant = nullptr;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_bandwidthPriority, &tmp))
|
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto const priority = tr_priority_t(tmp);
|
2020-11-01 21:47:57 +00:00
|
|
|
|
|
|
|
if (tr_isPriority(priority))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
tr_torrentSetPriority(tor, priority);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-03-18 13:11:59 +00:00
|
|
|
if (std::string_view group; tr_variantDictFindStrView(args_in, TR_KEY_group, &group))
|
|
|
|
{
|
2022-04-27 15:41:28 +00:00
|
|
|
tor->setBandwidthGroup(group);
|
2022-03-18 13:11:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (errmsg == nullptr && tr_variantDictFindList(args_in, TR_KEY_labels, &tmp_variant))
|
2019-02-17 09:33:57 +00:00
|
|
|
{
|
|
|
|
errmsg = setLabels(tor, tmp_variant);
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (errmsg == nullptr && tr_variantDictFindList(args_in, TR_KEY_files_unwanted, &tmp_variant))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-02-17 09:33:57 +00:00
|
|
|
errmsg = setFileDLs(tor, false, tmp_variant);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (errmsg == nullptr && tr_variantDictFindList(args_in, TR_KEY_files_wanted, &tmp_variant))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-02-17 09:33:57 +00:00
|
|
|
errmsg = setFileDLs(tor, true, tmp_variant);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_peer_limit, &tmp))
|
|
|
|
{
|
|
|
|
tr_torrentSetPeerLimit(tor, tmp);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (errmsg == nullptr && tr_variantDictFindList(args_in, TR_KEY_priority_high, &tmp_variant))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-02-17 09:33:57 +00:00
|
|
|
errmsg = setFilePriorities(tor, TR_PRI_HIGH, tmp_variant);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (errmsg == nullptr && tr_variantDictFindList(args_in, TR_KEY_priority_low, &tmp_variant))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-02-17 09:33:57 +00:00
|
|
|
errmsg = setFilePriorities(tor, TR_PRI_LOW, tmp_variant);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (errmsg == nullptr && tr_variantDictFindList(args_in, TR_KEY_priority_normal, &tmp_variant))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-02-17 09:33:57 +00:00
|
|
|
errmsg = setFilePriorities(tor, TR_PRI_NORMAL, tmp_variant);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_downloadLimit, &tmp))
|
|
|
|
{
|
|
|
|
tr_torrentSetSpeedLimit_KBps(tor, TR_DOWN, tmp);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_downloadLimited, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_torrentUseSpeedLimit(tor, TR_DOWN, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_honorsSessionLimits, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_torrentUseSessionLimits(tor, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_uploadLimit, &tmp))
|
|
|
|
{
|
|
|
|
tr_torrentSetSpeedLimit_KBps(tor, TR_UP, tmp);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_uploadLimited, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_torrentUseSpeedLimit(tor, TR_UP, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_seedIdleLimit, &tmp))
|
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
tr_torrentSetIdleLimit(tor, (uint16_t)tmp);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_seedIdleMode, &tmp))
|
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
tr_torrentSetIdleMode(tor, (tr_idlelimit)tmp);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindReal(args_in, TR_KEY_seedRatioLimit, &d))
|
|
|
|
{
|
|
|
|
tr_torrentSetRatioLimit(tor, d);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_seedRatioMode, &tmp))
|
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
tr_torrentSetRatioMode(tor, (tr_ratiolimit)tmp);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_queuePosition, &tmp))
|
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
tr_torrentSetQueuePosition(tor, (int)tmp);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (errmsg == nullptr && tr_variantDictFindList(args_in, TR_KEY_trackerAdd, &tmp_variant))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-02-17 09:33:57 +00:00
|
|
|
errmsg = addTrackerUrls(tor, tmp_variant);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (errmsg == nullptr && tr_variantDictFindList(args_in, TR_KEY_trackerRemove, &tmp_variant))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-02-17 09:33:57 +00:00
|
|
|
errmsg = removeTrackers(tor, tmp_variant);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (errmsg == nullptr && tr_variantDictFindList(args_in, TR_KEY_trackerReplace, &tmp_variant))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-02-17 09:33:57 +00:00
|
|
|
errmsg = replaceTrackers(tor, tmp_variant);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-02-18 17:52:01 +00:00
|
|
|
if (std::string_view txt; errmsg == nullptr && tr_variantDictFindStrView(args_in, TR_KEY_trackerList, &txt))
|
|
|
|
{
|
|
|
|
if (!tor->setTrackerList(txt))
|
|
|
|
{
|
|
|
|
errmsg = "Invalid tracker list";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 15:40:03 +00:00
|
|
|
session->rpcNotify(TR_RPC_TORRENT_CHANGED, tor);
|
2008-05-12 13:05:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return errmsg;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* torrentSetLocation(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2021-11-15 20:53:53 +00:00
|
|
|
auto location = std::string_view{};
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-11-15 20:53:53 +00:00
|
|
|
if (!tr_variantDictFindStrView(args_in, TR_KEY_location, &location))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return "no location";
|
|
|
|
}
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_sys_path_is_relative(location))
|
|
|
|
{
|
|
|
|
return "new location path is not absolute";
|
|
|
|
}
|
2015-10-20 21:22:19 +00:00
|
|
|
|
2021-10-18 14:48:45 +00:00
|
|
|
auto move = bool{};
|
2021-11-17 21:56:50 +00:00
|
|
|
(void)tr_variantDictFindBool(args_in, TR_KEY_move, &move);
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2021-10-18 14:48:45 +00:00
|
|
|
for (auto* tor : getTorrents(session, args_in))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-15 20:53:53 +00:00
|
|
|
tor->setLocation(location, move, nullptr, nullptr);
|
2022-08-14 15:40:03 +00:00
|
|
|
session->rpcNotify(TR_RPC_TORRENT_MOVED, tor);
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
|
|
|
|
2008-05-12 13:05:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void torrentRenamePathDone(tr_torrent* tor, char const* oldpath, char const* newname, int error, void* user_data)
|
2013-01-20 01:31:58 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct tr_rpc_idle_data*>(user_data);
|
2013-01-20 01:31:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(data->args_out, TR_KEY_id, tr_torrentId(tor));
|
|
|
|
tr_variantDictAddStr(data->args_out, TR_KEY_path, oldpath);
|
|
|
|
tr_variantDictAddStr(data->args_out, TR_KEY_name, newname);
|
2013-01-20 01:31:58 +00:00
|
|
|
|
2022-04-03 18:23:00 +00:00
|
|
|
tr_idle_function_done(data, error != 0 ? tr_strerror(error) : SuccessResult);
|
2013-01-20 01:31:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* torrentRenamePath(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data)
|
2013-01-20 01:31:58 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
char const* errmsg = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-16 04:10:36 +00:00
|
|
|
auto oldpath = std::string_view{};
|
|
|
|
(void)tr_variantDictFindStrView(args_in, TR_KEY_path, &oldpath);
|
|
|
|
auto newname = std::string_view{};
|
|
|
|
(void)tr_variantDictFindStrView(args_in, TR_KEY_name, &newname);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-08 05:44:31 +00:00
|
|
|
if (auto const torrents = getTorrents(session, args_in); std::size(torrents) == 1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-16 04:10:36 +00:00
|
|
|
torrents[0]->renamePath(oldpath, newname, torrentRenamePathDone, idle_data);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errmsg = "torrent-rename-path requires 1 torrent";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
return errmsg;
|
2013-01-20 01:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-02-17 23:35:57 +00:00
|
|
|
static void onPortTested(tr_web::FetchResponse const& web_response)
|
2009-04-06 16:36:00 +00:00
|
|
|
{
|
2022-04-21 14:28:38 +00:00
|
|
|
auto const& [status, body, did_connect, did_timeout, user_data] = web_response;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct tr_rpc_idle_data*>(user_data);
|
2009-04-06 16:36:00 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
if (status != 200)
|
2009-04-06 16:36:00 +00:00
|
|
|
{
|
2022-04-03 18:23:00 +00:00
|
|
|
tr_idle_function_done(
|
|
|
|
data,
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't test port: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", tr_webGetResponseStr(status)),
|
|
|
|
fmt::arg("error_code", status)));
|
2009-04-06 16:36:00 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else /* success */
|
2009-04-06 16:36:00 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
bool const is_open = tr_strvStartsWith(body, '1');
|
|
|
|
tr_variantDictAddBool(data->args_out, TR_KEY_port_is_open, is_open);
|
2022-04-03 18:23:00 +00:00
|
|
|
tr_idle_function_done(data, SuccessResult);
|
2009-04-06 16:36:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
static char const* portTest(
|
|
|
|
tr_session* session,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_in*/,
|
|
|
|
tr_variant* /*args_out*/,
|
2021-10-10 16:52:26 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data)
|
2009-04-06 16:36:00 +00:00
|
|
|
{
|
2022-04-21 15:58:13 +00:00
|
|
|
auto const port = session->peerPort();
|
|
|
|
auto const url = fmt::format(FMT_STRING("https://portcheck.transmissionbt.com/{:d}"), port.host());
|
2022-02-17 23:35:57 +00:00
|
|
|
session->web->fetch({ url, onPortTested, idle_data });
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2009-04-06 16:36:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-02-17 23:35:57 +00:00
|
|
|
static void onBlocklistFetched(tr_web::FetchResponse const& web_response)
|
2009-04-03 23:21:40 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
auto const& [status, body, did_connect, did_timeout, user_data] = web_response;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct tr_rpc_idle_data*>(user_data);
|
2022-02-16 18:33:50 +00:00
|
|
|
auto* const session = data->session;
|
2009-04-03 23:21:40 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
if (status != 200)
|
2009-04-03 23:21:40 +00:00
|
|
|
{
|
2022-01-17 16:20:12 +00:00
|
|
|
// we failed to download the blocklist...
|
2022-04-03 18:23:00 +00:00
|
|
|
tr_idle_function_done(
|
|
|
|
data,
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't fetch blocklist: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", tr_webGetResponseStr(status)),
|
|
|
|
fmt::arg("error_code", status)));
|
2022-01-17 16:20:12 +00:00
|
|
|
return;
|
2009-04-03 23:21:40 +00:00
|
|
|
}
|
2010-10-31 17:16:12 +00:00
|
|
|
|
2022-01-17 16:20:12 +00:00
|
|
|
// see if we need to decompress the content
|
|
|
|
auto content = std::vector<char>{};
|
|
|
|
content.resize(1024 * 128);
|
|
|
|
for (;;)
|
|
|
|
{
|
2022-08-31 00:30:47 +00:00
|
|
|
auto decompressor = std::unique_ptr<libdeflate_decompressor, void (*)(libdeflate_decompressor*)>{
|
|
|
|
libdeflate_alloc_decompressor(),
|
|
|
|
libdeflate_free_decompressor
|
|
|
|
};
|
2022-01-17 16:20:12 +00:00
|
|
|
auto actual_size = size_t{};
|
|
|
|
auto const decompress_result = libdeflate_gzip_decompress(
|
|
|
|
decompressor.get(),
|
2022-02-16 18:33:50 +00:00
|
|
|
std::data(body),
|
|
|
|
std::size(body),
|
2022-01-17 16:20:12 +00:00
|
|
|
std::data(content),
|
|
|
|
std::size(content),
|
|
|
|
&actual_size);
|
|
|
|
if (decompress_result == LIBDEFLATE_INSUFFICIENT_SPACE)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2022-01-17 16:20:12 +00:00
|
|
|
// need a bigger buffer
|
|
|
|
content.resize(content.size() * 2);
|
|
|
|
continue;
|
2013-05-23 00:11:09 +00:00
|
|
|
}
|
2022-01-17 16:20:12 +00:00
|
|
|
if (decompress_result == LIBDEFLATE_BAD_DATA)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2022-01-17 16:20:12 +00:00
|
|
|
// couldn't decompress it; maybe we downloaded an uncompressed file
|
2022-02-16 18:33:50 +00:00
|
|
|
content.assign(std::begin(body), std::end(body));
|
2010-04-19 16:18:12 +00:00
|
|
|
}
|
2022-01-17 16:20:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-04-03 23:21:40 +00:00
|
|
|
|
2022-01-17 16:20:12 +00:00
|
|
|
// tr_blocklistSetContent needs a source file,
|
|
|
|
// so save content into a tmpfile
|
2022-07-23 07:13:18 +00:00
|
|
|
auto const filename = tr_pathbuf{ session->configDir(), "/blocklist.tmp"sv };
|
2022-04-02 14:06:02 +00:00
|
|
|
if (tr_error* error = nullptr; !tr_saveFile(filename, content, &error))
|
2022-01-17 16:20:12 +00:00
|
|
|
{
|
2022-04-03 18:23:00 +00:00
|
|
|
tr_idle_function_done(
|
|
|
|
data,
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't save '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", filename),
|
|
|
|
fmt::arg("error", error->message),
|
|
|
|
fmt::arg("error_code", error->code)));
|
2022-01-17 16:20:12 +00:00
|
|
|
tr_error_clear(&error);
|
|
|
|
return;
|
2009-04-03 23:21:40 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 16:20:12 +00:00
|
|
|
// feed it to the session and give the client a response
|
2022-05-15 16:32:22 +00:00
|
|
|
size_t const rule_count = tr_blocklistSetContent(session, filename);
|
|
|
|
tr_variantDictAddInt(data->args_out, TR_KEY_blocklist_size, static_cast<int64_t>(rule_count));
|
2022-04-02 14:06:02 +00:00
|
|
|
tr_sys_path_remove(filename);
|
2022-04-03 18:23:00 +00:00
|
|
|
tr_idle_function_done(data, SuccessResult);
|
2009-04-03 23:21:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* blocklistUpdate(
|
|
|
|
tr_session* session,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_in*/,
|
|
|
|
tr_variant* /*args_out*/,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data)
|
2009-04-03 23:21:40 +00:00
|
|
|
{
|
2022-02-17 23:35:57 +00:00
|
|
|
session->web->fetch({ session->blocklistUrl(), onBlocklistFetched, idle_data });
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2009-04-03 23:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void addTorrentImpl(struct tr_rpc_idle_data* data, tr_ctor* ctor)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2021-12-24 22:05:17 +00:00
|
|
|
tr_torrent* duplicate_of = nullptr;
|
|
|
|
tr_torrent* tor = tr_torrentNew(ctor, &duplicate_of);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctorFree(ctor);
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2022-04-03 18:23:00 +00:00
|
|
|
if (tor == nullptr && duplicate_of == nullptr)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2022-04-03 18:23:00 +00:00
|
|
|
tr_idle_function_done(data, "invalid or corrupt torrent file"sv);
|
|
|
|
return;
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 18:23:00 +00:00
|
|
|
static auto constexpr Fields = std::array<tr_quark, 3>{ TR_KEY_id, TR_KEY_name, TR_KEY_hashString };
|
|
|
|
if (duplicate_of != nullptr)
|
2013-05-22 20:35:38 +00:00
|
|
|
{
|
2022-04-03 18:23:00 +00:00
|
|
|
addTorrentInfo(
|
|
|
|
duplicate_of,
|
|
|
|
TrFormat::Object,
|
|
|
|
tr_variantDictAdd(data->args_out, TR_KEY_torrent_duplicate),
|
|
|
|
std::data(Fields),
|
|
|
|
std::size(Fields));
|
|
|
|
tr_idle_function_done(data, "duplicate torrent"sv);
|
|
|
|
return;
|
2013-05-22 20:35:38 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 15:40:03 +00:00
|
|
|
data->session->rpcNotify(TR_RPC_TORRENT_ADDED, tor);
|
2022-04-03 18:23:00 +00:00
|
|
|
addTorrentInfo(
|
|
|
|
tor,
|
|
|
|
TrFormat::Object,
|
|
|
|
tr_variantDictAdd(data->args_out, TR_KEY_torrent_added),
|
|
|
|
std::data(Fields),
|
|
|
|
std::size(Fields));
|
|
|
|
tr_idle_function_done(data, SuccessResult);
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct add_torrent_idle_data
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* data;
|
|
|
|
tr_ctor* ctor;
|
2009-01-18 15:24:26 +00:00
|
|
|
};
|
|
|
|
|
2022-02-17 23:35:57 +00:00
|
|
|
static void onMetadataFetched(tr_web::FetchResponse const& web_response)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
auto const& [status, body, did_connect, did_timeout, user_data] = web_response;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct add_torrent_idle_data*>(user_data);
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace(fmt::format(
|
|
|
|
"torrentAdd: HTTP response code was {} ({}); response length was {} bytes",
|
|
|
|
status,
|
|
|
|
tr_webGetResponseStr(status),
|
|
|
|
std::size(body)));
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
if (status == 200 || status == 221) /* http or ftp success.. */
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
tr_ctorSetMetainfo(data->ctor, std::data(body), std::size(body), nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
addTorrentImpl(data->data, data->ctor);
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2022-04-03 18:23:00 +00:00
|
|
|
tr_idle_function_done(
|
|
|
|
data->data,
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't fetch torrent: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", tr_webGetResponseStr(status)),
|
|
|
|
fmt::arg("error_code", status)));
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 18:11:44 +00:00
|
|
|
delete data;
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 23:03:55 +00:00
|
|
|
static bool isCurlURL(std::string_view url)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2021-11-15 23:03:55 +00:00
|
|
|
auto constexpr Schemes = std::array<std::string_view, 4>{ "http"sv, "https"sv, "ftp"sv, "sftp"sv };
|
|
|
|
auto const parsed = tr_urlParse(url);
|
|
|
|
return parsed && std::find(std::begin(Schemes), std::end(Schemes), parsed->scheme) != std::end(Schemes);
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 16:31:20 +00:00
|
|
|
static auto fileListFromList(tr_variant* list)
|
2009-04-02 20:43:42 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
size_t const n = tr_variantListSize(list);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-10-20 16:31:20 +00:00
|
|
|
auto files = std::vector<tr_file_index_t>{};
|
|
|
|
files.reserve(n);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-20 16:31:20 +00:00
|
|
|
auto file_index = int64_t{};
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
if (tr_variantGetInt(tr_variantListChild(list, i), &file_index))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
files.push_back(file_index);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-04-02 20:43:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return files;
|
2009-04-02 20:43:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static char const* torrentAdd(tr_session* session, tr_variant* args_in, tr_variant* /*args_out*/, tr_rpc_idle_data* idle_data)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(idle_data != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-11-15 23:03:55 +00:00
|
|
|
auto filename = std::string_view{};
|
|
|
|
(void)tr_variantDictFindStrView(args_in, TR_KEY_filename, &filename);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-11-15 23:03:55 +00:00
|
|
|
auto metainfo_base64 = std::string_view{};
|
|
|
|
(void)tr_variantDictFindStrView(args_in, TR_KEY_metainfo, &metainfo_base64);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-15 23:03:55 +00:00
|
|
|
if (std::empty(filename) && std::empty(metainfo_base64))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return "no filename or metainfo specified";
|
|
|
|
}
|
2008-05-20 17:33:54 +00:00
|
|
|
|
2021-11-16 05:20:06 +00:00
|
|
|
auto download_dir = std::string_view{};
|
|
|
|
if (tr_variantDictFindStrView(args_in, TR_KEY_download_dir, &download_dir) && tr_sys_path_is_relative(download_dir))
|
2015-10-20 21:22:19 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
return "download directory path is not absolute";
|
2015-10-20 21:22:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 16:31:20 +00:00
|
|
|
auto i = int64_t{};
|
|
|
|
tr_variant* l = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctor* ctor = tr_ctorNew(session);
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* set the optional arguments */
|
2011-03-15 04:22:47 +00:00
|
|
|
|
2021-11-15 01:17:03 +00:00
|
|
|
auto cookies = std::string_view{};
|
|
|
|
(void)tr_variantDictFindStrView(args_in, TR_KEY_cookies, &cookies);
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2021-11-16 05:20:06 +00:00
|
|
|
if (!std::empty(download_dir))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-16 05:20:06 +00:00
|
|
|
auto const sz_download_dir = std::string{ download_dir };
|
|
|
|
tr_ctorSetDownloadDir(ctor, TR_FORCE, sz_download_dir.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_paused, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_ctorSetPaused(ctor, TR_FORCE, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-05-20 17:33:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_peer_limit, &i))
|
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
tr_ctorSetPeerLimit(ctor, TR_FORCE, (uint16_t)i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-02-02 07:48:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_bandwidthPriority, &i))
|
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
tr_ctorSetBandwidthPriority(ctor, (tr_priority_t)i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindList(args_in, TR_KEY_files_unwanted, &l))
|
2015-10-20 21:06:01 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
auto const files = fileListFromList(l);
|
|
|
|
tr_ctorSetFilesWanted(ctor, std::data(files), std::size(files), false);
|
2015-10-20 21:06:01 +00:00
|
|
|
}
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindList(args_in, TR_KEY_files_wanted, &l))
|
2015-10-20 21:06:01 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
auto const files = fileListFromList(l);
|
|
|
|
tr_ctorSetFilesWanted(ctor, std::data(files), std::size(files), true);
|
2015-10-20 21:06:01 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindList(args_in, TR_KEY_priority_low, &l))
|
2015-10-20 21:06:01 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
auto const files = fileListFromList(l);
|
|
|
|
tr_ctorSetFilePriorities(ctor, std::data(files), std::size(files), TR_PRI_LOW);
|
2015-10-20 21:06:01 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindList(args_in, TR_KEY_priority_normal, &l))
|
2015-10-20 21:06:01 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
auto const files = fileListFromList(l);
|
|
|
|
tr_ctorSetFilePriorities(ctor, std::data(files), std::size(files), TR_PRI_NORMAL);
|
2015-10-20 21:06:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindList(args_in, TR_KEY_priority_high, &l))
|
2015-10-20 21:06:01 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
auto const files = fileListFromList(l);
|
|
|
|
tr_ctorSetFilePriorities(ctor, std::data(files), std::size(files), TR_PRI_HIGH);
|
2015-10-20 21:06:01 +00:00
|
|
|
}
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2022-02-03 06:02:30 +00:00
|
|
|
if (tr_variantDictFindList(args_in, TR_KEY_labels, &l))
|
|
|
|
{
|
2022-02-09 01:04:29 +00:00
|
|
|
auto [labels, errmsg] = makeLabels(l);
|
|
|
|
|
|
|
|
if (errmsg != nullptr)
|
2022-02-03 06:02:30 +00:00
|
|
|
{
|
2022-02-09 01:04:29 +00:00
|
|
|
tr_ctorFree(ctor);
|
2022-02-03 06:02:30 +00:00
|
|
|
return errmsg;
|
|
|
|
}
|
|
|
|
|
2022-04-18 05:53:48 +00:00
|
|
|
tr_ctorSetLabels(ctor, std::data(labels), std::size(labels));
|
2022-02-03 06:02:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace(fmt::format("torrentAdd: filename is '{}'", filename));
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (isCurlURL(filename))
|
2015-10-20 21:06:01 +00:00
|
|
|
{
|
2022-08-17 18:11:44 +00:00
|
|
|
auto* const d = new add_torrent_idle_data{ idle_data, ctor };
|
2022-02-17 23:35:57 +00:00
|
|
|
auto options = tr_web::FetchOptions{ filename, onMetadataFetched, d };
|
2022-02-14 02:09:56 +00:00
|
|
|
options.cookies = cookies;
|
2022-02-16 18:33:50 +00:00
|
|
|
session->web->fetch(std::move(options));
|
2015-10-20 21:06:01 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2015-10-20 21:06:01 +00:00
|
|
|
{
|
2021-11-15 23:03:55 +00:00
|
|
|
if (std::empty(filename))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-01-08 12:46:25 +00:00
|
|
|
auto const metainfo = tr_base64_decode(metainfo_base64);
|
2021-12-26 16:25:07 +00:00
|
|
|
tr_ctorSetMetainfo(ctor, std::data(metainfo), std::size(metainfo), nullptr);
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-06-11 20:46:40 +00:00
|
|
|
if (tr_sys_path_exists(tr_pathbuf{ filename }))
|
|
|
|
{
|
|
|
|
tr_ctorSetMetainfoFromFile(ctor, filename);
|
|
|
|
}
|
|
|
|
else
|
2021-11-15 23:03:55 +00:00
|
|
|
{
|
2022-06-11 20:46:40 +00:00
|
|
|
tr_ctorSetMetainfoFromMagnetLink(ctor, filename);
|
2021-11-15 23:03:55 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
addTorrentImpl(idle_data, ctor);
|
2008-05-12 13:05:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2008-05-12 13:05:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-03-18 13:11:59 +00:00
|
|
|
static char const* groupGet(tr_session* s, tr_variant* args_in, tr_variant* args_out, struct tr_rpc_idle_data* /*idle_data*/)
|
|
|
|
{
|
|
|
|
std::set<std::string_view> names;
|
|
|
|
|
|
|
|
if (std::string_view one_name; tr_variantDictFindStrView(args_in, TR_KEY_name, &one_name))
|
|
|
|
{
|
|
|
|
names.insert(one_name);
|
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
else if (tr_variant* names_list = nullptr; tr_variantDictFindList(args_in, TR_KEY_name, &names_list))
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto const names_count = tr_variantListSize(names_list);
|
2022-03-25 05:24:04 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < names_count; ++i)
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto const* const v = tr_variantListChild(names_list, i);
|
2022-03-18 13:11:59 +00:00
|
|
|
if (std::string_view l; tr_variantIsString(v) && tr_variantGetStrView(v, &l))
|
|
|
|
{
|
|
|
|
names.insert(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
auto* const list = tr_variantDictAddList(args_out, TR_KEY_group, 1);
|
|
|
|
for (auto const& [name, group] : s->bandwidth_groups_)
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-03-18 16:34:29 +00:00
|
|
|
if (names.empty() || names.count(name.sv()) > 0)
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
|
|
|
tr_variant* dict = tr_variantListAddDict(list, 5);
|
|
|
|
auto limits = group->getLimits();
|
2022-03-28 02:55:30 +00:00
|
|
|
tr_variantDictAddStr(dict, TR_KEY_name, name);
|
2022-03-18 13:11:59 +00:00
|
|
|
tr_variantDictAddBool(dict, TR_KEY_uploadLimited, limits.up_limited);
|
|
|
|
tr_variantDictAddInt(dict, TR_KEY_uploadLimit, limits.up_limit_KBps);
|
|
|
|
tr_variantDictAddBool(dict, TR_KEY_downloadLimited, limits.down_limited);
|
|
|
|
tr_variantDictAddInt(dict, TR_KEY_downloadLimit, limits.down_limit_KBps);
|
|
|
|
tr_variantDictAddBool(dict, TR_KEY_honorsSessionLimits, group->areParentLimitsHonored(TR_UP));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char const* groupSet(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
struct tr_rpc_idle_data* /*idle_data*/)
|
|
|
|
{
|
2022-03-18 16:34:29 +00:00
|
|
|
auto name = std::string_view{};
|
2022-03-25 05:24:04 +00:00
|
|
|
(void)tr_variantDictFindStrView(args_in, TR_KEY_name, &name);
|
2022-03-18 16:34:29 +00:00
|
|
|
name = tr_strvStrip(name);
|
|
|
|
if (std::empty(name))
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
|
|
|
return "No group name given";
|
|
|
|
}
|
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
auto& group = session->getBandwidthGroup(name);
|
|
|
|
auto limits = group.getLimits();
|
2022-03-18 13:11:59 +00:00
|
|
|
|
2022-03-21 20:22:50 +00:00
|
|
|
(void)tr_variantDictFindBool(args_in, TR_KEY_speed_limit_down_enabled, &limits.down_limited);
|
|
|
|
(void)tr_variantDictFindBool(args_in, TR_KEY_speed_limit_up_enabled, &limits.up_limited);
|
2022-03-18 16:34:29 +00:00
|
|
|
|
|
|
|
if (auto limit = int64_t{}; tr_variantDictFindInt(args_in, TR_KEY_speed_limit_down, &limit))
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-03-18 16:34:29 +00:00
|
|
|
limits.down_limit_KBps = limit;
|
2022-03-18 13:11:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
if (auto limit = int64_t{}; tr_variantDictFindInt(args_in, TR_KEY_speed_limit_up, &limit))
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-03-18 16:34:29 +00:00
|
|
|
limits.up_limit_KBps = limit;
|
2022-03-18 13:11:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
group.setLimits(&limits);
|
2022-03-18 13:11:59 +00:00
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
if (auto honors = bool{}; tr_variantDictFindBool(args_in, TR_KEY_honorsSessionLimits, &honors))
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-03-18 16:34:29 +00:00
|
|
|
group.honorParentLimits(TR_UP, honors);
|
|
|
|
group.honorParentLimits(TR_DOWN, honors);
|
2022-03-18 13:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* sessionSet(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant* args_in,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2021-11-13 18:09:14 +00:00
|
|
|
auto download_dir = std::string_view{};
|
2021-11-13 20:49:10 +00:00
|
|
|
auto incomplete_dir = std::string_view{};
|
2015-11-05 21:48:00 +00:00
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
if (tr_variantDictFindStrView(args_in, TR_KEY_download_dir, &download_dir) && tr_sys_path_is_relative(download_dir))
|
2015-11-05 21:48:00 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
return "download directory path is not absolute";
|
2015-11-05 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 20:49:10 +00:00
|
|
|
if (tr_variantDictFindStrView(args_in, TR_KEY_incomplete_dir, &incomplete_dir) && tr_sys_path_is_relative(incomplete_dir))
|
2015-11-05 21:48:00 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
return "incomplete torrents directory path is not absolute";
|
2015-11-05 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 16:31:20 +00:00
|
|
|
auto d = double{};
|
|
|
|
auto i = int64_t{};
|
2021-11-05 06:29:19 +00:00
|
|
|
auto sv = std::string_view{};
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_cache_size_mb, &i))
|
|
|
|
{
|
|
|
|
tr_sessionSetCacheLimit_MB(session, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_alt_speed_up, &i))
|
|
|
|
{
|
|
|
|
tr_sessionSetAltSpeed_KBps(session, TR_UP, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_alt_speed_down, &i))
|
|
|
|
{
|
|
|
|
tr_sessionSetAltSpeed_KBps(session, TR_DOWN, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_alt_speed_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionUseAltSpeed(session, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_alt_speed_time_begin, &i))
|
|
|
|
{
|
|
|
|
tr_sessionSetAltSpeedBegin(session, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_alt_speed_time_end, &i))
|
|
|
|
{
|
|
|
|
tr_sessionSetAltSpeedEnd(session, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_alt_speed_time_day, &i))
|
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
tr_sessionSetAltSpeedDay(session, tr_sched_day(i));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_alt_speed_time_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionUseAltSpeedTime(session, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_blocklist_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
session->useBlocklist(val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-11-14 01:09:33 +00:00
|
|
|
if (tr_variantDictFindStrView(args_in, TR_KEY_blocklist_url, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 01:09:33 +00:00
|
|
|
session->setBlocklistUrl(sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
if (!std::empty(download_dir))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-13 18:09:14 +00:00
|
|
|
session->setDownloadDir(download_dir);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_queue_stalled_minutes, &i))
|
|
|
|
{
|
|
|
|
tr_sessionSetQueueStalledMinutes(session, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_queue_stalled_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetQueueStalledEnabled(session, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-02-20 17:54:20 +00:00
|
|
|
if (tr_variantDictFindStrView(args_in, TR_KEY_default_trackers, &sv))
|
|
|
|
{
|
|
|
|
session->setDefaultTrackers(sv);
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_download_queue_size, &i))
|
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
tr_sessionSetQueueSize(session, TR_DOWN, (int)i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_download_queue_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetQueueEnabled(session, TR_DOWN, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-11-13 20:49:10 +00:00
|
|
|
if (!std::empty(incomplete_dir))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-13 20:49:10 +00:00
|
|
|
session->setIncompleteDir(incomplete_dir);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_incomplete_dir_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
session->useIncompleteDir(val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_peer_limit_global, &i))
|
|
|
|
{
|
|
|
|
tr_sessionSetPeerLimit(session, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_peer_limit_per_torrent, &i))
|
|
|
|
{
|
|
|
|
tr_sessionSetPeerLimitPerTorrent(session, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_pex_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetPexEnabled(session, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_dht_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetDHTEnabled(session, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_utp_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetUTPEnabled(session, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_lpd_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetLPDEnabled(session, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_peer_port_random_on_start, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetPeerPortRandomOnStart(session, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_peer_port, &i))
|
|
|
|
{
|
2022-08-03 15:07:45 +00:00
|
|
|
tr_sessionSetPeerPort(session, i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_port_forwarding_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetPortForwardingEnabled(session, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_rename_partial_files, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetIncompleteFileNamingEnabled(session, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindReal(args_in, TR_KEY_seedRatioLimit, &d))
|
|
|
|
{
|
|
|
|
tr_sessionSetRatioLimit(session, d);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_seedRatioLimited, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetRatioLimited(session, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_idle_seeding_limit, &i))
|
|
|
|
{
|
|
|
|
tr_sessionSetIdleLimit(session, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_idle_seeding_limit_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetIdleLimited(session, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_start_added_torrents, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetPaused(session, !val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_seed_queue_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetQueueEnabled(session, TR_UP, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_seed_queue_size, &i))
|
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
tr_sessionSetQueueSize(session, TR_UP, (int)i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-02-14 04:07:12 +00:00
|
|
|
for (auto const& [enabled_key, script_key, script] : tr_session::Scripts)
|
2021-10-12 21:32:32 +00:00
|
|
|
{
|
2022-02-14 04:07:12 +00:00
|
|
|
if (auto enabled = bool{}; tr_variantDictFindBool(args_in, enabled_key, &enabled))
|
|
|
|
{
|
|
|
|
session->useScript(script, enabled);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-02-14 04:07:12 +00:00
|
|
|
if (auto file = std::string_view{}; tr_variantDictFindStrView(args_in, script_key, &file))
|
|
|
|
{
|
|
|
|
session->setScript(script, file);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_trash_original_torrent_files, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetDeleteSource(session, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_speed_limit_down, &i))
|
|
|
|
{
|
|
|
|
tr_sessionSetSpeedLimit_KBps(session, TR_DOWN, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_speed_limit_down_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionLimitSpeed(session, TR_DOWN, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_speed_limit_up, &i))
|
|
|
|
{
|
|
|
|
tr_sessionSetSpeedLimit_KBps(session, TR_UP, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_speed_limit_up_enabled, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionLimitSpeed(session, TR_UP, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-11-05 06:29:19 +00:00
|
|
|
if (tr_variantDictFindStrView(args_in, TR_KEY_encryption, &sv))
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
if (sv == "required"sv)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_sessionSetEncryption(session, TR_ENCRYPTION_REQUIRED);
|
|
|
|
}
|
2021-11-05 06:29:19 +00:00
|
|
|
else if (sv == "tolerated"sv)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_sessionSetEncryption(session, TR_CLEAR_PREFERRED);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_sessionSetEncryption(session, TR_ENCRYPTION_PREFERRED);
|
|
|
|
}
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 18:43:42 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_anti_brute_force_threshold, &i))
|
|
|
|
{
|
|
|
|
tr_sessionSetAntiBruteForceThreshold(session, i);
|
|
|
|
}
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_anti_brute_force_enabled, &val))
|
2020-11-14 18:43:42 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetAntiBruteForceEnabled(session, val);
|
2020-11-14 18:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 15:40:03 +00:00
|
|
|
session->rpcNotify(TR_RPC_SESSION_CHANGED, nullptr);
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* sessionStats(
|
|
|
|
tr_session* session,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_in*/,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_variant* args_out,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2008-06-18 22:01:15 +00:00
|
|
|
{
|
2022-03-01 23:06:29 +00:00
|
|
|
auto const& torrents = session->torrents();
|
2022-03-24 05:18:41 +00:00
|
|
|
auto const total = std::size(torrents);
|
|
|
|
auto const running = std::count_if(
|
|
|
|
std::begin(torrents),
|
|
|
|
std::end(torrents),
|
|
|
|
[](auto const* tor) { return tor->isRunning; });
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(args_out, TR_KEY_activeTorrentCount, running);
|
2022-08-14 19:41:57 +00:00
|
|
|
tr_variantDictAddReal(args_out, TR_KEY_downloadSpeed, session->pieceSpeedBps(TR_DOWN));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(args_out, TR_KEY_pausedTorrentCount, total - running);
|
|
|
|
tr_variantDictAddInt(args_out, TR_KEY_torrentCount, total);
|
2022-08-14 19:41:57 +00:00
|
|
|
tr_variantDictAddReal(args_out, TR_KEY_uploadSpeed, session->pieceSpeedBps(TR_UP));
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
auto stats = session->stats().cumulative();
|
2021-09-12 17:41:49 +00:00
|
|
|
tr_variant* d = tr_variantDictAddDict(args_out, TR_KEY_cumulative_stats, 5);
|
2022-07-28 23:56:40 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_downloadedBytes, stats.downloadedBytes);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_filesAdded, stats.filesAdded);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_secondsActive, stats.secondsActive);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_sessionCount, stats.sessionCount);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_uploadedBytes, stats.uploadedBytes);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
stats = session->stats().current();
|
2017-04-19 12:04:45 +00:00
|
|
|
d = tr_variantDictAddDict(args_out, TR_KEY_current_stats, 5);
|
2022-07-28 23:56:40 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_downloadedBytes, stats.downloadedBytes);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_filesAdded, stats.filesAdded);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_secondsActive, stats.secondsActive);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_sessionCount, stats.sessionCount);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_uploadedBytes, stats.uploadedBytes);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2008-06-18 22:01:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 16:31:20 +00:00
|
|
|
static constexpr std::string_view getEncryptionModeString(tr_encryption_mode mode)
|
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case TR_CLEAR_PREFERRED:
|
|
|
|
return "tolerated"sv;
|
|
|
|
|
|
|
|
case TR_ENCRYPTION_REQUIRED:
|
|
|
|
return "required"sv;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "preferred"sv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 22:19:24 +00:00
|
|
|
static void addSessionField(tr_session const* s, tr_variant* d, tr_quark key)
|
2016-09-04 10:16:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (key)
|
2016-09-04 10:16:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_alt_speed_up:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetAltSpeed_KBps(s, TR_UP));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_alt_speed_down:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetAltSpeed_KBps(s, TR_DOWN));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_alt_speed_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionUsesAltSpeed(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_alt_speed_time_begin:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetAltSpeedBegin(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_alt_speed_time_end:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetAltSpeedEnd(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_alt_speed_time_day:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetAltSpeedDay(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_alt_speed_time_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionUsesAltSpeedTime(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_blocklist_enabled:
|
2021-11-14 01:09:33 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->useBlocklist());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_blocklist_url:
|
2021-11-14 01:09:33 +00:00
|
|
|
tr_variantDictAddStr(d, key, s->blocklistUrl());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_cache_size_mb:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetCacheLimit_MB(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_blocklist_size:
|
|
|
|
tr_variantDictAddInt(d, key, tr_blocklistGetRuleCount(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_config_dir:
|
2022-07-23 07:13:18 +00:00
|
|
|
tr_variantDictAddStr(d, key, s->configDir());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-20 17:54:20 +00:00
|
|
|
case TR_KEY_default_trackers:
|
2022-02-20 19:00:52 +00:00
|
|
|
tr_variantDictAddStr(d, key, s->defaultTrackersStr());
|
2022-02-20 17:54:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_download_dir:
|
2021-11-14 01:09:33 +00:00
|
|
|
tr_variantDictAddStr(d, key, s->downloadDir());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_download_dir_free_space:
|
2021-11-13 18:09:14 +00:00
|
|
|
tr_variantDictAddInt(d, key, tr_dirSpace(s->downloadDir()).free);
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_download_queue_enabled:
|
2022-08-13 19:56:10 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->queueEnabled(TR_DOWN));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_download_queue_size:
|
2022-08-13 19:56:10 +00:00
|
|
|
tr_variantDictAddInt(d, key, s->queueSize(TR_DOWN));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peer_limit_global:
|
2022-08-13 22:41:42 +00:00
|
|
|
tr_variantDictAddInt(d, key, s->peerLimit());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peer_limit_per_torrent:
|
2022-08-13 22:41:42 +00:00
|
|
|
tr_variantDictAddInt(d, key, s->peerLimitPerTorrent());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_incomplete_dir:
|
2021-11-14 01:09:33 +00:00
|
|
|
tr_variantDictAddStr(d, key, s->incompleteDir());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_incomplete_dir_enabled:
|
2021-11-14 01:09:33 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->useIncompleteDir());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_pex_enabled:
|
2022-08-15 00:33:28 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->allowsPEX());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2022-08-26 02:27:11 +00:00
|
|
|
case TR_KEY_tcp_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, s->allowsTCP());
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_utp_enabled:
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->allowsUTP());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_dht_enabled:
|
2022-08-15 00:33:28 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->allowsDHT());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_lpd_enabled:
|
2022-08-15 00:33:28 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->allowsLPD());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peer_port:
|
2022-04-21 15:58:13 +00:00
|
|
|
tr_variantDictAddInt(d, key, s->peerPort().host());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peer_port_random_on_start:
|
2022-08-15 00:33:28 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->isPortRandom());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_port_forwarding_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionIsPortForwardingEnabled(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_rename_partial_files:
|
2022-08-15 00:33:28 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->isIncompleteFileNamingEnabled());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_rpc_version:
|
2021-12-28 17:23:47 +00:00
|
|
|
tr_variantDictAddInt(d, key, RpcVersion);
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2021-10-13 03:03:28 +00:00
|
|
|
case TR_KEY_rpc_version_semver:
|
2021-12-28 17:23:47 +00:00
|
|
|
tr_variantDictAddStrView(d, key, RpcVersionSemver);
|
2021-10-13 03:03:28 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_rpc_version_minimum:
|
2021-12-28 17:23:47 +00:00
|
|
|
tr_variantDictAddInt(d, key, RpcVersionMin);
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seedRatioLimit:
|
2022-08-15 00:33:28 +00:00
|
|
|
tr_variantDictAddReal(d, key, s->desiredRatio());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seedRatioLimited:
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->isRatioLimited());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_idle_seeding_limit:
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_variantDictAddInt(d, key, s->idleLimitMinutes());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_idle_seeding_limit_enabled:
|
2022-08-15 00:33:28 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->isIdleLimited());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seed_queue_enabled:
|
2022-08-13 19:56:10 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->queueEnabled(TR_UP));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seed_queue_size:
|
2022-08-13 19:56:10 +00:00
|
|
|
tr_variantDictAddInt(d, key, s->queueSize(TR_UP));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_start_added_torrents:
|
2022-08-14 01:04:36 +00:00
|
|
|
tr_variantDictAddBool(d, key, !s->shouldPauseAddedTorrents());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_trash_original_torrent_files:
|
2022-08-14 01:04:36 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->shouldDeleteSource());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_speed_limit_up:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetSpeedLimit_KBps(s, TR_UP));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_speed_limit_up_enabled:
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->isSpeedLimited(TR_UP));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_speed_limit_down:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetSpeedLimit_KBps(s, TR_DOWN));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_speed_limit_down_enabled:
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->isSpeedLimited(TR_DOWN));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2021-10-12 21:32:32 +00:00
|
|
|
case TR_KEY_script_torrent_added_filename:
|
2022-06-15 22:19:24 +00:00
|
|
|
tr_variantDictAddStr(d, key, s->script(TR_SCRIPT_ON_TORRENT_ADDED));
|
2021-10-12 21:32:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_KEY_script_torrent_added_enabled:
|
2022-06-15 22:19:24 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->useScript(TR_SCRIPT_ON_TORRENT_ADDED));
|
2021-10-12 21:32:32 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_script_torrent_done_filename:
|
2022-06-15 22:19:24 +00:00
|
|
|
tr_variantDictAddStr(d, key, s->script(TR_SCRIPT_ON_TORRENT_DONE));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_script_torrent_done_enabled:
|
2022-06-15 22:19:24 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->useScript(TR_SCRIPT_ON_TORRENT_DONE));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-14 04:07:12 +00:00
|
|
|
case TR_KEY_script_torrent_done_seeding_filename:
|
2022-06-15 22:19:24 +00:00
|
|
|
tr_variantDictAddStr(d, key, s->script(TR_SCRIPT_ON_TORRENT_DONE_SEEDING));
|
2022-02-14 04:07:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_KEY_script_torrent_done_seeding_enabled:
|
2022-06-15 22:19:24 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->useScript(TR_SCRIPT_ON_TORRENT_DONE_SEEDING));
|
2022-02-14 04:07:12 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_queue_stalled_enabled:
|
2022-08-13 19:56:10 +00:00
|
|
|
tr_variantDictAddBool(d, key, s->queueStalledEnabled());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_queue_stalled_minutes:
|
2022-08-13 19:56:10 +00:00
|
|
|
tr_variantDictAddInt(d, key, s->queueStalledMinutes());
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2020-11-14 18:43:42 +00:00
|
|
|
case TR_KEY_anti_brute_force_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionGetAntiBruteForceEnabled(s));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_KEY_anti_brute_force_threshold:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetAntiBruteForceThreshold(s));
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_units:
|
|
|
|
tr_formatter_get_units(tr_variantDictAddDict(d, key, 0));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_version:
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(d, key, LONG_VERSION_STRING);
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_encryption:
|
2021-10-20 16:31:20 +00:00
|
|
|
tr_variantDictAddStr(d, key, getEncryptionModeString(tr_sessionGetEncryption(s)));
|
|
|
|
break;
|
2016-09-05 19:16:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_session_id:
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_variantDictAddStr(d, key, s->sessionId());
|
2016-09-05 19:16:56 +00:00
|
|
|
break;
|
2016-09-04 10:16:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static char const* sessionGet(tr_session* s, tr_variant* args_in, tr_variant* args_out, tr_rpc_idle_data* /*idle_data*/)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2022-02-08 05:44:31 +00:00
|
|
|
if (tr_variant* fields = nullptr; tr_variantDictFindList(args_in, TR_KEY_fields, &fields))
|
2016-09-04 10:16:20 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const field_count = tr_variantListSize(fields);
|
2016-09-04 10:16:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (size_t i = 0; i < field_count; ++i)
|
2016-09-04 10:16:20 +00:00
|
|
|
{
|
2021-11-01 23:29:13 +00:00
|
|
|
auto field_name = std::string_view{};
|
2022-04-24 19:34:32 +00:00
|
|
|
if (!tr_variantGetStrView(tr_variantListChild(fields, i), &field_name))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-04 10:16:20 +00:00
|
|
|
|
2022-08-28 21:17:07 +00:00
|
|
|
if (auto const field_id = tr_quark_lookup(field_name); field_id)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-01 23:29:13 +00:00
|
|
|
addSessionField(s, args_out, *field_id);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2016-09-04 10:16:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
for (tr_quark field_id = TR_KEY_NONE + 1; field_id < TR_N_KEYS; ++field_id)
|
|
|
|
{
|
|
|
|
addSessionField(s, args_out, field_id);
|
|
|
|
}
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
static char const* freeSpace(
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_session* /*session*/,
|
2021-10-10 16:52:26 +00:00
|
|
|
tr_variant* args_in,
|
|
|
|
tr_variant* args_out,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2013-02-09 04:05:03 +00:00
|
|
|
{
|
2021-11-14 01:09:33 +00:00
|
|
|
auto path = std::string_view{};
|
|
|
|
|
|
|
|
if (!tr_variantDictFindStrView(args_in, TR_KEY_path, &path))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return "directory path argument is missing";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_sys_path_is_relative(path))
|
|
|
|
{
|
|
|
|
return "directory path is not absolute";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the free space */
|
2021-10-18 03:04:20 +00:00
|
|
|
auto const old_errno = errno;
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = 0;
|
2021-11-13 18:09:14 +00:00
|
|
|
auto const dir_space = tr_dirSpace(path);
|
2021-10-18 03:04:20 +00:00
|
|
|
char const* const err = dir_space.free < 0 || dir_space.total < 0 ? tr_strerror(errno) : nullptr;
|
|
|
|
errno = old_errno;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* response */
|
2021-11-14 01:09:33 +00:00
|
|
|
tr_variantDictAddStr(args_out, TR_KEY_path, path);
|
2021-10-14 17:07:16 +00:00
|
|
|
tr_variantDictAddInt(args_out, TR_KEY_size_bytes, dir_space.free);
|
|
|
|
tr_variantDictAddInt(args_out, TR_KEY_total_size, dir_space.total);
|
2017-04-19 12:04:45 +00:00
|
|
|
return err;
|
2013-02-09 04:05:03 +00:00
|
|
|
}
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static char const* sessionClose(
|
|
|
|
tr_session* session,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_variant* /*args_in*/,
|
|
|
|
tr_variant* /*args_out*/,
|
|
|
|
tr_rpc_idle_data* /*idle_data*/)
|
2011-01-15 18:12:45 +00:00
|
|
|
{
|
2022-08-14 15:40:03 +00:00
|
|
|
session->rpcNotify(TR_RPC_SESSION_CLOSE, nullptr);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2011-01-15 18:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
using handler = char const* (*)(tr_session*, tr_variant*, tr_variant*, struct tr_rpc_idle_data*);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-11-05 06:29:19 +00:00
|
|
|
struct rpc_method
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
std::string_view name;
|
2017-04-19 12:04:45 +00:00
|
|
|
bool immediate;
|
|
|
|
handler func;
|
2008-05-12 13:05:06 +00:00
|
|
|
};
|
|
|
|
|
2022-03-18 13:11:59 +00:00
|
|
|
static auto constexpr Methods = std::array<rpc_method, 24>{ {
|
2021-11-05 06:29:19 +00:00
|
|
|
{ "blocklist-update"sv, false, blocklistUpdate },
|
|
|
|
{ "free-space"sv, true, freeSpace },
|
2022-03-18 13:11:59 +00:00
|
|
|
{ "group-get"sv, true, groupGet },
|
|
|
|
{ "group-set"sv, true, groupSet },
|
2021-11-05 06:29:19 +00:00
|
|
|
{ "port-test"sv, false, portTest },
|
|
|
|
{ "queue-move-bottom"sv, true, queueMoveBottom },
|
|
|
|
{ "queue-move-down"sv, true, queueMoveDown },
|
|
|
|
{ "queue-move-top"sv, true, queueMoveTop },
|
|
|
|
{ "queue-move-up"sv, true, queueMoveUp },
|
|
|
|
{ "session-close"sv, true, sessionClose },
|
|
|
|
{ "session-get"sv, true, sessionGet },
|
|
|
|
{ "session-set"sv, true, sessionSet },
|
|
|
|
{ "session-stats"sv, true, sessionStats },
|
|
|
|
{ "torrent-add"sv, false, torrentAdd },
|
|
|
|
{ "torrent-get"sv, true, torrentGet },
|
|
|
|
{ "torrent-reannounce"sv, true, torrentReannounce },
|
|
|
|
{ "torrent-remove"sv, true, torrentRemove },
|
|
|
|
{ "torrent-rename-path"sv, false, torrentRenamePath },
|
|
|
|
{ "torrent-set"sv, true, torrentSet },
|
|
|
|
{ "torrent-set-location"sv, true, torrentSetLocation },
|
|
|
|
{ "torrent-start"sv, true, torrentStart },
|
|
|
|
{ "torrent-start-now"sv, true, torrentStartNow },
|
|
|
|
{ "torrent-stop"sv, true, torrentStop },
|
|
|
|
{ "torrent-verify"sv, true, torrentVerify },
|
|
|
|
} };
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void noop_response_callback(tr_session* /*session*/, tr_variant* /*response*/, void* /*user_data*/)
|
2009-01-20 02:03:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void tr_rpc_request_exec_json(
|
|
|
|
tr_session* session,
|
|
|
|
tr_variant const* request,
|
|
|
|
tr_rpc_response_func callback,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* callback_user_data)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2021-12-01 23:11:57 +00:00
|
|
|
auto* const mutable_request = const_cast<tr_variant*>(request);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* args_in = tr_variantDictFind(mutable_request, TR_KEY_arguments);
|
2021-09-15 00:18:09 +00:00
|
|
|
char const* result = nullptr;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (callback == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
callback = noop_response_callback;
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-11-05 06:29:19 +00:00
|
|
|
// parse the request's method name
|
|
|
|
auto sv = std::string_view{};
|
|
|
|
rpc_method const* method = nullptr;
|
|
|
|
if (!tr_variantDictFindStrView(mutable_request, TR_KEY_method, &sv))
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
result = "no method name";
|
2013-05-23 00:11:09 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
auto const it = std::find_if(std::begin(Methods), std::end(Methods), [&sv](auto const& row) { return row.name == sv; });
|
|
|
|
if (it == std::end(Methods))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
result = "method name not recognized";
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-11-05 06:29:19 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
method = &*it;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* if we couldn't figure out which method to use, return an error */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (result != nullptr)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
auto response = tr_variant{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(&response, 3);
|
|
|
|
tr_variantDictAddDict(&response, TR_KEY_arguments, 0);
|
|
|
|
tr_variantDictAddStr(&response, TR_KEY_result, result);
|
|
|
|
|
2022-02-08 05:44:31 +00:00
|
|
|
if (auto tag = int64_t{}; tr_variantDictFindInt(mutable_request, TR_KEY_tag, &tag))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddInt(&response, TR_KEY_tag, tag);
|
|
|
|
}
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
(*callback)(session, &response, callback_user_data);
|
2011-04-27 21:22:08 +00:00
|
|
|
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&response);
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
2017-05-13 22:38:31 +00:00
|
|
|
else if (method->immediate)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2021-10-20 16:31:20 +00:00
|
|
|
auto response = tr_variant{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(&response, 3);
|
2021-10-20 16:31:20 +00:00
|
|
|
tr_variant* const args_out = tr_variantDictAddDict(&response, TR_KEY_arguments, 0);
|
2021-09-15 00:18:09 +00:00
|
|
|
result = (*method->func)(session, args_in, args_out, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (result == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
result = "success";
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantDictAddStr(&response, TR_KEY_result, result);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-02-08 05:44:31 +00:00
|
|
|
if (auto tag = int64_t{}; tr_variantDictFindInt(mutable_request, TR_KEY_tag, &tag))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddInt(&response, TR_KEY_tag, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
(*callback)(session, &response, callback_user_data);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&response);
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2022-08-17 18:11:44 +00:00
|
|
|
auto* const data = new tr_rpc_idle_data{};
|
2017-04-19 12:04:45 +00:00
|
|
|
data->session = session;
|
2022-08-17 18:11:44 +00:00
|
|
|
tr_variantInitDict(&data->response, 3);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-08 05:44:31 +00:00
|
|
|
if (auto tag = int64_t{}; tr_variantDictFindInt(mutable_request, TR_KEY_tag, &tag))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-17 18:11:44 +00:00
|
|
|
tr_variantDictAddInt(&data->response, TR_KEY_tag, tag);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-10-20 21:06:01 +00:00
|
|
|
|
2022-08-17 18:11:44 +00:00
|
|
|
data->args_out = tr_variantDictAddDict(&data->response, TR_KEY_arguments, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
data->callback = callback;
|
|
|
|
data->callback_user_data = callback_user_data;
|
2017-05-13 22:38:31 +00:00
|
|
|
result = (*method->func)(session, args_in, data->args_out, data);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* Async operation failed prematurely? Invoke callback or else client will not get a reply */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (result != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_idle_function_done(data, result);
|
|
|
|
}
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2008-05-24 18:22:16 +00:00
|
|
|
/**
|
|
|
|
* Munge the URI into a usable form.
|
|
|
|
*
|
|
|
|
* We have very loose typing on this to make the URIs as simple as possible:
|
|
|
|
* - anything not a 'tag' or 'method' is automatically in 'arguments'
|
|
|
|
* - values that are all-digits are numbers
|
|
|
|
* - values that are all-digits or commas are number lists
|
|
|
|
* - all other values are strings
|
|
|
|
*/
|
2021-10-15 23:34:22 +00:00
|
|
|
void tr_rpc_parse_list_str(tr_variant* setme, std::string_view str)
|
2008-05-24 18:22:16 +00:00
|
|
|
{
|
2021-10-15 23:34:22 +00:00
|
|
|
auto const values = tr_parseNumberRange(str);
|
2022-09-07 16:04:28 +00:00
|
|
|
auto const value_count = std::size(values);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (value_count == 0)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2021-10-20 02:30:50 +00:00
|
|
|
tr_variantInitStr(setme, str);
|
2013-05-23 00:11:09 +00:00
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
else if (value_count == 1)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitInt(setme, values[0]);
|
2013-05-23 00:11:09 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_variantInitList(setme, value_count);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2021-09-25 19:37:28 +00:00
|
|
|
for (auto const& value : values)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-25 19:37:28 +00:00
|
|
|
tr_variantListAddInt(setme, value);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-05-24 18:22:16 +00:00
|
|
|
}
|
|
|
|
}
|