2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2016 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 }}}
51 rt-email-group-admin - Command line tool for administrating NotifyGroup actions
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'
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.
71 For example you can create the following action using this script:
73 rt-email-group-admin --create 'Notify developers' --group 'Development Team'
75 Then you can add the followoing scrip to your Bugs queue:
78 Action: Notify developers
80 Stage: TransactionCreate
82 Your development team will be notified on every new ticket in the queue.
89 # fix lib paths, some may be relative
90 BEGIN { # BEGIN RT CMD BOILERPLATE
93 my @libs = ("@RT_LIB_PATH@", "@LOCAL_LIB_PATH@");
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 );
106 use Getopt::Long qw(GetOptions);
107 Getopt::Long::Configure( "pass_through" );
114 if ( GetOptions( 'list' => \$tmp ) && $tmp ) {
117 elsif ( GetOptions( 'create=s' => \$tmp ) && $tmp ) {
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'} } ) {
130 elsif ( GetOptions( 'add=s' => \$tmp ) && $tmp ) {
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'} } ) {
142 elsif ( GetOptions( 'switch=s' => \$tmp ) && $tmp ) {
144 $opts->{'name'} = $tmp;
146 elsif ( GetOptions( 'rename=s' => \$tmp ) && $tmp ) {
148 $opts->{'name'} = $tmp;
149 GetOptions( 'newname=s' => \$opts->{'newname'} );
150 unless ( $opts->{'newname'} ) {
155 elsif ( GetOptions( 'delete=s' => \$tmp ) && $tmp) {
157 $opts->{'name'} = $tmp;
167 Pod::Usage::pod2usage({ verbose => 2 });
171 if ( GetOptions( 'help|h' => \$help ) && $help ) {
182 require RT::Principal;
185 require RT::ScripActions;
191 print STDERR $@ ."\n";
199 rt-email-group-admin --COMMAND ARGS
205 Lists actions and its descriptions.
210 my $actions = _get_our_actions();
211 while( my $a = $actions->Next ) {
220 print "Name: ". $action->Name() ."\n";
221 print "Module: ". $action->ExecModule() ."\n";
223 my @princ = argument_to_list( $action );
227 my $obj = RT::Principal->new( RT->SystemUser );
229 next unless $obj->id;
231 print "\t". $obj->PrincipalType;
232 print "\t=> ". $obj->Object->Name;
233 print "(Disabled!!!)" if $obj->Disabled;
240 =head2 create NAME [--comment] [--group GNAME] [--user NAME-OR-EMAIL]
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.
249 my $actions = RT::ScripActions->new( RT->SystemUser );
252 VALUE => $opts->{'name'},
254 if ( $actions->Count ) {
255 print STDERR "ScripAction '". $opts->{'name'} ."' allready exists\n";
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";
266 my $action = __create_empty( $opts->{'name'}, $opts->{'comment'} );
268 __add( $action, $_ ) foreach( @users );
269 __add( $action, $_ ) foreach( @groups );
276 my $as_comment = shift || 0;
277 require RT::ScripAction;
278 my $action = RT::ScripAction->new( RT->SystemUser );
281 Description => "Created with rt-email-group-admin script",
282 ExecModule => $as_comment? 'NotifyGroupAsComment': 'NotifyGroup',
292 grep { $_->[1] ? 1: do { print STDERR "Group '$_->[0]' skipped, doesn't exist\n"; 0; } }
293 map { [$_, __check_group($_)] } @_;
298 my $instance = shift;
300 my $obj = RT::Group->new( RT->SystemUser );
301 $obj->LoadUserDefinedGroup( $instance );
302 return $obj->id ? $obj : undef;
308 grep { $_->[1] ? 1: do { print STDERR "User '$_->[0]' skipped, doesn't exist and couldn't autocreate\n"; 0; } }
309 map { [$_, __check_user($_)] } @_;
314 my $instance = shift;
316 my $obj = RT::User->new( RT->SystemUser );
317 $obj->Load( $instance );
318 $obj->LoadByEmail( $instance )
319 if not $obj->id and $instance =~ /@/;
322 my ($ok, $msg) = $obj->Create(
324 EmailAddress => $instance,
326 Comments => 'Autocreated when added to notify action via rt-email-group-admin',
328 print STDERR "Autocreate of user '$instance' failed: $msg\n"
332 return $obj->id ? $obj : undef;
335 =head2 add NAME [--group GNAME] [--user NAME-OR-EMAIL]
337 Adds groups and/or users to recipients of the action NAME. The user, if
338 specified, will be autocreated if necessary.
343 my $action = _get_action_by_name( $opts->{'name'} );
345 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
349 my @groups = _check_groups( @{ $opts->{'groups'} } );
350 my @users = _check_users( @{ $opts->{'users'} } );
352 unless ( @users + @groups ) {
353 print STDERR "List of groups and users is empty\n";
357 __add( $action, $_ ) foreach @users;
358 __add( $action, $_ ) foreach @groups;
368 my @cur = argument_to_list( $action );
371 return if grep $_ == $id, @cur;
375 return $action->__Set( Field => 'Argument', Value => join(',', @cur) );
380 Deletes action NAME if scrips doesn't use it.
385 my $action = _get_action_by_name( $opts->{'name'} );
387 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
392 my $scrips = RT::Scrips->new( RT->SystemUser );
393 $scrips->Limit( FIELD => 'ScripAction', VALUE => $action->id );
394 $scrips->FindAllRows;
395 if ( $scrips->Count ) {
397 while( my $s = $scrips->Next ) {
400 print STDERR "ScripAction '". $opts->{'name'} ."'"
401 . " is in use by Scrip(s) ". join( ", ", map "#$_", @sid )
406 return __delete( $action );
410 require DBIx::SearchBuilder::Record;
411 return DBIx::SearchBuilder::Record::Delete( shift );
414 sub _get_action_by_name {
416 my $actions = _get_our_actions();
422 if ( $actions->Count > 1 ) {
423 print STDERR "More then one ScripAction with name '$name'\n";
426 return $actions->First;
431 Switch action NAME from notify as correspondence to comment and back.
436 my $action = _get_action_by_name( $opts->{'name'} );
438 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
443 NotifyGroup => 'NotifyGroupAsComment',
444 NotifyGroupAsComment => 'NotifyGroup'
447 return $action->__Set(
448 Field => 'ExecModule',
449 Value => $h{ $action->ExecModule }
453 =head2 rename NAME --newname NEWNAME
455 Renames action NAME to NEWNAME.
460 my $action = _get_action_by_name( $opts->{'name'} );
462 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
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";
473 return $action->__Set(
475 Value => $opts->{'newname'},
481 If command has option --group or --user then you can use it more then once,
482 if other is not specified.
490 sub argument_to_list {
492 require RT::Action::NotifyGroup;
493 return RT::Action::NotifyGroup->__SplitArg( $action->Argument );
496 sub _get_our_actions {
497 my $actions = RT::ScripActions->new( RT->SystemUser );
499 FIELD => 'ExecModule',
500 VALUE => 'NotifyGroup',
501 ENTRYAGGREGATOR => 'OR',
504 FIELD => 'ExecModule',
505 VALUE => 'NotifyGroupAsComment',
506 ENTRYAGGREGATOR => 'OR',
514 Ruslan U. Zakirov E<lt>ruz@bestpractical.comE<gt>
518 L<RT::Action::NotifyGroup>, L<RT::Action::NotifyGroupAsComment>