2008-05-18 16:44:30 +00:00
|
|
|
/*
|
2009-01-10 23:09:07 +00:00
|
|
|
* This file Copyright (C) 2008-2009 Charles Kerr <charles@transmissionbt.com>
|
2007-04-18 16:39:10 +00:00
|
|
|
*
|
2008-05-18 16:44:30 +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-18 16:44:30 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2007-04-18 16:39:10 +00:00
|
|
|
*
|
2008-05-19 00:12:31 +00:00
|
|
|
* $Id$
|
2008-05-18 16:44:30 +00:00
|
|
|
*/
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2009-05-08 14:56:11 +00:00
|
|
|
#include <ctype.h> /* isspace */
|
2008-07-10 20:59:15 +00:00
|
|
|
#include <errno.h>
|
2009-04-22 21:00:52 +00:00
|
|
|
#include <math.h>
|
2007-04-18 16:39:10 +00:00
|
|
|
#include <stdio.h>
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h> /* strcmp */
|
2007-07-18 23:04:26 +00:00
|
|
|
|
2009-02-25 18:06:57 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <direct.h> /* getcwd */
|
|
|
|
#else
|
|
|
|
#include <unistd.h> /* getcwd */
|
|
|
|
#endif
|
2008-11-15 17:39:54 +00:00
|
|
|
|
2009-06-03 15:47:08 +00:00
|
|
|
#include <event.h>
|
2009-01-24 00:28:41 +00:00
|
|
|
|
|
|
|
#define CURL_DISABLE_TYPECHECK /* otherwise -Wunreachable-code goes insane */
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <curl/curl.h>
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/bencode.h>
|
2008-09-05 14:31:58 +00:00
|
|
|
#include <libtransmission/rpcimpl.h>
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <libtransmission/json.h>
|
2008-07-08 14:29:05 +00:00
|
|
|
#include <libtransmission/tr-getopt.h>
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <libtransmission/utils.h>
|
|
|
|
#include <libtransmission/version.h>
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#define MY_NAME "transmission-remote"
|
|
|
|
#define DEFAULT_HOST "localhost"
|
2008-12-13 23:17:36 +00:00
|
|
|
#define DEFAULT_PORT atoi(TR_DEFAULT_RPC_PORT_STR)
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2009-08-13 03:54:54 +00:00
|
|
|
enum { TAG_SESSION, TAG_STATS, TAG_LIST, TAG_DETAILS, TAG_FILES, TAG_PEERS };
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-07-07 21:23:34 +00:00
|
|
|
static const char*
|
|
|
|
getUsage( void )
|
|
|
|
{
|
2008-07-10 20:59:15 +00:00
|
|
|
return
|
2008-09-23 19:11:04 +00:00
|
|
|
"Transmission " LONG_VERSION_STRING
|
|
|
|
" http://www.transmissionbt.com/\n"
|
|
|
|
"A fast and easy BitTorrent client\n"
|
|
|
|
"\n"
|
|
|
|
"Usage: " MY_NAME
|
|
|
|
" [host] [options]\n"
|
|
|
|
" "
|
|
|
|
MY_NAME " [port] [options]\n"
|
|
|
|
" "
|
|
|
|
MY_NAME " [host:port] [options]\n"
|
|
|
|
"\n"
|
|
|
|
"See the man page for detailed explanations and many examples.";
|
2008-07-07 21:23:34 +00:00
|
|
|
}
|
|
|
|
|
2008-07-08 14:29:05 +00:00
|
|
|
static tr_option opts[] =
|
2008-07-07 21:23:34 +00:00
|
|
|
{
|
2009-01-18 15:27:12 +00:00
|
|
|
{ 'a', "add", "Add torrent files by filename or URL", "a", 0, NULL },
|
2009-08-17 20:41:06 +00:00
|
|
|
{ 970, "alt-speed", "Use the alternate Limits", "as", 0, NULL },
|
|
|
|
{ 971, "no-alt-speed", "Don't use the alternate Limits", "AS", 0, NULL },
|
|
|
|
{ 972, "alt-speed-downlimit", "max alternate download speed (in KB/s)", "asd", 1, "<speed>" },
|
|
|
|
{ 973, "alt-speed-uplimit", "max alternate upload speed (in KB/s)", "asu", 1, "<speed>" },
|
|
|
|
{ 974, "alt-speed-scheduler", "Use the scheduled on/off times", "asc", 0, NULL },
|
|
|
|
{ 975, "no-alt-speed-scheduler","Don't use the scheduled on/off times", "ASC", 0, NULL },
|
|
|
|
{ 976, "alt-speed-time-begin", "Time to start using the alt speed limits (in hhmm)", NULL, 1, "<time>" },
|
|
|
|
{ 977, "alt-speed-time-end", "Time to stop using the alt speed limits (in hhmm)", NULL, 1, "<time>" },
|
|
|
|
{ 978, "alt-speed-days", "Numbers for any/all days of the week - eg. \"1-7\"", NULL, 1, "<days>" },
|
2009-01-17 14:58:50 +00:00
|
|
|
{ 'b', "debug", "Print debugging information", "b", 0, NULL },
|
|
|
|
{ 'd', "downlimit", "Set the maximum global download speed in KB/s", "d", 1, "<speed>" },
|
|
|
|
{ 'D', "no-downlimit", "Don't limit the global download speed", "D", 0, NULL },
|
|
|
|
{ 910, "encryption-required", "Encrypt all peer connections", "er", 0, NULL },
|
|
|
|
{ 911, "encryption-preferred", "Prefer encrypted peer connections", "ep", 0, NULL },
|
|
|
|
{ 912, "encryption-tolerated", "Prefer unencrypted peer connections", "et", 0, NULL },
|
2009-02-09 19:36:29 +00:00
|
|
|
{ 'f', "files", "List the current torrent(s)' files", "f", 0, NULL },
|
2009-01-17 14:58:50 +00:00
|
|
|
{ 'g', "get", "Mark files for download", "g", 1, "<files>" },
|
|
|
|
{ 'G', "no-get", "Mark files for not downloading", "G", 1, "<files>" },
|
2009-02-09 19:36:29 +00:00
|
|
|
{ 'i', "info", "Show the current torrent(s)' details", "i", 0, NULL },
|
|
|
|
{ 920, "session-info", "Show the session's details", "si", 0, NULL },
|
2009-08-13 03:54:54 +00:00
|
|
|
{ 921, "session-stats", "Show the session's statistics", "st", 0, NULL },
|
2009-01-17 14:58:50 +00:00
|
|
|
{ 'l', "list", "List all torrents", "l", 0, NULL },
|
2009-05-13 15:54:04 +00:00
|
|
|
{ 960, "move", "Move current torrent's data to a new folder", NULL, 1, "<path>" },
|
|
|
|
{ 961, "find", "Tell Transmission where to find a torrent's data", NULL, 1, "<path>" },
|
2009-01-17 14:58:50 +00:00
|
|
|
{ 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", 0, NULL },
|
|
|
|
{ 'M', "no-portmap", "Disable portmapping", "M", 0, NULL },
|
|
|
|
{ 'n', "auth", "Set authentication info", "n", 1, "<username:password>" },
|
2009-02-10 16:22:59 +00:00
|
|
|
{ 'N', "netrc", "Set authentication info from a .netrc file", "N", 1, "<filename>" },
|
2009-05-21 00:15:54 +00:00
|
|
|
{ 'o', "dht", "Enable distributed hash tables (DHT)", "o", 0, NULL },
|
|
|
|
{ 'O', "no-dht", "Disable distributed hash tables (DHT)", "O", 0, NULL },
|
2009-01-17 14:58:50 +00:00
|
|
|
{ 'p', "port", "Port for incoming peers (Default: " TR_DEFAULT_PEER_PORT_STR ")", "p", 1, "<port>" },
|
2009-04-09 21:10:58 +00:00
|
|
|
{ 'P', "random-port", "Random port for incomping peers", "P", 0, NULL },
|
2009-01-17 14:58:50 +00:00
|
|
|
{ 900, "priority-high", "Set the files' priorities as high", "ph", 1, "<files>" },
|
|
|
|
{ 901, "priority-normal", "Set the files' priorities as normal", "pn", 1, "<files>" },
|
|
|
|
{ 902, "priority-low", "Set the files' priorities as low", "pl", 1, "<files>" },
|
|
|
|
{ 'r', "remove", "Remove the current torrent(s)", "r", 0, NULL },
|
2009-02-09 19:36:29 +00:00
|
|
|
{ 930, "peers", "Set the current torrent(s)' maximum number of peers each", "pr", 1, "<max>" },
|
|
|
|
{ 931, "global-peers", "Set the global maximum number of peers", "gpr", 1, "<max>" },
|
2009-01-17 14:58:50 +00:00
|
|
|
{ 'R', "remove-and-delete", "Remove the current torrent(s) and delete local data", NULL, 0, NULL },
|
2009-02-13 18:23:56 +00:00
|
|
|
{ 950, "seedratio", "Let the current torrent(s) seed until a specific ratio", "sr", 1, "ratio" },
|
|
|
|
{ 951, "seedratio-default", "Let the current torrent(s) use the global seedratio settings", "srd", 0, NULL },
|
|
|
|
{ 952, "no-seedratio", "Let the current torrent(s) seed regardless of ratio", "SR", 0, NULL },
|
|
|
|
{ 953, "global-seedratio", "All torrents, unless overridden by a per-torrent setting, should seed until a specific ratio", "gsr", 1, "ratio" },
|
|
|
|
{ 954, "no-global-seedratio", "All torrents, unless overridden by a per-torrent setting, should seed regardless of ratio", "GSR", 0, NULL },
|
2009-01-17 14:58:50 +00:00
|
|
|
{ 's', "start", "Start the current torrent(s)", "s", 0, NULL },
|
|
|
|
{ 'S', "stop", "Stop the current torrent(s)", "S", 0, NULL },
|
|
|
|
{ 't', "torrent", "Set the current torrent(s)", "t", 1, "<torrent>" },
|
|
|
|
{ 'u', "uplimit", "Set the maximum global upload speed in KB/s", "u", 1, "<speed>" },
|
|
|
|
{ 'U', "no-uplimit", "Don't limit the global upload speed", "U", 0, NULL },
|
|
|
|
{ 'v', "verify", "Verify the current torrent(s)", "v", 0, NULL },
|
2009-01-23 19:36:34 +00:00
|
|
|
{ 'V', "version", "Show version number and exit", "V", 0, NULL },
|
2009-01-17 14:58:50 +00:00
|
|
|
{ 'w', "download-dir", "Set the default download folder", "w", 1, "<path>" },
|
|
|
|
{ 'x', "pex", "Enable peer exchange (PEX)", "x", 0, NULL },
|
|
|
|
{ 'X', "no-pex", "Disable peer exchange (PEX)", "X", 0, NULL },
|
2009-02-09 19:36:29 +00:00
|
|
|
{ 940, "peer-info", "List the current torrent(s)' peers", "pi", 0, NULL },
|
2009-01-17 14:58:50 +00:00
|
|
|
{ 0, NULL, NULL, NULL, 0, NULL }
|
2008-07-07 21:23:34 +00:00
|
|
|
};
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
|
|
|
showUsage( void )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_getopt_usage( MY_NAME, getUsage( ), opts );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static int
|
|
|
|
numarg( const char * arg )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char * end = NULL;
|
2008-05-18 16:44:30 +00:00
|
|
|
const long num = strtol( arg, &end, 10 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
if( *end )
|
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
fprintf( stderr, "Not a number: \"%s\"\n", arg );
|
|
|
|
showUsage( );
|
2009-05-18 16:05:56 +00:00
|
|
|
exit( EXIT_FAILURE );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
return num;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static char * reqs[256]; /* arbitrary max */
|
2008-09-23 19:11:04 +00:00
|
|
|
static int reqCount = 0;
|
|
|
|
static int debug = 0;
|
2008-06-05 18:16:59 +00:00
|
|
|
static char * auth = NULL;
|
2009-02-10 16:22:59 +00:00
|
|
|
static char * netrc = NULL;
|
2009-05-08 14:56:11 +00:00
|
|
|
static char * sessionId = NULL;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2009-02-25 18:06:57 +00:00
|
|
|
static char*
|
|
|
|
tr_getcwd( void )
|
|
|
|
{
|
|
|
|
char buf[2048];
|
|
|
|
*buf = '\0';
|
|
|
|
#ifdef WIN32
|
|
|
|
_getcwd( buf, sizeof( buf ) );
|
|
|
|
#else
|
|
|
|
getcwd( buf, sizeof( buf ) );
|
|
|
|
#endif
|
|
|
|
return tr_strdup( buf );
|
|
|
|
}
|
2008-11-15 17:39:54 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static char*
|
2008-10-14 03:03:29 +00:00
|
|
|
absolutify( const char * path )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-10-14 03:03:29 +00:00
|
|
|
char * buf;
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( *path == '/' )
|
2008-10-14 03:03:29 +00:00
|
|
|
buf = tr_strdup( path );
|
|
|
|
else {
|
2008-10-14 03:39:16 +00:00
|
|
|
char * cwd = tr_getcwd( );
|
2008-10-14 03:03:29 +00:00
|
|
|
buf = tr_buildPath( cwd, path, NULL );
|
2008-10-14 03:39:16 +00:00
|
|
|
tr_free( cwd );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-10-14 03:03:29 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
return buf;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-20 17:33:54 +00:00
|
|
|
static char*
|
|
|
|
getEncodedMetainfo( const char * filename )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t len = 0;
|
|
|
|
char * b64 = NULL;
|
2008-05-20 17:33:54 +00:00
|
|
|
uint8_t * buf = tr_loadFile( filename, &len );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-11 16:04:46 +00:00
|
|
|
if( buf )
|
|
|
|
{
|
|
|
|
b64 = tr_base64_encode( buf, len, NULL );
|
|
|
|
tr_free( buf );
|
|
|
|
}
|
2008-05-20 17:33:54 +00:00
|
|
|
return b64;
|
|
|
|
}
|
|
|
|
|
2008-07-07 21:23:34 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
addIdArg( tr_benc * args,
|
|
|
|
const char * id )
|
2008-07-07 21:23:34 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !*id )
|
|
|
|
{
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"No torrent specified! Please use the -t option first.\n" );
|
2008-07-07 21:23:34 +00:00
|
|
|
id = "-1"; /* no torrent will have this ID, so should be a no-op */
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
if( strcmp( id, "all" ) )
|
|
|
|
{
|
2009-05-08 17:24:16 +00:00
|
|
|
const char * pch;
|
|
|
|
tr_bool isList = strchr(id,',') || strchr(id,'-');
|
|
|
|
tr_bool isNum = TRUE;
|
|
|
|
for( pch=id; isNum && *pch; ++pch )
|
|
|
|
if( !isdigit( *pch ) )
|
|
|
|
isNum = FALSE;
|
|
|
|
if( isNum || isList )
|
|
|
|
tr_rpc_parse_list_str( tr_bencDictAdd( args, "ids" ), id, strlen( id ) );
|
|
|
|
else
|
|
|
|
tr_bencDictAddStr( args, "ids", id ); /* it's a torrent sha hash */
|
2008-07-07 21:23:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-17 20:41:06 +00:00
|
|
|
static void
|
|
|
|
addTime( tr_benc * args, const char * key, const char * arg )
|
|
|
|
{
|
|
|
|
int time;
|
|
|
|
tr_bool success = FALSE;
|
|
|
|
|
|
|
|
if( arg && ( strlen( arg ) == 4 ) )
|
|
|
|
{
|
|
|
|
const char hh[3] = { arg[0], arg[1], '\0' };
|
|
|
|
const char mm[3] = { arg[2], arg[3], '\0' };
|
|
|
|
const int hour = atoi( hh );
|
|
|
|
const int min = atoi( mm );
|
|
|
|
|
|
|
|
if( 0<=hour && hour<24 && 0<=min && min<60 )
|
|
|
|
{
|
|
|
|
time = min + ( hour * 60 );
|
|
|
|
success = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( success )
|
|
|
|
tr_bencDictAddInt( args, key, time );
|
|
|
|
else
|
|
|
|
fprintf( stderr, "Please specify the time of day in 'hhmm' format.\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
addDays( tr_benc * args, const char * key, const char * arg )
|
|
|
|
{
|
|
|
|
int days = 0;
|
|
|
|
|
|
|
|
if( arg )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int valueCount;
|
|
|
|
int * values = tr_parseNumberRange( arg, -1, &valueCount );
|
|
|
|
for( i=0; i<valueCount; ++i )
|
|
|
|
{
|
|
|
|
if ( values[i] < 0 || values[i] > 7 ) continue;
|
|
|
|
if ( values[i] == 7 ) values[i] = 0;
|
|
|
|
|
|
|
|
days |= 1 << values[i];
|
|
|
|
}
|
|
|
|
tr_free( values );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( days )
|
|
|
|
tr_bencDictAddInt( args, key, days );
|
|
|
|
else
|
|
|
|
fprintf( stderr, "Please specify the days of the week in '1-3,4,7' format.\n" );
|
|
|
|
}
|
|
|
|
|
2008-07-10 20:59:15 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
addFiles( tr_benc * args,
|
|
|
|
const char * key,
|
|
|
|
const char * arg )
|
2008-07-10 20:59:15 +00:00
|
|
|
{
|
|
|
|
tr_benc * files = tr_bencDictAddList( args, key, 100 );
|
|
|
|
|
|
|
|
if( !*arg )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "No files specified!\n" );
|
|
|
|
arg = "-1"; /* no file will have this index, so should be a no-op */
|
|
|
|
}
|
|
|
|
if( strcmp( arg, "all" ) )
|
|
|
|
{
|
2009-02-09 17:44:47 +00:00
|
|
|
int i;
|
|
|
|
int valueCount;
|
|
|
|
int * values = tr_parseNumberRange( arg, -1, &valueCount );
|
|
|
|
for( i=0; i<valueCount; ++i )
|
|
|
|
tr_bencListAddInt( files, values[i] );
|
|
|
|
tr_free( values );
|
2008-07-10 20:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-26 14:47:07 +00:00
|
|
|
#define TR_N_ELEMENTS( ary ) ( sizeof( ary ) / sizeof( *ary ) )
|
|
|
|
|
|
|
|
static const char * files_keys[] = {
|
2009-01-17 14:58:50 +00:00
|
|
|
"files",
|
|
|
|
"name",
|
|
|
|
"priorities",
|
|
|
|
"wanted"
|
2008-07-26 14:47:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char * details_keys[] = {
|
2009-01-17 14:58:50 +00:00
|
|
|
"activityDate",
|
|
|
|
"addedDate",
|
|
|
|
"announceResponse",
|
2008-09-23 19:11:04 +00:00
|
|
|
"announceURL",
|
2009-01-17 14:58:50 +00:00
|
|
|
"comment",
|
|
|
|
"corruptEver",
|
|
|
|
"creator",
|
|
|
|
"dateCreated",
|
|
|
|
"doneDate",
|
|
|
|
"downloadDir",
|
|
|
|
"downloadedEver",
|
2009-08-05 01:25:36 +00:00
|
|
|
"error",
|
2009-01-17 14:58:50 +00:00
|
|
|
"errorString",
|
|
|
|
"eta",
|
|
|
|
"hashString",
|
|
|
|
"haveUnchecked",
|
|
|
|
"haveValid",
|
|
|
|
"id",
|
|
|
|
"isPrivate",
|
|
|
|
"lastAnnounceTime",
|
|
|
|
"lastScrapeTime",
|
|
|
|
"leechers",
|
|
|
|
"leftUntilDone",
|
|
|
|
"name",
|
|
|
|
"nextAnnounceTime",
|
|
|
|
"nextScrapeTime",
|
|
|
|
"peersConnected",
|
|
|
|
"peersGettingFromUs",
|
|
|
|
"peersSendingToUs",
|
|
|
|
"pieceCount",
|
|
|
|
"pieceSize",
|
|
|
|
"rateDownload",
|
|
|
|
"rateUpload",
|
|
|
|
"recheckProgress",
|
|
|
|
"scrapeResponse",
|
|
|
|
"seeders",
|
|
|
|
"sizeWhenDone",
|
|
|
|
"startDate",
|
|
|
|
"status",
|
|
|
|
"timesCompleted",
|
|
|
|
"totalSize",
|
2008-09-23 19:11:04 +00:00
|
|
|
"uploadedEver",
|
2009-03-02 05:48:32 +00:00
|
|
|
"pieces",
|
2009-01-17 14:58:50 +00:00
|
|
|
"webseeds",
|
|
|
|
"webseedsSendingToUs"
|
2008-07-26 14:47:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char * list_keys[] = {
|
2009-02-23 03:26:01 +00:00
|
|
|
"error",
|
2009-02-18 02:24:12 +00:00
|
|
|
"errorString",
|
2008-11-30 17:03:45 +00:00
|
|
|
"eta",
|
|
|
|
"id",
|
2008-09-23 19:11:04 +00:00
|
|
|
"leftUntilDone",
|
|
|
|
"name",
|
2008-11-30 17:03:45 +00:00
|
|
|
"peersGettingFromUs",
|
|
|
|
"peersSendingToUs",
|
2008-09-23 19:11:04 +00:00
|
|
|
"rateDownload",
|
2008-11-30 17:03:45 +00:00
|
|
|
"rateUpload",
|
|
|
|
"sizeWhenDone",
|
|
|
|
"status",
|
|
|
|
"uploadRatio"
|
2008-07-26 14:47:07 +00:00
|
|
|
};
|
|
|
|
|
2009-05-18 16:05:56 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
readargs( int argc,
|
|
|
|
const char ** argv )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2009-05-18 16:05:56 +00:00
|
|
|
int c;
|
|
|
|
int addingTorrents = 0;
|
|
|
|
int status = EXIT_SUCCESS;
|
|
|
|
char id[4096];
|
2008-07-08 03:19:01 +00:00
|
|
|
const char * optarg;
|
2008-07-07 21:23:34 +00:00
|
|
|
|
|
|
|
*id = '\0';
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
while( ( c = tr_getopt( getUsage( ), argc, argv, opts, &optarg ) ) )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i, n;
|
|
|
|
int addArg = TRUE;
|
2008-07-26 14:47:07 +00:00
|
|
|
tr_benc top, *args, *fields;
|
2008-05-19 18:16:58 +00:00
|
|
|
tr_bencInitDict( &top, 3 );
|
2008-05-20 17:33:54 +00:00
|
|
|
args = tr_bencDictAddDict( &top, "arguments", 0 );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-07-07 21:23:34 +00:00
|
|
|
switch( c )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
case TR_OPT_UNK:
|
2008-09-23 19:11:04 +00:00
|
|
|
if( addingTorrents )
|
|
|
|
{
|
|
|
|
char * tmp = getEncodedMetainfo( optarg );
|
|
|
|
if( tmp )
|
|
|
|
{
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-add" );
|
|
|
|
tr_bencDictAddStr( args, "metainfo", tmp );
|
|
|
|
tr_free( tmp );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-add" );
|
|
|
|
tr_bencDictAddStr( args, "filename", optarg );
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Unknown option: %s\n", optarg );
|
|
|
|
addArg = FALSE;
|
2009-05-18 16:05:56 +00:00
|
|
|
status |= EXIT_FAILURE;
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
|
|
|
addingTorrents = 1;
|
|
|
|
addArg = FALSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
debug = 1;
|
|
|
|
addArg = FALSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-06-25 01:57:31 +00:00
|
|
|
tr_bencDictAddInt( args, TR_PREFS_KEY_DSPEED, numarg( optarg ) );
|
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_DSPEED_ENABLED, TRUE );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-06-25 01:57:31 +00:00
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_DSPEED_ENABLED, FALSE );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
|
2009-08-17 20:41:06 +00:00
|
|
|
case 970:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_ALT_SPEED_ENABLED, TRUE );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 971:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_ALT_SPEED_ENABLED, FALSE );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 972:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddInt( args, TR_PREFS_KEY_ALT_SPEED_DOWN, numarg( optarg ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 973:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddInt( args, TR_PREFS_KEY_ALT_SPEED_UP, numarg( optarg ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 974:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED, TRUE );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 975:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED, FALSE );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 976:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
addTime( args, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 977:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
addTime( args, TR_PREFS_KEY_ALT_SPEED_TIME_END, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 978:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
addDays( args, TR_PREFS_KEY_ALT_SPEED_TIME_DAY, optarg );
|
|
|
|
break;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
case 'f':
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-get" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_FILES );
|
|
|
|
addIdArg( args, id );
|
|
|
|
n = TR_N_ELEMENTS( files_keys );
|
|
|
|
fields = tr_bencDictAddList( args, "fields", n );
|
|
|
|
for( i = 0; i < n; ++i )
|
|
|
|
tr_bencListAddStr( fields, files_keys[i] );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'g':
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set" );
|
|
|
|
addIdArg( args, id );
|
|
|
|
addFiles( args, "files-wanted", optarg );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'G':
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set" );
|
|
|
|
addIdArg( args, id );
|
|
|
|
addFiles( args, "files-unwanted", optarg );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-get" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_DETAILS );
|
|
|
|
addIdArg( args, id );
|
|
|
|
n = TR_N_ELEMENTS( details_keys );
|
|
|
|
fields = tr_bencDictAddList( args, "fields", n );
|
|
|
|
for( i = 0; i < n; ++i )
|
|
|
|
tr_bencListAddStr( fields, details_keys[i] );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-get" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_LIST );
|
|
|
|
n = TR_N_ELEMENTS( list_keys );
|
|
|
|
fields = tr_bencDictAddList( args, "fields", n );
|
|
|
|
for( i = 0; i < n; ++i )
|
|
|
|
tr_bencListAddStr( fields, list_keys[i] );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-06-25 01:57:31 +00:00
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_PORT_FORWARDING, TRUE );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-06-25 01:57:31 +00:00
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_PORT_FORWARDING, FALSE );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
auth = tr_strdup( optarg );
|
|
|
|
addArg = FALSE;
|
|
|
|
break;
|
|
|
|
|
2009-02-10 16:22:59 +00:00
|
|
|
case 'N':
|
|
|
|
netrc = tr_strdup( optarg );
|
|
|
|
addArg = FALSE;
|
|
|
|
break;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
case 'p':
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-03-25 19:18:00 +00:00
|
|
|
tr_bencDictAddInt( args, TR_PREFS_KEY_PEER_PORT, numarg( optarg ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
|
2009-04-03 01:26:18 +00:00
|
|
|
case 'P':
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-04-03 04:29:27 +00:00
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, TRUE);
|
2009-04-03 01:26:18 +00:00
|
|
|
break;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
case 'r':
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-remove" );
|
|
|
|
addIdArg( args, id );
|
|
|
|
break;
|
|
|
|
|
2008-12-09 17:01:49 +00:00
|
|
|
case 'R':
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-remove" );
|
|
|
|
addIdArg( args, id );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( args, "delete-local-data", TRUE );
|
2008-12-09 17:01:49 +00:00
|
|
|
break;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
case 's':
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-start" );
|
|
|
|
addIdArg( args, id );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S':
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-stop" );
|
|
|
|
addIdArg( args, id );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
tr_strlcpy( id, optarg, sizeof( id ) );
|
|
|
|
addArg = FALSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-06-25 01:57:31 +00:00
|
|
|
tr_bencDictAddInt( args, TR_PREFS_KEY_USPEED, numarg( optarg ) );
|
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_USPEED_ENABLED, TRUE );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'U':
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-06-25 01:57:31 +00:00
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_USPEED_ENABLED, FALSE );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-verify" );
|
|
|
|
addIdArg( args, id );
|
|
|
|
break;
|
|
|
|
|
2009-01-23 19:36:34 +00:00
|
|
|
case 'V':
|
2009-02-25 18:06:57 +00:00
|
|
|
fprintf( stderr, "Transmission %s\n", LONG_VERSION_STRING );
|
|
|
|
exit( 0 );
|
2009-01-23 19:36:34 +00:00
|
|
|
break;
|
|
|
|
|
2008-10-14 03:03:29 +00:00
|
|
|
case 'w': {
|
|
|
|
char * path = absolutify( optarg );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-03-25 19:18:00 +00:00
|
|
|
tr_bencDictAddStr( args, TR_PREFS_KEY_DOWNLOAD_DIR, path );
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( path );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
2008-10-14 03:03:29 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-05-21 00:15:54 +00:00
|
|
|
case 'o':
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_DHT_ENABLED, TRUE );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'O':
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_DHT_ENABLED, FALSE );
|
|
|
|
break;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
case 'x':
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_PEX_ENABLED, TRUE );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'X':
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( args, TR_PREFS_KEY_PEX_ENABLED, FALSE );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 900:
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set" );
|
|
|
|
addIdArg( args, id );
|
|
|
|
addFiles( args, "priority-high", optarg );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 901:
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set" );
|
|
|
|
addIdArg( args, id );
|
|
|
|
addFiles( args, "priority-normal", optarg );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 902:
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set" );
|
|
|
|
addIdArg( args, id );
|
|
|
|
addFiles( args, "priority-low", optarg );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 910:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-03-25 19:18:00 +00:00
|
|
|
tr_bencDictAddStr( args, TR_PREFS_KEY_ENCRYPTION, "required" );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 911:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-03-25 19:18:00 +00:00
|
|
|
tr_bencDictAddStr( args, TR_PREFS_KEY_ENCRYPTION, "preferred" );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 912:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-03-25 19:18:00 +00:00
|
|
|
tr_bencDictAddStr( args, TR_PREFS_KEY_ENCRYPTION, "tolerated" );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
|
2009-01-23 19:36:34 +00:00
|
|
|
case 920:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-get" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_SESSION );
|
|
|
|
break;
|
|
|
|
|
2009-08-13 03:54:54 +00:00
|
|
|
case 921:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-stats" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_STATS );
|
|
|
|
break;
|
|
|
|
|
2009-02-09 19:36:29 +00:00
|
|
|
case 930:
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set" );
|
|
|
|
addIdArg( args, id );
|
|
|
|
tr_bencDictAddInt( args, "peer-limit", atoi(optarg) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 931:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-03-25 19:18:00 +00:00
|
|
|
tr_bencDictAddInt( args, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, atoi(optarg) );
|
2009-02-09 19:36:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 940:
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-get" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_PEERS );
|
|
|
|
addIdArg( args, id );
|
|
|
|
fields = tr_bencDictAddList( args, "fields", 1 );
|
|
|
|
tr_bencListAddStr( fields, "peers" );
|
|
|
|
break;
|
|
|
|
|
2009-02-13 18:23:56 +00:00
|
|
|
case 950:
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set" );
|
2009-04-14 14:24:57 +00:00
|
|
|
tr_bencDictAddReal( args, "seedRatioLimit", atof(optarg) );
|
|
|
|
tr_bencDictAddInt( args, "seedRatioMode", TR_RATIOLIMIT_SINGLE );
|
2009-02-13 18:23:56 +00:00
|
|
|
addIdArg( args, id );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 951:
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set" );
|
2009-04-14 14:24:57 +00:00
|
|
|
tr_bencDictAddInt( args, "seedRatioMode", TR_RATIOLIMIT_GLOBAL );
|
2009-02-13 18:23:56 +00:00
|
|
|
addIdArg( args, id );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 952:
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set" );
|
2009-04-14 14:24:57 +00:00
|
|
|
tr_bencDictAddInt( args, "seedRatioMode", TR_RATIOLIMIT_UNLIMITED );
|
2009-02-13 18:23:56 +00:00
|
|
|
addIdArg( args, id );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 953:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-04-14 14:24:57 +00:00
|
|
|
tr_bencDictAddReal( args, "seedRatioLimit", atof(optarg) );
|
|
|
|
tr_bencDictAddBool( args, "seedRatioLimited", TRUE );
|
2009-02-13 18:23:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 954:
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
2009-04-14 14:24:57 +00:00
|
|
|
tr_bencDictAddBool( args, "seedRatioLimited", FALSE );
|
2009-02-13 18:23:56 +00:00
|
|
|
break;
|
|
|
|
|
2009-05-13 15:54:04 +00:00
|
|
|
case 960:
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set-location" );
|
|
|
|
tr_bencDictAddStr( args, "location", optarg );
|
|
|
|
tr_bencDictAddBool( args, "move", TRUE );
|
|
|
|
addIdArg( args, id );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 961:
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set-location" );
|
|
|
|
tr_bencDictAddStr( args, "location", optarg );
|
|
|
|
tr_bencDictAddBool( args, "move", FALSE );
|
|
|
|
addIdArg( args, id );
|
|
|
|
break;
|
|
|
|
|
2008-07-21 19:24:35 +00:00
|
|
|
case TR_OPT_ERR:
|
2008-09-23 19:11:04 +00:00
|
|
|
fprintf( stderr, "invalid option\n" );
|
|
|
|
showUsage( );
|
2009-05-18 16:05:56 +00:00
|
|
|
status |= EXIT_FAILURE;
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf( stderr, "got opt [%d]\n", (int)c );
|
|
|
|
showUsage( );
|
|
|
|
break;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-27 21:47:07 +00:00
|
|
|
if( addArg )
|
2008-12-30 22:07:39 +00:00
|
|
|
{
|
2009-06-02 01:48:48 +00:00
|
|
|
reqs[reqCount++] = tr_bencToStr( &top, TR_FMT_JSON_LEAN, NULL );
|
2008-12-30 22:07:39 +00:00
|
|
|
}
|
|
|
|
|
2008-05-19 18:16:58 +00:00
|
|
|
tr_bencFree( &top );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
2009-05-18 16:05:56 +00:00
|
|
|
|
|
|
|
return status;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
/* [host:port] or [host] or [port] */
|
|
|
|
static void
|
2009-03-29 23:05:32 +00:00
|
|
|
getHostAndPort( int * argc, char ** argv, char ** host, int * port )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
if( *argv[1] != '-' )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
2008-05-18 16:44:30 +00:00
|
|
|
const char * s = argv[1];
|
|
|
|
const char * delim = strchr( s, ':' );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( delim ) /* user passed in both host and port */
|
|
|
|
{
|
|
|
|
*host = tr_strndup( s, delim - s );
|
|
|
|
*port = atoi( delim + 1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char * end;
|
2008-05-18 16:44:30 +00:00
|
|
|
const int i = strtol( s, &end, 10 );
|
|
|
|
if( !*end ) /* user passed in a port */
|
|
|
|
*port = i;
|
|
|
|
else /* user passed in a host */
|
|
|
|
*host = tr_strdup( s );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
*argc -= 1;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 1; i < *argc; ++i )
|
|
|
|
argv[i] = argv[i + 1];
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static size_t
|
2008-09-23 19:11:04 +00:00
|
|
|
writeFunc( void * ptr,
|
|
|
|
size_t size,
|
|
|
|
size_t nmemb,
|
|
|
|
void * buf )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
const size_t byteCount = size * nmemb;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
evbuffer_add( buf, ptr, byteCount );
|
|
|
|
return byteCount;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2009-08-22 15:08:07 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
etaToString( char * buf, size_t buflen, int64_t eta )
|
|
|
|
{
|
|
|
|
if( eta < 0 )
|
|
|
|
tr_snprintf( buf, buflen, "Unknown" );
|
|
|
|
else if( eta < 60 )
|
|
|
|
tr_snprintf( buf, buflen, "%" PRId64 "sec", eta );
|
|
|
|
else if( eta < ( 60 * 60 ) )
|
|
|
|
tr_snprintf( buf, buflen, "%" PRId64 " min", eta / 60 );
|
|
|
|
else if( eta < ( 60 * 60 * 24 ) )
|
|
|
|
tr_snprintf( buf, buflen, "%" PRId64 " hrs", eta / ( 60 * 60 ) );
|
|
|
|
else
|
|
|
|
tr_snprintf( buf, buflen, "%" PRId64 " days", eta / ( 60 * 60 * 24 ) );
|
|
|
|
}
|
|
|
|
|
2009-08-13 03:54:54 +00:00
|
|
|
static char*
|
|
|
|
tr_strltime( char * buf, int seconds, size_t buflen )
|
2008-06-21 15:49:18 +00:00
|
|
|
{
|
2009-08-13 03:54:54 +00:00
|
|
|
int days, hours, minutes;
|
|
|
|
char d[128], h[128], m[128], s[128];
|
|
|
|
|
|
|
|
if( seconds < 0 )
|
|
|
|
seconds = 0;
|
|
|
|
|
|
|
|
days = seconds / 86400;
|
|
|
|
hours = ( seconds % 86400 ) / 3600;
|
|
|
|
minutes = ( seconds % 3600 ) / 60;
|
|
|
|
seconds = ( seconds % 3600 ) % 60;
|
|
|
|
|
2009-08-15 16:43:25 +00:00
|
|
|
tr_snprintf( d, sizeof( d ), "%'d day%s", days, days==1?"":"s" );
|
|
|
|
tr_snprintf( h, sizeof( h ), "%'d hour%s", hours, hours==1?"":"s" );
|
|
|
|
tr_snprintf( m, sizeof( m ), "%'d minute%s", minutes, minutes==1?"":"s" );
|
|
|
|
tr_snprintf( s, sizeof( s ), "%'d second%s", seconds, seconds==1?"":"s" );
|
2009-08-13 03:54:54 +00:00
|
|
|
|
|
|
|
if( days )
|
|
|
|
{
|
|
|
|
if( days >= 4 || !hours )
|
|
|
|
tr_strlcpy( buf, d, buflen );
|
|
|
|
else
|
|
|
|
tr_snprintf( buf, buflen, "%s, %s", d, h );
|
|
|
|
}
|
|
|
|
else if( hours )
|
|
|
|
{
|
|
|
|
if( hours >= 4 || !minutes )
|
|
|
|
tr_strlcpy( buf, h, buflen );
|
|
|
|
else
|
|
|
|
tr_snprintf( buf, buflen, "%s, %s", h, m );
|
|
|
|
}
|
|
|
|
else if( minutes )
|
|
|
|
{
|
|
|
|
if( minutes >= 4 || !seconds )
|
|
|
|
tr_strlcpy( buf, m, buflen );
|
|
|
|
else
|
|
|
|
tr_snprintf( buf, buflen, "%s, %s", m, s );
|
|
|
|
}
|
|
|
|
else tr_strlcpy( buf, s, buflen );
|
|
|
|
|
|
|
|
return buf;
|
2008-06-21 15:49:18 +00:00
|
|
|
}
|
|
|
|
|
2008-07-07 03:38:22 +00:00
|
|
|
#define KILOBYTE_FACTOR 1024.0
|
2008-09-23 19:11:04 +00:00
|
|
|
#define MEGABYTE_FACTOR ( 1024.0 * 1024.0 )
|
|
|
|
#define GIGABYTE_FACTOR ( 1024.0 * 1024.0 * 1024.0 )
|
2008-07-07 03:38:22 +00:00
|
|
|
|
2008-11-30 17:03:45 +00:00
|
|
|
static char*
|
|
|
|
strlratio2( char * buf, double ratio, size_t buflen )
|
|
|
|
{
|
2009-03-04 16:16:02 +00:00
|
|
|
return tr_strratio( buf, buflen, ratio, "Inf" );
|
2008-11-30 17:03:45 +00:00
|
|
|
}
|
|
|
|
|
2008-07-07 05:53:15 +00:00
|
|
|
static char*
|
2008-09-23 19:11:04 +00:00
|
|
|
strlratio( char * buf,
|
|
|
|
double numerator,
|
|
|
|
double denominator,
|
|
|
|
size_t buflen )
|
2008-07-07 05:53:15 +00:00
|
|
|
{
|
2008-11-30 17:03:45 +00:00
|
|
|
double ratio;
|
|
|
|
|
2008-07-07 05:53:15 +00:00
|
|
|
if( denominator )
|
2008-11-30 17:03:45 +00:00
|
|
|
ratio = numerator / denominator;
|
2008-07-07 05:53:15 +00:00
|
|
|
else if( numerator )
|
2008-11-30 17:03:45 +00:00
|
|
|
ratio = TR_RATIO_INF;
|
2008-07-07 05:53:15 +00:00
|
|
|
else
|
2008-11-30 17:03:45 +00:00
|
|
|
ratio = TR_RATIO_NA;
|
|
|
|
|
|
|
|
return strlratio2( buf, ratio, buflen );
|
2008-07-07 05:53:15 +00:00
|
|
|
}
|
|
|
|
|
2008-07-07 03:38:22 +00:00
|
|
|
static char*
|
2009-01-30 23:29:18 +00:00
|
|
|
strlsize( char * buf, int64_t size, size_t buflen )
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
|
|
|
if( !size )
|
|
|
|
tr_strlcpy( buf, "None", buflen );
|
|
|
|
else if( size < (int64_t)KILOBYTE_FACTOR )
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_snprintf( buf, buflen, "%'" PRId64 " bytes", (int64_t)size );
|
|
|
|
else
|
|
|
|
{
|
2008-07-07 03:38:22 +00:00
|
|
|
double displayed_size;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( size < (int64_t)MEGABYTE_FACTOR )
|
|
|
|
{
|
2008-07-07 03:38:22 +00:00
|
|
|
displayed_size = (double) size / KILOBYTE_FACTOR;
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( buf, buflen, "%'.1f KB", displayed_size );
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else if( size < (int64_t)GIGABYTE_FACTOR )
|
|
|
|
{
|
2008-07-07 03:38:22 +00:00
|
|
|
displayed_size = (double) size / MEGABYTE_FACTOR;
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( buf, buflen, "%'.1f MB", displayed_size );
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-07 03:38:22 +00:00
|
|
|
displayed_size = (double) size / GIGABYTE_FACTOR;
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( buf, buflen, "%'.1f GB", displayed_size );
|
2008-07-07 03:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2008-10-31 23:56:14 +00:00
|
|
|
static char*
|
|
|
|
getStatusString( tr_benc * t, char * buf, size_t buflen )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-11-01 06:22:14 +00:00
|
|
|
int64_t status;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-11-01 06:22:14 +00:00
|
|
|
if( !tr_bencDictFindInt( t, "status", &status ) )
|
2008-10-31 23:56:14 +00:00
|
|
|
{
|
2008-11-01 06:22:14 +00:00
|
|
|
*buf = '\0';
|
|
|
|
}
|
|
|
|
else switch( status )
|
|
|
|
{
|
|
|
|
case TR_STATUS_STOPPED:
|
2008-10-31 23:56:14 +00:00
|
|
|
tr_strlcpy( buf, "Stopped", buflen );
|
2008-11-01 06:22:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_CHECK_WAIT:
|
|
|
|
case TR_STATUS_CHECK: {
|
|
|
|
const char * str = status == TR_STATUS_CHECK_WAIT
|
|
|
|
? "Will Verify"
|
|
|
|
: "Verifying";
|
|
|
|
double percent;
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindReal( t, "recheckProgress", &percent ) )
|
2009-04-22 21:00:52 +00:00
|
|
|
tr_snprintf( buf, buflen, "%s (%.0f%%)", str, floor(percent*100.0) );
|
2008-10-31 23:56:14 +00:00
|
|
|
else
|
2008-11-01 06:22:14 +00:00
|
|
|
tr_strlcpy( buf, str, buflen );
|
|
|
|
|
|
|
|
break;
|
2008-10-31 23:56:14 +00:00
|
|
|
}
|
2008-11-01 06:22:14 +00:00
|
|
|
|
|
|
|
case TR_STATUS_DOWNLOAD:
|
|
|
|
case TR_STATUS_SEED: {
|
2009-08-17 14:29:39 +00:00
|
|
|
int64_t fromUs = 0;
|
|
|
|
int64_t toUs = 0;
|
|
|
|
tr_bencDictFindInt( t, "peersGettingFromUs", &fromUs );
|
|
|
|
tr_bencDictFindInt( t, "peersSendingToUs", &toUs );
|
|
|
|
if( fromUs && toUs )
|
|
|
|
tr_strlcpy( buf, "Up & Down", buflen );
|
|
|
|
else if( toUs )
|
|
|
|
tr_strlcpy( buf, "Downloading", buflen );
|
|
|
|
else if( fromUs ) {
|
|
|
|
int64_t leftUntilDone = 0;
|
|
|
|
tr_bencDictFindInt( t, "leftUntilDone", &leftUntilDone );
|
|
|
|
if( leftUntilDone > 0 )
|
|
|
|
tr_strlcpy( buf, "Uploading", buflen );
|
|
|
|
else
|
|
|
|
tr_strlcpy( buf, "Seeding", buflen );
|
|
|
|
} else {
|
|
|
|
tr_strlcpy( buf, "Idle", buflen );
|
2009-02-18 02:24:12 +00:00
|
|
|
}
|
2008-11-01 06:22:14 +00:00
|
|
|
break;
|
2008-10-31 23:56:14 +00:00
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-10-31 23:56:14 +00:00
|
|
|
return buf;
|
2008-07-10 17:19:43 +00:00
|
|
|
}
|
|
|
|
|
2009-01-10 14:23:58 +00:00
|
|
|
static const char*
|
|
|
|
getTrackerDateStr( const time_t t, tr_bool isStopped )
|
|
|
|
{
|
|
|
|
const char * str;
|
|
|
|
switch( t ) {
|
|
|
|
case 0: str = isStopped ? "None (Stopped)\n" : "None\n"; break;
|
|
|
|
case 1: str = "In Progress\n"; break;
|
|
|
|
default: str = ctime( &t ); break;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2009-01-23 19:36:34 +00:00
|
|
|
static void
|
|
|
|
printSession( tr_benc * top )
|
|
|
|
{
|
|
|
|
tr_benc *args;
|
|
|
|
if( ( tr_bencDictFindDict( top, "arguments", &args ) ) )
|
|
|
|
{
|
|
|
|
const char * str;
|
|
|
|
int64_t i;
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bool boolVal;
|
2009-01-23 19:36:34 +00:00
|
|
|
|
|
|
|
printf( "VERSION\n" );
|
|
|
|
if( tr_bencDictFindStr( args, "version", &str ) )
|
|
|
|
printf( " Daemon version: %s\n", str );
|
|
|
|
if( tr_bencDictFindInt( args, "rpc-version", &i ) )
|
|
|
|
printf( " RPC version: %" PRId64 "\n", i );
|
|
|
|
if( tr_bencDictFindInt( args, "rpc-version-minimum", &i ) )
|
|
|
|
printf( " RPC minimum version: %" PRId64 "\n", i );
|
|
|
|
printf( "\n" );
|
|
|
|
|
|
|
|
printf( "TRANSFER\n" );
|
2009-06-25 01:57:31 +00:00
|
|
|
if( tr_bencDictFindStr( args, TR_PREFS_KEY_DOWNLOAD_DIR, &str ) )
|
2009-01-23 19:36:34 +00:00
|
|
|
printf( " Download directory: %s\n", str );
|
2009-06-25 01:57:31 +00:00
|
|
|
if( tr_bencDictFindInt( args, TR_PREFS_KEY_PEER_PORT, &i ) )
|
2009-01-23 19:36:34 +00:00
|
|
|
printf( " Listenport: %" PRId64 "\n", i );
|
2009-06-25 01:57:31 +00:00
|
|
|
if( tr_bencDictFindBool( args, TR_PREFS_KEY_PORT_FORWARDING, &boolVal ) )
|
2009-03-29 23:05:32 +00:00
|
|
|
printf( " Portforwarding enabled: %s\n", ( boolVal ? "Yes" : "No" ) );
|
2009-06-25 01:57:31 +00:00
|
|
|
if( tr_bencDictFindBool( args, TR_PREFS_KEY_PEX_ENABLED, &boolVal ) )
|
2009-03-29 23:05:32 +00:00
|
|
|
printf( " Peer exchange allowed: %s\n", ( boolVal ? "Yes" : "No" ) );
|
2009-06-25 01:57:31 +00:00
|
|
|
if( tr_bencDictFindStr( args, TR_PREFS_KEY_ENCRYPTION, &str ) )
|
2009-01-23 19:36:34 +00:00
|
|
|
printf( " Encryption: %s\n", str );
|
|
|
|
printf( "\n" );
|
|
|
|
|
2009-06-03 13:07:55 +00:00
|
|
|
{
|
|
|
|
tr_bool altEnabled, altTimeEnabled, upEnabled, downEnabled;
|
|
|
|
int64_t altDown, altUp, altBegin, altEnd, altDay, upLimit, downLimit, peerLimit;
|
|
|
|
|
2009-06-25 01:57:31 +00:00
|
|
|
if( tr_bencDictFindInt ( args, TR_PREFS_KEY_ALT_SPEED_DOWN, &altDown ) &&
|
|
|
|
tr_bencDictFindBool( args, TR_PREFS_KEY_ALT_SPEED_ENABLED, &altEnabled ) &&
|
|
|
|
tr_bencDictFindInt ( args, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN, &altBegin ) &&
|
|
|
|
tr_bencDictFindBool( args, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED, &altTimeEnabled ) &&
|
|
|
|
tr_bencDictFindInt ( args, TR_PREFS_KEY_ALT_SPEED_TIME_END, &altEnd ) &&
|
|
|
|
tr_bencDictFindInt ( args, TR_PREFS_KEY_ALT_SPEED_TIME_DAY, &altDay ) &&
|
|
|
|
tr_bencDictFindInt ( args, TR_PREFS_KEY_ALT_SPEED_UP, &altUp ) &&
|
|
|
|
tr_bencDictFindInt ( args, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, &peerLimit ) &&
|
|
|
|
tr_bencDictFindInt ( args, TR_PREFS_KEY_DSPEED, &downLimit ) &&
|
|
|
|
tr_bencDictFindBool( args, TR_PREFS_KEY_DSPEED_ENABLED, &downEnabled ) &&
|
|
|
|
tr_bencDictFindInt ( args, TR_PREFS_KEY_USPEED, &upLimit ) &&
|
|
|
|
tr_bencDictFindBool( args, TR_PREFS_KEY_USPEED_ENABLED, &upEnabled ) )
|
2009-06-03 13:07:55 +00:00
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
printf( "LIMITS\n" );
|
2009-06-03 13:24:02 +00:00
|
|
|
printf( " Peer limit: %" PRId64 "\n", peerLimit );
|
2009-06-03 13:07:55 +00:00
|
|
|
|
|
|
|
if( altEnabled )
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%"PRId64" KB/s", altUp );
|
|
|
|
else if( upEnabled )
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%"PRId64" KB/s", upLimit );
|
|
|
|
else
|
|
|
|
tr_strlcpy( buf, "Unlimited", sizeof( buf ) );
|
|
|
|
printf( " Upload speed limit: %s (%s limit: %"PRId64" KB/s; %s turtle limit: %"PRId64" KB/s)\n",
|
|
|
|
buf,
|
|
|
|
(upEnabled?"Enabled":"Disabled"), upLimit,
|
|
|
|
(altEnabled?"Enabled":"Disabled"), altUp );
|
|
|
|
|
|
|
|
if( altEnabled )
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%"PRId64" KB/s", altDown );
|
|
|
|
else if( downEnabled )
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%"PRId64" KB/s", downLimit );
|
|
|
|
else
|
2009-06-23 01:07:09 +00:00
|
|
|
tr_strlcpy( buf, "Unlimited", sizeof( buf ) );
|
2009-06-03 13:07:55 +00:00
|
|
|
printf( " Download speed limit: %s (%s limit: %"PRId64" KB/s; %s turtle limit: %"PRId64" KB/s)\n",
|
|
|
|
buf,
|
|
|
|
(downEnabled?"Enabled":"Disabled"), downLimit,
|
|
|
|
(altEnabled?"Enabled":"Disabled"), altDown );
|
|
|
|
|
|
|
|
if( altTimeEnabled ) {
|
|
|
|
printf( " Turtle schedule: %02d:%02d - %02d:%02d ",
|
|
|
|
(int)(altBegin/60), (int)(altBegin%60),
|
|
|
|
(int)(altEnd/60), (int)(altEnd%60) );
|
|
|
|
if( altDay & TR_SCHED_SUN ) printf( "Sun " );
|
|
|
|
if( altDay & TR_SCHED_MON ) printf( "Mon " );
|
|
|
|
if( altDay & TR_SCHED_TUES ) printf( "Tue " );
|
|
|
|
if( altDay & TR_SCHED_WED ) printf( "Wed " );
|
|
|
|
if( altDay & TR_SCHED_THURS ) printf( "Thu " );
|
|
|
|
if( altDay & TR_SCHED_FRI ) printf( "Fri " );
|
|
|
|
if( altDay & TR_SCHED_SAT ) printf( "Sat " );
|
|
|
|
printf( "\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-23 19:36:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-13 03:54:54 +00:00
|
|
|
static void
|
|
|
|
printSessionStats( tr_benc * top )
|
|
|
|
{
|
|
|
|
tr_benc *args, *d;
|
|
|
|
if( ( tr_bencDictFindDict( top, "arguments", &args ) ) )
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
int64_t up, down, secs, sessions;
|
|
|
|
|
|
|
|
if( tr_bencDictFindDict( args, "current-stats", &d )
|
|
|
|
&& tr_bencDictFindInt( d, "uploadedBytes", &up )
|
|
|
|
&& tr_bencDictFindInt( d, "downloadedBytes", &down )
|
|
|
|
&& tr_bencDictFindInt( d, "secondsActive", &secs ) )
|
|
|
|
{
|
|
|
|
printf( "\nCURRENT SESSION\n" );
|
|
|
|
printf( " Uploaded: %s\n", strlsize( buf, up, sizeof( buf ) ) );
|
|
|
|
printf( " Downloaded: %s\n", strlsize( buf, down, sizeof( buf ) ) );
|
|
|
|
printf( " Ratio: %s\n", strlratio( buf, up, down, sizeof( buf ) ) );
|
|
|
|
printf( " Duration: %s\n", tr_strltime( buf, secs, sizeof( buf ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindDict( args, "cumulative-stats", &d )
|
|
|
|
&& tr_bencDictFindInt( d, "sessionCount", &sessions )
|
|
|
|
&& tr_bencDictFindInt( d, "uploadedBytes", &up )
|
|
|
|
&& tr_bencDictFindInt( d, "downloadedBytes", &down )
|
|
|
|
&& tr_bencDictFindInt( d, "secondsActive", &secs ) )
|
|
|
|
{
|
|
|
|
printf( "\nTOTAL\n" );
|
|
|
|
printf( " Started %lu times\n", (unsigned long)sessions );
|
|
|
|
printf( " Uploaded: %s\n", strlsize( buf, up, sizeof( buf ) ) );
|
|
|
|
printf( " Downloaded: %s\n", strlsize( buf, down, sizeof( buf ) ) );
|
|
|
|
printf( " Ratio: %s\n", strlratio( buf, up, down, sizeof( buf ) ) );
|
|
|
|
printf( " Duration: %s\n", tr_strltime( buf, secs, sizeof( buf ) ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-07 05:53:15 +00:00
|
|
|
static void
|
|
|
|
printDetails( tr_benc * top )
|
|
|
|
{
|
|
|
|
tr_benc *args, *torrents;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( tr_bencDictFindDict( top, "arguments", &args ) )
|
|
|
|
&& ( tr_bencDictFindList( args, "torrents", &torrents ) ) )
|
2008-07-07 05:53:15 +00:00
|
|
|
{
|
|
|
|
int ti, tCount;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( ti = 0, tCount = tr_bencListSize( torrents ); ti < tCount;
|
|
|
|
++ti )
|
2008-07-07 05:53:15 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * t = tr_bencListChild( torrents, ti );
|
|
|
|
tr_benc * l;
|
2009-03-02 05:48:32 +00:00
|
|
|
const uint8_t * raw;
|
|
|
|
size_t rawlen;
|
2008-07-07 05:53:15 +00:00
|
|
|
const char * str;
|
2008-09-23 19:11:04 +00:00
|
|
|
char buf[512];
|
|
|
|
char buf2[512];
|
|
|
|
int64_t i, j, k;
|
2009-01-10 14:23:58 +00:00
|
|
|
tr_bool isStopped;
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bool boolVal;
|
2009-01-10 14:23:58 +00:00
|
|
|
|
|
|
|
isStopped = tr_bencDictFindInt( t, "status", &i ) && (i==TR_STATUS_STOPPED);
|
2008-07-07 05:53:15 +00:00
|
|
|
|
|
|
|
printf( "NAME\n" );
|
|
|
|
if( tr_bencDictFindInt( t, "id", &i ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
printf( " Id: %" PRId64 "\n", i );
|
2008-07-07 05:53:15 +00:00
|
|
|
if( tr_bencDictFindStr( t, "name", &str ) )
|
|
|
|
printf( " Name: %s\n", str );
|
|
|
|
if( tr_bencDictFindStr( t, "hashString", &str ) )
|
|
|
|
printf( " Hash: %s\n", str );
|
|
|
|
printf( "\n" );
|
|
|
|
|
|
|
|
printf( "TRANSFER\n" );
|
2008-10-31 23:56:14 +00:00
|
|
|
getStatusString( t, buf, sizeof( buf ) );
|
|
|
|
printf( " State: %s\n", buf );
|
2008-07-10 17:19:43 +00:00
|
|
|
|
2009-01-17 14:58:50 +00:00
|
|
|
if( tr_bencDictFindStr( t, "downloadDir", &str ) )
|
|
|
|
printf( " Location: %s\n", str );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindInt( t, "sizeWhenDone", &i )
|
|
|
|
&& tr_bencDictFindInt( t, "leftUntilDone", &j ) )
|
2008-07-10 17:19:43 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
strlratio( buf, 100.0 * ( i - j ), i, sizeof( buf ) );
|
2008-07-10 19:22:49 +00:00
|
|
|
printf( " Percent Done: %s%%\n", buf );
|
2008-07-10 17:19:43 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindInt( t, "eta", &i ) )
|
2009-08-13 03:54:54 +00:00
|
|
|
printf( " ETA: %s\n", tr_strltime( buf, i, sizeof( buf ) ) );
|
2008-07-07 05:53:15 +00:00
|
|
|
if( tr_bencDictFindInt( t, "rateDownload", &i ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
printf( " Download Speed: %.1f KB/s\n", i / 1024.0 );
|
2008-07-07 05:53:15 +00:00
|
|
|
if( tr_bencDictFindInt( t, "rateUpload", &i ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
printf( " Upload Speed: %.1f KB/s\n", i / 1024.0 );
|
|
|
|
if( tr_bencDictFindInt( t, "haveUnchecked", &i )
|
|
|
|
&& tr_bencDictFindInt( t, "haveValid", &j ) )
|
2008-07-07 05:53:15 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
strlsize( buf, i + j, sizeof( buf ) );
|
2008-07-07 05:53:15 +00:00
|
|
|
strlsize( buf2, j, sizeof( buf2 ) );
|
|
|
|
printf( " Have: %s (%s verified)\n", buf, buf2 );
|
|
|
|
}
|
2008-07-10 17:19:43 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindInt( t, "sizeWhenDone", &i )
|
|
|
|
&& tr_bencDictFindInt( t, "totalSize", &j ) )
|
2008-07-07 05:53:15 +00:00
|
|
|
{
|
|
|
|
strlsize( buf, j, sizeof( buf ) );
|
|
|
|
strlsize( buf2, i, sizeof( buf2 ) );
|
|
|
|
printf( " Total size: %s (%s wanted)\n", buf, buf2 );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindInt( t, "downloadedEver", &i )
|
|
|
|
&& tr_bencDictFindInt( t, "uploadedEver", &j ) )
|
|
|
|
{
|
2008-07-07 05:53:15 +00:00
|
|
|
strlsize( buf, i, sizeof( buf ) );
|
|
|
|
printf( " Downloaded: %s\n", buf );
|
|
|
|
strlsize( buf, j, sizeof( buf ) );
|
|
|
|
printf( " Uploaded: %s\n", buf );
|
2008-07-21 18:44:54 +00:00
|
|
|
strlratio( buf, j, i, sizeof( buf ) );
|
2008-07-07 05:53:15 +00:00
|
|
|
printf( " Ratio: %s\n", buf );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindInt( t, "corruptEver", &i ) )
|
|
|
|
{
|
2008-07-07 05:53:15 +00:00
|
|
|
strlsize( buf, i, sizeof( buf ) );
|
|
|
|
printf( " Corrupt DL: %s\n", buf );
|
|
|
|
}
|
2009-08-05 01:25:36 +00:00
|
|
|
if( tr_bencDictFindStr( t, "errorString", &str ) && str && *str &&
|
|
|
|
tr_bencDictFindInt( t, "error", &i ) && i )
|
|
|
|
{
|
|
|
|
switch( i ) {
|
2009-08-06 07:04:19 +00:00
|
|
|
case TR_STAT_TRACKER_WARNING: printf( " Tracker gave a warning: %s\n", str ); break;
|
|
|
|
case TR_STAT_TRACKER_ERROR: printf( " Tracker gave an error: %s\n", str ); break;
|
2009-08-05 01:25:36 +00:00
|
|
|
case TR_STAT_LOCAL_ERROR: printf( " Error: %s\n", str ); break;
|
|
|
|
default: break; /* no error */
|
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindInt( t, "peersConnected", &i )
|
|
|
|
&& tr_bencDictFindInt( t, "peersGettingFromUs", &j )
|
|
|
|
&& tr_bencDictFindInt( t, "peersSendingToUs", &k ) )
|
2008-07-27 14:29:43 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
printf(
|
|
|
|
" Peers: "
|
|
|
|
"connected to %" PRId64 ", "
|
|
|
|
"uploading to %" PRId64
|
|
|
|
", "
|
|
|
|
"downloading from %"
|
|
|
|
PRId64 "\n",
|
|
|
|
i, j, k );
|
2008-07-27 14:29:43 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
if( tr_bencDictFindList( t, "webseeds", &l )
|
|
|
|
&& tr_bencDictFindInt( t, "webseedsSendingToUs", &i ) )
|
2008-07-27 14:29:43 +00:00
|
|
|
{
|
|
|
|
const int64_t n = tr_bencListSize( l );
|
|
|
|
if( n > 0 )
|
2008-09-23 19:11:04 +00:00
|
|
|
printf(
|
|
|
|
" Web Seeds: downloading from %" PRId64 " of %"
|
|
|
|
PRId64
|
|
|
|
" web seeds\n", i, n );
|
2008-07-27 14:29:43 +00:00
|
|
|
}
|
2008-07-07 05:53:15 +00:00
|
|
|
printf( "\n" );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-07-07 05:53:15 +00:00
|
|
|
printf( "HISTORY\n" );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindInt( t, "addedDate", &i ) && i )
|
|
|
|
{
|
2008-07-07 05:53:15 +00:00
|
|
|
const time_t tt = i;
|
|
|
|
printf( " Date added: %s", ctime( &tt ) );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindInt( t, "doneDate", &i ) && i )
|
|
|
|
{
|
2008-07-07 05:53:15 +00:00
|
|
|
const time_t tt = i;
|
|
|
|
printf( " Date finished: %s", ctime( &tt ) );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindInt( t, "startDate", &i ) && i )
|
|
|
|
{
|
2008-07-07 05:53:15 +00:00
|
|
|
const time_t tt = i;
|
|
|
|
printf( " Date started: %s", ctime( &tt ) );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindInt( t, "activityDate", &i ) && i )
|
|
|
|
{
|
2008-07-07 05:53:15 +00:00
|
|
|
const time_t tt = i;
|
|
|
|
printf( " Latest activity: %s", ctime( &tt ) );
|
|
|
|
}
|
|
|
|
printf( "\n" );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-07-07 05:53:15 +00:00
|
|
|
printf( "TRACKER\n" );
|
2009-01-10 14:23:58 +00:00
|
|
|
if( tr_bencDictFindInt( t, "lastAnnounceTime", &i ) )
|
|
|
|
printf( " Latest announce: %s", getTrackerDateStr( (time_t)i, isStopped ) );
|
2008-07-07 05:53:15 +00:00
|
|
|
if( tr_bencDictFindStr( t, "announceURL", &str ) )
|
|
|
|
printf( " Announce URL: %s\n", str );
|
2009-01-10 14:23:58 +00:00
|
|
|
if( tr_bencDictFindStr( t, "announceResponse", &str ) && str && *str )
|
2008-07-07 05:53:15 +00:00
|
|
|
printf( " Announce response: %s\n", str );
|
2009-01-10 14:23:58 +00:00
|
|
|
if( tr_bencDictFindInt( t, "nextAnnounceTime", &i ) )
|
|
|
|
printf( " Next announce: %s", getTrackerDateStr( (time_t)i, isStopped ) );
|
|
|
|
if( tr_bencDictFindInt( t, "lastScrapeTime", &i ) )
|
|
|
|
printf( " Latest scrape: %s", getTrackerDateStr( (time_t)i, isStopped ) );
|
2008-07-07 05:53:15 +00:00
|
|
|
if( tr_bencDictFindStr( t, "scrapeResponse", &str ) )
|
|
|
|
printf( " Scrape response: %s\n", str );
|
2009-01-10 14:23:58 +00:00
|
|
|
if( tr_bencDictFindInt( t, "nextScrapeTime", &i ) )
|
|
|
|
printf( " Next scrape: %s", getTrackerDateStr( (time_t)i, isStopped ) );
|
|
|
|
if( tr_bencDictFindInt( t, "seeders", &i ) && tr_bencDictFindInt( t, "leechers", &j ) )
|
|
|
|
printf( " Tracker knows of %" PRId64 " seeders and %" PRId64 " leechers\n", i, j );
|
2008-07-07 05:53:15 +00:00
|
|
|
if( tr_bencDictFindInt( t, "timesCompleted", &i ) )
|
2009-01-10 14:23:58 +00:00
|
|
|
printf( " Tracker has seen %" PRId64 " clients complete this torrent\n", i );
|
2008-07-07 05:53:15 +00:00
|
|
|
printf( "\n" );
|
|
|
|
|
|
|
|
printf( "ORIGINS\n" );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindInt( t, "dateCreated", &i ) && i )
|
|
|
|
{
|
2008-07-07 05:53:15 +00:00
|
|
|
const time_t tt = i;
|
|
|
|
printf( " Date created: %s", ctime( &tt ) );
|
|
|
|
}
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindBool( t, "isPrivate", &boolVal ) )
|
|
|
|
printf( " Public torrent: %s\n", ( boolVal ? "No" : "Yes" ) );
|
2008-07-07 05:53:15 +00:00
|
|
|
if( tr_bencDictFindStr( t, "comment", &str ) && str && *str )
|
|
|
|
printf( " Comment: %s\n", str );
|
|
|
|
if( tr_bencDictFindStr( t, "creator", &str ) && str && *str )
|
|
|
|
printf( " Creator: %s\n", str );
|
|
|
|
if( tr_bencDictFindInt( t, "pieceCount", &i ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
printf( " Piece Count: %" PRId64 "\n", i );
|
2008-07-07 05:53:15 +00:00
|
|
|
if( tr_bencDictFindInt( t, "pieceSize", &i ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
printf( " Piece Size: %" PRId64 "\n", i );
|
2009-03-02 05:48:32 +00:00
|
|
|
printf( "\n" );
|
|
|
|
|
|
|
|
printf("PIECES\n ");
|
|
|
|
if( tr_bencDictFindRaw( t, "pieces", &raw, &rawlen ) && tr_bencDictFindInt( t, "pieceCount", &j ) ) {
|
|
|
|
int len;
|
|
|
|
char * str = tr_base64_decode( raw, rawlen, &len );
|
|
|
|
for( i=k=0; k<len; ++k ) {
|
|
|
|
int e;
|
|
|
|
for( e=0; i<j && e<8; ++e, ++i )
|
|
|
|
printf( str[k] & (1<<(7-e)) ? "1" : "0" );
|
|
|
|
printf( " " );
|
|
|
|
if( !(i%64) )
|
|
|
|
printf( "\n " );
|
|
|
|
}
|
|
|
|
tr_free( str );
|
|
|
|
}
|
|
|
|
printf( "\n" );
|
2008-07-07 05:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-07 03:38:22 +00:00
|
|
|
static void
|
|
|
|
printFileList( tr_benc * top )
|
|
|
|
{
|
|
|
|
tr_benc *args, *torrents;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( tr_bencDictFindDict( top, "arguments", &args ) )
|
|
|
|
&& ( tr_bencDictFindList( args, "torrents", &torrents ) ) )
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
|
|
|
int i, in;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0, in = tr_bencListSize( torrents ); i < in; ++i )
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * d = tr_bencListChild( torrents, i );
|
|
|
|
tr_benc * files, *priorities, *wanteds;
|
2008-07-07 03:38:22 +00:00
|
|
|
const char * name;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindStr( d, "name", &name )
|
|
|
|
&& tr_bencDictFindList( d, "files", &files )
|
|
|
|
&& tr_bencDictFindList( d, "priorities", &priorities )
|
|
|
|
&& tr_bencDictFindList( d, "wanted", &wanteds ) )
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int j = 0, jn = tr_bencListSize( files );
|
2008-07-07 03:38:22 +00:00
|
|
|
printf( "%s (%d files):\n", name, jn );
|
2008-09-23 19:11:04 +00:00
|
|
|
printf( "%3s %4s %8s %3s %9s %s\n", "#", "Done",
|
|
|
|
"Priority", "Get", "Size",
|
|
|
|
"Name" );
|
|
|
|
for( j = 0, jn = tr_bencListSize( files ); j < jn; ++j )
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int64_t have;
|
|
|
|
int64_t length;
|
|
|
|
int64_t priority;
|
|
|
|
int64_t wanted;
|
2008-07-07 03:38:22 +00:00
|
|
|
const char * filename;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * file = tr_bencListChild( files, j );
|
|
|
|
if( tr_bencDictFindInt( file, "length", &length )
|
|
|
|
&& tr_bencDictFindStr( file, "name", &filename )
|
|
|
|
&& tr_bencDictFindInt( file, "bytesCompleted", &have )
|
|
|
|
&& tr_bencGetInt( tr_bencListChild( priorities,
|
|
|
|
j ), &priority )
|
|
|
|
&& tr_bencGetInt( tr_bencListChild( wanteds,
|
|
|
|
j ), &wanted ) )
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char sizestr[64];
|
|
|
|
double percent = (double)have / length;
|
2008-07-07 03:38:22 +00:00
|
|
|
const char * pristr;
|
2008-08-11 19:05:02 +00:00
|
|
|
strlsize( sizestr, length, sizeof( sizestr ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
switch( priority )
|
|
|
|
{
|
|
|
|
case TR_PRI_LOW:
|
|
|
|
pristr = "Low"; break;
|
|
|
|
|
|
|
|
case TR_PRI_HIGH:
|
|
|
|
pristr = "High"; break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
pristr = "Normal"; break;
|
2008-07-07 03:38:22 +00:00
|
|
|
}
|
2008-07-22 12:20:17 +00:00
|
|
|
printf( "%3d: %3.0f%% %-8s %-3s %9s %s\n",
|
2009-01-20 16:16:11 +00:00
|
|
|
j,
|
2009-04-22 21:00:52 +00:00
|
|
|
floor( 100.0 * percent ),
|
2008-07-22 12:20:17 +00:00
|
|
|
pristr,
|
2008-09-23 19:11:04 +00:00
|
|
|
( wanted ? "Yes" : "No" ),
|
2008-07-22 12:20:17 +00:00
|
|
|
sizestr,
|
|
|
|
filename );
|
2008-07-07 03:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-22 17:59:31 +00:00
|
|
|
static void
|
2008-11-04 17:04:41 +00:00
|
|
|
printPeersImpl( tr_benc * peers )
|
2008-08-22 17:59:31 +00:00
|
|
|
{
|
2008-11-04 17:04:41 +00:00
|
|
|
int i, n;
|
2009-01-17 16:44:16 +00:00
|
|
|
printf( "%-20s %-12s %-5s %-6s %-6s %s\n",
|
|
|
|
"Address", "Flags", "Done", "Down", "Up", "Client" );
|
2008-11-04 17:04:41 +00:00
|
|
|
for( i = 0, n = tr_bencListSize( peers ); i < n; ++i )
|
|
|
|
{
|
2009-01-17 16:44:16 +00:00
|
|
|
double progress;
|
2008-11-04 17:04:41 +00:00
|
|
|
const char * address, * client, * flagstr;
|
2009-01-17 16:44:16 +00:00
|
|
|
int64_t rateToClient, rateToPeer;
|
|
|
|
tr_benc * d = tr_bencListChild( peers, i );
|
|
|
|
|
2008-11-04 17:04:41 +00:00
|
|
|
if( tr_bencDictFindStr( d, "address", &address )
|
|
|
|
&& tr_bencDictFindStr( d, "clientName", &client )
|
2009-03-29 23:05:32 +00:00
|
|
|
&& tr_bencDictFindReal( d, "progress", &progress )
|
2008-11-04 17:04:41 +00:00
|
|
|
&& tr_bencDictFindStr( d, "flagStr", &flagstr )
|
|
|
|
&& tr_bencDictFindInt( d, "rateToClient", &rateToClient )
|
|
|
|
&& tr_bencDictFindInt( d, "rateToPeer", &rateToPeer ) )
|
|
|
|
{
|
2009-01-17 16:44:16 +00:00
|
|
|
printf( "%-20s %-12s %-5.1f %6.1f %6.1f %s\n",
|
|
|
|
address, flagstr, (progress*100.0),
|
2008-11-04 17:04:41 +00:00
|
|
|
rateToClient / 1024.0,
|
|
|
|
rateToPeer / 1024.0,
|
|
|
|
client );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-22 17:59:31 +00:00
|
|
|
|
2008-11-04 17:04:41 +00:00
|
|
|
static void
|
|
|
|
printPeers( tr_benc * top )
|
|
|
|
{
|
|
|
|
tr_benc *args, *torrents;
|
|
|
|
|
|
|
|
if( tr_bencDictFindDict( top, "arguments", &args )
|
|
|
|
&& tr_bencDictFindList( args, "torrents", &torrents ) )
|
2008-08-22 17:59:31 +00:00
|
|
|
{
|
|
|
|
int i, n;
|
2008-11-04 17:04:41 +00:00
|
|
|
for( i=0, n=tr_bencListSize( torrents ); i<n; ++i )
|
2008-08-22 17:59:31 +00:00
|
|
|
{
|
2008-11-04 17:04:41 +00:00
|
|
|
tr_benc * peers;
|
|
|
|
tr_benc * torrent = tr_bencListChild( torrents, i );
|
|
|
|
if( tr_bencDictFindList( torrent, "peers", &peers ) ) {
|
|
|
|
printPeersImpl( peers );
|
|
|
|
if( i+1<n )
|
|
|
|
printf( "\n" );
|
2008-08-22 17:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-07 03:38:22 +00:00
|
|
|
static void
|
|
|
|
printTorrentList( tr_benc * top )
|
|
|
|
{
|
|
|
|
tr_benc *args, *list;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( tr_bencDictFindDict( top, "arguments", &args ) )
|
|
|
|
&& ( tr_bencDictFindList( args, "torrents", &list ) ) )
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
|
|
|
int i, n;
|
2009-02-25 18:06:57 +00:00
|
|
|
int64_t total_up = 0, total_down = 0, total_size = 0;
|
|
|
|
char haveStr[32];
|
|
|
|
|
2009-02-23 03:26:01 +00:00
|
|
|
printf( "%-4s %-4s %9s %-8s %6s %6s %-5s %-11s %s\n",
|
2009-01-30 23:29:18 +00:00
|
|
|
"ID", "Done", "Have", "ETA", "Up", "Down", "Ratio", "Status",
|
2008-09-23 19:11:04 +00:00
|
|
|
"Name" );
|
2009-02-25 18:06:57 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0, n = tr_bencListSize( list ); i < n; ++i )
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
2008-11-30 17:03:45 +00:00
|
|
|
int64_t id, eta, status, up, down;
|
|
|
|
int64_t sizeWhenDone, leftUntilDone;
|
|
|
|
double ratio;
|
|
|
|
const char * name;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * d = tr_bencListChild( list, i );
|
2008-11-30 17:03:45 +00:00
|
|
|
if( tr_bencDictFindInt( d, "eta", &eta )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& tr_bencDictFindInt( d, "id", &id )
|
|
|
|
&& tr_bencDictFindInt( d, "leftUntilDone", &leftUntilDone )
|
|
|
|
&& tr_bencDictFindStr( d, "name", &name )
|
|
|
|
&& tr_bencDictFindInt( d, "rateDownload", &down )
|
|
|
|
&& tr_bencDictFindInt( d, "rateUpload", &up )
|
|
|
|
&& tr_bencDictFindInt( d, "sizeWhenDone", &sizeWhenDone )
|
|
|
|
&& tr_bencDictFindInt( d, "status", &status )
|
2009-03-29 23:05:32 +00:00
|
|
|
&& tr_bencDictFindReal( d, "uploadRatio", &ratio ) )
|
2008-07-07 03:38:22 +00:00
|
|
|
{
|
|
|
|
char etaStr[16];
|
2008-10-31 23:56:14 +00:00
|
|
|
char statusStr[64];
|
2008-11-27 17:28:18 +00:00
|
|
|
char ratioStr[32];
|
2009-02-09 17:44:47 +00:00
|
|
|
char doneStr[8];
|
2009-02-23 03:26:01 +00:00
|
|
|
int64_t error;
|
|
|
|
char errorMark;
|
2009-02-09 17:44:47 +00:00
|
|
|
|
|
|
|
if( sizeWhenDone )
|
|
|
|
tr_snprintf( doneStr, sizeof( doneStr ), "%d%%", (int)( 100.0 * ( sizeWhenDone - leftUntilDone ) / sizeWhenDone ) );
|
|
|
|
else
|
|
|
|
tr_strlcpy( doneStr, "n/a", sizeof( doneStr ) );
|
2009-01-30 23:29:18 +00:00
|
|
|
|
|
|
|
strlsize( haveStr, sizeWhenDone - leftUntilDone, sizeof( haveStr ) );
|
2008-11-27 17:28:18 +00:00
|
|
|
|
2009-08-22 14:52:24 +00:00
|
|
|
if( leftUntilDone || eta != -1 )
|
2009-08-22 15:08:07 +00:00
|
|
|
etaToString( etaStr, sizeof( etaStr ), eta );
|
2008-07-07 03:38:22 +00:00
|
|
|
else
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( etaStr, sizeof( etaStr ), "Done" );
|
2009-02-23 03:26:01 +00:00
|
|
|
if( tr_bencDictFindInt( d, "error", &error ) && error )
|
|
|
|
errorMark = '*';
|
|
|
|
else
|
|
|
|
errorMark = ' ';
|
2008-09-23 19:11:04 +00:00
|
|
|
printf(
|
2009-02-23 03:26:01 +00:00
|
|
|
"%4d%c %4s %9s %-8s %6.1f %6.1f %5s %-11s %s\n",
|
|
|
|
(int)id, errorMark,
|
2009-02-09 17:44:47 +00:00
|
|
|
doneStr,
|
2009-01-30 23:29:18 +00:00
|
|
|
haveStr,
|
2008-09-23 19:11:04 +00:00
|
|
|
etaStr,
|
|
|
|
up / 1024.0,
|
|
|
|
down / 1024.0,
|
2008-11-30 17:03:45 +00:00
|
|
|
strlratio2( ratioStr, ratio, sizeof( ratioStr ) ),
|
2008-10-31 23:56:14 +00:00
|
|
|
getStatusString( d, statusStr, sizeof( statusStr ) ),
|
2008-09-23 19:11:04 +00:00
|
|
|
name );
|
2009-02-25 18:06:57 +00:00
|
|
|
|
|
|
|
total_up += up;
|
2009-02-27 15:59:09 +00:00
|
|
|
total_down += down;
|
2009-02-25 18:06:57 +00:00
|
|
|
total_size += sizeWhenDone - leftUntilDone;
|
2008-07-07 03:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-25 18:06:57 +00:00
|
|
|
|
2009-03-02 05:48:32 +00:00
|
|
|
printf( "Sum: %9s %6.1f %6.1f\n",
|
2009-02-25 18:06:57 +00:00
|
|
|
strlsize( haveStr, total_size, sizeof( haveStr ) ),
|
|
|
|
total_up / 1024.0,
|
|
|
|
total_down / 1024.0 );
|
2008-07-07 03:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 16:05:56 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
processResponse( const char * host,
|
|
|
|
int port,
|
|
|
|
const void * response,
|
|
|
|
size_t len )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_benc top;
|
2009-05-18 16:05:56 +00:00
|
|
|
int status = EXIT_SUCCESS;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-27 21:47:07 +00:00
|
|
|
if( debug )
|
2009-05-16 05:51:52 +00:00
|
|
|
fprintf( stderr, "got response (len %d):\n--------\n%*.*s\n--------\n",
|
|
|
|
(int)len, (int)len, (int)len, (const char*) response );
|
2008-05-27 21:47:07 +00:00
|
|
|
|
2009-06-01 18:57:37 +00:00
|
|
|
if( tr_jsonParse( NULL, response, len, &top, NULL ) )
|
2009-05-18 16:05:56 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_nerr( MY_NAME, "Unable to parse response \"%*.*s\"", (int)len,
|
|
|
|
(int)len, (char*)response );
|
2009-05-18 16:05:56 +00:00
|
|
|
status |= EXIT_FAILURE;
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
else
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int64_t tag = -1;
|
2008-05-18 16:44:30 +00:00
|
|
|
const char * str;
|
|
|
|
tr_bencDictFindInt( &top, "tag", &tag );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
switch( tag )
|
|
|
|
{
|
2009-01-23 19:36:34 +00:00
|
|
|
case TAG_SESSION:
|
|
|
|
printSession( &top ); break;
|
|
|
|
|
2009-08-13 03:54:54 +00:00
|
|
|
case TAG_STATS:
|
|
|
|
printSessionStats( &top ); break;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
case TAG_FILES:
|
|
|
|
printFileList( &top ); break;
|
|
|
|
|
|
|
|
case TAG_DETAILS:
|
|
|
|
printDetails( &top ); break;
|
|
|
|
|
|
|
|
case TAG_LIST:
|
|
|
|
printTorrentList( &top ); break;
|
|
|
|
|
|
|
|
case TAG_PEERS:
|
2008-11-04 17:04:41 +00:00
|
|
|
printPeers( &top ); break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
default:
|
2009-05-18 16:05:56 +00:00
|
|
|
if( !tr_bencDictFindStr( &top, "result", &str ) )
|
|
|
|
status |= EXIT_FAILURE;
|
|
|
|
else {
|
2008-09-23 19:11:04 +00:00
|
|
|
printf( "%s:%d responded: \"%s\"\n", host, port, str );
|
2009-05-18 16:05:56 +00:00
|
|
|
if( strcmp( str, "success") )
|
|
|
|
status |= EXIT_FAILURE;
|
|
|
|
}
|
2008-07-07 05:53:15 +00:00
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_bencFree( &top );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
2009-05-18 16:05:56 +00:00
|
|
|
|
|
|
|
return status;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2009-05-08 14:56:11 +00:00
|
|
|
/* look for a session id in the header in case the server gives back a 409 */
|
|
|
|
static size_t
|
|
|
|
parseResponseHeader( void *ptr, size_t size, size_t nmemb, void * stream UNUSED )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2009-05-08 14:56:11 +00:00
|
|
|
const char * line = ptr;
|
|
|
|
const size_t line_len = size * nmemb;
|
|
|
|
const char * key = TR_RPC_SESSION_ID_HEADER ": ";
|
|
|
|
const size_t key_len = strlen( key );
|
2009-05-08 02:37:46 +00:00
|
|
|
|
2009-05-08 14:56:11 +00:00
|
|
|
if( ( line_len >= key_len ) && !memcmp( line, key, key_len ) )
|
|
|
|
{
|
|
|
|
const char * begin = line + key_len;
|
|
|
|
const char * end = begin;
|
|
|
|
while( !isspace( *end ) )
|
|
|
|
++end;
|
|
|
|
tr_free( sessionId );
|
|
|
|
sessionId = tr_strndup( begin, end-begin );
|
|
|
|
}
|
|
|
|
|
|
|
|
return line_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURL*
|
|
|
|
tr_curl_easy_init( struct evbuffer * writebuf )
|
|
|
|
{
|
|
|
|
CURL * curl = curl_easy_init( );
|
2009-02-10 16:22:59 +00:00
|
|
|
curl_easy_setopt( curl, CURLOPT_USERAGENT, MY_NAME "/" LONG_VERSION_STRING );
|
2008-05-19 00:12:31 +00:00
|
|
|
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, writeFunc );
|
2009-05-08 14:56:11 +00:00
|
|
|
curl_easy_setopt( curl, CURLOPT_WRITEDATA, writebuf );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_HEADERFUNCTION, parseResponseHeader );
|
2008-05-19 18:16:58 +00:00
|
|
|
curl_easy_setopt( curl, CURLOPT_POST, 1 );
|
2009-02-10 16:22:59 +00:00
|
|
|
curl_easy_setopt( curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY );
|
2009-03-10 00:10:20 +00:00
|
|
|
curl_easy_setopt( curl, CURLOPT_TIMEOUT, 60L );
|
2009-05-08 14:56:11 +00:00
|
|
|
curl_easy_setopt( curl, CURLOPT_VERBOSE, debug );
|
2009-06-09 15:45:09 +00:00
|
|
|
curl_easy_setopt( curl, CURLOPT_ENCODING, "" ); /* "" tells curl to fill in the blanks with what it was compiled to support */
|
2009-02-10 16:22:59 +00:00
|
|
|
if( netrc )
|
|
|
|
curl_easy_setopt( curl, CURLOPT_NETRC_FILE, netrc );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( auth )
|
2008-06-05 18:16:59 +00:00
|
|
|
curl_easy_setopt( curl, CURLOPT_USERPWD, auth );
|
2009-05-08 14:56:11 +00:00
|
|
|
if( sessionId ) {
|
|
|
|
char * h = tr_strdup_printf( "%s: %s", TR_RPC_SESSION_ID_HEADER, sessionId );
|
|
|
|
struct curl_slist * custom_headers = curl_slist_append( NULL, h );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_HTTPHEADER, custom_headers );
|
|
|
|
/* fixme: leaks */
|
|
|
|
}
|
|
|
|
return curl;
|
|
|
|
}
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2009-05-18 16:05:56 +00:00
|
|
|
static int
|
2009-05-08 14:56:11 +00:00
|
|
|
processRequests( const char * host,
|
|
|
|
int port,
|
|
|
|
const char ** reqs,
|
|
|
|
int reqCount )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
CURL * curl = NULL;
|
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
|
|
|
char * url = tr_strdup_printf( "http://%s:%d/transmission/rpc", host, port );
|
2009-05-18 16:05:56 +00:00
|
|
|
int status = EXIT_SUCCESS;
|
2009-05-08 14:56:11 +00:00
|
|
|
|
|
|
|
for( i=0; i<reqCount; ++i )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
|
|
|
CURLcode res;
|
2009-05-08 14:56:11 +00:00
|
|
|
evbuffer_drain( buf, EVBUFFER_LENGTH( buf ) );
|
|
|
|
|
|
|
|
if( curl == NULL )
|
|
|
|
{
|
|
|
|
curl = tr_curl_easy_init( buf );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_URL, url );
|
|
|
|
}
|
|
|
|
|
2008-05-19 18:16:58 +00:00
|
|
|
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, reqs[i] );
|
2009-05-08 14:56:11 +00:00
|
|
|
|
2008-05-24 18:22:16 +00:00
|
|
|
if( debug )
|
2008-11-23 15:38:44 +00:00
|
|
|
fprintf( stderr, "posting:\n--------\n%s\n--------\n", reqs[i] );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( res = curl_easy_perform( curl ) ) )
|
2009-05-18 16:05:56 +00:00
|
|
|
{
|
2009-05-08 14:56:11 +00:00
|
|
|
tr_nerr( MY_NAME, "(%s:%d) %s", host, port, curl_easy_strerror( res ) );
|
2009-05-18 16:05:56 +00:00
|
|
|
status |= EXIT_FAILURE;
|
|
|
|
}
|
2009-05-08 14:56:11 +00:00
|
|
|
else {
|
|
|
|
long response;
|
|
|
|
curl_easy_getinfo( curl, CURLINFO_RESPONSE_CODE, &response );
|
|
|
|
switch( response ) {
|
|
|
|
case 200:
|
2009-05-18 16:05:56 +00:00
|
|
|
status |= processResponse( host, port, EVBUFFER_DATA(buf), EVBUFFER_LENGTH(buf) );
|
2009-05-08 14:56:11 +00:00
|
|
|
break;
|
|
|
|
case 409:
|
|
|
|
/* session id failed. our curl header func has already
|
|
|
|
* pulled the new session id from this response's headers,
|
|
|
|
* build a new CURL* and try again */
|
|
|
|
curl_easy_cleanup( curl );
|
|
|
|
curl = NULL;
|
|
|
|
--i;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf( stderr, "Unexpected response: %s\n", (char*)EVBUFFER_DATA(buf) );
|
2009-05-18 16:05:56 +00:00
|
|
|
status |= EXIT_FAILURE;
|
2009-05-08 14:56:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
2008-05-19 18:16:58 +00:00
|
|
|
tr_free( url );
|
2008-05-18 16:44:30 +00:00
|
|
|
evbuffer_free( buf );
|
2009-05-08 14:56:11 +00:00
|
|
|
if( curl != NULL )
|
|
|
|
curl_easy_cleanup( curl );
|
2009-05-18 16:05:56 +00:00
|
|
|
return status;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
main( int argc,
|
|
|
|
char ** argv )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
int port = DEFAULT_PORT;
|
2008-05-18 16:44:30 +00:00
|
|
|
char * host = NULL;
|
2009-05-18 16:05:56 +00:00
|
|
|
int exit_status = EXIT_SUCCESS;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2009-05-18 16:05:56 +00:00
|
|
|
if( argc < 2 ) {
|
2008-05-18 16:44:30 +00:00
|
|
|
showUsage( );
|
2009-05-18 16:05:56 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
getHostAndPort( &argc, argv, &host, &port );
|
|
|
|
if( host == NULL )
|
|
|
|
host = tr_strdup( DEFAULT_HOST );
|
|
|
|
|
2009-05-18 16:05:56 +00:00
|
|
|
exit_status |= readargs( argc, (const char**)argv );
|
2008-05-18 16:44:30 +00:00
|
|
|
if( reqCount )
|
2009-05-18 16:05:56 +00:00
|
|
|
exit_status = processRequests( host, port, (const char**)reqs, reqCount );
|
|
|
|
else {
|
2008-05-18 16:44:30 +00:00
|
|
|
showUsage( );
|
2009-05-18 16:05:56 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2009-05-18 16:05:56 +00:00
|
|
|
for( i=0; i<reqCount; ++i )
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_free( reqs[i] );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-19 18:16:58 +00:00
|
|
|
tr_free( host );
|
2009-05-18 16:05:56 +00:00
|
|
|
return exit_status;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|