[Rt-commit] [svn] r1338 - in rt/branches/3.2-RELEASE/lib/RT: . Action Interface

alexmv at pallas.eruditorum.org alexmv at pallas.eruditorum.org
Tue Aug 17 17:05:11 EDT 2004


Author: alexmv
Date: Tue Aug 17 17:05:10 2004
New Revision: 1338

Modified:
   rt/branches/3.2-RELEASE/lib/RT/Action/SendEmail.pm
   rt/branches/3.2-RELEASE/lib/RT/EmailParser.pm
   rt/branches/3.2-RELEASE/lib/RT/I18N.pm
   rt/branches/3.2-RELEASE/lib/RT/Interface/Email.pm
   rt/branches/3.2-RELEASE/lib/RT/Queue_Overlay.pm
   rt/branches/3.2-RELEASE/lib/RT/User_Overlay.pm
Log:
RT-Ticket: 5716
RT-Update: correspond
RT-Status: resolved

 * Move code which is not specific to parsing mime messages into
   RT::Email::Interface; this removes a large base of duplicated code,
   and some sketchy "methods" that were getting called with "" as self
   often



Modified: rt/branches/3.2-RELEASE/lib/RT/Action/SendEmail.pm
==============================================================================
--- rt/branches/3.2-RELEASE/lib/RT/Action/SendEmail.pm	(original)
+++ rt/branches/3.2-RELEASE/lib/RT/Action/SendEmail.pm	Tue Aug 17 17:05:10 2004
@@ -54,7 +54,7 @@
 
 use MIME::Words qw(encode_mimeword);
 
-use RT::EmailParser;
+use RT::Interface::Email;
 use Mail::Address;
 
 =head1 NAME
@@ -484,11 +484,11 @@
 
     # Weed out any RT addresses. We really don't want to talk to ourselves!
     @{ $self->{'To'} } =
-      RT::EmailParser::CullRTAddresses( "", @{ $self->{'To'} } );
+      RT::Interface::Email::CullRTAddresses( @{ $self->{'To'} } );
     @{ $self->{'Cc'} } =
-      RT::EmailParser::CullRTAddresses( "", @{ $self->{'Cc'} } );
+      RT::Interface::Email::CullRTAddresses( @{ $self->{'Cc'} } );
     @{ $self->{'Bcc'} } =
-      RT::EmailParser::CullRTAddresses( "", @{ $self->{'Bcc'} } );
+      RT::Interface::Email::CullRTAddresses( @{ $self->{'Bcc'} } );
 
     # If there are no recipients, don't try to send the message.
     # If the transaction has content and has the header RT-Squelch-Replies-To

Modified: rt/branches/3.2-RELEASE/lib/RT/EmailParser.pm
==============================================================================
--- rt/branches/3.2-RELEASE/lib/RT/EmailParser.pm	(original)
+++ rt/branches/3.2-RELEASE/lib/RT/EmailParser.pm	Tue Aug 17 17:05:10 2004
@@ -89,96 +89,12 @@
 }
 
 
