11 |
11 |
use SL::Helper::EmailProcessing;
|
12 |
12 |
use SL::Presenter::Tag qw(link_tag);
|
13 |
13 |
|
|
14 |
use Params::Validate qw(:all);
|
14 |
15 |
use List::MoreUtils qw(any);
|
15 |
16 |
|
16 |
17 |
sub sync_record_email_folder {
|
17 |
18 |
my ($self, $config) = @_;
|
18 |
19 |
|
19 |
|
my $imap_client = SL::IMAPClient->new(%$config);
|
20 |
|
return "IMAP client is disabled." unless $imap_client;
|
|
20 |
my %imap_config;
|
|
21 |
foreach my $key (qw(enabled hostname port ssl username password base_folder)) {
|
|
22 |
if (defined $config->{$key}) {
|
|
23 |
$imap_config{$key} = $config->{$key};
|
|
24 |
}
|
|
25 |
}
|
|
26 |
|
|
27 |
my $imap_client = SL::IMAPClient->new(%imap_config);
|
21 |
28 |
|
22 |
29 |
my $email_import = $imap_client->update_emails_from_folder(
|
23 |
|
$config->{folder},
|
24 |
|
{
|
25 |
|
email_journal => {
|
26 |
|
status => 'imported',
|
27 |
|
record_type => $config->{record_type},
|
28 |
|
},
|
|
30 |
folder => $config->{folder},
|
|
31 |
email_journal_params => {
|
|
32 |
record_type => $config->{record_type},
|
29 |
33 |
}
|
30 |
34 |
);
|
31 |
35 |
return "No emails to import." unless $email_import;
|
... | ... | |
57 |
61 |
$result .= "Error while processing email journal $email_journal_id attachments with $function_name: $@";
|
58 |
62 |
};
|
59 |
63 |
}
|
60 |
|
if ($created_records) {
|
|
64 |
if ($created_records && $config->{processed_imap_flag}) {
|
61 |
65 |
$imap_client->set_flag_for_email(
|
62 |
|
$email_journal, $config->{processed_imap_flag});
|
63 |
|
} else {
|
|
66 |
email_journal => $email_journal,
|
|
67 |
flag => $config->{processed_imap_flag},
|
|
68 |
);
|
|
69 |
} elsif ($config->{not_processed_imap_flag}) {
|
64 |
70 |
$imap_client->set_flag_for_email(
|
65 |
|
$email_journal, $config->{not_processed_imap_flag});
|
|
71 |
email_journal => $email_journal,
|
|
72 |
flag => $config->{not_processed_imap_flag},
|
|
73 |
);
|
66 |
74 |
}
|
67 |
75 |
}
|
68 |
76 |
$result .= "Processed attachments with "
|
... | ... | |
105 |
113 |
my ($self, $job_obj) = @_;
|
106 |
114 |
$self->{job_obj} = $job_obj;
|
107 |
115 |
|
108 |
|
my $data = $job_obj->data_as_hash;
|
|
116 |
my %spec = (
|
|
117 |
folder => {
|
|
118 |
type => SCALAR,
|
|
119 |
optional => 1,
|
|
120 |
},
|
|
121 |
record_type => {
|
|
122 |
optional => 1,
|
|
123 |
default => 'catch_all',
|
|
124 |
callbacks => {
|
|
125 |
'valid record type' => sub {
|
|
126 |
my $valid_record_types = SL::DB::EmailJournal->meta->{columns}->{record_type}->{check_in};
|
|
127 |
unless (any {$_[0] eq $_} @$valid_record_types) {
|
|
128 |
die "record_type '$_[0]' is not valid. Possible values:\n- " . join("\n- ", @$valid_record_types);
|
|
129 |
}
|
|
130 |
},
|
|
131 |
},
|
|
132 |
},
|
|
133 |
process_imported_emails => {
|
|
134 |
type => SCALAR | ARRAYREF,
|
|
135 |
optional => 1,
|
|
136 |
callbacks => {
|
|
137 |
'function is implemented' => sub {
|
|
138 |
foreach my $function_name (ref $_[0] eq 'ARRAY' ? @{$_[0]} : ($_[0])) {
|
|
139 |
!!SL::Helper::EmailProcessing->can_function($function_name) or
|
|
140 |
die "Function '$function_name' not implemented in SL::Helper::EmailProcessing";
|
|
141 |
}
|
|
142 |
1;
|
|
143 |
}
|
|
144 |
}
|
|
145 |
},
|
|
146 |
processed_imap_flag => {
|
|
147 |
type => SCALAR,
|
|
148 |
optional => 1,
|
|
149 |
},
|
|
150 |
not_processed_imap_flag => {
|
|
151 |
type => SCALAR,
|
|
152 |
optional => 1,
|
|
153 |
},
|
|
154 |
email_import_ids_to_delete => {
|
|
155 |
type => ARRAYREF,
|
|
156 |
optional => 1,
|
|
157 |
},
|
|
158 |
# email config
|
|
159 |
enabled => { type => BOOLEAN, optional => 1, },
|
|
160 |
hostname => { type => SCALAR, optional => 1, },
|
|
161 |
port => { type => SCALAR, optional => 1, },
|
|
162 |
ssl => { type => BOOLEAN, optional => 1, },
|
|
163 |
username => { type => SCALAR, optional => 1, },
|
|
164 |
password => { type => SCALAR, optional => 1, },
|
|
165 |
base_folder => { type => SCALAR, optional => 1, },
|
|
166 |
);
|
109 |
167 |
|
110 |
|
my $email_import_ids_to_delete = $data->{email_import_ids_to_delete} || [];
|
|
168 |
my @bj_data = $job_obj->data_as_hash;
|
|
169 |
my %data = validate_with(
|
|
170 |
params => \@bj_data,
|
|
171 |
spec => \%spec,
|
|
172 |
called => "data filed in Background Job",
|
|
173 |
);
|
111 |
174 |
|
112 |
|
my $record_type = $data->{record_type};
|
113 |
|
my $config = $::lx_office_conf{"record_emails_imap/record_type/$record_type"}
|
|
175 |
my $record_type = $data{record_type};
|
|
176 |
my $loaded_config = $::lx_office_conf{"record_emails_imap/record_type/$record_type"}
|
114 |
177 |
|| $::lx_office_conf{record_emails_imap}
|
115 |
178 |
|| {};
|
|
179 |
|
|
180 |
my @loaded_config = %$loaded_config;
|
|
181 |
my %config = validate_with(
|
|
182 |
params => \@loaded_config,
|
|
183 |
spec => \%spec,
|
|
184 |
called => "kivitendo.conf in [record_emails_imap] with type $record_type",
|
|
185 |
);
|
116 |
186 |
# overwrite with background job data
|
117 |
|
$config->{$_} = $data->{$_} for keys %$data;
|
118 |
|
$config->{record_type} ||= 'catch_all';
|
119 |
|
|
120 |
|
$record_type = $config->{record_type};
|
121 |
|
if ($record_type) {
|
122 |
|
my $valid_record_types = SL::DB::EmailJournal->meta->{columns}->{record_type}->{check_in};
|
123 |
|
unless (any {$record_type eq $_} @$valid_record_types) {
|
124 |
|
die "record_type '$record_type' is not valid. Possible values:\n- " . join("\n- ", @$valid_record_types);
|
125 |
|
}
|
126 |
|
}
|
|
187 |
$config{$_} = $data{$_} for keys %data;
|
127 |
188 |
|
128 |
189 |
my @results;
|
129 |
|
if (scalar $email_import_ids_to_delete) {
|
130 |
|
push @results, $self->delete_email_imports($email_import_ids_to_delete);
|
|
190 |
if (scalar $config{email_import_ids_to_delete}) {
|
|
191 |
push @results, $self->delete_email_imports($config{email_import_ids_to_delete});
|
131 |
192 |
}
|
132 |
193 |
|
133 |
|
push @results, $self->sync_record_email_folder($config);
|
|
194 |
push @results, $self->sync_record_email_folder(\%config);
|
134 |
195 |
|
135 |
196 |
return join("\n", grep { $_ ne ''} @results);
|
136 |
197 |
}
|
Email Background Jobs: validiere Daten-Feld