Mail::Qmail::Queue::Receive::Body - Receive message body when emulating qmail-queue


Mail-Qmail-Queue documentation Contained in the Mail-Qmail-Queue distribution.

Index


Code Index:

NAME

Top

Mail::Qmail::Queue::Receive::Body - Receive message body when emulating qmail-queue

SYNOPSIS

Top

  use Mail::Qmail::Queue::Receive::Body;

  my $qq_body = Mail::Qmail::Queue::Receive::Body->new
    or die "Couldn't get qmail-queue body\n"

  print "Message body: ",$qq_body->body,"\n";

  my $fh = $qq_body->body_fh
    or die "Error getting body handle: $!\n";
  while (<$fh>) {
    s/perl/Pathologically Eclectic Rubbish Lister/gi;
    print;
  }
  $fh->close
    or die "Error closing message: $!\n";

DESCRIPTION

Top

Mail::Qmail::Queue::Receive::Body is designed for use in qmail-queue emulation. This is a way of modifying the behavior of qmail by replacing its queueing mechanism with your own program, which may modify or reject the message, then call the real qmail-queue program to queue the message. This is commonly done with Bruce Guenter's QMAILQUEUE patch (http://www.qmail.org/top.html#qmailqueue), also included in netqmail (http://www.qmail.org/netqmail/). This patch lets you override the standard qmail-queue program by setting the environment variable QMAILQUEUE. It can also be done by renaming the original qmail-queue, installing your script in its place, and having your script call the renamed qmail-queue to inject the message.

For a simplified interface, see Mail::Qmail::Queue::Message. To read the message envelope, see Mail::Qmail::Queue::Receive::Envelope. To re-inject the message, see Mail::Qmail::Queue::Send.

Note that the specifications for qmail-queue's interface require that the message be read before the envelope.

The constructor and methods of this class will die if they encounter a serious error. If you would prefer different behavior, use eval to catch these and handle them as exceptions.

CONSTRUCTOR

new ( %options )

Creates a new qmail-queue message body reader, but does not start reading it.

Available options are:

FileHandle

Read the body from the specified file handle, instead of the default of file desriptor 0.

METHODS

body_fh( )

Returns a filehandle from which the body can be read.

close( )

Closes the filehandle with the message body, and returns the result of the close.

body( )

Returns the entire body as a string, then closes the filehandle. Note that this can consume a lot of memory for a very large message; reading it from the handle returned by the body_fh method will be more efficient.

SEE ALSO

Top

qmail-queue(8), Mail::Qmail::Queue::Message, Mail::Qmail::Queue::Receive::Envelope, Mail::Qmail::Queue::Send.

COPYRIGHT

Top


Mail-Qmail-Queue documentation Contained in the Mail-Qmail-Queue distribution.
package Mail::Qmail::Queue::Receive::Body;
our $VERSION = 0.02;
#
# Copyright 2006 Scott Gifford
#
# This library is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.

use warnings;
use strict;

use constant QQ_BODY_FD => 0;

use Mail::Qmail::Queue::Error qw(:errcodes :fail);

sub new
{
    my $class = shift;
    my %o = @_;
    my $self = bless {}, $class;
    
    if ($o{FileHandle})
    {
	$self->{_fh} = $o{FileHandle};
    }
    else
    {
	$self->{_fh} = FileHandle->new_from_fd(QQ_BODY_FD,"r")
	    or permfail QQ_EXIT_READERR, "Couldn't open FD 0 to read message: $!\n";
    }
    $self;
}

sub body_fh
{
    my $self = shift;
    return $self->{_fh};
}


sub close
{
    my $self = shift;
    return close($self->{_fh});
}


sub body
{
    my $self = shift;
    my $fh = $self->{_fh};
    local $/ = undef;
    my $body = <$fh>;
    $self->close()
        or die "Error closing message body filehandle: $!\n";
    return $body;
}


1;