-
-# {{{ sub debug
-
-sub debug {
-    my $val = shift;
-    my ($debug);
-    if ($val) {
-        $RT::Logger->debug( $val . "\n" );
-        if ($debug) {
-            print STDERR "$val\n";
-        }
-    }
-    if ($debug) {
-        return (1);
-    }
-}
-
-# }}}
-
-# {{{ sub CheckForLoops 
-
-sub CheckForLoops {
-    my $self = shift;
-
-    my $head = $self->Head;
-
-    #If this instance of RT sent it our, we don't want to take it in
-    my $RTLoop = $head->get("X-RT-Loop-Prevention") || "";
-    chomp($RTLoop);    #remove that newline
-    if ( $RTLoop =~ /^\Q$RT::rtname\E/o ) {
-        return (1);
-    }
-
-    # TODO: We might not trap the case where RT instance A sends a mail
-    # to RT instance B which sends a mail to ...
-    return (undef);
-}
-
-# }}}
-
-# {{{ sub CheckForSuspiciousSender
-
-sub CheckForSuspiciousSender {
-    my $self = shift;
-
-    #if it's from a postmaster or mailer daemon, it's likely a bounce.
-
-    #TODO: better algorithms needed here - there is no standards for
-    #bounces, so it's very difficult to separate them from anything
-    #else.  At the other hand, the Return-To address is only ment to be
-    #used as an error channel, we might want to put up a separate
-    #Return-To address which is treated differently.
-
-    #TODO: search through the whole email and find the right Ticket ID.
-
-    my ( $From, $junk ) = $self->ParseSenderAddressFromHead();
-
-    if ( ( $From =~ /^mailer-daemon/i ) or ( $From =~ /^postmaster/i ) ) {
-        return (1);
-
-    }
-
-    return (undef);
-
-}
-
-# }}}
-
-# {{{ sub CheckForAutoGenerated
-sub CheckForAutoGenerated {
-    my $self = shift;
-    my $head = $self->Head;
-
-    my $Precedence = $head->get("Precedence") || "";
-    if ( $Precedence =~ /^(bulk|junk)/i ) {
-        return (1);
-    }
-    else {
-        return (undef);
-    }
-}
-
-# }}}
-
+# {{{ sub SmartParseMIMEEntityFromScalar
 
 =head2 SmartParseMIMEEntityFromScalar { Message => SCALAR_REF, Decode => BOOL }
 
 Parse a message stored in a scalar from scalar_ref
 
-
 =cut
 
 sub SmartParseMIMEEntityFromScalar {
@@ -223,8 +139,16 @@
 
 }
 
+# }}}
+
 # {{{ sub ParseMIMEEntityFromSTDIN
 
+=head2 ParseMIMEEntityFromSTDIN
+
+Parse a message from standard input
+
+=cut
+
 sub ParseMIMEEntityFromSTDIN {
     my $self = shift;
     my $postprocess = (@_ ? shift : 1);
@@ -233,6 +157,8 @@
 
 # }}}
 
+# {{{ ParseMIMEEntityFromScalar
+
 =head2 ParseMIMEEntityFromScalar  $message
 
 Takes either a scalar or a reference to a scalr which contains a stringified MIME message.
@@ -241,7 +167,6 @@
 Returns true if it wins.
 Returns false if it loses.
 
-
 =cut
 
 sub ParseMIMEEntityFromScalar {
@@ -251,6 +176,7 @@
     $self->_ParseMIMEEntity($message,'parse_data', $postprocess);
 }
 
+# }}}
 
 # {{{ ParseMIMEEntityFromFilehandle *FH
 
@@ -285,8 +211,8 @@
 }
 
 # }}}
-#
-#  {{{ _ParseMIMEEntity {
+
+# {{{ _ParseMIMEEntity
 sub _ParseMIMEEntity {
     my $self = shift;
     my $message = shift;
@@ -314,7 +240,6 @@
 
 }
 
-
 # }}}
 
 # {{{ _PostProcessNewEntity 
@@ -337,221 +262,10 @@
     # Unfold headers that are have embedded newlines
     $self->Head->unfold;
 
