2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
7 # <sales@bestpractical.com>
9 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
31 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
48 # END BPS TAGGED BLOCK }}}
52 rt-email-group-admin - Command line tool for administrating NotifyGroup actions
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'
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.
72 For example you can create the following action using this script:
74 rt-email-group-admin --create 'Notify developers' --group 'Development Team'
76 Then you can add the followoing scrip to your Bugs queue:
79 Action: Notify developers
81 Stage: TransactionCreate
83 Your development team will be notified on every new ticket in the queue.
90 # fix lib paths, some may be relative
91 BEGIN { # BEGIN RT CMD BOILERPLATE
94 my @libs = ("@RT_LIB_PATH@", "@LOCAL_LIB_PATH@");
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 );
107 use Getopt::Long qw(GetOptions);
108 Getopt::Long::Configure( "pass_through" );
115 if ( GetOptions( 'list' => \$tmp ) && $tmp ) {
118 elsif ( GetOptions( 'create=s' => \$tmp ) && $tmp ) {
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'} } ) {
131 elsif ( GetOptions( 'add=s' => \$tmp ) && $tmp ) {
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'} } ) {
143 elsif ( GetOptions( 'switch=s' => \$tmp ) && $tmp ) {
145 $opts->{'name'} = $tmp;
147 elsif ( GetOptions( 'rename=s' => \$tmp ) && $tmp ) {
149 $opts->{'name'} = $tmp;
150 GetOptions( 'newname=s' => \$opts->{'newname'} );
151 unless ( $opts->{'newname'} ) {
156 elsif ( GetOptions( 'delete=s' => \$tmp ) && $tmp) {
158 $opts->{'name'} = $tmp;
168 Pod::Usage::pod2usage({ verbose => 2 });
172 if ( GetOptions( 'help|h' => \$help ) && $help ) {
183 require RT::Principal;
186 require RT::ScripActions;
192 print STDERR $@ ."\n";
200 rt-email-group-admin --COMMAND ARGS
206 Lists actions and its descriptions.
211 my $actions = _get_our_actions();
212 while( my $a = $actions->Next ) {
221 print "Name: ". $action->Name() ."\n";
222 print "Module: ". $action->ExecModule() ."\n";
224 my @princ = argument_to_list( $action );
228 my $obj = RT::Principal->new( RT->SystemUser );
230 next unless $obj->id;
232 print "\t". $obj->PrincipalType;
233 print "\t=> ". $obj->Object->Name;
234 print "(Disabled!!!)" if $obj->Disabled;
241 =head2 create NAME [--comment] [--group GNAME] [--user NAME-OR-EMAIL]
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.
250 my $actions = RT::ScripActions->new( RT->SystemUser );
253 VALUE => $opts->{'name'},
255 if ( $actions->Count ) {
256 print STDERR "ScripAction '". $opts->{'name'} ."' allready exists\n";
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";
267 my $action = __create_empty( $opts->{'name'}, $opts->{'comment'} );
269 __add( $action, $_ ) foreach( @users );
270 __add( $action, $_ ) foreach( @groups );
277 my $as_comment = shift || 0;
278 require RT::ScripAction;
279 my $action = RT::ScripAction->new( RT->SystemUser );
282 Description => "Created with rt-email-group-admin script",
283 ExecModule => $as_comment? 'NotifyGroupAsComment': 'NotifyGroup',
293 grep { $_->[1] ? 1: do { print STDERR "Group '$_->[0]' skipped, doesn't exist\n"; 0; } }
294 map { [$_, __check_group($_)] } @_;
299 my $instance = shift;
301 my $obj = RT::Group->new( RT->SystemUser );
302 $obj->LoadUserDefinedGroup( $instance );
303 return $obj->id ? $obj : undef;
309 grep { $_->[1] ? 1: do { print STDERR "User '$_->[0]' skipped, doesn't exist and couldn't autocreate\n"; 0; } }
310 map { [$_, __check_user($_)] } @_;
315 my $instance = shift;
317 my $obj = RT::User->new( RT->SystemUser );
318 $obj->Load( $instance );
319 $obj->LoadByEmail( $instance )
320 if not $obj->id and $instance =~ /@/;
323 my ($ok, $msg) = $obj->Create(
325 EmailAddress => $instance,
327 Comments => 'Autocreated when added to notify action via rt-email-group-admin',
329 print STDERR "Autocreate of user '$instance' failed: $msg\n"
333 return $obj->id ? $obj : undef;
336 =head2 add NAME [--group GNAME] [--user NAME-OR-EMAIL]
338 Adds groups and/or users to recipients of the action NAME. The user, if
339 specified, will be autocreated if necessary.
344 my $action = _get_action_by_name( $opts->{'name'} );
346 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
350 my @groups = _check_groups( @{ $opts->{'groups'} } );
351 my @users = _check_users( @{ $opts->{'users'} } );
353 unless ( @users + @groups ) {
354 print STDERR "List of groups and users is empty\n";
358 __add( $action, $_ ) foreach @users;
359 __add( $action, $_ ) foreach @groups;
369 my @cur = argument_to_list( $action );
372 return if grep $_ == $id, @cur;
376 return $action->__Set( Field => 'Argument', Value => join(',', @cur) );
381 Deletes action NAME if scrips doesn't use it.
386 my $action = _get_action_by_name( $opts->{'name'} );
388 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
393 my $scrips = RT::Scrips->new( RT->SystemUser );
394 $scrips->Limit( FIELD => 'ScripAction', VALUE => $action->id );
395 $scrips->FindAllRows;
396 if ( $scrips->Count ) {
398 while( my $s = $scrips->Next ) {
401 print STDERR "ScripAction '". $opts->{'name'} ."'"
402 . " is in use by Scrip(s) ". join( ", ", map "#$_", @sid )
407 return __delete( $action );
411 require DBIx::SearchBuilder::Record;
412 return DBIx::SearchBuilder::Record::Delete( shift );
415 sub _get_action_by_name {
417 my $actions = _get_our_actions();
423 if ( $actions->Count > 1 ) {
424 print STDERR "More then one ScripAction with name '$name'\n";
427 return $actions->First;
432 Switch action NAME from notify as correspondence to comment and back.
437 my $action = _get_action_by_name( $opts->{'name'} );
439 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
444 NotifyGroup => 'NotifyGroupAsComment',
445 NotifyGroupAsComment => 'NotifyGroup'
448 return $action->__Set(
449 Field => 'ExecModule',
450 Value => $h{ $action->ExecModule }
454 =head2 rename NAME --newname NEWNAME
456 Renames action NAME to NEWNAME.
461 my $action = _get_action_by_name( $opts->{'name'} );
463 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
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";
474 return $action->__Set(
476 Value => $opts->{'newname'},
482 If command has option --group or --user then you can use it more then once,
483 if other is not specified.
491 sub argument_to_list {
493 require RT::Action::NotifyGroup;
494 return RT::Action::NotifyGroup->__SplitArg( $action->Argument );
497 sub _get_our_actions {
498 my $actions = RT::ScripActions->new( RT->SystemUser );
500 FIELD => 'ExecModule',
501 VALUE => 'NotifyGroup',
502 ENTRYAGGREGATOR => 'OR',
505 FIELD => 'ExecModule',
506 VALUE => 'NotifyGroupAsComment',
507 ENTRYAGGREGATOR => 'OR',
515 Ruslan U. Zakirov E<lt>ruz@bestpractical.comE<gt>
519 L<RT::Action::NotifyGroup>, L<RT::Action::NotifyGroupAsComment>