5646d7e34d53856e23ddbc76e1e73565eefdf722
[freeside.git] / rt / lib / RT / Action / NotifyGroup.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
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
51 RT::Action::NotifyGroup - RT Action that sends notifications to groups and/or users
52
53 =head1 DESCRIPTION
54
55 RT action module that allow you to notify particular groups and/or users.
56 Distribution is shipped with C<rt-email-group-admin> script that
57 is command line tool for managing NotifyGroup scrip actions. For more
58 more info see its documentation.
59
60 =cut
61
62 package RT::Action::NotifyGroup;
63
64 use strict;
65 use warnings;
66 use base qw(RT::Action::Notify);
67
68 require RT::User;
69 require RT::Group;
70
71 =head1 METHODS
72
73 =head2 SetRecipients
74
75 Sets the recipients of this message to Groups and/or Users.
76 Respects RT's NotifyActor configuration.
77
78 To send email to the selected receipients regardless of RT's NotifyActor
79 configuration, include AlwaysNotifyActor in the list of arguments.
80
81 =cut
82
83 sub SetRecipients {
84     my $self = shift;
85
86     my $arg = $self->Argument;
87     foreach( $self->__SplitArg( $arg ) ) {
88         $self->_HandleArgument( $_ );
89     }
90
91     $self->{'seen_ueas'} = {};
92
93     return 1;
94 }
95
96 sub _HandleArgument {
97     my $self = shift;
98     my $instance = shift;
99
100     return if ( $instance eq 'AlwaysNotifyActor' );
101
102     if ( $instance !~ /\D/ ) {
103         my $obj = RT::Principal->new( $self->CurrentUser );
104         $obj->Load( $instance );
105         return $self->_HandlePrincipal( $obj );
106     }
107
108     my $group = RT::Group->new( $self->CurrentUser );
109     $group->LoadUserDefinedGroup( $instance );
110     # to check disabled and so on
111     return $self->_HandlePrincipal( $group->PrincipalObj )
112         if $group->id;
113
114     require Email::Address;
115
116     my $user = RT::User->new( $self->CurrentUser );
117     if ( $instance =~ /^$Email::Address::addr_spec$/ ) {
118         $user->LoadByEmail( $instance );
119         return $self->__PushUserAddress( $instance )
120             unless $user->id;
121     } else {
122         $user->Load( $instance );
123     }
124     return $self->_HandlePrincipal( $user->PrincipalObj )
125         if $user->id;
126
127     $RT::Logger->error(
128         "'$instance' is not principal id, group name, user name,"
129         ." user email address or any email address"
130     );
131
132     return;
133 }
134
135 sub _HandlePrincipal {
136     my $self = shift;
137     my $obj = shift;
138     unless( $obj->id ) {
139         $RT::Logger->error( "Couldn't load principal #$obj" );
140         return;
141     }
142     if( $obj->Disabled ) {
143         $RT::Logger->info( "Principal #$obj is disabled => skip" );
144         return;
145     }
146     if( !$obj->PrincipalType ) {
147         $RT::Logger->crit( "Principal #$obj has empty type" );
148     } elsif( lc $obj->PrincipalType eq 'user' ) {
149         $self->__HandleUserArgument( $obj->Object );
150     } elsif( lc $obj->PrincipalType eq 'group' ) {
151         $self->__HandleGroupArgument( $obj->Object );
152     } else {
153         $RT::Logger->info( "Principal #$obj has unsupported type" );
154     }
155     return;
156 }
157
158 sub __HandleUserArgument {
159     my $self = shift;
160     my $obj = shift;
161     
162     my $uea = $obj->EmailAddress;
163     unless( $uea ) {
164         $RT::Logger->warning( "User #". $obj->id ." has no email address" );
165         return;
166     }
167     $self->__PushUserAddress( $uea );
168 }
169
170 sub __HandleGroupArgument {
171     my $self = shift;
172     my $obj = shift;
173
174     my $members = $obj->UserMembersObj;
175     while( my $m = $members->Next ) {
176         $self->__HandleUserArgument( $m );
177     }
178 }
179
180 sub __SplitArg {
181     return grep length, map {s/^\s+//; s/\s+$//; $_} split /,/, $_[1];
182 }
183
184 sub __PushUserAddress {
185     my $self = shift;
186     my $uea = shift;
187     push @{ $self->{'To'} }, $uea unless $self->{'seen_ueas'}{ $uea }++;
188     return;
189 }
190
191
192 =head1 AUTHOR
193
194 Ruslan U. Zakirov E<lt>ruz@bestpractical.comE<gt>
195
196 L<RT::Action::NotifyGroupAsComment>, F<rt-email-group-admin>
197
198 =cut
199
200 RT::Base->_ImportOverlays();
201
202 1;