73b80aa90531146be9c216293cbb25f9bf60c52b
[freeside.git] / rt / bin / rt-crontool.in
1 #!@PERL@
2 # BEGIN LICENSE BLOCK
3
4 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
5
6 # (Except where explictly superceded by other copyright notices)
7
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
11 # from www.gnu.org.
12
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.
17
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.
22
23
24 # END LICENSE BLOCK
25
26 use strict;
27 use Carp;
28
29 use lib ("@RT_LIB_PATH@", "@LOCAL_LIB_PATH@");
30
31 package RT;
32
33 use Getopt::Long;
34
35 use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
36 use RT::Tickets;
37 use RT::Template;
38
39 #Clean out all the nasties from the environment
40 CleanEnv();
41
42 # Load the config file
43 RT::LoadConfig();
44
45 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
46 RT::Init();
47
48 #Drop setgid permissions
49 RT::DropSetGIDPermissions();
50
51 #Get the current user all loaded
52 my $CurrentUser = GetCurrentUser();
53
54 unless ( $CurrentUser->Id ) {
55     print loc("No RT user found. Please consult your RT administrator.\n");
56     exit(1);
57 }
58
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,
68             "help"            => \$help,
69             "verbose|v"       => \$verbose );
70
71 help() if $help;
72
73 # We _must_ have a search object
74 load_module($search);
75 load_module($action)    if ($action);
76 load_module($condition) if ($condition);
77
78 # load template if specified
79 my $template_obj;
80 if ($template_id) {
81     $template_obj = RT::Template->new($RT::Nobody);
82     $template_obj->LoadById($template_id);
83 }
84
85 #At the appointed time:
86
87 #find a bunch of tickets
88 my $tickets = RT::Tickets->new($CurrentUser);
89 my $search  = $search->new( TicketsObj => $tickets, Argument => $search_arg );
90
91 $search->Prepare();
92
93 # TicketsFound is an RT::Tickets object
94 my $tickets = $search->TicketsObj;
95
96 #for each ticket we've found
97 while ( my $ticket = $tickets->Next() ) {
98     print "\n" . $ticket->Id() . ": " if ($verbose);
99
100     # perform some more advanced check
101     if ($condition) {
102         my $condition_obj = $condition->new( TicketObj => $ticket,
103                                              Argument  => $condition_arg );
104
105         # if the condition doesn't apply, get out of here
106
107         next unless ( $condition_obj->IsApplicable );
108         print loc("Condition matches...") if ($verbose);
109     }
110
111     #prepare our action
112     my $action_obj = $action->new( TicketObj => $ticket,
113                                    TemplateObj => $template_obj,
114                                    Argument  => $action_arg );
115
116     #if our preparation, move onto the next ticket
117     next unless ( $action_obj->Prepare );
118     print loc("Action prepared...") if ($verbose);
119
120     #commit our action.
121     next unless ( $action_obj->Commit );
122     print loc("Action committed.") if ($verbose);
123 }
124
125 # {{{ load_module 
126
127 =head2 load_module
128
129 Loads a perl module, dying nicely if it can't find it.
130
131 =cut
132
133 sub load_module {
134     my $modname = shift;
135     eval "require $modname";
136     if ($@) {
137         die loc( "Failed to load module [_1]. ([_2])", $modname, $@ );
138     }
139
140 }
141
142 # }}}
143
144 # {{{ loc 
145
146 =head2 loc LIST
147
148 Localize this string, with the current user's currentuser object
149
150 =cut
151
152 sub loc {
153     $CurrentUser->loc(@_);
154 }
155
156 # }}}
157
158 sub help {
159
160     print loc( "[_1] is a tool to act on tickets from an external scheduling tool, such as cron.", $0 )
161       . "\n";
162     print loc("It takes several arguments:") . "\n\n";
163
164     print "     "
165       . loc( "[_1] - Specify the search module you want to use", "--search" )
166       . "\n";
167     print "     "
168       . loc( "[_1] - An argument to pass to [_2]", "--search-argument", "--search" )
169       . "\n";
170
171     print "     "
172       . loc( "[_1] - Specify the condition module you want to use", "--condition" )
173       . "\n";
174     print "     "
175       . loc( "[_1] - An argument to pass to [_2]", "--condition-argument", "--condition" )
176       . "\n";
177     print "     "
178       . loc( "[_1] - Specify the action module you want to use", "--action" )
179       . "\n";
180     print "     "
181       . loc( "[_1] - An argument to pass to [_2]", "--action-argument", "--action" )
182       . "\n";
183     print "     "
184       . loc( "[_1] - Output status updates to STDOUT", "--verbose" ) . "\n";
185     print "\n";
186     print "\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";
192     print "\n";
193     print loc("Example:");
194     print "\n";
195     print " "
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:"
197       )
198       . "\n\n";
199
200     print " sbin/cron_shim \\\n";
201     print
202       "  --search RT::Search::ActiveTicketsInQueue  --search-arg general \\\n";
203     print
204       "  --condition RT::Condition::UntouchedInHours --condition-arg 4 \\\n";
205     print "  --action RT::Action::SetPriority --action-arg 99 \\\n";
206     print "  --verbose\n";
207
208
209     exit(0);
210 }