1 | /****************************************************************************** |
---|
2 | * $Id: transmissioncli.c 2356 2007-07-15 17:19:07Z 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 <getopt.h> |
---|
30 | #include <signal.h> |
---|
31 | #include <transmission.h> |
---|
32 | #include <makemeta.h> |
---|
33 | #ifdef SYS_BEOS |
---|
34 | #include <kernel/OS.h> |
---|
35 | #define usleep snooze |
---|
36 | #endif |
---|
37 | |
---|
38 | /* macro to shut up "unused parameter" warnings */ |
---|
39 | #ifdef __GNUC__ |
---|
40 | #define UNUSED __attribute__((unused)) |
---|
41 | #else |
---|
42 | #define UNUSED |
---|
43 | #endif |
---|
44 | |
---|
45 | const char * USAGE = |
---|
46 | "Usage: %s [options] file.torrent [options]\n\n" |
---|
47 | "Options:\n" |
---|
48 | " -c, --create-from <file> Create torrent from the specified source file.\n" |
---|
49 | " -a, --announce <url> Used in conjunction with -c.\n" |
---|
50 | " -r, --private Used in conjunction with -c.\n" |
---|
51 | " -m, --comment <text> Adds an optional comment when creating a torrent.\n" |
---|
52 | " -d, --download <int> Maximum download rate (-1 = no limit, default = -1)\n" |
---|
53 | " -f, --finish <shell script> Command you wish to run on completion\n" |
---|
54 | " -h, --help Print this help and exit\n" |
---|
55 | " -i, --info Print metainfo and exit\n" |
---|
56 | " -n --nat-traversal Attempt NAT traversal using NAT-PMP or UPnP IGD\n" |
---|
57 | " -p, --port <int> Port we should listen on (default = %d)\n" |
---|
58 | " -s, --scrape Print counts of seeders/leechers and exit\n" |
---|
59 | " -u, --upload <int> Maximum upload rate (-1 = no limit, default = 20)\n" |
---|
60 | " -v, --verbose <int> Verbose level (0 to 2, default = 0)\n"; |
---|
61 | |
---|
62 | static int showHelp = 0; |
---|
63 | static int showInfo = 0; |
---|
64 | static int showScrape = 0; |
---|
65 | static int isPrivate = 0; |
---|
66 | static int verboseLevel = 0; |
---|
67 | static int bindPort = TR_DEFAULT_PORT; |
---|
68 | static int uploadLimit = 20; |
---|
69 | static int downloadLimit = -1; |
---|
70 | static char * torrentPath = NULL; |
---|
71 | static int natTraversal = 0; |
---|
72 | static sig_atomic_t gotsig = 0; |
---|
73 | static tr_torrent_t * tor; |
---|
74 | |
---|
75 | static char * finishCall = NULL; |
---|
76 | static char * announce = NULL; |
---|
77 | static char * sourceFile = NULL; |
---|
78 | static char * comment = NULL; |
---|
79 | |
---|
80 | static int parseCommandLine ( int argc, char ** argv ); |
---|
81 | static void sigHandler ( int signal ); |
---|
82 | |
---|
83 | char * getStringRatio( float ratio ) |
---|
84 | { |
---|
85 | static char string[20]; |
---|
86 | |
---|
87 | if( ratio == TR_RATIO_NA ) |
---|
88 | return "n/a"; |
---|
89 | snprintf( string, sizeof string, "%.3f", ratio ); |
---|
90 | return string; |
---|
91 | } |
---|
92 | |
---|
93 | #define LINEWIDTH 80 |
---|
94 | |
---|
95 | int main( int argc, char ** argv ) |
---|
96 | { |
---|
97 | int i, error; |
---|
98 | tr_handle_t * h; |
---|
99 | tr_stat_t * s; |
---|
100 | tr_handle_status_t * hstat; |
---|
101 | |
---|
102 | printf( "Transmission %s - http://transmission.m0k.org/\n\n", |
---|
103 | LONG_VERSION_STRING ); |
---|
104 | |
---|
105 | /* Get options */ |
---|
106 | if( parseCommandLine( argc, argv ) ) |
---|
107 | { |
---|
108 | printf( USAGE, argv[0], TR_DEFAULT_PORT ); |
---|
109 | return EXIT_FAILURE; |
---|
110 | } |
---|
111 | |
---|
112 | if( showHelp ) |
---|
113 | { |
---|
114 | printf( USAGE, argv[0], TR_DEFAULT_PORT ); |
---|
115 | return EXIT_SUCCESS; |
---|
116 | } |
---|
117 | |
---|
118 | if( verboseLevel < 0 ) |
---|
119 | { |
---|
120 | verboseLevel = 0; |
---|
121 | } |
---|
122 | else if( verboseLevel > 9 ) |
---|
123 | { |
---|
124 | verboseLevel = 9; |
---|
125 | } |
---|
126 | if( verboseLevel ) |
---|
127 | { |
---|
128 | static char env[11]; |
---|
129 | snprintf( env, sizeof env, "TR_DEBUG=%d", verboseLevel ); |
---|
130 | putenv( env ); |
---|
131 | } |
---|
132 | |
---|
133 | if( bindPort < 1 || bindPort > 65535 ) |
---|
134 | { |
---|
135 | printf( "Invalid port '%d'\n", bindPort ); |
---|
136 | return EXIT_FAILURE; |
---|
137 | } |
---|
138 | |
---|
139 | /* Initialize libtransmission */ |
---|
140 | h = tr_init( "cli" ); |
---|
141 | |
---|
142 | if( sourceFile && *sourceFile ) /* creating a torrent */ |
---|
143 | { |
---|
144 | int ret; |
---|
145 | tr_metainfo_builder_t* builder = tr_metaInfoBuilderCreate( h, sourceFile ); |
---|
146 | tr_makeMetaInfo( builder, NULL, announce, comment, isPrivate ); |
---|
147 | while( !builder->isDone ) { |
---|
148 | usleep( 1 ); |
---|
149 | printf( "." ); |
---|
150 | } |
---|
151 | ret = !builder->failed; |
---|
152 | tr_metaInfoBuilderFree( builder ); |
---|
153 | return ret; |
---|
154 | } |
---|
155 | |
---|
156 | /* Open and parse torrent file */ |
---|
157 | if( !( tor = tr_torrentInit( h, torrentPath, ".", 0, &error ) ) ) |
---|
158 | { |
---|
159 | printf( "Failed opening torrent file `%s'\n", torrentPath ); |
---|
160 | tr_close( h ); |
---|
161 | return EXIT_FAILURE; |
---|
162 | } |
---|
163 | |
---|
164 | if( showInfo ) |
---|
165 | { |
---|
166 | const tr_info_t * info = tr_torrentInfo( tor ); |
---|
167 | |
---|
168 | s = tr_torrentStat( tor ); |
---|
169 | |
---|
170 | /* Print torrent info (quite à la btshowmetainfo) */ |
---|
171 | printf( "hash: " ); |
---|
172 | for( i = 0; i < SHA_DIGEST_LENGTH; i++ ) |
---|
173 | { |
---|
174 | printf( "%02x", info->hash[i] ); |
---|
175 | } |
---|
176 | printf( "\n" ); |
---|
177 | printf( "tracker: %s:%d\n", |
---|
178 | s->tracker->address, s->tracker->port ); |
---|
179 | printf( "announce: %s\n", s->tracker->announce ); |
---|
180 | printf( "size: %"PRIu64" (%"PRIu64" * %d + %"PRIu64")\n", |
---|
181 | info->totalSize, info->totalSize / info->pieceSize, |
---|
182 | info->pieceSize, info->totalSize % info->pieceSize ); |
---|
183 | if( info->comment[0] ) |
---|
184 | { |
---|
185 | printf( "comment: %s\n", info->comment ); |
---|
186 | } |
---|
187 | if( info->creator[0] ) |
---|
188 | { |
---|
189 | printf( "creator: %s\n", info->creator ); |
---|
190 | } |
---|
191 | if( TR_FLAG_PRIVATE & info->flags ) |
---|
192 | { |
---|
193 | printf( "private flag set\n" ); |
---|
194 | } |
---|
195 | printf( "file(s):\n" ); |
---|
196 | for( i = 0; i < info->fileCount; i++ ) |
---|
197 | { |
---|
198 | printf( " %s (%"PRIu64")\n", info->files[i].name, |
---|
199 | info->files[i].length ); |
---|
200 | } |
---|
201 | |
---|
202 | goto cleanup; |
---|
203 | } |
---|
204 | |
---|
205 | if( showScrape ) |
---|
206 | { |
---|
207 | int seeders, leechers, downloaded; |
---|
208 | |
---|
209 | if( tr_torrentScrape( tor, &seeders, &leechers, &downloaded ) ) |
---|
210 | { |
---|
211 | printf( "Scrape failed.\n" ); |
---|
212 | } |
---|
213 | else |
---|
214 | { |
---|
215 | printf( "%d seeder(s), %d leecher(s), %d download(s).\n", |
---|
216 | seeders, leechers, downloaded ); |
---|
217 | } |
---|
218 | |
---|
219 | goto cleanup; |
---|
220 | } |
---|
221 | |
---|
222 | signal( SIGINT, sigHandler ); |
---|
223 | |
---|
224 | tr_setBindPort( h, bindPort ); |
---|
225 | tr_setGlobalUploadLimit( h, uploadLimit ); |
---|
226 | tr_setGlobalDownloadLimit( h, downloadLimit ); |
---|
227 | |
---|
228 | tr_natTraversalEnable( h, natTraversal ); |
---|
229 | |
---|
230 | tr_torrentStart( tor ); |
---|
231 | |
---|
232 | for( ;; ) |
---|
233 | { |
---|
234 | char string[LINEWIDTH]; |
---|
235 | int chars = 0; |
---|
236 | int result; |
---|
237 | |
---|
238 | sleep( 1 ); |
---|
239 | |
---|
240 | if( gotsig ) |
---|
241 | { |
---|
242 | gotsig = 0; |
---|
243 | tr_torrentStop( tor ); |
---|
244 | } |
---|
245 | |
---|
246 | s = tr_torrentStat( tor ); |
---|
247 | |
---|
248 | if( s->status & TR_STATUS_CHECK_WAIT ) |
---|
249 | { |
---|
250 | chars = snprintf( string, sizeof string, |
---|
251 | "Waiting to check files... %.2f %%", 100.0 * s->percentDone ); |
---|
252 | } |
---|
253 | else if( s->status & TR_STATUS_CHECK ) |
---|
254 | { |
---|
255 | chars = snprintf( string, sizeof string, |
---|
256 | "Checking files... %.2f %%", 100.0 * s->percentDone ); |
---|
257 | } |
---|
258 | else if( s->status & TR_STATUS_DOWNLOAD ) |
---|
259 | { |
---|
260 | chars = snprintf( string, sizeof string, |
---|
261 | "Progress: %.2f %%, %d peer%s, dl from %d (%.2f KB/s), " |
---|
262 | "ul to %d (%.2f KB/s) [%s]", 100.0 * s->percentDone, |
---|
263 | s->peersConnected, ( s->peersConnected == 1 ) ? "" : "s", |
---|
264 | s->peersUploading, s->rateDownload, |
---|
265 | s->peersDownloading, s->rateUpload, |
---|
266 | getStringRatio(s->ratio) ); |
---|
267 | } |
---|
268 | else if( s->status & TR_STATUS_SEED ) |
---|
269 | { |
---|
270 | chars = snprintf( string, sizeof string, |
---|
271 | "Seeding, uploading to %d of %d peer(s), %.2f KB/s [%s]", |
---|
272 | s->peersDownloading, s->peersTotal, |
---|
273 | s->rateUpload, getStringRatio(s->ratio) ); |
---|
274 | } |
---|
275 | else if( s->status & TR_STATUS_STOPPING ) |
---|
276 | { |
---|
277 | chars = snprintf( string, sizeof string, "Stopping..." ); |
---|
278 | } |
---|
279 | if( ( signed )sizeof string > chars ) |
---|
280 | { |
---|
281 | memset( &string[chars], ' ', sizeof string - 1 - chars ); |
---|
282 | } |
---|
283 | string[sizeof string - 1] = '\0'; |
---|
284 | fprintf( stderr, "\r%s", string ); |
---|
285 | |
---|
286 | if( s->error ) |
---|
287 | { |
---|
288 | fprintf( stderr, "\n%s\n", s->errorString ); |
---|
289 | } |
---|
290 | else if( verboseLevel > 0 ) |
---|
291 | { |
---|
292 | fprintf( stderr, "\n" ); |
---|
293 | } |
---|
294 | |
---|
295 | if( tr_getDone(tor) || tr_getComplete(tor) ) |
---|
296 | { |
---|
297 | result = system(finishCall); |
---|
298 | } |
---|
299 | } |
---|
300 | fprintf( stderr, "\n" ); |
---|
301 | |
---|
302 | /* Try for 5 seconds to delete any port mappings for nat traversal */ |
---|
303 | tr_natTraversalEnable( h, 0 ); |
---|
304 | for( i = 0; i < 10; i++ ) |
---|
305 | { |
---|
306 | hstat = tr_handleStatus( h ); |
---|
307 | if( TR_NAT_TRAVERSAL_DISABLED == hstat->natTraversalStatus ) |
---|
308 | { |
---|
309 | /* Port mappings were deleted */ |
---|
310 | break; |
---|
311 | } |
---|
312 | usleep( 500000 ); |
---|
313 | } |
---|
314 | |
---|
315 | cleanup: |
---|
316 | tr_torrentClose( tor ); |
---|
317 | tr_close( h ); |
---|
318 | |
---|
319 | return EXIT_SUCCESS; |
---|
320 | } |
---|
321 | |
---|
322 | static int parseCommandLine( int argc, char ** argv ) |
---|
323 | { |
---|
324 | for( ;; ) |
---|
325 | { |
---|
326 | static struct option long_options[] = |
---|
327 | { { "help", no_argument, NULL, 'h' }, |
---|
328 | { "info", no_argument, NULL, 'i' }, |
---|
329 | { "scrape", no_argument, NULL, 's' }, |
---|
330 | { "private", no_argument, NULL, 'r' }, |
---|
331 | { "verbose", required_argument, NULL, 'v' }, |
---|
332 | { "port", required_argument, NULL, 'p' }, |
---|
333 | { "upload", required_argument, NULL, 'u' }, |
---|
334 | { "download", required_argument, NULL, 'd' }, |
---|
335 | { "finish", required_argument, NULL, 'f' }, |
---|
336 | { "create", required_argument, NULL, 'c' }, |
---|
337 | { "comment", required_argument, NULL, 'm' }, |
---|
338 | { "announce", required_argument, NULL, 'a' }, |
---|
339 | { "nat-traversal", no_argument, NULL, 'n' }, |
---|
340 | { 0, 0, 0, 0} }; |
---|
341 | |
---|
342 | int c, optind = 0; |
---|
343 | c = getopt_long( argc, argv, "hisrv:p:u:d:f:c:m:a:n:", |
---|
344 | long_options, &optind ); |
---|
345 | if( c < 0 ) |
---|
346 | { |
---|
347 | break; |
---|
348 | } |
---|
349 | switch( c ) |
---|
350 | { |
---|
351 | case 'h': |
---|
352 | showHelp = 1; |
---|
353 | break; |
---|
354 | case 'i': |
---|
355 | showInfo = 1; |
---|
356 | break; |
---|
357 | case 's': |
---|
358 | showScrape = 1; |
---|
359 | break; |
---|
360 | case 'r': |
---|
361 | isPrivate = 1; |
---|
362 | break; |
---|
363 | case 'v': |
---|
364 | verboseLevel = atoi( optarg ); |
---|
365 | break; |
---|
366 | case 'p': |
---|
367 | bindPort = atoi( optarg ); |
---|
368 | break; |
---|
369 | case 'u': |
---|
370 | uploadLimit = atoi( optarg ); |
---|
371 | break; |
---|
372 | case 'd': |
---|
373 | downloadLimit = atoi( optarg ); |
---|
374 | break; |
---|
375 | case 'f': |
---|
376 | finishCall = optarg; |
---|
377 | break; |
---|
378 | case 'c': |
---|
379 | sourceFile = optarg; |
---|
380 | break; |
---|
381 | case 'm': |
---|
382 | comment = optarg; |
---|
383 | break; |
---|
384 | case 'a': |
---|
385 | announce = optarg; |
---|
386 | break; |
---|
387 | case 'n': |
---|
388 | natTraversal = 1; |
---|
389 | break; |
---|
390 | default: |
---|
391 | return 1; |
---|
392 | } |
---|
393 | } |
---|
394 | |
---|
395 | if( optind > argc - 1 ) |
---|
396 | { |
---|
397 | return !showHelp; |
---|
398 | } |
---|
399 | |
---|
400 | torrentPath = argv[optind]; |
---|
401 | |
---|
402 | return 0; |
---|
403 | } |
---|
404 | |
---|
405 | static void sigHandler( int signal ) |
---|
406 | { |
---|
407 | switch( signal ) |
---|
408 | { |
---|
409 | case SIGINT: |
---|
410 | gotsig = 1; |
---|
411 | break; |
---|
412 | |
---|
413 | default: |
---|
414 | break; |
---|
415 | } |
---|
416 | } |
---|