File: mailto.pm

package info (click to toggle)
libwww-perl 5.36-1.1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 848 kB
  • ctags: 400
  • sloc: perl: 6,366; makefile: 51; sh: 6
file content (87 lines) | stat: -rw-r--r-- 2,129 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#
# $Id: mailto.pm,v 1.7 1996/04/09 15:44:38 aas Exp $
#
# This module implements the mailto protocol.  It is just a simple
# frontend to the Unix sendmail program.  In the long run this module
# will built using the Mail::Send module.

package LWP::Protocol::mailto;

require LWP::Protocol;
require HTTP::Request;
require HTTP::Response;
require HTTP::Status;

use Carp;

@ISA = qw(LWP::Protocol);

$SENDMAIL = "/usr/lib/sendmail";


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

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

    # check method
    $method = $request->method;

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

    # check url
    my $url = $request->url;

    my $scheme = $url->scheme;
    if ($scheme ne 'mailto') {
	return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
				  "LWP::file::request called for '$scheme'";
    }
    unless (-x $SENDMAIL) {
	return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
				  "You don't have $SENDMAIL";
    }

    open(SENDMAIL, "| $SENDMAIL -oi -t") or
	return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
				  "Can't run $SENDMAIL: $!";

    my $addr = $url->encoded822addr;

    $request->header('To', $addr);
    print SENDMAIL $request->headers_as_string;
    print SENDMAIL "\n";
    my $content = $request->content;
    if (defined $content) {
	my $contRef = ref($content) ? $content : \$content;
	if (ref($contRef) eq 'SCALAR') {
	    print SENDMAIL $$contRef;
	} elsif (ref($contRef) eq 'CODE') {
	    # Callback provides data
	    my $d;
	    while (length($d = &$contRef)) {
		print SENDMAIL $d;
	    }
	}
    }
    close(SENDMAIL);

    my $response = new HTTP::Response &HTTP::Status::RC_ACCEPTED,
				     'Mail accepted by sendmail';
    $response->header('Content-Type', 'text/plain');
    $response->content("Mail sent to <$addr>\n");

    return $response;
}

1;