Upgrade to 4.2.8
[usit-rt.git] / lib / RT / Action / NotifyGroup.pm
CommitLineData
84fb5b46
MKG
1# BEGIN BPS TAGGED BLOCK {{{
2#
3# COPYRIGHT:
4#
3ffc5f4f 5# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
84fb5b46
MKG
6# <sales@bestpractical.com>
7#
8# (Except where explicitly superseded by other copyright notices)
9#
10#
11# LICENSE:
12#
13# This work is made available to you under the terms of Version 2 of
14# the GNU General Public License. A copy of that license should have
15# been provided with this software, but in any event can be snarfed
16# from www.gnu.org.
17#
18# This work is distributed in the hope that it will be useful, but
19# WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21# General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program; if not, write to the Free Software
25# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26# 02110-1301 or visit their web page on the internet at
27# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28#
29#
30# CONTRIBUTION SUBMISSION POLICY:
31#
32# (The following paragraph is not intended to limit the rights granted
33# to you to modify and distribute this software under the terms of
34# the GNU General Public License and is only of importance to you if
35# you choose to contribute your changes and enhancements to the
36# community by submitting them to Best Practical Solutions, LLC.)
37#
38# By intentionally submitting any modifications, corrections or
39# derivatives to this work, or any other work intended for use with
40# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41# you are the copyright holder for those contributions and you grant
42# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43# royalty-free, perpetual, license to use, copy, create derivative
44# works based on those contributions, and sublicense and distribute
45# those contributions and any derivatives thereof.
46#
47# END BPS TAGGED BLOCK }}}
48
49=head1 NAME
50
51RT::Action::NotifyGroup - RT Action that sends notifications to groups and/or users
52
53=head1 DESCRIPTION
54
55RT action module that allow you to notify particular groups and/or users.
56Distribution is shipped with C<rt-email-group-admin> script that
57is command line tool for managing NotifyGroup scrip actions. For more
58more info see its documentation.
59
60=cut
61
62package RT::Action::NotifyGroup;
63
64use strict;
65use warnings;
66use base qw(RT::Action::Notify);
67
68require RT::User;
69require RT::Group;
70
71=head1 METHODS
72
73=head2 SetRecipients
74
75Sets the recipients of this message to Groups and/or Users.
76
77=cut
78
79sub SetRecipients {
80 my $self = shift;
81
82 my $arg = $self->Argument;
83 foreach( $self->__SplitArg( $arg ) ) {
84 $self->_HandleArgument( $_ );
85 }
86
87 my $creatorObj = $self->TransactionObj->CreatorObj;
88 my $creator = $creatorObj->EmailAddress();
89
90 my $TransactionCurrentUser = RT::CurrentUser->new;
91 $TransactionCurrentUser->LoadByName($creatorObj->Name);
92
93 unless (RT->Config->Get('NotifyActor',$TransactionCurrentUser)) {
94 @{ $self->{'To'} } = grep ( !/^\Q$creator\E$/, @{ $self->{'To'} } );
95 }
96
97 $self->{'seen_ueas'} = {};
98
99 return 1;
100}
101
102sub _HandleArgument {
103 my $self = shift;
104 my $instance = shift;
105
106 if ( $instance !~ /\D/ ) {
107 my $obj = RT::Principal->new( $self->CurrentUser );
108 $obj->Load( $instance );
109 return $self->_HandlePrincipal( $obj );
110 }
111
112 my $group = RT::Group->new( $self->CurrentUser );
113 $group->LoadUserDefinedGroup( $instance );
114 # to check disabled and so on
115 return $self->_HandlePrincipal( $group->PrincipalObj )
116 if $group->id;
117
118 require Email::Address;
119
120 my $user = RT::User->new( $self->CurrentUser );
121 if ( $instance =~ /^$Email::Address::addr_spec$/ ) {
122 $user->LoadByEmail( $instance );
123 return $self->__PushUserAddress( $instance )
124 unless $user->id;
125 } else {
126 $user->Load( $instance );
127 }
128 return $self->_HandlePrincipal( $user->PrincipalObj )
129 if $user->id;
130
131 $RT::Logger->error(
132 "'$instance' is not principal id, group name, user name,"
133 ." user email address or any email address"
134 );
135
136 return;
137}
138
139sub _HandlePrincipal {
140 my $self = shift;
141 my $obj = shift;
142 unless( $obj->id ) {
143 $RT::Logger->error( "Couldn't load principal #$obj" );
144 return;
145 }
146 if( $obj->Disabled ) {
147 $RT::Logger->info( "Principal #$obj is disabled => skip" );
148 return;
149 }
150 if( !$obj->PrincipalType ) {
151 $RT::Logger->crit( "Principal #$obj has empty type" );
152 } elsif( lc $obj->PrincipalType eq 'user' ) {
153 $self->__HandleUserArgument( $obj->Object );
154 } elsif( lc $obj->PrincipalType eq 'group' ) {
155 $self->__HandleGroupArgument( $obj->Object );
156 } else {
157 $RT::Logger->info( "Principal #$obj has unsupported type" );
158 }
159 return;
160}
161
162sub __HandleUserArgument {
163 my $self = shift;
164 my $obj = shift;
165
166 my $uea = $obj->EmailAddress;
167 unless( $uea ) {
168 $RT::Logger->warning( "User #". $obj->id ." has no email address" );
169 return;
170 }
171 $self->__PushUserAddress( $uea );
172}
173
174sub __HandleGroupArgument {
175 my $self = shift;
176 my $obj = shift;
177
178 my $members = $obj->UserMembersObj;
179 while( my $m = $members->Next ) {
180 $self->__HandleUserArgument( $m );
181 }
182}
183
184sub __SplitArg {
185 return grep length, map {s/^\s+//; s/\s+$//; $_} split /,/, $_[1];
186}
187
188sub __PushUserAddress {
189 my $self = shift;
190 my $uea = shift;
191 push @{ $self->{'To'} }, $uea unless $self->{'seen_ueas'}{ $uea }++;
192 return;
193}
194
195
196=head1 AUTHOR
197
198Ruslan U. Zakirov E<lt>ruz@bestpractical.comE<gt>
199
200L<RT::Action::NotifyGroupAsComment>, F<rt-email-group-admin>
201
202=cut
203
204RT::Base->_ImportOverlays();
205
2061;