WebEditor/OldFeatures/Notify.pm untested

filter: Statementstmt 0.0% 0 / 150 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 257 CC 31 SCAR 48.7
CC
31
Cov
0%
notify
210.0
do_notify
182.0
resolve_notify_receivers
42.0
CRAP 129.7
1 # -*- perl -*-
2  
3 #
4 # $Id: Notify.pm,v 1.15 2005/01/10 08:29:44 eserte Exp $
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2004 Slaven Rezic. All rights reserved.
8 # This package is free software; you can redistribute it and/or
9 # modify it under the same terms as Perl itself.
10 #
11 # Mail: slaven@rezic.de
12 # WWW: http://www.rezic.de/eserte/
13 #
14  
15 package WebEditor::OldFeatures::Notify;
16  
17 use strict;
18 use vars qw($VERSION);
19 $VERSION = sprintf("%d.%02d", q$Revision: 1.15 $ =~ /(\d+)\.(\d+)/);
20  
21 use mixin::with 'WebEditor::OldController';
22  
23 use Mail::Send;
24 use Mail::Mailer 1.53;
25 use Data::Dumper;
26 use WE::Util::LangString qw(langstring);
27 use Sys::Hostname qw(hostname);
28  
29 # 0: no debugging
30 # 1: show notify calls
31 # 2: trace SMTP, show args to Mail::Send
32 use constant DEBUG => 0;
33  
34 sub notify {
35 my($self, $action, $info, $retinfo) = @_;
36 warn "Notify $action $info...\n" if DEBUG;
37  
38 my $c = $self->C;
39 my $f = $c->project->features;
40 my $rcvs = $f && $f->{notify} && $f->{notify}{$action};
41 if (!$rcvs) {
42 $rcvs = [];
43 } else {
44 if (!UNIVERSAL::isa($rcvs, "ARRAY")) {
45 $rcvs = [split /\s*,\s*/, $rcvs];
46 }
47 }
48 my $all = $f->{notify}{all};
49 if ($all) {
50 if (UNIVERSAL::isa($all, "ARRAY")) {
51 push @$rcvs, @$all;
52 } else {
53 $all = [split /\s*,\s*/, $all];
54 push @$rcvs, @$all;
55 }
56 }
57 warn "Found notify receivers: @$rcvs\n" if DEBUG;
58  
59 if (@$rcvs) {
60 if ($c->siteext && $c->siteext->notify_background) {
61 if (fork == 0) {
62 $self->do_notify($action,
63 -info => $info,
64 -receivers => $rcvs,
65 );
66 CORE::exit(0);
67 }
68 if ($retinfo) {
69 # XXX langres
70 $retinfo->{message} = "Notify process started.";
71 }
72 } else {
73 eval {
74 my $_retinfo =
75 $self->do_notify($action,
76 -info => $info,
77 -receivers => $rcvs,
78 );
79 if ($retinfo) {
80 $retinfo->{receivers} = $_retinfo->{receivers}
81 }
82 };
83 if ($@) {
84 if ($retinfo) {
85 $retinfo->{message} = $@;
86 }
87 warn $@;
88 }
89 }
90 }
91 }
92  
93 sub do_notify {
94 my $self = shift;
95 my $action = shift;
96 my %args = @_;
97 my $retinfo = {};
98 my $info = delete $args{-info};
99 my $receivers = delete $args{-receivers};
100 die "Unknown arguments: " . join(", ", %args) if %args;
101 if (!$receivers) {
102 die "No receivers";
103 }
104 $receivers = $self->resolve_notify_receivers($receivers);
105 if (!@$receivers) {
106 warn "Resolved to null receivers";
107 $retinfo->{receivers} = [];
108 return $retinfo;
109 }
110  
111 my $root = $self->Root;
112 my $objdb = $root->ObjDB;
113 my $c = $self->C;
114 my $user = $root->CurrentUser || "unknown";
115  
116 my $subject = "web.editor system message: $action (site @{[ $c->project->longname ]})";
117  
118 my @mailerargs;
119 if ($c->siteext && $c->siteext->notify_mailer) {
120 @mailerargs = @{ $c->siteext->notify_mailer };
121 }
122 if (@mailerargs && $mailerargs[0] eq 'smtp' && DEBUG >= 2) {
123 push @mailerargs, Debug => 10;
124 }
125  
126 my $msg = new Mail::Send Subject => $subject;
127 for my $receiver (@$receivers) {
128 $msg->add("To", $receiver);
129 }
130 $msg->add("MIME-Version", "1.0");
131 $msg->add("Content-Type", "text/html; charset=ISO-8859-1");
132 # $msg->add("Content-Transfer-Enconding", "8bit");
133 # XXX Verwendung von Templates?
134 # XXX de vs. en?
135 # XXX Besserer Text
136 my $fh = $msg->open(@mailerargs);
137 if (DEBUG >= 2) {
138 require Data::Dumper;
139 print STDERR "Line " . __LINE__ . ", File: " . __FILE__ . "\n" . Data::Dumper->new([\@mailerargs],[])->Indent(1)->Useqq(1)->Dump;
140 }
141  
142 print $fh <<EOF;
143 <x-html>
144 The action <b>`$action'</b> was performed by <b>`$user'</b> <br>
145 in the web.editor system for the site <b> @{[ $c->project->longname ]} </b> <br>
146 on host @{[ hostname() ]}.
147 EOF
148 if ($info) {
149 while(my($k,$v) = each %$info) {
150 if ($k eq 'Id') {
151 my @ids = UNIVERSAL::isa($v, "ARRAY") ? @$v : $v;
152 my @titles = map {
153 my $o = $objdb->get_object($_);
154 if ($o) {
155 langstring($o->Title);
156 } else {
157 "Object with Id $_";
158 }
159 } @ids;
160 if (@titles > 10) {
161 splice @titles, 10;
162 }
163 print $fh "\n<br>";
164 print $fh "Documents (@ids): ";
165 print $fh join(", ", map { WebEditor::OldController::_html($_) } @titles) . "\n<br>";
166 delete $info->{$k};
167 }
168 }
169 if (keys %$info) {
170 print $fh "<br>\nAdditional information:\n<br>";
171 print $fh Dumper($info);
172 }
173 }
174 print $fh "</x-html>";
175 $fh->close;
176  
177 $retinfo->{receivers} = $receivers;
178  
179 $retinfo;
180 }
181  
182 sub resolve_notify_receivers {
183 my($self, $receivers) = @_;
184 my $userdb = $self->Root->UserDB;
185 my @new_receivers;
186  
187 my $add_user = sub {
188 my $username = shift;
189 my $user = $userdb->get_user($username);
190 if ($user) {
191 if ($user->{email}) {
192 push @new_receivers, $user->{email};
193 } else {
194 warn "The notify user <" . $username . "> has no email address!";
195 }
196 } else {
197 warn "The username $username is unknown";
198 }
199 };
200  
201 for my $receiver (@$receivers) {
202 TRY: {
203 if ($receiver =~ /\@/) {
204 push @new_receivers, $receiver;
205 last TRY;
206 }
207  
208 if ($userdb->user_exists($receiver)) {
209 $add_user->($receiver);
210 last TRY;
211 }
212  
213 my @users = $userdb->get_users_of_group($receiver);
214 if (@users) {
215 $add_user->($_) for (@users);
216 } else {
217 warn "The nofify user $receiver is neither an email address, a user nor a group";
218 }
219 }
220 }
221 \@new_receivers;
222 }
223  
224 1;
225  
226 __END__