2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2014 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
92 my @libs = ("@RT_LIB_PATH@", "@LOCAL_LIB_PATH@");
96 unless ( File::Spec->file_name_is_absolute($lib) ) {
98 if ( File::Spec->file_name_is_absolute(__FILE__) ) {
99 $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
104 $bin_path = $FindBin::Bin;
107 $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
114 use Getopt::Long qw(GetOptions);
115 Getopt::Long::Configure( "pass_through" );
122 if ( GetOptions( 'list' => \$tmp ) && $tmp ) {
125 elsif ( GetOptions( 'create=s' => \$tmp ) && $tmp ) {
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'} } ) {
138 elsif ( GetOptions( 'add=s' => \$tmp ) && $tmp ) {
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'} } ) {
150 elsif ( GetOptions( 'switch=s' => \$tmp ) && $tmp ) {
152 $opts->{'name'} = $tmp;
154 elsif ( GetOptions( 'rename=s' => \$tmp ) && $tmp ) {
156 $opts->{'name'} = $tmp;
157 GetOptions( 'newname=s' => \$opts->{'newname'} );
158 unless ( $opts->{'newname'} ) {
163 elsif ( GetOptions( 'delete=s' => \$tmp ) && $tmp) {
165 $opts->{'name'} = $tmp;
175 Pod::Usage::pod2usage({ verbose => 2 });
179 if ( GetOptions( 'help|h' => \$help ) && $help ) {
190 require RT::Principal;
193 require RT::ScripActions;
199 print STDERR $@ ."\n";
207 rt-email-group-admin --COMMAND ARGS
213 Lists actions and its descriptions.
218 my $actions = _get_our_actions();
219 while( my $a = $actions->Next ) {
228 print "Name: ". $action->Name() ."\n";
229 print "Module: ". $action->ExecModule() ."\n";
231 my @princ = argument_to_list( $action );
235 my $obj = RT::Principal->new( RT->SystemUser );
237 next unless $obj->id;
239 print "\t". $obj->PrincipalType;
240 print "\t=> ". $obj->Object->Name;
241 print "(Disabled!!!)" if $obj->Disabled;
248 =head2 create NAME [--comment] [--group GNAME] [--user NAME-OR-EMAIL]
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.
257 my $actions = RT::ScripActions->new( RT->SystemUser );
260 VALUE => $opts->{'name'},
262 if ( $actions->Count ) {
263 print STDERR "ScripAction '". $opts->{'name'} ."' allready exists\n";
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";
274 my $action = __create_empty( $opts->{'name'}, $opts->{'comment'} );
276 __add( $action, $_ ) foreach( @users );
277 __add( $action, $_ ) foreach( @groups );
284 my $as_comment = shift || 0;
285 require RT::ScripAction;
286 my $action = RT::ScripAction->new( RT->SystemUser );
289 Description => "Created with rt-email-group-admin script",
290 ExecModule => $as_comment? 'NotifyGroupAsComment': 'NotifyGroup',
300 grep { $_->[1] ? 1: do { print STDERR "Group '$_->[0]' skipped, doesn't exist\n"; 0; } }
301 map { [$_, __check_group($_)] } @_;
306 my $instance = shift;
308 my $obj = RT::Group->new( RT->SystemUser );
309 $obj->LoadUserDefinedGroup( $instance );
310 return $obj->id ? $obj : undef;
316 grep { $_->[1] ? 1: do { print STDERR "User '$_->[0]' skipped, doesn't exist and couldn't autocreate\n"; 0; } }
317 map { [$_, __check_user($_)] } @_;
322 my $instance = shift;
324 my $obj = RT::User->new( RT->SystemUser );
325 $obj->Load( $instance );
326 $obj->LoadByEmail( $instance )
327 if not $obj->id and $instance =~ /@/;
330 my ($ok, $msg) = $obj->Create(
332 EmailAddress => $instance,
334 Comments => 'Autocreated when added to notify action via rt-email-group-admin',
336 print STDERR "Autocreate of user '$instance' failed: $msg\n"
340 return $obj->id ? $obj : undef;
343 =head2 add NAME [--group GNAME] [--user NAME-OR-EMAIL]
345 Adds groups and/or users to recipients of the action NAME. The user, if
346 specified, will be autocreated if necessary.
351 my $action = _get_action_by_name( $opts->{'name'} );
353 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
357 my @groups = _check_groups( @{ $opts->{'groups'} } );
358 my @users = _check_users( @{ $opts->{'users'} } );
360 unless ( @users + @groups ) {
361 print STDERR "List of groups and users is empty\n";
365 __add( $action, $_ ) foreach @users;
366 __add( $action, $_ ) foreach @groups;
376 my @cur = argument_to_list( $action );
379 return if grep $_ == $id, @cur;
383 return $action->__Set( Field => 'Argument', Value => join(',', @cur) );
388 Deletes action NAME if scrips doesn't use it.
393 my $action = _get_action_by_name( $opts->{'name'} );
395 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
400 my $scrips = RT::Scrips->new( RT->SystemUser );
401 $scrips->Limit( FIELD => 'ScripAction', VALUE => $action->id );
402 if ( $scrips->Count ) {
404 while( my $s = $scrips->Next ) {
407 print STDERR "ScripAction '". $opts->{'name'} ."'"
408 . " is in use by Scrip(s) ". join( ", ", map "#$_", @sid )
413 return __delete( $action );
417 require DBIx::SearchBuilder::Record;
418 return DBIx::SearchBuilder::Record::Delete( shift );
421 sub _get_action_by_name {
423 my $actions = _get_our_actions();
429 if ( $actions->Count > 1 ) {
430 print STDERR "More then one ScripAction with name '$name'\n";
433 return $actions->First;
438 Switch action NAME from notify as correspondence to comment and back.
443 my $action = _get_action_by_name( $opts->{'name'} );
445 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
450 NotifyGroup => 'NotifyGroupAsComment',
451 NotifyGroupAsComment => 'NotifyGroup'
454 return $action->__Set(
455 Field => 'ExecModule',
456 Value => $h{ $action->ExecModule }
460 =head2 rename NAME --newname NEWNAME
462 Renames action NAME to NEWNAME.
467 my $action = _get_action_by_name( $opts->{'name'} );
469 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
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";
480 return $action->__Set(
482 Value => $opts->{'newname'},
488 If command has option --group or --user then you can use it more then once,
489 if other is not specified.
497 sub argument_to_list {
499 require RT::Action::NotifyGroup;
500 return RT::Action::NotifyGroup->__SplitArg( $action->Argument );
503 sub _get_our_actions {
504 my $actions = RT::ScripActions->new( RT->SystemUser );
506 FIELD => 'ExecModule',
507 VALUE => 'NotifyGroup',
508 ENTRYAGGREGATOR => 'OR',
511 FIELD => 'ExecModule',
512 VALUE => 'NotifyGroupAsComment',
513 ENTRYAGGREGATOR => 'OR',
521 Ruslan U. Zakirov E<lt>ruz@bestpractical.comE<gt>
525 L<RT::Action::NotifyGroup>, L<RT::Action::NotifyGroupAsComment>