2008-05-12 00:41:55 +00:00
|
|
|
/*
|
2010-01-04 21:00:47 +00:00
|
|
|
* This file Copyright (C) 2008-2010 Mnemosyne LLC
|
2008-05-12 00:41:55 +00:00
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-05-12 00:41:55 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2008-05-28 17:17:12 +00:00
|
|
|
* $Id$
|
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 */
|
2009-04-03 23:21:40 +00:00
|
|
|
#include <unistd.h> /* unlink */
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
#include <event.h> /* evbuffer */
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "bencode.h"
|
2009-04-13 19:04:21 +00:00
|
|
|
#include "completion.h"
|
2008-05-13 12:52:58 +00:00
|
|
|
#include "json.h"
|
2009-04-13 19:04:21 +00:00
|
|
|
#include "rpcimpl.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
#include "session.h"
|
2009-01-19 21:17:29 +00:00
|
|
|
#include "stats.h"
|
2008-05-12 00:41:55 +00:00
|
|
|
#include "torrent.h"
|
|
|
|
#include "utils.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
|
|
|
|
2010-07-23 00:48:45 +00:00
|
|
|
#define RPC_VERSION 10
|
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
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#define TR_N_ELEMENTS( ary ) ( sizeof( ary ) / sizeof( *ary ) )
|
|
|
|
|
2009-01-20 02:03:09 +00:00
|
|
|
#if 0
|
|
|
|
#define dbgmsg(fmt, ...) \
|
|
|
|
do { \
|
|
|
|
fprintf( stderr, "%s:%d"#fmt, __FILE__, __LINE__, __VA_ARGS__ ); \
|
|
|
|
fprintf( stderr, "\n" ); \
|
|
|
|
} while( 0 )
|
|
|
|
#else
|
2009-01-18 15:24:26 +00:00
|
|
|
#define dbgmsg( ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_deepLoggingIsActive( ) ) \
|
|
|
|
tr_deepLog( __FILE__, __LINE__, "RPC", __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
2009-01-20 02:03:09 +00:00
|
|
|
#endif
|
2009-01-18 15:24:26 +00:00
|
|
|
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-08-14 02:12:29 +00:00
|
|
|
static tr_rpc_callback_status
|
2008-12-14 11:21:11 +00:00
|
|
|
notify( tr_session * session,
|
2008-09-23 19:11:04 +00:00
|
|
|
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;
|
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
if( session->rpc_func )
|
2008-09-23 19:11:04 +00:00
|
|
|
status = session->rpc_func( session, type, tor,
|
|
|
|
session->rpc_func_user_data );
|
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
|
|
|
|
{
|
|
|
|
tr_session * session;
|
|
|
|
tr_benc * response;
|
|
|
|
tr_benc * args_out;
|
2009-01-19 18:11:47 +00:00
|
|
|
tr_rpc_response_func callback;
|
2009-01-18 15:24:26 +00:00
|
|
|
void * callback_user_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2009-01-19 14:10:39 +00:00
|
|
|
tr_idle_function_done( struct tr_rpc_idle_data * data, const char * result )
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2009-06-02 01:48:48 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2009-01-18 15:24:26 +00:00
|
|
|
|
|
|
|
if( result == NULL )
|
|
|
|
result = "success";
|
|
|
|
tr_bencDictAddStr( data->response, "result", result );
|
|
|
|
|
2009-06-02 01:48:48 +00:00
|
|
|
tr_bencToBuf( data->response, TR_FMT_JSON_LEAN, buf );
|
2009-01-19 18:11:47 +00:00
|
|
|
(*data->callback)( data->session, (const char*)EVBUFFER_DATA(buf),
|
|
|
|
EVBUFFER_LENGTH(buf), data->callback_user_data );
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2009-06-02 01:48:48 +00:00
|
|
|
evbuffer_free( buf );
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_bencFree( data->response );
|
|
|
|
tr_free( data->response );
|
|
|
|
tr_free( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
static tr_torrent **
|
2008-12-14 11:21:11 +00:00
|
|
|
getTorrents( tr_session * session,
|
|
|
|
tr_benc * args,
|
|
|
|
int * setmeCount )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int torrentCount = 0;
|
|
|
|
int64_t id;
|
2008-05-12 00:41:55 +00:00
|
|
|
tr_torrent ** torrents = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * ids;
|
2009-03-23 00:16:37 +00:00
|
|
|
const char * str;
|
2008-05-12 00:41:55 +00:00
|
|
|
|
|
|
|
if( tr_bencDictFindList( args, "ids", &ids ) )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
2008-05-12 00:41:55 +00:00
|
|
|
const int n = tr_bencListSize( ids );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
torrents = tr_new0( tr_torrent *, n );
|
2008-05-12 00:41:55 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
|
|
|
tr_torrent * tor = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * node = tr_bencListChild( ids, i );
|
2008-05-12 00:41:55 +00:00
|
|
|
const char * str;
|
|
|
|
if( tr_bencGetInt( node, &id ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tor = tr_torrentFindFromId( session, id );
|
2008-05-12 00:41:55 +00:00
|
|
|
else if( tr_bencGetStr( node, &str ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tor = tr_torrentFindFromHashString( session, str );
|
2008-05-12 00:41:55 +00:00
|
|
|
if( tor )
|
|
|
|
torrents[torrentCount++] = tor;
|
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else if( tr_bencDictFindInt( args, "ids", &id )
|
|
|
|
|| tr_bencDictFindInt( args, "id", &id ) )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
|
|
|
tr_torrent * tor;
|
2008-09-23 19:11:04 +00:00
|
|
|
torrents = tr_new0( tr_torrent *, 1 );
|
2008-12-14 11:21:11 +00:00
|
|
|
if( ( tor = tr_torrentFindFromId( session, id ) ) )
|
2008-05-18 16:44:30 +00:00
|
|
|
torrents[torrentCount++] = tor;
|
|
|
|
}
|
2009-03-23 00:16:37 +00:00
|
|
|
else if( tr_bencDictFindStr( args, "ids", &str ) )
|
|
|
|
{
|
|
|
|
if( !strcmp( str, "recently-active" ) )
|
|
|
|
{
|
|
|
|
tr_torrent * tor = NULL;
|
2009-11-26 18:47:08 +00:00
|
|
|
const time_t now = tr_time( );
|
2009-04-04 05:29:08 +00:00
|
|
|
const time_t window = RECENTLY_ACTIVE_SECONDS;
|
2009-03-23 00:16:37 +00:00
|
|
|
const int n = tr_sessionCountTorrents( session );
|
|
|
|
torrents = tr_new0( tr_torrent *, n );
|
2009-03-26 18:06:54 +00:00
|
|
|
while( ( tor = tr_torrentNext( session, tor ) ) )
|
|
|
|
if( tor->anyDate >= now - window )
|
2009-03-23 00:16:37 +00:00
|
|
|
torrents[torrentCount++] = tor;
|
|
|
|
}
|
2009-05-08 17:24:16 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_torrent * tor;
|
|
|
|
torrents = tr_new0( tr_torrent *, 1 );
|
|
|
|
if(( tor = tr_torrentFindFromHashString( session, str )))
|
|
|
|
torrents[torrentCount++] = tor;
|
|
|
|
}
|
2009-03-23 00:16:37 +00:00
|
|
|
}
|
2008-05-12 00:41:55 +00:00
|
|
|
else /* all of them */
|
|
|
|
{
|
|
|
|
tr_torrent * tor = NULL;
|
2010-04-29 23:08:11 +00:00
|
|
|
const int n = tr_sessionCountTorrents( session );
|
2008-09-23 19:11:04 +00:00
|
|
|
torrents = tr_new0( tr_torrent *, n );
|
2008-12-14 11:21:11 +00:00
|
|
|
while( ( tor = tr_torrentNext( session, tor ) ) )
|
2008-05-12 00:41:55 +00:00
|
|
|
torrents[torrentCount++] = tor;
|
|
|
|
}
|
|
|
|
|
|
|
|
*setmeCount = torrentCount;
|
|
|
|
return torrents;
|
|
|
|
}
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static const char*
|
2009-01-18 15:24:26 +00:00
|
|
|
torrentStart( tr_session * session,
|
|
|
|
tr_benc * args_in,
|
|
|
|
tr_benc * args_out UNUSED,
|
2009-12-28 23:27:17 +00:00
|
|
|
struct tr_rpc_idle_data * idle_data UNUSED )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i, torrentCount;
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
assert( idle_data == NULL );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < torrentCount; ++i )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
|
|
|
tr_torrent * tor = torrents[i];
|
2010-02-21 23:03:29 +00:00
|
|
|
if( !tor->isRunning )
|
|
|
|
{
|
|
|
|
tr_torrentStart( tor );
|
|
|
|
notify( session, TR_RPC_TORRENT_STARTED, tor );
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-05-12 00:41:55 +00:00
|
|
|
tr_free( torrents );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
2009-01-18 15:24:26 +00:00
|
|
|
torrentStop( tr_session * session,
|
|
|
|
tr_benc * args_in,
|
|
|
|
tr_benc * args_out UNUSED,
|
2009-12-28 23:27:17 +00:00
|
|
|
struct tr_rpc_idle_data * idle_data UNUSED )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i, torrentCount;
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
assert( idle_data == NULL );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < torrentCount; ++i )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
|
|
|
tr_torrent * tor = torrents[i];
|
2010-06-25 06:57:34 +00:00
|
|
|
|
2010-02-21 23:03:29 +00:00
|
|
|
if( tor->isRunning )
|
|
|
|
{
|
2010-06-25 06:57:34 +00:00
|
|
|
tor->isStopping = TRUE;
|
2010-02-21 23:03:29 +00:00
|
|
|
notify( session, TR_RPC_TORRENT_STOPPED, tor );
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
tr_free( torrents );
|
2008-05-12 00:41:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-20 23:58:59 +00:00
|
|
|
static const char*
|
2009-01-18 15:24:26 +00:00
|
|
|
torrentRemove( tr_session * session,
|
|
|
|
tr_benc * args_in,
|
|
|
|
tr_benc * args_out UNUSED,
|
2009-12-28 23:27:17 +00:00
|
|
|
struct tr_rpc_idle_data * idle_data UNUSED )
|
2008-05-20 23:58:59 +00:00
|
|
|
{
|
2008-12-09 17:01:49 +00:00
|
|
|
int i;
|
|
|
|
int torrentCount;
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
assert( idle_data == NULL );
|
|
|
|
|
2008-12-09 17:01:49 +00:00
|
|
|
for( i=0; i<torrentCount; ++i )
|
2008-05-20 23:58:59 +00:00
|
|
|
{
|
2008-12-09 17:01:49 +00:00
|
|
|
tr_torrent * tor = torrents[i];
|
2008-12-14 11:21:11 +00:00
|
|
|
const tr_rpc_callback_status status = notify( session, TR_RPC_TORRENT_REMOVING, tor );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bool deleteFlag;
|
|
|
|
if( tr_bencDictFindBool( args_in, "delete-local-data", &deleteFlag ) && deleteFlag )
|
2009-12-09 03:51:21 +00:00
|
|
|
tr_torrentDeleteLocalData( tor, NULL );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !( status & TR_RPC_NOREMOVE ) )
|
2008-08-14 02:12:29 +00:00
|
|
|
tr_torrentRemove( tor );
|
2008-05-20 23:58:59 +00:00
|
|
|
}
|
2008-12-14 11:21:11 +00:00
|
|
|
|
2008-05-20 23:58:59 +00:00
|
|
|
tr_free( torrents );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-26 18:06:54 +00:00
|
|
|
static const char*
|
|
|
|
torrentReannounce( tr_session * session,
|
|
|
|
tr_benc * args_in,
|
|
|
|
tr_benc * args_out UNUSED,
|
2009-12-28 23:27:17 +00:00
|
|
|
struct tr_rpc_idle_data * idle_data UNUSED )
|
2009-03-26 18:06:54 +00:00
|
|
|
{
|
|
|
|
int i, torrentCount;
|
|
|
|
tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
|
|
|
|
|
|
|
|
assert( idle_data == NULL );
|
|
|
|
|
|
|
|
for( i=0; i<torrentCount; ++i )
|
|
|
|
{
|
|
|
|
tr_torrent * tor = torrents[i];
|
|
|
|
if( tr_torrentCanManualUpdate( tor ) )
|
|
|
|
{
|
|
|
|
tr_torrentManualUpdate( tor );
|
|
|
|
notify( session, TR_RPC_TORRENT_CHANGED, tor );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( torrents );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
static const char*
|
2009-01-18 15:24:26 +00:00
|
|
|
torrentVerify( tr_session * session,
|
|
|
|
tr_benc * args_in,
|
|
|
|
tr_benc * args_out UNUSED,
|
2009-12-28 23:27:17 +00:00
|
|
|
struct tr_rpc_idle_data * idle_data UNUSED )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i, torrentCount;
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
assert( idle_data == NULL );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < torrentCount; ++i )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
|
|
|
tr_torrent * tor = torrents[i];
|
|
|
|
tr_torrentVerify( tor );
|
2008-12-14 11:21:11 +00:00
|
|
|
notify( session, TR_RPC_TORRENT_CHANGED, tor );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-12-14 11:21:11 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_free( torrents );
|
2008-05-12 00:41:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-03-31 22:13:43 +00:00
|
|
|
static void
|
|
|
|
addFileStats( const tr_torrent * tor, tr_benc * list )
|
|
|
|
{
|
|
|
|
tr_file_index_t i;
|
|
|
|
tr_file_index_t n;
|
|
|
|
const tr_info * info = tr_torrentInfo( tor );
|
|
|
|
tr_file_stat * files = tr_torrentFiles( tor, &n );
|
|
|
|
|
|
|
|
for( i = 0; i < info->fileCount; ++i )
|
|
|
|
{
|
|
|
|
const tr_file * file = &info->files[i];
|
|
|
|
tr_benc * d = tr_bencListAddDict( list, 3 );
|
|
|
|
tr_bencDictAddInt( d, "bytesCompleted", files[i].bytesCompleted );
|
|
|
|
tr_bencDictAddInt( d, "priority", file->priority );
|
|
|
|
tr_bencDictAddBool( d, "wanted", !file->dnd );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrentFilesFree( files, n );
|
|
|
|
}
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
addFiles( const tr_torrent * tor,
|
|
|
|
tr_benc * list )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_file_index_t i;
|
2008-07-21 18:42:51 +00:00
|
|
|
tr_file_index_t n;
|
|
|
|
const tr_info * info = tr_torrentInfo( tor );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_file_stat * files = tr_torrentFiles( tor, &n );
|
2008-07-21 18:42:51 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < info->fileCount; ++i )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
|
|
|
const tr_file * file = &info->files[i];
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * d = tr_bencListAddDict( list, 3 );
|
2008-07-21 18:42:51 +00:00
|
|
|
tr_bencDictAddInt( d, "bytesCompleted", files[i].bytesCompleted );
|
2008-05-12 00:41:55 +00:00
|
|
|
tr_bencDictAddInt( d, "length", file->length );
|
|
|
|
tr_bencDictAddStr( d, "name", file->name );
|
|
|
|
}
|
2008-07-21 18:42:51 +00:00
|
|
|
|
|
|
|
tr_torrentFilesFree( files, n );
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2008-06-16 03:47:50 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
addWebseeds( const tr_info * info,
|
|
|
|
tr_benc * webseeds )
|
2008-06-16 03:47:50 +00:00
|
|
|
{
|
|
|
|
int i;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
for( i = 0; i < info->webseedCount; ++i )
|
2008-06-16 03:47:50 +00:00
|
|
|
tr_bencListAddStr( webseeds, info->webseeds[i] );
|
|
|
|
}
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
addTrackers( const tr_info * info,
|
|
|
|
tr_benc * trackers )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
|
|
|
int i;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
for( i = 0; i < info->trackerCount; ++i )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
|
|
|
const tr_tracker_info * t = &info->trackers[i];
|
2010-01-10 04:23:09 +00:00
|
|
|
tr_benc * d = tr_bencListAddDict( trackers, 4 );
|
2008-05-12 00:41:55 +00:00
|
|
|
tr_bencDictAddStr( d, "announce", t->announce );
|
2010-01-13 22:40:07 +00:00
|
|
|
tr_bencDictAddInt( d, "id", t->id );
|
2008-05-12 00:41:55 +00:00
|
|
|
tr_bencDictAddStr( d, "scrape", t->scrape );
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_bencDictAddInt( d, "tier", t->tier );
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static void
|
|
|
|
addTrackerStats( const tr_tracker_stat * st, int n, tr_benc * list )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
{
|
|
|
|
const tr_tracker_stat * s = &st[i];
|
2010-09-01 20:54:04 +00:00
|
|
|
tr_benc * d = tr_bencListAddDict( list, 26 );
|
2009-09-26 03:36:58 +00:00
|
|
|
tr_bencDictAddStr ( d, "announce", s->announce );
|
2009-10-09 21:30:34 +00:00
|
|
|
tr_bencDictAddInt ( d, "announceState", s->announceState );
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_bencDictAddInt ( d, "downloadCount", s->downloadCount );
|
|
|
|
tr_bencDictAddBool( d, "hasAnnounced", s->hasAnnounced );
|
|
|
|
tr_bencDictAddBool( d, "hasScraped", s->hasScraped );
|
|
|
|
tr_bencDictAddStr ( d, "host", s->host );
|
2010-01-13 22:46:22 +00:00
|
|
|
tr_bencDictAddInt ( d, "id", s->id );
|
2009-10-10 00:17:00 +00:00
|
|
|
tr_bencDictAddBool( d, "isBackup", s->isBackup );
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_bencDictAddInt ( d, "lastAnnouncePeerCount", s->lastAnnouncePeerCount );
|
|
|
|
tr_bencDictAddStr ( d, "lastAnnounceResult", s->lastAnnounceResult );
|
|
|
|
tr_bencDictAddInt ( d, "lastAnnounceStartTime", s->lastAnnounceStartTime );
|
|
|
|
tr_bencDictAddBool( d, "lastAnnounceSucceeded", s->lastAnnounceSucceeded );
|
|
|
|
tr_bencDictAddInt ( d, "lastAnnounceTime", s->lastAnnounceTime );
|
2010-02-05 05:16:18 +00:00
|
|
|
tr_bencDictAddBool( d, "lastAnnounceTimedOut", s->lastAnnounceTimedOut );
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_bencDictAddStr ( d, "lastScrapeResult", s->lastScrapeResult );
|
|
|
|
tr_bencDictAddInt ( d, "lastScrapeStartTime", s->lastScrapeStartTime );
|
|
|
|
tr_bencDictAddBool( d, "lastScrapeSucceeded", s->lastScrapeSucceeded );
|
|
|
|
tr_bencDictAddInt ( d, "lastScrapeTime", s->lastScrapeTime );
|
2010-02-25 23:06:05 +00:00
|
|
|
tr_bencDictAddInt ( d, "lastScrapeTimedOut", s->lastScrapeTimedOut );
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_bencDictAddInt ( d, "leecherCount", s->leecherCount );
|
|
|
|
tr_bencDictAddInt ( d, "nextAnnounceTime", s->nextAnnounceTime );
|
|
|
|
tr_bencDictAddInt ( d, "nextScrapeTime", s->nextScrapeTime );
|
2010-09-01 20:54:04 +00:00
|
|
|
tr_bencDictAddStr ( d, "scrape", s->scrape );
|
2009-10-09 21:30:34 +00:00
|
|
|
tr_bencDictAddInt ( d, "scrapeState", s->scrapeState );
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_bencDictAddInt ( d, "seederCount", s->seederCount );
|
|
|
|
tr_bencDictAddInt ( d, "tier", s->tier );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-22 17:59:31 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
addPeers( const tr_torrent * tor,
|
|
|
|
tr_benc * list )
|
2008-08-22 17:59:31 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
int peerCount;
|
2008-08-22 17:59:31 +00:00
|
|
|
tr_peer_stat * peers = tr_torrentPeers( tor, &peerCount );
|
|
|
|
|
|
|
|
tr_bencInitList( list, peerCount );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < peerCount; ++i )
|
2008-08-22 17:59:31 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * d = tr_bencListAddDict( list, 14 );
|
2008-08-22 17:59:31 +00:00
|
|
|
const tr_peer_stat * peer = peers + i;
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddStr ( d, "address", peer->addr );
|
|
|
|
tr_bencDictAddStr ( d, "clientName", peer->client );
|
|
|
|
tr_bencDictAddBool( d, "clientIsChoked", peer->clientIsChoked );
|
|
|
|
tr_bencDictAddBool( d, "clientIsInterested", peer->clientIsInterested );
|
|
|
|
tr_bencDictAddStr ( d, "flagStr", peer->flagStr );
|
|
|
|
tr_bencDictAddBool( d, "isDownloadingFrom", peer->isDownloadingFrom );
|
|
|
|
tr_bencDictAddBool( d, "isEncrypted", peer->isEncrypted );
|
|
|
|
tr_bencDictAddBool( d, "isIncoming", peer->isIncoming );
|
|
|
|
tr_bencDictAddBool( d, "isUploadingTo", peer->isUploadingTo );
|
|
|
|
tr_bencDictAddBool( d, "peerIsChoked", peer->peerIsChoked );
|
|
|
|
tr_bencDictAddBool( d, "peerIsInterested", peer->peerIsInterested );
|
|
|
|
tr_bencDictAddInt ( d, "port", peer->port );
|
|
|
|
tr_bencDictAddReal( d, "progress", peer->progress );
|
2010-09-03 04:30:43 +00:00
|
|
|
tr_bencDictAddInt ( d, "rateToClient", toSpeedBytes( peer->rateToClient_KBps ) );
|
|
|
|
tr_bencDictAddInt ( d, "rateToPeer", toSpeedBytes( peer->rateToPeer_KBps ) );
|
2008-08-22 17:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrentPeersFree( peers, peerCount );
|
|
|
|
}
|
|
|
|
|
2009-05-15 20:00:12 +00:00
|
|
|
/* faster-than-strcmp() optimization. this is kind of clumsy,
|
|
|
|
but addField() was in the profiler's top 10 list, and this
|
|
|
|
makes it 4x faster... */
|
|
|
|
#define tr_streq(a,alen,b) ((alen+1==sizeof(b)) && !memcmp(a,b,alen))
|
|
|
|
|
2008-05-12 13:05:06 +00:00
|
|
|
static void
|
2009-05-15 20:00:12 +00:00
|
|
|
addField( const tr_torrent * tor, tr_benc * d, const char * key )
|
2008-05-12 13:05:06 +00:00
|
|
|
{
|
|
|
|
const tr_info * inf = tr_torrentInfo( tor );
|
2008-06-16 03:47:50 +00:00
|
|
|
const tr_stat * st = tr_torrentStat( (tr_torrent*)tor );
|
2009-05-15 20:00:12 +00:00
|
|
|
const size_t keylen = strlen( key );
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2009-05-15 20:00:12 +00:00
|
|
|
if( tr_streq( key, keylen, "activityDate" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->activityDate );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "addedDate" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->addedDate );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "bandwidthPriority" ) )
|
2009-04-18 23:17:30 +00:00
|
|
|
tr_bencDictAddInt( d, key, tr_torrentGetPriority( tor ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "comment" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddStr( d, key, inf->comment ? inf->comment : "" );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "corruptEver" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->corruptEver );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "creator" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddStr( d, key, inf->creator ? inf->creator : "" );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "dateCreated" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, inf->dateCreated );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "desiredAvailable" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->desiredAvailable );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "doneDate" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->doneDate );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "downloadDir" ) )
|
2009-01-17 14:58:50 +00:00
|
|
|
tr_bencDictAddStr( d, key, tr_torrentGetDownloadDir( tor ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "downloadedEver" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->downloadedEver );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "downloadLimit" ) )
|
2010-07-23 01:38:34 +00:00
|
|
|
tr_bencDictAddInt( d, key, tr_torrentGetSpeedLimit_KBps( tor, TR_DOWN ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "downloadLimited" ) )
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, key, tr_torrentUsesSpeedLimit( tor, TR_DOWN ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "error" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->error );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "errorString" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddStr( d, key, st->errorString );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "eta" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->eta );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "files" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
addFiles( tor, tr_bencDictAddList( d, key, inf->fileCount ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "fileStats" ) )
|
2009-03-31 22:13:43 +00:00
|
|
|
addFileStats( tor, tr_bencDictAddList( d, key, inf->fileCount ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "hashString" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddStr( d, key, tor->info.hashString );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "haveUnchecked" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->haveUnchecked );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "haveValid" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->haveValid );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "honorsSessionLimits" ) )
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, key, tr_torrentUsesSessionLimits( tor ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "id" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->id );
|
2010-04-02 17:57:25 +00:00
|
|
|
else if( tr_streq( key, keylen, "isFinished" ) )
|
|
|
|
tr_bencDictAddBool( d, key, st->finished );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "isPrivate" ) )
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, key, tr_torrentIsPrivate( tor ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "leftUntilDone" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->leftUntilDone );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "manualAnnounceTime" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->manualAnnounceTime );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "maxConnectedPeers" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, tr_torrentGetPeerLimit( tor ) );
|
2009-12-03 15:23:43 +00:00
|
|
|
else if( tr_streq( key, keylen, "magnetLink" ) ) {
|
|
|
|
char * str = tr_torrentGetMagnetLink( tor );
|
|
|
|
tr_bencDictAddStr( d, key, str );
|
|
|
|
tr_free( str );
|
|
|
|
}
|
2009-12-03 12:40:23 +00:00
|
|
|
else if( tr_streq( key, keylen, "metadataPercentComplete" ) )
|
2009-12-02 20:05:19 +00:00
|
|
|
tr_bencDictAddReal( d, key, st->metadataPercentComplete );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "name" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddStr( d, key, inf->name );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "percentDone" ) )
|
2009-03-30 00:57:41 +00:00
|
|
|
tr_bencDictAddReal( d, key, st->percentDone );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "peer-limit" ) )
|
2009-03-27 12:05:41 +00:00
|
|
|
tr_bencDictAddInt( d, key, tr_torrentGetPeerLimit( tor ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "peers" ) )
|
2008-08-22 17:59:31 +00:00
|
|
|
addPeers( tor, tr_bencDictAdd( d, key ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "peersConnected" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->peersConnected );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "peersFrom" ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2010-01-14 00:25:03 +00:00
|
|
|
tr_benc * tmp = tr_bencDictAddDict( d, key, 6 );
|
2008-06-17 04:47:20 +00:00
|
|
|
const int * f = st->peersFrom;
|
2009-11-26 05:13:58 +00:00
|
|
|
tr_bencDictAddInt( tmp, "fromCache", f[TR_PEER_FROM_RESUME] );
|
2010-01-14 00:25:03 +00:00
|
|
|
tr_bencDictAddInt( tmp, "fromDht", f[TR_PEER_FROM_DHT] );
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( tmp, "fromIncoming", f[TR_PEER_FROM_INCOMING] );
|
2010-01-14 00:25:03 +00:00
|
|
|
tr_bencDictAddInt( tmp, "fromLtep", f[TR_PEER_FROM_LTEP] );
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( tmp, "fromPex", f[TR_PEER_FROM_PEX] );
|
|
|
|
tr_bencDictAddInt( tmp, "fromTracker", f[TR_PEER_FROM_TRACKER] );
|
2008-06-16 03:47:50 +00:00
|
|
|
}
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "peersGettingFromUs" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->peersGettingFromUs );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "peersKnown" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->peersKnown );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "peersSendingToUs" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->peersSendingToUs );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "pieces" ) ) {
|
2009-03-02 05:48:32 +00:00
|
|
|
const tr_bitfield * pieces = tr_cpPieceBitfield( &tor->completion );
|
|
|
|
char * str = tr_base64_encode( pieces->bits, pieces->byteCount, NULL );
|
2009-12-09 12:44:23 +00:00
|
|
|
tr_bencDictAddStr( d, key, str!=NULL ? str : "" );
|
2009-03-02 05:48:32 +00:00
|
|
|
tr_free( str );
|
|
|
|
}
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "pieceCount" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, inf->pieceCount );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "pieceSize" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, inf->pieceSize );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "priorities" ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-06-17 16:25:13 +00:00
|
|
|
tr_file_index_t i;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * p = tr_bencDictAddList( d, key, inf->fileCount );
|
|
|
|
for( i = 0; i < inf->fileCount; ++i )
|
2008-06-17 16:25:13 +00:00
|
|
|
tr_bencListAddInt( p, inf->files[i].priority );
|
|
|
|
}
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "rateDownload" ) )
|
2010-09-03 04:30:43 +00:00
|
|
|
tr_bencDictAddInt( d, key, toSpeedBytes( st->pieceDownloadSpeed_KBps ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "rateUpload" ) )
|
2010-09-03 04:30:43 +00:00
|
|
|
tr_bencDictAddInt( d, key, toSpeedBytes( st->pieceUploadSpeed_KBps ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "recheckProgress" ) )
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddReal( d, key, st->recheckProgress );
|
2010-07-24 02:57:39 +00:00
|
|
|
else if( tr_streq( key, keylen, "seedIdleLimit" ) )
|
|
|
|
tr_bencDictAddInt( d, key, tr_torrentGetIdleLimit( tor ) );
|
|
|
|
else if( tr_streq( key, keylen, "seedIdleMode" ) )
|
|
|
|
tr_bencDictAddInt( d, key, tr_torrentGetIdleMode( tor ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "seedRatioLimit" ) )
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddReal( d, key, tr_torrentGetRatioLimit( tor ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "seedRatioMode" ) )
|
2009-03-29 18:45:35 +00:00
|
|
|
tr_bencDictAddInt( d, key, tr_torrentGetRatioMode( tor ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "sizeWhenDone" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->sizeWhenDone );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "startDate" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->startDate );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "status" ) )
|
2008-10-20 17:54:56 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->activity );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "trackers" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
addTrackers( inf, tr_bencDictAddList( d, key, inf->trackerCount ) );
|
2009-09-25 21:05:59 +00:00
|
|
|
else if( tr_streq( key, keylen, "trackerStats" ) ) {
|
|
|
|
int n;
|
|
|
|
tr_tracker_stat * s = tr_torrentTrackers( tor, &n );
|
|
|
|
addTrackerStats( s, n, tr_bencDictAddList( d, key, n ) );
|
|
|
|
tr_torrentTrackersFree( s, n );
|
|
|
|
}
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "torrentFile" ) )
|
2009-04-02 22:59:30 +00:00
|
|
|
tr_bencDictAddStr( d, key, inf->torrent );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "totalSize" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, inf->totalSize );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "uploadedEver" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->uploadedEver );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "uploadLimit" ) )
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_bencDictAddInt( d, key, tr_torrentGetSpeedLimit_KBps( tor, TR_UP ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "uploadLimited" ) )
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, key, tr_torrentUsesSpeedLimit( tor, TR_UP ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "uploadRatio" ) )
|
2009-04-19 17:02:27 +00:00
|
|
|
tr_bencDictAddReal( d, key, st->ratio );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "wanted" ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_file_index_t i;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * w = tr_bencDictAddList( d, key, inf->fileCount );
|
|
|
|
for( i = 0; i < inf->fileCount; ++i )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencListAddInt( w, inf->files[i].dnd ? 0 : 1 );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "webseeds" ) )
|
2009-06-12 12:10:09 +00:00
|
|
|
addWebseeds( inf, tr_bencDictAddList( d, key, inf->webseedCount ) );
|
2009-05-15 20:00:12 +00:00
|
|
|
else if( tr_streq( key, keylen, "webseedsSendingToUs" ) )
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->webseedsSendingToUs );
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2008-07-26 14:47:07 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
addInfo( const tr_torrent * tor,
|
|
|
|
tr_benc * d,
|
|
|
|
tr_benc * fields )
|
2008-07-26 14:47:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
const int n = tr_bencListSize( fields );
|
2008-07-26 14:47:07 +00:00
|
|
|
const char * str;
|
2008-06-16 03:47:50 +00:00
|
|
|
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencInitDict( d, n );
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
2008-07-26 14:47:07 +00:00
|
|
|
if( tr_bencGetStr( tr_bencListChild( fields, i ), &str ) )
|
|
|
|
addField( tor, d, str );
|
2008-06-16 03:47:50 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
static const char*
|
2009-01-18 15:24:26 +00:00
|
|
|
torrentGet( tr_session * session,
|
|
|
|
tr_benc * args_in,
|
|
|
|
tr_benc * args_out,
|
2009-12-28 23:27:17 +00:00
|
|
|
struct tr_rpc_idle_data * idle_data UNUSED )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i, torrentCount;
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_benc * list = tr_bencDictAddList( args_out, "torrents", torrentCount );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * fields;
|
2009-01-23 18:44:15 +00:00
|
|
|
const char * msg = NULL;
|
2009-04-04 05:29:08 +00:00
|
|
|
const char * strVal;
|
2008-06-16 03:47:50 +00:00
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
assert( idle_data == NULL );
|
|
|
|
|
2009-04-04 05:29:08 +00:00
|
|
|
if( tr_bencDictFindStr( args_in, "ids", &strVal ) && !strcmp( strVal, "recently-active" ) ) {
|
|
|
|
int n = 0;
|
|
|
|
tr_benc * d;
|
2009-11-26 18:47:08 +00:00
|
|
|
const time_t now = tr_time( );
|
2009-04-04 05:29:08 +00:00
|
|
|
const int interval = RECENTLY_ACTIVE_SECONDS;
|
|
|
|
tr_benc * removed_out = tr_bencDictAddList( args_out, "removed", 0 );
|
|
|
|
while(( d = tr_bencListChild( &session->removedTorrents, n++ ))) {
|
|
|
|
int64_t intVal;
|
|
|
|
if( tr_bencDictFindInt( d, "date", &intVal ) && ( intVal >= now - interval ) ) {
|
|
|
|
tr_bencDictFindInt( d, "id", &intVal );
|
|
|
|
tr_bencListAddInt( removed_out, intVal );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-14 11:45:12 +00:00
|
|
|
if( !tr_bencDictFindList( args_in, "fields", &fields ) )
|
2008-08-14 10:39:27 +00:00
|
|
|
msg = "no fields specified";
|
2008-09-23 19:11:04 +00:00
|
|
|
else for( i = 0; i < torrentCount; ++i )
|
2009-09-25 21:05:59 +00:00
|
|
|
addInfo( torrents[i], tr_bencListAdd( list ), fields );
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
tr_free( torrents );
|
2008-08-14 10:39:27 +00:00
|
|
|
return msg;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2008-06-16 03:47:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-03-03 20:06:45 +00:00
|
|
|
static const char*
|
2008-09-23 19:11:04 +00:00
|
|
|
setFilePriorities( tr_torrent * tor,
|
|
|
|
int priority,
|
|
|
|
tr_benc * list )
|
2008-05-12 13:05:06 +00:00
|
|
|
{
|
2009-03-03 20:06:45 +00:00
|
|
|
int i;
|
|
|
|
int64_t tmp;
|
|
|
|
int fileCount = 0;
|
|
|
|
const int n = tr_bencListSize( list );
|
|
|
|
const char * errmsg = NULL;
|
2008-07-10 20:59:15 +00:00
|
|
|
tr_file_index_t * files = tr_new0( tr_file_index_t, tor->info.fileCount );
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2008-07-10 20:59:15 +00:00
|
|
|
if( n )
|
|
|
|
{
|
2009-03-03 20:06:45 +00:00
|
|
|
for( i = 0; i < n; ++i ) {
|
|
|
|
if( tr_bencGetInt( tr_bencListChild( list, i ), &tmp ) ) {
|
|
|
|
if( 0 <= tmp && tmp < tor->info.fileCount ) {
|
2008-07-10 20:59:15 +00:00
|
|
|
files[fileCount++] = tmp;
|
2009-03-03 20:06:45 +00:00
|
|
|
} else {
|
|
|
|
errmsg = "file index out of range";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else /* if empty set, apply to all */
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
|
|
|
tr_file_index_t t;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( t = 0; t < tor->info.fileCount; ++t )
|
2008-07-10 20:59:15 +00:00
|
|
|
files[fileCount++] = t;
|
|
|
|
}
|
2008-05-12 13:05:06 +00:00
|
|
|
|
|
|
|
if( fileCount )
|
|
|
|
tr_torrentSetFilePriorities( tor, files, fileCount, priority );
|
|
|
|
|
|
|
|
tr_free( files );
|
2009-03-03 20:06:45 +00:00
|
|
|
return errmsg;
|
2008-05-12 13:05:06 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 20:06:45 +00:00
|
|
|
static const char*
|
2008-09-23 19:11:04 +00:00
|
|
|
setFileDLs( tr_torrent * tor,
|
|
|
|
int do_download,
|
|
|
|
tr_benc * list )
|
2008-05-12 13:05:06 +00:00
|
|
|
{
|
2009-03-03 20:06:45 +00:00
|
|
|
int i;
|
|
|
|
int64_t tmp;
|
|
|
|
int fileCount = 0;
|
|
|
|
const int n = tr_bencListSize( list );
|
|
|
|
const char * errmsg = NULL;
|
2008-07-10 20:59:15 +00:00
|
|
|
tr_file_index_t * files = tr_new0( tr_file_index_t, tor->info.fileCount );
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( n ) /* if argument list, process them */
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
2009-03-03 20:06:45 +00:00
|
|
|
for( i = 0; i < n; ++i ) {
|
|
|
|
if( tr_bencGetInt( tr_bencListChild( list, i ), &tmp ) ) {
|
|
|
|
if( 0 <= tmp && tmp < tor->info.fileCount ) {
|
2008-07-10 20:59:15 +00:00
|
|
|
files[fileCount++] = tmp;
|
2009-03-03 20:06:45 +00:00
|
|
|
} else {
|
|
|
|
errmsg = "file index out of range";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else /* if empty set, apply to all */
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
|
|
|
tr_file_index_t t;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( t = 0; t < tor->info.fileCount; ++t )
|
2008-07-10 20:59:15 +00:00
|
|
|
files[fileCount++] = t;
|
|
|
|
}
|
2008-05-12 13:05:06 +00:00
|
|
|
|
|
|
|
if( fileCount )
|
|
|
|
tr_torrentSetFileDLs( tor, files, fileCount, do_download );
|
|
|
|
|
|
|
|
tr_free( files );
|
2009-03-03 20:06:45 +00:00
|
|
|
return errmsg;
|
2008-05-12 13:05:06 +00:00
|
|
|
}
|
|
|
|
|
2010-06-30 05:52:24 +00:00
|
|
|
static tr_bool
|
2010-07-27 19:43:32 +00:00
|
|
|
findAnnounceUrl( const tr_tracker_info * t, int n, const char * url, int * pos )
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
tr_bool found = FALSE;
|
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
for( i=0; i<n; ++i )
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2010-07-27 19:43:32 +00:00
|
|
|
if( !strcmp( t[i].announce, url ) )
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
|
|
|
found = TRUE;
|
2010-07-27 19:43:32 +00:00
|
|
|
if( pos ) *pos = i;
|
2010-06-30 05:52:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
static int
|
|
|
|
copyTrackers( tr_tracker_info * tgt, const tr_tracker_info * src, int n )
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
2010-07-27 19:43:32 +00:00
|
|
|
int maxTier = -1;
|
|
|
|
|
|
|
|
for( i=0; i<n; ++i )
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2010-07-27 19:43:32 +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
|
|
|
}
|
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
return maxTier;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
freeTrackers( tr_tracker_info * trackers, int n )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
tr_free( trackers[i].announce );
|
|
|
|
|
|
|
|
tr_free( trackers );
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
2010-07-27 19:43:32 +00:00
|
|
|
addTrackerUrls( tr_torrent * tor, tr_benc * urls )
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
2010-07-27 19:43:32 +00:00
|
|
|
int n;
|
|
|
|
int tier;
|
|
|
|
tr_benc * val;
|
|
|
|
tr_tracker_info * trackers;
|
|
|
|
tr_bool changed = FALSE;
|
2010-06-30 05:52:24 +00:00
|
|
|
const tr_info * inf = tr_torrentInfo( tor );
|
2010-07-27 19:43:32 +00:00
|
|
|
const char * errmsg = NULL;
|
2010-06-30 05:52:24 +00:00
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
/* make a working copy of the existing announce list */
|
|
|
|
n = inf->trackerCount;
|
|
|
|
trackers = tr_new0( tr_tracker_info, n + tr_bencListSize( urls ) );
|
|
|
|
tier = copyTrackers( trackers, inf->trackers, n );
|
2010-06-30 05:52:24 +00:00
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
/* and add the new ones */
|
|
|
|
i = 0;
|
|
|
|
while(( val = tr_bencListChild( urls, i++ ) ))
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2010-07-27 19:43:32 +00:00
|
|
|
const char * announce = NULL;
|
2010-06-30 05:52:24 +00:00
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
if( tr_bencGetStr( val, &announce )
|
2010-08-21 12:50:13 +00:00
|
|
|
&& tr_urlIsValid( announce, -1 )
|
2010-07-27 19:43:32 +00:00
|
|
|
&& !findAnnounceUrl( trackers, n, announce, NULL ) )
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2010-07-27 19:43:32 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
if( !changed )
|
|
|
|
errmsg = "invalid argument";
|
|
|
|
else if( !tr_torrentSetAnnounceList( tor, trackers, n ) )
|
|
|
|
errmsg = "error setting announce list";
|
|
|
|
|
|
|
|
freeTrackers( trackers, n );
|
2010-06-30 05:52:24 +00:00
|
|
|
return errmsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
2010-07-27 19:43:32 +00:00
|
|
|
replaceTrackerUrls( tr_torrent * tor, tr_benc * urls )
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2010-07-27 19:43:32 +00:00
|
|
|
int i;
|
|
|
|
tr_benc * pair[2];
|
|
|
|
tr_tracker_info * trackers;
|
|
|
|
tr_bool changed = FALSE;
|
2010-06-30 05:52:24 +00:00
|
|
|
const tr_info * inf = tr_torrentInfo( tor );
|
2010-07-27 19:43:32 +00:00
|
|
|
const int n = inf->trackerCount;
|
|
|
|
const char * errmsg = NULL;
|
2010-06-30 05:52:24 +00:00
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
/* make a working copy of the existing announce list */
|
|
|
|
trackers = tr_new0( tr_tracker_info, n );
|
|
|
|
copyTrackers( trackers, inf->trackers, n );
|
2010-06-30 05:52:24 +00:00
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
/* make the substitutions... */
|
|
|
|
i = 0;
|
|
|
|
while(((pair[0] = tr_bencListChild(urls,i))) &&
|
|
|
|
((pair[1] = tr_bencListChild(urls,i+1))))
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2010-07-27 19:43:32 +00:00
|
|
|
const char * oldval;
|
|
|
|
const char * newval;
|
|
|
|
|
|
|
|
if( tr_bencGetStr( pair[0], &oldval )
|
|
|
|
&& tr_bencGetStr( pair[1], &newval )
|
|
|
|
&& strcmp( oldval, newval )
|
2010-08-21 12:50:13 +00:00
|
|
|
&& tr_urlIsValid( newval, -1 )
|
2010-07-27 19:43:32 +00:00
|
|
|
&& findAnnounceUrl( trackers, n, oldval, &i ) )
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2010-07-27 19:43:32 +00:00
|
|
|
tr_free( trackers[i].announce );
|
|
|
|
trackers[i].announce = tr_strdup( newval );
|
|
|
|
changed = TRUE;
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
i += 2;
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
if( !changed )
|
|
|
|
errmsg = "invalid argument";
|
|
|
|
else if( !tr_torrentSetAnnounceList( tor, trackers, n ) )
|
|
|
|
errmsg = "error setting announce list";
|
|
|
|
|
|
|
|
freeTrackers( trackers, n );
|
2010-06-30 05:52:24 +00:00
|
|
|
return errmsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
2010-07-27 19:43:32 +00:00
|
|
|
removeTrackerUrls( tr_torrent * tor, tr_benc * urls )
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2010-07-27 19:43:32 +00:00
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
tr_benc * val;
|
|
|
|
tr_tracker_info * trackers;
|
|
|
|
tr_bool changed = FALSE;
|
2010-06-30 05:52:24 +00:00
|
|
|
const tr_info * inf = tr_torrentInfo( tor );
|
2010-07-27 19:43:32 +00:00
|
|
|
const char * errmsg = NULL;
|
2010-06-30 05:52:24 +00:00
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
/* make a working copy of the existing announce list */
|
|
|
|
n = inf->trackerCount;
|
|
|
|
trackers = tr_new0( tr_tracker_info, n );
|
|
|
|
copyTrackers( trackers, inf->trackers, n );
|
2010-06-30 05:52:24 +00:00
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
/* remove the ones specified in the urls list */
|
|
|
|
i = 0;
|
|
|
|
while(( val = tr_bencListChild( urls, i++ )))
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2010-07-27 19:43:32 +00:00
|
|
|
int pos;
|
|
|
|
const char * url;
|
|
|
|
if( tr_bencGetStr( val, &url ) && findAnnounceUrl( trackers, n, url, &pos ) )
|
2010-06-30 05:52:24 +00:00
|
|
|
{
|
2010-07-27 19:43:32 +00:00
|
|
|
tr_removeElementFromArray( trackers, pos, sizeof( tr_tracker_info ), n-- );
|
|
|
|
changed = TRUE;
|
2010-06-30 05:52:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
if( !changed )
|
|
|
|
errmsg = "invalid argument";
|
|
|
|
else if( !tr_torrentSetAnnounceList( tor, trackers, n ) )
|
|
|
|
errmsg = "error setting announce list";
|
|
|
|
|
|
|
|
freeTrackers( trackers, n );
|
2010-06-30 05:52:24 +00:00
|
|
|
return errmsg;
|
|
|
|
}
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
static const char*
|
2009-01-18 15:24:26 +00:00
|
|
|
torrentSet( tr_session * session,
|
|
|
|
tr_benc * args_in,
|
|
|
|
tr_benc * args_out UNUSED,
|
2009-12-28 23:27:17 +00:00
|
|
|
struct tr_rpc_idle_data * idle_data UNUSED )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2009-03-03 20:06:45 +00:00
|
|
|
const char * errmsg = NULL;
|
|
|
|
int i, torrentCount;
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
assert( idle_data == NULL );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < torrentCount; ++i )
|
2008-05-12 13:05:06 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int64_t tmp;
|
2009-02-13 18:23:56 +00:00
|
|
|
double d;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * files;
|
2010-07-27 19:43:32 +00:00
|
|
|
tr_benc * urls;
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bool boolVal;
|
2008-05-12 13:05:06 +00:00
|
|
|
tr_torrent * tor = torrents[i];
|
|
|
|
|
2009-04-18 23:17:30 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "bandwidthPriority", &tmp ) )
|
|
|
|
if( tr_isPriority( tmp ) )
|
|
|
|
tr_torrentSetPriority( tor, tmp );
|
2009-12-09 01:36:31 +00:00
|
|
|
if( !errmsg && tr_bencDictFindList( args_in, "files-unwanted", &files ) )
|
|
|
|
errmsg = setFileDLs( tor, FALSE, files );
|
|
|
|
if( !errmsg && tr_bencDictFindList( args_in, "files-wanted", &files ) )
|
|
|
|
errmsg = setFileDLs( tor, TRUE, files );
|
2008-06-17 16:25:13 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "peer-limit", &tmp ) )
|
|
|
|
tr_torrentSetPeerLimit( tor, tmp );
|
2009-03-03 20:06:45 +00:00
|
|
|
if( !errmsg && tr_bencDictFindList( args_in, "priority-high", &files ) )
|
|
|
|
errmsg = setFilePriorities( tor, TR_PRI_HIGH, files );
|
|
|
|
if( !errmsg && tr_bencDictFindList( args_in, "priority-low", &files ) )
|
|
|
|
errmsg = setFilePriorities( tor, TR_PRI_LOW, files );
|
|
|
|
if( !errmsg && tr_bencDictFindList( args_in, "priority-normal", &files ) )
|
|
|
|
errmsg = setFilePriorities( tor, TR_PRI_NORMAL, files );
|
2009-03-27 01:40:51 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "downloadLimit", &tmp ) )
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_torrentSetSpeedLimit_KBps( tor, TR_DOWN, tmp );
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, "downloadLimited", &boolVal ) )
|
|
|
|
tr_torrentUseSpeedLimit( tor, TR_DOWN, boolVal );
|
|
|
|
if( tr_bencDictFindBool( args_in, "honorsSessionLimits", &boolVal ) )
|
|
|
|
tr_torrentUseSessionLimits( tor, boolVal );
|
2009-03-27 01:40:51 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "uploadLimit", &tmp ) )
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_torrentSetSpeedLimit_KBps( tor, TR_UP, tmp );
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, "uploadLimited", &boolVal ) )
|
|
|
|
tr_torrentUseSpeedLimit( tor, TR_UP, boolVal );
|
2010-07-24 02:57:39 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "seedIdleLimit", &tmp ) )
|
|
|
|
tr_torrentSetIdleLimit( tor, tmp );
|
|
|
|
if( tr_bencDictFindInt( args_in, "seedIdleMode", &tmp ) )
|
|
|
|
tr_torrentSetIdleMode( tor, tmp );
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindReal( args_in, "seedRatioLimit", &d ) )
|
2009-03-29 18:45:35 +00:00
|
|
|
tr_torrentSetRatioLimit( tor, d );
|
|
|
|
if( tr_bencDictFindInt( args_in, "seedRatioMode", &tmp ) )
|
|
|
|
tr_torrentSetRatioMode( tor, tmp );
|
2010-07-27 19:43:32 +00:00
|
|
|
if( !errmsg && tr_bencDictFindList( args_in, "trackerAdd", &urls ) )
|
|
|
|
errmsg = addTrackerUrls( tor, urls );
|
|
|
|
if( !errmsg && tr_bencDictFindList( args_in, "trackerRemove", &urls ) )
|
|
|
|
errmsg = removeTrackerUrls( tor, urls );
|
|
|
|
if( !errmsg && tr_bencDictFindList( args_in, "trackerReplace", &urls ) )
|
|
|
|
errmsg = replaceTrackerUrls( tor, urls );
|
2008-12-14 11:21:11 +00:00
|
|
|
notify( session, TR_RPC_TORRENT_CHANGED, tor );
|
2008-05-12 13:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( torrents );
|
2009-03-03 20:06:45 +00:00
|
|
|
return errmsg;
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2009-05-13 15:54:04 +00:00
|
|
|
static const char*
|
|
|
|
torrentSetLocation( tr_session * session,
|
|
|
|
tr_benc * args_in,
|
|
|
|
tr_benc * args_out UNUSED,
|
2009-12-28 23:27:17 +00:00
|
|
|
struct tr_rpc_idle_data * idle_data UNUSED )
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
|
|
|
const char * errmsg = NULL;
|
|
|
|
const char * location = NULL;
|
|
|
|
|
|
|
|
assert( idle_data == NULL );
|
|
|
|
|
|
|
|
if( !tr_bencDictFindStr( args_in, "location", &location ) )
|
|
|
|
{
|
|
|
|
errmsg = "no location";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_bool move = FALSE;
|
|
|
|
int i, torrentCount;
|
|
|
|
tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
|
|
|
|
|
|
|
|
tr_bencDictFindBool( args_in, "move", &move );
|
|
|
|
|
|
|
|
for( i=0; i<torrentCount; ++i )
|
|
|
|
{
|
|
|
|
tr_torrent * tor = torrents[i];
|
2009-05-20 16:02:12 +00:00
|
|
|
tr_torrentSetLocation( tor, location, move, NULL, NULL );
|
2009-10-21 14:02:02 +00:00
|
|
|
notify( session, TR_RPC_TORRENT_MOVED, tor );
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( torrents );
|
|
|
|
}
|
|
|
|
|
|
|
|
return errmsg;
|
|
|
|
}
|
|
|
|
|
2008-05-12 13:05:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-04-06 16:36:00 +00:00
|
|
|
static void
|
|
|
|
portTested( tr_session * session UNUSED,
|
|
|
|
long response_code,
|
|
|
|
const void * response,
|
|
|
|
size_t response_byte_count,
|
|
|
|
void * user_data )
|
|
|
|
{
|
|
|
|
char result[1024];
|
|
|
|
struct tr_rpc_idle_data * data = user_data;
|
|
|
|
|
|
|
|
if( response_code != 200 )
|
|
|
|
{
|
2010-06-21 16:44:35 +00:00
|
|
|
tr_snprintf( result, sizeof( result ), "portTested: http error %ld: %s",
|
2009-04-06 16:36:00 +00:00
|
|
|
response_code, tr_webGetResponseStr( response_code ) );
|
|
|
|
}
|
|
|
|
else /* success */
|
|
|
|
{
|
|
|
|
const tr_bool isOpen = response_byte_count && *(char*)response == '1';
|
|
|
|
tr_bencDictAddBool( data->args_out, "port-is-open", isOpen );
|
|
|
|
tr_snprintf( result, sizeof( result ), "success" );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_idle_function_done( data, result );
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
portTest( tr_session * session,
|
|
|
|
tr_benc * args_in UNUSED,
|
|
|
|
tr_benc * args_out UNUSED,
|
|
|
|
struct tr_rpc_idle_data * idle_data )
|
|
|
|
{
|
|
|
|
const int port = tr_sessionGetPeerPort( session );
|
|
|
|
char * url = tr_strdup_printf( "http://portcheck.transmissionbt.com/%d", port );
|
2009-04-06 20:33:50 +00:00
|
|
|
tr_webRun( session, url, NULL, portTested, idle_data );
|
2009-04-06 16:36:00 +00:00
|
|
|
tr_free( url );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-04-03 23:21:40 +00:00
|
|
|
static void
|
|
|
|
gotNewBlocklist( tr_session * session,
|
|
|
|
long response_code,
|
|
|
|
const void * response,
|
|
|
|
size_t response_byte_count,
|
|
|
|
void * user_data )
|
|
|
|
{
|
|
|
|
char result[1024];
|
|
|
|
struct tr_rpc_idle_data * data = user_data;
|
|
|
|
|
|
|
|
if( response_code != 200 )
|
|
|
|
{
|
2010-06-21 16:44:35 +00:00
|
|
|
tr_snprintf( result, sizeof( result ), "gotNewBlocklist: http error %ld: %s",
|
2009-04-03 23:21:40 +00:00
|
|
|
response_code, tr_webGetResponseStr( response_code ) );
|
|
|
|
}
|
2010-04-19 16:18:12 +00:00
|
|
|
else /* successfully fetched the blocklist... */
|
2009-04-03 23:21:40 +00:00
|
|
|
{
|
2010-04-19 16:18:12 +00:00
|
|
|
const char * configDir = tr_sessionGetConfigDir( session );
|
|
|
|
char * filename = tr_buildPath( configDir, "blocklist.tmp", NULL );
|
2010-06-21 16:44:35 +00:00
|
|
|
FILE * fp = fopen( filename, "wb+" );
|
2009-04-03 23:21:40 +00:00
|
|
|
|
2010-04-19 16:18:12 +00:00
|
|
|
if( fp == NULL )
|
|
|
|
{
|
|
|
|
tr_snprintf( result, sizeof( result ),
|
|
|
|
_( "Couldn't save file \"%1$s\": %2$s" ),
|
|
|
|
filename, tr_strerror( errno ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const size_t n = fwrite( response, 1, response_byte_count, fp );
|
|
|
|
fclose( fp );
|
|
|
|
|
|
|
|
if( n != response_byte_count )
|
|
|
|
{
|
|
|
|
tr_snprintf( result, sizeof( result ),
|
|
|
|
_( "Couldn't save file \"%1$s\": %2$s" ),
|
|
|
|
filename, tr_strerror( errno ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* feed it to the session */
|
|
|
|
const int ruleCount = tr_blocklistSetContent( session, filename );
|
|
|
|
|
|
|
|
/* give the client a response */
|
|
|
|
tr_bencDictAddInt( data->args_out, "blocklist-size", ruleCount );
|
|
|
|
tr_snprintf( result, sizeof( result ), "success" );
|
|
|
|
}
|
2009-04-03 23:21:40 +00:00
|
|
|
|
2010-04-19 16:18:12 +00:00
|
|
|
/* cleanup */
|
|
|
|
unlink( filename );
|
|
|
|
}
|
2009-04-03 23:21:40 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free( filename );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_idle_function_done( data, result );
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
blocklistUpdate( tr_session * session,
|
|
|
|
tr_benc * args_in UNUSED,
|
|
|
|
tr_benc * args_out UNUSED,
|
|
|
|
struct tr_rpc_idle_data * idle_data )
|
|
|
|
{
|
2009-04-07 20:38:15 +00:00
|
|
|
const char * url = "http://update.transmissionbt.com/level1";
|
2009-04-03 23:21:40 +00:00
|
|
|
tr_webRun( session, url, NULL, gotNewBlocklist, idle_data );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
static void
|
|
|
|
addTorrentImpl( struct tr_rpc_idle_data * data, tr_ctor * ctor )
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
const char * result = NULL;
|
2009-04-02 17:30:29 +00:00
|
|
|
tr_torrent * tor = tr_torrentNew( ctor, &err );
|
2009-01-18 15:24:26 +00:00
|
|
|
|
|
|
|
tr_ctorFree( ctor );
|
|
|
|
|
|
|
|
if( tor )
|
|
|
|
{
|
|
|
|
tr_benc fields;
|
|
|
|
tr_bencInitList( &fields, 3 );
|
|
|
|
tr_bencListAddStr( &fields, "id" );
|
|
|
|
tr_bencListAddStr( &fields, "name" );
|
|
|
|
tr_bencListAddStr( &fields, "hashString" );
|
|
|
|
addInfo( tor, tr_bencDictAdd( data->args_out, "torrent-added" ), &fields );
|
|
|
|
notify( data->session, TR_RPC_TORRENT_ADDED, tor );
|
|
|
|
tr_bencFree( &fields );
|
|
|
|
}
|
2009-08-05 01:59:16 +00:00
|
|
|
else if( err == TR_PARSE_DUPLICATE )
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
|
|
|
result = "duplicate torrent";
|
|
|
|
}
|
2009-08-05 01:59:16 +00:00
|
|
|
else if( err == TR_PARSE_ERR )
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
|
|
|
result = "invalid or corrupt torrent file";
|
|
|
|
}
|
|
|
|
|
2009-01-19 14:10:39 +00:00
|
|
|
tr_idle_function_done( data, result );
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct add_torrent_idle_data
|
|
|
|
{
|
|
|
|
struct tr_rpc_idle_data * data;
|
|
|
|
tr_ctor * ctor;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
gotMetadataFromURL( tr_session * session UNUSED,
|
|
|
|
long response_code,
|
|
|
|
const void * response,
|
|
|
|
size_t response_byte_count,
|
|
|
|
void * user_data )
|
|
|
|
{
|
|
|
|
struct add_torrent_idle_data * data = user_data;
|
|
|
|
|
|
|
|
dbgmsg( "torrentAdd: HTTP response code was %ld (%s); response length was %zu bytes",
|
|
|
|
response_code, tr_webGetResponseStr( response_code ), response_byte_count );
|
|
|
|
|
2010-02-20 18:45:33 +00:00
|
|
|
if( response_code==200 || response_code==221 ) /* http or ftp success.. */
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
|
|
|
tr_ctorSetMetainfo( data->ctor, response, response_byte_count );
|
|
|
|
addTorrentImpl( data->data, data->ctor );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char result[1024];
|
2010-06-21 16:44:35 +00:00
|
|
|
tr_snprintf( result, sizeof( result ), "gotMetadataFromURL: http error %ld: %s",
|
2009-01-18 15:24:26 +00:00
|
|
|
response_code, tr_webGetResponseStr( response_code ) );
|
2009-01-19 14:10:39 +00:00
|
|
|
tr_idle_function_done( data->data, result );
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_bool
|
|
|
|
isCurlURL( const char * filename )
|
|
|
|
{
|
|
|
|
if( filename == NULL )
|
|
|
|
return FALSE;
|
|
|
|
|
2010-01-20 18:41:22 +00:00
|
|
|
return !strncmp( filename, "ftp://", 6 ) ||
|
|
|
|
!strncmp( filename, "http://", 7 ) ||
|
|
|
|
!strncmp( filename, "https://", 8 );
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
2009-04-02 20:43:42 +00:00
|
|
|
static tr_file_index_t*
|
|
|
|
fileListFromList( tr_benc * list, tr_file_index_t * setmeCount )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
const size_t childCount = tr_bencListSize( list );
|
|
|
|
tr_file_index_t n = 0;
|
|
|
|
tr_file_index_t * files = tr_new0( tr_file_index_t, childCount );
|
|
|
|
|
|
|
|
for( i=0; i<childCount; ++i ) {
|
|
|
|
int64_t intVal;
|
|
|
|
if( tr_bencGetInt( tr_bencListChild( list, i ), &intVal ) )
|
|
|
|
files[n++] = (tr_file_index_t)intVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
*setmeCount = n;
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
static const char*
|
2009-01-18 15:24:26 +00:00
|
|
|
torrentAdd( tr_session * session,
|
|
|
|
tr_benc * args_in,
|
|
|
|
tr_benc * args_out UNUSED,
|
|
|
|
struct tr_rpc_idle_data * idle_data )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2008-05-20 17:33:54 +00:00
|
|
|
const char * filename = NULL;
|
|
|
|
const char * metainfo_base64 = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
assert( idle_data != NULL );
|
|
|
|
|
2008-05-20 17:33:54 +00:00
|
|
|
tr_bencDictFindStr( args_in, "filename", &filename );
|
|
|
|
tr_bencDictFindStr( args_in, "metainfo", &metainfo_base64 );
|
|
|
|
if( !filename && !metainfo_base64 )
|
|
|
|
return "no filename or metainfo specified";
|
2008-05-18 16:44:30 +00:00
|
|
|
else
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int64_t i;
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bool boolVal;
|
2008-05-12 13:05:06 +00:00
|
|
|
const char * str;
|
2009-04-02 20:43:42 +00:00
|
|
|
tr_benc * l;
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_ctor * ctor = tr_ctorNew( session );
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
|
|
/* set the optional arguments */
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2009-06-25 01:57:31 +00:00
|
|
|
if( tr_bencDictFindStr( args_in, TR_PREFS_KEY_DOWNLOAD_DIR, &str ) )
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_ctorSetDownloadDir( ctor, TR_FORCE, str );
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, "paused", &boolVal ) )
|
|
|
|
tr_ctorSetPaused( ctor, TR_FORCE, boolVal );
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2008-05-12 16:33:17 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "peer-limit", &i ) )
|
|
|
|
tr_ctorSetPeerLimit( ctor, TR_FORCE, i );
|
2008-05-20 17:33:54 +00:00
|
|
|
|
2010-02-02 07:48:03 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "bandwidthPriority", &i ) )
|
|
|
|
tr_ctorSetBandwidthPriority( ctor, i );
|
|
|
|
|
2009-04-02 20:43:42 +00:00
|
|
|
if( tr_bencDictFindList( args_in, "files-unwanted", &l ) ) {
|
|
|
|
tr_file_index_t fileCount;
|
|
|
|
tr_file_index_t * files = fileListFromList( l, &fileCount );
|
|
|
|
tr_ctorSetFilesWanted( ctor, files, fileCount, FALSE );
|
|
|
|
tr_free( files );
|
|
|
|
}
|
|
|
|
if( tr_bencDictFindList( args_in, "files-wanted", &l ) ) {
|
|
|
|
tr_file_index_t fileCount;
|
|
|
|
tr_file_index_t * files = fileListFromList( l, &fileCount );
|
|
|
|
tr_ctorSetFilesWanted( ctor, files, fileCount, TRUE );
|
|
|
|
tr_free( files );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindList( args_in, "priority-low", &l ) ) {
|
|
|
|
tr_file_index_t fileCount;
|
|
|
|
tr_file_index_t * files = fileListFromList( l, &fileCount );
|
|
|
|
tr_ctorSetFilePriorities( ctor, files, fileCount, TR_PRI_LOW );
|
|
|
|
tr_free( files );
|
|
|
|
}
|
|
|
|
if( tr_bencDictFindList( args_in, "priority-normal", &l ) ) {
|
|
|
|
tr_file_index_t fileCount;
|
|
|
|
tr_file_index_t * files = fileListFromList( l, &fileCount );
|
|
|
|
tr_ctorSetFilePriorities( ctor, files, fileCount, TR_PRI_NORMAL );
|
|
|
|
tr_free( files );
|
|
|
|
}
|
|
|
|
if( tr_bencDictFindList( args_in, "priority-high", &l ) ) {
|
|
|
|
tr_file_index_t fileCount;
|
|
|
|
tr_file_index_t * files = fileListFromList( l, &fileCount );
|
|
|
|
tr_ctorSetFilePriorities( ctor, files, fileCount, TR_PRI_HIGH );
|
|
|
|
tr_free( files );
|
|
|
|
}
|
|
|
|
|
2010-02-03 14:43:39 +00:00
|
|
|
dbgmsg( "torrentAdd: filename is \"%s\"", filename ? filename : "(null)" );
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
if( isCurlURL( filename ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-01-18 15:24:26 +00:00
|
|
|
struct add_torrent_idle_data * d = tr_new0( struct add_torrent_idle_data, 1 );
|
|
|
|
d->data = idle_data;
|
|
|
|
d->ctor = ctor;
|
|
|
|
tr_webRun( session, filename, NULL, gotMetadataFromURL, d );
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2009-01-18 15:24:26 +00:00
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2010-01-20 18:15:09 +00:00
|
|
|
char * fname = tr_strstrip( tr_strdup( filename ) );
|
|
|
|
|
|
|
|
if( fname == NULL )
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2009-01-18 15:24:26 +00:00
|
|
|
int len;
|
2009-11-24 02:16:31 +00:00
|
|
|
char * metainfo = tr_base64_decode( metainfo_base64, -1, &len );
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_ctorSetMetainfo( ctor, (uint8_t*)metainfo, len );
|
|
|
|
tr_free( metainfo );
|
|
|
|
}
|
2010-01-20 18:15:09 +00:00
|
|
|
else if( !strncmp( fname, "magnet:?", 8 ) )
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_ctorSetMetainfoFromMagnetLink( ctor, fname );
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-20 18:15:09 +00:00
|
|
|
tr_ctorSetMetainfoFromFile( ctor, fname );
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
addTorrentImpl( idle_data, ctor );
|
2010-01-20 18:15:09 +00:00
|
|
|
|
|
|
|
tr_free( fname );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2008-05-12 13:05:06 +00:00
|
|
|
}
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-12 13:05:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
static const char*
|
2009-01-18 15:24:26 +00:00
|
|
|
sessionSet( tr_session * session,
|
|
|
|
tr_benc * args_in,
|
|
|
|
tr_benc * args_out UNUSED,
|
2009-12-28 23:27:17 +00:00
|
|
|
struct tr_rpc_idle_data * idle_data UNUSED )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int64_t i;
|
2009-02-13 18:23:56 +00:00
|
|
|
double d;
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bool boolVal;
|
2008-05-12 16:33:17 +00:00
|
|
|
const char * str;
|
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
assert( idle_data == NULL );
|
|
|
|
|
2010-07-04 06:07:21 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_MAX_CACHE_SIZE_MB, &i ) )
|
|
|
|
tr_sessionSetCacheLimit_MB( session, i );
|
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_ALT_SPEED_UP_KBps, &i ) )
|
|
|
|
tr_sessionSetAltSpeed_KBps( session, TR_UP, i );
|
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_ALT_SPEED_DOWN_KBps, &i ) )
|
|
|
|
tr_sessionSetAltSpeed_KBps( session, TR_DOWN, i );
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_ALT_SPEED_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionUseAltSpeed( session, boolVal );
|
2009-03-28 16:47:01 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN, &i ) )
|
|
|
|
tr_sessionSetAltSpeedBegin( session, i );
|
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_ALT_SPEED_TIME_END, &i ) )
|
|
|
|
tr_sessionSetAltSpeedEnd( session, i );
|
2009-04-04 02:17:39 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_ALT_SPEED_TIME_DAY, &i ) )
|
|
|
|
tr_sessionSetAltSpeedDay( session, i );
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionUseAltSpeedTime( session, boolVal );
|
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_BLOCKLIST_ENABLED, &boolVal ) )
|
|
|
|
tr_blocklistSetEnabled( session, boolVal );
|
2009-03-25 19:18:00 +00:00
|
|
|
if( tr_bencDictFindStr( args_in, TR_PREFS_KEY_DOWNLOAD_DIR, &str ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetDownloadDir( session, str );
|
2009-10-19 05:25:50 +00:00
|
|
|
if( tr_bencDictFindStr( args_in, TR_PREFS_KEY_INCOMPLETE_DIR, &str ) )
|
|
|
|
tr_sessionSetIncompleteDir( session, str );
|
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionSetIncompleteDirEnabled( session, boolVal );
|
2009-03-25 19:18:00 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, &i ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetPeerLimit( session, i );
|
2009-03-25 19:18:00 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_PEER_LIMIT_TORRENT, &i ) )
|
|
|
|
tr_sessionSetPeerLimitPerTorrent( session, i );
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_PEX_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionSetPexEnabled( session, boolVal );
|
2009-05-21 00:15:54 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_DHT_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionSetDHTEnabled( session, boolVal );
|
2010-05-08 08:42:45 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_LPD_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionSetLPDEnabled( session, boolVal );
|
2009-04-03 04:29:27 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, &boolVal ) )
|
|
|
|
tr_sessionSetPeerPortRandomOnStart( session, boolVal );
|
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_PEER_PORT, &i ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetPeerPort( session, i );
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_PORT_FORWARDING, &boolVal ) )
|
|
|
|
tr_sessionSetPortForwardingEnabled( session, boolVal );
|
2010-02-01 04:43:10 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_RENAME_PARTIAL_FILES, &boolVal ) )
|
|
|
|
tr_sessionSetIncompleteFileNamingEnabled( session, boolVal );
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindReal( args_in, "seedRatioLimit", &d ) )
|
2009-03-29 18:45:35 +00:00
|
|
|
tr_sessionSetRatioLimit( session, d );
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, "seedRatioLimited", &boolVal ) )
|
|
|
|
tr_sessionSetRatioLimited( session, boolVal );
|
2010-07-24 02:57:39 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_IDLE_LIMIT, &i ) )
|
|
|
|
tr_sessionSetIdleLimit( session, i );
|
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_IDLE_LIMIT_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionSetIdleLimited( session, boolVal );
|
2010-03-17 19:23:03 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_START, &boolVal ) )
|
|
|
|
tr_sessionSetPaused( session, !boolVal );
|
2010-05-08 22:42:28 +00:00
|
|
|
if( tr_bencDictFindStr( args_in, TR_PREFS_KEY_SCRIPT_TORRENT_DONE_FILENAME, &str ) )
|
|
|
|
tr_sessionSetTorrentDoneScript( session, str );
|
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_SCRIPT_TORRENT_DONE_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionSetTorrentDoneScriptEnabled( session, boolVal );
|
2010-03-17 19:23:03 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_TRASH_ORIGINAL, &boolVal ) )
|
|
|
|
tr_sessionSetDeleteSource( session, boolVal );
|
2010-07-04 06:07:21 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_DSPEED_KBps, &i ) )
|
|
|
|
tr_sessionSetSpeedLimit_KBps( session, TR_DOWN, i );
|
2009-06-25 01:57:31 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_DSPEED_ENABLED, &boolVal ) )
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_sessionLimitSpeed( session, TR_DOWN, boolVal );
|
2010-07-04 06:07:21 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_USPEED_KBps, &i ) )
|
|
|
|
tr_sessionSetSpeedLimit_KBps( session, TR_UP, i );
|
2009-06-25 01:57:31 +00:00
|
|
|
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_USPEED_ENABLED, &boolVal ) )
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_sessionLimitSpeed( session, TR_UP, boolVal );
|
2009-06-25 01:57:31 +00:00
|
|
|
if( tr_bencDictFindStr( args_in, TR_PREFS_KEY_ENCRYPTION, &str ) ) {
|
2008-05-12 16:33:17 +00:00
|
|
|
if( !strcmp( str, "required" ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetEncryption( session, TR_ENCRYPTION_REQUIRED );
|
2008-05-12 16:33:17 +00:00
|
|
|
else if( !strcmp( str, "tolerated" ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetEncryption( session, TR_CLEAR_PREFERRED );
|
2008-05-12 16:33:17 +00:00
|
|
|
else
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetEncryption( session, TR_ENCRYPTION_PREFERRED );
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
notify( session, TR_RPC_SESSION_CHANGED, NULL );
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-18 22:01:15 +00:00
|
|
|
static const char*
|
2009-01-18 15:24:26 +00:00
|
|
|
sessionStats( tr_session * session,
|
|
|
|
tr_benc * args_in UNUSED,
|
|
|
|
tr_benc * args_out,
|
2009-12-28 23:27:17 +00:00
|
|
|
struct tr_rpc_idle_data * idle_data UNUSED )
|
2008-06-18 22:01:15 +00:00
|
|
|
{
|
2009-01-13 04:43:38 +00:00
|
|
|
int running = 0;
|
|
|
|
int total = 0;
|
2009-08-10 20:04:08 +00:00
|
|
|
tr_benc * d;
|
|
|
|
tr_session_stats currentStats = { 0.0f, 0, 0, 0, 0, 0 };
|
|
|
|
tr_session_stats cumulativeStats = { 0.0f, 0, 0, 0, 0, 0 };
|
2009-01-13 04:43:38 +00:00
|
|
|
tr_torrent * tor = NULL;
|
2008-12-14 11:21:11 +00:00
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
assert( idle_data == NULL );
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
while(( tor = tr_torrentNext( session, tor ))) {
|
2008-06-18 22:01:15 +00:00
|
|
|
++total;
|
|
|
|
if( tor->isRunning )
|
|
|
|
++running;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-08-10 20:04:08 +00:00
|
|
|
tr_sessionGetStats( session, ¤tStats );
|
|
|
|
tr_sessionGetCumulativeStats( session, &cumulativeStats );
|
2009-01-19 21:17:29 +00:00
|
|
|
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_bencDictAddInt ( args_out, "activeTorrentCount", running );
|
|
|
|
tr_bencDictAddReal( args_out, "downloadSpeed", tr_sessionGetPieceSpeed_KBps( session, TR_DOWN ) );
|
|
|
|
tr_bencDictAddInt ( args_out, "pausedTorrentCount", total - running );
|
|
|
|
tr_bencDictAddInt ( args_out, "torrentCount", total );
|
|
|
|
tr_bencDictAddReal( args_out, "uploadSpeed", tr_sessionGetPieceSpeed_KBps( session, TR_UP ) );
|
2009-01-19 21:17:29 +00:00
|
|
|
|
2009-08-10 20:04:08 +00:00
|
|
|
d = tr_bencDictAddDict( args_out, "cumulative-stats", 5 );
|
|
|
|
tr_bencDictAddInt( d, "downloadedBytes", cumulativeStats.downloadedBytes );
|
|
|
|
tr_bencDictAddInt( d, "filesAdded", cumulativeStats.filesAdded );
|
|
|
|
tr_bencDictAddInt( d, "secondsActive", cumulativeStats.secondsActive );
|
|
|
|
tr_bencDictAddInt( d, "sessionCount", cumulativeStats.sessionCount );
|
|
|
|
tr_bencDictAddInt( d, "uploadedBytes", cumulativeStats.uploadedBytes );
|
2009-01-19 21:17:29 +00:00
|
|
|
|
2009-08-10 20:04:08 +00:00
|
|
|
d = tr_bencDictAddDict( args_out, "current-stats", 5 );
|
|
|
|
tr_bencDictAddInt( d, "downloadedBytes", currentStats.downloadedBytes );
|
|
|
|
tr_bencDictAddInt( d, "filesAdded", currentStats.filesAdded );
|
|
|
|
tr_bencDictAddInt( d, "secondsActive", currentStats.secondsActive );
|
|
|
|
tr_bencDictAddInt( d, "sessionCount", currentStats.sessionCount );
|
|
|
|
tr_bencDictAddInt( d, "uploadedBytes", currentStats.uploadedBytes );
|
2009-01-19 21:17:29 +00:00
|
|
|
|
2008-06-18 22:01:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
static const char*
|
2009-03-25 19:18:00 +00:00
|
|
|
sessionGet( tr_session * s,
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_benc * args_in UNUSED,
|
|
|
|
tr_benc * args_out,
|
2009-12-28 23:27:17 +00:00
|
|
|
struct tr_rpc_idle_data * idle_data UNUSED )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2008-05-12 16:33:17 +00:00
|
|
|
const char * str;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * d = args_out;
|
2008-05-12 16:33:17 +00:00
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
assert( idle_data == NULL );
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_UP_KBps, tr_sessionGetAltSpeed_KBps(s,TR_UP) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_DOWN_KBps, tr_sessionGetAltSpeed_KBps(s,TR_DOWN) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_ALT_SPEED_ENABLED, tr_sessionUsesAltSpeed(s) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN, tr_sessionGetAltSpeedBegin(s) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_END,tr_sessionGetAltSpeedEnd(s) );
|
2009-04-04 02:17:39 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_DAY,tr_sessionGetAltSpeedDay(s) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED, tr_sessionUsesAltSpeedTime(s) );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, tr_blocklistIsEnabled( s ) );
|
2010-07-15 01:03:56 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_MAX_CACHE_SIZE_MB, tr_sessionGetCacheLimit_MB( s ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddInt ( d, "blocklist-size", tr_blocklistGetRuleCount( s ) );
|
2010-02-02 18:27:57 +00:00
|
|
|
tr_bencDictAddStr ( d, "config-dir", tr_sessionGetConfigDir( s ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_DOWNLOAD_DIR, tr_sessionGetDownloadDir( s ) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, tr_sessionGetPeerLimit( s ) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_LIMIT_TORRENT, tr_sessionGetPeerLimitPerTorrent( s ) );
|
2009-10-19 05:25:50 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_INCOMPLETE_DIR, tr_sessionGetIncompleteDir( s ) );
|
2009-10-20 03:14:44 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, tr_sessionIsIncompleteDirEnabled( s ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_PEX_ENABLED, tr_sessionIsPexEnabled( s ) );
|
2009-05-21 00:15:54 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_DHT_ENABLED, tr_sessionIsDHTEnabled( s ) );
|
2010-05-08 08:42:45 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_LPD_ENABLED, tr_sessionIsLPDEnabled( s ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT, tr_sessionGetPeerPort( s ) );
|
2010-08-16 02:11:10 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, tr_sessionGetPeerPortRandomOnStart( s ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_PORT_FORWARDING, tr_sessionIsPortForwardingEnabled( s ) );
|
2010-02-01 04:43:10 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_RENAME_PARTIAL_FILES, tr_sessionIsIncompleteFileNamingEnabled( s ) );
|
2010-04-02 17:57:25 +00:00
|
|
|
tr_bencDictAddInt ( d, "rpc-version", RPC_VERSION );
|
|
|
|
tr_bencDictAddInt ( d, "rpc-version-minimum", RPC_VERSION_MIN );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddReal( d, "seedRatioLimit", tr_sessionGetRatioLimit( s ) );
|
2009-03-29 18:45:35 +00:00
|
|
|
tr_bencDictAddBool( d, "seedRatioLimited", tr_sessionIsRatioLimited( s ) );
|
2010-07-24 02:57:39 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_IDLE_LIMIT, tr_sessionGetIdleLimit( s ) );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_IDLE_LIMIT_ENABLED, tr_sessionIsIdleLimited( s ) );
|
2010-03-17 19:23:03 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_START, !tr_sessionGetPaused( s ) );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_TRASH_ORIGINAL, tr_sessionGetDeleteSource( s ) );
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_USPEED_KBps, tr_sessionGetSpeedLimit_KBps( s, TR_UP ) );
|
2009-06-25 01:57:31 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED, tr_sessionIsSpeedLimited( s, TR_UP ) );
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_DSPEED_KBps, tr_sessionGetSpeedLimit_KBps( s, TR_DOWN ) );
|
2009-06-25 01:57:31 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_DSPEED_ENABLED, tr_sessionIsSpeedLimited( s, TR_DOWN ) );
|
2010-05-08 22:42:28 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_SCRIPT_TORRENT_DONE_FILENAME, tr_sessionGetTorrentDoneScript( s ) );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_SCRIPT_TORRENT_DONE_ENABLED, tr_sessionIsTorrentDoneScriptEnabled( s ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddStr ( d, "version", LONG_VERSION_STRING );
|
2009-03-25 19:18:00 +00:00
|
|
|
switch( tr_sessionGetEncryption( s ) ) {
|
2008-12-14 11:21:11 +00:00
|
|
|
case TR_CLEAR_PREFERRED: str = "tolerated"; break;
|
2009-08-10 20:04:08 +00:00
|
|
|
case TR_ENCRYPTION_REQUIRED: str = "required"; break;
|
2008-12-14 11:21:11 +00:00
|
|
|
default: str = "preferred"; break;
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
2009-06-25 01:57:31 +00:00
|
|
|
tr_bencDictAddStr( d, TR_PREFS_KEY_ENCRYPTION, str );
|
2008-05-12 16:33:17 +00:00
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-01-19 18:11:47 +00:00
|
|
|
typedef const char* ( *handler )( tr_session*, tr_benc*, tr_benc*, struct tr_rpc_idle_data * );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
static struct method
|
|
|
|
{
|
|
|
|
const char * name;
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_bool immediate;
|
2009-01-19 18:11:47 +00:00
|
|
|
handler func;
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
methods[] =
|
|
|
|
{
|
2009-05-13 15:54:04 +00:00
|
|
|
{ "port-test", FALSE, portTest },
|
|
|
|
{ "blocklist-update", FALSE, blocklistUpdate },
|
|
|
|
{ "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-set", TRUE, torrentSet },
|
|
|
|
{ "torrent-set-location", TRUE, torrentSetLocation },
|
|
|
|
{ "torrent-start", TRUE, torrentStart },
|
|
|
|
{ "torrent-stop", TRUE, torrentStop },
|
|
|
|
{ "torrent-verify", TRUE, torrentVerify },
|
|
|
|
{ "torrent-reannounce", TRUE, torrentReannounce }
|
2008-05-12 13:05:06 +00:00
|
|
|
};
|
|
|
|
|
2009-01-20 02:03:09 +00:00
|
|
|
static void
|
|
|
|
noop_response_callback( tr_session * session UNUSED,
|
|
|
|
const char * response UNUSED,
|
|
|
|
size_t response_len UNUSED,
|
|
|
|
void * user_data UNUSED )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-12-30 22:07:39 +00:00
|
|
|
static void
|
2009-01-18 15:24:26 +00:00
|
|
|
request_exec( tr_session * session,
|
|
|
|
tr_benc * request,
|
|
|
|
tr_rpc_response_func callback,
|
|
|
|
void * callback_user_data )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
2009-01-19 21:37:34 +00:00
|
|
|
int i;
|
2008-05-12 00:41:55 +00:00
|
|
|
const char * str;
|
2009-01-19 21:37:34 +00:00
|
|
|
tr_benc * args_in = tr_bencDictFind( request, "arguments" );
|
2008-05-12 00:41:55 +00:00
|
|
|
const char * result = NULL;
|
|
|
|
|
2009-01-20 02:03:09 +00:00
|
|
|
if( callback == NULL )
|
|
|
|
callback = noop_response_callback;
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
/* parse the request */
|
2008-05-12 23:51:17 +00:00
|
|
|
if( !tr_bencDictFindStr( request, "method", &str ) )
|
|
|
|
result = "no method name";
|
2009-01-18 15:24:26 +00:00
|
|
|
else {
|
2008-05-13 17:31:09 +00:00
|
|
|
const int n = TR_N_ELEMENTS( methods );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
2008-05-13 17:31:09 +00:00
|
|
|
if( !strcmp( str, methods[i].name ) )
|
2008-05-12 13:05:06 +00:00
|
|
|
break;
|
2009-01-18 15:24:26 +00:00
|
|
|
if( i ==n )
|
|
|
|
result = "method name not recognized";
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
/* if we couldn't figure out which method to use, return an error */
|
|
|
|
if( result != NULL )
|
|
|
|
{
|
2009-01-19 21:37:34 +00:00
|
|
|
int64_t tag;
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_benc response;
|
2009-06-02 01:48:48 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2009-01-18 15:24:26 +00:00
|
|
|
|
|
|
|
tr_bencInitDict( &response, 3 );
|
|
|
|
tr_bencDictAddDict( &response, "arguments", 0 );
|
|
|
|
tr_bencDictAddStr( &response, "result", result );
|
2009-01-19 21:37:34 +00:00
|
|
|
if( tr_bencDictFindInt( request, "tag", &tag ) )
|
|
|
|
tr_bencDictAddInt( &response, "tag", tag );
|
2009-06-02 01:48:48 +00:00
|
|
|
tr_bencToBuf( &response, TR_FMT_JSON_LEAN, buf );
|
2009-01-19 18:11:47 +00:00
|
|
|
(*callback)( session, (const char*)EVBUFFER_DATA(buf),
|
|
|
|
EVBUFFER_LENGTH( buf ), callback_user_data );
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2009-06-02 01:48:48 +00:00
|
|
|
evbuffer_free( buf );
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_bencFree( &response );
|
|
|
|
}
|
2009-02-09 16:01:10 +00:00
|
|
|
else if( methods[i].immediate )
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2009-01-19 21:37:34 +00:00
|
|
|
int64_t tag;
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_benc response;
|
|
|
|
tr_benc * args_out;
|
2009-06-03 23:42:13 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2009-01-18 15:24:26 +00:00
|
|
|
|
|
|
|
tr_bencInitDict( &response, 3 );
|
|
|
|
args_out = tr_bencDictAddDict( &response, "arguments", 0 );
|
|
|
|
result = (*methods[i].func)( session, args_in, args_out, NULL );
|
|
|
|
if( result == NULL )
|
|
|
|
result = "success";
|
|
|
|
tr_bencDictAddStr( &response, "result", result );
|
2009-01-19 21:37:34 +00:00
|
|
|
if( tr_bencDictFindInt( request, "tag", &tag ) )
|
|
|
|
tr_bencDictAddInt( &response, "tag", tag );
|
2009-06-02 01:48:48 +00:00
|
|
|
tr_bencToBuf( &response, TR_FMT_JSON_LEAN, buf );
|
2009-01-19 18:11:47 +00:00
|
|
|
(*callback)( session, (const char*)EVBUFFER_DATA(buf),
|
|
|
|
EVBUFFER_LENGTH(buf), callback_user_data );
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2009-06-03 23:42:13 +00:00
|
|
|
evbuffer_free( buf );
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_bencFree( &response );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-19 21:37:34 +00:00
|
|
|
int64_t tag;
|
2009-01-18 15:24:26 +00:00
|
|
|
struct tr_rpc_idle_data * data = tr_new0( struct tr_rpc_idle_data, 1 );
|
|
|
|
data->session = session;
|
|
|
|
data->response = tr_new0( tr_benc, 1 );
|
|
|
|
tr_bencInitDict( data->response, 3 );
|
2009-01-19 21:37:34 +00:00
|
|
|
if( tr_bencDictFindInt( request, "tag", &tag ) )
|
|
|
|
tr_bencDictAddInt( data->response, "tag", tag );
|
2009-01-18 15:24:26 +00:00
|
|
|
data->args_out = tr_bencDictAddDict( data->response, "arguments", 0 );
|
|
|
|
data->callback = callback;
|
|
|
|
data->callback_user_data = callback_user_data;
|
|
|
|
(*methods[i].func)( session, args_in, data->args_out, data );
|
|
|
|
}
|
2008-05-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2008-12-30 22:07:39 +00:00
|
|
|
void
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_rpc_request_exec_json( tr_session * session,
|
|
|
|
const void * request_json,
|
2009-01-19 14:05:43 +00:00
|
|
|
int request_len,
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_rpc_response_func callback,
|
|
|
|
void * callback_user_data )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
|
|
|
tr_benc top;
|
2008-12-30 22:07:39 +00:00
|
|
|
int have_content;
|
2008-05-13 12:52:58 +00:00
|
|
|
|
|
|
|
if( request_len < 0 )
|
|
|
|
request_len = strlen( request_json );
|
|
|
|
|
2009-06-01 18:57:37 +00:00
|
|
|
have_content = !tr_jsonParse( "rpc", request_json, request_len, &top, NULL );
|
2009-01-18 15:24:26 +00:00
|
|
|
request_exec( session, have_content ? &top : NULL, callback, callback_user_data );
|
2008-05-13 12:52:58 +00:00
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
if( have_content )
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
2008-05-13 12:52:58 +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
|
|
|
|
*/
|
|
|
|
void
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_rpc_parse_list_str( tr_benc * setme,
|
2009-02-09 17:25:48 +00:00
|
|
|
const char * str,
|
2009-01-19 14:05:43 +00:00
|
|
|
int len )
|
2008-05-24 18:22:16 +00:00
|
|
|
|
|
|
|
{
|
2009-02-09 17:25:48 +00:00
|
|
|
int valueCount;
|
|
|
|
int * values = tr_parseNumberRange( str, len, &valueCount );
|
2008-05-24 18:22:16 +00:00
|
|
|
|
2009-02-09 17:25:48 +00:00
|
|
|
if( valueCount == 0 )
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_bencInitStr( setme, str, len );
|
2009-02-09 17:25:48 +00:00
|
|
|
else if( valueCount == 1 )
|
|
|
|
tr_bencInitInt( setme, values[0] );
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
tr_bencInitList( setme, valueCount );
|
|
|
|
for( i=0; i<valueCount; ++i )
|
|
|
|
tr_bencListAddInt( setme, values[i] );
|
2008-05-24 18:22:16 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 17:25:48 +00:00
|
|
|
tr_free( values );
|
2008-05-24 18:22:16 +00:00
|
|
|
}
|
|
|
|
|
2008-12-30 22:07:39 +00:00
|
|
|
void
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_rpc_request_exec_uri( tr_session * session,
|
|
|
|
const void * request_uri,
|
2009-01-19 14:05:43 +00:00
|
|
|
int request_len,
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_rpc_response_func callback,
|
|
|
|
void * callback_user_data )
|
2008-05-13 12:52:58 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc top, * args;
|
|
|
|
char * request = tr_strndup( request_uri, request_len );
|
2008-05-18 16:44:30 +00:00
|
|
|
const char * pch;
|
|
|
|
|
|
|
|
tr_bencInitDict( &top, 3 );
|
|
|
|
args = tr_bencDictAddDict( &top, "arguments", 0 );
|
|
|
|
|
|
|
|
pch = strchr( request, '?' );
|
|
|
|
if( !pch ) pch = request;
|
|
|
|
while( pch )
|
2008-05-13 17:31:09 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
const char * delim = strchr( pch, '=' );
|
|
|
|
const char * next = strchr( pch, '&' );
|
|
|
|
if( delim )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char * key = tr_strndup( pch, delim - pch );
|
|
|
|
int isArg = strcmp( key, "method" ) && strcmp( key, "tag" );
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_benc * parent = isArg ? args : ⊤
|
2008-05-24 18:22:16 +00:00
|
|
|
tr_rpc_parse_list_str( tr_bencDictAdd( parent, key ),
|
2008-09-23 19:11:04 +00:00
|
|
|
delim + 1,
|
|
|
|
next ? (size_t)(
|
|
|
|
next -
|
|
|
|
( delim + 1 ) ) : strlen( delim + 1 ) );
|
2008-05-24 18:22:16 +00:00
|
|
|
tr_free( key );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
pch = next ? next + 1 : NULL;
|
2008-05-13 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
request_exec( session, &top, callback, callback_user_data );
|
2008-05-13 17:31:09 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
/* cleanup */
|
|
|
|
tr_bencFree( &top );
|
2008-05-13 17:31:09 +00:00
|
|
|
tr_free( request );
|
2008-05-13 12:52:58 +00:00
|
|
|
}
|