Revert "transmission: update from 2.13 to 2.22"
[tomato.git] / release / src / router / transmission / utils / edit.c
blob1fa5f1985dad4e2b82eaa826bd91f3d1e307a50d
1 /*
2 * This file Copyright (C) 2010 Mnemosyne LLC
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
8 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10 * $Id: edit.c 11160 2010-08-08 22:53:24Z charles $
13 #include <stdio.h>
15 #include <event.h> /* evbuffer */
17 #include <libtransmission/transmission.h>
18 #include <libtransmission/bencode.h>
19 #include <libtransmission/tr-getopt.h>
20 #include <libtransmission/utils.h>
21 #include <libtransmission/version.h>
23 #define MY_NAME "transmission-edit"
25 static int fileCount = 0;
26 static tr_bool showVersion = FALSE;
27 static const char ** files = NULL;
28 static const char * add = NULL;
29 static const char * deleteme = NULL;
30 static const char * replace[2] = { NULL, NULL };
32 static tr_option options[] =
34 { 'a', "add", "Add a tracker's announce URL", "a", 1, "<url>" },
35 { 'd', "delete", "Delete a tracker's announce URL", "d", 1, "<url>" },
36 { 'r', "replace", "Search and replace a substring in the announce URLs", "r", 1, "<old> <new>" },
37 { 'V', "version", "Show version number and exit", "V", 0, NULL },
38 { 0, NULL, NULL, NULL, 0, NULL }
41 static const char *
42 getUsage( void )
44 return "Usage: " MY_NAME " [options] torrent-file(s)";
47 static int
48 parseCommandLine( int argc, const char ** argv )
50 int c;
51 const char * optarg;
53 while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg )))
55 switch( c )
57 case 'a': add = optarg;
58 break;
59 case 'd': deleteme = optarg;
60 break;
61 case 'r': replace[0] = optarg;
62 c = tr_getopt( getUsage( ), argc, argv, options, &optarg );
63 if( c != TR_OPT_UNK ) return 1;
64 replace[1] = optarg;
65 break;
66 case 'V': showVersion = TRUE;
67 break;
68 case TR_OPT_UNK: files[fileCount++] = optarg; break;
69 default: return 1;
73 return 0;
76 static tr_bool
77 removeURL( tr_benc * metainfo, const char * url )
79 const char * str;
80 tr_benc * announce_list;
81 tr_bool changed = FALSE;
83 if( tr_bencDictFindStr( metainfo, "announce", &str ) && !strcmp( str, url ) )
85 printf( "\tRemoved \"%s\" from \"announce\"\n", str );
86 tr_bencDictRemove( metainfo, "announce" );
87 changed = TRUE;
90 if( tr_bencDictFindList( metainfo, "announce-list", &announce_list ) )
92 tr_benc * tier;
93 int tierIndex = 0;
94 while(( tier = tr_bencListChild( announce_list, tierIndex )))
96 tr_benc * node;
97 int nodeIndex = 0;
98 while(( node = tr_bencListChild( tier, nodeIndex )))
100 if( tr_bencGetStr( node, &str ) && !strcmp( str, url ) )
102 printf( "\tRemoved \"%s\" from \"announce-list\" tier #%d\n", str, (tierIndex+1) );
103 tr_bencListRemove( tier, nodeIndex );
104 changed = TRUE;
106 else ++nodeIndex;
109 if( tr_bencListSize( tier ) == 0 )
111 printf( "\tNo URLs left in tier #%d... removing tier\n", (tierIndex+1) );
112 tr_bencListRemove( announce_list, tierIndex );
114 else ++tierIndex;
117 if( tr_bencListSize( announce_list ) == 0 )
119 printf( "\tNo tiers left... removing announce-list\n" );
120 tr_bencDictRemove( metainfo, "announce-list" );
124 /* if we removed the "announce" field and there's still another track left,
125 * use it as the "announce" field */
126 if( changed && !tr_bencDictFindStr( metainfo, "announce", &str ) )
128 tr_benc * tier;
129 tr_benc * node;
131 if(( tier = tr_bencListChild( announce_list, 0 ))) {
132 if(( node = tr_bencListChild( tier, 0 ))) {
133 if( tr_bencGetStr( node, &str ) ) {
134 tr_bencDictAddStr( metainfo, "announce", str );
135 printf( "\tAdded \"%s\" to announce\n", str );
142 return changed;
145 static char*
146 replaceSubstr( const char * str, const char * in, const char * out )
148 char * walk;
149 struct evbuffer * buf = evbuffer_new( );
150 const size_t inlen = strlen( in );
151 const size_t outlen = strlen( out );
153 while(( walk = strstr( str, in )))
155 evbuffer_add( buf, str, walk-str );
156 evbuffer_add( buf, out, outlen );
157 str = walk + inlen;
160 walk = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
161 evbuffer_free( buf );
162 return walk;
165 static tr_bool
166 replaceURL( tr_benc * metainfo, const char * in, const char * out )
168 const char * str;
169 tr_benc * announce_list;
170 tr_bool changed = FALSE;
172 if( tr_bencDictFindStr( metainfo, "announce", &str ) && strstr( str, in ) )
174 char * newstr = replaceSubstr( str, in, out );
175 printf( "\tReplaced in \"announce\": \"%s\" --> \"%s\"\n", str, newstr );
176 tr_bencDictAddStr( metainfo, "announce", newstr );
177 tr_free( newstr );
178 changed = TRUE;
181 if( tr_bencDictFindList( metainfo, "announce-list", &announce_list ) )
183 tr_benc * tier;
184 int tierCount = 0;
185 while(( tier = tr_bencListChild( announce_list, tierCount++ )))
187 tr_benc * node;
188 int nodeCount = 0;
189 while(( node = tr_bencListChild( tier, nodeCount++ )))
191 if( tr_bencGetStr( node, &str ) && strstr( str, in ) )
193 char * newstr = replaceSubstr( str, in, out );
194 printf( "\tReplaced in \"announce-list\" tier %d: \"%s\" --> \"%s\"\n", tierCount, str, newstr );
195 tr_bencFree( node );
196 tr_bencInitStr( node, newstr, -1 );
197 tr_free( newstr );
198 changed = TRUE;
204 return changed;
207 static tr_bool
208 addURL( tr_benc * metainfo, const char * url )
210 const char * str;
211 tr_benc * announce_list;
212 tr_bool changed = FALSE;
213 tr_bool match = FALSE;
215 /* maybe add it to "announce" */
216 if( !tr_bencDictFindStr( metainfo, "announce", &str ) )
218 printf( "\tAdded \"%s\" in \"announce\"\n", url );
219 tr_bencDictAddStr( metainfo, "announce", url );
220 changed = TRUE;
223 /* see if it's already in announce-list */
224 if( tr_bencDictFindList( metainfo, "announce-list", &announce_list ) ) {
225 tr_benc * tier;
226 int tierCount = 0;
227 while(( tier = tr_bencListChild( announce_list, tierCount++ ))) {
228 tr_benc * node;
229 int nodeCount = 0;
230 while(( node = tr_bencListChild( tier, nodeCount++ )))
231 if( tr_bencGetStr( node, &str ) && !strcmp( str, url ) )
232 match = TRUE;
236 /* if it's not in announce-list, add it now */
237 if( !match )
239 tr_benc * tier;
241 if( !tr_bencDictFindList( metainfo, "announce-list", &announce_list ) )
242 announce_list = tr_bencDictAddList( metainfo, "announce-list", 1 );
244 tier = tr_bencListAddList( announce_list, 1 );
245 tr_bencListAddStr( tier, url );
246 printf( "\tAdded \"%s\" to \"announce-list\" tier %zu\n", url, tr_bencListSize( announce_list ) );
247 changed = TRUE;
250 return changed;
254 main( int argc, char * argv[] )
256 int i;
257 int changedCount = 0;
259 files = tr_new0( const char*, argc );
261 tr_setMessageLevel( TR_MSG_ERR );
263 if( parseCommandLine( argc, (const char**)argv ) )
264 return EXIT_FAILURE;
266 if( showVersion )
268 fprintf( stderr, MY_NAME" "LONG_VERSION_STRING"\n" );
269 return 0;
272 if( fileCount < 1 )
274 fprintf( stderr, "ERROR: No torrent files specified.\n" );
275 tr_getopt_usage( MY_NAME, getUsage( ), options );
276 fprintf( stderr, "\n" );
277 return EXIT_FAILURE;
280 if( !add && !deleteme && !replace[0] )
282 fprintf( stderr, "ERROR: Must specify -a, -d or -r\n" );
283 tr_getopt_usage( MY_NAME, getUsage( ), options );
284 fprintf( stderr, "\n" );
285 return EXIT_FAILURE;
288 for( i=0; i<fileCount; ++i )
290 tr_benc top;
291 tr_bool changed = FALSE;
292 const char * filename = files[i];
294 printf( "%s\n", filename );
296 if( tr_bencLoadFile( &top, TR_FMT_BENC, filename ) )
298 printf( "\tError reading file\n" );
299 continue;
302 if( deleteme != NULL )
303 changed |= removeURL( &top, deleteme );
305 if( add != NULL )
306 changed = addURL( &top, add );
308 if( replace[0] && replace[1] )
309 changed |= replaceURL( &top, replace[0], replace[1] );
311 if( changed )
313 ++changedCount;
314 tr_bencToFile( &top, TR_FMT_BENC, filename );
317 tr_bencFree( &top );
320 printf( "Changed %d files\n", changedCount );
322 tr_free( files );
323 return 0;