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