| 1 | # $Id: Wrapper.pm,v 1.5 2004/02/26 19:24:52 tvierling Exp $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2002-2004 Todd Vierling <tv@pobox.com> <tv@duh.org> | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # Redistribution and use in source and binary forms, with or without | ||
| 7 | # modification, are permitted provided that the following conditions are met: | ||
| 8 | # | ||
| 9 | # 1. Redistributions of source code must retain the above copyright notice, | ||
| 10 | # this list of conditions and the following disclaimer. | ||
| 11 | # | ||
| 12 | # 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | # notice, this list of conditions and the following disclaimer in the | ||
| 14 | # documentation and/or other materials provided with the distribution. | ||
| 15 | # | ||
| 16 | # 3. Neither the name of the author nor the names of contributors may be used | ||
| 17 | # to endorse or promote products derived from this software without specific | ||
| 18 | # prior written permission. | ||
| 19 | # | ||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 21 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 24 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 25 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 26 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 27 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 28 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 29 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 30 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | |||
| 32 | package Mail::Milter::Wrapper; | ||
| 33 | |||
| 34 | use 5.006; | ||
| 35 | |||
| 36 | use strict; | ||
| 37 | use warnings; | ||
| 38 | |||
| 39 | use Carp; | ||
| 40 | use Mail::Milter; | ||
| 41 | use Sendmail::Milter 0.18; # get needed constants | ||
| 42 | |||
| 43 | our $VERSION = '0.03'; | ||
| 44 | |||
| 45 | =pod | ||
| 46 | |||
| 47 | =head1 NAME | ||
| 48 | |||
| 49 | Mail::Milter::Wrapper - Perl extension for wrappering milter objects | ||
| 50 | |||
| 51 | =head1 SYNOPSIS | ||
| 52 | |||
| 53 | use Mail::Milter::Wrapper; | ||
| 54 | |||
| 55 | my $milter = ...; | ||
| 56 | my $wrapper = new Mail::Milter::Wrapper($milter, \&foo); | ||
| 57 | |||
| 58 | use Sendmail::Milter; | ||
| 59 | ... | ||
| 60 | Sendmail::Milter::register('foo', $wrapper, SMFI_CURR_ACTS); | ||
| 61 | |||
| 62 | =head1 DESCRIPTION | ||
| 63 | |||
| 64 | Mail::Milter::Wrapper wraps another milter, allowing for interception | ||
| 65 | of the passed arguments and/or return code of the contained milter. | ||
| 66 | |||
| 67 | =head1 METHODS | ||
| 68 | |||
| 69 | =over 4 | ||
| 70 | |||
| 71 | =item new(MILTER, CODEREF[, CALLBACK ...]) | ||
| 72 | |||
| 73 | Creates a Mail::Milter::Wrapper object. | ||
| 74 | |||
| 75 | MILTER is the milter to wrap, which may be a plain hash reference or an | ||
| 76 | instance of a hashref object such as C<Mail::Milter::Object>. CODEREF is | ||
| 77 | the wrapper subroutine. CALLBACKs, if specified, are named callbacks | ||
| 78 | which are needed by the wrapper, even if the contained milter does not use | ||
| 79 | them. | ||
| 80 | |||
| 81 | The wrapper subroutine will be called with the following arguments, in | ||
| 82 | this order: | ||
| 83 | |||
| 84 | * reference to the wrapper | ||
| 85 | * name of callback | ||
| 86 | * subroutine reference to call into the wrapped milter | ||
| 87 | * arguments for the callback (>= 0) | ||
| 88 | |||
| 89 | This subroutine should ALWAYS pass the "close" callback through to the | ||
| 90 | contained milter. Failure to do so may corrupt the contained milter's | ||
| 91 | state information and cause memory leaks. | ||
| 92 | |||
| 93 | As an example, a simple subroutine which just passes the callback through | ||
| 94 | might be written as: | ||
| 95 | |||
| 96 | sub callback_wrapper { | ||
| 97 | shift; # don't need $this | ||
| 98 | my $cbname = shift; | ||
| 99 | my $callback_sub = shift; | ||
| 100 | |||
| 101 | &$callback_sub(@_); | ||
| 102 | } | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub new ($$&;@) { | ||
| 107 | my $this = bless {}, shift; | ||
| 108 | my $callbacks = shift; | ||
| 109 | my $wrapper_sub = shift || croak 'new Wrapper: wrapper_sub is undef'; | ||
| 110 | my %needed_cbs = map { $_ => 1 } @_; | ||
| 111 | |||
| 112 | my $pkg = caller; | ||
| 113 | |||
| 114 | foreach my $cbname (keys %Sendmail::Milter::DEFAULT_CALLBACKS) { | ||
| 115 | my $cbref = $callbacks->{$cbname}; | ||
| 116 | |||
| 117 | if (defined($cbref)) { | ||
| 118 | $cbref = Mail::Milter::resolve_callback($cbref, $pkg); | ||
| 119 | } elsif (defined($needed_cbs{$cbname})) { | ||
| 120 | $cbref = sub { SMFIS_CONTINUE; }; | ||
| 121 | } | ||
| 122 | |||
| 123 | next unless defined($cbref); | ||
| 124 | |||
| 125 | $this->{$cbname} = sub { | ||
| 126 | &$wrapper_sub($this, $cbname, $cbref, @_); | ||
| 127 | }; | ||
| 128 | } | ||
| 129 | |||
| 130 | $this; | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; | ||
| 134 | __END__ |