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