From 5c96d46d56f2066bb40d9a34c4db56f53f43c6f2 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 31 Dec 2009 14:00:35 +0000 Subject: merging 3.8.7!!! --- rt/bin/mason_handler.fcgi | 13 ++- rt/bin/mason_handler.scgi | 12 ++- rt/bin/mason_handler.svc | 13 ++- rt/bin/rt-crontool | 211 ++++++++++++++++++++++++++++++++++------------ rt/bin/rt-mailgate | 164 ++++++++++++++++++++++++++--------- 5 files changed, 308 insertions(+), 105 deletions(-) (limited to 'rt/bin') diff --git a/rt/bin/mason_handler.fcgi b/rt/bin/mason_handler.fcgi index 38f5901..8092455 100755 --- a/rt/bin/mason_handler.fcgi +++ b/rt/bin/mason_handler.fcgi @@ -2,8 +2,8 @@ # BEGIN BPS TAGGED BLOCK {{{ # # COPYRIGHT: -# -# This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC +# +# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC # # # (Except where explicitly superseded by other copyright notices) @@ -51,12 +51,17 @@ package RT::Mason; use strict; use vars '$Handler'; use File::Basename; -require ('/opt/rt3/bin/webmux.pl'); + +require (dirname(__FILE__) . '/webmux.pl'); # Enter CGI::Fast mode, which should also work as a vanilla CGI script. require CGI::Fast; RT::Init(); +$Handler ||= RT::Interface::Web::Handler->new( + RT->Config->Get('MasonParameters') +); + while ( my $cgi = CGI::Fast->new ) { # the whole point of fastcgi requires the env to get reset here.. @@ -67,7 +72,7 @@ while ( my $cgi = CGI::Fast->new ) { $ENV{'ENV'} = '' if defined $ENV{'ENV'}; $ENV{'IFS'} = '' if defined $ENV{'IFS'}; - Module::Refresh->refresh if $RT::DevelMode; + Module::Refresh->refresh if RT->Config->Get('DevelMode'); RT::ConnectToDatabase(); if ( ( !$Handler->interp->comp_exists( $cgi->path_info ) ) diff --git a/rt/bin/mason_handler.scgi b/rt/bin/mason_handler.scgi index faff8a5..1050866 100755 --- a/rt/bin/mason_handler.scgi +++ b/rt/bin/mason_handler.scgi @@ -2,8 +2,8 @@ # BEGIN BPS TAGGED BLOCK {{{ # # COPYRIGHT: -# -# This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC +# +# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC # # # (Except where explicitly superseded by other copyright notices) @@ -50,11 +50,17 @@ package RT::Mason; use strict; use vars '$Handler'; -require ('/opt/rt3/bin/webmux.pl'); +use File::Basename; + +require (dirname(__FILE__) . '/webmux.pl'); require CGI; RT::Init(); +$Handler ||= RT::Interface::Web::Handler->new( + RT->Config->Get('MasonParameters') +); + my $cgi = CGI->new; if ( ( !$Handler->interp->comp_exists( $cgi->path_info ) ) diff --git a/rt/bin/mason_handler.svc b/rt/bin/mason_handler.svc index fc97da9..4276b6e 100644 --- a/rt/bin/mason_handler.svc +++ b/rt/bin/mason_handler.svc @@ -2,8 +2,8 @@ # BEGIN BPS TAGGED BLOCK {{{ # # COPYRIGHT: -# -# This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC +# +# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC # # # (Except where explicitly superseded by other copyright notices) @@ -111,7 +111,7 @@ BEGIN { $Win32::TieRegistry::Registry->{ 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\\'. 'W3SVC\Parameters\Virtual Roots\\' - }->{$RT::WebPath || '/'} = "$path,,205"; + }->{RT->Config->Get('WebPath') || '/'} = "$path,,205"; $Win32::TieRegistry::Registry->{ 'HKEY_LOCAL_MACHINE\Software\FASTCGI\.html\\' @@ -225,13 +225,18 @@ warn "Begin listening on $ENV{'FCGI_SOCKET_PATH'}\n"; require CGI::Fast; RT::Init(); +$Handler ||= RT::Interface::Web::Handler->new( + RT->Config->Get('MasonParameters') +); + # Response loop while( my $cgi = CGI::Fast->new ) { my $comp = $ENV{'PATH_INFO'}; $comp = $1 if ($comp =~ /^(.*)$/); - $comp =~ s|^$RT::WebPath\b||i; + my $web_path = RT->Config->Get('WebPath'); + $comp =~ s|^\Q$web_path\E\b||i; $comp .= "index.html" if ($comp =~ /\/$/); $comp =~ s/.pl$/.html/g; diff --git a/rt/bin/rt-crontool b/rt/bin/rt-crontool index 8fcd631..13c11bf 100644 --- a/rt/bin/rt-crontool +++ b/rt/bin/rt-crontool @@ -2,8 +2,8 @@ # BEGIN BPS TAGGED BLOCK {{{ # # COPYRIGHT: -# -# This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC +# +# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC # # # (Except where explicitly superseded by other copyright notices) @@ -49,9 +49,32 @@ use strict; use Carp; -use lib ("/opt/rt3/local/lib", "/opt/rt3/lib"); +# fix lib paths, some may be relative +BEGIN { + require File::Spec; + my @libs = ("lib", "local/lib"); + my $bin_path; + + for my $lib (@libs) { + unless ( File::Spec->file_name_is_absolute($lib) ) { + unless ($bin_path) { + if ( File::Spec->file_name_is_absolute(__FILE__) ) { + $bin_path = ( File::Spec->splitpath(__FILE__) )[1]; + } + else { + require FindBin; + no warnings "once"; + $bin_path = $FindBin::Bin; + } + } + $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib ); + } + unshift @INC, $lib; + } -package RT; +} + +use RT; use Getopt::Long; @@ -62,54 +85,66 @@ use RT::Template; #Clean out all the nasties from the environment CleanEnv(); +my ( $search, $condition, $action, $search_arg, $condition_arg, $action_arg, + $template, $template_id, $transaction, $transaction_type, $help, $log, $verbose ); +GetOptions( + "search=s" => \$search, + "search-arg=s" => \$search_arg, + "condition=s" => \$condition, + "condition-arg=s" => \$condition_arg, + "action-arg=s" => \$action_arg, + "action=s" => \$action, + "template=s" => \$template, + "template-id=s" => \$template_id, + "transaction=s" => \$transaction, + "transaction-type=s" => \$transaction_type, + "log=s" => \$log, + "verbose|v" => \$verbose, + "help" => \$help, +); + # Load the config file RT::LoadConfig(); +# adjust logging to the screen according to options +RT->Config->Set( LogToScreen => $log ) if $log; + #Connect to the database and get RT::SystemUser and RT::Nobody loaded RT::Init(); #Get the current user all loaded my $CurrentUser = GetCurrentUser(); +# show help even if there is no current user +help() if $help; + unless ( $CurrentUser->Id ) { print loc("No RT user found. Please consult your RT administrator.\n"); exit(1); } -my ( $search, $condition, $action, $search_arg, $condition_arg, $action_arg, - $template_id, $transaction, $transaction_type, $help, $verbose ); -GetOptions( "search=s" => \$search, - "search-arg=s" => \$search_arg, - "condition=s" => \$condition, - "condition-arg=s" => \$condition_arg, - "action-arg=s" => \$action_arg, - "action=s" => \$action, - "template-id=s" => \$template_id, - "transaction=s" => \$transaction, - "transaction-type=s" => \$transaction_type, - "help" => \$help, - "verbose|v" => \$verbose ); - -help() if $help or not $search or not $action; - -$transaction ||= 'first'; -unless ( $transaction =~ /^(first|last)$/i ) { - print STDERR loc("--transaction argument could be only 'first' or 'last'"); +help() unless $search && $action; + +$transaction = lc( $transaction||'' ); +if ( $transaction && $transaction !~ /^(first|all|last)$/i ) { + print STDERR loc("--transaction argument could be only 'first', 'last' or 'all'"); + exit 1; +} + +if ( $template && $template_id ) { + print STDERR loc("--template-id is deprecated argument and can not be used with --template"); exit 1; } -$transaction = lc($transaction) eq 'first'? 'ASC': 'DESC'; +elsif ( $template_id ) { +# don't warn + $template = $template_id; +} # We _must_ have a search object load_module($search); load_module($action) if ($action); load_module($condition) if ($condition); -# load template if specified -my $template_obj; -if ($template_id) { - $template_obj = RT::Template->new($CurrentUser); - $template_obj->Load($template_id); -} my $void_scrip = RT::Scrip->new( $CurrentUser ); my $void_scrip_action = RT::ScripAction->new( $CurrentUser ); @@ -132,9 +167,31 @@ my $tickets = $search->TicketsObj; while ( my $ticket = $tickets->Next() ) { print $ticket->Id() . ": " if ($verbose); - my $transaction = get_transaction($ticket); - print loc("Using transaction #[_1]...", $transaction->id) - if $verbose && $transaction; + my $template_obj = get_template( $ticket ); + + if ( $transaction ) { + my $txns = get_transactions($ticket); + my $found = 0; + while ( my $txn = $txns->Next ) { + print loc("Using transaction #[_1]...", $txn->id) + if $verbose; + process($ticket, $txn, $template_obj); + $found = 1; + } + print loc("Couldn't find suitable transaction, skipping") + if $verbose && !$found; + } else { + print loc("Processing without transaction, some conditions and actions may fail. Consider using --transaction argument") + if $verbose; + + process($ticket, undef, $template_obj); + } +} + +sub process { + my $ticket = shift; + my $transaction = shift; + my $template_obj = shift; # perform some more advanced check if ($condition) { @@ -149,8 +206,8 @@ while ( my $ticket = $tickets->Next() ) { # if the condition doesn't apply, get out of here - next unless ( $condition_obj->IsApplicable ); - print loc("Condition matches...") if ($verbose); + return unless $condition_obj->IsApplicable; + print loc("Condition matches...") if $verbose; } #prepare our action @@ -165,34 +222,76 @@ while ( my $ticket = $tickets->Next() ) { ); #if our preparation, move onto the next ticket - next unless ( $action_obj->Prepare ); - print loc("Action prepared...") if ($verbose); + return unless $action_obj->Prepare; + print loc("Action prepared...") if $verbose; #commit our action. - next unless ( $action_obj->Commit ); - print loc("Action committed.\n") if ($verbose); + return unless $action_obj->Commit; + print loc("Action committed.\n") if $verbose; } -=head2 get_transaction +=head2 get_transactions -Takes ticket and returns its transaction acording to command -line arguments C<--transaction> and <--transaction-type>. +Takes ticket and returns L object with transactions +of the ticket according to command line arguments C<--transaction> +and <--transaction-type>. =cut -sub get_transaction { +sub get_transactions { my $ticket = shift; my $txns = $ticket->Transactions; + my $order = $transaction eq 'last'? 'DESC': 'ASC'; $txns->OrderByCols( - { FIELD => 'Created', ORDER => $transaction }, - { FIELD => 'id', ORDER => $transaction }, + { FIELD => 'Created', ORDER => $order }, + { FIELD => 'id', ORDER => $order }, ); - $txns->Limit( FIELD => 'Type', VALUE => $transaction_type ) - if $transaction_type; - $txns->RowsPerPage(1); - return $txns->First; + if ( $transaction_type ) { + $transaction_type =~ s/^\s+//; + $transaction_type =~ s/\s+$//; + foreach my $type ( split /\s*,\s*/, $transaction_type ) { + $txns->Limit( FIELD => 'Type', VALUE => $type, ENTRYAGGREGATOR => 'OR' ); + } + } + $txns->RowsPerPage(1) unless $transaction eq 'all'; + return $txns; } +=head2 get_template + +Takes a ticket and returns a template according to command line options. + +=cut + +{ my $cache = undef; +sub get_template { + my $ticket = shift; + return undef unless $template; + + unless ( $template =~ /\D/ ) { + # by id + return $cache if $cache; + + my $cache = RT::Template->new( $RT::SystemUser ); + $cache->Load( $template ); + die "Failed to load template '$template'" + unless $cache->id; + return $cache; + } + + my $queue = $ticket->Queue; + return $cache->{ $queue } if $cache->{ $queue }; + + my $res = RT::Template->new( $RT::SystemUser ); + $res->LoadQueueTemplate( Queue => $queue, Name => $template ); + unless ( $res->id ) { + $res->LoadGlobalTemplate( $template ); + die "Failed to load template '$template', either for queue #$queue or global" + unless $res->id; + } + return $cache->{ $queue } = $res; +} } + # {{{ load_module =head2 load_module @@ -236,31 +335,33 @@ sub help { . loc( "[_1] - Specify the search module you want to use", "--search" ) . "\n"; print " " - . loc( "[_1] - An argument to pass to [_2]", "--search-argument", "--search" ) + . loc( "[_1] - An argument to pass to [_2]", "--search-arg", "--search" ) . "\n"; print " " . loc( "[_1] - Specify the condition module you want to use", "--condition" ) . "\n"; print " " - . loc( "[_1] - An argument to pass to [_2]", "--condition-argument", "--condition" ) + . loc( "[_1] - An argument to pass to [_2]", "--condition-arg", "--condition" ) . "\n"; print " " . loc( "[_1] - Specify the action module you want to use", "--action" ) . "\n"; print " " - . loc( "[_1] - An argument to pass to [_2]", "--action-argument", "--action" ) + . loc( "[_1] - An argument to pass to [_2]", "--action-arg", "--action" ) . "\n"; print " " - . loc( "[_1] - Specify id of the template you want to use", "--template-id" ) + . loc( "[_1] - Specify name or id of template(s) you want to use", "--template" ) . "\n"; print " " - . loc( "[_1] - Specify if you want to use either 'first' or 'last' transaction", "--transaction" ) + . loc( "[_1] - Specify if you want to use either 'first', 'last' or 'all' transactions", "--transaction" ) . "\n"; print " " - . loc( "[_1] - Specify the type of a transaction you want to use", "--transaction-type" ) + . loc( "[_1] - Specify the comma separated list of transactions' types you want to use", "--transaction-type" ) . "\n"; print " " + . loc( "[_1] - Adjust LogToScreen config option", "--log" ) . "\n"; + print " " . loc( "[_1] - Output status updates to STDOUT", "--verbose" ) . "\n"; print "\n"; print "\n"; diff --git a/rt/bin/rt-mailgate b/rt/bin/rt-mailgate index 8db26db..d9e85a7 100755 --- a/rt/bin/rt-mailgate +++ b/rt/bin/rt-mailgate @@ -2,8 +2,8 @@ # BEGIN BPS TAGGED BLOCK {{{ # # COPYRIGHT: -# -# This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC +# +# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC # # # (Except where explicitly superseded by other copyright notices) @@ -52,30 +52,34 @@ rt-mailgate - Mail interface to RT3. =cut - use strict; use warnings; + use Getopt::Long; use LWP::UserAgent; +use HTTP::Request::Common qw($DYNAMIC_FILE_UPLOAD); +$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" ); -if ( $opts{help} ) { +if ( $opts{'help'} ) { require Pod::Usage; import Pod::Usage; pod2usage("RT Mail Gateway\n"); exit 1; # Don't want to succeed if this is really an email! } -for (qw(url)) { - die "$0 invoked improperly\n\nNo $_ provided to mail gateway!\n" unless $opts{$_}; +unless ( $opts{'url'} ) { + print STDERR "$0 invoked improperly\n\nNo 'url' provided to mail gateway!\n"; + exit 1; } -my $ua = LWP::UserAgent->new(); -$ua->cookie_jar( { file => $opts{jar} } ); +my $ua = new LWP::UserAgent; +$ua->cookie_jar( { file => $opts{'jar'} } ) if $opts{'jar'}; my %args = ( SessionType => 'REST', # Surpress login box @@ -84,37 +88,54 @@ foreach ( qw(queue action) ) { $args{$_} = $opts{$_} if defined $opts{$_}; }; -# Read the message in from STDIN -$args{'message'} = do { local (@ARGV, $/); <> }; - -unless ( $args{message} =~ /\S/ ) { - print STDERR "$0: no message passed on STDIN!\n"; - exit 0; +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"; } -if ($opts{'extension'}) { - $args{$opts{'extension'}} = $ENV{'EXTENSION'}; +# 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"; } -# Set up cookie here. +# 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', + ]; +} my $full_url = $opts{'url'}. "/REST/1.0/NoAuth/mail-gateway"; -warn "Connecting to $full_url" if $opts{'debug'}; +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} ); +$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; -warn $content if ($opts{debug}); +print STDERR $content ."\n" if $opts{'debug'}; 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. - warn <is_success(); + return if $r->is_success; # This ordinarily oughtn't to be able to happen, suggests a bug in RT. # So only load these heavy modules when they're needed. @@ -140,17 +164,64 @@ 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 ); - warn $formatter->format($tree); - warn "This is $0 exiting because of an undefined server error" if ($opts{debug}); + 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; } +sub write_down_message { + use File::Temp qw(tempfile); + + local $@; + my ($fh, $filename) = eval { tempfile() }; + if ( !$fh || $@ ) { + print STDERR "$0: Couldn't create temp file, using memory\n"; + print STDERR "error: $@\n" if $@; + + 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 ); + } + + 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"; + exit EX_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"; + exit 0; + } + print STDERR "$0: temp file is '$filename'\n" if $opts{'debug'}; + return (filename => $filename); +} + =head1 SYNOPSIS @@ -166,8 +237,6 @@ Usual invocation (from MTA): -See C for more. - =head1 OPTIONS =over 3 @@ -178,7 +247,7 @@ Specifies what happens to email sent to this alias. The avaliable basic actions are: C, C. -If you've set the RT configuration variable B<$RT::UnsafeEmailCommands>, +If you've set the RT configuration variable B<< C >>, C and C are also available. You can execute two or more actions on a single message using a C<-> separated list. RT will execute the actions in the listed order. For example you can use C, @@ -259,13 +328,13 @@ there are situations in which you will want to authenticate users before allowing them to communicate with the system. You can do this via a plug-in mechanism in the RT configuration. -You can set the array C<@RT::MailPlugins> to be a list of plugins. The +You can set the array C<@MailPlugins> to be a list of plugins. The default plugin, if this is not given, is C - that is, authentication of the person is done based on the C header of the email. If you have additional filters or authentication mechanisms, you can list them here and they will be called in order: - @RT::MailPlugins = ( + Set( @MailPlugins => "Filter::SpamAssassin", "Auth::LDAP", # ... @@ -273,12 +342,12 @@ can list them here and they will be called in order: See the documentation for any additional plugins you have. -You may also put Perl subroutines into the C<@RT::MailPlugins> array, if +You may also put Perl subroutines into the C<@MailPlugins> array, if they behave as described below. =head1 WRITING PLUGINS -What's actually going on in the above is that C<@RT::MailPlugins> is a +What's actually going on in the above is that C<@MailPlugins> is a list of Perl modules; RT prepends C to the name, to form a package name, and then C's this module. The module is expected to provide a C subroutine, which takes a hash of @@ -319,5 +388,22 @@ the correspondent) or one, which is the normal mode of operation. Additionally, if C<-1> is returned, then the processing of the plug-ins stops immediately and the message is ignored. +=head1 ENVIRONMENT + +=over 4 + +=item EXTENSION + +Some MTAs will route mail sent to user-foo@host or user+foo@host to user@host +and present "foo" in the environment variable C. Mailgate adds value +of this variable to message in the C field of the message +header. + +See also C<--extension> option. Note that value of the environment variable is +always added to the message header when it's not empty even if C<--extension> +option is not provided. + +=back 4 + =cut -- cgit v1.1