1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2005 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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
45 # END BPS TAGGED BLOCK }}}
46 package RT::Interface::Email;
56 use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
58 # set the version for version checking
59 $VERSION = do { my @r = (q$Revision: 1.1.1.6 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
63 # your exported package globals go here,
64 # as well as any optionally exported functions
69 &CheckForSuspiciousSender
70 &CheckForAutoGenerated
73 &ParseCcAddressesFromHead
74 &ParseSenderAddressFromHead
75 &ParseErrorsToAddressFromHead
76 &ParseAddressFromHeader
83 RT::Interface::Email - helper functions for parsing email sent to RT
87 use lib "!!RT_LIB_PATH!!";
88 use lib "!!RT_ETC_PATH!!";
90 use RT::Interface::Email qw(Gateway CreateUser);
97 ok(require RT::Interface::Email);
107 # {{{ sub CheckForLoops
112 #If this instance of RT sent it our, we don't want to take it in
113 my $RTLoop = $head->get("X-RT-Loop-Prevention") || "";
114 chomp ($RTLoop); #remove that newline
115 if ($RTLoop eq "$RT::rtname") {
119 # TODO: We might not trap the case where RT instance A sends a mail
120 # to RT instance B which sends a mail to ...
126 # {{{ sub CheckForSuspiciousSender
128 sub CheckForSuspiciousSender {
131 #if it's from a postmaster or mailer daemon, it's likely a bounce.
133 #TODO: better algorithms needed here - there is no standards for
134 #bounces, so it's very difficult to separate them from anything
135 #else. At the other hand, the Return-To address is only ment to be
136 #used as an error channel, we might want to put up a separate
137 #Return-To address which is treated differently.
139 #TODO: search through the whole email and find the right Ticket ID.
141 my ($From, $junk) = ParseSenderAddressFromHead($head);
143 if (($From =~ /^mailer-daemon\@/i) or
144 ($From =~ /^postmaster\@/i)){
155 # {{{ sub CheckForAutoGenerated
156 sub CheckForAutoGenerated {
159 my $Precedence = $head->get("Precedence") || "" ;
160 if ($Precedence =~ /^(bulk|junk)/i) {
164 # First Class mailer uses this as a clue.
165 my $FCJunk = $head->get("X-FC-Machinegenerated") || "";
166 if ($FCJunk =~ /^true/i) {
175 # {{{ sub CheckForBounce
179 my $ReturnPath = $head->get("Return-path") || "" ;
180 return ($ReturnPath =~ /<>/);
187 =head2 IsRTAddress ADDRESS
189 Takes a single parameter, an email address.
190 Returns true if that address matches the $RTAddressRegexp.
191 Returns false, otherwise.
196 my $address = shift || '';
198 # Example: the following rule would tell RT not to Cc
199 # "tickets@noc.example.com"
200 if ( defined($RT::RTAddressRegexp) &&
201 $address =~ /$RT::RTAddressRegexp/i ) {
210 # {{{ CullRTAddresses
212 =head2 CullRTAddresses ARRAY
214 Takes a single argument, an array of email addresses.
215 Returns the same array with any IsRTAddress()es weeded out.
219 sub CullRTAddresses {
220 return (grep { IsRTAddress($_) } @_);
227 my %args = (To => $RT::OwnerEmail,
229 From => $RT::CorrespondAddress,
230 Subject => 'There has been an error',
231 Explanation => 'Unexplained error',
238 $RT::Logger->log(level => $args{'LogLevel'},
239 message => $args{'Explanation'}
241 my $entity = MIME::Entity->build( Type =>"multipart/mixed",
242 From => $args{'From'},
245 Subject => $args{'Subject'},
246 Precedence => 'bulk',
247 'X-RT-Loop-Prevention' => $RT::rtname,
250 $entity->attach( Data => $args{'Explanation'}."\n");
252 my $mimeobj = $args{'MIMEObj'};
254 $mimeobj->sync_headers();
255 $entity->add_part($mimeobj);
258 if ($args{'Attach'}) {
259 $entity->attach(Data => $args{'Attach'}, Type => 'message/rfc822');
263 if ($RT::MailCommand eq 'sendmailpipe') {
264 open (MAIL, "|$RT::SendmailPath $RT::SendmailBounceArguments $RT::SendmailArguments") || return(0);
265 print MAIL $entity->as_string;
269 $entity->send($RT::MailCommand, $RT::MailParams);
278 my ($Username, $Address, $Name, $ErrorsTo, $entity) = @_;
279 my $NewUser = RT::User->new($RT::SystemUser);
281 my ($Val, $Message) =
282 $NewUser->Create(Name => ($Username || $Address),
283 EmailAddress => $Address,
287 Comments => 'Autocreated on ticket submission'
292 # Deal with the race condition of two account creations at once
295 $NewUser->LoadByName($Username);
298 unless ($NewUser->Id) {
299 $NewUser->LoadByEmail($Address);
302 unless ($NewUser->Id) {
303 MailError( To => $ErrorsTo,
304 Subject => "User could not be created",
305 Explanation => "User creation failed in mailgateway: $Message",
312 #Load the new user object
313 my $CurrentUser = RT::CurrentUser->new();
314 $CurrentUser->LoadByEmail($Address);
316 unless ($CurrentUser->id) {
317 $RT::Logger->warning("Couldn't load user '$Address'.". "giving up");
318 MailError( To => $ErrorsTo,
319 Subject => "User could not be loaded",
320 Explanation => "User '$Address' could not be loaded in the mail gateway",
330 # {{{ ParseCcAddressesFromHead
332 =head2 ParseCcAddressesFromHead HASHREF
334 Takes a hashref object containing QueueObj, Head and CurrentUser objects.
335 Returns a list of all email addresses in the To and Cc
336 headers b<except> the current Queue\'s email addresses, the CurrentUser\'s
337 email address and anything that the configuration sub RT::IsRTAddress matches.
341 sub ParseCcAddressesFromHead {
342 my %args = ( Head => undef,
344 CurrentUser => undef,
349 my @ToObjs = Mail::Address->parse($args{'Head'}->get('To'));
350 my @CcObjs = Mail::Address->parse($args{'Head'}->get('Cc'));
352 foreach my $AddrObj (@ToObjs, @CcObjs) {
353 my $Address = $AddrObj->address;
354 $Address = $args{'CurrentUser'}->UserObj->CanonicalizeEmailAddress($Address);
355 next if ($args{'CurrentUser'}->EmailAddress =~ /^\Q$Address\E$/i);
356 next if ($args{'QueueObj'}->CorrespondAddress =~ /^\Q$Address\E$/i);
357 next if ($args{'QueueObj'}->CommentAddress =~ /^\Q$Address\E$/i);
358 next if (RT::EmailParser->IsRTAddress($Address));
360 push (@Addresses, $Address);
368 # {{{ ParseSenderAdddressFromHead
370 =head2 ParseSenderAddressFromHead
372 Takes a MIME::Header object. Returns a tuple: (user@host, friendly name)
373 of the From (evaluated in order of Reply-To:, From:, Sender)
377 sub ParseSenderAddressFromHead {
379 #Figure out who's sending this message.
380 my $From = $head->get('Reply-To') ||
381 $head->get('From') ||
382 $head->get('Sender');
383 return (ParseAddressFromHeader($From));
387 # {{{ ParseErrorsToAdddressFromHead
389 =head2 ParseErrorsToAddressFromHead
391 Takes a MIME::Header object. Return a single value : user@host
392 of the From (evaluated in order of Return-path:,Errors-To:,Reply-To:,
397 sub ParseErrorsToAddressFromHead {
399 #Figure out who's sending this message.
401 foreach my $header ('Return-path', 'Errors-To' , 'Reply-To', 'From', 'Sender' ) {
402 # If there's a header of that name
403 my $headerobj = $head->get($header);
405 my ($addr, $name ) = ParseAddressFromHeader($headerobj);
406 # If it's got actual useful content...
407 return ($addr) if ($addr);
413 # {{{ ParseAddressFromHeader
415 =head2 ParseAddressFromHeader ADDRESS
417 Takes an address from $head->get('Line') and returns a tuple: user@host, friendly name
422 sub ParseAddressFromHeader{
425 # Perl 5.8.0 breaks when doing regex matches on utf8
426 Encode::_utf8_off($Addr) if $] == 5.008;
427 my @Addresses = Mail::Address->parse($Addr);
429 my $AddrObj = $Addresses[0];
431 unless (ref($AddrObj)) {
435 my $Name = ($AddrObj->phrase || $AddrObj->comment || $AddrObj->address);
437 #Lets take the from and load a user object.
438 my $Address = $AddrObj->address;
440 return ($Address, $Name);
444 # {{{ sub ParseTicketId
451 my $test_name = $RT::EmailSubjectTagRegex || qr/\Q$RT::rtname\E/i;
453 if ( $Subject =~ s/\[$test_name\s+\#(\d+)\s*\]//i ) {
455 $RT::Logger->debug("Found a ticket ID. It's $id");
466 =head2 Gateway ARGSREF
476 This performs all the "guts" of the mail rt-mailgate program, and is
477 designed to be called from the web interface with a message, user
480 Can also take an optional 'ticket' parameter; this ticket id overrides
481 any ticket id found in the subject.
487 (status code, message, optional ticket object)
489 status code is a numeric value.
491 for temporary failures, the status code should be -75
493 for permanent failures which are handled by RT, the status code
496 for succces, the status code should be 1
505 my %args = %$argsref;
507 # Set some reasonable defaults
508 $args{'action'} ||= 'correspond';
509 $args{'queue'} ||= '1';
511 # Validate the action
512 my ($status, @actions) = IsCorrectAction( $args{'action'} );
515 # Can't safely loc this. What object do we loc around?
516 $RT::Logger->crit("Mail gateway called with an invalid action paramenter '".$actions[0]."' for queue '".$args{'queue'}."'");
518 return ( -75, "Invalid 'action' parameter", undef );
521 my $parser = RT::EmailParser->new();
523 $parser->SmartParseMIMEEntityFromScalar( Message => $args{'message'});
525 if (!$parser->Entity()) {
527 To => $RT::OwnerEmail,
528 Subject => "RT Bounce: Unparseable message",
529 Explanation => "RT couldn't process the message below",
530 Attach => $args{'message'}
533 return(0,"Failed to parse this message. Something is likely badly wrong with the message");
536 my $Message = $parser->Entity();
537 my $head = $Message->head;
539 my ( $CurrentUser, $AuthStat, $error );
541 # Initalize AuthStat so comparisons work correctly
542 $AuthStat = -9999999;
544 my $ErrorsTo = ParseErrorsToAddressFromHead($head);
546 my $MessageId = $head->get('Message-ID')
547 || "<no-message-id-" . time . rand(2000) . "\@.$RT::Organization>";
549 #Pull apart the subject line
550 my $Subject = $head->get('Subject') || '';
553 $args{'ticket'} ||= ParseTicketId($Subject);
556 my $Right = 'CreateTicket';
557 if ( $args{'ticket'} ) {
558 $SystemTicket = RT::Ticket->new($RT::SystemUser);
559 $SystemTicket->Load( $args{'ticket'} );
560 # if there's an existing ticket, this must be a reply
561 $Right = 'ReplyToTicket';
564 #Set up a queue object
565 my $SystemQueueObj = RT::Queue->new($RT::SystemUser);
566 $SystemQueueObj->Load( $args{'queue'} );
568 # We can safely have no queue of we have a known-good ticket
569 unless ( $args{'ticket'} || $SystemQueueObj->id ) {
570 return ( -75, "RT couldn't find the queue: " . $args{'queue'}, undef );
573 # Authentication Level
574 # -1 - Get out. this user has been explicitly declined
575 # 0 - User may not do anything (Not used at the moment)
577 # 2 - User is allowed to specify status updates etc. a la enhanced-mailgate
579 push @RT::MailPlugins, "Auth::MailFrom" unless @RT::MailPlugins;
581 # Since this needs loading, no matter what
583 foreach (@RT::MailPlugins) {
586 if ( ref($_) eq "CODE" ) {
590 $_ = "RT::Interface::Email::".$_ unless $_ =~ /^RT::Interface::Email::/;
593 $RT::Logger->crit("Couldn't load module '$_': $@");
597 if ( !defined( $Code = *{ $_ . "::GetCurrentUser" }{CODE} ) ) {
598 $RT::Logger->crit("No GetCurrentUser code found in $_ module");
603 foreach my $action ( @actions ) {
605 ( $CurrentUser, $NewAuthStat ) = $Code->(
607 RawMessageRef => \$args{'message'},
608 CurrentUser => $CurrentUser,
609 AuthLevel => $AuthStat,
611 Ticket => $SystemTicket,
612 Queue => $SystemQueueObj
616 # If a module returns a "-1" then we discard the ticket, so.
617 $AuthStat = -1 if $NewAuthStat == -1;
619 # You get the highest level of authentication you were assigned.
620 $AuthStat = $NewAuthStat if $NewAuthStat > $AuthStat;
622 last if $AuthStat == -1;
625 last if $AuthStat == -1;
628 # {{{ If authentication fails and no new user was created, get out.
629 if ( !$CurrentUser or !$CurrentUser->Id or $AuthStat == -1 ) {
631 # If the plugins refused to create one, they lose.
632 unless ( $AuthStat == -1 ) {
634 # Notify the RT Admin of the failure.
635 # XXX Should this be configurable?
637 To => $RT::OwnerEmail,
638 Subject => "Could not load a valid user",
639 Explanation => <<EOT,
640 RT could not load a valid user, and RT's configuration does not allow
641 for the creation of a new user for this email ($ErrorsTo).
643 You might need to grant 'Everyone' the right '$Right' for the
644 queue @{[$args{'queue'}]}.
651 # Also notify the requestor that his request has been dropped.
654 Subject => "Could not load a valid user",
655 Explanation => <<EOT,
656 RT could not load a valid user, and RT's configuration does not allow
657 for the creation of a new user for your email.
664 return ( 0, "Could not load a valid user", undef );
669 # {{{ Lets check for mail loops of various sorts.
670 my $IsBounce = CheckForBounce($head);
672 my $IsAutoGenerated = CheckForAutoGenerated($head);
674 my $IsSuspiciousSender = CheckForSuspiciousSender($head);
676 my $IsALoop = CheckForLoops($head);
678 my $SquelchReplies = 0;
680 #If the message is autogenerated, we need to know, so we can not
681 # send mail to the sender
682 if ( $IsBounce || $IsSuspiciousSender || $IsAutoGenerated || $IsALoop ) {
684 $ErrorsTo = $RT::OwnerEmail;
689 # {{{ Drop it if it's disallowed
690 if ( $AuthStat == 0 ) {
693 Subject => "Permission Denied",
694 Explanation => "You do not have permission to communicate with RT",
700 # {{{ Warn someone if it's a loop
702 # Warn someone if it's a loop, before we drop it on the ground
704 $RT::Logger->crit("RT Recieved mail ($MessageId) from itself.");
706 #Should we mail it to RTOwner?
707 if ($RT::LoopsToRTOwner) {
709 To => $RT::OwnerEmail,
710 Subject => "RT Bounce: $Subject",
711 Explanation => "RT thinks this message may be a bounce",
716 #Do we actually want to store it?
717 return ( 0, "Message Bounced", undef ) unless ($RT::StoreLoops);
722 # {{{ Squelch replies if necessary
723 # Don't let the user stuff the RT-Squelch-Replies-To header.
724 if ( $head->get('RT-Squelch-Replies-To') ) {
726 'RT-Relocated-Squelch-Replies-To',
727 $head->get('RT-Squelch-Replies-To')
729 $head->delete('RT-Squelch-Replies-To');
732 if ($SquelchReplies) {
734 # Squelch replies to the sender, and also leave a clue to
735 # allow us to squelch ALL outbound messages. This way we
736 # can punt the logic of "what to do when we get a bounce"
737 # to the scrip. We might want to notify nobody. Or just
738 # the RT Owner. Or maybe all Privileged watchers.
739 my ( $Sender, $junk ) = ParseSenderAddressFromHead($head);
740 $head->add( 'RT-Squelch-Replies-To', $Sender );
741 $head->add( 'RT-DetectedAutoGenerated', 'true' );
746 my $Ticket = RT::Ticket->new($CurrentUser);
748 # {{{ If we don't have a ticket Id, we're creating a new ticket
749 if ( (!$SystemTicket || !$SystemTicket->Id) &&
750 grep /^(comment|correspond)$/, @actions ) {
752 # {{{ Create a new ticket
755 my @Requestors = ( $CurrentUser->id );
757 if ($RT::ParseNewMessageForTicketCcs) {
758 @Cc = ParseCcAddressesFromHead(
760 CurrentUser => $CurrentUser,
761 QueueObj => $SystemQueueObj
765 my ( $id, $Transaction, $ErrStr ) = $Ticket->Create(
766 Queue => $SystemQueueObj->Id,
768 Requestor => \@Requestors,
775 Subject => "Ticket creation failed",
776 Explanation => $ErrStr,
779 $RT::Logger->error("Create failed: $id / $Transaction / $ErrStr ");
780 return ( 0, "Ticket creation failed", $Ticket );
782 # strip comments&corresponds from the actions we don't need record twice
783 @actions = grep !/^(comment|correspond)$/, @actions;
784 $args{'ticket'} = $id;
789 $Ticket->Load( $args{'ticket'} );
790 unless ( $Ticket->Id ) {
791 my $message = "Could not find a ticket with id " . $args{'ticket'};
794 Subject => "Message not recorded",
795 Explanation => $message,
799 return ( 0, $message );
803 foreach my $action( @actions ) {
804 # If the action is comment, add a comment.
805 if ( $action =~ /^(comment|correspond)$/i ) {
806 my ( $status, $msg );
807 if ( $action =~ /^correspond$/i ) {
808 ( $status, $msg ) = $Ticket->Correspond( MIMEObj => $Message );
811 ( $status, $msg ) = $Ticket->Comment( MIMEObj => $Message );
815 #Warn the sender that we couldn't actually submit the comment.
818 Subject => "Message not recorded",
822 return ( 0, "Message not recorded", $Ticket );
825 elsif ($RT::UnsafeEmailCommands && $action =~ /^take$/i ) {
826 my ( $status, $msg ) = $Ticket->SetOwner( $CurrentUser->id );
829 #Warn the sender that we couldn't actually submit the comment.
832 Subject => "Ticket not taken",
836 return ( 0, "Ticket not taken", $Ticket );
839 elsif ( $RT::UnsafeEmailCommands && $action =~ /^resolve$/i ) {
840 my ( $status, $msg ) = $Ticket->SetStatus( 'resolved' );
842 #Warn the sender that we couldn't actually submit the comment.
845 Subject => "Ticket not resolved",
849 return ( 0, "Ticket not resolved", $Ticket );
855 #Return mail to the sender with an error
858 Subject => "RT Configuration error",
861 . "' not a recognized action."
862 . " Your RT administrator has misconfigured "
863 . "the mail aliases which invoke RT",
866 $RT::Logger->crit( $args{'action'} . " type unknown for $MessageId" );
869 "Configuration error: "
871 . " not a recognized action",
878 return ( 1, "Success", $Ticket );
884 my @actions = split /-/, $action;
885 foreach ( @actions ) {
886 return (0, $_) unless /^(?:comment|correspond|take|resolve)$/;
888 return (1, @actions);
892 eval "require RT::Interface::Email_Vendor";
893 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Email_Vendor.pm});
894 eval "require RT::Interface::Email_Local";
895 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Email_Local.pm});