import rt 3.2.2
[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-2004 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 ("@RT_LIB_PATH@", "@LOCAL_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, $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             "help"            => \$help,
87             "verbose|v"       => \$verbose );
88
89 help() if $help or not $search or not $action;
90
91 # We _must_ have a search object
92 load_module($search);
93 load_module($action)    if ($action);
94 load_module($condition) if ($condition);
95
96 # load template if specified
97 my $template_obj;
98 if ($template_id) {
99     $template_obj = RT::Template->new($CurrentUser);
100     $template_obj->Load($template_id);
101 }
102
103 #At the appointed time:
104
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
111 );
112
113 $search->Prepare();
114
115 # TicketsFound is an RT::Tickets object
116 my $tickets = $search->TicketsObj;
117
118 #for each ticket we've found
119 while ( my $ticket = $tickets->Next() ) {
120     print $ticket->Id() . ": " if ($verbose);
121
122     # perform some more advanced check
123     if ($condition) {
124         my $condition_obj = $condition->new( TicketObj => $ticket,
125                                              Argument  => $condition_arg,
126                                              CurrentUser => $CurrentUser );
127
128         # if the condition doesn't apply, get out of here
129
130         next unless ( $condition_obj->IsApplicable );
131         print loc("Condition matches...") if ($verbose);
132     }
133
134     #prepare our action
135     my $action_obj = $action->new(
136         TicketObj   => $ticket,
137         TemplateObj => $template_obj,
138         Argument    => $action_arg,
139         CurrentUser => $CurrentUser
140     );
141
142     #if our preparation, move onto the next ticket
143     next unless ( $action_obj->Prepare );
144     print loc("Action prepared...") if ($verbose);
145
146     #commit our action.
147     next unless ( $action_obj->Commit );
148     print loc("Action committed.\n") if ($verbose);
149 }
150
151 # {{{ load_module 
152
153 =head2 load_module
154
155 Loads a perl module, dying nicely if it can't find it.
156
157 =cut
158
159 sub load_module {
160     my $modname = shift;
161     eval "require $modname";
162     if ($@) {
163         die loc( "Failed to load module [_1]. ([_2])", $modname, $@ );
164     }
165
166 }
167
168 # }}}
169
170 # {{{ loc 
171
172 =head2 loc LIST
173
174 Localize this string, with the current user's currentuser object
175
176 =cut
177
178 sub loc {
179     $CurrentUser->loc(@_);
180 }
181
182 # }}}
183
184 sub help {
185
186     print loc( "[_1] is a tool to act on tickets from an external scheduling tool, such as cron.", $0 )
187       . "\n";
188     print loc("It takes several arguments:") . "\n\n";
189
190     print "     "
191       . loc( "[_1] - Specify the search module you want to use", "--search" )
192       . "\n";
193     print "     "
194       . loc( "[_1] - An argument to pass to [_2]", "--search-argument", "--search" )
195       . "\n";
196
197     print "     "
198       . loc( "[_1] - Specify the condition module you want to use", "--condition" )
199       . "\n";
200     print "     "
201       . loc( "[_1] - An argument to pass to [_2]", "--condition-argument", "--condition" )
202       . "\n";
203     print "     "
204       . loc( "[_1] - Specify the action module you want to use", "--action" )
205       . "\n";
206     print "     "
207       . loc( "[_1] - An argument to pass to [_2]", "--action-argument", "--action" )
208       . "\n";
209     print "     "
210       . loc( "[_1] - Output status updates to STDOUT", "--verbose" ) . "\n";
211     print "\n";
212     print "\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";
218     print "\n";
219     print loc("Example:");
220     print "\n";
221     print " "
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:"
223       )
224       . "\n\n";
225
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";
231
232     print "\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";
237  
238  
239  
240
241
242
243     exit(0);
244 }