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