[Rt-commit] rt branch, 3.9-trunk, updated. rt-3.8.8-578-ga7a0e02

Jesse Vincent jesse at bestpractical.com
Wed Aug 25 19:40:36 EDT 2010


The branch, 3.9-trunk has been updated
       via  a7a0e02c14e1da249dc1a28faf55a64dcacab3d0 (commit)
       via  c888e1ab7ed2b98f9c334a80b0010476bbcda28c (commit)
       via  edf6f1b3adece12ceb53dab08a76a6420243af64 (commit)
       via  76ca01be6bff497b5130881b120d954f51b1aec1 (commit)
       via  c704f04e24c315c341665b976f68fe35ad3fceed (commit)
       via  b6c8b3085ac2ac8f6fd49d04b9add0ce5c086a9f (commit)
       via  aa5aa4fa1d75e37c63e810ecc3056d0ce8203847 (commit)
       via  522b3841925b24c2da320aff3abb7ff05f787463 (commit)
       via  b7b85a8dce5661094a8984980c7b6729318f9faa (commit)
       via  bd4ec06c73d87b50f281b19afffe5ca05ad56d60 (commit)
       via  717d44adfeb73a74defb095e00de79ba682f0070 (commit)
      from  c2d87516b5d947ff3aaa74bd50547501b555ccef (commit)

Summary of changes:
 bin/rt-mailgate.in                    |  292 +++++++++++++++++++++------------
 lib/RT/Test.pm                        |   22 +++-
 share/html/Admin/Elements/ShowKeyInfo |    1 +
 t/mail/gateway.t                      |   36 ++--
 4 files changed, 228 insertions(+), 123 deletions(-)

- Log -----------------------------------------------------------------
commit 717d44adfeb73a74defb095e00de79ba682f0070
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Aug 25 15:25:59 2010 -0400

    Cache GetKeyInfo for 2 minutes to avoid many, many heavy calls to gnupg
    (especially while running tests).

diff --git a/share/html/Admin/Elements/ShowKeyInfo b/share/html/Admin/Elements/ShowKeyInfo
index a6afa29..f6aa0ce 100644
--- a/share/html/Admin/Elements/ShowKeyInfo
+++ b/share/html/Admin/Elements/ShowKeyInfo
@@ -78,6 +78,7 @@ $EmailAddress
 $Type => 'public'
 </%ARGS>
 <%INIT>
+return if ($m->cache_self( key => join("||",$EmailAddress,$Type), expires_in => '2 minutes'));
 require RT::Crypt::GnuPG;
 my %res = RT::Crypt::GnuPG::GetKeyInfo( $EmailAddress, $Type );
 

commit bd4ec06c73d87b50f281b19afffe5ca05ad56d60
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Aug 25 15:44:47 2010 -0400

    start refactoring rt-mailgate so we can use it as a library

diff --git a/bin/rt-mailgate.in b/bin/rt-mailgate.in
index ac2400e..7f69d5d 100755
--- a/bin/rt-mailgate.in
+++ b/bin/rt-mailgate.in
@@ -66,75 +66,98 @@ use constant BUFFER_SIZE => 8192;
 my %opts;
 GetOptions( \%opts, "queue=s", "action=s", "url=s", "jar=s", "help", "debug", "extension=s", "timeout=i" );
 
-if ( $opts{'help'} ) {
-    require Pod::Usage;
-    Pod::Usage::pod2usage({verbose =>2});
-    exit 1;    # Don't want to succeed if this is really an email!
-}
+validate_args();
 
-unless ( $opts{'url'} ) {
-    print STDERR "$0 invoked improperly\n\nNo 'url' provided to mail gateway!\n";
-    exit 1;
-}
+my $ua;
 
-my $ua = new LWP::UserAgent;
-$ua->cookie_jar( { file => $opts{'jar'} } ) if $opts{'jar'};
-
-my %args = (
-    SessionType => 'REST', # Surpress login box
-);
-foreach ( qw(queue action) ) {
-    $args{$_} = $opts{$_} if defined $opts{$_};
-};
-
-if ( ($opts{'extension'} || '') =~ /^(?:action|queue|ticket)$/i ) {
-    $args{ lc $opts{'extension'} } = $ENV{'EXTENSION'} || $opts{$opts{'extension'}};
-} elsif ( $opts{'extension'} && $ENV{'EXTENSION'} ) {
-    print STDERR "Value of the --extension argument is not action, queue or ticket"
-        .", but environment variable EXTENSION is also defined. The former is ignored.\n";
-}
 
