| 1 | # $Id: Object.pm,v 1.3 2004/02/26 19:24:51 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::Object; | ||
| 33 | |||
| 34 | 1 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 35 | 1 | ▶ | use base Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 36 | |||
| 37 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 38 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 39 | |||
| 40 | 0 | ▶ | use Sendmail::Milter 0.18; # get needed constants |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 41 | use Symbol; | ||
| 42 | use UNIVERSAL; | ||
| 43 | |||
| 44 | our $VERSION = '0.03'; | ||
| 45 | |||
| 46 | =pod | ||
| 47 | |||
| 48 | =head1 NAME | ||
| 49 | |||
| 50 | Mail::Milter::Object - Perl extension to encapsulate a milter in an object | ||
| 51 | |||
| 52 | =head1 SYNOPSIS | ||
| 53 | |||
| 54 | package Foo; | ||
| 55 | use base Mail::Milter::Object; | ||
| 56 | |||
| 57 | sub connect_callback { | ||
| 58 | my $this = shift; | ||
| 59 | my $ctx = shift; | ||
| 60 | my @connect_args = @_; | ||
| 61 | ... | ||
| 62 | } | ||
| 63 | |||
| 64 | ... | ||
| 65 | my $milter = new Foo; | ||
| 66 | |||
| 67 | =head1 DESCRIPTION | ||
| 68 | |||
| 69 | Normally, milters passed to C<Sendmail::Milter> consist of nondescript | ||
| 70 | hash references. C<Mail::Milter::Object> transforms these callback hashes | ||
| 71 | into fully qualified objects that are easier to maintain and understand. | ||
| 72 | In conjunction with C<Mail::Milter::Chain>, this also allows for a more | ||
| 73 | modular approach to milter implementation, by allowing each milter to be a | ||
| 74 | small, granular object that can exist independently of other milters. | ||
| 75 | |||
| 76 | Each object inheriting from this class has access to the hash reference | ||
| 77 | making up the object itself. Two caveats must be noted when accessing | ||
| 78 | this hashref: | ||
| 79 | |||
| 80 | * Key names used for private data should be prefixed by an underscore (_) | ||
| 81 | in order to prevent accidental recognition as a callback name. | ||
| 82 | |||
| 83 | * Since a milter object can be reused many times throughout its existence, | ||
| 84 | and perhaps reentrantly if threads are in use, the hashref should contain | ||
| 85 | only global configuration data for this object rather than per-message | ||
| 86 | data. Data stored per message or connection should be stashed in the | ||
| 87 | milter context object by calling C<getpriv()> and C<setpriv()> on the | ||
| 88 | context object. | ||
| 89 | |||
| 90 | =head1 METHODS | ||
| 91 | |||
| 92 | =over 4 | ||
| 93 | |||
| 94 | =item new() | ||
| 95 | |||
| 96 | Creates a new C<Mail::Milter::Object>. The fully qualified class is | ||
| 97 | scanned for milter callback methods with names of the form | ||
| 98 | CALLBACK_callback. If such a method exists, a corresponding callback | ||
| 99 | entry point is added to this object. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub new ($) { | ||
| 104 | my $this = bless {}, shift; | ||
| 105 | |||
| 106 | foreach my $cbname (keys %Sendmail::Milter::DEFAULT_CALLBACKS) { | ||
| 107 | my $fullcbname = $cbname.'_callback'; | ||
| 108 | next unless (UNIVERSAL::can($this, $fullcbname)); | ||
| 109 | |||
| 110 | $this->{$cbname} = sub { | ||
| 111 | $this->$fullcbname(@_); | ||
| 112 | }; | ||
| 113 | } | ||
| 114 | |||
| 115 | $this; | ||
| 116 | } | ||
| 117 | |||
| 118 | 1; | ||
| 119 | __END__ | ||