2008-05-18 16:44:30 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.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)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2007-04-18 16:39:10 +00:00
|
|
|
*
|
2008-05-18 16:44:30 +00:00
|
|
|
* $Id:$
|
|
|
|
*/
|
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
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h> /* getcwd */
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <third-party/libevent/event.h>
|
|
|
|
#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>
|
|
|
|
#include <libtransmission/rpc.h>
|
|
|
|
#include <libtransmission/json.h>
|
|
|
|
#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"
|
|
|
|
#define DEFAULT_PORT TR_DEFAULT_RPC_PORT
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
enum { TAG_LIST };
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
|
|
|
showUsage( void )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
puts( "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"
|
|
|
|
"Options:\n"
|
|
|
|
" -a --add <torrent> Add a torrent\n"
|
|
|
|
" -c --encryption required Require encryption for all peers\n"
|
|
|
|
" -c --encryption preferred Prefer peers to use encryption\n"
|
|
|
|
" -c --encryption tolerated Prefer peers to use plaintext\n"
|
|
|
|
" -d --download-limit <int> Max download rate in KiB/s\n"
|
|
|
|
" -D --download-unlimited No download rate limit\n"
|
|
|
|
" -e --enable-pex Enable peer exchange\n"
|
|
|
|
" -E --disable-pex Disable peer exchange\n"
|
|
|
|
" -f --folder <path> Folder to set for new torrents\n"
|
|
|
|
" -g --debug Print debugging information\n"
|
|
|
|
" -h --help Display this message and exit\n"
|
|
|
|
" -l --list Long list of all torrent and status\n"
|
|
|
|
" -m --port-mapping Automatic port mapping via NAT-PMP or UPnP\n"
|
|
|
|
" -M --no-port-mapping Disable automatic port mapping\n"
|
|
|
|
" -p --port <int> Port to listen for incoming peers\n"
|
|
|
|
" -r --remove <int> Remove the torrent with the given ID\n"
|
|
|
|
" -r --remove all Remove all torrents\n"
|
|
|
|
" -s --start <int> Start the torrent with the given ID\n"
|
|
|
|
" -s --start all Start all stopped torrents\n"
|
|
|
|
" -S --stop <int> Stop the torrent with the given ID\n"
|
|
|
|
" -S --stop all Stop all running torrents\n"
|
|
|
|
" -u --upload-limit <int> Max upload rate in KiB/s\n"
|
|
|
|
" -U --upload-unlimited No upload rate limit\n"
|
|
|
|
" -v --verify <id> Verify the torrent's local data\n" );
|
|
|
|
exit( 0 );
|
|
|
|
}
|
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-05-18 16:44:30 +00:00
|
|
|
char * end = NULL;
|
|
|
|
const long num = strtol( arg, &end, 10 );
|
|
|
|
if( *end ) {
|
|
|
|
fprintf( stderr, "Not a number: \"%s\"\n", arg );
|
|
|
|
showUsage( );
|
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 */
|
|
|
|
static int reqCount = 0;
|
|
|
|
static int debug = 0;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static char*
|
|
|
|
absolutify( char * buf, size_t len, const char * path )
|
|
|
|
{
|
|
|
|
if( *path == '/' )
|
|
|
|
tr_strlcpy( buf, path, len );
|
|
|
|
else {
|
|
|
|
char cwd[MAX_PATH_LENGTH];
|
|
|
|
getcwd( cwd, sizeof( cwd ) );
|
|
|
|
tr_buildPath( buf, len, cwd, path, NULL );
|
|
|
|
}
|
|
|
|
return buf;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
|
|
|
readargs( int argc, char ** argv )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
int opt;
|
|
|
|
char optstr[] = "a:c:d:DeEf:ghlmMp:r:s:S:u:Uv:";
|
|
|
|
|
|
|
|
const struct option longopts[] =
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
|
|
|
{ "add", required_argument, NULL, 'a' },
|
2007-10-26 03:43:27 +00:00
|
|
|
{ "encryption", required_argument, NULL, 'c' },
|
2007-04-18 16:39:10 +00:00
|
|
|
{ "download-limit", required_argument, NULL, 'd' },
|
|
|
|
{ "download-unlimited", no_argument, NULL, 'D' },
|
|
|
|
{ "enable-pex", no_argument, NULL, 'e' },
|
|
|
|
{ "disable-pex", no_argument, NULL, 'E' },
|
|
|
|
{ "folder", required_argument, NULL, 'f' },
|
2008-05-18 16:44:30 +00:00
|
|
|
{ "debug", no_argument, NULL, 'g' },
|
2007-04-18 16:39:10 +00:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "list", no_argument, NULL, 'l' },
|
|
|
|
{ "port-mapping", no_argument, NULL, 'm' },
|
|
|
|
{ "no-port-mapping", no_argument, NULL, 'M' },
|
|
|
|
{ "port", required_argument, NULL, 'p' },
|
|
|
|
{ "remove", required_argument, NULL, 'r' },
|
|
|
|
{ "start", required_argument, NULL, 's' },
|
|
|
|
{ "stop", required_argument, NULL, 'S' },
|
|
|
|
{ "upload-limit", required_argument, NULL, 'u' },
|
|
|
|
{ "upload-unlimited", no_argument, NULL, 'U' },
|
2008-02-27 17:38:39 +00:00
|
|
|
{ "verify", required_argument, NULL, 'v' },
|
2007-04-18 16:39:10 +00:00
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
while((( opt = getopt_long( argc, argv, optstr, longopts, NULL ))) != -1 )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
char * req = NULL;
|
|
|
|
char buf[MAX_PATH_LENGTH];
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
switch( opt )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
case 'g': debug = 1; break;
|
|
|
|
case 'h': showUsage( ); break;
|
|
|
|
case 'a': req = tr_strdup_printf( "method=torrent-add&filename=%s", optarg ); break;
|
|
|
|
case 'c': req = tr_strdup_printf( "method=session-set&encryption=%s", optarg ); break;
|
|
|
|
case 'd': req = tr_strdup_printf( "method=session-set&speed-limit-down=%d&speed-limit-down-enabled=1", numarg(optarg) ); break;
|
|
|
|
case 'D': req = tr_strdup( "method=session-set&speed-limit-down-enabled=0" ); break;
|
|
|
|
case 'u': req = tr_strdup_printf( "method=session-set&speed-limit-up=%d&speed-limit-up-enabled:1", numarg(optarg) ); break;
|
|
|
|
case 'U': req = tr_strdup( "method=session-set&speed-limit-up-enabled=0" ); break;
|
|
|
|
case 'e': req = tr_strdup( "method=session-set&pex-allowed=1" ); break;
|
|
|
|
case 'E': req = tr_strdup( "method=session-set&pex-allowed=0" ); break;
|
|
|
|
case 'f': req = tr_strdup_printf( "method=session-set&download-dir=%s", absolutify(buf,sizeof(buf),optarg)); break;
|
|
|
|
case 'l': req = tr_strdup_printf( "method=torrent-list&tag=%d", TAG_LIST ); break;
|
|
|
|
case 'm': req = tr_strdup( "method=session-set&port-forwarding-enabled=1" ); break;
|
|
|
|
case 'M': req = tr_strdup( "method=session-set&port-forwarding-enabled=0" ); break;
|
|
|
|
case 'p': req = tr_strdup_printf( "method=session-set&port=%d", numarg( optarg ) ); break;
|
|
|
|
case 'r': req = strcmp( optarg, "all" )
|
|
|
|
? tr_strdup_printf( "method=torrent-remove&ids=%s", optarg )
|
|
|
|
: tr_strdup ( "method=torrent-remove" ); break;
|
|
|
|
case 's': req = strcmp( optarg, "all" )
|
|
|
|
? tr_strdup_printf( "method=torrent-start&ids=%s", optarg )
|
|
|
|
: tr_strdup ( "method=torrent-start" ); break;
|
|
|
|
case 'S': req = strcmp( optarg, "all" )
|
|
|
|
? tr_strdup_printf( "method=torrent-stop&ids=%s", optarg )
|
|
|
|
: tr_strdup ( "method=torrent-stop" ); break;
|
|
|
|
case 'v': req = strcmp( optarg, "all" )
|
|
|
|
? tr_strdup_printf( "method=torrent-verify&ids=%s", optarg )
|
|
|
|
: tr_strdup ( "method=torrent-verify" ); break;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( req )
|
|
|
|
reqs[reqCount++] = req;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
/* [host:port] or [host] or [port] */
|
|
|
|
static void
|
|
|
|
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] != '-' )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const char * s = argv[1];
|
|
|
|
const char * delim = strchr( s, ':' );
|
|
|
|
if( delim ) { /* user passed in both host and port */
|
|
|
|
*host = tr_strndup( s, delim-s );
|
|
|
|
*port = atoi( delim+1 );
|
|
|
|
} else {
|
|
|
|
char * end;
|
|
|
|
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;
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
evbuffer_add( buf, ptr, byteCount );
|
|
|
|
return byteCount;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static const char*
|
|
|
|
torrentStatusToString( int i )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
switch( i )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
case TR_STATUS_CHECK_WAIT: return "Will Verify";
|
|
|
|
case TR_STATUS_CHECK: return "Verifying";
|
|
|
|
case TR_STATUS_DOWNLOAD: return "Downloading";
|
|
|
|
case TR_STATUS_SEED: return "Seeding";
|
|
|
|
case TR_STATUS_STOPPED: return "Stopped";
|
|
|
|
default: return "Error";
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
|
|
|
processResponse( 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;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( tr_jsonParse( response, len, &top, NULL ) )
|
|
|
|
tr_nerr( MY_NAME, "Unable to parse response\n" );
|
|
|
|
else
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_benc *args, *list;
|
|
|
|
int64_t tag = -1;
|
|
|
|
const char * str;
|
|
|
|
tr_bencDictFindInt( &top, "tag", &tag );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( tr_bencDictFindStr( &top, "result", &str ) )
|
|
|
|
printf( "Server responded: \"%s\"\n", str );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( ( tag == TAG_LIST ) &&
|
|
|
|
( tr_bencDictFindDict( &top, "arguments", &args ) ) &&
|
|
|
|
( tr_bencDictFindList( args, "list", &list ) ) )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
int i, n;
|
|
|
|
for( i=0, n=tr_bencListSize( list ); i<n; ++i )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
int64_t id, status;
|
|
|
|
const char *name, *ratiostr, *upstr, *dnstr;
|
|
|
|
tr_benc * d = tr_bencListChild( list, i );
|
|
|
|
if( tr_bencDictFindInt( d, "id", &id )
|
|
|
|
&& tr_bencDictFindInt( d, "status", &status )
|
|
|
|
&& tr_bencDictFindStr( d, "name", &name )
|
|
|
|
&& tr_bencDictFindStr( d, "ratio", &ratiostr )
|
|
|
|
&& tr_bencDictFindStr( d, "rateUpload", &upstr )
|
|
|
|
&& tr_bencDictFindStr( d, "rateDownload", &dnstr ) )
|
|
|
|
{
|
|
|
|
printf( "%4d. Up: %5.1f Down: %5.1f Ratio: %4.1f %-15s %s\n",
|
|
|
|
(int)id,
|
|
|
|
strtod( upstr, NULL ),
|
|
|
|
strtod( dnstr, NULL ),
|
|
|
|
strtod( ratiostr, NULL ),
|
|
|
|
torrentStatusToString( status ),
|
|
|
|
name );
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
|
|
|
processRequests( const char * host, int port,
|
|
|
|
const char ** reqs, int reqCount )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
int i;
|
|
|
|
CURL * curl;
|
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
|
|
|
|
|
|
|
curl = curl_easy_init( );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_VERBOSE, debug );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_USERAGENT, MY_NAME"/"LONG_VERSION_STRING );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, writeFunc, 0 );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_WRITEDATA, buf, 0 );
|
|
|
|
|
|
|
|
for( i=0; i<reqCount; ++i )
|
|
|
|
{
|
|
|
|
CURLcode res;
|
|
|
|
char * url = tr_strdup_printf( "http://%s:%d/transmission?%s",
|
|
|
|
host, port, reqs[i] );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_URL, url );
|
|
|
|
if(( res = curl_easy_perform( curl )))
|
|
|
|
tr_nerr( MY_NAME, "%s\n", curl_easy_strerror( res ) );
|
2007-04-18 16:39:10 +00:00
|
|
|
else
|
2008-05-18 16:44:30 +00:00
|
|
|
processResponse( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
evbuffer_drain( buf, EVBUFFER_LENGTH( buf ) );
|
|
|
|
tr_free( url );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
evbuffer_free( buf );
|
|
|
|
curl_easy_cleanup( curl );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-05-18 16:44:30 +00:00
|
|
|
main( int argc, char ** argv )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
int i;
|
|
|
|
int port = DEFAULT_PORT;
|
|
|
|
char * host = NULL;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( argc < 2 )
|
|
|
|
showUsage( );
|
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 );
|
|
|
|
|
|
|
|
readargs( argc, argv );
|
|
|
|
if( reqCount )
|
|
|
|
processRequests( host, port, (const char**)reqs, reqCount );
|
2007-04-18 16:39:10 +00:00
|
|
|
else
|
2008-05-18 16:44:30 +00:00
|
|
|
showUsage( );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
for( i=0; i<reqCount; ++i )
|
|
|
|
tr_free( reqs[i] );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|