]> sipb.mit.edu Git - ikiwiki.git/commitdiff
finish notifyemail plugin
authorJoey Hess <joey@kitenet.net>
Wed, 28 Mar 2012 22:52:11 +0000 (18:52 -0400)
committerJoey Hess <joey@kitenet.net>
Wed, 28 Mar 2012 22:52:11 +0000 (18:52 -0400)
IkiWiki/Plugin/notifyemail.pm [moved from IkiWiki/Plugin/changemail.pm with 62% similarity]
doc/plugins/notifyemail.mdwn [moved from doc/plugins/changeemail.mdwn with 73% similarity]
templates/notifyemail.tmpl [moved from templates/changeemail.tmpl with 76% similarity]

similarity index 62%
rename from IkiWiki/Plugin/changemail.pm
rename to IkiWiki/Plugin/notifyemail.pm
index c112502a4f3c632adac01576ed7ce44b810e7ed9..192e8d7a30d7ad837525f716edd03284c31cec58 100644 (file)
@@ -1,15 +1,15 @@
 #!/usr/bin/perl
-package IkiWiki::Plugin::changemail;
+package IkiWiki::Plugin::notifyemail;
 
 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 => \&notify);
+       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 => \&notify);
 }
 
 sub getsetup () {
@@ -27,13 +27,10 @@ sub formbuilder_setup (@) {
        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,
+       $form->field(name => "subscriptions", size => 50,
                fieldset => "preferences",
                comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")",
-               value => IkiWiki::userinfo_get($user_name, "subscriptions"));
+               value => getsubscriptions($session->param("name")));
 }
 
 sub formbuilder (@) {
@@ -45,6 +42,13 @@ sub formbuilder (@) {
        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;
@@ -53,6 +57,14 @@ sub setsubscriptions ($$) {
        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;
@@ -63,30 +75,37 @@ sub notify (@) {
        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 $ispage=defined pagetype($file);
+                       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);
@@ -98,17 +117,17 @@ sub notify (@) {
                        else {
                                $url=$config{wikiurl}; # crummy fallback url
                        }
-                       my $template=template("changemail.tmpl");
+                       my $template=template("notifyemail.tmpl");
                        $template->param(
                                wikiname => $config{wikiname},
                                url => $url,
                                prefsurl => IkiWiki::cgiurl(do => "prefs"),
-                               ispage => $ispage,
-                               content => $ispage ? readfile(srcfile($file)) : "",
+                               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 a commit email.
+                       #translators: This is used as the subject of an email.
                        my $subject=sprintf(gettext("%s: change notification for %s"),
                                $config{wikiname}, $page);
                        sendmail(
@@ -120,7 +139,7 @@ sub notify (@) {
                }
        }
 
-       exit 0; # daemon child
+       #exit 0; # daemon child
 }
 
 1
similarity index 73%
rename from doc/plugins/changeemail.mdwn
rename to doc/plugins/notifyemail.mdwn
index f37d0f5adf49283193aa9997e7bea5ebf834a937..88e88e106fe89b1269f3f6effc9f4bec74b9b19c 100644 (file)
@@ -1,10 +1,12 @@
-This plugin allows emailing users when pages are created or changed. 
+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 pagespec will send an email that includes the new content of
+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,
similarity index 76%
rename from templates/changeemail.tmpl
rename to templates/notifyemail.tmpl
index c73bf92d3a7f244f8601203003d5e835ea66b207..88972c36c75aab8c24222b83a040581aee4bce91 100644 (file)
@@ -2,8 +2,8 @@ A change has been made to <TMPL_VAR URL>
 
 To stop these notifications, visit <TMPL_VAR PREFSURL>
 
-<TMPL_IF ISPAGE>
+<TMPL_IF NAME=SHOWCONTENT>
 ----
 
 <TMPL_VAR CONTENT>
-<TMPL_IF>
+</TMPL_IF>