]>
Commit | Line | Data |
---|---|---|
a7865a2b DM |
1 | package PMG::RuleDB::MatchField; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
a7865a2b DM |
5 | use DBI; |
6 | use Digest::SHA; | |
7 | use MIME::Words; | |
8 | ||
3d5b36f0 DM |
9 | use PVE::SafeSyslog; |
10 | ||
758c7b6b | 11 | use PMG::Utils; |
a7865a2b DM |
12 | use PMG::RuleDB::Object; |
13 | ||
14 | use base qw(PMG::RuleDB::Object); | |
15 | ||
16 | sub otype { | |
17 | return 3002; | |
18 | } | |
19 | ||
20 | sub oclass { | |
21 | return 'what'; | |
22 | } | |
23 | ||
24 | sub otype_text { | |
25 | return 'Match Field'; | |
26 | } | |
27 | ||
a7865a2b DM |
28 | sub new { |
29 | my ($type, $field, $field_value, $ogroup) = @_; | |
a0ec4098 | 30 | |
a7865a2b DM |
31 | my $class = ref($type) || $type; |
32 | ||
7a2cf7e6 | 33 | my $self = $class->SUPER::new($class->otype(), $ogroup); |
a7865a2b DM |
34 | |
35 | $self->{field} = $field; | |
36 | $self->{field_value} = $field_value; | |
a0ec4098 | 37 | |
a7865a2b DM |
38 | return $self; |
39 | } | |
40 | ||
41 | sub load_attr { | |
42 | my ($type, $ruledb, $id, $ogroup, $value) = @_; | |
a0ec4098 | 43 | |
a7865a2b DM |
44 | my $class = ref($type) || $type; |
45 | ||
9ef3f143 | 46 | defined($value) || die "undefined value: ERROR";; |
a7865a2b DM |
47 | |
48 | my ($field, $field_value) = $value =~ m/^([^:]*)\:(.*)$/; | |
49 | ||
9ef3f143 DM |
50 | defined($field) || die "undefined object attribute: ERROR"; |
51 | defined($field_value) || die "undefined object attribute: ERROR"; | |
a7865a2b | 52 | |
3d5b36f0 DM |
53 | # use known constructor, bless afterwards (because sub class can have constructor |
54 | # with other parameter signature). | |
55 | my $obj = PMG::RuleDB::MatchField->new($field, $field_value, $ogroup); | |
56 | bless $obj, $class; | |
57 | ||
a7865a2b | 58 | $obj->{id} = $id; |
a0ec4098 | 59 | |
a7865a2b | 60 | $obj->{digest} = Digest::SHA::sha1_hex($id, $field, $field_value, $ogroup); |
a0ec4098 | 61 | |
a7865a2b DM |
62 | return $obj; |
63 | } | |
64 | ||
65 | sub save { | |
66 | my ($self, $ruledb) = @_; | |
67 | ||
9ef3f143 | 68 | defined($self->{ogroup}) || die "undefined ogroup: ERROR"; |
a7865a2b DM |
69 | |
70 | my $new_value = "$self->{field}:$self->{field_value}"; | |
71 | $new_value =~ s/\\/\\\\/g; | |
72 | ||
73 | if (defined ($self->{id})) { | |
74 | # update | |
a0ec4098 | 75 | |
a7865a2b | 76 | $ruledb->{dbh}->do( |
a0ec4098 | 77 | "UPDATE Object SET Value = ? WHERE ID = ?", |
a7865a2b DM |
78 | undef, $new_value, $self->{id}); |
79 | ||
80 | } else { | |
81 | # insert | |
82 | ||
83 | my $sth = $ruledb->{dbh}->prepare( | |
84 | "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " . | |
85 | "VALUES (?, ?, ?);"); | |
86 | ||
87 | $sth->execute($self->ogroup, $self->otype, $new_value); | |
a0ec4098 DC |
88 | |
89 | $self->{id} = PMG::Utils::lastid($ruledb->{dbh}, 'object_id_seq'); | |
a7865a2b | 90 | } |
a0ec4098 | 91 | |
a7865a2b DM |
92 | return $self->{id}; |
93 | } | |
94 | ||
95 | sub parse_entity { | |
96 | my ($self, $entity) = @_; | |
97 | ||
98 | return undef if !$self->{field}; | |
99 | ||
100 | my $res; | |
101 | ||
102 | if (my $id = $entity->head->mime_attr ('x-proxmox-tmp-aid')) { | |
103 | chomp $id; | |
104 | ||
55723d70 | 105 | if (defined(my $value = $entity->head->get($self->{field}))) { |
a7865a2b DM |
106 | chomp $value; |
107 | ||
108 | my $decvalue = MIME::Words::decode_mimewords($value); | |
109 | ||
110 | if ($decvalue =~ m|$self->{field_value}|i) { | |
111 | push @$res, $id; | |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | foreach my $part ($entity->parts) { | |
117 | if (my $match = $self->parse_entity($part)) { | |
118 | push @$res, @$match; | |
119 | } | |
120 | } | |
121 | ||
122 | return $res; | |
123 | } | |
124 | ||
125 | sub what_match { | |
126 | my ($self, $queue, $entity, $msginfo) = @_; | |
127 | ||
128 | return $self->parse_entity ($entity); | |
129 | } | |
130 | ||
131 | sub short_desc { | |
132 | my $self = shift; | |
a0ec4098 | 133 | |
a7865a2b | 134 | return "$self->{field}=$self->{field_value}"; |
3d5b36f0 | 135 | } |
a7865a2b | 136 | |
39458a82 DC |
137 | sub properties { |
138 | my ($class) = @_; | |
139 | ||
140 | return { | |
141 | field => { | |
142 | description => "The Field", | |
143 | type => 'string', | |
144 | pattern => '[0-9a-zA-Z\/\\\[\]\+\-\.\*\_]+', | |
145 | maxLength => 1024, | |
146 | }, | |
147 | value => { | |
148 | description => "The Value", | |
149 | type => 'string', | |
39458a82 DC |
150 | maxLength => 1024, |
151 | }, | |
152 | }; | |
153 | } | |
154 | ||
155 | sub get { | |
156 | my ($self) = @_; | |
157 | ||
158 | return { | |
159 | field => $self->{field}, | |
160 | value => $self->{field_value}, | |
161 | }; | |
162 | } | |
163 | ||
164 | sub update { | |
165 | my ($self, $param) = @_; | |
166 | ||
167 | $self->{field_value} = $param->{value}; | |
168 | $self->{field} = $param->{field}; | |
169 | } | |
170 | ||
a7865a2b DM |
171 | 1; |
172 | ||
173 | __END__ | |
174 | ||
175 | =head1 PMG::RuleDB::MatchField | |
176 | ||
177 | Match Header Fields |