2008-05-12 00:41:55 +00:00
|
|
|
/*
|
2009-01-10 23:09:07 +00:00
|
|
|
* This file Copyright (C) 2008-2009 Charles Kerr <charles@transmissionbt.com>
|
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 */
|
|
|
|
#include <stdlib.h> /* strtol */
|
2008-05-12 13:05:06 +00:00
|
|
|
#include <string.h> /* strcmp */
|
|
|
|
|
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"
|
2008-09-05 14:31:58 +00:00
|
|
|
#include "rpcimpl.h"
|
2008-05-13 12:52:58 +00:00
|
|
|
#include "json.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"
|
2009-03-02 05:48:32 +00:00
|
|
|
#include "completion.h"
|
2008-05-12 00:41:55 +00:00
|
|
|
#include "utils.h"
|
2009-01-18 15:24:26 +00:00
|
|
|
#include "web.h"
|
2008-05-12 00:41:55 +00:00
|
|
|
|
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
|
|
|
{
|
|
|
|
struct evbuffer * buf = tr_getBuffer( );
|
|
|
|
|
|
|
|
if( result == NULL )
|
|
|
|
result = "success";
|
|
|
|
tr_bencDictAddStr( data->response, "result", result );
|
|
|
|
|
|
|
|
tr_bencSaveAsJSON( data->response, 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
|
|
|
|
|
|
|
tr_releaseBuffer( buf );
|
|
|
|
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;
|
|
|
|
const time_t now = time( NULL );
|
|
|
|
const time_t window = 120;
|
|
|
|
const int n = tr_sessionCountTorrents( session );
|
|
|
|
torrents = tr_new0( tr_torrent *, n );
|
|
|
|
while( ( tor = tr_torrentNext( session, tor ) ) ) {
|
|
|
|
time_t a = tor->activityDate;
|
|
|
|
a = MAX( a, tor->addedDate );
|
|
|
|
a = MAX( a, tor->doneDate );
|
|
|
|
a = MAX( a, tor->startDate );
|
|
|
|
if( a >= now - window )
|
|
|
|
torrents[torrentCount++] = tor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-12 00:41:55 +00:00
|
|
|
else /* all of them */
|
|
|
|
{
|
|
|
|
tr_torrent * tor = NULL;
|
2008-12-14 11:21: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,
|
|
|
|
struct tr_rpc_idle_data * idle_data )
|
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_torrentStart( tor );
|
2008-12-14 11:21:11 +00:00
|
|
|
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,
|
|
|
|
struct tr_rpc_idle_data * idle_data )
|
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_torrentStop( tor );
|
2008-12-14 11:21:11 +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,
|
|
|
|
struct tr_rpc_idle_data * idle_data )
|
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 );
|
2008-12-09 17:01:49 +00:00
|
|
|
int64_t deleteFlag;
|
|
|
|
if( tr_bencDictFindInt( args_in, "delete-local-data", &deleteFlag ) && deleteFlag )
|
2008-12-23 16:04:11 +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;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
struct tr_rpc_idle_data * idle_data )
|
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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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];
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * d = tr_bencListAddDict( trackers, 3 );
|
2008-05-12 00:41:55 +00:00
|
|
|
tr_bencDictAddStr( d, "announce", t->announce );
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
tr_bencDictAddStr( d, "address", peer->addr );
|
|
|
|
tr_bencDictAddStr( d, "clientName", peer->client );
|
|
|
|
tr_bencDictAddInt( d, "clientIsChoked", peer->clientIsChoked );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddInt( d, "clientIsInterested",
|
|
|
|
peer->clientIsInterested );
|
2008-08-22 17:59:31 +00:00
|
|
|
tr_bencDictAddStr( d, "flagStr", peer->flagStr );
|
|
|
|
tr_bencDictAddInt( d, "isDownloadingFrom", peer->isDownloadingFrom );
|
|
|
|
tr_bencDictAddInt( d, "isEncrypted", peer->isEncrypted );
|
|
|
|
tr_bencDictAddInt( d, "isIncoming", peer->isIncoming );
|
|
|
|
tr_bencDictAddInt( d, "isUploadingTo", peer->isUploadingTo );
|
|
|
|
tr_bencDictAddInt( d, "peerIsChoked", peer->peerIsChoked );
|
|
|
|
tr_bencDictAddInt( d, "peerIsInterested", peer->peerIsInterested );
|
2008-11-20 20:39:19 +00:00
|
|
|
tr_bencDictAddInt( d, "port", peer->port );
|
2008-08-22 17:59:31 +00:00
|
|
|
tr_bencDictAddDouble( d, "progress", peer->progress );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddInt( d, "rateToClient",
|
|
|
|
(int)( peer->rateToClient * 1024.0 ) );
|
|
|
|
tr_bencDictAddInt( d, "rateToPeer",
|
|
|
|
(int)( peer->rateToPeer * 1024.0 ) );
|
2008-08-22 17:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrentPeersFree( peers, peerCount );
|
|
|
|
}
|
|
|
|
|
2008-05-12 13:05:06 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +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 );
|
2008-05-12 13:05:06 +00:00
|
|
|
|
2008-07-26 14:47:07 +00:00
|
|
|
if( !strcmp( key, "activityDate" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->activityDate );
|
|
|
|
else if( !strcmp( key, "addedDate" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->addedDate );
|
|
|
|
else if( !strcmp( key, "announceResponse" ) )
|
|
|
|
tr_bencDictAddStr( d, key, st->announceResponse );
|
|
|
|
else if( !strcmp( key, "announceURL" ) )
|
|
|
|
tr_bencDictAddStr( d, key, st->announceURL );
|
|
|
|
else if( !strcmp( key, "comment" ) )
|
|
|
|
tr_bencDictAddStr( d, key, inf->comment ? inf->comment : "" );
|
|
|
|
else if( !strcmp( key, "corruptEver" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->corruptEver );
|
|
|
|
else if( !strcmp( key, "creator" ) )
|
|
|
|
tr_bencDictAddStr( d, key, inf->creator ? inf->creator : "" );
|
|
|
|
else if( !strcmp( key, "dateCreated" ) )
|
|
|
|
tr_bencDictAddInt( d, key, inf->dateCreated );
|
|
|
|
else if( !strcmp( key, "desiredAvailable" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->desiredAvailable );
|
|
|
|
else if( !strcmp( key, "doneDate" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->doneDate );
|
2009-01-17 14:58:50 +00:00
|
|
|
else if( !strcmp( key, "downloadDir" ) )
|
|
|
|
tr_bencDictAddStr( d, key, tr_torrentGetDownloadDir( tor ) );
|
2008-07-26 14:47:07 +00:00
|
|
|
else if( !strcmp( key, "downloadedEver" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->downloadedEver );
|
2008-12-02 19:46:51 +00:00
|
|
|
else if( !strcmp( key, "downloaders" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->downloaders );
|
2008-07-26 14:47:07 +00:00
|
|
|
else if( !strcmp( key, "error" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->error );
|
|
|
|
else if( !strcmp( key, "errorString" ) )
|
|
|
|
tr_bencDictAddStr( d, key, st->errorString );
|
|
|
|
else if( !strcmp( key, "eta" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->eta );
|
|
|
|
else if( !strcmp( key, "files" ) )
|
|
|
|
addFiles( tor, tr_bencDictAddList( d, key, inf->fileCount ) );
|
|
|
|
else if( !strcmp( key, "hashString" ) )
|
|
|
|
tr_bencDictAddStr( d, key, tor->info.hashString );
|
|
|
|
else if( !strcmp( key, "haveUnchecked" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->haveUnchecked );
|
|
|
|
else if( !strcmp( key, "haveValid" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->haveValid );
|
|
|
|
else if( !strcmp( key, "id" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->id );
|
|
|
|
else if( !strcmp( key, "isPrivate" ) )
|
|
|
|
tr_bencDictAddInt( d, key, tr_torrentIsPrivate( tor ) );
|
|
|
|
else if( !strcmp( key, "lastAnnounceTime" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->lastAnnounceTime );
|
|
|
|
else if( !strcmp( key, "lastScrapeTime" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->lastScrapeTime );
|
|
|
|
else if( !strcmp( key, "leechers" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->leechers );
|
|
|
|
else if( !strcmp( key, "leftUntilDone" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->leftUntilDone );
|
|
|
|
else if( !strcmp( key, "manualAnnounceTime" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->manualAnnounceTime );
|
|
|
|
else if( !strcmp( key, "maxConnectedPeers" ) )
|
|
|
|
tr_bencDictAddInt( d, key, tr_torrentGetPeerLimit( tor ) );
|
|
|
|
else if( !strcmp( key, "name" ) )
|
|
|
|
tr_bencDictAddStr( d, key, inf->name );
|
|
|
|
else if( !strcmp( key, "nextAnnounceTime" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->nextAnnounceTime );
|
|
|
|
else if( !strcmp( key, "nextScrapeTime" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->nextScrapeTime );
|
2008-08-22 17:59:31 +00:00
|
|
|
else if( !strcmp( key, "peers" ) )
|
|
|
|
addPeers( tor, tr_bencDictAdd( d, key ) );
|
2008-07-26 14:47:07 +00:00
|
|
|
else if( !strcmp( key, "peersConnected" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->peersConnected );
|
2008-09-23 19:11:04 +00:00
|
|
|
else if( !strcmp( key, "peersFrom" ) )
|
|
|
|
{
|
|
|
|
tr_benc * tmp = tr_bencDictAddDict( d, key, 4 );
|
2008-06-17 04:47:20 +00:00
|
|
|
const int * f = st->peersFrom;
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_bencDictAddInt( tmp, "fromCache", f[TR_PEER_FROM_CACHE] );
|
|
|
|
tr_bencDictAddInt( tmp, "fromIncoming", f[TR_PEER_FROM_INCOMING] );
|
|
|
|
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
|
|
|
}
|
2008-07-26 14:47:07 +00:00
|
|
|
else if( !strcmp( key, "peersGettingFromUs" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->peersGettingFromUs );
|
|
|
|
else if( !strcmp( key, "peersKnown" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->peersKnown );
|
|
|
|
else if( !strcmp( key, "peersSendingToUs" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->peersSendingToUs );
|
2009-03-02 05:48:32 +00:00
|
|
|
else if( !strcmp( key, "pieces" ) ) {
|
|
|
|
const tr_bitfield * pieces = tr_cpPieceBitfield( &tor->completion );
|
|
|
|
char * str = tr_base64_encode( pieces->bits, pieces->byteCount, NULL );
|
|
|
|
tr_bencDictAddStr( d, key, str );
|
|
|
|
tr_free( str );
|
|
|
|
}
|
2008-07-26 14:47:07 +00:00
|
|
|
else if( !strcmp( key, "pieceCount" ) )
|
|
|
|
tr_bencDictAddInt( d, key, inf->pieceCount );
|
|
|
|
else if( !strcmp( key, "pieceSize" ) )
|
|
|
|
tr_bencDictAddInt( d, key, inf->pieceSize );
|
2008-09-23 19:11:04 +00:00
|
|
|
else if( !strcmp( key, "priorities" ) )
|
|
|
|
{
|
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 );
|
|
|
|
}
|
2008-07-26 14:47:07 +00:00
|
|
|
else if( !strcmp( key, "rateDownload" ) )
|
2008-11-08 02:49:04 +00:00
|
|
|
tr_bencDictAddInt( d, key, (int)( st->pieceDownloadSpeed * 1024 ) );
|
2008-07-26 14:47:07 +00:00
|
|
|
else if( !strcmp( key, "rateUpload" ) )
|
2008-11-08 02:49:04 +00:00
|
|
|
tr_bencDictAddInt( d, key, (int)( st->pieceUploadSpeed * 1024 ) );
|
2008-07-26 14:47:07 +00:00
|
|
|
else if( !strcmp( key, "recheckProgress" ) )
|
|
|
|
tr_bencDictAddDouble( d, key, st->recheckProgress );
|
|
|
|
else if( !strcmp( key, "scrapeResponse" ) )
|
|
|
|
tr_bencDictAddStr( d, key, st->scrapeResponse );
|
|
|
|
else if( !strcmp( key, "scrapeURL" ) )
|
|
|
|
tr_bencDictAddStr( d, key, st->scrapeURL );
|
|
|
|
else if( !strcmp( key, "seeders" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->seeders );
|
|
|
|
else if( !strcmp( key, "sizeWhenDone" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->sizeWhenDone );
|
2009-03-04 19:52:57 +00:00
|
|
|
else if( !strcmp( key, "speed-limit-down" ) )
|
|
|
|
tr_bencDictAddInt( d, key, tr_torrentGetSpeedLimit( tor, TR_DOWN ) );
|
|
|
|
else if( !strcmp( key, "speed-limit-down-enabled" ) )
|
|
|
|
tr_bencDictAddInt( d, key, tr_torrentIsUsingSpeedLimit( tor, TR_DOWN ) );
|
|
|
|
else if( !strcmp( key, "speed-limit-down-global-enabled" ) )
|
|
|
|
tr_bencDictAddInt( d, key, tr_torrentIsUsingGlobalSpeedLimit( tor, TR_DOWN ) );
|
|
|
|
else if( !strcmp( key, "speed-limit-up" ) )
|
|
|
|
tr_bencDictAddInt( d, key, tr_torrentGetSpeedLimit( tor, TR_UP ) );
|
|
|
|
else if( !strcmp( key, "speed-limit-up-enabled" ) )
|
|
|
|
tr_bencDictAddInt( d, key, tr_torrentIsUsingSpeedLimit( tor, TR_UP ) );
|
|
|
|
else if( !strcmp( key, "speed-limit-up-global-enabled" ) )
|
|
|
|
tr_bencDictAddInt( d, key, tr_torrentIsUsingGlobalSpeedLimit( tor, TR_UP ) );
|
2008-07-26 14:47:07 +00:00
|
|
|
else if( !strcmp( key, "startDate" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->startDate );
|
|
|
|
else if( !strcmp( key, "status" ) )
|
2008-10-20 17:54:56 +00:00
|
|
|
tr_bencDictAddInt( d, key, st->activity );
|
2008-07-26 14:47:07 +00:00
|
|
|
else if( !strcmp( key, "swarmSpeed" ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddInt( d, key, (int)( st->swarmSpeed * 1024 ) );
|
2008-07-26 14:47:07 +00:00
|
|
|
else if( !strcmp( key, "timesCompleted" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->timesCompleted );
|
|
|
|
else if( !strcmp( key, "trackers" ) )
|
|
|
|
addTrackers( inf, tr_bencDictAddList( d, key, inf->trackerCount ) );
|
|
|
|
else if( !strcmp( key, "totalSize" ) )
|
|
|
|
tr_bencDictAddInt( d, key, inf->totalSize );
|
|
|
|
else if( !strcmp( key, "uploadedEver" ) )
|
|
|
|
tr_bencDictAddInt( d, key, st->uploadedEver );
|
|
|
|
else if( !strcmp( key, "uploadRatio" ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddDouble( d, key,
|
|
|
|
tr_getRatio( st->uploadedEver,
|
|
|
|
st->downloadedEver ) );
|
|
|
|
else if( !strcmp( key, "wanted" ) )
|
|
|
|
{
|
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
|
|
|
}
|
2008-07-26 14:47:07 +00:00
|
|
|
else if( !strcmp( key, "webseeds" ) )
|
|
|
|
addWebseeds( inf, tr_bencDictAddList( d, key, inf->trackerCount ) );
|
|
|
|
else if( !strcmp( key, "webseedsSendingToUs" ) )
|
|
|
|
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,
|
|
|
|
struct tr_rpc_idle_data * idle_data )
|
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;
|
2008-06-16 03:47:50 +00:00
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
assert( idle_data == NULL );
|
|
|
|
|
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 )
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
struct tr_rpc_idle_data * idle_data )
|
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;
|
2008-05-12 13:05:06 +00:00
|
|
|
tr_torrent * tor = torrents[i];
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( tr_bencDictFindList( args_in, "files-unwanted", &files ) )
|
|
|
|
setFileDLs( tor, FALSE, files );
|
|
|
|
if( tr_bencDictFindList( args_in, "files-wanted", &files ) )
|
|
|
|
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 );
|
2008-06-17 16:25:13 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "speed-limit-down", &tmp ) )
|
|
|
|
tr_torrentSetSpeedLimit( tor, TR_DOWN, tmp );
|
|
|
|
if( tr_bencDictFindInt( args_in, "speed-limit-down-enabled", &tmp ) )
|
2009-03-04 19:52:57 +00:00
|
|
|
tr_torrentUseSpeedLimit( tor, TR_DOWN, tmp!=0 );
|
|
|
|
if( tr_bencDictFindInt( args_in, "speed-limit-down-global-enabled", &tmp ) )
|
|
|
|
tr_torrentUseGlobalSpeedLimit( tor, TR_DOWN, tmp!=0 );
|
2008-06-17 16:25:13 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "speed-limit-up", &tmp ) )
|
|
|
|
tr_torrentSetSpeedLimit( tor, TR_UP, tmp );
|
|
|
|
if( tr_bencDictFindInt( args_in, "speed-limit-up-enabled", &tmp ) )
|
2009-03-04 19:52:57 +00:00
|
|
|
tr_torrentUseSpeedLimit( tor, TR_UP, tmp!=0 );
|
|
|
|
if( tr_bencDictFindInt( args_in, "speed-limit-up-global-enabled", &tmp ) )
|
|
|
|
tr_torrentUseGlobalSpeedLimit( tor, TR_UP, tmp!=0 );
|
2009-02-13 18:23:56 +00:00
|
|
|
if( tr_bencDictFindDouble( args_in, "ratio-limit", &d ) )
|
|
|
|
tr_torrentSetRatioLimit( tor, d );
|
|
|
|
if( tr_bencDictFindInt( args_in, "ratio-limit-mode", &tmp ) )
|
|
|
|
tr_torrentSetRatioMode( tor, tmp );
|
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
|
|
|
}
|
|
|
|
|
2008-05-12 13:05:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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;
|
|
|
|
tr_torrent * tor = tr_torrentNew( data->session, ctor, &err );
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
else if( err == TR_EDUPLICATE )
|
|
|
|
{
|
|
|
|
result = "duplicate torrent";
|
|
|
|
}
|
|
|
|
else if( err == TR_EINVALID )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
|
|
|
|
if( response_code == 200 )
|
|
|
|
{
|
|
|
|
tr_ctorSetMetainfo( data->ctor, response, response_byte_count );
|
|
|
|
addTorrentImpl( data->data, data->ctor );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char result[1024];
|
|
|
|
tr_snprintf( result, sizeof( result ), "http error %ld: %s",
|
|
|
|
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;
|
|
|
|
|
|
|
|
return ( strstr( filename, "ftp://" ) != NULL )
|
|
|
|
|| ( strstr( filename, "http://" ) != NULL )
|
|
|
|
|| ( strstr( filename, "https://" ) != NULL );
|
|
|
|
}
|
|
|
|
|
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;
|
2008-05-12 13:05:06 +00:00
|
|
|
const char * str;
|
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 */
|
2008-05-18 16:44:30 +00:00
|
|
|
if( tr_bencDictFindStr( args_in, "download-dir", &str ) )
|
|
|
|
tr_ctorSetDownloadDir( ctor, TR_FORCE, str );
|
2008-05-12 23:51:17 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "paused", &i ) )
|
|
|
|
tr_ctorSetPaused( ctor, TR_FORCE, i );
|
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
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
dbgmsg( "torrentAdd: filename is \"%s\"", filename );
|
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
|
|
|
{
|
2009-01-18 15:24:26 +00:00
|
|
|
if( filename != NULL )
|
|
|
|
tr_ctorSetMetainfoFromFile( ctor, filename );
|
|
|
|
else {
|
|
|
|
int len;
|
|
|
|
char * metainfo = tr_base64_decode( metainfo_base64, -1, &len );
|
|
|
|
tr_ctorSetMetainfo( ctor, (uint8_t*)metainfo, len );
|
|
|
|
tr_free( metainfo );
|
|
|
|
}
|
|
|
|
addTorrentImpl( idle_data, ctor );
|
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,
|
|
|
|
struct tr_rpc_idle_data * idle_data )
|
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;
|
2008-05-12 16:33:17 +00:00
|
|
|
const char * str;
|
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
assert( idle_data == NULL );
|
|
|
|
|
2009-03-25 19:18:00 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_ALT_LIMIT_ENABLED, &i ) )
|
|
|
|
tr_sessionSetAltSpeedLimitEnabled( session, i!=0 );
|
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_ALT_BEGIN, &i ) )
|
|
|
|
tr_sessionSetAltSpeedLimitBegin( session, i );
|
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_ALT_END, &i ) )
|
|
|
|
tr_sessionSetAltSpeedLimitEnd( session, i );
|
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_ALT_DL_LIMIT, &i ) )
|
|
|
|
tr_sessionSetAltSpeedLimit( session, TR_DOWN, i );
|
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_ALT_UL_LIMIT, &i ) )
|
|
|
|
tr_sessionSetAltSpeedLimit( session, TR_UP, i );
|
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_BLOCKLIST_ENABLED, &i ) )
|
|
|
|
tr_blocklistSetEnabled( session, i!=0 );
|
|
|
|
if( tr_bencDictFindStr( args_in, TR_PREFS_KEY_DOWNLOAD_DIR, &str ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetDownloadDir( session, str );
|
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 );
|
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_PEX_ENABLED, &i ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetPexEnabled( session, i );
|
2009-03-25 19:18:00 +00:00
|
|
|
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-25 19:18:00 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_PORT_FORWARDING, &i ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetPortForwardingEnabled( session, i );
|
2008-05-12 16:33:17 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "speed-limit-down", &i ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetSpeedLimit( session, TR_DOWN, i );
|
2008-05-12 16:33:17 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "speed-limit-down-enabled", &i ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetSpeedLimitEnabled( session, TR_DOWN, i );
|
2008-05-12 16:33:17 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "speed-limit-up", &i ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetSpeedLimit( session, TR_UP, i );
|
2008-05-12 16:33:17 +00:00
|
|
|
if( tr_bencDictFindInt( args_in, "speed-limit-up-enabled", &i ) )
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetSpeedLimitEnabled( session, TR_UP, i );
|
2009-02-13 18:23:56 +00:00
|
|
|
if( tr_bencDictFindDouble( args_in, "ratio-limit", &d ) )
|
|
|
|
tr_sessionSetRatioLimit( session, d );
|
|
|
|
if( tr_bencDictFindInt( args_in, "ratio-limit-enabled", &i ) )
|
|
|
|
tr_sessionSetRatioLimited( session, i );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindStr( args_in, "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,
|
|
|
|
struct tr_rpc_idle_data * idle_data )
|
2008-06-18 22:01:15 +00:00
|
|
|
{
|
2009-01-13 04:43:38 +00:00
|
|
|
int running = 0;
|
|
|
|
int total = 0;
|
2009-01-19 21:17:29 +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-01-19 21:17:29 +00:00
|
|
|
tr_sessionGetStats( session, ¤tStats );
|
|
|
|
tr_sessionGetCumulativeStats( session, &cumulativeStats );
|
|
|
|
|
2009-01-13 04:43:38 +00:00
|
|
|
tr_bencDictAddInt( args_out, "activeTorrentCount", running );
|
|
|
|
tr_bencDictAddInt( args_out, "downloadSpeed", (int)( tr_sessionGetPieceSpeed( session, TR_DOWN ) * 1024 ) );
|
|
|
|
tr_bencDictAddInt( args_out, "pausedTorrentCount", total - running );
|
|
|
|
tr_bencDictAddInt( args_out, "torrentCount", total );
|
|
|
|
tr_bencDictAddInt( args_out, "uploadSpeed", (int)( tr_sessionGetPieceSpeed( session, TR_UP ) * 1024 ) );
|
2009-01-19 21:17:29 +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 );
|
|
|
|
|
|
|
|
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 );
|
|
|
|
|
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,
|
|
|
|
struct tr_rpc_idle_data * idle_data )
|
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 );
|
2009-03-25 19:18:00 +00:00
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_ALT_LIMIT_ENABLED, tr_sessionIsAltSpeedLimitEnabled( s ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_ALT_UL_LIMIT, tr_sessionGetAltSpeedLimit( s, TR_UP ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_ALT_DL_LIMIT, tr_sessionGetAltSpeedLimit( s, TR_DOWN ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_ALT_BEGIN, tr_sessionGetAltSpeedLimitBegin( s ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_ALT_END, tr_sessionGetAltSpeedLimitEnd( s ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, tr_blocklistIsEnabled( s ) );
|
|
|
|
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 ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_PEX_ENABLED, tr_sessionIsPexEnabled( s ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_PEER_PORT, tr_sessionGetPeerPort( s ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_PORT_FORWARDING, tr_sessionIsPortForwardingEnabled( s ) );
|
2009-01-16 01:03:33 +00:00
|
|
|
tr_bencDictAddInt( d, "rpc-version", 4 );
|
|
|
|
tr_bencDictAddInt( d, "rpc-version-minimum", 1 );
|
2009-03-25 19:18:00 +00:00
|
|
|
tr_bencDictAddInt( d, "speed-limit-up", tr_sessionGetSpeedLimit( s, TR_UP ) );
|
|
|
|
tr_bencDictAddInt( d, "speed-limit-up-enabled", tr_sessionIsSpeedLimitEnabled( s, TR_UP ) );
|
|
|
|
tr_bencDictAddInt( d, "speed-limit-down", tr_sessionGetSpeedLimit( s, TR_DOWN ) );
|
|
|
|
tr_bencDictAddInt( d, "speed-limit-down-enabled", tr_sessionIsSpeedLimitEnabled( s, TR_DOWN ) );
|
|
|
|
tr_bencDictAddDouble( d, "ratio-limit", tr_sessionGetRatioLimit( s ) );
|
|
|
|
tr_bencDictAddInt( d, "ratio-limit-enabled", tr_sessionIsRatioLimited( s ) );
|
2008-12-10 21:46:24 +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;
|
|
|
|
case TR_ENCRYPTION_REQUIRED: str = "required"; break;
|
|
|
|
default: str = "preferred"; break;
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
tr_bencDictAddStr( d, "encryption", str );
|
|
|
|
|
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[] =
|
|
|
|
{
|
|
|
|
{ "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-start", TRUE, torrentStart },
|
|
|
|
{ "torrent-stop", TRUE, torrentStop },
|
|
|
|
{ "torrent-verify", TRUE, torrentVerify }
|
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;
|
|
|
|
struct evbuffer * buf = tr_getBuffer( );
|
|
|
|
|
|
|
|
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-01-18 15:24:26 +00:00
|
|
|
tr_bencSaveAsJSON( &response, 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
|
|
|
|
|
|
|
tr_releaseBuffer( buf );
|
|
|
|
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;
|
|
|
|
struct evbuffer * buf = tr_getBuffer( );
|
|
|
|
|
|
|
|
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-01-18 15:24:26 +00:00
|
|
|
tr_bencSaveAsJSON( &response, 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
|
|
|
|
|
|
|
tr_releaseBuffer( buf );
|
|
|
|
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 );
|
|
|
|
|
2008-08-18 03:44:09 +00:00
|
|
|
have_content = !tr_jsonParse( 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
|
|
|
}
|