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