transmission/cli/cli.c

432 lines
14 KiB
C
Raw Normal View History

2006-07-16 19:39:23 +00:00
/******************************************************************************
* $Id$
*
* Copyright (c) 2005-2006 Transmission authors and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <libtransmission/transmission.h>
#include <libtransmission/bencode.h>
2008-07-08 16:50:34 +00:00
#include <libtransmission/tr-getopt.h>
#include <libtransmission/utils.h> /* tr_wait_msec */
#include <libtransmission/version.h>
#include <libtransmission/web.h> /* tr_webRun */
2006-07-16 19:39:23 +00:00
/***
****
***/
#define MEM_K 1024
#define MEM_K_STR "KiB"
#define MEM_M_STR "MiB"
#define MEM_G_STR "GiB"
#define MEM_T_STR "TiB"
#define DISK_K 1024
#define DISK_B_STR "B"
#define DISK_K_STR "KiB"
#define DISK_M_STR "MiB"
#define DISK_G_STR "GiB"
#define DISK_T_STR "TiB"
#define SPEED_K 1024
#define SPEED_B_STR "B/s"
#define SPEED_K_STR "KiB/s"
#define SPEED_M_STR "MiB/s"
#define SPEED_G_STR "GiB/s"
#define SPEED_T_STR "TiB/s"
/***
****
***/
2008-07-09 01:48:29 +00:00
#define LINEWIDTH 80
2010-08-05 13:24:46 +00:00
#define MY_CONFIG_NAME "transmission"
#define MY_READABLE_NAME "transmission-cli"
static tr_bool verify = 0;
static sig_atomic_t gotsig = 0;
static sig_atomic_t manualUpdate = 0;
static const char * torrentPath = NULL;
static const struct tr_option options[] =
{
{ 'b', "blocklist", "Enable peer blocklists", "b", 0, NULL },
{ 'B', "no-blocklist", "Disable peer blocklists", "B", 0, NULL },
{ 'd', "downlimit", "Set max download speed in "SPEED_K_STR, "d", 1, "<speed>" },
{ 'D', "no-downlimit", "Don't limit the 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 },
{ 'f', "finish", "Run a script when the torrent finishes", "f", 1, "<script>" },
{ 'g', "config-dir", "Where to find configuration files", "g", 1, "<path>" },
{ 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", 0, NULL },
{ 'M', "no-portmap", "Disable portmapping", "M", 0, NULL },
{ 'p', "port", "Port for incoming peers (Default: " TR_DEFAULT_PEER_PORT_STR ")", "p", 1, "<port>" },
{ 't', "tos", "Peer socket TOS (0 to 255, default=" TR_DEFAULT_PEER_SOCKET_TOS_STR ")", "t", 1, "<tos>" },
{ 'u', "uplimit", "Set max upload speed in "SPEED_K_STR, "u", 1, "<speed>" },
{ 'U', "no-uplimit", "Don't limit the upload speed", "U", 0, NULL },
{ 'v', "verify", "Verify the specified torrent", "v", 0, NULL },
{ 'w', "download-dir", "Where to save downloaded data", "w", 1, "<path>" },
{ 0, NULL, NULL, NULL, 0, NULL }
};
static const char *
getUsage( void )
{
return "A fast and easy BitTorrent client\n"
"\n"
2010-08-05 13:24:46 +00:00
"Usage: " MY_READABLE_NAME " [options] <file|url|magnet>";
}
static int parseCommandLine( tr_benc*, int argc, const char ** argv );
static void sigHandler( int signal );
2006-07-16 19:39:23 +00:00
static char*
tr_strlratio( char * buf,
double ratio,
size_t buflen )
{
2008-07-09 01:48:29 +00:00
if( (int)ratio == TR_RATIO_NA )
tr_strlcpy( buf, _( "None" ), buflen );
else if( (int)ratio == TR_RATIO_INF )
tr_strlcpy( buf, "Inf", buflen );
else if( ratio < 10.0 )
tr_snprintf( buf, buflen, "%.2f", ratio );
2008-07-09 01:48:29 +00:00
else if( ratio < 100.0 )
tr_snprintf( buf, buflen, "%.1f", ratio );
2008-07-09 01:48:29 +00:00
else
tr_snprintf( buf, buflen, "%.0f", ratio );
2008-07-09 01:48:29 +00:00
return buf;
}
static tr_bool waitingOnWeb;
static void
onTorrentFileDownloaded( tr_session * session UNUSED,
long response_code UNUSED,
const void * response,
size_t response_byte_count,
void * ctor )
{
tr_ctorSetMetainfo( ctor, response, response_byte_count );
waitingOnWeb = FALSE;
}
2008-07-09 01:48:29 +00:00
static void
getStatusStr( const tr_stat * st,
char * buf,
size_t buflen )
2008-07-09 01:48:29 +00:00
{
if( st->activity & TR_STATUS_CHECK_WAIT )
2008-07-09 01:48:29 +00:00
{
tr_snprintf( buf, buflen, "Waiting to verify local files" );
2008-07-09 01:48:29 +00:00
}
else if( st->activity & TR_STATUS_CHECK )
2008-07-09 01:48:29 +00:00
{
tr_snprintf( buf, buflen,
"Verifying local files (%.2f%%, %.2f%% valid)",
tr_truncd( 100 * st->recheckProgress, 2 ),
tr_truncd( 100 * st->percentDone, 2 ) );
2008-07-09 01:48:29 +00:00
}
else if( st->activity & TR_STATUS_DOWNLOAD )
2008-07-09 01:48:29 +00:00
{
char upStr[80];
char dnStr[80];
2008-07-09 01:48:29 +00:00
char ratioStr[80];
tr_formatter_speed_KBps( upStr, st->pieceUploadSpeed_KBps, sizeof( upStr ) );
tr_formatter_speed_KBps( dnStr, st->pieceDownloadSpeed_KBps, sizeof( dnStr ) );
2008-07-09 01:48:29 +00:00
tr_strlratio( ratioStr, st->ratio, sizeof( ratioStr ) );
tr_snprintf( buf, buflen,
"Progress: %.1f%%, "
"dl from %d of %d peers (%s), "
"ul to %d (%s) "
"[%s]",
tr_truncd( 100 * st->percentDone, 1 ),
st->peersSendingToUs, st->peersConnected, upStr,
st->peersGettingFromUs, dnStr,
ratioStr );
2008-07-09 01:48:29 +00:00
}
else if( st->activity & TR_STATUS_SEED )
2008-07-09 01:48:29 +00:00
{
char upStr[80];
2008-07-09 01:48:29 +00:00
char ratioStr[80];
tr_formatter_speed_KBps( upStr, st->pieceUploadSpeed_KBps, sizeof( upStr ) );
2008-07-09 01:48:29 +00:00
tr_strlratio( ratioStr, st->ratio, sizeof( ratioStr ) );
tr_snprintf( buf, buflen,
"Seeding, uploading to %d of %d peer(s), %s [%s]",
st->peersGettingFromUs, st->peersConnected, upStr, ratioStr );
2008-07-09 01:48:29 +00:00
}
2008-07-09 15:31:35 +00:00
else *buf = '\0';
2008-07-09 01:48:29 +00:00
}
static const char*
getConfigDir( int argc, const char ** argv )
{
int c;
const char * configDir = NULL;
const char * optarg;
const int ind = tr_optind;
while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg ))) {
if( c == 'g' ) {
configDir = optarg;
break;
}
}
tr_optind = ind;
if( configDir == NULL )
2010-08-05 13:24:46 +00:00
configDir = tr_getDefaultConfigDir( MY_CONFIG_NAME );
return configDir;
}
int
main( int argc, char ** argv )
2006-07-16 19:39:23 +00:00
{
2008-10-28 19:49:33 +00:00
int error;
tr_session * h;
tr_ctor * ctor;
tr_torrent * tor = NULL;
tr_benc settings;
const char * configDir;
uint8_t * fileContents;
size_t fileLength;
2006-07-16 19:39:23 +00:00
tr_formatter_mem_init( MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR );
tr_formatter_size_init( DISK_K,DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR );
tr_formatter_speed_init( SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR );
printf( "Transmission %s - http://www.transmissionbt.com/\n",
LONG_VERSION_STRING );
2006-07-16 19:39:23 +00:00
/* user needs to pass in at least one argument */
if( argc < 2 ) {
2010-08-05 13:24:46 +00:00
tr_getopt_usage( MY_READABLE_NAME, getUsage( ), options );
return EXIT_FAILURE;
}
/* load the defaults from config file + libtransmission defaults */
tr_bencInitDict( &settings, 0 );
configDir = getConfigDir( argc, (const char**)argv );
2010-08-05 13:24:46 +00:00
tr_sessionLoadSettings( &settings, configDir, MY_CONFIG_NAME );
/* the command line overrides defaults */
if( parseCommandLine( &settings, argc, (const char**)argv ) )
return EXIT_FAILURE;
2008-07-08 16:50:34 +00:00
/* Check the options for validity */
if( !torrentPath ) {
fprintf( stderr, "No torrent specified!\n" );
2008-07-08 16:50:34 +00:00
return EXIT_FAILURE;
2006-07-16 19:39:23 +00:00
}
h = tr_sessionInit( "cli", configDir, FALSE, &settings );
2006-07-16 19:39:23 +00:00
ctor = tr_ctorNew( h );
fileContents = tr_loadFile( torrentPath, &fileLength );
tr_ctorSetPaused( ctor, TR_FORCE, FALSE );
if( fileContents != NULL ) {
tr_ctorSetMetainfo( ctor, fileContents, fileLength );
} else if( !memcmp( torrentPath, "magnet:?", 8 ) ) {
tr_ctorSetMetainfoFromMagnetLink( ctor, torrentPath );
} else if( !memcmp( torrentPath, "http", 4 ) ) {
tr_webRun( h, torrentPath, NULL, onTorrentFileDownloaded, ctor );
waitingOnWeb = TRUE;
while( waitingOnWeb ) tr_wait_msec( 1000 );
} else {
fprintf( stderr, "ERROR: Unrecognized torrent \"%s\".\n", torrentPath );
fprintf( stderr, " * If you're trying to create a torrent, use transmission-create.\n" );
fprintf( stderr, " * If you're trying to see a torrent's info, use transmission-show.\n" );
tr_sessionClose( h );
return EXIT_FAILURE;
}
tr_free( fileContents );
2006-07-16 19:39:23 +00:00
tor = tr_torrentNew( ctor, &error );
tr_ctorFree( ctor );
2008-07-09 01:48:29 +00:00
if( !tor )
{
fprintf( stderr, "Failed opening torrent file `%s'\n", torrentPath );
2008-05-18 16:44:30 +00:00
tr_sessionClose( h );
return EXIT_FAILURE;
}
2006-07-16 19:39:23 +00:00
signal( SIGINT, sigHandler );
2008-09-05 15:13:06 +00:00
#ifndef WIN32
signal( SIGHUP, sigHandler );
2008-09-05 15:13:06 +00:00
#endif
2006-07-16 19:39:23 +00:00
tr_torrentStart( tor );
if( verify )
{
2008-07-09 01:48:29 +00:00
verify = 0;
tr_torrentVerify( tor );
}
for( ; ; )
2006-07-16 19:39:23 +00:00
{
char line[LINEWIDTH];
2008-07-09 01:48:29 +00:00
const tr_stat * st;
const char * messageName[] = { NULL, "Tracker gave a warning:",
"Tracker gave an error:",
"Error:" };
2006-07-16 19:39:23 +00:00
tr_wait_msec( 200 );
2006-07-16 19:39:23 +00:00
if( gotsig )
{
2007-02-13 05:20:52 +00:00
gotsig = 0;
printf( "\nStopping torrent...\n" );
2007-02-13 05:20:52 +00:00
tr_torrentStop( tor );
}
if( manualUpdate )
{
manualUpdate = 0;
if( !tr_torrentCanManualUpdate( tor ) )
fprintf(
stderr,
"\nReceived SIGHUP, but can't send a manual update now\n" );
else
{
fprintf( stderr,
"\nReceived SIGHUP: manual update scheduled\n" );
tr_torrentManualUpdate( tor );
}
}
2006-07-16 19:39:23 +00:00
2008-07-09 01:48:29 +00:00
st = tr_torrentStat( tor );
if( st->activity & TR_STATUS_STOPPED )
2007-07-18 17:31:00 +00:00
break;
2006-07-16 19:39:23 +00:00
2008-07-09 01:48:29 +00:00
getStatusStr( st, line, sizeof( line ) );
2008-07-09 15:31:35 +00:00
printf( "\r%-*s", LINEWIDTH, line );
if( messageName[st->error] )
fprintf( stderr, "\n%s: %s\n", messageName[st->error], st->errorString );
2006-07-16 19:39:23 +00:00
}
tr_sessionSaveSettings( h, configDir, &settings );
2008-07-09 01:48:29 +00:00
printf( "\n" );
tr_bencFree( &settings );
2008-05-18 16:44:30 +00:00
tr_sessionClose( h );
return EXIT_SUCCESS;
2006-07-16 19:39:23 +00:00
}
2008-07-08 16:50:34 +00:00
/***
****
****
****
***/
static int
parseCommandLine( tr_benc * d, int argc, const char ** argv )
2008-07-08 16:50:34 +00:00
{
int c;
2008-07-08 16:50:34 +00:00
const char * optarg;
while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg )))
2006-07-16 19:39:23 +00:00
{
switch( c )
{
case 'b': tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, TRUE );
break;
case 'B': tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, FALSE );
break;
case 'd': tr_bencDictAddInt ( d, TR_PREFS_KEY_DSPEED_KBps, atoi( optarg ) );
tr_bencDictAddBool( d, TR_PREFS_KEY_DSPEED_ENABLED, TRUE );
break;
case 'D': tr_bencDictAddBool( d, TR_PREFS_KEY_DSPEED_ENABLED, FALSE );
break;
case 'f': tr_bencDictAddStr( d, TR_PREFS_KEY_SCRIPT_TORRENT_DONE_FILENAME, optarg );
tr_bencDictAddBool( d, TR_PREFS_KEY_SCRIPT_TORRENT_DONE_ENABLED, TRUE );
break;
case 'g': /* handled above */
break;
case 'm': tr_bencDictAddBool( d, TR_PREFS_KEY_PORT_FORWARDING, TRUE );
break;
case 'M': tr_bencDictAddBool( d, TR_PREFS_KEY_PORT_FORWARDING, FALSE );
break;
case 'p': tr_bencDictAddInt( d, TR_PREFS_KEY_PEER_PORT, atoi( optarg ) );
break;
case 't': tr_bencDictAddInt( d, TR_PREFS_KEY_PEER_SOCKET_TOS, atoi( optarg ) );
break;
case 'u': tr_bencDictAddInt( d, TR_PREFS_KEY_USPEED_KBps, atoi( optarg ) );
tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED, TRUE );
break;
case 'U': tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED, FALSE );
break;
case 'v': verify = 1;
break;
case 'w': tr_bencDictAddStr( d, TR_PREFS_KEY_DOWNLOAD_DIR, optarg );
break;
case 910: tr_bencDictAddInt( d, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_REQUIRED );
break;
case 911: tr_bencDictAddInt( d, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_PREFERRED );
break;
case 912: tr_bencDictAddInt( d, TR_PREFS_KEY_ENCRYPTION, TR_CLEAR_PREFERRED );
break;
case TR_OPT_UNK:
if( torrentPath == NULL )
torrentPath = optarg;
break;
default: return 1;
2006-07-16 19:39:23 +00:00
}
}
return 0;
}
2008-07-08 16:50:34 +00:00
static void
sigHandler( int signal )
2006-07-16 19:39:23 +00:00
{
switch( signal )
{
case SIGINT:
gotsig = 1; break;
2008-09-05 15:13:06 +00:00
#ifndef WIN32
case SIGHUP:
manualUpdate = 1; break;
2008-09-05 15:13:06 +00:00
#endif
default:
break;
2006-07-16 19:39:23 +00:00
}
}