Skip to content

Commit

Permalink
Item14378: More fixes to the offline mailnotify
Browse files Browse the repository at this point in the history
  - Restrict notification runs to administrators.
  - Add back in code to save the most recent timestamp.
  - Document the rest handler, and add quiet, and dry-run options.

Quite a bit of code has been cribbed from the MailerContrib.
  • Loading branch information
gac410 committed Apr 29, 2017
1 parent 23a13d0 commit 14c5456
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 23 deletions.
21 changes: 20 additions & 1 deletion data/System/NotificationPlugin.txt
@@ -1,4 +1,4 @@
%META:TOPICINFO{author="ProjectContributor" date="1493338831" format="1.1" version="1"}%
%META:TOPICINFO{author="ProjectContributor" date="1493436293" format="1.1" version="1"}%
---+!! <nop>%TOPIC%
<!--
* Set SHORTDESCRIPTION = %$SHORTDESCRIPTION%
Expand Down Expand Up @@ -100,6 +100,25 @@ generated by the =%<nop>NTF{popup="on"}%= macro is pressed.
%ENDSECTION{"notification"}%
</verbatim>

---++ Running "Normal" notifications.

<div class="foswikiHelp">%T% Note that normal notifications are disabled in
bin/configure by default.</div>

Normal (batch) notifications are sent by running a REST handler
=NotificationPlugin/mailnotify=. As the command writes to the logs, be sure
to run it with the web server userid, to prevent file ownership changes.

| *Option* | *Description* |
| =-q=1= | Quiet mode. No output is printed. Basic information logged to the Foswiki debug log. |
| =-n=1= | Dry-run. No email will be sent. The run timestamps are not changed. |

<form name="mailnotify" action="%SCRIPTURLPATH{rest}%/NotificationPlugin/mailnotify" method="post">
<input type="checkbox" name="q" /> Quiet mode, no results printed.<br/>
<input type="checkbox" name="n" checked /> Dry run - no email sent<br/>
<input type="submit" class="foswikiSubmit" value="Run mailnotify" />
</form>

