| 1 | # | ||
| 2 | # Courier::Filter::Util | ||
| 3 | # Utility class for the Courier::Filter framework. | ||
| 4 | # | ||
| 5 | # (C) 2003-2008 Julian Mehnle <julian@mehnle.net> | ||
| 6 | # $Id: Util.pm 210 2008-03-21 19:30:31Z julian $ | ||
| 7 | # | ||
| 8 | ############################################################################### | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Courier::Filter::Util - Utility class used by the Courier::Filter framework | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | package Courier::Filter::Util; | ||
| 17 | |||
| 18 | use warnings; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | use base 'Exporter'; | ||
| 22 | |||
| 23 | use Error ':try'; | ||
| 24 | |||
| 25 | use Courier::Error; | ||
| 26 | |||
| 27 | use constant TRUE => (0 == 0); | ||
| 28 | use constant FALSE => not TRUE; | ||
| 29 | |||
| 30 | our @EXPORT_OK = qw( | ||
| 31 | ipv4_address_pattern | ||
| 32 | ipv6_address_pattern | ||
| 33 | loopback_address_pattern | ||
| 34 | ); | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | use Courier::Filter::Util qw( | ||
| 39 | ipv4_address_pattern | ||
| 40 | ipv6_address_pattern | ||
| 41 | loopback_address_pattern | ||
| 42 | ); | ||
| 43 | |||
| 44 | $message->remote_host =~ / ^ (?: ::ffff: )? $(\ipv4_address_pattern} $ /x; | ||
| 45 | $message->remote_host =~ / ^ $(\ipv6_address_pattern} $ /x; | ||
| 46 | $message->remote_host =~ / ^ ${\loopback_address_pattern} $ /x; | ||
| 47 | |||
| 48 | =head1 DESCRIPTION | ||
| 49 | |||
| 50 | B<Courier::Filter::Util> is Courier::Filter's utility class. | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | # Implementation: | ||
| 55 | ############################################################################### | ||
| 56 | |||
| 57 | =head2 Constants | ||
| 58 | |||
| 59 | The following constants are provided: | ||
| 60 | |||
| 61 | =over | ||
| 62 | |||
| 63 | =item B<ipv4_address_pattern> | ||
| 64 | |||
| 65 | A regular expression matching an IPv4 address in "dotted decimal" notation. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | use constant octet_decimal_pattern => qr/ 0*? \d | \d\d | [01]\d\d | 2[0-4]\d | 25[0-5] /x; | ||
| 70 | |||
| 71 | use constant ipv4_address_pattern => qr/ ${\octet_decimal_pattern} (?: \. ${\octet_decimal_pattern} ){3} /x; | ||
| 72 | |||
| 73 | =item B<ipv6_address_pattern> | ||
| 74 | |||
| 75 | A regular expression matching an IPv6 address in full RFC 4291 syntax. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | use constant hexword_pattern => qr/\p{IsXDigit}{1,4}/; | ||
| 80 | use constant two_hexwords_or_ipv4_address_pattern => qr/ | ||
| 81 | ${\hexword_pattern} : ${\hexword_pattern} | ${\ipv4_address_pattern} | ||
| 82 | /x; | ||
| 83 | |||
| 84 | use constant ipv6_address_pattern => qr/ | ||
| 85 | # x:x:x:x:x:x:x:x | x:x:x:x:x:x:n.n.n.n | ||
| 86 | (?: ${\hexword_pattern} : ){6} ${\two_hexwords_or_ipv4_address_pattern} | | ||
| 87 | # x::x:x:x:x:x:x | x::x:x:x:x:n.n.n.n | ||
| 88 | (?: ${\hexword_pattern} : ){1} : (?: ${\hexword_pattern} : ){4} ${\two_hexwords_or_ipv4_address_pattern} | | ||
| 89 | # x[:x]::x:x:x:x:x | x[:x]::x:x:x:n.n.n.n | ||
| 90 | (?: ${\hexword_pattern} : ){1,2} : (?: ${\hexword_pattern} : ){3} ${\two_hexwords_or_ipv4_address_pattern} | | ||
| 91 | # x[:...]::x:x:x:x | x[:...]::x:x:n.n.n.n | ||
| 92 | (?: ${\hexword_pattern} : ){1,3} : (?: ${\hexword_pattern} : ){2} ${\two_hexwords_or_ipv4_address_pattern} | | ||
| 93 | # x[:...]::x:x:x | x[:...]::x:n.n.n.n | ||
| 94 | (?: ${\hexword_pattern} : ){1,4} : (?: ${\hexword_pattern} : ){1} ${\two_hexwords_or_ipv4_address_pattern} | | ||
| 95 | # x[:...]::x:x | x[:...]::n.n.n.n | ||
| 96 | (?: ${\hexword_pattern} : ){1,5} : ${\two_hexwords_or_ipv4_address_pattern} | | ||
| 97 | # x[:...]::x | - | ||
| 98 | (?: ${\hexword_pattern} : ){1,6} : ${\hexword_pattern} | | ||
| 99 | # x[:...]:: | - | ||
| 100 | (?: ${\hexword_pattern} : ){1,7} : | | ||
| 101 | # ::[...:]x | - | ||
| 102 | :: (?: ${\hexword_pattern} : ){0,6} ${\hexword_pattern} | | ||
| 103 | # - | ::[...:]n.n.n.n | ||
| 104 | :: (?: ${\hexword_pattern} : ){0,5} ${\two_hexwords_or_ipv4_address_pattern} | | ||
| 105 | # :: | - | ||
| 106 | :: | ||
| 107 | /x; | ||
| 108 | |||
| 109 | =item B<loopback_address_pattern> | ||
| 110 | |||
| 111 | A regular expression matching an IPv4 or IPv6 loopback address (C<127.n.n.n>, | ||
| 112 | C<::ffff:127.n.n.n.n>, C<::1>). | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | use constant loopback_address_pattern => qr/ (?: ::ffff: )? 127 (?: \.\d{1,3} ){3} | ::1 /x; | ||
| 117 | |||
| 118 | =back | ||
| 119 | |||
| 120 | =head1 SEE ALSO | ||
| 121 | |||
| 122 | L<Courier::Filter>. | ||
| 123 | |||
| 124 | For AVAILABILITY, SUPPORT, and LICENSE information, see | ||
| 125 | L<Courier::Filter::Overview>. | ||
| 126 | |||
| 127 | =head1 AUTHOR | ||
| 128 | |||
| 129 | Julian Mehnle <julian@mehnle.net> | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | TRUE; |