RT-Client-CLI

 view release on metacpan or  search on metacpan

script/rt-mailgate  view on Meta::CPAN

    my $self        = shift;
    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'};

    $ua->timeout( exists( $opts->{'timeout'} ) ? $opts->{'timeout'} : 180 );
    my $r = $ua->post( $full_url, $post_params, Content_Type => 'form-data' );

    # Follow 3 redirects
    my $n = 0;
    while ($n++ < 3 and $r->is_redirect) {
        $full_url = $r->header( "Location" );
        $r = $ua->post( $full_url, $post_params, Content_Type => 'form-data' );
    }

    $self->check_failure($r);

    my $content = $r->content;
    print STDERR $content . "\n" if $opts->{'debug'};

    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;
RT server error.

The RT server which handled your email did not behave as expected. It
said:

$content
EOF

    return $self->tempfail();
}

sub check_failure {
    my $self = shift;
    my $r    = shift;
    return if $r->is_success;

    print STDERR "HTTP request failed: @{[ $r->status_line ]}. "
                ."Your webserver logs may have more information or there may be a network problem.\n";
    print STDERR "\n$0: undefined server error\n" if $opts->{'debug'};
    return $self->tempfail();
}

sub slurp_message {
    my $self = shift;

    local $@;

    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 $@;

        my $message = \do { local ( @ARGV, $/ ); <STDIN> };
        unless ( $$message =~ /\S/ ) {
            print STDERR "$0: no message passed on STDIN\n";
            $self->exit_with_success;
        }
        $$message = $opts->{'headers'} . $$message if $opts->{'headers'};
        return ( { content => $message } );
    }

    binmode $fh;
    binmode \*STDIN;

    print $fh $opts->{'headers'} if $opts->{'headers'};

    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";
            return $self->tempfail();
        } elsif ( !$status ) {
            last;
        }
        $empty = 0 if $buf =~ /\S/;
        print $fh $buf;
    }
    close $fh;

    if ($empty) {
        print STDERR "$0: no message passed on STDIN\n";
        $self->exit_with_success;
    }
    print STDERR "$0: temp file is '$filename'\n" if $opts->{'debug'};
    return ( { filename => $filename } );
}

=head1 SYNOPSIS

    rt-mailgate --help : this text

Usual invocation (from MTA):

    rt-mailgate --action (correspond|comment|...) --queue queuename
                --url http://your.rt.server
                [ --debug ]
                [ --extension (queue|action|ticket) ]
                [ --timeout seconds ]



=head1 OPTIONS

=over 3

=item C<--action>

Specifies what happens to email sent to this alias.  The available basic
actions are: C<correspond>, C<comment>.  Additional actions, such as
C<take> or C<resolve>, may be available depending on your local
C<@MailPlugins> configuration.



( run in 3.950 seconds using v1.01-cache-2.11-cpan-4ac696b4eb4 )