]> git.proxmox.com Git - pmg-api.git/blame - PMG/ModGroup.pm
add more ruledb classes
[pmg-api.git] / PMG / ModGroup.pm
CommitLineData
6fc0045f
DM
1package PMG::ModGroup;
2
3use strict;
4use warnings;
5use Carp;
6
7sub new {
8 my ($type, $entity, $targets) = @_;
9
10 my $self = {
11 targets => $targets,
12 ea => [$entity],
13 groups => [$targets],
14 entity => $entity,
15 };
16
17 bless $self, $type;
18
19 return $self;
20}
21
22# compute subgroups
23# set ro (read only) to 1 if you do not plan to modify the
24# entity (in actions like 'accept' od 'bcc'). This
25# just return the existing groups but does not create new groups.
26
27sub subgroups {
28 my ($self, $targets, $ro) = @_;
29
30 my $groups;
31 if ($ro) {
32 my @copy = @{$self->{groups}};
33 $groups = \@copy;
34 } else {
35 $groups = $self->{groups};
36 }
37
38 my $ea = $self->{ea};
39
40 my $res;
41
42 for my $i (0..$#$groups) {
43 my $g = @$groups[$i];
44
45 my $tcount = -1;
46 my ($ma, $ua);
47 foreach my $member (@$g) {
48 my $found = 0;
49 foreach my $t (@$targets) {
50 if ($member eq $t) {
51 $found = 1;
52 $tcount++;
53 }
54 }
55 if ($found) {
56 push @$ma, $member;
57 } else {
58 push @$ua, $member;
59 }
60 }
61
62 next if $tcount == -1;
63
64 if ($tcount < $#$g) {
65 @$groups[$i] = $ma;
66 push @$groups, $ua;
67 if ($ro) {
68 push @$ea, @$ea[$i];
69 } else {
70 my $e = @$ea[$i];
71 my $copy = $e->dup;
72
73 # also copy proxmox attribute
74 foreach (keys %$e) {$copy->{$_} = $e->{$_} if $_ =~ m/^PMX_/};
75
76 push @$ea, $copy;
77 }
78 }
79 push @$res, [$ma, @$ea[$i]];
80 }
81
82 return $res;
83}
84
851;
86
87__END__
88
89=head1 PMG::RuleDB::ModGroup
90
91The idea behind the modification group object (ModGroup) is that some
92modification are target specific. For example a mail can be posted to
93two receiver:
94
95 TO: user1@domain1.com, user2@domain2.com
96
97and we have different rules to add disclaimers for those domains:
98
99 Rule1: .*@domain1.com --> add disclaimer one
100 Rule2: .*@domain2.com --> add disclaimer two
101
102Both Rules are matching, because there are two different receivers,
103one in each domain. If we simply modify the original mail we end up
104with a mail containing both disclaimers, which is not what we want.
105
106Another example is when you have receiver specific content filters,
107for example you don't want to get .exe files for a specific user, but
108allow it for everone else:
109
110 Rule1: user1@domain1.com && .exe attachments --> remove attachments
111 Rule1: .*@domain2.com && .exe attachments --> accept
112
113So you don't want to remeove the .exe file for user2@domain.com
114
115Instead we want to group modification by matching rule targets.
116
117 $mod_group = PMG::RuleDB::ModGroup->new ($targets, $entity);
118
119 $targets ... array of all receivers
120 $entity ... original mail
121
122return a new ModGroup Object. Action Objects which do target specific
123modification have to call:
124
125 my $subgroups = $mod_group->subgroups ($rule_targets);
126
127 foreach my $ta (@$subgroups) {
128 my ($targets, $entity) = (@$ta[0], @$ta[1]);
129 my_modify_entity ($entity, $targets);
130 }
131
132That way we seemlessly hide the fact that mails are delivered to more
133than one receipient, without the requirement to make a copy for each
134receipient (which would lead to many unnecessays notification
135mail). Insead we only make a minimum number of copies for specific
136target groups.