2006-01-12 17:43:21 +00:00
|
|
|
|
/******************************************************************************
|
|
|
|
|
* Copyright (c) 2005 Eric Petit
|
|
|
|
|
*
|
|
|
|
|
* 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 <getopt.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <transmission.h>
|
2006-01-12 18:29:20 +00:00
|
|
|
|
#ifdef SYS_BEOS
|
|
|
|
|
#include <kernel/OS.h>
|
|
|
|
|
#define usleep snooze
|
|
|
|
|
#endif
|
2006-01-12 17:43:21 +00:00
|
|
|
|
|
|
|
|
|
#define USAGE \
|
|
|
|
|
"Usage: %s [options] file.torrent [options]\n\n" \
|
|
|
|
|
"Options:\n" \
|
|
|
|
|
" -h, --help Print this help and exit\n" \
|
|
|
|
|
" -i, --info Print metainfo and exit\n" \
|
|
|
|
|
" -s, --scrape Print counts of seeders/leechers and exit\n" \
|
|
|
|
|
" -v, --verbose <int> Verbose level (0 to 2, default = 0)\n" \
|
2006-01-12 18:40:47 +00:00
|
|
|
|
" -p, --port <int> Port we should listen on (default = %d)\n" \
|
2006-01-12 18:43:18 +00:00
|
|
|
|
" -u, --upload <int> Maximum upload rate (-1 = no limit, default = 20)\n" \
|
2006-04-07 12:09:08 +00:00
|
|
|
|
" -d, --download <int> Maximum download rate (-1 = no limit, default = -1)\n" \
|
2006-01-12 18:43:18 +00:00
|
|
|
|
" -f, --finish <shell script> Command you wish to run on completion\n"
|
2006-01-12 17:43:21 +00:00
|
|
|
|
|
2006-04-07 12:09:08 +00:00
|
|
|
|
static int showHelp = 0;
|
|
|
|
|
static int showInfo = 0;
|
|
|
|
|
static int showScrape = 0;
|
|
|
|
|
static int verboseLevel = 0;
|
|
|
|
|
static int bindPort = TR_DEFAULT_PORT;
|
|
|
|
|
static int uploadLimit = 20;
|
|
|
|
|
static int downloadLimit = -1;
|
|
|
|
|
static char * torrentPath = NULL;
|
|
|
|
|
static volatile char mustDie = 0;
|
2006-01-12 17:43:21 +00:00
|
|
|
|
|
2006-01-12 18:43:18 +00:00
|
|
|
|
static char * finishCall = NULL;
|
|
|
|
|
|
2006-01-12 17:43:21 +00:00
|
|
|
|
static int parseCommandLine ( int argc, char ** argv );
|
|
|
|
|
static void sigHandler ( int signal );
|
|
|
|
|
|
|
|
|
|
int main( int argc, char ** argv )
|
|
|
|
|
{
|
2006-03-23 12:39:39 +00:00
|
|
|
|
int i, error;
|
|
|
|
|
tr_handle_t * h;
|
|
|
|
|
tr_torrent_t * tor;
|
|
|
|
|
tr_stat_t * s;
|
2006-01-12 17:43:21 +00:00
|
|
|
|
|
|
|
|
|
printf( "Transmission %s - http://transmission.m0k.org/\n\n",
|
|
|
|
|
VERSION_STRING );
|
|
|
|
|
|
|
|
|
|
/* Get options */
|
|
|
|
|
if( parseCommandLine( argc, argv ) )
|
|
|
|
|
{
|
2006-01-12 18:40:47 +00:00
|
|
|
|
printf( USAGE, argv[0], TR_DEFAULT_PORT );
|
2006-01-12 17:43:21 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( showHelp )
|
|
|
|
|
{
|
2006-01-12 18:40:47 +00:00
|
|
|
|
printf( USAGE, argv[0], TR_DEFAULT_PORT );
|
2006-01-12 17:43:21 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( verboseLevel < 0 )
|
|
|
|
|
{
|
|
|
|
|
verboseLevel = 0;
|
|
|
|
|
}
|
|
|
|
|
else if( verboseLevel > 9 )
|
|
|
|
|
{
|
|
|
|
|
verboseLevel = 9;
|
|
|
|
|
}
|
|
|
|
|
if( verboseLevel )
|
|
|
|
|
{
|
|
|
|
|
static char env[11];
|
|
|
|
|
sprintf( env, "TR_DEBUG=%d", verboseLevel );
|
|
|
|
|
putenv( env );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( bindPort < 1 || bindPort > 65535 )
|
|
|
|
|
{
|
|
|
|
|
printf( "Invalid port '%d'\n", bindPort );
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Initialize libtransmission */
|
|
|
|
|
h = tr_init();
|
|
|
|
|
|
|
|
|
|
/* Open and parse torrent file */
|
2006-03-23 12:39:39 +00:00
|
|
|
|
if( !( tor = tr_torrentInit( h, torrentPath, &error ) ) )
|
2006-01-12 17:43:21 +00:00
|
|
|
|
{
|
|
|
|
|
printf( "Failed opening torrent file `%s'\n", torrentPath );
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( showInfo )
|
|
|
|
|
{
|
2006-03-23 12:39:39 +00:00
|
|
|
|
tr_info_t * info = tr_torrentInfo( tor );
|
2006-01-12 17:43:21 +00:00
|
|
|
|
|
|
|
|
|
/* Print torrent info (quite <20> la btshowmetainfo) */
|
|
|
|
|
printf( "hash: " );
|
|
|
|
|
for( i = 0; i < SHA_DIGEST_LENGTH; i++ )
|
|
|
|
|
{
|
|
|
|
|
printf( "%02x", info->hash[i] );
|
|
|
|
|
}
|
|
|
|
|
printf( "\n" );
|
|
|
|
|
printf( "tracker: %s:%d\n",
|
|
|
|
|
info->trackerAddress, info->trackerPort );
|
|
|
|
|
printf( "announce: %s\n", info->trackerAnnounce );
|
|
|
|
|
printf( "size: %lld (%lld * %d + %lld)\n",
|
|
|
|
|
info->totalSize, info->totalSize / info->pieceSize,
|
|
|
|
|
info->pieceSize, info->totalSize % info->pieceSize );
|
|
|
|
|
printf( "file(s):\n" );
|
|
|
|
|
for( i = 0; i < info->fileCount; i++ )
|
|
|
|
|
{
|
|
|
|
|
printf( " %s (%lld)\n", info->files[i].name,
|
|
|
|
|
info->files[i].length );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
goto cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( showScrape )
|
|
|
|
|
{
|
|
|
|
|
int seeders, leechers;
|
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
|
if( tr_torrentScrape( tor, &seeders, &leechers ) )
|
2006-01-12 17:43:21 +00:00
|
|
|
|
{
|
|
|
|
|
printf( "Scrape failed.\n" );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf( "%d seeder(s), %d leecher(s).\n", seeders, leechers );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
goto cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
signal( SIGINT, sigHandler );
|
|
|
|
|
|
|
|
|
|
tr_setBindPort( h, bindPort );
|
|
|
|
|
tr_setUploadLimit( h, uploadLimit );
|
2006-04-07 12:09:08 +00:00
|
|
|
|
tr_setDownloadLimit( h, downloadLimit );
|
2006-01-12 17:43:21 +00:00
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
|
tr_torrentSetFolder( tor, "." );
|
|
|
|
|
tr_torrentStart( tor );
|
2006-01-12 17:43:21 +00:00
|
|
|
|
|
|
|
|
|
while( !mustDie )
|
|
|
|
|
{
|
|
|
|
|
char string[80];
|
|
|
|
|
int chars = 0;
|
2006-01-12 18:43:18 +00:00
|
|
|
|
int result;
|
2006-01-12 17:43:21 +00:00
|
|
|
|
|
|
|
|
|
sleep( 1 );
|
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
|
s = tr_torrentStat( tor );
|
2006-01-12 17:43:21 +00:00
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
|
if( s->status & TR_STATUS_CHECK )
|
2006-01-12 17:43:21 +00:00
|
|
|
|
{
|
|
|
|
|
chars = snprintf( string, 80,
|
2006-03-23 12:39:39 +00:00
|
|
|
|
"Checking files... %.2f %%", 100.0 * s->progress );
|
2006-01-12 17:43:21 +00:00
|
|
|
|
}
|
2006-03-23 12:39:39 +00:00
|
|
|
|
else if( s->status & TR_STATUS_DOWNLOAD )
|
2006-01-12 17:43:21 +00:00
|
|
|
|
{
|
|
|
|
|
chars = snprintf( string, 80,
|
2006-01-12 18:50:20 +00:00
|
|
|
|
"Progress: %.2f %%, %d peer%s, dl from %d (%.2f KB/s), "
|
2006-03-23 12:39:39 +00:00
|
|
|
|
"ul to %d (%.2f KB/s)", 100.0 * s->progress,
|
|
|
|
|
s->peersTotal, ( s->peersTotal == 1 ) ? "" : "s",
|
|
|
|
|
s->peersUploading, s->rateDownload,
|
|
|
|
|
s->peersDownloading, s->rateUpload );
|
2006-01-12 17:43:21 +00:00
|
|
|
|
}
|
2006-03-23 12:39:39 +00:00
|
|
|
|
else if( s->status & TR_STATUS_SEED )
|
2006-01-12 17:43:21 +00:00
|
|
|
|
{
|
|
|
|
|
chars = snprintf( string, 80,
|
2006-01-12 18:50:20 +00:00
|
|
|
|
"Seeding, uploading to %d of %d peer(s), %.2f KB/s",
|
2006-03-23 12:39:39 +00:00
|
|
|
|
s->peersDownloading, s->peersTotal,
|
|
|
|
|
s->rateUpload );
|
2006-01-12 17:43:21 +00:00
|
|
|
|
}
|
|
|
|
|
memset( &string[chars], ' ', 79 - chars );
|
|
|
|
|
string[79] = '\0';
|
|
|
|
|
fprintf( stderr, "\r%s", string );
|
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
|
if( s->error & TR_ETRACKER )
|
2006-01-12 17:43:21 +00:00
|
|
|
|
{
|
2006-03-23 12:39:39 +00:00
|
|
|
|
fprintf( stderr, "\n%s\n", s->trackerError );
|
2006-01-12 17:43:21 +00:00
|
|
|
|
}
|
|
|
|
|
else if( verboseLevel > 0 )
|
|
|
|
|
{
|
|
|
|
|
fprintf( stderr, "\n" );
|
|
|
|
|
}
|
2006-01-12 18:43:18 +00:00
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
|
if( tr_getFinished( tor ) )
|
2006-01-12 18:43:18 +00:00
|
|
|
|
{
|
|
|
|
|
result = system(finishCall);
|
|
|
|
|
}
|
2006-01-12 17:43:21 +00:00
|
|
|
|
}
|
|
|
|
|
fprintf( stderr, "\n" );
|
|
|
|
|
|
|
|
|
|
/* Try for 5 seconds to notice the tracker that we are leaving */
|
2006-03-23 12:39:39 +00:00
|
|
|
|
tr_torrentStop( tor );
|
2006-01-12 17:43:21 +00:00
|
|
|
|
for( i = 0; i < 10; i++ )
|
|
|
|
|
{
|
2006-03-23 12:39:39 +00:00
|
|
|
|
s = tr_torrentStat( tor );
|
|
|
|
|
if( s->status & TR_STATUS_PAUSE )
|
2006-01-12 17:43:21 +00:00
|
|
|
|
{
|
|
|
|
|
/* The 'stopped' message was sent */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
usleep( 500000 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cleanup:
|
2006-03-23 12:39:39 +00:00
|
|
|
|
tr_torrentClose( h, tor );
|
2006-01-12 17:43:21 +00:00
|
|
|
|
|
|
|
|
|
failed:
|
|
|
|
|
tr_close( h );
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int parseCommandLine( int argc, char ** argv )
|
|
|
|
|
{
|
|
|
|
|
for( ;; )
|
|
|
|
|
{
|
|
|
|
|
static struct option long_options[] =
|
2006-04-07 12:09:08 +00:00
|
|
|
|
{ { "help", no_argument, NULL, 'h' },
|
|
|
|
|
{ "info", no_argument, NULL, 'i' },
|
|
|
|
|
{ "scrape", no_argument, NULL, 's' },
|
|
|
|
|
{ "verbose", required_argument, NULL, 'v' },
|
|
|
|
|
{ "port", required_argument, NULL, 'p' },
|
|
|
|
|
{ "upload", required_argument, NULL, 'u' },
|
|
|
|
|
{ "download", required_argument, NULL, 'd' },
|
|
|
|
|
{ "finish", required_argument, NULL, 'f' },
|
2006-01-12 18:43:18 +00:00
|
|
|
|
{ 0, 0, 0, 0} };
|
2006-01-12 17:43:21 +00:00
|
|
|
|
|
|
|
|
|
int c, optind = 0;
|
2006-04-07 12:09:08 +00:00
|
|
|
|
c = getopt_long( argc, argv, "hisv:p:u:d:f:", long_options, &optind );
|
2006-01-12 17:43:21 +00:00
|
|
|
|
if( c < 0 )
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
switch( c )
|
|
|
|
|
{
|
|
|
|
|
case 'h':
|
|
|
|
|
showHelp = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 'i':
|
|
|
|
|
showInfo = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 's':
|
|
|
|
|
showScrape = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 'v':
|
|
|
|
|
verboseLevel = atoi( optarg );
|
|
|
|
|
break;
|
|
|
|
|
case 'p':
|
|
|
|
|
bindPort = atoi( optarg );
|
|
|
|
|
break;
|
|
|
|
|
case 'u':
|
|
|
|
|
uploadLimit = atoi( optarg );
|
|
|
|
|
break;
|
2006-04-07 12:09:08 +00:00
|
|
|
|
case 'd':
|
|
|
|
|
downloadLimit = atoi( optarg );
|
|
|
|
|
break;
|
2006-01-12 18:43:18 +00:00
|
|
|
|
case 'f':
|
|
|
|
|
finishCall = optarg;
|
|
|
|
|
break;
|
2006-01-12 17:43:21 +00:00
|
|
|
|
default:
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( optind > argc - 1 )
|
|
|
|
|
{
|
|
|
|
|
return !showHelp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
torrentPath = argv[optind];
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sigHandler( int signal )
|
|
|
|
|
{
|
|
|
|
|
switch( signal )
|
|
|
|
|
{
|
|
|
|
|
case SIGINT:
|
|
|
|
|
mustDie = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|