3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
5 # (Except where explictly superceded by other copyright notices)
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
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.
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.
24 package RT::Interface::Email;
34 use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
36 # set the version for version checking
37 $VERSION = do { my @r = (q$Revision: 1.1.1.3 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
41 # your exported package globals go here,
42 # as well as any optionally exported functions
47 &CheckForSuspiciousSender
48 &CheckForAutoGenerated
50 &ParseCcAddressesFromHead
51 &ParseSenderAddressFromHead
52 &ParseErrorsToAddressFromHead
53 &ParseAddressFromHeader
60 RT::Interface::Email - helper functions for parsing email sent to RT
64 use lib "!!RT_LIB_PATH!!";
65 use lib "!!RT_ETC_PATH!!";
67 use RT::Interface::Email qw(Gateway CreateUser);
74 ok(require RT::Interface::Email);
84 # {{{ sub CheckForLoops
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") {
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 ...
103 # {{{ sub CheckForSuspiciousSender
105 sub CheckForSuspiciousSender {
108 #if it's from a postmaster or mailer daemon, it's likely a bounce.
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.
116 #TODO: search through the whole email and find the right Ticket ID.
118 my ($From, $junk) = ParseSenderAddressFromHead($head);
120 if (($From =~ /^mailer-daemon/i) or
121 ($From =~ /^postmaster/i)){
132 # {{{ sub CheckForAutoGenerated
133 sub CheckForAutoGenerated {
136 my $Precedence = $head->get("Precedence") || "" ;
137 if ($Precedence =~ /^(bulk|junk)/i) {
150 my %args = (To => $RT::OwnerEmail,
152 From => $RT::CorrespondAddress,
153 Subject => 'There has been an error',
154 Explanation => 'Unexplained error',
161 $RT::Logger->log(level => $args{'LogLevel'},
162 message => $args{'Explanation'}
164 my $entity = MIME::Entity->build( Type =>"multipart/mixed",
165 From => $args{'From'},
168 Subject => $args{'Subject'},
169 'X-RT-Loop-Prevention' => $RT::rtname,
172 $entity->attach( Data => $args{'Explanation'}."\n");
174 my $mimeobj = $args{'MIMEObj'};
176 $mimeobj->sync_headers();
177 $entity->add_part($mimeobj);
180 if ($args{'Attach'}) {
181 $entity->attach(Data => $args{'Attach'}, Type => 'message/rfc822');
185 if ($RT::MailCommand eq 'sendmailpipe') {
186 open (MAIL, "|$RT::SendmailPath $RT::SendmailArguments") || return(0);
187 print MAIL $entity->as_string;
191 $entity->send($RT::MailCommand, $RT::MailParams);
200 my ($Username, $Address, $Name, $ErrorsTo, $entity) = @_;
201 my $NewUser = RT::User->new($RT::SystemUser);
203 my ($Val, $Message) =
204 $NewUser->Create(Name => ($Username || $Address),
205 EmailAddress => $Address,
209 Comments => 'Autocreated on ticket submission'
214 # Deal with the race condition of two account creations at once
217 $NewUser->LoadByName($Username);
220 unless ($NewUser->Id) {
221 $NewUser->LoadByEmail($Address);
224 unless ($NewUser->Id) {
225 MailError( To => $ErrorsTo,
226 Subject => "User could not be created",
227 Explanation => "User creation failed in mailgateway: $Message",
234 #Load the new user object
235 my $CurrentUser = RT::CurrentUser->new();
236 $CurrentUser->LoadByEmail($Address);
238 unless ($CurrentUser->id) {
239 $RT::Logger->warning("Couldn't load user '$Address'.". "giving up");
240 MailError( To => $ErrorsTo,
241 Subject => "User could not be loaded",
242 Explanation => "User '$Address' could not be loaded in the mail gateway",
251 # {{{ ParseCcAddressesFromHead
253 =head2 ParseCcAddressesFromHead HASHREF
255 Takes a hashref object containing QueueObj, Head and CurrentUser objects.
256 Returns a list of all email addresses in the To and Cc
257 headers b<except> the current Queue\'s email addresses, the CurrentUser\'s
258 email address and anything that the configuration sub RT::IsRTAddress matches.
262 sub ParseCcAddressesFromHead {
263 my %args = ( Head => undef,
265 CurrentUser => undef,
270 my @ToObjs = Mail::Address->parse($args{'Head'}->get('To'));
271 my @CcObjs = Mail::Address->parse($args{'Head'}->get('Cc'));
273 foreach my $AddrObj (@ToObjs, @CcObjs) {
274 my $Address = $AddrObj->address;
275 $Address = $args{'CurrentUser'}->UserObj->CanonicalizeEmailAddress($Address);
276 next if ($args{'CurrentUser'}->EmailAddress =~ /^$Address$/i);
277 next if ($args{'QueueObj'}->CorrespondAddress =~ /^$Address$/i);
278 next if ($args{'QueueObj'}->CommentAddress =~ /^$Address$/i);
279 next if (RT::EmailParser::IsRTAddress(undef, $Address));
281 push (@Addresses, $Address);
289 # {{{ ParseSenderAdddressFromHead
291 =head2 ParseSenderAddressFromHead
293 Takes a MIME::Header object. Returns a tuple: (user@host, friendly name)
294 of the From (evaluated in order of Reply-To:, From:, Sender)
298 sub ParseSenderAddressFromHead {
300 #Figure out who's sending this message.
301 my $From = $head->get('Reply-To') ||
302 $head->get('From') ||
303 $head->get('Sender');
304 return (ParseAddressFromHeader($From));
308 # {{{ ParseErrorsToAdddressFromHead
310 =head2 ParseErrorsToAddressFromHead
312 Takes a MIME::Header object. Return a single value : user@host
313 of the From (evaluated in order of Errors-To:,Reply-To:, From:, Sender)
317 sub ParseErrorsToAddressFromHead {
319 #Figure out who's sending this message.
321 foreach my $header ('Errors-To' , 'Reply-To', 'From', 'Sender' ) {
322 # If there's a header of that name
323 my $headerobj = $head->get($header);
325 my ($addr, $name ) = ParseAddressFromHeader($headerobj);
326 # If it's got actual useful content...
327 return ($addr) if ($addr);
333 # {{{ ParseAddressFromHeader
335 =head2 ParseAddressFromHeader ADDRESS
337 Takes an address from $head->get('Line') and returns a tuple: user@host, friendly name
342 sub ParseAddressFromHeader{
345 my @Addresses = Mail::Address->parse($Addr);
347 my $AddrObj = $Addresses[0];
349 unless (ref($AddrObj)) {
353 my $Name = ($AddrObj->phrase || $AddrObj->comment || $AddrObj->address);
355 #Lets take the from and load a user object.
356 my $Address = $AddrObj->address;
358 return ($Address, $Name);
364 =head2 Gateway ARGSREF
374 This performs all the "guts" of the mail rt-mailgate program, and is
375 designed to be called from the web interface with a message, user
378 Can also take an optional 'ticket' parameter; this ticket id overrides
379 any ticket id found in the subject.
385 (status code, message, optional ticket object)
387 status code is a numeric value.
389 for temporary failures, status code should be -75
391 for permanent failures which are handled by RT, status code should be 0
393 for succces, the status code should be 1
402 my %args = %$argsref;
404 # Set some reasonable defaults
405 $args{'action'} = 'correspond' unless ( $args{'action'} );
406 $args{'queue'} = '1' unless ( $args{'queue'} );
408 # Validate the action
409 unless ( $args{'action'} =~ /^(comment|correspond|action)$/ ) {
411 # Can't safely loc this. What object do we loc around?
412 $RT::Logger->crit("Mail gateway called with an invalid action paramenter '".$args{'action'}."' for queue '".$args{'queue'}."'");
414 return ( -75, "Invalid 'action' parameter", undef );
417 my $parser = RT::EmailParser->new();
418 my ( $fh, $temp_file );
421 # on NFS and NTFS, it is possible that tempfile() conflicts
422 # with other processes, causing a race condition. we try to
423 # accommodate this by pausing and retrying.
424 last if ( $fh, $temp_file ) = eval { File::Temp::tempfile(undef, UNLINK => 0) };
428 binmode $fh; #thank you, windows
430 print $fh $args{'message'};
433 if ( -f $temp_file ) {
434 $parser->ParseMIMEEntityFromFile($temp_file);
435 unlink( $temp_file );
436 if ($parser->Entity) {
437 delete $args{'message'};
443 #If for some reason we weren't able to parse the message using a temp file
444 # try it with a scalar
445 if ($args{'message'}) {
446 $parser->ParseMIMEEntityFromScalar($args{'message'});
450 if (!$parser->Entity()) {
452 To => $RT::OwnerEmail,
453 Subject => "RT Bounce: Unparseable message",
454 Explanation => "RT couldn't process the message below",
455 Attach => $args{'message'}
458 return(0,"Failed to parse this message. Something is likely badly wrong with the message");
461 my $Message = $parser->Entity();
462 my $head = $Message->head;
464 my ( $CurrentUser, $AuthStat, $status, $error );
466 # Initalize AuthStat so comparisons work correctly
467 $AuthStat = -9999999;
469 my $ErrorsTo = ParseErrorsToAddressFromHead($head);
471 my $MessageId = $head->get('Message-Id')
472 || "<no-message-id-" . time . rand(2000) . "\@.$RT::Organization>";
474 #Pull apart the subject line
475 my $Subject = $head->get('Subject') || '';
478 $args{'ticket'} ||= $parser->ParseTicketId($Subject);
481 if ( $args{'ticket'} ) {
482 $SystemTicket = RT::Ticket->new($RT::SystemUser);
483 $SystemTicket->Load( $args{'ticket'} );
486 #Set up a queue object
487 my $SystemQueueObj = RT::Queue->new($RT::SystemUser);
488 $SystemQueueObj->Load( $args{'queue'} );
490 # We can safely have no queue of we have a known-good ticket
491 unless ( $args{'ticket'} || $SystemQueueObj->id ) {
492 return ( -75, "RT couldn't find the queue: " . $args{'queue'}, undef );
495 # Authentication Level
496 # -1 - Get out. this user has been explicitly declined
497 # 0 - User may not do anything (Not used at the moment)
499 # 2 - User is allowed to specify status updates etc. a la enhanced-mailgate
501 push @RT::MailPlugins, "Auth::MailFrom" unless @RT::MailPlugins;
503 # Since this needs loading, no matter what
505 for (@RT::MailPlugins) {
508 if ( ref($_) eq "CODE" ) {
512 $_ = "RT::Interface::Email::$_" unless /^RT::Interface::Email::/;
515 die ("Couldn't load module $_: $@");
519 if ( !defined( $Code = *{ $_ . "::GetCurrentUser" }{CODE} ) ) {
520 die ("No GetCurrentUser code found in $_ module");
525 ( $CurrentUser, $NewAuthStat ) = $Code->(
527 CurrentUser => $CurrentUser,
528 AuthLevel => $AuthStat,
529 Action => $args{'action'},
530 Ticket => $SystemTicket,
531 Queue => $SystemQueueObj
534 # If a module returns a "-1" then we discard the ticket, so.
535 $AuthStat = -1 if $NewAuthStat == -1;
537 # You get the highest level of authentication you were assigned.
538 $AuthStat = $NewAuthStat if $NewAuthStat > $AuthStat;
539 last if $AuthStat == -1;
542 # {{{ If authentication fails and no new user was created, get out.
543 if ( !$CurrentUser or !$CurrentUser->Id or $AuthStat == -1 ) {
545 # If the plugins refused to create one, they lose.
546 unless ( $AuthStat == -1 ) {
548 # Notify the RT Admin of the failure.
549 # XXX Should this be configurable?
551 To => $RT::OwnerEmail,
552 Subject => "Could not load a valid user",
553 Explanation => <<EOT,
554 RT could not load a valid user, and RT's configuration does not allow
555 for the creation of a new user for this email ($ErrorsTo).
557 You might need to grant 'Everyone' the right 'CreateTicket' for the
558 queue @{[$args{'queue'}]}.
565 # Also notify the requestor that his request has been dropped.
568 Subject => "Could not load a valid user",
569 Explanation => <<EOT,
570 RT could not load a valid user, and RT's configuration does not allow
571 for the creation of a new user for your email.
578 return ( 0, "Could not load a valid user", undef );
583 # {{{ Lets check for mail loops of various sorts.
584 my $IsAutoGenerated = CheckForAutoGenerated($head);
586 my $IsSuspiciousSender = CheckForSuspiciousSender($head);
588 my $IsALoop = CheckForLoops($head);
590 my $SquelchReplies = 0;
592 #If the message is autogenerated, we need to know, so we can not
593 # send mail to the sender
594 if ( $IsSuspiciousSender || $IsAutoGenerated || $IsALoop ) {
596 $ErrorsTo = $RT::OwnerEmail;
601 # {{{ Drop it if it's disallowed
602 if ( $AuthStat == 0 ) {
605 Subject => "Permission Denied",
606 Explanation => "You do not have permission to communicate with RT",
612 # {{{ Warn someone if it's a loop
614 # Warn someone if it's a loop, before we drop it on the ground
616 $RT::Logger->crit("RT Recieved mail ($MessageId) from itself.");
618 #Should we mail it to RTOwner?
619 if ($RT::LoopsToRTOwner) {
621 To => $RT::OwnerEmail,
622 Subject => "RT Bounce: $Subject",
623 Explanation => "RT thinks this message may be a bounce",
627 #Do we actually want to store it?
628 return ( 0, "Message Bounced", undef ) unless ($RT::StoreLoops);
634 # {{{ Squelch replies if necessary
635 # Don't let the user stuff the RT-Squelch-Replies-To header.
636 if ( $head->get('RT-Squelch-Replies-To') ) {
638 'RT-Relocated-Squelch-Replies-To',
639 $head->get('RT-Squelch-Replies-To')
641 $head->delete('RT-Squelch-Replies-To');
644 if ($SquelchReplies) {
645 ## TODO: This is a hack. It should be some other way to
646 ## indicate that the transaction should be "silent".
648 my ( $Sender, $junk ) = ParseSenderAddressFromHead($head);
649 $head->add( 'RT-Squelch-Replies-To', $Sender );
654 my $Ticket = RT::Ticket->new($CurrentUser);
656 # {{{ If we don't have a ticket Id, we're creating a new ticket
657 if ( !$args{'ticket'} ) {
659 # {{{ Create a new ticket
662 my @Requestors = ( $CurrentUser->id );
664 if ($RT::ParseNewMessageForTicketCcs) {
665 @Cc = ParseCcAddressesFromHead(
667 CurrentUser => $CurrentUser,
668 QueueObj => $SystemQueueObj
672 my ( $id, $Transaction, $ErrStr ) = $Ticket->Create(
673 Queue => $SystemQueueObj->Id,
675 Requestor => \@Requestors,
682 Subject => "Ticket creation failed",
683 Explanation => $ErrStr,
686 $RT::Logger->error("Create failed: $id / $Transaction / $ErrStr ");
687 return ( 0, "Ticket creation failed", $Ticket );
695 # If the action is comment, add a comment.
696 elsif ( $args{'action'} =~ /^(comment|correspond)$/i ) {
697 $Ticket->Load( $args{'ticket'} );
698 unless ( $Ticket->Id ) {
699 my $message = "Could not find a ticket with id " . $args{'ticket'};
702 Subject => "Message not recorded",
703 Explanation => $message,
707 return ( 0, $message );
710 my ( $status, $msg );
711 if ( $args{'action'} =~ /^correspond$/ ) {
712 ( $status, $msg ) = $Ticket->Correspond( MIMEObj => $Message );
715 ( $status, $msg ) = $Ticket->Comment( MIMEObj => $Message );
719 #Warn the sender that we couldn't actually submit the comment.
722 Subject => "Message not recorded",
726 return ( 0, "Message not recorded", $Ticket );
732 #Return mail to the sender with an error
735 Subject => "RT Configuration error",
738 . "' not a recognized action."
739 . " Your RT administrator has misconfigured "
740 . "the mail aliases which invoke RT",
743 $RT::Logger->crit( $args{'action'} . " type unknown for $MessageId" );
746 "Configuration error: "
748 . " not a recognized action",
754 return ( 1, "Success", $Ticket );
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});