Guest User

Untitled

a guest
Oct 19th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.02 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. # xseed script for TheFriscoFainter
  4. # @author leafy
  5. # <3
  6.  
  7. use Bencode qw/ bencode bdecode /;
  8. use Getopt::Long qw/ GetOptions /;
  9. use Data::Dumper;
  10. use File::Temp qw/ tempfile /;
  11. use Digest::MD5 qw/ md5_base64 /;
  12.  
  13. my $usage = '
  14. Usage: xseed.pl [options] -- filename [filename] ...
  15.  
  16. Options:
  17.  -h, --help            show this help message and exit
  18.  -e, --edit-in-place   overwrite files without prompting
  19.  -o OUTFILE, --output=OUTFILE
  20.                        specify output filename. (can not be used if multiple
  21.                        files are given)
  22.  -d PATH, --directory=PATH
  23.                        specify a directory to save output to
  24.  -p, --private         make torrent private
  25.  
  26. Written by leafy for TheFriscoFainter@baconbits
  27. ';
  28.  
  29. my %options = {
  30.     "help" => "",
  31.     "edit_in_place" => "",
  32.     "output_file" => "",
  33.     "output_dir" => "",
  34.     "private" => ""
  35. };
  36. GetOptions(
  37.     "help" => \$options{"help"},
  38.     "h" => \$options{"help"},
  39.     "e" => \$options{"edit_in_place"},
  40.     "edit-in-place" => \$options{"edit_in_place"},
  41.     "o=s" => \$options{"output_file"},
  42.     "output=s" => \$options{"output_file"},
  43.     "d=s" => \$options{"output_dir"},
  44.     "directory=s" => \$options{"output_dir"},
  45.     "p" => \$options{"private"},
  46.     "private" => \$options{"private"}
  47. );
  48.  
  49. if(1 == $options{"help"}) {
  50.     print $usage;
  51.     exit;
  52. }
  53.  
  54. foreach my $filename (@ARGV) {
  55.     open(my $h, "<", $filename); # File handle
  56.     binmode($h);
  57.    
  58.     my $file = "";
  59.     while(defined($_ = <$h>)) {
  60.         $file .= $_;
  61.     }
  62.    
  63.     my $torrent = bdecode $file;
  64.     if(not defined($torrent->{"info"}->{"files"})) {
  65.         # Single file mode - put into multiple file mode
  66.         my %file;
  67.         $file{"name"} = $torrent->{"info"}->{"name"};
  68.         $file{"length"} = $torrent->{"info"}->{"length"};
  69.         $file{"path"} = [$file{"name"}];
  70.        
  71.         if(defined($torrent->{"info"}->{"md5sum"})) {
  72.             $file{"md5sum"} = $torrent->{"info"}->{"md5sum"};
  73.         }
  74.        
  75.         $torrent->{"info"}->{"name"} = $file{"name"};
  76.         delete $torrent->{"info"}->{"length"};
  77.         delete $torrent->{"info"}->{"md5sum"};
  78.        
  79.         $torrent->{"info"}->{"files"} = [\%file];
  80.     }
  81.    
  82.     # Generate a random filename
  83.    
  84.     my %randfile;
  85.     $randfile{"name"} = md5_base64(time());
  86.     $randfile{"length"} = 0;
  87.     $randfile{"path"} = [$randfile{"name"}];
  88.    
  89.    
  90.     push(@{$torrent->{"info"}->{"files"}}, \%randfile);
  91.    
  92.     if(1 == $options{"private"}) {
  93.         $torrent->{"info"}->{"private"} = "1";
  94.     }
  95.    
  96.     # Finished editing, encode it!
  97.    
  98.     $file = bencode $torrent;
  99.    
  100.    
  101.     if(1 == $options{"edit_in_place"}) {
  102.         open $h, "+>", $filename;
  103.         binmode($h);
  104.         print $h $file;
  105.     }
  106.     else {
  107.         if(length(@ARGV) == 1) {
  108.             # Single file
  109.             if(not defined($options{"output_file"}) and not defined($options{"output_dir"})) {
  110.                 print STDERR "You didn't specify an output file or directory.";
  111.                 exit;
  112.             }
  113.             else {
  114.                 if(defined($options{"output_file"})) {
  115.                     open $h, "+>", $options{"output_file"};
  116.                 }
  117.                 else {
  118.                     open $h, "+>", ($options{"output_dir"} . "/" . $filename);
  119.                 }
  120.             }
  121.             binmode($h);
  122.             print $h $file;
  123.         }
  124.     }
  125.     close $h;
  126. }
Add Comment
Please, Sign In to add comment