2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2013 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 );
118 require RT::Principal;
121 require RT::ScripActions;
123 use Getopt::Long qw(GetOptions);
130 Getopt::Long::Configure( "pass_through" );
131 if ( GetOptions( 'list' => \$tmp ) && $tmp ) {
134 elsif ( GetOptions( 'create=s' => \$tmp ) && $tmp ) {
136 $opts->{'name'} = $tmp;
137 $opts->{'groups'} = [];
138 $opts->{'users'} = [];
139 GetOptions( 'comment' => \$opts->{'comment'} );
140 GetOptions( 'group:s@' => $opts->{'groups'} );
141 GetOptions( 'user:s@' => $opts->{'users'} );
142 unless ( @{ $opts->{'users'} } + @{ $opts->{'groups'} } ) {
147 elsif ( GetOptions( 'add=s' => \$tmp ) && $tmp ) {
149 $opts->{'name'} = $tmp;
150 $opts->{'groups'} = [];
151 $opts->{'users'} = [];
152 GetOptions( 'group:s@' => $opts->{'groups'} );
153 GetOptions( 'user:s@' => $opts->{'users'} );
154 unless ( @{ $opts->{'users'} } + @{ $opts->{'groups'} } ) {
159 elsif ( GetOptions( 'switch=s' => \$tmp ) && $tmp ) {
161 $opts->{'name'} = $tmp;
163 elsif ( GetOptions( 'rename=s' => \$tmp ) && $tmp ) {
165 $opts->{'name'} = $tmp;
166 GetOptions( 'newname=s' => \$opts->{'newname'} );
167 unless ( $opts->{'newname'} ) {
172 elsif ( GetOptions( 'delete=s' => \$tmp ) && $tmp) {
174 $opts->{'name'} = $tmp;
184 eval "require Pod::PlainText;";
186 print "see `perldoc $0`\n";
188 my $parser = Pod::PlainText->new( sentence => 0, width => 78 );
189 $parser->parse_from_file( $0 );
198 print STDERR $@ ."\n";
206 rt-email-group-admin --COMMAND ARGS
212 Lists actions and its descriptions.
217 my $actions = _get_our_actions();
218 while( my $a = $actions->Next ) {
227 print "Name: ". $action->Name() ."\n";
228 print "Module: ". $action->ExecModule() ."\n";
230 my @princ = argument_to_list( $action );
234 my $obj = RT::Principal->new( $RT::SystemUser );
236 next unless $obj->id;
238 print "\t". $obj->PrincipalType;
239 print "\t=> ". $obj->Object->Name;
240 print "(Disabled!!!)" if $obj->Disabled;
247 =head2 create NAME [--comment] [--group GNAME] [--user UNAME]
249 Creates new action with NAME and adds users and/or groups to its
250 recipient list. Would be notify as comment if --comment specified.
255 my $actions = RT::ScripActions->new( $RT::SystemUser );
258 VALUE => $opts->{'name'},
260 if ( $actions->Count ) {
261 print STDERR "ScripAction '". $opts->{'name'} ."' allready exists\n";
265 my @groups = _check_groups( @{ $opts->{'groups'} } );
266 my @users = _check_users( @{ $opts->{'users'} } );
267 unless ( @users + @groups ) {
268 print STDERR "List of groups and users is empty\n";
272 my $action = __create_empty( $opts->{'name'}, $opts->{'comment'} );
274 __add( $action, $_ ) foreach( @users );
275 __add( $action, $_ ) foreach( @groups );
282 my $as_comment = shift || 0;
283 require RT::ScripAction;
284 my $action = RT::ScripAction->new( $RT::SystemUser );
287 Description => "Created with rt-email-group-admin script",
288 ExecModule => $as_comment? 'NotifyGroupAsComment': 'NotifyGroup',
297 return grep { $_ ? 1: do { print STDERR "Group '$_' skipped, doesn't exist\n"; 0; } }
298 map { __check_group($_) } @_;
303 my $instance = shift;
305 my $obj = RT::Group->new( $RT::SystemUser );
306 $obj->LoadUserDefinedGroup( $instance );
307 return $obj->id ? $obj : undef;
312 return grep { $_ ? 1: do { print STDERR "User '$_' skipped, doesn't exist\n"; 0; } }
313 map { __check_user($_) } @_;
318 my $instance = shift;
320 my $obj = RT::User->new( $RT::SystemUser );
321 $obj->Load( $instance );
322 return $obj->id ? $obj : undef;
325 =head2 add NAME [--group GNAME] [--user UNAME]
327 Adds groups and/or users to recipients of the action NAME.
332 my $action = _get_action_by_name( $opts->{'name'} );
334 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
338 my @groups = _check_groups( @{ $opts->{'groups'} } );
339 my @users = _check_users( @{ $opts->{'users'} } );
341 unless ( @users + @groups ) {
342 print STDERR "List of groups and users is empty\n";
346 __add( $action, $_ ) foreach @users;
347 __add( $action, $_ ) foreach @groups;
357 my @cur = argument_to_list( $action );
360 return if grep $_ == $id, @cur;
364 return $action->__Set( Field => 'Argument', Value => join(',', @cur) );
369 Deletes action NAME if scrips doesn't use it.
374 my $action = _get_action_by_name( $opts->{'name'} );
376 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
381 my $scrips = RT::Scrips->new( $RT::SystemUser );
382 $scrips->Limit( FIELD => 'ScripAction', VALUE => $action->id );
383 if ( $scrips->Count ) {
385 while( my $s = $scrips->Next ) {
388 print STDERR "ScripAction '". $opts->{'name'} ."'"
389 . " is in use by Scrip(s) ". join( ", ", map "#$_", @sid )
394 return __delete( $action );
398 require DBIx::SearchBuilder::Record;
399 return DBIx::SearchBuilder::Record::Delete( shift );
402 sub _get_action_by_name {
404 my $actions = _get_our_actions();
410 if ( $actions->Count > 1 ) {
411 print STDERR "More then one ScripAction with name '$name'\n";
414 return $actions->First;
419 Switch action NAME from notify as correspondence to comment and back.
424 my $action = _get_action_by_name( $opts->{'name'} );
426 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
431 NotifyGroup => 'NotifyGroupAsComment',
432 NotifyGroupAsComment => 'NotifyGroup'
435 return $action->__Set(
436 Field => 'ExecModule',
437 Value => $h{ $action->ExecModule }
441 =head2 rename NAME --newname NEWNAME
443 Renames action NAME to NEWNAME.
448 my $action = _get_action_by_name( $opts->{'name'} );
450 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
454 my $actions = RT::ScripActions->new( $RT::SystemUser );
455 $actions->Limit( FIELD => 'Name', VALUE => $opts->{'newname'} );
456 if ( $actions->Count ) {
457 print STDERR "ScripAction '". $opts->{'newname'} ."' allready exists\n";
461 return $action->__Set(
463 Value => $opts->{'newname'},
469 If command has option --group or --user then you can use it more then once,
470 if other is not specified.
478 sub argument_to_list {
480 require RT::Action::NotifyGroup;
481 return RT::Action::NotifyGroup->__SplitArg( $action->Argument );
484 sub _get_our_actions {
485 my $actions = RT::ScripActions->new( $RT::SystemUser );
487 FIELD => 'ExecModule',
488 VALUE => 'NotifyGroup',
489 ENTRYAGGREGATOR => 'OR',
492 FIELD => 'ExecModule',
493 VALUE => 'NotifyGroupAsComment',
494 ENTRYAGGREGATOR => 'OR',
502 Ruslan U. Zakirov E<lt>ruz@bestpractical.comE<gt>
506 L<RT::Action::NotifyGroup>, L<RT::Action::NotifyGroupAsComment>