X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/a22be4eef0781548158fdb22bcbfe23ab6581ff6..c037714b77535c849c8d8ef4807af2b59737a5c5:/IkiWiki/Plugin/notifyemail.pm diff --git a/IkiWiki/Plugin/notifyemail.pm b/IkiWiki/Plugin/notifyemail.pm index 192e8d7a3..b50a22a00 100644 --- a/IkiWiki/Plugin/notifyemail.pm +++ b/IkiWiki/Plugin/notifyemail.pm @@ -6,7 +6,6 @@ use strict; use IkiWiki 3.00; sub import { - hook(type => "formbuilder_setup", id => "notifyemail", call => \&formbuilder_setup); hook(type => "formbuilder", id => "notifyemail", call => \&formbuilder); hook(type => "getsetup", id => "notifyemail", call => \&getsetup); hook(type => "changes", id => "notifyemail", call => \¬ify); @@ -17,29 +16,26 @@ sub getsetup () { plugin => { safe => 1, rebuild => 0, - section => "misc", }, } -sub formbuilder_setup (@) { +sub formbuilder (@) { my %params=@_; - my $form=$params{form}; return unless $form->title eq "preferences"; my $session=$params{session}; + my $username=$session->param("name"); $form->field(name => "subscriptions", size => 50, fieldset => "preferences", - comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")", - value => getsubscriptions($session->param("name"))); -} - -sub formbuilder (@) { - my %params=@_; - my $form=$params{form}; - return unless $form->title eq "preferences" && - $form->submitted eq "Save Preferences" && $form->validate && - defined $form->field("subscriptions"); - setsubscriptions($form->field('name'), $form->field('subscriptions')); + comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")"); + if (! $form->submitted) { + $form->field(name => "subscriptions", force => 1, + value => getsubscriptions($username)); + } + elsif ($form->submitted eq "Save Preferences" && $form->validate && + defined $form->field("subscriptions")) { + setsubscriptions($username, $form->field('subscriptions')); + } } sub getsubscriptions ($) { @@ -62,43 +58,58 @@ sub subscribe ($$) { my $user=shift; my $addpagespec=shift; my $pagespec=getsubscriptions($user); - setsubscriptions($user, $pagespec." or ".$addpagespec); + setsubscriptions($user, + length $pagespec ? $pagespec." or ".$addpagespec : $addpagespec); +} + +# 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); + } } sub notify (@) { my @files=@_; return unless @files; + return if $config{rebuild}; eval q{use Mail::Sendmail}; error $@ if $@; eval q{use IkiWiki::UserInfo}; error $@ if $@; + eval q{use URI}; + error($@) if $@; # 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: $!"); + 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; + 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; - print "!!$user\n"; foreach my $file (@files) { my $page=pagename($file); - print "file: $file ($page)\n"; next unless pagespec_match($page, $pagespec); my $content=""; my $showcontent=defined pagetype($file); @@ -112,24 +123,37 @@ sub notify (@) { } elsif (defined $pagestate{$page}{meta}{permalink}) { # need to use permalink for an internal page - $url=$pagestate{$page}{meta}{permalink}; + $url=URI->new_abs($pagestate{$page}{meta}{permalink}, $config{url}); } else { - $url=$config{wikiurl}; # crummy fallback url + $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; + } + 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; } my $template=template("notifyemail.tmpl"); $template->param( wikiname => $config{wikiname}, url => $url, - prefsurl => IkiWiki::cgiurl(do => "prefs"), + prefsurl => $prefsurl, showcontent => $showcontent, content => $content, ); - #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 an email. - my $subject=sprintf(gettext("%s: change notification for %s"), - $config{wikiname}, $page); sendmail( To => $email, From => "$config{wikiname} <$config{adminemail}>", @@ -139,7 +163,7 @@ sub notify (@) { } } - #exit 0; # daemon child + exit 0; # daemon child } 1