| 1 | # $Id: RejectMsgEditor.pm,v 1.6 2004/02/26 19:24:53 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::RejectMsgEditor; | ||
| 33 | |||
| 34 | use 5.006; | ||
| 35 | use base Exporter; | ||
| 36 | use base Mail::Milter::Wrapper; | ||
| 37 | |||
| 38 | use strict; | ||
| 39 | use warnings; | ||
| 40 | |||
| 41 | use Carp; | ||
| 42 | use Mail::Milter::ContextWrapper; | ||
| 43 | use Sendmail::Milter 0.18; # get needed constants | ||
| 44 | |||
| 45 | our $VERSION = '0.03'; | ||
| 46 | |||
| 47 | =pod | ||
| 48 | |||
| 49 | =head1 NAME | ||
| 50 | |||
| 51 | Mail::Milter::Wrapper::RejectMsgEditor - milter wrapper to edit rejection messages | ||
| 52 | |||
| 53 | =head1 SYNOPSIS | ||
| 54 | |||
| 55 | use Mail::Milter::Wrapper::RejectMsgEditor; | ||
| 56 | |||
| 57 | my $milter = ...; | ||
| 58 | my $wrapper = new Mail::Milter::Wrapper::RejectMsgEditor($milter, \&sub); | ||
| 59 | |||
| 60 | my $wrapper2 = &RejectMsgEditor($milter, \&sub); # convenience | ||
| 61 | |||
| 62 | =head1 DESCRIPTION | ||
| 63 | |||
| 64 | Mail::Milter::Wrapper::RejectMsgEditor is a convenience milter wrapper | ||
| 65 | which allows editing of the messages returned for all SMFIS_REJECT | ||
| 66 | rejections. The subroutine provided should edit $_ and need not return | ||
| 67 | any value. | ||
| 68 | |||
| 69 | If the contained milter did not call C<$ctx->setreply()> before returning | ||
| 70 | a rejection code, then a default message will be used. | ||
| 71 | |||
| 72 | For example: | ||
| 73 | |||
| 74 | my $wrapped_milter = &RejectMsgEditor($milter, sub { | ||
| 75 | s,$, - Please e-mail postmaster\@foo.com for assistance., | ||
| 76 | }); | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | our @EXPORT = qw(&RejectMsgEditor); | ||
| 81 | |||
| 82 | sub RejectMsgEditor { | ||
| 83 | new Mail::Milter::Wrapper::RejectMsgEditor(@_); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub new ($$\&) { | ||
| 87 | my $this = Mail::Milter::Wrapper::new(shift, shift, | ||
| 88 | \&wrapper, qw{connect close}); | ||
| 89 | |||
| 90 | $this->{_editor} = shift; | ||
| 91 | |||
| 92 | $this; | ||
| 93 | } | ||
| 94 | |||
| 95 | # internal methods | ||
| 96 | sub wrapper { | ||
| 97 | my $this = shift; | ||
| 98 | my $cbname = shift; | ||
| 99 | my $callback_sub = shift; | ||
| 100 | my $oldctx = shift; | ||
| 101 | |||
| 102 | my $newctx = $oldctx->getpriv(); | ||
| 103 | |||
| 104 | unless (defined($newctx)) { | ||
| 105 | $newctx = new Mail::Milter::ContextWrapper($oldctx, { | ||
| 106 | setreply => sub { shift->set_key(reply => [ @_ ]); }, | ||
| 107 | }); | ||
| 108 | $oldctx->setpriv($newctx); | ||
| 109 | } | ||
| 110 | |||
| 111 | my $rc = &$callback_sub($newctx, @_); | ||
| 112 | my $reply = $newctx->get_key('reply'); | ||
| 113 | $newctx->set_key(reply => undef); | ||
| 114 | |||
| 115 | if ($rc == SMFIS_REJECT) { | ||
| 116 | $reply = [ 554, '5.7.0', 'Command rejected' ] unless $reply; | ||
| 117 | |||
| 118 | local $_ = $reply->[2]; | ||
| 119 | &{$this->{_editor}}; | ||
| 120 | $reply->[2] = $_; | ||
| 121 | } | ||
| 122 | |||
| 123 | $oldctx->setreply(@$reply) if $reply; | ||
| 124 | $oldctx->setpriv(undef) if ($cbname eq 'close'); | ||
| 125 | |||
| 126 | $rc; | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; | ||
| 130 | __END__ |