2010-06-16 14:27:24 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2010-06-16 14:27:24 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <stdio.h> /* fprintf () */
|
|
|
|
#include <string.h> /* strcmp (), strchr (), memcmp () */
|
|
|
|
#include <stdlib.h> /* getenv (), qsort () */
|
2010-06-16 14:27:24 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#define CURL_DISABLE_TYPECHECK /* otherwise -Wunreachable-code goes insane */
|
2010-12-12 16:43:19 +00:00
|
|
|
#include <curl/curl.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/tr-getopt.h>
|
|
|
|
#include <libtransmission/utils.h>
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <libtransmission/web.h> /* tr_webGetResponseStr () */
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
#include <libtransmission/version.h>
|
|
|
|
|
|
|
|
#define MY_NAME "transmission-show"
|
|
|
|
#define TIMEOUT_SECS 30
|
|
|
|
|
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
|
|
|
|
#define DISK_B_STR "B"
|
|
|
|
#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
|
|
|
|
2010-06-16 14:27:24 +00:00
|
|
|
static tr_option options[] =
|
|
|
|
{
|
2012-07-23 15:28:27 +00:00
|
|
|
{ 'm', "magnet", "Give a magnet link for the specified torrent", "m", 0, NULL },
|
2010-06-16 14:27:24 +00:00
|
|
|
{ 's', "scrape", "Ask the torrent's trackers how many peers are in the torrent's swarm", "s", 0, NULL },
|
2010-08-08 22:53:24 +00:00
|
|
|
{ 'V', "version", "Show version number and exit", "V", 0, NULL },
|
2010-06-16 14:27:24 +00:00
|
|
|
{ 0, NULL, NULL, NULL, 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *
|
2012-12-05 17:29:46 +00:00
|
|
|
getUsage (void)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return "Usage: " MY_NAME " [options] <.torrent file>";
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2012-07-23 15:28:27 +00:00
|
|
|
static bool magnetFlag = false;
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool scrapeFlag = false;
|
|
|
|
static bool showVersion = false;
|
2010-06-16 14:27:24 +00:00
|
|
|
const char * filename = NULL;
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
parseCommandLine (int argc, const char ** argv)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int c;
|
|
|
|
const char * optarg;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
while ((c = tr_getopt (getUsage (), argc, argv, options, &optarg)))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (c)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
case 'm':
|
|
|
|
magnetFlag = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
scrapeFlag = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V':
|
|
|
|
showVersion = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_OPT_UNK:
|
|
|
|
filename = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 1;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-23 15:28:27 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
doShowMagnet (const tr_info * inf)
|
2012-07-23 15:28:27 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * str = tr_torrentInfoGetMagnetLink (inf);
|
|
|
|
printf ("%s", str);
|
|
|
|
tr_free (str);
|
2012-07-23 15:28:27 +00:00
|
|
|
}
|
|
|
|
|
2010-11-03 16:59:24 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_files_by_name (const void * va, const void * vb)
|
2010-11-03 16:59:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const tr_file * a = * (const tr_file**)va;
|
|
|
|
const tr_file * b = * (const tr_file**)vb;
|
|
|
|
return strcmp (a->name, b->name);
|
2010-11-03 16:59:24 +00:00
|
|
|
}
|
|
|
|
|
2010-06-16 14:27:24 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
showInfo (const tr_info * inf)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int i;
|
|
|
|
char buf[128];
|
|
|
|
tr_file ** files;
|
|
|
|
int prevTier = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*** General Info
|
|
|
|
**/
|
|
|
|
|
|
|
|
printf ("GENERAL\n\n");
|
|
|
|
printf (" Name: %s\n", inf->name);
|
|
|
|
printf (" Hash: %s\n", inf->hashString);
|
|
|
|
printf (" Created by: %s\n", inf->creator ? inf->creator : "Unknown");
|
|
|
|
if (!inf->dateCreated)
|
|
|
|
{
|
|
|
|
printf (" Created on: Unknown\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct tm tm = *localtime (&inf->dateCreated);
|
|
|
|
printf (" Created on: %s", asctime (&tm));
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
if (inf->comment && *inf->comment)
|
|
|
|
printf (" Comment: %s\n", inf->comment);
|
|
|
|
printf (" Piece Count: %d\n", inf->pieceCount);
|
|
|
|
printf (" Piece Size: %s\n", tr_formatter_mem_B (buf, inf->pieceSize, sizeof (buf)));
|
|
|
|
printf (" Total Size: %s\n", tr_formatter_size_B (buf, inf->totalSize, sizeof (buf)));
|
|
|
|
printf (" Privacy: %s\n", inf->isPrivate ? "Private torrent" : "Public torrent");
|
|
|
|
|
|
|
|
/**
|
|
|
|
*** Trackers
|
|
|
|
**/
|
|
|
|
|
|
|
|
printf ("\nTRACKERS\n");
|
|
|
|
for (i=0; i<inf->trackerCount; ++i)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (prevTier != inf->trackers[i].tier)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
prevTier = inf->trackers[i].tier;
|
|
|
|
printf ("\n Tier #%d\n", prevTier + 1);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
printf (" %s\n", inf->trackers[i].announce);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
2011-01-05 16:15:41 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (inf->webseedCount > 0)
|
2011-01-05 16:15:41 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
printf ("\nWEBSEEDS\n\n");
|
2011-01-05 16:15:41 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<inf->webseedCount; ++i)
|
|
|
|
printf (" %s\n", inf->webseeds[i]);
|
2011-01-05 16:15:41 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/**
|
|
|
|
*** Files
|
|
|
|
**/
|
|
|
|
|
|
|
|
printf ("\nFILES\n\n");
|
|
|
|
files = tr_new (tr_file*, inf->fileCount);
|
|
|
|
for (i=0; i< (int)inf->fileCount; ++i)
|
|
|
|
files[i] = &inf->files[i];
|
|
|
|
qsort (files, inf->fileCount, sizeof (tr_file*), compare_files_by_name);
|
|
|
|
for (i=0; i< (int)inf->fileCount; ++i)
|
|
|
|
printf (" %s (%s)\n", files[i]->name, tr_formatter_size_B (buf, files[i]->length, sizeof (buf)));
|
|
|
|
tr_free (files);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2012-12-05 17:29:46 +00:00
|
|
|
writeFunc (void * ptr, size_t size, size_t nmemb, void * buf)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const size_t byteCount = size * nmemb;
|
|
|
|
evbuffer_add (buf, ptr, byteCount);
|
|
|
|
return byteCount;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURL*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_curl_easy_init (struct evbuffer * writebuf)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
CURL * curl = curl_easy_init ();
|
|
|
|
curl_easy_setopt (curl, CURLOPT_USERAGENT, MY_NAME "/" LONG_VERSION_STRING);
|
|
|
|
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writeFunc);
|
|
|
|
curl_easy_setopt (curl, CURLOPT_WRITEDATA, writebuf);
|
|
|
|
curl_easy_setopt (curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
|
|
|
|
curl_easy_setopt (curl, CURLOPT_VERBOSE, getenv ("TR_CURL_VERBOSE") != NULL);
|
|
|
|
curl_easy_setopt (curl, CURLOPT_ENCODING, "");
|
|
|
|
return curl;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
doScrape (const tr_info * inf)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int i;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<inf->trackerCount; ++i)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
CURL * curl;
|
|
|
|
CURLcode res;
|
|
|
|
struct evbuffer * buf;
|
|
|
|
const char * scrape = inf->trackers[i].scrape;
|
|
|
|
char * url;
|
|
|
|
char escaped[SHA_DIGEST_LENGTH*3 + 1];
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (scrape == NULL)
|
|
|
|
continue;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_http_escape_sha1 (escaped, inf->hash);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
url = tr_strdup_printf ("%s%cinfo_hash=%s",
|
|
|
|
scrape,
|
|
|
|
strchr (scrape, '?') ? '&' : '?',
|
|
|
|
escaped);
|
2010-06-17 02:49:39 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
printf ("%s ... ", url);
|
|
|
|
fflush (stdout);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
buf = evbuffer_new ();
|
|
|
|
curl = tr_curl_easy_init (buf);
|
|
|
|
curl_easy_setopt (curl, CURLOPT_URL, url);
|
|
|
|
curl_easy_setopt (curl, CURLOPT_TIMEOUT, TIMEOUT_SECS);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((res = curl_easy_perform (curl)))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
printf ("error: %s\n", curl_easy_strerror (res));
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
long response;
|
|
|
|
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &response);
|
|
|
|
if (response != 200)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
printf ("error: unexpected response %ld \"%s\"\n",
|
|
|
|
response,
|
|
|
|
tr_webGetResponseStr (response));
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else /* HTTP OK */
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant top;
|
|
|
|
tr_variant * files;
|
2012-12-05 17:29:46 +00:00
|
|
|
bool matched = false;
|
|
|
|
const char * begin = (const char*) evbuffer_pullup (buf, -1);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (!tr_variantFromBenc (&top, begin, evbuffer_get_length(buf)))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindDict (&top, TR_KEY_files, &files))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int i = 0;
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_quark key;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * val;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
while (tr_variantDictChild (files, i++, &key, &val))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!memcmp (inf->hash, tr_quark_get_string(key,NULL), SHA_DIGEST_LENGTH))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int64_t seeders = -1;
|
|
|
|
int64_t leechers = -1;
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictFindInt (val, TR_KEY_complete, &seeders);
|
|
|
|
tr_variantDictFindInt (val, TR_KEY_incomplete, &leechers);
|
2012-12-05 17:29:46 +00:00
|
|
|
printf ("%d seeders, %d leechers\n", (int)seeders, (int)leechers);
|
|
|
|
matched = true;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&top);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!matched)
|
|
|
|
printf ("no match\n");
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
curl_easy_cleanup (curl);
|
|
|
|
evbuffer_free (buf);
|
|
|
|
tr_free (url);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
main (int argc, char * argv[])
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int err;
|
|
|
|
tr_info inf;
|
|
|
|
tr_ctor * ctor;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_setMessageLevel (TR_MSG_ERR);
|
|
|
|
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);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (parseCommandLine (argc, (const char**)argv))
|
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (showVersion)
|
2010-08-08 22:53:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
fprintf (stderr, MY_NAME" "LONG_VERSION_STRING"\n");
|
|
|
|
return EXIT_SUCCESS;
|
2010-08-08 22:53:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* make sure the user specified a filename */
|
|
|
|
if (!filename)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
fprintf (stderr, "ERROR: No .torrent file specified.\n");
|
|
|
|
tr_getopt_usage (MY_NAME, getUsage (), options);
|
|
|
|
fprintf (stderr, "\n");
|
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* try to parse the .torrent file */
|
|
|
|
ctor = tr_ctorNew (NULL);
|
|
|
|
tr_ctorSetMetainfoFromFile (ctor, filename);
|
|
|
|
err = tr_torrentParse (ctor, &inf);
|
|
|
|
tr_ctorFree (ctor);
|
|
|
|
if (err)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
fprintf (stderr, "Error parsing .torrent file \"%s\"\n", filename);
|
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (magnetFlag)
|
2012-07-23 15:28:27 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
doShowMagnet (&inf);
|
2012-07-23 15:28:27 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else
|
2012-07-23 15:28:27 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
printf ("Name: %s\n", inf.name);
|
|
|
|
printf ("File: %s\n", filename);
|
|
|
|
printf ("\n");
|
|
|
|
fflush (stdout);
|
|
|
|
|
|
|
|
if (scrapeFlag)
|
|
|
|
doScrape (&inf);
|
|
|
|
else
|
|
|
|
showInfo (&inf);
|
2012-07-23 15:28:27 +00:00
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* cleanup */
|
|
|
|
putc ('\n', stdout);
|
|
|
|
tr_metainfoFree (&inf);
|
|
|
|
return EXIT_SUCCESS;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|