1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC
6 # <jesse@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/copyleft/gpl.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
48 package RT::Interface::Email;
55 use UNIVERSAL::require;
59 use vars qw ( @ISA @EXPORT_OK);
61 # set the version for version checking
66 # your exported package globals go here,
67 # as well as any optionally exported functions
72 &CheckForSuspiciousSender
73 &CheckForAutoGenerated
76 &ParseCcAddressesFromHead
77 &ParseSenderAddressFromHead
78 &ParseErrorsToAddressFromHead
79 &ParseAddressFromHeader
86 RT::Interface::Email - helper functions for parsing email sent to RT
90 use lib "!!RT_LIB_PATH!!";
91 use lib "!!RT_ETC_PATH!!";
93 use RT::Interface::Email qw(Gateway CreateUser);
100 ok(require RT::Interface::Email);
109 # {{{ sub CheckForLoops
114 #If this instance of RT sent it our, we don't want to take it in
115 my $RTLoop = $head->get("X-RT-Loop-Prevention") || "";
116 chomp($RTLoop); #remove that newline
117 if ( $RTLoop eq "$RT::rtname" ) {
121 # TODO: We might not trap the case where RT instance A sends a mail
122 # to RT instance B which sends a mail to ...
128 # {{{ sub CheckForSuspiciousSender
130 sub CheckForSuspiciousSender {
133 #if it's from a postmaster or mailer daemon, it's likely a bounce.
135 #TODO: better algorithms needed here - there is no standards for
136 #bounces, so it's very difficult to separate them from anything
137 #else. At the other hand, the Return-To address is only ment to be
138 #used as an error channel, we might want to put up a separate
139 #Return-To address which is treated differently.
141 #TODO: search through the whole email and find the right Ticket ID.
143 my ( $From, $junk ) = ParseSenderAddressFromHead($head);
145 if ( ( $From =~ /^mailer-daemon\@/i )
146 or ( $From =~ /^postmaster\@/i ) )
158 # {{{ sub CheckForAutoGenerated
159 sub CheckForAutoGenerated {
162 my $Precedence = $head->get("Precedence") || "";
163 if ( $Precedence =~ /^(bulk|junk)/i ) {
167 # First Class mailer uses this as a clue.
168 my $FCJunk = $head->get("X-FC-Machinegenerated") || "";
169 if ( $FCJunk =~ /^true/i ) {
178 # {{{ sub CheckForBounce
182 my $ReturnPath = $head->get("Return-path") || "";
183 return ( $ReturnPath =~ /<>/ );
190 =head2 IsRTAddress ADDRESS
192 Takes a single parameter, an email address.
193 Returns true if that address matches the $RTAddressRegexp.
194 Returns false, otherwise.
199 my $address = shift || '';
201 # Example: the following rule would tell RT not to Cc
202 # "tickets@noc.example.com"
203 if ( defined($RT::RTAddressRegexp)
204 && $address =~ /$RT::RTAddressRegexp/i )
214 # {{{ CullRTAddresses
216 =head2 CullRTAddresses ARRAY
218 Takes a single argument, an array of email addresses.
219 Returns the same array with any IsRTAddress()es weeded out.
223 sub CullRTAddresses {
224 return grep !IsRTAddress($_), @_;
232 To => $RT::OwnerEmail,
234 From => $RT::CorrespondAddress,
235 Subject => 'There has been an error',
236 Explanation => 'Unexplained error',
244 level => $args{'LogLevel'},
245 message => $args{'Explanation'}
247 # the colons are necessary to make ->build include non-standard headers
248 my $entity = MIME::Entity->build(
249 Type => "multipart/mixed",
250 From => $args{'From'},
253 Subject => $args{'Subject'},
254 'Precedence:' => 'bulk',
255 'X-RT-Loop-Prevention:' => $RT::rtname,
256 'In-Reply-To:' => $args{'MIMEObj'} ? $args{'MIMEObj'}->head->get('Message-Id') : undef
259 $entity->attach( Data => $args{'Explanation'} . "\n" );
261 my $mimeobj = $args{'MIMEObj'};
263 $mimeobj->sync_headers();
264 $entity->add_part($mimeobj);
267 if ( $args{'Attach'} ) {
268 $entity->attach( Data => $args{'Attach'}, Type => 'message/rfc822' );
272 if ( $RT::MailCommand eq 'sendmailpipe' ) {
274 "|$RT::SendmailPath $RT::SendmailBounceArguments $RT::SendmailArguments"
277 print MAIL $entity->as_string;
280 $entity->send( $RT::MailCommand, $RT::MailParams );
289 my ( $Username, $Address, $Name, $ErrorsTo, $entity ) = @_;
290 my $NewUser = RT::User->new($RT::SystemUser);
292 my ( $Val, $Message ) = $NewUser->Create(
293 Name => ( $Username || $Address ),
294 EmailAddress => $Address,
298 Comments => 'Autocreated on ticket submission'
303 # Deal with the race condition of two account creations at once
305 $NewUser->LoadByName($Username);
308 unless ( $NewUser->Id ) {
309 $NewUser->LoadByEmail($Address);
312 unless ( $NewUser->Id ) {
315 Subject => "User could not be created",
317 "User creation failed in mailgateway: $Message",
324 #Load the new user object
325 my $CurrentUser = RT::CurrentUser->new();
326 $CurrentUser->LoadByEmail($Address);
328 unless ( $CurrentUser->id ) {
329 $RT::Logger->warning(
330 "Couldn't load user '$Address'." . "giving up" );
333 Subject => "User could not be loaded",
335 "User '$Address' could not be loaded in the mail gateway",
346 # {{{ ParseCcAddressesFromHead
348 =head2 ParseCcAddressesFromHead HASHREF
350 Takes a hashref object containing QueueObj, Head and CurrentUser objects.
351 Returns a list of all email addresses in the To and Cc
352 headers b<except> the current Queue\'s email addresses, the CurrentUser\'s
353 email address and anything that the configuration sub RT::IsRTAddress matches.
357 sub ParseCcAddressesFromHead {
361 CurrentUser => undef,
367 my @ToObjs = Mail::Address->parse( $args{'Head'}->get('To') );
368 my @CcObjs = Mail::Address->parse( $args{'Head'}->get('Cc') );
370 foreach my $AddrObj ( @ToObjs, @CcObjs ) {
371 my $Address = $AddrObj->address;
372 $Address = $args{'CurrentUser'}
373 ->UserObj->CanonicalizeEmailAddress($Address);
374 next if ( $args{'CurrentUser'}->EmailAddress =~ /^\Q$Address\E$/i );
375 next if ( $args{'QueueObj'}->CorrespondAddress =~ /^\Q$Address\E$/i );
376 next if ( $args{'QueueObj'}->CommentAddress =~ /^\Q$Address\E$/i );
377 next if ( RT::EmailParser->IsRTAddress($Address) );
379 push( @Addresses, $Address );
386 # {{{ ParseSenderAdddressFromHead
388 =head2 ParseSenderAddressFromHead
390 Takes a MIME::Header object. Returns a tuple: (user@host, friendly name)
391 of the From (evaluated in order of Reply-To:, From:, Sender)
395 sub ParseSenderAddressFromHead {
398 #Figure out who's sending this message.
399 my $From = $head->get('Reply-To')
400 || $head->get('From')
401 || $head->get('Sender');
402 return ( ParseAddressFromHeader($From) );
407 # {{{ ParseErrorsToAdddressFromHead
409 =head2 ParseErrorsToAddressFromHead
411 Takes a MIME::Header object. Return a single value : user@host
412 of the From (evaluated in order of Return-path:,Errors-To:,Reply-To:,
417 sub ParseErrorsToAddressFromHead {
420 #Figure out who's sending this message.
422 foreach my $header ( 'Errors-To', 'Reply-To', 'From', 'Sender' ) {
424 # If there's a header of that name
425 my $headerobj = $head->get($header);
427 my ( $addr, $name ) = ParseAddressFromHeader($headerobj);
429 # If it's got actual useful content...
430 return ($addr) if ($addr);
437 # {{{ ParseAddressFromHeader
439 =head2 ParseAddressFromHeader ADDRESS
441 Takes an address from $head->get('Line') and returns a tuple: user@host, friendly name
445 sub ParseAddressFromHeader {
448 # Some broken mailers send: ""Vincent, Jesse"" <jesse@fsck.com>. Hate
449 $Addr =~ s/\"\"(.*?)\"\"/\"$1\"/g;
450 my @Addresses = Mail::Address->parse($Addr);
452 my ($AddrObj) = grep ref $_, @Addresses;
453 unless ( $AddrObj ) {
454 return ( undef, undef );
457 my $Name = ( $AddrObj->phrase || $AddrObj->comment || $AddrObj->address );
459 #Lets take the from and load a user object.
460 my $Address = $AddrObj->address;
462 return ( $Address, $Name );
467 # {{{ sub ParseTicketId
473 my $test_name = $RT::EmailSubjectTagRegex || qr/\Q$RT::rtname\E/i;
475 if ( $Subject =~ s/\[$test_name\s+\#(\d+)\s*\]//i ) {
477 $RT::Logger->debug("Found a ticket ID. It's $id");
486 =head2 Gateway ARGSREF
496 This performs all the "guts" of the mail rt-mailgate program, and is
497 designed to be called from the web interface with a message, user
500 Can also take an optional 'ticket' parameter; this ticket id overrides
501 any ticket id found in the subject.
507 (status code, message, optional ticket object)
509 status code is a numeric value.
511 for temporary failures, the status code should be -75
513 for permanent failures which are handled by RT, the status code
516 for succces, the status code should be 1
525 action => 'correspond',
535 # Validate the action
536 my ( $status, @actions ) = IsCorrectAction( $args{'action'} );
540 "Invalid 'action' parameter "
548 my $parser = RT::EmailParser->new();
549 $parser->SmartParseMIMEEntityFromScalar( Message => $args{'message'} );
550 my $Message = $parser->Entity();
554 To => $RT::OwnerEmail,
555 Subject => "RT Bounce: Unparseable message",
556 Explanation => "RT couldn't process the message below",
557 Attach => $args{'message'}
561 "Failed to parse this message. Something is likely badly wrong with the message"
565 my $head = $Message->head;
567 my $ErrorsTo = ParseErrorsToAddressFromHead($head);
569 my $MessageId = $head->get('Message-ID')
570 || "<no-message-id-" . time . rand(2000) . "\@.$RT::Organization>";
572 #Pull apart the subject line
573 my $Subject = $head->get('Subject') || '';
576 # {{{ Lets check for mail loops of various sorts.
577 my ($should_store_machine_generated_message, $IsALoop, $result);
578 ( $should_store_machine_generated_message, $ErrorsTo, $result, $IsALoop ) =
579 _HandleMachineGeneratedMail(
581 ErrorsTo => $ErrorsTo,
583 MessageId => $MessageId
586 # Do not pass loop messages to MailPlugins, to make sure the loop
587 # is broken, unless $RT::StoreLoops is set.
588 if ($IsALoop && !$should_store_machine_generated_message) {
589 return ( 0, $result, undef );
592 $args{'ticket'} ||= ParseTicketId($Subject);
594 $SystemTicket = RT::Ticket->new($RT::SystemUser);
595 $SystemTicket->Load( $args{'ticket'} ) if ( $args{'ticket'} ) ;
596 if ( $SystemTicket->id ) {
597 $Right = 'ReplyToTicket';
599 $Right = 'CreateTicket';
602 #Set up a queue object
603 my $SystemQueueObj = RT::Queue->new($RT::SystemUser);
604 $SystemQueueObj->Load( $args{'queue'} );
606 # We can safely have no queue of we have a known-good ticket
607 unless ( $SystemTicket->id || $SystemQueueObj->id ) {
608 return ( -75, "RT couldn't find the queue: " . $args{'queue'}, undef );
611 # Authentication Level ($AuthStat)
612 # -1 - Get out. this user has been explicitly declined
613 # 0 - User may not do anything (Not used at the moment)
615 # 2 - User is allowed to specify status updates etc. a la enhanced-mailgate
616 my ( $CurrentUser, $AuthStat, $error );
618 # Initalize AuthStat so comparisons work correctly
619 $AuthStat = -9999999;
621 push @RT::MailPlugins, "Auth::MailFrom" unless @RT::MailPlugins;
623 # if plugin returns AuthStat -2 we skip action
624 # NOTE: this is experimental API and it would be changed
625 my %skip_action = ();
627 # Since this needs loading, no matter what
628 foreach (@RT::MailPlugins) {
629 my ($Code, $NewAuthStat);
630 if ( ref($_) eq "CODE" ) {
634 $Class = "RT::Interface::Email::" . $Class
635 unless $Class =~ /^RT::Interface::Email::/;
637 do { $RT::Logger->error("Couldn't load $Class: $@"); next };
640 unless ( defined( $Code = *{ $Class . "::GetCurrentUser" }{CODE} ) ) {
641 $RT::Logger->crit( "No 'GetCurrentUser' function found in '$Class' module");
646 foreach my $action (@actions) {
647 ( $CurrentUser, $NewAuthStat ) = $Code->(
649 RawMessageRef => \$args{'message'},
650 CurrentUser => $CurrentUser,
651 AuthLevel => $AuthStat,
653 Ticket => $SystemTicket,
654 Queue => $SystemQueueObj
657 # You get the highest level of authentication you were assigned, unless you get the magic -1
658 # If a module returns a "-1" then we discard the ticket, so.
659 $AuthStat = $NewAuthStat
660 if ( $NewAuthStat > $AuthStat or $NewAuthStat == -1 or $NewAuthStat == -2 );
662 last if $AuthStat == -1;
663 $skip_action{$action}++ if $AuthStat == -2;
666 # strip actions we should skip
667 @actions = grep !$skip_action{$_}, @actions if $AuthStat == -2;
668 last unless @actions;
670 last if $AuthStat == -1;
672 # {{{ If authentication fails and no new user was created, get out.
673 if ( !$CurrentUser || !$CurrentUser->id || $AuthStat == -1 ) {
675 # If the plugins refused to create one, they lose.
676 unless ( $AuthStat == -1 ) {
677 _NoAuthorizedUserFound(
680 Requestor => $ErrorsTo,
681 Queue => $args{'queue'}
685 return ( 0, "Could not load a valid user", undef );
688 # If we got a user, but they don't have the right to say things
689 if ( $AuthStat == 0 ) {
692 Subject => "Permission Denied",
694 "You do not have permission to communicate with RT",
699 "$ErrorsTo tried to submit a message to "
701 . " without permission.",
707 unless ($should_store_machine_generated_message) {
708 return ( 0, $result, undef );
711 # if plugin's updated SystemTicket then update arguments
712 $args{'ticket'} = $SystemTicket->Id if $SystemTicket && $SystemTicket->Id;
714 my $Ticket = RT::Ticket->new($CurrentUser);
716 if ( !$args{'ticket'} && grep /^(comment|correspond)$/, @actions )
720 my @Requestors = ( $CurrentUser->id );
722 if ($RT::ParseNewMessageForTicketCcs) {
723 @Cc = ParseCcAddressesFromHead(
725 CurrentUser => $CurrentUser,
726 QueueObj => $SystemQueueObj
730 my ( $id, $Transaction, $ErrStr ) = $Ticket->Create(
731 Queue => $SystemQueueObj->Id,
733 Requestor => \@Requestors,
740 Subject => "Ticket creation failed: $Subject",
741 Explanation => $ErrStr,
744 return ( 0, "Ticket creation failed: $ErrStr", $Ticket );
747 # strip comments&corresponds from the actions we don't need
748 # to record them if we've created the ticket just now
749 @actions = grep !/^(comment|correspond)$/, @actions;
750 $args{'ticket'} = $id;
752 } elsif ( $args{'ticket'} ) {
754 $Ticket->Load( $args{'ticket'} );
755 unless ( $Ticket->Id ) {
756 my $error = "Could not find a ticket with id " . $args{'ticket'};
759 Subject => "Message not recorded: $Subject",
760 Explanation => $error,
764 return ( 0, $error );
766 $args{'ticket'} = $Ticket->id;
768 return ( 1, "Success", $Ticket );
772 foreach my $action (@actions) {
774 # If the action is comment, add a comment.
775 if ( $action =~ /^(?:comment|correspond)$/i ) {
776 my $method = ucfirst lc $action;
777 my ( $status, $msg ) = $Ticket->$method( MIMEObj => $Message );
780 #Warn the sender that we couldn't actually submit the comment.
783 Subject => "Message not recorded: $Subject",
787 return ( 0, "Message not recorded: $msg", $Ticket );
789 } elsif ($RT::UnsafeEmailCommands) {
790 my ( $status, $msg ) = _RunUnsafeAction(
792 ErrorsTo => $ErrorsTo,
795 CurrentUser => $CurrentUser,
797 return ($status, $msg, $Ticket) unless $status == 1;
800 return ( 1, "Success", $Ticket );
803 sub _RunUnsafeAction {
809 CurrentUser => undef,
813 if ( $args{'Action'} =~ /^take$/i ) {
814 my ( $status, $msg ) = $args{'Ticket'}->SetOwner( $args{'CurrentUser'}->id );
817 To => $args{'ErrorsTo'},
818 Subject => "Ticket not taken",
820 MIMEObj => $args{'Message'}
822 return ( 0, "Ticket not taken" );
824 } elsif ( $args{'Action'} =~ /^resolve$/i ) {
825 my ( $status, $msg ) = $args{'Ticket'}->SetStatus('resolved');
828 #Warn the sender that we couldn't actually submit the comment.
830 To => $args{'ErrorsTo'},
831 Subject => "Ticket not resolved",
833 MIMEObj => $args{'Message'}
835 return ( 0, "Ticket not resolved" );
838 return ( 0, "Not supported unsafe action $args{'Action'}", $args{'Ticket'} );
840 return ( 1, "Success" );
843 =head2 _NoAuthorizedUserFound
845 Emails the RT Owner and the requestor when the auth plugins return "No auth user found"
849 sub _NoAuthorizedUserFound {
858 # Notify the RT Admin of the failure.
860 To => $RT::OwnerEmail,
861 Subject => "Could not load a valid user",
862 Explanation => <<EOT,
863 RT could not load a valid user, and RT's configuration does not allow
864 for the creation of a new user for this email (@{[$args{Requestor}]}).
866 You might need to grant 'Everyone' the right '@{[$args{Right}]}' for the
867 queue @{[$args{'Queue'}]}.
870 MIMEObj => $args{'Message'},
874 # Also notify the requestor that his request has been dropped.
875 if ($args{'Requestor'} ne $RT::OwnerEmail) {
877 To => $args{'Requestor'},
878 Subject => "Could not load a valid user",
879 Explanation => <<EOT,
880 RT could not load a valid user, and RT's configuration does not allow
881 for the creation of a new user for your email.
884 MIMEObj => $args{'Message'},
890 =head2 _HandleMachineGeneratedMail
897 Checks the message to see if it's a bounce, if it looks like a loop, if it's autogenerated, etc.
898 Returns a triple of ("Should we continue (boolean)", "New value for $ErrorsTo", "Status message",
899 "This message appears to be a loop (boolean)" );
903 sub _HandleMachineGeneratedMail {
904 my %args = ( Message => undef, ErrorsTo => undef, Subject => undef, MessageId => undef, @_ );
905 my $head = $args{'Message'}->head;
906 my $ErrorsTo = $args{'ErrorsTo'};
908 my $IsBounce = CheckForBounce($head);
910 my $IsAutoGenerated = CheckForAutoGenerated($head);
912 my $IsSuspiciousSender = CheckForSuspiciousSender($head);
914 my $IsALoop = CheckForLoops($head);
916 my $SquelchReplies = 0;
918 #If the message is autogenerated, we need to know, so we can not
919 # send mail to the sender
920 if ( $IsBounce || $IsSuspiciousSender || $IsAutoGenerated || $IsALoop ) {
922 $ErrorsTo = $RT::OwnerEmail;
925 # Warn someone if it's a loop, before we drop it on the ground
927 $RT::Logger->crit("RT Received mail (".$args{MessageId}.") from itself.");
929 #Should we mail it to RTOwner?
930 if ($RT::LoopsToRTOwner) {
932 To => $RT::OwnerEmail,
933 Subject => "RT Bounce: ".$args{'Subject'},
934 Explanation => "RT thinks this message may be a bounce",
935 MIMEObj => $args{Message}
939 #Do we actually want to store it?
940 return ( 0, $ErrorsTo, "Message Bounced", $IsALoop ) unless ($RT::StoreLoops);
943 # Squelch replies if necessary
944 # Don't let the user stuff the RT-Squelch-Replies-To header.
945 if ( $head->get('RT-Squelch-Replies-To') ) {
947 'RT-Relocated-Squelch-Replies-To',
948 $head->get('RT-Squelch-Replies-To')
950 $head->delete('RT-Squelch-Replies-To');
953 if ($SquelchReplies) {
955 # Squelch replies to the sender, and also leave a clue to
956 # allow us to squelch ALL outbound messages. This way we
957 # can punt the logic of "what to do when we get a bounce"
958 # to the scrip. We might want to notify nobody. Or just
959 # the RT Owner. Or maybe all Privileged watchers.
960 my ( $Sender, $junk ) = ParseSenderAddressFromHead($head);
961 $head->add( 'RT-Squelch-Replies-To', $Sender );
962 $head->add( 'RT-DetectedAutoGenerated', 'true' );
964 return ( 1, $ErrorsTo, "Handled machine detection", $IsALoop );
967 =head2 IsCorrectAction
969 Returns a list of valid actions we've found for this message
973 sub IsCorrectAction {
975 my @actions = grep $_, split /-/, $action;
976 return ( 0, '(no value)' ) unless @actions;
978 return ( 0, $_ ) unless /^(?:comment|correspond|take|resolve)$/;
980 return ( 1, @actions );
983 eval "require RT::Interface::Email_Vendor";
984 die $@ if ( $@ && $@ !~ qr{^Can't locate RT/Interface/Email_Vendor.pm} );
985 eval "require RT::Interface::Email_Local";
986 die $@ if ( $@ && $@ !~ qr{^Can't locate RT/Interface/Email_Local.pm} );