1 | /****************************************************************************** |
---|
2 | * $Id: transmissioncli.c 1287 2006-12-27 00:33:22Z joshe $ |
---|
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 | #ifdef SYS_BEOS |
---|
33 | #include <kernel/OS.h> |
---|
34 | #define usleep snooze |
---|
35 | #endif |
---|
36 | |
---|
37 | #define USAGE \ |
---|
38 | "Usage: %s [options] file.torrent [options]\n\n" \ |
---|
39 | "Options:\n" \ |
---|
40 | " -d, --download <int> Maximum download rate (-1 = no limit, default = -1)\n"\ |
---|
41 | " -f, --finish <shell script> Command you wish to run on completion\n" \ |
---|
42 | " -h, --help Print this help and exit\n" \ |
---|
43 | " -i, --info Print metainfo and exit\n" \ |
---|
44 | " -n --nat-traversal Attempt NAT traversal using NAT-PMP or UPnP IGD\n" \ |
---|
45 | " -p, --port <int> Port we should listen on (default = %d)\n" \ |
---|
46 | " -s, --scrape Print counts of seeders/leechers and exit\n" \ |
---|
47 | " -u, --upload <int> Maximum upload rate (-1 = no limit, default = 20)\n" \ |
---|
48 | " -v, --verbose <int> Verbose level (0 to 2, default = 0)\n" |
---|
49 | |
---|
50 | static int showHelp = 0; |
---|
51 | static int showInfo = 0; |
---|
52 | static int showScrape = 0; |
---|
53 | static int verboseLevel = 0; |
---|
54 | static int bindPort = TR_DEFAULT_PORT; |
---|
55 | static int uploadLimit = 20; |
---|
56 | static int downloadLimit = -1; |
---|
57 | static char * torrentPath = NULL; |
---|
58 | static volatile char mustDie = 0; |
---|
59 | static int natTraversal = 0; |
---|
60 | |
---|
61 | static char * finishCall = NULL; |
---|
62 | |
---|
63 | static int parseCommandLine ( int argc, char ** argv ); |
---|
64 | static void sigHandler ( int signal ); |
---|
65 | |
---|
66 | int main( int argc, char ** argv ) |
---|
67 | { |
---|
68 | int i, error, nat; |
---|
69 | tr_handle_t * h; |
---|
70 | tr_torrent_t * tor; |
---|
71 | tr_stat_t * s; |
---|
72 | |
---|
73 | printf( "Transmission %s (%d) - http://transmission.m0k.org/\n\n", |
---|
74 | VERSION_STRING, VERSION_REVISION ); |
---|
75 | |
---|
76 | /* Get options */ |
---|
77 | if( parseCommandLine( argc, argv ) ) |
---|
78 | { |
---|
79 | printf( USAGE, argv[0], TR_DEFAULT_PORT ); |
---|
80 | return 1; |
---|
81 | } |
---|
82 | |
---|
83 | if( showHelp ) |
---|
84 | { |
---|
85 | printf( USAGE, argv[0], TR_DEFAULT_PORT ); |
---|
86 | return 0; |
---|
87 | } |
---|
88 | |
---|
89 | if( verboseLevel < 0 ) |
---|
90 | { |
---|
91 | verboseLevel = 0; |
---|
92 | } |
---|
93 | else if( verboseLevel > 9 ) |
---|
94 | { |
---|
95 | verboseLevel = 9; |
---|
96 | } |
---|
97 | if( verboseLevel ) |
---|
98 | { |
---|
99 | static char env[11]; |
---|
100 | sprintf( env, "TR_DEBUG=%d", verboseLevel ); |
---|
101 | putenv( env ); |
---|
102 | } |
---|
103 | |
---|
104 | if( bindPort < 1 || bindPort > 65535 ) |
---|
105 | { |
---|
106 | printf( "Invalid port '%d'\n", bindPort ); |
---|
107 | return 1; |
---|
108 | } |
---|
109 | |
---|
110 | /* Initialize libtransmission */ |
---|
111 | h = tr_init(); |
---|
112 | |
---|
113 | /* Open and parse torrent file */ |
---|
114 | if( !( tor = tr_torrentInit( h, torrentPath, 0, &error ) ) ) |
---|
115 | { |
---|
116 | printf( "Failed opening torrent file `%s'\n", torrentPath ); |
---|
117 | goto failed; |
---|
118 | } |
---|
119 | |
---|
120 | if( showInfo ) |
---|
121 | { |
---|
122 | tr_info_t * info = tr_torrentInfo( tor ); |
---|
123 | |
---|
124 | s = tr_torrentStat( tor ); |
---|
125 | |
---|
126 | /* Print torrent info (quite à la btshowmetainfo) */ |
---|
127 | printf( "hash: " ); |
---|
128 | for( i = 0; i < SHA_DIGEST_LENGTH; i++ ) |
---|
129 | { |
---|
130 | printf( "%02x", info->hash[i] ); |
---|
131 | } |
---|
132 | printf( "\n" ); |
---|
133 | printf( "tracker: %s:%d\n", |
---|
134 | s->trackerAddress, s->trackerPort ); |
---|
135 | printf( "announce: %s\n", s->trackerAnnounce ); |
---|
136 | printf( "size: %"PRIu64" (%"PRIu64" * %d + %"PRIu64")\n", |
---|
137 | info->totalSize, info->totalSize / info->pieceSize, |
---|
138 | info->pieceSize, info->totalSize % info->pieceSize ); |
---|
139 | if( info->comment[0] ) |
---|
140 | { |
---|
141 | printf( "comment: %s\n", info->comment ); |
---|
142 | } |
---|
143 | if( info->creator[0] ) |
---|
144 | { |
---|
145 | printf( "creator: %s\n", info->creator ); |
---|
146 | } |
---|
147 | printf( "file(s):\n" ); |
---|
148 | for( i = 0; i < info->fileCount; i++ ) |
---|
149 | { |
---|
150 | printf( " %s (%"PRIu64")\n", info->files[i].name, |
---|
151 | info->files[i].length ); |
---|
152 | } |
---|
153 | |
---|
154 | goto cleanup; |
---|
155 | } |
---|
156 | |
---|
157 | if( showScrape ) |
---|
158 | { |
---|
159 | int seeders, leechers, downloaded; |
---|
160 | |
---|
161 | if( tr_torrentScrape( tor, &seeders, &leechers, &downloaded ) ) |
---|
162 | { |
---|
163 | printf( "Scrape failed.\n" ); |
---|
164 | } |
---|
165 | else |
---|
166 | { |
---|
167 | printf( "%d seeder(s), %d leecher(s), %d download(s).\n", |
---|
168 | seeders, leechers, downloaded ); |
---|
169 | } |
---|
170 | |
---|
171 | goto cleanup; |
---|
172 | } |
---|
173 | |
---|
174 | signal( SIGINT, sigHandler ); |
---|
175 | |
---|
176 | tr_setBindPort( h, bindPort ); |
---|
177 | tr_setGlobalUploadLimit( h, uploadLimit ); |
---|
178 | tr_setGlobalDownloadLimit( h, downloadLimit ); |
---|
179 | |
---|
180 | if( natTraversal ) |
---|
181 | { |
---|
182 | tr_natTraversalEnable( h ); |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | tr_natTraversalDisable( h ); |
---|
187 | } |
---|
188 | |
---|
189 | tr_torrentSetFolder( tor, "." ); |
---|
190 | tr_torrentStart( tor ); |
---|
191 | |
---|
192 | while( !mustDie ) |
---|
193 | { |
---|
194 | char string[80]; |
---|
195 | int chars = 0; |
---|
196 | int result; |
---|
197 | |
---|
198 | sleep( 1 ); |
---|
199 | |
---|
200 | s = tr_torrentStat( tor ); |
---|
201 | |
---|
202 | if( s->status & TR_STATUS_CHECK ) |
---|
203 | { |
---|
204 | chars = snprintf( string, 80, |
---|
205 | "Checking files... %.2f %%", 100.0 * s->progress ); |
---|
206 | } |
---|
207 | else if( s->status & TR_STATUS_DOWNLOAD ) |
---|
208 | { |
---|
209 | chars = snprintf( string, 80, |
---|
210 | "Progress: %.2f %%, %d peer%s, dl from %d (%.2f KB/s), " |
---|
211 | "ul to %d (%.2f KB/s)", 100.0 * s->progress, |
---|
212 | s->peersTotal, ( s->peersTotal == 1 ) ? "" : "s", |
---|
213 | s->peersUploading, s->rateDownload, |
---|
214 | s->peersDownloading, s->rateUpload ); |
---|
215 | } |
---|
216 | else if( s->status & TR_STATUS_SEED ) |
---|
217 | { |
---|
218 | chars = snprintf( string, 80, |
---|
219 | "Seeding, uploading to %d of %d peer(s), %.2f KB/s", |
---|
220 | s->peersDownloading, s->peersTotal, |
---|
221 | s->rateUpload ); |
---|
222 | } |
---|
223 | memset( &string[chars], ' ', 79 - chars ); |
---|
224 | string[79] = '\0'; |
---|
225 | fprintf( stderr, "\r%s", string ); |
---|
226 | |
---|
227 | if( s->error & TR_ETRACKER ) |
---|
228 | { |
---|
229 | fprintf( stderr, "\n%s\n", s->trackerError ); |
---|
230 | } |
---|
231 | else if( verboseLevel > 0 ) |
---|
232 | { |
---|
233 | fprintf( stderr, "\n" ); |
---|
234 | } |
---|
235 | |
---|
236 | if( tr_getFinished( tor ) ) |
---|
237 | { |
---|
238 | result = system(finishCall); |
---|
239 | } |
---|
240 | } |
---|
241 | fprintf( stderr, "\n" ); |
---|
242 | |
---|
243 | /* Try for 5 seconds to notify the tracker that we are leaving |
---|
244 | and to delete any port mappings for nat traversal */ |
---|
245 | tr_torrentStop( tor ); |
---|
246 | tr_natTraversalDisable( h ); |
---|
247 | for( i = 0; i < 10; i++ ) |
---|
248 | { |
---|
249 | s = tr_torrentStat( tor ); |
---|
250 | nat = tr_natTraversalStatus( h ); |
---|
251 | if( s->status & TR_STATUS_PAUSE && TR_NAT_TRAVERSAL_DISABLED == nat ) |
---|
252 | { |
---|
253 | /* The 'stopped' tracker message was sent |
---|
254 | and port mappings were deleted */ |
---|
255 | break; |
---|
256 | } |
---|
257 | usleep( 500000 ); |
---|
258 | } |
---|
259 | |
---|
260 | cleanup: |
---|
261 | tr_torrentClose( h, tor ); |
---|
262 | |
---|
263 | failed: |
---|
264 | tr_close( h ); |
---|
265 | |
---|
266 | return 0; |
---|
267 | } |
---|
268 | |
---|
269 | static int parseCommandLine( int argc, char ** argv ) |
---|
270 | { |
---|
271 | for( ;; ) |
---|
272 | { |
---|
273 | static struct option long_options[] = |
---|
274 | { { "help", no_argument, NULL, 'h' }, |
---|
275 | { "info", no_argument, NULL, 'i' }, |
---|
276 | { "scrape", no_argument, NULL, 's' }, |
---|
277 | { "verbose", required_argument, NULL, 'v' }, |
---|
278 | { "port", required_argument, NULL, 'p' }, |
---|
279 | { "upload", required_argument, NULL, 'u' }, |
---|
280 | { "download", required_argument, NULL, 'd' }, |
---|
281 | { "finish", required_argument, NULL, 'f' }, |
---|
282 | { "nat-traversal", no_argument, NULL, 'n' }, |
---|
283 | { 0, 0, 0, 0} }; |
---|
284 | |
---|
285 | int c, optind = 0; |
---|
286 | c = getopt_long( argc, argv, "hisv:p:u:d:f:n", long_options, &optind ); |
---|
287 | if( c < 0 ) |
---|
288 | { |
---|
289 | break; |
---|
290 | } |
---|
291 | switch( c ) |
---|
292 | { |
---|
293 | case 'h': |
---|
294 | showHelp = 1; |
---|
295 | break; |
---|
296 | case 'i': |
---|
297 | showInfo = 1; |
---|
298 | break; |
---|
299 | case 's': |
---|
300 | showScrape = 1; |
---|
301 | break; |
---|
302 | case 'v': |
---|
303 | verboseLevel = atoi( optarg ); |
---|
304 | break; |
---|
305 | case 'p': |
---|
306 | bindPort = atoi( optarg ); |
---|
307 | break; |
---|
308 | case 'u': |
---|
309 | uploadLimit = atoi( optarg ); |
---|
310 | break; |
---|
311 | case 'd': |
---|
312 | downloadLimit = atoi( optarg ); |
---|
313 | break; |
---|
314 | case 'f': |
---|
315 | finishCall = optarg; |
---|
316 | break; |
---|
317 | case 'n': |
---|
318 | natTraversal = 1; |
---|
319 | break; |
---|
320 | default: |
---|
321 | return 1; |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | if( optind > argc - 1 ) |
---|
326 | { |
---|
327 | return !showHelp; |
---|
328 | } |
---|
329 | |
---|
330 | torrentPath = argv[optind]; |
---|
331 | |
---|
332 | return 0; |
---|
333 | } |
---|
334 | |
---|
335 | static void sigHandler( int signal ) |
---|
336 | { |
---|
337 | switch( signal ) |
---|
338 | { |
---|
339 | case SIGINT: |
---|
340 | mustDie = 1; |
---|
341 | break; |
---|
342 | |
---|
343 | default: |
---|
344 | break; |
---|
345 | } |
---|
346 | } |
---|