blib/lib/OTRS/ForwardQueue.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 49 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 187 CC 9 SCAR 34.4
CC
9
Cov
0%
process_queue
90.0
BEGIN
2.0
BEGIN
2.0
CRAP 31.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.07';
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 $nc_tt->process('notify_customer.tt', $nc_vars, \$nc_output) || die $nc_tt->error() . "\n";
178
179 # Add a new article, which should be emailed automatically to the customer.
180 # Remember that To/From are reversed here, since we are sending an email to
181 # the customer who raised the ticket.
182 my $article_id = $TicketObject->ArticleSend(
183 TicketID => $ticket_id,
184 ArticleType => 'email-external',
185 SenderType => 'system',
186 From => $first_article{'ToRealname'},
187 To => $first_article{'From'},
188 Subject => 'Ticket forwarded: ' . $ticket{'Title'},
189 Body => $nc_output,
190 Charset => 'ISO-8859-15',
191 MimeType => 'text/plain',
192 HistoryType => 'EmailCustomer',
193 HistoryComment => 'Notified customer of ticket forwarding',
194 UserID => $self->get_query('UserID'),
195 AutoResponseType => 'auto reply',
196 OrigHeader => {
197 From => $first_article{'ToRealname'},
198 To => $first_article{'From'},
199 Subject => 'Ticket forwarded: ' . $ticket{'Title'},
200 },
201 );
202 }
203 }
204
205 unless ($self->exists_option('DisableHistory') && $self->defined_option('DisableHistory') && $self->get_option('DisableHistory'))
206 {
207 # Log the change in the history
208 my $history_success = $TicketObject->HistoryAdd(
209 Name => $self->get_option('HistoryComment'),
210 HistoryType => 'Misc',
211 TicketID => $ticket_id,
212 CreateUserID => $self->get_query('UserID'),
213 );
214 }
215
216 unless ($self->exists_option('DisableClosing') && $self->defined_option('DisableClosing') && $self->get_option('DisableClosing'))
217 {
218 # Mark the ticket as successfully closed
219 my $close_success = $TicketObject->TicketStateSet(
220 State => 'closed successful',
221 TicketID => $ticket_id,
222 UserID => $self->get_query('UserID'),
223 SendNoNotifications => 1,
224 );
225 }
226 }
227 }
228  
229 __PACKAGE__->meta->make_immutable;
230  
231 1; # Magic true value required at end of module
232  
233 =pod
234  
235 =encoding UTF-8
236  
237 =head1 NAME
238  
239 OTRS::ForwardQueue - Forwards the contents of an OTRS queue to a given email address.
240  
241 =head1 VERSION
242  
243 version 0.07
244  
245 =head1 SYNOPSIS
246  
247 use OTRS::ForwardQueue;
248  
249 %query = (
250 Queues => ['MyQueue'],
251 States => ['new', 'open'],
252 Locks => ['unlock'],
253 UserID => 1,
254 );
255  
256 %options = (
257 ForwardTo => 'nobody@example.org',
258 TempDir => '/tmp',
259 HistoryComment => 'Forward to other request system',
260 SMTP => 1,
261 SMTPServer => 'smtp.example.org',
262 NotifyCustomer => 1,
263 NotifyCustomerTemplate => 'notify_customer.tt',
264 TemplatesPath => '/usr/local/templates',
265 Debug => 1,
266 );
267  
268 my $fq = OTRS::ForwardQueue->new('query' => \%query, 'options' => \%options);
269  
270 $fp->process_queue();
271  
272 =head1 DESCRIPTION
273  
274 This module queries the Open Technology Real Services (OTRS) ticket management
275 system for tickets matching the query provided and then forwards these
276 tickets to an email address, closing them in OTRS.
277  
278 The following functions are provided:
279  
280 =over
281  
282 =item new(\%query, \%options)
283  
284 Produced automatically by Moose, this is the constructor for the class.
285  
286 =over
287  
288 =item \%query
289  
290 Reference to a hash which contains the query parameters. This takes the same
291 key/value pairs as the C<TicketSearch> function of C<Kernel::System::TicketSearch>,
292 except that the C<Result> value is always set to 'ARRAY'.
293  
294 =item \%options
295  
296 Required list of options which affect how the queue is processed.
297  
298 =over
299  
300 =item C<HistoryComment> (required): The comment left in the history of the ticket when it is forwarded to anther system.
301  
302 =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'>.
303  
304 =item C<ForwardTo> (required): The email address to forward tickets to.
305  
306 =item C<DisableLocking> (optional): Set to 1 to disable locking of forwarded tickets. Default behaviour is to lock tickets.
307  
308 =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.
309  
310 =item C<DisableClosing> (optional): Set to 1 to disable closing the ticket after forwarding. Default behaviour is to mark ticket as 'closed successful'.
311  
312 =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.
313  
314 =item C<SMTP> (optional): Set to 1 to use an SMTP server to send email, instead of the local MTA.
315  
316 =item C<SMTPServer> (optional): Host name or IP address of the SMTP server to use. Only effective if C<SMTP> is set to 1.
317  
318 =item C<NotifyCustomer> (optional): Set to 1 to create a new article on the ticket and notify the customer that it has been forwarded.
319  
320 =item C<NotifyCustomerTemplate> (optional): Relative filename to template for customer notification. Required if C<NotifyCustomer> is set to 1.
321  
322 =item C<TemplatesPath> (optional): Absolute path to template directory. Required if C<NotifyCustomerTemplate> is set.
323  
324 =item C<Debug> (optional): Set to 1 to print extra debugging information, such as the IDs of forwarded tickets.
325  
326 =back
327  
328 =back
329  
330 =item process_queue()
331  
332 Processes the queue based on the options passed in the constructor.
333  
334 =back
335  
336 =head1 DEPENDENCIES
337  
338 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.
339  
340 This module requires the following modules:
341  
342 =over 4
343  
344 =item * L<Moose>
345  
346 =item * L<namespace::autoclean>
347  
348 =item * L<Email::Simple>
349  
350 =item * L<Email::Sender>
351  
352 =item * L<Template> - For dynamically producing the body of emails.
353  
354 =back
355  
356 Although some of the above modules are used for optional features, all the dependencies
357 must be installed as this module will attempt to import all of them.
358  
359 You must also have the OTRS source installed and available via C<@INC>. This module has only
360 been tested with OTRS 3.2.10.
361  
362 =head1 RUNNING AS A CRON JOB
363  
364 Running a script which uses this module as a cron job may require some additional tweaks.
365 The easiest way is to create a small wrapper script to set the various library paths
366 correctly, such as the one below:
367  
368 #!/bin/bash
369
370 # Set this to the absolute path to your OTRS install, so those
371 # modules can be loaded
372 FQ_OTRS_LIB="-I/path/to/otrs"
373
374 # Comment out this line if you are not using local::lib
375 FQ_LOCAL_LIB="-I$HOME/perl5/lib/perl5"
376
377 # Change this to the path of your script
378 /usr/bin/perl "$FQ_OTRS_LIB" "$FQ_LOCAL_LIB" /path/to/script.pl
379  
380 =head1 INCOMPATIBILITIES
381  
382 None reported.
383  
384 =head1 BUGS AND LIMITATIONS
385  
386 No bugs have been reported.
387  
388 Please report any bugs through the Github issue system:
389  
390 L<https://github.com/pwaring/OTRS-ForwardQueue/issues>
391  
392 =head1 AUTHOR
393  
394 Paul Waring <paul.waring@manchester.ac.uk>
395  
396 =head1 COPYRIGHT AND LICENSE
397  
398 This software is Copyright (c) 2014 by University of Manchester.
399  
400 This is free software, licensed under:
401  
402 The GNU Affero General Public License, Version 3, November 2007
403  
404 =cut
405  
406 __END__
407  
408 # ABSTRACT: Forwards the contents of an OTRS queue to a given email address.
409