---++ Plugin Installation Instructions
%$INSTALL_INSTRUCTIONS%
Some unusual files are installed that are worthy of mention:
Expand Down
100 changes: 78 additions & 22 deletions lib/Foswiki/Plugins/NotificationPlugin.pm
Expand Up @@ -570,21 +570,42 @@ sub mailnotify_Handler {

my ( $session, $subject, $verb, $response ) = @_;

if ( !Foswiki::Func::isAnAdmin() ) {
$response->header( -status => 403, -type => 'text/plain' );
$response->print("Only administrators can do that");
return;
}

# Don't use the $response; we want to see things happening
local $| = 1; # autoflush on
require CGI;
print CGI::header(
-status => 200,
-type => 'text/plain',
-charset => $Foswiki::cfg{Site}{CharSet},
);

my $web = $session->{webName};
my $topic = $session->{topicName};

my $query = Foswiki::Func::getCgiQuery();
my $wikiName = Foswiki::Func::getWikiName();
my $scriptUrlPath = Foswiki::Func::getScriptUrlPath( $web, $topic, 'view' );

my $quiet = $query->param('q');
my $quiet = $query->param('q');
my $dryrun = $query->param('n');

$debug = '0' if $quiet;
my $report = ($quiet) ? 0 : 1;

&Foswiki::Func::writeDebug("START REGULAR NOTIFICATIONS");
&Foswiki::Func::writeDebug("===========================");
$debug && print "Foswiki mail notification\n";
$debug && print "- to suppress all normal output: mailnotify -q\n";
$report && print "Foswiki mail notification\n";
$report
&& print
"- to suppress all normal output: ./rest /NotificationPlugin/mailnotify -q=1\n";
$report
&& print
"- to run without modifying files or sending emails: ./rest /NotificationPlugin/mailnotify -n=1\n";
my @users = getUsers();

my %notify;
Expand Down Expand Up @@ -612,16 +633,33 @@ sub mailnotify_Handler {
#print STDERR Data::Dumper::Dumper( \%notify );

my @allChanges;
my %lastmodify;

# Build a list of unique topics that have been changed.
foreach my $web ( Foswiki::Func::getListOfWebs('user') ) {

$lastmodify{$web} = 0;
# Find the last notification time
my $metadir = Foswiki::Func::getWorkArea('NotificationPlugin');
my $notmeta = $web;
$notmeta =~ s#/#.#g;
$notmeta = "$metadir/$notmeta";

my $timeOfLastNotify = 0;
if ( open( F, '<', $notmeta ) ) {
local $/ = undef;
$timeOfLastNotify = <F>;
close(F);
}

if ($report) {
_UTF8print( "\tLast notification for $web was at "
. Foswiki::Time::formatTime( $timeOfLastNotify, 'iso' )
. "\n" );
}

my $currmodify = 0;
my %exclude;

my $it = Foswiki::Func::eachChangeSince( $web, $lastmodify{$web} + 1 );
my $it = Foswiki::Func::eachChangeSince( $web, $timeOfLastNotify + 1 );
while ( $it->hasNext() ) {
my $change = $it->next();
next if $change->{minor};
Expand All @@ -638,7 +676,17 @@ sub mailnotify_Handler {
}

# save date of the last modification
#&Foswiki::Store::saveFile( "$dataDir/$web/.mailnotify", $currmodify );
if ( $currmodify > 0 ) {
if ( !$dryrun && open( F, '>', $notmeta ) ) {
print F $currmodify;
close(F);
}
if ($report) {
_UTF8print( "\tMost recent modification time for $web: "
. Foswiki::Time::formatTime( $currmodify, 'iso' )
. "\n" );
}
}
}

#print STDERR Data::Dumper::Dumper( \@allChanges );
Expand Down Expand Up @@ -741,8 +789,6 @@ sub mailnotify_Handler {
#print STDERR "CHANGES: $head\n";
$newText =~ s/%TEXTHEAD%/$head/g;
$htmltopiclist .= $newText;

#print STDERR "===============================\n$newText\n=====================\n";
$count++;
}
}
Expand Down Expand Up @@ -774,8 +820,6 @@ sub mailnotify_Handler {
$htmlEmailBody =~ s/%TOPICLIST%/$htmltopiclist/goi;
$htmlEmailBody =~ s/%REGEXLIST%/$htmlregexlist/goi;

#print "HTML EMAIL BODY = \n==================================\n$htmlEmailBody\n===============================\n";

Foswiki::Func::readTemplate( "mailnotify", $skin );

my $email =
Expand Down Expand Up @@ -808,25 +852,37 @@ sub mailnotify_Handler {
$email =~ s/(action=\")$scriptUrlPath/$1..\/../goi;
$email =~ s|( ?) *</*nop/*>\n?|$1|gois;

$debug && print "- Sending mail notification to $user\n";
$report && print "- Sending mail notification to $user\n";
&Foswiki::Func::writeDebug("MAIL SENT TO $user ...");

#print STDERR "=============\n($email)\n===========\n";
#
#$email = Foswiki::encode_utf8( $email );

my $error = &Foswiki::Func::sendEmail($email);
if ($error) {
&Foswiki::Func::writeDebug("ERROR IN SENDING MAIL - $error");
print STDERR "* $error\n";
if ($dryrun) {
_UTF8print("Sending email to $user: <$mail>");
}
else {
my $error = &Foswiki::Func::sendEmail($email);
if ($error) {
&Foswiki::Func::writeDebug(
"ERROR IN SENDING MAIL - $error");
print STDERR "* $error\n";
}
}
}
}

&Foswiki::Func::writeDebug("FINISH REGULAR NOTIFICATIONS");
&Foswiki::Func::writeDebug("============================");
$debug && print "End Foswiki mail notification\n";
$report && print "End Foswiki mail notification\n";

return undef;
}

sub _UTF8print {
if ($Foswiki::UNICODE) {
print Foswiki::encode_utf8( $_[0] );
}
else {
print $_[0];
}
}

1;
Expand Down

0 comments on commit 14c5456

Please sign in to comment.