X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=rt%2Flib%2FRT%2FEmailParser.pm;h=e9a00f16c03c7dc0247bc9d2c71e7703162f187b;hp=bba4d7ec7752eb8af3e2c4ce160df96a08d5b9e5;hb=c582e92888b4a5553e1b4e5214cf35217e4a0cf0;hpb=289340780927b5bac2c7604d7317c3063c6dd8cc diff --git a/rt/lib/RT/EmailParser.pm b/rt/lib/RT/EmailParser.pm index bba4d7ec7..e9a00f16c 100644 --- a/rt/lib/RT/EmailParser.pm +++ b/rt/lib/RT/EmailParser.pm @@ -35,7 +35,8 @@ use File::Temp qw/tempdir/; =head1 NAME - RT::Interface::CLI - helper functions for creating a commandline RT interface + RT::EmailParser - helper functions for parsing parts from incoming + email messages =head1 SYNOPSIS @@ -295,205 +296,6 @@ sub ParseTicketId { # }}} -# {{{ sub MailError - -=head2 MailError { } - - -# TODO this doesn't belong here. -# TODO doc this - - -=cut - - -sub MailError { - my $self = shift; - - my %args = ( - To => $RT::OwnerEmail, - Bcc => undef, - From => $RT::CorrespondAddress, - Subject => 'There has been an error', - Explanation => 'Unexplained error', - MIMEObj => undef, - LogLevel => 'crit', - @_ - ); - - $RT::Logger->log( - level => $args{'LogLevel'}, - message => $args{'Explanation'} - ); - my $entity = MIME::Entity->build( - Type => "multipart/mixed", - From => $args{'From'}, - Bcc => $args{'Bcc'}, - To => $args{'To'}, - Subject => $args{'Subject'}, - 'X-RT-Loop-Prevention' => $RT::rtname, - ); - - $entity->attach( Data => $args{'Explanation'} . "\n" ); - - my $mimeobj = $args{'MIMEObj'}; - $mimeobj->sync_headers(); - $entity->add_part($mimeobj); - - if ( $RT::MailCommand eq 'sendmailpipe' ) { - open( MAIL, "|$RT::SendmailPath $RT::SendmailArguments" ) || return (0); - print MAIL $entity->as_string; - close(MAIL); - } - else { - $entity->send( $RT::MailCommand, $RT::MailParams ); - } -} - -# }}} - - - -# {{{ sub GetCurrentUser - -sub GetCurrentUser { - my $self = shift; - my $ErrorsTo = shift; - - my %UserInfo = (); - - #Suck the address of the sender out of the header - my ( $Address, $Name ) = $self->ParseSenderAddressFromHead(); - - my $tempuser = RT::User->new($RT::SystemUser); - - #This will apply local address canonicalization rules - $Address = $tempuser->CanonicalizeEmailAddress($Address); - - #If desired, synchronize with an external database - my $UserFoundInExternalDatabase = 0; - - # Username is the 'Name' attribute of the user that RT uses for things - # like authentication - my $Username = undef; - ( $UserFoundInExternalDatabase, %UserInfo ) = - $self->LookupExternalUserInfo( $Address, $Name ); - - $Address = $UserInfo{'EmailAddress'}; - $Username = $UserInfo{'Name'}; - - #Get us a currentuser object to work with. - my $CurrentUser = RT::CurrentUser->new(); - - # First try looking up by a username, if we got one from the external - # db lookup. Next, try looking up by email address. Failing that, - # try looking up by users who have this user's email address as their - # username. - - if ($Username) { - $CurrentUser->LoadByName($Username); - } - - unless ( $CurrentUser->Id ) { - $CurrentUser->LoadByEmail($Address); - } - - #If we can't get it by email address, try by name. - unless ( $CurrentUser->Id ) { - $CurrentUser->LoadByName($Address); - } - - unless ( $CurrentUser->Id ) { - - #If we couldn't load a user, determine whether to create a user - - # {{{ If we require an incoming address to be found in the external - # user database, reject the incoming message appropriately - if ( $RT::SenderMustExistInExternalDatabase - && !$UserFoundInExternalDatabase ) { - - my $Message = - "Sender's email address was not found in the user database."; - - # {{{ This code useful only if you've defined an AutoRejectRequest template - - require RT::Template; - my $template = new RT::Template($RT::Nobody); - $template->Load('AutoRejectRequest'); - $Message = $template->Content || $Message; - - # }}} - - MailError( - To => $ErrorsTo, - Subject => "Ticket Creation failed: user could not be created", - Explanation => $Message, - MIMEObj => $self->Entity, - LogLevel => 'notice' ); - - return ($CurrentUser); - - } - - # }}} - - else { - my $NewUser = RT::User->new($RT::SystemUser); - - my ( $Val, $Message ) = $NewUser->Create( - Name => ( $Username || $Address ), - EmailAddress => $Address, - RealName => "$Name", - Password => undef, - Privileged => 0, - Comments => 'Autocreated on ticket submission' - ); - - unless ($Val) { - - # Deal with the race condition of two account creations at once - # - if ($Username) { - $NewUser->LoadByName($Username); - } - - unless ( $NewUser->Id ) { - $NewUser->LoadByEmail($Address); - } - - unless ( $NewUser->Id ) { - MailError(To => $ErrorsTo, - Subject => "User could not be created", - Explanation => - "User creation failed in mailgateway: $Message", - MIMEObj => $self->Entity, - LogLevel => 'crit' ); - } - } - } - - #Load the new user object - $CurrentUser->LoadByEmail($Address); - - unless ( $CurrentUser->id ) { - $RT::Logger->warning( - "Couldn't load user '$Address'." . "giving up" ); - MailError( - To => $ErrorsTo, - Subject => "User could not be loaded", - Explanation => - "User '$Address' could not be loaded in the mail gateway", - MIMEObj => $self->Entity, - LogLevel => 'crit' ); - - } - } - - return ($CurrentUser); - -} - -# }}} # {{{ ParseCcAddressesFromHead @@ -789,19 +591,20 @@ A private instance method which sets up a mime parser to do its job ## Over max size and return them sub _SetupMIMEParser { - my $self = shift; + my $self = shift; my $parser = shift; - my $AttachmentDir = File::Temp::tempdir( TMPDIR => 1, CLEANUP => 1 ); # Set up output directory for files: - $parser->output_dir("$AttachmentDir"); - $parser->filer->ignore_filename(1); + my $tmpdir = File::Temp::tempdir( TMPDIR => 1, CLEANUP => 1 ); + push ( @{ $self->{'AttachmentDirs'} }, $tmpdir ); + $parser->output_dir($tmpdir); + $parser->filer->ignore_filename(1); #If someone includes a message, extract it $parser->extract_nested_messages(1); - $parser->extract_uuencode(1); ### default is false + $parser->extract_uuencode(1); ### default is false # Set up the prefix for files with auto-generated names: $parser->output_prefix("part"); @@ -810,8 +613,16 @@ sub _SetupMIMEParser { $parser->output_to_core(0); } + # }}} +sub DESTROY { + my $self = shift; + File::Path::rmtree([@{$self->{'AttachmentDirs'}}],0,1); +} + + + eval "require RT::EmailParser_Vendor"; die $@ if ($@ && $@ !~ qr{^Can't locate RT/EmailParser_Vendor.pm}); eval "require RT::EmailParser_Local";