4 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
6 # (Except where explictly superceded by other copyright notices)
8 # This work is made available to you under the terms of Version 2 of
9 # the GNU General Public License. A copy of that license should have
10 # been provided with this software, but in any event can be snarfed
13 # This work is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # Unless otherwise specified, all modifications, corrections or
19 # extensions to this work which alter its source code become the
20 # property of Best Practical Solutions, LLC when submitted for
21 # inclusion in the work.
29 use lib ("/opt/rt3/lib", "/opt/rt3/local/lib");
35 use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
39 #Clean out all the nasties from the environment
42 # Load the config file
45 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
48 #Drop setgid permissions
49 RT::DropSetGIDPermissions();
51 #Get the current user all loaded
52 my $CurrentUser = GetCurrentUser();
54 unless ( $CurrentUser->Id ) {
55 print loc("No RT user found. Please consult your RT administrator.\n");
59 my ( $search, $condition, $action, $search_arg, $condition_arg, $action_arg,
60 $template_id, $help, $verbose );
61 GetOptions( "search=s" => \$search,
62 "search-arg=s" => \$search_arg,
63 "condition=s" => \$condition,
64 "condition-arg=s" => \$condition_arg,
65 "action-arg=s" => \$action_arg,
66 "action=s" => \$action,
67 "template-id=s" => \$template_id,
69 "verbose|v" => \$verbose );
73 # We _must_ have a search object
75 load_module($action) if ($action);
76 load_module($condition) if ($condition);
78 # load template if specified
81 $template_obj = RT::Template->new($RT::Nobody);
82 $template_obj->LoadById($template_id);
85 #At the appointed time:
87 #find a bunch of tickets
88 my $tickets = RT::Tickets->new($CurrentUser);
89 my $search = $search->new( TicketsObj => $tickets, Argument => $search_arg );
93 # TicketsFound is an RT::Tickets object
94 my $tickets = $search->TicketsObj;
96 #for each ticket we've found
97 while ( my $ticket = $tickets->Next() ) {
98 print "\n" . $ticket->Id() . ": " if ($verbose);
100 # perform some more advanced check
102 my $condition_obj = $condition->new( TicketObj => $ticket,
103 Argument => $condition_arg );
105 # if the condition doesn't apply, get out of here
107 next unless ( $condition_obj->IsApplicable );
108 print loc("Condition matches...") if ($verbose);
112 my $action_obj = $action->new( TicketObj => $ticket,
113 TemplateObj => $template_obj,
114 Argument => $action_arg );
116 #if our preparation, move onto the next ticket
117 next unless ( $action_obj->Prepare );
118 print loc("Action prepared...") if ($verbose);
121 next unless ( $action_obj->Commit );
122 print loc("Action committed.") if ($verbose);
129 Loads a perl module, dying nicely if it can't find it.
135 eval "require $modname";
137 die loc( "Failed to load module [_1]. ([_2])", $modname, $@ );
148 Localize this string, with the current user's currentuser object
153 $CurrentUser->loc(@_);
160 print loc( "[_1] is a tool to act on tickets from an external scheduling tool, such as cron.", $0 )
162 print loc("It takes several arguments:") . "\n\n";
165 . loc( "[_1] - Specify the search module you want to use", "--search" )
168 . loc( "[_1] - An argument to pass to [_2]", "--search-argument", "--search" )
172 . loc( "[_1] - Specify the condition module you want to use", "--condition" )
175 . loc( "[_1] - An argument to pass to [_2]", "--condition-argument", "--condition" )
178 . loc( "[_1] - Specify the action module you want to use", "--action" )
181 . loc( "[_1] - An argument to pass to [_2]", "--action-argument", "--action" )
184 . loc( "[_1] - Output status updates to STDOUT", "--verbose" ) . "\n";
187 print loc("Security:")."\n";
188 print loc("This tool allows the user to run arbitrary perl modules from within RT.")." ".
189 loc("If this tool were setgid, a hostile local user could use this tool to gain administrative access to RT.")." ".
190 loc("It is incredibly important that nonprivileged users not be allowed to run this tool."). " " .
191 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";
193 print loc("Example:");
196 . 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:"
200 print " bin/rt-cron-tool \\\n";
202 " --search RT::Search::ActiveTicketsInQueue --search-arg general \\\n";
204 " --condition RT::Condition::UntouchedInHours --condition-arg 4 \\\n";
205 print " --action RT::Action::SetPriority --action-arg 99 \\\n";
206 print " --verbose\n";
209 print loc("Escalate tickets");
210 print "rt-crontool \\\n";
211 print " --search RT::Search::ActiveTicketsInQueue --search-arg thequeuename \\\n";
212 print " --action RT::Action::EscalatePriority \\\n";