#####
# web2mht v.1.1.0.0
# by Xen 2006
# Lambda Core Research Facility
# GNU GPL v.3
#####

use strict;
use Getopt::Long;
use Pod::Usage;
use Win32::OLE;
use URI::Split qw(uri_split);
use Tk;
use Win32::Clipboard;

#####
# down2met(URL, filename) - save web page to file in MHTML format
#####
sub down2mht {
    my($address, $file) = @_;
    
    my($iMsg) = Win32::OLE->new('CDO.Message') or die "No CDO.Message\n";
    my($iConf) = Win32::OLE->new('CDO.Configuration') or die "No CDO.Configuration\n";
    my($iStream);
  
    $iMsg->{Configuration} = $iConf->{Fields};
    $iMsg->CreateMHTMLBody($address);
    $iStream = $iMsg->GetStream;
    $iStream->SaveToFile($file, 1);
};

#####
# makename(URL) => namefromURL.mht
#####
sub makename {
    my($URL) = @_;
    my($name) = "";
    
    my($scheme, $auth, $path, $query, $frag) = uri_split($URL);
    if ($path =~ /^$/) {
        if ($query =~ /^$/) {
            $name = $auth;
        } else {
            $name = $auth.".".$query;
        }
    } else {
        $path =~ /^\/(?:(?:[^\/]*)\/)*?([^\/]*)\/?$/;
        my($path1) = $1;
        if ($query =~ /^$/) {
            $name = $auth.".".$path1;
        } else {
            $name = $auth.".".$path1.".".$query;
        }
    }        
    $name =~ s/[\\|\/|\?|\|\&|:]/\./g;
    $name =~ s/^\.(.*)/$1/g;
    $name =~ s/(.*?)\.{1,}$/$1/g;
    $name =~ s/^www\.(.*)/$1/g;
    $name =~ s/(.*?)\.{2,}(.*?)/$1\.$2/g;
    $name = $name.".mht";
    return $name;
    
}
;

#####
# repairname(filename) => filename without unallowed chars
#####
sub repairname {
    my($name) = @_;
    $name =~ s/[\\|\/|\?|\|\&|:]/\./g;
    $name =~ s/^\.(.*)/$1/g;
    $name =~ s/(.*?)\.{1,}/$1/g;
    $name =~ s/(.*?)\.{2,}(.*?)/$1\.$2/g;
    return $name;
}
;

#####
#download(URL, directory, filenme);
#####
sub download {
    my($URL, $directory, $name) = @_;
    my($file) = $directory."/".repairname($name).".mht";
    down2mht($URL, $file);
    exit (0);
}

sub down2mhtDialog {
    my($URL, $directory) = @_;
    my($name) = "";
    my($top) = MainWindow->new();
    
    $top->title("web2mht");
    my($label) = $top->Label(text => 'Enter file name:');
    my($entry) =  $top->Entry(textvariable => \$name, width => 40);
    my($pastebutton) = $top->Button(text => 'Paste',
                                    command => sub {$name = Win32::Clipboard()->Get()});
    my($button) = $top->Button(text => 'Save',
                               command => sub { download($URL, $directory, $name)});
    $label->pack();
    $entry->pack();
    $pastebutton->pack();
    $button->pack();
    MainLoop();
}
;

#BEGIN

my($file) = '';
my($directory) = '';
my($URL) = 'http://lc.gyvv.sk';
my($askname) = 0;

pod2usage(-exitstatus => 0, -verbose => 2) if ($#ARGV == -1);

if ($#ARGV == 0) {
    $URL=$ARGV[0];
} else {
    GetOptions ('output|o=s' => \$file,
                'url=s' => \$URL,
                'dir|d=s' => \$directory,
                'ask|a' => \$askname);
}

if ($file =~ /^$/) { $file = makename($URL); }
if ($directory !~ /^$/) { $file = $directory.'/'.$file; }

if ($askname == 1) {down2mhtDialog($URL, $directory); } else {down2mht($URL, $file);}

#END


__END__

=head1 NAME
  
web2mht.pl - Save web page as MHTML file
    
=head1 SYNOPSIS
    
web2mht.pl [options]
    
    Options:
        --a|ask      filename dialog
        --url        page URL
        --d|dir      diretory
        --o|output   file to save page

or web2mht.pl URL
    
=head1 OPTIONS
    
=over 8

=item B<--a|ask>
    
    Ask for filename. Unallowed characters are replaced with ".".
   
=item B<--dir|d>
    
    Directory to save web page. Format: c:/dir/to/save

=item B<--output|o>
    
    Name of file to save page.

=item B<--URL>
    
    Web page URL.
    
=back
    
=head1 DESCRIPTION
    
B<web2mht.pl> will save the given page to file in MHTML format.

=head1 SEE ALSO

B<Opera> install: (add web2mht.pl to PATH)

    Add these lines to standard_menu.ini [Links Panel Item Menu]

        Platform Windows, Item, "Save as MHT" 	= "Copy link,,,,"IE" & Execute program, "web2mht.pl","--url %c --d c:\\temp""
        Platform Windows, Item, "Save as MHT as..." 	= "Copy link,,,,"IE" & Execute program, "web2mht.pl","--url %c --a --d c:\\temp""

    Add these lines to active toolbar.ini [Browser Toolbar.content]
    
        Button98, "MHT"="Execute program, "web2mht.pl", "--url %u --d c:\\temp", "MHT", "MHT""
        Button99, "MHT..."="Execute program, "web2mht.pl", "--url %u --a --d c:\\temp", "MHT...", "MHT""

=cut

