line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Message::Rules; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Message::Rules::VERSION = '1.132770'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
1259
|
use strict;use warnings; |
|
1
|
|
|
1
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
7
|
1
|
|
|
1
|
|
825
|
use Message::Match qw(mmatch); |
|
1
|
|
|
|
|
560
|
|
|
1
|
|
|
|
|
73
|
|
8
|
1
|
|
|
1
|
|
736
|
use Message::Transform qw(mtransform); |
|
1
|
|
|
|
|
407
|
|
|
1
|
|
|
|
|
46
|
|
9
|
1
|
|
|
1
|
|
5
|
use File::Find; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
46
|
|
10
|
1
|
|
|
1
|
|
4
|
use JSON; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
1
|
|
|
1
|
0
|
474
|
my $class = shift; |
14
|
1
|
|
|
|
|
4
|
my $self = {}; |
15
|
1
|
|
|
|
|
3
|
bless ($self, $class); |
16
|
1
|
|
|
|
|
6
|
return $self; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
{ |
20
|
|
|
|
|
|
|
my @loaded_configs; |
21
|
|
|
|
|
|
|
my $add_config = sub { |
22
|
|
|
|
|
|
|
my $thing = shift; |
23
|
|
|
|
|
|
|
$thing->{order} = 0 unless $thing->{order}; |
24
|
|
|
|
|
|
|
push @loaded_configs, $thing; |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
my $wanted = sub { |
27
|
|
|
|
|
|
|
my $f = $File::Find::name; |
28
|
|
|
|
|
|
|
return unless -f "$ENV{PWD}/$f"; |
29
|
|
|
|
|
|
|
return if $f =~ /\/\./; |
30
|
|
|
|
|
|
|
my $contents; |
31
|
|
|
|
|
|
|
eval { |
32
|
|
|
|
|
|
|
open my $fh, '<', "$ENV{PWD}/$f" or die "open of $f failed: $!\n"; |
33
|
|
|
|
|
|
|
read $fh, $contents, 102400 or die "read of $f failed: $!\n"; |
34
|
|
|
|
|
|
|
close $fh or die "close of $f failed: $!\n"; |
35
|
|
|
|
|
|
|
}; |
36
|
|
|
|
|
|
|
die "Message::Rules::load_rules_from_directory: $@\n" if $@; |
37
|
|
|
|
|
|
|
my $conf = decode_json $contents; |
38
|
|
|
|
|
|
|
if(not ref $conf) { |
39
|
|
|
|
|
|
|
die "Message::Rules::load_rules_from_directory: $f did not contain a reference"; |
40
|
|
|
|
|
|
|
return; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
if(ref $conf eq 'HASH') { |
43
|
|
|
|
|
|
|
$add_config->($conf); |
44
|
|
|
|
|
|
|
return; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
if(ref $conf eq 'ARRAY') { |
47
|
|
|
|
|
|
|
$add_config->($_) for @{$conf}; |
48
|
|
|
|
|
|
|
return; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
die "Message::Rules::load_rules_from_directory: $f did not contain either a HASH or ARRAY reference"; |
51
|
|
|
|
|
|
|
return; |
52
|
|
|
|
|
|
|
}; |
53
|
|
|
|
|
|
|
my $get_sorted_configs = sub { |
54
|
|
|
|
|
|
|
my @configs = sort { $a->{order} <=> $b->{order}} @loaded_configs; |
55
|
|
|
|
|
|
|
@loaded_configs = (); |
56
|
|
|
|
|
|
|
return \@configs; |
57
|
|
|
|
|
|
|
}; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub load_rules_from_directory { |
60
|
1
|
|
|
1
|
1
|
1
|
my $self = shift; |
61
|
1
|
|
|
|
|
2
|
my $directory = shift; |
62
|
1
|
50
|
|
|
|
22
|
die "Message::Rules::load_rules_from_directory: passed directory ($directory) does not exist\n" |
63
|
|
|
|
|
|
|
if not -e $directory; |
64
|
1
|
50
|
|
|
|
12
|
die "Message::Rules::load_rules_from_directory: passed directory ($directory) is not a directory\n" |
65
|
|
|
|
|
|
|
if not -d $directory; |
66
|
1
|
|
|
|
|
87
|
find($wanted, $directory); |
67
|
1
|
|
|
|
|
3
|
$self->{rules} = $get_sorted_configs->(); |
68
|
1
|
|
|
|
|
6
|
return $self->{rules}; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub merge_rules { |
73
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
74
|
1
|
|
|
|
|
2
|
my $message = shift; |
75
|
|
|
|
|
|
|
|
76
|
1
|
|
|
|
|
1
|
foreach my $conf (@{$self->{rules}}) { |
|
1
|
|
|
|
|
3
|
|
77
|
1
|
50
|
|
|
|
5
|
next unless mmatch $message, $conf->{match}; |
78
|
1
|
|
|
|
|
42
|
mtransform($message, $conf->{transform}); |
79
|
|
|
|
|
|
|
} |
80
|
1
|
|
|
|
|
24
|
return $message; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
__END__ |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 NAME |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Message::Rules - Apply a pile of rules to incoming messages |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 SYNOPSIS |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
use Message::Rules; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 DESCRIPTION |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
my $r = Message::Rules->new(); |
99
|
|
|
|
|
|
|
$r->load_rules_from_directory('conf/dir'); |
100
|
|
|
|
|
|
|
my $m = $r->merge_rules({main => 'thing'}); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 METHODS |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 load_rules_from_directory($directory); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Iterate through the passed directory tree and load all of |
108
|
|
|
|
|
|
|
the rules found therein. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 merge_rules($message); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Pass $message through the loaded ruleset, and return the |
113
|
|
|
|
|
|
|
updated message. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 TODO |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Tons. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 BUGS |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
None known. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 COPYRIGHT |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Copyright (c) 2013 Dana M. Diederich. All Rights Reserved. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 AUTHOR |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Dana M. Diederich <dana@realms.org> |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=cut |
132
|
|
|
|
|
|
|
|