import rt 3.6.4
[freeside.git] / rt / bin / rt-crontool.in
1 #!@PERL@
2 # BEGIN BPS TAGGED BLOCK {{{
3
4 # COPYRIGHT:
5 #  
6 # This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC 
7 #                                          <jesse@bestpractical.com>
8
9 # (Except where explicitly superseded by other copyright notices)
10
11
12 # LICENSE:
13
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
17 # from www.gnu.org.
18
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.
23
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.
29
30
31 # CONTRIBUTION SUBMISSION POLICY:
32
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.)
38
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.
47
48 # END BPS TAGGED BLOCK }}}
49 use strict;
50 use Carp;
51
52 use lib ("@LOCAL_LIB_PATH@", "@RT_LIB_PATH@");
53
54 package RT;
55
56 use Getopt::Long;
57
58 use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
59 use RT::Tickets;
60 use RT::Template;
61
62 #Clean out all the nasties from the environment
63 CleanEnv();
64
65 # Load the config file
66 RT::LoadConfig();
67
68 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
69 RT::Init();
70
71 #Get the current user all loaded
72 my $CurrentUser = GetCurrentUser();
73
74 unless ( $CurrentUser->Id ) {
75     print loc("No RT user found. Please consult your RT administrator.\n");
76     exit(1);
77 }
78
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,
90             "help"               => \$help,
91             "verbose|v"          => \$verbose );
92
93 help() if $help or not $search or not $action;
94
95 $transaction ||= 'first';
96 unless ( $transaction =~ /^(first|last)$/i ) {
97     print STDERR loc("--transaction argument could be only 'first' or 'last'");
98     exit 1;
99 }
100 $transaction = lc($transaction) eq 'first'? 'ASC': 'DESC';
101
102 # We _must_ have a search object
103 load_module($search);
104 load_module($action)    if ($action);
105 load_module($condition) if ($condition);
106
107 # load template if specified
108 my $template_obj;
109 if ($template_id) {
110     $template_obj = RT::Template->new($CurrentUser);
111     $template_obj->Load($template_id);
112 }
113 my $void_scrip = RT::Scrip->new( $CurrentUser );
114 my $void_scrip_action = RT::ScripAction->new( $CurrentUser );
115
116 #At the appointed time:
117
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
124 );
125
126 $search->Prepare();
127
128 # TicketsFound is an RT::Tickets object
129 my $tickets = $search->TicketsObj;
130
131 #for each ticket we've found
132 while ( my $ticket = $tickets->Next() ) {
133     print $ticket->Id() . ": " if ($verbose);
134
135     my $transaction = get_transaction($ticket);
136     print loc("Using transaction #[_1]...", $transaction->id)
137         if $verbose && $transaction;
138
139     # perform some more advanced check
140     if ($condition) {
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,
148         );
149
150         # if the condition doesn't apply, get out of here
151
152         next unless ( $condition_obj->IsApplicable );
153         print loc("Condition matches...") if ($verbose);
154     }
155
156     #prepare our action
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,
165     );
166
167     #if our preparation, move onto the next ticket
168     next unless ( $action_obj->Prepare );
169     print loc("Action prepared...") if ($verbose);
170
171     #commit our action.
172     next unless ( $action_obj->Commit );
173     print loc("Action committed.\n") if ($verbose);
174 }
175
176 =head2 get_transaction
177
178 Takes ticket and returns its transaction acording to command
179 line arguments C<--transaction> and <--transaction-type>.
180
181 =cut
182
183 sub get_transaction {
184     my $ticket = shift;
185     my $txns = $ticket->Transactions;
186     $txns->OrderByCols(
187         { FIELD => 'Created', ORDER => $transaction },
188         { FIELD => 'id', ORDER => $transaction },
189     );
190     $txns->Limit( FIELD => 'Type', VALUE => $transaction_type )
191         if $transaction_type;
192     $txns->RowsPerPage(1);
193     return $txns->First;
194 }
195
196 # {{{ load_module 
197
198 =head2 load_module
199
200 Loads a perl module, dying nicely if it can't find it.
201
202 =cut
203
204 sub load_module {
205     my $modname = shift;
206     eval "require $modname";
207     if ($@) {
208         die loc( "Failed to load module [_1]. ([_2])", $modname, $@ );
209     }
210
211 }
212
213 # }}}
214
215 # {{{ loc 
216
217 =head2 loc LIST
218
219 Localize this string, with the current user's currentuser object
220
221 =cut
222
223 sub loc {
224     $CurrentUser->loc(@_);
225 }
226
227 # }}}
228
229 sub help {
230
231     print loc( "[_1] is a tool to act on tickets from an external scheduling tool, such as cron.", $0 )
232       . "\n";
233     print loc("It takes several arguments:") . "\n\n";
234
235     print "     "
236       . loc( "[_1] - Specify the search module you want to use", "--search" )
237       . "\n";
238     print "     "
239       . loc( "[_1] - An argument to pass to [_2]", "--search-argument", "--search" )
240       . "\n";
241
242     print "     "
243       . loc( "[_1] - Specify the condition module you want to use", "--condition" )
244       . "\n";
245     print "     "
246       . loc( "[_1] - An argument to pass to [_2]", "--condition-argument", "--condition" )
247       . "\n";
248     print "     "
249       . loc( "[_1] - Specify the action module you want to use", "--action" )
250       . "\n";
251     print "     "
252       . loc( "[_1] - An argument to pass to [_2]", "--action-argument", "--action" )
253       . "\n";
254     print "     "
255       . loc( "[_1] - Specify id of the template you want to use", "--template-id" )
256       . "\n";
257     print "     "
258       . loc( "[_1] - Specify if you want to use either 'first' or 'last' transaction", "--transaction" )
259       . "\n";
260     print "     "
261       . loc( "[_1] - Specify the type of a transaction you want to use", "--transaction-type" )
262       . "\n";
263     print "     "
264       . loc( "[_1] - Output status updates to STDOUT", "--verbose" ) . "\n";
265     print "\n";
266     print "\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";
272     print "\n";
273     print loc("Example:");
274     print "\n";
275     print " "
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:"
277       )
278       . "\n\n";
279
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";
285
286     print "\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";
291  
292  
293  
294
295
296
297     exit(0);
298 }