import of rt 3.0.9
[freeside.git] / rt / lib / RT / Interface / Email.pm
1 # BEGIN LICENSE BLOCK
2
3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4
5 # (Except where explictly superceded by other copyright notices)
6
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
10 # from www.gnu.org.
11
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
21
22
23 # END LICENSE BLOCK
24 package RT::Interface::Email;
25
26 use strict;
27 use Mail::Address;
28 use MIME::Entity;
29 use RT::EmailParser;
30 use File::Temp;
31
32 BEGIN {
33     use Exporter ();
34     use vars qw ($VERSION  @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
35     
36     # set the version for version checking
37     $VERSION = do { my @r = (q$Revision: 1.1.1.2 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
38     
39     @ISA         = qw(Exporter);
40     
41     # your exported package globals go here,
42     # as well as any optionally exported functions
43     @EXPORT_OK   = qw(
44               &CreateUser
45                       &GetMessageContent
46                       &CheckForLoops 
47                       &CheckForSuspiciousSender
48                       &CheckForAutoGenerated 
49                       &MailError 
50                       &ParseCcAddressesFromHead
51                       &ParseSenderAddressFromHead 
52                       &ParseErrorsToAddressFromHead
53                       &ParseAddressFromHeader
54               &Gateway);
55
56 }
57
58 =head1 NAME
59
60   RT::Interface::CLI - helper functions for creating a commandline RT interface
61
62 =head1 SYNOPSIS
63
64   use lib "!!RT_LIB_PATH!!";
65   use lib "!!RT_ETC_PATH!!";
66
67   use RT::Interface::Email  qw(Gateway CreateUser);
68
69 =head1 DESCRIPTION
70
71
72 =begin testing
73
74 ok(require RT::Interface::Email);
75
76 =end testing
77
78
79 =head1 METHODS
80
81 =cut
82
83
84 # {{{ sub CheckForLoops 
85
86 sub CheckForLoops  {
87     my $head = shift;
88     
89     #If this instance of RT sent it our, we don't want to take it in
90     my $RTLoop = $head->get("X-RT-Loop-Prevention") || "";
91     chomp ($RTLoop); #remove that newline
92     if ($RTLoop eq "$RT::rtname") {
93         return (1);
94     }
95     
96     # TODO: We might not trap the case where RT instance A sends a mail
97     # to RT instance B which sends a mail to ...
98     return (undef);
99 }
100
101 # }}}
102
103 # {{{ sub CheckForSuspiciousSender
104
105 sub CheckForSuspiciousSender {
106     my $head = shift;
107
108     #if it's from a postmaster or mailer daemon, it's likely a bounce.
109     
110     #TODO: better algorithms needed here - there is no standards for
111     #bounces, so it's very difficult to separate them from anything
112     #else.  At the other hand, the Return-To address is only ment to be
113     #used as an error channel, we might want to put up a separate
114     #Return-To address which is treated differently.
115     
116     #TODO: search through the whole email and find the right Ticket ID.
117
118     my ($From, $junk) = ParseSenderAddressFromHead($head);
119     
120     if (($From =~ /^mailer-daemon/i) or
121         ($From =~ /^postmaster/i)){
122         return (1);
123         
124     }
125     
126     return (undef);
127
128 }
129
130 # }}}
131
132 # {{{ sub CheckForAutoGenerated
133 sub CheckForAutoGenerated {
134     my $head = shift;
135     
136     my $Precedence = $head->get("Precedence") || "" ;
137     if ($Precedence =~ /^(bulk|junk)/i) {
138         return (1);
139     }
140     else {
141         return (0);
142     }
143 }
144
145 # }}}
146
147
148 # {{{ sub MailError 
149 sub MailError {
150     my %args = (To => $RT::OwnerEmail,
151                 Bcc => undef,
152                 From => $RT::CorrespondAddress,
153                 Subject => 'There has been an error',
154                 Explanation => 'Unexplained error',
155                 MIMEObj => undef,
156         Attach => undef,
157                 LogLevel => 'crit',
158                 @_);
159
160
161     $RT::Logger->log(level => $args{'LogLevel'}, 
162                      message => $args{'Explanation'}
163                     );
164     my $entity = MIME::Entity->build( Type  =>"multipart/mixed",
165                                       From => $args{'From'},
166                                       Bcc => $args{'Bcc'},
167                                       To => $args{'To'},
168                                       Subject => $args{'Subject'},
169                                       'X-RT-Loop-Prevention' => $RT::rtname,
170                                     );
171
172     $entity->attach(  Data => $args{'Explanation'}."\n");
173     
174     my $mimeobj = $args{'MIMEObj'};
175     if ($mimeobj) {
176         $mimeobj->sync_headers();
177         $entity->add_part($mimeobj);
178     }
179    
180     if ($args{'Attach'}) {
181         $entity->attach(Data => $args{'Attach'}, Type => 'message/rfc822');
182
183     }
184
185  
186     if ($RT::MailCommand eq 'sendmailpipe') {
187         open (MAIL, "|$RT::SendmailPath $RT::SendmailArguments") || return(0);
188         print MAIL $entity->as_string;
189         close(MAIL);
190     }
191     else {
192         $entity->send($RT::MailCommand, $RT::MailParams);
193     }
194 }
195
196 # }}}
197
198 # {{{ Create User
199
200 sub CreateUser {
201     my ($Username, $Address, $Name, $ErrorsTo, $entity) = @_;
202     my $NewUser = RT::User->new($RT::SystemUser);
203
204     my ($Val, $Message) = 
205       $NewUser->Create(Name => ($Username || $Address),
206                        EmailAddress => $Address,
207                        RealName => $Name,
208                        Password => undef,
209                        Privileged => 0,
210                        Comments => 'Autocreated on ticket submission'
211                       );
212     
213     unless ($Val) {
214         
215         # Deal with the race condition of two account creations at once
216         #
217         if ($Username) {
218             $NewUser->LoadByName($Username);
219         }
220         
221         unless ($NewUser->Id) {
222             $NewUser->LoadByEmail($Address);
223         }
224         
225         unless ($NewUser->Id) {  
226             MailError( To => $ErrorsTo,
227                        Subject => "User could not be created",
228                        Explanation => "User creation failed in mailgateway: $Message",
229                        MIMEObj => $entity,
230                        LogLevel => 'crit'
231                      );
232         }
233     }
234
235     #Load the new user object
236     my $CurrentUser = RT::CurrentUser->new();
237     $CurrentUser->LoadByEmail($Address);
238
239     unless ($CurrentUser->id) {
240             $RT::Logger->warning("Couldn't load user '$Address'.".  "giving up");
241                 MailError( To => $ErrorsTo,
242                            Subject => "User could not be loaded",
243                            Explanation => "User  '$Address' could not be loaded in the mail gateway",
244                            MIMEObj => $entity,
245                            LogLevel => 'crit'
246                      );
247     }
248
249     return $CurrentUser;
250 }
251 # }}}       
252 # {{{ ParseCcAddressesFromHead 
253
254 =head2 ParseCcAddressesFromHead HASHREF
255
256 Takes a hashref object containing QueueObj, Head and CurrentUser objects.
257 Returns a list of all email addresses in the To and Cc 
258 headers b<except> the current Queue\'s email addresses, the CurrentUser\'s 
259 email address  and anything that the configuration sub RT::IsRTAddress matches.
260
261 =cut
262   
263 sub ParseCcAddressesFromHead {
264     my %args = ( Head => undef,
265                  QueueObj => undef,
266                  CurrentUser => undef,
267                  @_ );
268     
269     my (@Addresses);
270         
271     my @ToObjs = Mail::Address->parse($args{'Head'}->get('To'));
272     my @CcObjs = Mail::Address->parse($args{'Head'}->get('Cc'));
273     
274     foreach my $AddrObj (@ToObjs, @CcObjs) {
275         my $Address = $AddrObj->address;
276         $Address = $args{'CurrentUser'}->UserObj->CanonicalizeEmailAddress($Address);
277         next if ($args{'CurrentUser'}->EmailAddress =~ /^$Address$/i);
278         next if ($args{'QueueObj'}->CorrespondAddress =~ /^$Address$/i);
279         next if ($args{'QueueObj'}->CommentAddress =~ /^$Address$/i);
280         next if (RT::EmailParser::IsRTAddress(undef, $Address));
281         
282         push (@Addresses, $Address);
283     }
284     return (@Addresses);
285 }
286
287
288 # }}}
289
290 # {{{ ParseSenderAdddressFromHead
291
292 =head2 ParseSenderAddressFromHead
293
294 Takes a MIME::Header object. Returns a tuple: (user@host, friendly name) 
295 of the From (evaluated in order of Reply-To:, From:, Sender)
296
297 =cut
298
299 sub ParseSenderAddressFromHead {
300     my $head = shift;
301     #Figure out who's sending this message.
302     my $From = $head->get('Reply-To') || 
303       $head->get('From') || 
304         $head->get('Sender');
305     return (ParseAddressFromHeader($From));
306 }
307 # }}}
308
309 # {{{ ParseErrorsToAdddressFromHead
310
311 =head2 ParseErrorsToAddressFromHead
312
313 Takes a MIME::Header object. Return a single value : user@host
314 of the From (evaluated in order of Errors-To:,Reply-To:, From:, Sender)
315
316 =cut
317
318 sub ParseErrorsToAddressFromHead {
319     my $head = shift;
320     #Figure out who's sending this message.
321
322     foreach my $header ('Errors-To' , 'Reply-To', 'From', 'Sender' ) {
323         # If there's a header of that name
324         my $headerobj = $head->get($header);
325         if ($headerobj) {
326                 my ($addr, $name ) = ParseAddressFromHeader($headerobj);
327                 # If it's got actual useful content...
328                 return ($addr) if ($addr);
329         }
330     }
331 }
332 # }}}
333
334 # {{{ ParseAddressFromHeader
335
336 =head2 ParseAddressFromHeader ADDRESS
337
338 Takes an address from $head->get('Line') and returns a tuple: user@host, friendly name
339
340 =cut
341
342
343 sub ParseAddressFromHeader{
344     my $Addr = shift;
345     
346     my @Addresses = Mail::Address->parse($Addr);
347     
348     my $AddrObj = $Addresses[0];
349
350     unless (ref($AddrObj)) {
351         return(undef,undef);
352     }
353  
354     my $Name =  ($AddrObj->phrase || $AddrObj->comment || $AddrObj->address);
355     
356     #Lets take the from and load a user object.
357     my $Address = $AddrObj->address;
358
359     return ($Address, $Name);
360 }
361 # }}}
362
363
364
365 =head2 Gateway ARGSREF
366
367
368 Takes parameters:
369
370     action
371     queue
372     message
373
374
375 This performs all the "guts" of the mail rt-mailgate program, and is
376 designed to be called from the web interface with a message, user
377 object, and so on.
378
379 Returns:
380
381     An array of:
382     
383     (status code, message, optional ticket object)
384
385     status code is a numeric value.
386
387     for temporary failures, status code should be -75
388
389     for permanent failures which are handled by RT, status code should be 0
390     
391     for succces, the status code should be 1
392
393
394
395 =cut
396
397 sub Gateway {
398     my $argsref = shift;
399
400     my %args = %$argsref;
401
402     # Set some reasonable defaults
403     $args{'action'} = 'correspond' unless ( $args{'action'} );
404     $args{'queue'}  = '1'          unless ( $args{'queue'} );
405
406     # Validate the action
407     unless ( $args{'action'} =~ /^(comment|correspond|action)$/ ) {
408
409         # Can't safely loc this. What object do we loc around?
410         $RT::Logger->crit("Mail gateway called with an invalid action paramenter '".$args{'action'}."' for queue '".$args{'queue'}."'");
411
412         return ( -75, "Invalid 'action' parameter", undef );
413     }
414
415     my $parser = RT::EmailParser->new();
416     my ( $fh, $temp_file );
417     for ( 1 .. 10 ) {
418
419         # on NFS and NTFS, it is possible that tempfile() conflicts
420         # with other processes, causing a race condition. we try to
421         # accommodate this by pausing and retrying.
422         last if ( $fh, $temp_file ) = eval { File::Temp::tempfile(undef, UNLINK => 0) };
423         sleep 1;
424     }
425     if ($fh) {
426         binmode $fh;    #thank you, windows
427         $fh->autoflush(1);
428         print $fh $args{'message'};
429         close($fh);
430
431         if ( -f $temp_file ) {
432             $parser->ParseMIMEEntityFromFile($temp_file);
433             File::Temp::unlink0( $fh, $temp_file );
434             if ($parser->Entity) {
435                 delete $args{'message'};
436             }
437         }
438
439     }
440
441     #If for some reason we weren't able to parse the message using a temp file 
442     # try it with a scalar
443     if ($args{'message'}) {
444         $parser->ParseMIMEEntityFromScalar($args{'message'});
445
446     } 
447
448     if (!$parser->Entity()) {
449         MailError(
450             To          => $RT::OwnerEmail,
451             Subject     => "RT Bounce: Unparseable message",
452             Explanation => "RT couldn't process the message below",
453             Attach     => $args{'message'}
454         );
455
456         return(0,"Failed to parse this message. Something is likely badly wrong with the message");
457     }
458
459     my $Message = $parser->Entity();
460     my $head    = $Message->head;
461
462     my ( $CurrentUser, $AuthStat, $status, $error );
463
464     # Initalize AuthStat so comparisons work correctly
465     $AuthStat = -9999999;
466
467     my $ErrorsTo = ParseErrorsToAddressFromHead($head);
468
469     my $MessageId = $head->get('Message-Id')
470       || "<no-message-id-" . time . rand(2000) . "\@.$RT::Organization>";
471
472     #Pull apart the subject line
473     my $Subject = $head->get('Subject') || '';
474     chomp $Subject;
475
476     $args{'ticket'} ||= $parser->ParseTicketId($Subject);
477
478     my $SystemTicket;
479     if ( $args{'ticket'} ) {
480         $SystemTicket = RT::Ticket->new($RT::SystemUser);
481         $SystemTicket->Load( $args{'ticket'} );
482     }
483
484     #Set up a queue object
485     my $SystemQueueObj = RT::Queue->new($RT::SystemUser);
486     $SystemQueueObj->Load( $args{'queue'} );
487
488     # We can safely have no queue of we have a known-good ticket
489     unless ( $args{'ticket'} || $SystemQueueObj->id ) {
490         return ( -75, "RT couldn't find the queue: " . $args{'queue'}, undef );
491     }
492
493     # Authentication Level
494     # -1 - Get out.  this user has been explicitly declined
495     # 0 - User may not do anything (Not used at the moment)
496     # 1 - Normal user
497     # 2 - User is allowed to specify status updates etc. a la enhanced-mailgate
498
499     push @RT::MailPlugins, "Auth::MailFrom" unless @RT::MailPlugins;
500
501     # Since this needs loading, no matter what
502
503     for (@RT::MailPlugins) {
504         my $Code;
505         my $NewAuthStat;
506         if ( ref($_) eq "CODE" ) {
507             $Code = $_;
508         }
509         else {
510             $_ = "RT::Interface::Email::$_" unless /^RT::Interface::Email::/;
511             eval "require $_;";
512             if ($@) {
513                 die ("Couldn't load module $_: $@");
514                 next;
515             }
516             no strict 'refs';
517             if ( !defined( $Code = *{ $_ . "::GetCurrentUser" }{CODE} ) ) {
518                 die ("No GetCurrentUser code found in $_ module");
519                 next;
520             }
521         }
522
523         ( $CurrentUser, $NewAuthStat ) = $Code->(
524             Message     => $Message,
525             CurrentUser => $CurrentUser,
526             AuthLevel   => $AuthStat,
527             Action      => $args{'action'},
528             Ticket      => $SystemTicket,
529             Queue       => $SystemQueueObj
530         );
531
532         # If a module returns a "-1" then we discard the ticket, so.
533         $AuthStat = -1 if $NewAuthStat == -1;
534
535         # You get the highest level of authentication you were assigned.
536         $AuthStat = $NewAuthStat if $NewAuthStat > $AuthStat;
537         last if $AuthStat == -1;
538     }
539
540     # {{{ If authentication fails and no new user was created, get out.
541     if ( !$CurrentUser or !$CurrentUser->Id or $AuthStat == -1 ) {
542
543         # If the plugins refused to create one, they lose.
544         unless ( $AuthStat == -1 ) {
545
546             # Notify the RT Admin of the failure.
547             # XXX Should this be configurable?
548             MailError(
549                 To          => $RT::OwnerEmail,
550                 Subject     => "Could not load a valid user",
551                 Explanation => <<EOT,
552 RT could not load a valid user, and RT's configuration does not allow
553 for the creation of a new user for this email ($ErrorsTo).
554
555 You might need to grant 'Everyone' the right 'CreateTicket' for the
556 queue @{[$args{'queue'}]}.
557
558 EOT
559                 MIMEObj  => $Message,
560                 LogLevel => 'error'
561             );
562
563             # Also notify the requestor that his request has been dropped.
564             MailError(
565                 To          => $ErrorsTo,
566                 Subject     => "Could not load a valid user",
567                 Explanation => <<EOT,
568 RT could not load a valid user, and RT's configuration does not allow
569 for the creation of a new user for your email.
570
571 EOT
572                 MIMEObj  => $Message,
573                 LogLevel => 'error'
574             );
575         }
576         return ( 0, "Could not load a valid user", undef );
577     }
578
579     # }}}
580
581     # {{{ Lets check for mail loops of various sorts.
582     my $IsAutoGenerated = CheckForAutoGenerated($head);
583
584     my $IsSuspiciousSender = CheckForSuspiciousSender($head);
585
586     my $IsALoop = CheckForLoops($head);
587
588     my $SquelchReplies = 0;
589
590     #If the message is autogenerated, we need to know, so we can not
591     # send mail to the sender
592     if ( $IsSuspiciousSender || $IsAutoGenerated || $IsALoop ) {
593         $SquelchReplies = 1;
594         $ErrorsTo       = $RT::OwnerEmail;
595     }
596
597     # }}}
598
599     # {{{ Drop it if it's disallowed
600     if ( $AuthStat == 0 ) {
601         MailError(
602             To          => $ErrorsTo,
603             Subject     => "Permission Denied",
604             Explanation => "You do not have permission to communicate with RT",
605             MIMEObj     => $Message
606         );
607     }
608
609     # }}}
610     # {{{ Warn someone  if it's a loop
611
612     # Warn someone if it's a loop, before we drop it on the ground
613     if ($IsALoop) {
614         $RT::Logger->crit("RT Recieved mail ($MessageId) from itself.");
615
616         #Should we mail it to RTOwner?
617         if ($RT::LoopsToRTOwner) {
618             MailError(
619                 To          => $RT::OwnerEmail,
620                 Subject     => "RT Bounce: $Subject",
621                 Explanation => "RT thinks this message may be a bounce",
622                 MIMEObj     => $Message
623             );
624
625             #Do we actually want to store it?
626             return ( 0, "Message Bounced", undef ) unless ($RT::StoreLoops);
627         }
628     }
629
630     # }}}
631
632     # {{{ Squelch replies if necessary
633     # Don't let the user stuff the RT-Squelch-Replies-To header.
634     if ( $head->get('RT-Squelch-Replies-To') ) {
635         $head->add(
636             'RT-Relocated-Squelch-Replies-To',
637             $head->get('RT-Squelch-Replies-To')
638         );
639         $head->delete('RT-Squelch-Replies-To');
640     }
641
642     if ($SquelchReplies) {
643         ## TODO: This is a hack.  It should be some other way to
644         ## indicate that the transaction should be "silent".
645
646         my ( $Sender, $junk ) = ParseSenderAddressFromHead($head);
647         $head->add( 'RT-Squelch-Replies-To', $Sender );
648     }
649
650     # }}}
651
652     my $Ticket = RT::Ticket->new($CurrentUser);
653
654     # {{{ If we don't have a ticket Id, we're creating a new ticket
655     if ( !$args{'ticket'} ) {
656
657         # {{{ Create a new ticket
658
659         my @Cc;
660         my @Requestors = ( $CurrentUser->id );
661
662         if ($RT::ParseNewMessageForTicketCcs) {
663             @Cc = ParseCcAddressesFromHead(
664                 Head        => $head,
665                 CurrentUser => $CurrentUser,
666                 QueueObj    => $SystemQueueObj
667             );
668         }
669
670         my ( $id, $Transaction, $ErrStr ) = $Ticket->Create(
671             Queue     => $SystemQueueObj->Id,
672             Subject   => $Subject,
673             Requestor => \@Requestors,
674             Cc        => \@Cc,
675             MIMEObj   => $Message
676         );
677         if ( $id == 0 ) {
678             MailError(
679                 To          => $ErrorsTo,
680                 Subject     => "Ticket creation failed",
681                 Explanation => $ErrStr,
682                 MIMEObj     => $Message
683             );
684             $RT::Logger->error("Create failed: $id / $Transaction / $ErrStr ");
685             return ( 0, "Ticket creation failed", $Ticket );
686         }
687
688         # }}}
689     }
690
691     # }}}
692
693     #   If the action is comment, add a comment.
694     elsif ( $args{'action'} =~ /^(comment|correspond)$/i ) {
695         $Ticket->Load( $args{'ticket'} );
696         unless ( $Ticket->Id ) {
697             my $message = "Could not find a ticket with id " . $args{'ticket'};
698             MailError(
699                 To          => $ErrorsTo,
700                 Subject     => "Message not recorded",
701                 Explanation => $message,
702                 MIMEObj     => $Message
703             );
704
705             return ( 0, $message );
706         }
707
708         my ( $status, $msg );
709         if ( $args{'action'} =~ /^correspond$/ ) {
710             ( $status, $msg ) = $Ticket->Correspond( MIMEObj => $Message );
711         }
712         else {
713             ( $status, $msg ) = $Ticket->Comment( MIMEObj => $Message );
714         }
715         unless ($status) {
716
717             #Warn the sender that we couldn't actually submit the comment.
718             MailError(
719                 To          => $ErrorsTo,
720                 Subject     => "Message not recorded",
721                 Explanation => $msg,
722                 MIMEObj     => $Message
723             );
724             return ( 0, "Message not recorded", $Ticket );
725         }
726     }
727
728     else {
729
730         #Return mail to the sender with an error
731         MailError(
732             To          => $ErrorsTo,
733             Subject     => "RT Configuration error",
734             Explanation => "'"
735               . $args{'action'}
736               . "' not a recognized action."
737               . " Your RT administrator has misconfigured "
738               . "the mail aliases which invoke RT",
739             MIMEObj => $Message
740         );
741         $RT::Logger->crit( $args{'action'} . " type unknown for $MessageId" );
742         return (
743             -75,
744             "Configuration error: "
745               . $args{'action'}
746               . " not a recognized action",
747             $Ticket
748         );
749
750     }
751
752     return ( 1, "Success", $Ticket );
753 }
754
755 eval "require RT::Interface::Email_Vendor";
756 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Email_Vendor.pm});
757 eval "require RT::Interface::Email_Local";
758 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Email_Local.pm});
759
760 1;