2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2007 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 use lib ("@LOCAL_LIB_PATH@", "@RT_LIB_PATH@");
58 use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
62 #Clean out all the nasties from the environment
65 # Load the config file
68 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
71 #Get the current user all loaded
72 my $CurrentUser = GetCurrentUser();
74 unless ( $CurrentUser->Id ) {
75 print loc("No RT user found. Please consult your RT administrator.\n");
79 my ( $search, $condition, $action, $search_arg, $condition_arg, $action_arg,
80 $template_id, $transaction, $transaction_type, $help, $verbose );
81 GetOptions( "search=s" => \$search,
82 "search-arg=s" => \$search_arg,
83 "condition=s" => \$condition,
84 "condition-arg=s" => \$condition_arg,
85 "action-arg=s" => \$action_arg,
86 "action=s" => \$action,
87 "template-id=s" => \$template_id,
88 "transaction=s" => \$transaction,
89 "transaction-type=s" => \$transaction_type,
91 "verbose|v" => \$verbose );
93 help() if $help or not $search or not $action;
95 $transaction ||= 'first';
96 unless ( $transaction =~ /^(first|last)$/i ) {
97 print STDERR loc("--transaction argument could be only 'first' or 'last'");
100 $transaction = lc($transaction) eq 'first'? 'ASC': 'DESC';
102 # We _must_ have a search object
103 load_module($search);
104 load_module($action) if ($action);
105 load_module($condition) if ($condition);
107 # load template if specified
110 $template_obj = RT::Template->new($CurrentUser);
111 $template_obj->Load($template_id);
113 my $void_scrip = RT::Scrip->new( $CurrentUser );
114 my $void_scrip_action = RT::ScripAction->new( $CurrentUser );
116 #At the appointed time:
118 #find a bunch of tickets
119 my $tickets = RT::Tickets->new($CurrentUser);
120 my $search = $search->new(
121 TicketsObj => $tickets,
122 Argument => $search_arg,
123 CurrentUser => $CurrentUser
128 # TicketsFound is an RT::Tickets object
129 my $tickets = $search->TicketsObj;
131 #for each ticket we've found
132 while ( my $ticket = $tickets->Next() ) {
133 print $ticket->Id() . ": " if ($verbose);
135 my $transaction = get_transaction($ticket);
136 print loc("Using transaction #[_1]...", $transaction->id)
137 if $verbose && $transaction;
139 # perform some more advanced check
141 my $condition_obj = $condition->new(
142 TransactionObj => $transaction,
143 TicketObj => $ticket,
144 ScripObj => $void_scrip,
145 TemplateObj => $template_obj,
146 Argument => $condition_arg,
147 CurrentUser => $CurrentUser,
150 # if the condition doesn't apply, get out of here
152 next unless ( $condition_obj->IsApplicable );
153 print loc("Condition matches...") if ($verbose);
157 my $action_obj = $action->new(
158 TicketObj => $ticket,
159 TransactionObj => $transaction,
160 TemplateObj => $template_obj,
161 Argument => $action_arg,
162 ScripObj => $void_scrip,
163 ScripActionObj => $void_scrip_action,
164 CurrentUser => $CurrentUser,
167 #if our preparation, move onto the next ticket
168 next unless ( $action_obj->Prepare );
169 print loc("Action prepared...") if ($verbose);
172 next unless ( $action_obj->Commit );
173 print loc("Action committed.\n") if ($verbose);
176 =head2 get_transaction
178 Takes ticket and returns its transaction acording to command
179 line arguments C<--transaction> and <--transaction-type>.
183 sub get_transaction {
185 my $txns = $ticket->Transactions;
187 { FIELD => 'Created', ORDER => $transaction },
188 { FIELD => 'id', ORDER => $transaction },
190 $txns->Limit( FIELD => 'Type', VALUE => $transaction_type )
191 if $transaction_type;
192 $txns->RowsPerPage(1);
200 Loads a perl module, dying nicely if it can't find it.
206 eval "require $modname";
208 die loc( "Failed to load module [_1]. ([_2])", $modname, $@ );
219 Localize this string, with the current user's currentuser object
224 $CurrentUser->loc(@_);
231 print loc( "[_1] is a tool to act on tickets from an external scheduling tool, such as cron.", $0 )
233 print loc("It takes several arguments:") . "\n\n";
236 . loc( "[_1] - Specify the search module you want to use", "--search" )
239 . loc( "[_1] - An argument to pass to [_2]", "--search-argument", "--search" )
243 . loc( "[_1] - Specify the condition module you want to use", "--condition" )
246 . loc( "[_1] - An argument to pass to [_2]", "--condition-argument", "--condition" )
249 . loc( "[_1] - Specify the action module you want to use", "--action" )
252 . loc( "[_1] - An argument to pass to [_2]", "--action-argument", "--action" )
255 . loc( "[_1] - Specify id of the template you want to use", "--template-id" )
258 . loc( "[_1] - Specify if you want to use either 'first' or 'last' transaction", "--transaction" )
261 . loc( "[_1] - Specify the type of a transaction you want to use", "--transaction-type" )
264 . loc( "[_1] - Output status updates to STDOUT", "--verbose" ) . "\n";
267 print loc("Security:")."\n";
268 print loc("This tool allows the user to run arbitrary perl modules from within RT.")." ".
269 loc("If this tool were setgid, a hostile local user could use this tool to gain administrative access to RT.")." ".
270 loc("It is incredibly important that nonprivileged users not be allowed to run this tool."). " " .
271 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";
273 print loc("Example:");
276 . 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:"
280 print " bin/rt-crontool \\\n";
281 print " --search RT::Search::ActiveTicketsInQueue --search-arg general \\\n";
282 print " --condition RT::Condition::UntouchedInHours --condition-arg 4 \\\n";
283 print " --action RT::Action::SetPriority --action-arg 99 \\\n";
284 print " --verbose\n";
287 print loc("Escalate tickets"). "\n";
288 print " bin/rt-crontool \\\n";
289 print " --search RT::Search::ActiveTicketsInQueue --search-arg general \\\n";
290 print " --action RT::Action::EscalatePriority\n";