#!/usr/local/bin/perl -w use strict; use FileHandle; if (@ARGV == 0 || $ARGV[0] eq '--help') { die "Usage: enmime ...\n"; } my %Reverse_Mime; # See RFC-2045. @Reverse_Mime{'A'..'Z', 'a'..'z', 0..9, '+', '/'} = map{scalar(reverse(unpack('b6', chr($_))));} (0..63); my %Mime_table; @Mime_table{values(%Reverse_Mime)} = keys(%Reverse_Mime); my %Filehandles; my @Files = @ARGV; chdir or die "Could not chdir to home directory: $!"; foreach my $File (@Files) { unless ($Filehandles{$File} = new FileHandle($File)) { if ($File =~ s/(.*?):(.*)/$2/) { $Filehandles{$File} = new FileHandle("ssh $1 cat $2 |") or die "Could not open $File for read: $!"; } } } my $Output_line_length = 60; # Not counting newline. my $FileID = int(rand(32768)); my $Boundary = '-3263827-THX1138-NCC1701J-040870=:' . $FileID; my @Timestamp = (localtime)[5,4,3,2,1,0]; # We have liftoff. $Timestamp[1]+= 1; my $Timestamp = sprintf("%d%02d%02d%02d%02d%02d", @Timestamp); my $Hostname = `hostname`; chomp($Hostname); {my $LocalFileID = 'A'; sub File_header { my $Filename = $_[0]; $Filename =~ s!.*/!!; $LocalFileID++; return < Content-Description: HEADER }} {print <getline)) { my $Offset = 0; if ($Excess ne '') { $Input_line = $Excess . $Input_line; $Excess = ''; } my $Length = length($Input_line); ENCODE: while ($Offset < $Length) { my $Block = substr($Input_line, $Offset, 3); if (length($Block) < 3) { $Excess = $Block; last ENCODE; } my $Bits = unpack("B24", $Block); my @Six_bit_blocks = map{substr($Bits,$_*6,6)}(0..3); $Output_line .= join('', map{$Mime_table{$_}} @Six_bit_blocks); if (length($Output_line) == $Output_line_length) { print "$Output_line\n"; $Output_line = ''; } $Offset += 3; } } if ($Excess ne '') { my $Pad = 3 - length($Excess); # 1 to 2 and vice versa; my $Block = $Excess . "\0" x $Pad; # length cannot be 3 or 0. my $Bits = unpack("B24", $Block); my @Six_bit_blocks = map{substr($Bits,$_*6,6)}(0..(3-$Pad)); $Output_line .= join('', (map{$Mime_table{$_}} @Six_bit_blocks), '=' x $Pad); } if (length($Output_line) > 0) { print "$Output_line\n"; } $Filehandles{$File}->close or warn "Error closing file: $!"; } print "--$Boundary--\n";