<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package LWP::Protocol::mailto;

# This module implements the mailto protocol.  It is just a simple
# frontend to the Unix sendmail program except on MacOS, where it uses
# Mail::Internet.

require HTTP::Request;
require HTTP::Response;
require HTTP::Status;

use Carp;
use strict;

our $VERSION = '6.77';

use parent qw(LWP::Protocol);
our $SENDMAIL;

unless ($SENDMAIL = $ENV{SENDMAIL}) {
    for my $sm (qw(/usr/sbin/sendmail
		   /usr/lib/sendmail
		   /usr/ucblib/sendmail
		  ))
    {
	if (-x $sm) {
	    $SENDMAIL = $sm;
	    last;
	}
    }
    die "Can't find the 'sendmail' program" unless $SENDMAIL;
}

sub request
{
    my($self, $request, $proxy, $arg, $size) = @_;

    my ($mail, $addr) if $^O eq "MacOS";
    my @text = () if $^O eq "MacOS";

    # check proxy
    if (defined $proxy)
    {
	return HTTP::Response-&gt;new(HTTP::Status::RC_BAD_REQUEST,
				  'You can not proxy with mail');
    }

    # check method
    my $method = $request-&gt;method;

    if ($method ne 'POST') {
	return HTTP::Response-&gt;new( HTTP::Status::RC_BAD_REQUEST,
				  'Library does not allow method ' .
				  "$method for 'mailto:' URLs");
    }

    # check url
    my $url = $request-&gt;uri;

    my $scheme = $url-&gt;scheme;
    if ($scheme ne 'mailto') {
	return HTTP::Response-&gt;new( HTTP::Status::RC_INTERNAL_SERVER_ERROR,
			 "LWP::Protocol::mailto::request called for '$scheme'");
    }
    if ($^O eq "MacOS") {
	eval {
	    require Mail::Internet;
	};
	if($@) {
	    return HTTP::Response-&gt;new( HTTP::Status::RC_INTERNAL_SERVER_ERROR,
	               "You don't have MailTools installed");
	}
	unless ($ENV{SMTPHOSTS}) {
	    return HTTP::Response-&gt;new( HTTP::Status::RC_INTERNAL_SERVER_ERROR,
	               "You don't have SMTPHOSTS defined");
	}
    }
    else {
	unless (-x $SENDMAIL) {
	    return HTTP::Response-&gt;new( HTTP::Status::RC_INTERNAL_SERVER_ERROR,
	               "You don't have $SENDMAIL");
    }
    }
    if ($^O eq "MacOS") {
	    $mail = Mail::Internet-&gt;new or
	    return HTTP::Response-&gt;new( HTTP::Status::RC_INTERNAL_SERVER_ERROR,
	    "Can't get a Mail::Internet object");
    }
    else {
	open(SENDMAIL, "| $SENDMAIL -oi -t") or
	    return HTTP::Response-&gt;new( HTTP::Status::RC_INTERNAL_SERVER_ERROR,
	               "Can't run $SENDMAIL: $!");
    }
    if ($^O eq "MacOS") {
	$addr = $url-&gt;encoded822addr;
    }
    else {
	$request = $request-&gt;clone;  # we modify a copy
	my @h = $url-&gt;headers;  # URL headers override those in the request
	while (@h) {
	    my $k = shift @h;
	    my $v = shift @h;
	    next unless defined $v;
	    if (lc($k) eq "body") {
		$request-&gt;content($v);
	    }
	    else {
		$request-&gt;push_header($k =&gt; $v);
	    }
	}
    }
    if ($^O eq "MacOS") {
	$mail-&gt;add(To =&gt; $addr);
	$mail-&gt;add(split(/[:\n]/,$request-&gt;headers_as_string));
    }
    else {
	print SENDMAIL $request-&gt;headers_as_string;
	print SENDMAIL "\n";
    }
    my $content = $request-&gt;content;
    if (defined $content) {
	my $contRef = ref($content) ? $content : \$content;
	if (ref($contRef) eq 'SCALAR') {
	    if ($^O eq "MacOS") {
		@text = split("\n",$$contRef);
		foreach (@text) {
		    $_ .= "\n";
		}
	    }
	    else {
	    print SENDMAIL $$contRef;
	    }

	}
	elsif (ref($contRef) eq 'CODE') {
	    # Callback provides data
	    my $d;
	    if ($^O eq "MacOS") {
		my $stuff = "";
		while (length($d = &amp;$contRef)) {
		    $stuff .= $d;
		}
		@text = split("\n",$stuff);
		foreach (@text) {
		    $_ .= "\n";
		}
	    }
	    else {
		print SENDMAIL $d;
	    }
	}
    }
    if ($^O eq "MacOS") {
	$mail-&gt;body(\@text);
	unless ($mail-&gt;smtpsend) {
	    return HTTP::Response-&gt;new(HTTP::Status::RC_INTERNAL_SERVER_ERROR,
				       "Mail::Internet-&gt;smtpsend unable to send message to &lt;$addr&gt;");
	}
    }
    else {
	unless (close(SENDMAIL)) {
	    my $err = $! ? "$!" : "Exit status $?";
	    return HTTP::Response-&gt;new(HTTP::Status::RC_INTERNAL_SERVER_ERROR,
				       "$SENDMAIL: $err");
	}
    }


    my $response = HTTP::Response-&gt;new(HTTP::Status::RC_ACCEPTED,
				       "Mail accepted");
    $response-&gt;header('Content-Type', 'text/plain');
    if ($^O eq "MacOS") {
	$response-&gt;header('Server' =&gt; "Mail::Internet $Mail::Internet::VERSION");
	$response-&gt;content("Message sent to &lt;$addr&gt;\n");
    }
    else {
	$response-&gt;header('Server' =&gt; $SENDMAIL);
	my $to = $request-&gt;header("To");
	$response-&gt;content("Message sent to &lt;$to&gt;\n");
    }

    return $response;
}

1;
</pre></body></html>