-
-}
-
-# }}}
-
-# {{{ sub ParseTicketId 
-
-sub ParseTicketId {
-    my $self = shift;
-
-    my $Subject = shift;
-
-    if ( $Subject =~ s/\[\Q$RT::rtname\E\s+\#(\d+)\s*\]//i ) {
-        my $id = $1;
-        $RT::Logger->debug("Found a ticket ID. It's $id");
-        return ($id);
-    }
-    else {
-        return (undef);
-    }
-}
-
-# }}}
-
-
-
-# {{{ ParseCcAddressesFromHead 
-
-=head2 ParseCcAddressesFromHead HASHREF
-
-Takes a hashref object containing QueueObj, Head and CurrentUser objects.
-Returns a list of all email addresses in the To and Cc 
-headers b<except> the current Queue\'s email addresses, the CurrentUser\'s 
-email address  and anything that the $RTAddressRegexp matches.
-
-=cut
-
-sub ParseCcAddressesFromHead {
-
-    my $self = shift;
-
-    my %args = (
-        QueueObj    => undef,
-        CurrentUser => undef,
-        @_
-    );
-
-    my (@Addresses);
-
-    my @ToObjs = Mail::Address->parse( $self->Head->get('To') );
-    my @CcObjs = Mail::Address->parse( $self->Head->get('Cc') );
-
-    foreach my $AddrObj ( @ToObjs, @CcObjs ) {
-        my $Address = $AddrObj->address;
-        my $user = RT::User->new($RT::SystemUser);
-        $Address = $user->CanonicalizeEmailAddress($Address);
-        next if ( $args{'CurrentUser'}->EmailAddress   =~ /^$Address$/i );
-        next if ( $args{'QueueObj'}->CorrespondAddress =~ /^$Address$/i );
-        next if ( $args{'QueueObj'}->CommentAddress    =~ /^$Address$/i );
-        next if ( IsRTAddress($Address) );
-
-        push ( @Addresses, $Address );
-    }
-    return (@Addresses);
-}
-
-# }}}
-
-# {{{ ParseSenderAdddressFromHead
-
-=head2 ParseSenderAddressFromHead
-
-Takes a MIME::Header object. Returns a tuple: (user at host, friendly name) 
-of the From (evaluated in order of Reply-To:, From:, Sender)
-
-=cut
-
-sub ParseSenderAddressFromHead {
-    my $self = shift;
-
-    #Figure out who's sending this message.
-    my $From = $self->Head->get('Reply-To')
-      || $self->Head->get('From')
-      || $self->Head->get('Sender');
-    return ( $self->ParseAddressFromHeader($From) );
-}
-
-# }}}
-
-# {{{ ParseErrorsToAdddressFromHead
-
-=head2 ParseErrorsToAddressFromHead
-
-Takes a MIME::Header object. Return a single value : user at host
-of the From (evaluated in order of Errors-To:,Reply-To:, From:, Sender)
-
-=cut
-
-sub ParseErrorsToAddressFromHead {
-    my $self = shift;
-
-    #Figure out who's sending this message.
-
-    foreach my $header ( 'Errors-To', 'Reply-To', 'From', 'Sender' ) {
-
-        # If there's a header of that name
-        my $headerobj = $self->Head->get($header);
-        if ($headerobj) {
-            my ( $addr, $name ) = $self->ParseAddressFromHeader($headerobj);
-
-            # If it's got actual useful content...
-            return ($addr) if ($addr);
-        }
-    }
-}
-
-# }}}
-
-# {{{ ParseAddressFromHeader
-
-=head2 ParseAddressFromHeader ADDRESS
-
-Takes an address from $self->Head->get('Line') and returns a tuple: user at host, friendly name
-
-=cut
-
-sub ParseAddressFromHeader {
-    my $self = shift;
-    my $Addr = shift;
-
-    # Perl 5.8.0 breaks when doing regex matches on utf8
-    Encode::_utf8_off($Addr) if $] == 5.008;
-    my @Addresses = Mail::Address->parse($Addr);
-
-    my $AddrObj = $Addresses[0];
-
-    unless ( ref($AddrObj) ) {
-        return ( undef, undef );
-    }
-
-    my $Name = ( $AddrObj->phrase || $AddrObj->comment || $AddrObj->address );
-
-    #Lets take the from and load a user object.
-    my $Address = $AddrObj->address;
-
-    return ( $Address, $Name );
-}
-
-# }}}
-
-# {{{ IsRTAddress
-
-=item IsRTaddress ADDRESS
-
-Takes a single parameter, an email address. 
-Returns true if that address matches the $RTAddressRegexp.  
-Returns false, otherwise.
-
-=begin testing
-
-is(RT::EmailParser::IsRTAddress("","rt\@example.com"),1, "Regexp matched rt address" );
-is(RT::EmailParser::IsRTAddress("","frt\@example.com"),undef, "Regexp didn't match non-rt address" );
-
-=end testing
-
-=cut
-
-sub IsRTAddress {
-    my $self = shift;
-    my $address = shift;
-
-    # Example: the following rule would tell RT not to Cc 
-    #   "tickets at noc.example.com"
-    if ( defined($RT::RTAddressRegexp) &&
-                       $address =~ /$RT::RTAddressRegexp/ ) {
-        return(1);
-    } else {
-        return (undef);
-    }
-}
-
-# }}}
-
-
-# {{{ CullRTAddresses
-
-=item CullRTAddresses ARRAY
-
-Takes a single argument, an array of email addresses.
-Returns the same array with any IsRTAddress()es weeded out.
-
-=begin testing
-
- at before = ("rt\@example.com", "frt\@example.com");
- at after = ("frt\@example.com");
-ok(eq_array(RT::EmailParser::CullRTAddresses("", at before), at after), "CullRTAddresses only culls RT addresses");
-
-=end testing
-
-=cut
-
-sub CullRTAddresses {
-    my $self = shift;
-    my @addresses= (@_);
-    my @addrlist;
-
-    foreach my $addr( @addresses ) {
-      push (@addrlist, $addr)    unless IsRTAddress("", $addr);
-    }
-    return (@addrlist);
 }
 
 # }}}
 
