From a22be4eef0781548158fdb22bcbfe23ab6581ff6 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 28 Mar 2012 18:52:11 -0400 Subject: finish notifyemail plugin --- IkiWiki/Plugin/changemail.pm | 126 ------------------------------------ IkiWiki/Plugin/notifyemail.pm | 145 ++++++++++++++++++++++++++++++++++++++++++ doc/plugins/changeemail.mdwn | 12 ---- doc/plugins/notifyemail.mdwn | 14 ++++ templates/changeemail.tmpl | 9 --- templates/notifyemail.tmpl | 9 +++ 6 files changed, 168 insertions(+), 147 deletions(-) delete mode 100644 IkiWiki/Plugin/changemail.pm create mode 100644 IkiWiki/Plugin/notifyemail.pm delete mode 100644 doc/plugins/changeemail.mdwn create mode 100644 doc/plugins/notifyemail.mdwn delete mode 100644 templates/changeemail.tmpl create mode 100644 templates/notifyemail.tmpl diff --git a/IkiWiki/Plugin/changemail.pm b/IkiWiki/Plugin/changemail.pm deleted file mode 100644 index c112502a4..000000000 --- a/IkiWiki/Plugin/changemail.pm +++ /dev/null @@ -1,126 +0,0 @@ -#!/usr/bin/perl -package IkiWiki::Plugin::changemail; - -use warnings; -use strict; -use IkiWiki 3.00; - -sub import { - hook(type => "formbuilder_setup", id => "changemail", call => \&formbuilder_setup); - hook(type => "formbuilder", id => "changemail", call => \&formbuilder); - 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 formbuilder_setup (@) { - my %params=@_; - - my $form=$params{form}; - return unless $form->title eq "preferences"; - my $session=$params{session}; - my $user_name=$session->param("name"); - eval q{use IkiWiki::UserInfo}; - error $@ if $@; - $form->field(name => "subscriptions", force => 1, size => 50, - fieldset => "preferences", - comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")", - value => IkiWiki::userinfo_get($user_name, "subscriptions")); -} - -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')); -} - -sub setsubscriptions ($$) { - my $user=shift; - my $subscriptions=shift; - eval q{use IkiWiki::UserInfo}; - error $@ if $@; - IkiWiki::userinfo_set($user, "subscriptions", $subscriptions); -} - -sub notify (@) { - my @files=@_; - return unless @files; - - eval q{use Mail::Sendmail}; - error $@ if $@; - eval q{use IkiWiki::UserInfo}; - 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: $!"); - - # 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 diff --git a/IkiWiki/Plugin/notifyemail.pm b/IkiWiki/Plugin/notifyemail.pm new file mode 100644 index 000000000..192e8d7a3 --- /dev/null +++ b/IkiWiki/Plugin/notifyemail.pm @@ -0,0 +1,145 @@ +#!/usr/bin/perl +package IkiWiki::Plugin::notifyemail; + +use warnings; +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); +} + +sub getsetup () { + return + plugin => { + safe => 1, + rebuild => 0, + section => "misc", + }, +} + +sub formbuilder_setup (@) { + my %params=@_; + + my $form=$params{form}; + return unless $form->title eq "preferences"; + my $session=$params{session}; + $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')); +} + +sub getsubscriptions ($) { + my $user=shift; + eval q{use IkiWiki::UserInfo}; + error $@ if $@; + IkiWiki::userinfo_get($user, "subscriptions"); +} + +sub setsubscriptions ($$) { + my $user=shift; + my $subscriptions=shift; + eval q{use IkiWiki::UserInfo}; + error $@ if $@; + IkiWiki::userinfo_set($user, "subscriptions", $subscriptions); +} + +# Called by other plugins to subscribe the user to a pagespec. +sub subscribe ($$) { + my $user=shift; + my $addpagespec=shift; + my $pagespec=getsubscriptions($user); + setsubscriptions($user, $pagespec." or ".$addpagespec); +} + +sub notify (@) { + my @files=@_; + return unless @files; + + eval q{use Mail::Sendmail}; + error $@ if $@; + eval q{use IkiWiki::UserInfo}; + 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: $!"); + + # 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; + 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); + if ($showcontent) { + $content=eval { readfile(srcfile($file)) }; + $showcontent=0 if $@; + } + 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("notifyemail.tmpl"); + $template->param( + wikiname => $config{wikiname}, + url => $url, + prefsurl => IkiWiki::cgiurl(do => "prefs"), + 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}>", + Subject => $subject, + Message => $template->output, + ); + } + } + + #exit 0; # daemon child +} + +1 diff --git a/doc/plugins/changeemail.mdwn b/doc/plugins/changeemail.mdwn deleted file mode 100644 index f37d0f5ad..000000000 --- a/doc/plugins/changeemail.mdwn +++ /dev/null @@ -1,12 +0,0 @@ -This plugin allows emailing users when pages are created or changed. -It needs the [[!cpan Mail::SendMail]] perl module, and sends mail -using the local MTA. - -Each user can configure which pages they are interested in, using an -[[ikiwiki/PageSpec]] on their Preferences page. Any change to a page -matching the pagespec will send an email that includes the new content of -the page, and a link to the page on the web. - -To make it easy to subscribe to comment threads when posting a comment, -there is a check box that can be used to subscribe, without needing to -manually edit the [[ikiwiki/PageSpec]]. diff --git a/doc/plugins/notifyemail.mdwn b/doc/plugins/notifyemail.mdwn new file mode 100644 index 000000000..88e88e106 --- /dev/null +++ b/doc/plugins/notifyemail.mdwn @@ -0,0 +1,14 @@ +This plugin allows uses to subscribe to pages, and emails them when +they pages are created or changed. + +It needs the [[!cpan Mail::SendMail]] perl module, and sends mail +using the local MTA. + +Each user can configure which pages they are interested in, using an +[[ikiwiki/PageSpec]] on their Preferences page. Any change to a page +matching the PagSspec will send an email that includes the new content of +the page, and a link to the page on the web. + +To make it easy to subscribe to comment threads when posting a comment, +there is a check box that can be used to subscribe, without needing to +manually edit the [[ikiwiki/PageSpec]]. diff --git a/templates/changeemail.tmpl b/templates/changeemail.tmpl deleted file mode 100644 index c73bf92d3..000000000 --- a/templates/changeemail.tmpl +++ /dev/null @@ -1,9 +0,0 @@ -A change has been made to - -To stop these notifications, visit - - ----- - - - diff --git a/templates/notifyemail.tmpl b/templates/notifyemail.tmpl new file mode 100644 index 000000000..88972c36c --- /dev/null +++ b/templates/notifyemail.tmpl @@ -0,0 +1,9 @@ +A change has been made to + +To stop these notifications, visit + + +---- + + + -- cgit v1.2.3