home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _ff471b8573e7dfef328f3afc012f6e30 < prev    next >
Encoding:
Text File  |  2004-06-01  |  4.5 KB  |  187 lines

  1. #
  2. # $Id: mailto.pm,v 1.11 2003/10/23 19:11:33 uid39246 Exp $
  3. #
  4. # This module implements the mailto protocol.  It is just a simple
  5. # frontend to the Unix sendmail program except on MacOS, where it uses
  6. # Mail::Internet.
  7.  
  8. package LWP::Protocol::mailto;
  9.  
  10. require LWP::Protocol;
  11. require HTTP::Request;
  12. require HTTP::Response;
  13. require HTTP::Status;
  14.  
  15. use Carp;
  16. use strict;
  17. use vars qw(@ISA $SENDMAIL);
  18.  
  19. @ISA = qw(LWP::Protocol);
  20.  
  21. unless ($SENDMAIL = $ENV{SENDMAIL}) {
  22.     for my $sm (qw(/usr/sbin/sendmail
  23.            /usr/lib/sendmail
  24.            /usr/ucblib/sendmail
  25.           ))
  26.     {
  27.     if (-x $sm) {
  28.         $SENDMAIL = $sm;
  29.         last;
  30.     }
  31.     }
  32.     die "Can't find the 'sendmail' program" unless $SENDMAIL;
  33. }
  34.  
  35. sub request
  36. {
  37.     my($self, $request, $proxy, $arg, $size) = @_;
  38.  
  39.     my ($mail, $addr) if $^O eq "MacOS";
  40.     my @text = () if $^O eq "MacOS";
  41.  
  42.     # check proxy
  43.     if (defined $proxy)
  44.     {
  45.     return new HTTP::Response &HTTP::Status::RC_BAD_REQUEST,
  46.                   'You can not proxy with mail';
  47.     }
  48.  
  49.     # check method
  50.     my $method = $request->method;
  51.  
  52.     if ($method ne 'POST') {
  53.     return new HTTP::Response &HTTP::Status::RC_BAD_REQUEST,
  54.                   'Library does not allow method ' .
  55.                   "$method for 'mailto:' URLs";
  56.     }
  57.  
  58.     # check url
  59.     my $url = $request->url;
  60.  
  61.     my $scheme = $url->scheme;
  62.     if ($scheme ne 'mailto') {
  63.     return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  64.                   "LWP::file::request called for '$scheme'";
  65.     }
  66.     if ($^O eq "MacOS") {
  67.     eval {
  68.         require Mail::Internet;
  69.     };
  70.     if($@) {
  71.         return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  72.                    "You don't have MailTools installed";
  73.     }
  74.     unless ($ENV{SMTPHOSTS}) {
  75.         return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  76.                    "You don't have SMTPHOSTS defined";
  77.     }
  78.     }
  79.     else {
  80.     unless (-x $SENDMAIL) {
  81.         return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  82.                    "You don't have $SENDMAIL";
  83.     }
  84.     }
  85.     if ($^O eq "MacOS") {
  86.         $mail = Mail::Internet->new or
  87.         return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  88.         "Can't get a Mail::Internet object";
  89.     }
  90.     else {
  91.     open(SENDMAIL, "| $SENDMAIL -oi -t") or
  92.         return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  93.                    "Can't run $SENDMAIL: $!";
  94.     }
  95.     if ($^O eq "MacOS") {
  96.     $addr = $url->encoded822addr;
  97.     }
  98.     else {
  99.     $request = $request->clone;  # we modify a copy
  100.     my @h = $url->headers;  # URL headers override those in the request
  101.     while (@h) {
  102.         my $k = shift @h;
  103.         my $v = shift @h;
  104.         next unless defined $v;
  105.         if (lc($k) eq "body") {
  106.         $request->content($v);
  107.         }
  108.         else {
  109.         $request->push_header($k => $v);
  110.         }
  111.     }
  112.     }
  113.     if ($^O eq "MacOS") {
  114.     $mail->add(To => $addr);
  115.     $mail->add(split(/[:\n]/,$request->headers_as_string));
  116.     }
  117.     else {
  118.     print SENDMAIL $request->headers_as_string;
  119.     print SENDMAIL "\n";
  120.     }
  121.     my $content = $request->content;
  122.     if (defined $content) {
  123.     my $contRef = ref($content) ? $content : \$content;
  124.     if (ref($contRef) eq 'SCALAR') {
  125.         if ($^O eq "MacOS") {
  126.         @text = split("\n",$$contRef);
  127.         foreach (@text) {
  128.             $_ .= "\n";
  129.         }
  130.         }
  131.         else {
  132.         print SENDMAIL $$contRef;
  133.         }
  134.  
  135.     }
  136.     elsif (ref($contRef) eq 'CODE') {
  137.         # Callback provides data
  138.         my $d;
  139.         if ($^O eq "MacOS") {
  140.         my $stuff = "";
  141.         while (length($d = &$contRef)) {
  142.             $stuff .= $d;
  143.         }
  144.         @text = split("\n",$stuff);
  145.         foreach (@text) {
  146.             $_ .= "\n";
  147.         }
  148.         }
  149.         else {
  150.         print SENDMAIL $d;
  151.         }
  152.     }
  153.     }
  154.     if ($^O eq "MacOS") {
  155.     $mail->body(\@text);
  156.     unless ($mail->smtpsend) {
  157.         return HTTP::Response->new(&HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  158.                        "Mail::Internet->smtpsend unable to send message to <$addr>");
  159.     }
  160.     }
  161.     else {
  162.     unless (close(SENDMAIL)) {
  163.         my $err = $! ? "$!" : "Exit status $?";
  164.         return HTTP::Response->new(&HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  165.                        "$SENDMAIL: $err");
  166.     }
  167.     }
  168.  
  169.  
  170.     my $response = HTTP::Response->new(&HTTP::Status::RC_ACCEPTED,
  171.                        "Mail accepted");
  172.     $response->header('Content-Type', 'text/plain');
  173.     if ($^O eq "MacOS") {
  174.     $response->header('Server' => "Mail::Internet $Mail::Internet::VERSION");
  175.     $response->content("Message sent to <$addr>\n");
  176.     }
  177.     else {
  178.     $response->header('Server' => $SENDMAIL);
  179.     my $to = $request->header("To");
  180.     $response->content("Message sent to <$to>\n");
  181.     }
  182.  
  183.     return $response;
  184. }
  185.  
  186. 1;
  187.