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