]>
Commit | Line | Data |
---|---|---|
758c7b6b DM |
1 | package PMG::RuleDB::ModField; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
758c7b6b DM |
5 | use DBI; |
6 | use Digest::SHA; | |
aa3a005a | 7 | use Encode qw(encode decode); |
758c7b6b DM |
8 | |
9 | use PMG::Utils; | |
10 | use PMG::ModGroup; | |
11 | use PMG::RuleDB::Object; | |
12 | ||
13 | use base qw(PMG::RuleDB::Object); | |
14 | ||
15 | sub otype { | |
16 | return 4003; | |
17 | } | |
18 | ||
19 | sub oclass { | |
20 | return 'action'; | |
21 | } | |
22 | ||
23 | sub otype_text { | |
24 | return 'Header Attribute'; | |
25 | } | |
26 | ||
758c7b6b DM |
27 | sub final { |
28 | return 0; | |
29 | } | |
30 | ||
31 | sub priority { | |
32 | return 10; | |
33 | } | |
34 | ||
35 | sub new { | |
36 | my ($type, $field, $field_value, $ogroup) = @_; | |
37 | ||
38 | my $class = ref($type) || $type; | |
39 | ||
7a2cf7e6 | 40 | my $self = $class->SUPER::new($class->otype(), $ogroup); |
758c7b6b DM |
41 | |
42 | $self->{field} = $field; | |
43 | $self->{field_value} = $field_value; | |
44 | ||
45 | return $self; | |
46 | } | |
47 | ||
48 | sub load_attr { | |
49 | my ($type, $ruledb, $id, $ogroup, $value) = @_; | |
50 | ||
51 | my $class = ref($type) || $type; | |
52 | ||
53 | defined($value) || return undef; | |
54 | ||
55 | my ($field, $field_value) = $value =~ m/^([^\:]*)\:(.*)$/; | |
56 | ||
57 | (defined($field) && defined($field_value)) || return undef; | |
58 | ||
4418cffc SI |
59 | my $dec_field_value = PMG::Utils::try_decode_utf8($field_value); |
60 | ||
61 | my $obj = $class->new($field, $dec_field_value, $ogroup); | |
758c7b6b DM |
62 | $obj->{id} = $id; |
63 | ||
64 | $obj->{digest} = Digest::SHA::sha1_hex($id, $field, $field_value, $ogroup); | |
65 | ||
66 | return $obj; | |
67 | } | |
68 | ||
69 | sub save { | |
70 | my ($self, $ruledb) = @_; | |
71 | ||
72 | defined($self->{ogroup}) || return undef; | |
73 | ||
4418cffc | 74 | my $new_value = encode('UTF-8', "$self->{field}:$self->{field_value}"); |
758c7b6b DM |
75 | |
76 | if (defined ($self->{id})) { | |
77 | # update | |
78 | ||
79 | $ruledb->{dbh}->do( | |
80 | "UPDATE Object SET Value = ? WHERE ID = ?", | |
81 | undef, $new_value, $self->{id}); | |
82 | ||
83 | } else { | |
84 | # insert | |
85 | ||
86 | my $sth = $ruledb->{dbh}->prepare( | |
87 | "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " . | |
88 | "VALUES (?, ?, ?);"); | |
89 | ||
90 | $sth->execute($self->ogroup, $self->otype, $new_value); | |
91 | ||
92 | $self->{id} = PMG::Utils::lastid($ruledb->{dbh}, 'object_id_seq'); | |
93 | } | |
94 | ||
95 | return $self->{id}; | |
96 | } | |
97 | ||
98 | sub execute { | |
99 | my ($self, $queue, $ruledb, $mod_group, $targets, | |
100 | $msginfo, $vars, $marks) = @_; | |
101 | ||
3650871c | 102 | my $fvalue = PMG::Utils::subst_values_for_header($self->{field_value}, $vars); |
758c7b6b DM |
103 | |
104 | my $subgroups = $mod_group->subgroups($targets); | |
105 | ||
106 | foreach my $ta (@$subgroups) { | |
107 | my ($tg, $e) = (@$ta[0], @$ta[1]); | |
d3d6b5df | 108 | $e->head->replace($self->{field}, $fvalue); |
758c7b6b DM |
109 | } |
110 | } | |
111 | ||
112 | sub short_desc { | |
113 | my $self = shift; | |
114 | ||
115 | return "modify field: $self->{field}:$self->{field_value}"; | |
116 | } | |
117 | ||
b678cdfe DC |
118 | sub properties { |
119 | my ($class) = @_; | |
120 | ||
121 | return { | |
122 | field => { | |
123 | description => "The Field", | |
124 | type => 'string', | |
125 | pattern => '[0-9a-zA-Z\/\\\[\]\+\-\.\*\_]+', | |
126 | maxLength => 1024, | |
127 | }, | |
128 | value => { | |
129 | description => "The Value", | |
130 | type => 'string', | |
b678cdfe DC |
131 | maxLength => 1024, |
132 | }, | |
133 | }; | |
134 | } | |
135 | ||
136 | sub get { | |
137 | my ($self) = @_; | |
138 | ||
139 | return { | |
140 | field => $self->{field}, | |
141 | value => $self->{field_value}, | |
142 | }; | |
143 | } | |
144 | ||
145 | sub update { | |
146 | my ($self, $param) = @_; | |
147 | ||
148 | $self->{field_value} = $param->{value}; | |
149 | $self->{field} = $param->{field}; | |
150 | } | |
151 | ||
758c7b6b DM |
152 | 1; |
153 | ||
154 | __END__ | |
155 | ||
156 | =head1 PMG::RuleDB::ModField | |
157 | ||
158 | Modify fields of a message. |