-# add ENV{'EXTENSION'} as X-RT-MailExtension to the message header
-if ( my $value = ( $ENV{'EXTENSION'} || $opts{'extension'} ) ) {
-    # prepare value to avoid MIME format breakage
-    # strip trailing newline symbols
-    $value =~ s/(\r*\n)+$//;
-    # make a correct multiline header field,
-    # with tabs in the beginning of each line
-    $value =~ s/(\r*\n)/$1\t/g;
-    $opts{'headers'} .= "X-RT-Mail-Extension: $value\n";
+
+
+my %args = ();
+my %message;
+
+setup_session();
+
+upload_message();
+
+
+exit;
+
+sub validate_args {
+    if ( $opts{'help'} ) {
+        require Pod::Usage;
+        Pod::Usage::pod2usage( { verbose => 2 } );
+        exit 1;    # Don't want to succeed if this is really an email!
+    }
+
+    unless ( $opts{'url'} ) {
+        print STDERR
+            "$0 invoked improperly\n\nNo 'url' provided to mail gateway!\n";
+        exit 1;
+    }
 }
 
-# Read the message in from STDIN
-my %message = write_down_message();
-unless( $message{'filename'} ) {
-    $args{'message'} = [
-        undef, '',
-        'Content-Type' => 'application/octet-stream',
-        Content => ${ $message{'content'} },
-    ];
-} else {
-    $args{'message'} = [
-        $message{'filename'}, '',
-        'Content-Type' => 'application/octet-stream',
-    ];
+sub setup_session {
+    $ua = LWP::UserAgent->new();
+    $ua->cookie_jar( { file => $opts{'jar'} } ) if $opts{'jar'};
+    $args{SessionType} = 'REST';    # Surpress login box
+    foreach (qw(queue action)) {
+        $args{$_} = $opts{$_} if defined $opts{$_};
+    }
+
+    if ( ( $opts{'extension'} || '' ) =~ /^(?:action|queue|ticket)$/i ) {
+        $args{ lc $opts{'extension'} } = $ENV{'EXTENSION'}
+            || $opts{ $opts{'extension'} };
+    } elsif ( $opts{'extension'} && $ENV{'EXTENSION'} ) {
+        print STDERR
+            "Value of the --extension argument is not action, queue or ticket"
+            . ", but environment variable EXTENSION is also defined. The former is ignored.\n";
+    }
+
+    # add ENV{'EXTENSION'} as X-RT-MailExtension to the message header
+    if ( my $value = ( $ENV{'EXTENSION'} || $opts{'extension'} ) ) {
+
+        # prepare value to avoid MIME format breakage
+        # strip trailing newline symbols
+        $value =~ s/(\r*\n)+$//;
+
+        # make a correct multiline header field,
+        # with tabs in the beginning of each line
+        $value =~ s/(\r*\n)/$1\t/g;
+        $opts{'headers'} .= "X-RT-Mail-Extension: $value\n";
+    }
+
+    # Read the message in from STDIN
+    %message = write_down_message();
+    unless ( $message{'filename'} ) {
+        $args{'message'} = [ undef, '',
+                             'Content-Type' => 'application/octet-stream',
+                             Content        => ${ $message{'content'} },
+                           ];
+    } else {
+        $args{'message'} = [ $message{'filename'}, '',
+                             'Content-Type' => 'application/octet-stream',
+                           ];
+    }
+
 }
 
-my $full_url = $opts{'url'}. "/REST/1.0/NoAuth/mail-gateway";
-print STDERR "$0: connecting to $full_url\n" if $opts{'debug'};
+sub upload_message {
+    my $full_url = $opts{'url'} . "/REST/1.0/NoAuth/mail-gateway";
+    print STDERR "$0: connecting to $full_url\n" if $opts{'debug'};
 
-$ua->timeout( exists( $opts{'timeout'} )? $opts{'timeout'}: 180 );
-my $r = $ua->post( $full_url, \%args, Content_Type => 'form-data' );
-check_failure($r);
+    $ua->timeout( exists( $opts{'timeout'} ) ? $opts{'timeout'} : 180 );
+    my $r = $ua->post( $full_url, \%args, Content_Type => 'form-data' );
+    check_failure($r);
 
-my $content = $r->content;
-print STDERR $content ."\n" if $opts{'debug'};
+    my $content = $r->content;
+    print STDERR $content . "\n" if $opts{'debug'};
 
-if ( $content !~ /^(ok|not ok)/ ) {
+    if ( $content !~ /^(ok|not ok)/ ) {
 
-    # It's not the server's fault if the mail is bogus. We just want to know that
-    # *something* came out of the server.
-    print STDERR <<EOF;
+ # It's not the server's fault if the mail is bogus. We just want to know that
+ # *something* came out of the server.
+        print STDERR <<EOF;
 RT server error.
 
 The RT server which handled your email did not behave as expected. It
@@ -143,16 +166,14 @@ said:
 $content
 EOF
 
-    exit EX_TEMPFAIL;
+        exit EX_TEMPFAIL;
+    }
 }
 
-exit;
-
 END {
     unlink $message{'filename'} if $message{'filename'};
 }
 
-
 sub check_failure {
     my $r = shift;
     return if $r->is_success;

commit b7b85a8dce5661094a8984980c7b6729318f9faa
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Aug 25 16:04:44 2010 -0400

    further refactoring rt-mailgate for better subroutine isolation

diff --git a/bin/rt-mailgate.in b/bin/rt-mailgate.in
index 7f69d5d..9a0137a 100755
--- a/bin/rt-mailgate.in
+++ b/bin/rt-mailgate.in
@@ -58,39 +58,35 @@ use warnings;
 use Getopt::Long;
 use LWP::UserAgent;
 use HTTP::Request::Common qw($DYNAMIC_FILE_UPLOAD);
+use File::Temp qw(tempfile tempdir);
 $DYNAMIC_FILE_UPLOAD = 1;
 
 use constant EX_TEMPFAIL => 75;
 use constant BUFFER_SIZE => 8192;
 
-my %opts;
-GetOptions( \%opts, "queue=s", "action=s", "url=s", "jar=s", "help", "debug", "extension=s", "timeout=i" );
+my $opts = {};
+GetOptions( $opts, "queue=s", "action=s", "url=s", "jar=s", "help", "debug", "extension=s", "timeout=i" );
 
-validate_args();
+validate_post_params();
 
 my $ua;
-
-
-
-
-my %args = ();
+my %post_params = ();
 my %message;
 
-setup_session();
-
-upload_message();
+setup_session($opts);
+upload_message(\%post_params);
 
 
 exit;
 
-sub validate_args {
-    if ( $opts{'help'} ) {
+sub validate_post_params {
+    if ( $opts->{'help'} ) {
         require Pod::Usage;
         Pod::Usage::pod2usage( { verbose => 2 } );
         exit 1;    # Don't want to succeed if this is really an email!
     }
 
-    unless ( $opts{'url'} ) {
+    unless ( $opts->{'url'} ) {
         print STDERR
             "$0 invoked improperly\n\nNo 'url' provided to mail gateway!\n";
         exit 1;
@@ -98,24 +94,25 @@ sub validate_args {
 }
 
 sub setup_session {
+    my $opts = shift;
     $ua = LWP::UserAgent->new();
-    $ua->cookie_jar( { file => $opts{'jar'} } ) if $opts{'jar'};
-    $args{SessionType} = 'REST';    # Surpress login box
+    $ua->cookie_jar( { file => $opts->{'jar'} } ) if $opts->{'jar'};
+    $post_params{SessionType} = 'REST';    # Surpress login box
     foreach (qw(queue action)) {
-        $args{$_} = $opts{$_} if defined $opts{$_};
+        $post_params{$_} = $opts->{$_} if defined $opts->{$_};
     }
 
-    if ( ( $opts{'extension'} || '' ) =~ /^(?:action|queue|ticket)$/i ) {
-        $args{ lc $opts{'extension'} } = $ENV{'EXTENSION'}
-            || $opts{ $opts{'extension'} };
-    } elsif ( $opts{'extension'} && $ENV{'EXTENSION'} ) {
+    if ( ( $opts->{'extension'} || '' ) =~ /^(?:action|queue|ticket)$/i ) {
+        $post_params{ lc $opts->{'extension'} } = $ENV{'EXTENSION'}
+            || $opts->{ $opts->{'extension'} };
+    } elsif ( $opts->{'extension'} && $ENV{'EXTENSION'} ) {
         print STDERR
             "Value of the --extension argument is not action, queue or ticket"
             . ", but environment variable EXTENSION is also defined. The former is ignored.\n";
     }
 
     # add ENV{'EXTENSION'} as X-RT-MailExtension to the message header
-    if ( my $value = ( $ENV{'EXTENSION'} || $opts{'extension'} ) ) {
+    if ( my $value = ( $ENV{'EXTENSION'} || $opts->{'extension'} ) ) {
 
         # prepare value to avoid MIME format breakage
         # strip trailing newline symbols
@@ -124,18 +121,18 @@ sub setup_session {
         # make a correct multiline header field,
         # with tabs in the beginning of each line
         $value =~ s/(\r*\n)/$1\t/g;
-        $opts{'headers'} .= "X-RT-Mail-Extension: $value\n";
+        $opts->{'headers'} .= "X-RT-Mail-Extension: $value\n";
     }
 
     # Read the message in from STDIN
-    %message = write_down_message();
-    unless ( $message{'filename'} ) {
-        $args{'message'} = [ undef, '',
+    my $message = slurp_message();
+    unless ( $message->{'filename'} ) {
+        $post_params{'message'} = [ undef, '',
                              'Content-Type' => 'application/octet-stream',
-                             Content        => ${ $message{'content'} },
+                             Content        => ${ $message->{'content'} },
                            ];
     } else {
-        $args{'message'} = [ $message{'filename'}, '',
+        $post_params{'message'} = [ $message->{'filename'}, '',
                              'Content-Type' => 'application/octet-stream',
                            ];
     }
@@ -143,15 +140,16 @@ sub setup_session {
 }
 
 sub upload_message {
-    my $full_url = $opts{'url'} . "/REST/1.0/NoAuth/mail-gateway";
-    print STDERR "$0: connecting to $full_url\n" if $opts{'debug'};
+    my $post_params = shift;
+    my $full_url = $opts->{'url'} . "/REST/1.0/NoAuth/mail-gateway";
+    print STDERR "$0: connecting to $full_url\n" if $opts->{'debug'};
 
-    $ua->timeout( exists( $opts{'timeout'} ) ? $opts{'timeout'} : 180 );
-    my $r = $ua->post( $full_url, \%args, Content_Type => 'form-data' );
+    $ua->timeout( exists( $opts->{'timeout'} ) ? $opts->{'timeout'} : 180 );
+    my $r = $ua->post( $full_url, $post_params, Content_Type => 'form-data' );
     check_failure($r);
 
     my $content = $r->content;
-    print STDERR $content . "\n" if $opts{'debug'};
+    print STDERR $content . "\n" if $opts->{'debug'};
 
     if ( $content !~ /^(ok|not ok)/ ) {
 
@@ -170,9 +168,6 @@ EOF
     }
 }
 
-END {
-    unlink $message{'filename'} if $message{'filename'};
-}
 
 sub check_failure {
     my $r = shift;
@@ -193,15 +188,15 @@ sub check_failure {
         rightmargin => 50,
     );
     print STDERR $formatter->format( $tree );
-    print STDERR "\n$0: undefined server error\n" if $opts{'debug'};
+    print STDERR "\n$0: undefined server error\n" if $opts->{'debug'};
     exit EX_TEMPFAIL;
 }
 
-sub write_down_message {
-    use File::Temp qw(tempfile);
-
+sub slurp_message {
     local $@;
-    my ($fh, $filename) = eval { tempfile() };
+
+    my %message;
+    my ($fh, $filename) = eval { tempfile(DIR => tempdir(CLEANUP =>1)) };
     if ( !$fh || $@ ) {
         print STDERR "$0: Couldn't create temp file, using memory\n";
         print STDERR "error: $@\n" if $@;
@@ -211,14 +206,14 @@ sub write_down_message {
             print STDERR "$0: no message passed on STDIN\n";
             exit 0;
         }
-        $$message = $opts{'headers'} . $$message if $opts{'headers'};
-        return ( content => $message );
+        $$message = $opts->{'headers'} . $$message if $opts->{'headers'};
+        return ( {content => $message} );
     }
 
     binmode $fh;
     binmode \*STDIN;
     
-    print $fh $opts{'headers'} if $opts{'headers'};
+    print $fh $opts->{'headers'} if $opts->{'headers'};
 
     my $buf; my $empty = 1;
     while(1) {
@@ -238,8 +233,8 @@ sub write_down_message {
         print STDERR "$0: no message passed on STDIN\n";
         exit 0;
     }
-    print STDERR "$0: temp file is '$filename'\n" if $opts{'debug'};
-    return (filename => $filename);
+    print STDERR "$0: temp file is '$filename'\n" if $opts->{'debug'};
+    return ({filename => $filename});
 }
 
 

commit 522b3841925b24c2da320aff3abb7ff05f787463
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Aug 25 16:05:41 2010 -0400

    perltidy

diff --git a/bin/rt-mailgate.in b/bin/rt-mailgate.in
index 9a0137a..9f84216 100755
--- a/bin/rt-mailgate.in
+++ b/bin/rt-mailgate.in
@@ -1,41 +1,41 @@
 #!@PERL@ -w
 # BEGIN BPS TAGGED BLOCK {{{
-# 
+#
 # COPYRIGHT:
-# 
+#
 # This software is Copyright (c) 1996-2010 Best Practical Solutions, LLC
 #                                          <jesse at bestpractical.com>
-# 
+#
 # (Except where explicitly superseded by other copyright notices)
-# 
-# 
+#
+#
 # LICENSE:
-# 
+#
 # This work is made available to you under the terms of Version 2 of
 # the GNU General Public License. A copy of that license should have
 # been provided with this software, but in any event can be snarfed
 # from www.gnu.org.
-# 
+#
 # This work is distributed in the hope that it will be useful, but
 # WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 # General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301 or visit their web page on the internet at
 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
-# 
-# 
+#
+#
 # CONTRIBUTION SUBMISSION POLICY:
-# 
+#
 # (The following paragraph is not intended to limit the rights granted
 # to you to modify and distribute this software under the terms of
 # the GNU General Public License and is only of importance to you if
 # you choose to contribute your changes and enhancements to the
 # community by submitting them to Best Practical Solutions, LLC.)
-# 
+#
 # By intentionally submitting any modifications, corrections or
 # derivatives to this work, or any other work intended for use with
 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
@@ -44,8 +44,9 @@
 # royalty-free, perpetual, license to use, copy, create derivative
 # works based on those contributions, and sublicense and distribute
 # those contributions and any derivatives thereof.
-# 
+#
 # END BPS TAGGED BLOCK }}}
+
 =head1 NAME
 
 rt-mailgate - Mail interface to RT3.
@@ -65,7 +66,10 @@ use constant EX_TEMPFAIL => 75;
 use constant BUFFER_SIZE => 8192;
 
 my $opts = {};
-GetOptions( $opts, "queue=s", "action=s", "url=s", "jar=s", "help", "debug", "extension=s", "timeout=i" );
+GetOptions( $opts,   "queue=s", "action=s", "url=s",
+            "jar=s", "help",    "debug",    "extension=s",
+            "timeout=i"
+          );
 
 validate_post_params();
 
@@ -74,8 +78,7 @@ my %post_params = ();
 my %message;
 
 setup_session($opts);
-upload_message(\%post_params);
-
+upload_message( \%post_params );
 
 exit;
 
@@ -127,21 +130,23 @@ sub setup_session {
     # Read the message in from STDIN
     my $message = slurp_message();
     unless ( $message->{'filename'} ) {
-        $post_params{'message'} = [ undef, '',
-                             'Content-Type' => 'application/octet-stream',
-                             Content        => ${ $message->{'content'} },
-                           ];
+        $post_params{'message'} = [
+                                 undef, '',
+                                 'Content-Type' => 'application/octet-stream',
+                                 Content        => ${ $message->{'content'} },
+        ];
     } else {
-        $post_params{'message'} = [ $message->{'filename'}, '',
-                             'Content-Type' => 'application/octet-stream',
-                           ];
+        $post_params{'message'} = [
+                                 $message->{'filename'}, '',
+                                 'Content-Type' => 'application/octet-stream',
+        ];
     }
 
 }
 
 sub upload_message {
     my $post_params = shift;
-    my $full_url = $opts->{'url'} . "/REST/1.0/NoAuth/mail-gateway";
+    my $full_url    = $opts->{'url'} . "/REST/1.0/NoAuth/mail-gateway";
     print STDERR "$0: connecting to $full_url\n" if $opts->{'debug'};
 
     $ua->timeout( exists( $opts->{'timeout'} ) ? $opts->{'timeout'} : 180 );
@@ -168,7 +173,6 @@ EOF
     }
 }
 
-
 sub check_failure {
     my $r = shift;
     return if $r->is_success;
@@ -179,15 +183,14 @@ sub check_failure {
     require HTML::FormatText;
 
     my $error = $r->error_as_HTML;
-    my $tree  = HTML::TreeBuilder->new->parse( $error );
+    my $tree  = HTML::TreeBuilder->new->parse($error);
     $tree->eof;
 
     # It'll be a cold day in hell before RT sends out bounces in HTML
-    my $formatter = HTML::FormatText->new(
-        leftmargin  => 0,
-        rightmargin => 50,
-    );
-    print STDERR $formatter->format( $tree );
+    my $formatter =
+        HTML::FormatText->new( leftmargin  => 0,
+                               rightmargin => 50, );
+    print STDERR $formatter->format($tree);
     print STDERR "\n$0: undefined server error\n" if $opts->{'debug'};
     exit EX_TEMPFAIL;
 }
@@ -196,27 +199,29 @@ sub slurp_message {
     local $@;
 
     my %message;
-    my ($fh, $filename) = eval { tempfile(DIR => tempdir(CLEANUP =>1)) };
+    my ( $fh, $filename )
+        = eval { tempfile( DIR => tempdir( CLEANUP => 1 ) ) };
     if ( !$fh || $@ ) {
         print STDERR "$0: Couldn't create temp file, using memory\n";
         print STDERR "error: $@\n" if $@;
 
-        my $message = \do { local (@ARGV, $/); <> };
+        my $message = \do { local ( @ARGV, $/ ); <> };
         unless ( $$message =~ /\S/ ) {
             print STDERR "$0: no message passed on STDIN\n";
             exit 0;
         }
         $$message = $opts->{'headers'} . $$message if $opts->{'headers'};
-        return ( {content => $message} );
+        return ( { content => $message } );
     }
 
     binmode $fh;
     binmode \*STDIN;
-    
+
     print $fh $opts->{'headers'} if $opts->{'headers'};
 
-    my $buf; my $empty = 1;
-    while(1) {
+    my $buf;
+    my $empty = 1;
+    while (1) {
         my $status = read \*STDIN, $buf, BUFFER_SIZE;
         unless ( defined $status ) {
             print STDERR "$0: couldn't read message: $!\n";
@@ -226,18 +231,17 @@ sub slurp_message {
         }
         $empty = 0 if $buf =~ /\S/;
         print $fh $buf;
-    };
+    }
     close $fh;
 
-    if ( $empty ) {
+    if ($empty) {
         print STDERR "$0: no message passed on STDIN\n";
         exit 0;
     }
     print STDERR "$0: temp file is '$filename'\n" if $opts->{'debug'};
-    return ({filename => $filename});
+    return ( { filename => $filename } );
 }
 
-
 =head1 SYNOPSIS
 
     rt-mailgate --help : this text

commit aa5aa4fa1d75e37c63e810ecc3056d0ce8203847
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Aug 25 16:06:40 2010 -0400

    fixed a mistaken rename of a validation sub

diff --git a/bin/rt-mailgate.in b/bin/rt-mailgate.in
index 9f84216..a344f4d 100755
--- a/bin/rt-mailgate.in
+++ b/bin/rt-mailgate.in
@@ -71,7 +71,7 @@ GetOptions( $opts,   "queue=s", "action=s", "url=s",
             "timeout=i"
           );
 
-validate_post_params();
+validate_cli_flags();
 
 my $ua;
 my %post_params = ();
@@ -82,7 +82,7 @@ upload_message( \%post_params );
 
 exit;
 
-sub validate_post_params {
+sub validate_cli_flags {
     if ( $opts->{'help'} ) {
         require Pod::Usage;
         Pod::Usage::pod2usage( { verbose => 2 } );

commit b6c8b3085ac2ac8f6fd49d04b9add0ce5c086a9f
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Aug 25 16:14:00 2010 -0400

    further teasing apart of rt-mailgate

diff --git a/bin/rt-mailgate.in b/bin/rt-mailgate.in
index a344f4d..aa390ca 100755
--- a/bin/rt-mailgate.in
+++ b/bin/rt-mailgate.in
@@ -73,12 +73,11 @@ GetOptions( $opts,   "queue=s", "action=s", "url=s",
 
 validate_cli_flags();
 
-my $ua;
-my %post_params = ();
 my %message;
 
-setup_session($opts);
-upload_message( \%post_params );
+my $ua          = get_useragent($opts);
+my $post_params = setup_session($opts);
+upload_message( $ua => $post_params );
 
 exit;
 
@@ -96,10 +95,16 @@ sub validate_cli_flags {
     }
 }
 
-sub setup_session {
+sub get_useragent {
     my $opts = shift;
     $ua = LWP::UserAgent->new();
     $ua->cookie_jar( { file => $opts->{'jar'} } ) if $opts->{'jar'};
+    return $ua;
+}
+
+sub setup_session {
+    my $opts = shift;
+    my %post_params;
     $post_params{SessionType} = 'REST';    # Surpress login box
     foreach (qw(queue action)) {
         $post_params{$_} = $opts->{$_} if defined $opts->{$_};
@@ -142,9 +147,11 @@ sub setup_session {
         ];
     }
 
+    return \%post_params;
 }
 
 sub upload_message {
+    my $ua          = shift;
     my $post_params = shift;
     my $full_url    = $opts->{'url'} . "/REST/1.0/NoAuth/mail-gateway";
     print STDERR "$0: connecting to $full_url\n" if $opts->{'debug'};

commit c704f04e24c315c341665b976f68fe35ad3fceed
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Aug 25 16:15:54 2010 -0400

    remove unused variable

diff --git a/bin/rt-mailgate.in b/bin/rt-mailgate.in
index aa390ca..2f26eff 100755
--- a/bin/rt-mailgate.in
+++ b/bin/rt-mailgate.in
@@ -73,8 +73,6 @@ GetOptions( $opts,   "queue=s", "action=s", "url=s",
 
 validate_cli_flags();
 
-my %message;
-
 my $ua          = get_useragent($opts);
 my $post_params = setup_session($opts);
 upload_message( $ua => $post_params );

commit 76ca01be6bff497b5130881b120d954f51b1aec1
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Aug 25 16:17:03 2010 -0400

    slightly better isolation for argument checking in rt-mailgate

diff --git a/bin/rt-mailgate.in b/bin/rt-mailgate.in
index 2f26eff..3263fdb 100755
--- a/bin/rt-mailgate.in
+++ b/bin/rt-mailgate.in
@@ -71,7 +71,7 @@ GetOptions( $opts,   "queue=s", "action=s", "url=s",
             "timeout=i"
           );
 
-validate_cli_flags();
+validate_cli_flags($opts);
 
 my $ua          = get_useragent($opts);
 my $post_params = setup_session($opts);
@@ -80,6 +80,7 @@ upload_message( $ua => $post_params );
 exit;
 
 sub validate_cli_flags {
+    my $opts = shift;
     if ( $opts->{'help'} ) {
         require Pod::Usage;
         Pod::Usage::pod2usage( { verbose => 2 } );

commit edf6f1b3adece12ceb53dab08a76a6420243af64
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Aug 25 16:20:45 2010 -0400

    Librarize the actual meat of the rt-mailgate

diff --git a/bin/rt-mailgate.in b/bin/rt-mailgate.in
index 3263fdb..841f642 100755
--- a/bin/rt-mailgate.in
+++ b/bin/rt-mailgate.in
@@ -57,6 +57,19 @@ use strict;
 use warnings;
 
 use Getopt::Long;
+
+my $opts = {};
+GetOptions( $opts,   "queue=s", "action=s", "url=s",
+            "jar=s", "help",    "debug",    "extension=s",
+            "timeout=i"
+          );
+
+RT::Client::MailGateway->run($opts);
+
+exit;
+
+package RT::Client::MailGateway;
+
 use LWP::UserAgent;
 use HTTP::Request::Common qw($DYNAMIC_FILE_UPLOAD);
 use File::Temp qw(tempfile tempdir);
@@ -65,19 +78,16 @@ $DYNAMIC_FILE_UPLOAD = 1;
 use constant EX_TEMPFAIL => 75;
 use constant BUFFER_SIZE => 8192;
 
-my $opts = {};
-GetOptions( $opts,   "queue=s", "action=s", "url=s",
-            "jar=s", "help",    "debug",    "extension=s",
-            "timeout=i"
-          );
-
-validate_cli_flags($opts);
+sub run {
+    my $class = shift;
+    my $opts  = shift;
 
-my $ua          = get_useragent($opts);
-my $post_params = setup_session($opts);
-upload_message( $ua => $post_params );
+    validate_cli_flags($opts);
 
-exit;
+    my $ua          = get_useragent($opts);
+    my $post_params = setup_session($opts);
+    upload_message( $ua => $post_params );
+}
 
 sub validate_cli_flags {
     my $opts = shift;
@@ -96,7 +106,7 @@ sub validate_cli_flags {
 
 sub get_useragent {
     my $opts = shift;
-    $ua = LWP::UserAgent->new();
+    my $ua   = LWP::UserAgent->new();
     $ua->cookie_jar( { file => $opts->{'jar'} } ) if $opts->{'jar'};
     return $ua;
 }

commit c888e1ab7ed2b98f9c334a80b0010476bbcda28c
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Aug 25 18:54:49 2010 -0400

    make the rt-mailgate class really a class, so we can store classdata.

diff --git a/bin/rt-mailgate.in b/bin/rt-mailgate.in
index 841f642..a8fbc4e 100755
--- a/bin/rt-mailgate.in
+++ b/bin/rt-mailgate.in
@@ -64,9 +64,9 @@ GetOptions( $opts,   "queue=s", "action=s", "url=s",
             "timeout=i"
           );
 
-RT::Client::MailGateway->run($opts);
+my $gateway = RT::Client::MailGateway->new();
 
-exit;
+$gateway->run($opts);
 
 package RT::Client::MailGateway;
 
@@ -78,33 +78,76 @@ $DYNAMIC_FILE_UPLOAD = 1;
 use constant EX_TEMPFAIL => 75;
 use constant BUFFER_SIZE => 8192;
 
-sub run {
+sub new {
     my $class = shift;
-    my $opts  = shift;
+    my $self = bless {}, $class;
+    return $self;
+}
+
+sub run {
+    my $self = shift;
+    my $opts = shift;
+
+    if ( $opts->{running_in_test_harness} ) {
+        $self->{running_in_test_harness} = 1;
+    }
 
-    validate_cli_flags($opts);
+    $self->validate_cli_flags($opts);
 
-    my $ua          = get_useragent($opts);
-    my $post_params = setup_session($opts);
-    upload_message( $ua => $post_params );
+    my $ua          = $self->get_useragent($opts);
+    my $post_params = $self->setup_session($opts);
+    $self->upload_message( $ua => $post_params );
+    $self->exit_with_success();
+}
+
+sub exit_with_success {
+    my $self = shift;
+    if ( $self->{running_in_test_harness} ) {
+        return 1;
+    } else {
+        exit 0;
+    }
+}
+
+sub tempfail {
+    my $self = shift;
+    if ( $self->{running_in_test_harness} ) {
+        die "tempfail";
+    } else {
+
+        exit EX_TEMPFAIL;
+    }
+}
+
+sub permfail {
+    my $self = shift;
+    if ( $self->{running_in_test_harness} ) {
+        die "permfail";
+    } else {
+
+        exit 1;
+    }
 }
 
 sub validate_cli_flags {
+    my $self = shift;
     my $opts = shift;
     if ( $opts->{'help'} ) {
         require Pod::Usage;
         Pod::Usage::pod2usage( { verbose => 2 } );
-        exit 1;    # Don't want to succeed if this is really an email!
+        return $self->permfail()
+            ;    # Don't want to succeed if this is really an email!
     }
 
     unless ( $opts->{'url'} ) {
         print STDERR
             "$0 invoked improperly\n\nNo 'url' provided to mail gateway!\n";
-        exit 1;
+        return $self->permfail();
     }
 }
 
 sub get_useragent {
+    my $self = shift;
     my $opts = shift;
     my $ua   = LWP::UserAgent->new();
     $ua->cookie_jar( { file => $opts->{'jar'} } ) if $opts->{'jar'};
@@ -112,6 +155,7 @@ sub get_useragent {
 }
 
 sub setup_session {
+    my $self = shift;
     my $opts = shift;
     my %post_params;
     $post_params{SessionType} = 'REST';    # Surpress login box
@@ -142,7 +186,8 @@ sub setup_session {
     }
 
     # Read the message in from STDIN
-    my $message = slurp_message();
+    # _raw_message is used for testing
+    my $message = $opts->{'_raw_message'} || $self->slurp_message();
     unless ( $message->{'filename'} ) {
         $post_params{'message'} = [
                                  undef, '',
@@ -160,6 +205,7 @@ sub setup_session {
 }
 
 sub upload_message {
+    my $self        = shift;
     my $ua          = shift;
     my $post_params = shift;
     my $full_url    = $opts->{'url'} . "/REST/1.0/NoAuth/mail-gateway";
@@ -167,16 +213,16 @@ sub upload_message {
 
     $ua->timeout( exists( $opts->{'timeout'} ) ? $opts->{'timeout'} : 180 );
     my $r = $ua->post( $full_url, $post_params, Content_Type => 'form-data' );
-    check_failure($r);
+    $self->check_failure($r);
 
     my $content = $r->content;
     print STDERR $content . "\n" if $opts->{'debug'};
 
-    if ( $content !~ /^(ok|not ok)/ ) {
+    return if ( $content =~ /^(ok|not ok)/ );
 
  # It's not the server's fault if the mail is bogus. We just want to know that
  # *something* came out of the server.
-        print STDERR <<EOF;
+    print STDERR <<EOF;
 RT server error.
 
 The RT server which handled your email did not behave as expected. It
@@ -185,12 +231,12 @@ said:
 $content
 EOF
 
-        exit EX_TEMPFAIL;
-    }
+    return $self->tempfail();
 }
 
 sub check_failure {
-    my $r = shift;
+    my $self = shift;
+    my $r    = shift;
     return if $r->is_success;
 
     # This ordinarily oughtn't to be able to happen, suggests a bug in RT.
@@ -208,10 +254,12 @@ sub check_failure {
                                rightmargin => 50, );
     print STDERR $formatter->format($tree);
     print STDERR "\n$0: undefined server error\n" if $opts->{'debug'};
-    exit EX_TEMPFAIL;
+    return $self->tempfail();
 }
 
 sub slurp_message {
+    my $self = shift;
+
     local $@;
 
     my %message;
@@ -224,7 +272,7 @@ sub slurp_message {
         my $message = \do { local ( @ARGV, $/ ); <> };
         unless ( $$message =~ /\S/ ) {
             print STDERR "$0: no message passed on STDIN\n";
-            exit 0;
+            $self->exit_with_success;
         }
         $$message = $opts->{'headers'} . $$message if $opts->{'headers'};
         return ( { content => $message } );
@@ -241,7 +289,7 @@ sub slurp_message {
         my $status = read \*STDIN, $buf, BUFFER_SIZE;
         unless ( defined $status ) {
             print STDERR "$0: couldn't read message: $!\n";
-            exit EX_TEMPFAIL;
+            return $self->tempfail();
         } elsif ( !$status ) {
             last;
         }
@@ -252,7 +300,7 @@ sub slurp_message {
 
     if ($empty) {
         print STDERR "$0: no message passed on STDIN\n";
-        exit 0;
+        $self->exit_with_success;
     }
     print STDERR "$0: temp file is '$filename'\n" if $opts->{'debug'};
     return ( { filename => $filename } );

commit a7a0e02c14e1da249dc1a28faf55a64dcacab3d0
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Aug 25 19:36:47 2010 -0400

    Switch most of our rt-mailgate calls to skip the "spawn rt-mailgate and
    do an HTTP request" stage.

diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index 9acad53..5b8b741 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -773,7 +773,7 @@ sub run_and_capture {
     return ($?, $result);
 }
 
-sub send_via_mailgate {
+sub send_via_mailgate_and_http {
     my $self = shift;
     my $message = shift;
     my %args = (@_);
@@ -796,6 +796,26 @@ sub send_via_mailgate {
     return ($status, $id);
 }
 
+
+sub send_via_mailgate {
+    my $self    = shift;
+    my $message = shift;
+    my %args = ( action => 'correspond',
+                 queue  => 'General',
+                 @_
+               );
+
+    if ( UNIVERSAL::isa( $message, 'MIME::Entity' ) ) {
+        $message = $message->as_string;
+    }
+
+    my ( $status, $error_message, $ticket )
+        = RT::Interface::Email::Gateway( {%args, message => $message} );
+    return ( $status, $ticket->id );
+
+}
+
+
 sub open_mailgate_ok {
     my $class   = shift;
     my $baseurl = shift;
diff --git a/t/mail/gateway.t b/t/mail/gateway.t
index 1e27eb3..6ff1911 100644
--- a/t/mail/gateway.t
+++ b/t/mail/gateway.t
@@ -79,7 +79,7 @@ Subject: This is a test of new ticket creation
 
 Foob!
 EOF
-    my ($status, $id) = RT::Test->send_via_mailgate($text, url => undef);
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, url => undef);
     is ($status >> 8, 1, "The mail gateway exited with a failure");
     ok (!$id, "No ticket id") or diag "by mistake ticket #$id";
 }
@@ -93,7 +93,7 @@ Subject: This is a test of new ticket creation
 
 Foob!
 EOF
-    my ($status, $id) = RT::Test->send_via_mailgate($text, url => 'http://this.test.for.non-connection.is.expected.to.generate.an.error');
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, url => 'http://this.test.for.non-connection.is.expected.to.generate.an.error');
     is ($status >> 8, 75, "The mail gateway exited with a failure");
     ok (!$id, "No ticket id");
 }
@@ -121,7 +121,7 @@ Blah!
 Foob!
 EOF
 
-    my ($status, $id) = RT::Test->send_via_mailgate($text);
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok ($id, "Created ticket");
 
@@ -141,7 +141,7 @@ Blah!
 Foob!
 EOF
     local $ENV{'EXTENSION'} = "bad value with\nnewlines\n";
-    my ($status, $id) = RT::Test->send_via_mailgate($text);
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok ($id, "Created ticket #$id");
 
@@ -176,7 +176,7 @@ Subject: This is a test of new ticket creation
 
 Foob!
 EOF
-    my ($status, $id) = RT::Test->send_via_mailgate($text, extension => 'some-extension-arg' );
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'some-extension-arg' );
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok ($id, "Created ticket #$id");
 
@@ -210,7 +210,7 @@ Subject: using mailgate without --action arg
 Blah!
 Foob!
 EOF
-    my ($status, $id) = RT::Test->send_via_mailgate($text, extension => 'some-extension-arg' );
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'some-extension-arg' );
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok ($id, "Created ticket #$id");
 
@@ -230,7 +230,7 @@ Subject: This is a test of new ticket creation as an unknown user
 Blah!
 Foob!
 EOF
-    my ($status, $id) = RT::Test->send_via_mailgate($text);
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok (!$id, "no ticket created");
 
@@ -264,7 +264,7 @@ Subject: This is a test of new ticket creation as an unknown user
 Blah!
 Foob!
 EOF
-    my ($status, $id) = RT::Test->send_via_mailgate($text);
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok ($id, "ticket created");
 
@@ -290,7 +290,7 @@ Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a reply
 Blah!  (Should not work.)
 Foob!
 EOF
-    my ($status, $id) = RT::Test->send_via_mailgate($text);
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok (!$id, "no way to reply to the ticket");
 
@@ -318,7 +318,7 @@ Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a reply
 Blah!
 Foob!
 EOF
-    my ($status, $id) = RT::Test->send_via_mailgate($text);
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
     is ($status >> 8, 0, "The mail gateway exited normally");
     is ($id, $ticket_id, "replied to the ticket");
 
@@ -338,7 +338,7 @@ Blah!
 Foob!
 EOF
     local $ENV{'EXTENSION'} = $ticket_id;
-    my ($status, $id) = RT::Test->send_via_mailgate($text, extension => 'ticket');
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'ticket');
     is ($status >> 8, 0, "The mail gateway exited normally");
     is ($id, $ticket_id, "replied to the ticket");
 
@@ -369,7 +369,7 @@ Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a commen
 Blah!  (Should not work.)
 Foob!
 EOF
-    my ($status, $id) = RT::Test->send_via_mailgate($text, action => 'comment');
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, action => 'comment');
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok (!$id, "no way to comment on the ticket");
 
@@ -398,7 +398,7 @@ Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a commen
 Blah!
 Foob!
 EOF
-    my ($status, $id) = RT::Test->send_via_mailgate($text, action => 'comment');
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, action => 'comment');
     is ($status >> 8, 0, "The mail gateway exited normally");
     is ($id, $ticket_id, "replied to the ticket");
 
@@ -418,7 +418,7 @@ Blah!
 Foob!
 EOF
     local $ENV{'EXTENSION'} = 'comment';
-    my ($status, $id) = RT::Test->send_via_mailgate($text, extension => 'action');
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'action');
     is ($status >> 8, 0, "The mail gateway exited normally");
     is ($id, $ticket_id, "added comment to the ticket");
 
@@ -463,7 +463,7 @@ diag "Testing preservation of binary attachments";
         Encoding => 'base64',
     );
     # Create a ticket with a binary attachment
-    my ($status, $id) = RT::Test->send_via_mailgate($entity);
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($entity);
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok ($id, "created ticket");
 
@@ -519,7 +519,7 @@ Content-Type: text/plain; charset="utf-8"
 \303\241\303\251\303\255\303\263\303\272
 bye
 EOF
-    my ($status, $id) = RT::Test->send_via_mailgate($text);
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok ($id, "created ticket");
 
@@ -555,7 +555,7 @@ Content-Type: text/plain; charset="utf-8"
 \303\241\303\251\303\255\303\263\303\272
 bye
 EOF
-    my ($status, $id) = RT::Test->send_via_mailgate($text);
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok ($id, "created ticket");
 
@@ -585,7 +585,7 @@ Content-Type: text/plain; charset="utf-8"
 
 test
 EOF
-    my ($status, $id) = RT::Test->send_via_mailgate($text);
+    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok ($id, "created ticket");
 

-----------------------------------------------------------------------


More information about the Rt-commit mailing list