2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
7 # <jesse@bestpractical.com>
9 # (Except where explicitly superseded by other copyright notices)
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301 or visit their web page on the internet at
28 # http://www.gnu.org/copyleft/gpl.html.
31 # CONTRIBUTION SUBMISSION POLICY:
33 # (The following paragraph is not intended to limit the rights granted
34 # to you to modify and distribute this software under the terms of
35 # the GNU General Public License and is only of importance to you if
36 # you choose to contribute your changes and enhancements to the
37 # community by submitting them to Best Practical Solutions, LLC.)
39 # By intentionally submitting any modifications, corrections or
40 # derivatives to this work, or any other work intended for use with
41 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
42 # you are the copyright holder for those contributions and you grant
43 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
44 # royalty-free, perpetual, license to use, copy, create derivative
45 # works based on those contributions, and sublicense and distribute
46 # those contributions and any derivatives thereof.
48 # END BPS TAGGED BLOCK }}}
52 # fix lib paths, some may be relative
55 my @libs = ("lib", "local/lib");
59 unless ( File::Spec->file_name_is_absolute($lib) ) {
61 if ( File::Spec->file_name_is_absolute(__FILE__) ) {
62 $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
67 $bin_path = $FindBin::Bin;
70 $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
81 use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
85 #Clean out all the nasties from the environment
88 my ( $search, $condition, $action, $search_arg, $condition_arg, $action_arg,
89 $template, $template_id, $transaction, $transaction_type, $help, $log, $verbose );
91 "search=s" => \$search,
92 "search-arg=s" => \$search_arg,
93 "condition=s" => \$condition,
94 "condition-arg=s" => \$condition_arg,
95 "action-arg=s" => \$action_arg,
96 "action=s" => \$action,
97 "template=s" => \$template,
98 "template-id=s" => \$template_id,
99 "transaction=s" => \$transaction,
100 "transaction-type=s" => \$transaction_type,
102 "verbose|v" => \$verbose,
106 # Load the config file
109 # adjust logging to the screen according to options
110 RT->Config->Set( LogToScreen => $log ) if $log;
112 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
115 #Get the current user all loaded
116 my $CurrentUser = GetCurrentUser();
118 # show help even if there is no current user
121 unless ( $CurrentUser->Id ) {
122 print loc("No RT user found. Please consult your RT administrator.\n");
126 help() unless $search && $action;
128 $transaction = lc( $transaction||'' );
129 if ( $transaction && $transaction !~ /^(first|all|last)$/i ) {
130 print STDERR loc("--transaction argument could be only 'first', 'last' or 'all'");
134 if ( $template && $template_id ) {
135 print STDERR loc("--template-id is deprecated argument and can not be used with --template");
138 elsif ( $template_id ) {
140 $template = $template_id;
143 # We _must_ have a search object
144 load_module($search);
145 load_module($action) if ($action);
146 load_module($condition) if ($condition);
148 my $void_scrip = RT::Scrip->new( $CurrentUser );
149 my $void_scrip_action = RT::ScripAction->new( $CurrentUser );
151 #At the appointed time:
153 #find a bunch of tickets
154 my $tickets = RT::Tickets->new($CurrentUser);
155 my $search = $search->new(
156 TicketsObj => $tickets,
157 Argument => $search_arg,
158 CurrentUser => $CurrentUser
163 # TicketsFound is an RT::Tickets object
164 my $tickets = $search->TicketsObj;
166 #for each ticket we've found
167 while ( my $ticket = $tickets->Next() ) {
168 print $ticket->Id() . ": " if ($verbose);
170 my $template_obj = get_template( $ticket );
172 if ( $transaction ) {
173 my $txns = get_transactions($ticket);
175 while ( my $txn = $txns->Next ) {
176 print loc("Using transaction #[_1]...", $txn->id)
178 process($ticket, $txn, $template_obj);
181 print loc("Couldn't find suitable transaction, skipping")
182 if $verbose && !$found;
184 print loc("Processing without transaction, some conditions and actions may fail. Consider using --transaction argument")
187 process($ticket, undef, $template_obj);
193 my $transaction = shift;
194 my $template_obj = shift;
196 # perform some more advanced check
198 my $condition_obj = $condition->new(
199 TransactionObj => $transaction,
200 TicketObj => $ticket,
201 ScripObj => $void_scrip,
202 TemplateObj => $template_obj,
203 Argument => $condition_arg,
204 CurrentUser => $CurrentUser,
207 # if the condition doesn't apply, get out of here
209 return unless $condition_obj->IsApplicable;
210 print loc("Condition matches...") if $verbose;
214 my $action_obj = $action->new(
215 TicketObj => $ticket,
216 TransactionObj => $transaction,
217 TemplateObj => $template_obj,
218 Argument => $action_arg,
219 ScripObj => $void_scrip,
220 ScripActionObj => $void_scrip_action,
221 CurrentUser => $CurrentUser,
224 #if our preparation, move onto the next ticket
225 return unless $action_obj->Prepare;
226 print loc("Action prepared...") if $verbose;
229 return unless $action_obj->Commit;
230 print loc("Action committed.\n") if $verbose;
233 =head2 get_transactions
235 Takes ticket and returns L<RT::Transactions> object with transactions
236 of the ticket according to command line arguments C<--transaction>
237 and <--transaction-type>.
241 sub get_transactions {
243 my $txns = $ticket->Transactions;
244 my $order = $transaction eq 'last'? 'DESC': 'ASC';
246 { FIELD => 'Created', ORDER => $order },
247 { FIELD => 'id', ORDER => $order },
249 if ( $transaction_type ) {
250 $transaction_type =~ s/^\s+//;
251 $transaction_type =~ s/\s+$//;
252 foreach my $type ( split /\s*,\s*/, $transaction_type ) {
253 $txns->Limit( FIELD => 'Type', VALUE => $type, ENTRYAGGREGATOR => 'OR' );
256 $txns->RowsPerPage(1) unless $transaction eq 'all';
262 Takes a ticket and returns a template according to command line options.
269 return undef unless $template;
271 unless ( $template =~ /\D/ ) {
273 return $cache if $cache;
275 my $cache = RT::Template->new( $RT::SystemUser );
276 $cache->Load( $template );
277 die "Failed to load template '$template'"
282 my $queue = $ticket->Queue;
283 return $cache->{ $queue } if $cache->{ $queue };
285 my $res = RT::Template->new( $RT::SystemUser );
286 $res->LoadQueueTemplate( Queue => $queue, Name => $template );
287 unless ( $res->id ) {
288 $res->LoadGlobalTemplate( $template );
289 die "Failed to load template '$template', either for queue #$queue or global"
292 return $cache->{ $queue } = $res;
299 Loads a perl module, dying nicely if it can't find it.
305 eval "require $modname";
307 die loc( "Failed to load module [_1]. ([_2])", $modname, $@ );
318 Localize this string, with the current user's currentuser object
323 $CurrentUser->loc(@_);
330 print loc( "[_1] is a tool to act on tickets from an external scheduling tool, such as cron.", $0 )
332 print loc("It takes several arguments:") . "\n\n";
335 . loc( "[_1] - Specify the search module you want to use", "--search" )
338 . loc( "[_1] - An argument to pass to [_2]", "--search-arg", "--search" )
342 . loc( "[_1] - Specify the condition module you want to use", "--condition" )
345 . loc( "[_1] - An argument to pass to [_2]", "--condition-arg", "--condition" )
348 . loc( "[_1] - Specify the action module you want to use", "--action" )
351 . loc( "[_1] - An argument to pass to [_2]", "--action-arg", "--action" )
354 . loc( "[_1] - Specify name or id of template(s) you want to use", "--template" )
357 . loc( "[_1] - Specify if you want to use either 'first', 'last' or 'all' transactions", "--transaction" )
360 . loc( "[_1] - Specify the comma separated list of transactions' types you want to use", "--transaction-type" )
363 . loc( "[_1] - Adjust LogToScreen config option", "--log" ) . "\n";
365 . loc( "[_1] - Output status updates to STDOUT", "--verbose" ) . "\n";
368 print loc("Security:")."\n";
369 print loc("This tool allows the user to run arbitrary perl modules from within RT.")." ".
370 loc("If this tool were setgid, a hostile local user could use this tool to gain administrative access to RT.")." ".
371 loc("It is incredibly important that nonprivileged users not be allowed to run this tool."). " " .
372 loc("It is suggested that you create a non-privileged unix user with the correct group membership and RT access to run this tool.")."\n";
374 print loc("Example:");
377 . loc( "The following command will find all active tickets in the queue 'general' and set their priority to 99 if they haven't been touched in 4 hours:"
381 print " bin/rt-crontool \\\n";
382 print " --search RT::Search::ActiveTicketsInQueue --search-arg general \\\n";
383 print " --condition RT::Condition::UntouchedInHours --condition-arg 4 \\\n";
384 print " --action RT::Action::SetPriority --action-arg 99 \\\n";
385 print " --verbose\n";
388 print loc("Escalate tickets"). "\n";
389 print " bin/rt-crontool \\\n";
390 print " --search RT::Search::ActiveTicketsInQueue --search-arg general \\\n";
391 print " --action RT::Action::EscalatePriority\n";