starting to work...
[freeside.git] / rt / bin / rt-crontool
old mode 100644 (file)
new mode 100755 (executable)
index 6193280..e58bafc
@@ -3,7 +3,7 @@
 #
 # COPYRIGHT:
 #
-# This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
+# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
 #                                          <sales@bestpractical.com>
 #
 # (Except where explicitly superseded by other copyright notices)
@@ -52,7 +52,7 @@ use Carp;
 # fix lib paths, some may be relative
 BEGIN {
     require File::Spec;
-    my @libs = ("lib", "local/lib");
+    my @libs = ("/opt/rt3/lib", "/opt/rt3/local/lib");
     my $bin_path;
 
     for my $lib (@libs) {
@@ -120,7 +120,7 @@ my $CurrentUser = GetCurrentUser();
 help() if $help;
 
 unless ( $CurrentUser->Id ) {
-    print loc("No RT user found. Please consult your RT administrator.\n");
+    print loc("No RT user found. Please consult your RT administrator.");
     exit(1);
 }
 
@@ -166,7 +166,7 @@ my $tickets = $search->TicketsObj;
 
 #for each ticket we've found
 while ( my $ticket = $tickets->Next() ) {
-    print $ticket->Id() . ": " if ($verbose);
+    print $ticket->Id() . ":\n" if ($verbose);
 
     my $template_obj = get_template( $ticket );
 
@@ -174,15 +174,15 @@ while ( my $ticket = $tickets->Next() ) {
         my $txns = get_transactions($ticket);
         my $found = 0;
         while ( my $txn = $txns->Next ) {
-            print loc("Using transaction #[_1]...", $txn->id)
+            print "\t".loc("Using transaction #[_1]...", $txn->id)."\n"
                 if $verbose;
             process($ticket, $txn, $template_obj);
             $found = 1;
         }
-        print loc("Couldn't find suitable transaction, skipping")
+        print "\t".loc("Couldn't find suitable transaction, skipping")."\n"
             if $verbose && !$found;
     } else {
-        print loc("Processing without transaction, some conditions and actions may fail. Consider using --transaction argument")
+        print "\t".loc("Processing without transaction, some conditions and actions may fail. Consider using --transaction argument")."\n"
             if $verbose;
 
         process($ticket, undef, $template_obj);
@@ -208,7 +208,7 @@ sub process {
         # if the condition doesn't apply, get out of here
 
         return unless $condition_obj->IsApplicable;
-        print loc("Condition matches...") if $verbose;
+        print "\t".loc("Condition matches...")."\n" if $verbose;
     }
 
     #prepare our action
@@ -224,20 +224,20 @@ sub process {
 
     #if our preparation, move onto the next ticket
     return unless $action_obj->Prepare;
-    print loc("Action prepared...") if $verbose;
+    print "\t".loc("Action prepared...")."\n" if $verbose;
 
     #commit our action.
     return unless $action_obj->Commit;
-    print loc("Action committed.\n") if $verbose;
+    print "\t".loc("Action committed.")."\n" if $verbose;
 }
 
-=head2 get_transactions
-
-Takes ticket and returns L<RT::Transactions> object with transactions
-of the ticket according to command line arguments C<--transaction>
-and <--transaction-type>.
-
-=cut
+=head2 get_transactions
+# 
+Takes ticket and returns L<RT::Transactions> object with transactions
+of the ticket according to command line arguments C<--transaction>
+and <--transaction-type>.
+# 
+=cut
 
 sub get_transactions {
     my $ticket = shift;
@@ -258,11 +258,11 @@ sub get_transactions {
     return $txns;
 }
 
-=head2 get_template
-
-Takes a ticket and returns a template according to command line options.
-
-=cut
+=head2 get_template
+# 
+Takes a ticket and returns a template according to command line options.
+# 
+=cut
 
 { my $cache = undef;
 sub get_template {
@@ -273,7 +273,7 @@ sub get_template {
         # by id
         return $cache if $cache;
 
-        my $cache = RT::Template->new( $RT::SystemUser );
+        my $cache = RT::Template->new( RT->SystemUser );
         $cache->Load( $template );
         die "Failed to load template '$template'"
             unless $cache->id;
@@ -283,7 +283,7 @@ sub get_template {
     my $queue = $ticket->Queue;
     return $cache->{ $queue } if $cache->{ $queue };
 
-    my $res = RT::Template->new( $RT::SystemUser );
+    my $res = RT::Template->new( RT->SystemUser );
     $res->LoadQueueTemplate( Queue => $queue, Name => $template );
     unless ( $res->id ) {
         $res->LoadGlobalTemplate( $template );
@@ -293,13 +293,12 @@ sub get_template {
     return $cache->{ $queue } = $res;
 } }
 
-# {{{ load_module 
-
-=head2 load_module
-
-Loads a perl module, dying nicely if it can't find it.
 
-=cut
+# =head2 load_module
+# 
+# Loads a perl module, dying nicely if it can't find it.
+# 
+# =cut
 
 sub load_module {
     my $modname = shift;
@@ -310,21 +309,18 @@ sub load_module {
 
 }
 
-# }}}
 
-# {{{ loc 
 
-=head2 loc LIST
-
-Localize this string, with the current user's currentuser object
-
-=cut
+=head2 loc LIST
+# 
+Localize this string, with the current user's currentuser object
+# 
+=cut
 
 sub loc {
     $CurrentUser->loc(@_);
 }
 
-# }}}
 
 sub help {
 
@@ -389,7 +385,7 @@ sub help {
     print loc("Escalate tickets"). "\n";
     print " bin/rt-crontool \\\n";
     print "  --search RT::Search::ActiveTicketsInQueue  --search-arg general \\\n";
-    print "  --action RT::Action::EscalatePriority\n";
+    print"  --action RT::Action::EscalatePriority\n";
  
  
  
@@ -398,3 +394,90 @@ sub help {
 
     exit(0);
 }
+
+__END__
+
+=head1 NAME
+
+rt-crontool - a tool to act on tickets from an external scheduling tool
+
+=head1 SYNOPSIS
+
+    # find all active tickets in the queue 'general' and set their priority to 99 if they are overdue:
+    rt-crontool \
+      --search RT::Search::ActiveTicketsInQueue  --search-arg general \
+      --condition RT::Condition::Overdue \
+      --action RT::Action::SetPriority --action-arg 99 \
+      --verbose
+
+    # Escalate tickets
+      rt-crontool \
+        --search RT::Search::ActiveTicketsInQueue  --search-arg general \
+        --action RT::Action::EscalatePriority
+
+=head1 DESCRIPTION
+
+This script is a tool to act on tickets from an external scheduling tool, such
+as cron.
+
+Security:
+
+This tool allows the user to run arbitrary perl modules from within RT. If
+this tool were setgid, a hostile local user could use this tool to gain
+administrative access to RT. It is incredibly important that nonprivileged
+users not be allowed to run this tool. It is suggested that you create a
+non-privileged unix user with the correct group membership and RT access to
+run this tool.
+
+
+=head1 OPTIONS
+
+=over
+
+=item search 
+
+Specify the search module you want to use
+
+=item search-arg 
+
+An argument to pass to --search
+
+=item condition
+
+Specify the condition module you want to use
+
+=item condition-arg
+
+An argument to pass to --condition
+
+=item action 
+
+Specify the action module you want to use
+
+=item action-arg
+
+An argument to pass to --action
+
+=item template
+
+Specify name or id of template(s) you want to use
+
+=item transaction
+
+Specify if you want to use either 'first', 'last' or 'all' transactions
+
+
+=item transaction-type
+
+Specify the comma separated list of transactions' types you want to use
+
+=item log
+
+Adjust LogToScreen config option
+
+=item verbose
+
+Output status updates to STDOUT
+
+=back
+