2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
2011-01-19 13:48:47 +00:00
|
|
|
* Copyright (c) Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <stdio.h> /* fprintf () */
|
|
|
|
#include <stdlib.h> /* atoi () */
|
|
|
|
#include <string.h> /* memcmp () */
|
2006-07-16 19:39:23 +00:00
|
|
|
#include <signal.h>
|
2007-07-18 23:04:26 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2014-09-21 17:55:39 +00:00
|
|
|
#include <libtransmission/error.h>
|
2014-07-08 00:08:43 +00:00
|
|
|
#include <libtransmission/file.h>
|
2008-07-08 16:50:34 +00:00
|
|
|
#include <libtransmission/tr-getopt.h>
|
2010-01-01 22:40:54 +00:00
|
|
|
#include <libtransmission/utils.h> /* tr_wait_msec */
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
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
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
#define MEM_K 1024
|
|
|
|
#define MEM_K_STR "KiB"
|
|
|
|
#define MEM_M_STR "MiB"
|
|
|
|
#define MEM_G_STR "GiB"
|
2010-07-06 20:25:54 +00:00
|
|
|
#define MEM_T_STR "TiB"
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2012-02-03 21:21:52 +00:00
|
|
|
#define DISK_K 1000
|
2010-07-28 00:31:11 +00:00
|
|
|
#define DISK_B_STR "B"
|
2012-02-03 21:21:52 +00:00
|
|
|
#define DISK_K_STR "kB"
|
|
|
|
#define DISK_M_STR "MB"
|
|
|
|
#define DISK_G_STR "GB"
|
|
|
|
#define DISK_T_STR "TB"
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2012-02-03 21:21:52 +00:00
|
|
|
#define SPEED_K 1000
|
|
|
|
#define SPEED_B_STR "B/s"
|
|
|
|
#define SPEED_K_STR "kB/s"
|
|
|
|
#define SPEED_M_STR "MB/s"
|
|
|
|
#define SPEED_G_STR "GB/s"
|
|
|
|
#define SPEED_T_STR "TB/s"
|
2010-07-03 00:25:22 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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"
|
2008-01-16 16:47:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool showVersion = false;
|
|
|
|
static bool verify = false;
|
|
|
|
static sig_atomic_t gotsig = false;
|
|
|
|
static sig_atomic_t manualUpdate = false;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* torrentPath = NULL;
|
2009-05-15 15:42:13 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static struct tr_option const options[] =
|
2008-10-13 22:26:02 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
{ '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 },
|
|
|
|
{ 'V', "version", "Show version number and exit", "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
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* getUsage(void)
|
2008-10-13 22:26:02 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return "A fast and easy BitTorrent client\n"
|
|
|
|
"\n"
|
|
|
|
"Usage: " MY_READABLE_NAME " [options] <file|url|magnet>";
|
2008-10-13 22:26:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int parseCommandLine(tr_variant*, int argc, char const** argv);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void sigHandler(int signal);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static char* tr_strlratio(char* buf, double ratio, size_t buflen)
|
2007-02-15 20:56:25 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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);
|
|
|
|
}
|
|
|
|
else if (ratio < 100.0)
|
|
|
|
{
|
|
|
|
tr_snprintf(buf, buflen, "%.1f", ratio);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_snprintf(buf, buflen, "%.0f", ratio);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
2007-02-15 20:56:25 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool waitingOnWeb;
|
2009-12-02 22:56:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void onTorrentFileDownloaded(tr_session* session UNUSED, bool did_connect UNUSED, bool did_timeout UNUSED,
|
2017-04-20 16:02:19 +00:00
|
|
|
long response_code UNUSED, void const* response, size_t response_byte_count, void* ctor)
|
2009-12-02 22:56:57 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctorSetMetainfo(ctor, response, response_byte_count);
|
|
|
|
waitingOnWeb = false;
|
2009-12-02 22:56:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void getStatusStr(tr_stat const* st, char* buf, size_t buflen)
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (st->activity == TR_STATUS_CHECK_WAIT)
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(buf, buflen, "Waiting to verify local files");
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (st->activity == TR_STATUS_CHECK)
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (st->activity == TR_STATUS_DOWNLOAD)
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char upStr[80];
|
|
|
|
char dnStr[80];
|
|
|
|
char ratioStr[80];
|
|
|
|
|
|
|
|
tr_formatter_speed_KBps(upStr, st->pieceUploadSpeed_KBps, sizeof(upStr));
|
|
|
|
tr_formatter_speed_KBps(dnStr, st->pieceDownloadSpeed_KBps, sizeof(dnStr));
|
|
|
|
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, dnStr, st->peersGettingFromUs, upStr,
|
|
|
|
ratioStr);
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (st->activity == TR_STATUS_SEED)
|
2008-07-09 01:48:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char upStr[80];
|
|
|
|
char ratioStr[80];
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_formatter_speed_KBps(upStr, st->pieceUploadSpeed_KBps, sizeof(upStr));
|
|
|
|
tr_strlratio(ratioStr, st->ratio, sizeof(ratioStr));
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(buf, buflen, "Seeding, uploading to %d of %d peer(s), %s [%s]", st->peersGettingFromUs, st->peersConnected,
|
|
|
|
upStr, ratioStr);
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
*buf = '\0';
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* getConfigDir(int argc, char const** argv)
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int c;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* configDir = NULL;
|
|
|
|
char const* optarg;
|
|
|
|
int const ind = tr_optind;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((c = tr_getopt(getUsage(), argc, argv, options, &optarg)) != TR_OPT_DONE)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (c == 'g')
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
configDir = optarg;
|
|
|
|
break;
|
2009-01-09 19:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_optind = ind;
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (configDir == NULL)
|
|
|
|
{
|
|
|
|
configDir = tr_getDefaultConfigDir(MY_CONFIG_NAME);
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return configDir;
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_main(int argc, char* argv[])
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* h;
|
|
|
|
tr_ctor* ctor;
|
|
|
|
tr_torrent* tor = NULL;
|
|
|
|
tr_variant settings;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* configDir;
|
2017-04-19 12:04:45 +00:00
|
|
|
uint8_t* fileContents;
|
|
|
|
size_t fileLength;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2017-04-19 12:04:45 +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("%s %s\n", MY_READABLE_NAME, LONG_VERSION_STRING);
|
|
|
|
|
|
|
|
/* user needs to pass in at least one argument */
|
|
|
|
if (argc < 2)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_getopt_usage(MY_READABLE_NAME, getUsage(), options);
|
|
|
|
return EXIT_FAILURE;
|
2008-10-13 22:26:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* load the defaults from config file + libtransmission defaults */
|
|
|
|
tr_variantInitDict(&settings, 0);
|
2017-04-20 16:02:19 +00:00
|
|
|
configDir = getConfigDir(argc, (char const**)argv);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sessionLoadSettings(&settings, configDir, MY_CONFIG_NAME);
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the command line overrides defaults */
|
2017-04-30 16:25:26 +00:00
|
|
|
if (parseCommandLine(&settings, argc, (char const**)argv) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2010-10-18 03:11:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (showVersion)
|
|
|
|
{
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2010-10-18 03:11:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Check the options for validity */
|
2017-04-30 16:25:26 +00:00
|
|
|
if (torrentPath == NULL)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "No torrent specified!\n");
|
|
|
|
return EXIT_FAILURE;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2008-06-04 20:04:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindStr(&settings, TR_KEY_download_dir, &str, NULL))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_sys_path_exists(str, NULL))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error* error = NULL;
|
|
|
|
|
|
|
|
if (!tr_sys_dir_create(str, TR_SYS_DIR_CREATE_PARENTS, 0700, &error))
|
2013-01-16 00:21:02 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "Unable to create download directory \"%s\": %s\n", str, error->message);
|
|
|
|
tr_error_free(error);
|
|
|
|
return EXIT_FAILURE;
|
2013-01-16 00:21:02 +00:00
|
|
|
}
|
2011-10-25 16:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
h = tr_sessionInit(configDir, false, &settings);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ctor = tr_ctorNew(h);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
fileContents = tr_loadFile(torrentPath, &fileLength, NULL);
|
|
|
|
tr_ctorSetPaused(ctor, TR_FORCE, false);
|
|
|
|
|
|
|
|
if (fileContents != NULL)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctorSetMetainfo(ctor, fileContents, fileLength);
|
2009-12-02 22:56:57 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (memcmp(torrentPath, "magnet:?", 8) == 0)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctorSetMetainfoFromMagnetLink(ctor, torrentPath);
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (memcmp(torrentPath, "http", 4) == 0)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_webRun(h, torrentPath, onTorrentFileDownloaded, ctor);
|
|
|
|
waitingOnWeb = true;
|
|
|
|
|
|
|
|
while (waitingOnWeb)
|
|
|
|
{
|
|
|
|
tr_wait_msec(1000);
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
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;
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(fileContents);
|
|
|
|
|
|
|
|
tor = tr_torrentNew(ctor, NULL, NULL);
|
|
|
|
tr_ctorFree(ctor);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tor == NULL)
|
2008-01-16 16:47:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "Failed opening torrent file `%s'\n", torrentPath);
|
|
|
|
tr_sessionClose(h);
|
|
|
|
return EXIT_FAILURE;
|
2008-01-16 16:47:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
signal(SIGINT, sigHandler);
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
signal(SIGHUP, sigHandler);
|
2008-09-05 15:13:06 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrentStart(tor);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (verify)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
verify = false;
|
|
|
|
tr_torrentVerify(tor, NULL, NULL);
|
2008-07-09 01:48:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (;;)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char line[LINEWIDTH];
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_stat const* st;
|
|
|
|
char const* messageName[] =
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
NULL,
|
|
|
|
"Tracker gave a warning:",
|
|
|
|
"Tracker gave an error:",
|
|
|
|
"Error:"
|
|
|
|
};
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_wait_msec(200);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (gotsig)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
gotsig = false;
|
|
|
|
printf("\nStopping torrent...\n");
|
|
|
|
tr_torrentStop(tor);
|
2007-02-13 05:20:52 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (manualUpdate)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
manualUpdate = false;
|
|
|
|
|
|
|
|
if (!tr_torrentCanManualUpdate(tor))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "\nReceived SIGHUP, but can't send a manual update now\n");
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "\nReceived SIGHUP: manual update scheduled\n");
|
|
|
|
tr_torrentManualUpdate(tor);
|
2007-10-19 21:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
st = tr_torrentStat(tor);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (st->activity == TR_STATUS_STOPPED)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2009-08-05 01:25:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
getStatusStr(st, line, sizeof(line));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sessionSaveSettings(h, configDir, &settings);
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\n");
|
|
|
|
tr_variantFree(&settings);
|
|
|
|
tr_sessionClose(h);
|
|
|
|
return EXIT_SUCCESS;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-07-08 16:50:34 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int parseCommandLine(tr_variant* d, int argc, char const** argv)
|
2008-07-08 16:50:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int c;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* optarg;
|
2008-07-08 16:50:34 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((c = tr_getopt(getUsage(), argc, argv, options, &optarg)) != TR_OPT_DONE)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (c)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'b':
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_blocklist_enabled, true);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'B':
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_blocklist_enabled, false);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'd':
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_speed_limit_down, atoi(optarg));
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_speed_limit_down_enabled, true);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'D':
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_speed_limit_down_enabled, false);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'f':
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_script_torrent_done_filename, optarg);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_script_torrent_done_enabled, true);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'g': /* handled above */
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'm':
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_port_forwarding_enabled, true);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'M':
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_port_forwarding_enabled, false);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'p':
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_port, atoi(optarg));
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 't':
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_socket_tos, atoi(optarg));
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'u':
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_speed_limit_up, atoi(optarg));
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_speed_limit_up_enabled, true);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'U':
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_speed_limit_up_enabled, false);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'v':
|
2012-12-05 17:29:46 +00:00
|
|
|
verify = true;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'V':
|
2012-12-05 17:29:46 +00:00
|
|
|
showVersion = true;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'w':
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_download_dir, optarg);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 910:
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_encryption, TR_ENCRYPTION_REQUIRED);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 911:
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_encryption, TR_ENCRYPTION_PREFERRED);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 912:
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_encryption, TR_CLEAR_PREFERRED);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_OPT_UNK:
|
2012-12-05 17:29:46 +00:00
|
|
|
if (torrentPath == NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
torrentPath = optarg;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2012-12-05 17:29:46 +00:00
|
|
|
return 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void sigHandler(int signal)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (signal)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case SIGINT:
|
2012-12-05 17:29:46 +00:00
|
|
|
gotsig = true;
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case SIGHUP:
|
2012-12-05 17:29:46 +00:00
|
|
|
manualUpdate = true;
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-09-05 15:13:06 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
default:
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|