-
Notifications
You must be signed in to change notification settings - Fork 4
/
ActionNotify.pm
514 lines (429 loc) · 16.3 KB
/
ActionNotify.pm
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
# See bottom of file for copyright and license information
# This module contains the functionality of the bin/actionnotify script
package Foswiki::Plugins::ActionTrackerPlugin::ActionNotify;
use strict;
use integer;
# Added by NKO to fix problem with non danish names
use locale;
use Time::ParseDate ();
use Foswiki::Net ();
use Foswiki::Attrs ();
use Foswiki::Plugins::ActionTrackerPlugin ();
my $wikiWordRE;
my $options;
# PUBLIC actionnotify script entry point. Reinitialises Foswiki.
#
# Notify all persons of actions that match the search expression
# passed.
#
sub actionNotify {
my $expr = shift;
my $session = new Foswiki();
# Assign SESSION so that Func methods work
$Foswiki::Plugins::SESSION = $session;
if ($expr =~ /\bweb="([^" ,*]+)"/) {
Foswiki::Func::pushTopicContext($1, 'WebHome');
}
Foswiki::Plugins::ActionTrackerPlugin::lazyInit($session->{webName}, $session->{topicName});
if ( $expr =~ s/DEBUG//o ) {
print doNotifications( $session->{webName}, $expr, 1 ), "\n";
}
else {
doNotifications( $session->{webName}, $expr, 0 );
}
}
# Entry point separated from main entry point, because we may want
# to call it in a topic without re-initialising.
sub doNotifications {
my ( $webName, $expr, $debugMailer ) = @_;
$options = Foswiki::Plugins::ActionTrackerPlugin::Options::load();
# Disable the state shortcut in mails
$options->{ENABLESTATESHORTCUT} = 0;
my $attrs = new Foswiki::Attrs( $expr, 1 );
my $hdr = $attrs->remove('header') || $options->{TABLEHEADER};
my $bdy = $attrs->remove('format') || $options->{TABLEFORMAT};
my $orient = $options->{TABLEORIENT};
my $textform = $options->{TEXTFORMAT};
my $changes = $options->{NOTIFYCHANGES};
my $format =
new Foswiki::Plugins::ActionTrackerPlugin::Format( $hdr, $bdy, $orient,
$textform, $changes );
my $result = '';
my $webs = $attrs->remove('web') || '.*';
my $topics = $attrs->remove('topic') || '.*';
# Okay, we have tables of all the actions and a partial set of the
# people who can be notified.
my %notifications = ();
my %people = ();
my $date = $attrs->remove('changedsince');
if ( defined($date) ) {
# need to get rid of formatting done in actionnotify perl script
$date =~ s/[, ]+/ /go;
$date = _getRelativeDate($date);
_findChangesInWebs( $webs, $topics, $date, $format, \%notifications );
foreach my $key ( keys %notifications ) {
if ( defined( $notifications{$key} ) ) {
$people{$key} = 1;
}
}
}
my $actions;
if ( !$attrs->isEmpty() ) {
# Get all the actions that match the search
$actions =
Foswiki::Plugins::ActionTrackerPlugin::ActionSet::allActionsInWebs(
$webs, $attrs, 1 );
$actions->getActionees( \%people );
}
# Resolve all mail addresses
my $mailAddress = {};
my $unsatisfied = 0;
foreach my $key ( keys %people ) {
if ( !defined( _getMailAddress( $key, $mailAddress ) ) ) {
$unsatisfied = 1;
}
}
# If we could not find everyone, gather up WebNotifys as well.
_loadWebNotifies($mailAddress) if ($unsatisfied);
# Now cycle over the list of people and find their sets of actions
# or changes. When we find actions or changes for someone then
# combine them and add them to the notifications for each indicated
# mail address.
my %actionsPerEmail;
my %changesPerEmail;
my %notifyEmail;
foreach my $wikiname ( keys %people ) {
# first expand the mail address(es)
my $mailaddr = _getMailAddress( $wikiname, $mailAddress );
if ( !defined($mailaddr) ) {
Foswiki::Func::writeWarning("No mail address found for $wikiname");
$result .= "No mail address found for $wikiname<br />"
if ($debugMailer);
next;
}
# find all the actions for this wikiname
my $myActions;
if ($actions) {
my $ats = new Foswiki::Attrs( "who=\"$wikiname\"", 1 );
$myActions = $actions->search($ats);
}
# now add these to the lists for each mail address
foreach my $email ( split( /,\s*/, $mailaddr ) ) {
if ($myActions) {
if ( !defined( $actionsPerEmail{$email} ) ) {
$actionsPerEmail{$email} =
new Foswiki::Plugins::ActionTrackerPlugin::ActionSet();
}
$actionsPerEmail{$email}->concat($myActions);
$notifyEmail{$email} = 1;
}
if ( $notifications{$wikiname} ) {
if ( !defined( $changesPerEmail{$email} ) ) {
$changesPerEmail{$email}{text} = '';
$changesPerEmail{$email}{html} = '';
}
$changesPerEmail{$email}{text} .=
$notifications{$wikiname}{text};
$changesPerEmail{$email}{html} .=
$notifications{$wikiname}{html};
$notifyEmail{$email} = 1;
}
}
}
# Finally send out the messages
foreach my $email ( keys %notifyEmail ) {
my $actionsString = '';
my $actionsHTML = '';
my $changesString = '';
my $changesHTML = '';
if ( $actionsPerEmail{$email} ) {
# sorted by due date
$actionsPerEmail{$email}->sort();
$actionsString = $actionsPerEmail{$email}->formatAsString($format);
$actionsHTML =
$actionsPerEmail{$email}
->formatAsHTML( $format, 'href', 'atpChanges' );
}
if ( $changesPerEmail{$email} ) {
$changesString = $changesPerEmail{$email}{text};
$changesHTML = $changesPerEmail{$email}{html};
}
if ( $actionsString || $changesString ) {
my $message =
_composeActionsMail( $actionsString, $actionsHTML, $changesString,
$changesHTML, $date, $email, $format );
# COVERAGE OFF debug only
if ($debugMailer) {
$result .= $message;
}
else {
my $error = Foswiki::Func::sendEmail($message);
if ($error) {
$error = "ActionTrackerPlugin:ActionNotify: $error";
Foswiki::Func::writeWarning($error);
}
}
}
# COVERAGE ON
}
return $result;
}
# PRIVATE Process all known webs to get the list of notifiable people
sub _loadWebNotifies {
my ($mailAddress) = @_;
foreach my $web ( Foswiki::Func::getListOfWebs('user') ) {
_loadWebNotify( $web, $mailAddress );
}
}
# PRIVATE Get the actions that match attrs, and the contents
# of WebNotify, for a web
sub _loadWebNotify {
my ( $web, $mailAddress ) = @_;
# COVERAGE OFF safety net
if ( !Foswiki::Func::webExists($web) ) {
my $error = 'ActionTrackerPlugin:ActionNotify: did not find web $web';
Foswiki::Func::writeWarning($error);
return;
}
# COVERAGE ON
my $topicname = $Foswiki::cfg{NotifyTopicName};
return undef unless Foswiki::Func::topicExists( $web, $topicname );
my $list = {};
my $mainweb = Foswiki::Func::getMainWebname();
my $text = Foswiki::Func::readTopicText( $web, $topicname, undef, 1 );
foreach my $line ( split( /\r?\n/, $text ) ) {
if ( $line =~ /^\s+\*\s([\w\.]+)\s+-\s+([\w\-\.\+]+\@[\w\-\.\+]+)/o ) {
my $who = $1;
my $addr = $2;
$who =
Foswiki::Plugins::ActionTrackerPlugin::Action::_canonicalName(
$who);
if ( !defined( $mailAddress->{$who} ) ) {
Foswiki::Func::writeWarning(
'ActionTrackerPlugin:ActionNotify: mail address for $who found in WebNotify'
);
$mailAddress->{$who} = $addr;
}
}
}
}
# PRIVATE Try to get the mail address of a wikiName by looking up in the
# map of known addresses or, failing that, by opening their
# personal topic in the Main web and looking for Email:
sub _getMailAddress {
my ( $who, $mailAddress ) = @_;
if ( defined( $mailAddress->{$who} ) ) {
return $mailAddress->{$who};
}
my $addresses;
my $wikiWordRE = Foswiki::Func::getRegularExpression('wikiWordRegex');
my $webNameRE = Foswiki::Func::getRegularExpression('webNameRegex');
if ( $who =~ m/^([\w\-\.\+]+\@[\w\-\.\+]+)$/o ) {
# Valid mail address
$addresses = $who;
}
elsif ( $who =~ m/,\s*/o ) {
# Multiple addresses
# (e.g. who="GenghisKhan,AttillaTheHun")
# split on , and recursively expand
my @persons = split( /\s*,\s*/, $who );
foreach my $person (@persons) {
$person = _getMailAddress( $person, $mailAddress );
}
$addresses = join( ',', @persons );
# Replaced by NKO, so that danish names accepted ...
#damn its hard to be Danish
# } elsif ( $who =~ m/^[A-Z]+[a-z]+[A-Z]+\w+$/o ) {
}
elsif ( $who =~ m/^$wikiWordRE$/o ) {
# A legal topic wikiname
$who =
Foswiki::Plugins::ActionTrackerPlugin::Action::_canonicalName($who);
$addresses = _getMailAddress( $who, $mailAddress );
# Replaced by NKO
# } elsif ( $who =~ m/^(\w+)\.([A-Z]+[a-z]+[A-Z]+\w+)$/o ) {
}
elsif ( $who =~ m/^($webNameRE)\.($wikiWordRE)$/o ) {
my ( $inweb, $intopic ) = ( $1, $2 );
$addresses = join( ',', Foswiki::Func::wikinameToEmails($intopic) );
# LEGACY - Try and expand groups the old way
if ( !$addresses && Foswiki::Func::topicExists( $inweb, $intopic ) ) {
my $text =
Foswiki::Func::readTopicText( $inweb, $intopic, undef, 1 );
if ( $intopic =~ m/Group$/o ) {
# If it's a Group topic, match * Set GROUP =
if ( $text =~ m/^\s+\*\s+Set\s+GROUP\s*=\s*([^\r\n]+)/mo ) {
my @people = split( /\s*,\s*/, $1 );
foreach my $person (@people) {
$person = _getMailAddress( $person, $mailAddress );
}
$addresses = join( ',', @people );
}
}
}
}
if ( defined($addresses) ) {
if ( $addresses =~ m/^\s*$/o ) {
$addresses = undef;
}
else {
$mailAddress->{$who} = $addresses;
}
}
return $addresses;
}
# PRIVATE Mail the contents of the action set to the given user(s)
sub _composeActionsMail {
my ( $actionsString, $actionsHTML, $changesString, $changesHTML, $since,
$mailaddr, $format )
= @_;
my $from =
$Foswiki::cfg{WebMasterEmail}
|| Foswiki::Func::getPreferencesValue('WIKIWEBMASTER')
|| '';
my $text = Foswiki::Func::readTemplate('actionnotify') || <<'HERE';
From: %EMAILFROM%
To: %EMAILTO%
Subject: %SUBJECT% on %WIKITOOLNAME%
MIME-Version: 1.0
Content-Type: text/plain
ERROR: No actionnotify template installed - please inform %WIKIWEBMASTER%
HERE
my $subject = '';
if ($actionsString) {
$subject .= 'Outstanding actions';
}
if ($changesString) {
$subject .= ' and ' if ( $subject ne '' );
$subject .= 'Changes to actions';
}
$text =~ s/%SUBJECT%/$subject/go;
$text =~ s/%EMAILFROM%/$from/go;
$text =~ s/%EMAILTO%/$mailaddr/go;
if ( $actionsString ne '' ) {
$text =~ s/%ACTIONS_AS_STRING%/$actionsString/go;
my $asHTML = Foswiki::Func::renderText($actionsHTML);
$text =~ s/%ACTIONS_AS_HTML%/$asHTML/go;
$text =~ s/%ACTIONS%(.*?)%END%/$1/gso;
}
else {
$text =~ s/%ACTIONS%.*?%END%//gso;
}
if ($since) {
$since = Foswiki::Func::formatTime($since);
}
else {
$since = '';
}
$text =~ s/%SINCE%/$since/go;
if ( $changesString ne '' ) {
$text =~ s/%CHANGES_AS_STRING%/$changesString/go;
my $asHTML = Foswiki::Func::renderText($changesHTML);
$text =~ s/%CHANGES_AS_HTML%/$asHTML/go;
$text =~ s/%CHANGES%(.*?)%END%/$1/gso;
}
else {
$text =~ s/%CHANGES%.*?%END%//gso;
}
$text =
Foswiki::Func::expandCommonVariables( $text,
$Foswiki::cfg{HomeTopicName} );
$text =~ s/<img src=.*?[^>]>/[IMG]/goi; # remove all images
# add the url host to any internal urls that lack it
# SMELL: doesn't handle (undocumented) {ScriptUrlPaths}
my $sup = $Foswiki::cfg{ScriptUrlPath};
$sup =~ s#/$##;
my $sun = Foswiki::Func::getUrlHost() . $sup;
$text =~ s#href=\"$sup/#href=\"$sun/#ogi;
$text =~ s/<\/?nop( \/)?>//goi;
return $text;
}
# PRIVATE STATIC get the 'real' date from a relative date.
sub _getRelativeDate {
my $ago = shift;
my $triggerTime = Time::ParseDate::parsedate( $ago, PREFER_PAST => 1 );
return $triggerTime;
}
# PRIVATE STATIC
# Find the actions that have changed between today and a previous date
# in the given web and topic
sub _findChangesInTopic {
my ( $theWeb, $theTopic, $theDate, $format, $notifications ) = @_;
# Recover the rev at the previous date
my $oldrev =
Foswiki::Func::getRevisionAtTime( $theWeb, $theTopic, $theDate );
return unless defined($oldrev);
$oldrev =~ s/\d+\.(\d+)/$1/o;
# Recover the action set at that date
my $text = Foswiki::Func::readTopicText( $theWeb, $theTopic, $oldrev, 1 );
my $oldActions =
Foswiki::Plugins::ActionTrackerPlugin::ActionSet::load( $theWeb,
$theTopic, $text );
# Recover the current action set.
$text = Foswiki::Func::readTopicText( $theWeb, $theTopic, undef, 1 );
my $currentActions =
Foswiki::Plugins::ActionTrackerPlugin::ActionSet::load( $theWeb,
$theTopic, $text );
# find actions that have changed between the two dates. These
# are added as text to a hash keyed on the names of people
# interested in notification of that action.
$currentActions->findChanges( $oldActions, $theDate, $format,
$notifications );
}
# Gather all notifications for modifications in all topics in the
# given web, since the given date.
# $theDate is a string, not an integer
sub _findChangesInWeb {
my ( $web, $topics, $theDate, $format, $notifications ) = @_;
my $actions = new Foswiki::Plugins::ActionTrackerPlugin::ActionSet();
my @tops = Foswiki::Func::getTopicList($web);
my $grep = Foswiki::Func::searchInWebContent(
'%ACTION{.*}%',
$web,
\@tops,
{
type => 'regex',
casesensitive => 1,
files_without_match => 1
}
);
if( ref( $grep ) eq 'HASH' ) {
foreach my $topic ( keys %$grep ) {
_findChangesInTopic( $web, $topic, $theDate, $format, $notifications );
}
}
else {
while( $grep->hasNext() ) {
my $webtopic = $grep->next();
my ($foundWeb, $topic) = Foswiki::Func::normalizeWebTopicName($web, $webtopic);
_findChangesInTopic( $web, $topic, $theDate, $format, $notifications );
}
}
}
# PRIVATE STATIC
# Gather notifications for modifications in all webs matched in
# the "web" value of the attribute set. This searches all webs,
# INCLUDING those flagged NOSEARCHALL, on the assumption that
# people registering for notifications in those webs really want
# to know.
# $date is a string, not an integer
sub _findChangesInWebs {
my ( $webs, $topics, $date, $format, $notifications ) = @_;
my @weblist = grep { /^$webs$/ } Foswiki::Func::getListOfWebs('user');
foreach my $web (@weblist) {
_findChangesInWeb( $web, $topics, $date, $format, $notifications );
}
}
1;
__END__
Copyright (C) Motorola 2002 - All rights reserved
Copyright (C) 2004-2011 Crawford Currie http://c-dot.co.uk
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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, published at
http://www.gnu.org/copyleft/gpl.html