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>
|
2007-07-18 23:04:26 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2008-06-12 02:11:54 +00:00
|
|
|
#include <libtransmission/bencode.h>
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/makemeta.h>
|
2008-07-08 16:50:34 +00:00
|
|
|
#include <libtransmission/tr-getopt.h>
|
2007-11-12 15:15:51 +00:00
|
|
|
#include <libtransmission/utils.h> /* tr_wait */
|
2009-04-13 19:04:21 +00:00
|
|
|
#include <libtransmission/version.h>
|
2008-06-12 02:11:54 +00:00
|
|
|
#include <libtransmission/web.h> /* tr_webRun */
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-07-09 01:48:29 +00:00
|
|
|
#define LINEWIDTH 80
|
2008-07-08 16:50:34 +00:00
|
|
|
#define MY_NAME "transmission-cli"
|
2008-01-16 16:47:58 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
static tr_bool showInfo = 0;
|
|
|
|
static tr_bool showScrape = 0;
|
|
|
|
static tr_bool isPrivate = 0;
|
|
|
|
static tr_bool verify = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
static sig_atomic_t gotsig = 0;
|
|
|
|
static sig_atomic_t manualUpdate = 0;
|
|
|
|
|
|
|
|
static const char * torrentPath = NULL;
|
|
|
|
static const char * finishCall = NULL;
|
|
|
|
static const char * sourceFile = NULL;
|
|
|
|
static const char * comment = NULL;
|
|
|
|
|
2009-05-15 15:42:13 +00:00
|
|
|
#define MAX_ANNOUNCE 128
|
|
|
|
static tr_tracker_info announce[MAX_ANNOUNCE];
|
|
|
|
static int announceCount = 0;
|
|
|
|
|
2008-10-13 22:26:02 +00:00
|
|
|
static const struct tr_option options[] =
|
|
|
|
{
|
2008-12-13 23:17:36 +00:00
|
|
|
{ 'a', "announce", "Set the new torrent's announce URL", "a", 1, "<url>" },
|
|
|
|
{ 'b', "blocklist", "Enable peer blocklists", "b", 0, NULL },
|
|
|
|
{ 'B', "no-blocklist", "Disable peer blocklists", "B", 0, NULL },
|
|
|
|
{ 'c', "comment", "Set the new torrent's comment", "c", 1, "<comment>" },
|
|
|
|
{ 'd', "downlimit", "Set max download speed in KB/s", "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>" },
|
|
|
|
{ 'i', "info", "Show torrent details and exit", "i", 0, NULL },
|
|
|
|
{ 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", 0, NULL },
|
|
|
|
{ 'M', "no-portmap", "Disable portmapping", "M", 0, NULL },
|
|
|
|
{ 'n', "new", "Create a new torrent", "n", 1, "<source>" },
|
|
|
|
{ 'p', "port", "Port for incoming peers (Default: " TR_DEFAULT_PEER_PORT_STR ")", "p", 1, "<port>" },
|
|
|
|
{ 'r', "private", "Set the new torrent's 'private' flag", "r", 0, NULL },
|
|
|
|
{ 's', "scrape", "Scrape the torrent and exit", "s", 0, NULL },
|
|
|
|
{ '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 KB/s", "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 }
|
2008-10-13 22:26:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
getUsage( void )
|
|
|
|
{
|
|
|
|
return "A fast and easy BitTorrent client\n"
|
|
|
|
"\n"
|
2009-12-02 22:56:57 +00:00
|
|
|
"Usage: " MY_NAME " [options] <file|url|magnet>";
|
2008-10-13 22:26:02 +00:00
|
|
|
}
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
static int parseCommandLine( tr_benc*, int argc, const char ** argv );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
static void sigHandler( int signal );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-08-11 19:05:02 +00:00
|
|
|
static char*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_strlratio( char * buf,
|
|
|
|
double ratio,
|
|
|
|
size_t buflen )
|
2007-02-15 20:56:25 +00:00
|
|
|
{
|
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 )
|
2008-08-22 22:54:59 +00:00
|
|
|
tr_snprintf( buf, buflen, "%.2f", ratio );
|
2008-07-09 01:48:29 +00:00
|
|
|
else if( ratio < 100.0 )
|
2008-08-22 22:54:59 +00:00
|
|
|
tr_snprintf( buf, buflen, "%.1f", ratio );
|
2008-07-09 01:48:29 +00:00
|
|
|
else
|
2008-08-22 22:54:59 +00:00
|
|
|
tr_snprintf( buf, buflen, "%.0f", ratio );
|
2008-07-09 01:48:29 +00:00
|
|
|
return buf;
|
2007-02-15 20:56:25 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 02:11:54 +00:00
|
|
|
static int
|
|
|
|
is_rfc2396_alnum( char ch )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
return ( '0' <= ch && ch <= '9' )
|
|
|
|
|| ( 'A' <= ch && ch <= 'Z' )
|
|
|
|
|| ( 'a' <= ch && ch <= 'z' );
|
2008-06-12 02:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
escape( char * out,
|
|
|
|
const uint8_t * in,
|
|
|
|
int in_len ) /* rfc2396 */
|
2008-06-12 02:11:54 +00:00
|
|
|
{
|
|
|
|
const uint8_t *end = in + in_len;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-12 02:11:54 +00:00
|
|
|
while( in != end )
|
2008-09-23 19:11:04 +00:00
|
|
|
if( is_rfc2396_alnum( *in ) )
|
2008-06-12 02:11:54 +00:00
|
|
|
*out++ = (char) *in++;
|
|
|
|
else
|
2008-07-15 17:16:57 +00:00
|
|
|
out += tr_snprintf( out, 4, "%%%02X", (unsigned int)*in++ );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-12 02:11:54 +00:00
|
|
|
*out = '\0';
|
|
|
|
}
|
|
|
|
|
2007-09-28 15:12:21 +00:00
|
|
|
static void
|
2008-10-20 17:54:56 +00:00
|
|
|
torrentCompletenessChanged( tr_torrent * torrent UNUSED,
|
|
|
|
tr_completeness completeness UNUSED,
|
|
|
|
void * user_data UNUSED )
|
2007-09-28 15:12:21 +00:00
|
|
|
{
|
|
|
|
system( finishCall );
|
|
|
|
}
|
|
|
|
|
2009-12-02 22:56:57 +00:00
|
|
|
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-06-12 02:11:54 +00:00
|
|
|
static int leftToScrape = 0;
|
|
|
|
|
|
|
|
static void
|
2008-10-28 19:49:33 +00:00
|
|
|
scrapeDoneFunc( tr_session * session UNUSED,
|
|
|
|
long response_code,
|
|
|
|
const void * response,
|
|
|
|
size_t response_byte_count,
|
|
|
|
void * host )
|
2008-06-12 02:11:54 +00:00
|
|
|
{
|
|
|
|
tr_benc top, *files;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !tr_bencLoad( response, response_byte_count, &top, NULL )
|
|
|
|
&& tr_bencDictFindDict( &top, "files", &files )
|
|
|
|
&& files->val.l.count >= 2 )
|
2008-06-12 02:11:54 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int64_t complete = -1, incomplete = -1, downloaded = -1;
|
2008-06-12 02:11:54 +00:00
|
|
|
tr_benc * hash = &files->val.l.vals[1];
|
|
|
|
tr_bencDictFindInt( hash, "complete", &complete );
|
|
|
|
tr_bencDictFindInt( hash, "incomplete", &incomplete );
|
|
|
|
tr_bencDictFindInt( hash, "downloaded", &downloaded );
|
|
|
|
printf( "%4d seeders, %4d leechers, %5d downloads at %s\n",
|
2008-09-23 19:11:04 +00:00
|
|
|
(int)complete, (int)incomplete, (int)downloaded,
|
|
|
|
(char*)host );
|
2008-06-12 02:11:54 +00:00
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
fprintf( stderr, "Unable to parse response (http code %lu) at %s",
|
|
|
|
response_code,
|
|
|
|
(char*)host );
|
2008-06-12 02:11:54 +00:00
|
|
|
|
|
|
|
--leftToScrape;
|
2008-12-02 02:04:32 +00:00
|
|
|
|
|
|
|
tr_free( host );
|
2008-06-12 02:11:54 +00:00
|
|
|
}
|
|
|
|
|
2008-07-09 01:48:29 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
dumpInfo( FILE * out,
|
|
|
|
const tr_info * inf )
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
int prevTier = -1;
|
2008-07-09 01:48:29 +00:00
|
|
|
tr_file_index_t ff;
|
|
|
|
|
|
|
|
fprintf( out, "hash:\t" );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < SHA_DIGEST_LENGTH; ++i )
|
2008-07-09 01:48:29 +00:00
|
|
|
fprintf( out, "%02x", inf->hash[i] );
|
|
|
|
fprintf( out, "\n" );
|
|
|
|
|
|
|
|
fprintf( out, "name:\t%s\n", inf->name );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < inf->trackerCount; ++i )
|
|
|
|
{
|
|
|
|
if( prevTier != inf->trackers[i].tier )
|
|
|
|
{
|
2008-07-09 01:48:29 +00:00
|
|
|
prevTier = inf->trackers[i].tier;
|
2008-09-23 19:11:04 +00:00
|
|
|
fprintf( out, "\ntracker tier #%d:\n", ( prevTier + 1 ) );
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
|
|
|
fprintf( out, "\tannounce:\t%s\n", inf->trackers[i].announce );
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
fprintf( out, "size:\t%" PRIu64 " (%" PRIu64 " * %d + %" PRIu64 ")\n",
|
|
|
|
inf->totalSize, inf->totalSize / inf->pieceSize,
|
|
|
|
inf->pieceSize, inf->totalSize % inf->pieceSize );
|
2008-07-09 01:48:29 +00:00
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
if( inf->comment && *inf->comment )
|
2008-07-09 01:48:29 +00:00
|
|
|
fprintf( out, "comment:\t%s\n", inf->comment );
|
2008-07-09 15:31:35 +00:00
|
|
|
if( inf->creator && *inf->creator )
|
2008-07-09 01:48:29 +00:00
|
|
|
fprintf( out, "creator:\t%s\n", inf->creator );
|
|
|
|
if( inf->isPrivate )
|
|
|
|
fprintf( out, "private flag set\n" );
|
|
|
|
|
|
|
|
fprintf( out, "file(s):\n" );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( ff = 0; ff < inf->fileCount; ++ff )
|
|
|
|
fprintf( out, "\t%s (%" PRIu64 ")\n", inf->files[ff].name,
|
|
|
|
inf->files[ff].length );
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
getStatusStr( const tr_stat * st,
|
|
|
|
char * buf,
|
|
|
|
size_t buflen )
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
2008-10-20 17:54:56 +00:00
|
|
|
if( st->activity & TR_STATUS_CHECK_WAIT )
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( buf, buflen, "Waiting to verify local files" );
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
2008-10-20 17:54:56 +00:00
|
|
|
else if( st->activity & TR_STATUS_CHECK )
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_snprintf( buf, buflen,
|
|
|
|
"Verifying local files (%.2f%%, %.2f%% valid)",
|
2009-07-14 20:09:46 +00:00
|
|
|
tr_truncd( 100 * st->recheckProgress, 2 ),
|
|
|
|
tr_truncd( 100 * st->percentDone, 2 ) );
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
2008-10-20 17:54:56 +00:00
|
|
|
else if( st->activity & TR_STATUS_DOWNLOAD )
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
|
|
|
char ratioStr[80];
|
|
|
|
tr_strlratio( ratioStr, st->ratio, sizeof( ratioStr ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_snprintf(
|
|
|
|
buf, buflen,
|
|
|
|
"Progress: %.1f%%, dl from %d of %d peers (%.0f KB/s), "
|
|
|
|
"ul to %d (%.0f KB/s) [%s]",
|
2009-07-14 20:09:46 +00:00
|
|
|
tr_truncd( 100 * st->percentDone, 1 ),
|
2008-09-23 19:11:04 +00:00
|
|
|
st->peersSendingToUs,
|
|
|
|
st->peersConnected,
|
2008-11-08 02:49:04 +00:00
|
|
|
st->pieceDownloadSpeed,
|
2008-09-23 19:11:04 +00:00
|
|
|
st->peersGettingFromUs,
|
2008-11-08 02:49:04 +00:00
|
|
|
st->pieceUploadSpeed,
|
2008-09-23 19:11:04 +00:00
|
|
|
ratioStr );
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
2008-10-20 17:54:56 +00:00
|
|
|
else if( st->activity & TR_STATUS_SEED )
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
|
|
|
char ratioStr[80];
|
|
|
|
tr_strlratio( ratioStr, st->ratio, sizeof( ratioStr ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_snprintf(
|
|
|
|
buf, buflen,
|
|
|
|
"Seeding, uploading to %d of %d peer(s), %.0f KB/s [%s]",
|
|
|
|
st->peersGettingFromUs, st->peersConnected,
|
2008-11-08 02:49:04 +00:00
|
|
|
st->pieceUploadSpeed, 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
|
|
|
}
|
|
|
|
|
2008-12-13 23:17:36 +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;
|
|
|
|
|
2009-01-09 19:16:52 +00:00
|
|
|
while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg ))) {
|
|
|
|
if( c == 'g' ) {
|
|
|
|
configDir = optarg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
tr_optind = ind;
|
|
|
|
|
|
|
|
if( configDir == NULL )
|
|
|
|
configDir = tr_getDefaultConfigDir( MY_NAME );
|
|
|
|
|
|
|
|
return configDir;
|
|
|
|
}
|
|
|
|
|
2008-01-16 16:47:58 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
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;
|
2009-06-20 00:53:11 +00:00
|
|
|
tr_benc settings;
|
|
|
|
const char * configDir;
|
2009-08-10 20:04:08 +00:00
|
|
|
tr_bool haveSource;
|
|
|
|
tr_bool haveAnnounce;
|
2009-12-02 22:56:57 +00:00
|
|
|
uint8_t * fileContents;
|
|
|
|
size_t fileLength;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-01-01 18:14:05 +00:00
|
|
|
printf( "Transmission %s - http://www.transmissionbt.com/\n",
|
2007-07-10 19:40:21 +00:00
|
|
|
LONG_VERSION_STRING );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-10-13 22:26:02 +00:00
|
|
|
/* user needs to pass in at least one argument */
|
|
|
|
if( argc < 2 ) {
|
|
|
|
tr_getopt_usage( MY_NAME, getUsage( ), options );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
/* load the defaults from config file + libtransmission defaults */
|
|
|
|
tr_bencInitDict( &settings, 0 );
|
|
|
|
configDir = getConfigDir( argc, (const char**)argv );
|
|
|
|
tr_sessionLoadSettings( &settings, configDir, MY_NAME );
|
|
|
|
|
|
|
|
/* the command line overrides defaults */
|
|
|
|
if( parseCommandLine( &settings, argc, (const char**)argv ) )
|
2007-02-06 22:47:55 +00:00
|
|
|
return EXIT_FAILURE;
|
2008-01-01 18:14:05 +00:00
|
|
|
|
2008-07-08 16:50:34 +00:00
|
|
|
/* Check the options for validity */
|
2008-12-13 23:17:36 +00:00
|
|
|
if( !torrentPath ) {
|
2008-08-27 19:22:03 +00:00
|
|
|
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
|
|
|
}
|
2008-06-04 20:04:19 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
/* don't bind the port if we're just running the CLI
|
2008-12-13 23:17:36 +00:00
|
|
|
to get metainfo or to create a torrent */
|
2008-04-06 16:53:26 +00:00
|
|
|
if( showInfo || showScrape || ( sourceFile != NULL ) )
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_PORT, -1 );
|
|
|
|
|
|
|
|
h = tr_sessionInit( "cli", configDir, FALSE, &settings );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-06-20 00:53:11 +00:00
|
|
|
haveSource = sourceFile && *sourceFile;
|
|
|
|
haveAnnounce = announceCount > 0;
|
|
|
|
|
|
|
|
if( haveSource && !haveAnnounce )
|
|
|
|
fprintf( stderr, "Did you mean to create a torrent without a tracker's announce URL?\n" );
|
|
|
|
|
|
|
|
if( haveSource ) /* creating a torrent */
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2009-06-20 00:53:11 +00:00
|
|
|
int err;
|
|
|
|
tr_metainfo_builder * b;
|
|
|
|
fprintf( stderr, "creating torrent \"%s\"\n", torrentPath );
|
|
|
|
|
|
|
|
b = tr_metaInfoBuilderCreate( sourceFile );
|
2009-05-15 15:42:13 +00:00
|
|
|
tr_makeMetaInfo( b, torrentPath, announce, announceCount, comment, isPrivate );
|
2008-09-23 19:11:04 +00:00
|
|
|
while( !b->isDone )
|
|
|
|
{
|
2007-11-12 15:15:51 +00:00
|
|
|
tr_wait( 1000 );
|
2007-06-18 03:40:41 +00:00
|
|
|
printf( "." );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
err = b->result;
|
|
|
|
tr_metaInfoBuilderFree( b );
|
2008-03-24 17:33:08 +00:00
|
|
|
return err;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2007-12-22 03:51:12 +00:00
|
|
|
ctor = tr_ctorNew( h );
|
2009-12-02 22:56:57 +00:00
|
|
|
|
|
|
|
fileContents = tr_loadFile( torrentPath, &fileLength );
|
2008-06-12 02:11:54 +00:00
|
|
|
tr_ctorSetPaused( ctor, TR_FORCE, showScrape );
|
2009-12-02 22:56:57 +00:00
|
|
|
if( fileContents != NULL ) {
|
|
|
|
tr_ctorSetMetainfo( ctor, fileContents, fileLength );
|
|
|
|
} else if( !memcmp( torrentPath, "magnet:?", 8 ) ) {
|
|
|
|
tr_ctorSetMagnet( ctor, torrentPath );
|
|
|
|
} else if( !memcmp( torrentPath, "http", 4 ) ) {
|
|
|
|
tr_webRun( h, torrentPath, NULL, onTorrentFileDownloaded, ctor );
|
|
|
|
waitingOnWeb = TRUE;
|
|
|
|
while( waitingOnWeb ) tr_wait( 1000 );
|
|
|
|
}
|
|
|
|
tr_free( fileContents );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-06-12 02:11:54 +00:00
|
|
|
if( showScrape )
|
|
|
|
{
|
|
|
|
tr_info info;
|
|
|
|
|
2009-04-02 17:30:29 +00:00
|
|
|
if( !tr_torrentParse( ctor, &info ) )
|
2008-06-12 02:11:54 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
2008-06-12 02:11:54 +00:00
|
|
|
const time_t start = time( NULL );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < info.trackerCount; ++i )
|
2008-06-12 02:11:54 +00:00
|
|
|
{
|
|
|
|
if( info.trackers[i].scrape )
|
|
|
|
{
|
|
|
|
const char * scrape = info.trackers[i].scrape;
|
2008-09-23 19:11:04 +00:00
|
|
|
char escaped[SHA_DIGEST_LENGTH * 3 + 1];
|
|
|
|
char * url, *host;
|
2008-06-12 02:11:54 +00:00
|
|
|
escape( escaped, info.hash, SHA_DIGEST_LENGTH );
|
|
|
|
url = tr_strdup_printf( "%s%cinfo_hash=%s",
|
|
|
|
scrape,
|
2008-09-23 19:11:04 +00:00
|
|
|
strchr( scrape,
|
|
|
|
'?' ) ? '&' : '?',
|
2008-06-12 02:11:54 +00:00
|
|
|
escaped );
|
|
|
|
tr_httpParseURL( scrape, -1, &host, NULL, NULL );
|
|
|
|
++leftToScrape;
|
2009-12-02 22:56:57 +00:00
|
|
|
|
2008-06-12 02:11:54 +00:00
|
|
|
tr_webRun( h, url, NULL, scrapeDoneFunc, host );
|
|
|
|
tr_free( url );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( stderr, "scraping %d trackers:\n", leftToScrape );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
while( leftToScrape > 0 && ( ( time( NULL ) - start ) < 20 ) )
|
2008-06-12 02:11:54 +00:00
|
|
|
tr_wait( 250 );
|
|
|
|
}
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
if( showInfo )
|
|
|
|
{
|
2008-01-16 16:47:58 +00:00
|
|
|
tr_info info;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-04-02 17:30:29 +00:00
|
|
|
if( !tr_torrentParse( ctor, &info ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-07-09 01:48:29 +00:00
|
|
|
dumpInfo( stdout, &info );
|
2008-01-16 16:47:58 +00:00
|
|
|
tr_metainfoFree( &info );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-01-16 16:47:58 +00:00
|
|
|
tr_ctorFree( ctor );
|
2006-07-16 19:39:23 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2008-01-16 16:47:58 +00:00
|
|
|
|
2009-04-02 17:30:29 +00:00
|
|
|
tor = tr_torrentNew( ctor, &error );
|
2008-01-16 16:47:58 +00:00
|
|
|
tr_ctorFree( ctor );
|
2008-07-09 01:48:29 +00:00
|
|
|
if( !tor )
|
2008-01-16 16:47:58 +00:00
|
|
|
{
|
2008-08-27 19:22:03 +00:00
|
|
|
fprintf( stderr, "Failed opening torrent file `%s'\n", torrentPath );
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_sessionClose( h );
|
2008-01-16 16:47:58 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
signal( SIGINT, sigHandler );
|
2008-09-05 15:13:06 +00:00
|
|
|
#ifndef WIN32
|
2007-10-19 21:53:35 +00:00
|
|
|
signal( SIGHUP, sigHandler );
|
2008-09-05 15:13:06 +00:00
|
|
|
#endif
|
2008-10-20 17:54:56 +00:00
|
|
|
tr_torrentSetCompletenessCallback( tor, torrentCompletenessChanged, NULL );
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_torrentStart( tor );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( verify )
|
|
|
|
{
|
2008-07-09 01:48:29 +00:00
|
|
|
verify = 0;
|
|
|
|
tr_torrentVerify( tor );
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( ; ; )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char line[LINEWIDTH];
|
2008-07-09 01:48:29 +00:00
|
|
|
const tr_stat * st;
|
2009-08-06 07:04:19 +00:00
|
|
|
const char * messageName[] = { NULL, "Tracker gave a warning:",
|
|
|
|
"Tracker gave an error:",
|
2009-08-05 01:25:36 +00:00
|
|
|
"Error:" };
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-07-27 14:31:18 +00:00
|
|
|
tr_wait( 200 );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( gotsig )
|
|
|
|
{
|
2007-02-13 05:20:52 +00:00
|
|
|
gotsig = 0;
|
2008-07-09 15:39:53 +00:00
|
|
|
printf( "\nStopping torrent...\n" );
|
2007-02-13 05:20:52 +00:00
|
|
|
tr_torrentStop( tor );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
if( manualUpdate )
|
|
|
|
{
|
2007-10-19 21:53:35 +00:00
|
|
|
manualUpdate = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
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" );
|
2008-05-22 20:44:41 +00:00
|
|
|
tr_torrentManualUpdate( tor );
|
2007-10-19 21:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-07-09 01:48:29 +00:00
|
|
|
st = tr_torrentStat( tor );
|
2008-10-20 17:54:56 +00:00
|
|
|
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 );
|
2009-08-05 01:25:36 +00:00
|
|
|
|
|
|
|
if( messageName[st->error] )
|
|
|
|
fprintf( stderr, "\n%s: %s\n", messageName[st->error], st->errorString );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
tr_sessionSaveSettings( h, configDir, &settings );
|
|
|
|
|
2008-07-09 01:48:29 +00:00
|
|
|
printf( "\n" );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencFree( &settings );
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_sessionClose( h );
|
2007-02-06 22:47:55 +00:00
|
|
|
return EXIT_SUCCESS;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-07-08 16:50:34 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
****
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-01-01 18:14:05 +00:00
|
|
|
static int
|
2008-12-13 23:17:36 +00:00
|
|
|
parseCommandLine( tr_benc * d, int argc, const char ** argv )
|
2008-07-08 16:50:34 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int c;
|
2008-07-08 16:50:34 +00:00
|
|
|
const char * optarg;
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg )))
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
switch( c )
|
|
|
|
{
|
2009-05-15 15:42:13 +00:00
|
|
|
case 'a': if( announceCount + 1 < MAX_ANNOUNCE ) {
|
|
|
|
announce[announceCount].tier = announceCount;
|
|
|
|
announce[announceCount].announce = (char*) optarg;
|
|
|
|
++announceCount;
|
|
|
|
}
|
|
|
|
break;
|
2009-03-29 23:05:32 +00:00
|
|
|
case 'b': tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, TRUE );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
2009-03-29 23:05:32 +00:00
|
|
|
case 'B': tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, FALSE );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
|
|
|
case 'c': comment = optarg;
|
|
|
|
break;
|
2009-03-29 23:05:32 +00:00
|
|
|
case 'd': tr_bencDictAddInt ( d, TR_PREFS_KEY_DSPEED, atoi( optarg ) );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_DSPEED_ENABLED, TRUE );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
2009-03-29 23:05:32 +00:00
|
|
|
case 'D': tr_bencDictAddBool( d, TR_PREFS_KEY_DSPEED_ENABLED, FALSE );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
|
|
|
case 'f': finishCall = optarg;
|
|
|
|
break;
|
|
|
|
case 'g': /* handled above */
|
|
|
|
break;
|
|
|
|
case 'i': showInfo = 1;
|
|
|
|
break;
|
2009-03-29 23:05:32 +00:00
|
|
|
case 'm': tr_bencDictAddBool( d, TR_PREFS_KEY_PORT_FORWARDING, TRUE );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
2009-03-29 23:05:32 +00:00
|
|
|
case 'M': tr_bencDictAddBool( d, TR_PREFS_KEY_PORT_FORWARDING, FALSE );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
|
|
|
case 'n': sourceFile = optarg; break;
|
2008-12-15 19:30:58 +00:00
|
|
|
case 'p': tr_bencDictAddInt( d, TR_PREFS_KEY_PEER_PORT, atoi( optarg ) );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
|
|
|
case 'r': isPrivate = 1;
|
|
|
|
break;
|
|
|
|
case 's': showScrape = 1;
|
|
|
|
break;
|
|
|
|
case 't': tr_bencDictAddInt( d, TR_PREFS_KEY_PEER_SOCKET_TOS, atoi( optarg ) );
|
|
|
|
break;
|
|
|
|
case 'u': tr_bencDictAddInt( d, TR_PREFS_KEY_USPEED, atoi( optarg ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED, TRUE );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
2009-03-29 23:05:32 +00:00
|
|
|
case 'U': tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED, FALSE );
|
2008-12-13 23:17:36 +00:00
|
|
|
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;
|
2009-01-03 23:37:29 +00:00
|
|
|
case 911: tr_bencDictAddInt( d, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_PREFERRED );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
2009-01-03 23:37:29 +00:00
|
|
|
case 912: tr_bencDictAddInt( d, TR_PREFS_KEY_ENCRYPTION, TR_CLEAR_PREFERRED );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
case TR_OPT_UNK:
|
2008-12-13 23:17:36 +00:00
|
|
|
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 )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
case SIGINT:
|
|
|
|
gotsig = 1; break;
|
|
|
|
|
2008-09-05 15:13:06 +00:00
|
|
|
#ifndef WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
case SIGHUP:
|
|
|
|
manualUpdate = 1; break;
|
|
|
|
|
2008-09-05 15:13:06 +00:00
|
|
|
#endif
|
2008-09-23 19:11:04 +00:00
|
|
|
default:
|
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|