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