| 1 | package Mail::Toaster::Base; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '5.50'; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use Params::Validate ':all'; | ||
| 9 | |||
| 10 | our $verbose = our $last_audit = our $last_error = 0; # package variables | ||
| 11 | our (@audit, @errors); # package wide message stacks | ||
| 12 | our ($conf, $log); | ||
| 13 | our ($darwin, $dns, $freebsd, $qmail, $logs, $mysql, $setup, $toaster, $util); | ||
| 14 | |||
| 15 | our %std_opts = ( | ||
| 16 | test_ok => { type => BOOLEAN, optional => 1 }, | ||
| 17 | verbose => { type => BOOLEAN, optional => 1, default => $verbose }, | ||
| 18 | fatal => { type => BOOLEAN, optional => 1, default => 1 }, | ||
| 19 | ); | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my $class = shift; | ||
| 23 | my %p = validate( @_, { %std_opts } ); | ||
| 24 | my @caller = caller; | ||
| 25 | # warn sprintf( "Base.pm loaded by %s, %s, %s\n", @caller ) if $caller[0] ne 'main'; | ||
| 26 | return bless {}, $class; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub darwin { | ||
| 30 | my $self = shift; | ||
| 31 | return $darwin if ref $darwin; | ||
| 32 | require Mail::Toaster::Darwin; | ||
| 33 | return $darwin = Mail::Toaster::Darwin->new(); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub dns { | ||
| 37 | my $self = shift; | ||
| 38 | return $dns if ref $dns; | ||
| 39 | require Mail::Toaster::DNS; | ||
| 40 | return $dns = Mail::Toaster::DNS->new(); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub freebsd { | ||
| 44 | my $self = shift; | ||
| 45 | return $freebsd if ref $freebsd; | ||
| 46 | require Mail::Toaster::FreeBSD; | ||
| 47 | return $freebsd = Mail::Toaster::FreeBSD->new(); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub logs { | ||
| 51 | my $self = shift; | ||
| 52 | return $logs if ref $logs; | ||
| 53 | require Mail::Toaster::Logs; | ||
| 54 | return $logs = Mail::Toaster::Logs->new(); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub mysql { | ||
| 58 | my $self = shift; | ||
| 59 | return $mysql if ref $mysql; | ||
| 60 | require Mail::Toaster::Mysql; | ||
| 61 | return $mysql = Mail::Toaster::Mysql->new(); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub qmail { | ||
| 65 | my $self = shift; | ||
| 66 | return $qmail if ref $qmail; | ||
| 67 | require Mail::Toaster::Qmail; | ||
| 68 | return $qmail = Mail::Toaster::Qmail->new(); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub setup { | ||
| 72 | my $self = shift; | ||
| 73 | return $setup if ref $setup; | ||
| 74 | require Mail::Toaster::Setup; | ||
| 75 | return $setup = Mail::Toaster::Setup->new(); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub toaster { | ||
| 79 | my $self = shift; | ||
| 80 | return $toaster if ref $toaster; | ||
| 81 | require Mail::Toaster; | ||
| 82 | return $toaster = Mail::Toaster->new(); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub util { | ||
| 86 | my $self = shift; | ||
| 87 | return $util if ref $util; | ||
| 88 | require Mail::Toaster::Utility; | ||
| 89 | return $util = Mail::Toaster::Utility->new(); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub verbose { | ||
| 93 | return $verbose if 1 == scalar @_; | ||
| 94 | return $verbose = $std_opts{verbose}{default} = $_[1]; | ||
| 95 | }; | ||
| 96 | |||
| 97 | sub conf { | ||
| 98 | $conf = $_[1] if $_[1]; | ||
| 99 | return $conf if $conf; | ||
| 100 | $conf = $_[0]->util->parse_config( "toaster-watcher.conf" ); | ||
| 101 | }; | ||
| 102 | |||
| 103 | sub audit { | ||
| 104 | my $self = shift; | ||
| 105 | my $mess = shift; | ||
| 106 | |||
| 107 | my %p = validate( @_, { %std_opts } ); | ||
| 108 | |||
| 109 | if ($mess) { | ||
| 110 | push @audit, $mess; | ||
| 111 | print "$mess\n" if $verbose || $p{verbose}; | ||
| 112 | } | ||
| 113 | |||
| 114 | return \@audit; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub dump_audit { | ||
| 118 | my $self = shift; | ||
| 119 | my %p = validate( @_, { | ||
| 120 | quiet => { type => BOOLEAN, optional => 1, default => 0 }, | ||
| 121 | %std_opts, | ||
| 122 | } | ||
| 123 | ); | ||
| 124 | |||
| 125 | if ( 0 == scalar @audit ) { | ||
| 126 | print "dump_audit: no audit messages\n" if $p{verbose}; | ||
| 127 | return 1; | ||
| 128 | }; | ||
| 129 | |||
| 130 | if ( $last_audit == scalar @audit ) { | ||
| 131 | print "dump_audit: all messages dumped\n" if $p{verbose}; | ||
| 132 | return 1; | ||
| 133 | }; | ||
| 134 | |||
| 135 | if ( $p{quiet} ) { # hide/mask unreported messages | ||
| 136 | $last_audit = scalar @audit; | ||
| 137 | $last_error = scalar @errors; | ||
| 138 | return 1; | ||
| 139 | }; | ||
| 140 | |||
| 141 | print "\n\t\t\tAudit History Report \n\n"; | ||
| 142 | for( my $i = $last_audit; $i < scalar @audit; $i++ ) { | ||
| 143 | print " $audit[$i]\n"; | ||
| 144 | $last_audit++; | ||
| 145 | }; | ||
| 146 | return 1; | ||
| 147 | }; | ||
| 148 | |||
| 149 | sub error { | ||
| 150 | my $self = shift; | ||
| 151 | my $message = shift or carp "why call error w/o message?"; | ||
| 152 | my %p = validate( @_, | ||
| 153 | { location => { type => SCALAR, optional => 1 }, | ||
| 154 | frames => { type => SCALAR, optional => 1, default => 0 }, | ||
| 155 | %std_opts, | ||
| 156 | }, | ||
| 157 | ); | ||
| 158 | |||
| 159 | if ( $message ) { | ||
| 160 | # append message and location to the error stack | ||
| 161 | my @call = caller $p{frames}; | ||
| 162 | my $location = $p{location}; | ||
| 163 | if ( ! $location && scalar @call ) { | ||
| 164 | $location = join( ', ', $call[0], $call[2] ); | ||
| 165 | }; | ||
| 166 | push @errors, { errmsg => $message, errloc => $location }; | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | $message = $errors[-1]; | ||
| 170 | } | ||
| 171 | |||
| 172 | $self->dump_audit if $self->verbose; | ||
| 173 | $self->dump_errors if $p{fatal}; | ||
| 174 | |||
| 175 | exit 1 if $p{fatal}; | ||
| 176 | return; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub dump_errors { | ||
| 180 | my $self = shift; | ||
| 181 | |||
| 182 | if ( $last_error == scalar @errors ) { | ||
| 183 | print "all error messages dumped!\n" if $verbose; | ||
| 184 | return 1; | ||
| 185 | }; | ||
| 186 | |||
| 187 | print "\n\t\t\t Error History Report \n\n"; | ||
| 188 | my $i = 0; | ||
| 189 | foreach ( @errors ) { | ||
| 190 | $i++; | ||
| 191 | next if $i < $last_error; | ||
| 192 | my $msg = $_->{errmsg}; | ||
| 193 | my $loc = $_->{errloc} ? " at $_->{errloc}" : ''; | ||
| 194 | print $msg; | ||
| 195 | for (my $j=length($msg); $j < 90-length($loc); $j++) { print '.'; }; | ||
| 196 | print " $loc\n"; | ||
| 197 | }; | ||
| 198 | print "\n"; | ||
| 199 | $last_error = $i; | ||
| 200 | return 1; | ||
| 201 | }; | ||
| 202 | |||
| 203 | sub get_std_args { | ||
| 204 | my $self = shift; | ||
| 205 | my %p = @_; | ||
| 206 | my %args; | ||
| 207 | foreach ( qw/ verbose fatal test_ok / ) { | ||
| 208 | if ( defined $p{$_} ) { | ||
| 209 | $args{$_} = $p{$_}; | ||
| 210 | next; | ||
| 211 | }; | ||
| 212 | if ( $self->{$_} ) { | ||
| 213 | $args{$_} = $self->{$_}; | ||
| 214 | }; | ||
| 215 | }; | ||
| 216 | return %args; | ||
| 217 | }; | ||
| 218 | |||
| 219 | sub get_std_opts { return %std_opts }; | ||
| 220 | |||
| 221 | sub log { | ||
| 222 | my $self = shift; | ||
| 223 | my $mess = shift or return; | ||
| 224 | |||
| 225 | my $logfile = $conf->{'toaster_watcher_log'} or do { | ||
| 226 | warn "ERROR: no log file defined!\n"; | ||
| 227 | return; | ||
| 228 | }; | ||
| 229 | return if ( -e $logfile && ! -w $logfile ); | ||
| 230 | |||
| 231 | $self->util->logfile_append( $logfile, lines => [$mess], fatal => 0 ); | ||
| 232 | }; | ||
| 233 | |||
| 234 | 1; |