This commit was generated by cvs2svn to compensate for changes in r8690,
[freeside.git] / rt / lib / RT / Action / NotifyGroup.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4
5 # This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 #                                          <jesse@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
77 =cut
78
79 sub SetRecipients {
80     my $self = shift;
81
82     my $arg = $self->Argument;
83     foreach( $self->__SplitArg( $arg ) ) {
84         $self->_HandleArgument( $_ );
85     }
86
87     my $creator = $self->TransactionObj->CreatorObj->EmailAddress();
88     unless( $RT::NotifyActor ) {
89         @{ $self->{'To'} } = grep ( !/^\Q$creator\E$/, @{ $self->{'To'} } );
90     }
91
92     $self->{'seen_ueas'} = {};
93
94     return 1;
95 }
96
97 sub _HandleArgument {
98     my $self = shift;
99     my $instance = shift;
100
101     if ( $instance !~ /\D/ ) {
102         my $obj = RT::Principal->new( $self->CurrentUser );
103         $obj->Load( $instance );
104         return $self->_HandlePrincipal( $obj );
105     }
106
107     my $group = RT::Group->new( $self->CurrentUser );
108     $group->LoadUserDefinedGroup( $instance );
109     # to check disabled and so on
110     return $self->_HandlePrincipal( $group->PrincipalObj )
111         if $group->id;
112
113     require Email::Address;
114
115     my $user = RT::User->new( $self->CurrentUser );
116     if ( $instance =~ /^$Email::Address::addr_spec$/ ) {
117         $user->LoadByEmail( $instance );
118         return $self->__PushUserAddress( $instance )
119             unless $user->id;
120     } else {
121         $user->Load( $instance );
122     }
123     return $self->_HandlePrincipal( $user->PrincipalObj )
124         if $user->id;
125
126     $RT::Logger->error(
127         "'$instance' is not principal id, group name, user name,"
128         ." user email address or any email address"
129     );
130
131     return;
132 }
133
134 sub _HandlePrincipal {
135     my $self = shift;
136     my $obj = shift;
137     unless( $obj->id ) {
138         $RT::Logger->error( "Couldn't load principal #$obj" );
139         return;
140     }
141     if( $obj->Disabled ) {
142         $RT::Logger->info( "Principal #$obj is disabled => skip" );
143         return;
144     }
145     if( !$obj->PrincipalType ) {
146         $RT::Logger->crit( "Principal #$obj has empty type" );
147     } elsif( lc $obj->PrincipalType eq 'user' ) {
148         $self->__HandleUserArgument( $obj->Object );
149     } elsif( lc $obj->PrincipalType eq 'group' ) {
150         $self->__HandleGroupArgument( $obj->Object );
151     } else {
152         $RT::Logger->info( "Principal #$obj has unsupported type" );
153     }
154     return;
155 }
156
157 sub __HandleUserArgument {
158     my $self = shift;
159     my $obj = shift;
160     
161     my $uea = $obj->EmailAddress;
162     unless( $uea ) {
163         $RT::Logger->warning( "User #". $obj->id ." has no email address" );
164         return;
165     }
166     $self->__PushUserAddress( $uea );
167 }
168
169 sub __HandleGroupArgument {
170     my $self = shift;
171     my $obj = shift;
172
173     my $members = $obj->UserMembersObj;
174     while( my $m = $members->Next ) {
175         $self->__HandleUserArgument( $m );
176     }
177 }
178
179 sub __SplitArg {
180     return grep length, map {s/^\s+//; s/\s+$//; $_} split /,/, $_[1];
181 }
182
183 sub __PushUserAddress {
184     my $self = shift;
185     my $uea = shift;
186     push @{ $self->{'To'} }, $uea unless $self->{'seen_ueas'}{ $uea }++;
187     return;
188 }
189
190
191 =head1 AUTHOR
192
193 Ruslan U. Zakirov E<lt>ruz@bestpractical.comE<gt>
194
195 L<RT::Action::NotifyGroupAsComment>, F<rt-email-group-admin>
196
197 =cut
198
199 eval "require RT::Action::NotifyGroup_Vendor";
200 if ($@ && $@ !~ qr{^Can't locate RT/Action/NotifyGroup_Vendor.pm}) {
201     die $@;
202 };
203
204 eval "require RT::Action::NotifyGroup_Local";
205 if ($@ && $@ !~ qr{^Can't locate RT/Action/NotifyGroup_Local.pm}) {
206     die $@;
207 };
208
209 1;