* Add user(name) to the PageSpec for commit subscriptions.
[ikiwiki] / IkiWiki / UserInfo.pm
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use Storable;
6 use IkiWiki;
7
8 package IkiWiki;
9
10 sub userinfo_retrieve () { #{{{
11         my $userinfo=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
12         return $userinfo;
13 } #}}}
14         
15 sub userinfo_store ($) { #{{{
16         my $userinfo=shift;
17         
18         my $newfile="$config{wikistatedir}/userdb.new";
19         my $oldmask=umask(077);
20         my $ret=Storable::lock_store($userinfo, $newfile);
21         umask($oldmask);
22         if (defined $ret && $ret) {
23                 if (! rename($newfile, "$config{wikistatedir}/userdb")) {
24                         unlink($newfile);
25                         $ret=undef;
26                 }
27         }
28         return $ret;
29 } #}}}
30
31 sub userinfo_get ($$) { #{{{
32         my $user=shift;
33         my $field=shift;
34
35         my $userinfo=userinfo_retrieve();
36         if (! defined $userinfo ||
37             ! exists $userinfo->{$user} || ! ref $userinfo->{$user} ||
38             ! exists $userinfo->{$user}->{$field}) {
39                 return "";
40         }
41         return $userinfo->{$user}->{$field};
42 } #}}}
43
44 sub userinfo_set ($$$) { #{{{
45         my $user=shift;
46         my $field=shift;
47         my $value=shift;
48         
49         my $userinfo=userinfo_retrieve();
50         if (! defined $userinfo ||
51             ! exists $userinfo->{$user} || ! ref $userinfo->{$user}) {
52                 return "";
53         }
54         
55         $userinfo->{$user}->{$field}=$value;
56         return userinfo_store($userinfo);
57 } #}}}
58
59 sub userinfo_setall ($$) { #{{{
60         my $user=shift;
61         my $info=shift;
62         
63         my $userinfo=userinfo_retrieve();
64         if (! defined $userinfo) {
65                 $userinfo={};
66         }
67         $userinfo->{$user}=$info;
68         return userinfo_store($userinfo);
69 } #}}}
70
71 sub is_admin ($) { #{{{
72         my $user_name=shift;
73
74         return grep { $_ eq $user_name } @{$config{adminuser}};
75 } #}}}
76
77 sub get_banned_users () { #{{{
78         my @ret;
79         my $userinfo=userinfo_retrieve();
80         foreach my $user (keys %{$userinfo}) {
81                 push @ret, $user if $userinfo->{$user}->{banned};
82         }
83         return @ret;
84 } #}}}
85
86 sub set_banned_users (@) { #{{{
87         my %banned=map { $_ => 1 } @_;
88         my $userinfo=userinfo_retrieve();
89         foreach my $user (keys %{$userinfo}) {
90                 $userinfo->{$user}->{banned} = $banned{$user};
91         }
92         return userinfo_store($userinfo);
93 } #}}}
94
95 # Global used to pass information into the PageSpec function.
96 our $committer;
97
98 sub commit_notify_list ($@) { #{{{
99         $committer=shift;
100         my @pages = map pagename($_), @_;
101
102         my @ret;
103         my $userinfo=userinfo_retrieve();
104         foreach my $user (keys %{$userinfo}) {
105                 next if $user eq $committer;
106                 if (exists $userinfo->{$user}->{subscriptions} &&
107                     length $userinfo->{$user}->{subscriptions} &&
108                     exists $userinfo->{$user}->{email} &&
109                     length $userinfo->{$user}->{email} &&
110                     grep { pagespec_match($_, $userinfo->{$user}->{subscriptions}, "") }
111                         map pagename($_), @_) {
112                         push @ret, $userinfo->{$user}->{email};
113                 }
114         }
115         return @ret;
116 } #}}}
117
118 sub send_commit_mails ($$$@) { #{{{
119         my $messagesub=shift;
120         my $diffsub=shift;
121         my $user=shift;
122         my @changed_pages=@_;
123
124         return unless @changed_pages;
125
126         my @email_recipients=commit_notify_list($user, @changed_pages);
127         if (@email_recipients) {
128                 # TODO: if a commit spans multiple pages, this will send
129                 # subscribers a diff that might contain pages they did not
130                 # sign up for. Should separate the diff per page and
131                 # reassemble into one mail with just the pages subscribed to.
132                 my $diff=$diffsub->();
133                 my $message=$messagesub->();
134
135                 my $pagelist;
136                 if (@changed_pages > 2) {
137                         $pagelist="$changed_pages[0] $changed_pages[1] ...";
138                 }
139                 else {
140                         $pagelist.=join(" ", @changed_pages);
141                 }
142                 #translators: The three variables are the name of the wiki,
143                 #translators: A list of one or more pages that were changed,
144                 #translators: And the name of the user making the change.
145                 #translators: This is used as the subject of a commit email.
146                 my $subject=sprintf(gettext("update of %s's %s by %s"), 
147                         $config{wikiname}, $pagelist, $user);
148
149                 my $template=template("notifymail.tmpl");
150                 $template->param(
151                         wikiname => $config{wikiname},
152                         diff => $diff,
153                         user => $user,
154                         message => $message,
155                 );
156
157                 # Daemonize, in case the mail sending takes a while.
158                 defined(my $pid = fork) or error("Can't fork: $!");
159                 return if $pid;
160                 setsid() or error("Can't start a new session: $!");
161                 chdir '/';
162                 open STDIN, '/dev/null';
163                 open STDOUT, '>/dev/null';
164                 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
165
166                 unlockwiki(); # don't need to keep a lock on the wiki
167
168                 eval q{use Mail::Sendmail};
169                 error($@) if $@;
170                 foreach my $email (@email_recipients) {
171                         sendmail(
172                                 To => $email,
173                                 From => "$config{wikiname} <$config{adminemail}>",
174                                 Subject => $subject,
175                                 Message => $template->output,
176                         );
177                 }
178
179                 exit 0; # daemon process done
180         }
181 } #}}}
182
183 package IkiWiki::PageSpec;
184
185 sub match_user ($$$) { #{{{
186         shift;
187         my $user=shift;
188
189         return $user eq $committer;
190 } #}}}
191
192 1