Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.3 KiB

  1. #
  2. # $Id: mailto.pm,v 1.9 1999/03/19 21:00:13 gisle 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. package LWP::Protocol::mailto;
  8. require LWP::Protocol;
  9. require HTTP::Request;
  10. require HTTP::Response;
  11. require HTTP::Status;
  12. use Carp;
  13. use strict;
  14. use vars qw(@ISA $SENDMAIL);
  15. @ISA = qw(LWP::Protocol);
  16. $SENDMAIL ||= "/usr/lib/sendmail";
  17. sub request
  18. {
  19. my($self, $request, $proxy, $arg, $size) = @_;
  20. my ($mail, $addr) if $^O eq "MacOS";
  21. my @text = () if $^O eq "MacOS";
  22. # check proxy
  23. if (defined $proxy)
  24. {
  25. return new HTTP::Response &HTTP::Status::RC_BAD_REQUEST,
  26. 'You can not proxy with mail';
  27. }
  28. # check method
  29. my $method = $request->method;
  30. if ($method ne 'POST') {
  31. return new HTTP::Response &HTTP::Status::RC_BAD_REQUEST,
  32. 'Library does not allow method ' .
  33. "$method for 'mailto:' URLs";
  34. }
  35. # check url
  36. my $url = $request->url;
  37. my $scheme = $url->scheme;
  38. if ($scheme ne 'mailto') {
  39. return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  40. "LWP::file::request called for '$scheme'";
  41. }
  42. if ($^O eq "MacOS") {
  43. eval {
  44. require Mail::Internet;
  45. };
  46. if($@) {
  47. return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  48. "You don't have MailTools installed";
  49. }
  50. unless ($ENV{SMTPHOSTS}) {
  51. return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  52. "You don't have SMTPHOSTS defined";
  53. }
  54. } else {
  55. unless (-x $SENDMAIL) {
  56. return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  57. "You don't have $SENDMAIL";
  58. }
  59. }
  60. if ($^O eq "MacOS") {
  61. $mail = Mail::Internet->new or
  62. return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  63. "Can't get a Mail::Internet object";
  64. } else {
  65. open(SENDMAIL, "| $SENDMAIL -oi -t") or
  66. return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  67. "Can't run $SENDMAIL: $!";
  68. }
  69. if ($^O eq "MacOS") {
  70. $addr = $url->encoded822addr;
  71. } else {
  72. $request = $request->clone; # we modify a copy
  73. my @h = $url->headers; # URL headers override those in the request
  74. while (@h) {
  75. my $k = shift @h;
  76. my $v = shift @h;
  77. next unless defined $v;
  78. if (lc($k) eq "body") {
  79. $request->content($v);
  80. } else {
  81. $request->push_header($k => $v);
  82. }
  83. }
  84. }
  85. if ($^O eq "MacOS") {
  86. $mail->add(To => $addr);
  87. $mail->add(split(/[:\n]/,$request->headers_as_string));
  88. } else {
  89. print SENDMAIL $request->headers_as_string;
  90. print SENDMAIL "\n";
  91. }
  92. my $content = $request->content;
  93. if (defined $content) {
  94. my $contRef = ref($content) ? $content : \$content;
  95. if (ref($contRef) eq 'SCALAR') {
  96. if ($^O eq "MacOS") {
  97. @text = split("\n",$$contRef);
  98. foreach (@text) {
  99. $_ .= "\n";
  100. }
  101. } else {
  102. print SENDMAIL $$contRef;
  103. }
  104. } elsif (ref($contRef) eq 'CODE') {
  105. # Callback provides data
  106. my $d;
  107. if ($^O eq "MacOS") {
  108. my $stuff = "";
  109. while (length($d = &$contRef)) {
  110. $stuff .= $d;
  111. }
  112. @text = split("\n",$stuff);
  113. foreach (@text) {
  114. $_ .= "\n";
  115. }
  116. } else {
  117. print SENDMAIL $d;
  118. }
  119. }
  120. }
  121. if ($^O eq "MacOS") {
  122. $mail->body(\@text);
  123. unless ($mail->smtpsend) {
  124. return HTTP::Response->new(&HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  125. "Mail::Internet->smtpsend unable to send message to <$addr>");
  126. }
  127. } else {
  128. unless (close(SENDMAIL)) {
  129. my $err = $! ? "$!" : "Exit status $?";
  130. return HTTP::Response->new(&HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  131. "$SENDMAIL: $err");
  132. }
  133. }
  134. my $response = HTTP::Response->new(&HTTP::Status::RC_ACCEPTED,
  135. "Mail accepted");
  136. $response->header('Content-Type', 'text/plain');
  137. if ($^O eq "MacOS") {
  138. $response->header('Server' => "Mail::Internet $Mail::Internet::VERSION");
  139. $response->content("Message sent to <$addr>\n");
  140. } else {
  141. $response->header('Server' => $SENDMAIL);
  142. my $to = $request->header("To");
  143. $response->content("Message sent to <$to>\n");
  144. }
  145. return $response;
  146. }
  147. 1;