]> git.uio.no Git - usit-rt.git/blob - sbin/rt-email-group-admin
Upgrade to 4.2.2
[usit-rt.git] / sbin / rt-email-group-admin
1 #!/usr/bin/perl
2 # BEGIN BPS TAGGED BLOCK {{{
3 #
4 # COPYRIGHT:
5 #
6 # This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
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
51 rt-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
68 This script list, create, modify or delete scrip actions in the RT DB. Once
69 you've created an action you can use it in a scrip.
70
71 For example you can create the following action using this script:
72
73     rt-email-group-admin --create 'Notify developers' --group 'Development Team'
74
75 Then 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
82 Your development team will be notified on every new ticket in the queue.
83
84 =cut
85
86 use warnings;
87 use strict;
88
89 # fix lib paths, some may be relative
90 BEGIN { # BEGIN RT CMD BOILERPLATE
91     require File::Spec;
92     require Cwd;
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) ) {
98             $bin_path ||= ( File::Spec->splitpath(Cwd::abs_path(__FILE__)) )[1];
99             $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
100         }
101         unshift @INC, $lib;
102     }
103
104 }
105
106 use Getopt::Long qw(GetOptions);
107 Getopt::Long::Configure( "pass_through" );
108
109 our $cmd = 'usage';
110 our $opts = {};
111
112 sub 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
165 sub usage {
166     require Pod::Usage;
167     Pod::Usage::pod2usage({ verbose => 2 });
168 }
169
170 my $help;
171 if ( GetOptions( 'help|h' => \$help ) && $help ) {
172     usage();
173     exit;
174 }
175
176 parse_args();
177
178 require RT;
179 RT->LoadConfig;
180 RT->Init;
181
182 require RT::Principal;
183 require RT::User;
184 require RT::Group;
185 require RT::ScripActions;
186
187
188 {
189     eval "main::$cmd()";
190     if ( $@ ) {
191         print STDERR $@ ."\n";
192     }
193 }
194
195 exit(0);
196
197 =head1 USAGE
198
199 rt-email-group-admin --COMMAND ARGS
200
201 =head1 COMMANDS
202
203 =head2 list
204
205 Lists actions and its descriptions.
206
207 =cut
208
209 sub list {
210     my $actions = _get_our_actions();
211     while( my $a = $actions->Next ) {
212         _list( $a );
213     }
214     return;
215 }
216
217 sub _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
240 =head2 create NAME [--comment] [--group GNAME] [--user NAME-OR-EMAIL]
241
242 Creates new action with NAME and adds users and/or groups to its
243 recipient list. Would be notify as comment if --comment specified.  The
244 user, if specified, will be autocreated if necessary.
245
246 =cut
247
248 sub 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
274 sub __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
289 sub _check_groups
290 {
291     return map {$_->[1]}
292         grep { $_->[1] ? 1: do { print STDERR "Group '$_->[0]' skipped, doesn't exist\n"; 0; } }
293         map { [$_, __check_group($_)] } @_;
294 }
295
296 sub __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
305 sub _check_users
306 {
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($_)] } @_;
310 }
311
312 sub __check_user
313 {
314     my $instance = shift;
315     require RT::User;
316     my $obj = RT::User->new( RT->SystemUser );
317     $obj->Load( $instance );
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
332     return $obj->id ? $obj : undef;
333 }
334
335 =head2 add NAME [--group GNAME] [--user NAME-OR-EMAIL]
336
337 Adds groups and/or users to recipients of the action NAME.  The user, if
338 specified, will be autocreated if necessary.
339
340 =cut
341
342 sub 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
363 sub __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
380 Deletes action NAME if scrips doesn't use it.
381
382 =cut
383
384 sub 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 );
394     $scrips->FindAllRows;
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
409 sub __delete {
410     require DBIx::SearchBuilder::Record;
411     return DBIx::SearchBuilder::Record::Delete( shift );
412 }
413
414 sub _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
431 Switch action NAME from notify as correspondence to comment and back.
432
433 =cut
434
435 sub 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
455 Renames action NAME to NEWNAME.
456
457 =cut
458
459 sub 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
481 If command has option --group or --user then you can use it more then once,
482 if other is not specified.
483
484 =cut
485
486 ###############
487 #### Utils ####
488 ###############
489
490 sub argument_to_list {
491     my $action = shift;
492     require RT::Action::NotifyGroup;
493     return RT::Action::NotifyGroup->__SplitArg( $action->Argument );
494 }
495
496 sub _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
514 Ruslan U. Zakirov E<lt>ruz@bestpractical.comE<gt>
515
516 =head1 SEE ALSO
517
518 L<RT::Action::NotifyGroup>, L<RT::Action::NotifyGroupAsComment>
519
520 =cut