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