| 1 | # $Id: ContextWrapper.pm,v 1.4 2004/02/26 19:24:50 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::ContextWrapper; | ||
| 33 | |||
| 34 | use 5.006; | ||
| 35 | |||
| 36 | use strict; | ||
| 37 | use warnings; | ||
| 38 | |||
| 39 | use Carp; | ||
| 40 | use Sendmail::Milter 0.18; # get needed constants | ||
| 41 | |||
| 42 | our $VERSION = '0.03'; | ||
| 43 | our $AUTOLOAD; | ||
| 44 | |||
| 45 | =pod | ||
| 46 | |||
| 47 | =head1 NAME | ||
| 48 | |||
| 49 | Mail::Milter::ContextWrapper - Perl extension for wrappering the milter context | ||
| 50 | |||
| 51 | =head1 SYNOPSIS | ||
| 52 | |||
| 53 | use Mail::Milter::ContextWrapper; | ||
| 54 | |||
| 55 | my $oldctx = ($ctx from callback); | ||
| 56 | |||
| 57 | # in the connect_callback | ||
| 58 | $oldctx->setpriv(new Mail::Milter::ContextWrapper($ctx, | ||
| 59 | { methodname => \&methodimpl[, ...] })); | ||
| 60 | |||
| 61 | # in all callbacks | ||
| 62 | my $newctx = $ctx->getpriv(); | ||
| 63 | |||
| 64 | # in the close_callback | ||
| 65 | $oldctx->setpriv(undef); | ||
| 66 | |||
| 67 | =head1 DESCRIPTION | ||
| 68 | |||
| 69 | Mail::Milter::ContextWrapper wraps the milter context with replacement | ||
| 70 | methods defined by the caller. This can be used to intercept context | ||
| 71 | object actions and manipulate them from within a Mail::Milter::Wrapper. | ||
| 72 | |||
| 73 | Because the wrappering must occur on every callback, this implementation | ||
| 74 | suggests embedding the wrapper inside the private data of the milter | ||
| 75 | itself. This works with existing milters by providing separate "setpriv" | ||
| 76 | and "getpriv" methods within the wrapper that do not propagate up to the | ||
| 77 | embedded context object. | ||
| 78 | |||
| 79 | =head1 METHODS | ||
| 80 | |||
| 81 | =over 4 | ||
| 82 | |||
| 83 | =item new(CTX, { NAME => \&SUB[, ...] }) | ||
| 84 | |||
| 85 | Creates a Mail::Milter::ContextWrapper object. This should be called from | ||
| 86 | the "connect" callback and passed back to C<setpriv()>. | ||
| 87 | |||
| 88 | NAMEs are names of methods to override within the wrapper. These methods | ||
| 89 | will be called with the wrapper as first argument (like a normal object | ||
| 90 | method). | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub new ($$$) { | ||
| 95 | my $this = bless {}, shift; | ||
| 96 | |||
| 97 | $this->{ctx} = shift; | ||
| 98 | $this->{methods} = shift; | ||
| 99 | |||
| 100 | $this->{keys} = {}; | ||
| 101 | |||
| 102 | $this; | ||
| 103 | } | ||
| 104 | |||
| 105 | # private autoloader method | ||
| 106 | sub AUTOLOAD { | ||
| 107 | my $sub = $AUTOLOAD; | ||
| 108 | my $this = $_[0]; | ||
| 109 | |||
| 110 | $sub =~ s/^Mail::Milter::ContextWrapper:://; | ||
| 111 | my $subref = $this->{methods}{$sub}; | ||
| 112 | |||
| 113 | $subref = sub { | ||
| 114 | my $this = shift; | ||
| 115 | $this->get_ctx()->$sub(@_); | ||
| 116 | } unless defined($subref); | ||
| 117 | |||
| 118 | goto &$subref; | ||
| 119 | } | ||
| 120 | |||
| 121 | # since AUTOLOAD is here, we need a DESTROY | ||
| 122 | sub DESTROY { | ||
| 123 | my $this = shift; | ||
| 124 | %$this = (); | ||
| 125 | } | ||
| 126 | |||
| 127 | =pod | ||
| 128 | |||
| 129 | =item getpriv() | ||
| 130 | |||
| 131 | Returns a private data item set by C<setpriv()>. See L<Sendmail::Milter> | ||
| 132 | for more information. This implementation stores the datum in the | ||
| 133 | wrapper, thus allowing the parent context to store a reference to the | ||
| 134 | wrapper itself. | ||
| 135 | |||
| 136 | This method cannot be overridden by the user. | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | sub getpriv ($) { | ||
| 141 | my $this = shift; | ||
| 142 | |||
| 143 | $this->{priv}; | ||
| 144 | } | ||
| 145 | |||
| 146 | =pod | ||
| 147 | |||
| 148 | =item get_ctx() | ||
| 149 | |||
| 150 | Returns the parent context object stored within this wrapper. Typically | ||
| 151 | used by method overrides to defer back to the real method. | ||
| 152 | |||
| 153 | This method cannot be overridden by the user. | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | sub get_ctx ($) { | ||
| 158 | my $this = shift; | ||
| 159 | |||
| 160 | $this->{ctx}; | ||
| 161 | } | ||
| 162 | |||
| 163 | =pod | ||
| 164 | |||
| 165 | =item get_key(NAME) | ||
| 166 | |||
| 167 | Get a keyed data item separate from the C<getpriv> private data. This | ||
| 168 | provides out-of-band data storage that need not clobber the single "priv" | ||
| 169 | data item used by most milters. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | sub get_key ($$) { | ||
| 174 | my $this = shift; | ||
| 175 | my $key = shift; | ||
| 176 | |||
| 177 | $this->{keys}{$key}; | ||
| 178 | } | ||
| 179 | |||
| 180 | =pod | ||
| 181 | |||
| 182 | =item getpriv() | ||
| 183 | |||
| 184 | Sets a private data item to be returned by C<getpriv()>. See | ||
| 185 | L<Sendmail::Milter> for more information. This implementation stores the | ||
| 186 | datum in the wrapper, thus allowing the parent context to store a | ||
| 187 | reference to the wrapper itself. | ||
| 188 | |||
| 189 | This method cannot be overridden by the user. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub setpriv ($) { | ||
| 194 | my $this = shift; | ||
| 195 | |||
| 196 | $this->{priv} = shift; | ||
| 197 | 1; | ||
| 198 | } | ||
| 199 | |||
| 200 | =pod | ||
| 201 | |||
| 202 | =item set_key(NAME, VALUE) | ||
| 203 | |||
| 204 | =item set_key(NAME => VALUE) | ||
| 205 | |||
| 206 | Set a keyed data item separate from the C<getpriv> private data. This | ||
| 207 | provides out-of-band data storage that need not clobber the single "priv" | ||
| 208 | data item used by most milters. | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | sub set_key ($$$) { | ||
| 213 | my $this = shift; | ||
| 214 | my $key = shift; | ||
| 215 | |||
| 216 | $this->{keys}{$key} = shift; | ||
| 217 | 1; | ||
| 218 | } | ||
| 219 | |||
| 220 | 1; | ||
| 221 | __END__ |