#!/usr/bin/perl
##############################################################################
=pod

=head1 NAME

webpaste

=head1 DESCRIPTION

webPaste is a small script that acts as front-end to the paste bot family
http://sourceforge.net/projects/pastebot/. Those bots are an external
alternative to paste content from a user to a public place without clobbing
and annoying a lot of people in irc channels. This bot provides a web
interface to paste the data someone wants to publish.

This interface is neat if you have small amounts of data to paste, as soon as
it goes to extracts of logfiles or code-data, it can become quite painful to
paste screenpages over screenpages to a webformular.

This is where this script comes in. For data which is allready in file form,
there is no need to transfer it into a X buffer and from there into a web
formulary:

Call the script, it takes a filename as argument and pastes it to the paste
bot.

=head1 COPYRIGHT and LICENCE

 Copyright (c) 2003 Bruno Boettcher

 webPaste.pl is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


=cut

##############################################################################
use warnings;
use strict;
use Data::Dumper;

# sub POE::Kernel::ASSERT_DEFAULT () { 1 }
# sub POE::Kernel::ASSERT_EVENTS () { 1 }
# sub POE::Kernel::ASSERT_STATES () { 1 }
use HTTP::Request::Common qw(GET POST);
use POE::Component::Client::HTTP;
use POSIX qw(strftime);

use POE;
use POE::Session;

use HTML::TreeBuilder;
use HTTP::Cookies;
use HTML::Entities;

#beware the URL below is the URL that normally shows the result, not the one
#that proposes the formulary!!
my $pasteBot = 'http://nopaste.snit.ch:8000/paste';
#my $channel = '#poe';
my $channel = '';
my $nick = 'someuser';

my $VERSION = 0.2;
my $pastePage = "paste";


POE::Component::Client::HTTP->spawn( Alias => 'ua' , Agent     => 'Mozilla/4.0
    (compatible; MSIE 5.5; Windows NT 5.0)');

POE::Session->create(
    inline_states =>

    {
    _start => \&handler_start,
    finish => \&handler_end,
    }
);

$poe_kernel->run();


##############################################################################
=pod

=head1 FUNCTIONS OF THIS SCRIPT

=over 

=item handler_start

take the first argument given along on the command line, open that file, read
it in and send it to the paste bot

=cut

######################################################################
sub handler_start
{
  my $summary;
  my $file = shift(@ARGV);
  while($file =~ /^-/)
  {
    if($file =~ /-s/i)
    {
      $summary = shift(@ARGV);
      print("adding summary $summary\n");
      $file = shift(@ARGV);
    }# if($file =~ /-s/i)
    elsif($file =~ /-c/i)
    {
      $channel = shift(@ARGV);
      print("adding channel $channel\n");
      $file = shift(@ARGV);
    }# if($file =~ /-s/i)
    elsif($file =~ /-n/i)
    {
      $nick = shift(@ARGV);
      print("adding nick $nick\n");
      $file = shift(@ARGV);
    }# if($file =~ /-s/i)
    elsif($file =~ /-u/i)
    {
      $pasteBot = shift(@ARGV);
      print("adding URL $pasteBot\n");
      $file = shift(@ARGV);
    }# if($file =~ /-s/i)
  }# while($file =~ /^-/)

  $pasteBot .= '/' if(!($pasteBot =~ /\/$/));
  $pasteBot .= $pastePage;

  if(open(FILE,$file))
  {
    my @lines = <FILE>;
    my $text = join('',@lines);

    #$text = encode_entities($text);

    my $request = POST($pasteBot,
	[ paste => "$text",
	"channel" => $channel,
	"summary" => $summary,
	"nick"	=> $nick,
	]);

    $_[KERNEL]->post( "ua" => "request", "finish", $request);
  }# if(open(FILE,$file))
  else
  {
    die("usage: ./webPaste.pl [-n nick|-s summary|-c channel|-u url] filetopaste\n");
  }# else
}
##############################################################################
=pod

=item handler_end

The http request completed, read in the incoming webpage, transform it to text and extract the URL reference to the text...

=cut

######################################################################
sub handler_end
{
  my ( $this,$heap, $request_packet, $response_packet,$kernel ) = @_[ OBJECT, HEAP, ARG0, ARG1, KERNEL ];

  my $http_request  = $request_packet->[0];
  my $http_response = $response_packet->[0];
  my $answertree = HTML::TreeBuilder->new;
  $answertree->parse($http_response->content());   # !
    $answertree->eof();
  my $msg .= $answertree->as_text();
  $answertree->delete; # clear memory!
    $msg .= "  with  ".$http_response->{"_msg"} if($http_response->{"_msg"});
  $msg .= " for ".$http_request->uri() if($http_request->uri());
  $msg .= "\n";

  $msg =~ /This content is stored as (http:\/\/.*?\/\d+)./;
  my $link = $1;


  #print($msg."\nURL  $link\n");
  print("URL  $link\n");

  print("end pasting\n");
}#sub handler_end

__END__


=back

=head1 INSTALLATION

Just fetch the script from http://bboett.free.fr/webPaste.pl . Check the
perl-path at the beginning (this line: #!/usr/bin/perl), as the data section
at the top of the file (default nickname, server and  channel). After making
the file excutable (chmod +x webPaste.pl) you are ready to use it. 

You should place it somewhere the shell might find it, the rest of the help
supposes you have it in the local directory (thus ./) but it is suggested to
put it into ~/bin or /usr/local/bin.

=head1 USAGE

To invoque the script:

./webPaste.pl someFile

This script accepts some command line arguments, those
have to come before the filename:

  -n nick    to change the defautl nickname used by the script
  -s summary a summary describing the data that will be pasted
  -c channel a channel to post the URL to the pasted data
  -u URL the url of the paste bot

example:

./webPaste.pl -c '#perlhelp' -n "anonymouscoward" -s 'error log' -u http://nopaste.snit.ch:8000 core

You can preset the nickname, channel, pastebot-URL at the beginning of the
script.

If the channel given wasn't valid the script returns the URL where you can
view the pasted data, otherwise the URL is send to the selected channel.

=head1 AUTHOR

Bruno Böttcher <bboett at adlp dot org>


=head1 SEE ALSO

http://poe.perl.org/

=cut

