File Coverage

blib/lib/Log/Saftpresse/Plugin/Postfix/Utils.pm
Criterion Covered Total %
statement 12 37 32.4
branch 0 14 0.0
condition 0 6 0.0
subroutine 4 8 50.0
pod 0 4 0.0
total 16 69 23.1


line stmt bran cond sub pod time code
1             package Log::Saftpresse::Plugin::Postfix::Utils;
2              
3 1     1   4 use strict;
  1         1  
  1         24  
4 1     1   3 use warnings;
  1         1  
  1         39  
5              
6             # ABSTRACT: class with collection of some utility functions
7             our $VERSION = '1.5'; # VERSION
8              
9 1     1   4 use Log::Saftpresse::Constants;
  1         2  
  1         178  
10              
11             our (@ISA, @EXPORT_OK);
12              
13             BEGIN {
14 1     1   8 require Exporter;
15              
16 1         10 @ISA = qw(Exporter);
17 1         496 @EXPORT_OK = qw(
18             &string_trimmer &said_string_trimmer
19             &gimme_domain &postfix_remote &verp_mung
20             );
21             }
22              
23             # Trim a "said:" string, if necessary. Add elipses to show it.
24             # FIXME: This sometimes elides The Wrong Bits, yielding
25             # summaries that are less useful than they could be.
26             sub said_string_trimmer {
27 0     0 0   my($trimmedString, $maxLen) = @_;
28              
29 0           while(length($trimmedString) > $maxLen) {
30 0 0         if($trimmedString =~ /^.* said: /) {
    0          
31 0           $trimmedString =~ s/^.* said: //;
32             } elsif($trimmedString =~ /^.*: */) {
33 0           $trimmedString =~ s/^.*?: *//;
34             } else {
35 0           $trimmedString = substr($trimmedString, 0, $maxLen - 3) . "...";
36 0           last;
37             }
38             }
39              
40 0           return $trimmedString;
41             }
42              
43             # Trim a string, if necessary. Add elipses to show it.
44             sub string_trimmer {
45 0     0 0   my($trimmedString, $maxLen, $doNotTrim) = @_;
46              
47 0 0 0       $trimmedString = substr($trimmedString, 0, $maxLen - 3) . "..."
48             if(! $doNotTrim && (length($trimmedString) > $maxLen));
49 0           return $trimmedString;
50             }
51              
52             # if there's a real domain: uses that. Otherwise uses the IP addr.
53             # Lower-cases returned domain name.
54             #
55             # Optional bit of code elides the last octet of an IPv4 address.
56             # (In case one wants to assume an IPv4 addr. is a dialup or other
57             # dynamic IP address in a /24.)
58             # Does nothing interesting with IPv6 addresses.
59             # FIXME: I think the IPv6 address parsing may be weak
60              
61             sub postfix_remote {
62 0     0 0   $_ = $_[0];
63 0           my($domain, $ipAddr);
64              
65             # split domain/ipaddr into separates
66             # newer versions of Postfix have them "dom.ain[i.p.add.ress]"
67             # older versions of Postfix have them "dom.ain/i.p.add.ress"
68 0 0 0       unless((($domain, $ipAddr) = /^([^\[]+)\[((?:\d{1,3}\.){3}\d{1,3})\]/) == 2 ||
69             (($domain, $ipAddr) = /^([^\/]+)\/([0-9a-f.:]+)/i) == 2) {
70             # more exhaustive method
71 0           ($domain, $ipAddr) = /^([^\[\(\/]+)[\[\(\/]([^\]\)]+)[\]\)]?:?\s*$/;
72             }
73              
74             # "mach.host.dom"/"mach.host.do.co" to "host.dom"/"host.do.co"
75 0 0         if($domain eq 'unknown') {
76 0           $domain = $ipAddr;
77             # For identifying the host part on a Class C network (commonly
78             # seen with dial-ups) the following is handy.
79             # $domain =~ s/\.\d+$//;
80             } else {
81 0           $domain =~
82             s/^(.*)\.([^\.]+)\.([^\.]{3}|[^\.]{2,3}\.[^\.]{2})$/\L$2.$3/;
83             }
84              
85 0           return($domain, $ipAddr);
86             }
87             *gimme_domain = \&postfix_remote;
88              
89             # Hack for VERP (?) - convert address from somthing like
90             # "list-return-36-someuser=someplace.com@lists.domain.com"
91             # to "list-return-ID-someuser=someplace.com@lists.domain.com"
92             # to prevent per-user listing "pollution." More aggressive
93             # munging converts to something like
94             # "list-return@lists.domain.com" (Instead of "return," there
95             # may be numeric list name/id, "warn", "error", etc.?)
96             sub verp_mung {
97 0     0 0   my ( $level, $addr )= @_;
98              
99 0 0         if( $level ) {
100 0           $addr =~ s/((?:bounce[ds]?|no(?:list|reply|response)|return|sentto|\d+).*?)(?:[\+_\.\*-]\d+\b)+/$1-ID/i;
101 0 0         if($level > 1) {
102 0           $addr =~ s/[\*-](\d+[\*-])?[^=\*-]+[=\*][^\@]+\@/\@/;
103             }
104             }
105              
106 0           return $addr;
107             }
108              
109             1;
110              
111             __END__
112              
113             =pod
114              
115             =encoding UTF-8
116              
117             =head1 NAME
118              
119             Log::Saftpresse::Plugin::Postfix::Utils - class with collection of some utility functions
120              
121             =head1 VERSION
122              
123             version 1.5
124              
125             =head1 AUTHOR
126              
127             Markus Benning <ich@markusbenning.de>
128              
129             =head1 COPYRIGHT AND LICENSE
130              
131             This software is Copyright (c) 1998 by James S. Seymour, 2015 by Markus Benning.
132              
133             This is free software, licensed under:
134              
135             The GNU General Public License, Version 2, June 1991
136              
137             =cut