1 | /****************************************************************************** |
---|
2 | * $Id: cli.c 10955 2010-07-06 20:25:54Z 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/tr-getopt.h> |
---|
34 | #include <libtransmission/utils.h> /* tr_wait_msec */ |
---|
35 | #include <libtransmission/version.h> |
---|
36 | #include <libtransmission/web.h> /* tr_webRun */ |
---|
37 | |
---|
38 | /*** |
---|
39 | **** |
---|
40 | ***/ |
---|
41 | |
---|
42 | #define MEM_K 1024 |
---|
43 | #define MEM_K_STR "KiB" |
---|
44 | #define MEM_M_STR "MiB" |
---|
45 | #define MEM_G_STR "GiB" |
---|
46 | #define MEM_T_STR "TiB" |
---|
47 | |
---|
48 | #define DISK_K 1000 |
---|
49 | #define DISK_B_STR "B" |
---|
50 | #define DISK_K_STR "kB" |
---|
51 | #define DISK_M_STR "MB" |
---|
52 | #define DISK_G_STR "GB" |
---|
53 | #define DISK_T_STR "TB" |
---|
54 | |
---|
55 | #define SPEED_K 1000 |
---|
56 | #define SPEED_B_STR "B/s" |
---|
57 | #define SPEED_K_STR "kB/s" |
---|
58 | #define SPEED_M_STR "MB/s" |
---|
59 | #define SPEED_G_STR "GB/s" |
---|
60 | #define SPEED_T_STR "TB/s" |
---|
61 | |
---|
62 | /*** |
---|
63 | **** |
---|
64 | ***/ |
---|
65 | |
---|
66 | #define LINEWIDTH 80 |
---|
67 | #define MY_NAME "transmissioncli" |
---|
68 | |
---|
69 | static tr_bool verify = 0; |
---|
70 | static sig_atomic_t gotsig = 0; |
---|
71 | static sig_atomic_t manualUpdate = 0; |
---|
72 | |
---|
73 | static const char * torrentPath = NULL; |
---|
74 | |
---|
75 | static const struct tr_option options[] = |
---|
76 | { |
---|
77 | { 'b', "blocklist", "Enable peer blocklists", "b", 0, NULL }, |
---|
78 | { 'B', "no-blocklist", "Disable peer blocklists", "B", 0, NULL }, |
---|
79 | { 'd', "downlimit", "Set max download speed in "SPEED_K_STR, "d", 1, "<speed>" }, |
---|
80 | { 'D', "no-downlimit", "Don't limit the download speed", "D", 0, NULL }, |
---|
81 | { 910, "encryption-required", "Encrypt all peer connections", "er", 0, NULL }, |
---|
82 | { 911, "encryption-preferred", "Prefer encrypted peer connections", "ep", 0, NULL }, |
---|
83 | { 912, "encryption-tolerated", "Prefer unencrypted peer connections", "et", 0, NULL }, |
---|
84 | { 'f', "finish", "Run a script when the torrent finishes", "f", 1, "<script>" }, |
---|
85 | { 'g', "config-dir", "Where to find configuration files", "g", 1, "<path>" }, |
---|
86 | { 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", 0, NULL }, |
---|
87 | { 'M', "no-portmap", "Disable portmapping", "M", 0, NULL }, |
---|
88 | { 'p', "port", "Port for incoming peers (Default: " TR_DEFAULT_PEER_PORT_STR ")", "p", 1, "<port>" }, |
---|
89 | { 't', "tos", "Peer socket TOS (0 to 255, default=" TR_DEFAULT_PEER_SOCKET_TOS_STR ")", "t", 1, "<tos>" }, |
---|
90 | { 'u', "uplimit", "Set max upload speed in "SPEED_K_STR, "u", 1, "<speed>" }, |
---|
91 | { 'U', "no-uplimit", "Don't limit the upload speed", "U", 0, NULL }, |
---|
92 | { 'v', "verify", "Verify the specified torrent", "v", 0, NULL }, |
---|
93 | { 'w', "download-dir", "Where to save downloaded data", "w", 1, "<path>" }, |
---|
94 | { 0, NULL, NULL, NULL, 0, NULL } |
---|
95 | }; |
---|
96 | |
---|
97 | static const char * |
---|
98 | getUsage( void ) |
---|
99 | { |
---|
100 | return "A fast and easy BitTorrent client\n" |
---|
101 | "\n" |
---|
102 | "Usage: " MY_NAME " [options] <file|url|magnet>"; |
---|
103 | } |
---|
104 | |
---|
105 | static int parseCommandLine( tr_benc*, int argc, const char ** argv ); |
---|
106 | |
---|
107 | static void sigHandler( int signal ); |
---|
108 | |
---|
109 | static char* |
---|
110 | tr_strlratio( char * buf, |
---|
111 | double ratio, |
---|
112 | size_t buflen ) |
---|
113 | { |
---|
114 | if( (int)ratio == TR_RATIO_NA ) |
---|
115 | tr_strlcpy( buf, _( "None" ), buflen ); |
---|
116 | else if( (int)ratio == TR_RATIO_INF ) |
---|
117 | tr_strlcpy( buf, "Inf", buflen ); |
---|
118 | else if( ratio < 10.0 ) |
---|
119 | tr_snprintf( buf, buflen, "%.2f", ratio ); |
---|
120 | else if( ratio < 100.0 ) |
---|
121 | tr_snprintf( buf, buflen, "%.1f", ratio ); |
---|
122 | else |
---|
123 | tr_snprintf( buf, buflen, "%.0f", ratio ); |
---|
124 | return buf; |
---|
125 | } |
---|
126 | |
---|
127 | static tr_bool waitingOnWeb; |
---|
128 | |
---|
129 | static void |
---|
130 | onTorrentFileDownloaded( tr_session * session UNUSED, |
---|
131 | long response_code UNUSED, |
---|
132 | const void * response, |
---|
133 | size_t response_byte_count, |
---|
134 | void * ctor ) |
---|
135 | { |
---|
136 | tr_ctorSetMetainfo( ctor, response, response_byte_count ); |
---|
137 | waitingOnWeb = FALSE; |
---|
138 | } |
---|
139 | |
---|
140 | static void |
---|
141 | getStatusStr( const tr_stat * st, |
---|
142 | char * buf, |
---|
143 | size_t buflen ) |
---|
144 | { |
---|
145 | if( st->activity & TR_STATUS_CHECK_WAIT ) |
---|
146 | { |
---|
147 | tr_snprintf( buf, buflen, "Waiting to verify local files" ); |
---|
148 | } |
---|
149 | else if( st->activity & TR_STATUS_CHECK ) |
---|
150 | { |
---|
151 | tr_snprintf( buf, buflen, |
---|
152 | "Verifying local files (%.2f%%, %.2f%% valid)", |
---|
153 | tr_truncd( 100 * st->recheckProgress, 2 ), |
---|
154 | tr_truncd( 100 * st->percentDone, 2 ) ); |
---|
155 | } |
---|
156 | else if( st->activity & TR_STATUS_DOWNLOAD ) |
---|
157 | { |
---|
158 | char upStr[80]; |
---|
159 | char dnStr[80]; |
---|
160 | char ratioStr[80]; |
---|
161 | |
---|
162 | tr_formatter_speed_KBps( upStr, st->pieceUploadSpeed_KBps, sizeof( upStr ) ); |
---|
163 | tr_formatter_speed_KBps( dnStr, st->pieceDownloadSpeed_KBps, sizeof( dnStr ) ); |
---|
164 | tr_strlratio( ratioStr, st->ratio, sizeof( ratioStr ) ); |
---|
165 | |
---|
166 | tr_snprintf( buf, buflen, |
---|
167 | "Progress: %.1f%%, " |
---|
168 | "dl from %d of %d peers (%s), " |
---|
169 | "ul to %d (%s) " |
---|
170 | "[%s]", |
---|
171 | tr_truncd( 100 * st->percentDone, 1 ), |
---|
172 | st->peersSendingToUs, st->peersConnected, upStr, |
---|
173 | st->peersGettingFromUs, dnStr, |
---|
174 | ratioStr ); |
---|
175 | } |
---|
176 | else if( st->activity & TR_STATUS_SEED ) |
---|
177 | { |
---|
178 | char upStr[80]; |
---|
179 | char ratioStr[80]; |
---|
180 | |
---|
181 | tr_formatter_speed_KBps( upStr, st->pieceUploadSpeed_KBps, sizeof( upStr ) ); |
---|
182 | tr_strlratio( ratioStr, st->ratio, sizeof( ratioStr ) ); |
---|
183 | |
---|
184 | tr_snprintf( buf, buflen, |
---|
185 | "Seeding, uploading to %d of %d peer(s), %s [%s]", |
---|
186 | st->peersGettingFromUs, st->peersConnected, upStr, ratioStr ); |
---|
187 | } |
---|
188 | else *buf = '\0'; |
---|
189 | } |
---|
190 | |
---|
191 | static const char* |
---|
192 | getConfigDir( int argc, const char ** argv ) |
---|
193 | { |
---|
194 | int c; |
---|
195 | const char * configDir = NULL; |
---|
196 | const char * optarg; |
---|
197 | const int ind = tr_optind; |
---|
198 | |
---|
199 | while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg ))) { |
---|
200 | if( c == 'g' ) { |
---|
201 | configDir = optarg; |
---|
202 | break; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | tr_optind = ind; |
---|
207 | |
---|
208 | if( configDir == NULL ) |
---|
209 | configDir = tr_getDefaultConfigDir( MY_NAME ); |
---|
210 | |
---|
211 | return configDir; |
---|
212 | } |
---|
213 | |
---|
214 | int |
---|
215 | main( int argc, char ** argv ) |
---|
216 | { |
---|
217 | int error; |
---|
218 | tr_session * h; |
---|
219 | tr_ctor * ctor; |
---|
220 | tr_torrent * tor = NULL; |
---|
221 | tr_benc settings; |
---|
222 | const char * configDir; |
---|
223 | uint8_t * fileContents; |
---|
224 | size_t fileLength; |
---|
225 | |
---|
226 | tr_formatter_mem_init( MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR ); |
---|
227 | tr_formatter_size_init( DISK_K,DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR ); |
---|
228 | tr_formatter_speed_init( SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR ); |
---|
229 | |
---|
230 | printf( "Transmission %s - http://www.transmissionbt.com/\n", |
---|
231 | LONG_VERSION_STRING ); |
---|
232 | |
---|
233 | /* user needs to pass in at least one argument */ |
---|
234 | if( argc < 2 ) { |
---|
235 | tr_getopt_usage( MY_NAME, getUsage( ), options ); |
---|
236 | return EXIT_FAILURE; |
---|
237 | } |
---|
238 | |
---|
239 | /* load the defaults from config file + libtransmission defaults */ |
---|
240 | tr_bencInitDict( &settings, 0 ); |
---|
241 | configDir = getConfigDir( argc, (const char**)argv ); |
---|
242 | tr_sessionLoadSettings( &settings, configDir, MY_NAME ); |
---|
243 | |
---|
244 | /* the command line overrides defaults */ |
---|
245 | if( parseCommandLine( &settings, argc, (const char**)argv ) ) |
---|
246 | return EXIT_FAILURE; |
---|
247 | |
---|
248 | /* Check the options for validity */ |
---|
249 | if( !torrentPath ) { |
---|
250 | fprintf( stderr, "No torrent specified!\n" ); |
---|
251 | return EXIT_FAILURE; |
---|
252 | } |
---|
253 | |
---|
254 | h = tr_sessionInit( "cli", configDir, FALSE, &settings ); |
---|
255 | |
---|
256 | ctor = tr_ctorNew( h ); |
---|
257 | |
---|
258 | fileContents = tr_loadFile( torrentPath, &fileLength ); |
---|
259 | tr_ctorSetPaused( ctor, TR_FORCE, FALSE ); |
---|
260 | if( fileContents != NULL ) { |
---|
261 | tr_ctorSetMetainfo( ctor, fileContents, fileLength ); |
---|
262 | } else if( !memcmp( torrentPath, "magnet:?", 8 ) ) { |
---|
263 | tr_ctorSetMetainfoFromMagnetLink( ctor, torrentPath ); |
---|
264 | } else if( !memcmp( torrentPath, "http", 4 ) ) { |
---|
265 | tr_webRun( h, torrentPath, NULL, onTorrentFileDownloaded, ctor ); |
---|
266 | waitingOnWeb = TRUE; |
---|
267 | while( waitingOnWeb ) tr_wait_msec( 1000 ); |
---|
268 | } |
---|
269 | tr_free( fileContents ); |
---|
270 | |
---|
271 | tor = tr_torrentNew( ctor, &error ); |
---|
272 | tr_ctorFree( ctor ); |
---|
273 | if( !tor ) |
---|
274 | { |
---|
275 | fprintf( stderr, "Failed opening torrent file `%s'\n", torrentPath ); |
---|
276 | tr_sessionClose( h ); |
---|
277 | return EXIT_FAILURE; |
---|
278 | } |
---|
279 | |
---|
280 | signal( SIGINT, sigHandler ); |
---|
281 | #ifndef WIN32 |
---|
282 | signal( SIGHUP, sigHandler ); |
---|
283 | #endif |
---|
284 | tr_torrentStart( tor ); |
---|
285 | |
---|
286 | if( verify ) |
---|
287 | { |
---|
288 | verify = 0; |
---|
289 | tr_torrentVerify( tor ); |
---|
290 | } |
---|
291 | |
---|
292 | for( ; ; ) |
---|
293 | { |
---|
294 | char line[LINEWIDTH]; |
---|
295 | const tr_stat * st; |
---|
296 | const char * messageName[] = { NULL, "Tracker gave a warning:", |
---|
297 | "Tracker gave an error:", |
---|
298 | "Error:" }; |
---|
299 | |
---|
300 | tr_wait_msec( 200 ); |
---|
301 | |
---|
302 | if( gotsig ) |
---|
303 | { |
---|
304 | gotsig = 0; |
---|
305 | printf( "\nStopping torrent...\n" ); |
---|
306 | tr_torrentStop( tor ); |
---|
307 | } |
---|
308 | |
---|
309 | if( manualUpdate ) |
---|
310 | { |
---|
311 | manualUpdate = 0; |
---|
312 | if( !tr_torrentCanManualUpdate( tor ) ) |
---|
313 | fprintf( |
---|
314 | stderr, |
---|
315 | "\nReceived SIGHUP, but can't send a manual update now\n" ); |
---|
316 | else |
---|
317 | { |
---|
318 | fprintf( stderr, |
---|
319 | "\nReceived SIGHUP: manual update scheduled\n" ); |
---|
320 | tr_torrentManualUpdate( tor ); |
---|
321 | } |
---|
322 | } |
---|
323 | |
---|
324 | st = tr_torrentStat( tor ); |
---|
325 | if( st->activity & TR_STATUS_STOPPED ) |
---|
326 | break; |
---|
327 | |
---|
328 | getStatusStr( st, line, sizeof( line ) ); |
---|
329 | printf( "\r%-*s", LINEWIDTH, line ); |
---|
330 | |
---|
331 | if( messageName[st->error] ) |
---|
332 | fprintf( stderr, "\n%s: %s\n", messageName[st->error], st->errorString ); |
---|
333 | } |
---|
334 | |
---|
335 | tr_sessionSaveSettings( h, configDir, &settings ); |
---|
336 | |
---|
337 | printf( "\n" ); |
---|
338 | tr_bencFree( &settings ); |
---|
339 | tr_sessionClose( h ); |
---|
340 | return EXIT_SUCCESS; |
---|
341 | } |
---|
342 | |
---|
343 | /*** |
---|
344 | **** |
---|
345 | **** |
---|
346 | **** |
---|
347 | ***/ |
---|
348 | |
---|
349 | static int |
---|
350 | parseCommandLine( tr_benc * d, int argc, const char ** argv ) |
---|
351 | { |
---|
352 | int c; |
---|
353 | const char * optarg; |
---|
354 | |
---|
355 | while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg ))) |
---|
356 | { |
---|
357 | switch( c ) |
---|
358 | { |
---|
359 | case 'b': tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, TRUE ); |
---|
360 | break; |
---|
361 | case 'B': tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, FALSE ); |
---|
362 | break; |
---|
363 | case 'd': tr_bencDictAddInt ( d, TR_PREFS_KEY_DSPEED_KBps, atoi( optarg ) ); |
---|
364 | tr_bencDictAddBool( d, TR_PREFS_KEY_DSPEED_ENABLED, TRUE ); |
---|
365 | break; |
---|
366 | case 'D': tr_bencDictAddBool( d, TR_PREFS_KEY_DSPEED_ENABLED, FALSE ); |
---|
367 | break; |
---|
368 | case 'f': tr_bencDictAddStr( d, TR_PREFS_KEY_SCRIPT_TORRENT_DONE_FILENAME, optarg ); |
---|
369 | tr_bencDictAddBool( d, TR_PREFS_KEY_SCRIPT_TORRENT_DONE_ENABLED, TRUE ); |
---|
370 | break; |
---|
371 | case 'g': /* handled above */ |
---|
372 | break; |
---|
373 | case 'm': tr_bencDictAddBool( d, TR_PREFS_KEY_PORT_FORWARDING, TRUE ); |
---|
374 | break; |
---|
375 | case 'M': tr_bencDictAddBool( d, TR_PREFS_KEY_PORT_FORWARDING, FALSE ); |
---|
376 | break; |
---|
377 | case 'p': tr_bencDictAddInt( d, TR_PREFS_KEY_PEER_PORT, atoi( optarg ) ); |
---|
378 | break; |
---|
379 | case 't': tr_bencDictAddInt( d, TR_PREFS_KEY_PEER_SOCKET_TOS, atoi( optarg ) ); |
---|
380 | break; |
---|
381 | case 'u': tr_bencDictAddInt( d, TR_PREFS_KEY_USPEED_KBps, atoi( optarg ) ); |
---|
382 | tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED, TRUE ); |
---|
383 | break; |
---|
384 | case 'U': tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED, FALSE ); |
---|
385 | break; |
---|
386 | case 'v': verify = 1; |
---|
387 | break; |
---|
388 | case 'w': tr_bencDictAddStr( d, TR_PREFS_KEY_DOWNLOAD_DIR, optarg ); |
---|
389 | break; |
---|
390 | case 910: tr_bencDictAddInt( d, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_REQUIRED ); |
---|
391 | break; |
---|
392 | case 911: tr_bencDictAddInt( d, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_PREFERRED ); |
---|
393 | break; |
---|
394 | case 912: tr_bencDictAddInt( d, TR_PREFS_KEY_ENCRYPTION, TR_CLEAR_PREFERRED ); |
---|
395 | break; |
---|
396 | case TR_OPT_UNK: |
---|
397 | torrentPath = optarg; |
---|
398 | break; |
---|
399 | default: return 1; |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | return 0; |
---|
404 | } |
---|
405 | |
---|
406 | static void |
---|
407 | sigHandler( int signal ) |
---|
408 | { |
---|
409 | switch( signal ) |
---|
410 | { |
---|
411 | case SIGINT: |
---|
412 | gotsig = 1; break; |
---|
413 | |
---|
414 | #ifndef WIN32 |
---|
415 | case SIGHUP: |
---|
416 | manualUpdate = 1; break; |
---|
417 | |
---|
418 | #endif |
---|
419 | default: |
---|
420 | break; |
---|
421 | } |
---|
422 | } |
---|
423 | |
---|