Putting 4.2.0 on top of 4.0.17
[usit-rt.git] / sbin / rt-email-group-admin
CommitLineData
84fb5b46
MKG
1#!/usr/bin/perl
2# BEGIN BPS TAGGED BLOCK {{{
3#
4# COPYRIGHT:
5#
403d7b0b 6# This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
84fb5b46
MKG
7# <sales@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/licenses/old-licenses/gpl-2.0.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=head1 NAME
50
51rt-email-group-admin - Command line tool for administrating NotifyGroup actions
52
53=head1 SYNOPSIS
54
55 rt-email-group-admin --list
56 rt-email-group-admin --create 'Notify foo team' --group Foo
57 rt-email-group-admin --create 'Notify foo team as comment' --comment --group Foo
58 rt-email-group-admin --create 'Notify group Foo and Bar' --group Foo --group Bar
59 rt-email-group-admin --create 'Notify user foo@bar.com' --user foo@bar.com
60 rt-email-group-admin --create 'Notify VIPs' --user vip1@bar.com
61 rt-email-group-admin --add 'Notify VIPs' --user vip2@bar.com --group vip1 --user vip3@foo.com
62 rt-email-group-admin --rename 'Notify VIPs' --newname 'Inform VIPs'
63 rt-email-group-admin --switch 'Notify VIPs'
64 rt-email-group-admin --delete 'Notify user foo@bar.com'
65
66=head1 DESCRIPTION
67
68This script list, create, modify or delete scrip actions in the RT DB. Once
69you've created an action you can use it in a scrip.
70
71For example you can create the following action using this script:
72
73 rt-email-group-admin --create 'Notify developers' --group 'Development Team'
74
75Then you can add the followoing scrip to your Bugs queue:
76
77 Condition: On Create
78 Action: Notify developers
79 Template: Transaction
80 Stage: TransactionCreate
81
82Your development team will be notified on every new ticket in the queue.
83
84=cut
85
86use warnings;
87use strict;
88
89# fix lib paths, some may be relative
af59614d 90BEGIN { # BEGIN RT CMD BOILERPLATE
84fb5b46 91 require File::Spec;
af59614d 92 require Cwd;
84fb5b46
MKG
93 my @libs = ("lib", "local/lib");
94 my $bin_path;
95
96 for my $lib (@libs) {
97 unless ( File::Spec->file_name_is_absolute($lib) ) {
af59614d 98 $bin_path ||= ( File::Spec->splitpath(Cwd::abs_path(__FILE__)) )[1];
84fb5b46
MKG
99 $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
100 }
101 unshift @INC, $lib;
102 }
103
104}
105
106use Getopt::Long qw(GetOptions);
107Getopt::Long::Configure( "pass_through" );
108
109our $cmd = 'usage';
110our $opts = {};
111
112sub parse_args {
113 my $tmp;
114 if ( GetOptions( 'list' => \$tmp ) && $tmp ) {
115 $cmd = 'list';
116 }
117 elsif ( GetOptions( 'create=s' => \$tmp ) && $tmp ) {
118 $cmd = 'create';
119 $opts->{'name'} = $tmp;
120 $opts->{'groups'} = [];
121 $opts->{'users'} = [];
122 GetOptions( 'comment' => \$opts->{'comment'} );
123 GetOptions( 'group:s@' => $opts->{'groups'} );
124 GetOptions( 'user:s@' => $opts->{'users'} );
125 unless ( @{ $opts->{'users'} } + @{ $opts->{'groups'} } ) {
126 usage();
127 exit(-1);
128 }
129 }
130 elsif ( GetOptions( 'add=s' => \$tmp ) && $tmp ) {
131 $cmd = 'add';
132 $opts->{'name'} = $tmp;
133 $opts->{'groups'} = [];
134 $opts->{'users'} = [];
135 GetOptions( 'group:s@' => $opts->{'groups'} );
136 GetOptions( 'user:s@' => $opts->{'users'} );
137 unless ( @{ $opts->{'users'} } + @{ $opts->{'groups'} } ) {
138 usage();
139 exit(-1);
140 }
141 }
142 elsif ( GetOptions( 'switch=s' => \$tmp ) && $tmp ) {
143 $cmd = 'switch';
144 $opts->{'name'} = $tmp;
145 }
146 elsif ( GetOptions( 'rename=s' => \$tmp ) && $tmp ) {
147 $cmd = 'rename';
148 $opts->{'name'} = $tmp;
149 GetOptions( 'newname=s' => \$opts->{'newname'} );
150 unless ( $opts->{'newname'} ) {
151 usage();
152 exit(-1);
153 }
154 }
155 elsif ( GetOptions( 'delete=s' => \$tmp ) && $tmp) {
156 $cmd = 'delete';
157 $opts->{'name'} = $tmp;
158 } else {
159 $cmd = 'usage';
160 }
161
162 return;
163}
164
165sub usage {
166 require Pod::Usage;
167 Pod::Usage::pod2usage({ verbose => 2 });
168}
169
170my $help;
171if ( GetOptions( 'help|h' => \$help ) && $help ) {
172 usage();
173 exit;
174}
175
176parse_args();
177
178require RT;
179RT->LoadConfig;
180RT->Init;
181
182require RT::Principal;
183require RT::User;
184require RT::Group;
185require RT::ScripActions;
186
187
188{
189 eval "main::$cmd()";
190 if ( $@ ) {
191 print STDERR $@ ."\n";
192 }
193}
194
195exit(0);
196
197=head1 USAGE
198
199rt-email-group-admin --COMMAND ARGS
200
201=head1 COMMANDS
202
203=head2 list
204
205Lists actions and its descriptions.
206
207=cut
208
209sub list {
210 my $actions = _get_our_actions();
211 while( my $a = $actions->Next ) {
212 _list( $a );
213 }
214 return;
215}
216
217sub _list {
218 my $action = shift;
219
220 print "Name: ". $action->Name() ."\n";
221 print "Module: ". $action->ExecModule() ."\n";
222
223 my @princ = argument_to_list( $action );
224
225 print "Members: \n";
226 foreach( @princ ) {
227 my $obj = RT::Principal->new( RT->SystemUser );
228 $obj->Load( $_ );
229 next unless $obj->id;
230
231 print "\t". $obj->PrincipalType;
232 print "\t=> ". $obj->Object->Name;
233 print "(Disabled!!!)" if $obj->Disabled;
234 print "\n";
235 }
236 print "\n";
237 return;
238}
239
af59614d 240=head2 create NAME [--comment] [--group GNAME] [--user NAME-OR-EMAIL]
84fb5b46
MKG
241
242Creates new action with NAME and adds users and/or groups to its
af59614d
MKG
243recipient list. Would be notify as comment if --comment specified. The
244user, if specified, will be autocreated if necessary.
84fb5b46
MKG
245
246=cut
247
248sub create {
249 my $actions = RT::ScripActions->new( RT->SystemUser );
250 $actions->Limit(
251 FIELD => 'Name',
252 VALUE => $opts->{'name'},
253 );
254 if ( $actions->Count ) {
255 print STDERR "ScripAction '". $opts->{'name'} ."' allready exists\n";
256 exit(-1);
257 }
258
259 my @groups = _check_groups( @{ $opts->{'groups'} } );
260 my @users = _check_users( @{ $opts->{'users'} } );
261 unless ( @users + @groups ) {
262 print STDERR "List of groups and users is empty\n";
263 exit(-1);
264 }
265
266 my $action = __create_empty( $opts->{'name'}, $opts->{'comment'} );
267
268 __add( $action, $_ ) foreach( @users );
269 __add( $action, $_ ) foreach( @groups );
270
271 return;
272}
273
274sub __create_empty {
275 my $name = shift;
276 my $as_comment = shift || 0;
277 require RT::ScripAction;
278 my $action = RT::ScripAction->new( RT->SystemUser );
279 $action->Create(
280 Name => $name,
281 Description => "Created with rt-email-group-admin script",
282 ExecModule => $as_comment? 'NotifyGroupAsComment': 'NotifyGroup',
283 Argument => '',
284 );
285
286 return $action;
287}
288
289sub _check_groups
290{
af59614d
MKG
291 return map {$_->[1]}
292 grep { $_->[1] ? 1: do { print STDERR "Group '$_->[0]' skipped, doesn't exist\n"; 0; } }
293 map { [$_, __check_group($_)] } @_;
84fb5b46
MKG
294}
295
296sub __check_group
297{
298 my $instance = shift;
299 require RT::Group;
300 my $obj = RT::Group->new( RT->SystemUser );
301 $obj->LoadUserDefinedGroup( $instance );
302 return $obj->id ? $obj : undef;
303}
304
305sub _check_users
306{
af59614d
MKG
307 return map {$_->[1]}
308 grep { $_->[1] ? 1: do { print STDERR "User '$_->[0]' skipped, doesn't exist and couldn't autocreate\n"; 0; } }
309 map { [$_, __check_user($_)] } @_;
84fb5b46
MKG
310}
311
312sub __check_user
313{
314 my $instance = shift;
315 require RT::User;
316 my $obj = RT::User->new( RT->SystemUser );
317 $obj->Load( $instance );
af59614d
MKG
318 $obj->LoadByEmail( $instance )
319 if not $obj->id and $instance =~ /@/;
320
321 unless ($obj->id) {
322 my ($ok, $msg) = $obj->Create(
323 Name => $instance,
324 EmailAddress => $instance,
325 Privileged => 0,
326 Comments => 'Autocreated when added to notify action via rt-email-group-admin',
327 );
328 print STDERR "Autocreate of user '$instance' failed: $msg\n"
329 unless $ok;
330 }
331
84fb5b46
MKG
332 return $obj->id ? $obj : undef;
333}
334
af59614d 335=head2 add NAME [--group GNAME] [--user NAME-OR-EMAIL]
84fb5b46 336
af59614d
MKG
337Adds groups and/or users to recipients of the action NAME. The user, if
338specified, will be autocreated if necessary.
84fb5b46
MKG
339
340=cut
341
342sub add {
343 my $action = _get_action_by_name( $opts->{'name'} );
344 unless ( $action ) {
345 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
346 exit(-1);
347 }
348
349 my @groups = _check_groups( @{ $opts->{'groups'} } );
350 my @users = _check_users( @{ $opts->{'users'} } );
351
352 unless ( @users + @groups ) {
353 print STDERR "List of groups and users is empty\n";
354 exit(-1);
355 }
356
357 __add( $action, $_ ) foreach @users;
358 __add( $action, $_ ) foreach @groups;
359
360 return;
361}
362
363sub __add
364{
365 my $action = shift;
366 my $obj = shift;
367
368 my @cur = argument_to_list( $action );
369
370 my $id = $obj->id;
371 return if grep $_ == $id, @cur;
372
373 push @cur, $id;
374
375 return $action->__Set( Field => 'Argument', Value => join(',', @cur) );
376}
377
378=head2 delete NAME
379
380Deletes action NAME if scrips doesn't use it.
381
382=cut
383
384sub delete {
385 my $action = _get_action_by_name( $opts->{'name'} );
386 unless ( $action ) {
387 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
388 exit(-1);
389 }
390
391 require RT::Scrips;
392 my $scrips = RT::Scrips->new( RT->SystemUser );
393 $scrips->Limit( FIELD => 'ScripAction', VALUE => $action->id );
af59614d 394 $scrips->FindAllRows;
84fb5b46
MKG
395 if ( $scrips->Count ) {
396 my @sid;
397 while( my $s = $scrips->Next ) {
398 push @sid, $s->id;
399 }
400 print STDERR "ScripAction '". $opts->{'name'} ."'"
401 . " is in use by Scrip(s) ". join( ", ", map "#$_", @sid )
402 . "\n";
403 exit(-1);
404 }
405
406 return __delete( $action );
407}
408
409sub __delete {
410 require DBIx::SearchBuilder::Record;
411 return DBIx::SearchBuilder::Record::Delete( shift );
412}
413
414sub _get_action_by_name {
415 my $name = shift;
416 my $actions = _get_our_actions();
417 $actions->Limit(
418 FIELD => 'Name',
419 VALUE => $name
420 );
421
422 if ( $actions->Count > 1 ) {
423 print STDERR "More then one ScripAction with name '$name'\n";
424 }
425
426 return $actions->First;
427}
428
429=head2 switch NAME
430
431Switch action NAME from notify as correspondence to comment and back.
432
433=cut
434
435sub switch {
436 my $action = _get_action_by_name( $opts->{'name'} );
437 unless ( $action ) {
438 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
439 exit(-1);
440 }
441
442 my %h = (
443 NotifyGroup => 'NotifyGroupAsComment',
444 NotifyGroupAsComment => 'NotifyGroup'
445 );
446
447 return $action->__Set(
448 Field => 'ExecModule',
449 Value => $h{ $action->ExecModule }
450 );
451}
452
453=head2 rename NAME --newname NEWNAME
454
455Renames action NAME to NEWNAME.
456
457=cut
458
459sub rename {
460 my $action = _get_action_by_name( $opts->{'name'} );
461 unless ( $action ) {
462 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
463 exit(-1);
464 }
465
466 my $actions = RT::ScripActions->new( RT->SystemUser );
467 $actions->Limit( FIELD => 'Name', VALUE => $opts->{'newname'} );
468 if ( $actions->Count ) {
469 print STDERR "ScripAction '". $opts->{'newname'} ."' allready exists\n";
470 exit(-1);
471 }
472
473 return $action->__Set(
474 Field => 'Name',
475 Value => $opts->{'newname'},
476 );
477}
478
479=head2 NOTES
480
481If command has option --group or --user then you can use it more then once,
482if other is not specified.
483
484=cut
485
486###############
487#### Utils ####
488###############
489
490sub argument_to_list {
491 my $action = shift;
492 require RT::Action::NotifyGroup;
493 return RT::Action::NotifyGroup->__SplitArg( $action->Argument );
494}
495
496sub _get_our_actions {
497 my $actions = RT::ScripActions->new( RT->SystemUser );
498 $actions->Limit(
499 FIELD => 'ExecModule',
500 VALUE => 'NotifyGroup',
501 ENTRYAGGREGATOR => 'OR',
502 );
503 $actions->Limit(
504 FIELD => 'ExecModule',
505 VALUE => 'NotifyGroupAsComment',
506 ENTRYAGGREGATOR => 'OR',
507 );
508
509 return $actions;
510}
511
512=head1 AUTHOR
513
514Ruslan U. Zakirov E<lt>ruz@bestpractical.comE<gt>
515
516=head1 SEE ALSO
517
518L<RT::Action::NotifyGroup>, L<RT::Action::NotifyGroupAsComment>
519
520=cut