summaryrefslogtreecommitdiff
path: root/IkiWiki/UserInfo.pm
blob: d32f5c1bf9436fb30cfb93992723a554f3be8692 (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($_, $userinfo->{$user}->{subscriptions}, "") }
  90. map pagename($_), @_) {
  91. push @ret, $userinfo->{$user}->{email};
  92. }
  93. }
  94. return @ret;
  95. } #}}}
  96. sub send_commit_mails ($$$@) { #{{{
  97. my $messagesub=shift;
  98. my $diffsub=shift;
  99. my $user=shift;
  100. my @changed_pages=@_;
  101. return unless @changed_pages;
  102. my @email_recipients=commit_notify_list($user, @changed_pages);
  103. if (@email_recipients) {
  104. # TODO: if a commit spans multiple pages, this will send
  105. # subscribers a diff that might contain pages they did not
  106. # sign up for. Should separate the diff per page and
  107. # reassemble into one mail with just the pages subscribed to.
  108. my $diff=$diffsub->();
  109. my $message=$messagesub->();
  110. my $pagelist;
  111. if (@changed_pages > 2) {
  112. $pagelist="$changed_pages[0] $changed_pages[1] ...";
  113. }
  114. else {
  115. $pagelist.=join(" ", @changed_pages);
  116. }
  117. #translators: The three variables are the name of the wiki,
  118. #translators: A list of one or more pages that were changed,
  119. #translators: And the name of the user making the change.
  120. #translators: This is used as the subject of a commit email.
  121. my $subject=sprintf(gettext("update of %s's %s by %s"),
  122. $config{wikiname}, $pagelist, $user);
  123. my $template=template("notifymail.tmpl");
  124. $template->param(
  125. wikiname => $config{wikiname},
  126. diff => $diff,
  127. user => $user,
  128. message => $message,
  129. );
  130. # Daemonize, in case the mail sending takes a while.
  131. defined(my $pid = fork) or error("Can't fork: $!");
  132. return if $pid;
  133. setsid() or error("Can't start a new session: $!");
  134. chdir '/';
  135. open STDIN, '/dev/null';
  136. open STDOUT, '>/dev/null';
  137. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  138. unlockwiki(); # don't need to keep a lock on the wiki
  139. eval q{use Mail::Sendmail};
  140. error($@) if $@;
  141. foreach my $email (@email_recipients) {
  142. sendmail(
  143. To => $email,
  144. From => "$config{wikiname} <$config{adminemail}>",
  145. Subject => $subject,
  146. Message => $template->output,
  147. );
  148. }
  149. exit 0; # daemon process done
  150. }
  151. } #}}}
  152. 1