diff options
author | Joey Hess <joey@kitenet.net> | 2012-03-28 16:40:10 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2012-03-28 16:40:10 -0400 |
commit | 723c5b97d37125a4020670807fc1569918d37276 (patch) | |
tree | 00e5b070b07aa7c9d4b6000c8fae211d0c004441 /IkiWiki | |
parent | 84cd40d1792f83a625b0292d889c6698c6be8dad (diff) | |
download | ikiwiki-723c5b97d37125a4020670807fc1569918d37276.tar ikiwiki-723c5b97d37125a4020670807fc1569918d37276.tar.gz |
changemail: New plugin, sends emails about changed pages.
Diffstat (limited to 'IkiWiki')
-rw-r--r-- | IkiWiki/Plugin/changemail.pm | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/IkiWiki/Plugin/changemail.pm b/IkiWiki/Plugin/changemail.pm new file mode 100644 index 000000000..d0b7b8dbe --- /dev/null +++ b/IkiWiki/Plugin/changemail.pm @@ -0,0 +1,91 @@ +#!/usr/bin/perl +package IkiWiki::Plugin::changemail; + +use warnings; +use strict; +use IkiWiki 3.00; + +sub import { + hook(type => "getsetup", id => "changemail", call => \&getsetup); + hook(type => "change", id => "changemail", call => \¬ify); +} + +sub getsetup () { + return + plugin => { + safe => 1, + rebuild => 0, + section => "misc", + }, +} + +sub notify (@) { + my @files=@_; + return unless @files; + + eval q{use Mail::Sendmail}; + error $@ if $@; + eval q{use IkiWiki::UserInfo}; + + # Daemonize, in case the mail sending takes a while. + 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: $!"); + + # Don't need to keep a lock on the wiki as a daemon. + IkiWiki::unlockwiki(); + + my $userinfo=IkiWiki::userinfo_retrieve(); + exit 0 unless defined $userinfo; + + 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); + my $ispage=defined pagetype($file); + 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 + $url=$pagestate{$page}{meta}{permalink}; + } + else { + $url=$config{wikiurl}; # crummy fallback url + } + my $template=template("changemail.tmpl"); + $template->param( + wikiname => $config{wikiname}, + url => $url, + prefsurl => IkiWiki::cgiurl(do => "prefs"), + ispage => $ispage, + content => $ispage ? readfile(srcfile($file)) : "", + ); + #translators: The two variables are the name of the wiki, + #translators: and a page that was changed. + #translators: This is used as the subject of a commit email. + my $subject=sprintf(gettext("%s: change notification for %s"), + $config{wikiname}, $page); + sendmail( + To => $email, + From => "$config{wikiname} <$config{adminemail}>", + Subject => $subject, + Message => $template->output, + ); + } + } + + exit 0; # daemon child +} + +1 |