-
 # {{{ LookupExternalUserInfo
 
 
@@ -641,6 +355,7 @@
 }
 
 # }}}
+
 # {{{ _SetupMIMEParser 
 
 =head2 _SetupMIMEParser $parser
@@ -686,7 +401,6 @@
     # Temp files should never be recycled, especially when running under perl taint checking
     
     $parser->tmp_recycling(0);
-    
 
 }
 

Modified: rt/branches/3.2-RELEASE/lib/RT/I18N.pm
==============================================================================
--- rt/branches/3.2-RELEASE/lib/RT/I18N.pm	(original)
+++ rt/branches/3.2-RELEASE/lib/RT/I18N.pm	Tue Aug 17 17:05:10 2004
@@ -357,7 +357,6 @@
 
 # }}}
 
-
 # {{{ _GuessCharset
 
 =head2 _GuessCharset STRING
@@ -423,6 +422,7 @@
     return if $charset eq $enc and $preserve_words;
 
     foreach my $tag ( $head->tags ) {
+        next unless $tag; # seen in wild: headers with no name
         my @values = $head->get_all($tag);
         $head->delete($tag);
         foreach my $value (@values) {

Modified: rt/branches/3.2-RELEASE/lib/RT/Interface/Email.pm
==============================================================================
--- rt/branches/3.2-RELEASE/lib/RT/Interface/Email.pm	(original)
+++ rt/branches/3.2-RELEASE/lib/RT/Interface/Email.pm	Tue Aug 17 17:05:10 2004
@@ -64,15 +64,15 @@
     # as well as any optionally exported functions
     @EXPORT_OK   = qw(
               &CreateUser
-		      &GetMessageContent
-		      &CheckForLoops 
-		      &CheckForSuspiciousSender
-		      &CheckForAutoGenerated 
-		      &MailError 
-		      &ParseCcAddressesFromHead
-		      &ParseSenderAddressFromHead 
-		      &ParseErrorsToAddressFromHead
-                      &ParseAddressFromHeader
+              &GetMessageContent
+              &CheckForLoops 
+              &CheckForSuspiciousSender
+              &CheckForAutoGenerated 
+              &MailError 
+              &ParseCcAddressesFromHead
+              &ParseSenderAddressFromHead 
+              &ParseErrorsToAddressFromHead
+              &ParseAddressFromHeader
               &Gateway);
 
 }
@@ -166,6 +166,51 @@
 
 # }}}
 
+# {{{ IsRTAddress
+
+=item IsRTAddress ADDRESS
+
+Takes a single parameter, an email address. 
+Returns true if that address matches the $RTAddressRegexp.  
+Returns false, otherwise.
+
+=cut
+
+sub IsRTAddress {
+    my $address = shift;
+
+    # Example: the following rule would tell RT not to Cc 
+    #   "tickets at noc.example.com"
+    if ( defined($RT::RTAddressRegexp) &&
+                       $address =~ /$RT::RTAddressRegexp/ ) {
+        return(1);
+    } else {
+        return (undef);
+    }
+}
+
+# }}}
+
+# {{{ CullRTAddresses
+
+=item CullRTAddresses ARRAY
+
+Takes a single argument, an array of email addresses.
+Returns the same array with any IsRTAddress()es weeded out.
+
+=cut
+
+sub CullRTAddresses {
+    my @addresses = (@_);
+    my @addrlist;
+
+    foreach my $addr( @addresses ) {
+      push (@addrlist, $addr) unless IsRTAddress($addr);
+    }
+    return (@addrlist);
+}
+
+# }}}
 
 # {{{ sub MailError 
 sub MailError {
@@ -299,7 +344,7 @@
  	next if ($args{'CurrentUser'}->EmailAddress =~ /^$Address$/i);
 	next if ($args{'QueueObj'}->CorrespondAddress =~ /^$Address$/i);
 	next if ($args{'QueueObj'}->CommentAddress =~ /^$Address$/i);
-	next if (RT::EmailParser::IsRTAddress(undef, $Address));
+	next if (IsRTAddress(undef, $Address));
 	
 	push (@Addresses, $Address);
     }
@@ -365,6 +410,8 @@
 sub ParseAddressFromHeader{
     my $Addr = shift;
     
+    # Perl 5.8.0 breaks when doing regex matches on utf8
+    Encode::_utf8_off($Addr) if $] == 5.008;
     my @Addresses = Mail::Address->parse($Addr);
     
     my $AddrObj = $Addresses[0];
@@ -382,6 +429,22 @@
 }
 # }}}
 
