]> git.proxmox.com Git - pmg-api.git/blame - src/PMG/ModGroup.pm
fix #2430: ruledb disclaimer: make separator configurable
[pmg-api.git] / src / 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
c9cd35e4
DC
84# explode the groups, so we have one for each target we need
85# only to be used by the rRemove action when there was a spaminfo
86sub explode {
87 my ($self, $targets) = @_;
88
89 my $groups = $self->{groups};
90 my $ea = $self->{ea};
91 my $res;
92
2b2b30d3 93 # TODO: implement it more directly with less overhead!
c9cd35e4
DC
94 for my $target ($targets->@*) {
95 $self->subgroups([$target]);
96 }
97
98 return $self->subgroups($targets);
99}
100
6fc0045f
DM
1011;
102
103__END__
104
105=head1 PMG::RuleDB::ModGroup
106
107The idea behind the modification group object (ModGroup) is that some
108modification are target specific. For example a mail can be posted to
109two receiver:
110
111 TO: user1@domain1.com, user2@domain2.com
112
113and we have different rules to add disclaimers for those domains:
114
115 Rule1: .*@domain1.com --> add disclaimer one
116 Rule2: .*@domain2.com --> add disclaimer two
117
118Both Rules are matching, because there are two different receivers,
119one in each domain. If we simply modify the original mail we end up
120with a mail containing both disclaimers, which is not what we want.
121
122Another example is when you have receiver specific content filters,
123for example you don't want to get .exe files for a specific user, but
1359baef 124allow it for everyone else:
6fc0045f
DM
125
126 Rule1: user1@domain1.com && .exe attachments --> remove attachments
127 Rule1: .*@domain2.com && .exe attachments --> accept
128
129So you don't want to remeove the .exe file for user2@domain.com
130
131Instead we want to group modification by matching rule targets.
132
133 $mod_group = PMG::RuleDB::ModGroup->new ($targets, $entity);
134
135 $targets ... array of all receivers
136 $entity ... original mail
137
138return a new ModGroup Object. Action Objects which do target specific
139modification have to call:
140
141 my $subgroups = $mod_group->subgroups ($rule_targets);
142
143 foreach my $ta (@$subgroups) {
144 my ($targets, $entity) = (@$ta[0], @$ta[1]);
145 my_modify_entity ($entity, $targets);
146 }
147
1359baef
TL
148That way we seamlessly hide the fact that mails are delivered to more
149than one recipient, without the requirement to make a copy for each
150recipient (which would lead to many unnecessays notification
2b2b30d3 151mail). Instead we only make a minimum number of copies for specific
6fc0045f 152target groups.