aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/notifyemail.pm
blob: 2c1775f2e81df281a7c7769220acc26ade476cdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/perl
package IkiWiki::Plugin::notifyemail;

use warnings;
use strict;
use IkiWiki 3.00;

sub import {
	hook(type => "formbuilder", id => "notifyemail", call => \&formbuilder);
	hook(type => "getsetup", id => "notifyemail",  call => \&getsetup);
	hook(type => "changes", id => "notifyemail", call => \&notify);
}

sub getsetup () {
	return
		plugin => {
			safe => 1,
			rebuild => 0,
		},
}

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).")");
	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 ($) {
	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,
		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;

	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: $!");

	# 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 $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=URI->new_abs($pagestate{$page}{meta}{permalink}, $config{url});
			}
			else {
				$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 => $prefsurl,
				showcontent => $showcontent,
				content => $content,
			);
			sendmail(
				To => $email,
				From => "$config{wikiname} <$config{adminemail}>",
				Subject => $subject,
				Message => $template->output,
			);
		}
	}

	exit 0; # daemon child
}

1