#!/usr/bin/perl 
# shorlfilter by Adam Rosi-Kessel Copyright 2003, 2004
# Permission granted to modify and redistribute under the terms of the GPL v2.0 or later

use strict;

use WWW::Shorten::Shorl;
use Getopt::Long;
use Pod::Usage;

my $version = "0.5.2";

my $maximum_length = 45;     # anything longer than this length will be shorlified
my $help = 0;
my $man = 0;
my $retry = 1;
my $version_display = 0;

GetOptions(
           'length=i' => \$maximum_length, 
            'retry=i' => \$retry,
             'help|?' => \$help, 
                'man' => \$man,
            'version' => \$version_display
          );

if ($version_display) {
   print "shorlfilter $version\n";
   exit;
}
pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

my $full_input = "";
my %replacements;

while (<>) {
  $full_input .= $_;
  while (s{^(.*?)(https?://[^ <>\n\r]*)(.*)}{$1$3}i) {
    my ($pre, $url, $post) = ($1, $2, $3);
    $url =~ s/"$// if substr($pre,-1) eq '"';
    $url =~ s/'$// if substr($pre,-1) eq "'";
    $url =~ s/\)$// if substr($pre,-1) eq "(";
    if (not defined $replacements{$url} and length($url) > $maximum_length) {
      my $counter = 0;
      while (not ($replacements{$url} = makeashorterlink($url)) and $counter++ < $retry) { }  # retry query specified number of times
      delete($replacements{$url}) unless $replacements{$url};                                 # remove replacement string if query was unsuccessful
    }
  }
}

foreach (keys %replacements) {
  $full_input =~ s/\Q$_/$replacements{$_}/g;
}

print $full_input;

__END__

=head1 NAME

shorlfilter - text filter that turns long URLs into shorls (short URLs)

=head1 SYNOPSIS

shorlfilter [options] [file ...]

 Options:
   --help                brief help message
   --man                 full man page
   -v, --version         print version information and exit
   -l N, --length=N      minimum length of URL in characters to shorlify, default 45
   -r N, --retry=N       number of times to retry shorl query before failing

=head1 DESCRIPTION

Shorlfilter takes piped input or a filename from the command line and returns the same file with any long URLs turned into shorls.

See http://shorl.com for more information on shorls.

If shorlfilter is unable to get a replacement short URL from shorl.com, the long URL will be left unchanged in the output of the filter.

=head1 EXAMPLES

Convert all URLs longer than 60 characters to shorls in file_with_long_urls, save results in file_with_short_urls:

cat file_with_long_urls | shorlfilter -n 60 > file_with_short_urls

Add the following to your .muttrc to have all outgoing messages in the mutt mail user agent be shorlified:

macro compose y <filter-entry>'shorlfilter'\ry<send-message>'

Add the following to your .vimrc to have map F5 to filter the currently open document through shorlfilter:

noremap <F5> :%!shorlfilter<CR>

=head1 BUGS

Does not recognize URLs unless they start with http:// or https://. Ideally there would be better heuristics for recognizing long URLs in text even if they don't have the proper protocol string.

=head1 COPYRIGHT

Copyright (c) 2003, 2004 Adam Rosi-Kessel.
This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, to the extent permitted by law.

=head1 AUTHOR

Adam Rosi-Kessel, L<ajkessel@debian.org|mailto:ajkessel@debian.org>

=cut
