1 | /* |
---|
2 | * This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com> |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: remote.c 6334 2008-07-15 17:16:57Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <errno.h> |
---|
14 | #include <stdio.h> |
---|
15 | #include <stdlib.h> |
---|
16 | #include <string.h> /* strcmp */ |
---|
17 | |
---|
18 | #include <unistd.h> /* getcwd */ |
---|
19 | |
---|
20 | #include <libevent/event.h> |
---|
21 | #include <curl/curl.h> |
---|
22 | |
---|
23 | #include <libtransmission/transmission.h> |
---|
24 | #include <libtransmission/bencode.h> |
---|
25 | #include <libtransmission/rpc.h> |
---|
26 | #include <libtransmission/json.h> |
---|
27 | #include <libtransmission/tr-getopt.h> |
---|
28 | #include <libtransmission/utils.h> |
---|
29 | #include <libtransmission/version.h> |
---|
30 | |
---|
31 | #define MY_NAME "transmission-remote" |
---|
32 | #define DEFAULT_HOST "localhost" |
---|
33 | #define DEFAULT_PORT TR_DEFAULT_RPC_PORT |
---|
34 | |
---|
35 | enum { TAG_LIST, TAG_DETAILS, TAG_FILES }; |
---|
36 | |
---|
37 | static const char* |
---|
38 | getUsage( void ) |
---|
39 | { |
---|
40 | return |
---|
41 | "Transmission "LONG_VERSION_STRING" http://www.transmissionbt.com/\n" |
---|
42 | "A fast and easy BitTorrent client\n" |
---|
43 | "\n" |
---|
44 | "Usage: "MY_NAME" [host] [options]\n" |
---|
45 | " "MY_NAME" [port] [options]\n" |
---|
46 | " "MY_NAME" [host:port] [options]\n" |
---|
47 | "\n" |
---|
48 | "See the man page for detailed explanations and many examples."; |
---|
49 | } |
---|
50 | |
---|
51 | static tr_option opts[] = |
---|
52 | { |
---|
53 | { 'a', "add", "Add torrent files", "a", 0, NULL }, |
---|
54 | { 'b', "debug", "Print debugging information", "b", 0, NULL }, |
---|
55 | { 'd', "downlimit", "Set the maximum download speed in KB/s", "d", 1, "<number>" }, |
---|
56 | { 'D', "no-downlimit", "Don't limit the download speed", "D", 0, NULL }, |
---|
57 | { 910, "encryption-required", "Encrypt all peer connections", "er", 0, NULL }, |
---|
58 | { 911, "encryption-preferred", "Prefer encrypted peer connections", "ep", 0, NULL }, |
---|
59 | { 912, "encryption-tolerated", "Prefer unencrypted peer connections", "et", 0, NULL }, |
---|
60 | { 'f', "files", "List the current torrent's files", "f", 0, NULL }, |
---|
61 | { 'g', "get", "Mark files for download", "g", 1, "<files>" }, |
---|
62 | { 'G', "no-get", "Mark files for not downloading", "G", 1, "<files>" }, |
---|
63 | { 'h', "help", "Show this help page and exit", "h", 0, NULL }, |
---|
64 | { 'i', "info", "Show details of the current torrent(s)", "i", 0, NULL }, |
---|
65 | { 'l', "list", "List all torrents", "l", 0, NULL }, |
---|
66 | { 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", 0, NULL }, |
---|
67 | { 'M', "no-portmap", "Disable portmapping", "M", 0, NULL }, |
---|
68 | { 'n', "auth", "Set username for authentication", "n", 1, "<auth>" }, |
---|
69 | { 'p', "port", "Port to listen for incoming peers", "p", 1, "<port>" }, |
---|
70 | { 900, "priority-high", "Set the files' priorities as high", "ph", 1, "<files>" }, |
---|
71 | { 901, "priority-normal", "Set the files' priorities as normal", "pn", 1, "<files>" }, |
---|
72 | { 902, "priority-normal", "Set the files' priorities as low", "pl", 1, "<files>" }, |
---|
73 | { 'r', "remove", "Remove the current torrent(s)", "r", 0, NULL }, |
---|
74 | { 's', "start", "Start the current torrent(s)", "s", 0, NULL }, |
---|
75 | { 'S', "stop", "Stop the current torrent(s)", "S", 0, NULL }, |
---|
76 | { 't', "torrent", "Set the current torrent(s)", "t", 1, "<torrents>" }, |
---|
77 | { 'u', "uplimit", "Set the maximum upload speed in KB/s", "u", 1, "<number>" }, |
---|
78 | { 'U', "no-uplimit", "Don't limit the upload speed", "U", 0, NULL }, |
---|
79 | { 'v', "verify", "Verify the current torrent(s)", "v", 0, NULL }, |
---|
80 | { 'w', "download-dir", "Set the default download folder", "w", 1, "<path>" }, |
---|
81 | { 'x', "pex", "Enable peer exchange (PEX)", "x", 0, NULL }, |
---|
82 | { 'X', "no-pex", "Disable peer exchange (PEX)", "X", 0, NULL }, |
---|
83 | { 0, NULL, NULL, NULL, 0, NULL } |
---|
84 | }; |
---|
85 | |
---|
86 | static void |
---|
87 | showUsage( void ) |
---|
88 | { |
---|
89 | tr_getopt_usage( MY_NAME, getUsage(), opts ); |
---|
90 | exit( 0 ); |
---|
91 | } |
---|
92 | |
---|
93 | static int |
---|
94 | numarg( const char * arg ) |
---|
95 | { |
---|
96 | char * end = NULL; |
---|
97 | const long num = strtol( arg, &end, 10 ); |
---|
98 | if( *end ) { |
---|
99 | fprintf( stderr, "Not a number: \"%s\"\n", arg ); |
---|
100 | showUsage( ); |
---|
101 | } |
---|
102 | return num; |
---|
103 | } |
---|
104 | |
---|
105 | static char * reqs[256]; /* arbitrary max */ |
---|
106 | static int reqCount = 0; |
---|
107 | static int debug = 0; |
---|
108 | static char * auth = NULL; |
---|
109 | |
---|
110 | static char* |
---|
111 | absolutify( char * buf, size_t len, const char * path ) |
---|
112 | { |
---|
113 | if( *path == '/' ) |
---|
114 | tr_strlcpy( buf, path, len ); |
---|
115 | else { |
---|
116 | char cwd[MAX_PATH_LENGTH]; |
---|
117 | getcwd( cwd, sizeof( cwd ) ); |
---|
118 | tr_buildPath( buf, len, cwd, path, NULL ); |
---|
119 | } |
---|
120 | return buf; |
---|
121 | } |
---|
122 | |
---|
123 | static char* |
---|
124 | getEncodedMetainfo( const char * filename ) |
---|
125 | { |
---|
126 | size_t len = 0; |
---|
127 | uint8_t * buf = tr_loadFile( filename, &len ); |
---|
128 | char * b64 = tr_base64_encode( buf, len, NULL ); |
---|
129 | tr_free( buf ); |
---|
130 | return b64; |
---|
131 | } |
---|
132 | |
---|
133 | static void |
---|
134 | addIdArg( tr_benc * args, const char * id ) |
---|
135 | { |
---|
136 | if( !*id ) { |
---|
137 | fprintf( stderr, "No torrent specified! Please use the -t option first.\n" ); |
---|
138 | id = "-1"; /* no torrent will have this ID, so should be a no-op */ |
---|
139 | } |
---|
140 | if( strcmp( id, "all" ) ) { |
---|
141 | tr_rpc_parse_list_str( tr_bencDictAdd( args, "ids" ), id, strlen(id) ); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | static void |
---|
146 | addFiles( tr_benc * args, const char * key, const char * arg ) |
---|
147 | { |
---|
148 | tr_benc * files = tr_bencDictAddList( args, key, 100 ); |
---|
149 | |
---|
150 | if( !*arg ) |
---|
151 | { |
---|
152 | fprintf( stderr, "No files specified!\n" ); |
---|
153 | arg = "-1"; /* no file will have this index, so should be a no-op */ |
---|
154 | } |
---|
155 | if( strcmp( arg, "all" ) ) |
---|
156 | { |
---|
157 | const char * walk = arg; |
---|
158 | while( *walk ) { |
---|
159 | char * p; |
---|
160 | unsigned long l; |
---|
161 | errno = 0; |
---|
162 | l = strtol( walk, &p, 10 ); |
---|
163 | if( errno ) |
---|
164 | break; |
---|
165 | tr_bencListAddInt( files, l - 1 ); |
---|
166 | if( *p != ',' ) |
---|
167 | break; |
---|
168 | walk = p + 1; |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | static void |
---|
174 | readargs( int argc, const char ** argv ) |
---|
175 | { |
---|
176 | int c; |
---|
177 | int addingTorrents = 0; |
---|
178 | const char * optarg; |
---|
179 | char id[4096]; |
---|
180 | |
---|
181 | *id = '\0'; |
---|
182 | |
---|
183 | while(( c = tr_getopt( getUsage(), argc, argv, opts, &optarg ))) |
---|
184 | { |
---|
185 | char buf[MAX_PATH_LENGTH]; |
---|
186 | int addArg = TRUE; |
---|
187 | tr_benc top, *args; |
---|
188 | tr_bencInitDict( &top, 3 ); |
---|
189 | int64_t fields = 0; |
---|
190 | args = tr_bencDictAddDict( &top, "arguments", 0 ); |
---|
191 | |
---|
192 | switch( c ) |
---|
193 | { |
---|
194 | case TR_OPT_UNK: |
---|
195 | if( addingTorrents ) { |
---|
196 | char * tmp = getEncodedMetainfo( optarg ); |
---|
197 | tr_bencDictAddStr( &top, "method", "torrent-add" ); |
---|
198 | tr_bencDictAddStr( args, "metainfo", tmp ); |
---|
199 | tr_free( tmp ); |
---|
200 | } else { |
---|
201 | fprintf( stderr, "Unknown option: %s\n", optarg ); |
---|
202 | addArg = FALSE; |
---|
203 | } |
---|
204 | break; |
---|
205 | case 'a': addingTorrents = 1; |
---|
206 | addArg = FALSE; |
---|
207 | break; |
---|
208 | case 'b': debug = 1; |
---|
209 | addArg = FALSE; |
---|
210 | break; |
---|
211 | case 'd': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
212 | tr_bencDictAddInt( args, "speed-limit-down", numarg( optarg ) ); |
---|
213 | tr_bencDictAddInt( args, "speed-limit-down-enabled", 1 ); |
---|
214 | break; |
---|
215 | case 'D': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
216 | tr_bencDictAddInt( args, "speed-limit-down-enabled", 0 ); |
---|
217 | break; |
---|
218 | case 'f': tr_bencDictAddStr( &top, "method", "torrent-get" ); |
---|
219 | tr_bencDictAddInt( &top, "tag", TAG_FILES ); |
---|
220 | addIdArg( args, id ); |
---|
221 | fields = TR_RPC_TORRENT_ID |
---|
222 | | TR_RPC_TORRENT_FILES |
---|
223 | | TR_RPC_TORRENT_PRIORITIES; |
---|
224 | tr_bencDictAddInt( args, "fields", fields ); |
---|
225 | break; |
---|
226 | case 'g': tr_bencDictAddStr( &top, "method", "torrent-set" ); |
---|
227 | addIdArg( args, id ); |
---|
228 | addFiles( args, "files-wanted", optarg ); |
---|
229 | break; |
---|
230 | case 'G': tr_bencDictAddStr( &top, "method", "torrent-set" ); |
---|
231 | addIdArg( args, id ); |
---|
232 | addFiles( args, "files-unwanted", optarg ); |
---|
233 | break; |
---|
234 | case 'i': tr_bencDictAddStr( &top, "method", "torrent-get" ); |
---|
235 | tr_bencDictAddInt( &top, "tag", TAG_DETAILS ); |
---|
236 | addIdArg( args, id ); |
---|
237 | fields = TR_RPC_TORRENT_ACTIVITY |
---|
238 | | TR_RPC_TORRENT_ANNOUNCE |
---|
239 | | TR_RPC_TORRENT_ERROR |
---|
240 | | TR_RPC_TORRENT_HISTORY |
---|
241 | | TR_RPC_TORRENT_ID |
---|
242 | | TR_RPC_TORRENT_INFO |
---|
243 | | TR_RPC_TORRENT_SCRAPE |
---|
244 | | TR_RPC_TORRENT_SIZE |
---|
245 | | TR_RPC_TORRENT_TRACKER_STATS; |
---|
246 | tr_bencDictAddInt( args, "fields", fields ); |
---|
247 | break; |
---|
248 | case 'l': tr_bencDictAddStr( &top, "method", "torrent-get" ); |
---|
249 | tr_bencDictAddInt( &top, "tag", TAG_LIST ); |
---|
250 | fields = TR_RPC_TORRENT_ID |
---|
251 | | TR_RPC_TORRENT_ACTIVITY |
---|
252 | | TR_RPC_TORRENT_HISTORY |
---|
253 | | TR_RPC_TORRENT_SIZE; |
---|
254 | tr_bencDictAddInt( args, "fields", fields ); |
---|
255 | break; |
---|
256 | case 'm': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
257 | tr_bencDictAddInt( args, "port-forwarding-enabled", 1 ); |
---|
258 | break; |
---|
259 | case 'M': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
260 | tr_bencDictAddInt( args, "port-forwarding-enabled", 0 ); |
---|
261 | break; |
---|
262 | case 'n': auth = tr_strdup( optarg ); |
---|
263 | addArg = FALSE; |
---|
264 | break; |
---|
265 | case 'p': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
266 | tr_bencDictAddInt( args, "port", numarg( optarg ) ); |
---|
267 | break; |
---|
268 | case 'r': tr_bencDictAddStr( &top, "method", "torrent-remove" ); |
---|
269 | addIdArg( args, id ); |
---|
270 | break; |
---|
271 | case 's': tr_bencDictAddStr( &top, "method", "torrent-start" ); |
---|
272 | addIdArg( args, id ); |
---|
273 | break; |
---|
274 | case 'S': tr_bencDictAddStr( &top, "method", "torrent-stop" ); |
---|
275 | addIdArg( args, id ); |
---|
276 | break; |
---|
277 | case 't': tr_strlcpy( id, optarg, sizeof( id ) ); |
---|
278 | addArg = FALSE; |
---|
279 | break; |
---|
280 | case 'u': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
281 | tr_bencDictAddInt( args, "speed-limit-up", numarg( optarg ) ); |
---|
282 | tr_bencDictAddInt( args, "speed-limit-up-enabled", 1 ); |
---|
283 | break; |
---|
284 | case 'U': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
285 | tr_bencDictAddInt( args, "speed-limit-up-enabled", 0 ); |
---|
286 | break; |
---|
287 | case 'v': tr_bencDictAddStr( &top, "method", "torrent-verify" ); |
---|
288 | addIdArg( args, id ); |
---|
289 | break; |
---|
290 | case 'w': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
291 | tr_bencDictAddStr( args, "download-dir", |
---|
292 | absolutify(buf,sizeof(buf),optarg) ); |
---|
293 | break; |
---|
294 | case 'x': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
295 | tr_bencDictAddInt( args, "pex-allowed", 1 ); |
---|
296 | break; |
---|
297 | case 'X': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
298 | tr_bencDictAddInt( args, "pex-allowed", 0 ); |
---|
299 | break; |
---|
300 | case 900: tr_bencDictAddStr( &top, "method", "torrent-set" ); |
---|
301 | addIdArg( args, id ); |
---|
302 | addFiles( args, "priority-high", optarg ); |
---|
303 | break; |
---|
304 | case 901: tr_bencDictAddStr( &top, "method", "torrent-set" ); |
---|
305 | addIdArg( args, id ); |
---|
306 | addFiles( args, "priority-normal", optarg ); |
---|
307 | break; |
---|
308 | case 902: tr_bencDictAddStr( &top, "method", "torrent-set" ); |
---|
309 | addIdArg( args, id ); |
---|
310 | addFiles( args, "priority-low", optarg ); |
---|
311 | break; |
---|
312 | case 910: tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
313 | tr_bencDictAddStr( args, "encryption", "required" ); |
---|
314 | break; |
---|
315 | case 911: tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
316 | tr_bencDictAddStr( args, "encryption", "preferred" ); |
---|
317 | break; |
---|
318 | case 912: tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
319 | tr_bencDictAddStr( args, "encryption", "tolerated" ); |
---|
320 | break; |
---|
321 | default: fprintf( stderr, "got opt [%d]\n", (int)c ); |
---|
322 | showUsage( ); |
---|
323 | break; |
---|
324 | } |
---|
325 | |
---|
326 | if( addArg ) |
---|
327 | reqs[reqCount++] = tr_bencSaveAsJSON( &top, NULL ); |
---|
328 | tr_bencFree( &top ); |
---|
329 | } |
---|
330 | } |
---|
331 | |
---|
332 | /* [host:port] or [host] or [port] */ |
---|
333 | static void |
---|
334 | getHostAndPort( int * argc, char ** argv, char ** host, int * port ) |
---|
335 | { |
---|
336 | if( *argv[1] != '-' ) |
---|
337 | { |
---|
338 | int i; |
---|
339 | const char * s = argv[1]; |
---|
340 | const char * delim = strchr( s, ':' ); |
---|
341 | if( delim ) { /* user passed in both host and port */ |
---|
342 | *host = tr_strndup( s, delim-s ); |
---|
343 | *port = atoi( delim+1 ); |
---|
344 | } else { |
---|
345 | char * end; |
---|
346 | const int i = strtol( s, &end, 10 ); |
---|
347 | if( !*end ) /* user passed in a port */ |
---|
348 | *port = i; |
---|
349 | else /* user passed in a host */ |
---|
350 | *host = tr_strdup( s ); |
---|
351 | } |
---|
352 | |
---|
353 | *argc -= 1; |
---|
354 | for( i=1; i<*argc; ++i ) |
---|
355 | argv[i] = argv[i+1]; |
---|
356 | } |
---|
357 | } |
---|
358 | |
---|
359 | static size_t |
---|
360 | writeFunc( void * ptr, size_t size, size_t nmemb, void * buf ) |
---|
361 | { |
---|
362 | const size_t byteCount = size * nmemb; |
---|
363 | evbuffer_add( buf, ptr, byteCount ); |
---|
364 | return byteCount; |
---|
365 | } |
---|
366 | |
---|
367 | static void |
---|
368 | etaToString( char * buf, size_t buflen, int64_t eta ) |
---|
369 | { |
---|
370 | if( eta < 0 ) tr_snprintf( buf, buflen, "Unknown" ); |
---|
371 | else if( eta < 60 ) tr_snprintf( buf, buflen, "%"PRId64"sec", eta ); |
---|
372 | else if( eta < (60*60) ) tr_snprintf( buf, buflen, "%"PRId64" min", eta/60 ); |
---|
373 | else if( eta < (60*60*24) ) tr_snprintf( buf, buflen, "%"PRId64" hrs", eta/(60*60) ); |
---|
374 | else tr_snprintf( buf, buflen, "%"PRId64" days", eta/(60*60*24) ); |
---|
375 | } |
---|
376 | |
---|
377 | #define KILOBYTE_FACTOR 1024.0 |
---|
378 | #define MEGABYTE_FACTOR (1024.0 * 1024.0) |
---|
379 | #define GIGABYTE_FACTOR (1024.0 * 1024.0 * 1024.0) |
---|
380 | |
---|
381 | static char* |
---|
382 | strlratio( char * buf, double numerator, double denominator, size_t buflen ) |
---|
383 | { |
---|
384 | if( denominator ) |
---|
385 | { |
---|
386 | const double ratio = numerator / denominator; |
---|
387 | if( ratio < 10.0 ) |
---|
388 | tr_snprintf( buf, buflen, "%'.2f", ratio ); |
---|
389 | else if( ratio < 100.0 ) |
---|
390 | tr_snprintf( buf, buflen, "%'.1f", ratio ); |
---|
391 | else |
---|
392 | tr_snprintf( buf, buflen, "%'.0f", ratio ); |
---|
393 | } |
---|
394 | else if( numerator ) |
---|
395 | tr_strlcpy( buf, "Infinity", buflen ); |
---|
396 | else |
---|
397 | tr_strlcpy( buf, "None", buflen ); |
---|
398 | return buf; |
---|
399 | } |
---|
400 | |
---|
401 | static char* |
---|
402 | strlsize( char * buf, int64_t size, size_t buflen ) |
---|
403 | { |
---|
404 | if( !size ) |
---|
405 | tr_strlcpy( buf, "None", buflen ); |
---|
406 | else if( size < (int64_t)KILOBYTE_FACTOR ) |
---|
407 | tr_snprintf( buf, buflen, "%'"PRId64" bytes", (int64_t)size ); |
---|
408 | else { |
---|
409 | double displayed_size; |
---|
410 | if (size < (int64_t)MEGABYTE_FACTOR) { |
---|
411 | displayed_size = (double) size / KILOBYTE_FACTOR; |
---|
412 | tr_snprintf( buf, buflen, "%'.1f KB", displayed_size ); |
---|
413 | } else if (size < (int64_t)GIGABYTE_FACTOR) { |
---|
414 | displayed_size = (double) size / MEGABYTE_FACTOR; |
---|
415 | tr_snprintf( buf, buflen, "%'.1f MB", displayed_size ); |
---|
416 | } else { |
---|
417 | displayed_size = (double) size / GIGABYTE_FACTOR; |
---|
418 | tr_snprintf( buf, buflen, "%'.1f GB", displayed_size ); |
---|
419 | } |
---|
420 | } |
---|
421 | return buf; |
---|
422 | } |
---|
423 | |
---|
424 | static const char* |
---|
425 | torrentStatusToString( int i ) |
---|
426 | { |
---|
427 | switch( i ) |
---|
428 | { |
---|
429 | case TR_STATUS_CHECK_WAIT: return "Will Verify"; |
---|
430 | case TR_STATUS_CHECK: return "Verifying"; |
---|
431 | case TR_STATUS_DOWNLOAD: return "Downloading"; |
---|
432 | case TR_STATUS_SEED: return "Seeding"; |
---|
433 | case TR_STATUS_STOPPED: return "Stopped"; |
---|
434 | default: return "Error"; |
---|
435 | } |
---|
436 | } |
---|
437 | |
---|
438 | static int |
---|
439 | isVerifying( int status ) |
---|
440 | { |
---|
441 | return ( ( status == TR_STATUS_CHECK_WAIT ) || |
---|
442 | ( status == TR_STATUS_CHECK ) ); |
---|
443 | } |
---|
444 | |
---|
445 | static void |
---|
446 | printDetails( tr_benc * top ) |
---|
447 | { |
---|
448 | tr_benc *args, *torrents; |
---|
449 | |
---|
450 | if( ( tr_bencDictFindDict( top, "arguments", &args ) ) && |
---|
451 | ( tr_bencDictFindList( args, "torrents", &torrents ) ) ) |
---|
452 | { |
---|
453 | int ti, tCount; |
---|
454 | for( ti=0, tCount=tr_bencListSize( torrents ); ti<tCount; ++ti ) |
---|
455 | { |
---|
456 | tr_benc * t = tr_bencListChild( torrents, ti ); |
---|
457 | const char * str; |
---|
458 | char buf[512]; |
---|
459 | char buf2[512]; |
---|
460 | int64_t i, j; |
---|
461 | |
---|
462 | printf( "NAME\n" ); |
---|
463 | if( tr_bencDictFindInt( t, "id", &i ) ) |
---|
464 | printf( " Id: %"PRId64"\n", i ); |
---|
465 | if( tr_bencDictFindStr( t, "name", &str ) ) |
---|
466 | printf( " Name: %s\n", str ); |
---|
467 | if( tr_bencDictFindStr( t, "hashString", &str ) ) |
---|
468 | printf( " Hash: %s\n", str ); |
---|
469 | printf( "\n" ); |
---|
470 | |
---|
471 | printf( "TRANSFER\n" ); |
---|
472 | if( tr_bencDictFindInt( t, "status", &i ) ) |
---|
473 | { |
---|
474 | if( isVerifying( i ) && tr_bencDictFindStr( t, "recheckProgress", &str ) ) |
---|
475 | tr_snprintf( buf, sizeof( buf ), " (%.0f%% Done)", 100.0*atof(str) ); |
---|
476 | else |
---|
477 | *buf = '\0'; |
---|
478 | printf( " State: %s%s\n", torrentStatusToString( i ), buf ); |
---|
479 | |
---|
480 | } |
---|
481 | |
---|
482 | if( tr_bencDictFindInt( t, "sizeWhenDone", &i ) && |
---|
483 | tr_bencDictFindInt( t, "leftUntilDone", &j ) ) |
---|
484 | { |
---|
485 | strlratio( buf, 100.0*(i-j), i, sizeof( buf ) ); |
---|
486 | printf( " Percent Done: %s%%\n", buf ); |
---|
487 | } |
---|
488 | |
---|
489 | if( tr_bencDictFindInt( t, "eta", &i ) ) { |
---|
490 | etaToString( buf, sizeof( buf ), i ); |
---|
491 | printf( " ETA: %s\n", buf ); |
---|
492 | } |
---|
493 | if( tr_bencDictFindInt( t, "rateDownload", &i ) ) |
---|
494 | printf( " Download Speed: %.1f KB/s\n", i/1024.0 ); |
---|
495 | if( tr_bencDictFindInt( t, "rateUpload", &i ) ) |
---|
496 | printf( " Upload Speed: %.1f KB/s\n", i/1024.0 ); |
---|
497 | if( tr_bencDictFindInt( t, "haveUnchecked", &i ) && |
---|
498 | tr_bencDictFindInt( t, "haveValid", &j ) ) |
---|
499 | { |
---|
500 | strlsize( buf, i+j, sizeof( buf ) ); |
---|
501 | strlsize( buf2, j, sizeof( buf2 ) ); |
---|
502 | printf( " Have: %s (%s verified)\n", buf, buf2 ); |
---|
503 | } |
---|
504 | |
---|
505 | if( tr_bencDictFindInt( t, "sizeWhenDone", &i ) && |
---|
506 | tr_bencDictFindInt( t, "totalSize", &j ) ) |
---|
507 | { |
---|
508 | strlsize( buf, j, sizeof( buf ) ); |
---|
509 | strlsize( buf2, i, sizeof( buf2 ) ); |
---|
510 | printf( " Total size: %s (%s wanted)\n", buf, buf2 ); |
---|
511 | } |
---|
512 | if( tr_bencDictFindInt( t, "downloadedEver", &i ) && |
---|
513 | tr_bencDictFindInt( t, "uploadedEver", &j ) ) { |
---|
514 | strlsize( buf, i, sizeof( buf ) ); |
---|
515 | printf( " Downloaded: %s\n", buf ); |
---|
516 | strlsize( buf, j, sizeof( buf ) ); |
---|
517 | printf( " Uploaded: %s\n", buf ); |
---|
518 | strlratio( buf, i, j, sizeof( buf ) ); |
---|
519 | printf( " Ratio: %s\n", buf ); |
---|
520 | } |
---|
521 | if( tr_bencDictFindInt( t, "corruptEver", &i ) ) { |
---|
522 | strlsize( buf, i, sizeof( buf ) ); |
---|
523 | printf( " Corrupt DL: %s\n", buf ); |
---|
524 | } |
---|
525 | if( tr_bencDictFindStr( t, "errorString", &str ) && str && *str ) |
---|
526 | printf( " Error: %s\n", str ); |
---|
527 | printf( "\n" ); |
---|
528 | |
---|
529 | printf( "HISTORY\n" ); |
---|
530 | if( tr_bencDictFindInt( t, "addedDate", &i ) && i ) { |
---|
531 | const time_t tt = i; |
---|
532 | printf( " Date added: %s", ctime( &tt ) ); |
---|
533 | } |
---|
534 | if( tr_bencDictFindInt( t, "doneDate", &i ) && i ) { |
---|
535 | const time_t tt = i; |
---|
536 | printf( " Date finished: %s", ctime( &tt ) ); |
---|
537 | } |
---|
538 | if( tr_bencDictFindInt( t, "startDate", &i ) && i ) { |
---|
539 | const time_t tt = i; |
---|
540 | printf( " Date started: %s", ctime( &tt ) ); |
---|
541 | } |
---|
542 | if( tr_bencDictFindInt( t, "activityDate", &i ) && i ) { |
---|
543 | const time_t tt = i; |
---|
544 | printf( " Latest activity: %s", ctime( &tt ) ); |
---|
545 | } |
---|
546 | printf( "\n" ); |
---|
547 | |
---|
548 | printf( "TRACKER\n" ); |
---|
549 | if( tr_bencDictFindInt( t, "lastAnnounceTime", &i ) && i ) { |
---|
550 | const time_t tt = i; |
---|
551 | printf( " Latest announce: %s", ctime( &tt ) ); |
---|
552 | } |
---|
553 | if( tr_bencDictFindStr( t, "announceURL", &str ) ) |
---|
554 | printf( " Announce URL: %s\n", str ); |
---|
555 | if( tr_bencDictFindStr( t, "announceResponse", &str ) && str && *str ) |
---|
556 | printf( " Announce response: %s\n", str ); |
---|
557 | if( tr_bencDictFindInt( t, "nextAnnounceTime", &i ) && i ) { |
---|
558 | const time_t tt = i; |
---|
559 | printf( " Next announce: %s", ctime( &tt ) ); |
---|
560 | } |
---|
561 | if( tr_bencDictFindInt( t, "lastScrapeTime", &i ) && i ) { |
---|
562 | const time_t tt = i; |
---|
563 | printf( " Latest scrape: %s", ctime( &tt ) ); |
---|
564 | } |
---|
565 | if( tr_bencDictFindStr( t, "scrapeResponse", &str ) ) |
---|
566 | printf( " Scrape response: %s\n", str ); |
---|
567 | if( tr_bencDictFindInt( t, "nextScrapeTime", &i ) && i ) { |
---|
568 | const time_t tt = i; |
---|
569 | printf( " Next scrape: %s", ctime( &tt ) ); |
---|
570 | } |
---|
571 | if( tr_bencDictFindInt( t, "seeders", &i ) && |
---|
572 | tr_bencDictFindInt( t, "leechers", &j ) ) |
---|
573 | printf( " Tracker knows of %"PRId64" seeders and %"PRId64" leechers\n", i, j ); |
---|
574 | if( tr_bencDictFindInt( t, "timesCompleted", &i ) ) |
---|
575 | printf( " Tracker has seen %"PRId64" clients complete this torrent\n", i ); |
---|
576 | printf( "\n" ); |
---|
577 | |
---|
578 | printf( "ORIGINS\n" ); |
---|
579 | if( tr_bencDictFindInt( t, "dateCreated", &i ) && i ) { |
---|
580 | const time_t tt = i; |
---|
581 | printf( " Date created: %s", ctime( &tt ) ); |
---|
582 | } |
---|
583 | if( tr_bencDictFindInt( t, "isPrivate", &i ) ) |
---|
584 | printf( " Public torrent: %s\n", ( i ? "No" : "Yes" ) ); |
---|
585 | if( tr_bencDictFindStr( t, "comment", &str ) && str && *str ) |
---|
586 | printf( " Comment: %s\n", str ); |
---|
587 | if( tr_bencDictFindStr( t, "creator", &str ) && str && *str ) |
---|
588 | printf( " Creator: %s\n", str ); |
---|
589 | if( tr_bencDictFindInt( t, "pieceCount", &i ) ) |
---|
590 | printf( " Piece Count: %"PRId64"\n", i ); |
---|
591 | if( tr_bencDictFindInt( t, "pieceSize", &i ) ) |
---|
592 | printf( " Piece Size: %"PRId64"\n", i ); |
---|
593 | } |
---|
594 | } |
---|
595 | } |
---|
596 | |
---|
597 | static void |
---|
598 | printFileList( tr_benc * top ) |
---|
599 | { |
---|
600 | tr_benc *args, *torrents; |
---|
601 | |
---|
602 | if( ( tr_bencDictFindDict( top, "arguments", &args ) ) && |
---|
603 | ( tr_bencDictFindList( args, "torrents", &torrents ) ) ) |
---|
604 | { |
---|
605 | int i, in; |
---|
606 | for( i=0, in=tr_bencListSize( torrents ); i<in; ++i ) |
---|
607 | { |
---|
608 | tr_benc * d = tr_bencListChild( torrents, i ); |
---|
609 | tr_benc *files, *priorities, *wanteds; |
---|
610 | const char * name; |
---|
611 | if( tr_bencDictFindStr( d, "name", &name ) && |
---|
612 | tr_bencDictFindList( d, "files", &files ) && |
---|
613 | tr_bencDictFindList( d, "priorities", &priorities ) && |
---|
614 | tr_bencDictFindList( d, "wanted", &wanteds ) ) |
---|
615 | { |
---|
616 | int j=0, jn=tr_bencListSize(files); |
---|
617 | printf( "%s (%d files):\n", name, jn ); |
---|
618 | printf("%3s %8s %3s %9s %s\n", "#", "Priority", "Get", "Size", "Name" ); |
---|
619 | for( j=0, jn=tr_bencListSize( files ); j<jn; ++j ) |
---|
620 | { |
---|
621 | int64_t length; |
---|
622 | int64_t priority; |
---|
623 | int64_t wanted; |
---|
624 | const char * filename; |
---|
625 | tr_benc * file = tr_bencListChild( files, j ); |
---|
626 | if( tr_bencDictFindInt( file, "length", &length ) && |
---|
627 | tr_bencDictFindStr( file, "name", &filename ) && |
---|
628 | tr_bencGetInt( tr_bencListChild( priorities, j ), &priority ) && |
---|
629 | tr_bencGetInt( tr_bencListChild( wanteds, j ), &wanted ) ) |
---|
630 | { |
---|
631 | char sizestr[64]; |
---|
632 | strlsize( sizestr, length, sizeof( sizestr ) ); |
---|
633 | const char * pristr; |
---|
634 | switch( priority ) { |
---|
635 | case TR_PRI_LOW: pristr = "Low"; break; |
---|
636 | case TR_PRI_HIGH: pristr = "High"; break; |
---|
637 | default: pristr = "Normal"; break; |
---|
638 | } |
---|
639 | printf( "%3d: %-8s %-3s %9s %s\n", (j+1), pristr, (wanted?"Yes":"No"), sizestr, filename ); |
---|
640 | } |
---|
641 | } |
---|
642 | } |
---|
643 | } |
---|
644 | } |
---|
645 | } |
---|
646 | |
---|
647 | static void |
---|
648 | printTorrentList( tr_benc * top ) |
---|
649 | { |
---|
650 | tr_benc *args, *list; |
---|
651 | |
---|
652 | if( ( tr_bencDictFindDict( top, "arguments", &args ) ) && |
---|
653 | ( tr_bencDictFindList( args, "torrents", &list ) ) ) |
---|
654 | { |
---|
655 | int i, n; |
---|
656 | printf( "%-3s %-4s %-8s %-5s %-5s %-5s %-11s %s\n", |
---|
657 | "ID", "Done", "ETA", "Up", "Down", "Ratio", "Status", "Name" ); |
---|
658 | for( i=0, n=tr_bencListSize( list ); i<n; ++i ) |
---|
659 | { |
---|
660 | int64_t id, eta, status, up, down; |
---|
661 | int64_t sizeWhenDone, leftUntilDone; |
---|
662 | int64_t upEver, downEver; |
---|
663 | const char *name; |
---|
664 | tr_benc * d = tr_bencListChild( list, i ); |
---|
665 | if( tr_bencDictFindInt( d, "downloadedEver", &downEver ) |
---|
666 | && tr_bencDictFindInt( d, "eta", &eta ) |
---|
667 | && tr_bencDictFindInt( d, "id", &id ) |
---|
668 | && tr_bencDictFindInt( d, "leftUntilDone", &leftUntilDone ) |
---|
669 | && tr_bencDictFindStr( d, "name", &name ) |
---|
670 | && tr_bencDictFindInt( d, "rateDownload", &down ) |
---|
671 | && tr_bencDictFindInt( d, "rateUpload", &up ) |
---|
672 | && tr_bencDictFindInt( d, "sizeWhenDone", &sizeWhenDone ) |
---|
673 | && tr_bencDictFindInt( d, "status", &status ) |
---|
674 | && tr_bencDictFindInt( d, "uploadedEver", &upEver ) ) |
---|
675 | { |
---|
676 | char etaStr[16]; |
---|
677 | if( leftUntilDone ) |
---|
678 | etaToString( etaStr, sizeof( etaStr ), eta ); |
---|
679 | else |
---|
680 | tr_snprintf( etaStr, sizeof( etaStr ), "Done" ); |
---|
681 | printf( "%3d %3d%% %-8s %5.1f %5.1f %5.1f %-11s %s\n", |
---|
682 | (int)id, |
---|
683 | (int)(100.0*(sizeWhenDone-leftUntilDone)/sizeWhenDone), |
---|
684 | etaStr, |
---|
685 | up / 1024.0, |
---|
686 | down / 1024.0, |
---|
687 | (double)(downEver ? ((double)upEver/downEver) : 0.0), |
---|
688 | torrentStatusToString( status ), |
---|
689 | name ); |
---|
690 | } |
---|
691 | } |
---|
692 | } |
---|
693 | } |
---|
694 | |
---|
695 | static void |
---|
696 | processResponse( const char * host, int port, |
---|
697 | const void * response, size_t len ) |
---|
698 | { |
---|
699 | tr_benc top; |
---|
700 | |
---|
701 | if( debug ) |
---|
702 | fprintf( stderr, "got response: [%*.*s]\n", |
---|
703 | (int)len, (int)len, (const char*) response ); |
---|
704 | |
---|
705 | if( tr_jsonParse( response, len, &top, NULL ) ) |
---|
706 | tr_nerr( MY_NAME, "Unable to parse response \"%*.*s\"", (int)len, (int)len, (char*)response ); |
---|
707 | else |
---|
708 | { |
---|
709 | int64_t tag = -1; |
---|
710 | const char * str; |
---|
711 | tr_bencDictFindInt( &top, "tag", &tag ); |
---|
712 | |
---|
713 | if( tr_bencDictFindStr( &top, "result", &str ) ) |
---|
714 | printf( "%s:%d responded: \"%s\"\n", host, port, str ); |
---|
715 | switch( tag ) { |
---|
716 | case TAG_FILES: printFileList( &top ); break; |
---|
717 | case TAG_DETAILS: printDetails( &top ); break; |
---|
718 | case TAG_LIST: printTorrentList( &top ); break; |
---|
719 | default: break; |
---|
720 | } |
---|
721 | |
---|
722 | tr_bencFree( &top ); |
---|
723 | } |
---|
724 | } |
---|
725 | |
---|
726 | static void |
---|
727 | processRequests( const char * host, int port, |
---|
728 | const char ** reqs, int reqCount ) |
---|
729 | { |
---|
730 | int i; |
---|
731 | CURL * curl; |
---|
732 | struct evbuffer * buf = evbuffer_new( ); |
---|
733 | char * url = tr_strdup_printf( "http://%s:%d/transmission/rpc", host, port ); |
---|
734 | |
---|
735 | curl = curl_easy_init( ); |
---|
736 | curl_easy_setopt( curl, CURLOPT_VERBOSE, debug ); |
---|
737 | curl_easy_setopt( curl, CURLOPT_USERAGENT, MY_NAME"/"LONG_VERSION_STRING ); |
---|
738 | curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, writeFunc ); |
---|
739 | curl_easy_setopt( curl, CURLOPT_WRITEDATA, buf ); |
---|
740 | curl_easy_setopt( curl, CURLOPT_POST, 1 ); |
---|
741 | curl_easy_setopt( curl, CURLOPT_URL, url ); |
---|
742 | if( auth ) { |
---|
743 | curl_easy_setopt( curl, CURLOPT_USERPWD, auth ); |
---|
744 | curl_easy_setopt( curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY ); |
---|
745 | } |
---|
746 | |
---|
747 | for( i=0; i<reqCount; ++i ) |
---|
748 | { |
---|
749 | CURLcode res; |
---|
750 | curl_easy_setopt( curl, CURLOPT_POSTFIELDS, reqs[i] ); |
---|
751 | if( debug ) |
---|
752 | tr_ninf( MY_NAME, "posting [%s]\n", reqs[i] ); |
---|
753 | if(( res = curl_easy_perform( curl ))) |
---|
754 | tr_nerr( MY_NAME, "(%s:%d) %s", host, port, curl_easy_strerror( res ) ); |
---|
755 | else |
---|
756 | processResponse( host, port, EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) ); |
---|
757 | |
---|
758 | evbuffer_drain( buf, EVBUFFER_LENGTH( buf ) ); |
---|
759 | } |
---|
760 | |
---|
761 | /* cleanup */ |
---|
762 | tr_free( url ); |
---|
763 | evbuffer_free( buf ); |
---|
764 | curl_easy_cleanup( curl ); |
---|
765 | } |
---|
766 | |
---|
767 | int |
---|
768 | main( int argc, char ** argv ) |
---|
769 | { |
---|
770 | int i; |
---|
771 | int port = DEFAULT_PORT; |
---|
772 | char * host = NULL; |
---|
773 | |
---|
774 | if( argc < 2 ) |
---|
775 | showUsage( ); |
---|
776 | |
---|
777 | getHostAndPort( &argc, argv, &host, &port ); |
---|
778 | if( host == NULL ) |
---|
779 | host = tr_strdup( DEFAULT_HOST ); |
---|
780 | |
---|
781 | readargs( argc, (const char**)argv ); |
---|
782 | if( reqCount ) |
---|
783 | processRequests( host, port, (const char**)reqs, reqCount ); |
---|
784 | else |
---|
785 | showUsage( ); |
---|
786 | |
---|
787 | for( i=0; i<reqCount; ++i ) |
---|
788 | tr_free( reqs[i] ); |
---|
789 | |
---|
790 | tr_free( host ); |
---|
791 | return 0; |
---|
792 | } |
---|