1 | /****************************************************************************** |
---|
2 | * Copyright (c) 2005 Eric Petit |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
5 | * copy of this software and associated documentation files (the "Software"), |
---|
6 | * to deal in the Software without restriction, including without limitation |
---|
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
8 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
9 | * Software is furnished to do so, subject to the following conditions: |
---|
10 | * |
---|
11 | * The above copyright notice and this permission notice shall be included in |
---|
12 | * all copies or substantial portions of the Software. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
20 | * DEALINGS IN THE SOFTWARE. |
---|
21 | *****************************************************************************/ |
---|
22 | |
---|
23 | #include <stdio.h> |
---|
24 | #include <stdlib.h> |
---|
25 | #include <string.h> |
---|
26 | #include <unistd.h> |
---|
27 | #include <getopt.h> |
---|
28 | #include <signal.h> |
---|
29 | #include <transmission.h> |
---|
30 | #ifdef SYS_BEOS |
---|
31 | #include <kernel/OS.h> |
---|
32 | #define usleep snooze |
---|
33 | #endif |
---|
34 | |
---|
35 | #define USAGE \ |
---|
36 | "Usage: %s [options] file.torrent [options]\n\n" \ |
---|
37 | "Options:\n" \ |
---|
38 | " -h, --help Print this help and exit\n" \ |
---|
39 | " -i, --info Print metainfo and exit\n" \ |
---|
40 | " -s, --scrape Print counts of seeders/leechers and exit\n" \ |
---|
41 | " -v, --verbose <int> Verbose level (0 to 2, default = 0)\n" \ |
---|
42 | " -p, --port <int> Port we should listen on (default = %d)\n" \ |
---|
43 | " -u, --upload <int> Maximum upload rate (-1 = no limit, default = 20)\n" \ |
---|
44 | " -f, --finish <shell script> Command you wish to run on completion\n" |
---|
45 | |
---|
46 | static int showHelp = 0; |
---|
47 | static int showInfo = 0; |
---|
48 | static int showScrape = 0; |
---|
49 | static int verboseLevel = 0; |
---|
50 | static int bindPort = TR_DEFAULT_PORT; |
---|
51 | static int uploadLimit = 20; |
---|
52 | static char * torrentPath = NULL; |
---|
53 | static volatile char mustDie = 0; |
---|
54 | |
---|
55 | static char * finishCall = NULL; |
---|
56 | |
---|
57 | static int parseCommandLine ( int argc, char ** argv ); |
---|
58 | static void sigHandler ( int signal ); |
---|
59 | |
---|
60 | int main( int argc, char ** argv ) |
---|
61 | { |
---|
62 | int i, count; |
---|
63 | tr_handle_t * h; |
---|
64 | tr_stat_t * s; |
---|
65 | |
---|
66 | printf( "Transmission %s - http://transmission.m0k.org/\n\n", |
---|
67 | VERSION_STRING ); |
---|
68 | |
---|
69 | /* Get options */ |
---|
70 | if( parseCommandLine( argc, argv ) ) |
---|
71 | { |
---|
72 | printf( USAGE, argv[0], TR_DEFAULT_PORT ); |
---|
73 | return 1; |
---|
74 | } |
---|
75 | |
---|
76 | if( showHelp ) |
---|
77 | { |
---|
78 | printf( USAGE, argv[0], TR_DEFAULT_PORT ); |
---|
79 | return 0; |
---|
80 | } |
---|
81 | |
---|
82 | if( verboseLevel < 0 ) |
---|
83 | { |
---|
84 | verboseLevel = 0; |
---|
85 | } |
---|
86 | else if( verboseLevel > 9 ) |
---|
87 | { |
---|
88 | verboseLevel = 9; |
---|
89 | } |
---|
90 | if( verboseLevel ) |
---|
91 | { |
---|
92 | static char env[11]; |
---|
93 | sprintf( env, "TR_DEBUG=%d", verboseLevel ); |
---|
94 | putenv( env ); |
---|
95 | } |
---|
96 | |
---|
97 | if( bindPort < 1 || bindPort > 65535 ) |
---|
98 | { |
---|
99 | printf( "Invalid port '%d'\n", bindPort ); |
---|
100 | return 1; |
---|
101 | } |
---|
102 | |
---|
103 | /* Initialize libtransmission */ |
---|
104 | h = tr_init(); |
---|
105 | |
---|
106 | /* Open and parse torrent file */ |
---|
107 | if( tr_torrentInit( h, torrentPath ) ) |
---|
108 | { |
---|
109 | printf( "Failed opening torrent file `%s'\n", torrentPath ); |
---|
110 | goto failed; |
---|
111 | } |
---|
112 | |
---|
113 | if( showInfo ) |
---|
114 | { |
---|
115 | tr_info_t * info; |
---|
116 | |
---|
117 | count = tr_torrentStat( h, &s ); |
---|
118 | info = &s[0].info; |
---|
119 | |
---|
120 | /* Print torrent info (quite à la btshowmetainfo) */ |
---|
121 | printf( "hash: " ); |
---|
122 | for( i = 0; i < SHA_DIGEST_LENGTH; i++ ) |
---|
123 | { |
---|
124 | printf( "%02x", info->hash[i] ); |
---|
125 | } |
---|
126 | printf( "\n" ); |
---|
127 | printf( "tracker: %s:%d\n", |
---|
128 | info->trackerAddress, info->trackerPort ); |
---|
129 | printf( "announce: %s\n", info->trackerAnnounce ); |
---|
130 | printf( "size: %lld (%lld * %d + %lld)\n", |
---|
131 | info->totalSize, info->totalSize / info->pieceSize, |
---|
132 | info->pieceSize, info->totalSize % info->pieceSize ); |
---|
133 | printf( "file(s):\n" ); |
---|
134 | for( i = 0; i < info->fileCount; i++ ) |
---|
135 | { |
---|
136 | printf( " %s (%lld)\n", info->files[i].name, |
---|
137 | info->files[i].length ); |
---|
138 | } |
---|
139 | |
---|
140 | free( s ); |
---|
141 | goto cleanup; |
---|
142 | } |
---|
143 | |
---|
144 | if( showScrape ) |
---|
145 | { |
---|
146 | int seeders, leechers; |
---|
147 | |
---|
148 | if( tr_torrentScrape( h, 0, &seeders, &leechers ) ) |
---|
149 | { |
---|
150 | printf( "Scrape failed.\n" ); |
---|
151 | } |
---|
152 | else |
---|
153 | { |
---|
154 | printf( "%d seeder(s), %d leecher(s).\n", seeders, leechers ); |
---|
155 | } |
---|
156 | |
---|
157 | goto cleanup; |
---|
158 | } |
---|
159 | |
---|
160 | signal( SIGINT, sigHandler ); |
---|
161 | |
---|
162 | tr_setBindPort( h, bindPort ); |
---|
163 | tr_setUploadLimit( h, uploadLimit ); |
---|
164 | |
---|
165 | tr_torrentSetFolder( h, 0, "." ); |
---|
166 | tr_torrentStart( h, 0 ); |
---|
167 | |
---|
168 | while( !mustDie ) |
---|
169 | { |
---|
170 | char string[80]; |
---|
171 | int chars = 0; |
---|
172 | int result; |
---|
173 | |
---|
174 | sleep( 1 ); |
---|
175 | |
---|
176 | count = tr_torrentStat( h, &s ); |
---|
177 | |
---|
178 | if( s[0].status & TR_STATUS_CHECK ) |
---|
179 | { |
---|
180 | chars = snprintf( string, 80, |
---|
181 | "Checking files... %.2f %%", 100.0 * s[0].progress ); |
---|
182 | } |
---|
183 | else if( s[0].status & TR_STATUS_DOWNLOAD ) |
---|
184 | { |
---|
185 | chars = snprintf( string, 80, |
---|
186 | "Progress: %.2f %%, %d peer%s, dl from %d (%.2f kbps), " |
---|
187 | "ul to %d (%.2f kbps)", 100.0 * s[0].progress, |
---|
188 | s[0].peersTotal, ( s[0].peersTotal == 1 ) ? "" : "s", |
---|
189 | s[0].peersUploading, s[0].rateDownload, |
---|
190 | s[0].peersDownloading, s[0].rateUpload ); |
---|
191 | } |
---|
192 | else if( s[0].status & TR_STATUS_SEED ) |
---|
193 | { |
---|
194 | chars = snprintf( string, 80, |
---|
195 | "Seeding, uploading to %d of %d peer(s), %.2f kbps", |
---|
196 | s[0].peersDownloading, s[0].peersTotal, |
---|
197 | s[0].rateUpload ); |
---|
198 | } |
---|
199 | memset( &string[chars], ' ', 79 - chars ); |
---|
200 | string[79] = '\0'; |
---|
201 | fprintf( stderr, "\r%s", string ); |
---|
202 | |
---|
203 | if( s[0].status & TR_TRACKER_ERROR ) |
---|
204 | { |
---|
205 | fprintf( stderr, "\n%s\n", s[0].error ); |
---|
206 | } |
---|
207 | else if( verboseLevel > 0 ) |
---|
208 | { |
---|
209 | fprintf( stderr, "\n" ); |
---|
210 | } |
---|
211 | |
---|
212 | if( tr_getFinished( h, 0 ) ) |
---|
213 | { |
---|
214 | tr_setFinished( h, 0, 0 ); |
---|
215 | result = system(finishCall); |
---|
216 | } |
---|
217 | |
---|
218 | free( s ); |
---|
219 | } |
---|
220 | fprintf( stderr, "\n" ); |
---|
221 | |
---|
222 | /* Try for 5 seconds to notice the tracker that we are leaving */ |
---|
223 | tr_torrentStop( h, 0 ); |
---|
224 | for( i = 0; i < 10; i++ ) |
---|
225 | { |
---|
226 | count = tr_torrentStat( h, &s ); |
---|
227 | if( s[0].status & TR_STATUS_PAUSE ) |
---|
228 | { |
---|
229 | /* The 'stopped' message was sent */ |
---|
230 | free( s ); |
---|
231 | break; |
---|
232 | } |
---|
233 | free( s ); |
---|
234 | usleep( 500000 ); |
---|
235 | } |
---|
236 | |
---|
237 | cleanup: |
---|
238 | tr_torrentClose( h, 0 ); |
---|
239 | |
---|
240 | failed: |
---|
241 | tr_close( h ); |
---|
242 | |
---|
243 | return 0; |
---|
244 | } |
---|
245 | |
---|
246 | static int parseCommandLine( int argc, char ** argv ) |
---|
247 | { |
---|
248 | for( ;; ) |
---|
249 | { |
---|
250 | static struct option long_options[] = |
---|
251 | { { "help", no_argument, NULL, 'h' }, |
---|
252 | { "info", no_argument, NULL, 'i' }, |
---|
253 | { "scrape", no_argument, NULL, 's' }, |
---|
254 | { "verbose", required_argument, NULL, 'v' }, |
---|
255 | { "port", required_argument, NULL, 'p' }, |
---|
256 | { "upload", required_argument, NULL, 'u' }, |
---|
257 | { "finish", required_argument, NULL, 'f' }, |
---|
258 | { 0, 0, 0, 0} }; |
---|
259 | |
---|
260 | int c, optind = 0; |
---|
261 | c = getopt_long( argc, argv, "hisv:p:u:f:", long_options, &optind ); |
---|
262 | if( c < 0 ) |
---|
263 | { |
---|
264 | break; |
---|
265 | } |
---|
266 | switch( c ) |
---|
267 | { |
---|
268 | case 'h': |
---|
269 | showHelp = 1; |
---|
270 | break; |
---|
271 | case 'i': |
---|
272 | showInfo = 1; |
---|
273 | break; |
---|
274 | case 's': |
---|
275 | showScrape = 1; |
---|
276 | break; |
---|
277 | case 'v': |
---|
278 | verboseLevel = atoi( optarg ); |
---|
279 | break; |
---|
280 | case 'p': |
---|
281 | bindPort = atoi( optarg ); |
---|
282 | break; |
---|
283 | case 'u': |
---|
284 | uploadLimit = atoi( optarg ); |
---|
285 | break; |
---|
286 | case 'f': |
---|
287 | finishCall = optarg; |
---|
288 | break; |
---|
289 | default: |
---|
290 | return 1; |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | if( optind > argc - 1 ) |
---|
295 | { |
---|
296 | return !showHelp; |
---|
297 | } |
---|
298 | |
---|
299 | torrentPath = argv[optind]; |
---|
300 | |
---|
301 | return 0; |
---|
302 | } |
---|
303 | |
---|
304 | static void sigHandler( int signal ) |
---|
305 | { |
---|
306 | switch( signal ) |
---|
307 | { |
---|
308 | case SIGINT: |
---|
309 | mustDie = 1; |
---|
310 | break; |
---|
311 | |
---|
312 | default: |
---|
313 | break; |
---|
314 | } |
---|
315 | } |
---|