1 | /****************************************************************************** |
---|
2 | * $Id: cli.c 10048 2010-01-30 00:49:40Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2006 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #include <stdio.h> |
---|
26 | #include <stdlib.h> |
---|
27 | #include <string.h> |
---|
28 | #include <unistd.h> |
---|
29 | #include <signal.h> |
---|
30 | |
---|
31 | #include <libtransmission/transmission.h> |
---|
32 | #include <libtransmission/bencode.h> |
---|
33 | #include <libtransmission/makemeta.h> |
---|
34 | #include <libtransmission/tr-getopt.h> |
---|
35 | #include <libtransmission/utils.h> /* tr_wait_msec */ |
---|
36 | #include <libtransmission/version.h> |
---|
37 | #include <libtransmission/web.h> /* tr_webRun */ |
---|
38 | |
---|
39 | #define LINEWIDTH 80 |
---|
40 | #define MY_NAME "transmissioncli" |
---|
41 | |
---|
42 | static tr_bool showInfo = 0; |
---|
43 | static tr_bool showScrape = 0; |
---|
44 | static tr_bool isPrivate = 0; |
---|
45 | static tr_bool verify = 0; |
---|
46 | static sig_atomic_t gotsig = 0; |
---|
47 | static sig_atomic_t manualUpdate = 0; |
---|
48 | |
---|
49 | static const char * torrentPath = NULL; |
---|
50 | static const char * finishCall = NULL; |
---|
51 | static const char * sourceFile = NULL; |
---|
52 | static const char * comment = NULL; |
---|
53 | |
---|
54 | #define MAX_ANNOUNCE 128 |
---|
55 | static tr_tracker_info announce[MAX_ANNOUNCE]; |
---|
56 | static int announceCount = 0; |
---|
57 | |
---|
58 | static const struct tr_option options[] = |
---|
59 | { |
---|
60 | { 'a', "announce", "Set the new torrent's announce URL", "a", 1, "<url>" }, |
---|
61 | { 'b', "blocklist", "Enable peer blocklists", "b", 0, NULL }, |
---|
62 | { 'B', "no-blocklist", "Disable peer blocklists", "B", 0, NULL }, |
---|
63 | { 'c', "comment", "Set the new torrent's comment", "c", 1, "<comment>" }, |
---|
64 | { 'd', "downlimit", "Set max download speed in KB/s", "d", 1, "<speed>" }, |
---|
65 | { 'D', "no-downlimit", "Don't limit the download speed", "D", 0, NULL }, |
---|
66 | { 910, "encryption-required", "Encrypt all peer connections", "er", 0, NULL }, |
---|
67 | { 911, "encryption-preferred", "Prefer encrypted peer connections", "ep", 0, NULL }, |
---|
68 | { 912, "encryption-tolerated", "Prefer unencrypted peer connections", "et", 0, NULL }, |
---|
69 | { 'f', "finish", "Run a script when the torrent finishes", "f", 1, "<script>" }, |
---|
70 | { 'g', "config-dir", "Where to find configuration files", "g", 1, "<path>" }, |
---|
71 | { 'i', "info", "Show torrent details and exit", "i", 0, NULL }, |
---|
72 | { 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", 0, NULL }, |
---|
73 | { 'M', "no-portmap", "Disable portmapping", "M", 0, NULL }, |
---|
74 | { 'n', "new", "Create a new torrent", "n", 1, "<source>" }, |
---|
75 | { 'p', "port", "Port for incoming peers (Default: " TR_DEFAULT_PEER_PORT_STR ")", "p", 1, "<port>" }, |
---|
76 | { 'r', "private", "Set the new torrent's 'private' flag", "r", 0, NULL }, |
---|
77 | { 's', "scrape", "Scrape the torrent and exit", "s", 0, NULL }, |
---|
78 | { 't', "tos", "Peer socket TOS (0 to 255, default=" TR_DEFAULT_PEER_SOCKET_TOS_STR ")", "t", 1, "<tos>" }, |
---|
79 | { 'u', "uplimit", "Set max upload speed in KB/s", "u", 1, "<speed>" }, |
---|
80 | { 'U', "no-uplimit", "Don't limit the upload speed", "U", 0, NULL }, |
---|
81 | { 'v', "verify", "Verify the specified torrent", "v", 0, NULL }, |
---|
82 | { 'w', "download-dir", "Where to save downloaded data", "w", 1, "<path>" }, |
---|
83 | { 0, NULL, NULL, NULL, 0, NULL } |
---|
84 | }; |
---|
85 | |
---|
86 | static const char * |
---|
87 | getUsage( void ) |
---|
88 | { |
---|
89 | return "A fast and easy BitTorrent client\n" |
---|
90 | "\n" |
---|
91 | "Usage: " MY_NAME " [options] <file|url|magnet>"; |
---|
92 | } |
---|
93 | |
---|
94 | static int parseCommandLine( tr_benc*, int argc, const char ** argv ); |
---|
95 | |
---|
96 | static void sigHandler( int signal ); |
---|
97 | |
---|
98 | static char* |
---|
99 | tr_strlratio( char * buf, |
---|
100 | double ratio, |
---|
101 | size_t buflen ) |
---|
102 | { |
---|
103 | if( (int)ratio == TR_RATIO_NA ) |
---|
104 | tr_strlcpy( buf, _( "None" ), buflen ); |
---|
105 | else if( (int)ratio == TR_RATIO_INF ) |
---|
106 | tr_strlcpy( buf, "Inf", buflen ); |
---|
107 | else if( ratio < 10.0 ) |
---|
108 | tr_snprintf( buf, buflen, "%.2f", ratio ); |
---|
109 | else if( ratio < 100.0 ) |
---|
110 | tr_snprintf( buf, buflen, "%.1f", ratio ); |
---|
111 | else |
---|
112 | tr_snprintf( buf, buflen, "%.0f", ratio ); |
---|
113 | return buf; |
---|
114 | } |
---|
115 | |
---|
116 | static int |
---|
117 | is_rfc2396_alnum( char ch ) |
---|
118 | { |
---|
119 | return ( '0' <= ch && ch <= '9' ) |
---|
120 | || ( 'A' <= ch && ch <= 'Z' ) |
---|
121 | || ( 'a' <= ch && ch <= 'z' ); |
---|
122 | } |
---|
123 | |
---|
124 | static void |
---|
125 | escape( char * out, |
---|
126 | const uint8_t * in, |
---|
127 | int in_len ) /* rfc2396 */ |
---|
128 | { |
---|
129 | const uint8_t *end = in + in_len; |
---|
130 | |
---|
131 | while( in != end ) |
---|
132 | if( is_rfc2396_alnum( *in ) ) |
---|
133 | *out++ = (char) *in++; |
---|
134 | else |
---|
135 | out += tr_snprintf( out, 4, "%%%02X", (unsigned int)*in++ ); |
---|
136 | |
---|
137 | *out = '\0'; |
---|
138 | } |
---|
139 | |
---|
140 | static void |
---|
141 | torrentCompletenessChanged( tr_torrent * torrent UNUSED, |
---|
142 | tr_completeness completeness UNUSED, |
---|
143 | void * user_data UNUSED ) |
---|
144 | { |
---|
145 | system( finishCall ); |
---|
146 | } |
---|
147 | |
---|
148 | static tr_bool waitingOnWeb; |
---|
149 | |
---|
150 | static void |
---|
151 | onTorrentFileDownloaded( tr_session * session UNUSED, |
---|
152 | long response_code UNUSED, |
---|
153 | const void * response, |
---|
154 | size_t response_byte_count, |
---|
155 | void * ctor ) |
---|
156 | { |
---|
157 | tr_ctorSetMetainfo( ctor, response, response_byte_count ); |
---|
158 | waitingOnWeb = FALSE; |
---|
159 | } |
---|
160 | |
---|
161 | static int leftToScrape = 0; |
---|
162 | |
---|
163 | static void |
---|
164 | scrapeDoneFunc( tr_session * session UNUSED, |
---|
165 | long response_code, |
---|
166 | const void * response, |
---|
167 | size_t response_byte_count, |
---|
168 | void * host ) |
---|
169 | { |
---|
170 | tr_benc top, *files; |
---|
171 | |
---|
172 | if( !tr_bencLoad( response, response_byte_count, &top, NULL ) |
---|
173 | && tr_bencDictFindDict( &top, "files", &files ) |
---|
174 | && files->val.l.count >= 2 ) |
---|
175 | { |
---|
176 | int64_t complete = -1, incomplete = -1, downloaded = -1; |
---|
177 | tr_benc * hash = &files->val.l.vals[1]; |
---|
178 | tr_bencDictFindInt( hash, "complete", &complete ); |
---|
179 | tr_bencDictFindInt( hash, "incomplete", &incomplete ); |
---|
180 | tr_bencDictFindInt( hash, "downloaded", &downloaded ); |
---|
181 | printf( "%4d seeders, %4d leechers, %5d downloads at %s\n", |
---|
182 | (int)complete, (int)incomplete, (int)downloaded, |
---|
183 | (char*)host ); |
---|
184 | tr_bencFree( &top ); |
---|
185 | } |
---|
186 | else |
---|
187 | fprintf( stderr, "Unable to parse response (http code %lu) at %s", |
---|
188 | response_code, |
---|
189 | (char*)host ); |
---|
190 | |
---|
191 | --leftToScrape; |
---|
192 | |
---|
193 | tr_free( host ); |
---|
194 | } |
---|
195 | |
---|
196 | static void |
---|
197 | dumpInfo( FILE * out, |
---|
198 | const tr_info * inf ) |
---|
199 | { |
---|
200 | int i; |
---|
201 | int prevTier = -1; |
---|
202 | tr_file_index_t ff; |
---|
203 | |
---|
204 | fprintf( out, "hash:\t" ); |
---|
205 | for( i = 0; i < SHA_DIGEST_LENGTH; ++i ) |
---|
206 | fprintf( out, "%02x", inf->hash[i] ); |
---|
207 | fprintf( out, "\n" ); |
---|
208 | |
---|
209 | fprintf( out, "name:\t%s\n", inf->name ); |
---|
210 | |
---|
211 | for( i = 0; i < inf->trackerCount; ++i ) |
---|
212 | { |
---|
213 | if( prevTier != inf->trackers[i].tier ) |
---|
214 | { |
---|
215 | prevTier = inf->trackers[i].tier; |
---|
216 | fprintf( out, "\ntracker tier #%d:\n", ( prevTier + 1 ) ); |
---|
217 | } |
---|
218 | fprintf( out, "\tannounce:\t%s\n", inf->trackers[i].announce ); |
---|
219 | } |
---|
220 | |
---|
221 | fprintf( out, "size:\t%" PRIu64 " (%" PRIu64 " * %d + %" PRIu64 ")\n", |
---|
222 | inf->totalSize, inf->totalSize / inf->pieceSize, |
---|
223 | inf->pieceSize, inf->totalSize % inf->pieceSize ); |
---|
224 | |
---|
225 | if( inf->comment && *inf->comment ) |
---|
226 | fprintf( out, "comment:\t%s\n", inf->comment ); |
---|
227 | if( inf->creator && *inf->creator ) |
---|
228 | fprintf( out, "creator:\t%s\n", inf->creator ); |
---|
229 | if( inf->isPrivate ) |
---|
230 | fprintf( out, "private flag set\n" ); |
---|
231 | |
---|
232 | fprintf( out, "file(s):\n" ); |
---|
233 | for( ff = 0; ff < inf->fileCount; ++ff ) |
---|
234 | fprintf( out, "\t%s (%" PRIu64 ")\n", inf->files[ff].name, |
---|
235 | inf->files[ff].length ); |
---|
236 | } |
---|
237 | |
---|
238 | static void |
---|
239 | getStatusStr( const tr_stat * st, |
---|
240 | char * buf, |
---|
241 | size_t buflen ) |
---|
242 | { |
---|
243 | if( st->activity & TR_STATUS_CHECK_WAIT ) |
---|
244 | { |
---|
245 | tr_snprintf( buf, buflen, "Waiting to verify local files" ); |
---|
246 | } |
---|
247 | else if( st->activity & TR_STATUS_CHECK ) |
---|
248 | { |
---|
249 | tr_snprintf( buf, buflen, |
---|
250 | "Verifying local files (%.2f%%, %.2f%% valid)", |
---|
251 | tr_truncd( 100 * st->recheckProgress, 2 ), |
---|
252 | tr_truncd( 100 * st->percentDone, 2 ) ); |
---|
253 | } |
---|
254 | else if( st->activity & TR_STATUS_DOWNLOAD ) |
---|
255 | { |
---|
256 | char ratioStr[80]; |
---|
257 | tr_strlratio( ratioStr, st->ratio, sizeof( ratioStr ) ); |
---|
258 | tr_snprintf( |
---|
259 | buf, buflen, |
---|
260 | "Progress: %.1f%%, dl from %d of %d peers (%.0f KB/s), " |
---|
261 | "ul to %d (%.0f KB/s) [%s]", |
---|
262 | tr_truncd( 100 * st->percentDone, 1 ), |
---|
263 | st->peersSendingToUs, |
---|
264 | st->peersConnected, |
---|
265 | st->pieceDownloadSpeed, |
---|
266 | st->peersGettingFromUs, |
---|
267 | st->pieceUploadSpeed, |
---|
268 | ratioStr ); |
---|
269 | } |
---|
270 | else if( st->activity & TR_STATUS_SEED ) |
---|
271 | { |
---|
272 | char ratioStr[80]; |
---|
273 | tr_strlratio( ratioStr, st->ratio, sizeof( ratioStr ) ); |
---|
274 | tr_snprintf( |
---|
275 | buf, buflen, |
---|
276 | "Seeding, uploading to %d of %d peer(s), %.0f KB/s [%s]", |
---|
277 | st->peersGettingFromUs, st->peersConnected, |
---|
278 | st->pieceUploadSpeed, ratioStr ); |
---|
279 | } |
---|
280 | else *buf = '\0'; |
---|
281 | } |
---|
282 | |
---|
283 | static const char* |
---|
284 | getConfigDir( int argc, const char ** argv ) |
---|
285 | { |
---|
286 | int c; |
---|
287 | const char * configDir = NULL; |
---|
288 | const char * optarg; |
---|
289 | const int ind = tr_optind; |
---|
290 | |
---|
291 | while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg ))) { |
---|
292 | if( c == 'g' ) { |
---|
293 | configDir = optarg; |
---|
294 | break; |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | tr_optind = ind; |
---|
299 | |
---|
300 | if( configDir == NULL ) |
---|
301 | configDir = tr_getDefaultConfigDir( MY_NAME ); |
---|
302 | |
---|
303 | return configDir; |
---|
304 | } |
---|
305 | |
---|
306 | int |
---|
307 | main( int argc, |
---|
308 | char ** argv ) |
---|
309 | { |
---|
310 | int error; |
---|
311 | tr_session * h; |
---|
312 | tr_ctor * ctor; |
---|
313 | tr_torrent * tor = NULL; |
---|
314 | tr_benc settings; |
---|
315 | const char * configDir; |
---|
316 | tr_bool haveSource; |
---|
317 | tr_bool haveAnnounce; |
---|
318 | uint8_t * fileContents; |
---|
319 | size_t fileLength; |
---|
320 | |
---|
321 | printf( "Transmission %s - http://www.transmissionbt.com/\n", |
---|
322 | LONG_VERSION_STRING ); |
---|
323 | |
---|
324 | /* user needs to pass in at least one argument */ |
---|
325 | if( argc < 2 ) { |
---|
326 | tr_getopt_usage( MY_NAME, getUsage( ), options ); |
---|
327 | return EXIT_FAILURE; |
---|
328 | } |
---|
329 | |
---|
330 | /* load the defaults from config file + libtransmission defaults */ |
---|
331 | tr_bencInitDict( &settings, 0 ); |
---|
332 | configDir = getConfigDir( argc, (const char**)argv ); |
---|
333 | tr_sessionLoadSettings( &settings, configDir, MY_NAME ); |
---|
334 | |
---|
335 | /* the command line overrides defaults */ |
---|
336 | if( parseCommandLine( &settings, argc, (const char**)argv ) ) |
---|
337 | return EXIT_FAILURE; |
---|
338 | |
---|
339 | /* Check the options for validity */ |
---|
340 | if( !torrentPath ) { |
---|
341 | fprintf( stderr, "No torrent specified!\n" ); |
---|
342 | return EXIT_FAILURE; |
---|
343 | } |
---|
344 | |
---|
345 | /* don't bind the port if we're just running the CLI |
---|
346 | to get metainfo or to create a torrent */ |
---|
347 | if( showInfo || showScrape || ( sourceFile != NULL ) ) |
---|
348 | tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_PORT, -1 ); |
---|
349 | |
---|
350 | h = tr_sessionInit( "cli", configDir, FALSE, &settings ); |
---|
351 | |
---|
352 | haveSource = sourceFile && *sourceFile; |
---|
353 | haveAnnounce = announceCount > 0; |
---|
354 | |
---|
355 | if( haveSource && !haveAnnounce ) |
---|
356 | fprintf( stderr, "Did you mean to create a torrent without a tracker's announce URL?\n" ); |
---|
357 | |
---|
358 | if( haveSource ) /* creating a torrent */ |
---|
359 | { |
---|
360 | int err; |
---|
361 | tr_metainfo_builder * b; |
---|
362 | fprintf( stderr, "creating torrent \"%s\"\n", torrentPath ); |
---|
363 | |
---|
364 | b = tr_metaInfoBuilderCreate( sourceFile ); |
---|
365 | tr_makeMetaInfo( b, torrentPath, announce, announceCount, comment, isPrivate ); |
---|
366 | while( !b->isDone ) |
---|
367 | { |
---|
368 | tr_wait_msec( 1000 ); |
---|
369 | printf( "." ); |
---|
370 | } |
---|
371 | |
---|
372 | err = b->result; |
---|
373 | tr_metaInfoBuilderFree( b ); |
---|
374 | return err; |
---|
375 | } |
---|
376 | |
---|
377 | ctor = tr_ctorNew( h ); |
---|
378 | |
---|
379 | fileContents = tr_loadFile( torrentPath, &fileLength ); |
---|
380 | tr_ctorSetPaused( ctor, TR_FORCE, showScrape ); |
---|
381 | if( fileContents != NULL ) { |
---|
382 | tr_ctorSetMetainfo( ctor, fileContents, fileLength ); |
---|
383 | } else if( !memcmp( torrentPath, "magnet:?", 8 ) ) { |
---|
384 | tr_ctorSetMagnet( ctor, torrentPath ); |
---|
385 | } else if( !memcmp( torrentPath, "http", 4 ) ) { |
---|
386 | tr_webRun( h, torrentPath, NULL, onTorrentFileDownloaded, ctor ); |
---|
387 | waitingOnWeb = TRUE; |
---|
388 | while( waitingOnWeb ) tr_wait_msec( 1000 ); |
---|
389 | } |
---|
390 | tr_free( fileContents ); |
---|
391 | |
---|
392 | if( showScrape ) |
---|
393 | { |
---|
394 | tr_info info; |
---|
395 | |
---|
396 | if( !tr_torrentParse( ctor, &info ) ) |
---|
397 | { |
---|
398 | int i; |
---|
399 | const time_t start = time( NULL ); |
---|
400 | for( i = 0; i < info.trackerCount; ++i ) |
---|
401 | { |
---|
402 | if( info.trackers[i].scrape ) |
---|
403 | { |
---|
404 | const char * scrape = info.trackers[i].scrape; |
---|
405 | char escaped[SHA_DIGEST_LENGTH * 3 + 1]; |
---|
406 | char * url, *host; |
---|
407 | escape( escaped, info.hash, SHA_DIGEST_LENGTH ); |
---|
408 | url = tr_strdup_printf( "%s%cinfo_hash=%s", |
---|
409 | scrape, |
---|
410 | strchr( scrape, |
---|
411 | '?' ) ? '&' : '?', |
---|
412 | escaped ); |
---|
413 | tr_httpParseURL( scrape, -1, &host, NULL, NULL ); |
---|
414 | ++leftToScrape; |
---|
415 | |
---|
416 | tr_webRun( h, url, NULL, scrapeDoneFunc, host ); |
---|
417 | tr_free( url ); |
---|
418 | } |
---|
419 | } |
---|
420 | |
---|
421 | fprintf( stderr, "scraping %d trackers:\n", leftToScrape ); |
---|
422 | |
---|
423 | while( leftToScrape > 0 && ( ( time( NULL ) - start ) < 20 ) ) |
---|
424 | tr_wait_msec( 250 ); |
---|
425 | } |
---|
426 | goto cleanup; |
---|
427 | } |
---|
428 | |
---|
429 | if( showInfo ) |
---|
430 | { |
---|
431 | tr_info info; |
---|
432 | |
---|
433 | if( !tr_torrentParse( ctor, &info ) ) |
---|
434 | { |
---|
435 | dumpInfo( stdout, &info ); |
---|
436 | tr_metainfoFree( &info ); |
---|
437 | } |
---|
438 | |
---|
439 | tr_ctorFree( ctor ); |
---|
440 | goto cleanup; |
---|
441 | } |
---|
442 | |
---|
443 | tor = tr_torrentNew( ctor, &error ); |
---|
444 | tr_ctorFree( ctor ); |
---|
445 | if( !tor ) |
---|
446 | { |
---|
447 | fprintf( stderr, "Failed opening torrent file `%s'\n", torrentPath ); |
---|
448 | tr_sessionClose( h ); |
---|
449 | return EXIT_FAILURE; |
---|
450 | } |
---|
451 | |
---|
452 | signal( SIGINT, sigHandler ); |
---|
453 | #ifndef WIN32 |
---|
454 | signal( SIGHUP, sigHandler ); |
---|
455 | #endif |
---|
456 | tr_torrentSetCompletenessCallback( tor, torrentCompletenessChanged, NULL ); |
---|
457 | tr_torrentStart( tor ); |
---|
458 | |
---|
459 | if( verify ) |
---|
460 | { |
---|
461 | verify = 0; |
---|
462 | tr_torrentVerify( tor ); |
---|
463 | } |
---|
464 | |
---|
465 | for( ; ; ) |
---|
466 | { |
---|
467 | char line[LINEWIDTH]; |
---|
468 | const tr_stat * st; |
---|
469 | const char * messageName[] = { NULL, "Tracker gave a warning:", |
---|
470 | "Tracker gave an error:", |
---|
471 | "Error:" }; |
---|
472 | |
---|
473 | tr_wait_msec( 200 ); |
---|
474 | |
---|
475 | if( gotsig ) |
---|
476 | { |
---|
477 | gotsig = 0; |
---|
478 | printf( "\nStopping torrent...\n" ); |
---|
479 | tr_torrentStop( tor ); |
---|
480 | } |
---|
481 | |
---|
482 | if( manualUpdate ) |
---|
483 | { |
---|
484 | manualUpdate = 0; |
---|
485 | if( !tr_torrentCanManualUpdate( tor ) ) |
---|
486 | fprintf( |
---|
487 | stderr, |
---|
488 | "\nReceived SIGHUP, but can't send a manual update now\n" ); |
---|
489 | else |
---|
490 | { |
---|
491 | fprintf( stderr, |
---|
492 | "\nReceived SIGHUP: manual update scheduled\n" ); |
---|
493 | tr_torrentManualUpdate( tor ); |
---|
494 | } |
---|
495 | } |
---|
496 | |
---|
497 | st = tr_torrentStat( tor ); |
---|
498 | if( st->activity & TR_STATUS_STOPPED ) |
---|
499 | break; |
---|
500 | |
---|
501 | getStatusStr( st, line, sizeof( line ) ); |
---|
502 | printf( "\r%-*s", LINEWIDTH, line ); |
---|
503 | |
---|
504 | if( messageName[st->error] ) |
---|
505 | fprintf( stderr, "\n%s: %s\n", messageName[st->error], st->errorString ); |
---|
506 | } |
---|
507 | |
---|
508 | cleanup: |
---|
509 | |
---|
510 | tr_sessionSaveSettings( h, configDir, &settings ); |
---|
511 | |
---|
512 | printf( "\n" ); |
---|
513 | tr_bencFree( &settings ); |
---|
514 | tr_sessionClose( h ); |
---|
515 | return EXIT_SUCCESS; |
---|
516 | } |
---|
517 | |
---|
518 | /*** |
---|
519 | **** |
---|
520 | **** |
---|
521 | **** |
---|
522 | ***/ |
---|
523 | |
---|
524 | static int |
---|
525 | parseCommandLine( tr_benc * d, int argc, const char ** argv ) |
---|
526 | { |
---|
527 | int c; |
---|
528 | const char * optarg; |
---|
529 | |
---|
530 | while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg ))) |
---|
531 | { |
---|
532 | switch( c ) |
---|
533 | { |
---|
534 | case 'a': if( announceCount + 1 < MAX_ANNOUNCE ) { |
---|
535 | announce[announceCount].tier = announceCount; |
---|
536 | announce[announceCount].announce = (char*) optarg; |
---|
537 | ++announceCount; |
---|
538 | } |
---|
539 | break; |
---|
540 | case 'b': tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, TRUE ); |
---|
541 | break; |
---|
542 | case 'B': tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, FALSE ); |
---|
543 | break; |
---|
544 | case 'c': comment = optarg; |
---|
545 | break; |
---|
546 | case 'd': tr_bencDictAddInt ( d, TR_PREFS_KEY_DSPEED, atoi( optarg ) ); |
---|
547 | tr_bencDictAddBool( d, TR_PREFS_KEY_DSPEED_ENABLED, TRUE ); |
---|
548 | break; |
---|
549 | case 'D': tr_bencDictAddBool( d, TR_PREFS_KEY_DSPEED_ENABLED, FALSE ); |
---|
550 | break; |
---|
551 | case 'f': finishCall = optarg; |
---|
552 | break; |
---|
553 | case 'g': /* handled above */ |
---|
554 | break; |
---|
555 | case 'i': showInfo = 1; |
---|
556 | break; |
---|
557 | case 'm': tr_bencDictAddBool( d, TR_PREFS_KEY_PORT_FORWARDING, TRUE ); |
---|
558 | break; |
---|
559 | case 'M': tr_bencDictAddBool( d, TR_PREFS_KEY_PORT_FORWARDING, FALSE ); |
---|
560 | break; |
---|
561 | case 'n': sourceFile = optarg; break; |
---|
562 | case 'p': tr_bencDictAddInt( d, TR_PREFS_KEY_PEER_PORT, atoi( optarg ) ); |
---|
563 | break; |
---|
564 | case 'r': isPrivate = 1; |
---|
565 | break; |
---|
566 | case 's': showScrape = 1; |
---|
567 | break; |
---|
568 | case 't': tr_bencDictAddInt( d, TR_PREFS_KEY_PEER_SOCKET_TOS, atoi( optarg ) ); |
---|
569 | break; |
---|
570 | case 'u': tr_bencDictAddInt( d, TR_PREFS_KEY_USPEED, atoi( optarg ) ); |
---|
571 | tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED, TRUE ); |
---|
572 | break; |
---|
573 | case 'U': tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED, FALSE ); |
---|
574 | break; |
---|
575 | case 'v': verify = 1; |
---|
576 | break; |
---|
577 | case 'w': tr_bencDictAddStr( d, TR_PREFS_KEY_DOWNLOAD_DIR, optarg ); |
---|
578 | break; |
---|
579 | case 910: tr_bencDictAddInt( d, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_REQUIRED ); |
---|
580 | break; |
---|
581 | case 911: tr_bencDictAddInt( d, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_PREFERRED ); |
---|
582 | break; |
---|
583 | case 912: tr_bencDictAddInt( d, TR_PREFS_KEY_ENCRYPTION, TR_CLEAR_PREFERRED ); |
---|
584 | break; |
---|
585 | case TR_OPT_UNK: |
---|
586 | torrentPath = optarg; |
---|
587 | break; |
---|
588 | default: return 1; |
---|
589 | } |
---|
590 | } |
---|
591 | |
---|
592 | return 0; |
---|
593 | } |
---|
594 | |
---|
595 | static void |
---|
596 | sigHandler( int signal ) |
---|
597 | { |
---|
598 | switch( signal ) |
---|
599 | { |
---|
600 | case SIGINT: |
---|
601 | gotsig = 1; break; |
---|
602 | |
---|
603 | #ifndef WIN32 |
---|
604 | case SIGHUP: |
---|
605 | manualUpdate = 1; break; |
---|
606 | |
---|
607 | #endif |
---|
608 | default: |
---|
609 | break; |
---|
610 | } |
---|
611 | } |
---|
612 | |
---|