| 1 | package MYDan::Notify; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | use YAML::XS; | ||
| 6 | |||
| 7 | use File::Basename; | ||
| 8 | use MYDan::Util::Contact; | ||
| 9 | |||
| 10 | my ( %level, %code ); | ||
| 11 | BEGIN{ | ||
| 12 | my $o = MYDan::Util::OptConf->load()->dump( 'notify' ); | ||
| 13 | %level = %{$o->{level}}; | ||
| 14 | for my $file ( glob "$o->{code}/*" ) | ||
| 15 | { | ||
| 16 | my $name = basename $file; | ||
| 17 | $code{$name} = do $file; | ||
| 18 | die "notify load code $name fail" | ||
| 19 | unless $code{$name} && ref $code{$name} eq 'CODE'; | ||
| 20 | } | ||
| 21 | }; | ||
| 22 | |||
| 23 | sub new | ||
| 24 | { | ||
| 25 | my ( $class, %this ) = @_; | ||
| 26 | bless \%this, ref $class || $class; | ||
| 27 | } | ||
| 28 | |||
| 29 | |||
| 30 | =head3 notify | ||
| 31 | |||
| 32 | user => + { 'user1' => 2, 'user2' => 1 }, | ||
| 33 | mesg = > + { name => 'name1', attr => 'attr1', mesg => 'mesg1', time => 'time'} | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | sub notify | ||
| 38 | { | ||
| 39 | my ( $this, %param )= @_; | ||
| 40 | |||
| 41 | my $contact = MYDan::Util::Contact->new(); | ||
| 42 | |||
| 43 | my ( $user, $mesg ) = @param{qw( user mesg )}; | ||
| 44 | |||
| 45 | my %notify; | ||
| 46 | |||
| 47 | for my $u ( keys %$user ) | ||
| 48 | { | ||
| 49 | my $level = $user->{$u}; | ||
| 50 | for my $t ( @{$level{$level}} ) | ||
| 51 | { | ||
| 52 | map{ $notify{$t}{$_} = 1 }$contact->contact( $u => $t ); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | for my $m ( keys %notify ) | ||
| 57 | { | ||
| 58 | &{$code{$m}}( user => [keys %{$notify{$m}}], mesg => $mesg ); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | 1; |