import rt 3.4.6
[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-2005 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., 675 Mass Ave, Cambridge, MA 02139, USA.
27
28
29 # CONTRIBUTION SUBMISSION POLICY:
30
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.)
36
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.
45
46 # END BPS TAGGED BLOCK }}}
47 use strict;
48 use Carp;
49
50 use lib ("@LOCAL_LIB_PATH@", "@RT_LIB_PATH@");
51
52 package RT;
53
54 use Getopt::Long;
55
56 use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
57 use RT::Tickets;
58 use RT::Template;
59
60 #Clean out all the nasties from the environment
61 CleanEnv();
62
63 # Load the config file
64 RT::LoadConfig();
65
66 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
67 RT::Init();
68
69 #Get the current user all loaded
70 my $CurrentUser = GetCurrentUser();
71
72 unless ( $CurrentUser->Id ) {
73     print loc("No RT user found. Please consult your RT administrator.\n");
74     exit(1);
75 }
76
77 my ( $search, $condition, $action, $search_arg, $condition_arg, $action_arg,
78      $template_id, $transaction, $transaction_type, $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,
86             "transaction=s"      => \$transaction,
87             "transaction-type=s" => \$transaction_type,
88             "help"               => \$help,
89             "verbose|v"          => \$verbose );
90
91 help() if $help or not $search or not $action;
92
93 $transaction ||= 'first';
94 unless ( $transaction =~ /^(first|last)$/i ) {
95     print STDERR loc("--transaction argument could be only 'first' or 'last'");
96     exit 1;
97 }
98 $transaction = lc($transaction) eq 'first'? 'ASC': 'DESC';
99
100 # We _must_ have a search object
101 load_module($search);
102 load_module($action)    if ($action);
103 load_module($condition) if ($condition);
104
105 # load template if specified
106 my $template_obj;
107 if ($template_id) {
108     $template_obj = RT::Template->new($CurrentUser);
109     $template_obj->Load($template_id);
110 }
111 my $void_scrip = RT::Scrip->new( $CurrentUser );
112 my $void_scrip_action = RT::ScripAction->new( $CurrentUser );
113
114 #At the appointed time:
115
116 #find a bunch of tickets
117 my $tickets = RT::Tickets->new($CurrentUser);
118 my $search  = $search->new(
119     TicketsObj  => $tickets,
120     Argument    => $search_arg,
121     CurrentUser => $CurrentUser
122 );
123
124 $search->Prepare();
125
126 # TicketsFound is an RT::Tickets object
127 my $tickets = $search->TicketsObj;
128
129 #for each ticket we've found
130 while ( my $ticket = $tickets->Next() ) {
131     print $ticket->Id() . ": " if ($verbose);
132
133     my $transaction = get_transaction($ticket);
134     print loc("Using transaction #[_1]...", $transaction->id)
135         if $verbose && $transaction;
136
137     # perform some more advanced check
138     if ($condition) {
139         my $condition_obj = $condition->new(
140             TransactionObj => $transaction,
141             TicketObj      => $ticket,
142             ScripObj       => $void_scrip,
143             TemplateObj    => $template_obj,
144             Argument       => $condition_arg,
145             CurrentUser    => $CurrentUser,
146         );
147
148         # if the condition doesn't apply, get out of here
149
150         next unless ( $condition_obj->IsApplicable );
151         print loc("Condition matches...") if ($verbose);
152     }
153
154     #prepare our action
155     my $action_obj = $action->new(
156         TicketObj      => $ticket,
157         TransactionObj => $transaction,
158         TemplateObj    => $template_obj,
159         Argument       => $action_arg,
160         ScripObj       => $void_scrip,
161         ScripActionObj => $void_scrip_action,
162         CurrentUser    => $CurrentUser,
163     );
164
165     #if our preparation, move onto the next ticket
166     next unless ( $action_obj->Prepare );
167     print loc("Action prepared...") if ($verbose);
168
169     #commit our action.
170     next unless ( $action_obj->Commit );
171     print loc("Action committed.\n") if ($verbose);
172 }
173
174 =head2 get_transaction
175
176 Takes ticket and returns its transaction acording to command
177 line arguments C<--transaction> and <--transaction-type>.
178
179 =cut
180
181 sub get_transaction {
182     my $ticket = shift;
183     my $txns = $ticket->Transactions;
184     $txns->OrderByCols(
185         { FIELD => 'Created', ORDER => $transaction },
186         { FIELD => 'id', ORDER => $transaction },
187     );
188     $txns->Limit( FIELD => 'Type', VALUE => $transaction_type )
189         if $transaction_type;
190     $txns->RowsPerPage(1);
191     return $txns->First;
192 }
193
194 # {{{ load_module 
195
196 =head2 load_module
197
198 Loads a perl module, dying nicely if it can't find it.
199
200 =cut
201
202 sub load_module {
203     my $modname = shift;
204     eval "require $modname";
205     if ($@) {
206         die loc( "Failed to load module [_1]. ([_2])", $modname, $@ );
207     }
208
209 }
210
211 # }}}
212
213 # {{{ loc 
214
215 =head2 loc LIST
216
217 Localize this string, with the current user's currentuser object
218
219 =cut
220
221 sub loc {
222     $CurrentUser->loc(@_);
223 }
224
225 # }}}
226
227 sub help {
228
229     print loc( "[_1] is a tool to act on tickets from an external scheduling tool, such as cron.", $0 )
230       . "\n";
231     print loc("It takes several arguments:") . "\n\n";
232
233     print "     "
234       . loc( "[_1] - Specify the search module you want to use", "--search" )
235       . "\n";
236     print "     "
237       . loc( "[_1] - An argument to pass to [_2]", "--search-argument", "--search" )
238       . "\n";
239
240     print "     "
241       . loc( "[_1] - Specify the condition module you want to use", "--condition" )
242       . "\n";
243     print "     "
244       . loc( "[_1] - An argument to pass to [_2]", "--condition-argument", "--condition" )
245       . "\n";
246     print "     "
247       . loc( "[_1] - Specify the action module you want to use", "--action" )
248       . "\n";
249     print "     "
250       . loc( "[_1] - An argument to pass to [_2]", "--action-argument", "--action" )
251       . "\n";
252     print "     "
253       . loc( "[_1] - Specify id of the template you want to use", "--template-id" )
254       . "\n";
255     print "     "
256       . loc( "[_1] - Specify if you want to use either 'first' or 'last' tarnsaction", "--transaction" )
257       . "\n";
258     print "     "
259       . loc( "[_1] - Specify the type of a transaction you want to use", "--transaction-type" )
260       . "\n";
261     print "     "
262       . loc( "[_1] - Output status updates to STDOUT", "--verbose" ) . "\n";
263     print "\n";
264     print "\n";
265     print loc("Security:")."\n";
266     print loc("This tool allows the user to run arbitrary perl modules from within RT.")." ". 
267         loc("If this tool were setgid, a hostile local user could use this tool to gain administrative access to RT.")." ".
268         loc("It is incredibly important that nonprivileged users not be allowed to run this tool."). " " . 
269         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";
270     print "\n";
271     print loc("Example:");
272     print "\n";
273     print " "
274       . 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:"
275       )
276       . "\n\n";
277
278     print " bin/rt-crontool \\\n";
279     print "  --search RT::Search::ActiveTicketsInQueue  --search-arg general \\\n";
280     print "  --condition RT::Condition::UntouchedInHours --condition-arg 4 \\\n";
281     print "  --action RT::Action::SetPriority --action-arg 99 \\\n";
282     print "  --verbose\n";
283
284     print "\n";
285     print loc("Escalate tickets"). "\n";
286     print " bin/rt-crontool \\\n";
287     print "  --search RT::Search::ActiveTicketsInQueue  --search-arg general \\\n";
288     print "  --action RT::Action::EscalatePriority\n";
289  
290  
291  
292
293
294
295     exit(0);
296 }