blib/lib/OTRS/ForwardQueue.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 61 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 209 CC 11 SCAR 39.4
CC
11
Cov
0%
process_queue
132.0
BEGIN
2.0
BEGIN
2.0
CRAP 51.3
1 package OTRS::ForwardQueue;
2  
3 use 5.014;
4  
5 use strict;
6 use warnings;
7  
8 use Moose;
9 use namespace::autoclean;
10  
11 use Email::Sender::Simple;
12 use Email::Sender::Transport::SMTP;
13 use Email::Simple;
14 use Email::Simple::Creator;
15  
16 use Template;
17  
18 use Kernel::Config;
19 use Kernel::System::Encode;
20 use Kernel::System::Log;
21 use Kernel::System::Time;
22 use Kernel::System::Main;
23 use Kernel::System::DB;
24 use Kernel::System::Ticket;
25 use Kernel::System::Ticket::Article;
26  
27 our $VERSION = '0.12';
28  
29 has 'query' => (
30 traits => ['Hash'],
31 is => 'rw',
32 isa => 'HashRef',
33 required => 1,
34 handles => {
35 get_query => 'get',
36 set_query => 'set',
37 },
38 );
39  
40 has 'options' => (
41 traits => ['Hash'],
42 is => 'ro',
43 isa => 'HashRef',
44 required => 1,
45 handles => {
46 get_option => 'get',
47 exists_option => 'exists',
48 defined_option => 'defined',
49 },
50 );
51  
52 sub process_queue
53 {
54 my $self = shift;
55  
56 # Create all objects necessary for searching tickets
57 # Taken from documentation for Kernel::System::Ticket
58 my $ConfigObject = Kernel::Config->new();
59  
60 if ($self->exists_option('TempDir') && $self->defined_option('TempDir'))
61 {
62 $ConfigObject->Set( Key => 'TempDir', Value => $self->get_option('TempDir') );
63 }
64  
65 my $EncodeObject = Kernel::System::Encode->new(
66 ConfigObject => $ConfigObject,
67 );
68  
69 my $LogObject = Kernel::System::Log->new(
70 ConfigObject => $ConfigObject,
71 EncodeObject => $EncodeObject,
72 );
73  
74 my $TimeObject = Kernel::System::Time->new(
75 ConfigObject => $ConfigObject,
76 LogObject => $LogObject,
77 );
78  
79 my $MainObject = Kernel::System::Main->new(
80 ConfigObject => $ConfigObject,
81 EncodeObject => $EncodeObject,
82 LogObject => $LogObject,
83 );
84  
85 my $DBObject = Kernel::System::DB->new(
86 ConfigObject => $ConfigObject,
87 EncodeObject => $EncodeObject,
88 LogObject => $LogObject,
89 MainObject => $MainObject,
90 );
91  
92 my $TicketObject = Kernel::System::Ticket->new(
93 ConfigObject => $ConfigObject,
94 LogObject => $LogObject,
95 DBObject => $DBObject,
96 MainObject => $MainObject,
97 TimeObject => $TimeObject,
98 EncodeObject => $EncodeObject,
99 );
100
101 # Always return results as an array, as we use Ticket ID to obtain any additional
102 # information (results as a hash includes Ticket Number as well)
103 $self->set_query('Result' => 'ARRAY');
104
105 my @results = $TicketObject->TicketSearch(%{$self->query});
106
107 foreach my $ticket_id (@results)
108 {
109 if ($self->exists_option('Debug') && $self->defined_option('Debug') && $self->get_option('Debug'))
110 {
111 print "Processing ticket ID: $ticket_id\n";
112 }
113
114 my %ticket = $TicketObject->TicketGet(
115 TicketID => $ticket_id,
116 );
117
118 unless ($self->exists_option('DisableLocking') && $self->defined_option('DisableLocking') && $self->get_option('DisableLocking'))
119 {
120 # Lock ticket before proceeding, to prevent other users from accessing it
121 my $lock_success = $TicketObject->TicketLockSet(
122 Lock => 'lock',
123 TicketID => $ticket_id,
124 UserID => $self->get_query('UserID'),
125 SendNoNotification => 1,
126 );
127 }
128
129 unless ($self->exists_option('DisableEmail') && $self->defined_option('DisableEmail') && $self->get_option('DisableEmail'))
130 {
131 # First article in ticket will be the original user request - we need this for the
132 # body of the forwarded email and the full From: field
133 my %first_article = $TicketObject->ArticleFirstArticle(
134 TicketID => $ticket_id,
135 );
136
137 my $from_address = $first_article{'From'};
138 my $recipient = $self->get_option('ForwardTo');
139
140 my $forward_email = Email::Simple->create(
141 header => [
142 To => $recipient,
143 From => $from_address,
144 Subject => $ticket{'Title'},
145 ],
146 body => $first_article{'Body'},
147 );
148
149 # Set additional mail options, including envelope from
150 my %mail_options = (
151 from => $first_article{'CustomerID'},
152 );
153
154 if ($self->exists_option('SMTP') && $self->defined_option('SMTP') && $self->get_option('SMTP'))
155 {
156 my $transport = Email::Sender::Transport::SMTP->new({
157 host => $self->get_option('SMTPServer'),
158 });
159
160 $mail_options{'transport'} = $transport;
161 }
162
163 Email::Sender::Simple->send($forward_email, \%mail_options);
164
165 if ($self->exists_option('NotifyCustomer') && $self->defined_option('NotifyCustomer') && $self->get_option('NotifyCustomer'))
166 {
167 # Produce the body of the response to the customer
168 my $nc_tt = Template->new({
169 INCLUDE_PATH => $self->get_option('TemplatesPath')
170 }) || die "$Template::ERROR\n";
171
172 my $nc_output = '';
173 my $nc_vars = {
174 ticket => \%ticket,
175 };
176
177 my $notify_template = 'notify_customer.tt';
178
179 if ($self->exists_option('NotifyCustomerTemplate') && $self->defined_option('NotifyCustomerTemplate') && $self->get_option('NotifyCustomerTemplate'))
180 {
181 $notify_template = $self->get_option('NotifyCustomerTemplate');
182 }
183
184 $nc_tt->process($notify_template, $nc_vars, \$nc_output) || die $nc_tt->error() . "\n";
185
186 my $from_address = $first_article{'ToRealname'};
187
188 if ($self->exists_option('FromAddress') && $self->defined_option('FromAddress') && $self->get_option('FromAddress'))
189 {
190 $from_address = $self->get_option('FromAddress');
191 }
192
193 # Add a new article, which should be emailed automatically to the customer.
194 # Remember that To/From are reversed here, since we are sending an email to
195 # the customer who raised the ticket.
196 my $article_id = $TicketObject->ArticleSend(
197 TicketID => $ticket_id,
198 ArticleType => 'email-external',
199 SenderType => 'system',
200 From => $from_address,
201 To => $first_article{'From'},
202 Subject => 'Ticket forwarded: ' . $ticket{'Title'},
203 Body => $nc_output,
204 Charset => 'ISO-8859-15',
205 MimeType => 'text/plain',
206 HistoryType => 'EmailCustomer',
207 HistoryComment => 'Notified customer of ticket forwarding',
208 UserID => $self->get_query('UserID'),
209 AutoResponseType => 'auto reply',
210 OrigHeader => {
211 From => $from_address,
212 To => $first_article{'From'},
213 Subject => 'Ticket forwarded: ' . $ticket{'Title'},
214 },
215 );
216 }
217 }
218
219 unless ($self->exists_option('DisableHistory') && $self->defined_option('DisableHistory') && $self->get_option('DisableHistory'))
220 {
221 # Log the change in the history
222 my $history_success = $TicketObject->HistoryAdd(
223 Name => $self->get_option('HistoryComment'),
224 HistoryType => 'Misc',
225 TicketID => $ticket_id,
226 CreateUserID => $self->get_query('UserID'),
227 );
228 }
229
230 unless ($self->exists_option('DisableClosing') && $self->defined_option('DisableClosing') && $self->get_option('DisableClosing'))
231 {
232 # Mark the ticket as successfully closed
233 my $close_success = $TicketObject->TicketStateSet(
234 State => 'closed successful',
235 TicketID => $ticket_id,
236 UserID => $self->get_query('UserID'),
237 SendNoNotifications => 1,
238 );
239 }
240 }
241 }
242  
243 __PACKAGE__->meta->make_immutable;
244  
245 1; # Magic true value required at end of module
246  
247 =pod
248  
249 =encoding UTF-8
250  
251 =head1 NAME
252  
253 OTRS::ForwardQueue - Forwards the contents of an OTRS queue to a given email address.
254  
255 =head1 VERSION
256  
257 version 0.12
258  
259 =head1 SYNOPSIS
260  
261 use OTRS::ForwardQueue;
262  
263 %query = (
264 Queues => ['MyQueue'],
265 States => ['new', 'open'],
266 Locks => ['unlock'],
267 UserID => 1,
268 );
269  
270 %options = (
271 ForwardTo => 'nobody@example.org',
272 TempDir => '/tmp',
273 HistoryComment => 'Forward to other request system',
274 SMTP => 1,
275 SMTPServer => 'smtp.example.org',
276 NotifyCustomer => 1,
277 NotifyCustomerTemplate => 'notify_customer.tt',
278 TemplatesPath => '/usr/local/templates',
279 Debug => 1,
280 );
281  
282 my $fq = OTRS::ForwardQueue->new('query' => \%query, 'options' => \%options);
283  
284 $fp->process_queue();
285  
286 =head1 DESCRIPTION
287  
288 This module queries the Open Technology Real Services (OTRS) ticket management
289 system for tickets matching the query provided and then forwards these
290 tickets to an email address, closing them in OTRS.
291  
292 The following functions are provided:
293  
294 =over
295  
296 =item new(\%query, \%options)
297  
298 Produced automatically by Moose, this is the constructor for the class.
299  
300 =over
301  
302 =item \%query
303  
304 Reference to a hash which contains the query parameters. This takes the same
305 key/value pairs as the C<TicketSearch> function of C<Kernel::System::TicketSearch>,
306 except that the C<Result> value is always set to 'ARRAY'.
307  
308 =item \%options
309  
310 Required list of options which affect how the queue is processed.
311  
312 =over
313  
314 =item C<HistoryComment> (required): The comment left in the history of the ticket when it is forwarded to anther system.
315  
316 =item C<TempDir> (optional): Override the temporary directory used by the OTRS cache. Probably needs to be set if you are not running the module as the web server user (e.g. apache). If you get errors about file permissions, try setting this to C<'/tmp'>.
317  
318 =item C<ForwardTo> (required): The email address to forward tickets to.
319  
320 =item C<DisableLocking> (optional): Set to 1 to disable locking of forwarded tickets. Default behaviour is to lock tickets.
321  
322 =item C<DisableHistory> (optional): Set to 1 to disable leaving a comment in the history (effectively makes C<HistoryComment> redundant). Default behaviour is to add a comment.
323  
324 =item C<DisableClosing> (optional): Set to 1 to disable closing the ticket after forwarding. Default behaviour is to mark ticket as 'closed successful'.
325  
326 =item C<DisableEmail> (optional): Set to 1 to disable sending any emails, which effectively prevents forwarding of tickets. Default behaviour is to send email. Included as an option to allow module users to test their code before sending out emails.
327  
328 =item C<SMTP> (optional): Set to 1 to use an SMTP server to send email, instead of the local MTA.
329  
330 =item C<SMTPServer> (optional): Host name or IP address of the SMTP server to use. Only effective if C<SMTP> is set to 1.
331  
332 =item C<NotifyCustomer> (optional): Set to 1 to create a new article on the ticket and notify the customer that it has been forwarded.
333  
334 =item C<NotifyCustomerTemplate> (optional): Relative filename to template for customer notification. Required if C<NotifyCustomer> is set to 1.
335  
336 =item C<TemplatesPath> (optional): Absolute path to template directory. Required if C<NotifyCustomerTemplate> is set.
337  
338 =item C<Debug> (optional): Set to 1 to print extra debugging information, such as the IDs of forwarded tickets.
339  
340 =back
341  
342 =back
343  
344 =item process_queue()
345  
346 Processes the queue based on the options passed in the constructor.
347  
348 =back
349  
350 =head1 DEPENDENCIES
351  
352 Perl version 5.14 or higher is required. You may be able to use the module with older versions of Perl, but this is neither tested nor supported.
353  
354 This module requires the following modules:
355  
356 =over 4
357  
358 =item * L<Moose>
359  
360 =item * L<namespace::autoclean>
361  
362 =item * L<Email::Simple>
363  
364 =item * L<Email::Sender>
365  
366 =item * L<Template> - For dynamically producing the body of emails.
367  
368 =back
369  
370 Although some of the above modules are used for optional features, all the dependencies
371 must be installed as this module will attempt to import all of them.
372  
373 You must also have the OTRS source installed and available via C<@INC>. This module has
374 been tested with OTRS 3.2.10 and 3.3.9.
375  
376 =head1 RUNNING AS A CRON JOB
377  
378 Running a script which uses this module as a cron job may require some additional tweaks.
379 The easiest way is to create a small wrapper script to set the various library paths
380 correctly, such as the one below:
381  
382 #!/bin/bash
383
384 # Set this to the absolute path to your OTRS install, so those
385 # modules can be loaded
386 FQ_OTRS_LIB="-I/path/to/otrs"
387
388 # Comment out this line if you are not using local::lib
389 FQ_LOCAL_LIB="-I$HOME/perl5/lib/perl5"
390
391 # Change this to the path of your script
392 /usr/bin/perl "$FQ_OTRS_LIB" "$FQ_LOCAL_LIB" /path/to/script.pl
393  
394 =head1 INCOMPATIBILITIES
395  
396 None reported.
397  
398 =head1 BUGS AND LIMITATIONS
399  
400 No bugs have been reported.
401  
402 Please report any bugs through the Github issue system:
403  
404 L<https://github.com/pwaring/OTRS-ForwardQueue/issues>
405  
406 =head1 AUTHOR
407  
408 Paul Waring <paul.waring@manchester.ac.uk>
409  
410 =head1 COPYRIGHT AND LICENSE
411  
412 This software is Copyright (c) 2014 by University of Manchester.
413  
414 This is free software, licensed under:
415  
416 The GNU Affero General Public License, Version 3, November 2007
417  
418 =cut
419  
420 __END__
421  
422 # ABSTRACT: Forwards the contents of an OTRS queue to a given email address.
423