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