2012-03-28 22:40:10 +02:00
|
|
|
#!/usr/bin/perl
|
2012-03-29 00:52:11 +02:00
|
|
|
package IkiWiki::Plugin::notifyemail;
|
2012-03-28 22:40:10 +02:00
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use IkiWiki 3.00;
|
|
|
|
|
|
|
|
sub import {
|
2012-03-29 00:52:11 +02:00
|
|
|
hook(type => "formbuilder", id => "notifyemail", call => \&formbuilder);
|
|
|
|
hook(type => "getsetup", id => "notifyemail", call => \&getsetup);
|
|
|
|
hook(type => "changes", id => "notifyemail", call => \¬ify);
|
2012-03-28 22:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub getsetup () {
|
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2012-03-29 02:56:22 +02:00
|
|
|
sub formbuilder (@) {
|
2012-03-28 23:00:54 +02:00
|
|
|
my %params=@_;
|
|
|
|
my $form=$params{form};
|
|
|
|
return unless $form->title eq "preferences";
|
|
|
|
my $session=$params{session};
|
2012-03-29 02:56:22 +02:00
|
|
|
my $username=$session->param("name");
|
2012-03-29 00:52:11 +02:00
|
|
|
$form->field(name => "subscriptions", size => 50,
|
2012-03-28 23:00:54 +02:00
|
|
|
fieldset => "preferences",
|
2012-03-29 02:45:52 +02:00
|
|
|
comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")");
|
|
|
|
if (! $form->submitted) {
|
|
|
|
$form->field(name => "subscriptions", force => 1,
|
2012-03-29 02:56:22 +02:00
|
|
|
value => getsubscriptions($username));
|
|
|
|
}
|
|
|
|
elsif ($form->submitted eq "Save Preferences" && $form->validate &&
|
|
|
|
defined $form->field("subscriptions")) {
|
2016-12-24 16:03:51 +01:00
|
|
|
setsubscriptions($username, scalar $form->field('subscriptions'));
|
2012-03-29 02:45:52 +02:00
|
|
|
}
|
2012-03-28 23:00:54 +02:00
|
|
|
}
|
|
|
|
|
2012-03-29 00:52:11 +02:00
|
|
|
sub getsubscriptions ($) {
|
|
|
|
my $user=shift;
|
|
|
|
eval q{use IkiWiki::UserInfo};
|
|
|
|
error $@ if $@;
|
|
|
|
IkiWiki::userinfo_get($user, "subscriptions");
|
|
|
|
}
|
|
|
|
|
2012-03-28 23:00:54 +02:00
|
|
|
sub setsubscriptions ($$) {
|
|
|
|
my $user=shift;
|
|
|
|
my $subscriptions=shift;
|
|
|
|
eval q{use IkiWiki::UserInfo};
|
|
|
|
error $@ if $@;
|
|
|
|
IkiWiki::userinfo_set($user, "subscriptions", $subscriptions);
|
|
|
|
}
|
|
|
|
|
2012-03-29 00:52:11 +02:00
|
|
|
# Called by other plugins to subscribe the user to a pagespec.
|
|
|
|
sub subscribe ($$) {
|
|
|
|
my $user=shift;
|
|
|
|
my $addpagespec=shift;
|
|
|
|
my $pagespec=getsubscriptions($user);
|
2012-03-29 02:42:37 +02:00
|
|
|
setsubscriptions($user,
|
|
|
|
length $pagespec ? $pagespec." or ".$addpagespec : $addpagespec);
|
2012-03-29 00:52:11 +02:00
|
|
|
}
|
|
|
|
|
2012-04-02 19:39:29 +02:00
|
|
|
# Called by other plugins to subscribe an email to a pagespec.
|
|
|
|
sub anonsubscribe ($$) {
|
|
|
|
my $email=shift;
|
|
|
|
my $addpagespec=shift;
|
|
|
|
if (IkiWiki::Plugin::passwordauth->can("anonuser")) {
|
|
|
|
my $user=IkiWiki::Plugin::passwordauth::anonuser($email);
|
|
|
|
if (! defined $user) {
|
|
|
|
error(gettext("Cannot subscribe your email address without logging in."));
|
|
|
|
}
|
|
|
|
subscribe($user, $addpagespec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-28 22:40:10 +02:00
|
|
|
sub notify (@) {
|
|
|
|
my @files=@_;
|
|
|
|
return unless @files;
|
2013-05-18 22:26:48 +02:00
|
|
|
return if $config{rebuild};
|
2012-03-28 22:40:10 +02:00
|
|
|
|
|
|
|
eval q{use Mail::Sendmail};
|
|
|
|
error $@ if $@;
|
|
|
|
eval q{use IkiWiki::UserInfo};
|
2012-03-28 23:00:54 +02:00
|
|
|
error $@ if $@;
|
2012-03-29 01:39:08 +02:00
|
|
|
eval q{use URI};
|
|
|
|
error($@) if $@;
|
2012-03-28 22:40:10 +02:00
|
|
|
|
|
|
|
# Daemonize, in case the mail sending takes a while.
|
2012-03-29 01:39:08 +02:00
|
|
|
defined(my $pid = fork) or error("Can't fork: $!");
|
|
|
|
return if $pid; # parent
|
|
|
|
chdir '/';
|
|
|
|
open STDIN, '/dev/null';
|
|
|
|
open STDOUT, '>/dev/null';
|
|
|
|
POSIX::setsid() or error("Can't start a new session: $!");
|
|
|
|
open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
|
2012-03-28 22:40:10 +02:00
|
|
|
|
|
|
|
# Don't need to keep a lock on the wiki as a daemon.
|
|
|
|
IkiWiki::unlockwiki();
|
|
|
|
|
|
|
|
my $userinfo=IkiWiki::userinfo_retrieve();
|
2012-03-29 01:39:08 +02:00
|
|
|
exit 0 unless defined $userinfo;
|
2012-03-28 22:40:10 +02:00
|
|
|
|
|
|
|
foreach my $user (keys %$userinfo) {
|
|
|
|
my $pagespec=$userinfo->{$user}->{"subscriptions"};
|
|
|
|
next unless defined $pagespec && length $pagespec;
|
|
|
|
my $email=$userinfo->{$user}->{email};
|
|
|
|
next unless defined $email && length $email;
|
|
|
|
|
|
|
|
foreach my $file (@files) {
|
|
|
|
my $page=pagename($file);
|
|
|
|
next unless pagespec_match($page, $pagespec);
|
2012-03-29 00:52:11 +02:00
|
|
|
my $content="";
|
|
|
|
my $showcontent=defined pagetype($file);
|
|
|
|
if ($showcontent) {
|
|
|
|
$content=eval { readfile(srcfile($file)) };
|
|
|
|
$showcontent=0 if $@;
|
|
|
|
}
|
2012-03-28 22:40:10 +02:00
|
|
|
my $url;
|
|
|
|
if (! IkiWiki::isinternal($page)) {
|
|
|
|
$url=urlto($page, undef, 1);
|
|
|
|
}
|
|
|
|
elsif (defined $pagestate{$page}{meta}{permalink}) {
|
|
|
|
# need to use permalink for an internal page
|
2012-03-29 01:39:08 +02:00
|
|
|
$url=URI->new_abs($pagestate{$page}{meta}{permalink}, $config{url});
|
2012-03-28 22:40:10 +02:00
|
|
|
}
|
|
|
|
else {
|
2012-03-29 01:39:08 +02:00
|
|
|
$url=$config{url}; # crummy fallback url
|
|
|
|
}
|
|
|
|
my $pagedesc=$page;
|
|
|
|
if (defined $pagestate{$page}{meta}{title} &&
|
|
|
|
length $pagestate{$page}{meta}{title}) {
|
|
|
|
$pagedesc=qq{"$pagestate{$page}{meta}{title}"};
|
|
|
|
}
|
|
|
|
my $subject=gettext("change notification:")." ".$pagedesc;
|
|
|
|
if (pagetype($file) eq '_comment') {
|
|
|
|
$subject=gettext("comment notification:")." ".$pagedesc;
|
2012-03-28 22:40:10 +02:00
|
|
|
}
|
2012-04-02 19:39:29 +02:00
|
|
|
my $prefsurl=IkiWiki::cgiurl_abs(do => 'prefs');
|
|
|
|
if (IkiWiki::Plugin::passwordauth->can("anonusertoken")) {
|
|
|
|
my $token=IkiWiki::Plugin::passwordauth::anonusertoken($userinfo->{$user});
|
|
|
|
$prefsurl=IkiWiki::cgiurl_abs(
|
|
|
|
do => 'tokenauth',
|
|
|
|
name => $user,
|
|
|
|
token => $token,
|
|
|
|
) if defined $token;
|
|
|
|
}
|
2012-03-29 00:52:11 +02:00
|
|
|
my $template=template("notifyemail.tmpl");
|
2012-03-28 22:40:10 +02:00
|
|
|
$template->param(
|
|
|
|
wikiname => $config{wikiname},
|
|
|
|
url => $url,
|
2012-04-02 19:39:29 +02:00
|
|
|
prefsurl => $prefsurl,
|
2012-03-29 00:52:11 +02:00
|
|
|
showcontent => $showcontent,
|
|
|
|
content => $content,
|
2012-03-28 22:40:10 +02:00
|
|
|
);
|
|
|
|
sendmail(
|
|
|
|
To => $email,
|
|
|
|
From => "$config{wikiname} <$config{adminemail}>",
|
|
|
|
Subject => $subject,
|
|
|
|
Message => $template->output,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-29 01:39:08 +02:00
|
|
|
exit 0; # daemon child
|
2012-03-28 22:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1
|