/usr/sbin/rt-email-group-admin-4 is in request-tracker4 4.2.12-5.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | #!/usr/bin/perl
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
# <sales@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
#
#
# LICENSE:
#
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org.
#
# This work is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
#
#
# CONTRIBUTION SUBMISSION POLICY:
#
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
#
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
#
# END BPS TAGGED BLOCK }}}
=head1 NAME
rt-email-group-admin - Command line tool for administrating NotifyGroup actions
=head1 SYNOPSIS
rt-email-group-admin --list
rt-email-group-admin --create 'Notify foo team' --group Foo
rt-email-group-admin --create 'Notify foo team as comment' --comment --group Foo
rt-email-group-admin --create 'Notify group Foo and Bar' --group Foo --group Bar
rt-email-group-admin --create 'Notify user foo@bar.com' --user foo@bar.com
rt-email-group-admin --create 'Notify VIPs' --user vip1@bar.com
rt-email-group-admin --add 'Notify VIPs' --user vip2@bar.com --group vip1 --user vip3@foo.com
rt-email-group-admin --rename 'Notify VIPs' --newname 'Inform VIPs'
rt-email-group-admin --switch 'Notify VIPs'
rt-email-group-admin --delete 'Notify user foo@bar.com'
=head1 DESCRIPTION
This script list, create, modify or delete scrip actions in the RT DB. Once
you've created an action you can use it in a scrip.
For example you can create the following action using this script:
rt-email-group-admin --create 'Notify developers' --group 'Development Team'
Then you can add the followoing scrip to your Bugs queue:
Condition: On Create
Action: Notify developers
Template: Transaction
Stage: TransactionCreate
Your development team will be notified on every new ticket in the queue.
=cut
use warnings;
use strict;
# fix lib paths, some may be relative
BEGIN { # BEGIN RT CMD BOILERPLATE
require File::Spec;
require Cwd;
my @libs = ("/usr/share/request-tracker4/lib", "/usr/local/share/request-tracker4/lib");
my $bin_path;
for my $lib (@libs) {
unless ( File::Spec->file_name_is_absolute($lib) ) {
$bin_path ||= ( File::Spec->splitpath(Cwd::abs_path(__FILE__)) )[1];
$lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
}
unshift @INC, $lib;
}
}
use Getopt::Long qw(GetOptions);
Getopt::Long::Configure( "pass_through" );
our $cmd = 'usage';
our $opts = {};
sub parse_args {
my $tmp;
if ( GetOptions( 'list' => \$tmp ) && $tmp ) {
$cmd = 'list';
}
elsif ( GetOptions( 'create=s' => \$tmp ) && $tmp ) {
$cmd = 'create';
$opts->{'name'} = $tmp;
$opts->{'groups'} = [];
$opts->{'users'} = [];
GetOptions( 'comment' => \$opts->{'comment'} );
GetOptions( 'group:s@' => $opts->{'groups'} );
GetOptions( 'user:s@' => $opts->{'users'} );
unless ( @{ $opts->{'users'} } + @{ $opts->{'groups'} } ) {
usage();
exit(-1);
}
}
elsif ( GetOptions( 'add=s' => \$tmp ) && $tmp ) {
$cmd = 'add';
$opts->{'name'} = $tmp;
$opts->{'groups'} = [];
$opts->{'users'} = [];
GetOptions( 'group:s@' => $opts->{'groups'} );
GetOptions( 'user:s@' => $opts->{'users'} );
unless ( @{ $opts->{'users'} } + @{ $opts->{'groups'} } ) {
usage();
exit(-1);
}
}
elsif ( GetOptions( 'switch=s' => \$tmp ) && $tmp ) {
$cmd = 'switch';
$opts->{'name'} = $tmp;
}
elsif ( GetOptions( 'rename=s' => \$tmp ) && $tmp ) {
$cmd = 'rename';
$opts->{'name'} = $tmp;
GetOptions( 'newname=s' => \$opts->{'newname'} );
unless ( $opts->{'newname'} ) {
usage();
exit(-1);
}
}
elsif ( GetOptions( 'delete=s' => \$tmp ) && $tmp) {
$cmd = 'delete';
$opts->{'name'} = $tmp;
} else {
$cmd = 'usage';
}
return;
}
sub usage {
require Pod::Usage;
Pod::Usage::pod2usage({ verbose => 2 });
}
my $help;
if ( GetOptions( 'help|h' => \$help ) && $help ) {
usage();
exit;
}
parse_args();
require RT;
RT->LoadConfig;
RT->Init;
require RT::Principal;
require RT::User;
require RT::Group;
require RT::ScripActions;
{
eval "main::$cmd()";
if ( $@ ) {
print STDERR $@ ."\n";
}
}
exit(0);
=head1 USAGE
rt-email-group-admin --COMMAND ARGS
=head1 COMMANDS
=head2 list
Lists actions and its descriptions.
=cut
sub list {
my $actions = _get_our_actions();
while( my $a = $actions->Next ) {
_list( $a );
}
return;
}
sub _list {
my $action = shift;
print "Name: ". $action->Name() ."\n";
print "Module: ". $action->ExecModule() ."\n";
my @princ = argument_to_list( $action );
print "Members: \n";
foreach( @princ ) {
my $obj = RT::Principal->new( RT->SystemUser );
$obj->Load( $_ );
next unless $obj->id;
print "\t". $obj->PrincipalType;
print "\t=> ". $obj->Object->Name;
print "(Disabled!!!)" if $obj->Disabled;
print "\n";
}
print "\n";
return;
}
=head2 create NAME [--comment] [--group GNAME] [--user NAME-OR-EMAIL]
Creates new action with NAME and adds users and/or groups to its
recipient list. Would be notify as comment if --comment specified. The
user, if specified, will be autocreated if necessary.
=cut
sub create {
my $actions = RT::ScripActions->new( RT->SystemUser );
$actions->Limit(
FIELD => 'Name',
VALUE => $opts->{'name'},
);
if ( $actions->Count ) {
print STDERR "ScripAction '". $opts->{'name'} ."' allready exists\n";
exit(-1);
}
my @groups = _check_groups( @{ $opts->{'groups'} } );
my @users = _check_users( @{ $opts->{'users'} } );
unless ( @users + @groups ) {
print STDERR "List of groups and users is empty\n";
exit(-1);
}
my $action = __create_empty( $opts->{'name'}, $opts->{'comment'} );
__add( $action, $_ ) foreach( @users );
__add( $action, $_ ) foreach( @groups );
return;
}
sub __create_empty {
my $name = shift;
my $as_comment = shift || 0;
require RT::ScripAction;
my $action = RT::ScripAction->new( RT->SystemUser );
$action->Create(
Name => $name,
Description => "Created with rt-email-group-admin script",
ExecModule => $as_comment? 'NotifyGroupAsComment': 'NotifyGroup',
Argument => '',
);
return $action;
}
sub _check_groups
{
return map {$_->[1]}
grep { $_->[1] ? 1: do { print STDERR "Group '$_->[0]' skipped, doesn't exist\n"; 0; } }
map { [$_, __check_group($_)] } @_;
}
sub __check_group
{
my $instance = shift;
require RT::Group;
my $obj = RT::Group->new( RT->SystemUser );
$obj->LoadUserDefinedGroup( $instance );
return $obj->id ? $obj : undef;
}
sub _check_users
{
return map {$_->[1]}
grep { $_->[1] ? 1: do { print STDERR "User '$_->[0]' skipped, doesn't exist and couldn't autocreate\n"; 0; } }
map { [$_, __check_user($_)] } @_;
}
sub __check_user
{
my $instance = shift;
require RT::User;
my $obj = RT::User->new( RT->SystemUser );
$obj->Load( $instance );
$obj->LoadByEmail( $instance )
if not $obj->id and $instance =~ /@/;
unless ($obj->id) {
my ($ok, $msg) = $obj->Create(
Name => $instance,
EmailAddress => $instance,
Privileged => 0,
Comments => 'Autocreated when added to notify action via rt-email-group-admin',
);
print STDERR "Autocreate of user '$instance' failed: $msg\n"
unless $ok;
}
return $obj->id ? $obj : undef;
}
=head2 add NAME [--group GNAME] [--user NAME-OR-EMAIL]
Adds groups and/or users to recipients of the action NAME. The user, if
specified, will be autocreated if necessary.
=cut
sub add {
my $action = _get_action_by_name( $opts->{'name'} );
unless ( $action ) {
print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
exit(-1);
}
my @groups = _check_groups( @{ $opts->{'groups'} } );
my @users = _check_users( @{ $opts->{'users'} } );
unless ( @users + @groups ) {
print STDERR "List of groups and users is empty\n";
exit(-1);
}
__add( $action, $_ ) foreach @users;
__add( $action, $_ ) foreach @groups;
return;
}
sub __add
{
my $action = shift;
my $obj = shift;
my @cur = argument_to_list( $action );
my $id = $obj->id;
return if grep $_ == $id, @cur;
push @cur, $id;
return $action->__Set( Field => 'Argument', Value => join(',', @cur) );
}
=head2 delete NAME
Deletes action NAME if scrips doesn't use it.
=cut
sub delete {
my $action = _get_action_by_name( $opts->{'name'} );
unless ( $action ) {
print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
exit(-1);
}
require RT::Scrips;
my $scrips = RT::Scrips->new( RT->SystemUser );
$scrips->Limit( FIELD => 'ScripAction', VALUE => $action->id );
$scrips->FindAllRows;
if ( $scrips->Count ) {
my @sid;
while( my $s = $scrips->Next ) {
push @sid, $s->id;
}
print STDERR "ScripAction '". $opts->{'name'} ."'"
. " is in use by Scrip(s) ". join( ", ", map "#$_", @sid )
. "\n";
exit(-1);
}
return __delete( $action );
}
sub __delete {
require DBIx::SearchBuilder::Record;
return DBIx::SearchBuilder::Record::Delete( shift );
}
sub _get_action_by_name {
my $name = shift;
my $actions = _get_our_actions();
$actions->Limit(
FIELD => 'Name',
VALUE => $name
);
if ( $actions->Count > 1 ) {
print STDERR "More then one ScripAction with name '$name'\n";
}
return $actions->First;
}
=head2 switch NAME
Switch action NAME from notify as correspondence to comment and back.
=cut
sub switch {
my $action = _get_action_by_name( $opts->{'name'} );
unless ( $action ) {
print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
exit(-1);
}
my %h = (
NotifyGroup => 'NotifyGroupAsComment',
NotifyGroupAsComment => 'NotifyGroup'
);
return $action->__Set(
Field => 'ExecModule',
Value => $h{ $action->ExecModule }
);
}
=head2 rename NAME --newname NEWNAME
Renames action NAME to NEWNAME.
=cut
sub rename {
my $action = _get_action_by_name( $opts->{'name'} );
unless ( $action ) {
print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
exit(-1);
}
my $actions = RT::ScripActions->new( RT->SystemUser );
$actions->Limit( FIELD => 'Name', VALUE => $opts->{'newname'} );
if ( $actions->Count ) {
print STDERR "ScripAction '". $opts->{'newname'} ."' allready exists\n";
exit(-1);
}
return $action->__Set(
Field => 'Name',
Value => $opts->{'newname'},
);
}
=head2 NOTES
If command has option --group or --user then you can use it more then once,
if other is not specified.
=cut
###############
#### Utils ####
###############
sub argument_to_list {
my $action = shift;
require RT::Action::NotifyGroup;
return RT::Action::NotifyGroup->__SplitArg( $action->Argument );
}
sub _get_our_actions {
my $actions = RT::ScripActions->new( RT->SystemUser );
$actions->Limit(
FIELD => 'ExecModule',
VALUE => 'NotifyGroup',
ENTRYAGGREGATOR => 'OR',
);
$actions->Limit(
FIELD => 'ExecModule',
VALUE => 'NotifyGroupAsComment',
ENTRYAGGREGATOR => 'OR',
);
return $actions;
}
=head1 AUTHOR
Ruslan U. Zakirov E<lt>ruz@bestpractical.comE<gt>
=head1 SEE ALSO
L<RT::Action::NotifyGroup>, L<RT::Action::NotifyGroupAsComment>
=cut
|