2008-05-12 00:41:55 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2008-05-12 00:41:55 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2008-05-12 00:41:55 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-05-12 13:05:06 +00:00
|
|
|
#include <assert.h>
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <ctype.h> /* isdigit */
|
2010-04-19 16:18:12 +00:00
|
|
|
#include <errno.h>
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <stdlib.h> /* strtol */
|
2008-05-12 13:05:06 +00:00
|
|
|
#include <string.h> /* strcmp */
|
|
|
|
|
2014-07-13 21:50:56 +00:00
|
|
|
#include <zlib.h>
|
2010-10-31 17:16:12 +00:00
|
|
|
|
2010-12-24 08:58:41 +00:00
|
|
|
#include <event2/buffer.h>
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
#include "transmission.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"
|
2010-10-31 17:16:12 +00:00
|
|
|
#include "fdlimit.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2013-07-08 17:07:31 +00:00
|
|
|
#include "platform-quota.h" /* tr_device_info_get_free_space() */
|
2009-04-13 19:04:21 +00:00
|
|
|
#include "rpcimpl.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
#include "session.h"
|
2016-09-05 19:16:56 +00:00
|
|
|
#include "session-id.h"
|
2008-05-12 00:41:55 +00:00
|
|
|
#include "torrent.h"
|
|
|
|
#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"
|
2009-01-18 15:24:26 +00:00
|
|
|
#include "web.h"
|
2008-05-12 00:41:55 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#define RPC_VERSION 16
|
2010-04-02 17:57:25 +00:00
|
|
|
#define RPC_VERSION_MIN 1
|
|
|
|
|
2009-04-04 05:29:08 +00:00
|
|
|
#define RECENTLY_ACTIVE_SECONDS 60
|
|
|
|
|
2009-01-20 02:03:09 +00:00
|
|
|
#if 0
|
2017-05-22 20:12:57 +00:00
|
|
|
#define dbgmsg(fmt, ...) fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, __VA_ARGS__)
|
2009-01-20 02:03:09 +00:00
|
|
|
#else
|
2017-05-22 20:12:57 +00:00
|
|
|
#define dbgmsg(...) tr_logAddDeepNamed("RPC", __VA_ARGS__)
|
2017-04-19 12:04:45 +00:00
|
|
|
#endif
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_rpc_callback_status notify(tr_session* session, int type, tr_torrent* tor)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-08-14 02:12:29 +00:00
|
|
|
tr_rpc_callback_status status = 0;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (session->rpc_func != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 16:30:03 +00:00
|
|
|
status = (*session->rpc_func)(session, type, tor, session->rpc_func_user_data);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-08-14 02:12:29 +00:00
|
|
|
|
|
|
|
return status;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2008-05-12 00:41:55 +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
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session;
|
|
|
|
tr_variant* response;
|
|
|
|
tr_variant* args_out;
|
|
|
|
tr_rpc_response_func callback;
|
|
|
|
void* callback_user_data;
|
2009-01-18 15:24:26 +00:00
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void tr_idle_function_done(struct tr_rpc_idle_data* data, char const* result)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
result = "success";
|
|
|
|
}
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(data->response, TR_KEY_result, result);
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
(*data->callback)(data->session, data->response, data->callback_user_data);
|
|
|
|
|
|
|
|
tr_variantFree(data->response);
|
|
|
|
tr_free(data->response);
|
|
|
|
tr_free(data);
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_torrent** getTorrents(tr_session* session, tr_variant* args, int* setmeCount)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int torrentCount = 0;
|
|
|
|
int64_t id;
|
|
|
|
tr_torrent** torrents = NULL;
|
|
|
|
tr_variant* ids;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2008-05-12 00:41:55 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindList(args, TR_KEY_ids, &ids))
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const n = tr_variantListSize(ids);
|
2008-05-12 00:41:55 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
torrents = tr_new0(tr_torrent*, n);
|
2008-05-12 00:41:55 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor;
|
|
|
|
tr_variant* node = tr_variantListChild(ids, i);
|
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);
|
|
|
|
}
|
|
|
|
else if (tr_variantGetStr(node, &str, NULL))
|
|
|
|
{
|
|
|
|
tor = tr_torrentFindFromHashString(session, str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tor = NULL;
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tor != NULL)
|
|
|
|
{
|
|
|
|
torrents[torrentCount++] = tor;
|
|
|
|
}
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor;
|
|
|
|
torrents = tr_new0(tr_torrent*, 1);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((tor = tr_torrentFindFromId(session, id)) != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
torrents[torrentCount++] = tor;
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (tr_variantDictFindStr(args, TR_KEY_ids, &str, NULL))
|
2009-03-23 00:16:37 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (strcmp(str, "recently-active") == 0)
|
2009-03-23 00:16:37 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor = NULL;
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
|
|
|
time_t const window = RECENTLY_ACTIVE_SECONDS;
|
|
|
|
int const n = tr_sessionCountTorrents(session);
|
2017-04-19 12:04:45 +00:00
|
|
|
torrents = tr_new0(tr_torrent*, n);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((tor = tr_torrentNext(session, tor)) != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (tor->anyDate >= now - window)
|
|
|
|
{
|
|
|
|
torrents[torrentCount++] = tor;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor;
|
|
|
|
torrents = tr_new0(tr_torrent*, 1);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((tor = tr_torrentFindFromHashString(session, str)) != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
torrents[torrentCount++] = tor;
|
|
|
|
}
|
2009-05-08 17:24:16 +00:00
|
|
|
}
|
2009-03-23 00:16:37 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else /* all of them */
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
torrents = tr_sessionGetTorrents(session, &torrentCount);
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*setmeCount = torrentCount;
|
|
|
|
return torrents;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void notifyBatchQueueChange(tr_session* session, tr_torrent** torrents, int n)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
notify(session, TR_RPC_TORRENT_CHANGED, torrents[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
notify(session, TR_RPC_SESSION_QUEUE_POSITIONS_CHANGED, NULL);
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* queueMoveTop(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int n;
|
|
|
|
tr_torrent** torrents = getTorrents(session, args_in, &n);
|
|
|
|
tr_torrentsQueueMoveTop(torrents, n);
|
|
|
|
notifyBatchQueueChange(session, torrents, n);
|
|
|
|
tr_free(torrents);
|
|
|
|
return NULL;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* queueMoveUp(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int n;
|
|
|
|
tr_torrent** torrents = getTorrents(session, args_in, &n);
|
|
|
|
tr_torrentsQueueMoveUp(torrents, n);
|
|
|
|
notifyBatchQueueChange(session, torrents, n);
|
|
|
|
tr_free(torrents);
|
|
|
|
return NULL;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* queueMoveDown(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int n;
|
|
|
|
tr_torrent** torrents = getTorrents(session, args_in, &n);
|
|
|
|
tr_torrentsQueueMoveDown(torrents, n);
|
|
|
|
notifyBatchQueueChange(session, torrents, n);
|
|
|
|
tr_free(torrents);
|
|
|
|
return NULL;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* queueMoveBottom(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int n;
|
|
|
|
tr_torrent** torrents = getTorrents(session, args_in, &n);
|
|
|
|
tr_torrentsQueueMoveBottom(torrents, n);
|
|
|
|
notifyBatchQueueChange(session, torrents, n);
|
|
|
|
tr_free(torrents);
|
|
|
|
return NULL;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int compareTorrentByQueuePosition(void const* va, void const* vb)
|
2015-01-02 11:15:31 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_torrent const* a = *(tr_torrent const**)va;
|
|
|
|
tr_torrent const* b = *(tr_torrent const**)vb;
|
2013-10-20 17:24:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return a->queuePosition - b->queuePosition;
|
2015-01-02 11:15:31 +00:00
|
|
|
}
|
2013-10-20 17:24:57 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* torrentStart(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int torrentCount;
|
|
|
|
tr_torrent** torrents;
|
|
|
|
|
|
|
|
assert(idle_data == NULL);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
torrents = getTorrents(session, args_in, &torrentCount);
|
|
|
|
qsort(torrents, torrentCount, sizeof(tr_torrent*), compareTorrentByQueuePosition);
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < torrentCount; ++i)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor = torrents[i];
|
|
|
|
|
|
|
|
if (!tor->isRunning)
|
2010-02-21 23:03:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrentStart(tor);
|
|
|
|
notify(session, 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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(torrents);
|
|
|
|
return NULL;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* torrentStartNow(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int torrentCount;
|
|
|
|
tr_torrent** torrents;
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(idle_data == NULL);
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
torrents = getTorrents(session, args_in, &torrentCount);
|
|
|
|
qsort(torrents, torrentCount, sizeof(tr_torrent*), compareTorrentByQueuePosition);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < torrentCount; ++i)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor = torrents[i];
|
2013-05-23 00:11:09 +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);
|
|
|
|
notify(session, TR_RPC_TORRENT_STARTED, tor);
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(torrents);
|
|
|
|
return NULL;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* torrentStop(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int torrentCount;
|
|
|
|
tr_torrent** torrents;
|
|
|
|
|
|
|
|
assert(idle_data == NULL);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
torrents = getTorrents(session, args_in, &torrentCount);
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < torrentCount; ++i)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor = torrents[i];
|
2010-06-25 06:57:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tor->isRunning || tr_torrentIsQueued(tor))
|
2010-02-21 23:03:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->isStopping = true;
|
|
|
|
notify(session, 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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(torrents);
|
|
|
|
return NULL;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* torrentRemove(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2008-05-20 23:58:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int torrentCount;
|
|
|
|
tr_rpc_callback_type type;
|
|
|
|
bool deleteFlag;
|
|
|
|
tr_torrent** torrents;
|
|
|
|
|
|
|
|
assert(idle_data == NULL);
|
|
|
|
|
|
|
|
if (!tr_variantDictFindBool(args_in, TR_KEY_delete_local_data, &deleteFlag))
|
|
|
|
{
|
|
|
|
deleteFlag = false;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
type = deleteFlag ? TR_RPC_TORRENT_TRASHING : TR_RPC_TORRENT_REMOVING;
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
torrents = getTorrents(session, args_in, &torrentCount);
|
2011-02-06 17:30:46 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < torrentCount; ++i)
|
2008-05-20 23:58:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor = torrents[i];
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_rpc_callback_status const status = notify(session, type, tor);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((status & TR_RPC_NOREMOVE) == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_torrentRemove(tor, deleteFlag, NULL);
|
|
|
|
}
|
2008-05-20 23:58:59 +00:00
|
|
|
}
|
2008-12-14 11:21:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(torrents);
|
|
|
|
return NULL;
|
2008-05-20 23:58:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* torrentReannounce(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2009-03-26 18:06:54 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int torrentCount;
|
|
|
|
tr_torrent** torrents;
|
2009-03-26 18:06:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(idle_data == NULL);
|
2009-03-26 18:06:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
torrents = getTorrents(session, args_in, &torrentCount);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < torrentCount; ++i)
|
2009-03-26 18:06:54 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor = torrents[i];
|
2013-05-23 00:11:09 +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);
|
|
|
|
notify(session, TR_RPC_TORRENT_CHANGED, tor);
|
2009-03-26 18:06:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(torrents);
|
|
|
|
return NULL;
|
2009-03-26 18:06:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* torrentVerify(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int torrentCount;
|
|
|
|
tr_torrent** torrents;
|
|
|
|
|
|
|
|
assert(idle_data == NULL);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
torrents = getTorrents(session, args_in, &torrentCount);
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < torrentCount; ++i)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor = torrents[i];
|
|
|
|
tr_torrentVerify(tor, NULL, NULL);
|
|
|
|
notify(session, TR_RPC_TORRENT_CHANGED, tor);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-12-14 11:21:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(torrents);
|
|
|
|
return NULL;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_index_t n;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_info const* info = tr_torrentInfo(tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_stat* files = tr_torrentFiles(tor, &n);
|
2009-03-31 22:13:43 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t i = 0; i < info->fileCount; ++i)
|
2009-03-31 22:13:43 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_file const* file = &info->files[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* d = tr_variantListAddDict(list, 3);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_bytesCompleted, files[i].bytesCompleted);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_priority, file->priority);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_wanted, !file->dnd);
|
2009-03-31 22:13:43 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrentFilesFree(files, n);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_index_t n;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_info const* info = tr_torrentInfo(tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_stat* files = tr_torrentFiles(tor, &n);
|
2008-07-21 18:42:51 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t i = 0; i < info->fileCount; ++i)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_file const* file = &info->files[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* d = tr_variantListAddDict(list, 3);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_bytesCompleted, files[i].bytesCompleted);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_length, file->length);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_name, file->name);
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
2008-07-21 18:42:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrentFilesFree(files, n);
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void addWebseeds(tr_info const* info, tr_variant* webseeds)
|
2008-06-16 03:47:50 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < info->webseedCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantListAddStr(webseeds, info->webseeds[i]);
|
|
|
|
}
|
2008-06-16 03:47:50 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void addTrackers(tr_info const* info, tr_variant* trackers)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < info->trackerCount; ++i)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_tracker_info const* t = &info->trackers[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* d = tr_variantListAddDict(trackers, 4);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_announce, t->announce);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_id, t->id);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_scrape, t->scrape);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_tier, t->tier);
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void addTrackerStats(tr_tracker_stat const* st, int n, tr_variant* list)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_tracker_stat const* s = &st[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* d = tr_variantListAddDict(list, 26);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_announce, s->announce);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_announceState, s->announceState);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_downloadCount, s->downloadCount);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_hasAnnounced, s->hasAnnounced);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_hasScraped, s->hasScraped);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_host, s->host);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_id, s->id);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_isBackup, s->isBackup);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_lastAnnouncePeerCount, s->lastAnnouncePeerCount);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_lastAnnounceResult, s->lastAnnounceResult);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_lastAnnounceStartTime, s->lastAnnounceStartTime);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_lastAnnounceSucceeded, s->lastAnnounceSucceeded);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_lastAnnounceTime, s->lastAnnounceTime);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_lastAnnounceTimedOut, s->lastAnnounceTimedOut);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_lastScrapeResult, s->lastScrapeResult);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_lastScrapeStartTime, s->lastScrapeStartTime);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_lastScrapeSucceeded, s->lastScrapeSucceeded);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_lastScrapeTime, s->lastScrapeTime);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_lastScrapeTimedOut, s->lastScrapeTimedOut);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_leecherCount, s->leecherCount);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_nextAnnounceTime, s->nextAnnounceTime);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_nextScrapeTime, s->nextScrapeTime);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_scrape, s->scrape);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_scrapeState, s->scrapeState);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_seederCount, s->seederCount);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_tier, s->tier);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void addPeers(tr_torrent* tor, tr_variant* list)
|
2008-08-22 17:59:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int peerCount;
|
|
|
|
tr_peer_stat* peers = tr_torrentPeers(tor, &peerCount);
|
|
|
|
|
|
|
|
tr_variantInitList(list, peerCount);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < peerCount; ++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);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_rateToClient, toSpeedBytes(peer->rateToClient_KBps));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_rateToPeer, toSpeedBytes(peer->rateToPeer_KBps));
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrentPeersFree(peers, peerCount);
|
2008-08-22 17:59:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void addField(tr_torrent* const tor, tr_info const* const inf, tr_stat const* const st, tr_variant* const d,
|
|
|
|
tr_quark const key)
|
2008-05-12 13:05:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* str;
|
2012-12-05 17:29:46 +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:
|
|
|
|
tr_variantDictAddInt(d, key, st->activityDate);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_addedDate:
|
|
|
|
tr_variantDictAddInt(d, key, st->addedDate);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_bandwidthPriority:
|
|
|
|
tr_variantDictAddInt(d, key, tr_torrentGetPriority(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_comment:
|
|
|
|
tr_variantDictAddStr(d, key, inf->comment ? inf->comment : "");
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_corruptEver:
|
|
|
|
tr_variantDictAddInt(d, key, st->corruptEver);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_creator:
|
|
|
|
tr_variantDictAddStr(d, key, inf->creator ? inf->creator : "");
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_dateCreated:
|
|
|
|
tr_variantDictAddInt(d, key, inf->dateCreated);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_desiredAvailable:
|
|
|
|
tr_variantDictAddInt(d, key, st->desiredAvailable);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_doneDate:
|
|
|
|
tr_variantDictAddInt(d, key, st->doneDate);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_downloadDir:
|
|
|
|
tr_variantDictAddStr(d, key, tr_torrentGetDownloadDir(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_downloadedEver:
|
|
|
|
tr_variantDictAddInt(d, key, st->downloadedEver);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_downloadLimit:
|
|
|
|
tr_variantDictAddInt(d, key, 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:
|
|
|
|
tr_variantDictAddBool(d, key, 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:
|
|
|
|
tr_variantDictAddInt(d, key, st->error);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_errorString:
|
|
|
|
tr_variantDictAddStr(d, key, st->errorString);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_eta:
|
|
|
|
tr_variantDictAddInt(d, key, st->eta);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_files:
|
|
|
|
addFiles(tor, tr_variantDictAddList(d, key, inf->fileCount));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_fileStats:
|
|
|
|
addFileStats(tor, tr_variantDictAddList(d, key, inf->fileCount));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_hashString:
|
|
|
|
tr_variantDictAddStr(d, key, tor->info.hashString);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_haveUnchecked:
|
|
|
|
tr_variantDictAddInt(d, key, st->haveUnchecked);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_haveValid:
|
|
|
|
tr_variantDictAddInt(d, key, st->haveValid);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_honorsSessionLimits:
|
|
|
|
tr_variantDictAddBool(d, key, tr_torrentUsesSessionLimits(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_id:
|
|
|
|
tr_variantDictAddInt(d, key, st->id);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_isFinished:
|
|
|
|
tr_variantDictAddBool(d, key, st->finished);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_isPrivate:
|
|
|
|
tr_variantDictAddBool(d, key, tr_torrentIsPrivate(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_isStalled:
|
|
|
|
tr_variantDictAddBool(d, key, st->isStalled);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_leftUntilDone:
|
|
|
|
tr_variantDictAddInt(d, key, st->leftUntilDone);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_manualAnnounceTime:
|
|
|
|
tr_variantDictAddInt(d, key, st->manualAnnounceTime);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_maxConnectedPeers:
|
|
|
|
tr_variantDictAddInt(d, key, tr_torrentGetPeerLimit(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_magnetLink:
|
|
|
|
str = tr_torrentGetMagnetLink(tor);
|
|
|
|
tr_variantDictAddStr(d, key, str);
|
|
|
|
tr_free(str);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_metadataPercentComplete:
|
|
|
|
tr_variantDictAddReal(d, key, st->metadataPercentComplete);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_name:
|
|
|
|
tr_variantDictAddStr(d, key, tr_torrentName(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_percentDone:
|
|
|
|
tr_variantDictAddReal(d, key, st->percentDone);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peer_limit:
|
|
|
|
tr_variantDictAddInt(d, key, tr_torrentGetPeerLimit(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peers:
|
|
|
|
addPeers(tor, tr_variantDictAdd(d, key));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peersConnected:
|
|
|
|
tr_variantDictAddInt(d, key, 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* tmp = tr_variantDictAddDict(d, key, 7);
|
2017-04-20 16:02:19 +00:00
|
|
|
int const* f = st->peersFrom;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(tmp, TR_KEY_fromCache, f[TR_PEER_FROM_RESUME]);
|
|
|
|
tr_variantDictAddInt(tmp, TR_KEY_fromDht, f[TR_PEER_FROM_DHT]);
|
|
|
|
tr_variantDictAddInt(tmp, TR_KEY_fromIncoming, f[TR_PEER_FROM_INCOMING]);
|
|
|
|
tr_variantDictAddInt(tmp, TR_KEY_fromLpd, f[TR_PEER_FROM_LPD]);
|
|
|
|
tr_variantDictAddInt(tmp, TR_KEY_fromLtep, f[TR_PEER_FROM_LTEP]);
|
|
|
|
tr_variantDictAddInt(tmp, TR_KEY_fromPex, f[TR_PEER_FROM_PEX]);
|
|
|
|
tr_variantDictAddInt(tmp, TR_KEY_fromTracker, f[TR_PEER_FROM_TRACKER]);
|
|
|
|
break;
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peersGettingFromUs:
|
|
|
|
tr_variantDictAddInt(d, key, st->peersGettingFromUs);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peersSendingToUs:
|
|
|
|
tr_variantDictAddInt(d, key, st->peersSendingToUs);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_pieces:
|
|
|
|
if (tr_torrentHasMetadata(tor))
|
|
|
|
{
|
2012-09-19 05:11:19 +00:00
|
|
|
size_t byte_count = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
void* bytes = tr_torrentCreatePieceBitfield(tor, &byte_count);
|
|
|
|
char* str = tr_base64_encode(bytes, byte_count, NULL);
|
|
|
|
tr_variantDictAddStr(d, key, str != NULL ? str : "");
|
|
|
|
tr_free(str);
|
|
|
|
tr_free(bytes);
|
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddStr(d, key, "");
|
|
|
|
}
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_pieceCount:
|
|
|
|
tr_variantDictAddInt(d, key, inf->pieceCount);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_pieceSize:
|
|
|
|
tr_variantDictAddInt(d, key, inf->pieceSize);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_priorities:
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* p = tr_variantDictAddList(d, key, inf->fileCount);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t i = 0; i < inf->fileCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantListAddInt(p, inf->files[i].priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_queuePosition:
|
|
|
|
tr_variantDictAddInt(d, key, st->queuePosition);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_etaIdle:
|
|
|
|
tr_variantDictAddInt(d, key, st->etaIdle);
|
2013-01-13 20:39:56 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_rateDownload:
|
|
|
|
tr_variantDictAddInt(d, key, 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:
|
|
|
|
tr_variantDictAddInt(d, key, 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:
|
|
|
|
tr_variantDictAddReal(d, key, st->recheckProgress);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seedIdleLimit:
|
|
|
|
tr_variantDictAddInt(d, key, tr_torrentGetIdleLimit(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seedIdleMode:
|
|
|
|
tr_variantDictAddInt(d, key, tr_torrentGetIdleMode(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seedRatioLimit:
|
|
|
|
tr_variantDictAddReal(d, key, tr_torrentGetRatioLimit(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seedRatioMode:
|
|
|
|
tr_variantDictAddInt(d, key, tr_torrentGetRatioMode(tor));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_sizeWhenDone:
|
|
|
|
tr_variantDictAddInt(d, key, st->sizeWhenDone);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_startDate:
|
|
|
|
tr_variantDictAddInt(d, key, st->startDate);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_status:
|
|
|
|
tr_variantDictAddInt(d, key, st->activity);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_secondsDownloading:
|
|
|
|
tr_variantDictAddInt(d, key, st->secondsDownloading);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_secondsSeeding:
|
|
|
|
tr_variantDictAddInt(d, key, st->secondsSeeding);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_trackers:
|
|
|
|
addTrackers(inf, tr_variantDictAddList(d, key, inf->trackerCount));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_trackerStats:
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int n;
|
|
|
|
tr_tracker_stat* s = tr_torrentTrackers(tor, &n);
|
|
|
|
addTrackerStats(s, n, tr_variantDictAddList(d, key, n));
|
|
|
|
tr_torrentTrackersFree(s, n);
|
|
|
|
break;
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_torrentFile:
|
|
|
|
tr_variantDictAddStr(d, key, inf->torrent);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_totalSize:
|
|
|
|
tr_variantDictAddInt(d, key, inf->totalSize);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_uploadedEver:
|
|
|
|
tr_variantDictAddInt(d, key, st->uploadedEver);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_uploadLimit:
|
|
|
|
tr_variantDictAddInt(d, key, 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:
|
|
|
|
tr_variantDictAddBool(d, key, 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:
|
|
|
|
tr_variantDictAddReal(d, key, st->ratio);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_wanted:
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* w = tr_variantDictAddList(d, key, inf->fileCount);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t i = 0; i < inf->fileCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantListAddInt(w, inf->files[i].dnd ? 0 : 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_webseeds:
|
|
|
|
addWebseeds(inf, tr_variantDictAddList(d, key, inf->webseedCount));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_webseedsSendingToUs:
|
|
|
|
tr_variantDictAddInt(d, key, 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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void addInfo(tr_torrent* tor, tr_variant* d, tr_variant* fields)
|
2008-07-26 14:47:07 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const n = tr_variantListSize(fields);
|
2008-06-16 03:47:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(d, n);
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (n > 0)
|
2011-05-09 04:13:14 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_info const* const inf = tr_torrentInfo(tor);
|
|
|
|
tr_stat const* const st = tr_torrentStat((tr_torrent*)tor);
|
2011-05-09 04:13:14 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (tr_variantGetStr(tr_variantListChild(fields, i), &str, &len))
|
|
|
|
{
|
|
|
|
addField(tor, inf, st, d, tr_quark_new(str, len));
|
|
|
|
}
|
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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* torrentGet(tr_session* session, tr_variant* args_in, tr_variant* args_out,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int torrentCount;
|
|
|
|
tr_torrent** torrents = getTorrents(session, args_in, &torrentCount);
|
|
|
|
tr_variant* list = tr_variantDictAddList(args_out, TR_KEY_torrents, torrentCount);
|
|
|
|
tr_variant* fields;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* strVal;
|
|
|
|
char const* errmsg = NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
assert(idle_data == NULL);
|
|
|
|
|
|
|
|
if (tr_variantDictFindStr(args_in, TR_KEY_ids, &strVal, NULL) && strcmp(strVal, "recently-active") == 0)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
tr_variant* d;
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
|
|
|
int const interval = RECENTLY_ACTIVE_SECONDS;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* removed_out = tr_variantDictAddList(args_out, TR_KEY_removed, 0);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((d = tr_variantListChild(&session->removedTorrents, n++)) != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
int64_t intVal;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFindInt(d, TR_KEY_date, &intVal) && intVal >= now - interval)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictFindInt(d, TR_KEY_id, &intVal);
|
|
|
|
tr_variantListAddInt(removed_out, intVal);
|
2009-04-04 05:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_variantDictFindList(args_in, TR_KEY_fields, &fields))
|
|
|
|
{
|
|
|
|
errmsg = "no fields specified";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < torrentCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
addInfo(torrents[i], tr_variantListAdd(list), fields);
|
|
|
|
}
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(torrents);
|
|
|
|
return errmsg;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2008-06-16 03:47:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* setFilePriorities(tr_torrent* tor, int priority, tr_variant* list)
|
2008-05-12 13:05:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t tmp;
|
|
|
|
int fileCount = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const n = tr_variantListSize(list);
|
|
|
|
char const* errmsg = NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_index_t* files = tr_new0(tr_file_index_t, tor->info.fileCount);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (n != 0)
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantGetInt(tr_variantListChild(list, i), &tmp))
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (0 <= tmp && tmp < tor->info.fileCount)
|
|
|
|
{
|
|
|
|
files[fileCount++] = tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errmsg = "file index out of range";
|
|
|
|
}
|
2009-03-03 20:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else /* if empty set, apply to all */
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t t = 0; t < tor->info.fileCount; ++t)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
files[fileCount++] = t;
|
|
|
|
}
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (fileCount != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_torrentSetFilePriorities(tor, files, fileCount, priority);
|
|
|
|
}
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(files);
|
|
|
|
return errmsg;
|
2008-05-12 13:05:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* setFileDLs(tr_torrent* tor, bool do_download, tr_variant* list)
|
2008-05-12 13:05:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t tmp;
|
|
|
|
int fileCount = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const n = tr_variantListSize(list);
|
|
|
|
char const* errmsg = NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_index_t* files = tr_new0(tr_file_index_t, tor->info.fileCount);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (n != 0) /* if argument list, process them */
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantGetInt(tr_variantListChild(list, i), &tmp))
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (0 <= tmp && tmp < tor->info.fileCount)
|
|
|
|
{
|
|
|
|
files[fileCount++] = tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errmsg = "file index out of range";
|
|
|
|
}
|
2009-03-03 20:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else /* if empty set, apply to all */
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t t = 0; t < tor->info.fileCount; ++t)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
files[fileCount++] = t;
|
|
|
|
}
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fileCount)
|
|
|
|
{
|
|
|
|
tr_torrentSetFileDLs(tor, files, fileCount, do_download);
|
|
|
|
}
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(files);
|
|
|
|
return errmsg;
|
2008-05-12 13:05:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool findAnnounceUrl(tr_tracker_info const* t, int n, char const* url, int* pos)
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool found = false;
|
2010-06-30 05:52:24 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (strcmp(t[i].announce, url) == 0)
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
found = true;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (pos != NULL)
|
|
|
|
{
|
|
|
|
*pos = i;
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return found;
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int copyTrackers(tr_tracker_info* tgt, tr_tracker_info const* src, int n)
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int maxTier = -1;
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tgt[i].tier = src[i].tier;
|
|
|
|
tgt[i].announce = tr_strdup(src[i].announce);
|
|
|
|
maxTier = MAX(maxTier, src[i].tier);
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return maxTier;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void freeTrackers(tr_tracker_info* trackers, int n)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_free(trackers[i].announce);
|
|
|
|
}
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(trackers);
|
2010-06-30 05:52:24 +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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
int tier;
|
|
|
|
tr_variant* val;
|
|
|
|
tr_tracker_info* trackers;
|
|
|
|
bool changed = false;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_info const* inf = tr_torrentInfo(tor);
|
|
|
|
char const* errmsg = NULL;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* make a working copy of the existing announce list */
|
|
|
|
n = inf->trackerCount;
|
|
|
|
trackers = tr_new0(tr_tracker_info, n + tr_variantListSize(urls));
|
|
|
|
tier = copyTrackers(trackers, inf->trackers, n);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* and add the new ones */
|
|
|
|
i = 0;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((val = tr_variantListChild(urls, i++)) != NULL)
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* announce = NULL;
|
2010-06-30 05:52:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantGetStr(val, &announce, NULL) && tr_urlIsValidTracker(announce) &&
|
|
|
|
!findAnnounceUrl(trackers, n, announce, NULL))
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
trackers[n].tier = ++tier; /* add a new tier */
|
|
|
|
trackers[n].announce = tr_strdup(announce);
|
|
|
|
++n;
|
|
|
|
changed = true;
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!changed)
|
|
|
|
{
|
|
|
|
errmsg = "invalid argument";
|
|
|
|
}
|
|
|
|
else if (!tr_torrentSetAnnounceList(tor, trackers, n))
|
|
|
|
{
|
|
|
|
errmsg = "error setting announce list";
|
|
|
|
}
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
freeTrackers(trackers, n);
|
|
|
|
return errmsg;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int i;
|
|
|
|
tr_variant* pair[2];
|
|
|
|
tr_tracker_info* trackers;
|
|
|
|
bool changed = false;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_info const* inf = tr_torrentInfo(tor);
|
|
|
|
int const n = inf->trackerCount;
|
|
|
|
char const* errmsg = NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* make a working copy of the existing announce list */
|
|
|
|
trackers = tr_new0(tr_tracker_info, n);
|
|
|
|
copyTrackers(trackers, inf->trackers, n);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* make the substitutions... */
|
|
|
|
i = 0;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((pair[0] = tr_variantListChild(urls, i)) != NULL && (pair[1] = tr_variantListChild(urls, i + 1)) != NULL)
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
|
|
|
int64_t pos;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* newval;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantGetInt(pair[0], &pos) && tr_variantGetStr(pair[1], &newval, &len) && tr_urlIsValidTracker(newval) &&
|
|
|
|
pos < n && pos >= 0)
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(trackers[pos].announce);
|
|
|
|
trackers[pos].announce = tr_strndup(newval, len);
|
|
|
|
changed = true;
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
i += 2;
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!changed)
|
|
|
|
{
|
|
|
|
errmsg = "invalid argument";
|
|
|
|
}
|
|
|
|
else if (!tr_torrentSetAnnounceList(tor, trackers, n))
|
|
|
|
{
|
|
|
|
errmsg = "error setting announce list";
|
|
|
|
}
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
freeTrackers(trackers, n);
|
|
|
|
return errmsg;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
int t = 0;
|
|
|
|
int dup = -1;
|
|
|
|
int* tids;
|
|
|
|
tr_variant* val;
|
|
|
|
tr_tracker_info* trackers;
|
|
|
|
bool changed = false;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_info const* inf = tr_torrentInfo(tor);
|
|
|
|
char const* errmsg = NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* make a working copy of the existing announce list */
|
|
|
|
n = inf->trackerCount;
|
|
|
|
tids = tr_new0(int, n);
|
|
|
|
trackers = tr_new0(tr_tracker_info, n);
|
|
|
|
copyTrackers(trackers, inf->trackers, n);
|
|
|
|
|
|
|
|
/* remove the ones specified in the urls list */
|
|
|
|
i = 0;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((val = tr_variantListChild(ids, i++)) != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
int64_t pos;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantGetInt(val, &pos) && 0 <= pos && pos < n)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tids[t++] = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sort trackerIds and remove from largest to smallest so there is no need to recacluate array indicies */
|
|
|
|
qsort(tids, t, sizeof(int), compareInt);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while (t-- != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
/* check for duplicates */
|
|
|
|
if (tids[t] == dup)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_removeElementFromArray(trackers, tids[t], sizeof(tr_tracker_info), n--);
|
|
|
|
dup = tids[t];
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!changed)
|
|
|
|
{
|
|
|
|
errmsg = "invalid argument";
|
|
|
|
}
|
|
|
|
else if (!tr_torrentSetAnnounceList(tor, trackers, n))
|
|
|
|
{
|
|
|
|
errmsg = "error setting announce list";
|
|
|
|
}
|
|
|
|
|
|
|
|
freeTrackers(trackers, n);
|
|
|
|
tr_free(tids);
|
|
|
|
return errmsg;
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* torrentSet(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int torrentCount;
|
|
|
|
tr_torrent** torrents;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* errmsg = NULL;
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(idle_data == NULL);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
torrents = getTorrents(session, args_in, &torrentCount);
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < torrentCount; ++i)
|
2008-05-12 13:05:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t tmp;
|
|
|
|
double d;
|
|
|
|
tr_variant* files;
|
|
|
|
tr_variant* trackers;
|
|
|
|
bool boolVal;
|
|
|
|
tr_torrent* tor;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tor = torrents[i];
|
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))
|
|
|
|
{
|
|
|
|
if (tr_isPriority(tmp))
|
|
|
|
{
|
|
|
|
tr_torrentSetPriority(tor, tmp);
|
|
|
|
}
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (errmsg == NULL && tr_variantDictFindList(args_in, TR_KEY_files_unwanted, &files))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
errmsg = setFileDLs(tor, false, files);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (errmsg == NULL && tr_variantDictFindList(args_in, TR_KEY_files_wanted, &files))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
errmsg = setFileDLs(tor, true, files);
|
|
|
|
}
|
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
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (errmsg == NULL && tr_variantDictFindList(args_in, TR_KEY_priority_high, &files))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
errmsg = setFilePriorities(tor, TR_PRI_HIGH, files);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (errmsg == NULL && tr_variantDictFindList(args_in, TR_KEY_priority_low, &files))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
errmsg = setFilePriorities(tor, TR_PRI_LOW, files);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (errmsg == NULL && tr_variantDictFindList(args_in, TR_KEY_priority_normal, &files))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
errmsg = setFilePriorities(tor, TR_PRI_NORMAL, files);
|
|
|
|
}
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_downloadLimited, &boolVal))
|
|
|
|
{
|
|
|
|
tr_torrentUseSpeedLimit(tor, TR_DOWN, boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_honorsSessionLimits, &boolVal))
|
|
|
|
{
|
|
|
|
tr_torrentUseSessionLimits(tor, boolVal);
|
|
|
|
}
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_uploadLimited, &boolVal))
|
|
|
|
{
|
|
|
|
tr_torrentUseSpeedLimit(tor, TR_UP, boolVal);
|
|
|
|
}
|
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))
|
|
|
|
{
|
|
|
|
tr_torrentSetIdleLimit(tor, tmp);
|
|
|
|
}
|
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))
|
|
|
|
{
|
|
|
|
tr_torrentSetIdleMode(tor, tmp);
|
|
|
|
}
|
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))
|
|
|
|
{
|
|
|
|
tr_torrentSetRatioMode(tor, tmp);
|
|
|
|
}
|
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))
|
|
|
|
{
|
|
|
|
tr_torrentSetQueuePosition(tor, tmp);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (errmsg == NULL && tr_variantDictFindList(args_in, TR_KEY_trackerAdd, &trackers))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
errmsg = addTrackerUrls(tor, trackers);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (errmsg == NULL && tr_variantDictFindList(args_in, TR_KEY_trackerRemove, &trackers))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
errmsg = removeTrackers(tor, trackers);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (errmsg == NULL && tr_variantDictFindList(args_in, TR_KEY_trackerReplace, &trackers))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
errmsg = replaceTrackers(tor, trackers);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
notify(session, TR_RPC_TORRENT_CHANGED, tor);
|
2008-05-12 13:05:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(torrents);
|
|
|
|
return errmsg;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* torrentSetLocation(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* location = NULL;
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(idle_data == NULL);
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_variantDictFindStr(args_in, TR_KEY_location, &location, NULL))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool move;
|
2017-05-01 15:46:41 +00:00
|
|
|
int torrentCount;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent** torrents = getTorrents(session, args_in, &torrentCount);
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_variantDictFindBool(args_in, TR_KEY_move, &move))
|
|
|
|
{
|
|
|
|
move = false;
|
|
|
|
}
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < torrentCount; ++i)
|
2015-10-20 21:06:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor = torrents[i];
|
|
|
|
tr_torrentSetLocation(tor, location, move, NULL, NULL);
|
|
|
|
notify(session, TR_RPC_TORRENT_MOVED, tor);
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(torrents);
|
2015-10-20 21:06:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return NULL;
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* result;
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* 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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (error == 0)
|
|
|
|
{
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = tr_strerror(error);
|
|
|
|
}
|
2013-01-20 01:31:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_idle_function_done(data, result);
|
2013-01-20 01:31:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* torrentRenamePath(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data)
|
2013-01-20 01:31:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int torrentCount;
|
|
|
|
tr_torrent** torrents;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* oldpath = NULL;
|
|
|
|
char const* newname = NULL;
|
|
|
|
char const* errmsg = NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_variantDictFindStr(args_in, TR_KEY_path, &oldpath, NULL);
|
|
|
|
tr_variantDictFindStr(args_in, TR_KEY_name, &newname, NULL);
|
|
|
|
torrents = getTorrents(session, args_in, &torrentCount);
|
|
|
|
|
|
|
|
if (torrentCount == 1)
|
|
|
|
{
|
|
|
|
tr_torrentRenamePath(torrents[0], oldpath, newname, torrentRenamePathDone, idle_data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errmsg = "torrent-rename-path requires 1 torrent";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free(torrents);
|
|
|
|
return errmsg;
|
2013-01-20 01:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void portTested(tr_session* session UNUSED, bool did_connect UNUSED, bool did_timeout UNUSED, long response_code,
|
2017-04-20 16:02:19 +00:00
|
|
|
void const* response, size_t response_byte_count, void* user_data)
|
2009-04-06 16:36:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char result[1024];
|
|
|
|
struct tr_rpc_idle_data* data = user_data;
|
2009-04-06 16:36:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (response_code != 200)
|
2009-04-06 16:36:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(result, sizeof(result), "portTested: http error %ld: %s", response_code,
|
|
|
|
tr_webGetResponseStr(response_code));
|
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
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
bool const isOpen = response_byte_count != 0 && *(char*)response == '1';
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(data->args_out, TR_KEY_port_is_open, isOpen);
|
|
|
|
tr_snprintf(result, sizeof(result), "success");
|
2009-04-06 16:36:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_idle_function_done(data, result);
|
2009-04-06 16:36:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* portTest(tr_session* session, tr_variant* args_in UNUSED, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data)
|
2009-04-06 16:36:00 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const port = tr_sessionGetPeerPort(session);
|
2017-04-19 12:04:45 +00:00
|
|
|
char* url = tr_strdup_printf("https://portcheck.transmissionbt.com/%d", port);
|
|
|
|
tr_webRun(session, url, portTested, idle_data);
|
|
|
|
tr_free(url);
|
|
|
|
return NULL;
|
2009-04-06 16:36:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void gotNewBlocklist(tr_session* session, bool did_connect UNUSED, bool did_timeout UNUSED, long response_code,
|
2017-04-20 16:02:19 +00:00
|
|
|
void const* response, size_t response_byte_count, void* user_data)
|
2009-04-03 23:21:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char result[1024];
|
|
|
|
struct tr_rpc_idle_data* data = user_data;
|
2009-04-03 23:21:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*result = '\0';
|
2011-06-10 05:34:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (response_code != 200)
|
2009-04-03 23:21:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(result, sizeof(result), "gotNewBlocklist: http error %ld: %s", response_code,
|
|
|
|
tr_webGetResponseStr(response_code));
|
2009-04-03 23:21:40 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else /* successfully fetched the blocklist... */
|
2009-04-03 23:21:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t fd;
|
|
|
|
int err;
|
|
|
|
char* filename;
|
|
|
|
z_stream stream;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* configDir = tr_sessionGetConfigDir(session);
|
|
|
|
size_t const buflen = 1024 * 128; /* 128 KiB buffer */
|
2017-04-19 12:04:45 +00:00
|
|
|
uint8_t* buf = tr_valloc(buflen);
|
|
|
|
tr_error* error = NULL;
|
|
|
|
|
|
|
|
/* this is an odd Magic Number required by zlib to enable gz support.
|
2017-04-21 07:40:57 +00:00
|
|
|
See zlib's inflateInit2() documentation for a full description */
|
2017-04-20 16:02:19 +00:00
|
|
|
int const windowBits = 15 + 32;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
stream.zalloc = (alloc_func)Z_NULL;
|
|
|
|
stream.zfree = (free_func)Z_NULL;
|
|
|
|
stream.opaque = (voidpf)Z_NULL;
|
|
|
|
stream.next_in = (void*)response;
|
|
|
|
stream.avail_in = response_byte_count;
|
|
|
|
inflateInit2(&stream, windowBits);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
filename = tr_buildPath(configDir, "blocklist.tmp.XXXXXX", NULL);
|
|
|
|
fd = tr_sys_file_open_temp(filename, &error);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fd == TR_BAD_SYS_FILE)
|
2014-07-28 04:13:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(result, sizeof(result), _("Couldn't save file \"%1$s\": %2$s"), filename, error->message);
|
|
|
|
tr_error_clear(&error);
|
2014-07-28 04:13:38 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (;;)
|
2011-06-26 17:05:17 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
stream.next_out = (void*)buf;
|
|
|
|
stream.avail_out = buflen;
|
|
|
|
err = inflate(&stream, Z_NO_FLUSH);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (stream.avail_out < buflen)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_sys_file_write(fd, buf, buflen - stream.avail_out, NULL, &error))
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(result, sizeof(result), _("Couldn't save file \"%1$s\": %2$s"), filename, error->message);
|
|
|
|
tr_error_clear(&error);
|
|
|
|
break;
|
2010-10-31 17:16:12 +00:00
|
|
|
}
|
2010-04-19 16:18:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (err != Z_OK)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (err != Z_STREAM_END && err != Z_DATA_ERROR)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_snprintf(result, sizeof(result), _("Error uncompressing blocklist: %s (%d)"), zError(err), err);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2010-04-19 16:18:12 +00:00
|
|
|
}
|
2011-06-26 17:05:17 +00:00
|
|
|
}
|
2009-04-03 23:21:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
inflateEnd(&stream);
|
2011-06-10 05:34:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (err == Z_DATA_ERROR) /* couldn't inflate it... it's probably already uncompressed */
|
|
|
|
{
|
|
|
|
if (!tr_sys_file_write(fd, response, response_byte_count, NULL, &error))
|
|
|
|
{
|
|
|
|
tr_snprintf(result, sizeof(result), _("Couldn't save file \"%1$s\": %2$s"), filename, error->message);
|
|
|
|
tr_error_clear(&error);
|
|
|
|
}
|
|
|
|
}
|
2010-10-31 17:16:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_close(fd, NULL);
|
2014-01-19 02:03:41 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (*result != '\0')
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddError("%s", result);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* feed it to the session and give the client a response */
|
2017-04-20 16:02:19 +00:00
|
|
|
int const rule_count = tr_blocklistSetContent(session, filename);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(data->args_out, TR_KEY_blocklist_size, rule_count);
|
|
|
|
tr_snprintf(result, sizeof(result), "success");
|
2010-04-19 16:18:12 +00:00
|
|
|
}
|
2009-04-03 23:21:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_path_remove(filename, NULL);
|
|
|
|
tr_free(filename);
|
|
|
|
tr_free(buf);
|
2009-04-03 23:21:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_idle_function_done(data, result);
|
2009-04-03 23:21:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* blocklistUpdate(tr_session* session, tr_variant* args_in UNUSED, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data)
|
2009-04-03 23:21:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_webRun(session, session->blocklist_url, gotNewBlocklist, idle_data);
|
|
|
|
return NULL;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int err;
|
|
|
|
int duplicate_id;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* result;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor;
|
|
|
|
tr_quark key;
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
err = 0;
|
|
|
|
duplicate_id = 0;
|
|
|
|
tor = tr_torrentNew(ctor, &err, &duplicate_id);
|
|
|
|
tr_ctorFree(ctor);
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (err == 0)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
key = TR_KEY_torrent_added;
|
|
|
|
result = NULL;
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (err == TR_PARSE_DUPLICATE)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor = tr_torrentFindFromId(data->session, duplicate_id);
|
|
|
|
key = TR_KEY_torrent_duplicate;
|
|
|
|
result = "duplicate torrent";
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else /* err == TR_PARSE_ERR */
|
2013-08-17 16:31:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
key = 0;
|
|
|
|
result = "invalid or corrupt torrent file";
|
2013-08-17 16:31:03 +00:00
|
|
|
}
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tor != NULL && key != 0)
|
2013-05-22 20:35:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant fields;
|
|
|
|
tr_variantInitList(&fields, 3);
|
|
|
|
tr_variantListAddStr(&fields, "id");
|
|
|
|
tr_variantListAddStr(&fields, "name");
|
|
|
|
tr_variantListAddStr(&fields, "hashString");
|
|
|
|
addInfo(tor, tr_variantDictAdd(data->args_out, key), &fields);
|
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
notify(data->session, TR_RPC_TORRENT_ADDED, tor);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantFree(&fields);
|
|
|
|
result = NULL;
|
2013-05-22 20:35:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_idle_function_done(data, result);
|
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
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void gotMetadataFromURL(tr_session* session UNUSED, bool did_connect UNUSED, bool did_timeout UNUSED, long response_code,
|
2017-04-20 16:02:19 +00:00
|
|
|
void const* response, size_t response_byte_count, void* user_data)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct add_torrent_idle_data* data = user_data;
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg("torrentAdd: HTTP response code was %ld (%s); response length was %zu bytes", response_code,
|
|
|
|
tr_webGetResponseStr(response_code), response_byte_count);
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (response_code == 200 || response_code == 221) /* http or ftp success.. */
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctorSetMetainfo(data->ctor, response, response_byte_count);
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char result[1024];
|
|
|
|
tr_snprintf(result, sizeof(result), "gotMetadataFromURL: http error %ld: %s", response_code,
|
|
|
|
tr_webGetResponseStr(response_code));
|
|
|
|
tr_idle_function_done(data->data, result);
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(data);
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool isCurlURL(char const* filename)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (filename == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return strncmp(filename, "ftp://", 6) == 0 || strncmp(filename, "http://", 7) == 0 || strncmp(filename, "https://", 8) == 0;
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_file_index_t* fileListFromList(tr_variant* list, tr_file_index_t* setmeCount)
|
2009-04-02 20:43:42 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const childCount = tr_variantListSize(list);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_index_t n = 0;
|
|
|
|
tr_file_index_t* files = tr_new0(tr_file_index_t, childCount);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < childCount; ++i)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t intVal;
|
|
|
|
|
|
|
|
if (tr_variantGetInt(tr_variantListChild(list, i), &intVal))
|
|
|
|
{
|
|
|
|
files[n++] = (tr_file_index_t)intVal;
|
|
|
|
}
|
2009-04-02 20:43:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*setmeCount = n;
|
|
|
|
return files;
|
2009-04-02 20:43:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* torrentAdd(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* filename = NULL;
|
|
|
|
char const* metainfo_base64 = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(idle_data != NULL);
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictFindStr(args_in, TR_KEY_filename, &filename, NULL);
|
|
|
|
tr_variantDictFindStr(args_in, TR_KEY_metainfo, &metainfo_base64, NULL);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (filename == NULL && metainfo_base64 == NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return "no filename or metainfo specified";
|
|
|
|
}
|
2008-05-20 17:33:54 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* download_dir = NULL;
|
2015-10-20 21:22:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindStr(args_in, TR_KEY_download_dir, &download_dir, NULL))
|
2015-10-20 21:22:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_sys_path_is_relative(download_dir))
|
|
|
|
{
|
|
|
|
return "download directory path is not absolute";
|
|
|
|
}
|
2015-10-20 21:22:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t i;
|
|
|
|
bool boolVal;
|
|
|
|
tr_variant* l;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* cookies = NULL;
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictFindStr(args_in, TR_KEY_cookies, &cookies, NULL);
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (download_dir != NULL)
|
|
|
|
{
|
|
|
|
tr_ctorSetDownloadDir(ctor, TR_FORCE, download_dir);
|
|
|
|
}
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_paused, &boolVal))
|
|
|
|
{
|
|
|
|
tr_ctorSetPaused(ctor, TR_FORCE, boolVal);
|
|
|
|
}
|
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))
|
|
|
|
{
|
|
|
|
tr_ctorSetPeerLimit(ctor, TR_FORCE, i);
|
|
|
|
}
|
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))
|
|
|
|
{
|
|
|
|
tr_ctorSetBandwidthPriority(ctor, i);
|
|
|
|
}
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_index_t fileCount;
|
|
|
|
tr_file_index_t* files = fileListFromList(l, &fileCount);
|
|
|
|
tr_ctorSetFilesWanted(ctor, files, fileCount, false);
|
|
|
|
tr_free(files);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_index_t fileCount;
|
|
|
|
tr_file_index_t* files = fileListFromList(l, &fileCount);
|
|
|
|
tr_ctorSetFilesWanted(ctor, files, fileCount, true);
|
|
|
|
tr_free(files);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_index_t fileCount;
|
|
|
|
tr_file_index_t* files = fileListFromList(l, &fileCount);
|
|
|
|
tr_ctorSetFilePriorities(ctor, files, fileCount, TR_PRI_LOW);
|
|
|
|
tr_free(files);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_index_t fileCount;
|
|
|
|
tr_file_index_t* files = fileListFromList(l, &fileCount);
|
|
|
|
tr_ctorSetFilePriorities(ctor, files, fileCount, TR_PRI_NORMAL);
|
|
|
|
tr_free(files);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_index_t fileCount;
|
|
|
|
tr_file_index_t* files = fileListFromList(l, &fileCount);
|
|
|
|
tr_ctorSetFilePriorities(ctor, files, fileCount, TR_PRI_HIGH);
|
|
|
|
tr_free(files);
|
2015-10-20 21:06:01 +00:00
|
|
|
}
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg("torrentAdd: filename is \"%s\"", filename ? filename : " (null)");
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct add_torrent_idle_data* d = tr_new0(struct add_torrent_idle_data, 1);
|
|
|
|
d->data = idle_data;
|
|
|
|
d->ctor = ctor;
|
|
|
|
tr_webRunWithCookies(session, filename, cookies, gotMetadataFromURL, d);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* fname = tr_strstrip(tr_strdup(filename));
|
2015-10-20 21:06:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fname == NULL)
|
2015-10-20 21:06:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
|
|
|
char* metainfo = tr_base64_decode_str(metainfo_base64, &len);
|
|
|
|
tr_ctorSetMetainfo(ctor, (uint8_t*)metainfo, len);
|
|
|
|
tr_free(metainfo);
|
2015-10-20 21:06:01 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (strncmp(fname, "magnet:?", 8) == 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctorSetMetainfoFromMagnetLink(ctor, fname);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctorSetMetainfoFromFile(ctor, fname);
|
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);
|
2015-10-20 21:06:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(fname);
|
2008-05-12 13:05:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return NULL;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2008-05-12 13:05:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* sessionSet(tr_session* session, tr_variant* args_in, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(idle_data == NULL);
|
2015-11-05 21:48:00 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* download_dir = NULL;
|
|
|
|
char const* incomplete_dir = NULL;
|
2015-11-05 21:48:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindStr(args_in, TR_KEY_download_dir, &download_dir, NULL))
|
2015-11-05 21:48:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_sys_path_is_relative(download_dir))
|
|
|
|
{
|
|
|
|
return "download directory path is not absolute";
|
|
|
|
}
|
2015-11-05 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindStr(args_in, TR_KEY_incomplete_dir, &incomplete_dir, NULL))
|
2015-11-05 21:48:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_sys_path_is_relative(incomplete_dir))
|
|
|
|
{
|
|
|
|
return "incomplete torrents directory path is not absolute";
|
|
|
|
}
|
2015-11-05 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t i;
|
|
|
|
double d;
|
|
|
|
bool boolVal;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_alt_speed_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionUseAltSpeed(session, boolVal);
|
|
|
|
}
|
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))
|
|
|
|
{
|
|
|
|
tr_sessionSetAltSpeedDay(session, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_alt_speed_time_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionUseAltSpeedTime(session, boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_blocklist_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_blocklistSetEnabled(session, boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindStr(args_in, TR_KEY_blocklist_url, &str, NULL))
|
|
|
|
{
|
|
|
|
tr_blocklistSetURL(session, str);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (download_dir != NULL)
|
|
|
|
{
|
|
|
|
tr_sessionSetDownloadDir(session, download_dir);
|
|
|
|
}
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_queue_stalled_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetQueueStalledEnabled(session, boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(args_in, TR_KEY_download_queue_size, &i))
|
|
|
|
{
|
|
|
|
tr_sessionSetQueueSize(session, TR_DOWN, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_download_queue_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetQueueEnabled(session, TR_DOWN, boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (incomplete_dir != NULL)
|
|
|
|
{
|
|
|
|
tr_sessionSetIncompleteDir(session, incomplete_dir);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_incomplete_dir_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetIncompleteDirEnabled(session, boolVal);
|
|
|
|
}
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_pex_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetPexEnabled(session, boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_dht_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetDHTEnabled(session, boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_utp_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetUTPEnabled(session, boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_lpd_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetLPDEnabled(session, boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_peer_port_random_on_start, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetPeerPortRandomOnStart(session, boolVal);
|
|
|
|
}
|
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))
|
|
|
|
{
|
|
|
|
tr_sessionSetPeerPort(session, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_port_forwarding_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetPortForwardingEnabled(session, boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_rename_partial_files, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetIncompleteFileNamingEnabled(session, boolVal);
|
|
|
|
}
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_seedRatioLimited, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetRatioLimited(session, boolVal);
|
|
|
|
}
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_idle_seeding_limit_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetIdleLimited(session, boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_start_added_torrents, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetPaused(session, !boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_seed_queue_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetQueueEnabled(session, TR_UP, boolVal);
|
|
|
|
}
|
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))
|
|
|
|
{
|
|
|
|
tr_sessionSetQueueSize(session, TR_UP, i);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindStr(args_in, TR_KEY_script_torrent_done_filename, &str, NULL))
|
|
|
|
{
|
|
|
|
tr_sessionSetTorrentDoneScript(session, str);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_script_torrent_done_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetTorrentDoneScriptEnabled(session, boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_trash_original_torrent_files, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionSetDeleteSource(session, boolVal);
|
|
|
|
}
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_speed_limit_down_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionLimitSpeed(session, TR_DOWN, boolVal);
|
|
|
|
}
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(args_in, TR_KEY_speed_limit_up_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
tr_sessionLimitSpeed(session, TR_UP, boolVal);
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindStr(args_in, TR_KEY_encryption, &str, NULL))
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_strcmp0(str, "required") == 0)
|
|
|
|
{
|
|
|
|
tr_sessionSetEncryption(session, TR_ENCRYPTION_REQUIRED);
|
|
|
|
}
|
|
|
|
else if (tr_strcmp0(str, "tolerated") == 0)
|
|
|
|
{
|
|
|
|
tr_sessionSetEncryption(session, TR_CLEAR_PREFERRED);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_sessionSetEncryption(session, TR_ENCRYPTION_PREFERRED);
|
|
|
|
}
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
notify(session, TR_RPC_SESSION_CHANGED, NULL);
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return NULL;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* sessionStats(tr_session* session, tr_variant* args_in UNUSED, tr_variant* args_out,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2008-06-18 22:01:15 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int running = 0;
|
|
|
|
int total = 0;
|
|
|
|
tr_variant* d;
|
|
|
|
tr_session_stats currentStats = { 0.0f, 0, 0, 0, 0, 0 };
|
|
|
|
tr_session_stats cumulativeStats = { 0.0f, 0, 0, 0, 0, 0 };
|
|
|
|
tr_torrent* tor = NULL;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(idle_data == NULL);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((tor = tr_torrentNext(session, tor)) != NULL)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
++total;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tor->isRunning)
|
|
|
|
{
|
|
|
|
++running;
|
|
|
|
}
|
2008-06-18 22:01:15 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sessionGetStats(session, ¤tStats);
|
|
|
|
tr_sessionGetCumulativeStats(session, &cumulativeStats);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(args_out, TR_KEY_activeTorrentCount, running);
|
|
|
|
tr_variantDictAddReal(args_out, TR_KEY_downloadSpeed, tr_sessionGetPieceSpeed_Bps(session, TR_DOWN));
|
|
|
|
tr_variantDictAddInt(args_out, TR_KEY_pausedTorrentCount, total - running);
|
|
|
|
tr_variantDictAddInt(args_out, TR_KEY_torrentCount, total);
|
|
|
|
tr_variantDictAddReal(args_out, TR_KEY_uploadSpeed, tr_sessionGetPieceSpeed_Bps(session, TR_UP));
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
d = tr_variantDictAddDict(args_out, TR_KEY_cumulative_stats, 5);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_downloadedBytes, cumulativeStats.downloadedBytes);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_filesAdded, cumulativeStats.filesAdded);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_secondsActive, cumulativeStats.secondsActive);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_sessionCount, cumulativeStats.sessionCount);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_uploadedBytes, cumulativeStats.uploadedBytes);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
d = tr_variantDictAddDict(args_out, TR_KEY_current_stats, 5);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_downloadedBytes, currentStats.downloadedBytes);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_filesAdded, currentStats.filesAdded);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_secondsActive, currentStats.secondsActive);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_sessionCount, currentStats.sessionCount);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_uploadedBytes, currentStats.uploadedBytes);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return NULL;
|
2008-06-18 22:01:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void addSessionField(tr_session* 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:
|
|
|
|
tr_variantDictAddBool(d, key, tr_blocklistIsEnabled(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_blocklist_url:
|
|
|
|
tr_variantDictAddStr(d, key, tr_blocklistGetURL(s));
|
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:
|
|
|
|
tr_variantDictAddStr(d, key, tr_sessionGetConfigDir(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_download_dir:
|
|
|
|
tr_variantDictAddStr(d, key, tr_sessionGetDownloadDir(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_download_dir_free_space:
|
|
|
|
tr_variantDictAddInt(d, key, tr_device_info_get_free_space(s->downloadDir));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_download_queue_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionGetQueueEnabled(s, 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:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetQueueSize(s, 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:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetPeerLimit(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peer_limit_per_torrent:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetPeerLimitPerTorrent(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_incomplete_dir:
|
|
|
|
tr_variantDictAddStr(d, key, tr_sessionGetIncompleteDir(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_incomplete_dir_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionIsIncompleteDirEnabled(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_pex_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionIsPexEnabled(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_utp_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionIsUTPEnabled(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_dht_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionIsDHTEnabled(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_lpd_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionIsLPDEnabled(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_peer_port:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetPeerPort(s));
|
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:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionGetPeerPortRandomOnStart(s));
|
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:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionIsIncompleteFileNamingEnabled(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_rpc_version:
|
|
|
|
tr_variantDictAddInt(d, key, RPC_VERSION);
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_rpc_version_minimum:
|
|
|
|
tr_variantDictAddInt(d, key, RPC_VERSION_MIN);
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seedRatioLimit:
|
|
|
|
tr_variantDictAddReal(d, key, tr_sessionGetRatioLimit(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seedRatioLimited:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionIsRatioLimited(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_idle_seeding_limit:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetIdleLimit(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_idle_seeding_limit_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionIsIdleLimited(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_seed_queue_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionGetQueueEnabled(s, 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:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetQueueSize(s, 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:
|
|
|
|
tr_variantDictAddBool(d, key, !tr_sessionGetPaused(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_trash_original_torrent_files:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionGetDeleteSource(s));
|
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:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionIsSpeedLimited(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_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:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionIsSpeedLimited(s, TR_DOWN));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_script_torrent_done_filename:
|
|
|
|
tr_variantDictAddStr(d, key, tr_sessionGetTorrentDoneScript(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_script_torrent_done_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionIsTorrentDoneScriptEnabled(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_queue_stalled_enabled:
|
|
|
|
tr_variantDictAddBool(d, key, tr_sessionGetQueueStalledEnabled(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_queue_stalled_minutes:
|
|
|
|
tr_variantDictAddInt(d, key, tr_sessionGetQueueStalledMinutes(s));
|
2016-09-04 10:16:20 +00:00
|
|
|
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:
|
|
|
|
tr_variantDictAddStr(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:
|
2016-09-04 10:16:20 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2016-09-04 10:16:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (tr_sessionGetEncryption(s))
|
2016-09-04 10:16:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_CLEAR_PREFERRED:
|
2016-09-04 10:16:20 +00:00
|
|
|
str = "tolerated";
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_ENCRYPTION_REQUIRED:
|
2016-09-04 10:16:20 +00:00
|
|
|
str = "required";
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2016-09-04 10:16:20 +00:00
|
|
|
str = "preferred";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(d, key, str);
|
|
|
|
break;
|
2016-09-04 10:16:20 +00:00
|
|
|
}
|
2016-09-05 19:16:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_session_id:
|
|
|
|
tr_variantDictAddStr(d, key, tr_session_id_get_current(s->session_id));
|
2016-09-05 19:16:56 +00:00
|
|
|
break;
|
2016-09-04 10:16:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* sessionGet(tr_session* s, tr_variant* args_in, tr_variant* args_out,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* fields;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(idle_data == NULL);
|
2016-09-04 10:16:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* field_name;
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t field_name_len;
|
|
|
|
tr_quark field_id;
|
2016-09-04 10:16:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_variantGetStr(tr_variantListChild(fields, i), &field_name, &field_name_len))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-04 10:16:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_quark_lookup(field_name, field_name_len, &field_id))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-04 10:16:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
addSessionField(s, args_out, field_id);
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return NULL;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* freeSpace(tr_session* session, tr_variant* args_in, tr_variant* args_out,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2013-02-09 04:05:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int tmperr;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* path = NULL;
|
|
|
|
char const* err = NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t free_space = -1;
|
|
|
|
|
|
|
|
if (!tr_variantDictFindStr(args_in, TR_KEY_path, &path, NULL))
|
|
|
|
{
|
|
|
|
return "directory path argument is missing";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_sys_path_is_relative(path))
|
|
|
|
{
|
|
|
|
return "directory path is not absolute";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the free space */
|
|
|
|
tmperr = errno;
|
|
|
|
errno = 0;
|
|
|
|
free_space = tr_sessionGetDirFreeSpace(session, path);
|
|
|
|
|
|
|
|
if (free_space < 0)
|
|
|
|
{
|
|
|
|
err = tr_strerror(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = tmperr;
|
|
|
|
|
|
|
|
/* response */
|
|
|
|
if (path != NULL)
|
|
|
|
{
|
|
|
|
tr_variantDictAddStr(args_out, TR_KEY_path, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantDictAddInt(args_out, TR_KEY_size_bytes, free_space);
|
|
|
|
return err;
|
2013-02-09 04:05:03 +00:00
|
|
|
}
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* sessionClose(tr_session* session, tr_variant* args_in UNUSED, tr_variant* args_out UNUSED,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_rpc_idle_data* idle_data UNUSED)
|
2011-01-15 18:12:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
notify(session, TR_RPC_SESSION_CLOSE, NULL);
|
|
|
|
return NULL;
|
2011-01-15 18:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
typedef char const* (* handler)(tr_session*, tr_variant*, tr_variant*, struct tr_rpc_idle_data*);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
static struct method
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* name;
|
2017-04-19 12:04:45 +00:00
|
|
|
bool immediate;
|
|
|
|
handler func;
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
methods[] =
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
{ "port-test", false, portTest },
|
|
|
|
{ "blocklist-update", false, blocklistUpdate },
|
|
|
|
{ "free-space", true, freeSpace },
|
|
|
|
{ "session-close", true, sessionClose },
|
|
|
|
{ "session-get", true, sessionGet },
|
|
|
|
{ "session-set", true, sessionSet },
|
|
|
|
{ "session-stats", true, sessionStats },
|
|
|
|
{ "torrent-add", false, torrentAdd },
|
|
|
|
{ "torrent-get", true, torrentGet },
|
|
|
|
{ "torrent-remove", true, torrentRemove },
|
|
|
|
{ "torrent-rename-path", false, torrentRenamePath },
|
|
|
|
{ "torrent-set", true, torrentSet },
|
|
|
|
{ "torrent-set-location", true, torrentSetLocation },
|
|
|
|
{ "torrent-start", true, torrentStart },
|
|
|
|
{ "torrent-start-now", true, torrentStartNow },
|
|
|
|
{ "torrent-stop", true, torrentStop },
|
|
|
|
{ "torrent-verify", true, torrentVerify },
|
|
|
|
{ "torrent-reannounce", true, torrentReannounce },
|
|
|
|
{ "queue-move-top", true, queueMoveTop },
|
|
|
|
{ "queue-move-up", true, queueMoveUp },
|
|
|
|
{ "queue-move-down", true, queueMoveDown },
|
|
|
|
{ "queue-move-bottom", true, queueMoveBottom }
|
2008-05-12 13:05:06 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void noop_response_callback(tr_session* session UNUSED, tr_variant* response UNUSED, void* user_data UNUSED)
|
2009-01-20 02:03:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* const mutable_request = (tr_variant*)request;
|
|
|
|
tr_variant* args_in = tr_variantDictFind(mutable_request, TR_KEY_arguments);
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* result = NULL;
|
2017-05-13 22:38:31 +00:00
|
|
|
struct method* method = NULL;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (callback == NULL)
|
|
|
|
{
|
|
|
|
callback = noop_response_callback;
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* parse the request */
|
|
|
|
if (!tr_variantDictFindStr(mutable_request, TR_KEY_method, &str, NULL))
|
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
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; method == NULL && i < TR_N_ELEMENTS(methods); ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (strcmp(str, methods[i].name) == 0)
|
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
method = &methods[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
if (method == NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
result = "method name not recognized";
|
|
|
|
}
|
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 */
|
|
|
|
if (result != NULL)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t tag;
|
|
|
|
tr_variant response;
|
2009-01-18 15:24:26 +00:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(mutable_request, TR_KEY_tag, &tag))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t tag;
|
|
|
|
tr_variant response;
|
|
|
|
tr_variant* args_out;
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(&response, 3);
|
|
|
|
args_out = tr_variantDictAddDict(&response, TR_KEY_arguments, 0);
|
2017-05-13 22:38:31 +00:00
|
|
|
result = (*method->func)(session, args_in, args_out, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
result = "success";
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantDictAddStr(&response, TR_KEY_result, result);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(mutable_request, TR_KEY_tag, &tag))
|
|
|
|
{
|
|
|
|
tr_variantDictAddInt(&response, TR_KEY_tag, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
(*callback)(session, &response, callback_user_data);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t tag;
|
|
|
|
struct tr_rpc_idle_data* data = tr_new0(struct tr_rpc_idle_data, 1);
|
|
|
|
data->session = session;
|
|
|
|
data->response = tr_new0(tr_variant, 1);
|
|
|
|
tr_variantInitDict(data->response, 3);
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(mutable_request, TR_KEY_tag, &tag))
|
|
|
|
{
|
|
|
|
tr_variantDictAddInt(data->response, TR_KEY_tag, tag);
|
|
|
|
}
|
2015-10-20 21:06:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
data->args_out = tr_variantDictAddDict(data->response, TR_KEY_arguments, 0);
|
|
|
|
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 */
|
|
|
|
if (result != NULL)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_rpc_parse_list_str(tr_variant* setme, char const* str, size_t len)
|
2008-05-24 18:22:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int valueCount;
|
|
|
|
int* values = tr_parseNumberRange(str, len, &valueCount);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (valueCount == 0)
|
2013-05-23 00:11:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitStr(setme, str, len);
|
2013-05-23 00:11:09 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (valueCount == 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitList(setme, valueCount);
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < valueCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantListAddInt(setme, values[i]);
|
|
|
|
}
|
2008-05-24 18:22:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(values);
|
2008-05-24 18:22:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_rpc_request_exec_uri(tr_session* session, void const* request_uri, size_t request_uri_len,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_rpc_response_func callback, void* callback_user_data)
|
2008-05-13 12:52:58 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* pch;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant top;
|
|
|
|
tr_variant* args;
|
|
|
|
char* request = tr_strndup(request_uri, request_uri_len);
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(&top, 3);
|
|
|
|
args = tr_variantDictAddDict(&top, TR_KEY_arguments, 0);
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
pch = strchr(request, '?');
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (pch == NULL)
|
2008-05-13 17:31:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
pch = request;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while (pch != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* delim = strchr(pch, '=');
|
|
|
|
char const* next = strchr(pch, '&');
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (delim != NULL)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* key = tr_strndup(pch, (size_t)(delim - pch));
|
|
|
|
bool isArg = strcmp(key, "method") != 0 && strcmp(key, "tag") != 0;
|
|
|
|
tr_variant* parent = isArg ? args : ⊤
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_rpc_parse_list_str(tr_variantDictAdd(parent, tr_quark_new(key, (size_t)(delim - pch))), delim + 1,
|
2017-04-30 16:25:26 +00:00
|
|
|
next != NULL ? (size_t)(next - (delim + 1)) : strlen(delim + 1));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(key);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2013-05-23 00:11:09 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
pch = next != NULL ? next + 1 : NULL;
|
2008-05-13 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_rpc_request_exec_json(session, &top, callback, callback_user_data);
|
2008-05-13 17:31:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* cleanup */
|
|
|
|
tr_variantFree(&top);
|
|
|
|
tr_free(request);
|
2008-05-13 12:52:58 +00:00
|
|
|
}
|