#!/usr/local/bin/perl -w
# Created: Sun Feb  4 16:46:09 2001
# Last modified: Mon Dec 17 10:01:18 2001
# Time-stamp: <01/12/17 10:01:18 nevin>

# $Id: spamcop.pl,v 1.11 2001/12/17 15:02:01 nevin Exp $

# Your code is what follows http://spamcop.net/?code= in the URL
# provided to you by spamcop.
my $CODE = $ENV{SPAMCOP_CODE};

die "You need to define the SPAMCOP_CODE environment variable.\n" unless
  defined $CODE;

use strict;
require LWP::UserAgent;
use HTTP::Request::Common;
require HTTP::Response;

my $URI = 'http://spamcop.net/sc';
my $message;
my $nmode = 0;
my $kmode = 0;
my $mmode = 0;
my $gmode = 0;


$nmode = 1 if grep {$_ eq '-n'} @ARGV;
$kmode = 1 if grep {$_ eq '-k'} @ARGV;
$mmode = 1 if grep {$_ eq '-m'} @ARGV;
$gmode = 1 if grep {$_ eq '-g'} @ARGV;


{
  local $/ = undef;
  $message = <STDIN>;
}

my $ua = LWP::UserAgent->new;

print "Connecting to $URI.\n";
my $response = $ua->request(POST $URI,
			    [code   => $CODE,
			     action => 'submit',
			     oldverbose => '',
			     spam   => $message,
 			     verbose =>
 			     'Process spam and show technical data']);

if ($response->is_success){
  print "I don't know how to deal with this :(\n";
  print $response->content;
  print "\n";
}
elsif ($response->is_redirect){
  print "Success!\n";
  my $location = $response->header('Location');
  if ($kmode){
    ## Use Konqueror
    system ("kfmclient exec '$location'") &&
      warn "Something wrong: $?\n";
  }
  elsif ($nmode){
    ## Use Netscape
    system ("netscape -remote 'openURL($location)'")
      && warn "Something wrong: $?\n";
  }
  elsif ($mmode){
    ## Use mozilla
    system ("mozilla -remote 'openURL($location)'");
  }
  elsif ($gmode){
    ## Use galeon
    system ("galeon -n $location");
  }
  else{
    print "Please follow\n$location\n";
  }
}
else{
  print "Failed.\n";
  print $response->error_as_HTML();
}


__END__

=head1 NAME

spamcop - Client to SpamCop [1].


=head1 SYNOPSIS

spamcop < junk_mail

spamcop -n < junk_mail

spamcop -k < junk_mail

spamcop -m < junk_mail

spamcop -g < junk_mail


=head1 DESCRIPTION

Without no arguments, spamcop will send the STDIN to the SpamCop
reporting page and print the relavant link to STDOUT. With the -k, -n,
-g, and -m options, the link will be opened in Konqueror, Netscape, Galeon
or Mozilla respectively.

You will need to set the environment variable SPAMCOP_CODE. This is
the code that is provided to you by Spamcop. It's what follows
http://spamcop.net/?code= in the URL provided to you by spamcop.

=head1 REFERENCES

[1] http://www.spamcop.net

=head1 AUTHOR

Nevin Kapur <nevin@jhu.edu>

=cut

