2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2005 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., 675 Mass Ave, Cambridge, MA 02139, USA.
29 # CONTRIBUTION SUBMISSION POLICY:
31 # (The following paragraph is not intended to limit the rights granted
32 # to you to modify and distribute this software under the terms of
33 # the GNU General Public License and is only of importance to you if
34 # you choose to contribute your changes and enhancements to the
35 # community by submitting them to Best Practical Solutions, LLC.)
37 # By intentionally submitting any modifications, corrections or
38 # derivatives to this work, or any other work intended for use with
39 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
40 # you are the copyright holder for those contributions and you grant
41 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
42 # royalty-free, perpetual, license to use, copy, create derivative
43 # works based on those contributions, and sublicense and distribute
44 # those contributions and any derivatives thereof.
46 # END BPS TAGGED BLOCK }}}
50 use lib ("@LOCAL_LIB_PATH@", "@RT_LIB_PATH@");
56 use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
60 #Clean out all the nasties from the environment
63 # Load the config file
66 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
69 #Get the current user all loaded
70 my $CurrentUser = GetCurrentUser();
72 unless ( $CurrentUser->Id ) {
73 print loc("No RT user found. Please consult your RT administrator.\n");
77 my ( $search, $condition, $action, $search_arg, $condition_arg, $action_arg,
78 $template_id, $help, $verbose );
79 GetOptions( "search=s" => \$search,
80 "search-arg=s" => \$search_arg,
81 "condition=s" => \$condition,
82 "condition-arg=s" => \$condition_arg,
83 "action-arg=s" => \$action_arg,
84 "action=s" => \$action,
85 "template-id=s" => \$template_id,
87 "verbose|v" => \$verbose );
89 help() if $help or not $search or not $action;
91 # We _must_ have a search object
93 load_module($action) if ($action);
94 load_module($condition) if ($condition);
96 # load template if specified
99 $template_obj = RT::Template->new($CurrentUser);
100 $template_obj->Load($template_id);
103 #At the appointed time:
105 #find a bunch of tickets
106 my $tickets = RT::Tickets->new($CurrentUser);
107 my $search = $search->new(
108 TicketsObj => $tickets,
109 Argument => $search_arg,
110 CurrentUser => $CurrentUser
115 # TicketsFound is an RT::Tickets object
116 my $tickets = $search->TicketsObj;
118 #for each ticket we've found
119 while ( my $ticket = $tickets->Next() ) {
120 print $ticket->Id() . ": " if ($verbose);
122 # perform some more advanced check
124 my $condition_obj = $condition->new( TicketObj => $ticket,
125 Argument => $condition_arg,
126 CurrentUser => $CurrentUser );
128 # if the condition doesn't apply, get out of here
130 next unless ( $condition_obj->IsApplicable );
131 print loc("Condition matches...") if ($verbose);
135 my $action_obj = $action->new(
136 TicketObj => $ticket,
137 TemplateObj => $template_obj,
138 Argument => $action_arg,
139 CurrentUser => $CurrentUser
142 #if our preparation, move onto the next ticket
143 next unless ( $action_obj->Prepare );
144 print loc("Action prepared...") if ($verbose);
147 next unless ( $action_obj->Commit );
148 print loc("Action committed.\n") if ($verbose);
155 Loads a perl module, dying nicely if it can't find it.
161 eval "require $modname";
163 die loc( "Failed to load module [_1]. ([_2])", $modname, $@ );
174 Localize this string, with the current user's currentuser object
179 $CurrentUser->loc(@_);
186 print loc( "[_1] is a tool to act on tickets from an external scheduling tool, such as cron.", $0 )
188 print loc("It takes several arguments:") . "\n\n";
191 . loc( "[_1] - Specify the search module you want to use", "--search" )
194 . loc( "[_1] - An argument to pass to [_2]", "--search-argument", "--search" )
198 . loc( "[_1] - Specify the condition module you want to use", "--condition" )
201 . loc( "[_1] - An argument to pass to [_2]", "--condition-argument", "--condition" )
204 . loc( "[_1] - Specify the action module you want to use", "--action" )
207 . loc( "[_1] - An argument to pass to [_2]", "--action-argument", "--action" )
210 . loc( "[_1] - Output status updates to STDOUT", "--verbose" ) . "\n";
213 print loc("Security:")."\n";
214 print loc("This tool allows the user to run arbitrary perl modules from within RT.")." ".
215 loc("If this tool were setgid, a hostile local user could use this tool to gain administrative access to RT.")." ".
216 loc("It is incredibly important that nonprivileged users not be allowed to run this tool."). " " .
217 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";
219 print loc("Example:");
222 . 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:"
226 print " bin/rt-crontool \\\n";
227 print " --search RT::Search::ActiveTicketsInQueue --search-arg general \\\n";
228 print " --condition RT::Condition::UntouchedInHours --condition-arg 4 \\\n";
229 print " --action RT::Action::SetPriority --action-arg 99 \\\n";
230 print " --verbose\n";
233 print loc("Escalate tickets"). "\n";
234 print " bin/rt-crontool \\\n";
235 print " --search RT::Search::ActiveTicketsInQueue --search-arg general \\\n";
236 print " --action RT::Action::EscalatePriority\n";