| 1 | package MYDan::Subscribe; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | use YAML::XS; | ||
| 6 | |||
| 7 | use Data::Dumper; | ||
| 8 | use POSIX; | ||
| 9 | |||
| 10 | use MYDan::Util::OptConf; | ||
| 11 | use MYDan::Subscribe::Conf; | ||
| 12 | use File::Basename; | ||
| 13 | use MIME::Base64; | ||
| 14 | use Time::HiRes qw( gettimeofday ); | ||
| 15 | use MYDan::Notify; | ||
| 16 | |||
| 17 | my %o; BEGIN{ %o = MYDan::Util::OptConf->load()->dump('subscribe');}; | ||
| 18 | |||
| 19 | sub new | ||
| 20 | { | ||
| 21 | my ( $class, %this ) = @_; | ||
| 22 | $this{conf} = MYDan::Subscribe::Conf->new( path => "$o{conf}/subscribe" ); | ||
| 23 | $this{notify} = MYDan::Notify->new(); | ||
| 24 | bless \%this, ref $class || $class; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub input | ||
| 28 | { | ||
| 29 | my ( $this, $mesg, $name, $attr, $time ) = @_; | ||
| 30 | |||
| 31 | my %param = ( mesg => $mesg, name => $name, attr => $attr, time => $time ); | ||
| 32 | map{ | ||
| 33 | $param{name} = 'unkown' unless $param{name} && $param{name} =~ /^[a-zA-Z0-9_\-\.]+$/ | ||
| 34 | }qw( name attr ); | ||
| 35 | |||
| 36 | $param{time} ||= POSIX::strftime( "%Y-%m-%d_%H:%M:%S", localtime ); | ||
| 37 | |||
| 38 | my @s = $this->{conf}->get( map{ $_ => $param{$_} }qw( name attr) ); | ||
| 39 | |||
| 40 | my $file = sprintf "$o{logs}/%s", join '.', gettimeofday; | ||
| 41 | |||
| 42 | my %send; | ||
| 43 | eval{ | ||
| 44 | YAML::XS::DumpFile $file, \%param; | ||
| 45 | map{ $send{$_->[2]} = $_->[3] if ! $send{$_->[2]} || $send{$_->[2]} < $_->[3];}@s; | ||
| 46 | |||
| 47 | $this->{notify}->notify( mesg => \%param, user => \%send ); | ||
| 48 | }; | ||
| 49 | |||
| 50 | warn "Subscribe fail: $@\n" if $@; | ||
| 51 | YAML::XS::DumpFile sprintf( "$file.%s", $@ ? 'fail' : 'ok' ), \%send; | ||
| 52 | } | ||
| 53 | |||
| 54 | 1; |