Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- # xseed script for TheFriscoFainter
- # @author leafy
- # <3
- use Bencode qw/ bencode bdecode /;
- use Getopt::Long qw/ GetOptions /;
- use Data::Dumper;
- use File::Temp qw/ tempfile /;
- use Digest::MD5 qw/ md5_base64 /;
- my $usage = '
- Usage: xseed.pl [options] -- filename [filename] ...
- Options:
- -h, --help show this help message and exit
- -e, --edit-in-place overwrite files without prompting
- -o OUTFILE, --output=OUTFILE
- specify output filename. (can not be used if multiple
- files are given)
- -d PATH, --directory=PATH
- specify a directory to save output to
- -p, --private make torrent private
- Written by leafy for TheFriscoFainter@baconbits
- ';
- my %options = {
- "help" => "",
- "edit_in_place" => "",
- "output_file" => "",
- "output_dir" => "",
- "private" => ""
- };
- GetOptions(
- "help" => \$options{"help"},
- "h" => \$options{"help"},
- "e" => \$options{"edit_in_place"},
- "edit-in-place" => \$options{"edit_in_place"},
- "o=s" => \$options{"output_file"},
- "output=s" => \$options{"output_file"},
- "d=s" => \$options{"output_dir"},
- "directory=s" => \$options{"output_dir"},
- "p" => \$options{"private"},
- "private" => \$options{"private"}
- );
- if(1 == $options{"help"}) {
- print $usage;
- exit;
- }
- foreach my $filename (@ARGV) {
- open(my $h, "<", $filename); # File handle
- binmode($h);
- my $file = "";
- while(defined($_ = <$h>)) {
- $file .= $_;
- }
- my $torrent = bdecode $file;
- if(not defined($torrent->{"info"}->{"files"})) {
- # Single file mode - put into multiple file mode
- my %file;
- $file{"name"} = $torrent->{"info"}->{"name"};
- $file{"length"} = $torrent->{"info"}->{"length"};
- $file{"path"} = [$file{"name"}];
- if(defined($torrent->{"info"}->{"md5sum"})) {
- $file{"md5sum"} = $torrent->{"info"}->{"md5sum"};
- }
- $torrent->{"info"}->{"name"} = $file{"name"};
- delete $torrent->{"info"}->{"length"};
- delete $torrent->{"info"}->{"md5sum"};
- $torrent->{"info"}->{"files"} = [\%file];
- }
- # Generate a random filename
- my %randfile;
- $randfile{"name"} = md5_base64(time());
- $randfile{"length"} = 0;
- $randfile{"path"} = [$randfile{"name"}];
- push(@{$torrent->{"info"}->{"files"}}, \%randfile);
- if(1 == $options{"private"}) {
- $torrent->{"info"}->{"private"} = "1";
- }
- # Finished editing, encode it!
- $file = bencode $torrent;
- if(1 == $options{"edit_in_place"}) {
- open $h, "+>", $filename;
- binmode($h);
- print $h $file;
- }
- else {
- if(length(@ARGV) == 1) {
- # Single file
- if(not defined($options{"output_file"}) and not defined($options{"output_dir"})) {
- print STDERR "You didn't specify an output file or directory.";
- exit;
- }
- else {
- if(defined($options{"output_file"})) {
- open $h, "+>", $options{"output_file"};
- }
- else {
- open $h, "+>", ($options{"output_dir"} . "/" . $filename);
- }
- }
- binmode($h);
- print $h $file;
- }
- }
- close $h;
- }
Add Comment
Please, Sign In to add comment