+# {{{ sub ParseTicketId 
+
+sub ParseTicketId {
+    my $Subject = shift;
+
+    if ( $Subject =~ s/\[\Q$RT::rtname\E\s+\#(\d+)\s*\]//i ) {
+        my $id = $1;
+        $RT::Logger->debug("Found a ticket ID. It's $id");
+        return ($id);
+    }
+    else {
+        return (undef);
+    }
+}
+
+# }}}
 
 
 =head2 Gateway ARGSREF
@@ -469,7 +532,7 @@
     my $Subject = $head->get('Subject') || '';
     chomp $Subject;
 
-    $args{'ticket'} ||= $parser->ParseTicketId($Subject);
+    $args{'ticket'} ||= ParseTicketId($Subject);
 
     my $SystemTicket;
     my $Right = 'CreateTicket';

Modified: rt/branches/3.2-RELEASE/lib/RT/Queue_Overlay.pm
==============================================================================
--- rt/branches/3.2-RELEASE/lib/RT/Queue_Overlay.pm	(original)
+++ rt/branches/3.2-RELEASE/lib/RT/Queue_Overlay.pm	Tue Aug 17 17:05:10 2004
@@ -70,7 +70,7 @@
 use vars qw(@STATUS @ACTIVE_STATUS @INACTIVE_STATUS $RIGHTS);
 use RT::Groups;
 use RT::ACL;
-use RT::EmailParser;
+use RT::Interface::Email;
 
 
 @ACTIVE_STATUS = qw(new open stalled);
@@ -662,7 +662,7 @@
              my $new_user = RT::User->new($RT::SystemUser);
 
             my ( $Address, $Name ) =  
-               RT::EmailParser::ParseAddressFromHeader('', $args{'Email'});
+               RT::Interface::Email::ParseAddressFromHeader($args{'Email'});
 
             my ( $Val, $Message ) = $new_user->Create(
                 Name => $Address,

Modified: rt/branches/3.2-RELEASE/lib/RT/User_Overlay.pm
==============================================================================
--- rt/branches/3.2-RELEASE/lib/RT/User_Overlay.pm	(original)
+++ rt/branches/3.2-RELEASE/lib/RT/User_Overlay.pm	Tue Aug 17 17:05:10 2004
@@ -75,7 +75,7 @@
 use Digest::MD5;
 use RT::Principals;
 use RT::ACE;
-use RT::EmailParser;
+use RT::Interface::Email;
 
 
 # {{{ sub _Accessible 
@@ -573,7 +573,7 @@
         my ($val, $message);
 
 	my ( $Address, $Name ) =
-		RT::EmailParser::ParseAddressFromHeader('', $email);
+		RT::Interface::Email::ParseAddressFromHeader($email);
 	$email = $Address;
 
         $self->LoadByEmail($email);


More information about the Rt-commit mailing list